From ebc1c61a06729db927b03d444a889d4c9c6b9d6e Mon Sep 17 00:00:00 2001 From: Adam Getchell Date: Fri, 15 Dec 2023 10:06:10 -0800 Subject: [PATCH] Use uuid to identify cells and vertices - 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 --- Cargo.lock | 125 +++++++++++++++++++++++++++++++++ Cargo.toml | 3 +- src/delaunay_core/cell.rs | 35 +++++---- src/delaunay_core/point.rs | 14 ++++ src/delaunay_core/utilities.rs | 17 +++++ src/delaunay_core/vertex.rs | 36 +++------- src/lib.rs | 2 + 7 files changed, 192 insertions(+), 40 deletions(-) create mode 100644 src/delaunay_core/point.rs create mode 100644 src/delaunay_core/utilities.rs diff --git a/Cargo.lock b/Cargo.lock index b3c0799..42b31aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,131 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "d-delaunay" version = "0.1.0" +dependencies = [ + "uuid", +] + +[[package]] +name = "getrandom" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe9006bed769170c11f845cf00c7c1e9092aeb3f268e007c3e760ac68008070f" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.151" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4" + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "proc-macro2" +version = "1.0.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "syn" +version = "2.0.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c8b28c477cc3bf0e7966561e3460130e1255f7a1cf71931075f1c5e7a7e269" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "uuid" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e395fcf16a7a3d8127ec99782007af141946b4795001f876d54fb0d55978560" +dependencies = [ + "getrandom", + "rand", + "uuid-macro-internal", +] + +[[package]] +name = "uuid-macro-internal" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f49e7f3f3db8040a100710a11932239fd30697115e2ba4107080d8252939845e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" diff --git a/Cargo.toml b/Cargo.toml index 0d21ac3..a155a20 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" \ No newline at end of file +unsafe_code = "forbid" diff --git a/src/delaunay_core/cell.rs b/src/delaunay_core/cell.rs index 2e2bf9a..aaf228e 100644 --- a/src/delaunay_core/cell.rs +++ b/src/delaunay_core/cell.rs @@ -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 { pub vertices: Vec>, - index: usize, + uuid: Uuid, pub data: T, } #[allow(dead_code)] impl Cell { - pub fn new(vertices: Vec>, index: usize, data: T) -> Self { + pub fn new(vertices: Vec>, 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); } } diff --git a/src/delaunay_core/point.rs b/src/delaunay_core/point.rs new file mode 100644 index 0000000..77f8ad1 --- /dev/null +++ b/src/delaunay_core/point.rs @@ -0,0 +1,14 @@ +#[allow(dead_code)] +#[derive(Debug)] +pub struct Point { + pub x: T, + pub y: T, + pub z: T, +} + +#[allow(dead_code)] +impl Point { + pub fn new(x: T, y: T, z: T) -> Self { + Self { x, y, z } + } +} diff --git a/src/delaunay_core/utilities.rs b/src/delaunay_core/utilities.rs new file mode 100644 index 0000000..64cc570 --- /dev/null +++ b/src/delaunay_core/utilities.rs @@ -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); + } +} diff --git a/src/delaunay_core/vertex.rs b/src/delaunay_core/vertex.rs index 3e0614e..61bb36e 100644 --- a/src/delaunay_core/vertex.rs +++ b/src/delaunay_core/vertex.rs @@ -1,49 +1,35 @@ -#[allow(dead_code)] -#[derive(Debug)] -pub struct Point { - pub x: T, - pub y: T, - pub z: T, -} +use uuid::Uuid; -#[allow(dead_code)] -impl Point { - 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 { pub point: Point, - pub index: usize, + pub uuid: Uuid, pub data: U, } #[allow(dead_code)] impl Vertex { - pub fn new(point: Point, index: usize, data: U) -> Self { - Self { point, index, data } + pub fn new(point: Point, 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); } } diff --git a/src/lib.rs b/src/lib.rs index ceca443..c963926 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,8 @@ mod delaunay_core { pub mod cell; + pub mod point; pub mod triangulation_data_structure; + pub mod utilities; pub mod vertex; }