/
parking_lots.cpp
171 lines (160 loc) · 5.28 KB
/
parking_lots.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
#include "parking_lots.h"
#include <QDebug>
#include <QImage>
#include <QColor>
#include <QFile>
#include <QCoreApplication>
#include <QImageWriter>
#include <iostream>
#include <iomanip>
ParkingLots::ParkingLots()
{
}
ParkingLots::ParkingLots(const qreal &leftCorner, const qreal &bottomCorner)
{
QPointF tempPoint;
QPointF leftBottomCorner(leftCorner, bottomCorner);
OccupancyCell emptyCell;
QPointF leftBottomUtm(871831, 6077369);
qDebug() << "num of rows"<<_numOfRows;
qDebug() << "num of cols"<<_numOfCols;
for (int colNum = 0; colNum < _numOfCols; ++colNum)
{
for (int rowNum = 0; rowNum < _numOfRows; ++rowNum)
{
tempPoint.ry() =
leftBottomCorner.y() * _metersInPx + leftBottomUtm.y() +
rowNum * _height * _metersInPx +
(_height / 2) * _metersInPx;
tempPoint.rx() =
leftBottomCorner.x() * _metersInPx + leftBottomUtm.x() +
colNum * _width * _metersInPx +
(_width / 2) * _metersInPx;
// the rows are as follows:
// 01___23___45___67___ ...
// so we need to account for the gap
tempPoint.rx() = tempPoint.x() + (colNum / _gap_every) * (_gap) * _metersInPx;
_centers.append(tempPoint);
_occupancy.append(emptyCell);
_currentOccupancy.append(emptyCell);
}
}
}
void ParkingLots::reInitCurrentOccupancy()
{
_currentOccupancy.clear();
OccupancyCell emptyCell;
for (int i = 0; i < _occupancy.size(); ++i)
{
_currentOccupancy.append(emptyCell);
}
}
void ParkingLots::updateClosest(const QPointF &detection)
{
qreal minDist = 1E+20; //should be big enough
int minIndex = -1;
for (int i = 0; i < _centers.size(); ++i)
{
qreal currentDist = sqrt(QPointF::dotProduct(_centers[i] - detection, _centers[i] - detection));
if (minDist > currentDist)
{
minDist = currentDist;
minIndex = i;
}
}
if (minIndex < 0)
{
qDebug()<<"parking lots update failed for" << detection;
return;
}
_occupancy[minIndex].occupied++;
_currentOccupancy[minIndex].occupied++;
_tempUpdatedIndeces.append(minIndex);
_realDetections.append(detection);
}
void ParkingLots::updateLeftFree(const QString& date)
{
for (int i = 0; i < _occupancy.size(); ++i)
{
if (!_tempUpdatedIndeces.contains(i))
{
_occupancy[i].free++;
_currentOccupancy[i].free++;
}
}
_tempUpdatedIndeces.clear();
writeGraphFile(date, &_currentOccupancy);
reInitCurrentOccupancy();
}
void ParkingLots::update(const QVector<QPointF> &cars)
{
for (const QPointF& car: cars)
{
this->updateClosest(car);
}
}
void ParkingLots::writeGraphFile(const QString& type, const QVector<OccupancyCell> *occupancy) const
{
QFile file("/home/igor/Work/Thesis/MiscCode/OccupancyAnalyzer/grids/" + type + "_parkings_graph.dat");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
if (occupancy == nullptr)
{
occupancy = &_occupancy;
}
QTextStream out(&file);
out.setRealNumberPrecision(10);
out << "rows: " << _numOfRows << "\t";
out << "cols: " << _numOfCols << "\t";
out << "gap_every: " << _gap_every << endl;
for (int i = 0; i < _centers.size(); ++i)
{
out << "x: " << _centers[i].x() << " y: " << _centers[i].y();
out << " free_prob: " << occupancy->at(i).getFreeProb() << endl;
}
qDebug() << "file written";
}
void ParkingLots::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 colorGray = QColor(50, 50, 50);
for (int i = 0; i < _occupancy.size(); ++i)
{
int currentCenterX = floor((_centers[i].x() - xMin) * cellSize);
int currentCenterY = floor((_centers[i].y() - yMin) * cellSize);
for (int xx = currentCenterX - 2 * cellSize; xx < currentCenterX + 2 * cellSize; ++xx)
{
for (int yy = currentCenterY - cellSize; yy < currentCenterY + cellSize; ++yy)
{
OccupancyCell cell = _occupancy[i];
if (cell.free + cell.occupied < 1)
{
image.setPixel(xx, image.height() - yy, colorGray.rgb());
}
// amount of green color from 0 to 1
qreal greenAmount = (qreal) cell.free / (cell.free + cell.occupied);
greenAmount*=255;
QColor color = QColor(255 - greenAmount, greenAmount, 0);
image.setPixel(xx, image.height() - yy, color.rgb());
}
}
}
QString dirPath = QCoreApplication::applicationDirPath();
dirPath = dirPath.left(dirPath.lastIndexOf('/')) + "/OSMViewer/grids/";
qDebug() << "saving!!!";
qDebug() << image.size();
QImageWriter writer("/home/igor/Work/Thesis/MiscCode/OccupancyAnalyzer/grids/" + imageName + ".jpg");
if (!writer.write(image))
{
qDebug() << writer.errorString();
}
}