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

  ViewVC Help
Powered by ViewVC 1.1.26