Skip to content

Commit

Permalink
Requested revisions.
Browse files Browse the repository at this point in the history
  • Loading branch information
finnbear committed Jan 8, 2025
1 parent c708eb9 commit 59239c1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/aabb/aabb_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ impl<T: BHValue, const D: usize> Aabb<T, D> {
///
/// [`Aabb`]: struct.Aabb.html
pub fn intersects_aabb(&self, aabb: &Aabb<T, D>) -> bool {
// TODO: Try adding a SIMD specialization.
for i in 0..D {
if self.max[i] < aabb.min[i] || aabb.max[i] < self.min[i] {
return false;
Expand Down
14 changes: 8 additions & 6 deletions src/ball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use crate::{
};
use nalgebra::Point;

/// A 2d circle.
/// A circle, which can be used for traversing 2D BVHs.
pub type Circle<T> = Ball<T, 2>;

/// A 3d sphere.
/// A sphere, which can be used for traversing 3D BVHs.
pub type Sphere<T> = Ball<T, 3>;

/// In 2D, a circle. In 3D, a sphere. This can be used for traversing BVH's.
/// In 2D, a circle. In 3D, a sphere. This can be used for traversing BVHs.
#[derive(Debug, Clone, Copy)]
pub struct Ball<T: BHValue, const D: usize> {
/// The center of the ball.
Expand Down Expand Up @@ -62,6 +62,7 @@ impl<T: BHValue, const D: usize> Ball<T, D> {
for i in 0..D {
distance_squared += (point[i] - self.center[i]).powi(2);
}
// Squaring the RHS is faster than computing the square root of the LHS.
distance_squared <= self.radius.powi(2)
}

Expand All @@ -81,17 +82,18 @@ impl<T: BHValue, const D: usize> Ball<T, D> {
/// [`Aabb`]: struct.Aabb.html
/// [`Ball`]: struct.Ball.html
pub fn intersects_aabb(&self, aabb: &Aabb<T, D>) -> bool {
// https://gamemath.com/book/geomtests.html A.14
// https://gamemath.com/book/geomtests.html#intersection_sphere_aabb
// Finding the point in/on the AABB that is closest to the ball's center or,
// more specifically, find the squared distance betwen that point and the
// more specifically, find the squared distance between that point and the
// ball's center.
let mut distance_squared = T::zero();
for i in 0..D {
let closest_on_aabb = self.center[i].clamp(aabb.min[i], aabb.max[i]);
distance_squared += (closest_on_aabb - self.center[i]).powi(2);
}

// Then test if that point is in/on the ball.
// Then test if that point is in/on the ball. Squaring the RHS is faster than computing
// the square root of the LHS.
distance_squared <= self.radius.powi(2)
}
}
Expand Down

0 comments on commit 59239c1

Please sign in to comment.