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

  ViewVC Help
Powered by ViewVC 1.1.26