VTK 视角的旋转、平移、缩放
在CAD/CAM软件中,都需要旋转、平移和缩放视角,来观察操作图形。由于VTK定义的交互的类型不是很适用,所有通过定义一套自己的交互方式。
在下面代码中,鼠标左键平移,滚轮缩放,右键旋转。
先定义一个交互类型。继承自vtkInteractorStyleTrackballCamera,并且交换按键。
class Normal3DCameraStyle : public vtkInteractorStyleTrackballCamera { public: static Normal3DCameraStyle* New(); vtkTypeMacro(Normal3DCameraStyle, vtkInteractorStyleTrackballCamera); virtual void OnLeftButtonDown(void); virtual void OnLeftButtonUp(void); virtual void OnRightButtonDown(void); virtual void OnRightButtonUp(void); virtual void OnMiddleButtonDown(void); virtual void OnMiddleButtonUp(void); virtual void OnMouseWheelForward(void); virtual void OnMouseWheelBackward(void); virtual void OnMouseMove(void); }; vtkStandardNewMacro(Normal3DCameraStyle);//必须加!!!! void Normal3DCameraStyle::OnLeftButtonDown(void) { vtkInteractorStyleTrackballCamera::OnMiddleButtonDown(); } void Normal3DCameraStyle::OnLeftButtonUp(void) { vtkInteractorStyleTrackballCamera::OnMiddleButtonUp(); } void Normal3DCameraStyle::OnRightButtonDown(void) { vtkInteractorStyleTrackballCamera::OnLeftButtonDown(); } void Normal3DCameraStyle::OnRightButtonUp(void) { vtkInteractorStyleTrackballCamera::OnLeftButtonUp(); } void Normal3DCameraStyle::OnMiddleButtonDown(void) { } void Normal3DCameraStyle::OnMiddleButtonUp(void) { } void Normal3DCameraStyle::OnMouseWheelForward(void) { vtkInteractorStyleTrackballCamera::OnMouseWheelForward(); }; void Normal3DCameraStyle::OnMouseWheelBackward(void) { vtkInteractorStyleTrackballCamera::OnMouseWheelBackward(); }; void Normal3DCameraStyle::OnMouseMove(void) { vtkInteractorStyleTrackballCamera::OnMouseMove(); }
添加Normal3DCameraStyle到交换器中
vtkNew<Normal3DCameraStyle> style;
ui.vtk_main->interactor()->SetInteractorStyle(style);
下面是完整代码
class Normal3DCameraStyle : public vtkInteractorStyleTrackballCamera { public: static Normal3DCameraStyle* New(); vtkTypeMacro(Normal3DCameraStyle, vtkInteractorStyleTrackballCamera); virtual void OnLeftButtonDown(void); virtual void OnLeftButtonUp(void); virtual void OnRightButtonDown(void); virtual void OnRightButtonUp(void); virtual void OnMiddleButtonDown(void); virtual void OnMiddleButtonUp(void); virtual void OnMouseWheelForward(void); virtual void OnMouseWheelBackward(void); virtual void OnMouseMove(void); }; vtkStandardNewMacro(Normal3DCameraStyle);//必须加!!!! void Normal3DCameraStyle::OnLeftButtonDown(void) { vtkInteractorStyleTrackballCamera::OnMiddleButtonDown(); } void Normal3DCameraStyle::OnLeftButtonUp(void) { vtkInteractorStyleTrackballCamera::OnMiddleButtonUp(); } void Normal3DCameraStyle::OnRightButtonDown(void) { vtkInteractorStyleTrackballCamera::OnLeftButtonDown(); } void Normal3DCameraStyle::OnRightButtonUp(void) { vtkInteractorStyleTrackballCamera::OnLeftButtonUp(); } void Normal3DCameraStyle::OnMiddleButtonDown(void) { } void Normal3DCameraStyle::OnMiddleButtonUp(void) { } void Normal3DCameraStyle::OnMouseWheelForward(void) { vtkInteractorStyleTrackballCamera::OnMouseWheelForward(); }; void Normal3DCameraStyle::OnMouseWheelBackward(void) { vtkInteractorStyleTrackballCamera::OnMouseWheelBackward(); }; void Normal3DCameraStyle::OnMouseMove(void) { vtkInteractorStyleTrackballCamera::OnMouseMove(); } QtVtkTest::QtVtkTest(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat()); vtkNew<vtkRenderer> renderer; vtkNew<vtkNamedColors> colors; //定义球 vtkNew<vtkSphereSource> sphereSource; sphereSource->SetRadius(5); sphereSource->SetCenter(5, 0, 0); vtkNew<vtkPolyDataMapper> sphereMapper; sphereMapper->SetInputConnection(sphereSource->GetOutputPort()); vtkNew<vtkActor> sphereActor; sphereActor->SetMapper(sphereMapper); sphereActor->GetProperty()->SetColor(colors->GetColor4d("Green").GetData()); sphereActor->GetProperty()->EdgeVisibilityOn(); //显示线框 renderer->AddActor(sphereActor); renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData()); vtkNew<vtkGenericOpenGLRenderWindow> renderWindow; renderWindow->AddRenderer(renderer); renderWindow->SetWindowName("RenderWindowNoUIFile"); ui.vtk_main->setRenderWindow(renderWindow); // vtkNew<Normal3DCameraStyle> style; ui.vtk_main->interactor()->SetInteractorStyle(style); }