当前位置:   article > 正文

VTK—vtkCutter截取平面数据

VTK—vtkCutter截取平面数据

       ,

 

        本例 vtkCutter可以配合隐式函数截取数据使用vtkPlane隐式函数配合vtkWidget截取任意平面。

1.读入数据

  1. Create(vtkMultiBlockPLOT3DReader, reader);
  2. reader->SetXYZFileName("G:/Temp/vtkTest/combxyz.bin");
  3. reader->SetQFileName("G:/Temp/vtkTest/combq.bin");
  4. reader->SetScalarFunctionNumber(100);
  5. reader->Update();

        可以使用其他类型的数据测试。读取的数据文件在《VTK—vtkStructuredGrid提取维度面数据》文章中上传过。

2.定位截取平面

  1. vtkStructuredGrid *grid = (vtkStructuredGrid *) (reader->GetOutput()->GetBlock(0));
  2. double bounds[6] = {0};
  3. grid->GetBounds(bounds);
  4. Create(vtkPlane, plane);
  5. plane->SetOrigin((bounds[0] + bounds[1]) * 0.5,
  6. (bounds[2] + bounds[3]) * 0.5,
  7. (bounds[4] + bounds[5]) * 0.5);
  8. plane->SetNormal(0, 0, 1);

        初始时隐式函数平面定位在包络框中心,法线指向Z轴正方向。

3.配置vtkCutter

  1. Create(vtkCutter, cutter);
  2. cutter->SetCutFunction(plane);
  3. cutter->SetInputData(grid);

 4.使用vtkPlaneWidget交互定位切割平面

  1. Create(vtkPlaneWidget, widget);
  2. widget->SetInteractor(inter);
  3. widget->SetDefaultRenderer(render1);
  4. widget->SetOrigin(plane->GetOrigin());
  5. widget->SetPoint1(bounds[1],
  6. (bounds[2] + bounds[3]) * 0.5,
  7. (bounds[4] + bounds[5]) * 0.5);
  8. widget->SetPoint2((bounds[0] + bounds[1]) * 0.5,
  9. bounds[3],
  10. (bounds[4] + bounds[5]) * 0.5);
  11. widget->EnabledOn();
  12. Create(mycallback, callback);
  13. callback->m_plane = plane;
  14. widget->AddObserver(vtkCommand::InteractionEvent, callback);

4.1小部件四角拖动可调整平面显示大小

 4.2鼠标左键点击平面可以任意拖动

4.3鼠标左键点击平面+CTRL键可使平面绕法线旋转

4.4拖动箭头可改变平面角度

4.5鼠标中键可以沿着法线拖动平面

5.完整代码

  1. #include<vtkNew.h>
  2. #include<vtkPlane.h>
  3. #include<vtkCutter.h>
  4. #include<vtkCamera.h>
  5. #include<vtkAutoInit.h>
  6. #include<vtkRenderer.h>
  7. #include<vtkPlaneWidget.h>
  8. #include<vtkRenderWindow.h>
  9. #include<vtkDataSetMapper.h>
  10. #include<vtkStructuredGrid.h>
  11. #include<vtkMultiBlockDataSet.h>
  12. #include<vtkRendererCollection.h>
  13. #include<vtkRenderWindowInteractor.h>
  14. #include<vtkMultiBlockPLOT3DReader.h>
  15. VTK_MODULE_INIT(vtkRenderingOpenGL2)
  16. VTK_MODULE_INIT(vtkInteractionStyle)
  17. VTK_MODULE_INIT(vtkRenderingFreeType)
  18. #define Create(type,name)\
  19. vtkNew<type> name;
  20. class mycallback: public vtkCommand
  21. {
  22. public:
  23. static mycallback *New() {
  24. return new mycallback;
  25. }
  26. void Execute(vtkObject *caller, unsigned long eventId, void *callData) override
  27. {
  28. vtkPlaneWidget *widget = (vtkPlaneWidget *)caller;
  29. widget->GetPlane(m_plane);
  30. }
  31. vtkPlane *m_plane;
  32. private:
  33. mycallback() {
  34. m_plane = nullptr;
  35. }
  36. ~mycallback() {}
  37. };
  38. int main()
  39. {
  40. Create(vtkMultiBlockPLOT3DReader, reader);
  41. reader->SetXYZFileName("G:/Temp/vtkTest/combxyz.bin");
  42. reader->SetQFileName("G:/Temp/vtkTest/combq.bin");
  43. reader->SetScalarFunctionNumber(100);
  44. reader->Update();
  45. vtkStructuredGrid *grid = (vtkStructuredGrid *) (reader->GetOutput()->GetBlock(0));
  46. double bounds[6] = {0};
  47. grid->GetBounds(bounds);
  48. Create(vtkPlane, plane);
  49. plane->SetOrigin((bounds[0] + bounds[1]) * 0.5,
  50. (bounds[2] + bounds[3]) * 0.5,
  51. (bounds[4] + bounds[5]) * 0.5);
  52. plane->SetNormal(0, 0, 1);
  53. Create(vtkCutter, cutter);
  54. cutter->SetCutFunction(plane);
  55. cutter->SetInputData(grid);
  56. Create(vtkDataSetMapper, mapper1);
  57. mapper1->SetInputData(grid);
  58. Create(vtkActor, actor1);
  59. actor1->SetMapper(mapper1);
  60. Create(vtkRenderer, render1);
  61. render1->AddActor(actor1);
  62. render1->SetViewport(0, 0, 0.5, 1);
  63. Create(vtkDataSetMapper, mapper2);
  64. mapper2->SetInputConnection(cutter->GetOutputPort());
  65. Create(vtkActor, actor2);
  66. actor2->SetMapper(mapper2);
  67. Create(vtkRenderer, render2);
  68. render2->AddActor(actor2);
  69. render2->SetViewport(0.5, 0, 1, 1);
  70. Create(vtkRenderWindow, window);
  71. window->AddRenderer(render1);
  72. window->AddRenderer(render2);
  73. window->GetRenderers()->InitTraversal();
  74. Create(vtkRenderWindowInteractor, inter);
  75. inter->SetRenderWindow(window);
  76. Create(vtkCamera, camera);
  77. render1->SetActiveCamera(camera);
  78. render2->SetActiveCamera(camera);
  79. render1->ResetCamera();
  80. Create(vtkPlaneWidget, widget);
  81. widget->SetInteractor(inter);
  82. widget->SetDefaultRenderer(render1);
  83. widget->SetOrigin(plane->GetOrigin());
  84. widget->SetPoint1(bounds[1],
  85. (bounds[2] + bounds[3]) * 0.5,
  86. (bounds[4] + bounds[5]) * 0.5);
  87. widget->SetPoint2((bounds[0] + bounds[1]) * 0.5,
  88. bounds[3],
  89. (bounds[4] + bounds[5]) * 0.5);
  90. widget->EnabledOn();
  91. Create(mycallback, callback);
  92. callback->m_plane = plane;
  93. widget->AddObserver(vtkCommand::InteractionEvent, callback);
  94. inter->Start();
  95. return 0;
  96. }

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

闽ICP备14008679号