Skip to content

Commit 62359b5

Browse files
committed
3D -> d-D
Generalized to d-D, per the name.
1 parent 2559b06 commit 62359b5

File tree

5 files changed

+96
-80
lines changed

5 files changed

+96
-80
lines changed

src/delaunay_core/cell.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ use uuid::Uuid;
33
use super::{utilities::make_uuid, vertex::Vertex};
44

55
#[derive(Debug)]
6-
pub struct Cell<T, U, V> {
7-
pub vertices: Vec<Vertex<T, U>>,
6+
pub struct Cell<T, U, V, const D: usize> {
7+
pub vertices: Vec<Vertex<T, U, D>>,
88
pub uuid: Uuid,
99
pub neighbors: Option<Vec<Uuid>>,
1010
pub data: Option<V>,
1111
}
1212

13-
impl<T, U, V> Cell<T, U, V> {
14-
pub fn new_with_data(vertices: Vec<Vertex<T, U>>, data: V) -> Self {
13+
impl<T, U, V, const D: usize> Cell<T, U, V, D> {
14+
pub fn new_with_data(vertices: Vec<Vertex<T, U, D>>, data: V) -> Self {
1515
let uuid = make_uuid();
1616
let neighbors = None;
1717
let data = Some(data);
@@ -27,7 +27,7 @@ impl<T, U, V> Cell<T, U, V> {
2727
self.vertices.len()
2828
}
2929

30-
pub fn new(vertices: Vec<Vertex<T, U>>) -> Self {
30+
pub fn new(vertices: Vec<Vertex<T, U, D>>) -> Self {
3131
let uuid = make_uuid();
3232
let neighbors = None;
3333
let data = None;
@@ -38,6 +38,10 @@ impl<T, U, V> Cell<T, U, V> {
3838
data,
3939
}
4040
}
41+
42+
pub fn dim(&self) -> usize {
43+
D
44+
}
4145
}
4246

4347
#[cfg(test)]
@@ -49,13 +53,14 @@ mod tests {
4953

5054
#[test]
5155
fn make_cell_with_data() {
52-
let vertex1 = Vertex::new_with_data(Point::new(1.0, 2.0, 3.0), 3);
56+
let vertex1 = Vertex::new_with_data(Point::new([1.0, 2.0, 3.0]), "3D");
5357
let cell = Cell::new_with_data(vec![vertex1], 10);
5458

55-
assert_eq!(cell.vertices[0].point.x, 1.0);
56-
assert_eq!(cell.vertices[0].point.y, 2.0);
57-
assert_eq!(cell.vertices[0].point.z, 3.0);
58-
assert_eq!(cell.vertices[0].data, Some(3));
59+
assert_eq!(cell.vertices[0].point.coords[0], 1.0);
60+
assert_eq!(cell.vertices[0].point.coords[1], 2.0);
61+
assert_eq!(cell.vertices[0].point.coords[2], 3.0);
62+
assert_eq!(cell.vertices[0].data.unwrap(), "3D");
63+
assert_eq!(cell.dim(), 3);
5964
assert_eq!(cell.number_of_vertices(), 1);
6065
assert!(cell.neighbors.is_none());
6166
assert!(cell.data.is_some());
@@ -67,13 +72,14 @@ mod tests {
6772

6873
#[test]
6974
fn make_cell_without_data() {
70-
let vertex1 = Vertex::new_with_data(Point::new(1.0, 2.0, 3.0), 3);
71-
let cell: Cell<f64, i32, Option<()>> = Cell::new(vec![vertex1]);
75+
let vertex1 = Vertex::new_with_data(Point::new([1.0, 2.0, 3.0]), "3D");
76+
let cell: Cell<f64, &str, Option<()>, 3> = Cell::new(vec![vertex1]);
7277

73-
assert_eq!(cell.vertices[0].point.x, 1.0);
74-
assert_eq!(cell.vertices[0].point.y, 2.0);
75-
assert_eq!(cell.vertices[0].point.z, 3.0);
76-
assert_eq!(cell.vertices[0].data, Some(3));
78+
assert_eq!(cell.vertices[0].point.coords[0], 1.0);
79+
assert_eq!(cell.vertices[0].point.coords[1], 2.0);
80+
assert_eq!(cell.vertices[0].point.coords[2], 3.0);
81+
assert_eq!(cell.vertices[0].data.unwrap(), "3D");
82+
assert_eq!(cell.dim(), 3);
7783
assert_eq!(cell.number_of_vertices(), 1);
7884
assert!(cell.neighbors.is_none());
7985
assert!(cell.data.is_none());

src/delaunay_core/point.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#[derive(Debug, PartialEq, Clone)]
2-
pub struct Point<T> {
3-
pub x: T,
4-
pub y: T,
5-
pub z: T,
2+
pub struct Point<T, const D: usize> {
3+
pub coords: [T; D],
64
}
75

8-
impl<T> Point<T> {
9-
pub fn new(x: T, y: T, z: T) -> Self {
10-
Self { x, y, z }
6+
impl<T: Clone, const D: usize> Point<T, D> {
7+
pub fn new(coords: [T; D]) -> Self {
8+
Self { coords }
9+
}
10+
11+
pub fn dim(&self) -> usize {
12+
D
1113
}
1214
}
1315

@@ -18,13 +20,13 @@ mod tests {
1820

1921
#[test]
2022
fn make_point() {
21-
let point = Point::new(1.0, 2.0, 3.0);
22-
23-
assert_eq!(point.x, 1.0);
24-
assert_eq!(point.y, 2.0);
25-
assert_eq!(point.z, 3.0);
23+
let point = Point::new([1.0, 2.0, 3.0, 4.0]);
24+
assert_eq!(point.coords[0], 1.0);
25+
assert_eq!(point.coords[1], 2.0);
26+
assert_eq!(point.coords[2], 3.0);
27+
assert_eq!(point.dim(), 4);
2628

2729
// Human readable output for cargo test -- --nocapture
28-
println!("Point: {:?}", point);
30+
println!("Point: {:?} is {}-D", point, point.dim());
2931
}
3032
}

src/delaunay_core/triangulation_data_structure.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,39 @@ use std::collections::HashMap;
33
use uuid::Uuid;
44

55
#[derive(Debug)]
6-
pub struct Tds<T, U, V> {
7-
pub vertices: HashMap<Uuid, Vertex<U, V>>,
8-
pub cells: HashMap<Uuid, Cell<T, U, V>>,
6+
pub struct Tds<T, U, V, const D: usize> {
7+
pub vertices: HashMap<Uuid, Vertex<T, U, D>>,
8+
pub cells: HashMap<Uuid, Cell<T, U, V, D>>,
99
}
1010

11-
impl<T, U, V> Tds<T, U, V> {
12-
pub fn new(points: Vec<Point<U>>) -> Self {
11+
impl<T, U, V, const D: usize> Tds<T, U, V, D> {
12+
pub fn new(points: Vec<Point<T, D>>) -> Self {
1313
let vertices = Vertex::into_hashmap(Vertex::from_points(points));
1414
let cells = HashMap::new();
1515
Self { vertices, cells }
1616
}
1717
}
1818

19-
pub fn hello() -> i32 {
20-
println!("Hello, world!");
19+
pub fn start() -> i32 {
20+
println!("Starting ...");
2121
1
2222
}
2323

2424
#[cfg(test)]
2525
mod tests {
2626

27-
use crate::delaunay_core::triangulation_data_structure;
28-
2927
use super::*;
3028

3129
#[test]
3230
fn make_tds() {
3331
let points = vec![
34-
Point::new(1.0, 2.0, 3.0),
35-
Point::new(4.0, 5.0, 6.0),
36-
Point::new(7.0, 8.0, 9.0),
37-
Point::new(10.0, 11.0, 12.0),
32+
Point::new([1.0, 2.0, 3.0]),
33+
Point::new([4.0, 5.0, 6.0]),
34+
Point::new([7.0, 8.0, 9.0]),
35+
Point::new([10.0, 11.0, 12.0]),
3836
];
39-
let tds: triangulation_data_structure::Tds<usize, f64, usize> = Tds::new(points);
37+
38+
let tds: Tds<f64, usize, usize, 3> = Tds::new(points);
4039

4140
assert_eq!(tds.vertices.len(), 4);
4241
assert_eq!(tds.cells.len(), 0);

src/delaunay_core/vertex.rs

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ use std::option::Option;
55
use super::{point::Point, utilities::make_uuid};
66

77
#[derive(Debug, PartialEq, Clone)]
8-
pub struct Vertex<T, U> {
9-
pub point: Point<T>,
8+
pub struct Vertex<T, U, const D: usize> {
9+
pub point: Point<T, D>,
1010
pub uuid: Uuid,
1111
pub incident_cell: Option<Uuid>,
1212
pub data: Option<U>,
1313
}
1414

15-
impl<T, U> Vertex<T, U> {
16-
pub fn new_with_data(point: Point<T>, data: U) -> Self {
15+
impl<T, U, const D: usize> Vertex<T, U, D> {
16+
pub fn new_with_data(point: Point<T, D>, data: U) -> Self {
1717
let uuid = make_uuid();
1818
let incident_cell = None;
1919
let data = Some(data);
@@ -25,7 +25,7 @@ impl<T, U> Vertex<T, U> {
2525
}
2626
}
2727

28-
pub fn new(point: Point<T>) -> Self {
28+
pub fn new(point: Point<T, D>) -> Self {
2929
let uuid = make_uuid();
3030
let incident_cell = None;
3131
let data = None;
@@ -37,13 +37,17 @@ impl<T, U> Vertex<T, U> {
3737
}
3838
}
3939

40-
pub fn from_points(points: Vec<Point<T>>) -> Vec<Self> {
40+
pub fn from_points(points: Vec<Point<T, D>>) -> Vec<Self> {
4141
points.into_iter().map(|p| Self::new(p)).collect()
4242
}
4343

4444
pub fn into_hashmap(vertices: Vec<Self>) -> std::collections::HashMap<Uuid, Self> {
4545
vertices.into_iter().map(|v| (v.uuid, v)).collect()
4646
}
47+
48+
pub fn dim(&self) -> usize {
49+
D
50+
}
4751
}
4852
#[cfg(test)]
4953
mod tests {
@@ -52,64 +56,69 @@ mod tests {
5256

5357
#[test]
5458
fn make_vertex_with_data() {
55-
let vertex = Vertex::new_with_data(Point::new(1.0, 2.0, 3.0), 3);
59+
let vertex = Vertex::new_with_data(Point::new([1.0, 2.0, 3.0, 4.0]), "4D");
5660
println!("{:?}", vertex);
57-
assert_eq!(vertex.point.x, 1.0);
58-
assert_eq!(vertex.point.y, 2.0);
59-
assert_eq!(vertex.point.z, 3.0);
61+
assert_eq!(vertex.point.coords[0], 1.0);
62+
assert_eq!(vertex.point.coords[1], 2.0);
63+
assert_eq!(vertex.point.coords[2], 3.0);
64+
assert_eq!(vertex.point.coords[3], 4.0);
65+
assert_eq!(vertex.point.dim(), 4);
6066
assert!(vertex.incident_cell.is_none());
6167
assert!(vertex.data.is_some());
62-
assert_eq!(vertex.data.unwrap(), 3);
68+
assert_eq!(vertex.data.unwrap(), "4D");
6369
}
6470

6571
#[test]
6672
fn make_vertex_without_data() {
67-
let vertex: Vertex<f64, Option<()>> = Vertex::new(Point::new(1.0, 2.0, 3.0));
73+
let vertex: Vertex<f64, Option<()>, 3> = Vertex::new(Point::new([1.0, 2.0, 3.0]));
6874
println!("{:?}", vertex);
69-
assert_eq!(vertex.point.x, 1.0);
70-
assert_eq!(vertex.point.y, 2.0);
71-
assert_eq!(vertex.point.z, 3.0);
75+
assert_eq!(vertex.point.coords[0], 1.0);
76+
assert_eq!(vertex.point.coords[1], 2.0);
77+
assert_eq!(vertex.point.coords[2], 3.0);
7278
assert!(vertex.incident_cell.is_none());
7379
assert!(vertex.data.is_none());
7480
}
7581

7682
#[test]
7783
fn make_vertices_from_points() {
7884
let points = vec![
79-
Point::new(1.0, 2.0, 3.0),
80-
Point::new(4.0, 5.0, 6.0),
81-
Point::new(7.0, 8.0, 9.0),
85+
Point::new([1.0, 2.0, 3.0]),
86+
Point::new([4.0, 5.0, 6.0]),
87+
Point::new([7.0, 8.0, 9.0]),
8288
];
83-
let vertices: Vec<Vertex<f64, Option<()>>> = Vertex::from_points(points);
84-
println!("{:?}", vertices);
89+
let vertices: Vec<Vertex<f64, Option<()>, 3>> = Vertex::from_points(points);
90+
8591
assert_eq!(vertices.len(), 3);
86-
assert_eq!(vertices[0].point.x, 1.0);
87-
assert_eq!(vertices[0].point.y, 2.0);
88-
assert_eq!(vertices[0].point.z, 3.0);
89-
assert_eq!(vertices[1].point.x, 4.0);
90-
assert_eq!(vertices[1].point.y, 5.0);
91-
assert_eq!(vertices[1].point.z, 6.0);
92-
assert_eq!(vertices[2].point.x, 7.0);
93-
assert_eq!(vertices[2].point.y, 8.0);
94-
assert_eq!(vertices[2].point.z, 9.0);
92+
assert_eq!(vertices[0].point.coords[0], 1.0);
93+
assert_eq!(vertices[0].point.coords[1], 2.0);
94+
assert_eq!(vertices[0].point.coords[2], 3.0);
95+
assert_eq!(vertices[1].point.coords[0], 4.0);
96+
assert_eq!(vertices[1].point.coords[1], 5.0);
97+
assert_eq!(vertices[1].point.coords[2], 6.0);
98+
assert_eq!(vertices[2].point.coords[0], 7.0);
99+
assert_eq!(vertices[2].point.coords[1], 8.0);
100+
assert_eq!(vertices[2].point.coords[2], 9.0);
101+
102+
// Human readable output for cargo test -- --nocapture
103+
println!("{:?}", vertices);
95104
}
96105

97106
#[test]
98107
fn make_hashmap_from_vec() {
99108
let points = vec![
100-
Point::new(1.0, 2.0, 3.0),
101-
Point::new(4.0, 5.0, 6.0),
102-
Point::new(7.0, 8.0, 9.0),
109+
Point::new([1.0, 2.0, 3.0]),
110+
Point::new([4.0, 5.0, 6.0]),
111+
Point::new([7.0, 8.0, 9.0]),
103112
];
104-
let mut vertices: Vec<Vertex<f64, Option<()>>> = Vertex::from_points(points);
113+
let mut vertices: Vec<Vertex<f64, Option<()>, 3>> = Vertex::from_points(points);
105114
let hashmap = Vertex::into_hashmap(vertices.clone());
106-
println!("{:?}", hashmap);
115+
107116
assert_eq!(hashmap.len(), 3);
108117
for (key, val) in hashmap.iter() {
109118
assert_eq!(*key, val.uuid);
110119
}
111120

112-
let mut values: Vec<Vertex<f64, Option<()>>> = hashmap.into_values().collect();
121+
let mut values: Vec<Vertex<f64, Option<()>, 3>> = hashmap.into_values().collect();
113122
assert_eq!(values.len(), 3);
114123

115124
values.sort_by(|a, b| a.uuid.cmp(&b.uuid));

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ mod delaunay_core {
88

99
#[cfg(test)]
1010
mod tests {
11-
use crate::delaunay_core::triangulation_data_structure::hello;
11+
use crate::delaunay_core::triangulation_data_structure::start;
1212

1313
#[test]
1414
fn it_works() {
15-
let result = hello();
15+
let result = start();
1616
assert_eq!(result, 1);
1717
}
1818
}

0 commit comments

Comments
 (0)