Skip to content

Commit

Permalink
Aabb wrt centre/issue #155 (#182)
Browse files Browse the repository at this point in the history
* non-functional halfway modification, read the docs

* Working and with a single unit test to verify

* feat: simplify implementation of Aabb::scaled_wrt_center

* chore: move Aabb::scaled_wirt_center test to the test dir.

* chore: update changelog

---------

Co-authored-by: whatf0xx <[email protected]>
Co-authored-by: Sébastien Crozet <[email protected]>
  • Loading branch information
3 people authored Mar 24, 2024
1 parent c599c39 commit 59ecc59
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 0 deletions.
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

0 comments on commit 59ecc59

Please sign in to comment.