pub struct Aabb2 { /* private fields */ }Expand description
2D Axis-Aligned Bounding Box (AABB) implementation.
This module provides functionality for working with axis-aligned bounding boxes in 2D space. AABBs are fundamental for spatial partitioning, collision detection, and various geometric algorithms. The implementation supports both f32 and f64 precision through feature flags.
§Examples
use phys_geom::{Aabb2, math::Point2};
// Create an AABB from two points
let aabb = Aabb2::new(Point2::new(0.0, 0.0), Point2::new(10.0, 5.0));
// Get the center and size
let center = aabb.center();
let size = aabb.size();
// Check if two AABBs overlap
let other = Aabb2::new(Point2::new(5.0, 2.0), Point2::new(15.0, 8.0));
let overlaps = aabb.overlap_test(&other);2D Axis-Aligned Bounding Box.
An AABB represents a rectangular region in 2D space defined by its minimum and maximum coordinates. The box is axis-aligned, meaning its edges are parallel to the coordinate axes.
§Representation
The AABB is stored using two Point2 values:
min: The bottom-left corner of the boxmax: The top-right corner of the box
§Invariants
For a valid AABB, min.x <= max.x and min.y <= max.y must hold true.
The new_unchecked method enforces this with debug assertions.
Implementations§
Source§impl Aabb2
impl Aabb2
Sourcepub const ZERO: Aabb2
pub const ZERO: Aabb2
Zero-sized AABB located at the origin.
Both min and max points are at (0, 0), representing a degenerate box with zero area.
Sourcepub fn new(p1: Point2, p2: Point2) -> Aabb2
pub fn new(p1: Point2, p2: Point2) -> Aabb2
Creates a new AABB from two points.
The AABB will be the smallest axis-aligned rectangle that contains both points. This method automatically determines which point should be the min and max corners.
§Arguments
p1- First pointp2- Second point
§Returns
A new AABB that contains both input points.
§Examples
use phys_geom::{Aabb2, math::Point2};
let p1 = Point2::new(5.0, 10.0);
let p2 = Point2::new(1.0, 3.0);
let aabb = Aabb2::new(p1, p2);
assert_eq!(aabb.min(), Point2::new(1.0, 3.0));
assert_eq!(aabb.max(), Point2::new(5.0, 10.0));Sourcepub fn new_unchecked(min: Point2, max: Point2) -> Self
pub fn new_unchecked(min: Point2, max: Point2) -> Self
Creates a new AABB from explicit min and max points.
Unlike the new method, this function assumes the caller has already determined
which point is the minimum and which is the maximum. This is more efficient when
you already know the min/max relationship.
§Safety
The caller must ensure that min.x <= max.x and min.y <= max.y.
Debug assertions will catch violations in debug builds.
§Arguments
min- The minimum corner (bottom-left) of the AABBmax- The maximum corner (top-right) of the AABB
§Examples
use phys_geom::{Aabb2, math::Point2};
let min = Point2::new(0.0, 0.0);
let max = Point2::new(10.0, 5.0);
let aabb = Aabb2::new_unchecked(min, max);Sourcepub fn from_array_unchecked(values: [Real; 4]) -> Self
pub fn from_array_unchecked(values: [Real; 4]) -> Self
Creates an AABB from an array of 4 floating-point values.
The array elements should be in the order: [min_x, min_y, max_x, max_y]. This method is useful for interoperability with other systems that represent AABBs as flat arrays.
§Arguments
values- Array of 4 values: [min_x, min_y, max_x, max_y]
§Returns
A new AABB created from the array values.
§Examples
use phys_geom::{Aabb2, math::Point2};
let values = [0.0, 0.0, 10.0, 5.0];
let aabb = Aabb2::from_array_unchecked(values);
assert_eq!(aabb.min(), Point2::new(0.0, 0.0));
assert_eq!(aabb.max(), Point2::new(10.0, 5.0));Sourcepub fn min(&self) -> Point2
pub fn min(&self) -> Point2
Returns the minimum corner of the AABB.
This is the bottom-left corner of the bounding box, containing the smallest x and y coordinates within the AABB.
§Returns
The minimum point of the AABB.
Sourcepub fn max(&self) -> Point2
pub fn max(&self) -> Point2
Returns the maximum corner of the AABB.
This is the top-right corner of the bounding box, containing the largest x and y coordinates within the AABB.
§Returns
The maximum point of the AABB.
Sourcepub fn grow(&mut self, p: Point2)
pub fn grow(&mut self, p: Point2)
Expands the AABB to include the specified point.
If the point is already inside the AABB, this method has no effect. Otherwise, the AABB is expanded to include the point.
§Arguments
p- The point to include in the AABB
§Examples
use phys_geom::{Aabb2, math::Point2};
let mut aabb = Aabb2::new(Point2::new(0.0, 0.0), Point2::new(5.0, 5.0));
aabb.grow(Point2::new(10.0, -2.0));
assert_eq!(aabb.min(), Point2::new(0.0, -2.0));
assert_eq!(aabb.max(), Point2::new(10.0, 5.0));Sourcepub fn corners(&self) -> [Point2; 4]
pub fn corners(&self) -> [Point2; 4]
Returns the four corner points of the AABB.
The corners are returned in counter-clockwise order starting from the minimum corner:
- Bottom-left (min.x, min.y)
- Top-left (min.x, max.y)
- Top-right (max.x, max.y)
- Bottom-right (max.x, min.y)
§Returns
Array of 4 points representing the corners of the AABB.
§Examples
use phys_geom::{Aabb2, math::Point2};
let aabb = Aabb2::new(Point2::new(0.0, 0.0), Point2::new(10.0, 5.0));
let corners = aabb.corners();
assert_eq!(corners[0], Point2::new(0.0, 0.0)); // bottom-left
assert_eq!(corners[1], Point2::new(0.0, 5.0)); // top-left
assert_eq!(corners[2], Point2::new(10.0, 5.0)); // top-right
assert_eq!(corners[3], Point2::new(10.0, 0.0)); // bottom-rightSourcepub fn with_margin(&self, margin: Vec2) -> Self
pub fn with_margin(&self, margin: Vec2) -> Self
Expands the AABB by adding a margin on all sides.
The margin is added to both sides of the AABB, effectively increasing its size by 2 * margin in each dimension. This is useful for creating buffer zones around bounding boxes.
§Arguments
margin- The margin to add on all sides (must be non-negative)
§Returns
A new AABB expanded by the specified margin.
§Panics
In debug builds, this method panics if margin components are negative.
§Examples
use phys_geom::{Aabb2, math::{Point2, Vec2}};
let aabb = Aabb2::new(Point2::new(0.0, 0.0), Point2::new(10.0, 5.0));
let expanded = aabb.with_margin(Vec2::new(1.0, 0.5));
assert_eq!(expanded.min(), Point2::new(-1.0, -0.5));
assert_eq!(expanded.max(), Point2::new(11.0, 5.5));Sourcepub fn center(&self) -> Point2
pub fn center(&self) -> Point2
Returns the center point of the AABB.
The center is calculated as the midpoint between the min and max corners. This method automatically adapts to the current floating-point precision (f32 or f64).
§Returns
The center point of the AABB.
§Examples
use phys_geom::{Aabb2, math::Point2};
let aabb = Aabb2::new(Point2::new(0.0, 0.0), Point2::new(10.0, 5.0));
let center = aabb.center();
assert_eq!(center, Point2::new(5.0, 2.5));Sourcepub fn size(&self) -> Vec2
pub fn size(&self) -> Vec2
Returns the size (dimensions) of the AABB.
The size is calculated as max - min, giving the width and height of the AABB. Note that this method returns zero for degenerate AABBs where min equals max.
§Returns
A vector representing the width (x) and height (y) of the AABB.
§Examples
use phys_geom::{Aabb2, math::{Point2, Vec2}};
let aabb = Aabb2::new(Point2::new(0.0, 0.0), Point2::new(10.0, 5.0));
let size = aabb.size();
assert_eq!(size, Vec2::new(10.0, 5.0));Sourcepub fn overlap_test(&self, rhs: &Aabb2) -> bool
pub fn overlap_test(&self, rhs: &Aabb2) -> bool
Tests if this AABB overlaps with another AABB.
Two AABBs overlap if they share any common area. This method performs a separating axis test along both the x and y axes.
§Arguments
rhs- The other AABB to test against
§Returns
true if the AABBs overlap, false otherwise.
§Examples
use phys_geom::{Aabb2, math::Point2};
let aabb1 = Aabb2::new(Point2::new(0.0, 0.0), Point2::new(5.0, 5.0));
let aabb2 = Aabb2::new(Point2::new(3.0, 3.0), Point2::new(8.0, 8.0));
let aabb3 = Aabb2::new(Point2::new(10.0, 10.0), Point2::new(15.0, 15.0));
assert!(aabb1.overlap_test(&aabb2)); // overlapping
assert!(!aabb1.overlap_test(&aabb3)); // not overlappingTrait Implementations§
Source§impl Add<Matrix<f64, Const<2>, Const<1>, ArrayStorage<f64, 2, 1>>> for Aabb2
impl Add<Matrix<f64, Const<2>, Const<1>, ArrayStorage<f64, 2, 1>>> for Aabb2
Source§fn add(self, rhs: Vec2) -> Self::Output
fn add(self, rhs: Vec2) -> Self::Output
Translates the AABB by adding a vector.
This operation moves the entire AABB by the specified vector, preserving its size and shape.
§Arguments
rhs- The translation vector
§Returns
A new AABB translated by the vector.
§Examples
use phys_geom::{Aabb2, math::{Point2, Vec2}};
let aabb = Aabb2::new(Point2::new(0.0, 0.0), Point2::new(5.0, 5.0));
let translated = aabb + Vec2::new(10.0, 5.0);
assert_eq!(translated.min(), Point2::new(10.0, 5.0));
assert_eq!(translated.max(), Point2::new(15.0, 10.0));Source§impl AddAssign<Matrix<f64, Const<2>, Const<1>, ArrayStorage<f64, 2, 1>>> for Aabb2
impl AddAssign<Matrix<f64, Const<2>, Const<1>, ArrayStorage<f64, 2, 1>>> for Aabb2
Source§fn add_assign(&mut self, rhs: Vec2)
fn add_assign(&mut self, rhs: Vec2)
Translates the AABB in-place by adding a vector.
This operation moves the entire AABB by the specified vector, modifying the original AABB.
§Arguments
rhs- The translation vector
§Examples
use phys_geom::{Aabb2, math::{Point2, Vec2}};
let mut aabb = Aabb2::new(Point2::new(0.0, 0.0), Point2::new(5.0, 5.0));
aabb += Vec2::new(10.0, 5.0);
assert_eq!(aabb.min(), Point2::new(10.0, 5.0));
assert_eq!(aabb.max(), Point2::new(15.0, 10.0));Source§impl<'de> Deserialize<'de> for Aabb2
impl<'de> Deserialize<'de> for Aabb2
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Sub<Matrix<f64, Const<2>, Const<1>, ArrayStorage<f64, 2, 1>>> for Aabb2
impl Sub<Matrix<f64, Const<2>, Const<1>, ArrayStorage<f64, 2, 1>>> for Aabb2
Source§fn sub(self, rhs: Vec2) -> Self::Output
fn sub(self, rhs: Vec2) -> Self::Output
Translates the AABB by subtracting a vector.
This operation moves the entire AABB by the negative of the specified vector, preserving its size and shape.
§Arguments
rhs- The translation vector to subtract
§Returns
A new AABB translated by the negative vector.
§Examples
use phys_geom::{Aabb2, math::{Point2, Vec2}};
let aabb = Aabb2::new(Point2::new(10.0, 5.0), Point2::new(15.0, 10.0));
let translated = aabb - Vec2::new(10.0, 5.0);
assert_eq!(translated.min(), Point2::new(0.0, 0.0));
assert_eq!(translated.max(), Point2::new(5.0, 5.0));Source§impl SubAssign<Matrix<f64, Const<2>, Const<1>, ArrayStorage<f64, 2, 1>>> for Aabb2
impl SubAssign<Matrix<f64, Const<2>, Const<1>, ArrayStorage<f64, 2, 1>>> for Aabb2
Source§fn sub_assign(&mut self, rhs: Vec2)
fn sub_assign(&mut self, rhs: Vec2)
Translates the AABB in-place by subtracting a vector.
This operation moves the entire AABB by the negative of the specified vector, modifying the original AABB.
§Arguments
rhs- The translation vector to subtract
§Examples
use phys_geom::{Aabb2, math::{Point2, Vec2}};
let mut aabb = Aabb2::new(Point2::new(10.0, 5.0), Point2::new(15.0, 10.0));
aabb -= Vec2::new(10.0, 5.0);
assert_eq!(aabb.min(), Point2::new(0.0, 0.0));
assert_eq!(aabb.max(), Point2::new(5.0, 5.0));impl Copy for Aabb2
Auto Trait Implementations§
impl Freeze for Aabb2
impl RefUnwindSafe for Aabb2
impl Send for Aabb2
impl Sync for Aabb2
impl Unpin for Aabb2
impl UnwindSafe for Aabb2
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.