diff options
| author | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2026-11-16 22:34:35 +0100 |
|---|---|---|
| committer | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2026-11-16 22:34:35 +0100 |
| commit | ac72f8e2d88cb10315abac3d1ceaa5252bc36ff5 (patch) | |
| tree | a8cbb5b621c3dd8f14bdbe9e95784e5c6816a814 /crates/gipfel-core/src | |
| parent | f2f2798e60acdb995924eed66864a106735bd3d3 (diff) | |
core: validity-filter the multi-start, don't guard after the fact
The real cause of the placement-dependent 'fit failed': the exactly-
determined two-marker system has SEVERAL EXACT solutions - cost ties.
Degenerate ones (scale ~30 px, center at a marker azimuth +/- 90 deg,
where the tangent saturates) match both markers perfectly, won the
lowest-cost race arbitrarily, and were then rejected by the post-hoc
guards. A good solution existed but lost the tie -> random failures
that flipped when a marker moved a few pixels.
Fix: validity (scale >= 50, every known marker inside the projection
domain) now filters candidates INSIDE the seed loop, before the cost
comparison. First near-exact valid result (cost < 1 px^2) ends the
search early, which also makes the typical fit ~75x faster. If no
seed yields a valid fit, a plain run follows so the guards still
produce the most informative error.
Measured on a simulated Thaneller flow (Zugspitze fixed, Saeuling
dragged over a 41x21 grid around its true spot): failures dropped
from 163/861 to 0/861. On a deliberately absurd 61x17 grid (markers
up to 3000 px off): domain rejections 103 -> 0, remaining failures
are honest 'cannot determine scale' cases (marker in the other
peak's x-column).
Diffstat (limited to 'crates/gipfel-core/src')
| -rw-r--r-- | crates/gipfel-core/src/lsq.rs | 61 |
1 files changed, 44 insertions, 17 deletions
diff --git a/crates/gipfel-core/src/lsq.rs b/crates/gipfel-core/src/lsq.rs index bb2030b..81d25f5 100644 --- a/crates/gipfel-core/src/lsq.rs +++ b/crates/gipfel-core/src/lsq.rs @@ -388,6 +388,34 @@ pub fn comp_params( eprintln!("Performing calibration"); } + // Sanity domain for fitted solutions: rectilinear is only valid for + // |delta azimuth| < 90 deg (the tangent flips sign beyond it), + // cylindrical wraps at 180 deg. Deliberately wider than + // get_view_angle()'s 60 deg visibility heuristic - wide-angle photos + // legitimately show peaks 60-85 deg off center. + let domain = if proj.get_view_angle() >= std::f64::consts::PI { + std::f64::consts::PI + } else { + std::f64::consts::PI / 2.0 + }; + // A candidate fit is only usable if its scale is sane and every known + // marker stays inside the domain. The exactly-determined two-marker + // system has several EXACT solutions (cost ties via the tangent's + // periodicity per marker), and degenerate ones (scale ~ 30 px, center + // at a marker azimuth +/- 90 deg) match both markers perfectly and + // would otherwise win the cost race arbitrarily - only to be rejected + // by the guards below, producing "fit failed" even though a good + // solution existed. Validity must filter candidates BEFORE the cost + // comparison, not after. + let valid = |p: &ViewParams| -> bool { + if !(p.scale.is_finite() && p.scale >= 50.0) { + return false; + } + known + .iter() + .all(|&i| crate::projection::wrap_angle(hills[i].alph - p.a_center).abs() < domain) + }; + let level = if n == 1 { 0 } else { 1 }; if n > 1 { // Multi-start. The two-marker system is exactly determined and has @@ -403,7 +431,7 @@ pub fn comp_params( // across the full circle (60 deg apart). 6 centers x 2 scales x // 2 pitches = 24 tiny fits, still microseconds. let c0 = new_parms.a_center; - for k in 0..6usize { + 'seeds: for k in 0..6usize { let c_seed = crate::projection::wrap_angle(c0 + k as f64 * (std::f64::consts::TAU / 6.0)); for &s0 in &seed_scales { for &p0 in &[0.0f64, pitch_seed(hills, known, s0)] { @@ -412,14 +440,23 @@ pub fn comp_params( cand.scale = s0; cand.a_pitch = p0; let cost = lsq(proj, hills, known, &mut cand, level); - if cost.is_finite() && best.as_ref().map_or(true, |(c, _)| cost < *c) { + if cost.is_finite() && valid(&cand) && best.as_ref().map_or(true, |(c, _)| cost < *c) { best = Some((cost, cand)); + // near-exact and valid: stop searching + if cost < 1.0 { + break 'seeds; + } } } } } - if let Some((_, p)) = best { - new_parms = p; + match best { + Some((_, p)) => new_parms = p, + // No seed produced a valid fit: run the plain seeded fit so + // the guards below report the most informative reason. + None => { + lsq(proj, hills, known, &mut new_parms, level); + } } } else { lsq(proj, hills, known, &mut new_parms, level); @@ -432,19 +469,9 @@ pub fn comp_params( return Err("Could not determine reasonable view parameters".to_string()); } - // A fit that parks a marked peak outside the projection's true domain - // is nonsense: rectilinear is only valid for |delta azimuth| < 90 deg - // (the tangent flips sign beyond it), cylindrical wraps at 180 deg. - // Note this is deliberately wider than get_view_angle()'s 60 deg - // visibility heuristic - wide-angle photos legitimately show peaks - // 60-85 deg off center, and rejecting those caused placement- - // dependent "fit failed" errors. Catches the camera-looking- - // backwards solutions and grossly misplaced markers. - let domain = if proj.get_view_angle() >= std::f64::consts::PI { - std::f64::consts::PI - } else { - std::f64::consts::PI / 2.0 - }; + // Reject fits that park a marked peak outside the projection's true + // domain (see `domain` above): catches camera-looking-backwards + // solutions and grossly misplaced markers. for &i in known { let d = crate::projection::wrap_angle(hills[i].alph - new_parms.a_center).abs(); if d >= domain { |
