1 |
ksteube |
1147 |
""" |
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 |
jongui |
1148 |
def __init__(self, scene, viewport = Viewport.SOUTH_WEST): |
20 |
ksteube |
1147 |
""" |
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.__scene = scene |
30 |
|
|
self.__viewport = viewport |
31 |
jongui |
1148 |
self.__vtk_camera = vtk.vtkCamera() |
32 |
ksteube |
1147 |
|
33 |
jongui |
1148 |
# Keeps track whether camera has been modified. |
34 |
|
|
self.__modified = True |
35 |
|
|
# Keeps track whether the modification to the camera was due to the |
36 |
|
|
# instantiation. If it is, then __setupCamera() method is called. |
37 |
|
|
self.__initialization = True |
38 |
|
|
self.__scene._addVisualizationModules(self) |
39 |
|
|
|
40 |
ksteube |
1147 |
def __setupCamera(self): |
41 |
|
|
""" |
42 |
|
|
Setup the camera. |
43 |
|
|
""" |
44 |
|
|
|
45 |
|
|
# Assign the camera to the appropriate renderer |
46 |
|
|
self.__scene._setActiveCamera(self.__viewport, self.__vtk_camera) |
47 |
|
|
self.__resetCamera() |
48 |
|
|
|
49 |
|
|
def setFocalPoint(self, position): |
50 |
|
|
""" |
51 |
|
|
Set the focal point of the camera. |
52 |
|
|
|
53 |
|
|
@type position: L{GlobalPosition <position.GlobalPosition>} object |
54 |
|
|
@param position: Camera focal point |
55 |
|
|
""" |
56 |
|
|
|
57 |
|
|
self.__vtk_camera.SetFocalPoint(position._getGlobalPosition()) |
58 |
jongui |
1148 |
self.__modified = True |
59 |
ksteube |
1147 |
|
60 |
|
|
def setPosition(self, position): |
61 |
|
|
""" |
62 |
|
|
Set the position of the camera. |
63 |
|
|
|
64 |
|
|
@type position: L{GlobalPosition <position.GlobalPosition>} object |
65 |
|
|
@param position: Camera position |
66 |
|
|
""" |
67 |
|
|
|
68 |
|
|
self.__vtk_camera.SetPosition(position._getGlobalPosition()) |
69 |
jongui |
1148 |
self.__modified = True |
70 |
ksteube |
1147 |
|
71 |
|
|
def setClippingRange(self, near_clipping, far_clipping): |
72 |
|
|
""" |
73 |
|
|
Set the near and far clipping plane of the camera. |
74 |
|
|
|
75 |
|
|
@type near_clipping: Number |
76 |
|
|
@param near_clipping: Distance to the near clipping plane |
77 |
|
|
@type far_clipping: Number |
78 |
|
|
@param far_clipping: Distance to the far clipping plane |
79 |
|
|
""" |
80 |
|
|
|
81 |
|
|
self.__vtk_camera.SetClippingRange(near_clipping, far_clipping) |
82 |
|
|
|
83 |
|
|
def setViewUp(self, position): |
84 |
|
|
""" |
85 |
|
|
Set the view up direction of the camera. |
86 |
|
|
|
87 |
|
|
@type position: L{GlobalPosition <position.GlobalPosition>} object |
88 |
|
|
@param position: Camera view up direction |
89 |
|
|
""" |
90 |
|
|
|
91 |
|
|
self.__vtk_camera.SetViewUp(position._getGlobalPosition()) |
92 |
|
|
|
93 |
|
|
def azimuth(self, angle): |
94 |
|
|
""" |
95 |
|
|
Rotate the camera to the left and right. |
96 |
|
|
|
97 |
|
|
@type angle: Number |
98 |
|
|
@param angle: Degree to rotate the camera |
99 |
|
|
""" |
100 |
|
|
|
101 |
|
|
self.__vtk_camera.Azimuth(angle) |
102 |
jongui |
1148 |
self.__modified = True |
103 |
ksteube |
1147 |
|
104 |
|
|
def elevation(self, angle): |
105 |
|
|
""" |
106 |
|
|
Rotate the camera to the top and bottom. |
107 |
|
|
|
108 |
|
|
@type angle: Number |
109 |
|
|
@param angle: Degree to rotate the camera (only between -90 and 90) |
110 |
|
|
""" |
111 |
|
|
|
112 |
|
|
self.__vtk_camera.Elevation(angle) |
113 |
|
|
# Recompute the view up vector. If not used the elevation angle is |
114 |
|
|
# unable to exceed 87/-87 degrees. A warning resetting the |
115 |
|
|
# view up will also be thrown and the rendered object may be incorrect. |
116 |
|
|
# With the view up recomputed, the elevation angle can reach between |
117 |
|
|
# 90/-90 degrees. Exceeding that, the rendered object may be incorrect. |
118 |
|
|
self.__vtk_camera.OrthogonalizeViewUp() |
119 |
jongui |
1148 |
self.__modified = True |
120 |
ksteube |
1147 |
|
121 |
|
|
def roll(self, angle): |
122 |
|
|
""" |
123 |
|
|
Roll the camera to the left and right. |
124 |
|
|
|
125 |
|
|
@type angle: Number |
126 |
|
|
@param angle: Degree to roll the camera |
127 |
|
|
""" |
128 |
|
|
|
129 |
|
|
self.__vtk_camera.Roll(-angle) |
130 |
jongui |
1148 |
self.__modified = True |
131 |
ksteube |
1147 |
|
132 |
|
|
def backView(self): |
133 |
|
|
""" |
134 |
|
|
Rotate the camera to view the back of the rendered object. |
135 |
|
|
""" |
136 |
|
|
|
137 |
|
|
self.azimuth(180) |
138 |
|
|
|
139 |
|
|
def topView(self): |
140 |
|
|
""" |
141 |
|
|
Rotate the camera to view the top of the rendered object. |
142 |
|
|
""" |
143 |
|
|
|
144 |
|
|
self.elevation(90) |
145 |
|
|
|
146 |
|
|
def bottomView(self): |
147 |
|
|
""" |
148 |
|
|
Rotate the camera to view the bottom of the rendered object. |
149 |
|
|
""" |
150 |
|
|
|
151 |
|
|
self.elevation(-90) |
152 |
|
|
|
153 |
|
|
def leftView(self): |
154 |
|
|
""" |
155 |
|
|
Rotate the camera to view the left side of the rendered object. |
156 |
|
|
""" |
157 |
|
|
|
158 |
|
|
self.azimuth(-90) |
159 |
|
|
|
160 |
|
|
def rightView(self): |
161 |
|
|
""" |
162 |
|
|
Rotate the camera to view the right side of the rendered object. |
163 |
|
|
""" |
164 |
|
|
|
165 |
|
|
self.azimuth(90) |
166 |
|
|
|
167 |
|
|
def isometricView(self): |
168 |
|
|
""" |
169 |
|
|
Rotate the camera to view the isometric angle of the rendered object. |
170 |
|
|
""" |
171 |
|
|
|
172 |
|
|
self.roll(-45) |
173 |
|
|
self.elevation(-45) |
174 |
|
|
|
175 |
|
|
def dolly(self, distance): |
176 |
|
|
""" |
177 |
|
|
Move the camera towards (greater than 1) and away (less than 1) from |
178 |
|
|
the rendered object. |
179 |
|
|
|
180 |
|
|
@type distance: Number |
181 |
|
|
@param distance: Amount to move towards or away the rendered object |
182 |
|
|
""" |
183 |
|
|
|
184 |
|
|
self.__vtk_camera.Dolly(distance) |
185 |
jongui |
1148 |
self.__modified = True |
186 |
ksteube |
1147 |
|
187 |
|
|
def __resetCameraClippingRange(self): |
188 |
|
|
""" |
189 |
|
|
Reset the camera clipping range based on the bounds of the visible |
190 |
|
|
actors. This ensures the rendered object is not cut-off. |
191 |
|
|
Needs to be called whenever the camera's settings are modified. |
192 |
|
|
""" |
193 |
|
|
|
194 |
|
|
self.__scene._getRenderer()[self.__viewport].ResetCameraClippingRange() |
195 |
|
|
|
196 |
|
|
def __resetCamera(self): |
197 |
|
|
""" |
198 |
|
|
Repositions the camera to view the center point of the actors. |
199 |
|
|
""" |
200 |
|
|
|
201 |
|
|
self.__scene._getRenderer()[self.__viewport].ResetCamera() |
202 |
jongui |
1148 |
|
203 |
|
|
def _isModified(self): |
204 |
|
|
""" |
205 |
|
|
Return whether the Camera has been modified. |
206 |
|
|
|
207 |
|
|
@rtype: Boolean |
208 |
|
|
@return: True or False |
209 |
|
|
""" |
210 |
|
|
|
211 |
|
|
if (self.__modified == True): |
212 |
|
|
return True |
213 |
|
|
else: |
214 |
|
|
return False |
215 |
|
|
|
216 |
|
|
def _render(self): |
217 |
|
|
""" |
218 |
|
|
Render the camera. |
219 |
|
|
""" |
220 |
|
|
|
221 |
|
|
if(self._isModified() == True): |
222 |
|
|
# Will only be true once only when the camera is instantiated. |
223 |
|
|
if(self.__initialization == True): |
224 |
|
|
self.__setupCamera() |
225 |
|
|
self.__initialization == False |
226 |
|
|
|
227 |
|
|
self.__resetCameraClippingRange() |
228 |
|
|
self.__isModified = False |
229 |
|
|
|
230 |
|
|
|