BoundingBox2

Struct BoundingBox2 

Source
pub struct BoundingBox2<T: Float> {
    pub lower_corner: Vector2<T>,
    pub upper_corner: Vector2<T>,
}
Expand description

§2-D axis-aligned bounding box class.

  • tparam T - Real number type.
  • tparam N - Dimension.

Fields§

§lower_corner: Vector2<T>

Lower corner of the bounding box.

§upper_corner: Vector2<T>

Upper corner of the bounding box.

Implementations§

Source§

impl<T: Float> BoundingBox2<T>

§Constructor

Source

pub fn new_default() -> BoundingBox2<T>

Default constructor.

use vox_geometry_rust::bounding_box2::BoundingBox2D;
use vox_geometry_rust::constants::K_MAX_D;
let aabb = BoundingBox2D::new_default();

assert_eq!(K_MAX_D, aabb.lower_corner.x);
assert_eq!(K_MAX_D, aabb.lower_corner.y);

assert_eq!(-K_MAX_D, aabb.upper_corner.x);
assert_eq!(-K_MAX_D, aabb.upper_corner.y);
Source

pub fn new(point1: Vector2<T>, point2: Vector2<T>) -> BoundingBox2<T>

Constructs a box that tightly covers two points.

use vox_geometry_rust::bounding_box2::BoundingBox2D;
use vox_geometry_rust::vector2::Vector2D;
let aabb = BoundingBox2D::new(Vector2D::new(-2.0, 3.0), Vector2D::new(4.0, -2.0));

assert_eq!(-2.0, aabb.lower_corner.x);
assert_eq!(-2.0, aabb.lower_corner.y);

assert_eq!(4.0, aabb.upper_corner.x);
assert_eq!(3.0, aabb.upper_corner.y);
Source§

impl<T: Float> BoundingBox2<T>

Source

pub fn width(&self) -> T

Returns width of the box.

use vox_geometry_rust::bounding_box2::BoundingBox2D;
use vox_geometry_rust::vector2::Vector2D;
let aabb = BoundingBox2D::new(Vector2D::new(-2.0, 3.0), Vector2D::new(4.0, -2.0));

assert_eq!(6.0, aabb.width());
Source

pub fn height(&self) -> T

Returns height of the box.

use vox_geometry_rust::bounding_box2::BoundingBox2D;
use vox_geometry_rust::vector2::Vector2D;
let aabb = BoundingBox2D::new(Vector2D::new(-2.0, 3.0), Vector2D::new(4.0, -2.0));

assert_eq!(5.0, aabb.height());
Source

pub fn length(&self, axis: usize) -> T

Returns length of the box in given axis.

use vox_geometry_rust::bounding_box2::BoundingBox2D;
use vox_geometry_rust::vector2::Vector2D;
let aabb = BoundingBox2D::new(Vector2D::new(-2.0, 3.0), Vector2D::new(4.0, -2.0));

assert_eq!(6.0, aabb.length(0));
assert_eq!(5.0, aabb.length(1));
Source

pub fn overlaps(&self, other: &BoundingBox2<T>) -> bool

Returns true of this box and other box overlaps.

use vox_geometry_rust::bounding_box2::BoundingBox2D;
use vox_geometry_rust::vector2::Vector2D;
// x-axis is not overlapping
{
    let box1 = BoundingBox2D::new(Vector2D::new(-2.0, -2.0), Vector2D::new(4.0, 3.0));
    let box2 = BoundingBox2D::new(Vector2D::new(5.0, 1.0), Vector2D::new(8.0, 2.0));

    assert_eq!(box1.overlaps(&box2), false);
}

// y-axis is not overlapping
{
    let box1 = BoundingBox2D::new(Vector2D::new(-2.0, -2.0), Vector2D::new(4.0, 3.0));
    let box2 = BoundingBox2D::new(Vector2D::new(3.0, 4.0), Vector2D::new(8.0, 6.0));

    assert_eq!(box1.overlaps(&box2), false);
}

// overlapping
{
    let box1 = BoundingBox2D::new(Vector2D::new(-2.0, -2.0), Vector2D::new(4.0, 3.0));
    let box2 = BoundingBox2D::new(Vector2D::new(3.0, 1.0), Vector2D::new(8.0, 2.0));

    assert_eq!(box1.overlaps(&box2), true);
}
Source

pub fn contains(&self, point: &Vector2<T>) -> bool

Returns true if the input point is inside of this box.

use vox_geometry_rust::bounding_box2::BoundingBox2D;
use vox_geometry_rust::vector2::Vector2D;
// Not containing (x-axis is out)
{
    let aabb = BoundingBox2D::new(Vector2D::new(-2.0, -2.0), Vector2D::new(4.0, 3.0));
    let point = Vector2D::new(-3.0, 0.0);

    assert_eq!(aabb.contains(&point), false);
}

