1 #include <vtkAutoInit.h>
2 VTK_MODULE_INIT(vtkRenderingOpenGL)
3 VTK_MODULE_INIT(vtkInteractionStyle)
4 VTK_MODULE_INIT(vtkRenderingFreeType)
5
6 #include <vtkCommand.h>
7 #include <vtkSmartPointer.h>
8 #include <vtkJPEGReader.h>
9 #include <vtkImageActor.h>
10 #include <vtkRenderer.h>
11 #include <vtkRenderWindow.h>
12 #include <vtkRenderWindowInteractor.h>
13 #include <vtkInteractorStyleImage.h>
14
15 #include <vtkDistanceWidget.h>
16 #include <vtkDistanceRepresentation.h>
17 #include <vtkAngleWidget.h>
18 #include <vtkProperty2D.h>
19 #include <vtkLeaderActor2D.h>
20 #include <vtkAngleRepresentation2D.h>
21 #include <vtkBiDimensionalWidget.h>
22 #include <vtkBiDimensionalRepresentation2D.h>
23
24
25 class vtkBiDimensionalCallback: public vtkCommand
26 {
27 public:
28 static vtkBiDimensionalCallback* New()
29 {
30 return new vtkBiDimensionalCallback;
31 }
32
33 virtual void Execute(vtkObject* caller, unsigned long, void*)
34 {
35 vtkBiDimensionalWidget* biDimensionalWidget =
36 reinterpret_cast<vtkBiDimensionalWidget*> (caller);
37 vtkBiDimensionalRepresentation2D* representation =
38 static_cast<vtkBiDimensionalRepresentation2D*> (biDimensionalWidget->GetRepresentation());
39 double p1[3];
40 representation->GetPoint1DisplayPosition(p1);
41 double p2[3];
42 representation->GetPoint1DisplayPosition(p2);
43 double p3[3];
44 representation->GetPoint1DisplayPosition(p3);
45 double p4[3];
46 representation->GetPoint1DisplayPosition(p4);
47 //显示其中一个点的屏幕坐标(px)
48 std::cout << "P1: " << p1[0] << " " << p1[1] << " " << p1[2] << std::endl;
49 }
50 vtkBiDimensionalCallback() { }
51 };
52 int main()
53 {
54 int WidgetType;
55 std::cout << "Please select the Measurement Distance WidgetType: " << std::endl;
56 std::cin >> WidgetType;
57
58 vtkSmartPointer<vtkJPEGReader> reader = vtkSmartPointer<vtkJPEGReader>::New();
59 reader->SetFileName("vtk.jpg");
60 reader->Update();
61
62 vtkSmartPointer<vtkImageActor> imgActor = vtkSmartPointer<vtkImageActor>::New();
63 imgActor->SetInputData(reader->GetOutput());
64
65 vtkSmartPointer<vtkRenderer> render = vtkSmartPointer<vtkRenderer>::New();
66 render->AddActor(imgActor);
67 render->SetBackground(0, 0, 0);
68 render->ResetCamera();
69
70 vtkSmartPointer<vtkRenderWindow> rw = vtkSmartPointer<vtkRenderWindow>::New();
71 rw->AddRenderer(render);
72 rw->SetWindowName("MeasurementDistanceApp");
73 rw->SetSize(320, 320);
74 rw->Render();
75
76 vtkSmartPointer<vtkRenderWindowInteractor> rwi =
77 vtkSmartPointer<vtkRenderWindowInteractor>::New();
78 rwi->SetRenderWindow(rw);
79
80 vtkSmartPointer<vtkInteractorStyleImage> style =
81 vtkSmartPointer<vtkInteractorStyleImage>::New();
82 rwi->SetInteractorStyle(style);
83 /****************************************************************/
84 //vtkDistanceWidget
85 if (WidgetType == 0)
86 {
87 //实例化Widget
88 vtkSmartPointer<vtkDistanceWidget> distanceWidget =
89 vtkSmartPointer<vtkDistanceWidget>::New();
90 //指定渲染窗口交互器,来监听用户事件
91 distanceWidget->SetInteractor(rwi);
92 //必要时使用观察者/命令模式创建回调函数(此处没用)
93 //创建几何表达实体。用SetRepresentation()把事件与Widget关联起来
94 //或者使用Widget默认的几何表达实体
95 distanceWidget->CreateDefaultRepresentation();
96 static_cast<vtkDistanceRepresentation*> (distanceWidget->GetRepresentation())
97 ->SetLabelFormat("%-#6.3g px");
98 //激活Widget
99 distanceWidget->On();
100
101 rw->Render();
102 rwi->Initialize();
103 rwi->Start();
104 }
105 //vtkAngleWidget
106 if (WidgetType == 1)
107 {
108 vtkSmartPointer<vtkAngleWidget> angleWiget = vtkSmartPointer<vtkAngleWidget>::New();
109 angleWiget->SetInteractor(rwi);
110 //创建个性化的实体图标
111 vtkSmartPointer<vtkAngleRepresentation2D> angleRep =
112 vtkSmartPointer<vtkAngleRepresentation2D>::New();
113 angleRep->GetRay1()->GetProperty()->SetColor(0, 1, 0);
114 angleRep->GetRay1()->GetProperty()->SetLineWidth(3);
115 angleRep->GetRay2()->GetProperty()->SetColor(0, 1, 0);
116 angleRep->GetRay1()->GetProperty()->SetLineWidth(3);
117 angleRep->GetArc()->GetProperty()->SetColor(0, 1, 0);
118 angleRep->GetArc()->GetProperty()->SetLineWidth(3);
119 angleWiget->SetRepresentation(angleRep);
120 angleWiget->On();
121
122 rw->Render();
123 rwi->Initialize();
124 rwi->Start();
125 }
126 //vtkBiDimensionalWidget
127 if (WidgetType == 2)
128 {
129 vtkSmartPointer<vtkBiDimensionalWidget> bidimensionalWidget =
130 vtkSmartPointer<vtkBiDimensionalWidget>::New();
131 bidimensionalWidget->SetInteractor(rwi);
132 //采用默认的图标
133 bidimensionalWidget->CreateDefaultRepresentation();
134 //添加“观察者-命令模式(命令子类方案)”
135 vtkSmartPointer<vtkBiDimensionalCallback> bidiCallback =
136 vtkSmartPointer<vtkBiDimensionalCallback>::New();
137 bidimensionalWidget->AddObserver(vtkCommand::InteractionEvent, bidiCallback);
138 bidimensionalWidget->On();
139
140 rw->Render();
141 rwi->Initialize();
142 rwi->Start();
143 }
144 return 0;
145 }