Skip to main content

BoundingBox

Struct BoundingBox 

Source
pub struct BoundingBox { /* private fields */ }
Expand description

An axis-aligned box: the points whose coordinates all lie between those of its min and max corners, faces included.

A box is never empty: min is at most max on every axis. A box of a single point is flat on every axis.

§Examples

use geomcore::{BoundingBox, Point};

let a = BoundingBox::new(Point::new(0.0, 0.0, 0.0), Point::new(2.0, 1.0, 1.0));
let b = BoundingBox::new(Point::new(3.0, 0.0, 0.0), Point::new(4.0, 1.0, 1.0));
assert!(!a.overlaps(b));
// Within a distance of 1 of each other: the boxes enlarged by half of it touch.
assert!(a.enlarged(0.5).overlaps(b.enlarged(0.5)));
assert_eq!(a.union(b).max(), Point::new(4.0, 1.0, 1.0));

Implementations§

Source§

impl BoundingBox

Source

pub fn new(a: Point, b: Point) -> BoundingBox

The box with opposite corners a and b, in any order.

§Examples
use geomcore::{BoundingBox, Point};

let b = BoundingBox::new(Point::new(1.0, 0.0, 5.0), Point::new(0.0, 2.0, 3.0));
assert_eq!(b.min(), Point::new(0.0, 0.0, 3.0));
assert_eq!(b.max(), Point::new(1.0, 2.0, 5.0));
Source

pub fn from_points( points: impl IntoIterator<Item = Point>, ) -> Option<BoundingBox>

The smallest box holding every point of points, or None if there are none.

§Examples
use geomcore::{BoundingBox, Point};

let points = [Point::new(1.0, 0.0, 0.0), Point::new(-1.0, 2.0, 0.5)];
let b = BoundingBox::from_points(points).unwrap();
assert_eq!(b.min(), Point::new(-1.0, 0.0, 0.0));
assert_eq!(b.max(), Point::new(1.0, 2.0, 0.5));
assert_eq!(BoundingBox::from_points([]), None);
Source

pub fn min(self) -> Point

The corner with the smallest coordinates.

§Examples
use geomcore::{BoundingBox, Point};

let b = BoundingBox::new(Point::new(1.0, 2.0, 3.0), Point::ORIGIN);
assert_eq!(b.min(), Point::ORIGIN);
Source

pub fn max(self) -> Point

The corner with the largest coordinates.

§Examples
use geomcore::{BoundingBox, Point};

let b = BoundingBox::new(Point::new(1.0, 2.0, 3.0), Point::ORIGIN);
assert_eq!(b.max(), Point::new(1.0, 2.0, 3.0));
Source

pub fn center(self) -> Point

The middle of the box.

§Examples
use geomcore::{BoundingBox, Point};

let b = BoundingBox::new(Point::ORIGIN, Point::new(2.0, 4.0, 6.0));
assert_eq!(b.center(), Point::new(1.0, 2.0, 3.0));
Source

pub fn diagonal(self) -> Vector

The vector from the min corner to the max corner: the box’s size along each axis.

§Examples
use geomcore::{BoundingBox, Point, Vector};

let b = BoundingBox::new(Point::ORIGIN, Point::new(3.0, 4.0, 0.0));
assert_eq!(b.diagonal(), Vector::new(3.0, 4.0, 0.0));
assert_eq!(b.diagonal().magnitude(), 5.0);
Source

pub fn union(self, other: BoundingBox) -> BoundingBox

The smallest box holding both boxes.

§Examples
use geomcore::{BoundingBox, Point};

let a = BoundingBox::new(Point::ORIGIN, Point::new(1.0, 1.0, 1.0));
let b = BoundingBox::new(Point::new(2.0, -1.0, 0.0), Point::new(3.0, 0.0, 0.5));
let both = a.union(b);
assert_eq!(both.min(), Point::new(0.0, -1.0, 0.0));
assert_eq!(both.max(), Point::new(3.0, 1.0, 1.0));
Source

pub fn enlarged(self, distance: f64) -> BoundingBox

The box grown by distance on every side, for instance by a tolerance before an overlap test. A negative distance shrinks it, an axis shorter than twice the shrink collapsing to its middle.

§Examples
use geomcore::{BoundingBox, Point};

let b = BoundingBox::new(Point::ORIGIN, Point::new(4.0, 1.0, 1.0));
let grown = b.enlarged(0.5);
assert_eq!(grown.min(), Point::new(-0.5, -0.5, -0.5));
assert_eq!(grown.max(), Point::new(4.5, 1.5, 1.5));

let shrunk = b.enlarged(-1.0);
assert_eq!(shrunk.min(), Point::new(1.0, 0.5, 0.5));
assert_eq!(shrunk.max(), Point::new(3.0, 0.5, 0.5));
Source

pub fn encloses(self, point: Point) -> bool

Whether point is in the box, faces included.

§Examples
use geomcore::{BoundingBox, Point};

let b = BoundingBox::new(Point::ORIGIN, Point::new(1.0, 1.0, 1.0));
assert!(b.encloses(Point::new(0.5, 0.5, 0.5)));
assert!(b.encloses(Point::new(1.0, 0.0, 0.5)));
assert!(!b.encloses(Point::new(1.5, 0.5, 0.5)));
Source

pub fn overlaps(self, other: BoundingBox) -> bool

Whether the two boxes share a point, faces included: boxes that only touch overlap.

§Examples
use geomcore::{BoundingBox, Point};

let a = BoundingBox::new(Point::ORIGIN, Point::new(1.0, 1.0, 1.0));
let touching = BoundingBox::new(Point::new(1.0, 0.0, 0.0), Point::new(2.0, 1.0, 1.0));
let apart = BoundingBox::new(Point::new(1.5, 0.0, 0.0), Point::new(2.0, 1.0, 1.0));
assert!(a.overlaps(touching));
assert!(!a.overlaps(apart));

Trait Implementations§

Source§

impl Clone for BoundingBox

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for BoundingBox

Source§

impl Debug for BoundingBox

Source§

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

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

impl PartialEq for BoundingBox

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for BoundingBox

Auto Trait Implementations§

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.