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