Mon, 31 Mar 2025 21:02:35 -0500
arXiv link
| 13 | 1 | /*! |
| 2 | Implementation of the surface of the 3D cube as a [`ManifoldPoint`]. | |
| 3 | */ | |
| 0 | 4 | |
| 12 | 5 | use serde_repr::*; |
| 6 | use serde::Serialize; | |
| 0 | 7 | use alg_tools::loc::Loc; |
| 8 | use alg_tools::norms::{Norm, L2}; | |
| 56 | 9 | use alg_tools::impl_basic_space; |
|
37
d7cd14b8ccc0
Basic cylinder implementation
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
10 | use crate::manifold::{EmbeddedManifoldPoint, FacedManifoldPoint, ManifoldPoint}; |
| 0 | 11 | |
| 13 | 12 | /// All the difference faces of a [`OnCube`]. |
| 12 | 13 | #[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize_repr, Deserialize_repr)] |
| 14 | #[repr(u8)] | |
| 15 | pub enum Face {F1 = 1, F2 = 2, F3 = 3, F4 = 4, F5 = 5, F6 = 6} | |
| 0 | 16 | use Face::*; |
| 17 | ||
| 46 | 18 | /// General point in 2D |
| 0 | 19 | pub type Point = Loc<f64, 2>; |
| 20 | ||
| 46 | 21 | /// Types for faces adjacent to a given face. |
| 0 | 22 | pub type AdjacentFaces = [Face; 4]; |
| 23 | ||
| 46 | 24 | /// Types of paths on a cube |
| 12 | 25 | #[derive(Clone, Debug, Serialize)] |
| 0 | 26 | pub enum Path { |
| 46 | 27 | /// Direct path from an unindicated source face to a `destination` face. |
| 0 | 28 | Direct { destination : Face }, |
| 46 | 29 | /// Indirect path from an unindicated source face to a `destination` face, |
| 30 | /// via an `intermediate` face. | |
| 0 | 31 | Indirect { destination : Face, intermediate : Face }, |
| 32 | } | |
| 33 | ||
| 34 | /// An iterator over paths on a cube, from a source face to a destination face. | |
| 35 | #[derive(Clone, Debug)] | |
|
34
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
36 | pub enum PathIter { |
| 46 | 37 | /// Direct path to a destination. |
|
34
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
38 | Same { |
| 46 | 39 | /// Deistination face |
|
34
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
40 | destination : Face, |
| 46 | 41 | /// Indicator whether the only possible [`Path::Direct`] has already been returned. |
|
34
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
42 | exhausted : bool |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
43 | }, |
| 46 | 44 | /// Path via several possible intermedite faces. |
| 45 | /// This is used to generate several [`Path::Indirect`]. | |
|
34
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
46 | Indirect { |
| 46 | 47 | /// Destination face |
|
34
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
48 | destination : Face, |
| 46 | 49 | /// Possible intermediate faces |
|
34
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
50 | intermediate : AdjacentFaces, |
| 46 | 51 | /// Intermediate face index counter. |
|
34
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
52 | current : usize |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
53 | } |
| 0 | 54 | } |
| 55 | ||
| 56 | impl std::iter::Iterator for PathIter { | |
| 57 | type Item = Path; | |
| 58 | ||
| 59 | fn next(&mut self) -> Option<Self::Item> { | |
|
34
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
60 | match *self { |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
61 | PathIter::Same { destination, ref mut exhausted } => { |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
62 | if !*exhausted { |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
63 | *exhausted = true; |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
64 | return Some(Path::Direct { destination }) |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
65 | } |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
66 | None |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
67 | }, |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
68 | PathIter::Indirect { destination, intermediate : ref i, ref mut current } => { |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
69 | while *current < i.len() { |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
70 | let intermediate = i[*current]; |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
71 | *current += 1; |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
72 | if intermediate == destination { |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
73 | return Some(Path::Direct { destination }) |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
74 | } else if intermediate != destination.opposing_face() { |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
75 | return Some(Path::Indirect{ destination, intermediate }) |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
76 | } |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
77 | // Paths should never go through a face opposing the destination. |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
78 | } |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
79 | None |
| 0 | 80 | } |
| 81 | } | |
| 82 | } | |
| 83 | } | |
| 84 | ||
| 11 | 85 | impl std::fmt::Display for Face { |
| 86 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |
| 87 | let s = match *self { | |
| 88 | F1 => "F1", | |
| 89 | F2 => "F2", | |
| 90 | F3 => "F3", | |
| 91 | F4 => "F4", | |
| 92 | F5 => "F5", | |
| 93 | F6 => "F6", | |
| 94 | }; | |
| 95 | write!(f, "{}", s) | |
| 96 | } | |
| 97 | } | |
| 0 | 98 | |
| 99 | impl Face { | |
| 11 | 100 | /// Return an aray of all faces |
| 101 | pub fn all() -> [Face; 6] { | |
| 102 | [F1, F2, F3, F4, F5, F6] | |
| 103 | } | |
| 104 | ||
| 3 | 105 | /// Returns an array of the four faces adjacent to `self` in the |
| 106 | /// order [left, right, down, up] in the `self`-relative unfolding. | |
| 0 | 107 | pub fn adjacent_faces(&self) -> AdjacentFaces { |
| 108 | match *self { | |
| 3 | 109 | F1 => [F3, F2, F4, F5], |
| 110 | F2 => [F4, F5, F1, F6], | |
| 111 | F3 => [F5, F4, F1, F6], | |
| 112 | F4 => [F3, F2, F1, F6], | |
| 113 | F5 => [F2, F3, F1, F6], | |
| 114 | F6 => [F3, F2, F4, F5], | |
| 0 | 115 | } |
| 116 | } | |
| 117 | ||
| 118 | /// Returns the face opposing `self`. | |
| 119 | pub fn opposing_face(&self) -> Face { | |
| 120 | match *self { | |
| 121 | F1 => F6, | |
| 122 | F2 => F3, | |
| 123 | F3 => F2, | |
| 3 | 124 | F4 => F5, |
| 0 | 125 | F5 => F4, |
| 126 | F6 => F1, | |
| 127 | } | |
| 128 | } | |
| 129 | ||
| 130 | /// Converts a point on an adjacent face to the coordinate system of `self`. | |
| 131 | pub fn convert_adjacent(&self, adjacent : Face, p: &Point) -> Option<Point> { | |
| 132 | let Loc([x, y]) = *p; | |
| 133 | let mk = |x, y| Some(Loc([x, y])); | |
| 134 | match adjacent { | |
| 135 | F1 => match *self { | |
| 136 | F2 => mk(y, x - 1.0), | |
| 137 | F3 => mk(1.0 - y, -x), | |
| 138 | F4 => mk(x, -y), | |
| 139 | F5 => mk(1.0 - x, y - 1.0), | |
| 140 | F1 => mk(x, y), | |
| 141 | F6 => None, | |
| 142 | }, | |
| 143 | F2 => match *self { | |
| 144 | F1 => mk(y + 1.0, x), | |
| 145 | F4 => mk(x + 1.0, y), | |
| 146 | F5 => mk(x - 1.0, y), | |
| 147 | F6 => mk(2.0 - y, x), | |
| 148 | F2 => mk(x, y), | |
| 149 | F3 => None, | |
| 150 | }, | |
| 151 | F3 => match *self { | |
| 152 | F1 => mk(-y, 1.0 - x), | |
| 153 | F4 => mk(x - 1.0, y), | |
| 154 | F5 => mk(x + 1.0, y), | |
| 155 | F6 => mk(y - 1.0, 1.0 - x), | |
| 156 | F3 => mk(x, y), | |
| 157 | F2 => None, | |
| 158 | }, | |
| 159 | F4 => match *self { | |
| 160 | F1 => mk(x, -y), | |
| 161 | F2 => mk(x - 1.0, y), | |
| 162 | F3 => mk(x + 1.0, y), | |
| 163 | F6 => mk(x, y - 1.0), | |
| 164 | F4 => mk(x, y), | |
| 165 | F5 => None, | |
| 166 | }, | |
| 167 | F5 => match *self { | |
| 168 | F1 => mk(1.0 -x, y + 1.0), | |
| 169 | F2 => mk(x + 1.0, y), | |
| 170 | F3 => mk(x - 1.0, y), | |
| 171 | F6 => mk(1.0 -x, 2.0 - y), | |
| 172 | F5 => mk(x, y), | |
| 173 | F4 => None, | |
| 174 | }, | |
| 175 | F6 => match *self { | |
| 176 | F2 => mk(y, 2.0 - x), | |
| 177 | F3 => mk(1.0 - y, x + 1.0), | |
| 178 | F4 => mk(x, y + 1.0), | |
| 179 | F5 => mk(1.0 - x, 2.0 - y), | |
| 180 | F6 => mk(x, y), | |
| 181 | F1 => None, | |
| 182 | } | |
| 183 | } | |
| 184 | } | |
| 185 | ||
| 186 | /// Converts a point behind a path to the coordinate system of `self`. | |
| 187 | pub fn convert(&self, path : &Path, p: &Point) -> Point { | |
| 188 | use Path::*; | |
| 3 | 189 | //dbg!(*self, path); |
| 0 | 190 | match path { |
| 191 | &Direct{ destination : d} => self.convert_adjacent(d, p), | |
| 192 | &Indirect{ destination : d, intermediate : i } | |
| 3 | 193 | => {self.convert_adjacent(i, &i.convert_adjacent(d, p).unwrap())} |
| 0 | 194 | }.unwrap() |
| 195 | } | |
| 196 | ||
| 197 | ||
| 198 | /// Returns an iterator over all the paths from `self` to `other`. | |
| 199 | fn paths(&self, other : Face) -> PathIter { | |
|
34
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
200 | if other == *self { |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
201 | PathIter::Same { |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
202 | destination : other, |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
203 | exhausted : false |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
204 | } |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
205 | } else { |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
206 | PathIter::Indirect { |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
207 | intermediate : self.adjacent_faces(), |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
208 | destination : other, |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
209 | current : 0 |
|
aa6129697116
Same face path optimisation / possible fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
30
diff
changeset
|
210 | } |
| 0 | 211 | } |
| 212 | } | |
| 3 | 213 | |
| 13 | 214 | /// Indicates whether an unfolded point `p` is on this face, i.e., |
| 46 | 215 | /// has coordinates in $\[0,1\]^2$. |
| 3 | 216 | pub fn is_in_face(&self, p: &Point) -> bool { |
|
14
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
217 | p.iter().all(|t| 0.0 <= *t && *t <= 1.0) |
| 3 | 218 | } |
| 219 | ||
|
14
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
220 | /// Given an unfolded point `p` and a destination point `d` in unfolded coordinates, |
|
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
221 | /// but possibly outside this face, find the crossing point of the line between |
|
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
222 | /// `p` and `d` on an edge of (`self`). Return the point and the edge presented |
|
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
223 | /// by an adjacent face. |
| 13 | 224 | /// |
|
14
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
225 | /// Crossing at corners is decided arbitrarily. |
|
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
226 | pub fn find_crossing(&self, p :& Point, d : &Point) -> (Face, Point) { |
|
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
227 | //assert!(self.is_in_face(p)); |
|
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
228 | |
|
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
229 | if self.is_in_face(d) { |
|
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
230 | return (*self, *p) |
|
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
231 | } |
|
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
232 | |
|
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
233 | use std::cmp::Ordering::*; |
|
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
234 | |
| 3 | 235 | let &Loc([x, y]) = p; |
|
14
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
236 | let &Loc([xd, yd]) = d; |
|
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
237 | let tx = xd - x; |
|
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
238 | let ty = yd - y; |
|
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
239 | |
|
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
240 | // Move towards tangent as (x + s tx, y + s ty) for the largest s<=1.0 for which |
|
15
2f4af30af476
Simplify crossing calculations
Tuomo Valkonen <tuomov@iki.fi>
parents:
14
diff
changeset
|
241 | // both coordinates is within [0, 1]. Also gives the direction of move along |
|
2f4af30af476
Simplify crossing calculations
Tuomo Valkonen <tuomov@iki.fi>
parents:
14
diff
changeset
|
242 | // each coordinate. |
|
2f4af30af476
Simplify crossing calculations
Tuomo Valkonen <tuomov@iki.fi>
parents:
14
diff
changeset
|
243 | let (sx, dirx) = match tx.partial_cmp(&0.0) { |
|
2f4af30af476
Simplify crossing calculations
Tuomo Valkonen <tuomov@iki.fi>
parents:
14
diff
changeset
|
244 | Some(Less) => (1.0f64.min(-x/tx), Less), |
|
2f4af30af476
Simplify crossing calculations
Tuomo Valkonen <tuomov@iki.fi>
parents:
14
diff
changeset
|
245 | Some(Greater) => (1.0f64.min((1.0-x)/tx), Greater), |
|
2f4af30af476
Simplify crossing calculations
Tuomo Valkonen <tuomov@iki.fi>
parents:
14
diff
changeset
|
246 | _ => (1.0, Equal) |
|
14
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
247 | }; |
|
15
2f4af30af476
Simplify crossing calculations
Tuomo Valkonen <tuomov@iki.fi>
parents:
14
diff
changeset
|
248 | let (sy, diry) = match ty.partial_cmp(&0.0) { |
|
2f4af30af476
Simplify crossing calculations
Tuomo Valkonen <tuomov@iki.fi>
parents:
14
diff
changeset
|
249 | Some(Less) => (1.0f64.min(-y/ty), Less), |
|
2f4af30af476
Simplify crossing calculations
Tuomo Valkonen <tuomov@iki.fi>
parents:
14
diff
changeset
|
250 | Some(Greater) => (1.0f64.min((1.0-y)/ty), Greater), |
|
2f4af30af476
Simplify crossing calculations
Tuomo Valkonen <tuomov@iki.fi>
parents:
14
diff
changeset
|
251 | _ => (1.0, Equal), |
| 3 | 252 | }; |
| 253 | ||
| 254 | // TODO: how to properly handle corners? Just throw an error? | |
|
15
2f4af30af476
Simplify crossing calculations
Tuomo Valkonen <tuomov@iki.fi>
parents:
14
diff
changeset
|
255 | let (crossing, c) = match (sx < sy, dirx, diry) { |
|
2f4af30af476
Simplify crossing calculations
Tuomo Valkonen <tuomov@iki.fi>
parents:
14
diff
changeset
|
256 | // x move is less than y move, so crossing is either on left or right edge |
|
2f4af30af476
Simplify crossing calculations
Tuomo Valkonen <tuomov@iki.fi>
parents:
14
diff
changeset
|
257 | (true, Less, _) => (self.adjacent_faces()[0], sx), |
|
2f4af30af476
Simplify crossing calculations
Tuomo Valkonen <tuomov@iki.fi>
parents:
14
diff
changeset
|
258 | (true, Greater, _) => (self.adjacent_faces()[1], sx), |
|
2f4af30af476
Simplify crossing calculations
Tuomo Valkonen <tuomov@iki.fi>
parents:
14
diff
changeset
|
259 | (true, Equal, _) => (*self, sx), |
|
2f4af30af476
Simplify crossing calculations
Tuomo Valkonen <tuomov@iki.fi>
parents:
14
diff
changeset
|
260 | // y move is less than x move, so crossing is either on bottom or top edge |
|
2f4af30af476
Simplify crossing calculations
Tuomo Valkonen <tuomov@iki.fi>
parents:
14
diff
changeset
|
261 | (false, _, Less) => (self.adjacent_faces()[2], sy), |
|
2f4af30af476
Simplify crossing calculations
Tuomo Valkonen <tuomov@iki.fi>
parents:
14
diff
changeset
|
262 | (false, _, Greater) => (self.adjacent_faces()[3], sy), |
|
2f4af30af476
Simplify crossing calculations
Tuomo Valkonen <tuomov@iki.fi>
parents:
14
diff
changeset
|
263 | (false, _, Equal) => (*self, sy), |
|
14
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
264 | }; |
|
15
2f4af30af476
Simplify crossing calculations
Tuomo Valkonen <tuomov@iki.fi>
parents:
14
diff
changeset
|
265 | (crossing, Loc([x + c*tx, y + c*ty])) |
| 3 | 266 | } |
| 7 | 267 | |
| 268 | /// Get embedded 3D coordinates | |
| 269 | pub fn embedded_coords(&self, p : &Point) -> Loc<f64, 3> { | |
| 270 | let &Loc([x, y]) = p; | |
| 271 | Loc(match *self { | |
| 272 | F1 => [x, y, 0.0], | |
| 273 | F2 => [1.0, x, y], | |
| 274 | F3 => [0.0, 1.0-x, y], | |
| 275 | F4 => [x, 0.0, y], | |
| 276 | F5 => [1.0 - x, 1.0, y], | |
| 277 | F6 => [x, y, 1.0], | |
| 278 | }) | |
| 279 | } | |
| 0 | 280 | } |
| 281 | ||
| 46 | 282 | /// Point on a the surface of the unit cube $\[0,1\]^3$. |
| 12 | 283 | #[derive(Clone, Debug, PartialEq, Serialize)] |
| 0 | 284 | pub struct OnCube { |
| 285 | face : Face, | |
| 286 | point : Point, | |
| 287 | } | |
| 288 | ||
| 5 | 289 | impl OnCube { |
| 7 | 290 | /// Creates a new point on the cube, given a face and face-relative coordinates |
| 291 | /// in [0, 1]^2 | |
| 292 | pub fn new(face : Face, point : Point) -> Self { | |
| 293 | assert!(face.is_in_face(&point)); | |
| 294 | OnCube { face, point } | |
| 295 | } | |
| 296 | ||
| 5 | 297 | /// Calculates both the logarithmic map and distance to another point |
| 298 | fn log_dist(&self, other : &Self) -> (<Self as ManifoldPoint>::Tangent, f64) { | |
| 299 | let mut best_len = f64::INFINITY; | |
| 300 | let mut best_tan = Loc([0.0, 0.0]); | |
| 301 | for path in self.face.paths(other.face) { | |
| 302 | let tan = self.face.convert(&path, &other.point) - &self.point; | |
| 303 | let len = tan.norm(L2); | |
| 304 | if len < best_len { | |
| 305 | best_tan = tan; | |
| 306 | best_len = len; | |
| 307 | } | |
| 308 | } | |
| 309 | (best_tan, best_len) | |
| 310 | } | |
|
37
d7cd14b8ccc0
Basic cylinder implementation
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
311 | } |
| 12 | 312 | |
|
37
d7cd14b8ccc0
Basic cylinder implementation
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
313 | impl FacedManifoldPoint for OnCube { |
|
d7cd14b8ccc0
Basic cylinder implementation
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
314 | type Face = Face; |
| 13 | 315 | /// Returns the face of this point. |
|
37
d7cd14b8ccc0
Basic cylinder implementation
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
316 | fn face(&self) -> Face { |
| 12 | 317 | self.face |
| 318 | } | |
| 5 | 319 | } |
| 320 | ||
| 7 | 321 | |
| 322 | impl EmbeddedManifoldPoint for OnCube { | |
| 323 | type EmbeddedCoords = Loc<f64, 3>; | |
| 324 | ||
| 325 | /// Get embedded 3D coordinates | |
| 326 | fn embedded_coords(&self) -> Loc<f64, 3> { | |
| 327 | self.face.embedded_coords(&self.point) | |
| 328 | } | |
| 329 | } | |
| 330 | ||
| 0 | 331 | impl ManifoldPoint for OnCube { |
| 332 | type Tangent = Point; | |
| 333 | ||
| 8 | 334 | fn exp(self, tangent : &Self::Tangent) -> Self { |
| 3 | 335 | let mut face = self.face; |
|
14
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
336 | let mut point = self.point; |
|
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
337 | let mut dest = self.point + tangent; |
| 3 | 338 | loop { |
|
14
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
339 | let (next_face, cross) = face.find_crossing(&point, &dest); |
| 3 | 340 | if next_face == face { |
| 341 | break | |
| 342 | } | |
|
14
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
343 | point = next_face.convert_adjacent(face, &cross).unwrap(); |
|
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
344 | dest = next_face.convert_adjacent(face, &dest).unwrap(); |
| 3 | 345 | face = next_face; |
| 346 | } | |
|
14
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
347 | OnCube { face, point : dest } |
| 0 | 348 | } |
| 349 | ||
| 350 | fn log(&self, other : &Self) -> Self::Tangent { | |
| 5 | 351 | self.log_dist(other).0 |
| 352 | } | |
| 353 | ||
| 354 | fn dist_to(&self, other : &Self) -> f64 { | |
| 355 | self.log_dist(other).1 | |
| 0 | 356 | } |
|
6
df9628092285
Add a zero function on manifolds
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
357 | |
|
df9628092285
Add a zero function on manifolds
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
358 | fn tangent_origin(&self) -> Self::Tangent { |
|
df9628092285
Add a zero function on manifolds
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
359 | Loc([0.0, 0.0]) |
|
df9628092285
Add a zero function on manifolds
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
360 | } |
| 0 | 361 | } |
| 362 | ||
| 56 | 363 | impl_basic_space!(OnCube); |
| 364 | ||
| 365 | ||
| 0 | 366 | #[cfg(test)] |
| 367 | mod tests { | |
| 368 | use super::*; | |
| 369 | ||
| 46 | 370 | /// Tests that the distancse between the centers of all the faces are correctly calculated. |
| 0 | 371 | #[test] |
| 29 | 372 | fn center_distance() { |
| 373 | let center = Loc([0.5, 0.5]); | |
| 374 | ||
| 375 | for f1 in Face::all() { | |
| 376 | let p1 = OnCube { face : f1, point : center.clone() }; | |
| 377 | for f2 in Face::all() { | |
| 378 | let p2 = OnCube { face : f2, point : center.clone() }; | |
| 379 | if f1==f2 { | |
| 380 | assert_eq!(p1.dist_to(&p2), 0.0); | |
| 381 | } else if f1.opposing_face()==f2 { | |
| 382 | assert_eq!(p1.dist_to(&p2), 2.0); | |
| 383 | } else { | |
| 384 | assert_eq!(p1.dist_to(&p2), 1.0); | |
| 385 | } | |
| 386 | } | |
| 387 | } | |
| 388 | } | |
| 389 | ||
| 46 | 390 | /// Tests that the distances between points on the boundaries of distinct faces are |
| 391 | /// correctly calculated. | |
| 29 | 392 | #[test] |
| 393 | fn boundary_distance() { | |
| 394 | let left = Loc([0.0, 0.5]); | |
| 395 | let right = Loc([1.0, 0.5]); | |
| 396 | let down = Loc([0.5, 0.0]); | |
| 397 | let up = Loc([0.5, 1.0]); | |
| 398 | let center = Loc([0.5, 0.5]); | |
| 399 | ||
| 400 | for f1 in Face::all() { | |
| 401 | let pl = OnCube { face : f1, point : left.clone() }; | |
| 402 | let pr = OnCube { face : f1, point : right.clone() }; | |
| 403 | let pd = OnCube { face : f1, point : down.clone() }; | |
| 404 | let pu = OnCube { face : f1, point : up.clone() }; | |
| 405 | let a = f1.adjacent_faces(); | |
| 406 | let al = OnCube { face : a[0], point : center.clone() }; | |
| 407 | let ar = OnCube { face : a[1], point : center.clone() }; | |
| 408 | let ad = OnCube { face : a[2], point : center.clone() }; | |
| 409 | let au = OnCube { face : a[3], point : center.clone() }; | |
| 410 | let ao = OnCube { face : f1.opposing_face(), point : center.clone() }; | |
| 411 | ||
| 412 | assert_eq!(pl.dist_to(&al), 0.5); | |
| 413 | assert_eq!(pr.dist_to(&ar), 0.5); | |
| 414 | assert_eq!(pd.dist_to(&ad), 0.5); | |
| 415 | assert_eq!(pu.dist_to(&au), 0.5); | |
| 416 | assert_eq!(pl.dist_to(&ao), 1.5); | |
| 417 | assert_eq!(pr.dist_to(&ao), 1.5); | |
| 418 | assert_eq!(pd.dist_to(&ao), 1.5); | |
| 419 | assert_eq!(pu.dist_to(&ao), 1.5); | |
| 420 | } | |
| 421 | } | |
| 422 | ||
| 423 | ||
| 46 | 424 | /// Tests that the conversions between the coordinate systems of each face is working correctly. |
| 29 | 425 | #[test] |
| 10 | 426 | fn convert_adjacent() { |
| 427 | let point = Loc([0.4, 0.6]); | |
| 428 | ||
|
14
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
429 | for f1 in Face::all() { |
|
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
430 | for f2 in Face::all() { |
| 10 | 431 | println!("{:?}-{:?}", f1, f2); |
| 432 | match f1.convert_adjacent(f2, &point) { | |
| 433 | None => assert_eq!(f2.opposing_face(), f1), | |
| 434 | Some(q) => { | |
| 435 | match f2.convert_adjacent(f1, &q) { | |
| 436 | None => assert_eq!(f1.opposing_face(), f2), | |
| 437 | Some(p) => assert!((p-&point).norm(L2) < 1e-9), | |
| 438 | } | |
| 439 | } | |
| 440 | } | |
| 441 | } | |
| 442 | } | |
| 443 | } | |
| 444 | ||
| 445 | // This will fail, as different return path does not guarantee | |
| 446 | // that a point outside the face will be returned to its point of origin. | |
| 447 | // #[test] | |
| 448 | // fn convert_paths() { | |
| 449 | // let point = Loc([0.4, 0.6]); | |
| 450 | ||
|
14
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
451 | // for f1 in Face::all() { |
|
d0f20c6cb49c
Fixes to find_crossing and exp
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
452 | // for f2 in Face::all() { |
| 10 | 453 | // for p1 in f2.paths(f1) { |
| 454 | // for p2 in f1.paths(f2) { | |
| 455 | // println!("{:?}-{:?}; {:?} {:?}", f1, f2, p1, p2); | |
| 456 | // let v = &f2.convert(&p1, &point); | |
| 457 | // let q = f1.convert(&p2, v); | |
| 458 | // assert!((q-&point).norm(L2) < 1e-9, | |
| 459 | // "norm({}-{}) ≥ 1e-9 (dest {})", q, &point, &v); | |
| 460 | // } | |
| 461 | // } | |
| 462 | // } | |
| 463 | // } | |
| 464 | // } | |
| 465 | ||
| 46 | 466 | /// Tests that the logarithmic map is working correctly between adjacent faces. |
| 10 | 467 | #[test] |
| 468 | fn log_adjacent() { | |
| 0 | 469 | let p1 = OnCube{ face : F1, point : Loc([0.5, 0.5])}; |
| 470 | let p2 = OnCube{ face : F2, point : Loc([0.5, 0.5])}; | |
| 471 | ||
| 472 | assert_eq!(p1.log(&p2).norm(L2), 1.0); | |
| 473 | } | |
| 474 | ||
| 46 | 475 | /// Tests that the logarithmic map is working correctly between opposing faces. |
| 0 | 476 | #[test] |
| 10 | 477 | fn log_opposing_equal() { |
| 0 | 478 | let p1 = OnCube{ face : F1, point : Loc([0.5, 0.5])}; |
| 479 | let p2 = OnCube{ face : F6, point : Loc([0.5, 0.5])}; | |
| 480 | ||
| 481 | assert_eq!(p1.log(&p2).norm(L2), 2.0); | |
| 482 | } | |
| 483 | ||
| 46 | 484 | /// Tests that the logarithmic map is working correctly between opposing faces when there |
| 485 | /// is a unique shortest geodesic. | |
| 0 | 486 | #[test] |
| 10 | 487 | fn log_opposing_unique_shortest() { |
| 0 | 488 | let p1 = OnCube{ face : F1, point : Loc([0.3, 0.25])}; |
| 489 | let p2 = OnCube{ face : F6, point : Loc([0.3, 0.25])}; | |
| 490 | ||
| 491 | assert_eq!(p1.log(&p2).norm(L2), 1.5); | |
| 492 | } | |
| 493 | } | |
| 494 |