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: FreeCoordinate,
hx: FreeCoordinate,
ly: FreeCoordinate,
hy: FreeCoordinate,
lz: FreeCoordinate,
hz: FreeCoordinate,
) -> Self
pub fn new( lx: FreeCoordinate, hx: FreeCoordinate, ly: FreeCoordinate, hy: FreeCoordinate, lz: FreeCoordinate, hz: FreeCoordinate, ) -> Self
Constructs an Aab from individual coordinates.
Sourcepub fn from_lower_upper(
lower_bounds: impl Into<FreePoint>,
upper_bounds: impl Into<FreePoint>,
) -> Self
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.
Sourcepub const fn lower_bounds_p(&self) -> FreePoint
pub const fn lower_bounds_p(&self) -> FreePoint
The most negative corner of the box, as a Point3D.
Sourcepub const fn upper_bounds_p(&self) -> FreePoint
pub const fn upper_bounds_p(&self) -> FreePoint
The most positive corner of the box, as a Point3D.
Sourcepub fn lower_bounds_v(&self) -> FreeVector
pub fn lower_bounds_v(&self) -> FreeVector
The most negative corner of the box, as a Vector3D.
Sourcepub fn upper_bounds_v(&self) -> FreeVector
pub fn upper_bounds_v(&self) -> FreeVector
The most positive corner of the box, as a Vector3D.
Sourcepub fn face_coordinate(&self, face: Face6) -> FreeCoordinate
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.
Sourcepub fn size(&self) -> Size3D<FreeCoordinate, Cube>
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.
Sourcepub fn center(&self) -> FreePoint
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));Sourcepub fn contains(&self, point: FreePoint) -> bool
pub fn contains(&self, point: FreePoint) -> 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: Self) -> Self
pub fn union(self, other: Self) -> Self
Returns the smallest Aab which contains every point the two inputs contain,
including boundary points.
Sourcepub fn union_point(self, point: FreePoint) -> Self
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.]),
);Sourcepub fn random_point(self, rng: &mut impl Rng) -> FreePoint
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]).
Sourcepub fn translate(self, offset: FreeVector) -> Self
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.
Sourcepub fn scale(self, scalar: FreeCoordinate) -> Self
pub fn scale(self, scalar: FreeCoordinate) -> Self
Scale this AAB by the given amount (about the zero point, not its center).
Sourcepub fn expand(self, distance: FreeCoordinate) -> Self
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)
);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 From<GridAab> for Aab
impl From<GridAab> for Aab
Source§fn from(value: GridAab) -> Self
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.