[vtkusers] visualizing ply file

surendra sah imsurendra_sah at yahoo.com
Tue Jan 8 09:49:14 EST 2019


Hi All,I have a .ply file and need to visualize it in C++. I tried paraview in order to get the output view and I could see the visualization. With VTK, I used the below code to read, write and visualize the .ply file but I do not see anything on Render Window. The window is  blank and also the output file is in different format probably binary. Please help me to get the visualization the render window. Thanks

Few lines of my .ply file 
input.ply:
ply
format ascii 1.0
element vertex 1444314
property float x
property float y
property float z
property uchar red
property uchar green
property uchar blue
property uchar alpha
end_header
9.629254 11.183217 2.379896 120 138 143 0
9.687406 11.172439 2.375881 115 133 140 0
9.896527 11.479791 1.872450 32 40 48 0
9.750688 11.167464 2.372992 122 138 145 0
9.943034 11.452575 1.865112 36 34 57 0
9.825553 11.175802 2.372624 119 134 142 0
9.907058 11.464114 1.311501 43 60 71 0



Code://vtkvisualization.cxx
 #include <vtkPolyData.h>
#include <vtkPolyData.h>
#include <vtkPLYReader.h>
#include <vtkPLYWriter.h>
#include <vtkNamedColors.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <array>

int main ( int argc, char *argv[] )
{


  vtkSmartPointer<vtkNamedColors> colors =
      vtkSmartPointer<vtkNamedColors>::New();

  // Set the background color.
  std::array<unsigned char, 4> bkg{{26, 51, 102, 255}};
  //colors->SetName("RGB");
  colors->SetColor("BkgColor", bkg.data());  


  //defining source
  if(argc != 2)
    {
    std::cout << "Usage: " << argv[0] << "  Filename(.ply)" << std::endl;
    return EXIT_FAILURE;
    }

  std::string inputFilename = argv[1];

  vtkSmartPointer<vtkPLYReader> reader =
    vtkSmartPointer<vtkPLYReader>::New();

  reader->SetFileName ( inputFilename.c_str() );
  reader->Update();

  
//writing to a file
  vtkSmartPointer<vtkPLYWriter> plyWriter =
    vtkSmartPointer<vtkPLYWriter>::New();
  plyWriter->SetFileName("output.ply");
  //plyWriter->SetArrayName("RGB");
  plyWriter->SetInputData(reader->GetOutput());
  plyWriter->Write();

  // Visualize steps:

  //defining mapper
  vtkSmartPointer<vtkPolyDataMapper> mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInputConnection(reader->GetOutputPort());
  
  //defining actor
  vtkSmartPointer<vtkActor> actor =
    vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);

  //defining renderer
  // The renderer generates the image
  // which is then displayed on the render window.
  // It can be thought of as a scene to which the actor is added    
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  renderer->AddActor(actor); //i have put the actor here to tryout
  renderer->SetBackground(colors->GetColor3d("BkgColor").GetData());
  //renderer->SetBackground(0.1804,0.5451,0.3412); // Sea green
 // Zoom in a little by accessing the camera and invoking its "Zoom" method.
  renderer->ResetCamera();
  renderer->GetActiveCamera()->Zoom(2.5);

  //defining rendering window
  // The render window is the actual GUI window
  // that appears on the computer screen
  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->SetSize(300, 300);
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("Visualization of ply file");

  //defining interactor
  // The render window interactor captures mouse events
  // and will perform appropriate camera or actor manipulation
  // depending on the nature of the events.
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  //renderer->AddActor(actor);// i think this function should be in renderer, needs to check
  
  

  renderWindow->Render();
// This starts the event loop and as a side effect causes an initial render.
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt:cmake_minimum_required(VERSION 2.8)

PROJECT(vtkvisualization)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

add_executable(vtkvisualization MACOSX_BUNDLE vtkvisualization.cxx )

target_link_libraries(vtkvisualization ${VTK_LIBRARIES})



output.ply:ply
format binary_little_endian 1.0
comment VTK generated PLY File
obj_info vtkPolyData points and polygons: vtk4.0
element vertex 1444314
property float x
property float y
property float z
element face 0
property list uchar int vertex_indices
end_header
mAu\EE2A7P@\9D\FFAO\C22Ao at -XA9\AD7Aq\AC\EF?\D1A\EF\AD2A\DF@\ABA\BF=7A\FD\BB\EE?w5A\D02A\D9 at O\83Am7ADߧ?N A\C6$7A\84-\EE?\82NA\D8\D12A{\BB@\9F\D9A\98{7A\94\DBB?"vA\88:7A\BA\A7?se!A$U7A\C7\F1\ED?\818A՚2A:t at R\D5AeV7A\E4A?b\A6 A\80S7A\E0\84\A6?a"A\FE.7A\C5W\ED? AR2A6!@zA\C67Av\FDR>\D9A\C5>7A__??S\80!A;    7A\B8\AD\A5?.p#A





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://vtk.org/pipermail/vtkusers/attachments/20190108/85eac46e/attachment.html>


More information about the vtkusers mailing list