From 19cb8ff319c30e77e569ff8e13420334f5482dbf Mon Sep 17 00:00:00 2001 From: Andrew Northall Date: Thu, 10 Aug 2023 12:48:37 +0100 Subject: [PATCH] Set the edge weights to be the distance between nodes --- Cargo.toml | 2 +- src/read.rs | 20 ++++++++------------ 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 49c9656..bfdae14 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ readme = "README.md" authors = ["Andrew Northall "] keywords = ["caving", "caves", "cave-survey", "survex", "bindings"] categories = ["api-bindings", "external-ffi-bindings", "science::geo"] -version = "0.1.6" +version = "0.1.7" edition = "2021" include = ["**/*.rs", "lib/**/*", "Cargo.toml", "README.md", "LICENCE"] diff --git a/src/read.rs b/src/read.rs index f314a75..6d2282c 100644 --- a/src/read.rs +++ b/src/read.rs @@ -187,11 +187,9 @@ pub fn load_from_path(path: PathBuf) -> Result> { connections.len() ); - // Survex file reading is complete. We now need to compile our list of connections between - // two points into a list of connections between two nodes in the graph. To do this, we will - // loop through the connections vector and find the index of the node in the graph which - // corresponds to each set of coordinates. We will then add the connection to the graph. - let mut node_connections = Vec::new(); + // Survex file reading is complete. We now need to iterate over the connections vector and + // add the connections to the graph by looking up the node index for each station and adding + // an edge between them with the distance between the two stations as the weight. for (p1, p2) in connections.iter() { let from_station_node_index = data .get_by_coords(p1) @@ -203,15 +201,13 @@ pub fn load_from_path(path: PathBuf) -> Result> { .unwrap_or_else(|| panic!("Could not find station with coordinates {:?}", p2)) .borrow() .index; - node_connections.push((from_station_node_index, to_station_node_index)); + data.graph.add_edge( + from_station_node_index, + to_station_node_index, + p1.distance(p2), + ); } - data.graph.extend_with_edges(&node_connections); - - trace!( - "Extended graph with {} connections.", - node_connections.len() - ); trace!( "Graph now has {} nodes and {} edges.", data.graph.node_count(),