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
impl<T: Float> BoundingBox2<T>
§Constructor
Sourcepub fn new_default() -> BoundingBox2<T>
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);Sourcepub fn new(point1: Vector2<T>, point2: Vector2<T>) -> BoundingBox2<T>
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>
impl<T: Float> BoundingBox2<T>
Sourcepub fn width(&self) -> T
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());Sourcepub fn height(&self) -> T
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());Sourcepub fn length(&self, axis: usize) -> T
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));Sourcepub fn overlaps(&self, other: &BoundingBox2<T>) -> bool
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);
}Sourcepub fn contains(&self, point: &Vector2<T>) -> bool
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);
}Sourcepub fn intersects(&self, ray: &Ray2<T>) -> bool
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);Sourcepub fn closest_intersection(
&self,
ray: &Ray2<T>,
) -> BoundingBoxRayIntersection2<T>
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);Sourcepub fn mid_point(&self) -> Vector2<T>
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);Sourcepub fn diagonal_length(&self) -> T
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);Sourcepub fn diagonal_length_squared(&self) -> T
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);Sourcepub fn reset(&mut self)
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);Sourcepub fn merge_vec(&mut self, point: &Vector2<T>)
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);Sourcepub fn merge_box(&mut self, other: &BoundingBox2<T>)
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);Sourcepub fn expand(&mut self, delta: T)
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);Sourcepub fn corner(&self, idx: usize) -> Vector2<T>
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));Sourcepub fn is_empty(&self) -> bool
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>
impl<T: Float> Clone for BoundingBox2<T>
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> 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<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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