Skip to content

Commit

Permalink
3D -> d-D
Browse files Browse the repository at this point in the history
Generalized to d-D, per the name.
  • Loading branch information
acgetchell committed Dec 17, 2023
1 parent 2559b06 commit 62359b5
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 80 deletions.
38 changes: 22 additions & 16 deletions src/delaunay_core/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use uuid::Uuid;
use super::{utilities::make_uuid, vertex::Vertex};

#[derive(Debug)]
pub struct Cell<T, U, V> {
pub vertices: Vec<Vertex<T, U>>,
pub struct Cell<T, U, V, const D: usize> {
pub vertices: Vec<Vertex<T, U, D>>,
pub uuid: Uuid,
pub neighbors: Option<Vec<Uuid>>,
pub data: Option<V>,
}

impl<T, U, V> Cell<T, U, V> {
pub fn new_with_data(vertices: Vec<Vertex<T, U>>, data: V) -> Self {
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 {
let uuid = make_uuid();
let neighbors = None;
let data = Some(data);
Expand All @@ -27,7 +27,7 @@ impl<T, U, V> Cell<T, U, V> {
self.vertices.len()
}

pub fn new(vertices: Vec<Vertex<T, U>>) -> Self {
pub fn new(vertices: Vec<Vertex<T, U, D>>) -> Self {
let uuid = make_uuid();
let neighbors = None;
let data = None;
Expand All @@ -38,6 +38,10 @@ impl<T, U, V> Cell<T, U, V> {
data,
}
}

pub fn dim(&self) -> usize {
D
}
}

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

#[test]
fn make_cell_with_data() {
let vertex1 = Vertex::new_with_data(Point::new(1.0, 2.0, 3.0), 3);
let vertex1 = Vertex::new_with_data(Point::new([1.0, 2.0, 3.0]), "3D");
let cell = Cell::new_with_data(vec![vertex1], 10);

assert_eq!(cell.vertices[0].point.x, 1.0);
assert_eq!(cell.vertices[0].point.y, 2.0);
assert_eq!(cell.vertices[0].point.z, 3.0);
assert_eq!(cell.vertices[0].data, Some(3));
assert_eq!(cell.vertices[0].point.coords[0], 1.0);
assert_eq!(cell.vertices[0].point.coords[1], 2.0);
assert_eq!(cell.vertices[0].point.coords[2], 3.0);
assert_eq!(cell.vertices[0].data.unwrap(), "3D");
assert_eq!(cell.dim(), 3);
assert_eq!(cell.number_of_vertices(), 1);
assert!(cell.neighbors.is_none());
assert!(cell.data.is_some());
Expand All @@ -67,13 +72,14 @@ mod tests {

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

assert_eq!(cell.vertices[0].point.x, 1.0);
assert_eq!(cell.vertices[0].point.y, 2.0);
assert_eq!(cell.vertices[0].point.z, 3.0);
assert_eq!(cell.vertices[0].data, Some(3));
assert_eq!(cell.vertices[0].point.coords[0], 1.0);
assert_eq!(cell.vertices[0].point.coords[1], 2.0);
assert_eq!(cell.vertices[0].point.coords[2], 3.0);
assert_eq!(cell.vertices[0].data.unwrap(), "3D");
assert_eq!(cell.dim(), 3);
assert_eq!(cell.number_of_vertices(), 1);
assert!(cell.neighbors.is_none());
assert!(cell.data.is_none());
Expand Down
28 changes: 15 additions & 13 deletions src/delaunay_core/point.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#[derive(Debug, PartialEq, Clone)]
pub struct Point<T> {
pub x: T,
pub y: T,
pub z: T,
pub struct Point<T, const D: usize> {
pub coords: [T; D],
}

impl<T> Point<T> {
pub fn new(x: T, y: T, z: T) -> Self {
Self { x, y, z }
impl<T: Clone, const D: usize> Point<T, D> {
pub fn new(coords: [T; D]) -> Self {
Self { coords }
}

pub fn dim(&self) -> usize {
D
}
}

Expand All @@ -18,13 +20,13 @@ mod tests {

#[test]
fn make_point() {
let point = Point::new(1.0, 2.0, 3.0);

assert_eq!(point.x, 1.0);
assert_eq!(point.y, 2.0);
assert_eq!(point.z, 3.0);
let point = Point::new([1.0, 2.0, 3.0, 4.0]);
assert_eq!(point.coords[0], 1.0);
assert_eq!(point.coords[1], 2.0);
assert_eq!(point.coords[2], 3.0);
assert_eq!(point.dim(), 4);

// Human readable output for cargo test -- --nocapture
println!("Point: {:?}", point);
println!("Point: {:?} is {}-D", point, point.dim());
}
}
27 changes: 13 additions & 14 deletions src/delaunay_core/triangulation_data_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,39 @@ use std::collections::HashMap;
use uuid::Uuid;

#[derive(Debug)]
pub struct Tds<T, U, V> {
pub vertices: HashMap<Uuid, Vertex<U, V>>,
pub cells: HashMap<Uuid, Cell<T, U, V>>,
pub struct Tds<T, U, V, const D: usize> {
pub vertices: HashMap<Uuid, Vertex<T, U, D>>,
pub cells: HashMap<Uuid, Cell<T, U, V, D>>,
}

impl<T, U, V> Tds<T, U, V> {
pub fn new(points: Vec<Point<U>>) -> Self {
impl<T, U, V, const D: usize> Tds<T, U, V, D> {
pub fn new(points: Vec<Point<T, D>>) -> Self {
let vertices = Vertex::into_hashmap(Vertex::from_points(points));
let cells = HashMap::new();
Self { vertices, cells }
}
}

pub fn hello() -> i32 {
println!("Hello, world!");
pub fn start() -> i32 {
println!("Starting ...");
1
}

#[cfg(test)]
mod tests {

use crate::delaunay_core::triangulation_data_structure;

use super::*;

#[test]
fn make_tds() {
let points = vec![
Point::new(1.0, 2.0, 3.0),
Point::new(4.0, 5.0, 6.0),
Point::new(7.0, 8.0, 9.0),
Point::new(10.0, 11.0, 12.0),
Point::new([1.0, 2.0, 3.0]),
Point::new([4.0, 5.0, 6.0]),
Point::new([7.0, 8.0, 9.0]),
Point::new([10.0, 11.0, 12.0]),
];
let tds: triangulation_data_structure::Tds<usize, f64, usize> = Tds::new(points);

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

assert_eq!(tds.vertices.len(), 4);
assert_eq!(tds.cells.len(), 0);
Expand Down
79 changes: 44 additions & 35 deletions src/delaunay_core/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use std::option::Option;
use super::{point::Point, utilities::make_uuid};

#[derive(Debug, PartialEq, Clone)]
pub struct Vertex<T, U> {
pub point: Point<T>,
pub struct Vertex<T, U, const D: usize> {
pub point: Point<T, D>,
pub uuid: Uuid,
pub incident_cell: Option<Uuid>,
pub data: Option<U>,
}

impl<T, U> Vertex<T, U> {
pub fn new_with_data(point: Point<T>, data: U) -> Self {
impl<T, U, const D: usize> Vertex<T, U, D> {
pub fn new_with_data(point: Point<T, D>, data: U) -> Self {
let uuid = make_uuid();
let incident_cell = None;
let data = Some(data);
Expand All @@ -25,7 +25,7 @@ impl<T, U> Vertex<T, U> {
}
}

pub fn new(point: Point<T>) -> Self {
pub fn new(point: Point<T, D>) -> Self {
let uuid = make_uuid();
let incident_cell = None;
let data = None;
Expand All @@ -37,13 +37,17 @@ impl<T, U> Vertex<T, U> {
}
}

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

pub fn into_hashmap(vertices: Vec<Self>) -> std::collections::HashMap<Uuid, Self> {
vertices.into_iter().map(|v| (v.uuid, v)).collect()
}

pub fn dim(&self) -> usize {
D
}
}
#[cfg(test)]
mod tests {
Expand All @@ -52,64 +56,69 @@ mod tests {

#[test]
fn make_vertex_with_data() {
let vertex = Vertex::new_with_data(Point::new(1.0, 2.0, 3.0), 3);
let vertex = Vertex::new_with_data(Point::new([1.0, 2.0, 3.0, 4.0]), "4D");
println!("{:?}", vertex);
assert_eq!(vertex.point.x, 1.0);
assert_eq!(vertex.point.y, 2.0);
assert_eq!(vertex.point.z, 3.0);
assert_eq!(vertex.point.coords[0], 1.0);
assert_eq!(vertex.point.coords[1], 2.0);
assert_eq!(vertex.point.coords[2], 3.0);
assert_eq!(vertex.point.coords[3], 4.0);
assert_eq!(vertex.point.dim(), 4);
assert!(vertex.incident_cell.is_none());
assert!(vertex.data.is_some());
assert_eq!(vertex.data.unwrap(), 3);
assert_eq!(vertex.data.unwrap(), "4D");
}

#[test]
fn make_vertex_without_data() {
let vertex: Vertex<f64, Option<()>> = Vertex::new(Point::new(1.0, 2.0, 3.0));
let vertex: Vertex<f64, Option<()>, 3> = Vertex::new(Point::new([1.0, 2.0, 3.0]));
println!("{:?}", vertex);
assert_eq!(vertex.point.x, 1.0);
assert_eq!(vertex.point.y, 2.0);
assert_eq!(vertex.point.z, 3.0);
assert_eq!(vertex.point.coords[0], 1.0);
assert_eq!(vertex.point.coords[1], 2.0);
assert_eq!(vertex.point.coords[2], 3.0);
assert!(vertex.incident_cell.is_none());
assert!(vertex.data.is_none());
}

#[test]
fn make_vertices_from_points() {
let points = vec![
Point::new(1.0, 2.0, 3.0),
Point::new(4.0, 5.0, 6.0),
Point::new(7.0, 8.0, 9.0),
Point::new([1.0, 2.0, 3.0]),
Point::new([4.0, 5.0, 6.0]),
Point::new([7.0, 8.0, 9.0]),
];
let vertices: Vec<Vertex<f64, Option<()>>> = Vertex::from_points(points);
println!("{:?}", vertices);
let vertices: Vec<Vertex<f64, Option<()>, 3>> = Vertex::from_points(points);

assert_eq!(vertices.len(), 3);
assert_eq!(vertices[0].point.x, 1.0);
assert_eq!(vertices[0].point.y, 2.0);
assert_eq!(vertices[0].point.z, 3.0);
assert_eq!(vertices[1].point.x, 4.0);
assert_eq!(vertices[1].point.y, 5.0);
assert_eq!(vertices[1].point.z, 6.0);
assert_eq!(vertices[2].point.x, 7.0);
assert_eq!(vertices[2].point.y, 8.0);
assert_eq!(vertices[2].point.z, 9.0);
assert_eq!(vertices[0].point.coords[0], 1.0);
assert_eq!(vertices[0].point.coords[1], 2.0);
assert_eq!(vertices[0].point.coords[2], 3.0);
assert_eq!(vertices[1].point.coords[0], 4.0);
assert_eq!(vertices[1].point.coords[1], 5.0);
assert_eq!(vertices[1].point.coords[2], 6.0);
assert_eq!(vertices[2].point.coords[0], 7.0);
assert_eq!(vertices[2].point.coords[1], 8.0);
assert_eq!(vertices[2].point.coords[2], 9.0);

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

#[test]
fn make_hashmap_from_vec() {
let points = vec![
Point::new(1.0, 2.0, 3.0),
Point::new(4.0, 5.0, 6.0),
Point::new(7.0, 8.0, 9.0),
Point::new([1.0, 2.0, 3.0]),
Point::new([4.0, 5.0, 6.0]),
Point::new([7.0, 8.0, 9.0]),
];
let mut vertices: Vec<Vertex<f64, Option<()>>> = Vertex::from_points(points);
let mut vertices: Vec<Vertex<f64, Option<()>, 3>> = Vertex::from_points(points);
let hashmap = Vertex::into_hashmap(vertices.clone());
println!("{:?}", hashmap);

assert_eq!(hashmap.len(), 3);
for (key, val) in hashmap.iter() {
assert_eq!(*key, val.uuid);
}

let mut values: Vec<Vertex<f64, Option<()>>> = hashmap.into_values().collect();
let mut values: Vec<Vertex<f64, Option<()>, 3>> = hashmap.into_values().collect();
assert_eq!(values.len(), 3);

values.sort_by(|a, b| a.uuid.cmp(&b.uuid));
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ mod delaunay_core {

#[cfg(test)]
mod tests {
use crate::delaunay_core::triangulation_data_structure::hello;
use crate::delaunay_core::triangulation_data_structure::start;

#[test]
fn it_works() {
let result = hello();
let result = start();
assert_eq!(result, 1);
}
}

0 comments on commit 62359b5

Please sign in to comment.