Mon, 21 Oct 2024 14:02:52 -0500
change experiment
0 | 1 | |
2 | use core::f64; | |
3 | ||
4 | use alg_tools::loc::Loc; | |
5 | use alg_tools::norms::{Norm, L2}; | |
7 | 6 | use crate::manifold::{ManifoldPoint, EmbeddedManifoldPoint}; |
0 | 7 | |
8 | #[derive(Copy, Clone, Debug, Eq, PartialEq)] | |
9 | pub enum Face {F1, F2, F3, F4, F5, F6} | |
10 | use Face::*; | |
11 | ||
12 | pub type Point = Loc<f64, 2>; | |
13 | ||
14 | pub type AdjacentFaces = [Face; 4]; | |
15 | ||
3 | 16 | #[derive(Clone, Debug)] |
0 | 17 | pub enum Path { |
18 | Direct { destination : Face }, | |
19 | Indirect { destination : Face, intermediate : Face }, | |
20 | } | |
21 | ||
22 | /// An iterator over paths on a cube, from a source face to a destination face. | |
23 | #[derive(Clone, Debug)] | |
24 | pub enum PathIter { | |
25 | Direct(Face), | |
26 | Indirect{ destination : Face, intermediate : AdjacentFaces, current : usize}, | |
27 | Exhausted, | |
28 | } | |
29 | ||
30 | impl std::iter::Iterator for PathIter { | |
31 | type Item = Path; | |
32 | ||
33 | fn next(&mut self) -> Option<Self::Item> { | |
34 | use PathIter::*; | |
35 | match self { | |
36 | &mut Exhausted => None, | |
37 | &mut Direct(destination) => { | |
38 | *self = Exhausted; | |
39 | Some(Path::Direct { destination }) | |
40 | }, | |
41 | &mut Indirect{destination, intermediate : ref i, ref mut current} => { | |
42 | if *current < i.len() { | |
43 | let intermediate = i[*current]; | |
44 | *current += 1; | |
45 | Some(Path::Indirect{ destination, intermediate }) | |
46 | } else { | |
47 | *self = Exhausted; | |
48 | None | |
49 | } | |
50 | } | |
51 | } | |
52 | } | |
53 | } | |
54 | ||
55 | ||
56 | impl Face { | |
3 | 57 | /// Returns an array of the four faces adjacent to `self` in the |
58 | /// order [left, right, down, up] in the `self`-relative unfolding. | |
0 | 59 | pub fn adjacent_faces(&self) -> AdjacentFaces { |
60 | match *self { | |
3 | 61 | F1 => [F3, F2, F4, F5], |
62 | F2 => [F4, F5, F1, F6], | |
63 | F3 => [F5, F4, F1, F6], | |
64 | F4 => [F3, F2, F1, F6], | |
65 | F5 => [F2, F3, F1, F6], | |
66 | F6 => [F3, F2, F4, F5], | |
0 | 67 | } |
68 | } | |
69 | ||
70 | /// Returns the face opposing `self`. | |
71 | pub fn opposing_face(&self) -> Face { | |
72 | match *self { | |
73 | F1 => F6, | |
74 | F2 => F3, | |
75 | F3 => F2, | |
3 | 76 | F4 => F5, |
0 | 77 | F5 => F4, |
78 | F6 => F1, | |
79 | } | |
80 | } | |
81 | ||
82 | /// Converts a point on an adjacent face to the coordinate system of `self`. | |
83 | pub fn convert_adjacent(&self, adjacent : Face, p: &Point) -> Option<Point> { | |
84 | let Loc([x, y]) = *p; | |
85 | let mk = |x, y| Some(Loc([x, y])); | |
86 | match adjacent { | |
87 | F1 => match *self { | |
88 | F2 => mk(y, x - 1.0), | |
89 | F3 => mk(1.0 - y, -x), | |
90 | F4 => mk(x, -y), | |
91 | F5 => mk(1.0 - x, y - 1.0), | |
92 | F1 => mk(x, y), | |
93 | F6 => None, | |
94 | }, | |
95 | F2 => match *self { | |
96 | F1 => mk(y + 1.0, x), | |
97 | F4 => mk(x + 1.0, y), | |
98 | F5 => mk(x - 1.0, y), | |
99 | F6 => mk(2.0 - y, x), | |
100 | F2 => mk(x, y), | |
101 | F3 => None, | |
102 | }, | |
103 | F3 => match *self { | |
104 | F1 => mk(-y, 1.0 - x), | |
105 | F4 => mk(x - 1.0, y), | |
106 | F5 => mk(x + 1.0, y), | |
107 | F6 => mk(y - 1.0, 1.0 - x), | |
108 | F3 => mk(x, y), | |
109 | F2 => None, | |
110 | }, | |
111 | F4 => match *self { | |
112 | F1 => mk(x, -y), | |
113 | F2 => mk(x - 1.0, y), | |
114 | F3 => mk(x + 1.0, y), | |
115 | F6 => mk(x, y - 1.0), | |
116 | F4 => mk(x, y), | |
117 | F5 => None, | |
118 | }, | |
119 | F5 => match *self { | |
120 | F1 => mk(1.0 -x, y + 1.0), | |
121 | F2 => mk(x + 1.0, y), | |
122 | F3 => mk(x - 1.0, y), | |
123 | F6 => mk(1.0 -x, 2.0 - y), | |
124 | F5 => mk(x, y), | |
125 | F4 => None, | |
126 | }, | |
127 | F6 => match *self { | |
128 | F2 => mk(y, 2.0 - x), | |
129 | F3 => mk(1.0 - y, x + 1.0), | |
130 | F4 => mk(x, y + 1.0), | |
131 | F5 => mk(1.0 - x, 2.0 - y), | |
132 | F6 => mk(x, y), | |
133 | F1 => None, | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
138 | /// Converts a point behind a path to the coordinate system of `self`. | |
139 | pub fn convert(&self, path : &Path, p: &Point) -> Point { | |
140 | use Path::*; | |
3 | 141 | //dbg!(*self, path); |
0 | 142 | match path { |
143 | &Direct{ destination : d} => self.convert_adjacent(d, p), | |
144 | &Indirect{ destination : d, intermediate : i } | |
3 | 145 | => {self.convert_adjacent(i, &i.convert_adjacent(d, p).unwrap())} |
0 | 146 | }.unwrap() |
147 | } | |
148 | ||
149 | ||
150 | /// Returns an iterator over all the paths from `self` to `other`. | |
151 | fn paths(&self, other : Face) -> PathIter { | |
3 | 152 | //dbg!(self, other); |
0 | 153 | if self.opposing_face() == other { |
154 | PathIter::Indirect { | |
155 | intermediate : self.adjacent_faces(), | |
156 | destination : other, | |
157 | current : 0 | |
158 | } | |
159 | } else { | |
160 | PathIter::Direct(other) | |
161 | } | |
162 | } | |
3 | 163 | |
164 | pub fn is_in_face(&self, p: &Point) -> bool { | |
165 | p.iter().map(|t| t.abs()).all(|t| 0.0 <= t && t <= 1.0) | |
166 | } | |
167 | ||
168 | pub fn find_crossing(&self, p : &Point) -> Face { | |
169 | let &Loc([x, y]) = p; | |
170 | use std::cmp::Ordering::*; | |
171 | let crossing = |t| match (0.0 <= t, t<=1.0) { | |
172 | (false, _) => Less, | |
173 | (_, false) => Greater, | |
174 | _ => Equal, | |
175 | }; | |
176 | ||
177 | // TODO: how to properly handle corners? Just throw an error? | |
178 | match (crossing(x), crossing(y)) { | |
179 | (Equal, Equal) => *self, | |
180 | (Less, _) => self.adjacent_faces()[0], | |
181 | (Greater, _) => self.adjacent_faces()[1], | |
182 | (Equal, Less) => self.adjacent_faces()[2], | |
183 | (Equal, Greater) => self.adjacent_faces()[3], | |
184 | } | |
185 | } | |
7 | 186 | |
187 | /// Get embedded 3D coordinates | |
188 | pub fn embedded_coords(&self, p : &Point) -> Loc<f64, 3> { | |
189 | let &Loc([x, y]) = p; | |
190 | Loc(match *self { | |
191 | F1 => [x, y, 0.0], | |
192 | F2 => [1.0, x, y], | |
193 | F3 => [0.0, 1.0-x, y], | |
194 | F4 => [x, 0.0, y], | |
195 | F5 => [1.0 - x, 1.0, y], | |
196 | F6 => [x, y, 1.0], | |
197 | }) | |
198 | } | |
0 | 199 | } |
200 | ||
201 | #[derive(Clone, Debug, PartialEq)] | |
202 | pub struct OnCube { | |
203 | face : Face, | |
204 | point : Point, | |
205 | } | |
206 | ||
5 | 207 | impl OnCube { |
7 | 208 | /// Creates a new point on the cube, given a face and face-relative coordinates |
209 | /// in [0, 1]^2 | |
210 | pub fn new(face : Face, point : Point) -> Self { | |
211 | assert!(face.is_in_face(&point)); | |
212 | OnCube { face, point } | |
213 | } | |
214 | ||
5 | 215 | /// Calculates both the logarithmic map and distance to another point |
216 | fn log_dist(&self, other : &Self) -> (<Self as ManifoldPoint>::Tangent, f64) { | |
217 | let mut best_len = f64::INFINITY; | |
218 | let mut best_tan = Loc([0.0, 0.0]); | |
219 | for path in self.face.paths(other.face) { | |
220 | let tan = self.face.convert(&path, &other.point) - &self.point; | |
221 | let len = tan.norm(L2); | |
222 | if len < best_len { | |
223 | best_tan = tan; | |
224 | best_len = len; | |
225 | } | |
226 | } | |
227 | (best_tan, best_len) | |
228 | } | |
229 | } | |
230 | ||
7 | 231 | |
232 | impl EmbeddedManifoldPoint for OnCube { | |
233 | type EmbeddedCoords = Loc<f64, 3>; | |
234 | ||
235 | /// Get embedded 3D coordinates | |
236 | fn embedded_coords(&self) -> Loc<f64, 3> { | |
237 | self.face.embedded_coords(&self.point) | |
238 | } | |
239 | } | |
240 | ||
0 | 241 | impl ManifoldPoint for OnCube { |
242 | type Tangent = Point; | |
243 | ||
8 | 244 | fn exp(self, tangent : &Self::Tangent) -> Self { |
3 | 245 | let mut face = self.face; |
246 | let mut point = self.point + tangent; | |
247 | loop { | |
248 | let next_face = face.find_crossing(&point); | |
249 | if next_face == face { | |
250 | break | |
251 | } | |
252 | point = next_face.convert_adjacent(face, &point).unwrap(); | |
253 | face = next_face; | |
254 | } | |
255 | OnCube { face, point } | |
0 | 256 | } |
257 | ||
258 | fn log(&self, other : &Self) -> Self::Tangent { | |
5 | 259 | self.log_dist(other).0 |
260 | } | |
261 | ||
262 | fn dist_to(&self, other : &Self) -> f64 { | |
263 | self.log_dist(other).1 | |
0 | 264 | } |
6
df9628092285
Add a zero function on manifolds
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
265 | |
df9628092285
Add a zero function on manifolds
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
266 | fn tangent_origin(&self) -> Self::Tangent { |
df9628092285
Add a zero function on manifolds
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
267 | Loc([0.0, 0.0]) |
df9628092285
Add a zero function on manifolds
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
268 | } |
0 | 269 | } |
270 | ||
271 | #[cfg(test)] | |
272 | mod tests { | |
273 | use super::*; | |
274 | ||
275 | #[test] | |
276 | fn log_test_adjacent() { | |
277 | let p1 = OnCube{ face : F1, point : Loc([0.5, 0.5])}; | |
278 | let p2 = OnCube{ face : F2, point : Loc([0.5, 0.5])}; | |
279 | ||
280 | assert_eq!(p1.log(&p2).norm(L2), 1.0); | |
281 | } | |
282 | ||
283 | #[test] | |
284 | fn log_test_opposing_equal() { | |
285 | let p1 = OnCube{ face : F1, point : Loc([0.5, 0.5])}; | |
286 | let p2 = OnCube{ face : F6, point : Loc([0.5, 0.5])}; | |
287 | ||
288 | assert_eq!(p1.log(&p2).norm(L2), 2.0); | |
289 | } | |
290 | ||
291 | #[test] | |
292 | fn log_test_opposing_unique_shortest() { | |
293 | let p1 = OnCube{ face : F1, point : Loc([0.3, 0.25])}; | |
294 | let p2 = OnCube{ face : F6, point : Loc([0.3, 0.25])}; | |
295 | ||
296 | assert_eq!(p1.log(&p2).norm(L2), 1.5); | |
297 | } | |
298 | } | |
299 |