Changeset 3493


Ignore:
Timestamp:
Aug 15, 2006, 4:45:28 PM (18 years ago)
Author:
jack
Message:

Offline visualiser is working fairly well.

Location:
inundation/visualiser
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/visualiser/realtime.py

    r3465 r3493  
    55    """A VTK-powered realtime visualiser which runs in its own thread.
    66    """
    7     def __init__(self, domain):
    8         Visualiser.__init__(self,domain)
     7    def __init__(self, source):
     8        """The source parameter is assumed to be a Domain.
     9        """
     10        Visualiser.__init__(self, source)
     11
     12        self.running = True
     13
     14        # Synchronisation Constructs
     15        self.sync_idle = Event()
     16        self.sync_idle.clear()
     17        self.sync_unpaused = Event()
     18        self.sync_unpaused.set()
     19        self.sync_redrawReady = Event()
     20        self.sync_redrawReady.clear()
     21
     22    def run(self):
     23        Visualiser.run(self)
     24        self.tk_root.after(100, self.sync_idle.set)
    925
    1026    def setup_gui(self):
     
    2036            self.sync_unpaused.set()
    2137            self.tk_pauseResume.config(text="Pause")
     38
     39    def shutdown(self):
     40        Visualiser.shutdown(self)
     41        self.running = False
     42        self.sync_idle.set()
     43        self.sync_unpaused.set()
  • inundation/visualiser/visualiser.py

    r3465 r3493  
    11from threading import Event, Thread
    2 from Tkinter import Tk
    3 from vtk import vtkRenderer
     2from Tkinter import Tk, Button, N, E, S, W
     3from vtk import vtkActor, vtkPolyDataMapper, vtkRenderer
    44from vtk.tk.vtkTkRenderWidget import vtkTkRenderWidget
    55
     
    77    """Superclass of both the realtime and offline VTK visualisers
    88    """
    9     def __init__(self, domain):
     9    def __init__(self, source):
    1010        Thread.__init__(self)
    1111
    12         self.domain = domain
    13         self.running = True
    14 
    15         # Synchronisation Constructs
    16         self.sync_idle = Event()
    17         self.sync_idle.clear()
    18         self.sync_unpaused = Event()
    19         self.sync_unpaused.set()
    20         self.sync_redrawReady = Event()
    21         self.sync_redrawReady.clear()
     12        self.source = source
    2213
    2314        # Structures for Height Based quantities
     
    2516        self.height_zScales = {}
    2617        self.height_dynamic = {}
     18        self.height_offset = {}
     19
     20        # Structures used for VTK
     21        self.vtk_actors = {}
     22        self.vtk_mappers = {}
     23        self.vtk_polyData = {}
    2724
    2825    def run(self):
    2926        self.setup_gui()
    30         self.tk_root.after(100, self.sync_idle.set)
     27        self.setup_grid()
     28        # Draw Height Quantities
     29        for q in self.height_quantities:
     30            self.update_height_quantity(q, self.height_dynamic[q])
     31            self.draw_height_quantity(q)
    3132        self.tk_root.mainloop()
     33
     34    def redraw_quantities(self, dynamic_only=False):
     35        """Redraw all dynamic quantities, unless dynamic_only is True.
     36        """
     37        # Height quantities
     38        for q in self.height_quantities:
     39            if (dynamic_only is False) or (self.height_dynamic[q]):
     40                self.update_height_quantity(q, self.height_dynamic[q])
     41                self.draw_height_quantity(q)
    3242
    3343    # --- Height Based Rendering --- #
    3444
    35     def render_quantity_height(self, quantityName, zScale=1.0, dynamic=True):
    36         """Instruct the visualiser to render a quantity
    37         using the value at a point as its height.
    38         The value at each point is multiplied by z_scale,
    39         and if dynamic=False, the quantity is not recalculated
    40         on each update.
     45    def setup_grid(self):
     46        """Create the vtkCellArray instance that represents the
     47        triangles. Subclasses are expected to override this function
     48        to read from their source as appropriate.
    4149        """
    42         if quantityName in self.domain.quantities:
    43             self.height_quantities.append(quantityName)
    44             self.height_zScales[quantityName] = zScale
    45             self.height_dynamic[quantityName] = dynamic
     50        pass
     51
     52    def render_quantity_height(self, quantityName, zScale=1.0, offset=0.0, dynamic=True):
     53        """Instruct the visualiser to render a quantity using the
     54        value at a point as its height.  The value at each point is
     55        multiplied by z_scale and is added to offset, and if
     56        dynamic=False, the quantity is not recalculated on each
     57        update.
     58        """
     59        self.height_quantities.append(quantityName)
     60        self.height_zScales[quantityName] = zScale
     61        self.height_offset[quantityName] = offset
     62        self.height_dynamic[quantityName] = dynamic
     63
     64    def update_height_quantity(self, quantityName, dynamic=True):
     65        """Create a vtkPolyData object and store it in
     66        self.vtk_polyData[q]. Subclasses are expected to override this
     67        function.
     68        """
     69        pass
     70
     71
     72    def draw_height_quantity(self, quantityName):
     73        """Use the vtkPolyData and prepare/update the rest of the VTK
     74        rendering pipeline.
     75        """
     76        if self.vtk_mappers.has_key(quantityName):
     77            mapper = self.vtk_mappers[quantityName]
     78        else:
     79            mapper = self.vtk_mappers[quantityName] = vtkPolyDataMapper()
     80        mapper.SetInput(self.vtk_polyData[quantityName])
     81        mapper.Update()
     82
     83        if not self.vtk_actors.has_key(quantityName):
     84            actor = self.vtk_actors[quantityName] = vtkActor()
     85            actor.SetMapper(mapper)
     86            actor.GetProperty().SetColor(0.5, 0.5, 0.5)
     87            self.vtk_renderer.AddActor(actor)
    4688
    4789    # --- Colour Coding --- #
     
    58100
    59101        self.tk_renderWidget = vtkTkRenderWidget(self.tk_root, width=400, height=400)
    60         self.tk_renderWidget.grid(row=0, column=0)
     102        self.tk_renderWidget.grid(row=0, column=0, sticky=N+S+E+W)
     103        self.tk_quit = Button(self.tk_root, text="Quit", command=self.shutdown)
     104        self.tk_quit.grid(row=2, column=0, sticky=E+W)
    61105        self.vtk_renderer = vtkRenderer()
    62106        self.tk_renderWidget.GetRenderWindow().AddRenderer(self.vtk_renderer)
     
    69113
    70114    def redraw(self):
    71         pass
     115        self.tk_renderWidget.GetRenderWindow().Render()
     116        self.tk_root.update_idletasks()
     117        self.tk_root.after(100, self.redraw)
    72118
    73119    def shutdown(self):
    74         self.running = False
    75         self.sync_idle.set()
    76         self.sync_unpaused.set()
    77120        self.tk_root.withdraw()
    78121        self.tk_root.quit()
Note: See TracChangeset for help on using the changeset viewer.