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