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

  ViewVC Help
Powered by ViewVC 1.1.26