网站代码模板免费,宣传片拍摄清单,企业网络搭建服务,网站建设程序结构开发环境#xff1a;
Windows 11 家庭中文版Microsoft Visual Studio Community 2019VTK-9.3.0.rc0vtk-example参考代码目的#xff1a;学习与总结 demo解决问题#xff1a;通过自定义vtkInteractorStyle类中成员函数OnLeftButtonDown#xff0c;判断鼠标当前选中的是哪个…
开发环境
Windows 11 家庭中文版Microsoft Visual Studio Community 2019VTK-9.3.0.rc0vtk-example参考代码目的学习与总结 demo解决问题通过自定义vtkInteractorStyle类中成员函数OnLeftButtonDown判断鼠标当前选中的是哪个actor同理可自定义鼠标右键、滚轮、键盘等事件 关键类vtkInteractorStyleTrackballActor允许用户与场景中彼此独立的对象进行交互旋转、平移等根据实际应用场景有如下常见替换对象 vtkInteractorStyleTrackballActor作用对象actor; 形式TrackballvtkInteractorStyleTrackballCamera作用对象Camera; 形式TrackballvtkInteractorStyleJoystickActor作用对象actor; 形式JoystickvtkInteractorStyleJoystickCamera作用对象Camera; 形式JoystickvtkInteractorStyleImage作用对象vtkImageActor; 形式绑定使相机的视图平面垂直于x-y平面……
参考vtkInteractorStyle详细介绍 #include vtkActor.h
#include vtkCamera.h
#include vtkCubeSource.h
#include vtkInteractorStyleTrackballActor.h
#include vtkNamedColors.h
#include vtkNew.h
#include vtkPolyDataMapper.h
#include vtkProperty.h
#include vtkRenderWindow.h
#include vtkRenderWindowInteractor.h
#include vtkRenderer.h
#include vtkSphereSource.hnamespace {// Handle mouse events.
class MouseInteractorStyle5 : public vtkInteractorStyleTrackballActor
{
public:static MouseInteractorStyle5* New();vtkTypeMacro(MouseInteractorStyle5, vtkInteractorStyleTrackballActor);virtual void OnLeftButtonDown() override{// Forward events.vtkInteractorStyleTrackballActor::OnLeftButtonDown();if (this-InteractionProp this-Cube){std::cout Picked cube. std::endl;}else if (this-InteractionProp this-Sphere){std::cout Picked sphere. std::endl;}}vtkActor* Cube;vtkActor* Sphere;
};vtkStandardNewMacro(MouseInteractorStyle5);} // namespaceint main(int, char*[])
{vtkNewvtkNamedColors colors;// Create a cube.vtkNewvtkCubeSource cubeSource;cubeSource-Update();vtkNewvtkPolyDataMapper cubeMapper;cubeMapper-SetInputConnection(cubeSource-GetOutputPort());vtkNewvtkActor cubeActor;cubeActor-SetMapper(cubeMapper);cubeActor-GetProperty()-SetColor(colors-GetColor3d(MistyRose).GetData());// Create a sphere.vtkNewvtkSphereSource sphereSource;sphereSource-SetCenter(2, 0, 0);sphereSource-Update();// Create a mapper.vtkNewvtkPolyDataMapper sphereMapper;sphereMapper-SetInputConnection(sphereSource-GetOutputPort());// Create an actor.vtkNewvtkActor sphereActor;sphereActor-SetMapper(sphereMapper);sphereActor-GetProperty()-SetColor(colors-GetColor3d(LightGoldenrodYellow).GetData());// A renderer and render window.vtkNewvtkRenderer renderer;vtkNewvtkRenderWindow renderWindow;renderWindow-AddRenderer(renderer);renderWindow-SetWindowName(SelectAnActor);// An interactor.vtkNewvtkRenderWindowInteractor renderWindowInteractor;renderWindowInteractor-SetRenderWindow(renderWindow);// Set the custom stype to use for interaction.vtkNewMouseInteractorStyle5 style;style-SetDefaultRenderer(renderer);style-Cube cubeActor;style-Sphere sphereActor;renderWindowInteractor-SetInteractorStyle(style);renderer-AddActor(cubeActor);renderer-AddActor(sphereActor);renderer-SetBackground(colors-GetColor3d(SlateGray).GetData());renderer-ResetCamera();renderer-GetActiveCamera()-Zoom(0.9);// Render and interact.renderWindow-Render();renderWindowInteractor-Initialize();renderWindowInteractor-Start();return EXIT_SUCCESS;
}