Skip to content

Fix to divide-by-zero blowup when normal vector is close to (1.0, 0.0… #247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,8 @@ pub extern "C" fn rotate_given_surface_normal(nx: f64, ny: f64, nz: f64, ux: &mu
let v: Vector3<f64> = into_surface.cross(&RUSTBCA_DIRECTION);
let c = into_surface.dot(&RUSTBCA_DIRECTION);
let vx = Matrix3::<f64>::new(0.0, -v.z, v.y, v.z, 0.0, -v.x, -v.y, v.x, 0.0);
let rotation_matrix = if c != -1.0 {

let rotation_matrix = if (c + 1.0).abs() > 1e-6 {
Matrix3::identity() + vx + vx*vx/(1. + c)
} else {
//If c == -1.0, the correct rotation should simply be a 180 degree rotation
Expand All @@ -1394,12 +1395,12 @@ pub extern "C" fn rotate_given_surface_normal(nx: f64, ny: f64, nz: f64, ux: &mu

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

*ux = incident.x + DELTA;
*uy = incident.y - DELTA;
*uz = incident.z;
let mag = (ux.powf(2.) + uy.powf(2.) + uz.powf(2.)).sqrt();
Expand Down
Loading