1 |
|
2 |
######################################################## |
3 |
# |
4 |
# Copyright (c) 2003-2010 by University of Queensland |
5 |
# Earth Systems Science Computational Center (ESSCC) |
6 |
# http://www.uq.edu.au/esscc |
7 |
# |
8 |
# Primary Business: Queensland, Australia |
9 |
# Licensed under the Open Software License version 3.0 |
10 |
# http://www.opensource.org/licenses/osl-3.0.php |
11 |
# |
12 |
######################################################## |
13 |
|
14 |
__copyright__="""Copyright (c) 2003-2010 by University of Queensland |
15 |
Earth Systems Science Computational Center (ESSCC) |
16 |
http://www.uq.edu.au/esscc |
17 |
Primary Business: Queensland, Australia""" |
18 |
__license__="""Licensed under the Open Software License version 3.0 |
19 |
http://www.opensource.org/licenses/osl-3.0.php""" |
20 |
__url__="https://launchpad.net/escript-finley" |
21 |
|
22 |
""" |
23 |
:var __author__: name of author |
24 |
:var __copyright__: copyrights |
25 |
:var __license__: licence agreement |
26 |
:var __url__: url entry point on documentation |
27 |
:var __version__: version |
28 |
:var __date__: date of the version |
29 |
""" |
30 |
|
31 |
__author__="John Ngui, john.ngui@uq.edu.au" |
32 |
|
33 |
from esys.escript import getMPISizeWorld |
34 |
if getMPISizeWorld()==1: import vtk |
35 |
|
36 |
class Transform: |
37 |
""" |
38 |
Class that defines the orientation of planes. |
39 |
|
40 |
:attention: There is a difference between performing rotation first followed by translation, and performing translation first followed by rotation. |
41 |
|
42 |
:attention: VTK's coordinate system and translation is NOT 100% precise. Consequently, performing maximum rotation and translation can potentially yield incorrect results. For instance, rotating a XY plane along the x-axis 90 degrees may NOT produce any results (as it is possible that the XY plane has just fallen outside the visible range). Therefore, rotating the XY plane 89.9 degrees instead, should be a better option in order to produce the correct results. |
43 |
""" |
44 |
|
45 |
def __init__(self): |
46 |
""" |
47 |
Initialise the transform object. |
48 |
""" |
49 |
if getMPISizeWorld()>1: |
50 |
raise ValueError,"pyvisi.Transform is not running on more than one processor." |
51 |
# NOTE: VTK's coordinates are not 100% precise. The origin is not |
52 |
# exaclty (0,0,0) and the normal is not exactly (0, 0, 1). There is a |
53 |
# slight variance. As a result, a slight alteration has to be done |
54 |
# in order for the plane to be displayed correctly. Otherwise, the |
55 |
# plane may just fall outside the bounding box and nothing |
56 |
# is displayed. |
57 |
self.__OFFSET_VARIANCE = 0.0000000001 |
58 |
self.__vtk_transform = vtk.vtkTransform() |
59 |
|
60 |
def translate(self, x_offset, y_offset, z_offset): |
61 |
""" |
62 |
Translate the rendered object along the x, y and z-axes. |
63 |
|
64 |
:type x_offset: Number |
65 |
:param x_offset: Amount to translate along the x-axis |
66 |
:type y_offset: Number |
67 |
:param y_offset: Amount to translate along the y-axis |
68 |
:type z_offset: Number |
69 |
:param z_offset: Amount to translate along the z-axis |
70 |
""" |
71 |
|
72 |
self.__vtk_transform.Translate(-x_offset, -y_offset, -z_offset) |
73 |
|
74 |
def rotateX(self, angle): |
75 |
""" |
76 |
Rotate the plane along the x-axis. |
77 |
|
78 |
:type angle: Number |
79 |
:param angle: Angle to rotate the plane |
80 |
""" |
81 |
|
82 |
self.__vtk_transform.RotateX(-angle) |
83 |
|
84 |
def rotateY(self, angle): |
85 |
""" |
86 |
Rotate the plane along the y-axis. |
87 |
|
88 |
:type angle: Number |
89 |
:param angle: Angle to rotate the plane |
90 |
""" |
91 |
|
92 |
self.__vtk_transform.RotateY(angle) |
93 |
|
94 |
def rotateZ(self, angle): |
95 |
""" |
96 |
Rotate the plane along the z-axis. |
97 |
|
98 |
:type angle: Number |
99 |
:param angle: Angle to rotate the plane |
100 |
""" |
101 |
|
102 |
self.__vtk_transform.RotateZ(angle) |
103 |
|
104 |
def setPlaneToXY(self, offset = 0): |
105 |
""" |
106 |
Set the plane orthogonal to the z-axis. |
107 |
|
108 |
:type offset: Number |
109 |
:param offset: Amount to translate along the z-axis |
110 |
""" |
111 |
|
112 |
self.translate(0, 0, offset + self.__OFFSET_VARIANCE) |
113 |
|
114 |
def setPlaneToYZ(self, offset = 0): |
115 |
""" |
116 |
Set the plane orthogonal to the x-axis. |
117 |
|
118 |
:type offset: Number |
119 |
:param offset: Amount to translate along the x-axis |
120 |
""" |
121 |
|
122 |
# NOTE: rotateY must come first before translate. Otherwise, |
123 |
# the output may be incorrect. |
124 |
self.rotateY(90) |
125 |
self.translate(offset, 0, 0) |
126 |
|
127 |
def setPlaneToXZ(self, offset = 0): |
128 |
""" |
129 |
Set the plane orthogonal to the y-axis. |
130 |
|
131 |
:type offset: Number |
132 |
:param offset: Amount to translate along the y-axis |
133 |
""" |
134 |
|
135 |
# rotateX must come first before translate. Otherwise, it won't work. |
136 |
self.rotateX(90) |
137 |
self.translate(0, offset, 0) |
138 |
|
139 |
def _getTransform(self): |
140 |
""" |
141 |
Return the transform instance. |
142 |
|
143 |
:rtype: vtkTransform |
144 |
:return: Transform instance that is used to specify the orientation |
145 |
of the plane |
146 |
""" |
147 |
|
148 |
return self.__vtk_transform |
149 |
|
150 |
|
151 |
############################################################################### |
152 |
|
153 |
|
154 |
class TransformFilter: |
155 |
""" |
156 |
Class that defines a transform poly data filter. |
157 |
""" |
158 |
|
159 |
def __init__(self): |
160 |
""" |
161 |
Initialise the transoform poly data filter. |
162 |
""" |
163 |
if getMPISizeWorld()>1: |
164 |
raise ValueError,"pyvisi.TransformFilter is not running on more than one processor." |
165 |
self.__vtk_transform_filter = vtk.vtkTransformPolyDataFilter() |
166 |
|
167 |
def _setupTransformFilter(self, plane_source, transform): |
168 |
""" |
169 |
Setup the transform filter. |
170 |
|
171 |
:type plane_source: vtkPolyData |
172 |
:param plane_source: Polygonal data |
173 |
:type transform: `Transform` object |
174 |
:param transform: Specifies the orientation of the plane source |
175 |
""" |
176 |
|
177 |
self.__plane_source = plane_source |
178 |
self.__transform = transform |
179 |
|
180 |
self.__setInput() |
181 |
self.__setTransform() |
182 |
|
183 |
def __setInput(self): |
184 |
""" |
185 |
Set the input for the transform poly data filter. |
186 |
""" |
187 |
|
188 |
self.__vtk_transform_filter.SetInput(self.__plane_source) |
189 |
|
190 |
def __setTransform(self): |
191 |
""" |
192 |
Set the transformation of the plane source. |
193 |
""" |
194 |
|
195 |
self.__vtk_transform_filter.SetTransform(self.__transform) |
196 |
|
197 |
def _getTransformFilterOutput(self): |
198 |
""" |
199 |
Return the output of the transform poly data filter. |
200 |
""" |
201 |
|
202 |
return self.__vtk_transform_filter.GetOutput() |
203 |
|
204 |
|
205 |
|