Fri, 06 Dec 2024 15:07:28 -0500
Rust edition = 2024
37 | 1 | /*! |
2 | Implementation of the surface of a 3D cylinder as a [`ManifoldPoint`]. | |
3 | */ | |
4 | ||
38
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
5 | use alg_tools::euclidean::{Euclidean, Dot}; |
37 | 6 | use serde_repr::*; |
7 | use serde::{Serialize, Deserialize}; | |
8 | use alg_tools::loc::Loc; | |
9 | use alg_tools::norms::{Norm, L2}; | |
10 | use alg_tools::types::Float; | |
11 | use crate::manifold::{ManifoldPoint, EmbeddedManifoldPoint, FacedManifoldPoint}; | |
12 | use crate::newton::{newton_sym1x1, newton_sym2x2}; | |
13 | ||
14 | /// Angle | |
15 | pub type Angle = f64; | |
16 | ||
17 | /// Cylindrical coordinates in ℝ^3 | |
18 | #[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)] | |
19 | pub struct CylCoords { | |
20 | pub r : f64, | |
21 | pub angle : Angle, | |
22 | pub z : f64 | |
23 | } | |
24 | ||
25 | impl CylCoords { | |
46 | 26 | /// Convert to cartesian coordinates. |
37 | 27 | #[inline] |
28 | pub fn to_cartesian(&self) -> Loc<f64, 3> { | |
29 | let &CylCoords{r, angle, z} = self; | |
30 | [r * angle.cos(), r * angle.sin(), z].into() | |
31 | } | |
32 | ||
46 | 33 | /// Form cylindrical coordinates from cartesian coordinates. |
37 | 34 | #[inline] |
35 | #[allow(dead_code)] | |
36 | pub fn from_cartesian(coords : Loc<f64, 3>) -> Self { | |
37 | let [x, y, z] = coords.into(); | |
38 | let r = (x*x + y*y).sqrt(); | |
39 | let angle = y.atan2(x); | |
40 | CylCoords {r, angle, z} | |
41 | } | |
42 | } | |
43 | ||
44 | /// Coordinates on a cap | |
45 | #[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)] | |
46 | pub struct CapPoint { | |
47 | pub r : f64, | |
48 | pub angle : Angle | |
49 | } | |
50 | ||
51 | impl CapPoint { | |
52 | #[inline] | |
53 | /// Convert to cylindrical coordinates given z coordinate | |
54 | fn cyl_coords(&self, z : f64) -> CylCoords { | |
55 | let CapPoint { r, angle } = *self; | |
56 | CylCoords { r, angle, z } | |
57 | } | |
58 | ||
59 | #[inline] | |
60 | /// Convert to cylindrical coordinates given z coordinate | |
38
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
61 | fn to_cartesian(&self) -> Loc<f64, 2> { |
37 | 62 | let CapPoint { r, angle } = *self; |
63 | [r * angle.cos(), r * angle.sin()].into() | |
64 | } | |
65 | ||
66 | #[inline] | |
67 | #[allow(dead_code)] | |
68 | /// Convert to cylindrical coordinates given z coordinate | |
69 | fn from_cartesian(coords : Loc<f64, 2>) -> Self { | |
70 | let [x, y] = coords.into(); | |
71 | let r = (x*x + y*y).sqrt(); | |
38
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
72 | let angle = normalise_angle(y.atan2(x)); |
37 | 73 | CapPoint { r, angle } |
74 | } | |
75 | ||
76 | #[inline] | |
77 | /// Calculate the vector between two points on the cap | |
78 | fn log(&self, other : &CapPoint) -> Loc<f64, 2> { | |
38
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
79 | other.to_cartesian() - self.to_cartesian() |
37 | 80 | } |
81 | ||
82 | #[inline] | |
83 | /// Calculate partial exponential map until boundary. | |
84 | /// Returns the final point within the cap as well as a remaining tangent on | |
85 | /// the side of the cylinder, if `t` wasn't fully used. | |
86 | fn partial_exp(&self, r : f64, t : &Tangent) -> (CapPoint, Option<Tangent>) { | |
38
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
87 | // |p + s t| <= r |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
88 | // |p|^2 + s^2 |t|^2 + 2s<p,t> = r^2 |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
89 | let p = self.to_cartesian(); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
90 | let np2 = p.norm2_squared(); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
91 | let nt2 = t.norm2_squared(); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
92 | let r2 = r * r; |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
93 | let d = p.dot(t); |
39 | 94 | assert!(np2 <= r2 + f64::EPSILON, "‖{p}‖ = {} > {r}", np2.sqrt()); |
38
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
95 | let s = (-d + (d*d + nt2 * (r2 - np2)).sqrt()) / nt2; |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
96 | if s < 1.0 { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
97 | (Self::from_cartesian(p + s * t), Some((1.0-s) * t)) |
37 | 98 | } else { |
38
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
99 | (Self::from_cartesian(p + t), None) |
37 | 100 | } |
101 | } | |
102 | ||
103 | #[inline] | |
104 | /// Convert tangent from side tangent to cap tangent | |
105 | fn tangent_from_side(&self, top : bool, t : Tangent) -> Tangent { | |
106 | if top { | |
107 | // The angle is such that down would be rotated to self.angle, counterclockwise | |
108 | // The new tangent is R[down + t] - R[down] = Rt. | |
40
1d865db9d3e4
Move rotate and reflect to alg_tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
109 | t.rotate(self.angle+f64::PI/2.0) |
37 | 110 | } else { |
111 | // The angle is such that up would be rotated to self.angle, clockwise | |
40
1d865db9d3e4
Move rotate and reflect to alg_tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
112 | t.reflect_y().rotate(self.angle+f64::PI/2.0) |
37 | 113 | } |
114 | } | |
115 | ||
116 | #[inline] | |
117 | /// Convert tangent from cap tangent to tangent tangent | |
118 | fn tangent_to_side(&self, top : bool, t : Tangent) -> Tangent { | |
119 | if top { | |
120 | // The angle is such that self.angle would be rotated to down, clockwise | |
40
1d865db9d3e4
Move rotate and reflect to alg_tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
121 | t.rotate(-self.angle-f64::PI/2.0) |
37 | 122 | } else { |
123 | // The angle is such that self.angle would be rotated to up, counterclockwise | |
40
1d865db9d3e4
Move rotate and reflect to alg_tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
124 | t.rotate(-self.angle-f64::PI/2.0).reflect_y() |
37 | 125 | } |
126 | } | |
127 | } | |
128 | ||
129 | /// Coordinates on a side | |
130 | #[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)] | |
131 | pub struct SidePoint { | |
132 | pub z : f64, | |
133 | pub angle : Angle | |
134 | } | |
135 | ||
46 | 136 | /// Calculates the difference between two angles, normalised to [–π, π]. |
37 | 137 | #[inline] |
138 | fn anglediff(mut φ1 : f64, mut φ2 : f64) -> f64 { | |
139 | let π = f64::PI; | |
140 | φ1 = normalise_angle(φ1); | |
141 | φ2 = normalise_angle(φ2); | |
142 | let α = φ2 - φ1; | |
143 | if α >= 0.0 { | |
144 | if α <= π { | |
145 | α | |
146 | } else { | |
147 | α - 2.0 * π | |
148 | } | |
149 | } else { | |
150 | if α >= -π { | |
151 | α | |
152 | } else { | |
153 | 2.0 * π + α | |
154 | } | |
155 | } | |
156 | } | |
157 | ||
46 | 158 | /// Normalises an angle to [0, 2π]. |
37 | 159 | #[inline] |
160 | pub fn normalise_angle(φ : f64) -> f64 { | |
161 | let π = f64::PI; | |
162 | φ.rem_euclid(2.0 * π) | |
163 | } | |
164 | ||
165 | impl SidePoint { | |
166 | #[inline] | |
167 | /// Convert to cylindrical coordinates given radius | |
168 | fn cyl_coords(&self, r : f64) -> CylCoords { | |
169 | let SidePoint { z, angle } = *self; | |
170 | CylCoords { r, angle, z } | |
171 | } | |
172 | ||
173 | #[inline] | |
174 | /// Calculate tangent vector between two points on the side, given radius | |
175 | fn log(&self, r : f64, other : &SidePoint) -> Loc<f64, 2> { | |
176 | let SidePoint{ z : z1, angle : angle1 } = *self; | |
177 | let SidePoint{ z : z2, angle : angle2 } = *other; | |
178 | let φ = anglediff(angle1, angle2); | |
179 | // TODO: is this correct? | |
180 | [r*φ, z2-z1].into() | |
181 | } | |
182 | ||
183 | #[inline] | |
184 | /// Calculate partial exponential map under boundary | |
185 | /// Returns a point on the next face, as well as a remaining tangent on | |
186 | /// the side of the cylinder, if `t` wasn't fully used. | |
187 | fn partial_exp(&self, r : f64, (a, b) : (f64, f64), t : &Tangent) | |
188 | -> (SidePoint, Option<Tangent>) | |
189 | { | |
190 | assert!(a <= self.z && self.z <= b); | |
191 | let Loc([_, h]) = *t; | |
192 | let s = if h > 0.0 { | |
193 | ((b - self.z)/h).min(1.0) | |
194 | } else if h < 0.0 { | |
195 | ((a - self.z)/h).min(1.0) | |
196 | } else { | |
197 | 1.0 | |
198 | }; | |
199 | let d = t * s; | |
200 | let p = self.unflatten(r, d); | |
201 | if s < 1.0 { | |
202 | (p, Some(t - d)) | |
203 | } else { | |
204 | (p, None) | |
205 | } | |
206 | } | |
207 | ||
208 | #[inline] | |
209 | /// Unflattens another point in the local coordinate system of self | |
210 | fn unflatten(&self, r : f64, Loc([v, h]) : Loc<f64, 2>) -> Self { | |
211 | SidePoint{ z : self.z + h, angle : normalise_angle(self.angle + v / r) } | |
212 | } | |
213 | ||
214 | } | |
215 | ||
46 | 216 | /// Point on some [`Cylinder`] |
37 | 217 | #[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)] |
218 | pub enum Point { | |
219 | Top(CapPoint), | |
220 | Bottom(CapPoint), | |
221 | Side(SidePoint), | |
222 | } | |
223 | ||
224 | /// Face on a [`Cylinder`] | |
225 | #[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize_repr, Deserialize_repr)] | |
226 | #[repr(u8)] | |
227 | pub enum Face { | |
228 | Top, | |
229 | Bottom, | |
230 | Side, | |
231 | } | |
232 | ||
46 | 233 | /// Point on a specific [`Cylinder`] |
37 | 234 | #[derive(Clone, Debug, PartialEq)] |
235 | pub struct OnCylinder<'a> { | |
46 | 236 | /// Face and coordinates of the point |
237 | point : Point, | |
238 | /// The specific cylinder the `point` lies on. | |
37 | 239 | cylinder : &'a Cylinder, |
240 | } | |
241 | ||
242 | ||
243 | /// Cylinder configuration | |
244 | #[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)] | |
245 | pub struct CylinderConfig { | |
46 | 246 | /// Number of Newton iterates two take to solve geodesics. |
37 | 247 | pub newton_iters : usize, |
248 | } | |
249 | ||
250 | impl Default for CylinderConfig { | |
251 | fn default() -> Self { | |
252 | CylinderConfig { newton_iters : 10 } | |
253 | } | |
254 | } | |
255 | ||
256 | /// A cylinder | |
257 | #[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)] | |
258 | pub struct Cylinder { | |
259 | /// Radius of the cylinder | |
260 | pub radius : f64, | |
261 | /// Height of the cylinder | |
262 | pub height : f64, | |
263 | /// Configuration for numerical methods | |
264 | pub config : CylinderConfig | |
265 | } | |
266 | ||
267 | ||
268 | impl std::fmt::Display for Face { | |
269 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |
270 | use Face::*; | |
271 | let s = match *self { | |
272 | Top => "Top", | |
273 | Bottom => "Bottom", | |
274 | Side => "Side", | |
275 | }; | |
276 | write!(f, "{}", s) | |
277 | } | |
278 | } | |
279 | ||
280 | impl Point { | |
46 | 281 | /// Returns the face of the point |
37 | 282 | fn face(&self) -> Face { |
283 | match *self { | |
284 | Point::Top(..) => Face::Top, | |
285 | Point::Bottom(_) => Face::Bottom, | |
286 | Point::Side(_) => Face::Side, | |
287 | } | |
288 | } | |
289 | } | |
290 | ||
291 | impl<'a> FacedManifoldPoint for OnCylinder<'a> { | |
292 | type Face = Face; | |
293 | /// Returns the face of this point. | |
294 | fn face(&self) -> Face { | |
295 | self.point.face() | |
296 | } | |
297 | } | |
298 | ||
299 | // Tangent vector | |
300 | type Tangent = Loc<f64, 2>; | |
301 | ||
46 | 302 | /// Goes through list of potential tangents (corresponding to several geodesics taking |
303 | /// different paths), and retuns the shortest tangent vector and the corresponding length. | |
37 | 304 | #[inline] |
305 | fn best_tangent<I>(tangents : I) -> (Tangent, f64) | |
306 | where I : IntoIterator<Item = Tangent> { | |
307 | tangents.into_iter() | |
308 | .map(|t| (t, t.norm(L2))) | |
309 | .reduce(|a@(_, la), b@(_, lb)| if la < lb { a } else { b }) | |
310 | .unwrap() | |
311 | } | |
312 | ||
313 | /// Swap the elements of a two-tuple | |
314 | #[inline] | |
315 | fn swap<A>((a, b) : (A, A)) -> (A, A) { | |
316 | (b, a) | |
317 | } | |
318 | ||
46 | 319 | /// Indicates whether the `a` and `b` are a distance of less than [`f64::EPSILON`]. |
37 | 320 | #[inline] |
321 | fn indistinguishable(a : f64, b : f64) -> bool { | |
322 | a > b - f64::EPSILON && a < b + f64::EPSILON | |
323 | } | |
324 | ||
325 | impl Cylinder { | |
326 | /// Return the cylindrical coordinates of `point` on this cylinder | |
327 | fn cyl_coords(&self, point : &Point) -> CylCoords { | |
328 | match *point { | |
329 | Point::Top(cap) => { cap.cyl_coords(self.top_z()) }, | |
330 | Point::Bottom(cap) => { cap.cyl_coords(self.bottom_z()) }, | |
331 | Point::Side(side) => { side.cyl_coords(self.radius) }, | |
332 | } | |
333 | } | |
334 | ||
46 | 335 | /// Returns the z coordinate of the top (cap) of the cylinder. |
37 | 336 | #[inline] |
337 | pub fn top_z(&self) -> f64 { | |
338 | self.height / 2.0 | |
339 | } | |
340 | ||
46 | 341 | /// Returns the z coordinate of the bottom (cap) of the cylinder. |
37 | 342 | #[inline] |
343 | pub fn bottom_z(&self) -> f64 { | |
344 | -self.height / 2.0 | |
345 | } | |
346 | ||
347 | /// Find angle where a geodesic from `side` to `(cap, z)` crosses the cap edge. | |
348 | /// | |
46 | 349 | /// Uses [`newton_sym1x1`]. |
37 | 350 | fn side_cap_crossing( |
351 | &self, | |
352 | side : &SidePoint, | |
353 | cap : &CapPoint, z : f64, // `z` is the z coordinate of cap | |
354 | ) -> Angle { | |
355 | let &SidePoint { z : z_1, angle : φ_1 } = side; | |
356 | let &CapPoint { r : r_2, angle : φ_2 } = cap; | |
357 | assert_ne!(z, z_1); | |
358 | let d_1 = z - z_1; | |
359 | let d2_1 = d_1 * d_1; | |
360 | let r = self.radius; | |
361 | let r2 = r * r; | |
362 | let r2_2 = r_2 * r_2; | |
363 | let rr_2 = r * r_2; | |
364 | ||
365 | let g = |α_1 : f64| { | |
366 | let ψ = φ_2 - φ_1 - α_1; | |
367 | let ψ_cos = ψ.cos(); | |
368 | let ψ_sin = ψ.sin(); | |
369 | let ψ_sin2 = ψ_sin * ψ_sin; | |
370 | let ψ_cos2 = ψ_cos * ψ_cos; | |
371 | let α2_1 = α_1 * α_1; | |
372 | let c = d2_1 + r2 * α2_1; | |
373 | let e = r2 + r2_2 - 2.0 * rr_2 * ψ_cos; | |
374 | let g = r2 * α_1 / c.sqrt() - rr_2 * ψ_sin / e.sqrt(); | |
375 | let h = r2 * d2_1 / c.powf(3.0/2.0) | |
376 | + r * r_2 * ((r2 + r2_2) * ψ_cos - rr_2 * (ψ_sin2 - 2.0 * ψ_cos2)) | |
377 | / e.powf(3.0/2.0); | |
378 | (h, g) | |
379 | }; | |
380 | ||
381 | let α_1 = newton_sym1x1(g, 0.0, self.config.newton_iters); | |
382 | normalise_angle(φ_1 + α_1) | |
383 | ||
384 | } | |
385 | ||
386 | /// Find angles where the geodesic passing through a cap at height `z` from `side1` to `side2` | |
387 | /// crosses the cap edge. **Panics if `side2.angle < side1.angle`.** | |
388 | /// | |
46 | 389 | /// Uses [`newton_sym2x2`]. |
37 | 390 | fn side_cap_side_crossing( |
391 | &self, | |
392 | side1 : &SidePoint, | |
393 | z : f64, | |
394 | side2 : &SidePoint | |
395 | ) -> (Angle, Angle) { | |
396 | let &SidePoint { z : z_1, angle : φ_1 } = side1; | |
397 | let &SidePoint { z : z_2, angle : φ_2 } = side2; | |
398 | assert!(φ_2 >= φ_1); | |
399 | assert_ne!(z_1, z); | |
400 | assert_ne!(z_2, z); | |
401 | let r = self.radius; | |
402 | let r2 = r * r; | |
403 | let d_1 = z - z_1; | |
404 | let d_2 = z - z_2; | |
405 | let d2_1 = d_1 * d_1; | |
406 | let d2_2 = d_2 * d_2; | |
407 | let g = |α_1 : f64, α_2 : f64| { | |
408 | let α2_1 = α_1 * α_1; | |
409 | let α2_2 = α_2 * α_2; | |
410 | let ψ = (α_1 + α_2 + φ_1 - φ_2) / 2.0; | |
411 | let ψ_sin = ψ.sin(); | |
412 | let ψ_cos = ψ.cos(); | |
413 | let c_1 = d2_1 + r2 * α2_1; | |
414 | let c_2 = d2_2 + r2 * α2_2; | |
415 | let g_1 = r2 * α_1 / c_1.sqrt() - r * ψ_cos; | |
416 | let g_2 = r2 * α_2 / c_2.sqrt() - r * ψ_cos; | |
417 | let h_12 = (r / 2.0) * ψ_sin; | |
418 | let h_11 = r2 * d2_1 / c_1.powf(3.0 / 2.0) + h_12; | |
419 | let h_22 = r2 * d2_2 / c_2.powf(3.0 / 2.0) + h_12; | |
420 | ([h_11, h_12, h_22], [g_1, g_2]) | |
421 | }; | |
422 | ||
423 | let [α_1, α_2] = newton_sym2x2(g, [0.0, 0.0], self.config.newton_iters); | |
424 | (normalise_angle(φ_1 + α_1), normalise_angle(φ_2 + α_2)) | |
425 | ||
426 | } | |
427 | ||
428 | /// Find angles where the geodesic passing through the side from `cap1` at height `z_1` | |
429 | /// to `cap2` at height `z_2` crosses the cap edges. | |
430 | /// **Panics if `cap2.angle < cap1.angle`.** | |
431 | /// | |
46 | 432 | /// Uses [`newton_sym2x2`]. |
37 | 433 | fn cap_side_cap_crossing( |
434 | &self, | |
435 | cap1 : &CapPoint, z_1 : f64, | |
436 | cap2 : &CapPoint, z_2 : f64, | |
437 | init_by_cap2 : bool | |
438 | ) -> (Angle, Angle) { | |
439 | let r = self.radius; | |
440 | let &CapPoint { r : r_1, angle : φ_1 } = cap1; | |
441 | let &CapPoint { r : r_2, angle : φ_2 } = cap2; | |
442 | assert!(φ_2 >= φ_1); | |
443 | assert_ne!(r_1, r); | |
444 | assert_ne!(r_2, r); | |
445 | assert_ne!(z_2, z_1); | |
446 | if r_1 == 0.0 && r_2 == 0.0 { | |
447 | // Singular case: both points are in the middle of the caps. | |
448 | return (φ_1, φ_1) | |
449 | } | |
450 | let r2 = r * r; | |
451 | let d = (z_2 - z_1).abs(); | |
452 | let d2 = d * d; | |
453 | let r2_1 = r_1 * r_1; | |
454 | let r2_2 = r_2 * r_2; | |
455 | let rr_1 = r * r_1; | |
456 | let rr_2 = r * r_2; | |
457 | let f = |α_1 : f64, α_2 : f64| { | |
458 | let cos_α1 = α_1.cos(); | |
459 | let sin_α1 = α_1.sin(); | |
460 | let cos_α2 = α_2.cos(); | |
461 | let sin_α2 = α_2.sin(); | |
462 | //let cos2_α1 = cos_α1 * cos_α1; | |
463 | let sin2_α1 = sin_α1 * sin_α1; | |
464 | //let cos2_α2 = cos_α2 * cos_α2; | |
465 | let sin2_α2 = sin_α2 * sin_α2; | |
466 | let ψ = φ_2 - φ_1 - α_1 - α_2; | |
467 | let ψ2 = ψ * ψ; | |
468 | let ψ2r2 = ψ2 * r2; | |
469 | //let r4 = r2 * r2; | |
470 | let b = d2 + ψ2r2; | |
471 | let c = r2 * ψ / b.sqrt(); | |
472 | let e_1 = r2 + r2_1 - 2.0 * rr_1 * cos_α1; | |
473 | let e_2 = r2 + r2_2 - 2.0 * rr_2 * cos_α2; | |
474 | let g_1 = rr_1 * sin_α1 / e_1.sqrt() - c; | |
475 | let g_2 = rr_2 * sin_α2 / e_2.sqrt() - c; | |
476 | let h_12 = r2 * (1.0 - ψ2r2 / b) / b.sqrt(); | |
477 | // let h_11 = rr_1 * ( (r2 + r2_1) * cos_α1 - rr_1 * ( sin2_α1 + 2.0 * cos2_α1) ) | |
478 | // / e_1.powf(3.0/2.0) + h_12; | |
479 | // let h_22 = rr_2 * ( (r2 + r2_2) * cos_α2 - rr_2 * ( sin2_α2 + 2.0 * cos2_α2) ) | |
480 | // / e_2.powf(3.0/2.0) + h_12; | |
481 | // let h_11 = rr_1 * cos_α1 / e_1.sqrt() - rr_1*rr_1 * sin2_α1 / e_1.powf(3.0/2.0) + h_12; | |
482 | // let h_22 = rr_2 * cos_α2 / e_2.sqrt() - rr_2*rr_2 * sin2_α2 / e_2.powf(3.0/2.0) + h_12; | |
483 | let h_11 = rr_1 * (cos_α1 - rr_1 * sin2_α1 / e_1) / e_1.sqrt() + h_12; | |
484 | let h_22 = rr_2 * (cos_α2 - rr_2 * sin2_α2 / e_2) / e_2.sqrt() + h_12; | |
485 | ([h_11, h_12, h_22], [g_1, g_2]) | |
486 | }; | |
487 | ||
488 | let α_init = if init_by_cap2 { | |
489 | [φ_2 - φ_1, 0.0] | |
490 | } else { | |
491 | [0.0, φ_2 - φ_1] | |
492 | }; | |
493 | let [α_1, α_2] = newton_sym2x2(f, α_init, self.config.newton_iters); | |
494 | (normalise_angle(φ_1 + α_1), normalise_angle(φ_2 - α_2)) | |
495 | } | |
496 | ||
46 | 497 | /// Calculates the log between points on a `cap` and a `side` of the cylinder, in this direction. |
498 | /// The `z` coordinate of the cap and an indication whether it is a `top` or bottom cap must | |
499 | /// also be given. | |
37 | 500 | fn cap_side_log( |
501 | &self, | |
502 | cap : &CapPoint, (z, top) : (f64, bool), | |
503 | side : &SidePoint | |
504 | ) -> Tangent { | |
505 | let r = self.radius; | |
506 | if indistinguishable(side.z, z) { | |
507 | // Degenerate case | |
508 | let capedge = CapPoint{ angle : side.angle, r }; | |
509 | cap.log(&capedge) | |
510 | } else if indistinguishable(r, cap.r) | |
511 | && anglediff(side.angle, cap.angle).abs() < f64::PI/2.0 { | |
512 | // Degenerate case | |
513 | let sideedge = SidePoint{ angle : cap.angle, z}; | |
514 | cap.tangent_from_side(top, sideedge.log(r, side)) | |
515 | } else { | |
516 | let φ = self.side_cap_crossing(side, cap, z); | |
517 | let capedge = CapPoint{ angle : φ, r }; | |
518 | let sideedge = SidePoint{ angle : φ, z }; | |
519 | let t1 = cap.log(&capedge); | |
520 | let t2 = sideedge.log(r, side); | |
41
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
521 | // Either option should give the same result. |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
522 | // The first one avoids division, but seems numerically more unstable due to |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
523 | // sines and cosines. |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
524 | //t1 + capedge.tangent_from_side(top, t2) |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
525 | let n = t1.norm(L2); |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
526 | if n < f64::EPSILON { |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
527 | capedge.tangent_from_side(top, t2) |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
528 | } else { |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
529 | (t1/n)*(n + t2.norm(L2)) |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
530 | } |
37 | 531 | } |
532 | } | |
533 | ||
46 | 534 | /// Calculates the log between points on a `side` and a `cap` of the cylinder, in this direction. |
535 | /// The `z` coordinate of the cap and an indication whether it is a `top` or bottom cap must | |
536 | /// also be given. | |
37 | 537 | fn side_cap_log( |
538 | &self, | |
539 | side : &SidePoint, | |
540 | cap : &CapPoint, (z, top) : (f64, bool), | |
541 | ) -> Tangent { | |
542 | let r = self.radius; | |
543 | if indistinguishable(side.z, z) { | |
544 | // Degenerate case | |
545 | let capedge = CapPoint{ angle : side.angle, r }; | |
546 | capedge.tangent_to_side(top, capedge.log(cap)) | |
547 | } else if indistinguishable(r, cap.r) | |
548 | && anglediff(side.angle, cap.angle).abs() < f64::PI/2.0 { | |
549 | // Degenerate case | |
550 | side.log(r, &SidePoint{ z, angle : cap.angle }) | |
551 | } else { | |
552 | let φ = self.side_cap_crossing(side, cap, z); | |
553 | let capedge = CapPoint{ angle : φ, r }; | |
554 | let sideedge = SidePoint{ angle : φ, z }; | |
555 | let t1 = side.log(r, &sideedge); | |
556 | let t2 = capedge.log(cap); | |
41
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
557 | // Either option should give the same result. |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
558 | // The first one avoids division, but seems numerically more unstable due to |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
559 | // sines and cosines. |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
560 | // t1 + capedge.tangent_to_side(top, t2) |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
561 | let n = t1.norm(L2); |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
562 | if n < f64::EPSILON { |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
563 | capedge.tangent_to_side(top, t2) |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
564 | } else { |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
565 | (t1/n)*(n + t2.norm(L2)) |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
566 | } |
37 | 567 | } |
568 | } | |
569 | ||
46 | 570 | /// Calculates the log between points on two sides of the cylinder, assuming the corresonding |
571 | /// geodesic crosses a cap at height `z`. The `top` parameter indicates whether this is a | |
572 | /// top cap. | |
37 | 573 | fn side_cap_side_log( |
574 | &self, | |
575 | side1 : &SidePoint, | |
576 | (z, top) : (f64, bool), | |
577 | side2 : &SidePoint | |
578 | ) -> Tangent { | |
579 | let r = self.radius; | |
580 | if indistinguishable(side1.z, z) { | |
581 | // Degenerate case | |
582 | let capedge1 = CapPoint{ angle : side1.angle, r }; | |
583 | capedge1.tangent_to_side(top, self.cap_side_log(&capedge1, (z, top), side2)) | |
584 | } else if indistinguishable(side2.z, z) { | |
585 | // Degenerate case | |
586 | let capedge2 = CapPoint{ angle : side2.angle, r }; | |
587 | self.side_cap_log(side1, &capedge2, (z, top)) | |
588 | } else { | |
589 | let (φ1, φ2) = if side2.angle >= side1.angle { | |
590 | self.side_cap_side_crossing(side1, z, side2) | |
591 | } else { | |
592 | swap(self.side_cap_side_crossing(side2, z, side1)) | |
593 | }; | |
594 | let capedge1 = CapPoint{ angle : φ1, r }; | |
595 | let sideedge1 = SidePoint{ angle : φ1, z }; | |
596 | let capedge2 = CapPoint{ angle : φ2, r }; | |
597 | let sideedge2 = SidePoint{ angle : φ2, z }; | |
598 | let t1 = side1.log(r, &sideedge1); | |
599 | let t2 = capedge1.log(&capedge2); | |
600 | let t3 = sideedge2.log(r, &side2); | |
41
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
601 | // Either option should give the same result. |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
602 | // The first one avoids division, but seems numerically more unstable due to |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
603 | // sines and cosines. |
37 | 604 | // |
41
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
605 | // t1 + capedge1.tangent_to_side(top, t2 + capedge2.tangent_from_side(top, t3)) |
37 | 606 | let n = t1.norm(L2); |
41
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
607 | if n < f64::EPSILON { |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
608 | let n2 = t2.norm(L2); |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
609 | capedge1.tangent_to_side(top, |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
610 | if n2 < f64::EPSILON { |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
611 | capedge2.tangent_from_side(top, t3) |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
612 | } else { |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
613 | (t2/n2)*(n2 + t3.norm(L2)) |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
614 | } |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
615 | ) |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
616 | } else { |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
617 | (t1/n)*(n + t2.norm(L2) + t3.norm(L2)) |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
618 | } |
37 | 619 | } |
620 | } | |
621 | ||
46 | 622 | /// Calculates the log between points on two caps of the cylinder, assuming the corresonding |
623 | /// geodesic crosses the side. The heights of the caps and indications whether they are | |
624 | /// top caps, must also be given. | |
37 | 625 | fn cap_side_cap_log( |
626 | &self, | |
627 | cap1 : &CapPoint, (z1, top1) : (f64, bool), | |
628 | cap2 : &CapPoint, (z2, top2) : (f64, bool), | |
629 | init_by_cap2 : bool, | |
630 | ) -> Tangent { | |
631 | let r = self.radius; | |
632 | if indistinguishable(cap1.r, r) { | |
633 | // Degenerate case | |
634 | let sideedge1 = SidePoint{ angle : cap1.angle, z : z1 }; | |
635 | cap1.tangent_from_side(top1, self.side_cap_log(&sideedge1, cap2, (z2, top2))) | |
636 | } else if indistinguishable(cap2.r, r) { | |
637 | // Degenerate case | |
638 | let sideedge2 = SidePoint{ angle : cap2.angle, z : z2 }; | |
639 | self.cap_side_log(cap1, (z1, top1), &sideedge2) | |
640 | } else { | |
641 | let (φ1, φ2) = if cap2.angle >= cap1.angle { | |
642 | self.cap_side_cap_crossing(cap1, z1, cap2, z2, init_by_cap2) | |
643 | } else { | |
644 | swap(self.cap_side_cap_crossing(cap2, z2, cap1, z1, !init_by_cap2)) | |
645 | }; | |
646 | let sideedge1 = SidePoint{ angle : φ1, z : z1 }; | |
647 | let capedge1 = CapPoint{ angle : φ1, r }; | |
648 | let sideedge2 = SidePoint{ angle : φ2, z : z2}; | |
649 | let capedge2 = CapPoint{ angle : φ2, r }; | |
650 | let t1 = cap1.log(&capedge1); | |
651 | let t2 = sideedge1.log(r, &sideedge2); | |
652 | let t3 = capedge2.log(cap2); | |
41
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
653 | // Either option should give the same result. |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
654 | // The first one avoids division, but seems numerically more unstable due to |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
655 | // sines and cosines. |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
656 | // t1 + capedge1.tangent_from_side(top1, t2 + capedge2.tangent_to_side(top2, t3)) |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
657 | let n = t1.norm(L2); |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
658 | if n < f64::EPSILON { |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
659 | let n2 = t2.norm(L2); |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
660 | capedge1.tangent_from_side(top1, |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
661 | if n2 < f64::EPSILON { |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
662 | capedge2.tangent_to_side(top2, t3) |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
663 | } else { |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
664 | (t2/n2)*(n2 + t3.norm(L2)) |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
665 | } |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
666 | ) |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
667 | } else { |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
668 | (t1/n)*(n + t2.norm(L2) + t3.norm(L2)) |
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
669 | } |
37 | 670 | } |
671 | } | |
672 | ||
46 | 673 | /// Calculates both the logarithmic map and distance between two points. |
37 | 674 | fn log_dist(&self, source : &Point, destination : &Point) -> (Tangent, f64) { |
675 | use Point::*; | |
676 | match (source, destination) { | |
677 | (Top(cap1), Top(cap2)) => { | |
678 | best_tangent([cap1.log(cap2)]) | |
679 | }, | |
680 | (Bottom(cap1), Bottom(cap2)) => { | |
681 | best_tangent([cap1.log(cap2)]) | |
682 | }, | |
683 | (Bottom(cap), Side(side)) => { | |
684 | best_tangent([self.cap_side_log(cap, (self.bottom_z(), false), side)]) | |
685 | }, | |
686 | (Top(cap), Side(side)) => { | |
687 | best_tangent([self.cap_side_log(cap, (self.top_z(), true), side)]) | |
688 | }, | |
689 | (Side(side), Bottom(cap)) => { | |
690 | best_tangent([self.side_cap_log(side, cap, (self.bottom_z(), false))]) | |
691 | }, | |
692 | (Side(side), Top(cap)) => { | |
693 | best_tangent([self.side_cap_log(side, cap, (self.top_z(), true))]) | |
694 | }, | |
695 | (Side(side1), Side(side2)) => { | |
696 | best_tangent([ | |
697 | side1.log(self.radius, side2), | |
698 | self.side_cap_side_log(side1, (self.top_z(), true), side2), | |
699 | self.side_cap_side_log(side1, (self.bottom_z(), false), side2), | |
700 | ]) | |
701 | }, | |
702 | (Top(cap1), Bottom(cap2)) => { | |
703 | best_tangent([ | |
704 | // We try a few possible initialisations for Newton | |
705 | self.cap_side_cap_log( | |
706 | cap1, (self.top_z(), true), | |
707 | cap2, (self.bottom_z(), false), | |
708 | false | |
709 | ), | |
710 | self.cap_side_cap_log( | |
711 | cap1, (self.top_z(), true), | |
712 | cap2, (self.bottom_z(), false), | |
713 | true | |
714 | ), | |
715 | ]) | |
716 | }, | |
717 | (Bottom(cap1), Top(cap2)) => { | |
718 | best_tangent([ | |
719 | // We try a few possible initialisations for Newton | |
720 | self.cap_side_cap_log( | |
721 | cap1, (self.bottom_z(), false), | |
722 | cap2, (self.top_z(), true), | |
723 | false | |
724 | ), | |
725 | self.cap_side_cap_log( | |
726 | cap1, (self.bottom_z(), false), | |
727 | cap2, (self.top_z(), true), | |
728 | true | |
729 | ), | |
730 | ]) | |
731 | }, | |
732 | } | |
733 | } | |
734 | ||
46 | 735 | /// Calculates the exponential map of `point` in the direction `t` until the next edge. |
736 | /// If `t` was fully exhausted before reaching an edge, the second return value is [`None`], | |
737 | /// otherwise it is a [`Some`] of the remaining tangent, translated at the returned point. | |
37 | 738 | #[allow(unreachable_code)] |
739 | #[allow(unused_variables)] | |
740 | fn partial_exp(&self, point : Point, t : Tangent) -> (Point, Option<Tangent>) { | |
741 | match point { | |
742 | Point::Top(cap) => { | |
743 | let (cap_new, t_new_basis) = cap.partial_exp(self.radius, &t); | |
744 | match t_new_basis { | |
745 | None => (Point::Top(cap_new), None), | |
746 | Some(t_new) => { | |
747 | let side_new = SidePoint{ angle : cap_new.angle, z : self.top_z() }; | |
748 | (Point::Side(side_new), Some(cap_new.tangent_to_side(true, t_new))) | |
749 | } | |
750 | } | |
751 | }, | |
752 | Point::Bottom(cap) => { | |
753 | let (cap_new, t_new_basis) = cap.partial_exp(self.radius, &t); | |
754 | match t_new_basis { | |
755 | None => (Point::Bottom(cap_new), None), | |
756 | Some(t_new) => { | |
757 | let side_new = SidePoint{ angle : cap_new.angle, z : self.bottom_z() }; | |
758 | (Point::Side(side_new), Some(cap_new.tangent_to_side(false, t_new))) | |
759 | } | |
760 | } | |
761 | }, | |
762 | Point::Side(side) => { | |
763 | let lims = (self.bottom_z(), self.top_z()); | |
764 | let (side_new, t_new_basis) = side.partial_exp(self.radius, lims, &t); | |
765 | match t_new_basis { | |
766 | None => (Point::Side(side_new), None), | |
767 | Some(t_new) => { | |
768 | if side_new.z >= self.top_z() - f64::EPSILON { | |
769 | let cap_new = CapPoint { angle : side_new.angle, r : self.radius }; | |
770 | (Point::Top(cap_new), Some(cap_new.tangent_from_side(true, t_new))) | |
38
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
771 | } else if side_new.z <= self.bottom_z() + f64::EPSILON { |
37 | 772 | let cap_new = CapPoint { angle : side_new.angle, r : self.radius }; |
773 | (Point::Bottom(cap_new), Some(cap_new.tangent_from_side(false, t_new))) | |
38
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
774 | } else { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
775 | (Point::Side(side_new), None) |
37 | 776 | } |
777 | } | |
778 | } | |
779 | } | |
780 | } | |
781 | } | |
782 | ||
46 | 783 | /// Calculates the exponential map from `point` in the direction of `tangent`. |
37 | 784 | fn exp(&self, point : &Point, tangent : &Tangent) -> Point { |
785 | let mut p = *point; | |
786 | let mut t = *tangent; | |
787 | loop { | |
788 | (p, t) = match self.partial_exp(p, t) { | |
789 | (p, None) => break p, | |
790 | (p, Some(t)) => (p, t), | |
791 | }; | |
792 | } | |
793 | } | |
794 | ||
795 | /// Check that `point` has valid coordinates, and normalise angles | |
796 | pub fn normalise(&self, point : Point) -> Option<Point> { | |
797 | match point { | |
798 | Point::Side(side) => { | |
799 | let a = self.bottom_z(); | |
800 | let b = self.top_z(); | |
801 | (a <= side.z && side.z <= b).then(|| { | |
802 | Point::Side(SidePoint{ angle : normalise_angle(side.angle), .. side }) | |
803 | }) | |
804 | }, | |
805 | Point::Bottom(cap) => { | |
806 | (cap.r <= self.radius).then(|| { | |
807 | Point::Bottom(CapPoint{ angle : normalise_angle(cap.angle), .. cap }) | |
808 | }) | |
809 | }, | |
810 | Point::Top(cap) => { | |
811 | (cap.r <= self.radius).then(|| { | |
812 | Point::Top(CapPoint{ angle : normalise_angle(cap.angle), .. cap }) | |
813 | }) | |
814 | }, | |
815 | } | |
816 | } | |
817 | ||
818 | /// Convert `p` into a a point associated with the cylinder. | |
819 | /// | |
820 | /// May panic if the coordinates are invalid. | |
821 | pub fn point_on(&self, point : Point) -> OnCylinder<'_> { | |
822 | match self.normalise(point) { | |
823 | None => panic!("{point:?} not on cylinder {self:?}"), | |
824 | Some(point) => OnCylinder { cylinder : self, point } | |
825 | } | |
826 | } | |
827 | ||
828 | /// Convert `p` into a a point on side associated with the cylinder. | |
829 | /// | |
830 | /// May panic if the coordinates are invalid. | |
831 | pub fn point_on_side(&self, side : SidePoint) -> OnCylinder<'_> { | |
832 | self.point_on(Point::Side(side)) | |
833 | } | |
834 | ||
835 | /// Convert `p` into a a point on top associated with the cylinder. | |
836 | /// | |
837 | /// May panic if the coordinates are invalid. | |
838 | pub fn point_on_top(&self, cap : CapPoint) -> OnCylinder<'_> { | |
839 | self.point_on(Point::Top(cap)) | |
840 | } | |
841 | ||
842 | /// Convert `p` into a a point on bottom associated with the cylinder. | |
843 | /// | |
844 | /// May panic if the coordinates are invalid. | |
845 | pub fn point_on_bottom(&self, cap : CapPoint) -> OnCylinder<'_> { | |
846 | self.point_on(Point::Bottom(cap)) | |
847 | } | |
848 | ||
849 | /// Convert `p` into a a point on side associated with the cylinder. | |
850 | /// | |
851 | /// May panic if the coordinates are invalid. | |
852 | pub fn on_side(&self, angle : Angle, z : f64) -> OnCylinder<'_> { | |
853 | self.point_on_side(SidePoint{ angle, z }) | |
854 | } | |
855 | ||
856 | /// Convert `p` into a a point on top associated with the cylinder. | |
857 | /// | |
858 | /// May panic if the coordinates are invalid. | |
859 | pub fn on_top(&self, angle : Angle, r : f64) -> OnCylinder<'_> { | |
860 | self.point_on_top(CapPoint{ angle, r }) | |
861 | } | |
862 | ||
863 | /// Convert `p` into a a point on bottom associated with the cylinder. | |
864 | /// | |
865 | /// May panic if the coordinates are invalid. | |
866 | pub fn on_bottom(&self, angle : Angle, r : f64) -> OnCylinder<'_> { | |
867 | self.point_on_bottom(CapPoint{ angle, r }) | |
868 | } | |
869 | } | |
870 | ||
871 | impl<'a> OnCylinder<'a> { | |
872 | /// Return the cylindrical coordinates of this point | |
873 | pub fn cyl_coords(&self) -> CylCoords { | |
874 | self.cylinder.cyl_coords(&self.point) | |
875 | } | |
876 | } | |
877 | ||
878 | impl<'a> EmbeddedManifoldPoint for OnCylinder<'a> { | |
879 | type EmbeddedCoords = Loc<f64, 3>; | |
880 | ||
881 | /// Get embedded 3D coordinates | |
882 | fn embedded_coords(&self) -> Loc<f64, 3> { | |
883 | self.cyl_coords().to_cartesian() | |
884 | } | |
885 | } | |
886 | ||
887 | impl<'a> ManifoldPoint for OnCylinder<'a> { | |
888 | type Tangent = Tangent; | |
889 | ||
890 | fn exp(self, tangent : &Self::Tangent) -> Self { | |
891 | let cylinder = self.cylinder; | |
892 | let point = cylinder.exp(&self.point, tangent); | |
893 | OnCylinder { cylinder, point } | |
894 | } | |
895 | ||
896 | fn log(&self, other : &Self) -> Self::Tangent { | |
897 | assert!(self.cylinder == other.cylinder); | |
898 | self.cylinder.log_dist(&self.point, &other.point).0 | |
899 | } | |
900 | ||
901 | fn dist_to(&self, other : &Self) -> f64 { | |
902 | assert!(self.cylinder == other.cylinder); | |
903 | self.cylinder.log_dist(&self.point, &other.point).1 | |
904 | } | |
905 | ||
906 | fn tangent_origin(&self) -> Self::Tangent { | |
907 | Loc([0.0, 0.0]) | |
908 | } | |
909 | } | |
910 | ||
911 | #[cfg(test)] | |
912 | mod tests { | |
913 | use super::*; | |
914 | ||
41
56d609b70a3b
Cylinder numerical stability hacks
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
915 | static TOL : f64 = 1e-7; |
38
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
916 | |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
917 | macro_rules! check_distance { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
918 | ($distance : expr, $expected : expr) => { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
919 | assert!( |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
920 | ($distance-$expected).abs() < TOL, |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
921 | "{} = {}, expected = {}", |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
922 | stringify!($distance), |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
923 | $distance, |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
924 | $expected, |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
925 | ) |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
926 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
927 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
928 | |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
929 | macro_rules! check_vec_eq { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
930 | ($left : expr, $right : expr) => { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
931 | let err = ($left-$right).norm(L2); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
932 | if err > TOL { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
933 | panic!("{:?} != {:?} [error {err}]", $left, $right); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
934 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
935 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
936 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
937 | |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
938 | macro_rules! check_point_eq { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
939 | ($left : expr, $right : expr) => { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
940 | match ($left, $right) { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
941 | (Point::Top(a), Point::Top(b)) | (Point::Bottom(a), Point::Bottom(b))=> { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
942 | if (a.angle-b.angle).abs() > TOL || (a.r-b.r).abs() > TOL { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
943 | panic!("{:?} != {:?}", a, b) |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
944 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
945 | }, |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
946 | (Point::Side(a), Point::Side(b)) => { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
947 | if (a.angle-b.angle).abs() > TOL || (a.z-b.z).abs() > TOL { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
948 | panic!("{:?} != {:?}", a, b) |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
949 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
950 | }, |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
951 | (a, b) => { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
952 | panic!("{:?} != {:?}", a, b) |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
953 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
954 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
955 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
956 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
957 | |
37 | 958 | static CYL : Cylinder = Cylinder { |
959 | radius : 1.0, | |
960 | height : 1.0, | |
961 | config : CylinderConfig { newton_iters : 20 }, | |
962 | }; | |
963 | ||
46 | 964 | /// Tests for correct distance calculation beween two points on the same cap. |
37 | 965 | #[test] |
46 | 966 | fn intra_cap_dist() { |
37 | 967 | let π = f64::PI; |
968 | let p1 = CYL.on_top(0.0, 0.5); | |
969 | let p2 = CYL.on_top(π, 0.5); | |
970 | let p3 = CYL.on_top(π/2.0, 0.5); | |
971 | ||
38
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
972 | check_distance!(p1.dist_to(&p2), 1.0); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
973 | check_distance!(p2.dist_to(&p3), 0.5_f64.sqrt()); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
974 | check_distance!(p3.dist_to(&p1), 0.5_f64.sqrt()); |
37 | 975 | } |
976 | ||
46 | 977 | /// Tests for correct distance calculation beween two points on the side. |
37 | 978 | #[test] |
46 | 979 | fn intra_side_dist() { |
37 | 980 | let π = f64::PI; |
981 | let p1 = CYL.on_side(0.0, 0.0); | |
982 | let p2 = CYL.on_side(0.0, 0.4); | |
983 | let p3 = CYL.on_side(π/2.0, 0.0); | |
984 | ||
38
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
985 | check_distance!(p1.dist_to(&p2), 0.4); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
986 | check_distance!(p1.dist_to(&p3), π/2.0*CYL.radius); |
37 | 987 | } |
988 | ||
46 | 989 | /// Tests for correct distance calculation beween two points on the side, when the corresponding |
990 | /// minimal geodesic has to cross a cap. | |
37 | 991 | #[test] |
46 | 992 | fn intra_side_over_cap_dist() { |
37 | 993 | let π = f64::PI; |
994 | let off = 0.05; | |
995 | let z = CYL.top_z() - off; | |
996 | let p1 = CYL.on_side(0.0, z); | |
997 | let p2 = CYL.on_side(π, z); | |
998 | ||
38
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
999 | check_distance!(p1.dist_to(&p2), 2.0 * (CYL.radius + off)); |
37 | 1000 | } |
1001 | ||
46 | 1002 | /// Tests for correct distance calculation between points on top and bottom caps. |
37 | 1003 | #[test] |
46 | 1004 | fn top_bottom_dist() { |
37 | 1005 | let π = f64::PI; |
1006 | let p1 = CYL.on_top(0.0, 0.0); | |
1007 | let p2 = CYL.on_bottom(0.0, 0.0); | |
1008 | ||
38
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1009 | check_distance!(p1.dist_to(&p2), 2.0 * CYL.radius + CYL.height); |
37 | 1010 | |
1011 | let p1 = CYL.on_top(0.0, CYL.radius / 2.0); | |
1012 | let p2 = CYL.on_bottom(0.0, CYL.radius / 2.0); | |
1013 | let p3 = CYL.on_bottom(π, CYL.radius / 2.0); | |
1014 | ||
38
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1015 | check_distance!(p1.dist_to(&p2), CYL.radius + CYL.height); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1016 | check_distance!(p1.dist_to(&p3), 2.0 * CYL.radius + CYL.height); |
37 | 1017 | } |
1018 | ||
46 | 1019 | /// Test for correct distance calculation between points on the side and a cap. |
37 | 1020 | #[test] |
46 | 1021 | fn top_side_dist() { |
37 | 1022 | let p1 = CYL.on_top(0.0, 0.0); |
1023 | let p2 = CYL.on_side(0.0, 0.0); | |
1024 | ||
38
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1025 | check_distance!(p1.dist_to(&p2), CYL.radius + CYL.height / 2.0); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1026 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1027 | |
46 | 1028 | /// Tests for correct tangent calculation from a cap to the side. |
38
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1029 | #[test] |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1030 | fn cap_side_partial_exp() { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1031 | let π = f64::PI; |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1032 | let angles = [0.0, π/2.0, π*5.0/6.0, π*7.0/5.0]; |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1033 | |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1034 | for φ in angles { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1035 | let p = CYL.on_top(φ, CYL.radius / 2.0); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1036 | let q_target = CYL.on_side(φ, CYL.height / 2.0); |
40
1d865db9d3e4
Move rotate and reflect to alg_tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
1037 | let t = Loc([CYL.radius, 0.0]).rotate(φ); |
38
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1038 | let t_target = Loc([0.0, -CYL.radius / 2.0]); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1039 | let (q, t_left) = CYL.partial_exp(p.point, t); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1040 | check_point_eq!(q, q_target.point); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1041 | if let Some(t_left_) = t_left { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1042 | check_vec_eq!(t_left_, t_target); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1043 | } else { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1044 | panic!("No tangent left"); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1045 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1046 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1047 | |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1048 | for φ in angles { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1049 | let p = CYL.on_top(φ + π/2.0, CYL.radius); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1050 | let q_target = CYL.on_side(φ, CYL.height / 2.0); |
40
1d865db9d3e4
Move rotate and reflect to alg_tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
1051 | let t = 2.0 * Loc([CYL.radius, -CYL.radius]).rotate(φ); |
38
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1052 | let t_target = Loc([-CYL.radius, -CYL.radius]); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1053 | let (q, t_left) = CYL.partial_exp(p.point, t); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1054 | check_point_eq!(q, q_target.point); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1055 | if let Some(t_left_) = t_left { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1056 | check_vec_eq!(t_left_, t_target); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1057 | } else { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1058 | panic!("No tangent left"); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1059 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1060 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1061 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1062 | |
46 | 1063 | /// Tests for correct tangent calculation from the side to a cap. |
38
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1064 | #[test] |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1065 | fn side_top_exp() { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1066 | let π = f64::PI; |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1067 | let angles = [0.0, π/2.0, π*5.0/6.0, π*7.0/5.0]; |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1068 | |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1069 | for φ in angles { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1070 | let pt = CYL.on_top(φ, CYL.radius); |
40
1d865db9d3e4
Move rotate and reflect to alg_tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
1071 | let t = Loc([0.1, 0.3]).rotate(φ); |
38
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1072 | let b = pt.clone().exp(&t); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1073 | let a = pt.exp(&(-t)); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1074 | check_point_eq!(a.exp(&(2.0*t)).point, b.point); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1075 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1076 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1077 | |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1078 | /// Tests that tangent “returned” on edge from cap to top, correctly |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1079 | /// sums with a top tangent. |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1080 | #[test] |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1081 | fn cap_side_log_exp() { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1082 | let π = f64::PI; |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1083 | let angles = [0.0, π/2.0, π*5.0/6.0, π*7.0/5.0]; |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1084 | |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1085 | for top in [true, false] { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1086 | for (&φ, &ψ) in angles.iter().zip(angles.iter().rev()) { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1087 | let a = if top { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1088 | CYL.on_top(φ, CYL.radius/2.0) |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1089 | } else { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1090 | CYL.on_bottom(φ, CYL.radius/2.0) |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1091 | }; |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1092 | let b = CYL.on_side(ψ, 0.0); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1093 | let t = a.log(&b); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1094 | let (p@Point::Side(side), Some(tpb)) = CYL.partial_exp(a.point, t) else { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1095 | panic!("No tangent or point not on side"); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1096 | }; |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1097 | let pp = CYL.point_on(p); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1098 | let tpb2 = pp.log(&b); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1099 | let tap = a.log(&pp); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1100 | check_vec_eq!(tpb, tpb2); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1101 | if top { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1102 | assert!(indistinguishable(side.z, CYL.top_z())); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1103 | } else { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1104 | assert!(indistinguishable(side.z, CYL.bottom_z())); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1105 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1106 | let cap = CapPoint{ angle : side.angle, r : CYL.radius }; |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1107 | let tbpcap = cap.tangent_from_side(top, tpb); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1108 | check_vec_eq!(tap + tbpcap, t); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1109 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1110 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1111 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1112 | |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1113 | |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1114 | /// Tests that tangent “returned” on edge from top to side, correctly |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1115 | /// sums with a side tangent. |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1116 | #[test] |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1117 | fn side_cap_log_exp() { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1118 | let π = f64::PI; |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1119 | let angles = [0.0, π/2.0, π*5.0/6.0, π*7.0/5.0]; |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1120 | |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1121 | for top in [true, false] { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1122 | for (&φ, &ψ) in angles.iter().zip(angles.iter().rev()) { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1123 | let a = CYL.on_side(ψ, 0.0); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1124 | let b = if top { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1125 | CYL.on_top(φ, CYL.radius/2.0) |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1126 | } else { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1127 | CYL.on_bottom(φ, CYL.radius/2.0) |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1128 | }; |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1129 | let t = a.log(&b); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1130 | let (p, cap, tpb) = match CYL.partial_exp(a.point, t) { |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1131 | (p@Point::Top(cap), Some(tpb)) if top => (p, cap, tpb), |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1132 | (p@Point::Bottom(cap), Some(tpb)) if !top => (p, cap, tpb), |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1133 | _ => panic!("No tangent or point not on correct cap"), |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1134 | }; |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1135 | let pp = CYL.point_on(p); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1136 | let tpb2 = pp.log(&b); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1137 | let tap = a.log(&pp); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1138 | check_vec_eq!(tpb, tpb2); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1139 | let tbpside = cap.tangent_to_side(top, tpb); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1140 | check_vec_eq!(tap + tbpside, t); |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1141 | } |
63318d1b4f00
Fixes and unit tests for cylinder
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
1142 | } |
37 | 1143 | } |
1144 | } |