Aabb2

Struct Aabb2 

Source
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 box
  • max: 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

Source

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.

Source

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 point
  • p2 - 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));
Source

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 AABB
  • max - 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);
Source

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));
Source

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.

Source

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.

Source

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));
Source

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:

  1. Bottom-left (min.x, min.y)
  2. Top-left (min.x, max.y)
  3. Top-right (max.x, max.y)
  4. 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-right
Source

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));
Source

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));
Source

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));
Source

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 overlapping

Trait Implementations§

Source§

impl Add<Matrix<f64, Const<2>, Const<1>, ArrayStorage<f64, 2, 1>>> for Aabb2

Source§

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§

type Output = Aabb2

The resulting type after applying the + operator.
Source§

impl AddAssign<Matrix<f64, Const<2>, Const<1>, ArrayStorage<f64, 2, 1>>> for Aabb2

Source§

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 Clone for Aabb2

Source§

fn clone(&self) -> Aabb2

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Aabb2

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Aabb2

Source§

fn default() -> Aabb2

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Aabb2

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Aabb2

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Sub<Matrix<f64, Const<2>, Const<1>, ArrayStorage<f64, 2, 1>>> for Aabb2

Source§

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§

type Output = Aabb2

The resulting type after applying the - operator.
Source§

impl SubAssign<Matrix<f64, Const<2>, Const<1>, ArrayStorage<f64, 2, 1>>> for Aabb2

Source§

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));
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T, Right> ClosedAdd<Right> for T
where T: Add<Right, Output = T> + AddAssign<Right>,

Source§

impl<T, Right> ClosedSub<Right> for T
where T: Sub<Right, Output = T> + SubAssign<Right>,

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,