Skip to content

Commit 4501b08

Browse files
authored
Merge pull request #247 from lcpp-org/fix_rotation
Fix to divide-by-zero blowup when normal vector is close to (1.0, 0.0…
2 parents 1312dd3 + c413397 commit 4501b08

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,8 @@ pub extern "C" fn rotate_given_surface_normal(nx: f64, ny: f64, nz: f64, ux: &mu
13821382
let v: Vector3<f64> = into_surface.cross(&RUSTBCA_DIRECTION);
13831383
let c = into_surface.dot(&RUSTBCA_DIRECTION);
13841384
let vx = Matrix3::<f64>::new(0.0, -v.z, v.y, v.z, 0.0, -v.x, -v.y, v.x, 0.0);
1385-
let rotation_matrix = if c != -1.0 {
1385+
1386+
let rotation_matrix = if (c + 1.0).abs() > 1e-6 {
13861387
Matrix3::identity() + vx + vx*vx/(1. + c)
13871388
} else {
13881389
//If c == -1.0, the correct rotation should simply be a 180 degree rotation
@@ -1394,12 +1395,12 @@ pub extern "C" fn rotate_given_surface_normal(nx: f64, ny: f64, nz: f64, ux: &mu
13941395

13951396
// ux must not be exactly 1.0 to avoid gimbal lock in RustBCA
13961397
// simple_bca does not normalize direction before proceeding, must be done manually
1397-
*ux = incident.x + DELTA;
13981398
assert!(
1399-
*ux > 0.0, "Error: RustBCA initial direction out of surface. Please check surface normals and incident direction. n = ({}, {}, {}) u = ({}, {}, {})",
1400-
nx, ny, nz, ux, uy, uz
1399+
incident.x + DELTA > 0.0, "Error: RustBCA initial direction out of surface. Please check surface normals and incident direction. c={} n = ({}, {}, {}) u = ({}, {}, {}), unew = ({}, {}, {})",
1400+
(c + 1.0).abs(), nx, ny, nz, ux, uy, uz, incident.x, incident.y, incident.z
14011401
);
14021402

1403+
*ux = incident.x + DELTA;
14031404
*uy = incident.y - DELTA;
14041405
*uz = incident.z;
14051406
let mag = (ux.powf(2.) + uy.powf(2.) + uz.powf(2.)).sqrt();

0 commit comments

Comments
 (0)