diff --git a/src/color/hsv.rs b/src/color/hsv.rs index 87010df..e816c2e 100644 --- a/src/color/hsv.rs +++ b/src/color/hsv.rs @@ -17,6 +17,8 @@ use std::num; use std::cast; use core::{Dimensional, Swap}; +use core::{Vec3, ToVec3, AsVec3}; +use core::{Vec4, ToVec4, AsVec4}; use color::{Color, FloatColor}; use color::{Channel, FloatChannel}; use color::{RGB, ToRGB, RGBA, ToRGBA}; @@ -25,6 +27,8 @@ use color::{RGB, ToRGB, RGBA, ToRGBA}; pub struct HSV { h: T, s: T, v: T } impl_dimensional!(HSV, T, 3) +impl_to_vec!(HSV, 3) +impl_as_vec!(HSV, 3) impl_swap!(HSV) impl_approx!(HSV { h, s, v }) @@ -126,6 +130,8 @@ impl ToRGB for HSV { pub struct HSVA { h: T, s: T, v: T, a: T } impl_dimensional!(HSVA, T, 4) +impl_to_vec!(HSVA, 4) +impl_as_vec!(HSVA, 4) impl_swap!(HSVA) impl_approx!(HSVA { h, s, v, a }) diff --git a/src/color/rgb.rs b/src/color/rgb.rs index 848c140..326c864 100644 --- a/src/color/rgb.rs +++ b/src/color/rgb.rs @@ -17,6 +17,8 @@ use std::num; use std::cast; use core::{Dimensional, Swap}; +use core::{Vec3, ToVec3, AsVec3}; +use core::{Vec4, ToVec4, AsVec4}; use color::{Color, FloatColor}; use color::{Channel, FloatChannel}; use color::{HSV, ToHSV, HSVA, ToHSVA}; @@ -25,6 +27,8 @@ use color::{HSV, ToHSV, HSVA, ToHSVA}; pub struct RGB { r: T, g: T, b: T } impl_dimensional!(RGB, T, 3) +impl_to_vec!(RGB, 3) +impl_as_vec!(RGB, 3) impl_swap!(RGB) impl_approx!(RGB { r, g, b }) @@ -123,6 +127,8 @@ impl ToHSV for RGB { pub struct RGBA { r: T, g: T, b: T, a: T } impl_dimensional!(RGBA, T, 4) +impl_to_vec!(RGBA, 4) +impl_as_vec!(RGBA, 4) impl_swap!(RGBA) impl_approx!(RGBA { r, g, b, a }) diff --git a/src/color/srgb.rs b/src/color/srgb.rs index f421c2c..6e062f0 100644 --- a/src/color/srgb.rs +++ b/src/color/srgb.rs @@ -14,6 +14,8 @@ // limitations under the License. use core::{Dimensional, Swap}; +use core::{Vec3, ToVec3, AsVec3}; +use core::{Vec4, ToVec4, AsVec4}; #[deriving(Clone, Eq)] pub struct SRGB { r: T, g: T, b: T } @@ -26,6 +28,8 @@ impl SRGB { } impl_dimensional!(SRGB, T, 3) +impl_to_vec!(SRGB, 3) +impl_as_vec!(SRGB, 3) impl_swap!(SRGB) impl_approx!(SRGB { r, g, b }) @@ -33,6 +37,8 @@ impl_approx!(SRGB { r, g, b }) pub struct SRGBA { r: T, g: T, b: T, a: T } impl_dimensional!(SRGBA, T, 4) +impl_to_vec!(SRGBA, 4) +impl_as_vec!(SRGBA, 4) impl_swap!(SRGBA) impl_approx!(SRGBA { r, g, b, a }) diff --git a/src/color/ycbcr.rs b/src/color/ycbcr.rs index e7b3f02..34a2e42 100644 --- a/src/color/ycbcr.rs +++ b/src/color/ycbcr.rs @@ -16,11 +16,14 @@ // http://en.wikipedia.org/wiki/YCbCr use core::{Dimensional, Swap}; +use core::{Vec3, ToVec3, AsVec3}; #[deriving(Clone, Eq)] pub struct YCbCr { y: T, cb: T, cr: T } impl_dimensional!(YCbCr, T, 3) +impl_to_vec!(YCbCr, 3) +impl_as_vec!(YCbCr, 3) impl_swap!(YCbCr) impl_approx!(YCbCr { y, cb, cr }) diff --git a/src/core/macros.rs b/src/core/macros.rs index 166a94a..1da1d9d 100644 --- a/src/core/macros.rs +++ b/src/core/macros.rs @@ -43,6 +43,47 @@ macro_rules! impl_dimensional( ) ) +macro_rules! impl_to_vec( + ($Self:ident, 2) => (impl_to_vec_helper!(ToVec2, $Self, Vec2, to_vec2, as_vec2)); + ($Self:ident, 3) => (impl_to_vec_helper!(ToVec3, $Self, Vec3, to_vec3, as_vec3)); + ($Self:ident, 4) => (impl_to_vec_helper!(ToVec4, $Self, Vec4, to_vec4, as_vec4)); +) + +macro_rules! impl_to_vec_helper( + ($ToVec:ident, $Self:ident, $Vec:ident, $to_vec:ident, $as_vec:ident) => ( + impl $ToVec for $Self { + #[inline] + pub fn $to_vec(&self) -> $Vec { + self.$as_vec().clone() + } + } + ) +) + +macro_rules! impl_as_vec( + ($Self:ident, 2) => (impl_as_vec_helper!(AsVec2, $Self, Vec2, as_vec2, as_mut_vec2)); + ($Self:ident, 3) => (impl_as_vec_helper!(AsVec3, $Self, Vec3, as_vec3, as_mut_vec3)); + ($Self:ident, 4) => (impl_as_vec_helper!(AsVec4, $Self, Vec4, as_vec4, as_mut_vec4)); +) + +macro_rules! impl_as_vec_helper( + ($AsVec:ident, $Self:ident, $Vec:ident, $as_vec:ident, $as_mut_vec:ident) => ( + impl $AsVec for $Self { + #[inline] + pub fn $as_vec<'a>(&'a self) -> &'a $Vec { + use std::cast::transmute; + unsafe { transmute(self) } + } + + #[inline] + pub fn $as_mut_vec<'a>(&'a mut self) -> &'a mut $Vec { + use std::cast::transmute; + unsafe { transmute(self) } + } + } + ) +) + macro_rules! impl_swap( ($Self:ident) => ( impl Swap for $Self { diff --git a/src/geom/point.rs b/src/geom/point.rs index fcb9dd1..d00cad1 100644 --- a/src/geom/point.rs +++ b/src/geom/point.rs @@ -49,6 +49,8 @@ pub trait Point: Eq pub struct Point2 { x: T, y: T } impl_dimensional!(Point2, T, 2) +impl_to_vec!(Point2, 2) +impl_as_vec!(Point2, 2) impl_swap!(Point2) impl_approx!(Point2 { x, y }) @@ -86,25 +88,6 @@ impl Point2 { } } -impl ToVec2 for Point2 { - #[inline] - pub fn to_vec2(&self) -> Vec2 { - self.as_vec2().clone() - } -} - -impl AsVec2 for Point2 { - #[inline] - pub fn as_vec2<'a>(&'a self) -> &'a Vec2 { - unsafe { cast::transmute(self) } - } - - #[inline] - pub fn as_mut_vec2<'a>(&'a mut self) -> &'a mut Vec2 { - unsafe { cast::transmute(self) } - } -} - impl ToVec3 for Point2 { /// Converts the point to a three-dimensional homogeneous vector: /// `[x, y] -> [x, y, 1]` @@ -206,6 +189,8 @@ mod test_point2 { pub struct Point3 { x: T, y: T, z: T } impl_dimensional!(Point3, T, 3) +impl_to_vec!(Point3, 3) +impl_as_vec!(Point3, 3) impl_swap!(Point3) impl_approx!(Point3 { x, y, z }) @@ -243,27 +228,6 @@ impl Point3 { } } -impl ToVec3 for Point3 { - /// Converts the point to a three-dimensional homogeneous vector: - /// `[x, y] -> [x, y, 1]` - #[inline] - pub fn to_vec3(&self) -> Vec3 { - self.as_vec3().clone() - } -} - -impl AsVec3 for Point3 { - #[inline] - pub fn as_vec3<'a>(&'a self) -> &'a Vec3 { - unsafe { cast::transmute(self) } - } - - #[inline] - pub fn as_mut_vec3<'a>(&'a mut self) -> &'a mut Vec3 { - unsafe { cast::transmute(self) } - } -} - impl ToVec4 for Point3 { /// Converts the point to a four-dimensional homogeneous vector: /// `[x, y, z] -> [x, y, z, 1]` diff --git a/src/lmath.rs b/src/lmath.rs index 58fd0d5..786aeed 100644 --- a/src/lmath.rs +++ b/src/lmath.rs @@ -26,7 +26,6 @@ // Macros mod macros; - #[path = "core/macros.rs"] mod core_macros;