1 |
""" |
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 |
|
72 |
def rotateZ(self, angle): |
73 |
""" |
74 |
Rotate the plane along the z-axis. |
75 |
|
76 |
@type angle: Number |
77 |
@param angle: Angle to rotate the plane |
78 |
""" |
79 |
|
80 |
self.__vtk_transform.RotateZ(angle) |
81 |
|
82 |
def setPlaneToXY(self, offset = 0): |
83 |
""" |
84 |
Set the plane orthogonal to the z-axis. |
85 |
|
86 |
@type offset: Number |
87 |
@param offset: Amount to translate along the z-axis |
88 |
""" |
89 |
|
90 |
self.translate(0, 0, offset + self.__OFFSET_VARIANCE) |
91 |
|
92 |
def setPlaneToYZ(self, offset = 0): |
93 |
""" |
94 |
Set the plane orthogonal to the x-axis. |
95 |
|
96 |
@type offset: Number |
97 |
@param offset: Amount to translate along the x-axis |
98 |
""" |
99 |
|
100 |
# NOTE: rotateY must come first before translate. Otherwise, |
101 |
# the output may be incorrect. |
102 |
self.rotateY(90) |
103 |
self.translate(offset, 0, 0) |
104 |
|
105 |
def setPlaneToXZ(self, offset = 0): |
106 |
""" |
107 |
Set the plane orthogonal to the y-axis. |
108 |
|
109 |
@type offset: Number |
110 |
@param offset: Amount to translate along the y-axis |
111 |
""" |
112 |
|
113 |
# rotateX must come first before translate. Otherwise, it won't work. |
114 |
self.rotateX(90) |
115 |
self.translate(0, offset, 0) |
116 |
|
117 |
def _getTransform(self): |
118 |
""" |
119 |
Return the transform instance. |
120 |
|
121 |
@rtype: vtkTransform |
122 |
@return: Transform instance that is used to specify the orientation |
123 |
of the plane |
124 |
""" |
125 |
|
126 |
return self.__vtk_transform |
127 |
|
128 |
|
129 |
############################################################################### |
130 |
|
131 |
|
132 |
class TransformFilter: |
133 |
""" |
134 |
Class that defines a transform poly data filter. |
135 |
""" |
136 |
|
137 |
def __init__(self, plane_source, transform): |
138 |
""" |
139 |
Initialise the transoform poly data filter. |
140 |
|
141 |
@type plane_source: vtkPolyData |
142 |
@param plane_source: Polygonal data |
143 |
@type transform: L{Transform <transform.Transform>} object |
144 |
@param transform: Specifies the orientation of the plane source |
145 |
""" |
146 |
|
147 |
self.__plane_source = plane_source |
148 |
self.__transform = transform |
149 |
|
150 |
self.__vtk_transform_filter = vtk.vtkTransformPolyDataFilter() |
151 |
|
152 |
self._setInput() |
153 |
self._setTransform() |
154 |
|
155 |
def _setInput(self): |
156 |
""" |
157 |
Set the input for the transform poly data filter. |
158 |
""" |
159 |
|
160 |
self.__vtk_transform_filter.SetInput(self.__plane_source) |
161 |
|
162 |
def _setTransform(self): |
163 |
""" |
164 |
Set the transformation of the plane source. |
165 |
""" |
166 |
|
167 |
self.__vtk_transform_filter.SetTransform(self.__transform) |
168 |
|
169 |
def _getOutput(self): |
170 |
""" |
171 |
Return the output of the transform poly data filter. |
172 |
""" |
173 |
|
174 |
return self.__vtk_transform_filter.GetOutput() |
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|