Skip to content

Commit

Permalink
feat: Use derive_builder
Browse files Browse the repository at this point in the history
  • Loading branch information
acgetchell committed Aug 25, 2024
1 parent e122aa9 commit 1d6adc8
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 43 deletions.
97 changes: 91 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
derive_builder = "0.20.0"
nalgebra = "0.33.0"
num-traits = "0.2.19"
serde = { version = "1.0.208", features = ["derive"] }
serde_json = "1.0.125"
serde = { version = "1.0.209", features = ["derive"] }
serde_json = "1.0.127"
serde_test = "1.0.177"
uuid = { version = "1.10.0", features = ["v4", "fast-rng", "macro-diagnostics", "serde"] }

Expand Down
110 changes: 91 additions & 19 deletions src/delaunay_core/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,16 +520,32 @@ where
#[cfg(test)]
mod tests {

use crate::delaunay_core::point::Point;
use crate::delaunay_core::{point::Point, vertex::VertexBuilder};

use super::*;

#[test]
fn cell_new() {
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 vertex1 = VertexBuilder::default()
.point(Point::new([0.0, 0.0, 1.0]))
.data(1)
.build()
.unwrap();
let vertex2 = VertexBuilder::default()
.point(Point::new([0.0, 1.0, 0.0]))
.data(1)
.build()
.unwrap();
let vertex3 = VertexBuilder::default()
.point(Point::new([1.0, 0.0, 0.0]))
.data(1)
.build()
.unwrap();
let vertex4 = VertexBuilder::default()
.point(Point::new([1.0, 1.0, 1.0]))
.data(2)
.build()
.unwrap();
let cell: Cell<f64, i32, Option<()>, 3> =
Cell::new(vec![vertex1, vertex2, vertex3, vertex4]).unwrap();

Expand All @@ -549,11 +565,31 @@ mod tests {

#[test]
fn cell_new_with_too_many_vertices() {
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 vertex5 = Vertex::new_with_data(Point::new([2.0, 2.0, 2.0]), 3);
let vertex1 = VertexBuilder::default()
.point(Point::new([0.0, 0.0, 1.0]))
.data(1)
.build()
.unwrap();
let vertex2 = VertexBuilder::default()
.point(Point::new([0.0, 1.0, 0.0]))
.data(1)
.build()
.unwrap();
let vertex3 = VertexBuilder::default()
.point(Point::new([1.0, 0.0, 0.0]))
.data(1)
.build()
.unwrap();
let vertex4 = VertexBuilder::default()
.point(Point::new([1.0, 1.0, 1.0]))
.data(2)
.build()
.unwrap();
let vertex5 = VertexBuilder::default()
.point(Point::new([2.0, 2.0, 2.0]))
.data(3)
.build()
.unwrap();
let cell: Result<Cell<f64, i32, Option<()>, 3>, &'static str> =
Cell::new(vec![vertex1, vertex2, vertex3, vertex4, vertex5]);

Expand All @@ -565,10 +601,26 @@ mod tests {

#[test]
fn cell_new_with_data() {
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 vertex1: Vertex<f64, i32, 3> = VertexBuilder::default()
.point(Point::new([0.0, 0.0, 1.0]))
.data(1)
.build()
.unwrap();
let vertex2 = VertexBuilder::default()
.point(Point::new([0.0, 1.0, 0.0]))
.data(1)
.build()
.unwrap();
let vertex3 = VertexBuilder::default()
.point(Point::new([1.0, 0.0, 0.0]))
.data(1)
.build()
.unwrap();
let vertex4 = VertexBuilder::default()
.point(Point::new([1.0, 1.0, 1.0]))
.data(2)
.build()
.unwrap();
let cell = Cell::new_with_data(vec![vertex1, vertex2, vertex3, vertex4], "three-one cell")
.unwrap();

Expand All @@ -589,11 +641,31 @@ mod tests {

#[test]
fn cell_new_with_data_with_too_many_vertices() {
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 vertex5 = Vertex::new_with_data(Point::new([2.0, 2.0, 2.0]), 3);
let vertex1: Vertex<f64, i32, 3> = VertexBuilder::default()
.point(Point::new([0.0, 0.0, 1.0]))
.data(1)
.build()
.unwrap();
let vertex2 = VertexBuilder::default()
.point(Point::new([0.0, 1.0, 0.0]))
.data(1)
.build()
.unwrap();
let vertex3 = VertexBuilder::default()
.point(Point::new([1.0, 0.0, 0.0]))
.data(1)
.build()
.unwrap();
let vertex4 = VertexBuilder::default()
.point(Point::new([1.0, 1.0, 1.0]))
.data(2)
.build()
.unwrap();
let vertex5 = VertexBuilder::default()
.point(Point::new([2.0, 2.0, 2.0]))
.data(3)
.build()
.unwrap();
let cell = Cell::new_with_data(
vec![vertex1, vertex2, vertex3, vertex4, vertex5],
"three-one cell",
Expand Down
21 changes: 11 additions & 10 deletions src/delaunay_core/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ where
T: Clone + Copy + Default + Into<f64> + PartialEq + PartialOrd,
[T; D]: Copy + Default + DeserializeOwned + Serialize + Sized,
[f64; D]: Default + DeserializeOwned + Serialize + Sized,
f64: From<T>,
{
fn from(coords: [T; D]) -> Self {
// Convert the `coords` array to `[f64; D]`
Expand Down Expand Up @@ -106,6 +107,16 @@ mod tests {

use super::*;

#[test]
fn point_default() {
let point: Point<f64, 4> = Default::default();

assert_eq!(point.coords, [0.0, 0.0, 0.0, 0.0]);

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

#[test]
fn point_new() {
let point = Point::new([1.0, 2.0, 3.0, 4.0]);
Expand Down Expand Up @@ -144,16 +155,6 @@ mod tests {
println!("Origin: {:?} is {}-D", point, point.dim());
}

#[test]
fn point_default_trait() {
let point: Point<f64, 4> = Default::default();

assert_eq!(point.coords, [0.0, 0.0, 0.0, 0.0]);

// Human readable output for cargo test -- --nocapture
println!("Default: {:?} is {}-D", point, point.dim());
}

#[test]
fn point_serialization() {
use serde_test::{assert_tokens, Token};
Expand Down
9 changes: 4 additions & 5 deletions src/delaunay_core/triangulation_data_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,10 @@ where
// handle case where vertices are constructed with data
let vertices = Vertex::into_hashmap(Vertex::from_points(points));
// let cells_vec = bowyer_watson(vertices);
let cells = HashMap::new();
// assign_neighbors(cells_vec);
// assign_incident_cells(vertices);

// Put cells_vec into hashmap
let cells = HashMap::new();
Self { vertices, cells }
}

Expand Down Expand Up @@ -280,14 +279,14 @@ where
}
}

// Remove bad cells from triangulation
triangulation.retain(|cell| !bad_cells.contains(cell));

// Create new cells from the boundary facets and new vertex
for facet in boundary_facets {
let new_cell = Cell::from_facet_and_vertex(facet, *vertex)?;
triangulation.push(new_cell);
}

// Remove bad cells from triangulation
triangulation.retain(|cell| !bad_cells.contains(cell));
}

// // Find the boundary of the polygonal hole
Expand Down
Loading

0 comments on commit 1d6adc8

Please sign in to comment.