当前位置:   article > 正文

VTK学习笔记(十一)高亮拾取点_vtkdatasetsurfacefilter

vtkdatasetsurfacefilter

最基础的物体还是点, 看看如何给点进行拾取并高亮。 想要拾取,先要激活窗口,然后按下字母'r',然后用鼠标进行选择,选择后就输出结果。如图所示。

 代码如下

  1. #include <vtkActor.h>
  2. #include <vtkAreaPicker.h>
  3. #include <vtkDataSetMapper.h>
  4. #include <vtkDataSetSurfaceFilter.h>
  5. #include <vtkExtractGeometry.h>
  6. #include <vtkIdFilter.h>
  7. #include <vtkIdTypeArray.h>
  8. #include <vtkInteractorStyleRubberBandPick.h>
  9. #include <vtkNamedColors.h>
  10. #include <vtkNew.h>
  11. #include <vtkObjectFactory.h>
  12. #include <vtkPlanes.h>
  13. #include <vtkPointData.h>
  14. #include <vtkPointSource.h>
  15. #include <vtkPolyData.h>
  16. #include <vtkPolyDataMapper.h>
  17. #include <vtkProperty.h>
  18. #include <vtkRenderWindow.h>
  19. #include <vtkRenderWindowInteractor.h>
  20. #include <vtkRenderer.h>
  21. #include <vtkRendererCollection.h>
  22. #include <vtkSmartPointer.h>
  23. #include <vtkUnstructuredGrid.h>
  24. #include <vtkVersion.h>
  25. #include <vtkVertexGlyphFilter.h>
  26. #if VTK_VERSION_NUMBER >= 89000000000ULL
  27. #define VTK890 1
  28. #endif
  29. #include <vtkAutoInit.h>
  30. VTK_MODULE_INIT(vtkRenderingOpenGL2);
  31. VTK_MODULE_INIT(vtkInteractionStyle);
  32. namespace {
  33. // Define interaction style
  34. class InteractorStyle : public vtkInteractorStyleRubberBandPick
  35. {
  36. public:
  37. static InteractorStyle* New();
  38. vtkTypeMacro(InteractorStyle, vtkInteractorStyleRubberBandPick);
  39. InteractorStyle()
  40. {
  41. this->SelectedMapper = vtkSmartPointer<vtkDataSetMapper>::New();
  42. this->SelectedActor = vtkSmartPointer<vtkActor>::New();
  43. this->SelectedActor->SetMapper(SelectedMapper);
  44. }
  45. virtual void OnLeftButtonUp() override
  46. {
  47. vtkNew<vtkNamedColors> colors;
  48. // Forward events
  49. vtkInteractorStyleRubberBandPick::OnLeftButtonUp();
  50. vtkPlanes* frustum =
  51. static_cast<vtkAreaPicker*>(this->GetInteractor()->GetPicker())
  52. ->GetFrustum();
  53. vtkNew<vtkExtractGeometry> extractGeometry;
  54. extractGeometry->SetImplicitFunction(frustum);
  55. extractGeometry->SetInputData(this->Points);
  56. extractGeometry->Update();
  57. vtkNew<vtkVertexGlyphFilter> glyphFilter;
  58. glyphFilter->SetInputConnection(extractGeometry->GetOutputPort());
  59. glyphFilter->Update();
  60. vtkPolyData* selected = glyphFilter->GetOutput();
  61. std::cout << "Selected " << selected->GetNumberOfPoints() << " points."
  62. << std::endl;
  63. std::cout << "Selected " << selected->GetNumberOfCells() << " cells."
  64. << std::endl;
  65. this->SelectedMapper->SetInputData(selected);
  66. this->SelectedMapper->ScalarVisibilityOff();
  67. vtkIdTypeArray* ids = dynamic_cast<vtkIdTypeArray*>(
  68. selected->GetPointData()->GetArray("OriginalIds"));
  69. for (vtkIdType i = 0; i < ids->GetNumberOfTuples(); i++)
  70. {
  71. std::cout << "Id " << i << " : " << ids->GetValue(i) << std::endl;
  72. }
  73. this->SelectedActor->GetProperty()->SetColor(
  74. colors->GetColor3d("Red").GetData());
  75. this->SelectedActor->GetProperty()->SetPointSize(5);
  76. this->CurrentRenderer->AddActor(SelectedActor);
  77. this->GetInteractor()->GetRenderWindow()->Render();
  78. this->HighlightProp(NULL);
  79. }
  80. void SetPoints(vtkSmartPointer<vtkPolyData> points)
  81. {
  82. this->Points = points;
  83. }
  84. private:
  85. vtkSmartPointer<vtkPolyData> Points;
  86. vtkSmartPointer<vtkActor> SelectedActor;
  87. vtkSmartPointer<vtkDataSetMapper> SelectedMapper;
  88. };
  89. vtkStandardNewMacro(InteractorStyle);
  90. } // namespace
  91. int main(int, char*[])
  92. {
  93. vtkNew<vtkNamedColors> colors;
  94. vtkNew<vtkPointSource> pointSource;
  95. pointSource->SetNumberOfPoints(20);
  96. pointSource->Update();
  97. vtkNew<vtkIdFilter> idFilter;
  98. idFilter->SetInputConnection(pointSource->GetOutputPort());
  99. #if VTK890
  100. idFilter->SetCellIdsArrayName("OriginalIds");
  101. idFilter->SetPointIdsArrayName("OriginalIds");
  102. #else
  103. idFilter->SetIdsArrayName("OriginalIds");
  104. #endif
  105. idFilter->Update();
  106. vtkNew<vtkDataSetSurfaceFilter> surfaceFilter;
  107. surfaceFilter->SetInputConnection(idFilter->GetOutputPort());
  108. surfaceFilter->Update();
  109. vtkPolyData* input = surfaceFilter->GetOutput();
  110. // Create a mapper and actor
  111. vtkNew<vtkPolyDataMapper> mapper;
  112. mapper->SetInputData(input);
  113. mapper->ScalarVisibilityOff();
  114. vtkNew<vtkActor> actor;
  115. actor->SetMapper(mapper);
  116. actor->GetProperty()->SetPointSize(3);
  117. actor->GetProperty()->SetColor(colors->GetColor3d("Gold").GetData());
  118. // Visualize
  119. vtkNew<vtkRenderer> renderer;
  120. vtkNew<vtkRenderWindow> renderWindow;
  121. renderWindow->AddRenderer(renderer);
  122. renderWindow->SetWindowName("HighlightSelectedPoints");
  123. vtkNew<vtkAreaPicker> areaPicker;
  124. vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  125. renderWindowInteractor->SetPicker(areaPicker);
  126. renderWindowInteractor->SetRenderWindow(renderWindow);
  127. renderer->AddActor(actor);
  128. renderer->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());
  129. renderWindow->Render();
  130. vtkNew<InteractorStyle> style;
  131. style->SetPoints(input);
  132. renderWindowInteractor->SetInteractorStyle(style);
  133. renderWindowInteractor->Start();
  134. return EXIT_SUCCESS;
  135. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/535478
推荐阅读
相关标签
  

闽ICP备14008679号