Ioanna Ioanna - 3 months ago 17
C++ Question

vtkSelectVisiblePoints filter output has 0 points

I’m trying to obtain a polyData with only the visible part of a 3D model.
To that extent, I’m passing the original data through a vtkSelectVisiblePoints filter.

I’m using a mock renderer, mapper and actor because I want to post-process the visible points before displaying them.

However, the output of the vtkSelectVisiblePoints filter contains “0” points for some reason…

Using the following example:
http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/SelectVisiblePoints

I came up with the following piece of code:

// Render window and interactor
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(800, 800);

vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();

renderWindowInteractor->SetRenderWindow(renderWindow);


// Mock renderer, mapper and actor
vtkSmartPointer<vtkRenderer> mockRenderer =
vtkSmartPointer<vtkRenderer>::New();

renderWindow->AddRenderer(mockRenderer);

vtkSmartPointer<vtkPolyDataMapper> mockMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mockMapper->SetInput(reader->GetOutput());

vtkSmartPointer<vtkActor> mockActor = vtkSmartPointer<vtkActor>::New();
mockActor->SetMapper(mockMapper);
mockRenderer->AddActor(mockActor);

// Set camera to the correct position
mockRenderer->GetActiveCamera()->SetPosition(0, -1, 0);
mockRenderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);
mockRenderer->GetActiveCamera()->SetViewUp(0, 1, 0);
mockRenderer->ResetCamera();

vtkSmartPointer<vtkSelectVisiblePoints> selectVisiblePoints =
vtkSmartPointer<vtkSelectVisiblePoints>::New();
selectVisiblePoints->SetInput(reader->GetOutput());
selectVisiblePoints->SetRenderer(mockRenderer);
selectVisiblePoints->Update();

std::cout << "Visible nr of points = " << selectVisiblePoints->GetOutput()->GetNumberOfPoints() << std::endl;

renderWindow->RemoveRenderer(mockRenderer);


… prints 0.

However, if I call
renderWindow->Render()
, the visible part of the model is correctly displayed...

Am I missing anything...?

Answer

The answer was right there, in the documentation of the filter:

Warning You must carefully synchronize the execution of this filter. The filter refers to a renderer, which is modified every time a render occurs. Therefore, the filter is always out of date, and always executes. You may have to perform two rendering passes

It worked as expected if I added the //new lines, like Arnas also suggested in his comment:

  vtkSmartPointer<vtkSelectVisiblePoints> selectVisiblePoints = 
    vtkSmartPointer<vtkSelectVisiblePoints>::New();
  selectVisiblePoints->SetInput(originalData);
  selectVisiblePoints->SetRenderer(renderer);
  selectVisiblePoints->Update();

  renderWindow->Render(); // new
  selectVisiblePoints->Update(); // new

  std::cout << "Visible nr of points = " << selectVisiblePoints->GetOutput()->GetNumberOfPoints() << std::endl;