1 |
jongui |
943 |
""" |
2 |
|
|
@author: John NGUI |
3 |
|
|
""" |
4 |
|
|
|
5 |
|
|
import vtk |
6 |
|
|
from constant import Source |
7 |
|
|
|
8 |
|
|
class DataCollector: |
9 |
|
|
""" |
10 |
|
|
Class that defines a data collector which deal with the source |
11 |
|
|
of the data for visualisation. |
12 |
|
|
""" |
13 |
|
|
|
14 |
|
|
def __init__(self, source = Source.XML): |
15 |
|
|
""" |
16 |
|
|
Initialise the data collector. |
17 |
|
|
|
18 |
|
|
@type source: L{Source <constant.Source>} constant |
19 |
|
|
@param source: Source input type |
20 |
|
|
""" |
21 |
|
|
|
22 |
|
|
if(source == Source.XML): # Source is an XML file. |
23 |
|
|
self.__vtk_xml_reader = vtk.vtkXMLUnstructuredGridReader() |
24 |
|
|
|
25 |
|
|
def setFileName(self, file_name): |
26 |
|
|
""" |
27 |
|
|
Set the source file name to read for the data collector. |
28 |
|
|
|
29 |
|
|
@type file_name: String |
30 |
|
|
@param file_name: Name of the file to read |
31 |
|
|
""" |
32 |
|
|
|
33 |
|
|
self.__vtk_xml_reader.SetFileName(file_name) |
34 |
|
|
self.__output = self.__vtk_xml_reader.GetOutput() |
35 |
|
|
# NOTE: Update must be called after SetFileName to make the reader |
36 |
|
|
# up to date. Otherwise, some output values may be incorrect. |
37 |
|
|
self.__vtk_xml_reader.Update() |
38 |
|
|
|
39 |
|
|
def _setActiveScalar(self, scalar): |
40 |
|
|
""" |
41 |
|
|
Specify the scalar field to laod from the source |
42 |
|
|
file for the data collector. |
43 |
|
|
|
44 |
|
|
@type scalar: String |
45 |
|
|
@param scalar: Scalar field to load from the file. |
46 |
|
|
""" |
47 |
|
|
|
48 |
|
|
self._getOutput().GetPointData().SetActiveScalars(scalar) |
49 |
|
|
|
50 |
|
|
def _setActiveVector(self, vector): |
51 |
|
|
""" |
52 |
|
|
Specify the vector field to laod from the source |
53 |
|
|
file for the data collector. |
54 |
|
|
|
55 |
|
|
@type vector: String |
56 |
|
|
@param vector: Vector field to load from the file. |
57 |
|
|
""" |
58 |
|
|
|
59 |
|
|
self._getOutput().GetPointData().SetActiveVectors(vector) |
60 |
|
|
|
61 |
|
|
def _setActiveTensor(self, tensor): |
62 |
|
|
""" |
63 |
|
|
Specify the tensor field to laod from the source |
64 |
|
|
file for the data collector. |
65 |
|
|
|
66 |
|
|
@type tensor: String |
67 |
|
|
@param tensor: Tensor field to load from the file. |
68 |
|
|
""" |
69 |
|
|
|
70 |
|
|
self._getOutput().GetPointData().SetActiveTensors(tensor) |
71 |
|
|
|
72 |
|
|
def _getScalarRange(self): |
73 |
|
|
""" |
74 |
|
|
Return the scalar range. |
75 |
|
|
|
76 |
|
|
@rtype: Two column tuple |
77 |
|
|
@return: Scalar range |
78 |
|
|
""" |
79 |
|
|
|
80 |
|
|
return self._getOutput().GetPointData().GetScalars().GetRange(-1) |
81 |
|
|
|
82 |
|
|
def _getVectorRange(self): |
83 |
|
|
""" |
84 |
|
|
Return the vector range. |
85 |
|
|
|
86 |
|
|
@rtype: Two column tuple |
87 |
|
|
@return: Vector range |
88 |
|
|
""" |
89 |
|
|
|
90 |
|
|
vector_range = self._getOutput().GetPointData().GetVectors().GetRange(-1) |
91 |
|
|
|
92 |
|
|
# NOTE: Generally GetRange(-1) returns the correct vector range. |
93 |
|
|
# However, there are certain data sets where GetRange(-1) seems |
94 |
|
|
# to return incorrect mimimum vector although the maximum vector is |
95 |
|
|
# correct. As a result, the mimimum vector has been hard coded to 0.0 |
96 |
|
|
# to accommodate those incorrect cases. |
97 |
|
|
return (0.0, vector_range[1]) |
98 |
|
|
|
99 |
|
|
def _getTensorRange(self): |
100 |
|
|
""" |
101 |
|
|
Return the tensor range. |
102 |
|
|
|
103 |
|
|
@rtype: Two column table |
104 |
|
|
@return: Tensor range |
105 |
|
|
""" |
106 |
|
|
|
107 |
|
|
return self._getOutput().GetPointData().GetTensors().GetRange() |
108 |
|
|
|
109 |
|
|
def _getOutput(self): |
110 |
|
|
""" |
111 |
|
|
Return the output of the data collector. |
112 |
|
|
|
113 |
|
|
@rtype: vtkUnstructuredGrid |
114 |
|
|
@return: Unstructured grid |
115 |
|
|
""" |
116 |
|
|
|
117 |
|
|
return self.__output |
118 |
|
|
|
119 |
|
|
|
120 |
|
|
from constant import ImageFormat |
121 |
|
|
|
122 |
|
|
class ImageReader: |
123 |
|
|
""" |
124 |
|
|
Class that defines an image reader. |
125 |
|
|
""" |
126 |
|
|
|
127 |
|
|
def __init__(self, format): |
128 |
|
|
""" |
129 |
|
|
Initialise the image reader. |
130 |
|
|
|
131 |
|
|
@type format: String |
132 |
|
|
@param format: Format of the image |
133 |
|
|
""" |
134 |
|
|
|
135 |
|
|
self.__format = format |
136 |
|
|
self.__vtk_image_reader = self.getImageReader() |
137 |
|
|
|
138 |
|
|
def getImageReader(self): |
139 |
|
|
""" |
140 |
|
|
Return the corresponding image reader based on the supplied image |
141 |
|
|
format. |
142 |
|
|
|
143 |
|
|
@rtype: vtkImageReader2 (i.e. vtkJPEGReader, etc) |
144 |
|
|
@return: Image reader |
145 |
|
|
""" |
146 |
|
|
|
147 |
|
|
if(self.__format == ImageFormat.JPG): |
148 |
|
|
return vtk.vtkJPEGReader() |
149 |
|
|
elif(self.__format == ImageFormat.BMP): |
150 |
|
|
return vtk.vtkBMPReader() |
151 |
|
|
elif(self.__format == ImageFormat.PNM): |
152 |
|
|
return vtk.vtkPNMReader() |
153 |
|
|
elif(self.__format == ImageFormat.PNG): |
154 |
|
|
return vtk.vtkPNGReader() |
155 |
|
|
elif(self.__format == ImageFormat.TIF): |
156 |
|
|
return vtk.vtkTIFFReader() |
157 |
|
|
|
158 |
|
|
def setFileName(self, file_name): |
159 |
|
|
""" |
160 |
|
|
Set the image file name. |
161 |
|
|
|
162 |
|
|
@type file_name: String |
163 |
|
|
@param file_name: Image file name which is to be read |
164 |
|
|
""" |
165 |
|
|
|
166 |
|
|
self.__vtk_image_reader.SetFileName(file_name) |
167 |
|
|
|
168 |
|
|
def _getOutput(self): |
169 |
|
|
""" |
170 |
|
|
Return the image reader. |
171 |
|
|
|
172 |
|
|
@rtype: vtkImageData |
173 |
|
|
@return Image data |
174 |
|
|
""" |
175 |
|
|
|
176 |
|
|
return self.__vtk_image_reader.GetOutput() |
177 |
|
|
|