-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute_model.cpp
93 lines (71 loc) · 2.27 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "route_model.h"
#include <iostream>
RouteModel::RouteModel(const std::vector<std::byte> &xml) : Model(xml) {
int counter = 0;
for(Model::Node n:this->Nodes()){
this->m_Nodes.push_back(RouteModel::Node(counter, this, n ));
counter++;
}
CreateNodeToRoadHashmap();
}
void RouteModel::CreateNodeToRoadHashmap(){
for(const Model::Road &road : Roads()){
if(road.type != Model::Road::Type::Footway){
for(const auto node : Ways()[road.way].nodes){
if(node_to_road.find(node) == node_to_road.end()){
node_to_road[node] = std::vector<const Model::Road*> {};
}
node_to_road[node].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);
}
//if(new_neighbor){//is not null
//this->neighbors.emplace_back(new_neighbor);
//}
}
}
auto &RouteModel::FindClosestNode(float x, float y){
Node input;
input.x = x;
input.y = y;
float min_dist = std::numeric_limits<float>::max();
int closest_idx;
float dist;
for(auto &road : Roads()){
if(road.type != Model::Road::Type::Footway) {
for(const auto node_idx : Ways()[road.way].nodes){
dist = input.distance(SNodes()[node_idx]);
if(dist<min_dist){
closest_idx = node_idx;
min_dist = dist;
}
/*if(node_to_road.find(node) == node_to_road.end()){
node_to_road[node] = std::vector<const Model::Road*> {};
}
node_to_road[node].push_back(&road);*/
}
///end
}
}
return SNodes()[closest_idx];
}