source: anuga_core/source/anuga/visualiser/realtime.py @ 3960

Last change on this file since 3960 was 3960, checked in by jack, 17 years ago

Converted about 1/2 the realtime vis to use accessor functions rather than accessing internals directly.

File size: 5.5 KB
Line 
1from Numeric import Float, zeros
2from Tkinter import Button, E, Tk, W
3from threading import Event
4from visualiser import Visualiser
5from vtk import vtkCellArray, vtkPoints, vtkPolyData
6
7class RealtimeVisualiser(Visualiser):
8    """A VTK-powered realtime 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    update() - Sync the visualiser to the current state of the model.
13    Should be called inside the evolve loop.
14
15    evolveFinished() - Clean up synchronisation constructs that tie the
16    visualiser to the evolve loop. Call this after the evolve loop finishes
17    to ensure a clean shutdown.
18    """
19    def __init__(self, source):
20        """The source parameter is assumed to be a Domain.
21        """
22        Visualiser.__init__(self, source)
23
24        self.running = True
25
26        self.xmin = None
27        self.xmax = None
28        self.ymin = None
29        self.ymax = None
30        self.zmin = None
31        self.zmax = None
32
33        # Synchronisation Constructs
34        self.sync_idle = Event()
35        self.sync_idle.clear()
36        self.sync_unpaused = Event()
37        self.sync_unpaused.set()
38        self.sync_redrawReady = Event()
39        self.sync_redrawReady.clear()
40
41    def run(self):
42        self.alter_tkroot(Tk.after, (100, self.sync_idle.set))
43        Visualiser.run(self)
44
45    def setup_grid(self):
46        self.vtk_cells = vtkCellArray()
47        triangles = self.source.get_triangles()
48        N_tri = len(self.source)
49        verticies = self.source.get_vertex_coordinates()
50        N_vert = len(verticies)
51        # Also build vert_index - a list of the x & y values of each vertex
52        self.vert_index = zeros((N_vert,2), Float)
53        for n in range(N_tri):
54            self.vtk_cells.InsertNextCell(3)
55            for v in range(3):
56                self.vert_index[triangles[n][v]] = verticies[n * 3 + v]
57                self.vtk_cells.InsertCellPoint(triangles[n][v])
58
59    def update_height_quantity(self, quantityName, dynamic=True):
60        N_vert = len(self.source.get_vertex_coordinates())
61        qty_index = zeros(N_vert, Float)
62        triangles = self.source.get_triangles()
63
64        for n in range(len(triangles)):
65            for v in range(3):
66                qty_index[triangles[n][v]] = self.source.get_quantity(quantityName).vertex_values[n][v]
67
68        points = vtkPoints()
69        for v in range(N_vert):
70            points.InsertNextPoint(self.vert_index[v][0],
71                                   self.vert_index[v][1],
72                                   qty_index[v] * self.height_zScales[quantityName]
73                                   + self.height_offset[quantityName])
74            if self.xmin == None or self.xmin > self.vert_index[v][0]:
75                self.xmin = self.vert_index[v][0]
76            if self.xmax == None or self.xmax < self.vert_index[v][0]:
77                self.xmax = self.vert_index[v][0]
78            if self.ymin == None or self.ymin > self.vert_index[v][1]:
79                self.ymin = self.vert_index[v][1]
80            if self.ymax == None or self.ymax < self.vert_index[v][1]:
81                self.ymax = self.vert_index[v][1]
82            if self.zmin == None or self.zmin > qty_index[v] * self.height_zScales[quantityName] + self.height_offset[quantityName]:
83                self.zmin = qty_index[v] * self.height_zScales[quantityName] + self.height_offset[quantityName]
84            if self.zmax == None or self.zmax < qty_index[v] * self.height_zScales[quantityName] + self.height_offset[quantityName]:
85                self.zmax = qty_index[v] * self.height_zScales[quantityName] + self.height_offset[quantityName]
86
87        polydata = self.vtk_polyData[quantityName] = vtkPolyData()
88        polydata.SetPoints(points)
89        polydata.SetPolys(self.vtk_cells)
90
91    def get_3d_bounds(self):
92        return [self.xmin, self.xmax, self.ymin, self.ymax, self.zmin, self.zmax]
93       
94    def build_quantity_dict(self):
95        triangles = self.source.get_triangles()
96        quantities = {}
97        for q in self.source.quantities.keys():
98            quantities[q], _ = self.source.get_quantity(q).get_vertex_values(xy=False)
99        return quantities
100
101    def setup_gui(self):
102        Visualiser.setup_gui(self)
103        self.tk_pauseResume = Button(self.tk_controlFrame, text="Pause", command=self.pauseResume)
104        self.tk_pauseResume.grid(row=1, column=0, sticky=E+W)
105
106    def pauseResume(self):
107        if self.sync_unpaused.isSet():
108            self.sync_unpaused.clear()
109            self.tk_pauseResume.config(text="Resume")
110        else:
111            self.sync_unpaused.set()
112            self.tk_pauseResume.config(text="Pause")
113
114    def shutdown(self):
115        Visualiser.shutdown(self)
116        self.running = False
117        self.sync_idle.set()
118        self.sync_unpaused.set()
119
120    def redraw(self):
121        if self.running and self.sync_unpaused.isSet():
122            self.sync_redrawReady.wait()
123            self.sync_redrawReady.clear()
124            self.redraw_quantities()
125            self.sync_idle.set()
126        Visualiser.redraw(self)
127
128    def update(self):
129        """Sync the visualiser to the domain. Call this in the evolve loop."""
130        if self.running:
131            self.sync_redrawReady.set()
132            self.sync_idle.wait()
133            self.sync_idle.clear()
134            self.sync_unpaused.wait()
135
136    def evolveFinished(self):
137        """Stop the visualiser from waiting on signals from the evolve loop.
138        Call this just after the evolve loop to ensure a clean shutdown."""
139        self.running = False
140        self.sync_redrawReady.set()
Note: See TracBrowser for help on using the repository browser.