78 lines
2 KiB
Rust
78 lines
2 KiB
Rust
use crate::alt::LandmarkSet;
|
|
use crate::gridgraph::{EdgeCost, GridGraph};
|
|
use serde::{Deserialize, Serialize};
|
|
use std::cmp::Ordering;
|
|
use std::fs::File;
|
|
use std::io::BufReader;
|
|
use std::process::exit;
|
|
|
|
/// an approximation of the earths radius.
|
|
pub const EARTH_RADIUS: f64 = 6_371_000.0; // meters
|
|
|
|
/// serialization format for routing queries.
|
|
#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
|
|
pub struct RoutingQuery {
|
|
pub source: usize,
|
|
pub destination: usize,
|
|
}
|
|
|
|
/// loads the graph from the given path.
|
|
/// exits if an error occurs during loading.
|
|
pub fn load_graph(path: &str) -> Box<GridGraph> {
|
|
println!("Loading file from {}", path);
|
|
let file = match File::open(path) {
|
|
Ok(f) => f,
|
|
Err(e) => {
|
|
println!("Error while opening the file {}: {}", path, e);
|
|
exit(1)
|
|
}
|
|
};
|
|
|
|
let graph = match GridGraph::from_fmi_file(file) {
|
|
Ok(g) => g,
|
|
Err(e) => {
|
|
println!("Error while reading the graph: {:?}", e);
|
|
exit(1);
|
|
}
|
|
};
|
|
|
|
println!("Loaded graph file");
|
|
|
|
graph
|
|
}
|
|
|
|
/// loads a set of landmarks from the given path.
|
|
/// exits if an error occurs during loading.
|
|
pub fn load_landmarks(path: &str) -> LandmarkSet {
|
|
let landmarks = match File::open(path) {
|
|
Ok(f) => f,
|
|
Err(e) => {
|
|
println!("Error while opening landmark file {}: {:?}", path, e);
|
|
exit(1);
|
|
}
|
|
};
|
|
|
|
bincode::deserialize_from(BufReader::new(landmarks)).unwrap()
|
|
}
|
|
|
|
/// A heap element for Dijkstra's algorithm.
|
|
///
|
|
/// The comparison functions are inverted, so that Rusts MaxHeap works as a
|
|
/// MinHeap.
|
|
#[derive(Eq, PartialEq)]
|
|
pub struct DijkstraElement {
|
|
pub index: u32,
|
|
pub cost: EdgeCost,
|
|
}
|
|
|
|
impl Ord for DijkstraElement {
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
other.cost.cmp(&self.cost)
|
|
}
|
|
}
|
|
|
|
impl PartialOrd for DijkstraElement {
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
Some(self.cmp(other))
|
|
}
|
|
}
|