| 57 /// |
57 /// |
| 58 /// The halfspace is $H = \\{ t v + a \mid a^⊤ v = 0 \\}$, where $v$ is the orthogonal |
58 /// The halfspace is $H = \\{ t v + a \mid a^⊤ v = 0 \\}$, where $v$ is the orthogonal |
| 59 /// vector and $t$ the offset. |
59 /// vector and $t$ the offset. |
| 60 /// |
60 /// |
| 61 /// `U` is the element type, `F` the floating point number type, and `A` the type of the |
61 /// `U` is the element type, `F` the floating point number type, and `A` the type of the |
| 62 /// orthogonal (dual) vectors. They need implement [`Dot<U, F>`]. |
62 /// orthogonal (dual) vectors. They need implement [`Dot<U, Output=F>`]. |
| 63 #[derive(Clone,Copy,Debug,Serialize,Eq,PartialEq)] |
63 #[derive(Clone,Copy,Debug,Serialize,Eq,PartialEq)] |
| 64 pub struct Halfspace<A, F, U> where A : Dot<U, F>, F : Float { |
64 pub struct Halfspace<A, F, U> |
| |
65 where A : Dot<U, Field=F> + for<'a> Dot<&'a U>, |
| |
66 F : Float { |
| 65 pub orthogonal : A, |
67 pub orthogonal : A, |
| 66 pub offset : F, |
68 pub offset : F, |
| 67 _phantom : PhantomData<U>, |
69 _phantom : PhantomData<U>, |
| 68 } |
70 } |
| 69 |
71 |
| 70 impl<A,F,U> Halfspace<A,F,U> where A : Dot<U,F>, F : Float { |
72 impl<A,F,U> Halfspace<A,F,U> |
| |
73 where A : Dot<U, Field=F> + for<'a> Dot<&'a U>, |
| |
74 F : Float { |
| 71 #[inline] |
75 #[inline] |
| 72 pub fn new(orthogonal : A, offset : F) -> Self { |
76 pub fn new(orthogonal : A, offset : F) -> Self { |
| 73 Halfspace{ orthogonal : orthogonal, offset : offset, _phantom : PhantomData } |
77 Halfspace{ orthogonal : orthogonal, offset : offset, _phantom : PhantomData } |
| 74 } |
78 } |
| 75 } |
79 } |
| 76 |
80 |
| 77 /// Trait for generating a halfspace spanned by another set `Self` of elements of type `U`. |
81 /// Trait for generating a halfspace spanned by another set `Self` of elements of type `U`. |
| 78 pub trait SpannedHalfspace<F, U> where F : Float { |
82 pub trait SpannedHalfspace<F, U> where F : Float { |
| 79 /// Type of the orthogonal vector describing the halfspace. |
83 /// Type of the orthogonal vector describing the halfspace. |
| 80 type A : Dot<U, F>; |
84 type A : Dot<U, Field=F> + for<'a> Dot<&'a U>; |
| 81 /// Returns the halfspace spanned by this set. |
85 /// Returns the halfspace spanned by this set. |
| 82 fn spanned_halfspace(&self) -> Halfspace<Self::A, F, U>; |
86 fn spanned_halfspace(&self) -> Halfspace<Self::A, F, U>; |
| 83 } |
87 } |
| 84 |
88 |
| 85 // TODO: Gram-Schmidt for higher N. |
89 // TODO: Gram-Schmidt for higher N. |
| 101 let offset = x0.dot(&orthog); |
105 let offset = x0.dot(&orthog); |
| 102 Halfspace::new(orthog, offset) |
106 Halfspace::new(orthog, offset) |
| 103 } |
107 } |
| 104 } |
108 } |
| 105 |
109 |
| 106 impl<A,U,F> Set<U> for Halfspace<A,F,U> where A : Dot<U,F>, F : Float { |
110 impl<A,U,F> Set<U> for Halfspace<A,F,U> |
| |
111 where A : Dot<U, Field=F> + for<'a> Dot<&'a U>, |
| |
112 F : Float { |
| 107 #[inline] |
113 #[inline] |
| 108 fn contains(&self, item : &U) -> bool { |
114 fn contains(&self, item : &U) -> bool { |
| 109 self.orthogonal.dot(item) >= self.offset |
115 self.orthogonal.dot(item) >= self.offset |
| 110 } |
116 } |
| 111 } |
117 } |
| 112 |
118 |
| 113 /// Polygons defined by `N` `Halfspace`s. |
119 /// Polygons defined by `N` `Halfspace`s. |
| 114 #[derive(Clone,Copy,Debug,Eq,PartialEq)] |
120 #[derive(Clone,Debug,Eq,PartialEq)] |
| 115 pub struct NPolygon<A, F, U, const N : usize>(pub [Halfspace<A,F,U>; N]) where A : Dot<U,F>, F : Float; |
121 pub struct NPolygon<A, F, U, const N : usize>(pub [Halfspace<A,F,U>; N]) |
| |
122 where A : Dot<U, Field=F> + for<'a> Dot<&'a U>, |
| |
123 F : Float; |
| 116 |
124 |
| 117 impl<A,U,F,const N : usize> Set<U> for NPolygon<A,F,U,N> where A : Dot<U,F>, F : Float { |
125 impl<A,U,F,const N : usize> Set<U> for NPolygon<A,F,U,N> |
| |
126 where A : Dot<U, Field=F> + for<'a> Dot<&'a U>, |
| |
127 F : Float { |
| 118 fn contains(&self, item : &U) -> bool { |
128 fn contains(&self, item : &U) -> bool { |
| 119 self.0.iter().all(|halfspace| halfspace.contains(item)) |
129 self.0.iter().all(|halfspace| halfspace.contains(item)) |
| 120 } |
130 } |
| 121 } |
131 } |