[2212] | 1 | # VTK-Based visualiser |
---|
| 2 | # intended to replace realtime_visualisation_new.py |
---|
| 3 | |
---|
| 4 | import threading |
---|
| 5 | import Tkinter |
---|
| 6 | import vtk |
---|
[2265] | 7 | from Numeric import * |
---|
[2212] | 8 | from vtk.tk.vtkTkRenderWidget import vtkTkRenderWidget |
---|
[2421] | 9 | #from vtk_visualiser_ext import make_vtkpoints |
---|
[2212] | 10 | |
---|
| 11 | class Visualiser(threading.Thread): |
---|
| 12 | |
---|
| 13 | """A VTK-powered visualiser, designed to replace the VPython one. |
---|
| 14 | Intended to be run in its own thread. |
---|
| 15 | |
---|
| 16 | Customisation options for the visualiser are as follows: |
---|
| 17 | |
---|
| 18 | setup: Dictionary mapping quantity name -> boolean. |
---|
| 19 | if setup[q] is true, draw the quantity when the visualiser starts. |
---|
[2220] | 20 | |
---|
[2212] | 21 | updating: Dictionary mapping quantity name -> boolean. |
---|
| 22 | if updating[q] is true, update the rendering of this quantity each |
---|
| 23 | timestep. |
---|
[2220] | 24 | |
---|
[2212] | 25 | qcolor: Dictionary mapping quantity name -> (float, float, float) |
---|
| 26 | if the name of a quantity is found in qcolor, the colour specified (in |
---|
| 27 | (r, g, b) from 0.0 to 1.0) is used for display of that quantity instead of |
---|
| 28 | (0.5, 0.5, 0.5) (the default) |
---|
[2220] | 29 | |
---|
[2212] | 30 | scale_z: Dictionary mapping quantity name -> float |
---|
| 31 | Override the z scaling of this quantity with the float specified. |
---|
| 32 | |
---|
| 33 | default_scale_z: float |
---|
| 34 | multiply all z coordinates by this amount unless an overriding value |
---|
| 35 | exists in scale_z. |
---|
| 36 | """ |
---|
| 37 | |
---|
| 38 | def __init__(self, domain, default_scale_z=1.0, rect=None, title='Test'): |
---|
| 39 | threading.Thread.__init__(self) |
---|
| 40 | # Initialise data structures. setup and updating are maps |
---|
| 41 | # quantity name -> boolean, setup means to render it, |
---|
| 42 | # update means to update with time. |
---|
| 43 | # qcolor maps quantity name -> (float, float, float): the colour (r, g, b) |
---|
| 44 | self.setup = {} |
---|
| 45 | self.updating = {} |
---|
| 46 | self.qcolor = {} |
---|
| 47 | self.scale_z = {} |
---|
[2334] | 48 | self.coloring = {} |
---|
[2212] | 49 | self.default_scale_z = default_scale_z |
---|
| 50 | self.domain = domain |
---|
| 51 | self.vertices = domain.vertex_coordinates |
---|
[2386] | 52 | |
---|
[2212] | 53 | |
---|
[2386] | 54 | |
---|
| 55 | |
---|
[2212] | 56 | self.idle = threading.Event() |
---|
| 57 | self.redraw_ready = threading.Event() |
---|
[2267] | 58 | self.unpaused = threading.Event() |
---|
| 59 | self.unpaused.set() |
---|
[2220] | 60 | |
---|
[2212] | 61 | # Internal use - storage of vtk objects |
---|
| 62 | self.grids = {} |
---|
[2334] | 63 | self.scalars = {} |
---|
[2212] | 64 | self.actors = {} |
---|
| 65 | self.polydata = {} |
---|
| 66 | self.mappers = {} |
---|
| 67 | |
---|
| 68 | # Default options |
---|
| 69 | for x in self.domain.quantities: |
---|
| 70 | self.setup[x] = False |
---|
| 71 | self.updating[x] = False |
---|
| 72 | |
---|
| 73 | # Bounding box. |
---|
| 74 | if rect is None: |
---|
| 75 | self.max_x = max(max(self.vertices[:,0]),max(self.vertices[:,2]),max(self.vertices[:,4])) |
---|
| 76 | self.min_x = min(min(self.vertices[:,0]),min(self.vertices[:,2]),min(self.vertices[:,4])) |
---|
| 77 | self.max_y = max(max(self.vertices[:,1]),max(self.vertices[:,3]),max(self.vertices[:,5])) |
---|
| 78 | self.min_y = min(min(self.vertices[:,1]),min(self.vertices[:,3]),min(self.vertices[:,5])) |
---|
| 79 | else: |
---|
| 80 | self.max_x = rect[2] |
---|
| 81 | self.min_x = rect[0] |
---|
| 82 | self.max_y = rect[3] |
---|
| 83 | self.min_y = rect[1] |
---|
| 84 | |
---|
| 85 | self.range_x = self.max_x - self.min_x |
---|
| 86 | self.range_y = self.max_y - self.min_y |
---|
| 87 | self.range_xy = max(self.range_x, self.range_y) |
---|
[2220] | 88 | |
---|
[2212] | 89 | def run(self): |
---|
| 90 | self.initialise_gui() |
---|
| 91 | self.add_axes() |
---|
| 92 | self.setup_all() |
---|
[2221] | 93 | self.root.after(100, self.idle.set) |
---|
[2212] | 94 | self.root.mainloop() |
---|
| 95 | |
---|
| 96 | def initialise_gui(self): |
---|
| 97 | """Prepare the GUI for the Visualiser, and set up the |
---|
| 98 | renderer. |
---|
[2220] | 99 | |
---|
[2212] | 100 | """ |
---|
| 101 | # Using the TK VTK widget allows extendability |
---|
| 102 | # should a gui need adding. |
---|
| 103 | self.root = Tkinter.Tk() |
---|
| 104 | |
---|
| 105 | # Message handling with after |
---|
[2221] | 106 | self.root.after(100, self.redraw) |
---|
[2220] | 107 | |
---|
[2212] | 108 | self.renderWidget = vtkTkRenderWidget(self.root, width=400, height=400) |
---|
| 109 | self.renderWidget.pack(expand='true', fill='both') |
---|
| 110 | self.renderWindow = self.renderWidget.GetRenderWindow() |
---|
| 111 | self.renderer = vtk.vtkRenderer() |
---|
| 112 | self.renderWindow.AddRenderer(self.renderer) |
---|
| 113 | |
---|
[2214] | 114 | self.quitButton = Tkinter.Button(self.root, text='Quit', command=self.shutdown) |
---|
[2269] | 115 | self.quitButton.pack(side=Tkinter.BOTTOM) |
---|
[2220] | 116 | |
---|
[2267] | 117 | self.pauseButton = Tkinter.Button(self.root, text='Pause', command=self.unpaused.clear) |
---|
| 118 | self.pauseButton.pack(side=Tkinter.LEFT) |
---|
| 119 | |
---|
| 120 | self.resumeButton = Tkinter.Button(self.root, text='Resume', command=self.unpaused.set) |
---|
| 121 | self.resumeButton.pack(side=Tkinter.LEFT) |
---|
[2386] | 122 | |
---|
| 123 | #self.w2if = vtk.vtkWindowToImageFilter() |
---|
| 124 | #self.w2if.SetInput(self.renderWindow) |
---|
| 125 | |
---|
| 126 | #self.pnmWr = vtk.vtkPNMWriter() |
---|
| 127 | #self.pnmWr.SetInput(self.w2if.GetOutput()) |
---|
[2267] | 128 | |
---|
[2386] | 129 | |
---|
[2212] | 130 | def add_axes(self): |
---|
| 131 | """Add axes to this Visualiser |
---|
| 132 | """ |
---|
| 133 | pass |
---|
| 134 | |
---|
| 135 | def setup_all(self): |
---|
| 136 | """Draw in the data that is specified by setup or update |
---|
| 137 | """ |
---|
| 138 | |
---|
| 139 | self.N_tri = len(self.domain.triangles) |
---|
[2265] | 140 | self.N_vert = len(self.vertices) |
---|
[2212] | 141 | self.cells = vtk.vtkCellArray() |
---|
| 142 | self.vertices = self.domain.get_vertex_coordinates() |
---|
[2265] | 143 | self.vert_index = zeros((self.N_vert,2), Float) |
---|
| 144 | for n in range(self.N_vert): |
---|
[2212] | 145 | for i in range(3): |
---|
| 146 | self.vert_index[self.domain.triangles[n][i]] = self.vertices[n][i*2:i*2+2] |
---|
| 147 | |
---|
| 148 | # Prepare the list of cells |
---|
| 149 | for t in self.domain.triangles: |
---|
| 150 | self.cells.InsertNextCell(3) |
---|
| 151 | for i in range(3): |
---|
| 152 | self.cells.InsertCellPoint(t[i]) |
---|
| 153 | |
---|
| 154 | # Set up the rendering of each quantity |
---|
| 155 | for q in self.domain.quantities: |
---|
| 156 | if self.setup[q] | self.updating[q]: |
---|
| 157 | self.draw_quantity(q) |
---|
| 158 | |
---|
| 159 | def draw_quantity(self, q): |
---|
| 160 | if self.scale_z.has_key(q): |
---|
| 161 | scale = self.scale_z[q] |
---|
| 162 | else: |
---|
| 163 | scale = self.default_scale_z |
---|
[2334] | 164 | |
---|
[2386] | 165 | if q == 'elevation': |
---|
| 166 | shift = 0.05 |
---|
| 167 | else: |
---|
| 168 | shift = 0.0 |
---|
| 169 | |
---|
[2267] | 170 | ############################################################# |
---|
| 171 | # Disabled the make_vtkpoints call because the extension is |
---|
| 172 | # difficult to get to link under windows |
---|
| 173 | ############################################################# |
---|
| 174 | #self.grids[q] = make_vtkpoints(self.N_tri, |
---|
| 175 | # self.N_vert, |
---|
| 176 | # scale, |
---|
| 177 | # self.domain.quantities[q].vertex_values, |
---|
| 178 | # self.vert_index, |
---|
| 179 | # self.domain.triangles) |
---|
| 180 | #grid = self.grids[q] |
---|
| 181 | ############################################################# |
---|
[2269] | 182 | |
---|
[2267] | 183 | qty_index = zeros(self.N_vert, Float) |
---|
| 184 | for n in range(self.N_tri): |
---|
| 185 | for v in range(3): |
---|
| 186 | qty_index[self.domain.triangles[n][v]] = self.domain.quantities[q].vertex_values[n][v] |
---|
[2269] | 187 | |
---|
[2267] | 188 | self.grids[q] = vtk.vtkPoints() |
---|
[2334] | 189 | self.scalars[q] = vtk.vtkFloatArray() |
---|
[2266] | 190 | grid = self.grids[q] |
---|
[2334] | 191 | scalars = self.scalars[q] |
---|
[2269] | 192 | |
---|
[2267] | 193 | for v in range(self.N_vert): |
---|
| 194 | grid.InsertNextPoint(self.vert_index[v][0], |
---|
| 195 | self.vert_index[v][1], |
---|
[2386] | 196 | shift + qty_index[v] * scale) |
---|
[2334] | 197 | scalars.InsertNextValue(qty_index[v]); |
---|
[2212] | 198 | |
---|
| 199 | # Can't recycle vtkPolyData objects: Apparently they behave |
---|
| 200 | # unusually if the points (i.e. vertex data) is set after |
---|
| 201 | # the polys (i.e. triangle data) |
---|
| 202 | self.polydata[q] = vtk.vtkPolyData() |
---|
| 203 | polydata = self.polydata[q] |
---|
[2220] | 204 | |
---|
[2212] | 205 | polydata.SetPoints(grid) |
---|
[2334] | 206 | if self.coloring[q]: |
---|
| 207 | polydata.GetPointData().SetScalars(scalars); |
---|
[2212] | 208 | polydata.SetPolys(self.cells) |
---|
| 209 | |
---|
| 210 | if self.mappers.has_key(q): |
---|
| 211 | mapper = self.mappers[q] |
---|
| 212 | mapper.SetInput(polydata) |
---|
[2386] | 213 | mapper.SetScalarRange(-10.0,10.0) |
---|
[2212] | 214 | mapper.Update() |
---|
| 215 | else: |
---|
| 216 | self.mappers[q] = vtk.vtkPolyDataMapper() |
---|
| 217 | mapper = self.mappers[q] |
---|
| 218 | mapper.SetInput(polydata) |
---|
| 219 | |
---|
| 220 | if not self.actors.has_key(q): |
---|
| 221 | self.actors[q] = vtk.vtkActor() |
---|
| 222 | actor = self.actors[q] |
---|
| 223 | actor.SetMapper(mapper) |
---|
| 224 | |
---|
| 225 | if self.qcolor.has_key(q): |
---|
| 226 | actor.GetProperty().SetColor(self.qcolor[q]) |
---|
| 227 | else: |
---|
| 228 | actor.GetProperty().SetColor(0.5, 0.5, 0.5) |
---|
| 229 | |
---|
| 230 | self.renderer.AddActor(actor) |
---|
| 231 | |
---|
| 232 | def redraw(self): |
---|
| 233 | if self.redraw_ready.isSet(): |
---|
| 234 | self.redraw_ready.wait() |
---|
| 235 | self.redraw_ready.clear() |
---|
| 236 | for q in self.domain.quantities: |
---|
| 237 | if self.updating[q]: |
---|
| 238 | self.draw_quantity(q) |
---|
[2220] | 239 | |
---|
[2212] | 240 | self.renderWindow.Render() |
---|
[2386] | 241 | |
---|
| 242 | |
---|
| 243 | |
---|
[2212] | 244 | self.root.update_idletasks() |
---|
| 245 | self.idle.set() |
---|
[2334] | 246 | self.root.after(100, self.redraw) |
---|
| 247 | |
---|
[2214] | 248 | def shutdown(self): |
---|
| 249 | self.domain.visualise = False |
---|
[2265] | 250 | self.idle.set() |
---|
[2267] | 251 | self.unpaused.set() |
---|
[2214] | 252 | self.root.withdraw() |
---|
| 253 | self.root.quit() |
---|