-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarbleWorker.cpp
404 lines (318 loc) · 12 KB
/
MarbleWorker.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include "MarbleWorker.h"
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <QColor>
#include <marble/GeoDataMultiGeometry.h>
#include <marble/GeoDataLineString.h>
#include <marble/GeoDataPoint.h>
#include <marble/GeoDataCoordinates.h>
#include <marble/GeoDataLineStyle.h>
#include <marble/GeoDataStyle.h>
#include <marble/GeoDataPlacemark.h>
using namespace Marble;
MarbleWorker::MarbleWorker(QObject *parent) : QThread(parent) {
}
MarbleWorker::~MarbleWorker() {
mutex.lock();
stop = true;
condition.wakeAll();
mutex.unlock();
wait();
}
void MarbleWorker::loadData(QStringList filesToLoad) {
QMutexLocker locker(&mutex);
this->shouldLoadFile = true;
this->filenames = filesToLoad;
if (!isRunning()) {
start(LowPriority);
} else {
condition.wakeAll();
}
}
void MarbleWorker::findStartingPoints() {
QMutexLocker locker(&mutex);
this->shouldFindStartingPoints = true;
if (!isRunning()) {
start(LowPriority);
} else {
condition.wakeAll();
}
}
void MarbleWorker::receiveClick(float lat, float lon) {
QMutexLocker locker(&mutex);
this->receivedClick = true;
this->clickedLat = lat;
this->clickedLon = lon;
if (!isRunning()) {
start(LowPriority);
} else {
condition.wakeAll();
}
}
void MarbleWorker::receiveRedrawStartingPoints(int numberOfStartingPoints) {
QMutexLocker locker(&mutex);
this->redrawStartingPoints = true;
this->amountOfPointsToShow = numberOfStartingPoints;
if (!isRunning()) {
start(LowPriority);
} else {
condition.wakeAll();
}
}
Marble::GeoDataDocument *MarbleWorker::createDocument(Progress progress, int roads) {
GeoDataMultiGeometry *multiGeometry = new GeoDataMultiGeometry();
int roadCounter = 0;
for (std::vector<std::shared_ptr<RoadPoint>> loadedRoad : progress.loadedRoads) {
roadCounter++;
if (roadCounter <= roads) {
continue;
}
GeoDataLineString *lineString = new GeoDataLineString();
for (std::shared_ptr<RoadPoint> point : loadedRoad) {
lineString->append(GeoDataCoordinates(point->lon, point->lat, 0, GeoDataCoordinates::Degree));
}
multiGeometry->append(lineString);
}
GeoDataLineStyle lineStyle(QColor(120, 120, 120, 90));
lineStyle.setPhysicalWidth(5.0);
QSharedPointer<GeoDataStyle> geoDataStyle = QSharedPointer<GeoDataStyle>(new GeoDataStyle());
geoDataStyle->setLineStyle(lineStyle);
GeoDataPlacemark *routePlacemark = new GeoDataPlacemark();
routePlacemark->setGeometry(multiGeometry);
routePlacemark->setStyle(geoDataStyle);
GeoDataDocument *document = new GeoDataDocument();
document->append(routePlacemark);
return document;
}
void MarbleWorker::addRouteGraphsToDocument(std::vector<std::shared_ptr<RouteGraph>> routeGraphs, GeoDataDocument *document, int number) {
int counter = 0;
for (std::shared_ptr<RouteGraph> savedRouteGraph : routeGraphs) {
counter++;
if (counter > number && number >= 0) {
break;
}
GeoDataPlacemark *placemark = new GeoDataPlacemark();
placemark->setGeometry(new GeoDataPoint(savedRouteGraph->startingPoint->lon, savedRouteGraph->startingPoint->lat, 0, GeoDataCoordinates::Degree));
placemark->setDescription(QString("%1 %2").arg(savedRouteGraph->farthestEndpoint->lat, 0, 'f', 6).arg(savedRouteGraph->farthestEndpoint->lon, 0, 'f', 6));
document->append(placemark);
}
}
void MarbleWorker::addRouteGraphToDocument(std::shared_ptr<RouteGraph> routeGraph, GeoDataDocument *document) {
std::unordered_set<std::shared_ptr<RoadPoint>> visited;
std::vector<std::pair<std::shared_ptr<RoadPoint>, std::shared_ptr<RoadPoint>>> toVisit;
toVisit.push_back(std::pair(routeGraph->startingPoint, nullptr));
GeoDataMultiGeometry *multi = new GeoDataMultiGeometry();
GeoDataLineString *lineString = new GeoDataLineString();
auto it = toVisit.begin();
while (it != toVisit.end()) {
std::shared_ptr<RoadPoint> currentPoint = it->first;
visited.insert(currentPoint);
GeoDataCoordinates currentCoordinates(currentPoint->lon, currentPoint->lat, 0, GeoDataCoordinates::Degree);
if (lineString->size() == 0 && it->second != nullptr) {
GeoDataCoordinates connectedCoordinates(it->second->lon, it->second->lat, 0, GeoDataCoordinates::Degree);
lineString->append(connectedCoordinates);
}
lineString->append(currentCoordinates);
int possibleConnections = 0;
for (std::shared_ptr<Connection> connection : currentPoint->connections) {
std::shared_ptr<RoadPoint> connectedPoint = connection->connectedPoint;
if (visited.count(connectedPoint) == 0 && routeGraph->reachablePoints.count(connectedPoint) == 1) {
possibleConnections++;
it++;
it = toVisit.insert(it, std::pair(connectedPoint, currentPoint));
it--;
}
}
if (possibleConnections != 1) {
if (lineString->size() > 1) {
multi->append(lineString);
} else {
delete lineString;
}
lineString = new GeoDataLineString();
if (possibleConnections > 0) {
lineString->append(currentCoordinates);
}
}
it++;
}
if (lineString->size() > 1) {
multi->append(lineString);
}
GeoDataPlacemark *placemark = new GeoDataPlacemark();
placemark->setGeometry(multi);
GeoDataLineStyle lineStyle(QColor(255, 0, 0, 90));
lineStyle.setPhysicalWidth(5.0);
QSharedPointer<GeoDataStyle> geoDataStyle = QSharedPointer<GeoDataStyle>(new GeoDataStyle());
geoDataStyle->setLineStyle(lineStyle);
placemark->setStyle(geoDataStyle);
placemark->setZoomLevel(1);
document->append(placemark);
}
void MarbleWorker::run() {
forever {
mutex.lock();
if (this->stop) {
return;
}
bool shouldLoadFile = this->shouldLoadFile;
QStringList fileNames = this->filenames;
this->shouldLoadFile = false;
this->filenames = QStringList();
bool shouldFindStartingPoints = this->shouldFindStartingPoints;
this->shouldFindStartingPoints = false;
bool receivedClick = this->receivedClick;
this->receivedClick = false;
double clickedLat = this->clickedLat;
double clickedLon = this->clickedLon;
bool redrawStartingPoints = this->redrawStartingPoints;
this->redrawStartingPoints = false;
mutex.unlock();
if (shouldLoadFile) {
for (QString fileName : fileNames) {
GDALDataset *dataset;
dataset = (GDALDataset*) GDALOpenEx(fileName.toLocal8Bit().data(), GDAL_OF_VECTOR, NULL, NULL, NULL);
if (!dataset) {
continue;
}
int roads = 0;
if (!pointMap) {
pointMap = new PointMap();
}
pointMap->loadDataset(dataset, [this, &roads](Progress progress) {
Marble::GeoDataDocument *document = createDocument(progress, roads);
emit this->mapUpdated(document, false, QString(progress.message.c_str()), progress.percent);
roads = progress.loadedRoads.size();
printf("\r%s: %i%%", progress.message.c_str(), progress.percent);
std::cout << std::flush;
});
emit mapUpdated(nullptr, false, QString("Finished loading road data"), 100);
}
}
if (shouldFindStartingPoints) {
routeGraphs.clear();
emit mapUpdated(nullptr, false, QString("Searching for possible starting points"), 0);
// Find possible starting points
std::vector<std::shared_ptr<RoadPoint>> startingPoints;
// Go through all points found and find the ones that it is possible to go out from,
// but not into
for (std::shared_ptr<RoadPoint> possibleStart : pointMap->getAllPoints()) {
bool canRollOut = false;
bool canRollIn = false;
for (std::shared_ptr<Connection> connection : possibleStart->connections) {
if (connection->getOutputSpeed(MINIMUM_SPEED_KMH / 3.6) > 0) {
canRollOut = true;
}
std::shared_ptr<RoadPoint> otherPoint = connection->connectedPoint;
for (std::shared_ptr<Connection> otherConnection : otherPoint->connections) {
if (otherConnection->connectedPoint == possibleStart && otherConnection->getOutputSpeed(MINIMUM_SPEED_KMH / 3.6) > 0) {
canRollIn = true;
}
}
}
if (canRollOut && !canRollIn) {
startingPoints.push_back(possibleStart);
}
}
// Sort starting points by height
sort(startingPoints.begin(), startingPoints.end(), [](std::shared_ptr<RoadPoint> point1, std::shared_ptr<RoadPoint> point2){return point1->z > point2->z;});
int startingPointCounter = 0;
while (startingPointCounter < startingPoints.size()) {
std::shared_ptr<RoadPoint> currentPoint = startingPoints.at(startingPointCounter);
std::shared_ptr<RouteGraph> routeGraph = std::shared_ptr<RouteGraph>(new RouteGraph(currentPoint));
// Remove all starting points that can be reached from this initial starting point
auto startingPointIterator = startingPoints.begin();
while (startingPointIterator != startingPoints.end()) {
if (routeGraph->reachablePoints.count(*startingPointIterator) > 0) {
startingPointIterator = startingPoints.erase(startingPointIterator);
} else {
startingPointIterator++;
}
}
startingPointCounter++;
if (startingPointCounter % 100 == 0) {
GeoDataDocument *document = new GeoDataDocument();
addRouteGraphsToDocument(routeGraphs, document);
emit mapUpdated(document, false, QString("Searching for possible starting points"), (int) (startingPointCounter * 100.0 / startingPoints.size()));
}
// Don't add starting point that don't go very far
if (routeGraph->farthestDistance < MINIMUM_ROUTE_DISTANCE) {
continue;
}
bool shouldInsert = true;
auto routeGraphIterator = routeGraphs.begin();
while (routeGraphIterator != routeGraphs.end()) {
std::shared_ptr<RouteGraph> otherRouteGraph = *routeGraphIterator;
if (routeGraph->farthestEndpoint != otherRouteGraph->farthestEndpoint) {
routeGraphIterator++;
continue;
}
shouldInsert = false;
// This seems to be have all the same endpoints as our current starting point
// Check if this one is longer
if (routeGraph->farthestDistance > otherRouteGraph->farthestDistance) {
routeGraphIterator = routeGraphs.erase(routeGraphIterator);
routeGraphIterator = routeGraphs.insert(routeGraphIterator, routeGraph);
}
break;
}
if (shouldInsert) {
routeGraphs.push_back(routeGraph);
}
}
emit mapUpdated(nullptr, false, QString("Finished searching for starting points"), 100);
sort(this->routeGraphs.begin(), this->routeGraphs.end(), [](std::shared_ptr<RouteGraph> graph1, std::shared_ptr<RouteGraph> graph2){return graph1->farthestDistance > graph2->farthestDistance;});
redrawStartingPoints = true;
}
bool shouldRedraw = false;
GeoDataDocument *document = new GeoDataDocument();
if (receivedClick) {
// Find a route that is close enough
std::shared_ptr<RouteGraph> closestRouteGraph = nullptr;
for (auto routeGraph : routeGraphs) {
if (abs(routeGraph->startingPoint->lat - clickedLat) < 0.0001 && abs(routeGraph->startingPoint->lon - clickedLon) < 0.0001) {
closestRouteGraph = routeGraph;
break;
}
}
if (closestRouteGraph != nullptr) {
selectedRouteGraph = closestRouteGraph;
redrawStartingPoints = true;
shouldRedraw = true;
}
}
mutex.lock();
int numberOfStartingPoints = this->amountOfPointsToShow;
mutex.unlock();
if (numberOfStartingPoints == 100) {
numberOfStartingPoints = -1;
}
if (redrawStartingPoints) {
shouldRedraw = true;
addRouteGraphsToDocument(routeGraphs, document, numberOfStartingPoints);
}
if (shouldRedraw) {
if (selectedRouteGraph != nullptr) {
addRouteGraphToDocument(selectedRouteGraph, document);
}
QString statusString;
if (selectedRouteGraph != nullptr) {
statusString = QString("Farthest reachable point is %1 meters away").arg(selectedRouteGraph->farthestDistance, 0, 'f', 0);
} else {
statusString = QString("Showing %1 starting points");
if (numberOfStartingPoints == -1) {
statusString = statusString.arg("all");
} else {
statusString = statusString.arg(QString("%1 best").arg(numberOfStartingPoints));
}
}
emit mapUpdated(document, false, statusString, 0);
} else {
delete document;
}
mutex.lock();
condition.wait(&mutex);
mutex.unlock();
}
}