Changeset 3538


Ignore:
Timestamp:
Aug 29, 2006, 4:00:34 PM (18 years ago)
Author:
jack
Message:

Added function based scalar colouring to the visualiser.

Location:
anuga_core/source/anuga/visualiser
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/visualiser/offline.py

    r3537 r3538  
    77class OfflineVisualiser(Visualiser):
    88    """A VTK-powered offline visualiser which runs in its own thread.
     9    In addition to the functions provided by the standard visualiser,
     10    the following additional functions are provided:
     11
     12    precache_height_quantities() - Precache all the vtkpoints
     13    structures for any dynamic height based quantities to render.
    914    """
    1015    def __init__(self, source):
     
    5661        y = ravel(array(fin.variables['y'], Float))
    5762        if dynamic is True:
    58             q = array(fin.variables[quantityName][self.frameNumber], Float)
     63            q = array(fin.variables[quantityName][frameNumber], Float)
    5964        else:
    6065            q = ravel(array(fin.variables[quantityName], Float))
     
    6772        fin.close()
    6873        return points
     74
     75    def precache_height_quantities(self):
     76        """Precache any height-based quantities. Call before rendering
     77        beigns."""
     78        for q in self.height_quantities:
     79            if self.height_dynamic[q] is True:
     80                print 'Precaching %s' % q
     81                for i in range(self.maxFrameNumber + 1): # maxFrameNumber is zero-indexed
     82                    print ' - Frame %d of %d' % (i, self.maxFrameNumber)
     83                    self.vtk_heightQuantityCache[i][q]\
     84                        = self.read_height_quantity(q, True, i)
     85
     86    def build_quantity_dict(self):
     87        quantities = {}
     88        fin = NetCDFFile(self.source, 'r')
     89        for q in filter(lambda n:n != 'x' and n != 'y' and n != 'z' and n != 'time' and n != 'volumes', fin.variables.keys()):
     90            if len(fin.variables[q].shape) == 1: # Not a time-varying quantity
     91                quantities[q] = ravel(array(fin.variables[q], Float))
     92            else: # Time-varying, get the current timestep data
     93                quantities[q] = array(fin.variables[q][self.frameNumber], Float)
     94        fin.close()
     95        return quantities
    6996
    7097    def setup_gui(self):
  • anuga_core/source/anuga/visualiser/run_offline.py

    r3520 r3538  
    22
    33o = OfflineVisualiser("../../../../swollen_viewer/tests/cylinders.sww")
     4#o = OfflineVisualiser("../../../../swollen_viewer/tests/karratha_100m.sww")
    45o.render_quantity_height("elevation", dynamic=False)
    56o.render_quantity_height("stage", dynamic=True)
     7#o.colour_height_quantity('stage', (0.0, 0.0, 0.8))
     8o.colour_height_quantity('stage', (lambda q:q['stage'], 0, 10))
     9#o.precache_height_quantities()
    610o.run()
  • anuga_core/source/anuga/visualiser/visualiser.py

    r3493 r3538  
    11from threading import Event, Thread
    22from Tkinter import Tk, Button, N, E, S, W
    3 from vtk import vtkActor, vtkPolyDataMapper, vtkRenderer
     3from types import FunctionType, TupleType
     4from vtk import vtkActor, vtkFloatArray, vtkPolyDataMapper, vtkRenderer
    45from vtk.tk.vtkTkRenderWidget import vtkTkRenderWidget
    56
     
    1718        self.height_dynamic = {}
    1819        self.height_offset = {}
     20
     21        # Structures for colouring quantities
     22        self.colours_height = {}
    1923
    2024        # Structures used for VTK
     
    8488            actor = self.vtk_actors[quantityName] = vtkActor()
    8589            actor.SetMapper(mapper)
     90            self.vtk_renderer.AddActor(actor)
     91
     92        if self.colours_height.has_key(quantityName):
     93            colour = self.colours_height[quantityName]
     94            if type(colour) == TupleType:
     95                if type(colour[0]) == FunctionType:
     96                    # It's a function, so take colour[1] as the
     97                    # lower bound on the scalar range and
     98                    # colour[2] as the upper bound on the scalar
     99                    # range.
     100                    scalars = vtkFloatArray()
     101                    map(scalars.InsertNextValue, colour[0](self.build_quantity_dict()))
     102                    self.vtk_polyData[quantityName].GetPointData().SetScalars(scalars)
     103                    mapper.SetScalarRange(colour[1:])
     104                    mapper.Update()
     105                else:
     106                    # It's a 3-tuple representing an RGB value.
     107                    actor.GetProperty().SetColor(colour)
     108            else:
     109                actor.GetProperty().SetColor(0.5, 0.5, 0.5)
     110        else:
    86111            actor.GetProperty().SetColor(0.5, 0.5, 0.5)
    87             self.vtk_renderer.AddActor(actor)
    88112
    89113    # --- Colour Coding --- #
    90114
     115    def build_quantity_dict(self):
     116        """Build a dictionary mapping quantity name->list of vertex
     117        values for that quantity. Subclasses are expected to override
     118        this function."""
     119        pass
     120
     121    def colour_height_quantity(self, quantityName, colour=(0.5, 0.5, 0.5)):
     122        """Add colouring to a height based quantity.
     123
     124        The colour parameter can be one of the following:
     125        - a 3-tuple of values in [0,1] to specify R, G, B values
     126        - a 3-tuple of values:
     127          - a function that takes a dictionary mapping quantity name->Numeric array of vertex values.
     128            This function returns a list of vertex values to be used in the colour coding.
     129          - a float for the lower bound on the colouring
     130          - a float for the upper bound on the colouring
     131        """
     132        self.colours_height[quantityName] = colour
     133           
    91134    # --- Vector Fields --- #
    92135
Note: See TracChangeset for help on using the changeset viewer.