1 |
""" |
2 |
@author: John NGUI |
3 |
""" |
4 |
|
5 |
import vtk |
6 |
from mapper import DataSetMapper |
7 |
from actor import Actor3D |
8 |
from lookuptable import LookupTable |
9 |
from outline import Outline |
10 |
from constant import Viewport, Color, Lut, ColorMode |
11 |
from contourmodule import ContourModule |
12 |
from average import CellDataToPointData |
13 |
|
14 |
# NOTE: DataSetMapper, Actor3D and ContourModule were inherited to allow |
15 |
# access to their public methods from the driver. |
16 |
class Contour(DataSetMapper, Actor3D, ContourModule): |
17 |
""" |
18 |
Class that shows a scalar field by contour surfaces. The contour surfaces |
19 |
can either be colored or grey-scaled, depending on the lookup table used. |
20 |
This class can also be used to generate iso surfaces. |
21 |
""" |
22 |
|
23 |
# The SOUTH_WEST default viewport is used when there is only one viewport. |
24 |
# This saves the user from specifying the viewport when there is only one. |
25 |
# If no lut is specified, the color scheme will be used. |
26 |
def __init__(self, scene, data_collector, viewport = Viewport.SOUTH_WEST, |
27 |
lut = Lut.COLOR, cell_to_point = False, outline = True): |
28 |
""" |
29 |
Initialise the Contour. |
30 |
|
31 |
@attention: The source can either be point or cell data. If the |
32 |
source is cell data, a conversion to point data may or may not be |
33 |
required, in order for the object to be rendered correctly. |
34 |
If a conversion is needed, the 'cell_to_point' flag must be set to |
35 |
'True', otherwise 'False' (which is the default). |
36 |
|
37 |
@type scene: L{Scene <scene.Scene>} object |
38 |
@param scene: Scene in which objects are to be rendered on |
39 |
@type data_collector: L{DataCollector <datacollector.DataCollector>} |
40 |
object |
41 |
@param data_collector: Deal with source of data for visualisation |
42 |
@type viewport: L{Viewport <constant.Viewport>} constant |
43 |
@param viewport: Viewport in which objects are to be rendered on |
44 |
@type lut : L{Lut <constant.Lut>} constant |
45 |
@param lut: Lookup table color scheme |
46 |
@type cell_to_point: Boolean |
47 |
@param cell_to_point: Converts cell data to point data (by averaging) |
48 |
@type outline: Boolean |
49 |
@param outline: Places an outline around the domain surface |
50 |
""" |
51 |
|
52 |
self.__data_collector = data_collector |
53 |
self.__viewport = viewport |
54 |
self.__lut = lut |
55 |
self.__cell_to_point = cell_to_point |
56 |
self.__outline = outline |
57 |
|
58 |
self.__modified = True # Keeps track whether Contour has been modified. |
59 |
ContourModule.__init__(self) |
60 |
DataSetMapper.__init__(self) |
61 |
Actor3D.__init__(self) |
62 |
scene._addVisualizationModules(self) |
63 |
|
64 |
# ----- Outline ----- |
65 |
|
66 |
# NOTE: Changes cannot be made to the Outline's properties from the |
67 |
# driver. |
68 |
if(self.__outline == True): |
69 |
outline = Outline(self.__data_collector._getDataCollectorOutput()) |
70 |
mapper = DataSetMapper() |
71 |
mapper._setupDataSetMapper(outline._getOutlineOutput()) |
72 |
|
73 |
actor3D = Actor3D() |
74 |
actor3D._setupActor3D(mapper._getDataSetMapper()) |
75 |
# Default outline color is black. |
76 |
actor3D.setColor(Color.BLACK) |
77 |
|
78 |
# Default line width is 1. |
79 |
actor3D._setLineWidth(1) |
80 |
scene._addActor3D(self.__viewport, actor3D._getActor3D()) |
81 |
|
82 |
# ----- Contour ----- |
83 |
|
84 |
# NOTE: Lookup table color mapping (color or grey scale) MUST be set |
85 |
# before DataSetMapper. If it is done after DataSetMapper, no effect |
86 |
# will take place. |
87 |
if(self.__lut == Lut.COLOR): # Colored lookup table. |
88 |
lookup_table = LookupTable() |
89 |
lookup_table._setTableValue() |
90 |
elif(self.__lut == Lut.GREY_SCALE): # Grey scaled lookup table. |
91 |
lookup_table = LookupTable() |
92 |
lookup_table._setLookupTableToGreyScale() |
93 |
|
94 |
if(self.__cell_to_point == True): # Converts cell data to point data. |
95 |
c2p = CellDataToPointData( |
96 |
self.__data_collector._getDataCollectorOutput()) |
97 |
self._setupContourModule(c2p._getCellToPointOutput()) |
98 |
elif(self.__cell_to_point == False): # No conversion happens. |
99 |
self._setupContourModule( |
100 |
self.__data_collector._getDataCollectorOutput()) |
101 |
|
102 |
self._setupDataSetMapper(self._getContourModuleOutput(), |
103 |
lookup_table._getLookupTable()) |
104 |
|
105 |
self._setupActor3D(self._getDataSetMapper()) |
106 |
scene._addActor3D(self.__viewport, self._getActor3D()) |
107 |
|
108 |
def _isModified(self): |
109 |
""" |
110 |
Return whether the Contour or DataCollector has been modified. |
111 |
|
112 |
@rtype: Boolean |
113 |
@return: True or False |
114 |
""" |
115 |
|
116 |
return self.__modified or self.__data_collector._isModified() |
117 |
|
118 |
def _render(self, scene): |
119 |
""" |
120 |
Render the contour. |
121 |
|
122 |
@type scene: L{Scene <scene.Scene>} object |
123 |
@param scene: Scene in which objects are to be rendered on |
124 |
""" |
125 |
|
126 |
if (self._isModified() == True): |
127 |
if(self.__data_collector._isScalarSet() == True): |
128 |
self.__data_collector._setActiveScalar() |
129 |
|
130 |
# By default 10 contours are generated and the scalar range is based |
131 |
# on the scalar data range. |
132 |
contours = 10 |
133 |
lower_range = self.__data_collector._getScalarRange()[0] |
134 |
upper_range = self.__data_collector._getScalarRange()[1] |
135 |
|
136 |
if(self._isContoursSet() == True): |
137 |
contours = None |
138 |
if(self._isLowerRangeSet() == True): |
139 |
lower_range = None |
140 |
if(self._isUpperRangeSet() == True): |
141 |
upper_range = None |
142 |
|
143 |
self.generateContours(contours, lower_range, upper_range) |
144 |
self._generateContours() |
145 |
|
146 |
self._setScalarRange(self.__data_collector._getScalarRange()) |
147 |
self.__modified = False |
148 |
|
149 |
|
150 |
############################################################################### |
151 |
|
152 |
|
153 |
from transform import Transform |
154 |
from plane import Plane |
155 |
from cutter import Cutter |
156 |
|
157 |
# NOTE: DataSetMapper, Actor3D, ContourModule, Transform, Plane and Cutter were |
158 |
# inherited to allow access to their public methods from the driver. |
159 |
class ContourOnPlaneCut(DataSetMapper, Actor3D, ContourModule, Transform, |
160 |
Plane, Cutter): |
161 |
""" |
162 |
This class works in a similar way to L{MapOnPlaneCut <map.MapOnPlaneCut>}, |
163 |
except that it shows a scalar field by contour surfaces cut using a plane. |
164 |
""" |
165 |
|
166 |
# The SOUTH_WEST default viewport is used when there is only one viewport. |
167 |
# This saves the user from specifying the viewport when there is only one. |
168 |
# If no lut is specified, the color scheme will be used. |
169 |
def __init__(self, scene, data_collector, viewport = Viewport.SOUTH_WEST, |
170 |
lut = Lut.COLOR, cell_to_point = False, outline = True): |
171 |
|
172 |
""" |
173 |
Initialise the ContourOnPlaneCut. |
174 |
|
175 |
@attention: The source can either be point or cell data. If the |
176 |
source is cell data, a conversion to point data may or may not be |
177 |
required, in order for the object to be rendered correctly. |
178 |
If a conversion is needed, the 'cell_to_point' flag must be set to |
179 |
'True', otherwise 'False' (which is the default). |
180 |
|
181 |
@type scene: L{Scene <scene.Scene>} object |
182 |
@param scene: Scene in which objects are to be rendered on |
183 |
@type data_collector: L{DataCollector <datacollector.DataCollector>} |
184 |
object |
185 |
@param data_collector: Deal with source of data for visualisation |
186 |
@type viewport: L{Viewport <constant.Viewport>} constant |
187 |
@param viewport: Viewport in which objects are to be rendered on |
188 |
@type lut : L{Lut <constant.Lut>} constant |
189 |
@param lut: Lookup table color scheme |
190 |
@type cell_to_point: Boolean |
191 |
@param cell_to_point: Converts cell data to point data (by averaging) |
192 |
@type outline: Boolean |
193 |
@param outline: Places an outline around the domain surface |
194 |
""" |
195 |
|
196 |
self.__data_collector = data_collector |
197 |
self.__viewport = viewport |
198 |
self.__lut = lut |
199 |
self.__cell_to_point = cell_to_point |
200 |
self.__outline = outline |
201 |
|
202 |
# Keeps track whether ContourOnPlaneCut has been modified. |
203 |
self.__modified = True |
204 |
Transform.__init__(self) |
205 |
Plane.__init__(self) |
206 |
Cutter.__init__(self) |
207 |
ContourModule.__init__(self) |
208 |
DataSetMapper.__init__(self) |
209 |
Actor3D.__init__(self) |
210 |
scene._addVisualizationModules(self) |
211 |
|
212 |
# ----- Outline ----- |
213 |
|
214 |
# NOTE: Changes cannot be made to the Outline's properties from the |
215 |
# driver. |
216 |
if(self.__outline == True): |
217 |
outline = Outline(self.__data_collector._getDataCollectorOutput()) |
218 |
mapper = DataSetMapper() |
219 |
mapper._setupDataSetMapper(outline._getOutlineOutput()) |
220 |
|
221 |
actor3D = Actor3D() |
222 |
actor3D._setupActor3D(mapper._getDataSetMapper()) |
223 |
# Default outline color is black. |
224 |
actor3D.setColor(Color.BLACK) |
225 |
|
226 |
# Default line width is 1. |
227 |
actor3D._setLineWidth(1) |
228 |
scene._addActor3D(self.__viewport, actor3D._getActor3D()) |
229 |
|
230 |
# ----- Contour on a cut plane ----- |
231 |
|
232 |
# NOTE: Lookup table color mapping (color or grey scale) MUST be set |
233 |
# before DataSetMapper. If it is done after DataSetMapper, no effect |
234 |
# will take place. |
235 |
if(self.__lut == Lut.COLOR): # Colored lookup table. |
236 |
lookup_table = LookupTable() |
237 |
lookup_table._setTableValue() |
238 |
elif(self.__lut == Lut.GREY_SCALE): # Grey scaled lookup table. |
239 |
lookup_table = LookupTable() |
240 |
lookup_table._setLookupTableToGreyScale() |
241 |
|
242 |
self._setupPlane(self._getTransform()) |
243 |
|
244 |
if(self.__cell_to_point == True): # Converts cell data to point data. |
245 |
c2p = CellDataToPointData(self.__data_collector._getOutput()) |
246 |
self._setupCutter(c2p._getCellToPointOutput(), self._getPlane()) |
247 |
elif(self.__cell_to_point == False): # No conversion happens. |
248 |
self._setupCutter(self.__data_collector._getDataCollectorOutput(), |
249 |
self._getPlane()) |
250 |
|
251 |
self._setupContourModule(self._getCutterOutput()) |
252 |
self._setupDataSetMapper(self._getContourModuleOutput(), |
253 |
lookup_table._getLookupTable()) |
254 |
|
255 |
self._setupActor3D(self._getDataSetMapper()) |
256 |
scene._addActor3D(self.__viewport, self._getActor3D()) |
257 |
|
258 |
def _isModified(self): |
259 |
""" |
260 |
Return whether the ContourOnPlaneCut or DataCollector has been modified. |
261 |
|
262 |
@rtype: Boolean |
263 |
@return: True or False |
264 |
""" |
265 |
|
266 |
return self.__modified or self.__data_collector._isModified() |
267 |
|
268 |
def _render(self, scene): |
269 |
""" |
270 |
Render the contour cut using a plane. |
271 |
|
272 |
@type scene: L{Scene <scene.Scene>} object |
273 |
@param scene: Scene in which objects are to be rendered on |
274 |
""" |
275 |
|
276 |
if (self._isModified() == True): |
277 |
if(self.__data_collector._isScalarSet() == True): |
278 |
self.__data_collector._setActiveScalar() |
279 |
|
280 |
# By default 10 contours are generated and the scalar range is based |
281 |
# on the scalar data range. |
282 |
contours = 10 |
283 |
lower_range = self.__data_collector._getScalarRange()[0] |
284 |
upper_range = self.__data_collector._getScalarRange()[1] |
285 |
|
286 |
if(self._isContoursSet() == True): |
287 |
contours = None |
288 |
if(self._isLowerRangeSet() == True): |
289 |
lower_range = None |
290 |
if(self._isUpperRangeSet() == True): |
291 |
upper_range = None |
292 |
|
293 |
self.generateContours(contours, lower_range, upper_range) |
294 |
self._generateContours() |
295 |
|
296 |
self._setScalarRange(self.__data_collector._getScalarRange()) |
297 |
self.__modified = False |
298 |
|
299 |
|
300 |
############################################################################### |
301 |
|
302 |
|
303 |
from clipper import Clipper |
304 |
|
305 |
# NOTE: DataSetMapper, Actor3D, ContourModule, Transform, Plane and Clipper |
306 |
# were inherited to allow access to their public methods from the driver. |
307 |
class ContourOnPlaneClip(DataSetMapper, Actor3D, ContourModule, Transform, |
308 |
Plane, Clipper): |
309 |
""" |
310 |
This class works in a similar way to L{MapOnPlaneClip <map.MapOnPlaneClip>} |
311 |
, except that it shows a scalar field by contour surfaces clipped using |
312 |
a plane. |
313 |
""" |
314 |
|
315 |
# The SOUTH_WEST default viewport is used when there is only one viewport. |
316 |
# This saves the user from specifying the viewport when there is only one. |
317 |
# If no lut is specified, the color scheme will be used. |
318 |
def __init__(self, scene, data_collector, viewport = Viewport.SOUTH_WEST, |
319 |
lut = Lut.COLOR, cell_to_point = False, outline = True): |
320 |
|
321 |
""" |
322 |
Initialise the ContourOnPlaneClip. |
323 |
|
324 |
@attention: The source can either be point or cell data. If the |
325 |
source is cell data, a conversion to point data may or may not be |
326 |
required, in order for the object to be rendered correctly. |
327 |
If a conversion is needed, the 'cell_to_point' flag must be set to |
328 |
'True', otherwise 'False' (which is the default). |
329 |
|
330 |
@type scene: L{Scene <scene.Scene>} object |
331 |
@param scene: Scene in which objects are to be rendered on |
332 |
@type data_collector: L{DataCollector <datacollector.DataCollector>} |
333 |
object |
334 |
@param data_collector: Deal with source of data for visualisation |
335 |
@type viewport: L{Viewport <constant.Viewport>} constant |
336 |
@param viewport: Viewport in which objects are to be rendered on |
337 |
@type lut : L{Lut <constant.Lut>} constant |
338 |
@param lut: Lookup table color scheme |
339 |
@type cell_to_point: Boolean |
340 |
@param cell_to_point: Converts cell data to point data (by averaging) |
341 |
@type outline: Boolean |
342 |
@param outline: Places an outline around the domain surface |
343 |
""" |
344 |
|
345 |
self.__data_collector = data_collector |
346 |
self.__viewport = viewport |
347 |
self.__lut = lut |
348 |
self.__cell_to_point = cell_to_point |
349 |
self.__outline = outline |
350 |
|
351 |
# Keeps track whether ContourOnPlaneClip has been modified. |
352 |
self.__modified = True |
353 |
Transform.__init__(self) |
354 |
Plane.__init__(self) |
355 |
Clipper.__init__(self) |
356 |
ContourModule.__init__(self) |
357 |
DataSetMapper.__init__(self) |
358 |
Actor3D.__init__(self) |
359 |
scene._addVisualizationModules(self) |
360 |
|
361 |
# ----- Outline ----- |
362 |
|
363 |
# NOTE: Changes cannot be made to the Outline's properties from the |
364 |
# driver. |
365 |
if(self.__outline == True): |
366 |
outline = Outline(self.__data_collector._getDataCollectorOutput()) |
367 |
mapper = DataSetMapper() |
368 |
mapper._setupDataSetMapper(outline._getOutlineOutput()) |
369 |
|
370 |
actor3D = Actor3D() |
371 |
actor3D._setupActor3D(mapper._getDataSetMapper()) |
372 |
# Default outline color is black. |
373 |
actor3D.setColor(Color.BLACK) |
374 |
|
375 |
# Default line width is 1. |
376 |
actor3D._setLineWidth(1) |
377 |
scene._addActor3D(self.__viewport, actor3D._getActor3D()) |
378 |
|
379 |
# ----- Contour on a clipped plane ----- |
380 |
|
381 |
# NOTE: Lookup table color mapping (color or grey scale) MUST be set |
382 |
# before DataSetMapper. If it is done after DataSetMapper, no effect |
383 |
# will take place. |
384 |
if(self.__lut == Lut.COLOR): # Colored lookup table. |
385 |
lookup_table = LookupTable() |
386 |
lookup_table._setTableValue() |
387 |
elif(self.__lut == Lut.GREY_SCALE): # Grey scaled lookup table. |
388 |
lookup_table = LookupTable() |
389 |
lookup_table._setLookupTableToGreyScale() |
390 |
|
391 |
self._setupPlane(self._getTransform()) |
392 |
|
393 |
if(self.__cell_to_point == True): # Converts cell data to point data. |
394 |
c2p = CellDataToPointData( |
395 |
self.__data_collector._getDataCollectorOutput()) |
396 |
self._setupClipper(c2p._getCellToPointOutput(), self._getPlane()) |
397 |
elif(self.__cell_to_point == False): # No conversion happens. |
398 |
self._setupClipper(self.__data_collector._getDataCollectorOutput(), |
399 |
self._getPlane()) |
400 |
|
401 |
self._setClipFunction() |
402 |
self._setupContourModule(self._getClipperOutput()) |
403 |
|
404 |
self._setupDataSetMapper(self._getContourModuleOutput(), |
405 |
lookup_table._getLookupTable()) |
406 |
|
407 |
self._setupActor3D(self._getDataSetMapper()) |
408 |
scene._addActor3D(self.__viewport, self._getActor3D()) |
409 |
|
410 |
def _isModified(self): |
411 |
""" |
412 |
Return whether the ContourOnPlaneClip or DataCollector has been |
413 |
modified. |
414 |
|
415 |
@rtype: Boolean |
416 |
@return: True or False |
417 |
""" |
418 |
|
419 |
return self.__modified or self.__data_collector._isModified() |
420 |
|
421 |
def _render(self, scene): |
422 |
""" |
423 |
Render the contour clip using a plane. |
424 |
|
425 |
@type scene: L{Scene <scene.Scene>} object |
426 |
@param scene: Scene in which objects are to be rendered on |
427 |
""" |
428 |
|
429 |
if (self._isModified() == True): |
430 |
if(self.__data_collector._isScalarSet() == True): |
431 |
self.__data_collector._setActiveScalar() |
432 |
|
433 |
# By default 10 contours are generated and the scalar range is based |
434 |
# on the scalar data range. |
435 |
contours = 10 |
436 |
lower_range = self.__data_collector._getScalarRange()[0] |
437 |
upper_range = self.__data_collector._getScalarRange()[1] |
438 |
|
439 |
if(self._isContoursSet() == True): |
440 |
contours = None |
441 |
if(self._isLowerRangeSet() == True): |
442 |
lower_range = None |
443 |
if(self._isUpperRangeSet() == True): |
444 |
upper_range = None |
445 |
|
446 |
self.generateContours(contours, lower_range, upper_range) |
447 |
self._generateContours() |
448 |
|
449 |
self._setScalarRange(self.__data_collector._getScalarRange()) |
450 |
self.__modified = False |
451 |
|
452 |
|