moved the route reconstruction into the Route struct

This commit is contained in:
Johannes Erwerle 2022-09-15 12:44:54 +02:00
parent 32a81b3561
commit 367fc0ef3e
2 changed files with 40 additions and 56 deletions

View file

@ -107,6 +107,34 @@ impl LookupGrid {
}
impl Route {
/// Constructs a route from the start to the end node on the graph
/// based on the ancestor and distance lists of a routing algorithm.
pub fn construct(graph: &GridGraph, ancestors: &Vec<Option<u32>>, distance: &Vec<EdgeCost>, start: &GraphNode, end: &GraphNode) -> Option<Self>{
if ancestors[end.index as usize].is_some() {
let mut route = Route {
cost: distance[end.index as usize],
nodes: Vec::new(),
};
let mut current = end.index;
while current != start.index {
route
.nodes
.push(graph.nodes[current as usize].position);
current = ancestors[current as usize].unwrap();
}
route
.nodes
.push(graph.nodes[current as usize].position);
route.nodes.reverse();
return Some(route);
}
None
}
pub fn to_geojson(&self) -> Box<FeatureCollection> {
let mut features: Box<FeatureCollection> = Box::new(FeatureCollection {
bbox: None,
@ -205,10 +233,7 @@ impl GridGraph {
//println!("working on node {} with cost {}", index, cost);
let edge_start = self.edge_offsets[index as usize] as usize;
let edge_end = self.edge_offsets[(index + 1) as usize] as usize;
for edge in self.edges[edge_start..edge_end].iter() {
for edge in self.get_edges(index as usize).iter() {
let new_cost = cost + edge.cost;
if new_cost < distance[edge.neighbor as usize] {
@ -228,27 +253,7 @@ impl GridGraph {
println!("popped {} elements from the heap", popcount);
// now the route calculation is done. If a route exist we can construct
// it from the ancestors.
if ancestor[end.index as usize].is_some() {
let mut route = Route {
cost: distance[end.index as usize],
nodes: Vec::new(),
};
let mut current = end.index;
while current != start.index {
route.nodes.push(self.nodes[current as usize].position);
current = ancestor[current as usize].unwrap();
}
route.nodes.push(self.nodes[current as usize].position);
route.nodes.reverse();
return Some(route);
}
None
Route::construct(self, &ancestor, &distance, start, end)
}
/// returns the GraphNode nearest to that positon.