在完成任意平面切割后经常需要通过计算机设置切割平面位置,不然就需要一直使用手去拖动比较繁琐。
其实可以通过设置vtkImplicitPlaneWidget2的vtkImplicitPlaneRepresentation的值即可:
// set up representation for vtkImplicitPlaneWidget2 // you could set the normal and origin with the value you want vtkSmartPointer<vtkImplicitPlaneRepresentation> rep = vtkSmartPointer<vtkImplicitPlaneRepresentation>::New(); rep->SetPlaceFactor(1.25); // This must be set prior to placing the widget rep->PlaceWidget(coneSkinActor->GetBounds()); #ifndef SET_VALUE rep->SetNormal(measureNormal); rep->SetOrigin(point3); #endif vtkSmartPointer<vtkImplicitPlaneWidget2> implicitPlaneWidget = vtkImplicitPlaneWidget2::New(); implicitPlaneWidget->SetInteractor(iren); // this set the representation for the widget thus the normal and origin is transfer to the widget implicitPlaneWidget->SetRepresentation(rep);
结果如下图:第一图是没有设置法向量和初始点;第二图是初始化切割平面
完整代码:
// GitPro.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <vtkCommand.h> #include <vtkRenderer.h> #include <vtkRenderWindow.h> #include <vtkSphereSource.h> #include <vtkRenderWindowInteractor.h> #include <vtkPolyData.h> #include <vtkPolyDataMapper.h> #include <vtkClipPolyData.h> #include <vtkActor.h> #include <vtkConeSource.h> #include <vtkPlane.h> #include <vtkImageActor.h> #include <vtkImplicitPlaneWidget2.h> #include <vtkAngleWidget.h> #include <vtkPlaneWidget.h> #include <vtkSmartPointer.h> #include <vtkImplicitPlaneRepresentation.h> #include <vtkAngleRepresentation3D.h> #include <vtkPointHandleRepresentation3D.h> #include <vtkInteractorStyleTrackballCamera.h> #include <vtkJPEGReader.h> #include <vtkContourFilter.h> #include <vtkImageFlip.h> #include <vtkSmoothPolyDataFilter.h> #include <vtkPolyDataNormals.h> #include <vtkStripper.h> #include <vtkProperty.h> #include <vtkOutlineFilter.h> #include <vtkCamera.h> #include <vtkTriangle.h> #include <vtkImageViewer2.h> #include <vtkImageData.h> class VTKImplicitPlaneWidget2Callback : public vtkCommand { public: static VTKImplicitPlaneWidget2Callback *New() { return new VTKImplicitPlaneWidget2Callback; } public: virtual void Execute(vtkObject *caller,