Skip to content

Commit

Permalink
Construct hashmap of vertices in tds
Browse files Browse the repository at this point in the history
  • Loading branch information
acgetchell committed Dec 16, 2023
1 parent 7e8ae59 commit 2559b06
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 13 deletions.
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"CGAL",
"clippy",
"codecov",
"nocapture",
"Voronoi"
],
"ignoreWords": [],
Expand Down
10 changes: 8 additions & 2 deletions src/delaunay_core/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -60,19 +60,25 @@ 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<f64, i32, Option<()>> = 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);
assert_eq!(cell.vertices[0].data, Some(3));
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);
}
}
7 changes: 5 additions & 2 deletions src/delaunay_core/point.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Debug)]
#[derive(Debug, PartialEq, Clone)]
pub struct Point<T> {
pub x: T,
pub y: T,
Expand All @@ -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);
}
}
20 changes: 14 additions & 6 deletions src/delaunay_core/triangulation_data_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub struct Tds<T, U, V> {
}

impl<T, U, V> Tds<T, U, V> {
pub fn new(_points: Vec<Point<U>>) -> Self {
let vertices = HashMap::new();
pub fn new(points: Vec<Point<U>>) -> Self {
let vertices = Vertex::into_hashmap(Vertex::from_points(points));
let cells = HashMap::new();
Self { vertices, cells }
}
Expand All @@ -30,10 +30,18 @@ mod tests {

#[test]
fn make_tds() {
let tds: triangulation_data_structure::Tds<usize, f64, usize> =
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<usize, f64, usize> = Tds::new(points);

assert_eq!(tds.vertices.len(), 4);
assert_eq!(tds.cells.len(), 0);

// Human readable output for cargo test -- --nocapture
println!("{:?}", tds);
}
}
7 changes: 5 additions & 2 deletions src/delaunay_core/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
58 changes: 57 additions & 1 deletion src/delaunay_core/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::option::Option;

use super::{point::Point, utilities::make_uuid};

#[derive(Debug)]
#[derive(Debug, PartialEq, Clone)]
pub struct Vertex<T, U> {
pub point: Point<T>,
pub uuid: Uuid,
Expand Down Expand Up @@ -36,6 +36,14 @@ impl<T, U> Vertex<T, U> {
data,
}
}

pub fn from_points(points: Vec<Point<T>>) -> Vec<Self> {
points.into_iter().map(|p| Self::new(p)).collect()
}

pub fn into_hashmap(vertices: Vec<Self>) -> std::collections::HashMap<Uuid, Self> {
vertices.into_iter().map(|v| (v.uuid, v)).collect()
}
}
#[cfg(test)]
mod tests {
Expand Down Expand Up @@ -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<f64, Option<()>>> = 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<f64, Option<()>>> = 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<Vertex<f64, Option<()>>> = 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);
}
}

0 comments on commit 2559b06

Please sign in to comment.