1.标注类Widget
在可视化程序中,经常会对某个对象做一些标注说明,比如,在医学图像诊断中,常常会手动标注出被诊断为肿瘤的区域或者其他病变区域,并用文字进行标注。VTK中,与标注相关的Widget如下表所示:
- vtkTextWidget:在渲染场景中生成一串标识文本,可以随意调整该文本在渲染场景中的位置,缩放其大小等。
- vtkScalarBarWidget:根据输入的数据在渲染场景中生成一个标量条,通过设置颜色查找表,可以用标量条上的颜色来指示输入的数据。渲染场景中的标量条可以随意移动、改变大小、设置不同的方向等。
- vtkCaptionWidget:用一个带线框及箭头的文本信息来标注某一对象。
- vtkOrientationMarkerWidget:渲染场景中所渲染数据的方向指示标志。在医学图像领域有广泛的应用,比如,通过CT/MR等扫描的数据,当将其导入可视化应用程序时需要标识其上、下、左、右、前、后等方位。
- vtkBalloonWidget:当鼠标停留在渲染场景中的某个Actor一段时间后,会弹出提示信息。所提示的信息,除了可以用文本表示,也可以用图像表示。
2.标注类Widget应用程序
1 #include <vtkAutoInit.h>
2 VTK_MODULE_INIT(vtkRenderingOpenGL)
3 VTK_MODULE_INIT(vtkInteractionStyle)
4 VTK_MODULE_INIT(vtkRenderingFreeType)
5
6 #include <vtkSmartPointer.h>
7 #include <vtkUnstructuredGridReader.h>
8 #include <vtkUnstructuredGrid.h>
9 #include <vtkLookupTable.h>
10 #include <vtkDataSetMapper.h>
11 #include <vtkActor.h>
12 #include <vtkRenderer.h>
13 #include <vtkRenderWindow.h>
14 #include <vtkRenderWindowInteractor.h>
15
16 #include <vtkScalarBarActor.h>
17 #include <vtkScalarBarWidget.h>
18 #include <vtkTextActor.h>
19 #include <vtkTextWidget.h>
20 #include <vtkTextProperty.h>
21 #include <vtkTextRepresentation.h>
22 #include <vtkAxesActor.h>
23 #include <vtkOrientationMarkerWidget.h>
24 #include <vtkCaptionWidget.h>
25 #include <vtkCaptionActor2D.h>///
26 #include <vtkCaptionRepresentation.h>
27 #include <vtkBalloonWidget.h>
28 #include <vtkBalloonRepresentation.h>
29
30 int main()
31 {
32 vtkSmartPointer< vtkUnstructuredGridReader > reader = vtkSmartPointer< vtkUnstructuredGridReader >::New();
33 reader->SetFileName("data.vtk");
34 reader->Update();
35
36 vtkSmartPointer< vtkLookupTable > lut = vtkSmartPointer< vtkLookupTable >::New();
37 lut->Build();
38
39 vtkSmartPointer< vtkDataSetMapper > mapper = vtkSmartPointer< vtkDataSetMapper >::New();
40 mapper->SetInputData(reader->GetOutput());
41 mapper->SetScalarRange(reader->GetOutput()->GetScalarRange());
42 mapper->SetLookupTable(lut);
43
44 vtkSmartPointer< vtkActor > actor = vtkSmartPointer< vtkActor >::New();
45 actor->SetMapper(mapper);
46
47 vtkSmartPointer< vtkRenderer > renderer = vtkSmartPointer< vtkRenderer >::New();
48 renderer->AddActor(actor);
49 renderer->SetBackground(1, 1, 1);
50
51 vtkSmartPointer< vtkRenderWindow > renderWindow = vtkSmartPointer< vtkRenderWindow >::New();
52 renderWindow->AddRenderer(renderer);
53 renderWindow->Render();
54 renderWindow->SetWindowName("AnnotationWidget");
55 renderWindow->SetSize(400, 400);
56
57 vtkSmartPointer< vtkRenderWindowInteractor > interactor =
58 vtkSmartPointer< vtkRenderWindowInteractor >::New();
59 interactor->SetRenderWindow(renderWindow);
60 /********************************************************************************************/
61 //标注类测试
62 vtkScalarBarWidget
63 vtkSmartPointer< vtkScalarBarActor > scalarBarActor = vtkSmartPointer< vtkScalarBarActor >::New();
64 scalarBarActor->SetOrientationToHorizontal();
65 scalarBarActor->SetLookupTable(lut);
66
67 vtkSmartPointer< vtkScalarBarWidget > scalarBarWidget = vtkSmartPointer< vtkScalarBarWidget >::New();
68 scalarBarWidget->SetInteractor(interactor);
69 scalarBarWidget->SetScalarBarActor(scalarBarActor);
70 scalarBarWidget->On();
71
72 vtkTextWidget
73 vtkSmartPointer<vtkTextActor> textActor = vtkSmartPointer<vtkTextActor>::New();
74 textActor->SetInput("VTK Widgets");
75 textActor->GetTextProperty()->SetColor(1, 0, 0);
76
77 vtkSmartPointer<vtkTextWidget> textWidget = vtkSmartPointer<vtkTextWidget>::New();
78 textWidget->SetInteractor(interactor);
79 textWidget->SetTextActor(textActor);
80
81 vtkSmartPointer<vtkTextRepresentation> textRepresentation =
82 vtkSmartPointer<vtkTextRepresentation>::New();
83 textRepresentation->GetPositionCoordinate()->SetValue(0.15, 0.15);
84 textRepresentation->GetPosition2Coordinate()->SetValue(0.7, 0.2);
85
86 textWidget->SetRepresentation(textRepresentation);
87 textWidget->SelectableOff();
88 textWidget->On();
89
90 / vtkOrientationMarkerWidget
91 vtkSmartPointer<vtkAxesActor> iconActor = vtkSmartPointer<vtkAxesActor>::New();
92 vtkSmartPointer<vtkOrientationMarkerWidget> orientationWidget =
93 vtkSmartPointer<vtkOrientationMarkerWidget>::New();
94 orientationWidget->SetOutlineColor(0.9300, 0.5700, 0.1300);
95 orientationWidget->SetInteractor(interactor);
96 orientationWidget->SetOrientationMarker(iconActor);
97 orientationWidget->SetViewport(0.0, 0.0, 0.2, 0.2);
98 orientationWidget->SetEnabled(1);
99 orientationWidget->InteractiveOn();
100
101 vtkCaptionWidget
102 //vtkSmartPointer<vtkCaptionWidget> captionWidget =
103 // vtkSmartPointer<vtkCaptionWidget>::New();
104 //captionWidget->SetInteractor(interactor);
105
106 //vtkSmartPointer<vtkCaptionRepresentation> captionRepresentation =
107 // vtkSmartPointer<vtkCaptionRepresentation>::New();
108 //captionRepresentation->GetCaptionActor2D()->SetCaption("Caption Widget");
109 //captionRepresentation->GetCaptionActor2D()->GetTextActor()->GetTextProperty()->SetFontSize(20);
110 //
111 //double pos[3] = { .5, 0, 0 };
112 //captionRepresentation->SetAnchorPosition(pos);
113 //captionWidget->SetRepresentation(captionRepresentation);
114 //captionWidget->On();
115
116 / vtkBalloonWidget
117 vtkSmartPointer<vtkBalloonWidget> balloonWidget =
118 vtkSmartPointer<vtkBalloonWidget>::New();
119 balloonWidget->SetInteractor(interactor);
120
121 vtkSmartPointer<vtkBalloonRepresentation> balloonRep =
122 vtkSmartPointer<vtkBalloonRepresentation>::New();
123 balloonRep->SetBalloonLayoutToImageRight();
124
125 balloonWidget->SetRepresentation(balloonRep);
126 balloonWidget->AddBalloon(actor, "This is a widget example", NULL);
127 balloonWidget->On();
128
129 renderWindow->Render();
130 interactor->Initialize();
131 interactor->Start();
132 return 0;
133 }
输出结果如下:
使用标注类Widget需要注意的是,除了指定的Widget表达实体之外,某些Widget还需要与其他Actor协同使用;如上例中的vtkScalarBarWidget要与vtkScalarBarActor协同工作;vtkTextWidget要与vtkTextActor协同工作。