@@ -24,12 +24,14 @@ struct State {
24
24
node : usize ,
25
25
cost : usize ,
26
26
}
27
+
27
28
// Manually implement Ord so we get a min-heap instead of a max-heap
28
29
impl Ord for State {
29
30
fn cmp ( & self , other : & Self ) -> Ordering {
30
31
other. cost . cmp ( & self . cost )
31
32
}
32
33
}
34
+
33
35
impl PartialOrd for State {
34
36
fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
35
37
Some ( self . cmp ( other) )
@@ -53,10 +55,10 @@ impl Graph {
53
55
54
56
/// returns closes point of given long & lat
55
57
pub fn get_point_id ( & self , lat : f32 , long : f32 , travel_type : usize ) -> usize {
56
- // TODO check if travel_type can be used
57
58
let mut min_distance: f32 = std:: f32:: MAX ;
58
59
let mut min_distance_id: usize = 0 ;
59
- let adjacent_nodes = self . get_adjacent_node_ids ( lat, long) ;
60
+ let allowed_types = self . get_allowed_types ( travel_type) ;
61
+ let adjacent_nodes = self . get_adjacent_node_ids ( lat, long, & allowed_types) ;
60
62
for node_id in adjacent_nodes {
61
63
match self . nodes . get ( node_id) {
62
64
Some ( node) => {
@@ -66,12 +68,21 @@ impl Graph {
66
68
min_distance_id = node_id;
67
69
}
68
70
}
69
- None => continue ,
71
+ None => continue
70
72
}
71
73
}
72
74
return min_distance_id;
73
75
}
74
76
77
+ fn get_allowed_types ( & self , road_type : usize ) -> Vec < usize > {
78
+ return match road_type {
79
+ 0 => vec ! [ 0 , 1 , 5 ] ,
80
+ 1 => vec ! [ 1 , 2 , 3 , 5 ] ,
81
+ 2 => vec ! [ 4 , 5 ] ,
82
+ _ => vec ! [ 5 ]
83
+ } ;
84
+ }
85
+
75
86
/// converts node ids to node-coordinates
76
87
pub fn get_coordinates ( & self , path : Vec < usize > ) -> Vec < Node > {
77
88
return path. iter ( ) . map ( |x| self . nodes [ * x] ) . collect :: < Vec < Node > > ( ) ;
@@ -96,44 +107,49 @@ impl Graph {
96
107
}
97
108
}
98
109
110
+ fn is_valid_node_for_travel_type ( & self , node_id : usize , allowed_types : & Vec < usize > ) -> bool {
111
+ let incl_start = self . offset [ node_id] ;
112
+ let excl_end = self . offset [ node_id + 1 ] ;
113
+ for i in incl_start..excl_end {
114
+ let edge = & self . ways [ i] ;
115
+ if allowed_types. contains ( & edge. travel_type ) {
116
+ return true ;
117
+ }
118
+ }
119
+ return false ;
120
+ }
121
+
122
+ fn add_valid_node_ids_from_cell ( & self , node_ids : & mut Vec < usize > , cell : & ( usize , usize ) , allowed_types : & Vec < usize > ) {
123
+ match self . grid . get ( cell) {
124
+ Some ( adjacent_node_ids) => node_ids. extend ( adjacent_node_ids. iter ( ) . filter ( |& & x| self . is_valid_node_for_travel_type ( x, allowed_types) ) . collect :: < Vec < & usize > > ( ) ) ,
125
+ None => return
126
+ }
127
+ }
128
+
129
+
99
130
/// returns node_ids in adjacent grid cells
100
131
/// goes from most inner cell to cells with distance 1 to n until a node is found
101
- fn get_adjacent_node_ids ( & self , lat : f32 , lng : f32 ) -> Vec < usize > {
132
+ fn get_adjacent_node_ids ( & self , lat : f32 , lng : f32 , allowed_types : & Vec < usize > ) -> Vec < usize > {
102
133
let lat_grid = ( lat * GRID_MULTIPLICATOR as f32 ) as i32 ;
103
134
let lng_grid = ( lng * GRID_MULTIPLICATOR as f32 ) as i32 ;
104
135
let mut node_ids = Vec :: < usize > :: new ( ) ;
105
- match self . grid . get ( & ( lat_grid as usize , lng_grid as usize ) ) {
106
- Some ( adjacent_node_ids) => node_ids. extend ( adjacent_node_ids) ,
107
- None => ( ) ,
108
- }
136
+ self . add_valid_node_ids_from_cell ( & mut node_ids, & ( lat_grid as usize , lng_grid as usize ) , allowed_types) ;
109
137
let mut in_dist: i32 = 1 ;
110
138
loop {
111
139
for i in -in_dist..in_dist {
112
140
// top row left to right (increasing x, fix y)
113
- match self . grid . get ( & ( ( lat_grid+i) as usize , ( lng_grid+in_dist) as usize ) ) {
114
- Some ( adjacent_node_ids) => node_ids. extend ( adjacent_node_ids) ,
115
- None => continue ,
116
- }
141
+ self . add_valid_node_ids_from_cell ( & mut node_ids, & ( ( lat_grid + i) as usize , ( lng_grid + in_dist) as usize ) , allowed_types) ;
117
142
// right column top to bottom (fix x, decreasing y)
118
- match self . grid . get ( & ( ( lat_grid+in_dist) as usize , ( lng_grid-i) as usize ) ) {
119
- Some ( adjacent_node_ids) => node_ids. extend ( adjacent_node_ids) ,
120
- None => continue ,
121
- }
143
+ self . add_valid_node_ids_from_cell ( & mut node_ids, & ( ( lat_grid + in_dist) as usize , ( lng_grid - i) as usize ) , allowed_types) ;
122
144
// bottom row right to left (decreasing x, fix y)
123
- match self . grid . get ( & ( ( lat_grid-i) as usize , ( lng_grid-in_dist) as usize ) ) {
124
- Some ( adjacent_node_ids) => node_ids. extend ( adjacent_node_ids) ,
125
- None => continue ,
126
- }
145
+ self . add_valid_node_ids_from_cell ( & mut node_ids, & ( ( lat_grid - i) as usize , ( lng_grid - in_dist) as usize ) , allowed_types) ;
127
146
// left column bottom to top (fix x, increasing y)
128
- match self . grid . get ( & ( ( lat_grid-in_dist) as usize , ( lng_grid+i) as usize ) ) {
129
- Some ( adjacent_node_ids) => node_ids. extend ( adjacent_node_ids) ,
130
- None => continue ,
131
- }
147
+ self . add_valid_node_ids_from_cell ( & mut node_ids, & ( ( lat_grid - in_dist) as usize , ( lng_grid + i) as usize ) , allowed_types) ;
132
148
}
133
149
if node_ids. len ( ) > 0 {
134
150
return node_ids;
135
151
} else {
136
- // search in next level
152
+ // search in next level cells
137
153
in_dist += 1 ;
138
154
}
139
155
}
0 commit comments