-
Notifications
You must be signed in to change notification settings - Fork 9
/
firarenderarea.cpp
192 lines (179 loc) · 5.53 KB
/
firarenderarea.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
#include "firarenderarea.h"
#include <QPainter>
#include <QTime>
#include <QPen>
#include <QDebug>
#include <QEvent>
#include <QMouseEvent>
#include <math.h>
#include <assert.h>
FIRARenderArea::FIRARenderArea(QWidget *parent) :
QWidget(parent)
{
bsMutex = NULL;
beliefStateSh = NULL;
scribbling = false;
x[0] = 605*2/3;
y[0] = 410*2/3;
theta[0] = 3.1418/8;
drawTraj = false;
}
void FIRARenderArea::setEndPose(Pose p)
{
x[0] = (p.queryX() + HALF_FIELD_MAXX) * this->width() / (2.0 * HALF_FIELD_MAXX);
y[0] = (-p.queryY() + HALF_FIELD_MAXY) * this->height() / (2.0 * HALF_FIELD_MAXY);
theta[0] = -p.queryTheta();
update();
}
Pose FIRARenderArea::getEndPose()
{
int xx = (x[0] * HALF_FIELD_MAXX * 2) / this->width() - HALF_FIELD_MAXX;
int yy = -((y[0] * HALF_FIELD_MAXY * 2) / this->height() - HALF_FIELD_MAXY);
return Pose(xx, yy, -theta[0]);
}
void FIRARenderArea::setTrajectory(QPainterPath p)
{
traj = p;
update();
}
void FIRARenderArea::paintEvent(QPaintEvent *)
{
// qDebug() << "Pose in render area: " << pose.x() << ", " << pose.y() << ", " << pose.theta()*180/3.141;
QPainter painter(this);
drawField(painter);
BeliefState bs;
if(bsMutex) {
bsMutex->lock();
if(beliefStateSh)
bs = *beliefStateSh;
bsMutex->unlock();
}
for(int i = 0; i < 5; i++) {
if(bs.homeIsPresent[i]) {
drawBot(painter, bs.homeX[i], bs.homeY[i], bs.homeTheta[i], true);
}
if(bs.awayIsPresent[i]) {
drawBot(painter, bs.awayX[i], bs.awayY[i], bs.awayTheta[i], false);
}
}
drawBot(painter, predictedPose.queryX(), predictedPose.queryY(), predictedPose.queryTheta(), false);
if(bs.ballIsPresent) {
drawBall(painter, bs.ballX, bs.ballY);
}
drawPose(painter);
drawTrajectory(painter);
}
void FIRARenderArea::drawField(QPainter &painter)
{
painter.save();
painter.setRenderHint(QPainter::Antialiasing);
QRect field;
field.setTopLeft(QPoint(0,0));
field.setBottomRight(QPoint(this->width()-1, this->height()-1));
painter.setPen(Qt::black);
painter.setBrush(QBrush(Qt::green));
painter.drawRect(field);
painter.restore();
}
void FIRARenderArea::drawBot(QPainter &painter, double botX, double botY, double botTheta, bool isHome)
{
// size of field in qt is 605x410, same ratio as in ssl-vision.
painter.save();
painter.setRenderHint(QPainter::Antialiasing);
QRectF bot; // is in the strategy coordinate system only.
bot.setHeight(223); // 7.5 * fieldXConvert, what would be better? since fieldXConvert != fieldYConvert, its approx
bot.setWidth(223);
bot.moveCenter(QPoint(0, 0));
QLine midLine;
midLine.setLine(bot.center().x(), bot.center().y(), bot.center().x()+bot.width()/2, bot.center().y());
painter.translate(this->width()/2, this->height()/2);
painter.scale(this->width()/(double)(2*HALF_FIELD_MAXX), this->height()/(double)(2*HALF_FIELD_MAXY));
painter.translate(botX, -botY);
painter.rotate(-botTheta*180/3.1415); // pathetic
QPen pen;
if(isHome)
pen.setColor(Qt::blue);
else
pen.setColor(Qt::red);
pen.setWidth(10);
painter.setPen(pen);
painter.drawRect(bot);
painter.drawLine(midLine);
painter.restore();
}
void FIRARenderArea::drawBall(QPainter &painter, double ballX, double ballY)
{
painter.save();
painter.setRenderHint(QPainter::Antialiasing);
QPen pen;
pen.setColor("orange");
pen.setWidth(5);
QBrush brush;
brush.setColor("orange");
brush.setStyle(Qt::SolidPattern);
painter.setBrush(brush);
painter.setPen(pen);
painter.translate(this->width()/2, this->height()/2);
painter.scale(this->width()/(double)(2*HALF_FIELD_MAXX), this->height()/(double)(2*HALF_FIELD_MAXY));
painter.translate(ballX, -ballY);
// dia = 4.2 cm
painter.drawEllipse(QPointF(0, 0), 4.2/2*Constants::fieldXConvert, 4.2/2*Constants::fieldYConvert);
painter.restore();
}
void FIRARenderArea::drawPose(QPainter &painter)
{
painter.save();
painter.setRenderHint(QPainter::Antialiasing);
QPen pen;
pen.setColor(Qt::black);
pen.setWidth(2);
painter.setPen(pen);
int x2, y2;
x2 = x[0] + cos(theta[0])*50;
y2 = y[0] + sin(theta[0])*50;
painter.drawLine(x[0], y[0], x2, y2);
pen.setColor(Qt::red);
painter.setPen(pen);
painter.drawEllipse(QPointF(x[0], y[0]), 10, 10);
painter.restore();
}
void FIRARenderArea::drawTrajectory(QPainter &painter)
{
if (!drawTraj)
return;
painter.save();
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(this->width()/2, this->height()/2);
painter.scale(this->width()/(double)(2*HALF_FIELD_MAXX), -this->height()/(double)(2*HALF_FIELD_MAXY));
QPen pen;
pen.setColor(Qt::blue);
pen.setWidth(4);
painter.setPen(pen);
painter.drawPath(traj);
painter.restore();
}
void FIRARenderArea::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton) {
scribbling = true;
x[0] = event->x();
y[0] = event->y();
theta[0] = 0;
update();
}
}
void FIRARenderArea::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton && scribbling) {
scribbling = false;
theta[0] = atan2(event->y()-y[0], event->x()-x[0]);
update();
}
}
void FIRARenderArea::mouseMoveEvent(QMouseEvent *event)
{
if((event->buttons() & Qt::LeftButton) && scribbling) {
theta[0] = atan2(event->y()-y[0], event->x()-x[0]);
update();
}
}