summaryrefslogtreecommitdiff
path: root/crates/gipfel-core/src
diff options
context:
space:
mode:
authorJohannes Hofmann <Johannes.Hofmann@gmx.de>2026-11-10 09:03:48 +0100
committerJohannes Hofmann <Johannes.Hofmann@gmx.de>2026-11-10 09:03:48 +0100
commit02546370ec4458eef59a8f636fe05b57961d56ab (patch)
treeeb5523d92c04e740b6fb025803116ef7c96c59c7 /crates/gipfel-core/src
parentce3b45ddaab7ddc7017fa75dc5de64cd6ca70f93 (diff)
core: is_visible wraps the azimuth difference and rejects non-finite input
GipfelFind.lean Finding 1: with |alph - a_center| > 2pi (user enters azimuth 400deg, or a fit leaves a_center unnormalized) the single fold produced a NEGATIVE 'distance' that passed every comparison, making hills behind the camera visible. NaN or infinite azimuths — and a NaN center, which made everything visible — passed as well. Fix at the point of use: wrap with % 2pi before the fold, and bail out on non-finite alph/a_center. Regression test pins the Lean witnesses (out_of_range_center_angle_stays_safe).
Diffstat (limited to 'crates/gipfel-core/src')
-rw-r--r--crates/gipfel-core/src/panorama.rs42
1 files changed, 41 insertions, 1 deletions
diff --git a/crates/gipfel-core/src/panorama.rs b/crates/gipfel-core/src/panorama.rs
index 3737606..1af64bf 100644
--- a/crates/gipfel-core/src/panorama.rs
+++ b/crates/gipfel-core/src/panorama.rs
@@ -394,7 +394,17 @@ impl Panorama {
}
pub fn is_visible(&self, alph: f64) -> bool {
- let mut center_dist = (alph - self.parms.a_center).abs();
+ // NaN or infinite azimuths (from user input or a diverged fit) must
+ // never count as visible; a NaN center used to make everything so.
+ if !alph.is_finite() || !self.parms.a_center.is_finite() {
+ return false;
+ }
+ // Wrap into [0, 2pi) before the fold: a_center may lie outside
+ // [-pi, pi] (user enters "400 deg", or a fit leaves it unnormalized).
+ // Without this, |alph - a_center| > 2pi turned the single fold below
+ // into a NEGATIVE distance, which passed every comparison and made
+ // hills behind the camera visible (GipfelFind.lean Finding 1).
+ let mut center_dist = (alph - self.parms.a_center).abs() % (2.0 * PI);
if center_dist > PI {
center_dist = 2.0 * PI - center_dist;
}
@@ -504,4 +514,34 @@ mod tests {
pan.max_view_angle = 80f64.to_radians();
assert!(!pan.is_visible(a70));
}
+
+ #[test]
+ fn out_of_range_center_angle_stays_safe() {
+ // GipfelFind.lean Finding 1: with |alph - a_center| > 2pi the single
+ // fold produced a negative "distance" that passed every comparison.
+ // Hill azimuths come from atan2, so alph is always in [-pi, pi].
+ let mut raw = Panorama::new();
+ raw.parms.a_center = 400f64.to_radians(); // e.g. left by a fit
+ assert!(!raw.is_visible(-std::f64::consts::PI)); // 220 deg off, behind
+
+ // the setter must agree with the equivalent in-range azimuth
+ let mut pan400 = Panorama::new();
+ pan400.set_center_angle(400.0); // "bearing + full turn" input slip
+ let mut pan40 = Panorama::new();
+ pan40.set_center_angle(40.0);
+ for d in -180..=180 {
+ let a = (d as f64).to_radians();
+ assert_eq!(pan400.is_visible(a), pan40.is_visible(a), "az {d}");
+ }
+
+ // NaN / infinite azimuths are not visible (Finding 1b)
+ assert!(!pan40.is_visible(f64::NAN));
+ assert!(!pan40.is_visible(f64::INFINITY));
+ assert!(!pan40.is_visible(f64::NEG_INFINITY));
+
+ // a NaN camera azimuth hides everything (was: everything visible)
+ let mut bad = Panorama::new();
+ bad.parms.a_center = f64::NAN;
+ assert!(!bad.is_visible(0.0));
+ }
}