1 |
""" |
2 |
@author: John NGUI |
3 |
""" |
4 |
|
5 |
import vtk |
6 |
|
7 |
class Transform: |
8 |
""" |
9 |
Class that defines the orientation of rendered objects. |
10 |
""" |
11 |
|
12 |
def __init__(self): |
13 |
""" |
14 |
Initialise the transform object. |
15 |
""" |
16 |
|
17 |
# NOTE: VTK's values are not accurate. Origin is not exaclty (0,0,0) |
18 |
# 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 rendered object to be displayed correctly. Otherwise, the |
21 |
# rendered object may just fall outside the bounding box and nothing |
22 |
# is displayed. |
23 |
|
24 |
self.__OFFSET_VARIANCE = 0.0000000001 |
25 |
self.__vtk_transform = vtk.vtkTransform() |
26 |
|
27 |
def translate(self, x_offset, y_offset, z_offset): |
28 |
""" |
29 |
Translate the rendered object along the x, y and z-axes. |
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 rendered object along the x-axis. |
43 |
@type angle: Number |
44 |
@param angle: Angle to rotate the camera |
45 |
""" |
46 |
|
47 |
self.__vtk_transform.RotateX(-angle) |
48 |
|
49 |
def rotateY(self, angle): |
50 |
""" |
51 |
Rotate the rendered object along the y-axis. |
52 |
@type angle: Number |
53 |
@param angle: Angle to rotate the camera |
54 |
""" |
55 |
|
56 |
self.__vtk_transform.RotateY(angle) |
57 |
|
58 |
|
59 |
def rotateZ(self, angle): |
60 |
""" |
61 |
Rotate the rendered object along the z-axis. |
62 |
@type angle: Number |
63 |
@param angle: Angle to rotate the camera |
64 |
""" |
65 |
|
66 |
self.__vtk_transform.RotateZ(angle) |
67 |
|
68 |
def setPlaneToXY(self, offset = 0): |
69 |
""" |
70 |
Set the plane orthogonal to the z-axis. |
71 |
@type offset: Number |
72 |
@param offset: Amount to translate |
73 |
""" |
74 |
|
75 |
self.translate(0, 0, offset + self.__OFFSET_VARIANCE) |
76 |
|
77 |
def setPlaneToYZ(self, offset = 0): |
78 |
""" |
79 |
Set the plane orthogonal to the x-axis. |
80 |
@type offset: Number |
81 |
@param offset: Amount to translate |
82 |
""" |
83 |
|
84 |
# NOTE: rotateY must come first before translate. Otherwise, |
85 |
# the output may be incorrect. |
86 |
self.rotateY(90) |
87 |
self.translate(offset, 0, 0) |
88 |
|
89 |
def setPlaneToXZ(self, offset = 0): |
90 |
""" |
91 |
Set the plane orthogonal to the y-axis. |
92 |
@type offset: Number |
93 |
@param offset: Amount to translate |
94 |
""" |
95 |
|
96 |
# rotateX must come first before translate. Otherwise, it won't work. |
97 |
self.rotateX(90) |
98 |
self.translate(0, offset, 0) |
99 |
|
100 |
def _getTransform(self): |
101 |
""" |
102 |
Return the transform instance. |
103 |
@rtype: vtkTransform |
104 |
@return: VTK transform that is used to specify the orientation |
105 |
of objects |
106 |
""" |
107 |
|
108 |
return self.__vtk_transform |