/[escript]/trunk/pyvisi/py_src/scene.py
ViewVC logotype

Contents of /trunk/pyvisi/py_src/scene.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2881 - (show annotations)
Thu Jan 28 02:03:15 2010 UTC (11 years, 1 month ago) by jfenwick
File MIME type: text/x-python
File size: 11785 byte(s)
Don't panic.
Updating copyright stamps

1
2 ########################################################
3 #
4 # Copyright (c) 2003-2010 by University of Queensland
5 # Earth Systems Science Computational Center (ESSCC)
6 # http://www.uq.edu.au/esscc
7 #
8 # Primary Business: Queensland, Australia
9 # Licensed under the Open Software License version 3.0
10 # http://www.opensource.org/licenses/osl-3.0.php
11 #
12 ########################################################
13
14 __copyright__="""Copyright (c) 2003-2010 by University of Queensland
15 Earth Systems Science Computational Center (ESSCC)
16 http://www.uq.edu.au/esscc
17 Primary Business: Queensland, Australia"""
18 __license__="""Licensed under the Open Software License version 3.0
19 http://www.opensource.org/licenses/osl-3.0.php"""
20 __url__="https://launchpad.net/escript-finley"
21
22 """
23 :var __author__: name of author
24 :var __copyright__: copyrights
25 :var __license__: licence agreement
26 :var __url__: url entry point on documentation
27 :var __version__: version
28 :var __date__: date of the version
29 """
30
31 __author__="John Ngui, john.ngui@uq.edu.au"
32
33
34 from constant import Renderer, Color, Viewport, ImageFormat
35 from esys.escript import getMPISizeWorld
36 if getMPISizeWorld()==1: import vtk
37
38 class Scene:
39 """
40 Class that defines a scene. A scene is a window in which objects are to
41 be rendered on. Only one scene needs to be created. However, a scene may
42 be divided into four smaller windows called viewports (if needed).
43 Each viewport can render a different object.
44
45 :attention: If the IV or VRML renderer type is used, then only one viewport can be used (four viewports are not supported for these two cases).
46 """
47
48 def __init__(self, renderer = Renderer.ONLINE, num_viewport = 1,
49 x_size = 1152, y_size = 864):
50 """
51 Initialise the scene.
52
53 :type renderer: `Renderer` constant
54 :param renderer: Type of renderer
55 :type num_viewport: Number
56 :param num_viewport: Number of viewport(s) in the scene. Either 1 or 4
57 :type x_size: Number
58 :param x_size: Size of the render window along the x-axis
59 :type y_size: Number
60 :param y_size: Size of the render window along the y-axis
61 """
62 if getMPISizeWorld()>1:
63 raise ValueError,"pyvisi.Scene is not running on more than one processor."
64
65 self.__renderer = renderer
66 self.__num_viewport = num_viewport
67 self.__x_size = x_size
68 self.__y_size = y_size
69 # Stores the visualization modules (i.e. Map, Velocity, Ellipsoid, etc)
70 # which needs to be rendered.
71 self.__visualization_modules = []
72
73 self.__OFFLINE = "offline"
74 self.__VRML = "vrml"
75 self.__IV = "iv"
76
77 self.__vtk_render_window = vtk.vtkRenderWindow()
78 self.__setupScene()
79
80 def __setupScene(self):
81 """
82 Setup the scene.
83 """
84
85 self.__createViewport()
86 self.__addRenderer()
87 self.setBackground(Color.WHITE) # Default background color is white.
88
89
90 # Default title bar.
91 self.setTitleBar("Earth Systems Science Computational Centre (ESSCC)")
92 self.__setSize(self.__x_size, self.__y_size)
93
94 # True for Online rendering.
95 if(self.__renderer.startswith(Renderer.ONLINE)):
96 self.__setupOnlineRendering()
97 # True for all Online renderers except Renderer.ONLINE.
98 if(self.__renderer != Renderer.ONLINE):
99 self.__setupWindowToImage()
100 # True for Offline rendering.
101 elif(self.__renderer.startswith(self.__OFFLINE)):
102 self.__setupOfflineRendering()
103 self.__setupWindowToImage()
104 # True for Display rendering.
105 elif(self.__renderer.startswith(Renderer.DISPLAY)):
106 # True for all Display renderers except Renderer.DISPLAY.
107 if(self.__renderer != Renderer.DISPLAY):
108 self.__setupWindowToImage()
109
110 def __createViewport(self):
111 """
112 Create the viewport(s) in the scene.
113 """
114
115 # Create the renderer(s) for the viewport(s).
116 self.__vtk_renderer = []
117 for viewport in range(0, self.__num_viewport):
118 self.__vtk_renderer.append(vtk.vtkRenderer())
119
120 if(self.__num_viewport == 4):
121 # Renderer for the entire scene (background).
122 self.__vtk_renderer_background = vtk.vtkRenderer()
123
124 # Specify the positioning of the four viewports (between 0 and 1).
125 self.__vtk_renderer[Viewport.SOUTH_WEST].SetViewport(
126 0.0, 0.0, 0.5, 0.5)
127 self.__vtk_renderer[Viewport.NORTH_WEST].SetViewport(
128 0.0, 0.5013, 0.5, 1)
129 self.__vtk_renderer[Viewport.NORTH_EAST].SetViewport(
130 0.501, 0.5013, 1, 1)
131 self.__vtk_renderer[Viewport.SOUTH_EAST].SetViewport(
132 0.501, 0.0, 1.0, 0.5)
133
134 def setBackground(self, color):
135 """
136 Set the background color of the scene.
137
138 :type color: `Color` constant
139 :param color: Scene background color
140 """
141
142 # Color the entire scene (background) black initially.
143 # This is carried out mainly to have the borders between
144 # the viewports visibly black.
145 if(self.__num_viewport == 4):
146 self.__vtk_renderer_background.SetBackground(Color.BLACK)
147
148 for viewport in range(0, self.__num_viewport):
149 self.__vtk_renderer[viewport].SetBackground(color)
150
151 def __addRenderer(self):
152 """
153 Add the renderer(s) to the render window.
154 """
155
156 # Add the renderer for the black scene (background).
157 if(self.__num_viewport == 4):
158 self.__vtk_render_window.AddRenderer(
159 self.__vtk_renderer_background)
160
161 for viewport in range(0, self.__num_viewport):
162 self.__vtk_render_window.AddRenderer(self.__vtk_renderer[viewport])
163
164 def setTitleBar(self, text):
165 """
166 Set the text on the title bar of the render window.
167
168 :type text: String
169 :param text: Text on the title bar
170 """
171
172 self.__vtk_render_window.SetWindowName(text)
173
174 def __setSize(self, x_size, y_size):
175 """
176 Set the size of the render window.
177
178 :type x_size: Number
179 :param x_size: Size of the render window along the x-axis
180 :type y_size: Number
181 :param y_size: Size of the render window along the y-axis
182 """
183
184 self.__vtk_render_window.SetSize(x_size, y_size)
185
186 def __setupOnlineRendering(self):
187 """
188 Setup the window interactor for online rendering.
189 """
190
191 # Associate the window interactor with the render window.
192 self.__vtk_render_window_interactor = vtk.vtkRenderWindowInteractor()
193 self.__vtk_render_window_interactor.SetRenderWindow(
194 self.__vtk_render_window)
195 self.__vtk_render_window_interactor.Initialize()
196
197 def __setupOfflineRendering(self):
198 """
199 Enables the offline rendering (no window comes up).
200 """
201
202 # Enables the offscreen rendering.
203 self.__vtk_render_window.OffScreenRenderingOn()
204
205 def __setupWindowToImage(self):
206 """
207 Setup the window to image filter to convert the output from the render
208 window into an image, vrml or open inventor file.
209 """
210
211 self.__vtk_window_to_image = vtk.vtkWindowToImageFilter()
212 self.__vtk_window_to_image.SetInput(self.__vtk_render_window)
213 self.__vtk_image_writer = self.__getImageWriter()
214
215 def __getImageWriter(self):
216 """
217 Return the appropriate image writer or exporter based on the
218 specified renderer.
219
220 :rtype: vtkImageWriter or vtkExporter
221 :return: Image writer or exporter
222 """
223
224 if(self.__renderer.endswith(ImageFormat.JPG)):
225 return vtk.vtkJPEGWriter()
226 elif(self.__renderer.endswith(ImageFormat.BMP)):
227 return vtk.vtkBMPWriter()
228 elif(self.__renderer.endswith(ImageFormat.PNM)):
229 return vtk.vtkPNMWriter()
230 elif(self.__renderer.endswith(ImageFormat.PNG)):
231 return vtk.vtkPNGWriter()
232 elif(self.__renderer.endswith(ImageFormat.TIF)):
233 return vtk.vtkTIFFWriter()
234 elif(self.__renderer.endswith(ImageFormat.PS)):
235 return vtk.vtkPostScriptWriter()
236 elif(self.__renderer.endswith(self.__VRML)):
237 return vtk.vtkVRMLExporter() # Generates VRML files (.wrl).
238 elif(self.__renderer.endswith(self.__IV)):
239 return vtk.vtkIVExporter() # Generate OpenInventor files (.iv).
240
241 def __saveImage(self, image_name):
242 """
243 Save the rendered object as an image, vrml or open inventor file.
244
245 :type image_name: String
246 :param image_name: Name of the saved image, vrml or open inventor file
247 """
248
249 # NOTE: Render and Modified must be called everytime before writing
250 # an image, vrml or open inventor file. Otherwise, only the first
251 # object will always be saved. This is due to the architecture of VTK.
252 self.__vtk_render_window.Render()
253 self.__vtk_window_to_image.Modified()
254
255 # Retrieve the rendered object from the window and convert it into an
256 # image, vrml or open inventor file.
257 # True for all writers besides VRML.
258 if(not(self.__renderer.endswith(self.__VRML)) and \
259 not(self.__renderer.endswith(self.__IV))):
260 self.__vtk_image_writer.SetInput(
261 self.__vtk_window_to_image.GetOutput())
262 # True only for VRML and IV.
263 elif(self.__renderer.endswith(self.__VRML) or \
264 self.__renderer.endswith(self.__IV)):
265 self.__vtk_image_writer.SetInput(
266 self.__vtk_render_window)
267 self.__vtk_image_writer.SetFileName(image_name)
268 self.__vtk_image_writer.Write()
269
270 def __animate(self):
271 """
272 Animate the rendered object on-the-fly.
273 """
274
275 # With Render() ONLY, the rendered object is animated onto the
276 # scene on-the-fly and no interaction can occur.
277 self.__vtk_render_window.Render()
278
279 def render(self, image_name = None):
280 """
281 Render the object using either the online, offline or display mode.
282
283 :type image_name: String
284 :param image_name: Name of the saved image, vrml or open inventor file
285 """
286
287 for i in range(0, len(self.__visualization_modules)):
288 self.__visualization_modules[i]._render(self)
289 self.__vtk_render_window.Render()
290
291 self.__vtk_render_window.Render()
292
293 if(self.__renderer.startswith(Renderer.ONLINE)):
294 # NOTE: Once Start() is executed, the driver will not further
295 # execute any subsequent codes thereafter unless the 'q' or
296 # 'e' keys are pressed.
297 self.__vtk_render_window_interactor.Start()
298
299 # True for all online renderers except Renderer.ONLINE.
300 if(self.__renderer != Renderer.ONLINE):
301 self.__saveImage(image_name)
302 # True for all display renderers except Renderer.DISPLAY.
303 elif(self.__renderer.startswith(self.__OFFLINE) or
304 self.__renderer != Renderer.DISPLAY):
305 self.__saveImage(image_name)
306
307 def _addActor3D(self, viewport, actor):
308 """
309 Add the actor3D to the appropriate viewport.
310
311 :type viewport: `Viewport` constant
312 :param viewport: Viewport in which the actor3D is to be added to
313 :type actor: vtkActor
314 :param actor: Actor3D which is to be added to the viewport
315 """
316
317 self.__vtk_renderer[viewport].AddActor(actor)
318
319 def _addActor2D(self, viewport, actor):
320 """
321 Add the actor2D to the appropriate viewport.
322
323 :type viewport: `Viewport` constant
324 :param viewport: Viewport in which the actor2D is to be added to
325 :type actor: vtkActor2D
326 :param actor: Actor2D which is to be added to the viewport
327 """
328
329 self.__vtk_renderer[viewport].AddActor2D(actor)
330
331 def _setActiveCamera(self, viewport, camera):
332 """
333 Set the camera to the appropriate viewport.
334
335 :type viewport: `Viewport` constant
336 :param viewport: Viewport in which the camera is to be added to
337 :type camera: vtkCamera
338 :param camera: Camera which is to be assigned to the viewport
339 """
340
341 self.__vtk_renderer[viewport].SetActiveCamera(camera)
342
343 def _addLight(self, viewport, light):
344 """
345 Add the light to the appropriate viewport.
346
347 :type viewport: `Viewport` constant
348 :param viewport: Viewport in which the camera is to be added to
349 :type light: vtkLight
350 :param light: Light which is to be assigned to the viewport
351 """
352
353 self.__vtk_renderer[viewport].AddLight(light)
354
355 def _getRenderer(self):
356 """
357 Return the renderer(s)
358
359 :rtype: List
360 :return: A list of renderer(s)
361 """
362
363 return self.__vtk_renderer
364
365 def _addVisualizationModules(self, module):
366 """
367 Store visualization modules (i.e. Map, Velocity, Ellipsoid, etc)
368 which needs to be rendered.
369
370 :type module: Visualization modules
371 :param module: Visualization modules to be rendered
372 """
373
374 self.__visualization_modules.append(module)
375

  ViewVC Help
Powered by ViewVC 1.1.26