/[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 948 - (show annotations)
Thu Feb 1 07:23:34 2007 UTC (13 years, 6 months ago) by jongui
File MIME type: text/x-python
File size: 5310 byte(s)
fixed some bugs in pyvisi and added more epydocs.
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, z axes.
122
123 @type x0: Index of the first point on the x-axis
124 @param x1: Index of the last point on the x-axis
125 @type y0: Index of the first point on the y-axis
126 @param y1: Index of the last point on the y-axis
127 @type z0: Index of the first point on the z-axis
128 @param z1: Index of the last point on the z-axis
129 """
130
131 # NOTE: setExtent and setUpdateExtent MUST be used together.
132 #self.__vtk_structured_points.SetExtent(x0, x1, y0, y1, z0, z1)
133
134 def __setUpdateExtent(self, x0, x1, y0, y1, z0, z1):
135 """
136 Set the update extent in the oder of x, y, z axes.
137
138 @type x0: Number
139 @param x1: Index of the last point on the x-axis
140 @type y0: Number
141 @param y1: Index of the last point on the y-axis
142 @type z0: Number
143 @param z1: Index of the last point on the z-axis
144 """
145
146 # NOTE: setUpdateExtent and setExtent MUST be used together.
147 #self.__vtk_structured_points.SetUpdateExtent(x0, x1, y0, y1, z0, z1)
148
149 def setDimension(self, x, y, z):
150 """
151 Set the dimension on the x, y and z axes. The
152 smaller the dimension, the more points are populated.
153
154 @type x: Number
155 @param x: Dimension on the x-axis
156 @type y: Number
157 @param y: Dimension on the y-axis
158 @type z: Number
159 @param z: Dimension on the z-axis
160 """
161
162 self.__dims = [int((self.__l[0]*self.__fac)/x) + 1,
163 int((self.__l[1] * self.__fac) / y) + 1,
164 int((self.__l[2] * self.__fac) / z) + 1]
165
166 self.__setExtent(0, self.__dims[0] - 1, 0, self.__dims[1] - 1, 0,
167 self.__dims[2] - 1)
168 self.__setUpdateExtent(0, self.__dims[0] - 1, 0, self.__dims[1] - 1, 0,
169 self.__dims[2] - 1)
170
171 #self.__setWholeExtent(0, self.__dims[0] - 1, 0, self.__dims[1] - 1, 0,
172 # self.__dims[2] - 1)
173
174 self.__vtk_structured_points.SetDimensions(self.__dims)
175 self.__setSpacing()
176
177 def __setSpacing(self):
178 """
179 Set the spacing (width, height and length) of the cells that make up
180 the dataset.
181 """
182
183 self.__dims = [max(1, x - 1) for x in self.__dims]
184 self.__l = [max(1e-3, x) for x in self.__l]
185 sp = [self.__l[0] / self.__dims[0], self.__l[1] / self.__dims[1],
186 self.__l[2] / self.__dims[2]]
187
188 self.__vtk_structured_points.SetSpacing(sp)
189 # Update the changes made.
190 self.__vtk_structured_points.Update()
191
192 def _getStructuredPoints(self):
193 """
194 Return the structured points.
195
196 @rtype: vtkStructuredPoints
197 @return: Structured points
198 """
199
200 return self.__vtk_structured_points
201

  ViewVC Help
Powered by ViewVC 1.1.26