VTK笔记——Widget简介

很多时候,3D模型软件需要和用户进行交互,比如按钮单击,鼠标移动,滚轮缩放,快捷键控制等等。在VTK中,widget可以为这些复杂的交互提供便利。比如,vtkBoxWidget2 允许你在屏幕上移动box,它提供了平移box,旋转和缩放这个box的操作。当然,你会说,这些可以用vtkCubeSource 和指定的vtkInteractorStyle来实现,没错,但相比而言,widget以一种更一般,更便利和更独立的方式提供了所有的这些操作。

widget包含两个重要的组成部分:InteractionRepresentation.

Interaction

Interaction是一些名叫vtk*Widget的类(比如vtkBoxWidget2)。它包含了交互的所有选项和事件处理。

Representation

Representation是显示并与之交互的一类对象,以名叫vtk*Representation.

用法

创建一个widget:

1
2
3
vtkSmartPointer<vtkBoxWidget2> boxWidget =
vtkSmartPointer<vtkBoxWidget2>::New();
boxWidget->SetInteractor( renderWindowInteractor );

大多数widget都有默认的representation。如果你想改变它,可以手动创建一个representation并设置到widget.

1
2
3
4
5
 vtkSmartPointer<vtkBoxRepresentation> boxRep =
vtkSmartPointer<vtkBoxRepresentation>::New();
boxRep->SetPlaceFactor(1);
boxRep->PlaceWidget(actor->GetBounds());
boxWidget->SetRepresentation(boxRep);

事件处理

往往你希望响应用户在widget上所作的操作,那这时候就需要定义一个vtkCommand的子类,并在一个类似于vtkCallbackFunction回调函数的方法Execute中来处理这些操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class vtkBoxCallback : public vtkCommand
{
public:
static vtkBoxCallback *New()
{
return new vtkBoxCallback;
}

vtkSmartPointer<vtkActor> m_actor;

void SetActor(vtkSmartPointer<vtkActor> actor)
{
m_actor = actor;
}

virtual void Execute(vtkObject *caller, unsigned long, void*)
{
vtkSmartPointer<vtkBoxWidget2> boxWidget =
dynamic_cast<vtkBoxWidget2*>(caller);

vtkSmartPointer<vtkTransform> t =
vtkSmartPointer<vtkTransform>::New();

dynamic_cast<vtkBoxRepresentation*>(boxWidget->GetRepresentation())->GetTransform(t);
this->m_actor->SetUserTransform(t);
}

vtkBoxCallback(){}
};

绑定事件

将自定义的事件添加到widget上,需要创建vtkCommand对象并调用widget的AddObserver方法。

1
2
3
4
vtkSmartPointer<vtkBoxCallback> boxCallback =
vtkSmartPointer<vtkBoxCallback>::New();
boxCallback->SetActor(actor);
boxWidget->AddObserver(vtkCommand::InteractionEvent, boxCallback);

启用Widget

使用Widget时,一定要注意顺序:

1
2
3
boxWidget->On();

renderWindowInteractor->Start();

效果

在这里插入图片描述

示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <vtkSmartPointer.h>
#include <vtkConeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkBoxWidget2.h>
#include <vtkBoxRepresentation.h>
#include <vtkCommand.h>
#include <vtkTransform.h>

class vtkBoxCallback : public vtkCommand
{
public:
static vtkBoxCallback *New()
{
return new vtkBoxCallback;
}

vtkSmartPointer<vtkActor> m_actor;

void SetActor(vtkSmartPointer<vtkActor> actor)
{
m_actor = actor;
}

virtual void Execute(vtkObject *caller, unsigned long, void*)
{
vtkSmartPointer<vtkBoxWidget2> boxWidget =
dynamic_cast<vtkBoxWidget2*>(caller);

vtkSmartPointer<vtkTransform> t =
vtkSmartPointer<vtkTransform>::New();

dynamic_cast<vtkBoxRepresentation*>(boxWidget->GetRepresentation())->GetTransform(t);
this->m_actor->SetUserTransform(t);
}

vtkBoxCallback(){}
};

int main(int argc, char* argv[])
{
vtkSmartPointer<vtkConeSource> coneSource =
vtkSmartPointer<vtkConeSource>::New();
coneSource->SetHeight(1.5);

vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(coneSource->GetOutputPort());

vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);

vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
renderer->ResetCamera();

vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);

vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);

vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
renderWindowInteractor->SetInteractorStyle(style);

vtkSmartPointer<vtkBoxWidget2> boxWidget =
vtkSmartPointer<vtkBoxWidget2>::New();
boxWidget->SetInteractor(renderWindowInteractor);

vtkSmartPointer<vtkBoxRepresentation> boxRep =
vtkSmartPointer<vtkBoxRepresentation>::New();
boxRep->SetPlaceFactor(1);
boxRep->PlaceWidget(actor->GetBounds());
boxWidget->SetRepresentation(boxRep);

vtkSmartPointer<vtkBoxCallback> boxCallback =
vtkSmartPointer<vtkBoxCallback>::New();
boxCallback->SetActor(actor);
boxWidget->AddObserver(vtkCommand::InteractionEvent, boxCallback);

boxWidget->On();

renderWindowInteractor->Start();

return 0;
}

Ref

VTK Widgets Tutorials
VTKExamples-Widgets
在这里插入图片描述
在这里插入图片描述