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 |
|
|
def __init__(self, scene, data_collector, 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 data_collector: L{DataCollector <datacollector.DataCollector>} |
26 |
|
|
object |
27 |
|
|
@param data_collector: Deal with the source data for vizualisation |
28 |
|
|
@type viewport: L{Viewport <constant.Viewport>} constant |
29 |
|
|
@param viewport: Viewport in which objects are to be rendered on |
30 |
|
|
""" |
31 |
|
|
|
32 |
|
|
self.__scene = scene |
33 |
|
|
self.__data_collector = data_collector |
34 |
|
|
self.__viewport = viewport |
35 |
|
|
|
36 |
|
|
self.__vtk_camera = vtk.vtkCamera() |
37 |
|
|
self.__setupCamera() |
38 |
|
|
|
39 |
|
|
def __setupCamera(self): |
40 |
|
|
""" |
41 |
|
|
Setup the camera. |
42 |
|
|
""" |
43 |
|
|
|
44 |
|
|
# Assign the camera to the appropriate renderer |
45 |
|
|
self.__scene._setActiveCamera(self.__viewport, self.__vtk_camera) |
46 |
|
|
self.__resetCamera() |
47 |
|
|
|
48 |
|
|
def setFocalPoint(self, position): |
49 |
|
|
""" |
50 |
|
|
Set the focal point of the camera. |
51 |
|
|
|
52 |
|
|
@type position: L{GlobalPosition <position.GlobalPosition>} object |
53 |
|
|
@param position: Camera focal point |
54 |
|
|
""" |
55 |
|
|
|
56 |
|
|
self.__vtk_camera.SetFocalPoint(position._getGlobalPosition()) |
57 |
|
|
self.__resetCameraClippingRange() |
58 |
|
|
|
59 |
|
|
def setPosition(self, position): |
60 |
|
|
""" |
61 |
|
|
Set the position of the camera. |
62 |
|
|
|
63 |
|
|
@type position: L{GlobalPosition <position.GlobalPosition>} object |
64 |
|
|
@param position: Camera position |
65 |
|
|
""" |
66 |
|
|
|
67 |
|
|
self.__vtk_camera.SetPosition(position._getGlobalPosition()) |
68 |
|
|
self.__resetCameraClippingRange() |
69 |
|
|
|
70 |
|
|
def setClippingRange(self, near_clipping, far_clipping): |
71 |
|
|
""" |
72 |
|
|
Set the near and far clipping plane of the camera. |
73 |
|
|
|
74 |
|
|
@type near_clipping: Number |
75 |
|
|
@param near_clipping: Distance to the near clipping plane |
76 |
|
|
@type far_clipping: Number |
77 |
|
|
@param far_clipping: Distance to the far clipping plane |
78 |
|
|
""" |
79 |
|
|
|
80 |
|
|
self.__vtk_camera.SetClippingRange(near_clipping, far_clipping) |
81 |
|
|
|
82 |
|
|
def setViewUp(self, position): |
83 |
|
|
""" |
84 |
|
|
Set the view up direction of the camera. |
85 |
|
|
|
86 |
|
|
@type position: L{GlobalPosition <position.GlobalPosition>} object |
87 |
|
|
@param position: Camera view up direction |
88 |
|
|
""" |
89 |
|
|
|
90 |
|
|
self.__vtk_camera.SetViewUp(position._getGlobalPosition()) |
91 |
|
|
|
92 |
|
|
def azimuth(self, angle): |
93 |
|
|
""" |
94 |
|
|
Rotate the camera to the left and right. |
95 |
|
|
|
96 |
|
|
@type angle: Number |
97 |
|
|
@param angle: Degree to rotate the camera |
98 |
|
|
""" |
99 |
|
|
|
100 |
|
|
self.__vtk_camera.Azimuth(angle) |
101 |
|
|
self.__resetCameraClippingRange() |
102 |
|
|
|
103 |
|
|
def elevation(self, angle): |
104 |
|
|
""" |
105 |
|
|
Rotate the camera to the top and bottom. |
106 |
|
|
|
107 |
|
|
@type angle: Number |
108 |
|
|
@param angle: Degree to rotate the camera (only between -90 and 90) |
109 |
|
|
""" |
110 |
|
|
|
111 |
|
|
self.__vtk_camera.Elevation(angle) |
112 |
|
|
# Recompute the view up vector. If not used the elevation angle is |
113 |
|
|
# unable to exceed 87/-87 degrees. A warning resetting the |
114 |
|
|
# view up will also be thrown and the rendered object may be incorrect. |
115 |
|
|
# With the view up recomputed, the elevation angle can reach between |
116 |
|
|
# 90/-90 degrees. Exceeding that, the rendered object may be incorrect. |
117 |
|
|
self.__vtk_camera.OrthogonalizeViewUp() |
118 |
|
|
self.__resetCameraClippingRange() |
119 |
|
|
|
120 |
|
|
def roll(self, angle): |
121 |
|
|
""" |
122 |
|
|
Roll the camera to the left and right. |
123 |
|
|
|
124 |
|
|
@type angle: Number |
125 |
|
|
@param angle: Degree to roll the camera |
126 |
|
|
""" |
127 |
|
|
|
128 |
|
|
self.__vtk_camera.Roll(-angle) |
129 |
|
|
self.__resetCameraClippingRange() |
130 |
|
|
|
131 |
|
|
def backView(self): |
132 |
|
|
""" |
133 |
|
|
Rotate the camera to view the back of the rendered object. |
134 |
|
|
""" |
135 |
|
|
|
136 |
|
|
self.azimuth(180) |
137 |
|
|
|
138 |
|
|
def topView(self): |
139 |
|
|
""" |
140 |
|
|
Rotate the camera to view the top of the rendered object. |
141 |
|
|
""" |
142 |
|
|
|
143 |
|
|
self.elevation(90) |
144 |
|
|
|
145 |
|
|
def bottomView(self): |
146 |
|
|
""" |
147 |
|
|
Rotate the camera to view the bottom of the rendered object. |
148 |
|
|
""" |
149 |
|
|
|
150 |
|
|
self.elevation(-90) |
151 |
|
|
|
152 |
|
|
def leftView(self): |
153 |
|
|
""" |
154 |
|
|
Rotate the camera to view the left side of the rendered object. |
155 |
|
|
""" |
156 |
|
|
|
157 |
|
|
self.azimuth(-90) |
158 |
|
|
|
159 |
|
|
def rightView(self): |
160 |
|
|
""" |
161 |
|
|
Rotate the camera to view the right side of the rendered object. |
162 |
|
|
""" |
163 |
|
|
|
164 |
|
|
self.azimuth(90) |
165 |
|
|
|
166 |
|
|
def isometricView(self): |
167 |
|
|
""" |
168 |
|
|
Rotate the camera to view the isometric angle of the rendered object. |
169 |
|
|
""" |
170 |
|
|
|
171 |
|
|
self.roll(-45) |
172 |
|
|
self.elevation(-45) |
173 |
|
|
|
174 |
|
|
def dolly(self, distance): |
175 |
|
|
""" |
176 |
|
|
Move the camera towards (greater than 1) and away (less than 1) from |
177 |
|
|
the rendered object. |
178 |
|
|
|
179 |
|
|
@type distance: Number |
180 |
|
|
@param distance: Amount to move towards or away the rendered object |
181 |
|
|
""" |
182 |
|
|
|
183 |
|
|
self.__vtk_camera.Dolly(distance) |
184 |
|
|
self.__resetCameraClippingRange() |
185 |
|
|
|
186 |
|
|
def __resetCameraClippingRange(self): |
187 |
|
|
""" |
188 |
|
|
Reset the camera clipping range based on the bounds of the visible |
189 |
|
|
actors. This ensures the rendered object is not cut-off. |
190 |
|
|
Needs to be called whenever the camera's settings are modified. |
191 |
|
|
""" |
192 |
|
|
|
193 |
|
|
self.__scene._getRenderer()[self.__viewport].ResetCameraClippingRange() |
194 |
|
|
|
195 |
|
|
def __resetCamera(self): |
196 |
|
|
""" |
197 |
|
|
Repositions the camera to view the center point of the actors. |
198 |
|
|
""" |
199 |
|
|
|
200 |
|
|
self.__scene._getRenderer()[self.__viewport].ResetCamera() |
201 |
|
|
|