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. |
12 |
""" |
13 |
|
14 |
# The SOUTH_WEST default viewport is used when there is only one viewport. |
15 |
# This saves the user from specifying the viewport when there is only one. |
16 |
def __init__(self, scene, data_collector, viewport = Viewport.SOUTH_WEST): |
17 |
""" |
18 |
Initialise the camera. |
19 |
|
20 |
@type scene: L{Scene <scene.Scene>} object |
21 |
@param scene: Scene in which objects are to be rendered on |
22 |
@type data_collector: L{DataCollector <datacollector.DataCollector>} |
23 |
object |
24 |
@param data_collector: Deal with source of data for visualisation |
25 |
@type viewport: L{Viewport <constant.Viewport>} constant |
26 |
@param viewport: Viewport in which object are to be rendered on |
27 |
""" |
28 |
|
29 |
self.__scene = scene |
30 |
self.__data_collector = data_collector |
31 |
self.__viewport = viewport |
32 |
|
33 |
self.__vtk_camera = vtk.vtkCamera() |
34 |
self.__setupCamera() |
35 |
|
36 |
def __setupCamera(self): |
37 |
""" |
38 |
Setup the camera. |
39 |
""" |
40 |
|
41 |
# Default camera focal point is the center of the object. |
42 |
center = self.__data_collector._getOutput().GetCenter() |
43 |
self.setFocalPoint(GlobalPosition(center[0], center[1], center[2])) |
44 |
|
45 |
# Default camera position is the center of the object but with a slight |
46 |
# distance on the z-axis. |
47 |
self.setPosition(GlobalPosition(center[0], center[1], center[2] * 3)) |
48 |
|
49 |
# Assign the camera to the appropriate renderer |
50 |
self.__scene._setActiveCamera(self.__viewport, self.__vtk_camera) |
51 |
self.__resetCamera() |
52 |
|
53 |
def setFocalPoint(self, position): |
54 |
""" |
55 |
Set the focal point of the camera. |
56 |
|
57 |
@type position: L{GlobalPosition <position.GlobalPosition>} object |
58 |
@param position: Camera focal point |
59 |
""" |
60 |
|
61 |
self.__vtk_camera.SetFocalPoint(position._getGlobalPosition()) |
62 |
self.__resetCamera() |
63 |
|
64 |
def setPosition(self, position): |
65 |
""" |
66 |
Set the position of the camera. |
67 |
|
68 |
@type position: L{GlobalPosition <position.GlobalPosition>} object |
69 |
@param position: Camera position |
70 |
""" |
71 |
|
72 |
self.__vtk_camera.SetPosition(position._getGlobalPosition()) |
73 |
self.__resetCamera() |
74 |
|
75 |
def setClippingRange(self, near_clipping, far_clipping): |
76 |
""" |
77 |
Set the near and far clipping plane of the camera. |
78 |
|
79 |
@type near_clipping: Number |
80 |
@param near_clipping: Distance to the near clipping plane |
81 |
@type far_clipping: Number |
82 |
@param far_clipping: Distance to the far clipping plane |
83 |
""" |
84 |
|
85 |
self.vtk__camera.SetClippingRange(near_clipping, far_clipping) |
86 |
self.__resetCamera() |
87 |
|
88 |
def setViewUp(self, position): |
89 |
""" |
90 |
Set the view up direction of the camera. |
91 |
|
92 |
@type position: L{GlobalPosition <position.GlobalPosition>} object |
93 |
@param position: Camera view up direction |
94 |
""" |
95 |
|
96 |
self.__vtk_camera.SetViewUp(position._getGlobalPosition()) |
97 |
self.__resetCamera() |
98 |
|
99 |
def azimuth(self, angle): |
100 |
""" |
101 |
Rotate the camera to the left and right. |
102 |
|
103 |
@type angle: Number |
104 |
@param angle: Degree to rotate the camera |
105 |
""" |
106 |
|
107 |
self.__vtk_camera.Azimuth(angle) |
108 |
self.__resetCamera() |
109 |
|
110 |
def elevation(self, angle): |
111 |
""" |
112 |
Rotate the camera to the top and bottom. |
113 |
|
114 |
@type angle: Number |
115 |
@param angle: Degree to rotate the camera (only between -90 and 90) |
116 |
""" |
117 |
|
118 |
self.__vtk_camera.Elevation(angle) |
119 |
# Recompute the view up vector. If not used the elevation angle is |
120 |
# unable to exceed 87/-87 degrees. Also, a warning resetting the |
121 |
# view up will also be thrown and the rendered object may be incorrect. |
122 |
# With the view up recomputed, the elevation angle can reach between |
123 |
# 90/-90 degrees. Exceeding that, the rendered object may be incorrect. |
124 |
self.__vtk_camera.OrthogonalizeViewUp() |
125 |
self.__resetCamera() |
126 |
|
127 |
def roll(self, angle): |
128 |
""" |
129 |
Roll the camera to the left and right. |
130 |
|
131 |
@type angle: Number |
132 |
@param angle: Degree to roll the camera |
133 |
""" |
134 |
|
135 |
self.__vtk_camera.Roll(-angle) |
136 |
|
137 |
def backView(self): |
138 |
""" |
139 |
Rotate the camera to view the back of the rendered object. |
140 |
""" |
141 |
|
142 |
self.azimuth(180) |
143 |
self.__resetCamera() |
144 |
|
145 |
def topView(self): |
146 |
""" |
147 |
Rotate the camera to view the top of the rendered object. |
148 |
""" |
149 |
|
150 |
self.elevation(90) |
151 |
self.__resetCamera() |
152 |
|
153 |
def bottomView(self): |
154 |
""" |
155 |
Rotate the camera to view the bottom of the rendered object. |
156 |
""" |
157 |
|
158 |
self.elevation(-90) |
159 |
self.__resetCamera() |
160 |
|
161 |
def leftView(self): |
162 |
""" |
163 |
Rotate the camera to view the left side of the rendered object. |
164 |
""" |
165 |
|
166 |
self.azimuth(-90) |
167 |
self.__resetCamera() |
168 |
|
169 |
def rightView(self): |
170 |
""" |
171 |
Rotate the camera to view the right side of the rendered object. |
172 |
""" |
173 |
|
174 |
self.azimuth(90) |
175 |
self.__resetCamera() |
176 |
|
177 |
def isometricView(self): |
178 |
""" |
179 |
Rotate the camera to view the isometric side of the rendered object. |
180 |
""" |
181 |
|
182 |
self.roll(-45) |
183 |
self.elevation(-45) |
184 |
|
185 |
def __resetCamera(self): |
186 |
""" |
187 |
Reposition the camera so that all actors can be seen. Needs to |
188 |
be called whenever the camera's settings are modified in order for the |
189 |
changes to take effect correctly. |
190 |
""" |
191 |
|
192 |
self.__scene._getRenderer()[self.__viewport].ResetCamera() |
193 |
|