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
impl BoundingBox
Sourcepub fn new(a: Point, b: Point) -> BoundingBox
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));Sourcepub fn from_points(
points: impl IntoIterator<Item = Point>,
) -> Option<BoundingBox>
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);Sourcepub fn min(self) -> Point
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);Sourcepub fn max(self) -> Point
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));Sourcepub fn center(self) -> Point
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));Sourcepub fn diagonal(self) -> Vector
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);Sourcepub fn union(self, other: BoundingBox) -> BoundingBox
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));Sourcepub fn enlarged(self, distance: f64) -> BoundingBox
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));Sourcepub fn encloses(self, point: Point) -> bool
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)));Sourcepub fn overlaps(self, other: BoundingBox) -> bool
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));