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

Annotation of /trunk/pyvisi/py_src/contour.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1189 - (hide annotations)
Thu Jun 7 05:31:25 2007 UTC (13 years, 10 months ago) by jongui
File MIME type: text/x-python
File size: 16289 byte(s)
A Scalar Bar can now be displayed by creating an instance of a 'Legend'. The Scalar Bar can either show the scalar data range or vector data range. Corresponding examples and tests have also been added.
1 ksteube 1147 """
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 jongui 1148 from constant import Viewport, Color, Lut, ColorMode
11 ksteube 1147 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 jongui 1148 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 ksteube 1147
64     # ----- Outline -----
65    
66 jongui 1148 # 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 ksteube 1147
73 jongui 1148 actor3D = Actor3D()
74     actor3D._setupActor3D(mapper._getDataSetMapper())
75 ksteube 1147 # Default outline color is black.
76 jongui 1148 actor3D.setColor(Color.BLACK)
77 ksteube 1147
78     # Default line width is 1.
79 jongui 1148 actor3D._setLineWidth(1)
80 jongui 1158 scene._addActor3D(self.__viewport, actor3D._getActor3D())
81 ksteube 1147
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 jongui 1148 if(self.__lut == Lut.COLOR): # Colored lookup table.
88 ksteube 1147 lookup_table = LookupTable()
89     lookup_table._setTableValue()
90 jongui 1148 elif(self.__lut == Lut.GREY_SCALE): # Grey scaled lookup table.
91 ksteube 1147 lookup_table = LookupTable()
92     lookup_table._setLookupTableToGreyScale()
93    
94 jongui 1148 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 ksteube 1147
102 jongui 1148 self._setupDataSetMapper(self._getContourModuleOutput(),
103 ksteube 1147 lookup_table._getLookupTable())
104    
105 jongui 1148 self._setupActor3D(self._getDataSetMapper())
106 jongui 1158 scene._addActor3D(self.__viewport, self._getActor3D())
107 jongui 1148
108     def _isModified(self):
109     """
110     Return whether the Contour or DataCollector has been modified.
111 ksteube 1147
112 jongui 1148 @rtype: Boolean
113     @return: True or False
114     """
115 ksteube 1147
116 jongui 1148 return self.__modified or self.__data_collector._isModified()
117 ksteube 1147
118 jongui 1158 def _render(self, scene):
119 jongui 1148 """
120     Render the contour.
121 jongui 1158
122     @type scene: L{Scene <scene.Scene>} object
123     @param scene: Scene in which objects are to be rendered on
124 jongui 1148 """
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 jongui 1189 # self._isScalarRangeSet checks whether the scalar range has been
147     # specified by the user. If it has, then the scalar range
148     # read from the source will be ignored.
149     if(not(self._isScalarRangeSet())):
150     self._setScalarRange(self.__data_collector._getScalarRange())
151 jongui 1148 self.__modified = False
152    
153    
154 ksteube 1147 ###############################################################################
155    
156    
157     from transform import Transform
158     from plane import Plane
159     from cutter import Cutter
160    
161     # NOTE: DataSetMapper, Actor3D, ContourModule, Transform, Plane and Cutter were
162     # inherited to allow access to their public methods from the driver.
163     class ContourOnPlaneCut(DataSetMapper, Actor3D, ContourModule, Transform,
164     Plane, Cutter):
165     """
166     This class works in a similar way to L{MapOnPlaneCut <map.MapOnPlaneCut>},
167 jongui 1148 except that it shows a scalar field by contour surfaces cut using a plane.
168 ksteube 1147 """
169    
170     # The SOUTH_WEST default viewport is used when there is only one viewport.
171     # This saves the user from specifying the viewport when there is only one.
172     # If no lut is specified, the color scheme will be used.
173     def __init__(self, scene, data_collector, viewport = Viewport.SOUTH_WEST,
174     lut = Lut.COLOR, cell_to_point = False, outline = True):
175    
176     """
177     Initialise the ContourOnPlaneCut.
178    
179     @attention: The source can either be point or cell data. If the
180     source is cell data, a conversion to point data may or may not be
181     required, in order for the object to be rendered correctly.
182     If a conversion is needed, the 'cell_to_point' flag must be set to
183     'True', otherwise 'False' (which is the default).
184    
185     @type scene: L{Scene <scene.Scene>} object
186     @param scene: Scene in which objects are to be rendered on
187     @type data_collector: L{DataCollector <datacollector.DataCollector>}
188     object
189     @param data_collector: Deal with source of data for visualisation
190     @type viewport: L{Viewport <constant.Viewport>} constant
191     @param viewport: Viewport in which objects are to be rendered on
192     @type lut : L{Lut <constant.Lut>} constant
193     @param lut: Lookup table color scheme
194     @type cell_to_point: Boolean
195     @param cell_to_point: Converts cell data to point data (by averaging)
196     @type outline: Boolean
197     @param outline: Places an outline around the domain surface
198     """
199    
200 jongui 1148 self.__data_collector = data_collector
201     self.__viewport = viewport
202     self.__lut = lut
203     self.__cell_to_point = cell_to_point
204     self.__outline = outline
205    
206     # Keeps track whether ContourOnPlaneCut has been modified.
207     self.__modified = True
208     Transform.__init__(self)
209     Plane.__init__(self)
210     Cutter.__init__(self)
211     ContourModule.__init__(self)
212     DataSetMapper.__init__(self)
213     Actor3D.__init__(self)
214     scene._addVisualizationModules(self)
215 ksteube 1147
216 jongui 1148 # ----- Outline -----
217 ksteube 1147
218 jongui 1148 # NOTE: Changes cannot be made to the Outline's properties from the
219     # driver.
220     if(self.__outline == True):
221     outline = Outline(self.__data_collector._getDataCollectorOutput())
222     mapper = DataSetMapper()
223     mapper._setupDataSetMapper(outline._getOutlineOutput())
224 ksteube 1147
225 jongui 1148 actor3D = Actor3D()
226     actor3D._setupActor3D(mapper._getDataSetMapper())
227 ksteube 1147 # Default outline color is black.
228 jongui 1148 actor3D.setColor(Color.BLACK)
229 ksteube 1147
230     # Default line width is 1.
231 jongui 1148 actor3D._setLineWidth(1)
232 jongui 1158 scene._addActor3D(self.__viewport, actor3D._getActor3D())
233 ksteube 1147
234     # ----- Contour on a cut plane -----
235    
236     # NOTE: Lookup table color mapping (color or grey scale) MUST be set
237     # before DataSetMapper. If it is done after DataSetMapper, no effect
238     # will take place.
239 jongui 1148 if(self.__lut == Lut.COLOR): # Colored lookup table.
240 ksteube 1147 lookup_table = LookupTable()
241     lookup_table._setTableValue()
242 jongui 1148 elif(self.__lut == Lut.GREY_SCALE): # Grey scaled lookup table.
243 ksteube 1147 lookup_table = LookupTable()
244     lookup_table._setLookupTableToGreyScale()
245    
246 jongui 1148 self._setupPlane(self._getTransform())
247 ksteube 1147
248 jongui 1148 if(self.__cell_to_point == True): # Converts cell data to point data.
249     c2p = CellDataToPointData(self.__data_collector._getOutput())
250     self._setupCutter(c2p._getCellToPointOutput(), self._getPlane())
251     elif(self.__cell_to_point == False): # No conversion happens.
252     self._setupCutter(self.__data_collector._getDataCollectorOutput(),
253     self._getPlane())
254 ksteube 1147
255 jongui 1148 self._setupContourModule(self._getCutterOutput())
256     self._setupDataSetMapper(self._getContourModuleOutput(),
257 ksteube 1147 lookup_table._getLookupTable())
258    
259 jongui 1148 self._setupActor3D(self._getDataSetMapper())
260 jongui 1158 scene._addActor3D(self.__viewport, self._getActor3D())
261 jongui 1148
262     def _isModified(self):
263     """
264     Return whether the ContourOnPlaneCut or DataCollector has been modified.
265 ksteube 1147
266 jongui 1148 @rtype: Boolean
267     @return: True or False
268     """
269 ksteube 1147
270 jongui 1148 return self.__modified or self.__data_collector._isModified()
271 ksteube 1147
272 jongui 1158 def _render(self, scene):
273 jongui 1148 """
274     Render the contour cut using a plane.
275 jongui 1158
276     @type scene: L{Scene <scene.Scene>} object
277     @param scene: Scene in which objects are to be rendered on
278 jongui 1148 """
279    
280     if (self._isModified() == True):
281     if(self.__data_collector._isScalarSet() == True):
282     self.__data_collector._setActiveScalar()
283    
284     # By default 10 contours are generated and the scalar range is based
285     # on the scalar data range.
286     contours = 10
287     lower_range = self.__data_collector._getScalarRange()[0]
288     upper_range = self.__data_collector._getScalarRange()[1]
289    
290     if(self._isContoursSet() == True):
291     contours = None
292     if(self._isLowerRangeSet() == True):
293     lower_range = None
294     if(self._isUpperRangeSet() == True):
295     upper_range = None
296    
297     self.generateContours(contours, lower_range, upper_range)
298     self._generateContours()
299    
300 jongui 1189 # self._isScalarRangeSet checks whether the scalar range has been
301     # specified by the user. If it has, then the scalar range
302     # read from the source will be ignored.
303     if(not(self._isScalarRangeSet())):
304     self._setScalarRange(self.__data_collector._getScalarRange())
305 jongui 1148 self.__modified = False
306    
307    
308 ksteube 1147 ###############################################################################
309    
310    
311     from clipper import Clipper
312    
313     # NOTE: DataSetMapper, Actor3D, ContourModule, Transform, Plane and Clipper
314     # were inherited to allow access to their public methods from the driver.
315     class ContourOnPlaneClip(DataSetMapper, Actor3D, ContourModule, Transform,
316     Plane, Clipper):
317     """
318     This class works in a similar way to L{MapOnPlaneClip <map.MapOnPlaneClip>}
319     , except that it shows a scalar field by contour surfaces clipped using
320     a plane.
321     """
322    
323     # The SOUTH_WEST default viewport is used when there is only one viewport.
324     # This saves the user from specifying the viewport when there is only one.
325     # If no lut is specified, the color scheme will be used.
326     def __init__(self, scene, data_collector, viewport = Viewport.SOUTH_WEST,
327     lut = Lut.COLOR, cell_to_point = False, outline = True):
328    
329     """
330     Initialise the ContourOnPlaneClip.
331    
332     @attention: The source can either be point or cell data. If the
333     source is cell data, a conversion to point data may or may not be
334     required, in order for the object to be rendered correctly.
335     If a conversion is needed, the 'cell_to_point' flag must be set to
336     'True', otherwise 'False' (which is the default).
337    
338     @type scene: L{Scene <scene.Scene>} object
339     @param scene: Scene in which objects are to be rendered on
340     @type data_collector: L{DataCollector <datacollector.DataCollector>}
341     object
342     @param data_collector: Deal with source of data for visualisation
343     @type viewport: L{Viewport <constant.Viewport>} constant
344     @param viewport: Viewport in which objects are to be rendered on
345     @type lut : L{Lut <constant.Lut>} constant
346     @param lut: Lookup table color scheme
347     @type cell_to_point: Boolean
348     @param cell_to_point: Converts cell data to point data (by averaging)
349     @type outline: Boolean
350     @param outline: Places an outline around the domain surface
351     """
352    
353 jongui 1148 self.__data_collector = data_collector
354     self.__viewport = viewport
355     self.__lut = lut
356     self.__cell_to_point = cell_to_point
357     self.__outline = outline
358    
359     # Keeps track whether ContourOnPlaneClip has been modified.
360     self.__modified = True
361     Transform.__init__(self)
362     Plane.__init__(self)
363     Clipper.__init__(self)
364     ContourModule.__init__(self)
365     DataSetMapper.__init__(self)
366     Actor3D.__init__(self)
367     scene._addVisualizationModules(self)
368 ksteube 1147
369 jongui 1148 # ----- Outline -----
370 ksteube 1147
371 jongui 1148 # NOTE: Changes cannot be made to the Outline's properties from the
372     # driver.
373     if(self.__outline == True):
374     outline = Outline(self.__data_collector._getDataCollectorOutput())
375     mapper = DataSetMapper()
376     mapper._setupDataSetMapper(outline._getOutlineOutput())
377 ksteube 1147
378 jongui 1148 actor3D = Actor3D()
379     actor3D._setupActor3D(mapper._getDataSetMapper())
380 ksteube 1147 # Default outline color is black.
381 jongui 1148 actor3D.setColor(Color.BLACK)
382 ksteube 1147
383     # Default line width is 1.
384 jongui 1148 actor3D._setLineWidth(1)
385 jongui 1158 scene._addActor3D(self.__viewport, actor3D._getActor3D())
386 ksteube 1147
387     # ----- Contour on a clipped plane -----
388    
389     # NOTE: Lookup table color mapping (color or grey scale) MUST be set
390     # before DataSetMapper. If it is done after DataSetMapper, no effect
391     # will take place.
392 jongui 1148 if(self.__lut == Lut.COLOR): # Colored lookup table.
393 ksteube 1147 lookup_table = LookupTable()
394     lookup_table._setTableValue()
395 jongui 1148 elif(self.__lut == Lut.GREY_SCALE): # Grey scaled lookup table.
396 ksteube 1147 lookup_table = LookupTable()
397     lookup_table._setLookupTableToGreyScale()
398    
399 jongui 1148 self._setupPlane(self._getTransform())
400 ksteube 1147
401 jongui 1148 if(self.__cell_to_point == True): # Converts cell data to point data.
402     c2p = CellDataToPointData(
403     self.__data_collector._getDataCollectorOutput())
404     self._setupClipper(c2p._getCellToPointOutput(), self._getPlane())
405     elif(self.__cell_to_point == False): # No conversion happens.
406     self._setupClipper(self.__data_collector._getDataCollectorOutput(),
407     self._getPlane())
408 ksteube 1147
409 jongui 1148 self._setClipFunction()
410     self._setupContourModule(self._getClipperOutput())
411 ksteube 1147
412 jongui 1148 self._setupDataSetMapper(self._getContourModuleOutput(),
413 ksteube 1147 lookup_table._getLookupTable())
414    
415 jongui 1148 self._setupActor3D(self._getDataSetMapper())
416 jongui 1158 scene._addActor3D(self.__viewport, self._getActor3D())
417 jongui 1148
418     def _isModified(self):
419     """
420 jongui 1153 Return whether the ContourOnPlaneClip or DataCollector has been
421     modified.
422 ksteube 1147
423 jongui 1148 @rtype: Boolean
424     @return: True or False
425     """
426 ksteube 1147
427 jongui 1148 return self.__modified or self.__data_collector._isModified()
428    
429 jongui 1158 def _render(self, scene):
430 jongui 1148 """
431     Render the contour clip using a plane.
432 jongui 1158
433     @type scene: L{Scene <scene.Scene>} object
434     @param scene: Scene in which objects are to be rendered on
435 jongui 1148 """
436    
437     if (self._isModified() == True):
438     if(self.__data_collector._isScalarSet() == True):
439     self.__data_collector._setActiveScalar()
440    
441     # By default 10 contours are generated and the scalar range is based
442     # on the scalar data range.
443     contours = 10
444     lower_range = self.__data_collector._getScalarRange()[0]
445     upper_range = self.__data_collector._getScalarRange()[1]
446    
447     if(self._isContoursSet() == True):
448     contours = None
449     if(self._isLowerRangeSet() == True):
450     lower_range = None
451     if(self._isUpperRangeSet() == True):
452     upper_range = None
453    
454     self.generateContours(contours, lower_range, upper_range)
455     self._generateContours()
456    
457 jongui 1189 # self._isScalarRangeSet checks whether the scalar range has been
458     # specified by the user. If it has, then the scalar range
459     # read from the source will be ignored.
460     if(not(self._isScalarRangeSet())):
461     self._setScalarRange(self.__data_collector._getScalarRange())
462 jongui 1148 self.__modified = False
463    
464    

  ViewVC Help
Powered by ViewVC 1.1.26