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 |
self.__output = self.__vtk_xml_reader.GetOutput() |
54 |
|
55 |
# NOTE: Update must be called after SetFileName to make the reader |
56 |
# up to date. Otherwise, some output values may be incorrect. |
57 |
self.__vtk_xml_reader.Update() |
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 |
#self._getOutput().GetPointData().SetActiveScalars("temp") |
111 |
return self._getOutput().GetPointData().GetScalars().GetRange(-1) |
112 |
|
113 |
def _getVectorRange(self): |
114 |
""" |
115 |
Return the vector range. |
116 |
|
117 |
@rtype: Two column tuple containing numbers |
118 |
@return: Vector range |
119 |
""" |
120 |
|
121 |
print self._getOutput().GetPointData().GetVectors() |
122 |
vector_range = self._getOutput().GetPointData().GetVectors().GetRange(-1) |
123 |
|
124 |
# NOTE: Generally GetRange(-1) returns the correct vector range. |
125 |
# However, there are certain data sets where GetRange(-1) seems |
126 |
# to return incorrect mimimum vector although the maximum vector is |
127 |
# correct. As a result, the mimimum vector has been hard coded to 0.0 |
128 |
# to accommodate those incorrect cases. |
129 |
|
130 |
return (0.0, vector_range[1]) |
131 |
|
132 |
def _getTensorRange(self): |
133 |
""" |
134 |
Return the tensor range. |
135 |
|
136 |
@rtype: Two column table |
137 |
@return: Tensor range |
138 |
""" |
139 |
|
140 |
return self._getOutput().GetPointData().GetTensors().GetRange() |
141 |
|
142 |
def _getOutput(self): |
143 |
""" |
144 |
Return the output of the data collector. |
145 |
|
146 |
@rtype: vtkUnstructuredGrid |
147 |
@return: Unstructured grid |
148 |
""" |
149 |
|
150 |
return self.__output |
151 |
|
152 |
|
153 |
############################################################################### |
154 |
|
155 |
|
156 |
from constant import ImageFormat |
157 |
|
158 |
class ImageReader: |
159 |
""" |
160 |
Class that defines an image reader. |
161 |
""" |
162 |
|
163 |
def __init__(self, format): |
164 |
""" |
165 |
Initialise the image reader. |
166 |
|
167 |
@type format: String |
168 |
@param format: Format of the image |
169 |
""" |
170 |
|
171 |
self.__format = format |
172 |
self.__vtk_image_reader = self.getImageReader() |
173 |
|
174 |
def getImageReader(self): |
175 |
""" |
176 |
Return the appropriate image reader based on the supplied image |
177 |
format. |
178 |
|
179 |
@rtype: vtkImageReader2 (i.e. vtkJPEGReader, etc) |
180 |
@return: Image reader |
181 |
""" |
182 |
|
183 |
if(self.__format == ImageFormat.JPG): |
184 |
return vtk.vtkJPEGReader() |
185 |
elif(self.__format == ImageFormat.BMP): |
186 |
return vtk.vtkBMPReader() |
187 |
elif(self.__format == ImageFormat.PNM): |
188 |
return vtk.vtkPNMReader() |
189 |
elif(self.__format == ImageFormat.PNG): |
190 |
return vtk.vtkPNGReader() |
191 |
elif(self.__format == ImageFormat.TIF): |
192 |
return vtk.vtkTIFFReader() |
193 |
|
194 |
def setFileName(self, file_name): |
195 |
""" |
196 |
Set the image file name. |
197 |
|
198 |
@type file_name: String |
199 |
@param file_name: Image file name to be read |
200 |
""" |
201 |
|
202 |
self.__vtk_image_reader.SetFileName(file_name) |
203 |
|
204 |
def _getOutput(self): |
205 |
""" |
206 |
Return the output of the image reader. |
207 |
|
208 |
@rtype: vtkImageData |
209 |
@return Image data |
210 |
""" |
211 |
|
212 |
return self.__vtk_image_reader.GetOutput() |
213 |
|