Skip to content

Commit

Permalink
Bare-bones Tds ctor, make data optional
Browse files Browse the repository at this point in the history
- Basic (not yet correct) new function for triangulation data structures.
- Data fields for vertices and cells are now optional

It's expected that data will be inserted later, after calculations done on the triangulation.
  • Loading branch information
acgetchell committed Dec 16, 2023
1 parent 0b137ab commit 218cfc3
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 22 deletions.
47 changes: 36 additions & 11 deletions src/delaunay_core/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ use super::{utilities::make_uuid, vertex::Vertex};

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

impl<T, U, V> Cell<T, U, V> {
pub fn new(vertices: Vec<Vertex<U, V>>, data: T) -> Self {
pub fn new_with_data(vertices: Vec<Vertex<T, U>>, data: V) -> Self {
let uuid = make_uuid();
let neighbors = None;
let data = Some(data);
Cell {
vertices,
uuid,
Expand All @@ -25,6 +26,18 @@ impl<T, U, V> Cell<T, U, V> {
pub fn number_of_vertices(&self) -> usize {
self.vertices.len()
}

pub fn new(vertices: Vec<Vertex<T, U>>) -> Self {
let uuid = make_uuid();
let neighbors = None;
let data = None;
Cell {
vertices,
uuid,
neighbors,
data,
}
}
}

#[cfg(test)]
Expand All @@ -35,19 +48,31 @@ mod tests {
use super::*;

#[test]
fn make_cell() {
let vertex1 = Vertex::new(Point::new(1.0, 2.0, 3.0), 3);
let cell = Cell::new(vec![vertex1], 10);
fn make_cell_with_data() {
let vertex1 = Vertex::new_with_data(Point::new(1.0, 2.0, 3.0), 3);
let cell = Cell::new_with_data(vec![vertex1], 10);
println!("{:?}", cell);
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.number_of_vertices(), 1);
assert!(cell.neighbors.is_none());
assert!(cell.data.is_some());
assert_eq!(cell.data.unwrap(), 10);
}

#[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]);
println!("{:?}", cell);
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_ne!(cell.vertices[0].uuid, make_uuid());
assert_eq!(cell.vertices[0].data, 3);
assert_eq!(cell.vertices[0].data, Some(3));
assert_eq!(cell.number_of_vertices(), 1);
assert!(cell.neighbors.is_none());
assert_ne!(cell.uuid, make_uuid());
assert_ne!(cell.uuid, cell.vertices[0].uuid);
assert_eq!(cell.data, 10);
assert!(cell.data.is_none());
}
}
37 changes: 36 additions & 1 deletion src/delaunay_core/triangulation_data_structure.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,39 @@
pub fn tds() -> i32 {
use super::{cell::Cell, point::Point, vertex::Vertex};
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>>,
}

impl<T, U, V> Tds<T, U, V> {
pub fn new(_points: Vec<Point<U>>) -> Self {
let vertices = HashMap::new();
let cells = HashMap::new();
Self { vertices, cells }
}
}

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

#[cfg(test)]
mod tests {

use crate::delaunay_core::triangulation_data_structure;

use super::*;

#[test]
fn make_tds() {
let tds: triangulation_data_structure::Tds<usize, f64, usize> =
Tds::new(vec![Point::new(1.0, 2.0, 3.0)]);
println!("{:?}", tds);
assert_eq!(tds.vertices.len(), 0);
assert_eq!(tds.cells.len(), 0);
}
}
3 changes: 3 additions & 0 deletions src/delaunay_core/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ mod tests {
fn test_uuid() {
let uuid = make_uuid();
println!("make_uuid = {:?}", uuid);
println!("uuid version: {:?}\n", uuid.get_version_num());
assert_eq!(uuid.get_version_num(), 4);
assert_ne!(uuid, make_uuid());
}
}
38 changes: 30 additions & 8 deletions src/delaunay_core/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,26 @@ pub struct Vertex<T, U> {
pub point: Point<T>,
pub uuid: Uuid,
pub incident_cell: Option<Uuid>,
pub data: U,
pub data: Option<U>,
}

impl<T, U> Vertex<T, U> {
pub fn new(point: Point<T>, data: U) -> Self {
pub fn new_with_data(point: Point<T>, data: U) -> Self {
let uuid = make_uuid();
let incident_cell = None;
let data = Some(data);
Self {
point,
uuid,
incident_cell,
data,
}
}

pub fn new(point: Point<T>) -> Self {
let uuid = make_uuid();
let incident_cell = None;
let data = None;
Self {
point,
uuid,
Expand All @@ -30,16 +43,25 @@ mod tests {
use super::*;

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

#[test]
fn make_vertex_without_data() {
let vertex: Vertex<f64, Option<()>> = 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.uuid.get_version_num(), 4);
println!("uuid version: {:?}\n", vertex.uuid.get_version_num());
assert_ne!(vertex.uuid, make_uuid());
assert!(vertex.incident_cell.is_none());
assert_eq!(vertex.data, 3);
assert!(vertex.data.is_none());
}
}
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::tds;
use crate::delaunay_core::triangulation_data_structure::hello;

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

0 comments on commit 218cfc3

Please sign in to comment.