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