Skip to content

Commit 23cbdf0

Browse files
GabrielMajerignzlbg
authored andcommitted
Use fuzzy float comparisons in aobench tests
1 parent 3fb2099 commit 23cbdf0

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

examples/aobench/src/geometry/vec.rs

+9
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ impl V3D {
6060
basis[1] = basis[2].cross(basis[0]).normalized();
6161
basis
6262
}
63+
// Fuzzy float comparison between vectors
64+
#[inline(always)]
65+
#[must_use]
66+
pub fn almost_eq(&self, rhs: &Self) -> bool {
67+
const EPSILON: f32 = 1E-3;
68+
(self.x - rhs.x).abs() < EPSILON &&
69+
(self.y - rhs.y).abs() < EPSILON &&
70+
(self.z - rhs.z).abs() < EPSILON
71+
}
6372
}
6473

6574
impl Add for V3D {

examples/aobench/src/intersection/ray_plane.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl Intersect<Plane> for RayxN {
6060
let old_isect_i = old_isect.get(i);
6161
let ray_i = self.get(i);
6262
let isect_i = ray_i.intersect(plane, old_isect_i);
63-
assert_eq!(isect_i, isect.get(i), "\n\nplane: {:?}\n\nold_isect: {:?}\n\nrays: {:?}\n\ni: {:?}\nold_isect_i: {:?}\nray_i: {:?}\nisect_i: {:?}\n\n", plane, old_isect, self, i, old_isect_i, ray_i, isect_i);
63+
assert!(isect_i.almost_eq(&isect.get(i)), "{:?} !~= {:?}\n\nplane: {:?}\n\nold_isect: {:?}\n\nrays: {:?}\n\ni: {:?}\nold_isect_i: {:?}\nray_i: {:?}\n\n", isect_i, isect.get(i), plane, old_isect, self, i, old_isect_i, ray_i);
6464
}
6565
true
6666
});

examples/aobench/src/intersection/ray_sphere.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl Intersect<Sphere> for RayxN {
6666
let old_isect_i = old_isect.get(i);
6767
let ray_i = self.get(i);
6868
let isect_i = ray_i.intersect(sphere, old_isect_i);
69-
assert_eq!(isect_i, isect.get(i), "\n\nsphere: {:?}\n\nold_isect: {:?}\n\nrays: {:?}\n\ni: {:?}\nold_isect_i: {:?}\nray_i: {:?}\nisect_i: {:?}\n\n", sphere, old_isect, self, i, old_isect_i, ray_i, isect_i);
69+
assert!(isect_i.almost_eq(&isect.get(i)), "{:?} !~= {:?}\n\nsphere: {:?}\n\nold_isect: {:?}\n\nrays: {:?}\n\ni: {:?}\nold_isect_i: {:?}\nray_i: {:?}\n\n", isect_i, isect.get(i), sphere, old_isect, self, i, old_isect_i, ray_i);
7070
}
7171
true
7272
});

examples/aobench/src/intersection/single.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use geometry::V3D;
44

55
/// Intersection result
6-
#[derive(Copy, Clone, Debug, PartialEq)]
6+
#[derive(Copy, Clone, Debug)]
77
pub struct Isect {
88
pub t: f32,
99
pub p: V3D,
@@ -22,3 +22,15 @@ impl Default for Isect {
2222
}
2323
}
2424
}
25+
26+
impl Isect {
27+
#[inline(always)]
28+
#[must_use]
29+
pub fn almost_eq(&self, rhs: &Isect) -> bool {
30+
const EPSILON: f32 = 1E-3;
31+
(self.t - rhs.t).abs() < EPSILON &&
32+
self.p.almost_eq(&rhs.p) &&
33+
self.n.almost_eq(&rhs.n) &&
34+
self.hit == rhs.hit
35+
}
36+
}

0 commit comments

Comments
 (0)