Skip to content

Commit 2559b06

Browse files
committed
Construct hashmap of vertices in tds
1 parent 7e8ae59 commit 2559b06

File tree

6 files changed

+90
-13
lines changed

6 files changed

+90
-13
lines changed

cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"CGAL",
99
"clippy",
1010
"codecov",
11+
"nocapture",
1112
"Voronoi"
1213
],
1314
"ignoreWords": [],

src/delaunay_core/cell.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ mod tests {
5151
fn make_cell_with_data() {
5252
let vertex1 = Vertex::new_with_data(Point::new(1.0, 2.0, 3.0), 3);
5353
let cell = Cell::new_with_data(vec![vertex1], 10);
54-
println!("{:?}", cell);
54+
5555
assert_eq!(cell.vertices[0].point.x, 1.0);
5656
assert_eq!(cell.vertices[0].point.y, 2.0);
5757
assert_eq!(cell.vertices[0].point.z, 3.0);
@@ -60,19 +60,25 @@ mod tests {
6060
assert!(cell.neighbors.is_none());
6161
assert!(cell.data.is_some());
6262
assert_eq!(cell.data.unwrap(), 10);
63+
64+
// Human readable output for cargo test -- --nocapture
65+
println!("Cell: {:?}", cell);
6366
}
6467

6568
#[test]
6669
fn make_cell_without_data() {
6770
let vertex1 = Vertex::new_with_data(Point::new(1.0, 2.0, 3.0), 3);
6871
let cell: Cell<f64, i32, Option<()>> = Cell::new(vec![vertex1]);
69-
println!("{:?}", cell);
72+
7073
assert_eq!(cell.vertices[0].point.x, 1.0);
7174
assert_eq!(cell.vertices[0].point.y, 2.0);
7275
assert_eq!(cell.vertices[0].point.z, 3.0);
7376
assert_eq!(cell.vertices[0].data, Some(3));
7477
assert_eq!(cell.number_of_vertices(), 1);
7578
assert!(cell.neighbors.is_none());
7679
assert!(cell.data.is_none());
80+
81+
// Human readable output for cargo test -- --nocapture
82+
println!("Cell: {:?}", cell);
7783
}
7884
}

src/delaunay_core/point.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[derive(Debug)]
1+
#[derive(Debug, PartialEq, Clone)]
22
pub struct Point<T> {
33
pub x: T,
44
pub y: T,
@@ -19,9 +19,12 @@ mod tests {
1919
#[test]
2020
fn make_point() {
2121
let point = Point::new(1.0, 2.0, 3.0);
22-
println!("{:?}", point);
22+
2323
assert_eq!(point.x, 1.0);
2424
assert_eq!(point.y, 2.0);
2525
assert_eq!(point.z, 3.0);
26+
27+
// Human readable output for cargo test -- --nocapture
28+
println!("Point: {:?}", point);
2629
}
2730
}

src/delaunay_core/triangulation_data_structure.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ pub struct Tds<T, U, V> {
99
}
1010

1111
impl<T, U, V> Tds<T, U, V> {
12-
pub fn new(_points: Vec<Point<U>>) -> Self {
13-
let vertices = HashMap::new();
12+
pub fn new(points: Vec<Point<U>>) -> Self {
13+
let vertices = Vertex::into_hashmap(Vertex::from_points(points));
1414
let cells = HashMap::new();
1515
Self { vertices, cells }
1616
}
@@ -30,10 +30,18 @@ mod tests {
3030

3131
#[test]
3232
fn make_tds() {
33-
let tds: triangulation_data_structure::Tds<usize, f64, usize> =
34-
Tds::new(vec![Point::new(1.0, 2.0, 3.0)]);
35-
println!("{:?}", tds);
36-
assert_eq!(tds.vertices.len(), 0);
33+
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),
38+
];
39+
let tds: triangulation_data_structure::Tds<usize, f64, usize> = Tds::new(points);
40+
41+
assert_eq!(tds.vertices.len(), 4);
3742
assert_eq!(tds.cells.len(), 0);
43+
44+
// Human readable output for cargo test -- --nocapture
45+
println!("{:?}", tds);
3846
}
3947
}

src/delaunay_core/utilities.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ mod tests {
1212
#[test]
1313
fn test_uuid() {
1414
let uuid = make_uuid();
15-
println!("make_uuid = {:?}", uuid);
16-
println!("uuid version: {:?}\n", uuid.get_version_num());
15+
1716
assert_eq!(uuid.get_version_num(), 4);
1817
assert_ne!(uuid, make_uuid());
18+
19+
// Human readable output for cargo test -- --nocapture
20+
println!("make_uuid = {:?}", uuid);
21+
println!("uuid version: {:?}\n", uuid.get_version_num());
1922
}
2023
}

src/delaunay_core/vertex.rs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::option::Option;
44

55
use super::{point::Point, utilities::make_uuid};
66

7-
#[derive(Debug)]
7+
#[derive(Debug, PartialEq, Clone)]
88
pub struct Vertex<T, U> {
99
pub point: Point<T>,
1010
pub uuid: Uuid,
@@ -36,6 +36,14 @@ impl<T, U> Vertex<T, U> {
3636
data,
3737
}
3838
}
39+
40+
pub fn from_points(points: Vec<Point<T>>) -> Vec<Self> {
41+
points.into_iter().map(|p| Self::new(p)).collect()
42+
}
43+
44+
pub fn into_hashmap(vertices: Vec<Self>) -> std::collections::HashMap<Uuid, Self> {
45+
vertices.into_iter().map(|v| (v.uuid, v)).collect()
46+
}
3947
}
4048
#[cfg(test)]
4149
mod tests {
@@ -64,4 +72,52 @@ mod tests {
6472
assert!(vertex.incident_cell.is_none());
6573
assert!(vertex.data.is_none());
6674
}
75+
76+
#[test]
77+
fn make_vertices_from_points() {
78+
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),
82+
];
83+
let vertices: Vec<Vertex<f64, Option<()>>> = Vertex::from_points(points);
84+
println!("{:?}", vertices);
85+
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);
95+
}
96+
97+
#[test]
98+
fn make_hashmap_from_vec() {
99+
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),
103+
];
104+
let mut vertices: Vec<Vertex<f64, Option<()>>> = Vertex::from_points(points);
105+
let hashmap = Vertex::into_hashmap(vertices.clone());
106+
println!("{:?}", hashmap);
107+
assert_eq!(hashmap.len(), 3);
108+
for (key, val) in hashmap.iter() {
109+
assert_eq!(*key, val.uuid);
110+
}
111+
112+
let mut values: Vec<Vertex<f64, Option<()>>> = hashmap.into_values().collect();
113+
assert_eq!(values.len(), 3);
114+
115+
values.sort_by(|a, b| a.uuid.cmp(&b.uuid));
116+
vertices.sort_by(|a, b| a.uuid.cmp(&b.uuid));
117+
assert_eq!(values, vertices);
118+
119+
// Human readable output for cargo test -- --nocapture
120+
println!("values = {:?}", values);
121+
println!("vertices = {:?}", vertices);
122+
}
67123
}

0 commit comments

Comments
 (0)