1 |
ksteube |
1147 |
""" |
2 |
|
|
@author: John NGUI |
3 |
|
|
""" |
4 |
|
|
|
5 |
|
|
import vtk |
6 |
|
|
|
7 |
|
|
class Transform: |
8 |
|
|
""" |
9 |
|
|
Class that defines the orientation of planes. |
10 |
|
|
|
11 |
|
|
@attention: There is a difference between performing rotation first |
12 |
|
|
followed by translation, and performing translation first followed |
13 |
|
|
by rotation. |
14 |
|
|
|
15 |
|
|
@attention: VTK's coordinate system and translation is NOT 100% accurate. |
16 |
|
|
Consequently, performing maximum rotation and translation can potentially |
17 |
|
|
yield incorrect results. For instance, rotating a XY plane along the x-axis |
18 |
|
|
90 degrees may NOT produce any results (as it is possible that the XY |
19 |
|
|
plane has just fallen outside the visible range). However, rotating the |
20 |
|
|
XY plane 89.9 degrees instead should produce the correct resutls. |
21 |
|
|
""" |
22 |
|
|
|
23 |
|
|
def __init__(self): |
24 |
|
|
""" |
25 |
|
|
Initialise the transform object. |
26 |
|
|
""" |
27 |
|
|
|
28 |
|
|
# NOTE: VTK's coordinates are not 100% accurate. The origin is not |
29 |
|
|
# exaclty (0,0,0) and the normal is not exactly (0, 0, 1). There is a |
30 |
|
|
# slight variance. As a result, a slight alteration has to be done |
31 |
|
|
# in order for the plane to be displayed correctly. Otherwise, the |
32 |
|
|
# plane may just fall outside the bounding box and nothing |
33 |
|
|
# is displayed. |
34 |
|
|
self.__OFFSET_VARIANCE = 0.0000000001 |
35 |
|
|
self.__vtk_transform = vtk.vtkTransform() |
36 |
|
|
|
37 |
|
|
def translate(self, x_offset, y_offset, z_offset): |
38 |
|
|
""" |
39 |
|
|
Translate the rendered object along the x, y and z-axes. |
40 |
|
|
|
41 |
|
|
@type x_offset: Number |
42 |
|
|
@param x_offset: Amount to translate along the x-axis |
43 |
|
|
@type y_offset: Number |
44 |
|
|
@param y_offset: Amount to translate along the y-axis |
45 |
|
|
@type z_offset: Number |
46 |
|
|
@param z_offset: Amount to translate along the z-axis |
47 |
|
|
""" |
48 |
|
|
|
49 |
|
|
self.__vtk_transform.Translate(-x_offset, -y_offset, -z_offset) |
50 |
|
|
|
51 |
|
|
def rotateX(self, angle): |
52 |
|
|
""" |
53 |
|
|
Rotate the plane along the x-axis. |
54 |
|
|
|
55 |
|
|
@type angle: Number |
56 |
|
|
@param angle: Angle to rotate the plane |
57 |
|
|
""" |
58 |
|
|
|
59 |
|
|
self.__vtk_transform.RotateX(-angle) |
60 |
|
|
|
61 |
|
|
def rotateY(self, angle): |
62 |
|
|
""" |
63 |
|
|
Rotate the plane along the y-axis. |
64 |
|
|
|
65 |
|
|
@type angle: Number |
66 |
|
|
@param angle: Angle to rotate the plane |
67 |
|
|
""" |
68 |
|
|
|
69 |
|
|
self.__vtk_transform.RotateY(angle) |
70 |
|
|
|
71 |
|
|
def rotateZ(self, angle): |
72 |
|
|
""" |
73 |
|
|
Rotate the plane along the z-axis. |
74 |
|
|
|
75 |
|
|
@type angle: Number |
76 |
|
|
@param angle: Angle to rotate the plane |
77 |
|
|
""" |
78 |
|
|
|
79 |
|
|
self.__vtk_transform.RotateZ(angle) |
80 |
|
|
|
81 |
|
|
def setPlaneToXY(self, offset = 0): |
82 |
|
|
""" |
83 |
|
|
Set the plane orthogonal to the z-axis. |
84 |
|
|
|
85 |
|
|
@type offset: Number |
86 |
|
|
@param offset: Amount to translate along the z-axis |
87 |
|
|
""" |
88 |
|
|
|
89 |
|
|
self.translate(0, 0, offset + self.__OFFSET_VARIANCE) |
90 |
|
|
|
91 |
|
|
def setPlaneToYZ(self, offset = 0): |
92 |
|
|
""" |
93 |
|
|
Set the plane orthogonal to the x-axis. |
94 |
|
|
|
95 |
|
|
@type offset: Number |
96 |
|
|
@param offset: Amount to translate along the x-axis |
97 |
|
|
""" |
98 |
|
|
|
99 |
|
|
# NOTE: rotateY must come first before translate. Otherwise, |
100 |
|
|
# the output may be incorrect. |
101 |
|
|
self.rotateY(90) |
102 |
|
|
self.translate(offset, 0, 0) |
103 |
|
|
|
104 |
|
|
def setPlaneToXZ(self, offset = 0): |
105 |
|
|
""" |
106 |
|
|
Set the plane orthogonal to the y-axis. |
107 |
|
|
|
108 |
|
|
@type offset: Number |
109 |
|
|
@param offset: Amount to translate along the y-axis |
110 |
|
|
""" |
111 |
|
|
|
112 |
|
|
# rotateX must come first before translate. Otherwise, it won't work. |
113 |
|
|
self.rotateX(90) |
114 |
|
|
self.translate(0, offset, 0) |
115 |
|
|
|
116 |
|
|
def _getTransform(self): |
117 |
|
|
""" |
118 |
|
|
Return the transform instance. |
119 |
|
|
|
120 |
|
|
@rtype: vtkTransform |
121 |
|
|
@return: Transform instance that is used to specify the orientation |
122 |
|
|
of the plane |
123 |
|
|
""" |
124 |
|
|
|
125 |
|
|
return self.__vtk_transform |
126 |
|
|
|
127 |
|
|
|
128 |
|
|
############################################################################### |
129 |
|
|
|
130 |
|
|
|
131 |
|
|
class TransformFilter: |
132 |
|
|
""" |
133 |
|
|
Class that defines a transform poly data filter. |
134 |
|
|
""" |
135 |
|
|
|
136 |
jongui |
1148 |
def __init__(self): |
137 |
ksteube |
1147 |
""" |
138 |
|
|
Initialise the transoform poly data filter. |
139 |
jongui |
1148 |
""" |
140 |
ksteube |
1147 |
|
141 |
jongui |
1148 |
self.__vtk_transform_filter = vtk.vtkTransformPolyDataFilter() |
142 |
|
|
|
143 |
|
|
def _setupTransformFilter(self, plane_source, transform): |
144 |
|
|
""" |
145 |
|
|
Setup the transform filter. |
146 |
|
|
|
147 |
ksteube |
1147 |
@type plane_source: vtkPolyData |
148 |
|
|
@param plane_source: Polygonal data |
149 |
|
|
@type transform: L{Transform <transform.Transform>} object |
150 |
|
|
@param transform: Specifies the orientation of the plane source |
151 |
|
|
""" |
152 |
|
|
|
153 |
|
|
self.__plane_source = plane_source |
154 |
|
|
self.__transform = transform |
155 |
|
|
|
156 |
jongui |
1148 |
self.__setInput() |
157 |
|
|
self.__setTransform() |
158 |
ksteube |
1147 |
|
159 |
jongui |
1148 |
def __setInput(self): |
160 |
ksteube |
1147 |
""" |
161 |
|
|
Set the input for the transform poly data filter. |
162 |
|
|
""" |
163 |
|
|
|
164 |
|
|
self.__vtk_transform_filter.SetInput(self.__plane_source) |
165 |
|
|
|
166 |
jongui |
1148 |
def __setTransform(self): |
167 |
ksteube |
1147 |
""" |
168 |
|
|
Set the transformation of the plane source. |
169 |
|
|
""" |
170 |
|
|
|
171 |
|
|
self.__vtk_transform_filter.SetTransform(self.__transform) |
172 |
|
|
|
173 |
jongui |
1148 |
def _getTransformFilterOutput(self): |
174 |
ksteube |
1147 |
""" |
175 |
|
|
Return the output of the transform poly data filter. |
176 |
|
|
""" |
177 |
|
|
|
178 |
|
|
return self.__vtk_transform_filter.GetOutput() |
179 |
|
|
|
180 |
|
|
|
181 |
|
|
|
182 |
|
|
|
183 |
|
|
|
184 |
|
|
|