Cube

Struct Cube 

Source
#[repr(C)]
pub struct Cube { pub x: i32, pub y: i32, pub z: i32, }
Expand description

“A cube”, in this documentation, is a unit cube whose corners’ coordinates are integers. This type identifies such a cube by the coordinates of its most negative corner.

The valid coordinate range is that of GridCoordinate. Note, however, that in most applications, cubes with lower corner coordinates equal to GridCoordinate::MAX will not be valid, because their other corners are out of range. The Cube type does not enforce this, because it would be unergonomic to require fallible conversions there. Instead, the conversion from Cube to its bounding GridAab may panic. Generally, this should be avoided by checking the cube with GridAab::contains_cube() on some existing GridAab.

Considered in continuous space (real, or floating-point, coordinates), the ranges of coordinates a cube contains are half-open intervals: lower inclusive and upper exclusive.

§Representation

This struct is guaranteed to be three i32 without padding, and so may be reinterpreted as any type of identical layout such as [i32; 3].

§Why have a dedicated type for this?

  • Primarily, to avoid confusion between points (zero size) and cubes (nonzero size) that causes off-by-one errors when rotating objects.
  • To provide convenient methods for operations on cubes that aren’t natural operations on points.
  • To reduce our dependence on external math libraries as part of our API.

Fields§

§x: i32§y: i32§z: i32

Implementations§

Source§

impl Cube

Source

pub const ORIGIN: Self

Equal to Cube::new(0, 0, 0).

Note that this is not a box centered on the coordinate origin.

Source

pub const fn new( x: GridCoordinate, y: GridCoordinate, z: GridCoordinate, ) -> Self

Construct Cube { x, y, z } from the given coordinates.

Source

pub fn containing(point: FreePoint) -> Option<Self>

Convert a point in space to the unit cube that encloses it.

Such cubes are defined to be half-open intervals on each axis; that is, an integer coordinate is counted as part of the cube extending positively from that coordinate.

If the point coordinates are outside of the numeric range of GridCoordinate, returns None.

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

assert_eq!(Cube::containing(FreePoint::new(1.0, 1.5, -2.5)), Some(Cube::new(1, 1, -3)));
Source

pub fn lower_bounds(self) -> GridPoint

Returns the corner of this cube with the most negative coordinates.

Source

pub fn upper_bounds(self) -> GridPoint

Returns the corner of this cube with the most positive coordinates.

Panics if self has any coordinates equal to GridCoordinate::MAX. Generally, that should be avoided by checking the cube with GridAab::contains_cube() on some existing GridAab before calling this method.

Source

pub fn midpoint(self) -> FreePoint

Returns the midpoint of this cube.

Source

pub fn grid_aab(self) -> GridAab

Constructs a GridAab with a volume of 1, containing this cube.

Panics if self has any coordinates equal to GridCoordinate::MAX. Generally, that should be avoided by checking the cube with GridAab::contains_cube() on some existing GridAab before calling this method.

Source

pub fn aab(self) -> Aab

Returns the bounding box in floating-point coordinates containing this cube.

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

assert_eq!(
    Cube::new(10, 20, -30).aab(),
    Aab::new(10.0, 11.0, 20.0, 21.0, -30.0, -29.0)
);
Source

pub fn checked_add(self, v: GridVector) -> Option<Self>

Source

pub fn map(self, f: impl FnMut(GridCoordinate) -> GridCoordinate) -> Self

Apply a function to each coordinate independently.

If a different return type is desired, use .lower_bounds().map(f) instead.

Trait Implementations§

Source§

impl Add<Face6> for Cube

Source§

type Output = Cube

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Face6) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<Vector3D<i32, Cube>> for Cube

Source§

type Output = Cube

The resulting type after applying the + operator.
Source§

fn add(self, rhs: GridVector) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign<Face6> for Cube

Source§

fn add_assign(&mut self, rhs: Face6)

Performs the += operation. Read more
Source§

impl AddAssign<Vector3D<i32, Cube>> for Cube

Source§

fn add_assign(&mut self, rhs: GridVector)

Performs the += operation. Read more
Source§

impl AsMut<[i32; 3]> for Cube

Source§

fn as_mut(&mut self) -> &mut [GridCoordinate; 3]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsRef<[i32; 3]> for Cube

Source§

fn as_ref(&self) -> &[GridCoordinate; 3]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Borrow<[i32; 3]> for Cube

Source§

fn borrow(&self) -> &[GridCoordinate; 3]

Immutably borrows from an owned value. Read more
Source§

impl BorrowMut<[i32; 3]> for Cube

Source§

fn borrow_mut(&mut self) -> &mut [GridCoordinate; 3]

Mutably borrows from an owned value. Read more
Source§

impl Clone for Cube

Source§

fn clone(&self) -> Cube

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 Cube

Source§

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

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

impl Fmt<ConciseDebug> for Cube

Source§

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

Formats self as specified by fopt into destination fmt. Read more
Source§

impl From<[i32; 3]> for Cube

Source§

fn from([x, y, z]: [GridCoordinate; 3]) -> Self

Converts to this type from the input type.
Source§

impl From<Cube> for [GridCoordinate; 3]

Source§

fn from(_: Cube) -> [GridCoordinate; 3]

Converts to this type from the input type.
Source§

impl From<Cube> for GridPoint

Source§

fn from(_: Cube) -> GridPoint

Converts to this type from the input type.
Source§

impl From<Point3D<i32, Cube>> for Cube

Source§

fn from(_: GridPoint) -> Self

Converts to this type from the input type.
Source§

impl Hash for Cube

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Index<Axis> for Cube

Source§

type Output = i32

The returned type after indexing.
Source§

fn index(&self, index: Axis) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<Axis> for Cube

Source§

fn index_mut(&mut self, index: Axis) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl PartialEq for Cube

Source§

fn eq(&self, other: &Cube) -> 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 Sub<Vector3D<i32, Cube>> for Cube

Source§

type Output = Cube

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: GridVector) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub for Cube

Source§

type Output = Vector3D<i32, Cube>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Cube) -> Self::Output

Performs the - operation. Read more
Source§

impl SubAssign<Vector3D<i32, Cube>> for Cube

Source§

fn sub_assign(&mut self, rhs: GridVector)

Performs the -= operation. Read more
Source§

impl Zeroable for Cube

Source§

fn zeroed() -> Self

Source§

impl Copy for Cube

Source§

impl Eq for Cube

Source§

impl Pod for Cube

Source§

impl StructuralPartialEq for Cube

Auto Trait Implementations§

§

impl Freeze for Cube

§

impl RefUnwindSafe for Cube

§

impl Send for Cube

§

impl Sync for Cube

§

impl Unpin for Cube

§

impl UnwindSafe for Cube

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<T> CheckedBitPattern for T
where T: AnyBitPattern,

Source§

type Bits = T

Self must have the same layout as the specified Bits except for the possible invalid bit patterns being checked during is_valid_bit_pattern.
Source§

fn is_valid_bit_pattern(_bits: &T) -> bool

If this function returns true, then it must be valid to reinterpret bits as &Self.
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<F, T> Refmt<F> for T
where T: Fmt<F> + ?Sized, F: ?Sized,

Source§

fn refmt<'a>(&'a self, fopt: &'a F) -> Wrapper<'a, F, T>

Wrap this value so that when formatted with fmt::Debug or fmt::Display, it uses the given Fmt custom format type instead. Read more
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.
Source§

impl<T> AnyBitPattern for T
where T: Pod,

Source§

impl<T> NoUninit for T
where T: Pod,