/[escript]/trunk/pyvisi/py_src/point.py
ViewVC logotype

Contents of /trunk/pyvisi/py_src/point.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 949 - (show annotations)
Fri Feb 2 05:33:05 2007 UTC (13 years, 5 months ago) by jongui
File MIME type: text/x-python
File size: 5519 byte(s)
Fixed some more bugs and added more epydocs. Testing remains outstanding.
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.1.
37 self.setPointSourceRadius(0.1)
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.__setWholeExtent(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

  ViewVC Help
Powered by ViewVC 1.1.26