/[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 1115 - (show annotations)
Sun Apr 22 07:18:30 2007 UTC (12 years ago) by jongui
File MIME type: text/x-python
File size: 9370 byte(s)
The problem regarding the increasing use of memory when Map is instantiated is partially fixed. Some problems still persists when the Map instance invoke methods.

Therefore, the run_map.py auto test have been disabled for the time being.
1 """
2 @author: John NGUI
3 """
4
5 import vtk
6 from constant import Renderer, Color, Viewport
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 self.__modules = []
36
37 self.__OFFLINE = "offline"
38 self.__JPG = "jpg"
39 self.__BMP = "bmp"
40 self.__PNM = "pnm"
41 self.__PNG = "png"
42 self.__TIF = "tif"
43 self.__PS = "ps"
44
45 self.__vtk_render_window = vtk.vtkRenderWindow()
46 self.__setupScene()
47
48 def __setupScene(self):
49 """
50 Setup the scene.
51 """
52
53 self.__createViewport()
54 self.__addRenderer()
55 self.setBackground(Color.WHITE) # Default background color is white.
56
57 # Default title bar.
58 self.setTitleBar("Earth Systems Science Computational Centre (ESSCC)")
59 self.__setSize(self.__x_size, self.__y_size)
60
61 # True for Online rendering.
62 if(self.__renderer.startswith(Renderer.ONLINE)):
63 self.__setupOnlineRendering()
64 # True for all Online renderers except Renderer.ONLINE.
65 if(self.__renderer != Renderer.ONLINE):
66 self.__setupWindowToImage()
67 # True for Offline rendering.
68 elif(self.__renderer.startswith(self.__OFFLINE)):
69 self.__setupOfflineRendering()
70 self.__setupWindowToImage()
71 # True for Display rendering.
72 elif(self.__renderer.startswith(Renderer.DISPLAY)):
73 # True for all Display renderers except Renderer.DISPLAY.
74 if(self.__renderer != Renderer.DISPLAY):
75 self.__setupWindowToImage()
76
77 def __createViewport(self):
78 """
79 Create the viewport(s) in the scene.
80 """
81
82 # Create the renderer(s) for the viewport(s).
83 self.__vtk_renderer = []
84 for viewport in range(0, self.__num_viewport):
85 self.__vtk_renderer.append(vtk.vtkRenderer())
86
87 if(self.__num_viewport == 4):
88 # Renderer for the entire scene (background).
89 self.__vtk_renderer_background = vtk.vtkRenderer()
90
91 # Specify the positioning of the four viewports (between 0 and 1).
92 self.__vtk_renderer[Viewport.SOUTH_WEST].SetViewport(
93 0.0, 0.0, 0.5, 0.5)
94 self.__vtk_renderer[Viewport.NORTH_WEST].SetViewport(
95 0.0, 0.5013, 0.5, 1)
96 self.__vtk_renderer[Viewport.NORTH_EAST].SetViewport(
97 0.501, 0.5013, 1, 1)
98 self.__vtk_renderer[Viewport.SOUTH_EAST].SetViewport(
99 0.501, 0.0, 1.0, 0.5)
100
101 def setBackground(self, color):
102 """
103 Set the background color of the scene.
104
105 @type color: L{Color <constant.Color>} constant
106 @param color: Scene background color
107 """
108
109 # Color the entire scene (background) black initially.
110 # This is carried out mainly to have the borders between
111 # the viewports visibly black.
112 if(self.__num_viewport == 4):
113 self.__vtk_renderer_background.SetBackground(Color.BLACK)
114
115 for viewport in range(0, self.__num_viewport):
116 self.__vtk_renderer[viewport].SetBackground(color)
117
118 def __addRenderer(self):
119 """
120 Add the renderer(s) to the render window.
121 """
122
123 # Add the renderer for the black scene (background).
124 if(self.__num_viewport == 4):
125 self.__vtk_render_window.AddRenderer(
126 self.__vtk_renderer_background)
127
128 for viewport in range(0, self.__num_viewport):
129 self.__vtk_render_window.AddRenderer(self.__vtk_renderer[viewport])
130
131 def setTitleBar(self, text):
132 """
133 Set the text on the title bar of the render window.
134
135 @type text: String
136 @param text: Text on the title bar
137 """
138
139 self.__vtk_render_window.SetWindowName(text)
140
141 def __setSize(self, x_size, y_size):
142 """
143 Set the size of the render window.
144
145 @type x_size: Number
146 @param x_size: Size of the render window along the x-axis
147 @type y_size: Number
148 @param y_size: Size of the render window along the y-axis
149 """
150
151 self.__vtk_render_window.SetSize(x_size, y_size)
152
153 def __setupOnlineRendering(self):
154 """
155 Setup the window interactor for online rendering.
156 """
157
158 # Associate the window interactor with the render window.
159 self.__vtk_render_window_interactor = vtk.vtkRenderWindowInteractor()
160 self.__vtk_render_window_interactor.SetRenderWindow(
161 self.__vtk_render_window)
162 self.__vtk_render_window_interactor.Initialize()
163
164 def __setupOfflineRendering(self):
165 """
166 Enables the offline rendering (no window comes up).
167 """
168
169 # Enables the offscreen rendering.
170 self.__vtk_render_window.OffScreenRenderingOn()
171
172 def __setupWindowToImage(self):
173 """
174 Setup the window to image filter to convert the output from the render
175 window into an image.
176 """
177
178 self.__vtk_window_to_image = vtk.vtkWindowToImageFilter()
179 self.__vtk_window_to_image.SetInput(self.__vtk_render_window)
180 self.__vtk_image_writer = self.__getImageWriter()
181
182 def __getImageWriter(self):
183 """
184 Return the appropriate image writer based on the specified renderer.
185
186 @rtype: vtkImageWriter
187 @return: Image writer
188 """
189
190 if(self.__renderer.endswith(self.__JPG)):
191 return vtk.vtkJPEGWriter()
192 elif(self.__renderer.endswith(self.__BMP)):
193 return vtk.vtkBMPWriter()
194 elif(self.__renderer.endswith(self.__PNM)):
195 return vtk.vtkPNMWriter()
196 elif(self.__renderer.endswith(self.__PNG)):
197 return vtk.vtkPNGWriter()
198 elif(self.__renderer.endswith(self.__TIF)):
199 return vtk.vtkTIFFWriter()
200 elif(self.__renderer.endswith(self.__PS)):
201 return vtk.vtkPostScriptWriter()
202
203 def __saveImage(self, image_name):
204 """
205 Save the rendered object as an image.
206
207 @type image_name: String
208 @param image_name: Name of the saved image.
209 """
210
211 # NOTE: Render and Modified must be called everytime before writing
212 # an image. Otherwise, only the first image will always be saved.
213 # This is due to the architecture of VTK.
214 self.__vtk_render_window.Render()
215 self.__vtk_window_to_image.Modified()
216
217 # Retrieve the rendered object from the window and convert it into an
218 # image.
219 self.__vtk_image_writer.SetInput(
220 self.__vtk_window_to_image.GetOutput())
221 self.__vtk_image_writer.SetFileName(image_name)
222 self.__vtk_image_writer.Write()
223
224 def __animate(self):
225 """
226 Animate the rendered object on-the-fly.
227 """
228
229 # With Render() ONLY, the rendered object is animated onto the
230 # scene on-the-fly and no interaction can occur.
231 self.__vtk_render_window.Render()
232
233 def render(self, image_name = None):
234 """
235 Render the object using either the online, offline or display mode.
236 """
237
238 for i in range(0, len(self.__modules)):
239 self.__modules[i]._render()
240
241 self.__vtk_render_window.Render()
242
243 if(self.__renderer.startswith(Renderer.ONLINE)):
244 # NOTE: Once Start() is executed, the driver will not further
245 # execute any subsequent codes thereafter unless the 'q' or
246 # 'e' keys are pressed.
247 self.__vtk_render_window_interactor.Start()
248
249 # True for all online renderers except Renderer.ONLINE.
250 if(self.__renderer != Renderer.ONLINE):
251 self.__saveImage(image_name)
252 # True for all display renderers except Renderer.DISPLAY.
253 elif(self.__renderer.startswith(self.__OFFLINE) or
254 self.__renderer != Renderer.DISPLAY):
255 self.__saveImage(image_name)
256
257 def _addActor3D(self, viewport, actor):
258 """
259 Add the actor3D to the appropriate viewport.
260
261 @type viewport: L{Viewport <constant.Viewport>} constant
262 @param viewport: Viewport in which the actor3D is to be added to
263 @type actor: vtkActor
264 @param actor: Actor3D which is to be added to the viewport
265 """
266
267 self.__vtk_renderer[viewport].AddActor(actor)
268
269 def _addActor2D(self, viewport, actor):
270 """
271 Add the actor2D to the appropriate viewport.
272
273 @type viewport: L{Viewport <constant.Viewport>} constant
274 @param viewport: Viewport in which the actor2D is to be added to
275 @type actor: vtkActor2D
276 @param actor: Actor2D which is to be added to the viewport
277 """
278
279 self.__vtk_renderer[viewport].AddActor2D(actor)
280
281 def _setActiveCamera(self, viewport, camera):
282 """
283 Set the camera to the appropriate viewport.
284
285 @type viewport: L{Viewport <constant.Viewport>} constant
286 @param viewport: Viewport in which the camera is to be added to
287 @type camera: vtkCamera
288 @param camera: Camera which is to be assigned to the viewport
289 """
290
291 self.__vtk_renderer[viewport].SetActiveCamera(camera)
292
293 def _addLight(self, viewport, light):
294 """
295 Add the light to the appropriate viewport.
296
297 @type viewport: L{Viewport <constant.Viewport>} constant
298 @param viewport: Viewport in which the camera is to be added to
299 @type light: vtkLight
300 @param light: Light which is to be assigned to the viewport
301 """
302
303 self.__vtk_renderer[viewport].AddLight(light)
304
305 def _getRenderer(self):
306 """
307 Return the renderer(s)
308
309 @rtype: List
310 @return: A list of renderer(s)
311 """
312
313 return self.__vtk_renderer
314
315 def _addModules(self, module):
316 print "Module: ", module
317 self.__modules.append(module)

  ViewVC Help
Powered by ViewVC 1.1.26