/[escript]/trunk/pyvisi/py_src/camera.py
ViewVC logotype

Contents of /trunk/pyvisi/py_src/camera.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1002 - (show annotations)
Wed Feb 28 06:51:33 2007 UTC (13 years, 10 months ago) by jongui
File MIME type: text/x-python
File size: 4959 byte(s)
Included some try/except statements for error checking and some documentation. Also fixed a bug in DataCollector.
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 objects 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 along the z-axis.
47 self.setPosition(GlobalPosition(center[0], center[1], center[2] * 4))
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 self.__resetCamera()
137
138 def backView(self):
139 """
140 Rotate the camera to view the back of the rendered object.
141 """
142
143 self.azimuth(180)
144 self.__resetCamera()
145
146 def topView(self):
147 """
148 Rotate the camera to view the top of the rendered object.
149 """
150
151 self.elevation(90)
152 self.__resetCamera()
153
154 def bottomView(self):
155 """
156 Rotate the camera to view the bottom of the rendered object.
157 """
158
159 self.elevation(-90)
160 self.__resetCamera()
161
162 def leftView(self):
163 """
164 Rotate the camera to view the left side of the rendered object.
165 """
166
167 self.azimuth(-90)
168 self.__resetCamera()
169
170 def rightView(self):
171 """
172 Rotate the camera to view the right side of the rendered object.
173 """
174
175 self.azimuth(90)
176 self.__resetCamera()
177
178 def isometricView(self):
179 """
180 Rotate the camera to view the isometric angle of the rendered object.
181 """
182
183 self.roll(-45)
184 self.elevation(-45)
185
186 def __resetCamera(self):
187 """
188 Reposition the camera so that all actors can be seen. Needs to
189 be called whenever the camera's settings are modified in order for the
190 changes to take effect.
191 """
192
193 self.__scene._getRenderer()[self.__viewport].ResetCamera()
194

  ViewVC Help
Powered by ViewVC 1.1.26