1 |
""" |
2 |
@author: John NGUI |
3 |
""" |
4 |
|
5 |
import vtk |
6 |
from position import GlobalPosition |
7 |
from constant import Viewport |
8 |
|
9 |
class Camera: |
10 |
""" |
11 |
Class that defines a camera. A camera controls the display angle of |
12 |
the rendered object and one is usually created for a |
13 |
L{Scene <scene.Scene>}. However, if a L{Scene <scene.Scene>} has four |
14 |
viewports, then a separate camera may be created for each viewport. |
15 |
""" |
16 |
|
17 |
# The SOUTH_WEST default viewport is used when there is only one viewport. |
18 |
# This saves the user from specifying the viewport when there is only one. |
19 |
def __init__(self, scene, viewport = Viewport.SOUTH_WEST): |
20 |
""" |
21 |
Initialise the camera. |
22 |
|
23 |
@type scene: L{Scene <scene.Scene>} object |
24 |
@param scene: Scene in which objects are to be rendered on |
25 |
@type viewport: L{Viewport <constant.Viewport>} constant |
26 |
@param viewport: Viewport in which objects are to be rendered on |
27 |
""" |
28 |
|
29 |
self.__viewport = viewport |
30 |
self.__vtk_camera = vtk.vtkCamera() |
31 |
|
32 |
# Keeps track whether camera has been modified. |
33 |
self.__modified = True |
34 |
# Keeps track whether the modification to the camera was due to the |
35 |
# instantiation. If it is, then __setupCamera() method is called. |
36 |
self.__initialization = True |
37 |
scene._addVisualizationModules(self) |
38 |
|
39 |
def __setupCamera(self, scene): |
40 |
""" |
41 |
Setup the camera. |
42 |
|
43 |
@type scene: L{Scene <scene.Scene>} object |
44 |
@param scene: Scene in which objects are to be rendered on |
45 |
""" |
46 |
|
47 |
# Assign the camera to the appropriate renderer |
48 |
scene._setActiveCamera(self.__viewport, self.__vtk_camera) |
49 |
self.__resetCamera(scene) |
50 |
|
51 |
def setFocalPoint(self, position): |
52 |
""" |
53 |
Set the focal point of the camera. |
54 |
|
55 |
@type position: L{GlobalPosition <position.GlobalPosition>} object |
56 |
@param position: Camera focal point |
57 |
""" |
58 |
|
59 |
self.__vtk_camera.SetFocalPoint(position._getGlobalPosition()) |
60 |
self.__modified = True |
61 |
|
62 |
def setPosition(self, position): |
63 |
""" |
64 |
Set the position of the camera. |
65 |
|
66 |
@type position: L{GlobalPosition <position.GlobalPosition>} object |
67 |
@param position: Camera position |
68 |
""" |
69 |
|
70 |
self.__vtk_camera.SetPosition(position._getGlobalPosition()) |
71 |
self.__modified = True |
72 |
|
73 |
def setClippingRange(self, near_clipping, far_clipping): |
74 |
""" |
75 |
Set the near and far clipping plane of the camera. |
76 |
|
77 |
@type near_clipping: Number |
78 |
@param near_clipping: Distance to the near clipping plane |
79 |
@type far_clipping: Number |
80 |
@param far_clipping: Distance to the far clipping plane |
81 |
""" |
82 |
|
83 |
self.__vtk_camera.SetClippingRange(near_clipping, far_clipping) |
84 |
|
85 |
def setViewUp(self, position): |
86 |
""" |
87 |
Set the view up direction of the camera. |
88 |
|
89 |
@type position: L{GlobalPosition <position.GlobalPosition>} object |
90 |
@param position: Camera view up direction |
91 |
""" |
92 |
|
93 |
self.__vtk_camera.SetViewUp(position._getGlobalPosition()) |
94 |
|
95 |
def azimuth(self, angle): |
96 |
""" |
97 |
Rotate the camera to the left and right. |
98 |
|
99 |
@type angle: Number |
100 |
@param angle: Degree to rotate the camera |
101 |
""" |
102 |
|
103 |
self.__vtk_camera.Azimuth(angle) |
104 |
self.__modified = True |
105 |
|
106 |
def elevation(self, angle): |
107 |
""" |
108 |
Rotate the camera to the top and bottom. |
109 |
|
110 |
@type angle: Number |
111 |
@param angle: Degree to rotate the camera (only between -90 and 90) |
112 |
""" |
113 |
|
114 |
self.__vtk_camera.Elevation(angle) |
115 |
# Recompute the view up vector. If not used the elevation angle is |
116 |
# unable to exceed 87/-87 degrees. A warning resetting the |
117 |
# view up will also be thrown and the rendered object may be incorrect. |
118 |
# With the view up recomputed, the elevation angle can reach between |
119 |
# 90/-90 degrees. Exceeding that, the rendered object may be incorrect. |
120 |
self.__vtk_camera.OrthogonalizeViewUp() |
121 |
self.__modified = True |
122 |
|
123 |
def roll(self, angle): |
124 |
""" |
125 |
Roll the camera to the left and right. |
126 |
|
127 |
@type angle: Number |
128 |
@param angle: Degree to roll the camera |
129 |
""" |
130 |
|
131 |
self.__vtk_camera.Roll(-angle) |
132 |
self.__modified = True |
133 |
|
134 |
def backView(self): |
135 |
""" |
136 |
Rotate the camera to view the back of the rendered object. |
137 |
""" |
138 |
|
139 |
self.azimuth(180) |
140 |
|
141 |
def topView(self): |
142 |
""" |
143 |
Rotate the camera to view the top of the rendered object. |
144 |
""" |
145 |
|
146 |
self.elevation(90) |
147 |
|
148 |
def bottomView(self): |
149 |
""" |
150 |
Rotate the camera to view the bottom of the rendered object. |
151 |
""" |
152 |
|
153 |
self.elevation(-90) |
154 |
|
155 |
def leftView(self): |
156 |
""" |
157 |
Rotate the camera to view the left side of the rendered object. |
158 |
""" |
159 |
|
160 |
self.azimuth(-90) |
161 |
|
162 |
def rightView(self): |
163 |
""" |
164 |
Rotate the camera to view the right side of the rendered object. |
165 |
""" |
166 |
|
167 |
self.azimuth(90) |
168 |
|
169 |
def isometricView(self): |
170 |
""" |
171 |
Rotate the camera to view the isometric angle of the rendered object. |
172 |
""" |
173 |
|
174 |
self.roll(-45) |
175 |
self.elevation(-45) |
176 |
|
177 |
def dolly(self, distance): |
178 |
""" |
179 |
Move the camera towards (greater than 1) the rendered object. However, |
180 |
the camera is unable to be moved away from the rendered object. |
181 |
|
182 |
@type distance: Number |
183 |
@param distance: Amount to move towards the rendered object |
184 |
""" |
185 |
|
186 |
self.__vtk_camera.Dolly(distance) |
187 |
self.__modified = True |
188 |
|
189 |
def parallelProjectionOn(self): |
190 |
""" |
191 |
Enable camera parallel projection. |
192 |
""" |
193 |
|
194 |
self.__vtk_camera.ParallelProjectionOn() |
195 |
self.__modified = True |
196 |
|
197 |
def parallelProjectionOff(self): |
198 |
""" |
199 |
Disable camera parallel projection. |
200 |
""" |
201 |
|
202 |
self.__vtk_camera.ParallelProjectionOff() |
203 |
self.__modified = True |
204 |
|
205 |
def __resetCameraClippingRange(self, scene): |
206 |
""" |
207 |
Reset the camera clipping range based on the bounds of the visible |
208 |
actors. This ensures the rendered object is not cut-off. |
209 |
Needs to be called whenever the camera's settings are modified. |
210 |
|
211 |
@type scene: L{Scene <scene.Scene>} object |
212 |
@param scene: Scene in which objects are to be rendered on |
213 |
""" |
214 |
|
215 |
scene._getRenderer()[self.__viewport].ResetCameraClippingRange() |
216 |
|
217 |
def __resetCamera(self, scene): |
218 |
""" |
219 |
Repositions the camera to view the center point of the actors. |
220 |
|
221 |
@type scene: L{Scene <scene.Scene>} object |
222 |
@param scene: Scene in which objects are to be rendered on |
223 |
""" |
224 |
|
225 |
scene._getRenderer()[self.__viewport].ResetCamera() |
226 |
|
227 |
def _isModified(self): |
228 |
""" |
229 |
Return whether the Camera has been modified. |
230 |
|
231 |
@rtype: Boolean |
232 |
@return: True or False |
233 |
""" |
234 |
|
235 |
if (self.__modified == True): |
236 |
return True |
237 |
else: |
238 |
return False |
239 |
|
240 |
def _render(self, scene): |
241 |
""" |
242 |
Render the camera. |
243 |
|
244 |
@type scene: L{Scene <scene.Scene>} object |
245 |
@param scene: Scene in which objects are to be rendered on |
246 |
""" |
247 |
|
248 |
if(self._isModified() == True): |
249 |
# Will only be true once only when the camera is instantiated. |
250 |
if(self.__initialization == True): |
251 |
self.__setupCamera(scene) |
252 |
self.__initialization == False |
253 |
|
254 |
self.__resetCameraClippingRange(scene) |
255 |
self.__modified = False |
256 |
|
257 |
|