Skip to content

Commit

Permalink
Bugfix svenstaro#140 - no intersection slice on NaN.
Browse files Browse the repository at this point in the history
  • Loading branch information
finnbear committed Jan 13, 2025
1 parent 204a1c9 commit f3aac41
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "bvh-fuzz"
version = "0.0.0"
authors = ["Automatically generated"]
publish = false
edition = "2018"
edition = "2021"

[package.metadata]
cargo-fuzz = true
Expand Down
28 changes: 15 additions & 13 deletions fuzz/fuzz_targets/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,19 @@ impl<const D: usize> Workload<D> {
///
/// The contents are explained in the module-level comment up above.
fn fuzz(mut self) {
let mut bvh = Bvh::build(&mut self.shapes);
let ray = self.ray.ray();
let aabb = self.aabb.aabb();

// Make sure these functions agree and that the latter doesn't return NaN.
let intersects = ray.intersects_aabb(&aabb);
if let Some((entry, exit)) = ray.intersection_slice_for_aabb(&aabb) {
assert!(intersects);
assert!(entry <= exit, "{entry} {exit}");
} else {
assert!(!intersects);
}

let mut bvh = Bvh::build(&mut self.shapes);

if self.shapes.len()
+ self
Expand Down Expand Up @@ -367,18 +378,9 @@ impl<const D: usize> Workload<D> {
bvh.assert_tight();
let flat_bvh = bvh.flatten();

let traverse_ray = self.fuzz_traversal(
&bvh,
&flat_bvh,
&self.ray.ray(),
assert_ray_traversal_agreement,
);
self.fuzz_traversal(
&bvh,
&flat_bvh,
&self.aabb.aabb(),
assert_aabb_traversal_agreement,
);
let traverse_ray =
self.fuzz_traversal(&bvh, &flat_bvh, &ray, assert_ray_traversal_agreement);
self.fuzz_traversal(&bvh, &flat_bvh, &aabb, assert_aabb_traversal_agreement);
self.fuzz_traversal(
&bvh,
&flat_bvh,
Expand Down
10 changes: 6 additions & 4 deletions src/ray/ray_impl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! This module defines a Ray structure and intersection algorithms
//! for axis aligned bounding boxes and triangles.
use std::cmp::Ordering;

use crate::aabb::IntersectsAabb;
use crate::utils::fast_max;
use crate::{aabb::Aabb, bounding_hierarchy::BHValue};
Expand Down Expand Up @@ -124,15 +126,15 @@ impl<T: BHValue, const D: usize> Ray<T, D> {

let (inf, sup) = lbr.inf_sup(&rtr);

let tmin = inf.max();
let tmin = fast_max(inf.max(), T::zero());
let tmax = sup.min();

// no intersection
if tmin > tmax || tmax < T::zero() {
if matches!(tmin.partial_cmp(&tmax), Some(Ordering::Greater) | None) {
// tmin > tmax or either was NaN, meaning no intersection.
return None;
}

Some((fast_max(tmin, T::zero()), tmax))
Some((tmin, tmax))
}

/// Implementation of the
Expand Down

0 comments on commit f3aac41

Please sign in to comment.