/[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 1029 - (show annotations)
Wed Mar 14 05:01:13 2007 UTC (12 years, 5 months ago) by jongui
File MIME type: text/x-python
File size: 5192 byte(s)
Fixed problems in relation to updates propagating when multiple files/sources are read.
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. 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 source of data for visualisation
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 # Default camera focal point is the center of the object.
45 center = self.__data_collector._getOutput().GetCenter()
46 self.setFocalPoint(GlobalPosition(center[0], center[1], center[2]))
47
48 # Default camera position is the center of the object but with a slight
49 # distance along the z-axis.
50 self.setPosition(GlobalPosition(center[0], center[1], center[2] * 4))
51
52 # Assign the camera to the appropriate renderer
53 self.__scene._setActiveCamera(self.__viewport, self.__vtk_camera)
54 self.__resetCamera()
55
56 def setFocalPoint(self, position):
57 """
58 Set the focal point of the camera.
59
60 @type position: L{GlobalPosition <position.GlobalPosition>} object
61 @param position: Camera focal point
62 """
63
64 self.__vtk_camera.SetFocalPoint(position._getGlobalPosition())
65 self.__resetCamera()
66
67 def setPosition(self, position):
68 """
69 Set the position of the camera.
70
71 @type position: L{GlobalPosition <position.GlobalPosition>} object
72 @param position: Camera position
73 """
74
75 self.__vtk_camera.SetPosition(position._getGlobalPosition())
76 self.__resetCamera()
77
78 def setClippingRange(self, near_clipping, far_clipping):
79 """
80 Set the near and far clipping plane of the camera.
81
82 @type near_clipping: Number
83 @param near_clipping: Distance to the near clipping plane
84 @type far_clipping: Number
85 @param far_clipping: Distance to the far clipping plane
86 """
87
88 self.vtk__camera.SetClippingRange(near_clipping, far_clipping)
89 self.__resetCamera()
90
91 def setViewUp(self, position):
92 """
93 Set the view up direction of the camera.
94
95 @type position: L{GlobalPosition <position.GlobalPosition>} object
96 @param position: Camera view up direction
97 """
98
99 self.__vtk_camera.SetViewUp(position._getGlobalPosition())
100 self.__resetCamera()
101
102 def azimuth(self, angle):
103 """
104 Rotate the camera to the left and right.
105
106 @type angle: Number
107 @param angle: Degree to rotate the camera
108 """
109
110 self.__vtk_camera.Azimuth(angle)
111 self.__resetCamera()
112
113 def elevation(self, angle):
114 """
115 Rotate the camera to the top and bottom.
116
117 @type angle: Number
118 @param angle: Degree to rotate the camera (only between -90 and 90)
119 """
120
121 self.__vtk_camera.Elevation(angle)
122 # Recompute the view up vector. If not used the elevation angle is
123 # unable to exceed 87/-87 degrees. Also, a warning resetting the
124 # view up will also be thrown and the rendered object may be incorrect.
125 # With the view up recomputed, the elevation angle can reach between
126 # 90/-90 degrees. Exceeding that, the rendered object may be incorrect.
127 self.__vtk_camera.OrthogonalizeViewUp()
128 self.__resetCamera()
129
130 def roll(self, angle):
131 """
132 Roll the camera to the left and right.
133
134 @type angle: Number
135 @param angle: Degree to roll the camera
136 """
137
138 self.__vtk_camera.Roll(-angle)
139 self.__resetCamera()
140
141 def backView(self):
142 """
143 Rotate the camera to view the back of the rendered object.
144 """
145
146 self.azimuth(180)
147 self.__resetCamera()
148
149 def topView(self):
150 """
151 Rotate the camera to view the top of the rendered object.
152 """
153
154 self.elevation(90)
155 self.__resetCamera()
156
157 def bottomView(self):
158 """
159 Rotate the camera to view the bottom of the rendered object.
160 """
161
162 self.elevation(-90)
163 self.__resetCamera()
164
165 def leftView(self):
166 """
167 Rotate the camera to view the left side of the rendered object.
168 """
169
170 self.azimuth(-90)
171 self.__resetCamera()
172
173 def rightView(self):
174 """
175 Rotate the camera to view the right side of the rendered object.
176 """
177
178 self.azimuth(90)
179 self.__resetCamera()
180
181 def isometricView(self):
182 """
183 Rotate the camera to view the isometric angle of the rendered object.
184 """
185
186 self.roll(-45)
187 self.elevation(-45)
188
189 def __resetCamera(self):
190 """
191 Reposition the camera so that all actors can be seen. Needs to
192 be called whenever the camera's settings are modified in order for the
193 changes to take effect.
194 """
195
196 self.__scene._getRenderer()[self.__viewport].ResetCamera()
197

  ViewVC Help
Powered by ViewVC 1.1.26