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