More module re-arrangement

This commit is contained in:
Brendan Zabarauskas 2013-07-12 16:32:12 +10:00
parent 0cbfa7d040
commit 7615a55d84
13 changed files with 87 additions and 162 deletions

View file

@ -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<T> { h: T, s: T, v: T }
impl_dimensional!(HSV, T, 3)
impl_swap!(HSV)
impl_approx!(HSV { h, s, v })
impl<T:FloatChannel> HSV<T> {
@ -122,6 +125,8 @@ impl<T:Clone + FloatChannel> ToRGB for HSV<T> {
#[deriving(Clone, Eq)]
pub struct HSVA<T> { h: T, s: T, v: T, a: T }
impl_dimensional!(HSVA, T, 4)
impl_swap!(HSVA)
impl_approx!(HSVA { h, s, v, a })
impl<T:FloatChannel> HSVA<T> {

View file

@ -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<T> { r: T, g: T, b: T }
impl_dimensional!(RGB, T, 3)
impl_swap!(RGB)
impl_approx!(RGB { r, g, b })
impl<T:Channel> RGB<T> {
@ -119,6 +122,8 @@ impl<T:Clone + Channel> ToHSV for RGB<T> {
#[deriving(Clone, Eq)]
pub struct RGBA<T> { r: T, g: T, b: T, a: T }
impl_dimensional!(RGBA, T, 4)
impl_swap!(RGBA)
impl_approx!(RGBA { r, g, b, a })
impl<T:Channel> RGBA<T> {

View file

@ -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<T> { r: T, g: T, b: T }
@ -23,11 +25,15 @@ impl<T> SRGB<T> {
}
}
impl_dimensional!(SRGB, T, 3)
impl_swap!(SRGB)
impl_approx!(SRGB { r, g, b })
#[deriving(Clone, Eq)]
pub struct SRGBA<T> { r: T, g: T, b: T, a: T }
impl_dimensional!(SRGBA, T, 4)
impl_swap!(SRGBA)
impl_approx!(SRGBA { r, g, b, a })
impl<T> SRGBA<T> {

View file

@ -15,9 +15,13 @@
// http://en.wikipedia.org/wiki/YCbCr
use core::{Dimensional, Swap};
#[deriving(Clone, Eq)]
pub struct YCbCr<T> { y: T, cb: T, cr: T }
impl_dimensional!(YCbCr, T, 3)
impl_swap!(YCbCr)
impl_approx!(YCbCr { y, cb, cr })
impl<T> YCbCr<T> {

View file

@ -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<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;
}
pub trait Swap {
pub fn swap(&mut self, a: uint, b: uint);
}

View file

@ -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<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)
}

View file

@ -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<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) }
}
}
)
)
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)
}

View file

@ -123,6 +123,7 @@ pub type Mat2f64 = Mat2<f64>;
impl_mat!(Mat2, Vec2)
impl_mat_swap!(Mat2, Vec2)
impl_dimensional!(Mat2, Vec2<T>, 2)
impl_approx!(Mat3 { x, y, z })
pub trait ToMat2<T> {
@ -534,6 +535,7 @@ pub type Mat3f64 = Mat3<f64>;
impl_mat!(Mat3, Vec3)
impl_mat_swap!(Mat3, Vec3)
impl_dimensional!(Mat3, Vec3<T>, 3)
impl_approx!(Mat2 { x, y })
pub trait ToMat3<T> {
@ -1121,6 +1123,7 @@ pub type Mat4f64 = Mat4<f64>;
impl_mat!(Mat4, Vec4)
impl_mat_swap!(Mat4, Vec4)
impl_dimensional!(Mat4, Vec4<T>, 4)
impl_approx!(Mat4 { x, y, z, w })
pub trait ToMat4<T> {

View file

@ -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<f64>;
#[deriving(Clone, Eq)]
pub struct Quat<T> { s: T, v: Vec3<T> }
impl_dimensional!(Quat, T, 4)
impl_swap!(Quat)
impl_approx!(Quat { s, v })
pub trait ToQuat<T> {
pub fn to_quat(&self) -> Quat<T>;
}
@ -300,24 +304,6 @@ impl<T:Clone + Float> Quat<T> {
}
}
impl<T:Clone + Eq + ApproxEq<T>> ApproxEq<T> for Quat<T> {
#[inline]
pub fn approx_epsilon() -> T {
ApproxEq::approx_epsilon::<T,T>()
}
#[inline]
pub fn approx_eq(&self, other: &Quat<T>) -> bool {
self.approx_eq_eps(other, &ApproxEq::approx_epsilon::<T,T>())
}
#[inline]
pub fn approx_eq_eps(&self, other: &Quat<T>, 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::*;

View file

@ -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<T> {
pub fn as_mut_vec2<'a>(&'a mut self) -> &'a mut Vec2<T>;
}
impl_dimensional!(Vec2, T, 2)
impl_swap!(Vec2)
impl_approx!(Vec2 { x, y })
impl<T> Vec2<T> {
@ -575,6 +577,8 @@ pub trait AsVec3<T> {
pub fn as_mut_vec3<'a>(&'a mut self) -> &'a mut Vec3<T>;
}
impl_dimensional!(Vec3, T, 3)
impl_swap!(Vec3)
impl_approx!(Vec3 { x, y, z })
impl<T> Vec3<T> {
@ -1159,6 +1163,8 @@ pub trait AsVec4<T> {
pub fn as_mut_vec4<'a>(&'a mut self) -> &'a mut Vec4<T>;
}
impl_dimensional!(Vec4, T, 4)
impl_swap!(Vec4)
impl_approx!(Vec4 { x, y, z, w })
impl<T> Vec4<T> {

View file

@ -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<T> {
far: Plane3<T>,
}
impl_dimensional!(Frustum, Plane3<T>, 6)
impl_approx!(Frustum {
left, right,
top, bottom,
@ -44,6 +46,7 @@ pub struct FrustumPoints<T> {
far_bottom_right: Point3<T>,
}
impl_dimensional!(FrustumPoints, Point3<T>, 8)
impl_approx!(FrustumPoints {
near_top_left,
near_top_right,
@ -81,17 +84,6 @@ impl<T:Clone + Float> Frustum<T> {
far: Plane3::from_vec4(mat.row(3).sub_v(&mat.row(2)).normalize()),
}
}
pub fn base() -> Frustum<T> {
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<T:Clone + Float> Frustum<T> {

View file

@ -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<T, Vec, Ray>: Eq
#[deriving(Clone, Eq)]
pub struct Point2<T> { x: T, y: T }
impl_dimensional!(Point2, T, 2)
impl_swap!(Point2)
impl_approx!(Point2 { x, y })
impl<T:Num> Point2<T> {
@ -185,6 +188,8 @@ mod test_point2 {
#[deriving(Clone, Eq)]
pub struct Point3<T> { x: T, y: T, z: T }
impl_dimensional!(Point3, T, 3)
impl_swap!(Point3)
impl_approx!(Point3 { x, y, z })
impl<T:Num> Point3<T> {

View file

@ -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;