我是靠谱客的博主 等待音响,这篇文章主要介绍【VTK学习】空间几何变换,现在分享给大家,希望可以做个参考。

https://blog.csdn.net/minmindianzi/article/details/84279434

VTK相关的类有:

vtkTransform, vtkTransformFilter, vtkMatrix4x4

相关的方法有:

• RotateX(angle)、RotateY(angle)、RotateZ(angle)

• RotateWXYZ(angle,x,y,z)

• Scale(x,y,z)

• Translate(x,y,z)

• SetMatrix(m)、GetMatrix(m)

• PostMultiply()、PreMultiply()

• SetPosition(x,y,z)、GetPosition(x,y,z)

• SetOrientation(x,y,z)、 GetOrientation(x,y,z)

• SetScale(sx,sy,sz)

• SetOrigin(x,y,z)、GetOrigin

 

空间变换类 

复制代码
1
2
vtkSmartPointer<vtkTransform> trans = vtkSmartPointer<vtkTransform>::New();

 在VTK中,矩阵乘法默认是左乘(PreMultiply),在齐次矩阵符号中,M = M*A,其中M为当前变换矩阵,A为要应用矩阵(比如旋转矩阵或者平移矩阵)。

复制代码
1
trans->PreMultiply();//M=M*A

源码中是这样解释的: vtkTransform.h的156行

    * Sets the internal state of the transform to PreMultiply. All subsequent
   * operations will occur before those already represented in the
   * current transformation.  In homogeneous matrix notation, M = M*A where
   * M is the current transformation matrix and A is the applied matrix.
   * The default is PreMultiply.

A就是指“All subsequent operations”,M就是指“those already represented in the current transformation”

我对before的理解就是A左乘M(A的左边乘上M)

 

当然,也可以设置矩阵乘法为右乘(PostMultiply),在齐次矩阵符号中,M = A*M,其中M为当前变换矩阵,A为要应用矩阵(比如旋转矩阵或者平移矩阵)。

复制代码
1
trans->PostMultiply();//M=A*M

源码中是这样写的:  

   * Sets the internal state of the transform to PostMultiply. All subsequent
   * operations will occur after those already represented in the
   * current transformation.  In homogeneous matrix notation, M = A*M where
   * M is the current transformation matrix and A is the applied matrix.
   * The default is PreMultiply.

我对after的理解就是A右乘M(A的右边乘上M)

 

实例分析:

三维物体的初始齐次矩阵为:

 

矩阵右乘(post后置),先旋转再平移

复制代码
1
2
3
trans->PostMultiply();//M=A*M trans->RotateZ(30); trans->Translate(1, 0, 0);

 

 

矩阵右乘(post后置),先平移再旋转

复制代码
1
2
3
4
trans->PostMultiply();//M=A*M trans->Translate(1, 0, 0); trans->RotateZ(30);

 

矩阵左乘(pre前置),先旋转再平移

复制代码
1
2
3
trans->PreMultiply();//M=M*A trans->RotateZ(30); trans->Translate(1, 0, 0);

 

 

矩阵左乘(pre前置),先平移再旋转

复制代码
1
2
3
trans->PreMultiply();//M=M*A trans->Translate(1, 0, 0); trans->RotateZ(30);

 

