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 and its settings. |
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 the object is 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 |
center = self.__data_collector._getOutput().GetCenter() |
42 |
# Default camera focal point is the center of the object. |
43 |
self.setFocalPoint(GlobalPosition(center[0], center[1], center[2])) |
44 |
# Default camera position is the center of the object but with a slight |
45 |
# distance on the z-axis. |
46 |
self.setPosition(GlobalPosition(center[0], center[1], center[2] * 3)) |
47 |
# Assign the camera to the appropriate renderer |
48 |
#self.__scene._getRenderer()[self.__viewport].SetActiveCamera( |
49 |
# self.__vtk_camera) |
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 |
@type position: L{GlobalPosition <position.GlobalPosition>} object |
57 |
@param position: Camera focal point |
58 |
""" |
59 |
|
60 |
self.__vtk_camera.SetFocalPoint(position._getGlobalPosition()) |
61 |
self.__resetCamera() |
62 |
|
63 |
def setPosition(self, position): |
64 |
""" |
65 |
Set the position of the camera. |
66 |
@type position: L{GlobalPosition <position.GlobalPosition>} object |
67 |
@param position: Camera position |
68 |
""" |
69 |
|
70 |
self.__vtk_camera.SetPosition(position._getGlobalPosition()) |
71 |
self.__resetCamera() |
72 |
|
73 |
def setClippingRange(self, near_clipping, far_clipping): |
74 |
""" |
75 |
Set the near and far clipping plane of the camera. |
76 |
@type near_clipping: Number |
77 |
@param near_clipping: Distance to the near clipping plane |
78 |
@type far_clipping: Number |
79 |
@param far_clipping: Distance to the far clipping plane |
80 |
""" |
81 |
|
82 |
self.vtk__camera.SetClippingRange(near_clipping, far_clipping) |
83 |
self.__resetCamera() |
84 |
|
85 |
def setViewUp(self, position): |
86 |
""" |
87 |
Set the view up direction of the camera. |
88 |
@type position: L{GlobalPosition <position.GlobalPosition>} object |
89 |
@param position: Camera view up direction |
90 |
""" |
91 |
|
92 |
self.__vtk_camera.SetViewUp(position._getGlobalPosition()) |
93 |
self.__resetCamera() |
94 |
|
95 |
def azimuth(self, angle): |
96 |
""" |
97 |
Rotate the camera to the left and right. |
98 |
@type angle: Number |
99 |
@param angle: Degree to rotate the camera |
100 |
""" |
101 |
|
102 |
self.__vtk_camera.Azimuth(angle) |
103 |
self.__resetCamera() |
104 |
|
105 |
def elevation(self, angle): |
106 |
""" |
107 |
Rotate the camera to the top and bottom. |
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. Secondly, 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 degress. Exceeding that, the rendered object may be incorrect. |
118 |
self.__vtk_camera.OrthogonalizeViewUp() |
119 |
self.__resetCamera() |
120 |
|
121 |
def roll(self, angle): |
122 |
""" |
123 |
Roll the camera to the left and right. |
124 |
@type angle: Number |
125 |
@param angle: Degree to turn the camera |
126 |
""" |
127 |
|
128 |
self.__vtk_camera.Roll(-angle) |
129 |
|
130 |
def backView(self): |
131 |
""" |
132 |
View the back of the rendered object. |
133 |
""" |
134 |
|
135 |
self.azimuth(180) |
136 |
self.__resetCamera() |
137 |
|
138 |
def topView(self): |
139 |
""" |
140 |
View the top of the rendered object. |
141 |
""" |
142 |
|
143 |
self.elevation(90) |
144 |
self.__resetCamera() |
145 |
|
146 |
def bottomView(self): |
147 |
""" |
148 |
View the bottom of the rendered object. |
149 |
""" |
150 |
|
151 |
self.elevation(-90) |
152 |
self.__resetCamera() |
153 |
|
154 |
def leftView(self): |
155 |
""" |
156 |
View the left side of the rendered object. |
157 |
""" |
158 |
|
159 |
self.azimuth(-90) |
160 |
self.__resetCamera() |
161 |
|
162 |
def rightView(self): |
163 |
""" |
164 |
View the right side of the rendered object. |
165 |
""" |
166 |
|
167 |
self.azimuth(90) |
168 |
self.__resetCamera() |
169 |
|
170 |
def isometricView(self): |
171 |
""" |
172 |
View the isometric side of the rendered object. |
173 |
""" |
174 |
|
175 |
self.roll(-45) |
176 |
self.elevation(-45) |
177 |
|
178 |
def __resetCamera(self): |
179 |
""" |
180 |
Reposition the camera so that all actors can be seen. Needs to |
181 |
be called whenever the camera's settings are modified. |
182 |
""" |
183 |
|
184 |
self.__scene._getRenderer()[self.__viewport].ResetCamera() |
185 |
|