Skip to content

Commit

Permalink
Fuzz the old and new dist traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
finnbear committed Jan 14, 2025
1 parent d322779 commit 67ecdfb
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions fuzz/fuzz_targets/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,13 @@ impl<const D: usize> Workload<D> {
assert!(!intersects);
}

// HINT: uncomment this temporarily to test with small BVH's.
/*
if !self.mutations.is_empty() || self.shapes.len() > 5 {
return;
}
*/

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

if self.shapes.len()
Expand Down Expand Up @@ -390,23 +397,55 @@ impl<const D: usize> Workload<D> {
// Due to sphere geometry, `Mode::Grid` doesn't imply traversals will agree.
self.fuzz_traversal(&bvh, &flat_bvh, &self.ball.ball(), false);

let mut last_distance = f32::NEG_INFINITY;
let nearest_traverse_iterator = bvh
.nearest_traverse_iterator(&ray, &self.shapes)
.inspect(|shape| {
let distance = ray.intersection_slice_for_aabb(&shape.aabb()).unwrap().0;
assert!(
!assert_ray_traversal_agreement || distance >= last_distance,
"{distance} {last_distance}"
);
last_distance = distance;
})
.map(ByPtr)
.collect::<HashSet<_>>();
let mut last_distance = f32::INFINITY;
let farthest_traverse_iterator = bvh
.farthest_traverse_iterator(&ray, &self.shapes)
.inspect(|shape| {
let distance = ray.intersection_slice_for_aabb(&shape.aabb()).unwrap().1;
assert!(
!assert_ray_traversal_agreement || distance <= last_distance,
"{distance} {last_distance}"
);
last_distance = distance;
})
.map(ByPtr)
.collect::<HashSet<_>>();

let nearest_child_traverse_iterator = bvh
.nearest_child_traverse_iterator(&ray, &self.shapes)
.map(ByPtr)
.collect::<HashSet<_>>();
let farthest_child_traverse_iterator = bvh
.farthest_child_traverse_iterator(&ray, &self.shapes)
.map(ByPtr)
.collect::<HashSet<_>>();

if assert_ray_traversal_agreement {
assert_eq!(traverse_ray, nearest_traverse_iterator);
assert_eq!(traverse_ray, nearest_child_traverse_iterator);
} else {
// Fails, probably due to normal rounding errors.
}

// Since the algorithm is similar, these should agree regardless of mode.
assert_eq!(nearest_traverse_iterator, farthest_traverse_iterator);
assert_eq!(
nearest_child_traverse_iterator,
farthest_child_traverse_iterator
);

if let Some(mutation) = self.mutations.pop() {
match mutation {
Expand Down

0 comments on commit 67ecdfb

Please sign in to comment.