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.setNumberOfPoints(10) |
31 |
# Default center is of the sphere is the center of the data object. |
32 |
center = self.__object.GetCenter() |
33 |
self.setCenter(GlobalPosition(center[0], center[1], center[2])) |
34 |
# Default radius of the sphere is 0.1. |
35 |
self.setPointSourceRadius(0.1) |
36 |
self.__vtk_point_source.Update() |
37 |
|
38 |
def setPointSourceRadius(self, radius): |
39 |
""" |
40 |
Set the radius of the sphere. |
41 |
|
42 |
@type radius: Number |
43 |
@param radius: Radius of the sphere |
44 |
""" |
45 |
|
46 |
self.__vtk_point_source.SetRadius(radius) |
47 |
|
48 |
def setCenter(self, position): |
49 |
""" |
50 |
Set the center of the sphere. |
51 |
|
52 |
@type position: L{GLobalPosition <position.GlobalPosition>} object |
53 |
@param position: Center of the sphere |
54 |
""" |
55 |
|
56 |
self.__vtk_point_source.SetCenter(position._getGlobalPosition()) |
57 |
|
58 |
def setNumberOfPoints(self, points): |
59 |
""" |
60 |
Set the number of points to generate within the sphere. |
61 |
|
62 |
@type points: Number |
63 |
@param points: Number of points to generate |
64 |
""" |
65 |
|
66 |
self.__vtk_point_source.SetNumberOfPoints(points) |
67 |
|
68 |
def _getOutput(self): |
69 |
""" |
70 |
Return the point source. |
71 |
|
72 |
@rtype: vtkPolyData |
73 |
@return: Polygonal data |
74 |
""" |
75 |
|
76 |
return self.__vtk_point_source.GetOutput() |
77 |
|
78 |
|
79 |
class StructuredPoints: |
80 |
""" |
81 |
Class that defines the structured points. |
82 |
""" |
83 |
|
84 |
# NOTE: The algorithm of this class was extracted from Mayavi's |
85 |
# online source code. |
86 |
def __init__(self, object): |
87 |
""" |
88 |
Initialise the structured points. |
89 |
|
90 |
@type object: vtkUnstructuredGrid, etc |
91 |
@param object: Input for the structured points |
92 |
""" |
93 |
|
94 |
self.__object = object |
95 |
self.__vtk_structured_points = vtk.vtkStructuredPoints() |
96 |
self.__setupStructuredPoints() |
97 |
|
98 |
def __setupStructuredPoints(self): |
99 |
""" |
100 |
Setup the structured points. |
101 |
""" |
102 |
|
103 |
b = self.__object.GetBounds() |
104 |
self.__vtk_structured_points.SetOrigin(b[0], b[2], b[4]) |
105 |
self.__l = [b[1] - b[0], b[3] - b[2], b[5] - b[4]] |
106 |
tot_len = float(self.__l[0] + self.__l[1] + self.__l[2]) |
107 |
npnt = pow(self.__object.GetNumberOfPoints(), 1. / 3.) + 0.5 |
108 |
self.__fac = 3.0*npnt / tot_len |
109 |
|
110 |
# Default dimension is 1, 1, 1. |
111 |
self.setDimension(1, 1, 1) |
112 |
|
113 |
def __setExtent(self, x0, x1, y0, y1, z0, z1): |
114 |
""" |
115 |
Set the extent in the order of x, y, z axes. |
116 |
|
117 |
@type x0: Index of the first point on the x-axis |
118 |
@param x1: Index of the last point on the x-axis |
119 |
@type y0: Index of the first point on the y-axis |
120 |
@param y1: Index of the last point on the y-axis |
121 |
@type z0: Index of the first point on the z-axis |
122 |
@param z1: Index of the last point on the z-axis |
123 |
""" |
124 |
|
125 |
self.__vtk_structured_points.SetExtent(x0, x1, y0, y1, z0, z1) |
126 |
|
127 |
def __setUpdateExtent(self, x0, x1, y0, y1, z0, z1): |
128 |
""" |
129 |
Set the update extent in the oder of x, y, z axes. |
130 |
|
131 |
@type x0: Number |
132 |
@param x1: Index of the last point on the x-axis |
133 |
@type y0: Number |
134 |
@param y1: Index of the last point on the y-axis |
135 |
@type z0: Number |
136 |
@param z1: Index of the last point on the z-axis |
137 |
""" |
138 |
|
139 |
self.__vtk_structured_points.SetUpdateExtent(x0, x1, y0, y1, z0, z1) |
140 |
|
141 |
#def __setWholeExtent(self, x0, x1, y0, y1, z0, z1): |
142 |
# self.__vtk_structured_points.SetWholeExtent(x0, x1, y0, y1, z0, z1) |
143 |
|
144 |
def setDimension(self, x, y, z): |
145 |
""" |
146 |
Set the dimension (number of points) on the x, y and z axes. The |
147 |
smaller the dimension, the more points are populated. |
148 |
|
149 |
@type x: Number |
150 |
@param x: Number of points on the x-axis |
151 |
@type y: Number |
152 |
@param y: Number of points on the y-axis |
153 |
@type z: Number |
154 |
@param z: Number of points on the z-axis |
155 |
""" |
156 |
|
157 |
self.__dims = [int((self.__l[0]*self.__fac)/x) + 1, |
158 |
int((self.__l[1] * self.__fac) / y) + 1, |
159 |
int((self.__l[2] * self.__fac) / z) + 1] |
160 |
|
161 |
self.__setExtent(0, self.__dims[0] - 1, 0, self.__dims[1] - 1, 0, |
162 |
self.__dims[2] - 1) |
163 |
self.__setUpdateExtent(0, self.__dims[0] - 1, 0, self.__dims[1] - 1, 0, |
164 |
self.__dims[2] - 1) |
165 |
|
166 |
#self.__setWholeExtent(0, self.__dims[0] - 1, 0, self.__dims[1] - 1, 0, |
167 |
# self.__dims[2] - 1) |
168 |
|
169 |
self.__vtk_structured_points.SetDimensions(self.__dims) |
170 |
self.__setSpacing() |
171 |
|
172 |
def __setSpacing(self): |
173 |
""" |
174 |
Set the spacing (width, height and length) of the cells that make up |
175 |
the dataset. |
176 |
""" |
177 |
|
178 |
self.__dims = [max(1, x - 1) for x in self.__dims] |
179 |
self.__l = [max(1e-3, x) for x in self.__l] |
180 |
sp = [self.__l[0] / self.__dims[0], self.__l[1] / self.__dims[1], |
181 |
self.__l[2] / self.__dims[2]] |
182 |
|
183 |
self.__vtk_structured_points.SetSpacing(sp) |
184 |
# Update the changes made. |
185 |
self.__vtk_structured_points.Update() |
186 |
|
187 |
def _getStructuredPoints(self): |
188 |
""" |
189 |
Return the structured points. |
190 |
|
191 |
@rtype: vtkStructuredPoints |
192 |
@return: Structured points |
193 |
""" |
194 |
|
195 |
return self.__vtk_structured_points |
196 |
|