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 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 |
|
90 |
# NOTE: The algorithm of this class was extracted from Mayavi's |
91 |
# online source code. Does NOT work for second-order (non-linear) elements. |
92 |
def __init__(self, object): |
93 |
""" |
94 |
Initialise the structured points. |
95 |
|
96 |
@type object: vtkUnstructuredGrid, etc |
97 |
@param object: Input for the structured points |
98 |
""" |
99 |
|
100 |
self.__object = object |
101 |
self.__vtk_structured_points = vtk.vtkStructuredPoints() |
102 |
self.__setupStructuredPoints() |
103 |
|
104 |
def __setupStructuredPoints(self): |
105 |
""" |
106 |
Setup the structured points. |
107 |
""" |
108 |
|
109 |
b = self.__object.GetBounds() |
110 |
self.__vtk_structured_points.SetOrigin(b[0], b[2], b[4]) |
111 |
self.__l = [b[1] - b[0], b[3] - b[2], b[5] - b[4]] |
112 |
tot_len = float(self.__l[0] + self.__l[1] + self.__l[2]) |
113 |
npnt = pow(self.__object.GetNumberOfPoints(), 1. / 3.) + 0.5 |
114 |
self.__fac = 3.0*npnt / tot_len |
115 |
|
116 |
# Default dimension is 1, 1, 1. |
117 |
self.setDimension(1, 1, 1) |
118 |
|
119 |
def __setExtent(self, x0, x1, y0, y1, z0, z1): |
120 |
""" |
121 |
Set the extent in the order of x, y and z axes. |
122 |
|
123 |
@type x0: Number |
124 |
@param x0: Index of the first point on the x-axis |
125 |
@type x1: Number |
126 |
@param x1: Index of the last point on the x-axis |
127 |
@type y0: Number |
128 |
@param y0: Index of the first point on the y-axis |
129 |
@type y1: Number |
130 |
@param y1: Index of the last point on the y-axis |
131 |
@type z0: Number |
132 |
@param z0: Index of the first point on the z-axis |
133 |
@type z1: Number |
134 |
@param z1: Index of the last point on the z-axis |
135 |
""" |
136 |
|
137 |
self.__vtk_structured_points.SetExtent(x0, x1, y0, y1, z0, z1) |
138 |
|
139 |
def __setUpdateExtent(self, x0, x1, y0, y1, z0, z1): |
140 |
""" |
141 |
Set the update extent in the oder of x, y and z axes. |
142 |
|
143 |
@type x0: Number |
144 |
@param x0: Index of the first point on the x-axis |
145 |
@type x1: Number |
146 |
@param x1: Index of the last point on the x-axis |
147 |
@type y0: Number |
148 |
@param y0: Index of the first point on the y-axis |
149 |
@type y1: Number |
150 |
@param y1: Index of the last point on the y-axis |
151 |
@type z0: Number |
152 |
@param z0: Index of the first point on the z-axis |
153 |
@type z1: Number |
154 |
@param z1: Index of the last point on the z-axis |
155 |
""" |
156 |
|
157 |
self.__vtk_structured_points.SetUpdateExtent(x0, x1, y0, y1, z0, z1) |
158 |
|
159 |
def setDimension(self, x, y, z): |
160 |
""" |
161 |
Set the dimension on the x, y and z axes. The |
162 |
smaller the dimension, the more points are populated. |
163 |
|
164 |
@type x: Number |
165 |
@param x: Dimension on the x-axis |
166 |
@type y: Number |
167 |
@param y: Dimension on the y-axis |
168 |
@type z: Number |
169 |
@param z: Dimension on the z-axis |
170 |
""" |
171 |
|
172 |
self.__dims = [int((self.__l[0]*self.__fac)/x) + 1, |
173 |
int((self.__l[1] * self.__fac) / y) + 1, |
174 |
int((self.__l[2] * self.__fac) / z) + 1] |
175 |
|
176 |
self.__setExtent(0, self.__dims[0] - 1, 0, self.__dims[1] - 1, 0, |
177 |
self.__dims[2] - 1) |
178 |
self.__setUpdateExtent(0, self.__dims[0] - 1, 0, self.__dims[1] - 1, 0, |
179 |
self.__dims[2] - 1) |
180 |
|
181 |
self.__vtk_structured_points.SetDimensions(self.__dims) |
182 |
self.__setSpacing() |
183 |
|
184 |
def __setSpacing(self): |
185 |
""" |
186 |
Set the spacing (width, height and length) of the cells that make up |
187 |
the dataset. |
188 |
""" |
189 |
|
190 |
self.__dims = [max(1, x - 1) for x in self.__dims] |
191 |
self.__l = [max(1e-3, x) for x in self.__l] |
192 |
sp = [self.__l[0] / self.__dims[0], self.__l[1] / self.__dims[1], |
193 |
self.__l[2] / self.__dims[2]] |
194 |
|
195 |
self.__vtk_structured_points.SetSpacing(sp) |
196 |
# Update the changes made. |
197 |
self.__vtk_structured_points.Update() |
198 |
|
199 |
def _getStructuredPoints(self): |
200 |
""" |
201 |
Return the structured points. |
202 |
|
203 |
@rtype: vtkStructuredPoints |
204 |
@return: Structured points |
205 |
""" |
206 |
|
207 |
return self.__vtk_structured_points |
208 |
|
209 |
|
210 |
############################################################################## |
211 |
|
212 |
|
213 |
class MaskPoints: |
214 |
|
215 |
def __init__(self, object): |
216 |
self.__object = object |
217 |
self.__vtk_mask_points = vtk.vtkMaskPoints() |
218 |
|
219 |
self.__setupMaskPoints() |
220 |
|
221 |
def __setupMaskPoints(self): |
222 |
self.__setInput() |
223 |
|
224 |
def __setInput(self): |
225 |
self.__vtk_mask_points.SetInput(self.__object) |
226 |
|
227 |
def setRatio(self, ratio): |
228 |
self.__vtk_mask_points.SetOnRatio(ratio) |
229 |
|
230 |
def randomOn(self): |
231 |
self.__vtk_mask_points.RandomModeOn() |
232 |
|
233 |
def randomOff(self): |
234 |
self.__vtk_mask_points.RandomModeOff() |
235 |
|
236 |
def _getOutput(self): |
237 |
return self.__vtk_mask_points.GetOutput() |
238 |
|
239 |
|