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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 978 - (show annotations)
Fri Feb 16 07:02:18 2007 UTC (14 years, 9 months ago) by jongui
File MIME type: text/x-python
File size: 5720 byte(s)
Corrected some minor bugs. However, another bug was discover in relation to specifying the active scalar/vector/tensor. At this stage it appears that with one XML Reader, only one active scalar can be specified. If more than one is specified with only one XML reader, the rendered objects maybe incorrect. However, more test on this problem is required/
1 """
2 @author: John NGUI
3 """
4
5 import vtk
6 import tempfile, os
7 from constant import Source
8 try:
9 import esys.escript
10 except ImportError:
11 print "Warning: importing esys.escript failed."
12
13 class DataCollector:
14 """
15 Class that defines a data collector which dealrs with the source
16 of data for the visualisation.
17 """
18
19 def __init__(self, source = Source.XML):
20 """
21 Initialise the data collector.
22
23 @type source: L{Source <constant.Source>} constant
24 @param source: Source data type
25 """
26
27 self.__source=source
28 if(source == Source.XML): # Source is an XML file.
29 self.__vtk_xml_reader = vtk.vtkXMLUnstructuredGridReader()
30 # this is a temporary solution using a file:
31 if (source == Source.ESCRIPT):
32 self.__vtk_xml_reader = vtk.vtkXMLUnstructuredGridReader()
33 self.__tmp_file=tempfile.mkstemp(suffix=".xml")[1]
34 self.__vtk_xml_reader.SetFileName(self.__tmp_file)
35 self.__output = self.__vtk_xml_reader.GetOutput()
36 def __del__(self):
37 """
38 clean up
39 """
40 if (self.__source == Source.ESCRIPT):
41 if os.access(self.__tmp_file,os.F_OK): os.unlink(self.__tmp_file)
42
43
44 def setFileName(self, file_name):
45 """
46 Set the source file name to read.
47
48 @type file_name: String
49 @param file_name: Name of the file to read
50 """
51 if self.__source == Source.XML:
52 self.__vtk_xml_reader.SetFileName(file_name)
53
54 # NOTE: Update must be called after SetFileName to make the reader
55 # up to date. Otherwise, some output values may be incorrect.
56 self.__vtk_xml_reader.Update()
57 self.__output = self.__vtk_xml_reader.GetOutput()
58 else:
59 raise ValueError("source type %s does not support setFileName"%self.__source)
60
61 def setData(self,**args):
62 """
63 sets the data using. <name>=<data> sets the data tagged by <name> to the object <data>. It is expected
64 that the data are given in an appropriate source type.
65 """
66 if self.__source == Source.ESCRIPT:
67 esys.escript.saveVTK(self.__tmp_file,**args)
68 self.__vtk_xml_reader.Update()
69 else:
70 raise ValueError("source type %s does not support setData"%self.__source)
71
72 def _setActiveScalar(self, scalar):
73 """
74 Specify the scalar field to load from the source file.
75
76 @type scalar: String
77 @param scalar: Scalar field to load from the file.
78 """
79
80 self._getOutput().GetPointData().SetActiveScalars(scalar)
81
82 def _setActiveVector(self, vector):
83 """
84 Specify the vector field to load from the source file.
85
86 @type vector: String
87 @param vector: Vector field to load from the file.
88 """
89
90 self._getOutput().GetPointData().SetActiveVectors(vector)
91
92 def _setActiveTensor(self, tensor):
93 """
94 Specify the tensor field to load from the source file.
95
96 @type tensor: String
97 @param tensor: Tensor field to load from the file.
98 """
99
100 self._getOutput().GetPointData().SetActiveTensors(tensor)
101
102 def _getScalarRange(self):
103 """
104 Return the scalar range.
105
106 @rtype: Two column tuple containing numbers
107 @return: Scalar range
108 """
109
110 return self._getOutput().GetPointData().GetScalars().GetRange(-1)
111
112 def _getVectorRange(self):
113 """
114 Return the vector range.
115
116 @rtype: Two column tuple containing numbers
117 @return: Vector range
118 """
119
120 print self._getOutput().GetPointData().GetVectors()
121 vector_range = self._getOutput().GetPointData().GetVectors().GetRange(-1)
122
123 # NOTE: Generally GetRange(-1) returns the correct vector range.
124 # However, there are certain data sets where GetRange(-1) seems
125 # to return incorrect mimimum vector although the maximum vector is
126 # correct. As a result, the mimimum vector has been hard coded to 0.0
127 # to accommodate those incorrect cases.
128
129 return (0.0, vector_range[1])
130
131 def _getTensorRange(self):
132 """
133 Return the tensor range.
134
135 @rtype: Two column table
136 @return: Tensor range
137 """
138
139 return self._getOutput().GetPointData().GetTensors().GetRange(-1)
140
141 def _getOutput(self):
142 """
143 Return the output of the data collector.
144
145 @rtype: vtkUnstructuredGrid
146 @return: Unstructured grid
147 """
148
149 return self.__output
150
151
152 ###############################################################################
153
154
155 from constant import ImageFormat
156
157 class ImageReader:
158 """
159 Class that defines an image reader.
160 """
161
162 def __init__(self, format):
163 """
164 Initialise the image reader.
165
166 @type format: String
167 @param format: Format of the image
168 """
169
170 self.__format = format
171 self.__vtk_image_reader = self.getImageReader()
172
173 def getImageReader(self):
174 """
175 Return the appropriate image reader based on the supplied image
176 format.
177
178 @rtype: vtkImageReader2 (i.e. vtkJPEGReader, etc)
179 @return: Image reader
180 """
181
182 if(self.__format == ImageFormat.JPG):
183 return vtk.vtkJPEGReader()
184 elif(self.__format == ImageFormat.BMP):
185 return vtk.vtkBMPReader()
186 elif(self.__format == ImageFormat.PNM):
187 return vtk.vtkPNMReader()
188 elif(self.__format == ImageFormat.PNG):
189 return vtk.vtkPNGReader()
190 elif(self.__format == ImageFormat.TIF):
191 return vtk.vtkTIFFReader()
192
193 def setFileName(self, file_name):
194 """
195 Set the image file name.
196
197 @type file_name: String
198 @param file_name: Image file name to be read
199 """
200
201 self.__vtk_image_reader.SetFileName(file_name)
202
203 def _getOutput(self):
204 """
205 Return the output of the image reader.
206
207 @rtype: vtkImageData
208 @return Image data
209 """
210
211 return self.__vtk_image_reader.GetOutput()
212

  ViewVC Help
Powered by ViewVC 1.1.26