// Not containing (y-axis is out)
{
    let aabb = BoundingBox2D::new(Vector2D::new(-2.0, -2.0), Vector2D::new(4.0, 3.0));
    let point = Vector2D::new(2.0, 3.5);

    assert_eq!(aabb.contains(&point), false);
}

// Containing
{
    let aabb = BoundingBox2D::new(Vector2D::new(-2.0, -2.0), Vector2D::new(4.0, 3.0));
    let point = Vector2D::new(2.0, 0.0);

    assert_eq!(aabb.contains(&point), true);
}
Source

pub fn intersects(&self, ray: &Ray2<T>) -> bool

Returns true if the input ray is intersecting with this box.

use vox_geometry_rust::bounding_box2::BoundingBox2D;
use vox_geometry_rust::vector2::Vector2D;
use vox_geometry_rust::ray2::Ray2D;
let aabb = BoundingBox2D::new(Vector2D::new(-2.0, -2.0), Vector2D::new(4.0, 3.0));

let ray1 = Ray2D::new(Vector2D::new(-3.0, 0.0), Vector2D::new(2.0, 1.0).normalized());
assert_eq!(aabb.intersects(&ray1), true);

let ray2 = Ray2D::new(Vector2D::new(3.0, -1.0), Vector2D::new(-1.0, 2.0).normalized());
assert_eq!(aabb.intersects(&ray2), true);

let ray3 = Ray2D::new(Vector2D::new(1.0, -5.0), Vector2D::new(2.0, 1.0).normalized());
assert_eq!(aabb.intersects(&ray3), false);
Source

pub fn closest_intersection( &self, ray: &Ray2<T>, ) -> BoundingBoxRayIntersection2<T>

Returns intersection.isIntersecting = true if the input ray is intersecting with this box. If intersects, intersection.tNear is assigned with distant to the closest intersecting point, and intersection.tFar with furthest.

use vox_geometry_rust::bounding_box2::BoundingBox2D;
use vox_geometry_rust::bounding_box2::BoundingBoxRayIntersection2D;
use vox_geometry_rust::vector2::Vector2D;
use vox_geometry_rust::ray2::Ray2D;
 use vox_geometry_rust::assert_delta;
let aabb = BoundingBox2D::new(Vector2D::new(-2.0, -2.0), Vector2D::new(1.0, 0.0));

let ray1 = Ray2D::new(Vector2D::new(-4.0, -3.0), Vector2D::new(1.0, 1.0).normalized());
let intersection1 = aabb.closest_intersection(&ray1);
assert_eq!(intersection1.is_intersecting, true);
assert_delta!(Vector2D::new(2.0, 2.0).length(), intersection1.t_near, f64::EPSILON);
assert_eq!(Vector2D::new(3.0, 3.0).length(), intersection1.t_far);

let ray2 = Ray2D::new(Vector2D::new(0.0, -1.0), Vector2D::new(-2.0, 1.0).normalized());
let intersection2 = aabb.closest_intersection(&ray2);
assert_eq!(intersection2.is_intersecting, true);
assert_delta!(Vector2D::new(2.0, 1.0).length(), intersection2.t_near, f64::EPSILON);
Source

pub fn mid_point(&self) -> Vector2<T>

Returns the mid-point of this box.

use vox_geometry_rust::bounding_box2::BoundingBox2D;
use vox_geometry_rust::vector2::Vector2D;
let aabb = BoundingBox2D::new(Vector2D::new(-2.0, -2.0), Vector2D::new(4.0, 3.0));
let mid_point = aabb.mid_point();

assert_eq!(1.0, mid_point.x);
assert_eq!(0.5, mid_point.y);
Source

pub fn diagonal_length(&self) -> T

Returns diagonal length of this box.

use vox_geometry_rust::bounding_box2::BoundingBox2D;
use vox_geometry_rust::vector2::Vector2D;
let aabb = BoundingBox2D::new(Vector2D::new(-2.0, -2.0), Vector2D::new(4.0, 3.0));
let diagonal_length = aabb.diagonal_length();

assert_eq!(f64::sqrt(6.0 * 6.0 + 5.0 * 5.0), diagonal_length);
Source

pub fn diagonal_length_squared(&self) -> T

Returns squared diagonal length of this box.

use vox_geometry_rust::bounding_box2::BoundingBox2D;
use vox_geometry_rust::vector2::Vector2D;
let aabb = BoundingBox2D::new(Vector2D::new(-2.0, -2.0), Vector2D::new(4.0, 3.0));
let diagonal_length_squared = aabb.diagonal_length_squared();

assert_eq!(6.0 * 6.0 + 5.0 * 5.0, diagonal_length_squared);
Source

pub fn reset(&mut self)

Resets this box to initial state (min=infinite, max=-infinite).

use vox_geometry_rust::bounding_box2::BoundingBox2D;
use vox_geometry_rust::vector2::Vector2D;
let mut aabb = BoundingBox2D::new(Vector2D::new(-2.0, -2.0), Vector2D::new(4.0, 3.0));
aabb.reset();

let max_double = f64::MAX;

