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

  ViewVC Help
Powered by ViewVC 1.1.26