1 |
""" |
2 |
@author: John NGUI |
3 |
""" |
4 |
|
5 |
import vtk |
6 |
from position import GlobalPosition |
7 |
|
8 |
class PointSource: |
9 |
""" |
10 |
Class that defines the source (location) to generate points. The points are |
11 |
generated within the radius of a sphere. |
12 |
""" |
13 |
|
14 |
def __init__(self, object): |
15 |
""" |
16 |
Initialise the point source. |
17 |
""" |
18 |
|
19 |
self.__object = object |
20 |
self.__vtk_point_source = vtk.vtkPointSource() |
21 |
|
22 |
self.__setupPointSource() |
23 |
|
24 |
def __setupPointSource(self): |
25 |
""" |
26 |
Setup the point source. |
27 |
""" |
28 |
|
29 |
# Default number of points to generate within the sphere is 10. |
30 |
self.setPointSourceNumberOfPoints(10) |
31 |
# Default center of the sphere is the center of the data object. |
32 |
center = self.__object.GetCenter() |
33 |
self.setPointSourceCenter( |
34 |
GlobalPosition(center[0], center[1], center[2])) |
35 |
|
36 |
# Default radius of the sphere is 0.5. |
37 |
self.setPointSourceRadius(0.5) |
38 |
self.__vtk_point_source.Update() |
39 |
|
40 |
def setPointSourceRadius(self, radius): |
41 |
""" |
42 |
Set the radius of the sphere. |
43 |
|
44 |
@type radius: Number |
45 |
@param radius: Radius of the sphere |
46 |
""" |
47 |
|
48 |
self.__vtk_point_source.SetRadius(radius) |
49 |
|
50 |
def setPointSourceCenter(self, position): |
51 |
""" |
52 |
Set the center of the sphere. |
53 |
|
54 |
@type position: L{GLobalPosition <position.GlobalPosition>} object |
55 |
@param position: Center of the sphere |
56 |
""" |
57 |
|
58 |
self.__vtk_point_source.SetCenter(position._getGlobalPosition()) |
59 |
|
60 |
def setPointSourceNumberOfPoints(self, points): |
61 |
""" |
62 |
Set the number of points to generate within the sphere (the larger the |
63 |
number of points, the more streamlines are generated) |
64 |
|
65 |
@type points: Number |
66 |
@param points: Number of points to generate |
67 |
""" |
68 |
|
69 |
self.__vtk_point_source.SetNumberOfPoints(points) |
70 |
|
71 |
def _getOutput(self): |
72 |
""" |
73 |
Return the output of the point source. |
74 |
|
75 |
@rtype: vtkPolyData |
76 |
@return: Polygonal data |
77 |
""" |
78 |
|
79 |
return self.__vtk_point_source.GetOutput() |
80 |
|
81 |
|
82 |
############################################################################### |
83 |
|
84 |
|
85 |
class StructuredPoints: |
86 |
""" |
87 |
Class that defines the structured points. |
88 |
|
89 |
@status: This class is currently not included because it does NOT appear |
90 |
to work with second-order elements. |
91 |
""" |
92 |
|
93 |
# NOTE: The algorithm of this class was extracted from Mayavi's |
94 |
# online source code. Does NOT work for second-order (non-linear) elements. |
95 |
def __init__(self, object): |
96 |
""" |
97 |
Initialise the structured points. |
98 |
|
99 |
@type object: vtkUnstructuredGrid, etc |
100 |
@param object: Input for the structured points |
101 |
""" |
102 |
|
103 |
self.__object = object |
104 |
self.__vtk_structured_points = vtk.vtkStructuredPoints() |
105 |
self.__setupStructuredPoints() |
106 |
|
107 |
def __setupStructuredPoints(self): |
108 |
""" |
109 |
Setup the structured points. |
110 |
""" |
111 |
|
112 |
b = self.__object.GetBounds() |
113 |
self.__vtk_structured_points.SetOrigin(b[0], b[2], b[4]) |
114 |
self.__l = [b[1] - b[0], b[3] - b[2], b[5] - b[4]] |
115 |
tot_len = float(self.__l[0] + self.__l[1] + self.__l[2]) |
116 |
npnt = pow(self.__object.GetNumberOfPoints(), 1. / 3.) + 0.5 |
117 |
self.__fac = 3.0*npnt / tot_len |
118 |
|
119 |
# Default dimension is 1, 1, 1. |
120 |
self.setDimension(1, 1, 1) |
121 |
|
122 |
def __setExtent(self, x0, x1, y0, y1, z0, z1): |
123 |
""" |
124 |
Set the extent in the order of x, y and z axes. |
125 |
|
126 |
@type x0: Number |
127 |
@param x0: Index of the first point on the x-axis |
128 |
@type x1: Number |
129 |
@param x1: Index of the last point on the x-axis |
130 |
@type y0: Number |
131 |
@param y0: Index of the first point on the y-axis |
132 |
@type y1: Number |
133 |
@param y1: Index of the last point on the y-axis |
134 |
@type z0: Number |
135 |
@param z0: Index of the first point on the z-axis |
136 |
@type z1: Number |
137 |
@param z1: Index of the last point on the z-axis |
138 |
""" |
139 |
|
140 |
self.__vtk_structured_points.SetExtent(x0, x1, y0, y1, z0, z1) |
141 |
|
142 |
def __setUpdateExtent(self, x0, x1, y0, y1, z0, z1): |
143 |
""" |
144 |
Set the update extent in the oder of x, y and z axes. |
145 |
|
146 |
@type x0: Number |
147 |
@param x0: Index of the first point on the x-axis |
148 |
@type x1: Number |
149 |
@param x1: Index of the last point on the x-axis |
150 |
@type y0: Number |
151 |
@param y0: Index of the first point on the y-axis |
152 |
@type y1: Number |
153 |
@param y1: Index of the last point on the y-axis |
154 |
@type z0: Number |
155 |
@param z0: Index of the first point on the z-axis |
156 |
@type z1: Number |
157 |
@param z1: Index of the last point on the z-axis |
158 |
""" |
159 |
|
160 |
self.__vtk_structured_points.SetUpdateExtent(x0, x1, y0, y1, z0, z1) |
161 |
|
162 |
def setDimension(self, x, y, z): |
163 |
""" |
164 |
Set the dimension on the x, y and z axes. The |
165 |
smaller the dimension, the more points are populated. |
166 |
|
167 |
@type x: Number |
168 |
@param x: Dimension on the x-axis |
169 |
@type y: Number |
170 |
@param y: Dimension on the y-axis |
171 |
@type z: Number |
172 |
@param z: Dimension on the z-axis |
173 |
""" |
174 |
|
175 |
self.__dims = [int((self.__l[0]*self.__fac)/x) + 1, |
176 |
int((self.__l[1] * self.__fac) / y) + 1, |
177 |
int((self.__l[2] * self.__fac) / z) + 1] |
178 |
|
179 |
self.__setExtent(0, self.__dims[0] - 1, 0, self.__dims[1] - 1, 0, |
180 |
self.__dims[2] - 1) |
181 |
self.__setUpdateExtent(0, self.__dims[0] - 1, 0, self.__dims[1] - 1, 0, |
182 |
self.__dims[2] - 1) |
183 |
|
184 |
self.__vtk_structured_points.SetDimensions(self.__dims) |
185 |
self.__setSpacing() |
186 |
|
187 |
def __setSpacing(self): |
188 |
""" |
189 |
Set the spacing (width, height and length) of the cells that make up |
190 |
the dataset. |
191 |
""" |
192 |
|
193 |
self.__dims = [max(1, x - 1) for x in self.__dims] |
194 |
self.__l = [max(1e-3, x) for x in self.__l] |
195 |
sp = [self.__l[0] / self.__dims[0], self.__l[1] / self.__dims[1], |
196 |
self.__l[2] / self.__dims[2]] |
197 |
|
198 |
self.__vtk_structured_points.SetSpacing(sp) |
199 |
# Update the changes made. |
200 |
self.__vtk_structured_points.Update() |
201 |
|
202 |
def _getStructuredPoints(self): |
203 |
""" |
204 |
Return the structured points. |
205 |
|
206 |
@rtype: vtkStructuredPoints |
207 |
@return: Structured points |
208 |
""" |
209 |
|
210 |
return self.__vtk_structured_points |
211 |
|
212 |
|
213 |
############################################################################## |
214 |
|
215 |
|
216 |
class MaskPoints: |
217 |
""" |
218 |
Class that defines the masking of points. It is possible to mask |
219 |
every n'th point. This is useful to prevent the rendered object |
220 |
from being cluttered with arrows or ellipsoids. |
221 |
""" |
222 |
|
223 |
def __init__(self, object): |
224 |
""" |
225 |
Initialise the mask points. |
226 |
|
227 |
@type object: vtkDataSet (i.e. vtkUnstructuredGrid, etc) |
228 |
@param object: Data source from which to mask points |
229 |
""" |
230 |
|
231 |
self.__object = object |
232 |
self.__vtk_mask_points = vtk.vtkMaskPoints() |
233 |
|
234 |
self.__setupMaskPoints() |
235 |
|
236 |
def __setupMaskPoints(self): |
237 |
""" |
238 |
Setup the mask points. |
239 |
""" |
240 |
|
241 |
self.__setInput() |
242 |
|
243 |
def __setInput(self): |
244 |
""" |
245 |
Set the input for the mask points. |
246 |
""" |
247 |
|
248 |
self.__vtk_mask_points.SetInput(self.__object) |
249 |
|
250 |
def setRatio(self, ratio): |
251 |
""" |
252 |
Mask every nth point. |
253 |
|
254 |
@type ratio: Number |
255 |
@param ratio: Masking ratio |
256 |
""" |
257 |
|
258 |
self.__vtk_mask_points.SetOnRatio(ratio) |
259 |
|
260 |
def randomOn(self): |
261 |
""" |
262 |
Enables the randomization of the points selected for masking. |
263 |
""" |
264 |
|
265 |
self.__vtk_mask_points.RandomModeOn() |
266 |
|
267 |
def randomOff(self): |
268 |
""" |
269 |
Disables the randomization of the points selected for masking. |
270 |
""" |
271 |
|
272 |
self.__vtk_mask_points.RandomModeOff() |
273 |
|
274 |
def _getOutput(self): |
275 |
""" |
276 |
Return the output of the masked points. |
277 |
|
278 |
@rtype: vtkPolyData |
279 |
@return: Polygonal datda |
280 |
""" |
281 |
|
282 |
return self.__vtk_mask_points.GetOutput() |
283 |
|
284 |
|