/[escript]/trunk/pyvisi/py_src/scene.py
ViewVC logotype

Annotation of /trunk/pyvisi/py_src/scene.py

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26