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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 943 - (hide annotations)
Mon Jan 29 07:22:43 2007 UTC (14 years, 9 months ago) by jongui
File MIME type: text/x-python
File size: 5009 byte(s)
new pyvisi files
1 jongui 943 """
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 source code.
85     def __init__(self, object):
86     """
87     Initialise the structured points.
88    
89     @type object: vtkUnstructuredGrid, etc
90     @param object: Input for the structured points
91     """
92    
93     self.__object = object
94     self.__vtk_structured_points = vtk.vtkStructuredPoints()
95     self.__setupStructuredPoints()
96    
97     def __setupStructuredPoints(self):
98     """
99     Setup the structured points.
100     """
101    
102     b = self.__object.GetBounds()
103     self.__vtk_structured_points.SetOrigin(b[0], b[2], b[4])
104     self.__l = [b[1] - b[0], b[3] - b[2], b[5] - b[4]]
105     tot_len = float(self.__l[0] + self.__l[1] + self.__l[2])
106     npnt = pow(self.__object.GetNumberOfPoints(), 1. / 3.) + 0.5
107     self.__fac = 3.0*npnt / tot_len
108    
109     # Default dimension is 1, 1, 1.
110     self.setDimension(1, 1, 1)
111    
112     def __setExtent(self, x0, x1, y0, y1, z0, z1):
113     """
114     Set the extent in the order of x, y, z axes.
115    
116     @type x0: Index of the first point on the x-axis
117     @param x1: Index of the last point on the x-axis
118     @type y0: Index of the first point on the y-axis
119     @param y1: Index of the last point on the y-axis
120     @type z0: Index of the first point on the z-axis
121     @param z1: Index of the last point on the z-axis
122     """
123    
124     self.__vtk_structured_points.SetExtent(x0, x1, y0, y1, z0, z1)
125    
126     def __setUpdateExtent(self, x0, x1, y0, y1, z0, z1):
127     """
128     Set the update extent in the oder of x, y, z axes.
129    
130     @type x0: Number
131     @param x1: Index of the last point on the x-axis
132     @type y0: Number
133     @param y1: Index of the last point on the y-axis
134     @type z0: Number
135     @param z1: Index of the last point on the z-axis
136     """
137    
138     self.__vtk_structured_points.SetUpdateExtent(x0, x1, y0, y1, z0, z1)
139    
140     #def __setWholeExtent(self, x0, x1, y0, y1, z0, z1):
141     # self.__vtk_structured_points.SetWholeExtent(x0, x1, y0, y1, z0, z1)
142    
143     def setDimension(self, x, y, z):
144     """
145     Set the dimension (number of points) on the x, y and z axes.
146    
147     @type x: Number
148     @param x: Number of points on the x-axis
149     @type y: Number
150     @param y: Number of points on the y-axis
151     @type z: Number
152     @param z: Number of points on the z-axis
153     """
154    
155     self.__dims = [int((self.__l[0]*self.__fac)/x) + 1,
156     int((self.__l[1] * self.__fac) / y) + 1,
157     int((self.__l[2] * self.__fac) / z) + 1]
158    
159     self.__setExtent(0, self.__dims[0] - 1, 0, self.__dims[1] - 1, 0,
160     self.__dims[2] - 1)
161     self.__setUpdateExtent(0, self.__dims[0] - 1, 0, self.__dims[1] - 1, 0,
162     self.__dims[2] - 1)
163    
164     #self.__setWholeExtent(0, self.__dims[0] - 1, 0, self.__dims[1] - 1, 0,
165     # self.__dims[2] - 1)
166    
167     self.__vtk_structured_points.SetDimensions(self.__dims)
168     self.__setSpacing()
169    
170     def __setSpacing(self):
171     """
172     Set the spacing (width, height and length) of the cells that make up
173     the dataset.
174     """
175    
176     self.__dims = [max(1, x - 1) for x in self.__dims]
177     self.__l = [max(1e-3, x) for x in self.__l]
178     sp = [self.__l[0] / self.__dims[0], self.__l[1] / self.__dims[1],
179     self.__l[2] / self.__dims[2]]
180    
181     self.__vtk_structured_points.SetSpacing(sp)
182     # Update the changes made.
183     self.__vtk_structured_points.Update()
184    
185     def _getStructuredPoints(self):
186     """
187     Return the structured points.
188    
189     @rtype: vtkStructuredPoints
190     @return: Structured points
191     """
192    
193     return self.__vtk_structured_points
194    

  ViewVC Help
Powered by ViewVC 1.1.26