-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathservice.cpp
78 lines (61 loc) · 1.97 KB
/
pathservice.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
#include "pathservice.h"
PathService::PathService() = default;
std::map<RoadPoint, QVector<RoadPoint> *> *PathService::FromEdgesToAdjMatrix(QVector<RoadEdge> *edges)
{
std::map<RoadPoint, QVector<RoadPoint> *> *adjMatrix
= new std::map<RoadPoint, QVector<RoadPoint> *>;
std::map<RoadPoint, bool> been;
for (auto edge : *edges) {
if (!been[edge.startPos]) {
(*adjMatrix)[edge.startPos] = new QVector<RoadPoint>;
been[edge.startPos] = true;
}
if (!been[edge.endPos]) {
(*adjMatrix)[edge.endPos] = new QVector<RoadPoint>;
been[edge.endPos] = true;
}
(*adjMatrix)[edge.startPos]->push_back(edge.endPos);
}
return adjMatrix;
}
QVector<RoadPoint> *PathService::FindPath(std::map<RoadPoint, QVector<RoadPoint> *> *adjMatrix,
RoadPoint start,
RoadPoint end)
{
std::map<RoadPoint, float> dist;
std::map<RoadPoint, RoadPoint> prev;
std::map<RoadPoint, bool> been;
for (auto vertex : *adjMatrix) {
dist[vertex.first] = FLOAT_MAX_VALUE;
prev[vertex.first] = RoadPoint();
}
dist[start] = 0;
std::queue<RoadPoint> queue;
queue.push(start);
while (!queue.empty()) {
auto vertex = queue.front();
queue.pop();
if (been[vertex]) {
continue;
}
been[vertex] = true;
for (auto to : *(*adjMatrix)[vertex]) {
if (dist[to] > dist[vertex] + 1) {
queue.push(to);
dist[to] = dist[vertex] + 1;
prev[to] = vertex;
}
}
}
if (dist[end] == FLOAT_MAX_VALUE) {
return nullptr;
}
QVector<RoadPoint> *path = new QVector<RoadPoint>;
RoadPoint curr = end;
while (curr != RoadPoint()) {
path->push_back(curr);
curr = prev[curr];
}
std::reverse(path->begin(), path->end());
return path;
}