/[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 1148 - (hide annotations)
Wed May 16 22:45:33 2007 UTC (13 years, 11 months ago) by jongui
File MIME type: text/x-python
File size: 9723 byte(s)
Added the updated files for pyvisi and removed some incorrect reference images.
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     # 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 jongui 1148 for i in range(0, len(self.__visualization_modules)):
241     self.__visualization_modules[i]._render()
242 ksteube 1147
243     self.__vtk_render_window.Render()
244    
245     if(self.__renderer.startswith(Renderer.ONLINE)):
246     # NOTE: Once Start() is executed, the driver will not further
247     # execute any subsequent codes thereafter unless the 'q' or
248     # 'e' keys are pressed.
249     self.__vtk_render_window_interactor.Start()
250    
251     # True for all online renderers except Renderer.ONLINE.
252     if(self.__renderer != Renderer.ONLINE):
253     self.__saveImage(image_name)
254     # True for all display renderers except Renderer.DISPLAY.
255     elif(self.__renderer.startswith(self.__OFFLINE) or
256     self.__renderer != Renderer.DISPLAY):
257     self.__saveImage(image_name)
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 in which the actor3D is to be added to
265     @type actor: vtkActor
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 in which the actor2D is to be added to
277     @type actor: vtkActor2D
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 in which the camera is to be added to
289     @type camera: vtkCamera
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 in which the camera is to be added to
301     @type light: vtkLight
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: List
312     @return: A list of renderer(s)
313     """
314    
315     return self.__vtk_renderer
316    
317 jongui 1148 def _addVisualizationModules(self, module):
318     """
319     Store visualization modules (i.e. Map, Velocity, Ellipsoid, etc)
320     which needs to be rendered.
321    
322     @type module: Visualization object
323     @param module: Visualization object to be rendered
324     """
325    
326     self.__visualization_modules.append(module)
327    

  ViewVC Help
Powered by ViewVC 1.1.26