Skip to content

Commit

Permalink
Rationalize traits
Browse files Browse the repository at this point in the history
Remove unneeded traits from functions and make them consistent across data structures.
  • Loading branch information
acgetchell committed Jan 22, 2024
1 parent 0e16c16 commit 2102809
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 66 deletions.
41 changes: 17 additions & 24 deletions src/delaunay_core/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{point::Point, utilities::make_uuid, vertex::Vertex};
use na::{ComplexField, Const, OPoint};
use nalgebra as na;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{collections::HashMap, fmt::Debug, iter::Sum, ops::Div};
use std::{collections::HashMap, fmt::Debug, hash::Hash, iter::Sum, ops::Div};
use uuid::Uuid;

#[derive(Clone, Debug, Default, Deserialize, Eq, Serialize)]
Expand All @@ -24,13 +24,14 @@ use uuid::Uuid;
/// to the current cell, indexed such that the `i-th` neighbor is opposite the
/// `i-th`` vertex.
/// * `data`: The `data` property is an optional field that can hold a value of
/// type `V`. It allows storage of additional data associated with the `Cell`.
/// type `V`. It allows storage of additional data associated with the `Cell`;
/// the data must implement Eq, Hash, Ord, PartialEq, and PartialOrd.
pub struct Cell<T, U, V, const D: usize>
where
T: Clone + Copy + Default + PartialEq + PartialOrd,
U: Clone + Copy + PartialEq,
V: Clone + Copy + PartialEq,
[T; D]: Default + DeserializeOwned + Serialize + Sized,
U: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
V: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
[T; D]: Copy + Default + DeserializeOwned + Serialize + Sized,
{
/// The vertices of the cell.
pub vertices: Vec<Vertex<T, U, D>>,
Expand All @@ -44,12 +45,12 @@ where

impl<T, U, V, const D: usize> Cell<T, U, V, D>
where
T: Clone + ComplexField<RealField = T> + Copy + Default + PartialOrd + Sum,
U: Clone + Copy + PartialEq,
V: Clone + Copy + PartialEq,
T: Clone + ComplexField<RealField = T> + Copy + Default + PartialEq + PartialOrd + Sum,
U: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
V: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
for<'a> &'a T: Div<f64>,
f64: From<T>,
[T; D]: Default + DeserializeOwned + Serialize + Sized,
[T; D]: Copy + Default + DeserializeOwned + Serialize + Sized,
{
/// The function `new` creates a new `Cell`` object with the given
/// vertices. A D-dimensional cell has D + 1 vertices, so the number of
Expand Down Expand Up @@ -234,11 +235,7 @@ where
/// let cell = Cell::new_with_data(vec![vertex1, vertex2, vertex3, vertex4], "three-one cell").unwrap();
/// assert!(cell.contains_vertex(vertex1));
/// ```
pub fn contains_vertex(&self, vertex: Vertex<T, U, D>) -> bool
where
T: PartialEq,
U: PartialEq,
{
pub fn contains_vertex(&self, vertex: Vertex<T, U, D>) -> bool {
self.vertices.contains(&vertex)
}

Expand Down Expand Up @@ -281,8 +278,6 @@ where
/// will return an Err variant containing an error message.
fn circumcenter(&self) -> Result<Point<f64, D>, &'static str>
where
T: Clone + Copy + Debug + PartialEq,
[T; D]: Default + DeserializeOwned + Serialize + Sized,
[f64; D]: Default + DeserializeOwned + Serialize + Sized,
{
let dim = self.dim();
Expand Down Expand Up @@ -337,7 +332,6 @@ where
/// otherwise returns an Err with an error message.
fn circumradius(&self) -> Result<T, &'static str>
where
T: Clone + Copy + Default,
OPoint<T, Const<D>>: From<[f64; D]>,
[f64; D]: Default + DeserializeOwned + Serialize + Sized,
{
Expand Down Expand Up @@ -377,7 +371,6 @@ where
/// ```
pub fn circumsphere_contains(&self, vertex: Vertex<T, U, D>) -> Result<bool, &'static str>
where
T: Clone + Copy + Default + PartialOrd,
OPoint<T, Const<D>>: From<[f64; D]>,
[f64; D]: Default + DeserializeOwned + Serialize + Sized,
{
Expand All @@ -395,9 +388,9 @@ where
impl<T, U, V, const D: usize> PartialEq for Cell<T, U, V, D>
where
T: Clone + Copy + Default + PartialEq + PartialOrd,
U: Clone + Copy + PartialEq,
V: Clone + Copy + PartialEq,
[T; D]: Default + DeserializeOwned + Serialize + Sized,
U: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
V: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
[T; D]: Copy + Default + DeserializeOwned + Serialize + Sized,
{
#[inline]
fn eq(&self, other: &Self) -> bool {
Expand All @@ -413,9 +406,9 @@ where
impl<T, U, V, const D: usize> PartialOrd for Cell<T, U, V, D>
where
T: Clone + Copy + Default + PartialEq + PartialOrd,
U: Clone + Copy + PartialEq,
V: Clone + Copy + PartialEq,
[T; D]: Default + DeserializeOwned + Serialize + Sized,
U: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
V: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
[T; D]: Copy + Default + DeserializeOwned + Serialize + Sized,
{
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Expand Down
13 changes: 7 additions & 6 deletions src/delaunay_core/facet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use super::{cell::Cell, vertex::Vertex};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::hash::Hash;

#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, PartialOrd, Serialize)]
/// The `Facet` struct represents a facet of a d-dimensional simplex.
Expand All @@ -24,9 +25,9 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
pub struct Facet<T, U, V, const D: usize>
where
T: Clone + Copy + Default + PartialEq + PartialOrd,
U: Clone + Copy + PartialEq,
V: Clone + Copy + PartialEq,
[T; D]: Default + DeserializeOwned + Serialize + Sized,
U: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
V: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
[T; D]: Copy + Default + DeserializeOwned + Serialize + Sized,
{
/// The `Cell` that contains this facet.
pub cell: Cell<T, U, V, D>,
Expand All @@ -38,9 +39,9 @@ where
impl<T, U, V, const D: usize> Facet<T, U, V, D>
where
T: Clone + Copy + Default + PartialEq + PartialOrd,
U: Clone + Copy + PartialEq,
V: Clone + Copy + PartialEq,
[T; D]: Default + DeserializeOwned + Serialize + Sized,
U: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
V: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
[T; D]: Copy + Default + DeserializeOwned + Serialize + Sized,
{
/// The `new` function is a constructor for the `Facet` struct. It takes
/// in a `Cell` and a `Vertex` as arguments and returns a `Result`
Expand Down
10 changes: 5 additions & 5 deletions src/delaunay_core/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
/// integer, which means it cannot be changed and is known at compile time.
pub struct Point<T, const D: usize>
where
T: PartialOrd,
[T; D]: Copy + Default + DeserializeOwned + PartialEq + Serialize + Sized,
T: Clone + Copy + Default + PartialEq + PartialOrd,
[T; D]: Copy + Default + DeserializeOwned + Serialize + Sized,
{
/// The coordinates of the point.
pub coords: [T; D],
}

impl<T, const D: usize> From<[T; D]> for Point<f64, D>
where
T: Into<f64>,
[T; D]: Default + DeserializeOwned + Serialize + Sized,
T: Clone + Copy + Default + Into<f64> + PartialEq + PartialOrd,
[T; D]: Copy + Default + DeserializeOwned + Serialize + Sized,
[f64; D]: Default + DeserializeOwned + Serialize + Sized,
{
fn from(coords: [T; D]) -> Self {
Expand All @@ -37,7 +37,7 @@ where
impl<T, const D: usize> Point<T, D>
where
T: Clone + Copy + Default + PartialEq + PartialOrd,
[T; D]: Default + DeserializeOwned + Serialize + Sized,
[T; D]: Copy + Default + DeserializeOwned + Serialize + Sized,
{
/// The function `new` creates a new instance of a `Point` with the given
/// coordinates.
Expand Down
34 changes: 11 additions & 23 deletions src/delaunay_core/triangulation_data_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ use na::{ComplexField, Const, OPoint};
use nalgebra as na;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::cmp::{min, Ordering, PartialEq};
use std::collections::HashMap;
use std::iter::Sum;
use std::ops::{AddAssign, Div, SubAssign};
use std::{collections::HashMap, hash::Hash, iter::Sum};
use uuid::Uuid;

#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
Expand Down Expand Up @@ -46,9 +45,9 @@ use uuid::Uuid;
pub struct Tds<T, U, V, const D: usize>
where
T: Clone + Copy + Default + PartialEq + PartialOrd,
U: Clone + Copy + PartialEq,
V: Clone + Copy + PartialEq,
[T; D]: Default + DeserializeOwned + Serialize + Sized,
U: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
V: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
[T; D]: Copy + Default + DeserializeOwned + Serialize + Sized,
{
/// A `HashMap` that stores vertices with their corresponding `Uuid`s as
/// keys. Each `Vertex` has a `Point` of type T, vertex data of type U,
Expand All @@ -70,14 +69,15 @@ where
+ Copy
+ ComplexField<RealField = T>
+ Default
+ PartialEq
+ PartialOrd
+ SubAssign<f64>
+ Sum,
U: Clone + Copy + PartialEq,
V: Clone + Copy + PartialEq,
U: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
V: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
f64: From<T>,
for<'a> &'a T: Div<f64>,
[T; D]: Default + DeserializeOwned + Serialize + Sized,
[T; D]: Copy + Default + DeserializeOwned + Serialize + Sized,
{
/// The function creates a new instance of a triangulation data structure
/// with given points, initializing the vertices and cells.
Expand Down Expand Up @@ -128,10 +128,7 @@ where
/// 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,
{
pub fn add(&mut self, vertex: Vertex<T, U, D>) -> Result<(), &'static str> {
// Don't add if vertex with that point already exists
for val in self.vertices.values() {
if val.point.coords == vertex.point.coords {
Expand Down Expand Up @@ -210,11 +207,7 @@ where
/// # Returns:
///
/// A `Cell` that encompasses all vertices in the triangulation.
fn supercell(&self) -> Result<Cell<T, U, V, D>, &'static str>
where
T: Copy + Default + PartialOrd,
Vertex<T, U, D>: Clone, // Add the Clone trait bound for Vertex
{
fn supercell(&self) -> Result<Cell<T, U, V, D>, &'static str> {
// First, find the min and max coordinates
let mut min_coords = find_extreme_coordinates(self.vertices.clone(), Ordering::Less);
let mut max_coords = find_extreme_coordinates(self.vertices.clone(), Ordering::Greater);
Expand Down Expand Up @@ -253,13 +246,8 @@ where

fn bowyer_watson(&mut self) -> Result<Vec<Cell<T, U, V, D>>, &'static str>
where
T: Copy + Default + PartialOrd,
Vertex<T, U, D>: Clone, // Add the Clone trait bound for Vertex
OPoint<T, Const<D>>: From<[f64; D]>,
[f64; D]: Serialize + DeserializeOwned + Default,
f64: PartialOrd, // Replace f64: Ord with f64: PartialOrd
U: PartialOrd,
V: PartialOrd,
[f64; D]: Default + DeserializeOwned + Serialize + Sized,
{
let mut cells: Vec<Cell<T, U, V, D>> = Vec::new();

Expand Down
6 changes: 3 additions & 3 deletions src/delaunay_core/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use super::vertex::Vertex;
use serde::{de::DeserializeOwned, Serialize};
use std::cmp::Ordering;
use std::collections::HashMap;
use std::{cmp::Ordering, hash::Hash};
use uuid::Uuid;

/// The function `make_uuid` generates a version 4 UUID in Rust.
Expand Down Expand Up @@ -65,8 +65,8 @@ pub fn find_extreme_coordinates<T, U, const D: usize>(
ordering: Ordering,
) -> [T; D]
where
T: Clone + Copy + Default + PartialOrd,
U: Clone + Copy + PartialEq,
T: Clone + Copy + Default + PartialEq + PartialOrd,
U: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
[T; D]: Default + DeserializeOwned + Serialize + Sized,
{
let mut min_coords = [Default::default(); D];
Expand Down
15 changes: 10 additions & 5 deletions src/delaunay_core/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use super::{point::Point, utilities::make_uuid};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{cmp::Ordering, collections::HashMap, option::Option};
use std::{cmp::Ordering, collections::HashMap, hash::Hash, option::Option};
use uuid::Uuid;

#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, Serialize)]
Expand All @@ -22,10 +22,15 @@ use uuid::Uuid;
/// calculated by the `delaunay_core::triangulation_data_structure::Tds`.
/// * `data`: The `data` property is an optional field that can hold any
/// type `U`. It is used to store additional data associated with the vertex.
///
/// Data type T is in practice f64 which does not implement Eq, Hash, or Ord.
///
/// U is intended to be data associated with the vertex, e.g. a string, which
/// implements Eq, Hash, Ord, PartialEq, and PartialOrd.
pub struct Vertex<T, U, const D: usize>
where
T: Clone + Copy + Default + PartialEq + PartialOrd,
U: Clone + Copy + PartialEq,
U: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
[T; D]: Copy + Default + DeserializeOwned + Serialize + Sized,
{
/// The coordinates of the vertex in a D-dimensional space.
Expand All @@ -41,7 +46,7 @@ where
impl<T, U, const D: usize> Vertex<T, U, D>
where
T: Clone + Copy + Default + PartialEq + PartialOrd,
U: Clone + Copy + PartialEq,
U: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
[T; D]: Copy + Default + DeserializeOwned + Serialize + Sized,
{
/// The function creates a new instance of a `Vertex`.
Expand Down Expand Up @@ -180,7 +185,7 @@ where
impl<T, U, const D: usize> PartialEq for Vertex<T, U, D>
where
T: Clone + Copy + Default + PartialEq + PartialOrd,
U: Clone + Copy + PartialEq,
U: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
[T; D]: Copy + Default + DeserializeOwned + Serialize + Sized,
{
#[inline]
Expand All @@ -196,7 +201,7 @@ where
impl<T, U, const D: usize> PartialOrd for Vertex<T, U, D>
where
T: Clone + Copy + Default + PartialEq + PartialOrd,
U: Clone + Copy + PartialEq,
U: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
[T; D]: Copy + Default + DeserializeOwned + Serialize + Sized,
{
#[inline]
Expand Down

0 comments on commit 2102809

Please sign in to comment.