From 2559b06d0e98dd212329e2f38949baaa955b4026 Mon Sep 17 00:00:00 2001 From: Adam Getchell Date: Sat, 16 Dec 2023 13:33:42 -0800 Subject: [PATCH] Construct hashmap of vertices in tds --- cspell.json | 1 + src/delaunay_core/cell.rs | 10 +++- src/delaunay_core/point.rs | 7 ++- .../triangulation_data_structure.rs | 20 +++++-- src/delaunay_core/utilities.rs | 7 ++- src/delaunay_core/vertex.rs | 58 ++++++++++++++++++- 6 files changed, 90 insertions(+), 13 deletions(-) diff --git a/cspell.json b/cspell.json index 790fc3e..75284e0 100644 --- a/cspell.json +++ b/cspell.json @@ -8,6 +8,7 @@ "CGAL", "clippy", "codecov", + "nocapture", "Voronoi" ], "ignoreWords": [], diff --git a/src/delaunay_core/cell.rs b/src/delaunay_core/cell.rs index 8ee19ce..06c96cd 100644 --- a/src/delaunay_core/cell.rs +++ b/src/delaunay_core/cell.rs @@ -51,7 +51,7 @@ mod tests { fn make_cell_with_data() { let vertex1 = Vertex::new_with_data(Point::new(1.0, 2.0, 3.0), 3); let cell = Cell::new_with_data(vec![vertex1], 10); - println!("{:?}", cell); + assert_eq!(cell.vertices[0].point.x, 1.0); assert_eq!(cell.vertices[0].point.y, 2.0); assert_eq!(cell.vertices[0].point.z, 3.0); @@ -60,13 +60,16 @@ mod tests { assert!(cell.neighbors.is_none()); assert!(cell.data.is_some()); assert_eq!(cell.data.unwrap(), 10); + + // Human readable output for cargo test -- --nocapture + println!("Cell: {:?}", cell); } #[test] fn make_cell_without_data() { let vertex1 = Vertex::new_with_data(Point::new(1.0, 2.0, 3.0), 3); let cell: Cell> = Cell::new(vec![vertex1]); - println!("{:?}", cell); + assert_eq!(cell.vertices[0].point.x, 1.0); assert_eq!(cell.vertices[0].point.y, 2.0); assert_eq!(cell.vertices[0].point.z, 3.0); @@ -74,5 +77,8 @@ mod tests { assert_eq!(cell.number_of_vertices(), 1); assert!(cell.neighbors.is_none()); assert!(cell.data.is_none()); + + // Human readable output for cargo test -- --nocapture + println!("Cell: {:?}", cell); } } diff --git a/src/delaunay_core/point.rs b/src/delaunay_core/point.rs index 36c2692..295a387 100644 --- a/src/delaunay_core/point.rs +++ b/src/delaunay_core/point.rs @@ -1,4 +1,4 @@ -#[derive(Debug)] +#[derive(Debug, PartialEq, Clone)] pub struct Point { pub x: T, pub y: T, @@ -19,9 +19,12 @@ mod tests { #[test] fn make_point() { let point = Point::new(1.0, 2.0, 3.0); - println!("{:?}", point); + assert_eq!(point.x, 1.0); assert_eq!(point.y, 2.0); assert_eq!(point.z, 3.0); + + // Human readable output for cargo test -- --nocapture + println!("Point: {:?}", point); } } diff --git a/src/delaunay_core/triangulation_data_structure.rs b/src/delaunay_core/triangulation_data_structure.rs index f62587c..8572cec 100644 --- a/src/delaunay_core/triangulation_data_structure.rs +++ b/src/delaunay_core/triangulation_data_structure.rs @@ -9,8 +9,8 @@ pub struct Tds { } impl Tds { - pub fn new(_points: Vec>) -> Self { - let vertices = HashMap::new(); + pub fn new(points: Vec>) -> Self { + let vertices = Vertex::into_hashmap(Vertex::from_points(points)); let cells = HashMap::new(); Self { vertices, cells } } @@ -30,10 +30,18 @@ mod tests { #[test] fn make_tds() { - let tds: triangulation_data_structure::Tds = - Tds::new(vec![Point::new(1.0, 2.0, 3.0)]); - println!("{:?}", tds); - assert_eq!(tds.vertices.len(), 0); + let points = vec![ + Point::new(1.0, 2.0, 3.0), + Point::new(4.0, 5.0, 6.0), + Point::new(7.0, 8.0, 9.0), + Point::new(10.0, 11.0, 12.0), + ]; + let tds: triangulation_data_structure::Tds = Tds::new(points); + + assert_eq!(tds.vertices.len(), 4); assert_eq!(tds.cells.len(), 0); + + // Human readable output for cargo test -- --nocapture + println!("{:?}", tds); } } diff --git a/src/delaunay_core/utilities.rs b/src/delaunay_core/utilities.rs index d404591..f67b903 100644 --- a/src/delaunay_core/utilities.rs +++ b/src/delaunay_core/utilities.rs @@ -12,9 +12,12 @@ mod tests { #[test] fn test_uuid() { let uuid = make_uuid(); - println!("make_uuid = {:?}", uuid); - println!("uuid version: {:?}\n", uuid.get_version_num()); + assert_eq!(uuid.get_version_num(), 4); assert_ne!(uuid, make_uuid()); + + // Human readable output for cargo test -- --nocapture + println!("make_uuid = {:?}", uuid); + println!("uuid version: {:?}\n", uuid.get_version_num()); } } diff --git a/src/delaunay_core/vertex.rs b/src/delaunay_core/vertex.rs index c1f67b6..837b1db 100644 --- a/src/delaunay_core/vertex.rs +++ b/src/delaunay_core/vertex.rs @@ -4,7 +4,7 @@ use std::option::Option; use super::{point::Point, utilities::make_uuid}; -#[derive(Debug)] +#[derive(Debug, PartialEq, Clone)] pub struct Vertex { pub point: Point, pub uuid: Uuid, @@ -36,6 +36,14 @@ impl Vertex { data, } } + + pub fn from_points(points: Vec>) -> Vec { + points.into_iter().map(|p| Self::new(p)).collect() + } + + pub fn into_hashmap(vertices: Vec) -> std::collections::HashMap { + vertices.into_iter().map(|v| (v.uuid, v)).collect() + } } #[cfg(test)] mod tests { @@ -64,4 +72,52 @@ mod tests { assert!(vertex.incident_cell.is_none()); assert!(vertex.data.is_none()); } + + #[test] + fn make_vertices_from_points() { + let points = vec![ + Point::new(1.0, 2.0, 3.0), + Point::new(4.0, 5.0, 6.0), + Point::new(7.0, 8.0, 9.0), + ]; + let vertices: Vec>> = Vertex::from_points(points); + println!("{:?}", vertices); + assert_eq!(vertices.len(), 3); + assert_eq!(vertices[0].point.x, 1.0); + assert_eq!(vertices[0].point.y, 2.0); + assert_eq!(vertices[0].point.z, 3.0); + assert_eq!(vertices[1].point.x, 4.0); + assert_eq!(vertices[1].point.y, 5.0); + assert_eq!(vertices[1].point.z, 6.0); + assert_eq!(vertices[2].point.x, 7.0); + assert_eq!(vertices[2].point.y, 8.0); + assert_eq!(vertices[2].point.z, 9.0); + } + + #[test] + fn make_hashmap_from_vec() { + let points = vec![ + Point::new(1.0, 2.0, 3.0), + Point::new(4.0, 5.0, 6.0), + Point::new(7.0, 8.0, 9.0), + ]; + let mut vertices: Vec>> = Vertex::from_points(points); + let hashmap = Vertex::into_hashmap(vertices.clone()); + println!("{:?}", hashmap); + assert_eq!(hashmap.len(), 3); + for (key, val) in hashmap.iter() { + assert_eq!(*key, val.uuid); + } + + let mut values: Vec>> = hashmap.into_values().collect(); + assert_eq!(values.len(), 3); + + values.sort_by(|a, b| a.uuid.cmp(&b.uuid)); + vertices.sort_by(|a, b| a.uuid.cmp(&b.uuid)); + assert_eq!(values, vertices); + + // Human readable output for cargo test -- --nocapture + println!("values = {:?}", values); + println!("vertices = {:?}", vertices); + } }