beevee: add Axis enum implementation

This commit is contained in:
Bruno BELANYI 2020-03-24 01:39:22 +01:00
parent 9c56134c67
commit ca28281b67
2 changed files with 173 additions and 0 deletions

170
beevee/src/axis.rs Normal file
View file

@ -0,0 +1,170 @@
use crate::{Point, Vector};
use std::fmt::{Display, Formatter, Result};
use std::ops::{Index, IndexMut};
/// An enum for indexing different spatial structures.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Axis {
/// The X axis.
X = 0,
/// The Y axis.
Y = 1,
/// The Z axis.
Z = 2,
}
/// Display implementation for [`Axis`].
///
/// [`Axis`]: enum.Axis.html
///
/// # Examples
/// ```
/// # use beevee::Axis;
/// assert_eq!(format!("{}", Axis::X), "x");
/// assert_eq!(format!("{}", Axis::Y), "y");
/// assert_eq!(format!("{}", Axis::Z), "z");
/// ```
impl Display for Axis {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(
f,
"{}",
match *self {
Axis::X => "x",
Axis::Y => "y",
Axis::Z => "z",
}
)
}
}
/// Slice indexing implementation by [`Axis`]
///
/// [`Axis`]: enum.Axis.html
///
/// # Examples
/// ```
/// # use beevee::Axis;
/// #
/// let slice = &[0., 1., 2.];
/// assert_eq!(slice[Axis::X], 0.);
/// ```
impl<T> Index<Axis> for [T] {
type Output = T;
fn index(&self, axis: Axis) -> &Self::Output {
&self[axis as usize]
}
}
/// Mutable slice indexing implementation by [`Axis`]
///
/// [`Axis`]: enum.Axis.html
///
/// # Examples
/// ```
/// # use beevee::Axis;
/// #
/// let slice = &mut [0., 1., 2.];
/// slice[Axis::X] = 3.;
/// assert_eq!(slice, &[3., 1., 2.]);
/// ```
impl<T> IndexMut<Axis> for [T] {
fn index_mut(&mut self, axis: Axis) -> &mut T {
&mut self[axis as usize]
}
}
/// [`Point`] indexing implementation by [`Axis`]
///
/// [`Axis`]: enum.Axis.html
/// [`Point`]: type.Point.html
///
/// # Examples
/// ```
/// # use beevee::{Axis, Point};
/// #
/// let point = Point::new(0., 1., 2.);
/// assert_eq!(point[Axis::X], 0.);
/// ```
impl Index<Axis> for Point {
type Output = f32;
fn index(&self, axis: Axis) -> &Self::Output {
match axis {
Axis::X => &self.x,
Axis::Y => &self.y,
Axis::Z => &self.z,
}
}
}
/// Mutable [`Point`] indexing implementation by [`Axis`]
///
/// [`Axis`]: enum.Axis.html
/// [`Point`]: type.Point.html
///
/// # Examples
/// ```
/// # use beevee::{Axis, Point};
/// #
/// let mut point = Point::new(0., 1., 2.);
/// point[Axis::X] = 3.;
/// assert_eq!(point, Point::new(3., 1., 2.));
/// ```
impl IndexMut<Axis> for Point {
fn index_mut(&mut self, axis: Axis) -> &mut f32 {
match axis {
Axis::X => &mut self.x,
Axis::Y => &mut self.y,
Axis::Z => &mut self.z,
}
}
}
/// [`Vector`] indexing implementation by [`Axis`]
///
/// [`Axis`]: enum.Axis.html
/// [`Vector`]: type.Vector.html
///
/// # Examples
/// ```
/// # use beevee::{Axis, Vector};
/// #
/// let point = Vector::new(0., 1., 2.);
/// assert_eq!(point[Axis::X], 0.);
/// ```
impl Index<Axis> for Vector {
type Output = f32;
fn index(&self, axis: Axis) -> &Self::Output {
match axis {
Axis::X => &self.x,
Axis::Y => &self.y,
Axis::Z => &self.z,
}
}
}
/// Mutable [`Vector`] indexing implementation by [`Axis`]
///
/// [`Axis`]: enum.Axis.html
/// [`Vector`]: type.Vector.html
///
/// # Examples
/// ```
/// # use beevee::{Axis, Vector};
/// #
/// let mut point = Vector::new(0., 1., 2.);
/// point[Axis::X] = 3.;
/// assert_eq!(point, Vector::new(3., 1., 2.));
/// ```
impl IndexMut<Axis> for Vector {
fn index_mut(&mut self, axis: Axis) -> &mut f32 {
match axis {
Axis::X => &mut self.x,
Axis::Y => &mut self.y,
Axis::Z => &mut self.z,
}
}
}

View file

@ -15,6 +15,9 @@ pub type Vector = nalgebra::Vector3<f32>;
/// The module relating to Axis-Aligned Bouding Boxes.
pub mod aabb;
mod axis;
pub use axis::*;
/// The module relating to Bouding Volume Hiearchy
pub mod bvh;