-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTerrainInteractorStyle.py
262 lines (203 loc) · 8.11 KB
/
TerrainInteractorStyle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env python
# Author: Nick R. Rypkema (rypkema@mit.edu)
# License: MIT
'''
The LightField Visualizer - a VTK/PyQt4-based lightweight field robotics viewer
-------------------------------------------------------------------------------
custom interactor style for terrain
'''
import vtk
import math
import numpy as np
class TerrainInteractorStyle(vtk.vtkInteractorStyle):
def __init__(self):
self.MotionFactor = 10.0
self.MouseWheelMotionFactor = 1.0
self.AddObserver("MouseMoveEvent", self.mouseMoveEvent)
self.AddObserver("LeftButtonPressEvent", self.leftButtonPressEvent)
self.AddObserver("LeftButtonReleaseEvent", self.leftButtonReleaseEvent)
self.AddObserver("RightButtonPressEvent", self.rightButtonPressEvent)
self.AddObserver("RightButtonReleaseEvent", self.rightButtonReleaseEvent)
self.AddObserver("MiddleButtonPressEvent", self.middleButtonPressEvent)
self.AddObserver("MiddleButtonReleaseEvent", self.middleButtonReleaseEvent)
self.AddObserver("MouseWheelForwardEvent", self.mouseWheelForwardEvent)
self.AddObserver("MouseWheelBackwardEvent", self.mouseWheelBackwardEvent)
def mouseMoveEvent(self, obj, event):
x, y = self.GetInteractor().GetEventPosition()
state = self.GetState()
if state == 1: # VTKIS_ROTATE
self.FindPokedRenderer(x, y)
self.Rotate()
self.InvokeEvent(vtk.vtkCommand.InteractionEvent)
elif state == 2: # VTKIS_PAN
self.FindPokedRenderer(x, y)
self.Pan()
self.InvokeEvent(vtk.vtkCommand.InteractionEvent)
elif state == 4: # VTKIS_DOLLY
self.FindPokedRenderer(x, y)
self.Dolly()
self.InvokeEvent(vtk.vtkCommand.InteractionEvent)
def leftButtonPressEvent(self, obj, event):
x, y = self.GetInteractor().GetEventPosition()
self.FindPokedRenderer(x, y)
if self.GetCurrentRenderer() == None:
return
self.GrabFocus(self.OnMouseMove())
if self.GetInteractor().GetShiftKey():
self.StartPan()
else:
self.StartRotate()
def leftButtonReleaseEvent(self, obj, event):
state = self.GetState()
if state == 1:
self.EndRotate()
if self.GetInteractor():
self.ReleaseFocus()
elif state == 2:
self.EndPan()
if self.GetInteractor():
self.ReleaseFocus()
def middleButtonPressEvent(self, obj, event):
x, y = self.GetInteractor().GetEventPosition()
self.FindPokedRenderer(x, y)
if self.GetCurrentRenderer() == None:
return
self.GrabFocus(self.OnMouseMove())
self.StartPan()
def middleButtonReleaseEvent(self, obj, event):
state = self.GetState()
if state == 2:
self.EndPan()
if self.GetInteractor():
self.ReleaseFocus()
def rightButtonPressEvent(self, obj, event):
x, y = self.GetInteractor().GetEventPosition()
self.FindPokedRenderer(x, y)
if self.GetCurrentRenderer() == None:
return
self.GrabFocus(self.OnMouseMove())
self.StartDolly()
def rightButtonReleaseEvent(self, obj, event):
state = self.GetState()
if state == 4:
self.EndDolly()
if self.GetInteractor():
self.ReleaseFocus()
def mouseWheelForwardEvent(self, obj, event):
x, y = self.GetInteractor().GetEventPosition()
self.FindPokedRenderer(x, y)
if self.GetCurrentRenderer() == None:
return
self.GrabFocus(self.OnMouseMove())
self.StartDolly()
factor = 10*0.2*self.MouseWheelMotionFactor
self.DollyWheel(math.pow(1.1, factor))
self.EndDolly()
self.ReleaseFocus()
def mouseWheelBackwardEvent(self, obj, event):
x, y = self.GetInteractor().GetEventPosition()
self.FindPokedRenderer(x, y)
if self.GetCurrentRenderer() == None:
return
self.GrabFocus(self.OnMouseMove())
self.StartDolly()
factor = 10*-0.2*self.MouseWheelMotionFactor
self.DollyWheel(math.pow(1.1, factor))
self.EndDolly()
self.ReleaseFocus()
def Rotate(self):
if self.GetCurrentRenderer() == None:
return
rwi = self.GetInteractor()
x, y = rwi.GetEventPosition()
x_last, y_last = rwi.GetLastEventPosition()
dx = -(x-x_last)
dy = -(y-y_last)
size = self.GetCurrentRenderer().GetRenderWindow().GetSize()
a = dx/float(size[0]) * 180.0
e = dy/float(size[1]) * 180.0
if rwi.GetControlKey():
if abs(dx) >= abs(dy):
e = 0.0
else:
a = 0.0
camera = self.GetCurrentRenderer().GetActiveCamera()
camera.Azimuth(a)
dop = np.asarray(camera.GetDirectionOfProjection())
dop /= np.linalg.norm(dop)
vup = np.asarray(camera.GetViewUp())
vup /= np.linalg.norm(vup)
angle = np.degrees(math.acos(np.dot(dop, vup)))
if (angle + e) > 177.4 or (angle + e) < 2.6:
e = 0.0
camera.Elevation(e)
if self.GetAutoAdjustCameraClippingRange():
self.GetCurrentRenderer().ResetCameraClippingRange()
rwi.Render()
def Pan(self):
if self.GetCurrentRenderer() == None:
return
rwi = self.GetInteractor()
camera = self.GetCurrentRenderer().GetActiveCamera()
pos = list(camera.GetPosition())
fp = list(camera.GetFocalPoint())
focalPoint = [0,0,0]
self.ComputeWorldToDisplay(self.GetCurrentRenderer(), fp[0], fp[1], fp[2], focalPoint)
x, y = rwi.GetEventPosition()
x_last, y_last = rwi.GetLastEventPosition()
if rwi.GetControlKey():
mouseDeltaX = x - x_last
mouseDeltaY = y - y_last
if abs(mouseDeltaX) >= abs(mouseDeltaY):
y = y_last
else:
x = x_last
p1 = [0,0,0,0]
p2 = [0,0,0,0]
self.ComputeDisplayToWorld(self.GetCurrentRenderer(), x, y, focalPoint[2], p1)
self.ComputeDisplayToWorld(self.GetCurrentRenderer(), x_last, y_last, focalPoint[2], p2)
v = [0,0,0]
for i in xrange(3):
v[i] = p2[i] - p1[i]
pos[i] += v[i]
fp[i] += v[i]
camera.SetPosition(pos)
camera.SetFocalPoint(fp)
if rwi.GetLightFollowCamera():
self.GetCurrentRenderer().UpdateLightsGeometryToFollowCamera()
rwi.Render()
def Dolly(self):
if self.GetCurrentRenderer() == None:
return
rwi = self.GetInteractor()
x, y = rwi.GetEventPosition()
x_last, y_last = rwi.GetLastEventPosition()
camera = self.GetCurrentRenderer().GetActiveCamera()
center = self.GetCurrentRenderer().GetCenter()
dy = y - y_last
dyf = self.MotionFactor*dy/center[1]
zoomFactor = math.pow(1.1, dyf)
if camera.GetParallelProjection():
camera.SetParallelScale(camera.GetParallelScale()/zoomFactor)
else:
camera.Dolly(zoomFactor)
if self.GetAutoAdjustCameraClippingRange():
self.GetCurrentRenderer().ResetCameraClippingRange()
if rwi.GetLightFollowCamera():
self.GetCurrentRenderer().UpdateLightsGeometryToFollowCamera()
rwi.Render()
def DollyWheel(self, factor):
if self.GetCurrentRenderer() == None:
return
rwi = self.GetInteractor()
camera = self.GetCurrentRenderer().GetActiveCamera()
center = self.GetCurrentRenderer().GetCenter()
if camera.GetParallelProjection():
camera.SetParallelScale(camera.GetParallelScale()/factor)
else:
camera.Dolly(factor)
if self.GetAutoAdjustCameraClippingRange():
self.GetCurrentRenderer().ResetCameraClippingRange()
if rwi.GetLightFollowCamera():
self.GetCurrentRenderer().UpdateLightsGeometryToFollowCamera()
rwi.Render()