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