/[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 1158 - (show annotations)
Tue May 22 04:24:01 2007 UTC (12 years, 5 months ago) by jongui
File MIME type: text/x-python
File size: 9774 byte(s)
Made some changes to fix the memory leak problem. However, the problem still persists for run_escript_with_lazy_evaluation.py, run_streamline.py and run_streamline_with_lazy_evaluation.py.

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

  ViewVC Help
Powered by ViewVC 1.1.26