Skip to content

Commit e701d62

Browse files
committed
Refactor Tds struct and add new methods for counting vertices and cells
- 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.
1 parent 7645f34 commit e701d62

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
[![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)
55
[![codecov](https://codecov.io/gh/acgetchell/d-delaunay/graph/badge.svg?token=WT7qZGT9bO)](https://codecov.io/gh/acgetchell/d-delaunay)
66

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

99
## Introduction
1010

11-
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].
11+
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
12+
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]
13+
ecosystem.
1214

13-
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.
15+
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.
1416

1517
[Rust]: https://rust-lang.org
1618
[CGAL]: https://www.cgal.org/

src/delaunay_core/triangulation_data_structure.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ impl<T, U, V, const D: usize> Tds<T, U, V, D> {
2828
}
2929

3030
let result = self.vertices.insert(vertex.uuid, vertex);
31+
32+
// Hashmap::insert returns the old value if the key already exists and updates it with the new value
3133
match result {
3234
Some(_) => Err("Uuid already exists"),
3335
None => Ok(()),
@@ -39,10 +41,14 @@ impl<T, U, V, const D: usize> Tds<T, U, V, D> {
3941
}
4042

4143
pub fn dim(&self) -> i32 {
42-
let len = self.vertices.len() as i32;
44+
let len = self.number_of_vertices() as i32;
4345

4446
min(len - 1, D as i32)
4547
}
48+
49+
pub fn number_of_cells(&self) -> usize {
50+
self.cells.len()
51+
}
4652
}
4753

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

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

69-
assert_eq!(tds.vertices.len(), 4);
70-
assert_eq!(tds.cells.len(), 0);
75+
assert_eq!(tds.number_of_vertices(), 4);
76+
assert_eq!(tds.number_of_cells(), 0);
7177
assert_eq!(tds.dim(), 3);
7278

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

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

83-
assert_eq!(tds.vertices.len(), 0);
84-
assert_eq!(tds.cells.len(), 0);
89+
assert_eq!(tds.number_of_vertices(), 0);
90+
assert_eq!(tds.number_of_cells(), 0);
8591
assert_eq!(tds.dim(), -1);
8692

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

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

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

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

107113
let new_vertex5: Vertex<f64, usize, 3> = Vertex::new(Point::new([13.0, 14.0, 15.0]));
108114
let _ = tds.add(new_vertex5);
109-
assert_eq!(tds.vertices.len(), 5);
115+
assert_eq!(tds.number_of_vertices(), 5);
110116
assert_eq!(tds.dim(), 3);
111117
}
112118

@@ -121,13 +127,13 @@ mod tests {
121127

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

124-
assert_eq!(tds.vertices.len(), 4);
130+
assert_eq!(tds.number_of_vertices(), 4);
125131
assert_eq!(tds.cells.len(), 0);
126132
assert_eq!(tds.dim(), 3);
127133

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

133139
assert!(result.is_err());

0 commit comments

Comments
 (0)