Skip to content

Commit f826739

Browse files
committed
Merge branch 'master' of github.com:Stunkymonkey/osm-dijkstra
2 parents b7d7ed4 + c329a0f commit f826739

File tree

2 files changed

+190
-159
lines changed

2 files changed

+190
-159
lines changed

web/src/graph.rs

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ struct State {
2424
node: usize,
2525
cost: usize,
2626
}
27+
2728
// Manually implement Ord so we get a min-heap instead of a max-heap
2829
impl Ord for State {
2930
fn cmp(&self, other: &Self) -> Ordering {
3031
other.cost.cmp(&self.cost)
3132
}
3233
}
34+
3335
impl PartialOrd for State {
3436
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
3537
Some(self.cmp(other))
@@ -53,10 +55,10 @@ impl Graph {
5355

5456
/// returns closes point of given long & lat
5557
pub fn get_point_id(&self, lat: f32, long: f32, travel_type: usize) -> usize {
56-
// TODO check if travel_type can be used
5758
let mut min_distance: f32 = std::f32::MAX;
5859
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);
6062
for node_id in adjacent_nodes {
6163
match self.nodes.get(node_id) {
6264
Some(node) => {
@@ -66,12 +68,21 @@ impl Graph {
6668
min_distance_id = node_id;
6769
}
6870
}
69-
None => continue,
71+
None => continue
7072
}
7173
}
7274
return min_distance_id;
7375
}
7476

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+
7586
/// converts node ids to node-coordinates
7687
pub fn get_coordinates(&self, path: Vec<usize>) -> Vec<Node> {
7788
return path.iter().map(|x| self.nodes[*x]).collect::<Vec<Node>>();
@@ -96,44 +107,49 @@ impl Graph {
96107
}
97108
}
98109

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+
99130
/// returns node_ids in adjacent grid cells
100131
/// 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> {
102133
let lat_grid = (lat * GRID_MULTIPLICATOR as f32) as i32;
103134
let lng_grid = (lng * GRID_MULTIPLICATOR as f32) as i32;
104135
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);
109137
let mut in_dist: i32 = 1;
110138
loop {
111139
for i in -in_dist..in_dist {
112140
// 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);
117142
// 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);
122144
// 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);
127146
// 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);
132148
}
133149
if node_ids.len() > 0 {
134150
return node_ids;
135151
} else {
136-
// search in next level
152+
// search in next level cells
137153
in_dist += 1;
138154
}
139155
}

0 commit comments

Comments
 (0)