vtkSelectVisiblePoints过滤输出有0分

时间:2016-09-01 15:03:57

标签: c++ vtk

我正在尝试获取仅包含3D模型可见部分的polyData。 在这种程度上,我通过vtkSelectVisiblePoints过滤器传递原始数据。

我正在使用模拟渲染器,映射器和actor,因为我想在显示它们之前对可见点进行后处理。

但是,vtkSelectVisiblePoints过滤器的输出由于某种原因包含“0”点......

使用以下示例: http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/SelectVisiblePoints

我提出了以下代码:

  // 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);

...打印0。

但是,如果我调用renderWindow->Render(),模型的可见部分会正确显示...

我错过了什么......?

1 个答案:

答案 0 :(得分:2)

答案就在那里,documentation of the filter

  

警告您必须小心地同步此过滤器的执行。   过滤器引用渲染器,每次渲染时都会对其进行修改   发生。因此,过滤器始终是过时的   执行。您可能必须执行两次渲染过程

如果我添加了//new行,就像预期的那样工作,就像Arnas在他的评论中所建议的那样:

  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;