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協同工作。