From ff25068fa8696180a935f735f4535c65e331e9c0 Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Mon, 16 Nov 2026 21:49:01 +0100 Subject: web: refit view parameters when switching projection View parameters are projection-specific: a cylindrical scale is linear in azimuth, a rectilinear one tangent-based. Switching projections kept the old parameters, so the display reinterpreted them; the first marker drag then refit in the NEW projection, converging to parameters that are valid there but garbage for the other one - and switching back showed that garbage until yet another refit happened to save it (exactly the reported panorama -> rect -> drag -> back -> crazy -> tiny-drag-fixes-it chain). * web: the projection dropdown now refits to the markers right after switching (no-op without markers), so parameters always match the active projection. * core: the wide-branch initial guess (markers >180 deg apart in the image, chosen by the sign of the pixel difference) is now gated to projections that can show more than a half-plane. Rectilinear (120 deg max view) always uses the shortest arc. * regression test: panorama fit -> rectilinear interlude -> refit recovers the panorama parameters exactly. --- crates/gipfel-core/src/lsq.rs | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) (limited to 'crates/gipfel-core/src') diff --git a/crates/gipfel-core/src/lsq.rs b/crates/gipfel-core/src/lsq.rs index 834fdfe..c501845 100644 --- a/crates/gipfel-core/src/lsq.rs +++ b/crates/gipfel-core/src/lsq.rs @@ -308,10 +308,16 @@ pub fn comp_params( let dx = m2.x - m1.x; let two_pi = 2.0 * std::f64::consts::PI; let mut d_img = crate::projection::wrap_angle(m2.alph - m1.alph); - if dx >= 0.0 && d_img <= 0.0 { - d_img += two_pi; - } else if dx < 0.0 && d_img >= 0.0 { - d_img -= two_pi; + // The wide branch (markers >180 deg apart in the image) only + // applies to projections that can actually show more than a + // half-plane; the rectilinear projection is limited to 120 deg + // of view, so there the shortest arc is always the intended one. + if proj.get_view_angle() >= std::f64::consts::PI { + if dx >= 0.0 && d_img <= 0.0 { + d_img += two_pi; + } else if dx < 0.0 && d_img >= 0.0 { + d_img -= two_pi; + } } let scale_tmp = dx.abs() / d_img.abs(); if scale_tmp.is_nan() || scale_tmp < 50.0 { @@ -558,6 +564,33 @@ mod tests { assert_wide_fit(false, 200.0); } + /// User scenario: fit distant peaks in panorama mode, switch to + /// rectilinear (which refits with tangent-based scale), switch back. + /// Refitting in the cylindrical projection must fully recover the + /// panorama parameters - the fitted parameters of one projection are + /// allowed to be garbage for the other, but must never poison a + /// subsequent refit. + #[test] + fn refit_recovers_after_rectilinear_interlude() { + let (proj, hills, known, truth) = wide_panorama(false, 200.0); + let mut pano = ViewParams::new(); + comp_params(&*proj, &hills, &known, &mut pano, false).unwrap(); + + // reinterpret + refit in rectilinear mode (200 deg exceeds its + // domain, so the outcome is projection-weird but must stay finite) + let rect = projection_from_type(ProjectionType::Rectilinear); + let mut mixed = pano; + let _ = comp_params(&*rect, &hills, &known, &mut mixed, false); + assert!(mixed.a_center.is_finite() && mixed.scale.is_finite()); + + // switch back and refit: panorama parameters must come back + let mut back = mixed; + comp_params(&*proj, &hills, &known, &mut back, false).unwrap(); + let d = crate::projection::wrap_angle(back.a_center - truth.a_center); + assert!(d.abs() < 1e-6, "a_center {}", back.a_center); + assert!((back.scale - truth.scale).abs() < 1.0, "scale {}", back.scale); + } + /// The cylindrical projection formulas do not contain the distortion /// parameters (k0/k1/x0) at all, and its trait derivative defaults are /// NaN - so "fit distortion" must be a no-op in panorama mode: the fit -- cgit v1.2.3