Skip to content

Commit

Permalink
Refactor dim method to calculate the dimension based on the number …
Browse files Browse the repository at this point in the history
…of vertices. Update tests accordingly.

- Calculate the dimension based on the number of vertices in `dim` method
- Update tests for `make_cell_with_data`, `make_cell_without_data`, and `make_vertex_with_data`
- Add assertions for vertex dimensions in updated tests
  • Loading branch information
acgetchell committed Dec 17, 2023
1 parent 62359b5 commit 0a080de
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
47 changes: 31 additions & 16 deletions src/delaunay_core/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl<T, U, V, const D: usize> Cell<T, U, V, D> {
}

pub fn dim(&self) -> usize {
D
self.number_of_vertices() - 1
}
}

Expand All @@ -53,34 +53,49 @@ mod tests {

#[test]
fn make_cell_with_data() {
let vertex1 = Vertex::new_with_data(Point::new([1.0, 2.0, 3.0]), "3D");
let cell = Cell::new_with_data(vec![vertex1], 10);
let vertex1 = Vertex::new_with_data(Point::new([0.0, 0.0, 1.0]), 1);
let vertex2 = Vertex::new_with_data(Point::new([0.0, 1.0, 0.0]), 1);
let vertex3 = Vertex::new_with_data(Point::new([1.0, 0.0, 0.0]), 1);
let vertex4 = Vertex::new_with_data(Point::new([1.0, 1.0, 1.0]), 2);
let cell = Cell::new_with_data(vec![vertex1, vertex2, vertex3, vertex4], "three-one cell");

assert_eq!(cell.vertices[0].point.coords[0], 1.0);
assert_eq!(cell.vertices[0].point.coords[1], 2.0);
assert_eq!(cell.vertices[0].point.coords[2], 3.0);
assert_eq!(cell.vertices[0].data.unwrap(), "3D");
assert_eq!(cell.vertices[0], vertex1);
assert_eq!(cell.vertices[1], vertex2);
assert_eq!(cell.vertices[2], vertex3);
assert_eq!(cell.vertices[3], vertex4);
assert_eq!(cell.vertices[0].data.unwrap(), 1);
assert_eq!(cell.vertices[1].data.unwrap(), 1);
assert_eq!(cell.vertices[2].data.unwrap(), 1);
assert_eq!(cell.vertices[3].data.unwrap(), 2);
assert_eq!(cell.dim(), 3);
assert_eq!(cell.number_of_vertices(), 1);
assert_eq!(cell.number_of_vertices(), 4);
assert!(cell.neighbors.is_none());
assert!(cell.data.is_some());
assert_eq!(cell.data.unwrap(), 10);
assert_eq!(cell.data.unwrap(), "three-one cell");

// 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]), "3D");
let cell: Cell<f64, &str, Option<()>, 3> = Cell::new(vec![vertex1]);
let vertex1 = Vertex::new_with_data(Point::new([0.0, 0.0, 1.0]), 1);
let vertex2 = Vertex::new_with_data(Point::new([0.0, 1.0, 0.0]), 1);
let vertex3 = Vertex::new_with_data(Point::new([1.0, 0.0, 0.0]), 1);
let vertex4 = Vertex::new_with_data(Point::new([1.0, 1.0, 1.0]), 2);
let cell: Cell<f64, i32, Option<()>, 3> =
Cell::new(vec![vertex1, vertex2, vertex3, vertex4]);

assert_eq!(cell.vertices[0].point.coords[0], 1.0);
assert_eq!(cell.vertices[0].point.coords[1], 2.0);
assert_eq!(cell.vertices[0].point.coords[2], 3.0);
assert_eq!(cell.vertices[0].data.unwrap(), "3D");
assert_eq!(cell.vertices[0], vertex1);
assert_eq!(cell.vertices[1], vertex2);
assert_eq!(cell.vertices[2], vertex3);
assert_eq!(cell.vertices[3], vertex4);
assert_eq!(cell.vertices[0].data.unwrap(), 1);
assert_eq!(cell.vertices[1].data.unwrap(), 1);
assert_eq!(cell.vertices[2].data.unwrap(), 1);
assert_eq!(cell.vertices[3].data.unwrap(), 2);
assert_eq!(cell.dim(), 3);
assert_eq!(cell.number_of_vertices(), 1);
assert_eq!(cell.number_of_vertices(), 4);
assert!(cell.neighbors.is_none());
assert!(cell.data.is_none());

Expand Down
2 changes: 1 addition & 1 deletion src/delaunay_core/point.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Point<T, const D: usize> {
pub coords: [T; D],
}
Expand Down
8 changes: 6 additions & 2 deletions 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, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Vertex<T, U, const D: usize> {
pub point: Point<T, D>,
pub uuid: Uuid,
Expand Down Expand Up @@ -62,7 +62,7 @@ mod tests {
assert_eq!(vertex.point.coords[1], 2.0);
assert_eq!(vertex.point.coords[2], 3.0);
assert_eq!(vertex.point.coords[3], 4.0);
assert_eq!(vertex.point.dim(), 4);
assert_eq!(vertex.dim(), 4);
assert!(vertex.incident_cell.is_none());
assert!(vertex.data.is_some());
assert_eq!(vertex.data.unwrap(), "4D");
Expand All @@ -75,6 +75,7 @@ mod tests {
assert_eq!(vertex.point.coords[0], 1.0);
assert_eq!(vertex.point.coords[1], 2.0);
assert_eq!(vertex.point.coords[2], 3.0);
assert_eq!(vertex.dim(), 3);
assert!(vertex.incident_cell.is_none());
assert!(vertex.data.is_none());
}
Expand All @@ -92,12 +93,15 @@ mod tests {
assert_eq!(vertices[0].point.coords[0], 1.0);
assert_eq!(vertices[0].point.coords[1], 2.0);
assert_eq!(vertices[0].point.coords[2], 3.0);
assert_eq!(vertices[0].dim(), 3);
assert_eq!(vertices[1].point.coords[0], 4.0);
assert_eq!(vertices[1].point.coords[1], 5.0);
assert_eq!(vertices[1].point.coords[2], 6.0);
assert_eq!(vertices[1].dim(), 3);
assert_eq!(vertices[2].point.coords[0], 7.0);
assert_eq!(vertices[2].point.coords[1], 8.0);
assert_eq!(vertices[2].point.coords[2], 9.0);
assert_eq!(vertices[2].dim(), 3);

// Human readable output for cargo test -- --nocapture
println!("{:?}", vertices);
Expand Down

0 comments on commit 0a080de

Please sign in to comment.