diff -r d14c877e14b7 -r b3c35d16affe src/sets.rs --- a/src/sets.rs Tue Feb 20 12:33:16 2024 -0500 +++ b/src/sets.rs Mon Feb 03 19:22:16 2025 -0500 @@ -3,19 +3,19 @@ */ use std::ops::{RangeFull,RangeFrom,Range,RangeInclusive,RangeTo,RangeToInclusive}; -use std::marker::PhantomData; use crate::types::*; use crate::loc::Loc; -use crate::euclidean::Dot; +use crate::euclidean::Euclidean; +use crate::instance::{Space, Instance}; use serde::Serialize; -mod cube; +pub mod cube; pub use cube::Cube; /// Trait for arbitrary sets. The parameter `U` is the element type. -pub trait Set where U : ?Sized { +pub trait Set where U : Space { /// Check for element containment - fn contains(&self, item : &U) -> bool; + fn contains>(&self, item : I) -> bool; } /// Additional ordering (besides [`PartialOrd`]) of a subfamily of sets: @@ -31,22 +31,27 @@ impl Set> for Cube where U : Num + PartialOrd + Sized { - fn contains(&self, item : &Loc) -> bool { - self.0.iter().zip(item.iter()).all(|(s, x)| s.contains(x)) + fn contains>>(&self, item : I) -> bool { + self.0.iter().zip(item.ref_instance().iter()).all(|(s, x)| s.contains(x)) } } -impl Set for RangeFull { - fn contains(&self, _item : &U) -> bool { true } +impl Set for RangeFull { + fn contains>(&self, _item : I) -> bool { true } } macro_rules! impl_ranges { ($($range:ident),*) => { $( - impl Set - for $range - where Idx : PartialOrd, U : PartialOrd + ?Sized, Idx : PartialOrd { + impl Set for $range + where + Idx : PartialOrd, + U : PartialOrd + Space, + Idx : PartialOrd + { #[inline] - fn contains(&self, item : &U) -> bool { $range::contains(&self, item) } + fn contains>(&self, item : I) -> bool { + item.eval(|x| $range::contains(&self, x)) + } } )* } } @@ -57,44 +62,40 @@ /// /// The halfspace is $H = \\{ t v + a \mid a^⊤ v = 0 \\}$, where $v$ is the orthogonal /// vector and $t$ the offset. -/// -/// `U` is the element type, `F` the floating point number type, and `A` the type of the -/// orthogonal (dual) vectors. They need implement [`Dot`]. #[derive(Clone,Copy,Debug,Serialize,Eq,PartialEq)] -pub struct Halfspace where A : Dot, F : Float { +pub struct Halfspace where A : Euclidean, F : Float { pub orthogonal : A, pub offset : F, - _phantom : PhantomData, } -impl Halfspace where A : Dot, F : Float { +impl Halfspace where A : Euclidean, F : Float { #[inline] pub fn new(orthogonal : A, offset : F) -> Self { - Halfspace{ orthogonal : orthogonal, offset : offset, _phantom : PhantomData } + Halfspace{ orthogonal : orthogonal, offset : offset } } } /// Trait for generating a halfspace spanned by another set `Self` of elements of type `U`. -pub trait SpannedHalfspace where F : Float { +pub trait SpannedHalfspace where F : Float { /// Type of the orthogonal vector describing the halfspace. - type A : Dot; + type A : Euclidean; /// Returns the halfspace spanned by this set. - fn spanned_halfspace(&self) -> Halfspace; + fn spanned_halfspace(&self) -> Halfspace; } // TODO: Gram-Schmidt for higher N. -impl SpannedHalfspace> for [Loc; 2] { +impl SpannedHalfspace for [Loc; 2] { type A = Loc; - fn spanned_halfspace(&self) -> Halfspace> { + fn spanned_halfspace(&self) -> Halfspace { let (x0, x1) = (self[0], self[1]); Halfspace::new(x1-x0, x0[0]) } } // TODO: Gram-Schmidt for higher N. -impl SpannedHalfspace> for [Loc; 2] { +impl SpannedHalfspace for [Loc; 2] { type A = Loc; - fn spanned_halfspace(&self) -> Halfspace> { + fn spanned_halfspace(&self) -> Halfspace { let (x0, x1) = (&self[0], &self[1]); let d = x1 - x0; let orthog = loc![d[1], -d[0]]; // We don't normalise for efficiency @@ -103,19 +104,29 @@ } } -impl Set for Halfspace where A : Dot, F : Float { +impl Set for Halfspace +where + A : Euclidean, + F : Float, +{ #[inline] - fn contains(&self, item : &U) -> bool { + fn contains>(&self, item : I) -> bool { self.orthogonal.dot(item) >= self.offset } } /// Polygons defined by `N` `Halfspace`s. #[derive(Clone,Copy,Debug,Eq,PartialEq)] -pub struct NPolygon(pub [Halfspace; N]) where A : Dot, F : Float; +pub struct NPolygon(pub [Halfspace; N]) +where A : Euclidean, F : Float; -impl Set for NPolygon where A : Dot, F : Float { - fn contains(&self, item : &U) -> bool { - self.0.iter().all(|halfspace| halfspace.contains(item)) +impl Set for NPolygon +where + A : Euclidean, + F : Float, +{ + fn contains>(&self, item : I) -> bool { + let r = item.ref_instance(); + self.0.iter().all(|halfspace| halfspace.contains(r)) } }