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