From 45f4d6b2ebcd52857f9ecc480a6fdcb11cb6e24d Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Mon, 16 Nov 2026 23:01:33 +0100 Subject: core: a fit must actually fit - reject huge residual RMS User session log (browser console) showed the failure mode precisely: a panorama (Saeuling az 11.2 at px 150, Zugspitze az 91.7 at px 3398, linear pixel spacing) switched to rectilinear mode. Every seed landed at cost ~12900 (RMS ~80 px) and the fitter committed the garbage (pitch -81 deg, scale 91) because it merely passed the sanity filters. Rectilinear geometry cannot represent linear pixel spacing over wide spans: for an azimuth pair d apart, the x-ratio has a hard minimum (~5 for d = 40.9 deg, and it grows with d), so such marker sets have provably NO exact solution - the old 'always solvable' assumption held only for tan-consistent spacings. comp_params now computes rms = sqrt(cost/n) from the final cost and rejects rms > 30 px with: Fit cannot match the markers (RMS 80 px) - this projection cannot represent the marked geometry. Is this a panorama? Switch to cylindrical mode. Regression test uses the user's exact captured marker data. Also fixed a synthetic test placement (800/300 px for a 40.9 deg pair) that was itself rectilinearly impossible - the guard caught it, which is a nice independent confirmation. --- crates/gipfel-core/src/lsq.rs | 61 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/crates/gipfel-core/src/lsq.rs b/crates/gipfel-core/src/lsq.rs index f395f29..84717b5 100644 --- a/crates/gipfel-core/src/lsq.rs +++ b/crates/gipfel-core/src/lsq.rs @@ -509,6 +509,7 @@ pub fn comp_params( }; let level = if n == 1 { 0 } else { 1 }; + let mut final_cost; if n > 1 { // Multi-start. The two-marker system is exactly determined and has // multiple solution basins: the tangent repeats every 180 deg in @@ -559,27 +560,28 @@ pub fn comp_params( p.a_roll.to_degrees(), p.scale )); + final_cost = c; new_parms = p; } // No seed produced a valid fit: run the plain seeded fit so // the guards below report the most informative reason. None => { flog("no valid seed result; plain fit for diagnostics".to_string()); - lsq(proj, hills, known, &mut new_parms, level); + final_cost = lsq(proj, hills, known, &mut new_parms, level); } } } else { - let cost = lsq(proj, hills, known, &mut new_parms, level); + final_cost = lsq(proj, hills, known, &mut new_parms, level); flog(format!( - "single-marker fit cost={cost:.3} center={:.3} pitch={:.3}", + "single-marker fit cost={final_cost:.3} center={:.3} pitch={:.3}", new_parms.a_center.to_degrees(), new_parms.a_pitch.to_degrees() )); } if n >= 4 && fit_distortion && supports_distortion { - let cost = lsq(proj, hills, known, &mut new_parms, 2); + final_cost = lsq(proj, hills, known, &mut new_parms, 2); flog(format!( - "distortion fit cost={cost:.3} k0={:.5} k1={:.7} x0={:.1}", + "distortion fit cost={final_cost:.3} k0={:.5} k1={:.7} x0={:.1}", new_parms.k0, new_parms.k1, new_parms.x0 @@ -608,6 +610,20 @@ pub fn comp_params( } } + // The fit must actually MATCH the markers. cost = 0.5 * sum(resid^2) + // over 2n residuals, so rms = sqrt(cost/n). A huge residual means the + // marked geometry cannot be represented in this projection mode at + // all - e.g. a panorama (linear pixel spacing) fitted rectinearly has + // provably no exact solution for wide spans; the optimizer then + // commits absurd parameters (pitch -80 deg, scale at the sanity + // floor) that pass the other guards but never match the markers. + let rms = (final_cost / n as f64).max(0.0).sqrt(); + if final_cost.is_finite() && rms > 30.0 { + return bail(format!( + "Fit cannot match the markers (RMS {rms:.0} px) - this projection cannot represent the marked geometry. Is this a panorama? Switch to cylindrical mode." + )); + } + *parms = new_parms; Ok(()) } @@ -992,7 +1008,10 @@ mod tests { #[test] fn correct_order_fits_at_any_spacing() { let proj = projection_from_type(ProjectionType::Rectilinear); - for (x1, x2) in [(500.0, -500.0), (2000.0, -2000.0), (800.0, 300.0)] { + // (800, 100): both right of center - fittable because the x + // ratio (8) exceeds the minimum a 40.9 deg azimuth pair can + // achieve rectinearly (~5); (800, 300) would NOT be fittable. + for (x1, x2) in [(500.0, -500.0), (2000.0, -2000.0), (800.0, 100.0)] { let mut hills = Vec::new(); for (name, az) in [("Zugspitze", 58.4f64), ("Saeuling", 17.5)] { let mut h = Hill::new(name, 0.0, 0.0, 2500.0); @@ -1001,7 +1020,9 @@ mod tests { hills.push(h); } hills[0].x = x1; - hills[0].y = -200.0; + // same elevation -> same y (consistent with roll = 0, so an + // exact solution exists at every spacing) + hills[0].y = -100.0; hills[1].x = x2; hills[1].y = -100.0; let mut p = ViewParams::new(); @@ -1009,4 +1030,30 @@ mod tests { .unwrap_or_else(|e| panic!("spacing {}: {e}", x1 - x2)); } } + + /// Real user session (captured via the fit log): a panorama with + /// Saeuling az 11.24 at px 150 and Zugspitze az 91.68 at px 3398 - + /// linear pixel spacing, 80.4 deg apart. Rectilinearly there is NO + /// exact solution (the required tangent ratio has no real root), so + /// the optimizer lands at cost ~12900 (RMS ~80 px) with absurd + /// parameters (pitch -81 deg, scale 91). The RMS guard must reject + /// that instead of committing it. + #[test] + fn panorama_in_rectilinear_rejected_by_rms() { + let proj = projection_from_type(ProjectionType::Rectilinear); + let mut hills = Vec::new(); + for (name, az, el) in [("SAEULING", 11.24f64, -1.58f64), ("ZUGSPITZE", 91.68, 1.76)] { + let mut h = Hill::new(name, 0.0, 0.0, 2500.0); + h.alph = az.to_radians(); + h.a_elev = el.to_radians(); + hills.push(h); + } + hills[0].x = 150.0; + hills[0].y = -493.0; + hills[1].x = 3398.0; + hills[1].y = -651.0; + let mut p = ViewParams::new(); + let e = comp_params(&*proj, &hills, &[0, 1], &mut p, false).unwrap_err(); + assert!(e.contains("RMS"), "expected RMS rejection, got: {e}"); + } } -- cgit v1.2.3