VTK Users Guide 中C++例程之annotatePick

2018-06-17 21:31:40来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

本人将《The VTK User’s Guide 11th Edition 》中的TCL编写的例程进行了C++的转换,使用的平台是VS2015,VTK版本是8.0.1。之所以贴出来,以求方便大家使用C++来进行VTK的研究。

VTK/Examples/Annotation/Tcl/annotatePick.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 "vtkPolyDataMapper.h"
  8 #include "vtkLODActor.h"
  9 #include "vtkConeSource.h"
 10 #include "vtkGlyph3D.h"
 11 #include "vtkCallbackCommand.h"
 12 #include "vtkCommand.h"
 13 #include "vtkCellPicker.h"
 14 #include "vtkTextMapper.h"
 15 #include "vtkTextProperty.h"
 16 #include "vtkActor2D.h"
 17 #include "vtkCamera.h"
 18 #include "vtkAbstractPicker.h"
 19 #include "vtkPoints.h"
 20 #include "vtkObject.h"
 21 
 22 static vtkSmartPointer<vtkCellPicker> picker1;
 23 static vtkSmartPointer<vtkTextMapper> textMapper;
 24 static vtkSmartPointer<vtkActor2D> textActor;
 25 static vtkSmartPointer<vtkPoints> pickPos;
 26 static vtkSmartPointer<vtkRenderWindow> renWin;
 27 static double *selPt;
 28 static double x;
 29 static double y;
 30 static double xp;
 31 static double yp;
 32 static double zp;
 33 
 34 void myCallbackFunction(vtkObject* caller, long unsigned int eventId, void* clientData, void* callData)
 35 {
 36     picker1 = (vtkCellPicker*)clientData;
 37     if (picker1->GetCellId() < 0)
 38     {
 39         textActor->VisibilityOff();
 40     } 
 41     else
 42     {
 43         selPt = picker1->GetSelectionPoint();
 44         x = *selPt;
 45         y = *(selPt + 1);
 46         pickPos = picker1->GetPickedPositions();
 47         xp = *(pickPos->GetPoint(0));
 48         yp = *(pickPos->GetPoint(0)+1);
 49         zp = *(pickPos->GetPoint(0)+2);
 50         char str[50];
 51         sprintf_s(str, "%f,%f,%f", xp, yp, zp);
 52         textMapper->SetInput(str);
 53         textActor->SetPosition(x, y);
 54         textActor->VisibilityOn();
 55     }
 56 
 57     renWin->Render();
 58 }
 59 
 60 int main()
 61 {
 62     VTK_MODULE_INIT(vtkRenderingOpenGL2);
 63     VTK_MODULE_INIT(vtkRenderingFreeType);
 64     VTK_MODULE_INIT(vtkInteractionStyle);
 65 
 66     //# create a sphere source, mapper, and actor
 67     vtkSmartPointer<vtkSphereSource> sphere = vtkSmartPointer<vtkSphereSource>::New();
 68     vtkSmartPointer<vtkPolyDataMapper> sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
 69     sphereMapper->SetInputConnection(sphere->GetOutputPort());
 70     sphereMapper->GlobalImmediateModeRenderingOn();
 71     vtkSmartPointer<vtkLODActor> sphereActor = vtkSmartPointer<vtkLODActor>::New();
 72     sphereActor->SetMapper(sphereMapper);
 73 
 74     //# create the spikes by glyphing the sphere with a cone.Create the mapper
 75     //# and actor for the glyphs.
 76     vtkSmartPointer<vtkConeSource> cone = vtkSmartPointer<vtkConeSource>::New();
 77     vtkSmartPointer<vtkGlyph3D> glyph = vtkSmartPointer<vtkGlyph3D>::New();
 78     glyph->SetInputConnection(sphere->GetOutputPort());
 79     glyph->SetSourceConnection(cone->GetOutputPort());
 80     glyph->SetVectorModeToUseNormal();
 81     glyph->SetScaleModeToScaleByVector();
 82     glyph->SetScaleFactor(0.25);
 83     vtkSmartPointer<vtkPolyDataMapper> spikeMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
 84     spikeMapper->SetInputConnection(glyph->GetOutputPort());
 85     vtkSmartPointer<vtkLODActor> spikeActor = vtkSmartPointer<vtkLODActor>::New();
 86     spikeActor->SetMapper(spikeMapper);
 87 
 88     //# Create a cell picker.
 89     vtkSmartPointer<vtkCellPicker> picker = vtkSmartPointer<vtkCellPicker>::New();
 90     vtkSmartPointer<vtkCallbackCommand> annotatePick = vtkSmartPointer<vtkCallbackCommand>::New();
 91     annotatePick->SetCallback(myCallbackFunction);
 92     annotatePick->SetClientData(picker);
 93     picker->AddObserver(vtkCommand::EndPickEvent, annotatePick);
 94 
 95     //# Create a text mapper and actor to display the results of picking.
 96     textMapper = vtkSmartPointer<vtkTextMapper>::New();
 97     textMapper->GetTextProperty()->SetFontFamilyToArial();
 98     textMapper->GetTextProperty()->SetFontSize(10);
 99     textMapper->GetTextProperty()->BoldOn();
100     textMapper->GetTextProperty()->ShadowOn();
101     textMapper->GetTextProperty()->SetColor(1, 0, 0);
102     textActor = vtkSmartPointer<vtkActor2D>::New();
103     textActor->VisibilityOff();
104     textActor->SetMapper(textMapper);
105 
106     //Create the Renderer, RenderWindow, RenderWindowInteractor
107     vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer<vtkRenderer>::New();
108     renWin = vtkSmartPointer<vtkRenderWindow>::New();
109     renWin->AddRenderer(ren1);
110     vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
111     iren->SetRenderWindow(renWin);
112     iren->SetPicker(picker);
113 
114     //Add the actors to the renderer, set the background and size
115     ren1->AddActor2D(textActor);
116     ren1->AddActor(sphereActor);
117     ren1->AddActor(spikeActor);
118     ren1->SetBackground(1, 1, 1);
119     renWin->SetSize(300, 300);
120     ren1->ResetCamera();
121     ren1->GetActiveCamera()->Zoom(1.4);
122 
123     //# Pick the cell at this location.
124     picker->Pick(85.0, 126.0, 0, ren1);
125 
126     iren->Start();
127     return 0;
128 }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:NOIP2017 PJ全记录

下一篇:字符串变量的定义与引用