diff --git a/src/bin/gen_landmarks_greedy.rs b/src/bin/gen_landmarks_greedy.rs index c832d66..9352115 100644 --- a/src/bin/gen_landmarks_greedy.rs +++ b/src/bin/gen_landmarks_greedy.rs @@ -1,7 +1,8 @@ use bincode; use clap::Parser; -use fapra_osm_2::alt::{LandmarkSet, Landmark}; -use fapra_osm_2::gridgraph::{GridGraph, EdgeCost}; +use fapra_osm_2::alt::{Landmark, LandmarkSet}; +use fapra_osm_2::gridgraph::EdgeCost; +use fapra_osm_2::utils::load_graph; use std::fs::File; use std::io::prelude::*; use std::process::exit; @@ -11,7 +12,7 @@ use std::process::exit; struct Args { /// the FMI file to load #[clap(short, long)] - input: String, + graph: String, /// the file to which to write the landmarks #[clap(short, long)] @@ -25,16 +26,7 @@ struct Args { fn main() { let args = Args::parse(); - let file = match File::open(args.input.clone()) { - Ok(f) => f, - Err(e) => { - println!("Error while opening file: {}", e); - exit(1); - } - }; - - let graph = GridGraph::from_fmi_file(file).unwrap(); - println!("finished loading graph from file"); + let graph = load_graph(&args.graph); let mut output = match File::create(args.output.clone()) { Ok(f) => f, @@ -45,7 +37,6 @@ fn main() { }; fn compute_next_index(costs: &mut Vec, landmark: &Landmark) -> usize { - let mut max_index = 0; let mut max_cost = 0; @@ -67,7 +58,6 @@ fn main() { println!("selected initial node: {:?}", initial_node); - let mut landmark = Landmark::generate(*initial_node, &graph); let mut costs: Vec = landmark.distances.clone(); @@ -80,7 +70,10 @@ fn main() { while set.landmarks.len() < args.amount { let node = graph.nodes[next]; - println!("the next node will be {:?} with a distance of {}", node, costs[node.index as usize]); + println!( + "the next node will be {:?} with a distance of {}", + node, costs[node.index as usize] + ); landmark = Landmark::generate(node, &graph); next = compute_next_index(&mut costs, &landmark); @@ -89,5 +82,7 @@ fn main() { let encoded = bincode::serialize(&set).unwrap(); - output.write_all(&encoded).expect("Error while writing LandmarkSet data"); + output + .write_all(&encoded) + .expect("Error while writing LandmarkSet data"); } diff --git a/src/bin/gen_landmarks_random.rs b/src/bin/gen_landmarks_random.rs index 30951fe..438fec7 100644 --- a/src/bin/gen_landmarks_random.rs +++ b/src/bin/gen_landmarks_random.rs @@ -1,7 +1,7 @@ use bincode; use clap::Parser; use fapra_osm_2::alt::LandmarkSet; -use fapra_osm_2::gridgraph::GridGraph; +use fapra_osm_2::utils::load_graph; use std::fs::File; use std::io::prelude::*; use std::process::exit; @@ -11,7 +11,7 @@ use std::process::exit; struct Args { /// the FMI file to load #[clap(short, long)] - input: String, + graph: String, /// the file to which to write the landmarks #[clap(short, long)] @@ -25,16 +25,7 @@ struct Args { fn main() { let args = Args::parse(); - let file = match File::open(args.input.clone()) { - Ok(f) => f, - Err(e) => { - println!("Error while opening file: {}", e); - exit(1); - } - }; - - let grid = GridGraph::from_fmi_file(file).unwrap(); - println!("finished loading grid from file"); + let graph = load_graph(&args.graph); let mut output = match File::create(args.output.clone()) { Ok(f) => f, @@ -44,7 +35,7 @@ fn main() { } }; - let set = LandmarkSet::random_set(args.amount, &grid); + let set = LandmarkSet::random_set(args.amount, &graph); let encoded = bincode::serialize(&set).unwrap(); diff --git a/src/bin/generate_benchmark_targets.rs b/src/bin/generate_benchmark_targets.rs index d2e58b6..92e566f 100644 --- a/src/bin/generate_benchmark_targets.rs +++ b/src/bin/generate_benchmark_targets.rs @@ -1,5 +1,4 @@ -use fapra_osm_2::gridgraph::GridGraph; -use std::fs::File; +use fapra_osm_2::utils::load_graph; use clap::Parser; use std::process::exit; use rand::seq::SliceRandom; @@ -21,21 +20,7 @@ struct Args { fn main() { let args = Args::parse(); - let file = match File::open(args.graph.clone()) { - Ok(f) => f, - Err(e) => { - println!("Error while opening the file {}: {}", args.graph, 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); - } - }; + let graph = load_graph(&args.graph); let mut rng = rand::thread_rng(); diff --git a/src/bin/grid_to_geojson.rs b/src/bin/grid_to_geojson.rs index e6fc2f6..ea78b91 100644 --- a/src/bin/grid_to_geojson.rs +++ b/src/bin/grid_to_geojson.rs @@ -1,28 +1,18 @@ -use fapra_osm_2::gridgraph::GridGraph; +use fapra_osm_2::utils::load_graph; use clap::Parser; -use std::fs::File; -use std::process::exit; #[derive(Parser, Debug)] #[clap(author, version, about, long_about=None)] struct Args { /// the FMI file to load #[clap(short, long)] - input: String, + graph: String, } fn main() { let args = Args::parse(); - let file = match File::open(args.input.clone()) { - Ok(f) => f, - Err(e) => { - println!("Error while opening file: {}", e); - exit(1); - } - }; + let graph = load_graph(&args.graph); - let grid = GridGraph::from_fmi_file(file).unwrap(); - - println!("{}", grid.to_geojson().to_string()); + println!("{}", graph.to_geojson().to_string()); } diff --git a/src/bin/performance.rs b/src/bin/performance.rs index f629d42..a284fa7 100644 --- a/src/bin/performance.rs +++ b/src/bin/performance.rs @@ -1,8 +1,8 @@ use clap::Parser; -use fapra_osm_2::alt::{LandmarkBestSet, LandmarkSet}; +use fapra_osm_2::alt::LandmarkBestSet; use fapra_osm_2::astar::{estimate_haversine, AStar}; -use fapra_osm_2::gridgraph::{GridGraph, NodeId}; -use fapra_osm_2::utils::RoutingQuery; +use fapra_osm_2::gridgraph::NodeId; +use fapra_osm_2::utils::{load_graph, load_landmarks, RoutingQuery}; use serde_json; use std::fs::File; use std::io::BufReader; @@ -40,21 +40,7 @@ struct Args { fn main() { let args = Args::parse(); - let file = match File::open(args.graph.clone()) { - Ok(f) => f, - Err(e) => { - println!("Error while opening the file {}: {}", args.graph, 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); - } - }; + let graph = load_graph(&args.graph); let astar = AStar { graph: &(*graph) }; @@ -69,18 +55,7 @@ fn main() { let targets: Vec = serde_json::from_reader(BufReader::new(targets)).unwrap(); if let Some(landmark_path) = args.landmarks { - let landmarks = match File::open(landmark_path.clone()) { - Ok(f) => f, - Err(e) => { - println!( - "Error while opening landmark file {}: {:?}", - landmark_path, e - ); - exit(1); - } - }; - - let landmarks: LandmarkSet = bincode::deserialize_from(BufReader::new(landmarks)).unwrap(); + let landmarks = load_landmarks(&landmark_path); println!("running ALT"); diff --git a/src/bin/task6-rocket.rs b/src/bin/task6-rocket.rs index 5460806..fc10302 100644 --- a/src/bin/task6-rocket.rs +++ b/src/bin/task6-rocket.rs @@ -6,11 +6,9 @@ use fapra_osm_2::alt::{LandmarkBestSet, LandmarkSet}; use fapra_osm_2::astar::{estimate_haversine, AStar}; use fapra_osm_2::coordinates::{DegreeCoordinate, RadianCoordinate}; use fapra_osm_2::gridgraph::{GridGraph, NodeId, Route}; +use fapra_osm_2::utils::{load_graph, load_landmarks}; use rand::seq::SliceRandom; use serde::Serialize; -use std::fs::File; -use std::io::BufReader; -use std::process::exit; use std::time::Instant; use rocket::form::Form; @@ -25,7 +23,7 @@ use rocket_dyn_templates::{context, Engines, Template}; struct Args { /// name of the FMI file to read #[clap(short, long)] - filename: String, + graph: String, /// the landmarks to load #[clap(short, long)] @@ -141,37 +139,9 @@ struct GraphWrapper { fn rocket() -> _ { let args = Args::parse(); - println!("Loading file from {}", args.filename); - let file = match File::open(args.filename.clone()) { - Ok(f) => f, - Err(e) => { - println!("Error while opening the file {}: {}", args.filename, e); - exit(1) - } - }; + let graph = load_graph(&args.graph); - 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"); - - let landmarks = match File::open(args.landmarks.clone()) { - Ok(f) => f, - Err(e) => { - println!( - "Error while opening landmark file {}: {:?}", - args.landmarks, e - ); - exit(1); - } - }; - - let landmarks: LandmarkSet = bincode::deserialize_from(BufReader::new(landmarks)).unwrap(); + let landmarks = load_landmarks(&args.landmarks); rocket::build() .manage(GraphWrapper { diff --git a/src/utils.rs b/src/utils.rs index 7cc84e4..3d09969 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,9 +1,49 @@ +use crate::alt::LandmarkSet; +use crate::gridgraph::GridGraph; use serde::{Deserialize, Serialize}; +use std::fs::File; +use std::io::BufReader; +use std::process::exit; pub const EARTH_RADIUS: f64 = 6_371_000.0; // meters #[derive(Serialize, Deserialize, Debug, Copy, Clone)] -pub struct RoutingQuery{ +pub struct RoutingQuery { pub source: usize, pub destination: usize, } + +pub fn load_graph(path: &str) -> Box { + 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 +} + +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() +} diff --git a/utils/generate_landmarks.py b/utils/generate_landmarks.py index 60fd000..88356b7 100755 --- a/utils/generate_landmarks.py +++ b/utils/generate_landmarks.py @@ -13,5 +13,5 @@ output = argv[2] for i in range(2, 7): num = 2**i - system(f"cargo run --release --bin=gen_landmarks_greedy -- --input={graph} --output={output}_greedy_{ num }.bin --amount { num }") - system(f"cargo run --release --bin=gen_landmarks_random -- --input={graph} --output={output}_random_{ num }.bin --amount { num }") + system(f"cargo run --release --bin=gen_landmarks_greedy -- --graph={graph} --output={output}_greedy_{ num }.bin --amount { num }") + system(f"cargo run --release --bin=gen_landmarks_random -- --graph={graph} --output={output}_random_{ num }.bin --amount { num }")