Skip to content

Commit

Permalink
Constrain cell dimensionality
Browse files Browse the repository at this point in the history
The intrinsic dimension of the cells cannot exceed the extrinsic dimension of the vertices.
  • Loading branch information
acgetchell committed Dec 17, 2023
1 parent 0a080de commit 693a1df
Showing 1 changed file with 55 additions and 14 deletions.
69 changes: 55 additions & 14 deletions src/delaunay_core/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,38 @@ pub struct Cell<T, U, V, const D: usize> {
}

impl<T, U, V, const D: usize> Cell<T, U, V, D> {
pub fn new_with_data(vertices: Vec<Vertex<T, U, D>>, data: V) -> Self {
pub fn new(vertices: Vec<Vertex<T, U, D>>) -> Result<Self, &'static str> {
if vertices.len() > D + 1 {
return Err("Number of vertices must be less than or equal to D + 1");
}
let uuid = make_uuid();
let neighbors = None;
let data = Some(data);
Cell {
let data = None;
Ok(Cell {
vertices,
uuid,
neighbors,
data,
}
})
}

pub fn number_of_vertices(&self) -> usize {
self.vertices.len()
}

pub fn new(vertices: Vec<Vertex<T, U, D>>) -> Self {
pub fn new_with_data(vertices: Vec<Vertex<T, U, D>>, data: V) -> Result<Self, &'static str> {
if vertices.len() > D + 1 {
return Err("Number of vertices must be less than or equal to D + 1");
}
let uuid = make_uuid();
let neighbors = None;
let data = None;
Cell {
let data = Some(data);
Ok(Cell {
vertices,
uuid,
neighbors,
data,
}
})
}

pub fn number_of_vertices(&self) -> usize {
self.vertices.len()
}

pub fn dim(&self) -> usize {
Expand All @@ -57,7 +63,8 @@ mod tests {
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");
let cell = Cell::new_with_data(vec![vertex1, vertex2, vertex3, vertex4], "three-one cell")
.unwrap();

assert_eq!(cell.vertices[0], vertex1);
assert_eq!(cell.vertices[1], vertex2);
Expand All @@ -77,14 +84,32 @@ mod tests {
println!("Cell: {:?}", cell);
}

#[test]
fn make_cell_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 cell = Cell::new_with_data(
vec![vertex1, vertex2, vertex3, vertex4, vertex5],
"three-one cell",
);

assert!(cell.is_err());

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

#[test]
fn make_cell_without_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 cell: Cell<f64, i32, Option<()>, 3> =
Cell::new(vec![vertex1, vertex2, vertex3, vertex4]);
Cell::new(vec![vertex1, vertex2, vertex3, vertex4]).unwrap();

assert_eq!(cell.vertices[0], vertex1);
assert_eq!(cell.vertices[1], vertex2);
Expand All @@ -102,4 +127,20 @@ mod tests {
// Human readable output for cargo test -- --nocapture
println!("Cell: {:?}", cell);
}

#[test]
fn make_cell_without_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 cell: Result<Cell<f64, i32, Option<()>, 3>, &'static str> =
Cell::new(vec![vertex1, vertex2, vertex3, vertex4, vertex5]);

assert!(cell.is_err());

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

0 comments on commit 693a1df

Please sign in to comment.