diff --git a/src/delaunay_core/cell.rs b/src/delaunay_core/cell.rs index 814efc8..3464b4e 100644 --- a/src/delaunay_core/cell.rs +++ b/src/delaunay_core/cell.rs @@ -42,7 +42,23 @@ impl Cell { /// a `Result` type. If the condition `vertices.len() > D + 1` is true, it returns an `Err` variant /// with the message "Number of vertices must be less than or equal to D + 1". Otherwise, it returns /// an `Ok` variant with a `Cell` containing the provided `vertices`, a generated `uuid`, and - /// optional neighbor and data fields. Neighbors will be calculated by the `delaunay_core::triangulation_data_structure::Tds`. + /// optional neighbor and data fields. + /// + /// Neighbors will be calculated by the `delaunay_core::triangulation_data_structure::Tds`. + /// + /// # Example + /// + /// ``` + /// use d_delaunay::delaunay_core::cell::Cell; + /// use d_delaunay::delaunay_core::vertex::Vertex; + /// use d_delaunay::delaunay_core::point::Point; + /// let vertex1 = Vertex::new(Point::new([0.0, 0.0, 1.0])); + /// let vertex2 = Vertex::new(Point::new([0.0, 1.0, 0.0])); + /// let vertex3 = Vertex::new(Point::new([1.0, 0.0, 0.0])); + /// let vertex4 = Vertex::new(Point::new([1.0, 1.0, 1.0])); + /// let cell: Cell, Option<()>, 3> = Cell::new(vec![vertex1, vertex2, vertex3, vertex4]).unwrap(); + /// assert!(cell.neighbors.is_none()); + /// ``` pub fn new(vertices: Vec>) -> Result { if vertices.len() > D + 1 { return Err("Number of vertices must be less than or equal to D + 1"); @@ -73,6 +89,20 @@ impl Cell { /// an `Ok` variant with a `Cell` containing the provided `vertices`, a generated `uuid`, the /// provided data, and optional neighbor fields which will be later be calculated by the /// `delaunay_core::triangulation_data_structure::Tds`. + /// + /// # Example + /// + /// ``` + /// use d_delaunay::delaunay_core::cell::Cell; + /// use d_delaunay::delaunay_core::vertex::Vertex; + /// use d_delaunay::delaunay_core::point::Point; + /// let vertex1 = Vertex::new_with_data(Point::new([0.0, 0.0, 1.0]), 1); + /// let vertex2 = Vertex::new_with_data(Point::new([0.0, 1.0, 0.0]), 1); + /// let vertex3 = Vertex::new_with_data(Point::new([1.0, 0.0, 0.0]), 1); + /// let vertex4 = Vertex::new_with_data(Point::new([1.0, 1.0, 1.0]), 2); + /// let cell = Cell::new_with_data(vec![vertex1, vertex2, vertex3, vertex4], "three-one cell").unwrap(); + /// assert_eq!(cell.data.unwrap(), "three-one cell"); + /// ``` pub fn new_with_data(vertices: Vec>, data: V) -> Result { if vertices.len() > D + 1 { return Err("Number of vertices must be less than or equal to D + 1"); diff --git a/src/delaunay_core/point.rs b/src/delaunay_core/point.rs index fc98de3..8bc5cd5 100644 --- a/src/delaunay_core/point.rs +++ b/src/delaunay_core/point.rs @@ -24,9 +24,9 @@ impl Point { /// # Returns: /// /// The `new` function returns an instance of the `Point`. - /// - /// # Examples - /// + /// + /// # Example + /// /// ``` /// use d_delaunay::delaunay_core::point::Point; /// let point = Point::new([1.0, 2.0, 3.0, 4.0]); @@ -42,7 +42,7 @@ impl Point { /// /// The `dim` function is returning the value of `D`, which the number of coordinates. /// - /// # Examples + /// # Example /// /// ``` /// use d_delaunay::delaunay_core::point::Point; diff --git a/src/delaunay_core/triangulation_data_structure.rs b/src/delaunay_core/triangulation_data_structure.rs index 91f883e..0f19267 100644 --- a/src/delaunay_core/triangulation_data_structure.rs +++ b/src/delaunay_core/triangulation_data_structure.rs @@ -1,5 +1,5 @@ //! Data and operations on d-dimensional triangulation data structures. -//! +//! //! Intended to match functionality of [CGAL Triangulations](https://doc.cgal.org/latest/Triangulation/index.html). use super::{cell::Cell, point::Point, vertex::Vertex}; @@ -68,7 +68,21 @@ impl Tds { /// /// # Returns: /// - /// The function `add` returns a `Result<(), &'static str>`. + /// The function `add` returns `Ok(())` if the vertex was successfully added to the hashmap, or + /// an error message if the vertex already exists or if there is a uuid collision. + /// + /// # Example: + /// + /// ``` + /// use d_delaunay::delaunay_core::triangulation_data_structure::Tds; + /// use d_delaunay::delaunay_core::vertex::Vertex; + /// use d_delaunay::delaunay_core::point::Point; + /// let mut tds: Tds = Tds::new(Vec::new()); + /// let point = Point::new([1.0, 2.0, 3.0]); + /// let vertex = Vertex::new(point); + /// let result = tds.add(vertex); + /// assert!(result.is_ok()); + /// ``` pub fn add(&mut self, vertex: Vertex) -> Result<(), &'static str> where T: PartialEq, @@ -80,9 +94,10 @@ impl Tds { } } + // Hashmap::insert returns the old value if the key already exists and updates it with the new value let result = self.vertices.insert(vertex.uuid, vertex); - // Hashmap::insert returns the old value if the key already exists and updates it with the new value + // Return an error if there is a uuid collision match result { Some(_) => Err("Uuid already exists"), None => Ok(()), diff --git a/src/delaunay_core/utilities.rs b/src/delaunay_core/utilities.rs index 55a4d57..9f23afc 100644 --- a/src/delaunay_core/utilities.rs +++ b/src/delaunay_core/utilities.rs @@ -8,6 +8,14 @@ use uuid::Uuid; /// /// a randomly generated UUID (Universally Unique Identifier) using the `new_v4` method from the `Uuid` /// struct. +/// +/// # Example +/// +/// ``` +/// use d_delaunay::delaunay_core::utilities::make_uuid; +/// let uuid = make_uuid(); +/// assert_eq!(uuid.get_version_num(), 4); +/// ``` pub fn make_uuid() -> Uuid { Uuid::new_v4() } diff --git a/src/delaunay_core/vertex.rs b/src/delaunay_core/vertex.rs index 70c9fe9..684b811 100644 --- a/src/delaunay_core/vertex.rs +++ b/src/delaunay_core/vertex.rs @@ -44,9 +44,9 @@ impl Vertex { /// # Returns: /// /// The `new_with_data` function returns an instance of the `Vertex`. - /// - /// # Examples - /// + /// + /// # Example + /// /// ``` /// use d_delaunay::delaunay_core::vertex::Vertex; /// use d_delaunay::delaunay_core::point::Point; @@ -77,9 +77,9 @@ impl Vertex { /// # Returns: /// /// The `new` function returns an instance of the `Vertex`. - /// - /// # Examples - /// + /// + /// # Example + /// /// ``` /// use d_delaunay::delaunay_core::vertex::Vertex; /// use d_delaunay::delaunay_core::point::Point;