-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServosPosGraph.py
More file actions
301 lines (243 loc) · 11.1 KB
/
ServosPosGraph.py
File metadata and controls
301 lines (243 loc) · 11.1 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import sys
from enum import IntEnum
from PyQt4.QtOpenGL import *
from OpenGL.GL import *
from OpenGL.GLU import *
from PyQt4.QtCore import QObject, Qt
from PyQt4.QtGui import *
from shapely.geometry import LineString, Point
from scipy.interpolate import interp1d
from scipy.interpolate import splrep, splev, UnivariateSpline, InterpolatedUnivariateSpline
import numpy as np
import util
class Mode(IntEnum):
NORMAL = 0
DELETE = 1
MOVE = 2
ADD = 3
TRANSLATE = 4
SELECT = 5
COPY = 6
PASTE = 7
class ServosPosGraph(QGLWidget):
def __init__(self, gluton, parent = None):
fmt = QGLFormat()
fmt.setSampleBuffers(True) # antialiasing
super(ServosPosGraph, self).__init__(fmt, parent)
sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
sizePolicy.setHeightForWidth(True)
self.setSizePolicy(sizePolicy)
self.setMouseTracking(True)
self.closestKey = 0
self.closestKeyValuePos = None
self.closestCurveIndex = None
self.mode = Mode.NORMAL
self.copyBuffer = None
self.lastClickPos = None # type: QVector2D
self.interpolatedCurves = []
self.gluton = gluton # type: GLuton
self.glutonView = self.gluton.glutonCanvas # type: GlutonView
self.sliders = gluton.sliders
self.colors = [ # Move to Gluton.py
(87, 87, 87), # Dk. Gray
(173, 35, 35), # Red
(42, 75, 215), # Blue
(29, 105, 20), # Green
(129, 74, 25), # Brown
(129, 38, 192), # Purple
(160, 160, 160), # Lt. Gray
(129, 197, 122), # Lt. Green
(157, 175, 255), # Lt. Blue
(41, 208, 208), # Cyan
(255, 146, 51), # Orange
(255, 238, 51), # Yellow
(233, 222, 187), # Tan
(255, 205, 243), # Pink
(255, 255, 255), # White
]
#....................................................................................
# Connect the mode buttons at the top of the panel so when they are pressed the
# current mode is changed.
def setMode(mode):
self.mode = mode
self.glDraw()
self.gluton.ui.delButton .clicked.connect(lambda: setMode(Mode.DELETE))
self.gluton.ui.addButton .clicked.connect(lambda: setMode(Mode.ADD))
self.gluton.ui.moveButton .clicked.connect(lambda: setMode(Mode.MOVE))
self.gluton.ui.transButton .clicked.connect(lambda: setMode(Mode.TRANSLATE))
self.gluton.ui.selectButton .clicked.connect(lambda: setMode(Mode.SELECT))
self.gluton.ui.copyKeyValuesButton .clicked.connect(lambda: setMode(Mode.COPY))
self.gluton.ui.pasteKeyValuesButton .clicked.connect(lambda: setMode(Mode.PASTE))
# ....................................................................................
def paintGL(self):
self.makeCurrent()
# Need to do this each time for some reason. My guess is because the other canvas changes the projection
# matrix each frame when in orthographic mode.
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
padd = 5
glOrtho(-padd, 256 + padd, -padd, 256 + padd, -50.0, 50.0)
t = self.gluton.timeSlider.value()
glClearColor(0.1,0.1,0.1,1)
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(0.0, 0.0, 1.0)
glRectf(-5, -5, 5, 5)
glColor3f(1.0, 0.0, 0.0)
glBegin(GL_LINES)
glVertex3f(t, 0, 0)
glVertex3f(t, 256, 0)
glEnd()
curves = self.gluton.curves
def setColorAndLineWidth(index):
smallWidth = 2
fatWidth = 5
if self.closestCurveIndex is not None:
if self.closestCurveIndex == index: glLineWidth(fatWidth)
else: glLineWidth(smallWidth)
elif self.closestKeyValuePos is not None:
if self.closestKeyValuePos[0] == index: glLineWidth(fatWidth)
else: glLineWidth(smallWidth)
elif index == self.gluton.currBeingEdited: glLineWidth(fatWidth)
else: glLineWidth(smallWidth)
glColor3f(self.colors[index][0] / 255.0, self.colors[index][1] / 255.0, self.colors[index][2] / 255.0)
if self.gluton.servoPosGraphShowServo[index]: glDisable(GL_LINE_STIPPLE)
else:
glLineStipple(3, 0xAAAA)
glEnable(GL_LINE_STIPPLE)
self.interpolatedCurves = []
closestKey = self.gluton.getClosestKey()
for curveIndex in range(len(curves)):
points = []
curve = curves[curveIndex]
xValues = curve[0]
yValues = curve[1]
setColorAndLineWidth(curveIndex)
glBegin(GL_LINE_STRIP)
interpol = self.gluton.getSplrepInterpolator(curveIndex)
if interpol is not None:
x = np.linspace(0, 256, 256)
y = splev(x, interpol)
for i in range(len(y)):
glVertex2f(x[i], y[i])
points += [(x[i], y[i])]
else:
for i in range(len(xValues)):
glVertex2d(xValues[i], yValues[i])
points += [(xValues[i], yValues[i])]
glEnd()
if self.gluton.servoPosGraphShowServo[curveIndex]: self.interpolatedCurves += [LineString(points)]
else: self.interpolatedCurves += [None]
glColor3f(1,1,1)
for i in range(len(xValues)):
if xValues[i] == closestKey and self.mode != Mode.DELETE and self.mode != Mode.MOVE:
glPointSize(10)
glBegin(GL_POINTS)
glVertex2f(xValues[i], yValues[i])
glEnd()
else:
glPointSize(5)
glBegin(GL_POINTS)
glVertex2f(xValues[i], yValues[i])
glEnd()
glLineWidth(1)
if self.closestKeyValuePos is not None:
glColor3f(1,1,0)
glPointSize(10)
glBegin(GL_POINTS)
self.closestKeyValuePos[2].glVertexf()
glEnd()
def resizeGL(self, w: int, h: int):
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
padd = 5
glOrtho(-padd, 256 + padd, -padd, 256 + padd, -50.0, 50.0)
def initializeGL(self):
glClearColor(0.0, 0.0, 0.0, 1.0)
glClear(GL_COLOR_BUFFER_BIT)
def setMouseTracking(self, flag: bool):
def recursive_set(parent):
for child in parent.findChildren(QObject):
try: child.setMouseTracking(flag)
except: pass
recursive_set(child)
QWidget.setMouseTracking(self, flag)
recursive_set(self)
def getWC_Pos(self, x, y):
model = glGetDoublev(GL_MODELVIEW_MATRIX)
proj = glGetDoublev(GL_PROJECTION_MATRIX)
view = glGetIntegerv(GL_VIEWPORT)
objX, objY, objZ = gluUnProject(x, self.height() - y, 0, model, proj, view)
return QVector2D(objX, objY)
def mousePressEvent(self, event):
if not event.buttons() & Qt.LeftButton: return
self.lastClickPos = self.getWC_Pos(event.x(), event.y())
if self.mode == Mode.COPY:
if self.closestCurveIndex is None: return
self.copyBuffer = self.gluton.curves[self.closestCurveIndex].copy()
if self.mode == Mode.PASTE:
if self.closestCurveIndex is None or self.copyBuffer is None: return
self.gluton.curves[self.closestCurveIndex] = self.copyBuffer.copy()
self.gluton.updateServoSliders()
self.glDraw()
if self.closestKeyValuePos is None: return
if self.mode == Mode.DELETE:
(i, j, k) = self.closestKeyValuePos
del self.gluton.curves[i][0][j]
del self.gluton.curves[i][1][j]
self.closestKeyValuePos = None
self.glDraw()
def mouseMoveEvent(self, event: QMouseEvent):
if self.mode == Mode.NORMAL: return
mousePos = self.getWC_Pos(event.x(), event.y())
if event.buttons() & Qt.LeftButton:
if self.lastClickPos is None:
self.lastClickPos = mousePos
return
diff = mousePos - self.lastClickPos
for i in range(len(self.gluton.curves)):
if not self.gluton.servoPosGraphShowServo[i]: continue
self.lastClickPos = mousePos
return
minDist = sys.float_info.max
self.closestKeyValuePos = None
self.closestCurveIndex = None
if self.mode == Mode.DELETE or self.mode == Mode.MOVE:
for i in range(len(self.gluton.curves)):
if not self.gluton.servoPosGraphShowServo[i]: continue
curve = self.gluton.curves[i]
for j in range(len(curve[0])):
p = QVector2D(curve[0][j], curve[1][j])
dist = (mousePos - p).length()
if dist > 20 or dist > minDist: continue
self.closestKeyValuePos = (i, j, p)
minDist = dist
elif ( self.mode == Mode.TRANSLATE or
self.mode == Mode.SELECT or
self.mode == Mode.COPY or
self.mode == Mode.PASTE):
mousePos = Point(mousePos.x(), mousePos.y())
for i in range(len(self.interpolatedCurves)):
if self.interpolatedCurves[i] is None: continue
dist = mousePos.distance(self.interpolatedCurves[i])
if dist > minDist: continue
minDist = dist
self.closestCurveIndex = i
self.glutonView.highlightServo(self.gluton.servoNames[self.closestCurveIndex])
self.glutonView.glDraw()
self.glDraw()
def wheelEvent(self, event: QWheelEvent):
delta = event.delta() / 2
if delta == 0: return
t = self.gluton.timeSlider.value()
maxDelta = 3
t += min(maxDelta, max(-maxDelta, delta))
if t > self.gluton.timeSlider.maximum(): t = 0
elif t < 0: t = self.gluton.timeSlider.maximum()
self.gluton.timeSlider.setValue(t)
def leaveEvent(self, event):
self.closestKeyValuePos = None
self.closestCurveIndex = None
self.glutonView.unhighlightLastServo()
self.glDraw()
return super(ServosPosGraph, self).enterEvent(event)