-
Notifications
You must be signed in to change notification settings - Fork 0
/
occupancy_predictor.cpp
92 lines (88 loc) · 2.87 KB
/
occupancy_predictor.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
#include "occupancy_predictor.h"
#include <QImage>
#include <QColor>
#include <QCoreApplication>
void OccupancyPredictor::predictFromGrids(const QHash<QString, OccupancyGrid> &grids)
{
for (const QString& date: grids.keys())
{
updatePrediction(grids[date]);
}
}
void OccupancyPredictor::updatePrediction(const OccupancyGrid &grid)
{
int minX, maxX, minY, maxY;
grid.getBounds(minX, maxX, minY, maxY);
for (int x = minX; x < maxX; ++x)
{
for (int y = minY; y < maxY; ++y)
{
if (grid.getCellProbability(QPointF(x, y)) > 0.8)
{
_prediction[x][y].occupied++;
}
else
{
_prediction[x][y].free++;
}
}
}
}
PredictedOccupancyGrid OccupancyPredictor::getPrediction()
{
return _prediction;
}
void OccupancyPredictor::writeImage(
int xMin,
int yMin,
int xMax,
int yMax,
QString imageName) const
{
int width = xMax - xMin;
int height = yMax - yMin;
int cellSize = 10; //px
QImage image(width * cellSize, height * cellSize, QImage::Format_RGB32);
qDebug() << xMin << xMax << yMin << yMax;
QColor colorGreen = QColor(0, 100, 0);
QColor colorRed = QColor(100, 0, 0);
QColor colorGray = QColor(50, 50, 50);
for (int x = 0; x < image.width(); x+=cellSize)
{
for (int y = 0; y < image.height(); y+=cellSize)
{
for (int xx = x; xx < x + cellSize; ++xx)
{
for (int yy = y; yy < y + cellSize; ++yy)
{
if (xx < image.width() && yy < image.height())
{
OccupancyCell cell = _prediction[xMin + x/cellSize][yMin + y/cellSize];
if (cell.free < 1 || cell.occupied < 1)
{
image.setPixel(xx, image.height() - yy, colorGray.rgb());
}
else if (cell.free > cell.occupied)
{
image.setPixel(xx, image.height() - yy, colorGreen.rgb());
}
else if (cell.occupied > cell.free)
{
image.setPixel(xx, image.height() - yy, colorRed.rgb());
}
else
{
image.setPixel(xx, image.height() - yy, colorGray.rgb());
}
}
}
}
}
}
QString dirPath = QCoreApplication::applicationDirPath();
dirPath = dirPath.left(dirPath.lastIndexOf('/')) + "/OSMViewer/grids/";
qDebug() << "saving";
qDebug() << image.size();
qDebug() << image.save(dirPath + imageName + ".jpg");
qDebug() << "saved"<<dirPath + imageName + ".jpg";
}