From ca28281b67062f19305e8c8069c9eefc22e4bd41 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 24 Mar 2020 01:39:22 +0100 Subject: [PATCH] beevee: add Axis enum implementation --- beevee/src/axis.rs | 170 +++++++++++++++++++++++++++++++++++++++++++++ beevee/src/lib.rs | 3 + 2 files changed, 173 insertions(+) create mode 100644 beevee/src/axis.rs diff --git a/beevee/src/axis.rs b/beevee/src/axis.rs new file mode 100644 index 0000000..41d8cc4 --- /dev/null +++ b/beevee/src/axis.rs @@ -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 Index 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 IndexMut 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 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 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 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 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, + } + } +} diff --git a/beevee/src/lib.rs b/beevee/src/lib.rs index bd9f6e8..6164261 100644 --- a/beevee/src/lib.rs +++ b/beevee/src/lib.rs @@ -15,6 +15,9 @@ pub type Vector = nalgebra::Vector3; /// 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;