Skip to content

Commit

Permalink
Refactor Tds struct and add new methods for counting vertices and cells
Browse files Browse the repository at this point in the history
- Refactored the `Tds` struct in `triangulation_data_structure.rs` to include two new methods: `number_of_vertices()` and `number_of_cells()`.
- Updated the existing tests in the module to use these new methods instead of directly accessing the `vertices` and `cells` fields.
- Added assertions to verify that the counts returned by these methods are correct.

This commit improves code readability and encapsulation by providing dedicated methods for counting vertices and cells in the triangulation data structure.
  • Loading branch information
acgetchell committed Dec 18, 2023
1 parent 7645f34 commit e701d62
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
[![rust-clippy analyze](https://github.com/acgetchell/d-delaunay/actions/workflows/rust-clippy.yml/badge.svg)](https://github.com/acgetchell/d-delaunay/actions/workflows/rust-clippy.yml)
[![codecov](https://codecov.io/gh/acgetchell/d-delaunay/graph/badge.svg?token=WT7qZGT9bO)](https://codecov.io/gh/acgetchell/d-delaunay)

D-dimensional Delaunay triangulations in Rust, inspired by [CGAL].
D-dimensional Delaunay triangulations in [Rust], inspired by [CGAL].

## Introduction

This library implements d-dimensional Delaunay triangulations in [Rust]. It is inspired by the [CGAL] library, which is a C++ library for computational geometry; and [Spade], a Rust library implementing 2D Delaunay triangulations, Constrained Delaunay triangulations, and Voronoi diagrams. The eventual goal of this library is to provide a lightweight [Rust] alternative to [CGAL].
This library implements d-dimensional Delaunay triangulations in [Rust]. It is inspired by the [CGAL] library, which is a C++ library for computational geometry; and [Spade], a Rust library
implementing 2D Delaunay triangulations, Constrained Delaunay triangulations, and Voronoi diagrams. The eventual goal of this library is to provide a lightweight alternative to [CGAL] for the [Rust]
ecosystem.

At some point I may merge it into another library, such as [Spade], or [delaunay], but for now I am developing this library without trying to figure out how to fit into other coding styles and standards.
At some point I may merge it into another library, such as [Spade], or [delaunay], but for now I am developing this library without trying to figure out how to fit it into other coding styles and standards.

[Rust]: https://rust-lang.org
[CGAL]: https://www.cgal.org/
Expand Down
30 changes: 18 additions & 12 deletions src/delaunay_core/triangulation_data_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ impl<T, U, V, const D: usize> Tds<T, U, V, D> {
}

let result = self.vertices.insert(vertex.uuid, vertex);

// Hashmap::insert returns the old value if the key already exists and updates it with the new value
match result {
Some(_) => Err("Uuid already exists"),
None => Ok(()),
Expand All @@ -39,10 +41,14 @@ impl<T, U, V, const D: usize> Tds<T, U, V, D> {
}

pub fn dim(&self) -> i32 {
let len = self.vertices.len() as i32;
let len = self.number_of_vertices() as i32;

min(len - 1, D as i32)
}

pub fn number_of_cells(&self) -> usize {
self.cells.len()
}
}

pub fn start() -> i32 {
Expand All @@ -66,8 +72,8 @@ mod tests {

let tds: Tds<f64, usize, usize, 3> = Tds::new(points);

assert_eq!(tds.vertices.len(), 4);
assert_eq!(tds.cells.len(), 0);
assert_eq!(tds.number_of_vertices(), 4);
assert_eq!(tds.number_of_cells(), 0);
assert_eq!(tds.dim(), 3);

// Human readable output for cargo test -- --nocapture
Expand All @@ -80,33 +86,33 @@ mod tests {

let mut tds: Tds<f64, usize, usize, 3> = Tds::new(points);

assert_eq!(tds.vertices.len(), 0);
assert_eq!(tds.cells.len(), 0);
assert_eq!(tds.number_of_vertices(), 0);
assert_eq!(tds.number_of_cells(), 0);
assert_eq!(tds.dim(), -1);

let new_vertex1: Vertex<f64, usize, 3> = Vertex::new(Point::new([1.0, 2.0, 3.0]));
let _ = tds.add(new_vertex1);
assert_eq!(tds.vertices.len(), 1);
assert_eq!(tds.number_of_vertices(), 1);
assert_eq!(tds.dim(), 0);

let new_vertex2: Vertex<f64, usize, 3> = Vertex::new(Point::new([4.0, 5.0, 6.0]));
let _ = tds.add(new_vertex2);
assert_eq!(tds.vertices.len(), 2);
assert_eq!(tds.number_of_vertices(), 2);
assert_eq!(tds.dim(), 1);

let new_vertex3: Vertex<f64, usize, 3> = Vertex::new(Point::new([7.0, 8.0, 9.0]));
let _ = tds.add(new_vertex3);
assert_eq!(tds.vertices.len(), 3);
assert_eq!(tds.number_of_vertices(), 3);
assert_eq!(tds.dim(), 2);

let new_vertex4: Vertex<f64, usize, 3> = Vertex::new(Point::new([10.0, 11.0, 12.0]));
let _ = tds.add(new_vertex4);
assert_eq!(tds.vertices.len(), 4);
assert_eq!(tds.number_of_vertices(), 4);
assert_eq!(tds.dim(), 3);

let new_vertex5: Vertex<f64, usize, 3> = Vertex::new(Point::new([13.0, 14.0, 15.0]));
let _ = tds.add(new_vertex5);
assert_eq!(tds.vertices.len(), 5);
assert_eq!(tds.number_of_vertices(), 5);
assert_eq!(tds.dim(), 3);
}

Expand All @@ -121,13 +127,13 @@ mod tests {

let mut tds: Tds<f64, usize, usize, 3> = Tds::new(points);

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

let new_vertex1: Vertex<f64, usize, 3> = Vertex::new(Point::new([1.0, 2.0, 3.0]));
let result = tds.add(new_vertex1);
assert_eq!(tds.vertices.len(), 4);
assert_eq!(tds.number_of_vertices(), 4);
assert_eq!(tds.dim(), 3);

assert!(result.is_err());
Expand Down

0 comments on commit e701d62

Please sign in to comment.