/[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 1809 - (show annotations)
Thu Sep 25 06:43:44 2008 UTC (11 years, 2 months ago) by ksteube
File MIME type: text/x-python
File size: 11717 byte(s)
Copyright updated in all python files

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

  ViewVC Help
Powered by ViewVC 1.1.26