From 7615a55d84354434e911c3fdbd56df950588cfca Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Fri, 12 Jul 2013 16:32:12 +1000 Subject: [PATCH] More module re-arrangement --- src/color/hsv.rs | 5 ++ src/color/rgb.rs | 5 ++ src/color/srgb.rs | 6 +++ src/color/ycbcr.rs | 4 ++ src/core/core.rs | 17 ++++--- src/core/dim.rs | 88 --------------------------------- src/core/{swap.rs => macros.rs} | 65 +++++++++++------------- src/core/mat.rs | 3 ++ src/core/quat.rs | 24 ++------- src/core/vec.rs | 8 ++- src/geom/frustum.rs | 14 ++---- src/geom/point.rs | 5 ++ src/lmath.rs | 5 ++ 13 files changed, 87 insertions(+), 162 deletions(-) delete mode 100644 src/core/dim.rs rename src/core/{swap.rs => macros.rs} (56%) diff --git a/src/color/hsv.rs b/src/color/hsv.rs index 9ada9e4..87010df 100644 --- a/src/color/hsv.rs +++ b/src/color/hsv.rs @@ -16,6 +16,7 @@ use std::num; use std::cast; +use core::{Dimensional, Swap}; use color::{Color, FloatColor}; use color::{Channel, FloatChannel}; use color::{RGB, ToRGB, RGBA, ToRGBA}; @@ -23,6 +24,8 @@ use color::{RGB, ToRGB, RGBA, ToRGBA}; #[deriving(Clone, Eq)] pub struct HSV { h: T, s: T, v: T } +impl_dimensional!(HSV, T, 3) +impl_swap!(HSV) impl_approx!(HSV { h, s, v }) impl HSV { @@ -122,6 +125,8 @@ impl ToRGB for HSV { #[deriving(Clone, Eq)] pub struct HSVA { h: T, s: T, v: T, a: T } +impl_dimensional!(HSVA, T, 4) +impl_swap!(HSVA) impl_approx!(HSVA { h, s, v, a }) impl HSVA { diff --git a/src/color/rgb.rs b/src/color/rgb.rs index 2c28388..848c140 100644 --- a/src/color/rgb.rs +++ b/src/color/rgb.rs @@ -16,6 +16,7 @@ use std::num; use std::cast; +use core::{Dimensional, Swap}; use color::{Color, FloatColor}; use color::{Channel, FloatChannel}; use color::{HSV, ToHSV, HSVA, ToHSVA}; @@ -23,6 +24,8 @@ use color::{HSV, ToHSV, HSVA, ToHSVA}; #[deriving(Clone, Eq)] pub struct RGB { r: T, g: T, b: T } +impl_dimensional!(RGB, T, 3) +impl_swap!(RGB) impl_approx!(RGB { r, g, b }) impl RGB { @@ -119,6 +122,8 @@ impl ToHSV for RGB { #[deriving(Clone, Eq)] pub struct RGBA { r: T, g: T, b: T, a: T } +impl_dimensional!(RGBA, T, 4) +impl_swap!(RGBA) impl_approx!(RGBA { r, g, b, a }) impl RGBA { diff --git a/src/color/srgb.rs b/src/color/srgb.rs index 52f1176..f421c2c 100644 --- a/src/color/srgb.rs +++ b/src/color/srgb.rs @@ -13,6 +13,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +use core::{Dimensional, Swap}; + #[deriving(Clone, Eq)] pub struct SRGB { r: T, g: T, b: T } @@ -23,11 +25,15 @@ impl SRGB { } } +impl_dimensional!(SRGB, T, 3) +impl_swap!(SRGB) impl_approx!(SRGB { r, g, b }) #[deriving(Clone, Eq)] pub struct SRGBA { r: T, g: T, b: T, a: T } +impl_dimensional!(SRGBA, T, 4) +impl_swap!(SRGBA) impl_approx!(SRGBA { r, g, b, a }) impl SRGBA { diff --git a/src/color/ycbcr.rs b/src/color/ycbcr.rs index a7280e4..e7b3f02 100644 --- a/src/color/ycbcr.rs +++ b/src/color/ycbcr.rs @@ -15,9 +15,13 @@ // http://en.wikipedia.org/wiki/YCbCr +use core::{Dimensional, Swap}; + #[deriving(Clone, Eq)] pub struct YCbCr { y: T, cb: T, cr: T } +impl_dimensional!(YCbCr, T, 3) +impl_swap!(YCbCr) impl_approx!(YCbCr { y, cb, cr }) impl YCbCr { diff --git a/src/core/core.rs b/src/core/core.rs index 9dfe04f..0aa9c42 100644 --- a/src/core/core.rs +++ b/src/core/core.rs @@ -15,18 +15,23 @@ //! 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, ToVec2, AsVec2}; pub use self::vec::{Vec3, ToVec3, AsVec3}; pub use self::vec::{Vec4, ToVec4, AsVec4}; -pub mod dim; -pub mod swap; - pub mod mat; pub mod quat; pub mod vec; + +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; +} + +pub trait Swap { + pub fn swap(&mut self, a: uint, b: uint); +} diff --git a/src/core/dim.rs b/src/core/dim.rs deleted file mode 100644 index 4b3705c..0000000 --- a/src/core/dim.rs +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright 2013 The Lmath Developers. For a full listing of the authors, -// refer to the AUTHORS file at the top-level directory of this distribution. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// 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/swap.rs b/src/core/macros.rs similarity index 56% rename from src/core/swap.rs rename to src/core/macros.rs index 91a8e57..166a94a 100644 --- a/src/core/swap.rs +++ b/src/core/macros.rs @@ -13,11 +13,35 @@ // See the License for the specific language governing permissions and // limitations under the License. -use core::{Vec2, Vec3, Vec4, Quat}; +#[macro_escape]; -pub trait Swap { - pub fn swap(&mut self, a: uint, b: uint); -} +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) } + } + } + ) +) macro_rules! impl_swap( ($Self:ident) => ( @@ -31,36 +55,3 @@ 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/mat.rs b/src/core/mat.rs index 5fd3a9a..f147da0 100644 --- a/src/core/mat.rs +++ b/src/core/mat.rs @@ -123,6 +123,7 @@ pub type Mat2f64 = Mat2; impl_mat!(Mat2, Vec2) impl_mat_swap!(Mat2, Vec2) +impl_dimensional!(Mat2, Vec2, 2) impl_approx!(Mat3 { x, y, z }) pub trait ToMat2 { @@ -534,6 +535,7 @@ pub type Mat3f64 = Mat3; impl_mat!(Mat3, Vec3) impl_mat_swap!(Mat3, Vec3) +impl_dimensional!(Mat3, Vec3, 3) impl_approx!(Mat2 { x, y }) pub trait ToMat3 { @@ -1121,6 +1123,7 @@ pub type Mat4f64 = Mat4; impl_mat!(Mat4, Vec4) impl_mat_swap!(Mat4, Vec4) +impl_dimensional!(Mat4, Vec4, 4) impl_approx!(Mat4 { x, y, z, w }) pub trait ToMat4 { diff --git a/src/core/quat.rs b/src/core/quat.rs index 231a6d0..c09a86c 100644 --- a/src/core/quat.rs +++ b/src/core/quat.rs @@ -13,7 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use core::Dimensional; +use core::{Dimensional, Swap}; use core::{Mat3, ToMat3}; use core::Vec3; @@ -32,6 +32,10 @@ pub type Quatf64 = Quat; #[deriving(Clone, Eq)] pub struct Quat { s: T, v: Vec3 } +impl_dimensional!(Quat, T, 4) +impl_swap!(Quat) +impl_approx!(Quat { s, v }) + pub trait ToQuat { pub fn to_quat(&self) -> Quat; } @@ -300,24 +304,6 @@ impl Quat { } } -impl> ApproxEq for Quat { - #[inline] - pub fn approx_epsilon() -> T { - ApproxEq::approx_epsilon::() - } - - #[inline] - pub fn approx_eq(&self, other: &Quat) -> bool { - self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) - } - - #[inline] - pub fn approx_eq_eps(&self, other: &Quat, epsilon: &T) -> bool { - self.s.approx_eq_eps(&other.s, epsilon) && - self.v.approx_eq_eps(&other.v, epsilon) - } -} - #[cfg(test)] mod tests { use core::mat::*; diff --git a/src/core/vec.rs b/src/core/vec.rs index 1b6b99c..9bad23b 100644 --- a/src/core/vec.rs +++ b/src/core/vec.rs @@ -16,7 +16,7 @@ #[cfg(geom)] use std::cast; -use core::Dimensional; +use core::{Dimensional, Swap}; #[cfg(geom)] use geom::{Point2, Point3}; @@ -55,6 +55,8 @@ pub trait AsVec2 { pub fn as_mut_vec2<'a>(&'a mut self) -> &'a mut Vec2; } +impl_dimensional!(Vec2, T, 2) +impl_swap!(Vec2) impl_approx!(Vec2 { x, y }) impl Vec2 { @@ -575,6 +577,8 @@ pub trait AsVec3 { pub fn as_mut_vec3<'a>(&'a mut self) -> &'a mut Vec3; } +impl_dimensional!(Vec3, T, 3) +impl_swap!(Vec3) impl_approx!(Vec3 { x, y, z }) impl Vec3 { @@ -1159,6 +1163,8 @@ pub trait AsVec4 { pub fn as_mut_vec4<'a>(&'a mut self) -> &'a mut Vec4; } +impl_dimensional!(Vec4, T, 4) +impl_swap!(Vec4) impl_approx!(Vec4 { x, y, z, w }) impl Vec4 { diff --git a/src/geom/frustum.rs b/src/geom/frustum.rs index 572ec6b..f13de2d 100644 --- a/src/geom/frustum.rs +++ b/src/geom/frustum.rs @@ -13,6 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +use core::Dimensional; use core::Mat4; use geom::{Plane3, Point3}; @@ -26,6 +27,7 @@ pub struct Frustum { far: Plane3, } +impl_dimensional!(Frustum, Plane3, 6) impl_approx!(Frustum { left, right, top, bottom, @@ -44,6 +46,7 @@ pub struct FrustumPoints { far_bottom_right: Point3, } +impl_dimensional!(FrustumPoints, Point3, 8) impl_approx!(FrustumPoints { near_top_left, near_top_right, @@ -81,17 +84,6 @@ impl Frustum { far: Plane3::from_vec4(mat.row(3).sub_v(&mat.row(2)).normalize()), } } - - pub fn base() -> Frustum { - Frustum { - left: Plane3::from_abcd( one!(T), zero!(T), zero!(T), one!(T)), - right: Plane3::from_abcd(-one!(T), zero!(T), zero!(T), one!(T)), - bottom: Plane3::from_abcd( zero!(T), one!(T), zero!(T), one!(T)), - top: Plane3::from_abcd( zero!(T), -one!(T), zero!(T), one!(T)), - near: Plane3::from_abcd( zero!(T), zero!(T), -one!(T), one!(T)), - far: Plane3::from_abcd( zero!(T), zero!(T), one!(T), one!(T)), - } - } } impl Frustum { diff --git a/src/geom/point.rs b/src/geom/point.rs index b599f4b..0bfa4f7 100644 --- a/src/geom/point.rs +++ b/src/geom/point.rs @@ -22,6 +22,7 @@ use std::cast; +use core::{Dimensional, Swap}; use core::{Mat2, Mat3, Quat}; use core::{Vec2, ToVec2, AsVec2}; use core::{Vec3, ToVec3, AsVec3}; @@ -47,6 +48,8 @@ pub trait Point: Eq #[deriving(Clone, Eq)] pub struct Point2 { x: T, y: T } +impl_dimensional!(Point2, T, 2) +impl_swap!(Point2) impl_approx!(Point2 { x, y }) impl Point2 { @@ -185,6 +188,8 @@ mod test_point2 { #[deriving(Clone, Eq)] pub struct Point3 { x: T, y: T, z: T } +impl_dimensional!(Point3, T, 3) +impl_swap!(Point3) impl_approx!(Point3 { x, y, z }) impl Point3 { diff --git a/src/lmath.rs b/src/lmath.rs index e97c4fa..58fd0d5 100644 --- a/src/lmath.rs +++ b/src/lmath.rs @@ -23,8 +23,13 @@ #[license = "ASL2"]; #[crate_type = "lib"]; +// Macros + mod macros; +#[path = "core/macros.rs"] +mod core_macros; + #[path = "core/core.rs"] pub mod core;