Skip to content

Commit

Permalink
Use uuid to identify cells and vertices
Browse files Browse the repository at this point in the history
- Added the `uuid` crate as a dependency in `Cargo.toml`
- Updated the `Cell` struct in `cell.rs` to include a `uuid` field of type `Uuid`
- Modified the constructor of `Cell` in `cell.rs` to generate a unique UUID for each cell
- Added a new module called `point.rs` in the `delaunay_core` module, which defines a generic point struct
- Added a new module called `utilities.rs` in the `delaunay_core` module, which includes a function to generate UUIDs
- Updated the constructor of `Vertex` in `vertex.rs` to generate a unique UUID for each vertex
- Added tests for the new code changes
  • Loading branch information
acgetchell committed Dec 15, 2023
1 parent 885f1d4 commit ebc1c61
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 40 deletions.
125 changes: 125 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
uuid = { version = "1.6.1", features = ["v4", "fast-rng", "macro-diagnostics"] }

[lints.rust]
unsafe_code = "forbid"
unsafe_code = "forbid"
35 changes: 21 additions & 14 deletions src/delaunay_core/cell.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
use crate::delaunay_core::vertex::Vertex;
use uuid::Uuid;

use super::{utilities::make_uuid, vertex::Vertex};

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

#[allow(dead_code)]
impl<T, U, V> Cell<T, U, V> {
pub fn new(vertices: Vec<Vertex<U, V>>, index: usize, data: T) -> Self {
pub fn new(vertices: Vec<Vertex<U, V>>, data: T) -> Self {
let uuid = make_uuid();
Cell {
vertices,
index,
uuid,
data,
}
}

pub fn number_of_vertices(&self) -> usize {
self.vertices.len()
}
}

#[cfg(test)]
mod tests {

use crate::delaunay_core::point::Point;

use super::*;
#[test]
fn make_cell() {
use crate::delaunay_core::cell::Cell;
use crate::delaunay_core::vertex::Vertex;
let vertex1 = Vertex::new(
crate::delaunay_core::vertex::Point::new(1.0, 2.0, 3.0),
0,
3,
);
let cell = Cell::new(vec![vertex1], 5, 10);
let vertex1 = Vertex::new(Point::new(1.0, 2.0, 3.0), 3);
let cell = Cell::new(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].index, 0);
assert_ne!(cell.vertices[0].uuid, make_uuid());
assert_eq!(cell.vertices[0].data, 3);
assert_eq!(cell.index, 5);
assert_eq!(cell.number_of_vertices(), 1);
assert_ne!(cell.uuid, make_uuid());
assert_ne!(cell.uuid, cell.vertices[0].uuid);
assert_eq!(cell.data, 10);
}
}
14 changes: 14 additions & 0 deletions src/delaunay_core/point.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#[allow(dead_code)]
#[derive(Debug)]
pub struct Point<T> {
pub x: T,
pub y: T,
pub z: T,
}

#[allow(dead_code)]
impl<T> Point<T> {
pub fn new(x: T, y: T, z: T) -> Self {
Self { x, y, z }
}
}
17 changes: 17 additions & 0 deletions src/delaunay_core/utilities.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use uuid::Uuid;

pub fn make_uuid() -> Uuid {
Uuid::new_v4()
}

#[cfg(test)]
mod tests {

use super::*;

#[test]
fn test_uuid() {
let uuid = make_uuid();
println!("make_uuid = {:?}", uuid);
}
}
36 changes: 11 additions & 25 deletions src/delaunay_core/vertex.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,35 @@
#[allow(dead_code)]
#[derive(Debug)]
pub struct Point<T> {
pub x: T,
pub y: T,
pub z: T,
}
use uuid::Uuid;

#[allow(dead_code)]
impl<T> Point<T> {
pub fn new(x: T, y: T, z: T) -> Self {
Self { x, y, z }
}
}
use super::{point::Point, utilities::make_uuid};

#[allow(dead_code)]
#[derive(Debug)]
pub struct Vertex<T, U> {
pub point: Point<T>,
pub index: usize,
pub uuid: Uuid,
pub data: U,
}

#[allow(dead_code)]
impl<T, U> Vertex<T, U> {
pub fn new(point: Point<T>, index: usize, data: U) -> Self {
Self { point, index, data }
pub fn new(point: Point<T>, data: U) -> Self {
let uuid = make_uuid();
Self { point, uuid, data }
}
}

#[cfg(test)]
mod tests {

use super::*;

#[test]
fn make_vertex() {
use crate::delaunay_core::vertex::Vertex;

let vertex = Vertex::new(
crate::delaunay_core::vertex::Point::new(1.0, 2.0, 3.0),
0,
3,
);
let vertex = Vertex::new(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_eq!(vertex.index, 0);
assert_ne!(vertex.uuid, make_uuid());
assert_eq!(vertex.data, 3);
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod delaunay_core {
pub mod cell;
pub mod point;
pub mod triangulation_data_structure;
pub mod utilities;
pub mod vertex;
}

Expand Down

0 comments on commit ebc1c61

Please sign in to comment.