From 52084b49a323ae9a974e2787fe034a06be3ae758 Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Mon, 16 Nov 2026 23:08:41 +0100 Subject: core: pre-check same-side feasibility, suggest panorama mode up front For a rectilinear image, two markers with azimuth separation D that sit on the SAME side of the image center must obey |x_far / x_near| >= tan^2(45deg + D/2) (the minimum of tan(u+D)/tan(u) over all valid view centers). Below that no center/scale exists - the typical cause being a panorama (linear pixel spacing) fitted in rectilinear mode. comp_params now tests every marker pair against this bound BEFORE the optimizer runs and answers e.g. SAEULING and ZUGSPITZE are 80.4 deg apart in azimuth but only 22.7x apart in the image (both right of center); rectilinear geometry needs at least 140x for a pair this wide. This looks like a panorama - switch to cylindrical mode, or place the view center between the peaks. The bound diverges as D -> 90 deg, so same-side pairs 90+ deg apart are correctly always rejected. A 10% tolerance absorbs the elevation/pitch/ distortion perturbations of the pure-tangent model; the RMS guard from the previous commit remains as the backstop for other unsatisfiable marker sets (e.g. y-inconsistent placements). Tests: the user's captured panorama markers now produce the panorama hint in rectilinear mode and fit exactly in cylindrical mode; a same-side pair is verified to fit just above the ratio bound and be named-and-rejected just below it. --- crates/gipfel-core/src/lsq.rs | 94 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 87 insertions(+), 7 deletions(-) (limited to 'crates/gipfel-core') diff --git a/crates/gipfel-core/src/lsq.rs b/crates/gipfel-core/src/lsq.rs index 84717b5..5e9c28c 100644 --- a/crates/gipfel-core/src/lsq.rs +++ b/crates/gipfel-core/src/lsq.rs @@ -435,6 +435,52 @@ pub fn comp_params( // branch just chosen) instead of on the first marker: with markers // 340 deg apart, m1.alph is 170 deg off and the fit never recovers. new_parms.a_center = crate::projection::wrap_angle(m1.alph + d_img / 2.0); + // Rectilinear feasibility for same-side marker pairs. With + // x = s*tan(az - center), two peaks with azimuth separation D that + // both sit on the SAME side of the image center must satisfy + // |x_far / x_near| >= min over u of tan(u+D)/tan(u) = tan^2(45deg + + // D/2). Below that no center/scale exists - the classic case being + // a panorama (linear pixel spacing) fitted rectinearly. Detect it + // up front and name the fix instead of letting the optimizer churn + // and the RMS guard reject generically. The 0.9 tolerance absorbs + // elevation/pitch/distortion perturbations of the pure tan model. + if proj.get_view_angle() < std::f64::consts::PI { + for &i in known { + for &j in known { + if i == j { + continue; + } + let (near, far) = if hills[i].x.abs() <= hills[j].x.abs() { + (&hills[i], &hills[j]) + } else { + (&hills[j], &hills[i]) + }; + // opposite sides of center: always fittable (center + // between the peaks); a near marker at the center + // makes the ratio arbitrarily large + if near.x * far.x <= 0.0 || near.x.abs() < 5.0 { + continue; + } + let d = crate::projection::wrap_angle(far.alph - near.alph).abs(); + // tan^2(45deg + D/2); -> infinity as D -> 90deg, which + // correctly makes same-side pairs with D >= 90deg + // impossible + let min_ratio = (std::f64::consts::FRAC_PI_4 + d / 2.0).tan().powi(2); + let ratio = far.x.abs() / near.x.abs(); + if ratio < min_ratio * 0.9 { + return bail(format!( + "{} and {} are {:.1} deg apart in azimuth but only {:.1}x apart in the image (both {} of center); rectilinear geometry needs at least {:.0}x for a pair this wide. This looks like a panorama - switch to cylindrical mode, or place the view center between the peaks.", + near.name, + far.name, + d.to_degrees(), + ratio, + if far.x >= 0.0 { "right" } else { "left" }, + min_ratio + )); + } + } + } + } // Seed scales for the multi-start below: the linear estimate and a // tangent-aware variant (rectilinear geometry puts dx = s * 2 * // tan(d/2) for a centered pair, which the linear estimate @@ -1033,13 +1079,12 @@ mod tests { /// 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. + /// linear pixel spacing, 80.4 deg apart, both right of center. + /// Rectilinearly there is NO exact solution (same-side pairs 80.4 deg + /// apart need a >= 140x pixel ratio; these are 22.7x apart). The + /// pre-check must catch this and suggest cylindrical mode. #[test] - fn panorama_in_rectilinear_rejected_by_rms() { + fn panorama_in_rectilinear_suggests_cylindrical() { 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)] { @@ -1054,6 +1099,41 @@ mod tests { 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}"); + assert!( + e.contains("panorama") && e.contains("cylindrical"), + "expected panorama suggestion, got: {e}" + ); + // the same markers fit exactly in cylindrical mode + let cyl = projection_from_type(ProjectionType::Cylindrical); + let mut p = ViewParams::new(); + comp_params(&*cyl, &hills, &[0, 1], &mut p, false).unwrap(); + } + + /// Same-side pair just above the feasibility bound fits; just below + /// it is rejected with the panorama hint (tan^2(45 + 40.9/2) ~ 4.8 + /// minimum ratio for this 40.9 deg pair). + #[test] + fn same_side_ratio_bound() { + let proj = projection_from_type(ProjectionType::Rectilinear); + let mk = |x_far: f64| { + 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); + h.alph = az.to_radians(); + h.a_elev = 0.05; + hills.push(h); + } + hills[0].x = x_far; + hills[0].y = -100.0; + hills[1].x = 100.0; + hills[1].y = -100.0; + hills + }; + let mut p = ViewParams::new(); + // ratio 10 > 4.8: fittable + comp_params(&*proj, &mk(1000.0), &[0, 1], &mut p, false).unwrap(); + // ratio 3 < 4.8: impossible, named up front + let e = comp_params(&*proj, &mk(300.0), &[0, 1], &mut p, false).unwrap_err(); + assert!(e.contains("cylindrical"), "got: {e}"); } } -- cgit v1.2.3