Skip to content
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

Implement bounding volumes for primitive shapes #11336

Merged
merged 17 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
mod primitive_impls;

use glam::Mat2;

use super::BoundingVolume;
use crate::prelude::Vec2;

/// Computes the geometric center of the given set of points.
#[inline(always)]
fn point_cloud_2d_center(points: &[Vec2]) -> Vec2 {
assert!(
!points.is_empty(),
"cannot compute the center of an empty set of points"
);

let denom = 1.0 / points.len() as f32;
points.iter().fold(Vec2::ZERO, |acc, point| acc + *point) * denom
}

/// A trait with methods that return 2D bounded volumes for a shape
pub trait Bounded2d {
/// Get an axis-aligned bounding box for the shape with the given translation and rotation.
Expand All @@ -21,6 +37,41 @@ pub struct Aabb2d {
pub max: Vec2,
}

impl Aabb2d {
/// Computes the smallest [`Aabb2d`] containing the given set of points,
/// transformed by `translation` and `rotation`.
Jondolf marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Panics
///
/// Panics if the given set of points is empty.
#[inline(always)]
pub fn from_point_cloud(translation: Vec2, rotation: f32, points: &[Vec2]) -> Aabb2d {
// Transform all points by rotation
let rotation_mat = Mat2::from_angle(rotation);
let mut iter = points.iter().map(|point| rotation_mat * *point);

let first = iter
.next()
.expect("point cloud must contain at least one point for Aabb2d construction");

let (min, max) = iter.fold((first, first), |(prev_min, prev_max), point| {
(point.min(prev_min), point.max(prev_max))
});

Aabb2d {
min: min + translation,
max: max + translation,
}
}

/// Computes the smallest [`BoundingCircle`] containing this [`Aabb2d`].
#[inline(always)]
pub fn bounding_circle(&self) -> BoundingCircle {
let radius = self.min.distance(self.max) / 2.0;
BoundingCircle::new(self.center(), radius)
}
}

impl BoundingVolume for Aabb2d {
type Position = Vec2;
type HalfSize = Vec2;
Expand Down Expand Up @@ -207,11 +258,43 @@ impl BoundingCircle {
}
}

/// Computes a [`BoundingCircle`] containing the given set of points,
/// transformed by `translation` and `rotation`.
///
/// The bounding circle is not guaranteed to be the smallest possible.
#[inline(always)]
pub fn from_point_cloud(translation: Vec2, rotation: f32, points: &[Vec2]) -> BoundingCircle {
let center = point_cloud_2d_center(points);
let mut radius_squared = 0.0;

for point in points {
// Get squared version to avoid unnecessary sqrt calls
let distance_squared = point.distance_squared(center);
if distance_squared > radius_squared {
radius_squared = distance_squared;
}
}

BoundingCircle::new(
Mat2::from_angle(rotation) * center + translation,
radius_squared.sqrt(),
)
}

/// Get the radius of the bounding circle
#[inline(always)]
pub fn radius(&self) -> f32 {
self.circle.radius
}

/// Computes the smallest [`Aabb2d`] containing this [`BoundingCircle`].
#[inline(always)]
pub fn aabb_2d(&self) -> Aabb2d {
Aabb2d {
min: self.center - Vec2::splat(self.radius()),
max: self.center + Vec2::splat(self.radius()),
}
}
}

impl BoundingVolume for BoundingCircle {
Expand Down
Loading