vtk世界坐标拾取器
作者:互联网
1 /** 2 * @file main.cpp 3 * @brief vtk世界坐标拾取器 4 * @author 禅元天道(chanyuantiandao@126.com) 5 * @version 1.0.0 6 * @date 2022-07-19 7 */ 8 #include <vtkActor.h> 9 #include <vtkInteractorStyleTrackballCamera.h> 10 #include <vtkNamedColors.h> 11 #include <vtkNew.h> 12 #include <vtkObjectFactory.h> 13 #include <vtkPolyDataMapper.h> 14 #include <vtkProperty.h> 15 #include <vtkRenderWindow.h> 16 #include <vtkRenderWindowInteractor.h> 17 #include <vtkRenderer.h> 18 #include <vtkRendererCollection.h> 19 #include <vtkSphereSource.h> 20 #include <vtkWorldPointPicker.h> 21 22 #include "vtkAutoInit.h" 23 VTK_MODULE_INIT(vtkRenderingOpenGL2); 24 VTK_MODULE_INIT(vtkInteractionStyle); 25 VTK_MODULE_INIT(vtkRenderingFreeType) 26 27 namespace { 28 29 // Define interaction style 30 class MouseInteractorStyle : public vtkInteractorStyleTrackballCamera 31 { 32 public: 33 static MouseInteractorStyle* New(); 34 vtkTypeMacro(MouseInteractorStyle, vtkInteractorStyleTrackballCamera); 35 36 virtual void OnLeftButtonDown() override 37 { 38 // 鼠标点击点 39 std::cout << "Picking pixel: " << this->Interactor->GetEventPosition()[0] 40 << " " << this->Interactor->GetEventPosition()[1] << std::endl; 41 42 this->Interactor->GetPicker()->Pick(this->Interactor->GetEventPosition()[0], 43 this->Interactor->GetEventPosition()[1], 0, // 二维世界,z为0 44 this->Interactor->GetRenderWindow() 45 ->GetRenderers() 46 ->GetFirstRenderer()); 47 48 // 获取世界坐标值 49 double picked[3]; 50 this->Interactor->GetPicker()->GetPickPosition(picked); 51 52 std::cout << "Picked value: " << picked[0] << " " << picked[1] << " " 53 << picked[2] << std::endl; 54 55 // 事件转发 56 vtkInteractorStyleTrackballCamera::OnLeftButtonDown(); 57 } 58 }; 59 vtkStandardNewMacro(MouseInteractorStyle); 60 61 } // namespace 62 63 64 int main() 65 { 66 vtkNew<vtkNamedColors> colors; 67 68 vtkNew<vtkSphereSource> sphereSource; 69 sphereSource->Update(); 70 71 vtkNew<vtkPolyDataMapper> mapper; 72 mapper->SetInputConnection(sphereSource->GetOutputPort()); 73 vtkNew<vtkActor> actor; 74 actor->SetMapper(mapper); 75 actor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData()); 76 77 vtkNew<vtkRenderer> renderer; 78 vtkNew<vtkRenderWindow> renderWindow; 79 renderWindow->AddRenderer(renderer); 80 renderWindow->SetWindowName("WorldPointPicker"); 81 82 // ①世界坐标拾取器 83 vtkNew<vtkWorldPointPicker> worldPointPicker; 84 vtkNew<vtkRenderWindowInteractor> renderWindowInteractor; 85 // ②设置拾取器 86 renderWindowInteractor->SetPicker(worldPointPicker); 87 renderWindowInteractor->SetRenderWindow(renderWindow); 88 89 // ③设置交互样式,处理事件 90 vtkNew<MouseInteractorStyle> style; 91 renderWindowInteractor->SetInteractorStyle(style); 92 93 renderer->AddActor(actor); 94 renderer->SetBackground(colors->GetColor3d("SlateGray").GetData()); 95 96 renderWindow->Render(); 97 renderWindowInteractor->Start(); 98 99 return EXIT_SUCCESS; 100 }
标签:Interactor,vtk,世界坐标,拾取,renderWindowInteractor,actor,renderWindow,include,vtkNew 来源: https://www.cnblogs.com/chanyuantiandao/p/16493827.html