Skip to content

Commit 2073350

Browse files
committed
fix(lox-orbits): fix floating point issue in LOS calculation
1 parent a69f55f commit 2073350

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

crates/lox-orbits/src/analysis.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ pub fn line_of_sight(radius: f64, r1: DVec3, r2: DVec3) -> f64 {
4040
let r2n = r2.length();
4141
let theta1 = radius / r1n;
4242
let theta2 = radius / r2n;
43-
let theta = r1.dot(r2) / r1n / r2n;
43+
// Clamp to the domain of `acos` to avoid floating point errors when `r1 == r2`.
44+
let theta = (r1.dot(r2) / r1n / r2n).clamp(-1.0, 1.0);
4445
theta1.acos() + theta2.acos() - theta.acos()
4546
}
4647

@@ -505,6 +506,17 @@ mod tests {
505506
assert!(los_sun >= 0.0);
506507
}
507508

509+
#[test]
510+
fn test_line_of_sight_identical() {
511+
let r1 = DVec3::new(0.0, -4464.696, -5102.509);
512+
let r2 = DVec3::new(0.0, -4464.696, -5102.509);
513+
let r = Earth.equatorial_radius();
514+
515+
let los = line_of_sight(r, r1, r2);
516+
517+
assert!(los >= 0.0);
518+
}
519+
508520
#[test]
509521
fn test_line_of_sight_trait() {
510522
let r1 = DVec3::new(0.0, -4464.696, -5102.509);

0 commit comments

Comments
 (0)