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 |
|
12 |
def __init__(self): |
13 |
""" |
14 |
Initialise the transform object. |
15 |
""" |
16 |
|
17 |
# NOTE: VTK's values are not 100% accurate. Origin is not exaclty |
18 |
#(0,0,0) and normal is not exactly (0, 0, 1). There is a slight |
19 |
# variance. As a result, a slight alteration has to be done in order |
20 |
# for the plane to be displayed correctly. Otherwise, the |
21 |
# plane may just fall outside the bounding box and nothing |
22 |
# is displayed. |
23 |
self.__OFFSET_VARIANCE = 0.0000000001 |
24 |
self.__vtk_transform = vtk.vtkTransform() |
25 |
|
26 |
def translate(self, x_offset, y_offset, z_offset): |
27 |
""" |
28 |
Translate the rendered object along the x, y and z-axes. |
29 |
|
30 |
@type x_offset: Number |
31 |
@param x_offset: Amount to translate along the x-axis |
32 |
@type y_offset: Number |
33 |
@param y_offset: Amount to translate along the y-axis |
34 |
@type z_offset: Number |
35 |
@param z_offset: Amount to translate along the z-axis |
36 |
""" |
37 |
|
38 |
self.__vtk_transform.Translate(-x_offset, -y_offset, -z_offset) |
39 |
|
40 |
def rotateX(self, angle): |
41 |
""" |
42 |
Rotate the plane along the x-axis. |
43 |
|
44 |
@type angle: Number |
45 |
@param angle: Angle to rotate the camera |
46 |
""" |
47 |
|
48 |
self.__vtk_transform.RotateX(-angle) |
49 |
|
50 |
def rotateY(self, angle): |
51 |
""" |
52 |
Rotate the plane along the y-axis. |
53 |
|
54 |
@type angle: Number |
55 |
@param angle: Angle to rotate the camera |
56 |
""" |
57 |
|
58 |
self.__vtk_transform.RotateY(angle) |
59 |
|
60 |
|
61 |
def rotateZ(self, angle): |
62 |
""" |
63 |
Rotate the plane along the z-axis. |
64 |
|
65 |
@type angle: Number |
66 |
@param angle: Angle to rotate the camera |
67 |
""" |
68 |
|
69 |
self.__vtk_transform.RotateZ(angle) |
70 |
|
71 |
def setPlaneToXY(self, offset = 0): |
72 |
""" |
73 |
Set the plane orthogonal to the z-axis. |
74 |
|
75 |
@type offset: Number |
76 |
@param offset: Amount to translate along the z-axis |
77 |
""" |
78 |
|
79 |
self.translate(0, 0, offset + self.__OFFSET_VARIANCE) |
80 |
|
81 |
def setPlaneToYZ(self, offset = 0): |
82 |
""" |
83 |
Set the plane orthogonal to the x-axis. |
84 |
|
85 |
@type offset: Number |
86 |
@param offset: Amount to translate along the x-axis |
87 |
""" |
88 |
|
89 |
# NOTE: rotateY must come first before translate. Otherwise, |
90 |
# the output may be incorrect. |
91 |
self.rotateY(90) |
92 |
self.translate(offset, 0, 0) |
93 |
|
94 |
def setPlaneToXZ(self, offset = 0): |
95 |
""" |
96 |
Set the plane orthogonal to the y-axis. |
97 |
|
98 |
@type offset: Number |
99 |
@param offset: Amount to translate along the y-axis |
100 |
""" |
101 |
|
102 |
# rotateX must come first before translate. Otherwise, it won't work. |
103 |
self.rotateX(90) |
104 |
self.translate(0, offset, 0) |
105 |
|
106 |
def _getTransform(self): |
107 |
""" |
108 |
Return the transform instance. |
109 |
|
110 |
@rtype: vtkTransform |
111 |
@return: Transform instance that is used to specify the orientation |
112 |
of the plane |
113 |
""" |
114 |
|
115 |
return self.__vtk_transform |