Skip to content

Commit

Permalink
More documentation
Browse files Browse the repository at this point in the history
With runnable tests!
  • Loading branch information
acgetchell committed Dec 19, 2023
1 parent 24f6d0c commit 94bbbcb
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 14 deletions.
32 changes: 31 additions & 1 deletion src/delaunay_core/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,23 @@ impl<T, U, V, const D: usize> Cell<T, U, V, D> {
/// 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<f64, Option<()>, Option<()>, 3> = Cell::new(vec![vertex1, vertex2, vertex3, vertex4]).unwrap();
/// assert!(cell.neighbors.is_none());
/// ```
pub fn new(vertices: Vec<Vertex<T, U, D>>) -> Result<Self, &'static str> {
if vertices.len() > D + 1 {
return Err("Number of vertices must be less than or equal to D + 1");
Expand Down Expand Up @@ -73,6 +89,20 @@ impl<T, U, V, const D: usize> Cell<T, U, V, D> {
/// 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<Vertex<T, U, D>>, data: V) -> Result<Self, &'static str> {
if vertices.len() > D + 1 {
return Err("Number of vertices must be less than or equal to D + 1");
Expand Down
8 changes: 4 additions & 4 deletions src/delaunay_core/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ impl<T: Clone, const D: usize> Point<T, D> {
/// # 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]);
Expand All @@ -42,7 +42,7 @@ impl<T: Clone, const D: usize> Point<T, D> {
///
/// The `dim` function is returning the value of `D`, which the number of coordinates.
///
/// # Examples
/// # Example
///
/// ```
/// use d_delaunay::delaunay_core::point::Point;
Expand Down
21 changes: 18 additions & 3 deletions src/delaunay_core/triangulation_data_structure.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -68,7 +68,21 @@ impl<T, U, V, const D: usize> Tds<T, U, V, D> {
///
/// # 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<f64, usize, usize, 3> = 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<T, U, D>) -> Result<(), &'static str>
where
T: PartialEq,
Expand All @@ -80,9 +94,10 @@ impl<T, U, V, const D: usize> Tds<T, U, V, D> {
}
}

// 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(()),
Expand Down
8 changes: 8 additions & 0 deletions src/delaunay_core/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
12 changes: 6 additions & 6 deletions src/delaunay_core/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ impl<T, U, const D: usize> Vertex<T, U, D> {
/// # 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;
Expand Down Expand Up @@ -77,9 +77,9 @@ impl<T, U, const D: usize> Vertex<T, U, D> {
/// # 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;
Expand Down

0 comments on commit 94bbbcb

Please sign in to comment.