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
impl Aab
Sourcepub fn new(lx: f64, hx: f64, ly: f64, hy: f64, lz: f64, hz: f64) -> Aab
pub fn new(lx: f64, hx: f64, ly: f64, hy: f64, lz: f64, hz: f64) -> Aab
Constructs an Aab from individual coordinates.
Sourcepub fn from_lower_upper(
lower_bounds: impl Into<Point3D<f64, Cube>>,
upper_bounds: impl Into<Point3D<f64, Cube>>,
) -> Aab
pub fn from_lower_upper( lower_bounds: impl Into<Point3D<f64, Cube>>, upper_bounds: impl Into<Point3D<f64, Cube>>, ) -> Aab
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.
Sourcepub const fn lower_bounds_p(&self) -> Point3D<f64, Cube>
pub const fn lower_bounds_p(&self) -> Point3D<f64, Cube>
The most negative corner of the box, as a Point3D.
Sourcepub const fn upper_bounds_p(&self) -> Point3D<f64, Cube>
pub const fn upper_bounds_p(&self) -> Point3D<f64, Cube>
The most positive corner of the box, as a Point3D.
Sourcepub fn lower_bounds_v(&self) -> Vector3D<f64, Cube>
pub fn lower_bounds_v(&self) -> Vector3D<f64, Cube>
The most negative corner of the box, as a Vector3D.
Sourcepub fn upper_bounds_v(&self) -> Vector3D<f64, Cube>
pub fn upper_bounds_v(&self) -> Vector3D<f64, Cube>
The most positive corner of the box, as a Vector3D.
Sourcepub fn face_coordinate(&self, face: Face6) -> f64
pub fn face_coordinate(&self, face: Face6) -> f64
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.
Sourcepub fn size(&self) -> Size3D<f64, Cube>
pub fn size(&self) -> Size3D<f64, 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.
Sourcepub fn center(&self) -> Point3D<f64, Cube>
pub fn center(&self) -> Point3D<f64, Cube>
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));Sourcepub fn corner_point(&self, corner: Octant) -> Point3D<f64, Cube>
pub fn corner_point(&self, corner: Octant) -> Point3D<f64, Cube>
Returns one of the eight corner points of the box.
Note that Octant is used here only to identify the eight distinct positions and does
not mean that the corner necessarily lies in that octant of the coordinate space.
Sourcepub fn contains(&self, point: Point3D<f64, Cube>) -> bool
pub fn contains(&self, point: Point3D<f64, Cube>) -> bool
Returns whether this AAB, including the boundary, contains the point.
TODO: example + tests
Sourcepub fn intersects(&self, other: Aab) -> bool
pub fn intersects(&self, other: Aab) -> bool
Returns whether this AAB, including the boundary, intersects the other AAB.
TODO: example + tests
Sourcepub fn union(self, other: Aab) -> Aab
pub fn union(self, other: Aab) -> Aab
Returns the smallest Aab which contains every point the two inputs contain,
including boundary points.
Sourcepub fn union_point(self, point: Point3D<f64, Cube>) -> Aab
pub fn union_point(self, point: Point3D<f64, Cube>) -> Aab
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.]),
);Sourcepub fn random_point(self, rng: &mut impl RngCore) -> Point3D<f64, Cube>
pub fn random_point(self, rng: &mut impl RngCore) -> Point3D<f64, Cube>
Returns a random point within this box, using inclusive ranges
(lower_bounds[axis] ≤ random_point()[axis] ≤ upper_bounds[axis]).
Sourcepub fn translate(self, offset: Vector3D<f64, Cube>) -> Aab
pub fn translate(self, offset: Vector3D<f64, Cube>) -> Aab
Translate this box by the specified offset.
Note that due to rounding error, the result may not have the same size.
Sourcepub fn scale(self, scalar: f64) -> Aab
pub fn scale(self, scalar: f64) -> Aab
Scale this AAB by the given amount (about the zero point, not its center).
Sourcepub fn expand(self, distance: f64) -> Aab
pub fn expand(self, distance: f64) -> Aab
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)
);Sourcepub fn round_up_to_grid(self) -> GridAab
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<'de> Deserialize<'de> for Aab
impl<'de> Deserialize<'de> for Aab
Source§fn deserialize<D>(
deserializer: D,
) -> Result<Aab, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Aab, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl Serialize for Aab
impl Serialize for Aab
Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
impl Copy for Aab
impl Eq for Aab
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> 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> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
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