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/lsq.rs | 74 +++++++++++++++++++++++++++++++- crates/gipfel-core/src/projection/mod.rs | 23 ++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/gipfel-core/src/lsq.rs b/crates/gipfel-core/src/lsq.rs index 6a9d6c7..87e68af 100644 --- a/crates/gipfel-core/src/lsq.rs +++ b/crates/gipfel-core/src/lsq.rs @@ -298,7 +298,12 @@ pub fn comp_params( if n > 1 { let m2 = &hills[known[1]]; - let scale_tmp = (m2.x - m1.x).abs() / (m2.alph - m1.alph).abs(); + // The azimuth span between the two markers must be the shortest + // angular difference: with north between the peaks (e.g. 350 deg + // and 10 deg) the raw difference is 340 deg, giving an initial + // scale off by a factor of 17 and a fit that never recovers. + let d_alph = crate::projection::wrap_angle(m2.alph - m1.alph); + let scale_tmp = (m2.x - m1.x).abs() / d_alph.abs(); if scale_tmp.is_nan() || scale_tmp < 50.0 { return Err(format!("Could not determine initial scale value ({scale_tmp})")); } @@ -389,4 +394,71 @@ mod tests { assert!((p.k0 - truth.k0).abs() < 1e-5, "k0={}", p.k0); assert!((p.k1 - truth.k1).abs() < 1e-7, "k1={}", p.k1); } + + /// Two markers with north between them: one peak west of north + /// (azimuth 350 deg), one east (10 deg). The raw azimuth difference + /// is 340 deg; everything must operate on the wrapped 20 deg instead. + /// Regression test for the panorama-mode fit failure across north + /// (the cylindrical projection uses m_view - c_view linearly). + fn north_crossing(ty: ProjectionType) -> (Box, Vec, Vec, ViewParams) { + let proj = projection_from_type(ty); + let truth = ViewParams { + a_center: 0.0, + scale: 2000.0, + a_pitch: 0.03, + a_roll: 0.0, + k0: 0.0, + k1: 0.0, + x0: 0.0, + }; + let mut hills = Vec::new(); + for (i, (az_deg, el)) in [(350.0f64, 0.05f64), (10.0, 0.08)].iter().enumerate() { + let mut h = Hill::new(&format!("n{i}"), 0.0, 0.0, 2000.0); + h.alph = az_deg.to_radians(); + h.a_elev = *el; + let (x, y) = proj.get_coordinates(h.alph, h.a_elev, &truth); + h.x = x; + h.y = y; + hills.push(h); + } + (proj, hills, vec![0, 1], truth) + } + + fn assert_north_crossing_fits(ty: ProjectionType) { + let (proj, hills, known, truth) = north_crossing(ty); + let mut p = ViewParams::new(); + comp_params(&*proj, &hills, &known, &mut p, false).unwrap(); + let d_center = crate::projection::wrap_angle(p.a_center - truth.a_center); + assert!( + d_center.abs() < 1e-6, + "{ty:?}: a_center off: got {} rad, want 0", + p.a_center + ); + assert!((p.scale - truth.scale).abs() < 1.0, "{ty:?}: scale {}", p.scale); + assert!((p.a_pitch - truth.a_pitch).abs() < 1e-6, "{ty:?}: pitch {}", p.a_pitch); + } + + #[test] + fn fit_across_north_cylindrical() { + assert_north_crossing_fits(ProjectionType::Cylindrical); + } + + #[test] + fn fit_across_north_rectilinear() { + assert_north_crossing_fits(ProjectionType::Rectilinear); + } + + #[test] + fn projection_across_north_uses_shortest_arc() { + // cylindrical: x is linear in the azimuth difference, so a peak at + // 350 deg with the center at 0 deg must land at -10 deg * scale, + // not +350 deg * scale + let proj = projection_from_type(ProjectionType::Cylindrical); + let p = ViewParams::new(); + let (x, _) = proj.get_coordinates(350f64.to_radians(), 0.0, &p); + assert!( + (x - (-10f64.to_radians() * p.scale)).abs() < 1e-6, + "x={x}" + ); + } } 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