VTK Users Guide 中C++例程之ImplicitPlaneWidget
本人将《The VTK User’s Guide 11th Edition 》中的TCL编写的例程进行了C++的转换,使用的平台是VS2015,VTK版本是8.0.1。之所以贴出来,以求方便大家使用C++来进行VTK的研究。
VTK/Examples/GUI/Tcl/ImplicitPlaneWidget.tcl 之转成C++
1 #include "vtkAutoInit.h" 2 #include "vtkRenderer.h" 3 #include "vtkRenderWindow.h" 4 #include "vtkRenderWindowInteractor.h" 5 #include "vtkSmartPointer.h" 6 #include "vtkSphereSource.h" 7 #include "vtkConeSource.h" 8 #include "vtkGlyph3D.h" 9 #include "vtkAppendPolyData.h" 10 #include "vtkPolyDataMapper.h" 11 #include "vtkLODActor.h" 12 #include "vtkPlane.h" 13 #include "vtkClipPolyData.h" 14 #include "vtkProperty.h" 15 #include "vtkImplicitPlaneWidget.h" 16 #include "vtkCommand.h" 17 #include "vtkCallbackCommand.h" 18 19 static vtkSmartPointer<vtkImplicitPlaneWidget> planeWidget; 20 static vtkSmartPointer<vtkLODActor> selectActor; 21 static vtkSmartPointer<vtkPlane> plane; 22 23 void myCallbackFunction(vtkObject* caller, long unsigned int eventId, void* clientData, void* callData) 24 { 25 planeWidget->GetPlane(plane); 26 selectActor->VisibilityOn(); 27 } 28 29 int main() 30 { 31 VTK_MODULE_INIT(vtkRenderingOpenGL2); 32 VTK_MODULE_INIT(vtkRenderingFreeType); 33 VTK_MODULE_INIT(vtkInteractionStyle); 34 35 //# This example demonstrates how to use the vtkPlaneWidget to probe 36 //# a dataset and then generate contours on the probed data. 37 38 //# Create a mace out of filters. 39 vtkSmartPointer<vtkSphereSource> sphere = vtkSmartPointer<vtkSphereSource>::New(); 40 vtkSmartPointer<vtkConeSource> cone = vtkSmartPointer<vtkConeSource>::New(); 41 vtkSmartPointer<vtkGlyph3D> glyph = vtkSmartPointer<vtkGlyph3D>::New(); 42 glyph->SetInputConnection(sphere->GetOutputPort()); 43 glyph->SetSourceConnection(cone->GetOutputPort()); 44 glyph->SetVectorModeToUseNormal(); 45 glyph->SetScaleModeToScaleByVector(); 46 glyph->SetScaleFactor(0.25); 47 48 //# The sphere and spikes are appended into a single polydata. 49 //# This just makes things simpler to manage. 50 vtkSmartPointer<vtkAppendPolyData> apd = vtkSmartPointer<vtkAppendPolyData>::New(); 51 apd->AddInputConnection(glyph->GetOutputPort()); 52 apd->AddInputConnection(sphere->GetOutputPort()); 53 vtkSmartPointer<vtkPolyDataMapper> maceMapper = vtkSmartPointer<vtkPolyDataMapper>::New(); 54 maceMapper->SetInputConnection(apd->GetOutputPort()); 55 vtkSmartPointer<vtkLODActor> maceActor = vtkSmartPointer<vtkLODActor>::New(); 56 maceActor->SetMapper(maceMapper); 57 maceActor->VisibilityOn(); 58 59 //# This portion of the code clips the mace with the vtkPlanes 60 //# implicit function.The clipped region is colored green. 61 plane = vtkSmartPointer<vtkPlane>::New(); 62 vtkSmartPointer<vtkClipPolyData> clipper = vtkSmartPointer<vtkClipPolyData>::New(); 63 clipper->SetInputConnection(apd->GetOutputPort()); 64 clipper->SetClipFunction(plane); 65 clipper->InsideOutOn(); 66 vtkSmartPointer<vtkPolyDataMapper> selectMapper = vtkSmartPointer<vtkPolyDataMapper>::New(); 67 selectMapper->SetInputConnection(clipper->GetOutputPort()); 68 selectActor = vtkSmartPointer<vtkLODActor>::New(); 69 selectActor->SetMapper(selectMapper); 70 selectActor->GetProperty()->SetColor(0, 1, 0); 71 selectActor->VisibilityOff(); 72 selectActor->SetScale(1.01, 1.01, 1.01); 73 74 //Create the Renderer, RenderWindow, RenderWindowInteractor 75 vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer<vtkRenderer>::New(); 76 vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New(); 77 renWin->AddRenderer(ren1); 78 vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New(); 79 iren->SetRenderWindow(renWin); 80 81 //# Associate the line widget with the interactor 82 planeWidget = vtkSmartPointer<vtkImplicitPlaneWidget>::New(); 83 planeWidget->SetInteractor(iren); 84 planeWidget->SetPlaceFactor(1.25); 85 planeWidget->SetInputConnection(glyph->GetOutputPort()); 86 planeWidget->PlaceWidget(); 87 vtkSmartPointer<vtkCallbackCommand> myCallback = vtkSmartPointer<vtkCallbackCommand>::New(); 88 myCallback->SetCallback(myCallbackFunction); 89 planeWidget->AddObserver(vtkCommand::InteractionEvent,myCallback); 90 91 ren1->AddActor(maceActor); 92 ren1->AddActor(selectActor); 93 94 //Add the actors to the renderer; set the background and size; zoom in; 95 //and render. 96 ren1->SetBackground(1, 1, 1); 97 renWin->SetSize(300, 300); 98 ren1->SetBackground(0.1, 0.2, 0.4); 99 100 //ren1->ResetCamera(); 101 102 renWin->Render(); 103 iren->Start(); 104 105 return 0; 106 }
不要迷恋哥,哥只是一段代码。