VTK交互窗口
交互部件
VTK的Widget类主要包括vtk3DWidget和vtkAbstractWidget两个父类,它们都派生自vtkInteractorObserver。其中vtk3DWidget主要在三维渲染场景中生成一个可以用于控制数据的可视化实体,比如点、曲线、平面、球体等;vtkAbstractWidget是所有窗体的基类。
vtkAbstractWidget作为基类,只定义一些公共的API以及实现了“交互/表达实体”分离的设计机制,其中,把从vtkRenderWindowInteractor传递过来的信息交给vtkAbstractWidget的“交互”部分处理,而Widget的“表达实体”则对应一个vtkProp对象(或vtkWidgetRepresenttation的子类)。
此外,vtkAbstractWidget类提供了访问vtkWidgetEventTranslator对象的函数,即GetEventTranslator(),该对象的作用可以将VTK事件映射为Widget事件,通过vtkWidgetEventTranslator类,用户可以定制符合自己使用习惯的控制Widget的事件绑定。利用类vtkWidgetCallbackMapper将相应的Widget事件与各个受保护的静态操作函数关联起来。
vtkWidgetEventTranslator* eventTranslator = widget->GetEventTranslator();
eventTranslator->SetTranslation(vtkCommand::RightButtonPressEvent, vtkWidgetEvent::Select);
eventTranslator->SetTranslation(vtkCommand::RightButtonReleaseEvent, vtkWidgetEvent::EndSelect);
创建交互窗体步骤
窗体的创建以及使用基本都是类似的,如下。
- 实例化Widget
- 指定渲染窗口交互器。Widget可以通过它监听用户事件。
- 必要时使用观察者/命令模式创建回调函数。
- 创建合适的几何表达实体,并用SetRepresentation()函数把它与Widget关联起来,或者使用Widget默认的几何表达实体。
- 最后必须激活Widget,使其在渲染场景中显示。
static void CreateImage(vtkSmartPointer<vtkImageData> image,unsigned char *color1,unsigned char *color2);
int main(int, char *[])
{
// Create two images for texture
vtkSmartPointer<vtkImageData> image1 =vtkSmartPointer<vtkImageData>::New();
vtkSmartPointer<vtkImageData> image2 =vtkSmartPointer<vtkImageData>::New();
unsigned char banana[3] = { 227, 207, 87 };
unsigned char tomato[3] = { 255, 99, 71 };
CreateImage(image1, banana, tomato);
CreateImage(image2, tomato, banana);
// Create some geometry
vtkSmartPointer<vtkSphereSource> sphereSource =vtkSmartPointer<vtkSphereSource>::New();
sphereSource->Update();
vtkSmartPointer<vtkPolyDataMapper> mapper =vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(sphereSource->GetOutputPort());
vtkSmartPointer<vtkActor> actor =vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
// A renderer and render window
vtkSmartPointer<vtkRenderer> renderer =vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
// An interactor
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// Create the widget and its representation
vtkSmartPointer<vtkTexturedButtonRepresentation2D> buttonRepresentation =vtkSmartPointer<vtkTexturedButtonRepresentation2D>::New();
buttonRepresentation->SetNumberOfStates(2);
buttonRepresentation->SetButtonTexture(0, image1);
buttonRepresentation->SetButtonTexture(1, image2);
vtkSmartPointer<vtkButtonWidget> buttonWidget =vtkSmartPointer<vtkButtonWidget>::New();
buttonWidget->SetInteractor(renderWindowInteractor);
buttonWidget->SetRepresentation(buttonRepresentation);
// Add the actors to the scene
renderer->AddActor(actor);
renderer->SetBackground(.1, .2, .5);
renderWindow->SetSize(640, 480);
renderWindow->Render();
// Place the widget. Must be done after a render so that the
// viewport is defined.
// Here the widget placement is in normalized display coordinates
vtkSmartPointer<vtkCoordinate> upperRight =vtkSmartPointer<vtkCoordinate>::New();
upperRight->SetCoordinateSystemToNormalizedDisplay();
upperRight->SetValue(1.0, 1.0);
double bds[6];
double sz = 50.0;
bds[0] = upperRight->GetComputedDisplayValue(renderer)[0] - sz;
bds[1] = bds[0] + sz;
bds[2] = upperRight->GetComputedDisplayValue(renderer)[1] - sz;
bds[3] = bds[2] + sz;
bds[4] = bds[5] = 0.0;
// Scale to 1, default is .5
buttonRepresentation->SetPlaceFactor(1);
buttonRepresentation->PlaceWidget(bds);
buttonWidget->On();
// Begin mouse interaction
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
void CreateImage(vtkSmartPointer<vtkImageData> image,unsigned char* color1, unsigned char* color2)
{
// Specify the size of the image data
image->SetDimensions(10, 10, 1);
#if VTK_MAJOR_VERSION <= 5
image->SetNumberOfScalarComponents(3);
image->SetScalarTypeToUnsignedChar();
#else
image->AllocateScalars(VTK_UNSIGNED_CHAR, 3);
#endif
int* dims = image->GetDimensions();
// Fill the image with
for (int y = 0; y < dims[1]; y++) {
for (int x = 0; x < dims[0]; x++){
unsigned char* pixel =static_cast<unsigned char*>(image->GetScalarPointer(x, y, 0));
if (x < 5) {
pixel[0] = color1[0];
pixel[1] = color1[1];
pixel[2] = color1[2];
}
else{
pixel[0] = color2[0];
pixel[1] = color2[1];
pixel[2] = color2[2];
}
}
}
}