/[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 1004 - (show annotations)
Thu Mar 1 04:57:31 2007 UTC (12 years, 5 months ago) by jongui
File MIME type: text/x-python
File size: 8177 byte(s)
Updated the epydocs, comments and organized codes to ease readability.
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 in which objects are to be rendered on.
11 """
12
13 def __init__(self, renderer = Renderer.ONLINE, num_viewport = 1,
14 x_size = 1152, y_size = 864):
15 """
16 Initialise the scene.
17
18 @type renderer: String
19 @param renderer: Type of renderer (i.e.Renderer.ONLINE, etc)
20 @type num_viewport: Number
21 @param num_viewport: Number of viewport(s) in the scene. Either 1 or 4
22 @type x_size: Number
23 @param x_size: Size of the render window along the x-axis
24 @type y_size: Number
25 @param y_size: Size of the render window along the y-axis
26 """
27
28 self.__renderer = renderer
29 self.__num_viewport = num_viewport
30 self.__x_size = x_size
31 self.__y_size = y_size
32
33 self.__vtk_render_window = vtk.vtkRenderWindow()
34 self.__setupScene()
35
36 def __setupScene(self):
37 """
38 Setup the scene.
39 """
40
41 self.__createViewport()
42 self.__addRenderer()
43 self.setBackground(Color.WHITE) # Default background color is white.
44
45 # Default title bar.
46 self.setTitleBar("Earth Systems Science Computational Centre (ESSCC)")
47 self.__setSize(self.__x_size, self.__y_size)
48
49 if(self.__renderer == Renderer.ONLINE): # True for online rendering.
50 self.__setupOnlineRendering()
51 else: # True for offline rendering.
52 self.__setupOfflineRendering()
53
54 def __createViewport(self):
55 """
56 Create the viewport(s) in the scene.
57 """
58
59 # Create the renderer(s) for the viewport(s).
60 self.__vtk_renderer = []
61 for viewport in range(0, self.__num_viewport):
62 self.__vtk_renderer.append(vtk.vtkRenderer())
63
64 if(self.__num_viewport == 4):
65 # Renderer for the entire scene (background to the viewports).
66 self.__vtk_renderer_background = vtk.vtkRenderer()
67
68 # Specify the positioning of the four viewports (between 0 and 1).
69 self.__vtk_renderer[Viewport.SOUTH_WEST].SetViewport(
70 0.0, 0.0, 0.5, 0.5)
71 self.__vtk_renderer[Viewport.NORTH_WEST].SetViewport(
72 0.0, 0.5013, 0.5, 1)
73 self.__vtk_renderer[Viewport.NORTH_EAST].SetViewport(
74 0.501, 0.5013, 1, 1)
75 self.__vtk_renderer[Viewport.SOUTH_EAST].SetViewport(
76 0.501, 0.0, 1.0, 0.5)
77
78 def setBackground(self, color):
79 """
80 Set the background color of the scene.
81
82 @type color: L{Color <constant.Color>} constant
83 @param color: Scene background color
84 """
85
86 # Color the entire scene (background) black initially.
87 # This is carried out mainly to have the borders between
88 # the viewports visibly black.
89 if(self.__num_viewport == 4):
90 self.__vtk_renderer_background.SetBackground(Color.BLACK)
91
92 for viewport in range(0, self.__num_viewport):
93 self.__vtk_renderer[viewport].SetBackground(color)
94
95 def __addRenderer(self):
96 """
97 Add the renderer(s) to the render window.
98 """
99 # Renderer for the black scene (background).
100 if(self.__num_viewport == 4):
101 self.__vtk_render_window.AddRenderer(
102 self.__vtk_renderer_background)
103
104 for viewport in range(0, self.__num_viewport):
105 self.__vtk_render_window.AddRenderer(self.__vtk_renderer[viewport])
106
107 def setTitleBar(self, text):
108 """
109 Set the text on the title bar of the render window.
110
111 @type text: String
112 @param text: Text on the title bar
113 """
114
115 self.__vtk_render_window.SetWindowName(text)
116
117 def __setSize(self, x_size, y_size):
118 """
119 Set the size of the render window.
120
121 @type x_size: Number
122 @param x_size: Size of the render window along the x-axis
123 @type y_size: Number
124 @param y_size: Size of the render window along the y-axis
125 """
126
127 self.__vtk_render_window.SetSize(x_size, y_size)
128
129 def __setupOnlineRendering(self):
130 """
131 Setup the window interactor for online rendering.
132 """
133
134 # Associate the window interactor with the render window.
135 self.__vtk_render_window_interactor = vtk.vtkRenderWindowInteractor()
136 self.__vtk_render_window_interactor.SetRenderWindow(
137 self.__vtk_render_window)
138 self.__vtk_render_window_interactor.Initialize()
139
140 def __setupOfflineRendering(self):
141 """
142 Setup the window to image filter for offline rendering.
143 """
144
145 # Enable offscreen rendering.
146 self.__vtk_render_window.OffScreenRenderingOn()
147 # Convert the output of the render window into an image.
148 self.__vtk_window_to_image = vtk.vtkWindowToImageFilter()
149 self.__vtk_window_to_image.SetInput(self.__vtk_render_window)
150 self.__vtk_image_writer = self.__getImageWriter()
151
152 def __getImageWriter(self):
153 """
154 Return the appropriate image writer based on the specified offline
155 renderer.
156
157 @rtype: vtkImageWriter
158 @return: Image writer
159 """
160
161 if(self.__renderer == Renderer.OFFLINE_JPG):
162 return vtk.vtkJPEGWriter()
163 elif(self.__renderer == Renderer.OFFLINE_BMP):
164 return vtk.vtkBMPWriter()
165 elif(self.__renderer == Renderer.OFFLINE_PNM):
166 return vtk.vtkPNMWriter()
167 elif(self.__renderer == Renderer.OFFLINE_PNG):
168 return vtk.vtkPNGWriter()
169 elif(self.__renderer == Renderer.OFFLINE_TIF):
170 return vtk.vtkTIFFWriter()
171 elif(self.__renderer == Renderer.OFFLINE_PS):
172 return vtk.vtkPostScriptWriter()
173
174 def saveImage(self, image_name):
175 """
176 Save the rendered object as an image.
177
178 @type image_name: String
179 @param image_name: Name of the saved image.
180 """
181
182 try:
183 # NOTE: Render and Modified must be called everytime before writing
184 # an image. Otherwise, only the first image will always be saved.
185 # This is due to the architecture of VTK.
186 self.__vtk_render_window.Render()
187 self.__vtk_window_to_image.Modified()
188
189 # Retrieve rendered object from the window and convert it into an
190 # image.
191 self.__vtk_image_writer.SetInput(
192 self.__vtk_window_to_image.GetOutput())
193 self.__vtk_image_writer.SetFileName(image_name)
194 self.__vtk_image_writer.Write()
195 except AttributeError:
196 print "ERROR: Incorrect use of the '" + self.__renderer + \
197 "' renderer. Kindly, use the 'offline' renderer instead."
198
199 def animate(self):
200 """
201 Animate the object onto the scene on-the-fly. No interaction can occur.
202 """
203
204 self.__vtk_render_window.Render()
205
206 def render(self):
207 """
208 Render the object onto the scene.
209 """
210
211 try:
212 self.__vtk_render_window.Render()
213
214 # NOTE: Once Start is executed, the driver will not further execute
215 # any subsequent codes thereafter.
216 self.__vtk_render_window_interactor.Start()
217 except AttributeError:
218 print "ERROR: Incorrect use of the '" + self.__renderer + \
219 "' renderer. Kindly, switch to the 'online' renderer."
220
221 def _addActor3D(self, viewport, actor):
222 """
223 Add the actor3D to the appropriate viewport.
224
225 @type viewport: L{Viewport <constant.Viewport>} constant
226 @param viewport: Viewport which the actor3D is to be added to
227 @type actor: L{Actor3D <actor.Actor3D>} object
228 @param actor: Actor3D which is to be added to the viewport
229 """
230
231 self.__vtk_renderer[viewport].AddActor(actor)
232
233 def _addActor2D(self, viewport, actor):
234 """
235 Add the actor2D to the appropriate viewport.
236
237 @type viewport: L{Viewport <constant.Viewport>} constant
238 @param viewport: Viewport which the actor2D is to be added to
239 @type actor: L{Actor2D <actor.Actor2D>} object
240 @param actor: Actor2D which is to be added to the viewport
241 """
242
243 self.__vtk_renderer[viewport].AddActor2D(actor)
244
245 def _setActiveCamera(self, viewport, camera):
246 """
247 Set the camera to the appropriate viewport.
248
249 @type viewport: L{Viewport <constant.Viewport>} constant
250 @param viewport: Viewport which the camera is to be added to
251 @type camera: L{Camera <camera.Camera>} object
252 @param camera: Camera which is to be assigned to the viewport
253 """
254
255 self.__vtk_renderer[viewport].SetActiveCamera(camera)
256
257 def _addLight(self, viewport, light):
258 """
259 Add the light to the appropriate viewport.
260
261 @type viewport: L{Viewport <constant.Viewport>} constant
262 @param viewport: Viewport which the camera is to be added to
263 @type light: L{Light <light.Light>} object
264 @param light: Light which is to be assigned to the viewport
265 """
266
267 self.__vtk_renderer[viewport].AddLight(light)
268
269 def _getRenderer(self):
270 """
271 Return the renderer(s)
272
273 @rtype: One or more column tuple of vtkRenderer
274 @return: Renderer(s)
275 """
276
277 return self.__vtk_renderer

  ViewVC Help
Powered by ViewVC 1.1.26