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

Improve Aabb API #559

Merged
merged 3 commits into from
May 10, 2022
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
75 changes: 69 additions & 6 deletions crates/fj-math/src/aabb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,50 @@ pub struct Aabb<const D: usize> {
pub max: Point<D>,
}

impl<const D: usize> Aabb<D> {
/// Determine whether the AABB contains a given point
pub fn contains(&self, point: impl Into<Point<D>>) -> bool {
let point = point.into();

let min = self
.min
.coords
.components
.into_iter()
.zip(point.coords.components);
for (min, p) in min {
if min > p {
return false;
}
}

let max = self
.max
.coords
.components
.into_iter()
.zip(point.coords.components);
for (max, p) in max {
if max < p {
return false;
}
}

true
}
}

impl Aabb<2> {
/// Construct a 2-dimensional AABB from a list of points
///
/// The resulting AABB will contain all the points.
pub fn from_points(points: impl IntoIterator<Item = Point<2>>) -> Self {
let points: Vec<_> =
points.into_iter().map(|point| point.to_na()).collect();
pub fn from_points(
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> Self {
let points: Vec<_> = points
.into_iter()
.map(|point| point.into().to_na())
.collect();
parry2d_f64::bounding_volume::AABB::from_points(&points).into()
}

Expand All @@ -36,9 +73,13 @@ impl Aabb<3> {
/// Construct a 3-dimensional AABB from a list of points
///
/// The resulting AABB will contain all the points.
pub fn from_points(points: impl IntoIterator<Item = Point<3>>) -> Self {
let points: Vec<_> =
points.into_iter().map(|point| point.to_na()).collect();
pub fn from_points(
points: impl IntoIterator<Item = impl Into<Point<3>>>,
) -> Self {
let points: Vec<_> = points
.into_iter()
.map(|point| point.into().to_na())
.collect();
parry3d_f64::bounding_volume::AABB::from_points(&points).into()
}

Expand Down Expand Up @@ -98,3 +139,25 @@ impl From<parry3d_f64::bounding_volume::AABB> for Aabb<3> {
Self::from_parry(aabb)
}
}

#[cfg(test)]
mod tests {
use super::Aabb;

#[test]
fn contains() {
let aabb = Aabb::<2>::from_points([[1., 1.], [3., 3.]]);

assert!(aabb.contains([2., 2.]));

assert!(!aabb.contains([0., 0.]));
assert!(!aabb.contains([4., 0.]));
assert!(!aabb.contains([4., 4.]));
assert!(!aabb.contains([0., 4.]));

assert!(!aabb.contains([2., 0.]));
assert!(!aabb.contains([2., 4.]));
assert!(!aabb.contains([0., 2.]));
assert!(!aabb.contains([4., 2.]));
}
}
2 changes: 1 addition & 1 deletion crates/fj-math/src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl<const D: usize> Segment<D> {
let points = points.map(Into::into);
let [a, b] = points;

assert!(a != b, "Invalid segment; both points are identical {a:?}");
assert!(a != b, "Invalid segment; both points are identical: {a:?}");

Self { points }
}
Expand Down