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

Added function based scalar colouring to the visualiser.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.