Thu, 03 Sep 2026 10:50:27 -0500
test fix
| 5 | 1 | /*! |
| 2 | This module provides various sets and traits for them. | |
| 3 | */ | |
| 4 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
5 | use crate::euclidean::Euclidean; |
| 155 | 6 | use crate::instance::{BasicDecomposition, Instance, Space}; |
| 5 | 7 | use crate::loc::Loc; |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
8 | use crate::types::*; |
| 0 | 9 | use serde::Serialize; |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
10 | use std::ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive}; |
| 0 | 11 | |
|
41
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
12 | pub mod cube; |
| 5 | 13 | pub use cube::Cube; |
| 0 | 14 | |
| 15 | /// Trait for arbitrary sets. The parameter `U` is the element type. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
16 | pub trait Set<U> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
17 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
18 | U: Space, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
19 | { |
| 5 | 20 | /// Check for element containment |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
21 | fn contains<I: Instance<U>>(&self, item: I) -> bool; |
| 0 | 22 | } |
| 23 | ||
| 5 | 24 | /// Additional ordering (besides [`PartialOrd`]) of a subfamily of sets: |
| 25 | /// greatest lower bound and least upper bound. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
26 | pub trait SetOrd: Sized { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
27 | /// Returns the smallest set of same class contain both parameters. |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
28 | fn common(&self, other: &Self) -> Self; |
| 0 | 29 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
30 | /// Returns the greatest set of same class contaied by n both parameter sets. |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
31 | fn intersect(&self, other: &Self) -> Option<Self>; |
| 0 | 32 | } |
| 33 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
34 | impl<U, const N: usize> Set<Loc<N, U>> for Cube<N, U> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
35 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
36 | U: Num + PartialOrd + Sized, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
37 | { |
| 202 | 38 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
39 | fn contains<I: Instance<Loc<N, U>>>(&self, item: I) -> bool { |
| 202 | 40 | item.eval_ref(|r| self.0.iter().zip(r.iter()).all(|([a, b], x)| a<=x && x<=b)) |
| 0 | 41 | } |
| 42 | } | |
| 43 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
44 | impl<U: Space> Set<U> for RangeFull { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
45 | fn contains<I: Instance<U>>(&self, _item: I) -> bool { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
46 | true |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
47 | } |
| 0 | 48 | } |
| 49 | ||
| 50 | macro_rules! impl_ranges { | |
| 51 | ($($range:ident),*) => { $( | |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
41
diff
changeset
|
52 | impl<U,Idx> Set<U> for $range<Idx> |
|
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
41
diff
changeset
|
53 | where |
| 163 | 54 | U : Space<Decomp=BasicDecomposition>, |
| 164 | 55 | U::Principal : PartialOrd<Idx>, |
| 56 | Idx : PartialOrd + PartialOrd<U::Principal>, | |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
41
diff
changeset
|
57 | { |
| 0 | 58 | #[inline] |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
41
diff
changeset
|
59 | fn contains<I : Instance<U>>(&self, item : I) -> bool { |
|
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
41
diff
changeset
|
60 | item.eval(|x| $range::contains(&self, x)) |
|
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
41
diff
changeset
|
61 | } |
| 0 | 62 | } |
| 63 | )* } | |
| 64 | } | |
| 65 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
66 | impl_ranges!(RangeFrom, Range, RangeInclusive, RangeTo, RangeToInclusive); |
| 0 | 67 | |
| 5 | 68 | /// Halfspaces described by an orthogonal vector and an offset. |
| 69 | /// | |
| 70 | /// The halfspace is $H = \\{ t v + a \mid a^⊤ v = 0 \\}$, where $v$ is the orthogonal | |
| 71 | /// vector and $t$ the offset. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
72 | #[derive(Clone, Copy, Debug, Serialize, Eq, PartialEq)] |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
73 | pub struct Halfspace<A, F> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
74 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
75 | A: Euclidean<F>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
76 | F: Float, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
77 | { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
78 | pub orthogonal: A, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
79 | pub offset: F, |
| 0 | 80 | } |
| 81 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
82 | impl<A, F> Halfspace<A, F> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
83 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
84 | A: Euclidean<F>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
85 | F: Float, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
86 | { |
| 0 | 87 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
88 | pub fn new(orthogonal: A, offset: F) -> Self { |
| 155 | 89 | Halfspace { orthogonal: orthogonal, offset: offset } |
| 0 | 90 | } |
| 91 | } | |
| 92 | ||
| 5 | 93 | /// Trait for generating a halfspace spanned by another set `Self` of elements of type `U`. |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
94 | pub trait SpannedHalfspace<F> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
95 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
96 | F: Float, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
97 | { |
| 5 | 98 | /// Type of the orthogonal vector describing the halfspace. |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
99 | type A: Euclidean<F>; |
| 5 | 100 | /// Returns the halfspace spanned by this set. |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
41
diff
changeset
|
101 | fn spanned_halfspace(&self) -> Halfspace<Self::A, F>; |
| 0 | 102 | } |
| 103 | ||
| 104 | // TODO: Gram-Schmidt for higher N. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
105 | impl<F: Float> SpannedHalfspace<F> for [Loc<1, F>; 2] { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
106 | type A = Loc<1, F>; |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
41
diff
changeset
|
107 | fn spanned_halfspace(&self) -> Halfspace<Self::A, F> { |
| 0 | 108 | let (x0, x1) = (self[0], self[1]); |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
109 | Halfspace::new(x1 - x0, x0[0]) |
| 0 | 110 | } |
| 111 | } | |
| 112 | ||
| 113 | // TODO: Gram-Schmidt for higher N. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
114 | impl<F: Float> SpannedHalfspace<F> for [Loc<2, F>; 2] { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
115 | type A = Loc<2, F>; |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
41
diff
changeset
|
116 | fn spanned_halfspace(&self) -> Halfspace<Self::A, F> { |
| 0 | 117 | let (x0, x1) = (&self[0], &self[1]); |
| 118 | let d = x1 - x0; | |
| 119 | let orthog = loc![d[1], -d[0]]; // We don't normalise for efficiency | |
| 120 | let offset = x0.dot(&orthog); | |
| 121 | Halfspace::new(orthog, offset) | |
| 122 | } | |
| 123 | } | |
| 124 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
125 | impl<A, F> Set<A> for Halfspace<A, F> |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
41
diff
changeset
|
126 | where |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
127 | A: Euclidean<F>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
128 | F: Float, |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
41
diff
changeset
|
129 | { |
| 0 | 130 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
131 | fn contains<I: Instance<A>>(&self, item: I) -> bool { |
| 0 | 132 | self.orthogonal.dot(item) >= self.offset |
| 133 | } | |
| 134 | } | |
| 135 | ||
| 136 | /// Polygons defined by `N` `Halfspace`s. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
137 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
138 | pub struct NPolygon<A, const N: usize, F = f64>(pub [Halfspace<A, F>; N]) |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
139 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
140 | A: Euclidean<F>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
141 | F: Float; |
| 0 | 142 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
143 | impl<A, F, const N: usize> Set<A> for NPolygon<A, N, F> |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
41
diff
changeset
|
144 | where |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
145 | A: Euclidean<F>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
146 | F: Float, |
|
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
41
diff
changeset
|
147 | { |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
148 | fn contains<I: Instance<A>>(&self, item: I) -> bool { |
| 171 | 149 | item.eval_ref(|r| self.0.iter().all(|halfspace| halfspace.contains(r))) |
| 0 | 150 | } |
| 151 | } |