diff --git a/src/core/core.rs b/src/core/core.rs index f98c178..d9f0b99 100644 --- a/src/core/core.rs +++ b/src/core/core.rs @@ -16,11 +16,15 @@ // Core datatypes and conversion traits for 3D mathematics pub use self::dim::Dimensional; +pub use self::swap::Swap; + pub use self::mat::{Mat2, ToMat2, Mat3, ToMat3, Mat4, ToMat4}; pub use self::quat::{Quat, ToQuat}; pub use self::vec::{Vec2, Vec3, Vec4}; pub mod dim; +pub mod swap; + pub mod mat; pub mod quat; pub mod vec; diff --git a/src/core/dim.rs b/src/core/dim.rs index 316bcbd..4b3705c 100644 --- a/src/core/dim.rs +++ b/src/core/dim.rs @@ -13,9 +13,76 @@ // See the License for the specific language governing permissions and // limitations under the License. +use core::{Mat2, Mat3, Mat4}; +use core::{Vec2, Vec3, Vec4, Quat}; + pub trait Dimensional { pub fn index<'a>(&'a self, i: uint) -> &'a T; pub fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T; pub fn as_slice<'a>(&'a self) -> &'a Slice; pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut Slice; } + +macro_rules! impl_dimensional( + ($Self:ident, $T:ty, $n:expr) => ( + impl Dimensional<$T,[$T,..$n]> for $Self { + #[inline] + pub fn index<'a>(&'a self, i: uint) -> &'a $T { + &'a self.as_slice()[i] + } + + #[inline] + pub fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut $T { + &'a mut self.as_mut_slice()[i] + } + + #[inline] + pub fn as_slice<'a>(&'a self) -> &'a [$T,..$n] { + use std::cast::transmute; + unsafe { transmute(self) } + } + + #[inline] + pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [$T,..$n] { + use std::cast::transmute; + unsafe { transmute(self) } + } + } + ) +) + +impl_dimensional!(Vec2, T, 2) +impl_dimensional!(Vec3, T, 3) +impl_dimensional!(Vec4, T, 4) +impl_dimensional!(Quat, T, 4) +impl_dimensional!(Mat2, Vec2, 2) +impl_dimensional!(Mat3, Vec3, 3) +impl_dimensional!(Mat4, Vec4, 4) + +// This enclosing module is required because attributes don't play nice +// with macros yet +#[cfg(geom)] +pub mod geom_impls { + use super::Dimensional; + use geom::{Point2, Point3}; + + impl_dimensional!(Point2, T, 2) + impl_dimensional!(Point3, T, 3) +} + +// This enclosing module is required because attributes don't play nice +// with macros yet +#[cfg(color)] +pub mod color_impls { + use super::Dimensional; + use color::{HSV, HSVA, YCbCr}; + use color::{RGB, RGBA, SRGB, SRGBA}; + + impl_dimensional!(HSV, T, 3) + impl_dimensional!(HSVA, T, 4) + impl_dimensional!(RGB, T, 3) + impl_dimensional!(RGBA, T, 4) + impl_dimensional!(SRGB, T, 3) + impl_dimensional!(SRGBA, T, 4) + impl_dimensional!(YCbCr, T, 3) +} diff --git a/src/core/mat.rs b/src/core/mat.rs index 27b063d..1e6f1de 100644 --- a/src/core/mat.rs +++ b/src/core/mat.rs @@ -13,13 +13,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -use core::Dimensional; +use core::{Dimensional, Swap}; use core::{Quat, ToQuat}; use core::{Vec2, Vec3, Vec4}; #[path = "../num_macros.rs"] mod num_macros; -mod dim_macros; macro_rules! impl_mat( ($Mat:ident, $Vec:ident) => ( @@ -125,7 +124,6 @@ pub type Mat2f = Mat2; pub type Mat2f32 = Mat2; pub type Mat2f64 = Mat2; -impl_dimensional!(Mat2, Vec2, 2) impl_mat!(Mat2, Vec2) impl_mat_swap!(Mat2, Vec2) @@ -554,7 +552,6 @@ pub type Mat3f = Mat3; pub type Mat3f32 = Mat3; pub type Mat3f64 = Mat3; -impl_dimensional!(Mat3, Vec3, 3) impl_mat!(Mat3, Vec3) impl_mat_swap!(Mat3, Vec3) @@ -1160,7 +1157,6 @@ pub type Mat4f = Mat4; pub type Mat4f32 = Mat4; pub type Mat4f64 = Mat4; -impl_dimensional!(Mat4, Vec4, 4) impl_mat!(Mat4, Vec4) impl_mat_swap!(Mat4, Vec4) diff --git a/src/core/quat.rs b/src/core/quat.rs index c7ef793..365e7a2 100644 --- a/src/core/quat.rs +++ b/src/core/quat.rs @@ -19,7 +19,6 @@ use core::Vec3; #[path = "../num_macros.rs"] mod num_macros; -mod dim_macros; // GLSL-style type aliases @@ -36,9 +35,6 @@ pub type Quatf64 = Quat; #[deriving(Clone, Eq)] pub struct Quat { s: T, v: Vec3 } -impl_dimensional!(Quat, T, 4) -impl_swap!(Quat) - pub trait ToQuat { pub fn to_quat(&self) -> Quat; } diff --git a/src/core/dim_macros.rs b/src/core/swap.rs similarity index 54% rename from src/core/dim_macros.rs rename to src/core/swap.rs index 9f463d3..91a8e57 100644 --- a/src/core/dim_macros.rs +++ b/src/core/swap.rs @@ -13,39 +13,15 @@ // See the License for the specific language governing permissions and // limitations under the License. -#[macro_escape]; +use core::{Vec2, Vec3, Vec4, Quat}; -macro_rules! impl_dimensional( - ($Self:ident, $T:ty, $n:expr) => ( - impl Dimensional<$T,[$T,..$n]> for $Self { - #[inline] - pub fn index<'a>(&'a self, i: uint) -> &'a $T { - &'a self.as_slice()[i] - } - - #[inline] - pub fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut $T { - &'a mut self.as_mut_slice()[i] - } - - #[inline] - pub fn as_slice<'a>(&'a self) -> &'a [$T,..$n] { - use std::cast::transmute; - unsafe { transmute(self) } - } - - #[inline] - pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [$T,..$n] { - use std::cast::transmute; - unsafe { transmute(self) } - } - } - ) -) +pub trait Swap { + pub fn swap(&mut self, a: uint, b: uint); +} macro_rules! impl_swap( ($Self:ident) => ( - impl $Self { + impl Swap for $Self { #[inline] pub fn swap(&mut self, a: uint, b: uint) { let tmp = self.index(a).clone(); @@ -55,3 +31,36 @@ macro_rules! impl_swap( } ) ) + +impl_swap!(Vec2) +impl_swap!(Vec3) +impl_swap!(Vec4) +impl_swap!(Quat) + +// This enclosing module is required because attributes don't play nice +// with macros yet +#[cfg(geom)] +pub mod geom_impls { + use super::Swap; + use geom::{Point2, Point3}; + + impl_swap!(Point2) + impl_swap!(Point3) +} + +// This enclosing module is required because attributes don't play nice +// with macros yet +#[cfg(color)] +pub mod color_impls { + use super::Swap; + use color::{HSV, HSVA, YCbCr}; + use color::{RGB, RGBA, SRGB, SRGBA}; + + impl_swap!(HSV) + impl_swap!(HSVA) + impl_swap!(RGB) + impl_swap!(RGBA) + impl_swap!(SRGB) + impl_swap!(SRGBA) + impl_swap!(YCbCr) +} diff --git a/src/core/vec.rs b/src/core/vec.rs index 9d0f2bc..ff123c7 100644 --- a/src/core/vec.rs +++ b/src/core/vec.rs @@ -22,7 +22,6 @@ use geom::{Point2, Point3}; #[path = "../num_macros.rs"] mod num_macros; -mod dim_macros; #[deriving(Clone, Eq)] pub struct Vec2 { x: T, y: T } @@ -50,9 +49,6 @@ pub type Vec2u32 = Vec2; pub type Vec2u64 = Vec2; pub type Vec2b = Vec2; -impl_dimensional!(Vec2, T, 2) -impl_swap!(Vec2) - impl Vec2 { #[inline] pub fn new(x: T, y: T) -> Vec2 { @@ -590,9 +586,6 @@ pub type Vec3u32 = Vec3; pub type Vec3u64 = Vec3; pub type Vec3b = Vec3; -impl_dimensional!(Vec3, T, 3) -impl_swap!(Vec3) - impl Vec3 { #[inline] pub fn new(x: T, y: T, z: T) -> Vec3 { @@ -1195,9 +1188,6 @@ pub type Vec4u32 = Vec4; pub type Vec4u64 = Vec4; pub type Vec4b = Vec4; -impl_dimensional!(Vec4, T, 4) -impl_swap!(Vec4) - impl Vec4 { #[inline] pub fn new(x: T, y: T, z: T, w: T) -> Vec4 {