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