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