复制代码
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <vtkLineSource.h> #include <vtkPolyData.h> #include <vtkSmartPointer.h> #include <vtkPolyDataMapper.h> #include <vtkActor.h> #include <vtkRenderWindow.h> #include <vtkRenderer.h> #include <vtkRenderWindowInteractor.h> #include <vtkProperty.h> #include <vtkAxesActor.h> #include <vtkConeSource.h> #include <vtkTextActor.h> #include <vtkTextProperty.h> #include <vtkTransform.h> #include <vtkSphereSource.h> #include "vtkAutoInit.h" VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2 VTK_MODULE_INIT(vtkInteractionStyle); VTK_MODULE_INIT(vtkRenderingFreeType) int main(int, char *[]) { vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New(); sphereSource->SetRadius(0.1);//设置半径 sphereSource->Update(); vtkSmartPointer<vtkPolyDataMapper> sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New(); sphereMapper->SetInputConnection(sphereSource->GetOutputPort()); vtkSmartPointer<vtkActor> sphereActor = vtkSmartPointer<vtkActor>::New(); sphereActor->SetPosition(0, 0, 0);//设置演员的位置 sphereActor->SetMapper(sphereMapper);//设置演员的映射器 sphereActor->GetProperty()->SetColor(1, 0, 0);//设置演员的颜色 vtkSmartPointer<vtkConeSource> coneSource = vtkSmartPointer<vtkConeSource>::New(); coneSource->SetRadius(.2); coneSource->SetHeight(.5); coneSource->SetCenter(0, 0, 0); vtkSmartPointer<vtkPolyDataMapper> coneMapper = vtkSmartPointer<vtkPolyDataMapper>::New(); coneMapper->SetInputConnection(coneSource->GetOutputPort()); vtkSmartPointer<vtkActor> coneActor = vtkSmartPointer<vtkActor>::New(); coneActor->SetMapper(coneMapper); vtkSmartPointer<vtkActor> oriConeActor = vtkSmartPointer<vtkActor>::New(); oriConeActor->SetMapper(coneMapper); #define AXIS_LEN 1. vtkSmartPointer<vtkAxesActor> oriAxesActor = vtkSmartPointer<vtkAxesActor>::New(); oriAxesActor->SetPosition(0, 0, 0); oriAxesActor->SetTotalLength(AXIS_LEN, AXIS_LEN, AXIS_LEN); oriAxesActor->SetShaftType(0); oriAxesActor->SetAxisLabels(0); oriAxesActor->SetCylinderRadius(0.02); vtkSmartPointer<vtkAxesActor> axesActor = vtkSmartPointer<vtkAxesActor>::New(); axesActor->SetPosition(0, 0, 0); axesActor->SetTotalLength(AXIS_LEN, AXIS_LEN, AXIS_LEN); axesActor->SetShaftType(0); axesActor->SetAxisLabels(0); axesActor->SetCylinderRadius(0.02); vtkSmartPointer<vtkTextActor> textActor = vtkSmartPointer<vtkTextActor>::New(); textActor->SetPosition2(100, 40); textActor->GetTextProperty()->SetFontSize(24); textActor->GetTextProperty()->SetColor(1, 0, 0); vtkSmartPointer<vtkTransform> trans = vtkSmartPointer<vtkTransform>::New(); //trans->PostMultiply();//M=A*M trans->PreMultiply();//M=M*A trans->RotateZ(30); //coneActor->SetPosition(1, 0, 0);永远M=M*A trans->Translate(1, 0, 0); //trans->RotateZ(30); //trans->Translate(1, 0, 0); //trans->RotateZ(30); coneActor->SetUserTransform(trans); textActor->SetInput("PostMultiply()nTranslate(1, 0, 0)nRotateZ(30)"); cout << "GetMatrix:" << endl; if (coneActor->GetMatrix() != NULL) { coneActor->GetMatrix()->Print(cout); } else { cout << "NULL" << endl; } cout << "GetUserMatrix:" << endl; if (coneActor->GetUserMatrix() != NULL) { coneActor->GetUserMatrix()->Print(cout); } else { cout << "NULL" << endl; } vtkSmartPointer<vtkRenderer> renderer1 = vtkSmartPointer<vtkRenderer>::New(); vtkSmartPointer<vtkRenderer> renderer2 = vtkSmartPointer<vtkRenderer>::New(); vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->SetSize(800, 400); renderWindow->AddRenderer(renderer1); renderWindow->AddRenderer(renderer2); vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); renderWindowInteractor->SetRenderWindow(renderWindow); double leftViewport[] = { 0.0, 0.0, 0.5, 1.0 }; double rightViewport[] = { 0.5, 0.0, 1.0, 1.0 }; renderer1->AddActor(oriAxesActor); renderer1->AddActor(sphereActor); renderer1->AddActor(oriConeActor); renderer2->AddActor(axesActor); renderer2->AddActor(sphereActor); renderer2->AddActor(coneActor); renderer2->AddActor2D(textActor); renderer1->SetBackground(.3, .3, .5); renderer2->SetBackground(.2, .4, .5); renderer1->SetViewport(leftViewport); renderer2->SetViewport(rightViewport); renderWindow->Render(); renderWindowInteractor->Start(); return EXIT_SUCCESS; }

 

最后

以上就是等待音响最近收集整理的关于【VTK学习】空间几何变换的全部内容,更多相关【VTK学习】空间几何变换内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(98)

评论列表共有 0 条评论

立即
投稿
返回
顶部