From 9f4700da89f9ceda9f67ffe4b4d073dd68ecd739 Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Mon, 16 Nov 2026 23:16:38 +0100 Subject: core: review cleanup - drop the brute-force parts of the fit hunt With the real issue identified (panorama geometry fitted rectinearly: provably unsatisfiable marker sets) the pieces added while chasing it were re-evaluated against a 543-placement sweep of VALID geometries (straddle + same-side pairs, north crossings, pitch/roll spread, marker noise, cylindrical wide pairs): - center seeds: the six-way full-circle spread is unnecessary; the placement roulette it was built against was the degenerate cost ties (fixed by the validity filter) and the panorama phantom. A single derived center seed passes all 543 valid placements. The principled scale (linear + tangent-aware) and pitch seeds stay: 4 fits total. - final domain guard: redundant. The validity filter already enforces the same condition before a candidate can be chosen; the fallback path is caught by the RMS guard with a better message. - the sweep itself is promoted to tests/fit_sweep.rs as the permanent regression net for the seed/validity logic. Kept (each fixes a real, independent bug): repaint + refit on projection switch, validity-filtered candidate selection, marker order check, same-side feasibility pre-check with panorama hint, RMS guard, fit session logging (the instrument that found the issue). 43 core + 13 golden + 543-placement sweep green, clippy clean. --- crates/gipfel-core/src/lsq.rs | 78 +++++++++---------------- crates/gipfel-core/tests/fit_sweep.rs | 106 ++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 50 deletions(-) create mode 100644 crates/gipfel-core/tests/fit_sweep.rs diff --git a/crates/gipfel-core/src/lsq.rs b/crates/gipfel-core/src/lsq.rs index 4e8be31..182cd84 100644 --- a/crates/gipfel-core/src/lsq.rs +++ b/crates/gipfel-core/src/lsq.rs @@ -559,42 +559,35 @@ 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 - // the center (camera looking backwards projects identically), and - // pitch/roll can absorb marker y errors. A single start can land - // in a spurious minimum depending on where the user last dragged - // a marker - the "randomly sometimes fit failed" report. Try a - // small set of seeds and keep the lowest-cost result. + // Small seed grid. The exactly-determined two-marker system has + // multiple solution basins (the tangent repeats every 180 deg in + // the center), so a handful of principled starts are tried and + // the best VALID one wins: the derived center, two scale + // estimates (linear and tangent-aware), with and without the + // y-derived pitch seed. The validity filter below - not the seed + // spread - is what keeps degenerate cost-ties from winning. let mut best: Option<(f64, ViewParams)> = None; - // The center seed derived from the two markers can itself be way - // off when marker placements are rough; spread six center seeds - // 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; - '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)] { - let mut cand = new_parms; - cand.a_center = c_seed; - cand.scale = s0; - cand.a_pitch = p0; - let cost = lsq(proj, hills, known, &mut cand, level); - flog(format!( - "seed c={:>7.1} s={:>6.0} p={:>6.2} cost={:>12.3} {}", - c_seed.to_degrees(), - s0, - p0.to_degrees(), - cost, - if valid(&cand) { "valid" } else { "INVALID" } - )); - 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; - } + let c_seed = new_parms.a_center; + 'seeds: for &s0 in &seed_scales { + for &p0 in &[0.0f64, pitch_seed(hills, known, s0)] { + let mut cand = new_parms; + cand.a_center = c_seed; + cand.scale = s0; + cand.a_pitch = p0; + let cost = lsq(proj, hills, known, &mut cand, level); + flog(format!( + "seed c={:>7.1} s={:>6.0} p={:>6.2} cost={:>12.3} {}", + c_seed.to_degrees(), + s0, + p0.to_degrees(), + cost, + if valid(&cand) { "valid" } else { "INVALID" } + )); + 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; } } } @@ -643,21 +636,6 @@ pub fn comp_params( )); } - // 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 { - return bail(format!( - "Fit result outside the projection's domain (marker {} at {:.1} deg from center, limit {:.0} deg) - is this a panorama? Switch to cylindrical mode", - hills[i].name, - d.to_degrees(), - domain.to_degrees() - )); - } - } - // 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 diff --git a/crates/gipfel-core/tests/fit_sweep.rs b/crates/gipfel-core/tests/fit_sweep.rs new file mode 100644 index 0000000..70ea780 --- /dev/null +++ b/crates/gipfel-core/tests/fit_sweep.rs @@ -0,0 +1,106 @@ +// Regression sweep over VALID two-marker placements: ground-truth +// projections (straddling and same-side pairs, north crossings, pitch +// and roll spread, marker noise, cylindrical wide pairs) fitted from a +// neutral start. Every placement here must fit; this is the net that +// guards the seed/validity logic in comp_params. +use gipfel_core::hill::Hill; +use gipfel_core::projection::{projection_from_type, ProjectionType, ViewParams}; + +fn mk(pan_az: f64, pan_el: f64, x: f64, y: f64) -> Hill { + let mut h = Hill::new("h", 0.0, 0.0, 2000.0); + h.alph = pan_az.to_radians(); + h.a_elev = pan_el.to_radians(); + h.x = x; + h.y = y; + h +} + +fn fit_two(proj: ProjectionType, hills: &[Hill]) -> Result { + let p = projection_from_type(proj); + let mut v = ViewParams::new(); + match gipfel_core::lsq::comp_params(&*p, hills, &[0, 1], &mut v, false) { + Ok(()) => Ok(v), + Err(e) => Err(e), + } +} + +#[test] +fn sweep() { + let mut fails = 0; + let mut total = 0; + // Ground-truth generated pairs: project with known params, add noise. + for ¢er in &[0.0, 45.0, 90.0, 180.0, 350.0f64] { + for &scale in &[600.0, 1500.0, 3000.0] { + for &pitch in &[-15.0, 0.0, 12.0f64] { + for &span in &[8.0, 25.0, 55.0, 85.0f64] { + // straddling center + let projr = projection_from_type(ProjectionType::Rectilinear); + let truth = ViewParams { + a_center: center.to_radians(), + scale, + a_pitch: pitch.to_radians(), + a_roll: 0.05_f64, + k0: 0.0, + k1: 0.0, + x0: 0.0, + }; + let a1 = center - span / 2.0; + let a2 = center + span / 2.0; + let (x1, y1) = projr.get_coordinates(a1.to_radians(), 0.03, &truth); + let (x2, y2) = projr.get_coordinates(a2.to_radians(), 0.06, &truth); + for &noise in &[0.0, 5.0f64] { + let hills = vec![ + mk(a1, 0.03, x1 + noise, y1 - noise), + mk(a2, 0.06, x2 - noise, y2 + noise), + ]; + total += 1; + if fit_two(ProjectionType::Rectilinear, &hills).is_err() { + fails += 1; + panic!("straddle c={center} s={scale} p={pitch} span={span} n={noise}: {}", fit_two(ProjectionType::Rectilinear, &hills).unwrap_err()); + } + } + // same-side pair: both azimuths right of center + let a3 = center + 5.0; + let a4 = center + 5.0 + span.min(60.0); + let (x3, y3) = projr.get_coordinates(a3.to_radians(), 0.03, &truth); + let (x4, y4) = projr.get_coordinates(a4.to_radians(), 0.06, &truth); + if x3.abs() > 20.0 && x4.abs() > 20.0 && (x4 / x3).is_finite() { + let hills = vec![ + mk(a3, 0.03, x3, y3), + mk(a4, 0.06, x4, y4), + ]; + total += 1; + if fit_two(ProjectionType::Rectilinear, &hills).is_err() { + fails += 1; + panic!("same-side c={center} s={scale} p={pitch} span={span}: {}", fit_two(ProjectionType::Rectilinear, &hills).unwrap_err()); + } + } + } + } + } + } + // cylindrical wide pairs incl. seam straddle + for &dc in &[70.0, 150.0, 175.0f64] { + let cyl = projection_from_type(ProjectionType::Cylindrical); + let truth = ViewParams { + a_center: 170.0_f64.to_radians(), + scale: 200.0, + a_pitch: -0.1, + a_roll: 0.0, + k0: 0.0, + k1: 0.0, + x0: 0.0, + }; + let a1 = 170.0 - dc / 2.0; + let a2 = 170.0 + dc / 2.0; + let (x1, y1) = cyl.get_coordinates(a1.to_radians(), 0.03, &truth); + let (x2, y2) = cyl.get_coordinates(a2.to_radians(), 0.05, &truth); + let hills = vec![mk(a1, 0.03, x1, y1), mk(a2, 0.05, x2, y2)]; + total += 1; + if fit_two(ProjectionType::Cylindrical, &hills).is_err() { + fails += 1; + panic!("cyl dc={dc}: {}", fit_two(ProjectionType::Cylindrical, &hills).unwrap_err()); + } + } + assert_eq!(fails, 0, "{total} placements tested"); +} -- cgit v1.2.3