assert_eq!(max_double, aabb.lower_corner.x);
assert_eq!(max_double, aabb.lower_corner.y);

assert_eq!(-max_double, aabb.upper_corner.x);
assert_eq!(-max_double, aabb.upper_corner.y);
Source

pub fn merge_vec(&mut self, point: &Vector2<T>)

Merges this and other point.

use vox_geometry_rust::bounding_box2::BoundingBox2D;
use vox_geometry_rust::vector2::Vector2D;
let mut aabb = BoundingBox2D::new(Vector2D::new(-2.0, -2.0), Vector2D::new(4.0, 3.0));
let point = Vector2D::new(5.0, 1.0);
 
aabb.merge_vec(&point);
 
assert_eq!(-2.0, aabb.lower_corner.x);
assert_eq!(-2.0, aabb.lower_corner.y);
 
assert_eq!(5.0, aabb.upper_corner.x);
assert_eq!(3.0, aabb.upper_corner.y);
Source

pub fn merge_box(&mut self, other: &BoundingBox2<T>)

Merges this and other box.

use vox_geometry_rust::bounding_box2::BoundingBox2D;
use vox_geometry_rust::vector2::Vector2D;
let mut box1 = BoundingBox2D::new(Vector2D::new(-2.0, -2.0), Vector2D::new(4.0, 3.0));
let box2 = BoundingBox2D::new(Vector2D::new(3.0, 1.0), Vector2D::new(8.0, 2.0));

box1.merge_box(&box2);

assert_eq!(-2.0, box1.lower_corner.x);
assert_eq!(-2.0, box1.lower_corner.y);

assert_eq!(8.0, box1.upper_corner.x);
assert_eq!(3.0, box1.upper_corner.y);
Source

pub fn expand(&mut self, delta: T)

Expands this box by given delta to all direction. If the width of the box was x, expand(y) will result a box with x+y+y width.

use vox_geometry_rust::bounding_box2::BoundingBox2D;
use vox_geometry_rust::vector2::Vector2D;
let mut aabb = BoundingBox2D::new(Vector2D::new(-2.0, -2.0), Vector2D::new(4.0, 3.0));
aabb.expand(3.0);

assert_eq!(-5.0, aabb.lower_corner.x);
assert_eq!(-5.0, aabb.lower_corner.y);

assert_eq!(7.0, aabb.upper_corner.x);
assert_eq!(6.0, aabb.upper_corner.y);
Source

pub fn corner(&self, idx: usize) -> Vector2<T>

Returns corner position. Index starts from x-first order.

use vox_geometry_rust::bounding_box2::BoundingBox2D;
use vox_geometry_rust::vector2::Vector2D;
let aabb = BoundingBox2D::new(Vector2D::new(-2.0, -2.0), Vector2D::new(4.0, 3.0));
assert_eq!(Vector2D::new(-2.0, -2.0), aabb.corner(0));
assert_eq!(Vector2D::new(4.0, -2.0), aabb.corner(1));
assert_eq!(Vector2D::new(-2.0, 3.0), aabb.corner(2));
assert_eq!(Vector2D::new(4.0, 3.0), aabb.corner(3));
Source

pub fn clamp(&self, pt: &Vector2<T>) -> Vector2<T>

Returns the clamped point.

Source

pub fn is_empty(&self) -> bool

Returns true if the box is empty.

use vox_geometry_rust::bounding_box2::BoundingBox2D;
use vox_geometry_rust::vector2::Vector2D;
let mut aabb = BoundingBox2D::new(Vector2D::new(-2.0, -2.0), Vector2D::new(4.0, 3.0));
assert_eq!(aabb.is_empty(), false);
 
aabb.lower_corner = Vector2D::new(5.0, 1.0);
assert_eq!(aabb.is_empty(), true);
 
aabb.lower_corner = Vector2D::new(2.0, 4.0);
assert_eq!(aabb.is_empty(), true);
 
aabb.lower_corner = Vector2D::new(4.0, 1.0);
assert_eq!(aabb.is_empty(), true);

Trait Implementations§

Source§

impl<T: Float> Clone for BoundingBox2<T>

Source§

fn clone(&self) -> Self

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<T: Float + Debug> Debug for BoundingBox2<T>

Source§

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

§Example

use vox_geometry_rust::vector2::Vector2F;
let vec = Vector2F::new(10.0, 20.0);
assert_eq!(format!("{:?}", vec), "(10.0, 20.0)");

assert_eq!(format!("{:#?}", vec), "(
    10.0,
    20.0,
)");

Auto Trait Implementations§

§

impl<T> Freeze for BoundingBox2<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for BoundingBox2<T>
where T: RefUnwindSafe,

§

impl<T> Send for BoundingBox2<T>
where T: Send,

§

impl<T> Sync for BoundingBox2<T>
where T: Sync,

§

impl<T> Unpin for BoundingBox2<T>
where T: Unpin,

§

impl<T> UnwindSafe for BoundingBox2<T>
where T: UnwindSafe,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V