-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectorWindow.cpp
389 lines (318 loc) · 11.8 KB
/
ProjectorWindow.cpp
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* LiveFit
* Copyright (C) 2016 The University of Georgia
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "ProjectorWindow.hpp"
#include <QPainter>
#include <QDebug>
#include <QMouseEvent>
#include <QTextDocument>
#include "Util.hpp"
ProjectorWindow::ProjectorWindow(QWidget* parent)
: QOpenGLWidget(parent)
{
// Set the background color of this widget to black (i.e. projector off)
setStyleSheet("* {background-color: rgb(0,0,0);}");
// Set the size of the projector screen, in inches
mProjSize = QSize(50, 50);
// Lock to the fit after 10 points, until next sight
mNumPointFit = 10;
mFitPoints = true;
mPointRadius = 1;
mPointThickness = 0.;
mFitThickness = 0.;
mShowJet = true;
mShowParabola = true;
mShowParam = true;
mWaitTilFall = true;
mMinFallSpeed = 5;
mColorFitLock = QColor(255, 255, 255);
}
void ProjectorWindow::pushBall(TrackingBall ball)
{
// Add the spotted ball to the list of balls
mBalls.append(ball);
// If the history is too large, remove old balls from display
if (mBalls.size() > 10) {
mBalls.removeFirst();
}
}
/** Whether a has lesser center y value than b
* @return a.y < b.y */
static bool comparePredY(KFPrediction a, KFPrediction b) {
return (a.bbox().center().y() < b.bbox().center().y());
}
/** Solve 3 points (y,t) for a quadratic equation y(t) = At^2 + Bt + c
* @param pts A list of 3 points
* @param f An array of 3 doubles; <C,B,A> */
void fitYTPoints(QList<KFPrediction> pts, double f[3]) {
double t,y1,t1,y2,t2,y3,t3;
t = pts.at(0).t();
t1 = 0;
y1 = pts.at(0).bbox().center().y();
t2 = pts.at(1).t()-t;
y2 = pts.at(1).bbox().center().y();
t3 = pts.at(2).t()-t;
y3 = pts.at(2).bbox().center().y();
if (t1 == t2 || t2 == t3 || t1 == t3) {
f[0] = 0; f[1] = 0; f[2] = 0; return;
}
f[2] = ((y1 - y2)/(t1 - t2) - (y2 - y3)/(t2 - t3))/(t1 - t3);
f[1] = (y1 - y2)/(t1 - t2) - f[2]*(t1 + t2);
f[0] = y2 - f[2]*t2*t2 - f[1]*t2;
}
/** Solve 3 points (x,t) [really first, last] for a linear equation
* x(t) = mt + b
* @param pts A list of 3 points
* @param f An array of 2 doubles; <b, m> */
void fitXTPoints(QList<KFPrediction> pts, double f[2]) {
double t,y1,t1,y3,t3;
t = pts.at(0).t();
t1 = 0;
y1 = pts.at(0).bbox().center().x();
t3 = pts.at(2).t()-t;
y3 = pts.at(2).bbox().center().x();
f[1] = (y1 - y3)/(t1 - t3);
f[0] = y1 - f[1]*t1;
}
void ProjectorWindow::pushPred(KFPrediction pred)
{
if (mFitLocked && mLockFit) {
// If we are locked, and we have "lock until manual clear" checked
} else {
if (mDataStale) {
clearTrack();
}
// Add the prediction to the list of predictions
mPreds.append(KFPrediction(pred));
// Trim the viewer points if it's too long
if (mPreds.size() > 20) {
mPreds.removeFirst();
}
if (!mNumPointFit) {
// We fit to all predictions...
mFitPreds.append(KFPrediction(pred));
// If the history is too large, remove old predictions
if (mPreds.size() > 20) {
mFitPreds.removeFirst();
}
} else {
if ((mWaitTilFall && pred.jet().y()*pred.dt() < mMinFallSpeed) ||
(mFitPreds.size() < mNumPointFit)) {
mFitPreds.append(KFPrediction(pred));
mFitLocked = false;
}
if ((!mWaitTilFall || pred.jet().y()*pred.dt() >= mMinFallSpeed) &&
(mFitPreds.size() >= mNumPointFit)){
mFitLocked = true;
}
}
// We fit our parabola to the predictions
if (!mFitPoints) {
polynomialFitKFX(2, mFitPreds.at(0).t(), mFitPreds, mFitLineX);
polynomialFitKFY(3, mFitPreds.at(0).t(), mFitPreds, mFitParabolaY);
} else if (mFitLocked && mFitPreds.size() > 3 && mMarkedPoints.empty()) {
mMarkedPoints.append(mFitPreds.at(0));
mMarkedPoints.append(*std::min_element(
++mFitPreds.begin(), --mFitPreds.end(),
comparePredY));
mMarkedPoints.append(mFitPreds.at(mFitPreds.size()-1));
fitYTPoints(mMarkedPoints, mFitParabolaY);
fitXTPoints(mMarkedPoints, mFitLineX);
}
// Update the window. Hopefully, we can make this more efficient...
update();//(pred.bbox());
}
}
void ProjectorWindow::clearTrack()
{
mPreds.clear();
mFitPreds.clear();
mDataStale = false;
mMarkedPoints.clear();
mFitLocked = false;
update();
}
QRectF ProjectorWindow::relRectToWindow(QRectF rect) {
// Convert a rect in projector coordinates to widget coordinates
return QRectF(QPointF(rect.left()*width()/mProjSize.width(),
rect.top()*height()/mProjSize.height()),
QPointF(rect.right()*width()/mProjSize.width(),
rect.bottom()*height()/mProjSize.height()));
}
QPointF ProjectorWindow::relPointToWindow(QPointF pt) {
// Convert a point in projector coordinates to widget coordinates
return QPointF(pt.x()*width()/mProjSize.width(),
pt.y()*height()/mProjSize.height());
}
void ProjectorWindow::paintEvent(QPaintEvent* ev)
{
// This event is called when the widget is asked to repaint itself
// All of our custom painter logic should go in here.
// Painter to paint the window
QPainter painter;
// Hold on to the normal (transparent) brush
QBrush normalBrush;
// List of data HTMLs
QStringList dataHtml;
// Pens (i.e. line drawing settings for the painter)
QPen ballPen = QPen((QColor(0, 0, 255)));
ballPen.setWidthF(mPointThickness);
QPen kfSeenPen = QPen((QColor(255,0,0)));
kfSeenPen.setWidthF(mPointThickness);
QPen kfMissPen = QPen((QColor(255,255,0)));
kfMissPen.setWidthF(mPointThickness);
QPen markPen = QPen((QColor(255,255,255)));
markPen.setWidthF(mPointThickness);
QPen fitPen = QPen((QColor(255,0,255)));
fitPen.setWidthF(mFitThickness);
QPen fitLockPen = QPen(mColorFitLock);
fitLockPen.setWidthF(mFitThickness);
// We have to begin painting. Remember to end.
painter.begin(this);
normalBrush = painter.brush();
// This brush is the background color of the widget.
// Fill the window in case.
QBrush background = QBrush(QColor(0,0,0));
painter.fillRect(ev->rect(), background);
// Draw the ball indicators on the projector screen
painter.setPen(ballPen);
QList<TrackingBall>::const_iterator ballsIter;
for (ballsIter = mBalls.begin(); ballsIter != mBalls.end(); ++ballsIter) {
painter.drawEllipse(
(QPoint)(*ballsIter).center()*2,
(int)(*ballsIter).r()*2,
(int)(*ballsIter).r()*2);
}
// Draw the KF prediction indicators on the screen
QList<KFPrediction>::const_iterator predsIter;
for (predsIter = mPreds.begin(); predsIter != mPreds.end(); ++predsIter) {
// The color of the indicator depends on if the ball was actually
// seen on this frame or not
if (((*predsIter).seen() || !mColorMiss)) {
painter.setPen(kfSeenPen);
} else {
painter.setPen(kfMissPen);
}
// Draw the bounding box of the predicted ball
if (mVerboseKF) {
painter.drawRect(relRectToWindow((*predsIter).bbox()));
} else {
painter.drawEllipse(QRectF(relPointToWindow(
(*predsIter).bbox().center()) -
QPointF(mPointRadius/2,mPointRadius/2),
QSizeF(mPointRadius,mPointRadius)));
}
if (mShowJet) {
// Draw the predicted velocities of the predicted ball
painter.drawLine(relPointToWindow((*predsIter).bbox().center()),
relPointToWindow(
(*predsIter).bbox().center() +
((QPointF)(*predsIter).jet() *
(*predsIter).dt())));
}
}
int j; double t0;
j = 0;
// Draw the predicted flight trajectory of the ball
if (mShowParam || mShowFit) {
double a, b, A, B, C, U, V, W;
// x(t) = at + b
a = mFitLineX[1];
b = mFitLineX[0];
// y(t) = At^2 + Bt + C
A = mFitParabolaY[2];
B = mFitParabolaY[1];
C = mFitParabolaY[0];
// y(x) = Ux^2 + Vx + W
U = A/a/a;
V = B/a - 2*A*b/a/a;
W = A*b*b/a/a - B*b/a + C;
// Show the fit parabola
if (mShowParabola) {
if (mFitLocked) {
painter.setPen(fitLockPen);
} else
{
painter.setPen(fitPen);
}
QPointF p1(0, W);
QPointF p2(mProjSize.width(),
U*mProjSize.width()*mProjSize.width() +
V*mProjSize.width() + W);
QPointF c(0.5*mProjSize.width(), W + .5*mProjSize.width()*V);
QPainterPath fitPath(relPointToWindow(p1));
fitPath.quadTo(relPointToWindow(c), relPointToWindow(p2));
painter.drawPath(fitPath);
}
// Draw the predicted y(t) and x(t) equation
if (mShowParam) {
dataHtml << QString("y(t) = %1 t<sup>2</sup> + %2 t + %3")
.arg(A, 0, 'f', 3)
.arg(B, 0, 'f', 3)
.arg(C, 0, 'f', 3)
<< QString("x(t) = %1 t + %2")
.arg(a, 0, 'f', 3)
.arg(b, 0, 'f', 3);
}
}
if (!mMarkedPoints.empty()) {
t0 = mMarkedPoints.at(0).t();
}
//QStringList markHtml;
QRectF markRect;
QFont markFont = painter.font();
markFont.setPointSize(mFontSize);
int mMarkRadius = mPointRadius*0.8;
// Draw marked points
painter.setFont(markFont);
for (predsIter = mMarkedPoints.begin(); predsIter != mMarkedPoints.end();
++predsIter) {
painter.setPen(markPen);
dataHtml << QString("%1: (%2, %3, %4)")
.arg(j)
.arg(predsIter->t()-t0, 0, 'f', 3)
.arg(predsIter->bbox().center().x(), 0, 'f', 3)
.arg(predsIter->bbox().center().y(), 0, 'f', 3);
markRect = QRectF((relPointToWindow(
(*predsIter).bbox().center()) -
QPointF(mMarkRadius/2,mMarkRadius/2)),
QSizeF(mMarkRadius,mMarkRadius));
painter.setBrush(background);
painter.drawEllipse(markRect);
painter.setBrush(normalBrush);
painter.drawText(markRect,
Qt::AlignCenter|Qt::TextDontClip,
QString("%1").arg(j));
j++;
}
if (!dataHtml.empty()) {
QTextDocument td;
td.setDefaultStyleSheet(
QString("body {"
" color: rgb(255,255,255);"
" font-size: %1pt;"
"}")
.arg(mFontSize));
td.setHtml(QString(
"<body>%1</body>")
.arg(dataHtml.join("<br>"))
);
td.drawContents(&painter);
}
painter.end();
}