vtk拾取单元

  1 #include "vtkAutoInit.h"
  2 VTK_MODULE_INIT(vtkRenderingOpenGL2);
  3 VTK_MODULE_INIT(vtkInteractionStyle);
  4 VTK_MODULE_INIT(vtkRenderingFreeType)
  5 
  6 #include <vtkActor.h>
  7 #include <vtkInteractorStyleTrackballCamera.h>
  8 #include <vtkNamedColors.h>
  9 #include <vtkNew.h>
 10 #include <vtkObjectFactory.h>
 11 #include <vtkPointPicker.h>
 12 #include <vtkPolyDataMapper.h>
 13 #include <vtkProperty.h>
 14 #include <vtkRenderWindow.h>
 15 #include <vtkRenderWindowInteractor.h>
 16 #include <vtkRenderer.h>
 17 #include <vtkRendererCollection.h>
 18 #include <vtkSphereSource.h>
 19 #include <vtkRendererCollection.h>
 20 #include <vtkObjectFactory.h>
 21 #include <vtkCellPicker.h>
 22 #include <vtkSelectionNode.h>
 23 #include <vtkPolyData.h>
 24 #include <vtkUnstructuredGrid.h>
 25 #include <vtkSelection.h>
 26 #include <vtkExtractSelection.h>
 27 #include <vtkNamedColors.h>
 28 #include <vtkDataSetMapper.h>
 29 #include <vtkPoints.h>
 30 #include <vtkidList.h>
 31 
 32 namespace {
 33     class MouseInteractorStyle : public vtkInteractorStyleTrackballCamera
 34     {
 35     public:
 36         static MouseInteractorStyle* New();
 37 
 38         vtkTypeMacro(MouseInteractorStyle, vtkInteractorStyleTrackballCamera);
 39 
 40         MouseInteractorStyle() {
 41             displayActor = vtkSmartPointer<vtkActor>::New();
 42             displayActor->GetProperty()->SetColor(1.0, 0.0, 0.0);
 43         }
 44 
 45         virtual void OnLeftButtonDown() override
 46         {
 47             vtkNew<vtkNamedColors> colors;
 48 
 49             // 鼠标点击位置[x, y]
 50             int* pos = this->Interactor->GetEventPosition();
 51             // 上一次的鼠标点击位置
 52             int* lastPos = this->Interactor->GetLastEventPosition();
 53 
 54             auto picker = this->Interactor->GetPicker();
 55             // Pick(x, y, z, vtkRenderer)
 56             picker->Pick(pos[0], pos[1], 0, this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer());
 57 
 58             // 点击点的世界坐标
 59             double worldPos[3];
 60             picker->GetPickPosition(worldPos);
 61 
 62             if (picker->IsA("vtkCellPicker")) {
 63                 auto cellPicker = vtkCellPicker::SafeDownCast(picker);
 64                 std::cout << "Pick Position: [" << worldPos[0] << ", " << worldPos[1] << ", " << worldPos[2] << "]" << std::endl;
 65                 std::cout << "CellId: " << cellPicker->GetCellId() << std::endl;
 66                 std::cout << "PointId: " << cellPicker->GetPointId() << std::endl;
 67 
 68                 if (cellPicker->GetCellId() != -1)
 69                 {
 70                     if (nullptr == data) {
 71                         std::cout << "Source data not found! " << std::endl;
 72                         return;
 73                     }
 74                     // 高亮选择的单元
 75                     auto cell = data->GetCell(cellPicker->GetCellId());
 76                     std::cout << "Cell Class Name: " << cell->GetClassName() << std::endl;
 77                     vtkNew<vtkUnstructuredGrid> selectedCells;
 78                     vtkNew<vtkCellArray> cellArray;
 79                     vtkNew<vtkPoints> pointList;
 80                     vtkNew<vtkIdList> idList;
 81                     auto points = cell->GetPoints();
 82                     auto num = points->GetNumberOfPoints();
 83                     double p[3];
 84                     for (size_t i = 0; i < num; i++)
 85                     {
 86                         points->GetPoint(i, p);
 87                         std::cout << "Point" << i << ": " << std::endl;
 88                         std::cout << "\tx = " << p[0] << std::endl;
 89                         std::cout << "\ty = " << p[1] << std::endl;
 90                         std::cout << "\tz = " << p[2] << std::endl;
 91                         idList->InsertNextId(pointList->InsertNextPoint(p));
 92                     }
 93                     selectedCells->SetPoints(pointList);
 94                     selectedCells->InsertNextCell(cell->GetCellType(), idList);
 95 
 96                     std::cout << "Number of points in the selection: "
 97                         << selectedCells->GetNumberOfPoints() << std::endl;
 98                     std::cout << "Number of cells in the selection : "
 99                         << selectedCells->GetNumberOfCells() << std::endl;
100                     auto mapper = vtkSmartPointer<vtkDataSetMapper>::New();
101                     mapper->SetInputData(selectedCells);
102                     this->Interactor->GetRenderWindow()
103                         ->GetRenderers()
104                         ->GetFirstRenderer()
105                         ->RemoveActor(displayActor);
106                     displayActor->SetMapper(mapper);
107                     displayActor->GetProperty()->EdgeVisibilityOn();
108                     displayActor->GetProperty()->SetColor(1.0, 0.0, 0.0);
109                     displayActor->GetProperty()->SetPointSize(5.0);
110                     this->Interactor->GetRenderWindow()
111                         ->GetRenderers()
112                         ->GetFirstRenderer()
113                         ->AddActor(displayActor);
114                 }
115 
116             }
117             else {
118                 std::cout << "WorldPos: [" << worldPos[0] << ", " << worldPos[1] << ", " << worldPos[2] << "]" << std::endl;
119             }
120 
121             // 事件转发
122             vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
123         }
124 
125         vtkPolyData* data = nullptr;
126         vtkSmartPointer<vtkActor> displayActor = nullptr;
127     };
128     vtkStandardNewMacro(MouseInteractorStyle)
129 
130 }
131 
132 int main(int, char* [])
133 {
134     vtkNew<vtkNamedColors> colors;
135 
136     vtkNew<vtkSphereSource> sphereSource;
137     sphereSource->SetThetaResolution(20);
138     sphereSource->SetPhiResolution(20);
139     sphereSource->Update();
140 
141     // Create a mapper and actor
142     vtkNew<vtkPolyDataMapper> mapper;
143     mapper->SetInputConnection(sphereSource->GetOutputPort());
144     vtkNew<vtkActor> actor;
145     actor->SetMapper(mapper);
146     actor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());
147     actor->GetProperty()->SetRepresentationToWireframe();
148 
149     // Create a renderer, render window, and interactor
150     vtkNew<vtkRenderer> renderer;
151     // 如果此标志为On并且渲染引擎支持它,则线框模式下几何图形的隐(非可见)藏线将会被移除。
152     //renderer->UseHiddenLineRemovalOn();
153     vtkNew<vtkRenderWindow> renderWindow;
154     renderWindow->AddRenderer(renderer);
155     renderWindow->SetWindowName("PointPicker");
156 
157     vtkNew<vtkCellPicker> picker;
158     vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
159     renderWindowInteractor->SetPicker(picker);
160     renderWindowInteractor->SetRenderWindow(renderWindow);
161 
162     vtkNew<MouseInteractorStyle> style;
163     style->data = sphereSource->GetOutput();
164     renderWindowInteractor->SetInteractorStyle(style);
165 
166     // Add the actor to the scene
167     renderer->AddActor(actor);
168     renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
169 
170     // Render and interact
171     renderWindow->Render();
172     renderWindowInteractor->Start();
173 
174     return EXIT_SUCCESS;
175 }

 

posted @ 2022-07-20 10:41  禅元天道  阅读(447)  评论(0编辑  收藏  举报