From c2834deb56723838dd8b63844b70931437eaf437 Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Thu, 12 Nov 2026 22:33:02 +0100 Subject: core: fix fit when north lies between the two markers With markers at e.g. 350 deg and 10 deg the raw azimuth difference is 340 deg instead of 20 deg. Two places choked on that: * ProjArgs::from_params fed the raw difference into the projection formulas. The cylindrical projection uses it LINEARLY (x = scale * (m_view - c_view)), so the 350 deg peak was projected 350 degrees of scale to the right of center instead of 10 degrees to the left - no parameter set can satisfy both markers and the panorama-mode fit fails. The rectilinear projection is periodic and unaffected. Fix: represent m_view as c_view + wrapped difference, so every consumer (fit residuals, Jacobian, project_point) sees the shortest arc. get_coordinates already normalized for exactly this reason; the fit path bypassed it. * comp_params derived the initial scale from |d_alph| = 340 deg, an initial scale off by a factor of 17 - this hit rectilinear mode too. Use the wrapped difference. Regression tests: two-marker fits across north for both projections (the cylindrical one fails without the fix), plus a direct check that a 350 deg peak projects to -10 deg * scale. --- crates/gipfel-core/src/projection/mod.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'crates/gipfel-core/src/projection') diff --git a/crates/gipfel-core/src/projection/mod.rs b/crates/gipfel-core/src/projection/mod.rs index 65b74e5..360c377 100644 --- a/crates/gipfel-core/src/projection/mod.rs +++ b/crates/gipfel-core/src/projection/mod.rs @@ -9,6 +9,21 @@ pub mod rectilinear; pub const NAN: f64 = f64::NAN; +/// Wrap an angle into (-pi, pi]. Non-finite input yields NaN, which the +/// projection callers filter on. +pub fn wrap_angle(mut a: f64) -> f64 { + if !a.is_finite() { + return NAN; + } + a %= 2.0 * std::f64::consts::PI; + if a > std::f64::consts::PI { + a -= 2.0 * std::f64::consts::PI; + } else if a <= -std::f64::consts::PI { + a += 2.0 * std::f64::consts::PI; + } + a +} + /// The 7 fitted view parameters (radians where angular). #[derive(Clone, Copy, Debug, Default, PartialEq)] pub struct ViewParams { @@ -58,6 +73,14 @@ pub struct ProjArgs { impl ProjArgs { pub fn from_params(p: &ViewParams, m_view: f64, m_elev: f64) -> Self { + // Represent the mountain azimuth such that the difference + // `m_view - c_view` that every generated formula uses is the + // shortest one. The cylindrical projection uses that difference + // linearly (x = scale * delta_az), so a peak at 350 deg seen with + // the camera centered at 10 deg must contribute -20 deg, not +340. + // The rectilinear projection is periodic and unaffected, but is + // served equally well by the wrapped value. + let m_view = p.a_center + wrap_angle(m_view - p.a_center); ProjArgs { c_view: p.a_center, c_pitch: p.a_pitch, -- cgit v1.2.3