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

Aabb wrt centre/issue #155 #182

Merged
merged 5 commits into from
Mar 24, 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

- Fix a crash in `Qbvh::refit` that results from the QBVH tree becoming increasingly imbalanced.

### Added

- Add `Aabb::scaled_wrt_center` to scale an AABB while keeping its center unchanged.

## v0.13.6

### Fixed
Expand Down
16 changes: 16 additions & 0 deletions crates/parry2d/tests/geometry/aabb_scale.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use na::{Point2, Vector2};
use parry2d::bounding_volume::Aabb;

#[test]
fn test_aabb_scale_wrt_center() {
let aabb = Aabb::from_half_extents(Point2::new(1.0, 2.0), Vector2::new(4.0, 5.0));
let scale = Vector2::new(10.0, -20.0);
let scaled_aabb = aabb.scaled_wrt_center(&scale);
let scaled_aabb_neg = aabb.scaled_wrt_center(&-scale);
let scaled_aabb_abs = aabb.scaled_wrt_center(&scale.abs());

assert_eq!(&scaled_aabb, &scaled_aabb_neg);
assert_eq!(&scaled_aabb, &scaled_aabb_abs);
assert_eq!(aabb.center(), scaled_aabb.center());
assert_eq!(scaled_aabb.half_extents(), Vector2::new(40.0, 100.0));
}
1 change: 1 addition & 0 deletions crates/parry2d/tests/geometry/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod aabb_scale;
mod ball_ball_toi;
mod ball_cuboid_contact;
mod epa2;
Expand Down
16 changes: 16 additions & 0 deletions crates/parry3d/tests/geometry/aabb_scale.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use na::{Point3, Vector3};
use parry3d::bounding_volume::Aabb;

#[test]
fn test_aabb_scale_wrt_center() {
let aabb = Aabb::from_half_extents(Point3::new(1.0, 2.0, 3.0), Vector3::new(4.0, 5.0, 6.0));
let scale = Vector3::new(10.0, -20.0, 50.0);
let scaled_aabb = aabb.scaled_wrt_center(&scale);
let scaled_aabb_neg = aabb.scaled_wrt_center(&-scale);
let scaled_aabb_abs = aabb.scaled_wrt_center(&scale.abs());

assert_eq!(&scaled_aabb, &scaled_aabb_neg);
assert_eq!(&scaled_aabb, &scaled_aabb_abs);
assert_eq!(aabb.center(), scaled_aabb.center());
assert_eq!(scaled_aabb.half_extents(), Vector3::new(40.0, 100.0, 300.0));
}
1 change: 1 addition & 0 deletions crates/parry3d/tests/geometry/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod aabb_scale;
mod ball_ball_toi;
mod ball_triangle_toi;
mod convex_hull;
Expand Down
17 changes: 17 additions & 0 deletions src/bounding_volume/aabb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,23 @@ impl Aabb {
}
}

/// Returns an AABB with the same center as `self` but with extents scaled by `scale`.
///
/// # Parameters
/// - `scale`: the scaling factor. It can be non-uniform and/or negative. The AABB being
/// symmetric wrt. its center, a negative scale value has the same effect as scaling
/// by its absolute value.
#[inline]
#[must_use]
pub fn scaled_wrt_center(self, scale: &Vector<Real>) -> Self {
let center = self.center();
// Multiply the extents by the scale. Negative scaling might modify the half-extent
// sign, so we take the absolute value. The AABB being symmetric that absolute value
// is valid.
let half_extents = self.half_extents().component_mul(scale).abs();
Self::from_half_extents(center, half_extents)
}

/// The smallest bounding sphere containing this `Aabb`.
#[inline]
pub fn bounding_sphere(&self) -> BoundingSphere {
Expand Down
Loading