/[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 1067 - (show annotations)
Wed Mar 28 04:14:13 2007 UTC (15 years, 4 months ago) by jongui
File MIME type: text/x-python
File size: 9465 byte(s)
Added some test for the datacollector, map and scene modules.
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
11 to be rendered on. Only one scene needs to be created and can display
12 data from one source. However, a scene may be divided into four
13 smaller windows called viewports (if needed). The four viewports in
14 turn can display data from four different sources.
15 """
16
17 def __init__(self, renderer = Renderer.ONLINE, num_viewport = 1,
18 x_size = 1152, y_size = 864):
19 """
20 Initialise the scene.
21
22 @type renderer: String
23 @param renderer: Type of renderer (i.e.Renderer.ONLINE, etc)
24 @type num_viewport: Number
25 @param num_viewport: Number of viewport(s) in the scene. Either 1 or 4
26 @type x_size: Number
27 @param x_size: Size of the render window along the x-axis
28 @type y_size: Number
29 @param y_size: Size of the render window along the y-axis
30 """
31
32 self.__renderer = renderer
33 self.__num_viewport = num_viewport
34 self.__x_size = x_size
35 self.__y_size = y_size
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
78 def __createViewport(self):
79 """
80 Create the viewport(s) in the scene.
81 """
82
83 # Create the renderer(s) for the viewport(s).
84 self.__vtk_renderer = []
85 for viewport in range(0, self.__num_viewport):
86 self.__vtk_renderer.append(vtk.vtkRenderer())
87
88 if(self.__num_viewport == 4):
89 # Renderer for the entire scene (background to the viewports).
90 self.__vtk_renderer_background = vtk.vtkRenderer()
91
92 # Specify the positioning of the four viewports (between 0 and 1).
93 self.__vtk_renderer[Viewport.SOUTH_WEST].SetViewport(
94 0.0, 0.0, 0.5, 0.5)
95 self.__vtk_renderer[Viewport.NORTH_WEST].SetViewport(
96 0.0, 0.5013, 0.5, 1)
97 self.__vtk_renderer[Viewport.NORTH_EAST].SetViewport(
98 0.501, 0.5013, 1, 1)
99 self.__vtk_renderer[Viewport.SOUTH_EAST].SetViewport(
100 0.501, 0.0, 1.0, 0.5)
101
102 def setBackground(self, color):
103 """
104 Set the background color of the scene.
105
106 @type color: L{Color <constant.Color>} constant
107 @param color: Scene background color
108 """
109
110 # Color the entire scene (background) black initially.
111 # This is carried out mainly to have the borders between
112 # the viewports visibly black.
113 if(self.__num_viewport == 4):
114 self.__vtk_renderer_background.SetBackground(Color.BLACK)
115
116 for viewport in range(0, self.__num_viewport):
117 self.__vtk_renderer[viewport].SetBackground(color)
118
119 def __addRenderer(self):
120 """
121 Add the renderer(s) to the render window.
122 """
123 # 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. This prevents a window from coming
167 during the rendering process.
168 """
169
170 # Enable offscreen rendering.
171 self.__vtk_render_window.OffScreenRenderingOn()
172
173 def __setupWindowToImage(self):
174 """
175 Setup the window to image filter to convert the output of the render
176 window into an image.
177 """
178
179 self.__vtk_window_to_image = vtk.vtkWindowToImageFilter()
180 self.__vtk_window_to_image.SetInput(self.__vtk_render_window)
181 self.__vtk_image_writer = self.__getImageWriter()
182
183
184 def __getImageWriter(self):
185 """
186 Return the appropriate image writer based on the specified
187 renderer.
188
189 @rtype: vtkImageWriter
190 @return: Image writer
191 """
192
193 if(self.__renderer.endswith(self.__JPG)):
194 return vtk.vtkJPEGWriter()
195 elif(self.__renderer.endswith(self.__BMP)):
196 return vtk.vtkBMPWriter()
197 elif(self.__renderer.endswith(self.__PNM)):
198 return vtk.vtkPNMWriter()
199 elif(self.__renderer.endswith(self.__PNG)):
200 return vtk.vtkPNGWriter()
201 elif(self.__renderer.endswith(self.__TIF)):
202 return vtk.vtkTIFFWriter()
203 elif(self.__renderer.endswith(self.__PS)):
204 return vtk.vtkPostScriptWriter()
205
206 def __saveImage(self, image_name):
207 """
208 Save the rendered object as an image.
209
210 @type image_name: String
211 @param image_name: Name of the saved image.
212 """
213
214 # NOTE: Render and Modified must be called everytime before writing
215 # an image. Otherwise, only the first image will always be saved.
216 # This is due to the architecture of VTK.
217 #self.__vtk_render_window.Render()
218 self.__vtk_window_to_image.Modified()
219
220 # Retrieve rendered object from the window and convert it into an
221 # image.
222 self.__vtk_image_writer.SetInput(
223 self.__vtk_window_to_image.GetOutput())
224 self.__vtk_image_writer.SetFileName(image_name)
225 self.__vtk_image_writer.Write()
226
227 def __animate(self):
228 """
229 Animate the object onto the scene on-the-fly. No interaction can occur.
230 """
231
232 self.__vtk_render_window.Render()
233
234 def render(self, image_name = None):
235 """
236 Render the object either onto the scene or save it as an image or both.
237 (depending on the type of renderer).
238 """
239
240 # With Render() ONLY, the rendered object is animated onto the
241 # scene on-the-fly and no interaction can occur.
242 self.__vtk_render_window.Render()
243
244 if(self.__renderer.startswith(Renderer.ONLINE)):
245 # NOTE: Once Start is executed, the driver will not further execute
246 # any subsequent codes thereafter unless the 'q' or 'e' keys are
247 # pressed.
248 self.__vtk_render_window_interactor.Start()
249
250 # True for all online renderers except Renderer.ONLINE.
251 if(self.__renderer != Renderer.ONLINE):
252 self.__saveImage(image_name)
253 # True for all display renderers except Renderer.DISPLAY.
254 elif(self.__renderer.startswith(self.__OFFLINE) or
255 self.__renderer != Renderer.DISPLAY):
256 self.__saveImage(image_name)
257
258
259 def _addActor3D(self, viewport, actor):
260 """
261 Add the actor3D to the appropriate viewport.
262
263 @type viewport: L{Viewport <constant.Viewport>} constant
264 @param viewport: Viewport which the actor3D is to be added to
265 @type actor: L{Actor3D <actor.Actor3D>} object
266 @param actor: Actor3D which is to be added to the viewport
267 """
268
269 self.__vtk_renderer[viewport].AddActor(actor)
270
271 def _addActor2D(self, viewport, actor):
272 """
273 Add the actor2D to the appropriate viewport.
274
275 @type viewport: L{Viewport <constant.Viewport>} constant
276 @param viewport: Viewport which the actor2D is to be added to
277 @type actor: L{Actor2D <actor.Actor2D>} object
278 @param actor: Actor2D which is to be added to the viewport
279 """
280
281 self.__vtk_renderer[viewport].AddActor2D(actor)
282
283 def _setActiveCamera(self, viewport, camera):
284 """
285 Set the camera to the appropriate viewport.
286
287 @type viewport: L{Viewport <constant.Viewport>} constant
288 @param viewport: Viewport which the camera is to be added to
289 @type camera: L{Camera <camera.Camera>} object
290 @param camera: Camera which is to be assigned to the viewport
291 """
292
293 self.__vtk_renderer[viewport].SetActiveCamera(camera)
294
295 def _addLight(self, viewport, light):
296 """
297 Add the light to the appropriate viewport.
298
299 @type viewport: L{Viewport <constant.Viewport>} constant
300 @param viewport: Viewport which the camera is to be added to
301 @type light: L{Light <light.Light>} object
302 @param light: Light which is to be assigned to the viewport
303 """
304
305 self.__vtk_renderer[viewport].AddLight(light)
306
307 def _getRenderer(self):
308 """
309 Return the renderer(s)
310
311 @rtype: One or more column tuple of vtkRenderer
312 @return: Renderer(s)
313 """
314
315 return self.__vtk_renderer

  ViewVC Help
Powered by ViewVC 1.1.26