Move swap and dimensional impls into separate modules
This commit is contained in:
parent
b06785fc89
commit
6300d4b539
6 changed files with 110 additions and 48 deletions
|
@ -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;
|
||||
|
|
|
@ -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<T,Slice> {
|
||||
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<T> Dimensional<$T,[$T,..$n]> for $Self<T> {
|
||||
#[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<T>, 2)
|
||||
impl_dimensional!(Mat3, Vec3<T>, 3)
|
||||
impl_dimensional!(Mat4, Vec4<T>, 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)
|
||||
}
|
||||
|
|
|
@ -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<float>;
|
|||
pub type Mat2f32 = Mat2<f32>;
|
||||
pub type Mat2f64 = Mat2<f64>;
|
||||
|
||||
impl_dimensional!(Mat2, Vec2<T>, 2)
|
||||
impl_mat!(Mat2, Vec2)
|
||||
impl_mat_swap!(Mat2, Vec2)
|
||||
|
||||
|
@ -554,7 +552,6 @@ pub type Mat3f = Mat3<float>;
|
|||
pub type Mat3f32 = Mat3<f32>;
|
||||
pub type Mat3f64 = Mat3<f64>;
|
||||
|
||||
impl_dimensional!(Mat3, Vec3<T>, 3)
|
||||
impl_mat!(Mat3, Vec3)
|
||||
impl_mat_swap!(Mat3, Vec3)
|
||||
|
||||
|
@ -1160,7 +1157,6 @@ pub type Mat4f = Mat4<float>;
|
|||
pub type Mat4f32 = Mat4<f32>;
|
||||
pub type Mat4f64 = Mat4<f64>;
|
||||
|
||||
impl_dimensional!(Mat4, Vec4<T>, 4)
|
||||
impl_mat!(Mat4, Vec4)
|
||||
impl_mat_swap!(Mat4, Vec4)
|
||||
|
||||
|
|
|
@ -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<f64>;
|
|||
#[deriving(Clone, Eq)]
|
||||
pub struct Quat<T> { s: T, v: Vec3<T> }
|
||||
|
||||
impl_dimensional!(Quat, T, 4)
|
||||
impl_swap!(Quat)
|
||||
|
||||
pub trait ToQuat<T> {
|
||||
pub fn to_quat(&self) -> Quat<T>;
|
||||
}
|
||||
|
|
|
@ -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<T> Dimensional<$T,[$T,..$n]> for $Self<T> {
|
||||
#[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<T:Clone> $Self<T> {
|
||||
impl<T:Clone> Swap for $Self<T> {
|
||||
#[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)
|
||||
}
|
|
@ -22,7 +22,6 @@ use geom::{Point2, Point3};
|
|||
|
||||
#[path = "../num_macros.rs"]
|
||||
mod num_macros;
|
||||
mod dim_macros;
|
||||
|
||||
#[deriving(Clone, Eq)]
|
||||
pub struct Vec2<T> { x: T, y: T }
|
||||
|
@ -50,9 +49,6 @@ pub type Vec2u32 = Vec2<u32>;
|
|||
pub type Vec2u64 = Vec2<u64>;
|
||||
pub type Vec2b = Vec2<bool>;
|
||||
|
||||
impl_dimensional!(Vec2, T, 2)
|
||||
impl_swap!(Vec2)
|
||||
|
||||
impl<T> Vec2<T> {
|
||||
#[inline]
|
||||
pub fn new(x: T, y: T) -> Vec2<T> {
|
||||
|
@ -590,9 +586,6 @@ pub type Vec3u32 = Vec3<u32>;
|
|||
pub type Vec3u64 = Vec3<u64>;
|
||||
pub type Vec3b = Vec3<bool>;
|
||||
|
||||
impl_dimensional!(Vec3, T, 3)
|
||||
impl_swap!(Vec3)
|
||||
|
||||
impl<T> Vec3<T> {
|
||||
#[inline]
|
||||
pub fn new(x: T, y: T, z: T) -> Vec3<T> {
|
||||
|
@ -1195,9 +1188,6 @@ pub type Vec4u32 = Vec4<u32>;
|
|||
pub type Vec4u64 = Vec4<u64>;
|
||||
pub type Vec4b = Vec4<bool>;
|
||||
|
||||
impl_dimensional!(Vec4, T, 4)
|
||||
impl_swap!(Vec4)
|
||||
|
||||
impl<T> Vec4<T> {
|
||||
#[inline]
|
||||
pub fn new(x: T, y: T, z: T, w: T) -> Vec4<T> {
|
||||
|
|
Loading…
Reference in a new issue