1 |
jongui |
943 |
""" |
2 |
|
|
@author: John NGUI |
3 |
|
|
""" |
4 |
|
|
|
5 |
|
|
import vtk |
6 |
gross |
976 |
import tempfile, os |
7 |
jongui |
943 |
from constant import Source |
8 |
gross |
976 |
try: |
9 |
|
|
import esys.escript |
10 |
|
|
except ImportError: |
11 |
|
|
print "Warning: importing esys.escript failed." |
12 |
jongui |
943 |
|
13 |
|
|
class DataCollector: |
14 |
|
|
""" |
15 |
jongui |
947 |
Class that defines a data collector which dealrs with the source |
16 |
|
|
of data for the visualisation. |
17 |
jongui |
943 |
""" |
18 |
|
|
|
19 |
|
|
def __init__(self, source = Source.XML): |
20 |
|
|
""" |
21 |
|
|
Initialise the data collector. |
22 |
|
|
|
23 |
|
|
@type source: L{Source <constant.Source>} constant |
24 |
jongui |
947 |
@param source: Source data type |
25 |
jongui |
943 |
""" |
26 |
gross |
976 |
|
27 |
|
|
self.__source=source |
28 |
jongui |
943 |
if(source == Source.XML): # Source is an XML file. |
29 |
|
|
self.__vtk_xml_reader = vtk.vtkXMLUnstructuredGridReader() |
30 |
gross |
976 |
# 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 |
jongui |
943 |
|
43 |
gross |
976 |
|
44 |
jongui |
943 |
def setFileName(self, file_name): |
45 |
|
|
""" |
46 |
jongui |
947 |
Set the source file name to read. |
47 |
jongui |
943 |
|
48 |
|
|
@type file_name: String |
49 |
|
|
@param file_name: Name of the file to read |
50 |
|
|
""" |
51 |
gross |
976 |
if self.__source == Source.XML: |
52 |
|
|
self.__vtk_xml_reader.SetFileName(file_name) |
53 |
|
|
self.__output = self.__vtk_xml_reader.GetOutput() |
54 |
jongui |
943 |
|
55 |
gross |
976 |
# 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 |
jongui |
947 |
|
61 |
gross |
976 |
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 |
jongui |
943 |
|
72 |
|
|
def _setActiveScalar(self, scalar): |
73 |
|
|
""" |
74 |
jongui |
947 |
Specify the scalar field to load from the source file. |
75 |
jongui |
943 |
|
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 |
jongui |
947 |
Specify the vector field to load from the source file. |
85 |
jongui |
943 |
|
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 |
jongui |
947 |
Specify the tensor field to load from the source file. |
95 |
jongui |
943 |
|
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 |
jongui |
949 |
@rtype: Two column tuple containing numbers |
107 |
jongui |
943 |
@return: Scalar range |
108 |
|
|
""" |
109 |
jongui |
973 |
|
110 |
|
|
#self._getOutput().GetPointData().SetActiveScalars("temp") |
111 |
jongui |
943 |
return self._getOutput().GetPointData().GetScalars().GetRange(-1) |
112 |
|
|
|
113 |
|
|
def _getVectorRange(self): |
114 |
|
|
""" |
115 |
|
|
Return the vector range. |
116 |
|
|
|
117 |
jongui |
949 |
@rtype: Two column tuple containing numbers |
118 |
jongui |
943 |
@return: Vector range |
119 |
|
|
""" |
120 |
|
|
|
121 |
gross |
976 |
print self._getOutput().GetPointData().GetVectors() |
122 |
jongui |
948 |
vector_range = self._getOutput().GetPointData().GetVectors().GetRange(-1) |
123 |
jongui |
943 |
|
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 |
jongui |
973 |
|
130 |
jongui |
943 |
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 |
jongui |
947 |
############################################################################### |
154 |
|
|
|
155 |
|
|
|
156 |
jongui |
943 |
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 |
jongui |
947 |
Return the appropriate image reader based on the supplied image |
177 |
jongui |
943 |
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 |
jongui |
947 |
@param file_name: Image file name to be read |
200 |
jongui |
943 |
""" |
201 |
|
|
|
202 |
|
|
self.__vtk_image_reader.SetFileName(file_name) |
203 |
|
|
|
204 |
|
|
def _getOutput(self): |
205 |
|
|
""" |
206 |
jongui |
947 |
Return the output of the image reader. |
207 |
jongui |
943 |
|
208 |
|
|
@rtype: vtkImageData |
209 |
|
|
@return Image data |
210 |
|
|
""" |
211 |
|
|
|
212 |
|
|
return self.__vtk_image_reader.GetOutput() |
213 |
|
|
|