-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute_model.cpp
77 lines (64 loc) · 2.2 KB
/
route_model.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
#include "route_model.h"
#include <iostream>
RouteModel::RouteModel(const std::vector<std::byte> &xml) : Model(xml) {
// Create RouteModel nodes.
int counter = 0;
for (Model::Node node : this->Nodes()) {
m_Nodes.emplace_back(Node(counter, this, node));
counter++;
}
CreateNodeToRoadHashmap();
}
void RouteModel::CreateNodeToRoadHashmap() {
for (const Model::Road &road : Roads()) {
if (road.type != Model::Road::Type::Footway) {
for (int node_idx : Ways()[road.way].nodes) {
if (node_to_road.find(node_idx) == node_to_road.end()) {
node_to_road[node_idx] = std::vector<const Model::Road *> ();
}
node_to_road[node_idx].push_back(&road);
}
}
}
}
RouteModel::Node *RouteModel::Node::FindNeighbor(std::vector<int> node_indices) {
Node *closest_node = nullptr;
Node node;
for (int node_index : node_indices) {
node = parent_model->SNodes()[node_index];
if (this->distance(node) != 0 && !node.visited) {
if (closest_node == nullptr || this->distance(node) < this->distance(*closest_node)) {
closest_node = &parent_model->SNodes()[node_index];
}
}
}
return closest_node;
}
void RouteModel::Node::FindNeighbors() {
for (auto & road : parent_model->node_to_road[this->index]) {
RouteModel::Node *new_neighbor = this->FindNeighbor(parent_model->Ways()[road->way].nodes);
if (new_neighbor) {
this->neighbors.emplace_back(new_neighbor);
}
}
}
RouteModel::Node &RouteModel::FindClosestNode(float x, float y) {
Node input;
input.x = x;
input.y = y;
float min_dist = std::numeric_limits<float>::max();
float dist;
int closest_idx;
for (const Model::Road &road : Roads()) {
if (road.type != Model::Road::Type::Footway) {
for (int node_idx : Ways()[road.way].nodes) {
dist = input.distance(SNodes()[node_idx]);
if (dist < min_dist) {
closest_idx = node_idx;
min_dist = dist;
}
}
}
}
return SNodes()[closest_idx];
}