Aab

Struct Aab 

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

Axis-Aligned Box data type.

Note that this has continuous coordinates, and a discrete analogue exists as GridAab.

§Serialization stability warning

This type implements [serde::Serialize] and [serde::Deserialize], but serialization support is still experimental (as is the game data model in general). We do not guarantee that future versions of all-is-cubes will be able to deserialize data which is serialized by this version.

Additionally, the serialization schema is designed with serde_json in mind. It is not guaranteed that using a different data format crate, which may use a different subset of the information exposed via [serde::Serialize], will produce stable results.

Implementations§

Source§

impl Aab

Source

pub const ZERO: Aab

The Aab of zero size at the origin.

Source

pub fn new( lx: FreeCoordinate, hx: FreeCoordinate, ly: FreeCoordinate, hy: FreeCoordinate, lz: FreeCoordinate, hz: FreeCoordinate, ) -> Self

Constructs an Aab from individual coordinates.

Source

pub fn from_lower_upper( lower_bounds: impl Into<FreePoint>, upper_bounds: impl Into<FreePoint>, ) -> Self

Constructs an Aab from most-negative and most-positive corner points.

Panics if the points are not in the proper order or if they are NaN.

Source

pub const fn lower_bounds_p(&self) -> FreePoint

The most negative corner of the box, as a Point3D.

Source

pub const fn upper_bounds_p(&self) -> FreePoint

The most positive corner of the box, as a Point3D.

Source

pub fn lower_bounds_v(&self) -> FreeVector

The most negative corner of the box, as a Vector3D.

Source

pub fn upper_bounds_v(&self) -> FreeVector

The most positive corner of the box, as a Vector3D.

Source

pub fn face_coordinate(&self, face: Face6) -> FreeCoordinate

Returns the position of the identified face of the box on the axis it is perpendicular to.

Note that negative faces’ coordinates are inverted; that is, all results will be positive if the box contains its origin.

Source

pub fn size(&self) -> Size3D<FreeCoordinate, Cube>

Size of the box in each axis; equivalent to self.upper_bounds() - self.lower_bounds().

Note that due to floating-point rounding, translating one corner point by the size does not necessarily exactly reach the opposite corner. (See the Sterbenz lemma for when it does.) Therefore, in cases where exact comparisons matter, take care to prefer the corner points over calculating with the size.

Source

pub fn center(&self) -> FreePoint

The center of the enclosed volume.

use all_is_cubes::math::{Aab, FreePoint};

let aab = Aab::new(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);
assert_eq!(aab.center(), FreePoint::new(1.5, 3.5, 5.5));
Source

pub fn contains(&self, point: FreePoint) -> bool

Returns whether this AAB, including the boundary, contains the point.

TODO: example + tests

Source

pub fn intersects(&self, other: Aab) -> bool

Returns whether this AAB, including the boundary, intersects the other AAB.

TODO: example + tests

Source

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

Returns the smallest Aab which contains every point the two inputs contain, including boundary points.

Source

pub fn union_point(self, point: FreePoint) -> Self

Extend the bounds of self so that point is on the interior or edge of this.

If any coordinate is NaN, the box is unchanged on that axis.

§Example
use all_is_cubes::math::{Aab, FreePoint};

assert_eq!(
    Aab::ZERO.union_point(FreePoint::new(2., 5., 10.)),
    Aab::from_lower_upper([0., 0., 0.], [2., 5., 10.]),
);
Source

pub fn random_point(self, rng: &mut impl Rng) -> FreePoint

Returns a random point within this box, using inclusive ranges (lower_bounds[axis] ≤ random_point()[axis] ≤ upper_bounds[axis]).

Source

pub fn translate(self, offset: FreeVector) -> Self

Translate this box by the specified offset.

Note that due to rounding error, the result may not have the same size.

Source

pub fn scale(self, scalar: FreeCoordinate) -> Self

Scale this AAB by the given amount (about the zero point, not its center).

Source

pub fn expand(self, distance: FreeCoordinate) -> Self

Enlarges the AAB by moving each face outward by the specified distance (or inward if negative).

If this would result in a negative or NaN size, produces a zero size AAB located at the center point of self.

use all_is_cubes::math::Aab;

assert_eq!(
    Aab::new(1.0, 2.0, 3.0, 4.0, 5.0, 6.0).expand(0.25),
    Aab::new(0.75, 2.25, 2.75, 4.25, 4.75, 6.25)
);
Source

pub fn round_up_to_grid(self) -> GridAab

Construct the GridAab containing all cubes this Aab intersects.

Grid cubes are considered to be half-open ranges, so, for example, an Aab with exact integer bounds on some axis will convert exactly as one might intuitively expect, while non-integer bounds will be rounded outward:

use all_is_cubes::math::{Aab, GridAab};

let grid_aab = Aab::from_lower_upper([3.0, 0.5, 0.0], [5.0, 1.5, 1.0])
    .round_up_to_grid();
assert_eq!(grid_aab, GridAab::from_lower_upper([3, 0, 0], [5, 2, 1]));

assert!(grid_aab.contains_cube([4, 1, 0].into()));
assert!(!grid_aab.contains_cube([5, 1, 0].into()));

If the floating-point coordinates are out of GridCoordinate’s numeric range, then they will be clamped.

use all_is_cubes::math::{FreeCoordinate, GridCoordinate};

assert_eq!(
    Aab::from_lower_upper(
        [3.0, 0.0, 0.0],
        [(GridCoordinate::MAX as FreeCoordinate) * 10.0, 1.0, 1.0],
    ).round_up_to_grid(),
    GridAab::from_lower_upper([3, 0, 0], [GridCoordinate::MAX, 1, 1]),
);
assert_eq!(
    Aab::from_lower_upper(
        [3.0, 0.0, 0.0],
        [FreeCoordinate::INFINITY, 1.0, 1.0],
    ).round_up_to_grid(),
    GridAab::from_lower_upper([3, 0, 0], [GridCoordinate::MAX, 1, 1]),
);

(There is no handling of NaN, because Aab does not allow NaN values.)

Trait Implementations§

Source§

impl Clone for Aab

Source§

fn clone(&self) -> Aab

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 Aab

Source§

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

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

impl From<GridAab> for Aab

Source§

fn from(value: GridAab) -> Self

Converts value to floating-point coordinates.

This conversion is also available as GridAab::to_free(), which may be more convenient in a method chain.

Source§

impl PartialEq for Aab

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Wireframe for Aab

Source§

fn wireframe_points<E>(&self, output: &mut E)
where E: Extend<LineVertex>,

Represent this object as a line drawing, or wireframe. Read more
Source§

impl Copy for Aab

Source§

impl Eq for Aab

Aab rejects NaN values, so it can implement Eq even though it contains floats.

Source§

impl StructuralPartialEq for Aab

Auto Trait Implementations§

§

impl Freeze for Aab

§

impl RefUnwindSafe for Aab

§

impl Send for Aab

§

impl Sync for Aab

§

impl Unpin for Aab

§

impl UnwindSafe for Aab

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

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
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> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
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> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.