diff options
| author | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2026-11-12 22:51:46 +0100 |
|---|---|---|
| committer | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2026-11-12 22:51:46 +0100 |
| commit | 6902c44a6e0b9dc591923da203e2d9983458cd4c (patch) | |
| tree | 1c0b6c2527a4ad4364291e2bc619929053aa66dc /crates | |
| parent | d34f271864c98c67b0bd403f7e1b7e01b55bf8ef (diff) | |
core: fix fit for wide panoramas (markers >180 deg apart in image)
The initial guess in comp_params assumed the two markers are less than
180 deg apart: a_center started on the first marker and the scale came
from the shortest-arc azimuth difference. In panoramas wider than
180 deg the markers can straddle the seam - e.g. at -170 deg and
+170 deg of the view center they are 340 deg apart in the image but
only 20 deg on the compass. The start was then off by up to 170 deg in
azimuth and a factor of 17 in scale, and the fit never recovered.
Fix: derive the image-space azimuth difference from the SIGN OF THE
PIXEL DIFFERENCE (the user's marker placement tells us which branch
was meant; the magnitude is unique modulo 2pi for panoramas below
360 deg), and start the view center between the markers in that
branch.
The golden fit tests now compare a_center modulo 2pi: the C harness
settles wherever its >2pi input azimuths put it (380 deg), the Rust
port normalizes (20 deg) - same physical solution.
Tests: fits with markers 340 deg apart, 350 deg apart in reversed
marker order, and 200 deg apart (just over the seam). All three fail
without the fix (verified), pass with it.
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/gipfel-core/src/lsq.rs | 95 | ||||
| -rw-r--r-- | crates/gipfel-core/tests/golden.rs | 12 |
2 files changed, 99 insertions, 8 deletions
diff --git a/crates/gipfel-core/src/lsq.rs b/crates/gipfel-core/src/lsq.rs index 87e68af..f85e0ef 100644 --- a/crates/gipfel-core/src/lsq.rs +++ b/crates/gipfel-core/src/lsq.rs @@ -298,17 +298,31 @@ pub fn comp_params( if n > 1 { let m2 = &hills[known[1]]; - // The azimuth span between the two markers must be the shortest - // angular difference: with north between the peaks (e.g. 350 deg - // and 10 deg) the raw difference is 340 deg, giving an initial - // scale off by a factor of 17 and a fit that never recovers. - let d_alph = crate::projection::wrap_angle(m2.alph - m1.alph); - let scale_tmp = (m2.x - m1.x).abs() / d_alph.abs(); + // Azimuth difference as SEEN IN THE IMAGE. The shortest-arc wrap + // alone picks the wrong branch in panoramas wider than 180 deg: + // markers at -170 deg and +170 deg of the view center are 340 deg + // apart in the image but only 20 deg apart on the compass. The + // sign of the pixel difference tells us which branch the user + // intended; the magnitude is then unique modulo 2pi (panoramas + // below 360 deg). + 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; + } + let scale_tmp = dx.abs() / d_img.abs(); if scale_tmp.is_nan() || scale_tmp < 50.0 { return Err(format!("Could not determine initial scale value ({scale_tmp})")); } new_parms.scale = scale_tmp; new_parms.a_roll = 0.0; + // Start the view center between the two markers (in the image-space + // 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); } if n > 3 && fit_distortion { @@ -461,4 +475,73 @@ mod tests { "x={x}" ); } + + /// Wide panoramas: two markers more than 180 deg apart IN THE IMAGE, + /// i.e. straddling the seam at +/-180 deg from the view center. The + /// compass difference is then small (e.g. 20 deg) while the image + /// difference is large (340 deg); the initial guess must pick the + /// right branch. Only meaningful for the cylindrical projection - + /// rectilinear cannot exceed 180 deg of view angle. + fn wide_panorama(reversed: bool, span_deg: f64) -> (Box<dyn Projection>, Vec<Hill>, Vec<usize>, ViewParams) { + let proj = projection_from_type(ProjectionType::Cylindrical); + let truth = ViewParams { + a_center: 0.0, + scale: 2000.0, + a_pitch: 0.03, + a_roll: 0.0, + k0: 0.0, + k1: 0.0, + x0: 0.0, + }; + let half = (span_deg / 2.0).to_radians(); + let mut hills = Vec::new(); + for (i, az) in [half, -half].iter().enumerate() { + let mut h = Hill::new(&format!("w{i}"), 0.0, 0.0, 2500.0); + h.alph = *az; + h.a_elev = 0.05 + 0.02 * i as f64; + let (x, y) = proj.get_coordinates(h.alph, h.a_elev, &truth); + h.x = x; + h.y = y; + hills.push(h); + } + let known = if reversed { vec![1, 0] } else { vec![0, 1] }; + (proj, hills, known, truth) + } + + fn assert_wide_fit(reversed: bool, span_deg: f64) { + let (proj, hills, known, truth) = wide_panorama(reversed, span_deg); + let mut p = ViewParams::new(); + comp_params(&*proj, &hills, &known, &mut p, false).unwrap(); + let d = crate::projection::wrap_angle(p.a_center - truth.a_center); + assert!( + d.abs() < 1e-6, + "span {span_deg} rev {reversed}: a_center {} rad", + p.a_center + ); + assert!( + (p.scale - truth.scale).abs() < 1.0, + "span {span_deg} rev {reversed}: scale {}", + p.scale + ); + assert!( + (p.a_pitch - truth.a_pitch).abs() < 1e-6, + "span {span_deg} rev {reversed}: pitch {}", + p.a_pitch + ); + } + + #[test] + fn fit_wide_panorama_340_deg_apart() { + assert_wide_fit(false, 340.0); + } + + #[test] + fn fit_wide_panorama_350_deg_apart_reversed_order() { + assert_wide_fit(true, 350.0); + } + + #[test] + fn fit_wide_panorama_just_over_half_circle() { + assert_wide_fit(false, 200.0); + } } diff --git a/crates/gipfel-core/tests/golden.rs b/crates/gipfel-core/tests/golden.rs index fb12e40..2027800 100644 --- a/crates/gipfel-core/tests/golden.rs +++ b/crates/gipfel-core/tests/golden.rs @@ -253,10 +253,18 @@ fn fit_golden(projtype: ProjectionType, file: &str) { pan.parms.k1, pan.parms.x0, ]; - for (m, t) in mine.iter().zip(g.iter()) { + for (idx, (m, t)) in mine.iter().zip(g.iter()).enumerate() { // solver results: allow small drift, exact zeros must stay ~zero + // a_center (idx 0) is an angle: the C harness settles wherever its + // (possibly > 2pi) input azimuths put it; the Rust port normalizes. + // Compare modulo 2pi. + let d = if idx == 0 { + gipfel_core::projection::wrap_angle(m - t) + } else { + m - t + }; assert!( - (m - t).abs() <= 1e-6f64.max(m.abs() * 1e-6), + d.abs() <= 1e-6f64.max(m.abs() * 1e-6), "fit param {} vs {}", m, t |
