Move Dimensional trait and dimensional macros into separate files

This commit is contained in:
Brendan Zabarauskas 2013-06-16 15:34:59 +10:00
parent f20eca5cc5
commit a4c7332e2b
10 changed files with 252 additions and 207 deletions

21
src/dim.rs Normal file
View file

@ -0,0 +1,21 @@
// 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.
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;
}

222
src/dim_macros.rs Normal file
View file

@ -0,0 +1,222 @@
// 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.
#[macro_escape];
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_dimensional_fns(
($Self:ident, $T:ty, 2) => (
impl<T> $Self<T> {
#[inline]
pub fn from_slice<'a>(slice: [$T,..2]) -> $Self<T> {
use std::cast::transmute;
unsafe { transmute(slice) }
}
#[inline(always)]
pub fn map<U>(&self, f: &fn(&$T) -> U) -> [U,..2] {
[f(self.index(0)),
f(self.index(1))]
}
#[inline(always)]
pub fn map_mut(&mut self, f: &fn(&mut $T)) {
f(self.index_mut(0));
f(self.index_mut(1));
}
#[inline(always)]
pub fn zip<U, SU: Dimensional<U,[U,..2]>, V>(&self, other: &SU, f: &fn(&$T, &U) -> V) -> [V,..2] {
[f(self.index(0), other.index(0)),
f(self.index(1), other.index(1))]
}
#[inline(always)]
pub fn zip_mut<U, SU: Dimensional<U,[U,..2]>>(&mut self, other: &SU, f: &fn(&mut $T, &U)) {
f(self.index_mut(0), other.index(0));
f(self.index_mut(1), other.index(1));
}
#[inline(always)]
pub fn foldl<U>(&self, init: &U, f: &fn(&$T, &U) -> U) -> U {
f(self.index(0), &f(self.index(1), init))
}
#[inline(always)]
pub fn foldr<U>(&self, init: &U, f: &fn(&$T, &U) -> U) -> U {
f(self.index(1), &f(self.index(0), init))
}
}
);
($Self:ident, $T:ty, 3) => (
impl<T> $Self<T> {
#[inline]
pub fn from_slice<'a>(slice: [$T,..3]) -> $Self<T> {
use std::cast::transmute;
unsafe { transmute(slice) }
}
#[inline(always)]
pub fn map<U>(&self, f: &fn(&$T) -> U) -> [U,..3] {
[f(self.index(0)),
f(self.index(1)),
f(self.index(2))]
}
#[inline(always)]
pub fn map_mut(&mut self, f: &fn(&mut $T)) {
f(self.index_mut(0));
f(self.index_mut(1));
f(self.index_mut(2));
}
#[inline(always)]
pub fn zip<U, SU: Dimensional<U,[U,..3]>, V>(&self, other: &SU, f: &fn(&$T, &U) -> V) -> [V,..3] {
[f(self.index(0), other.index(0)),
f(self.index(1), other.index(1)),
f(self.index(2), other.index(2))]
}
#[inline(always)]
pub fn zip_mut<U, SU: Dimensional<U,[U,..3]>>(&mut self, other: &SU, f: &fn(&mut $T, &U)) {
f(self.index_mut(0), other.index(0));
f(self.index_mut(1), other.index(1));
f(self.index_mut(2), other.index(2));
}
#[inline(always)]
pub fn foldl<U>(&self, init: &U, f: &fn(&$T, &U) -> U) -> U {
f(self.index(0), &f(self.index(1), &f(self.index(2), init)))
}
#[inline(always)]
pub fn foldr<U>(&self, init: &U, f: &fn(&$T, &U) -> U) -> U {
f(self.index(2), &f(self.index(1), &f(self.index(0), init)))
}
}
);
($Self:ident, $T:ty, 4) => (
impl<T> $Self<T> {
#[inline]
pub fn from_slice<'a>(slice: [$T,..4]) -> $Self<T> {
use std::cast::transmute;
unsafe { transmute(slice) }
}
#[inline(always)]
pub fn map<U>(&self, f: &fn(&$T) -> U) -> [U,..4] {
[f(self.index(0)),
f(self.index(1)),
f(self.index(2)),
f(self.index(3))]
}
#[inline(always)]
pub fn map_mut(&mut self, f: &fn(&mut $T)) {
f(self.index_mut(0));
f(self.index_mut(1));
f(self.index_mut(2));
f(self.index_mut(3));
}
#[inline(always)]
pub fn zip<U, SU: Dimensional<U,[U,..4]>, V>(&self, other: &SU, f: &fn(&$T, &U) -> V) -> [V,..4] {
[f(self.index(0), other.index(0)),
f(self.index(1), other.index(1)),
f(self.index(2), other.index(2)),
f(self.index(3), other.index(3))]
}
#[inline(always)]
pub fn zip_mut<U, SU: Dimensional<U,[U,..4]>>(&mut self, other: &SU, f: &fn(&mut $T, &U)) {
f(self.index_mut(0), other.index(0));
f(self.index_mut(1), other.index(1));
f(self.index_mut(2), other.index(2));
f(self.index_mut(3), other.index(3));
}
#[inline(always)]
pub fn foldl<U>(&self, init: &U, f: &fn(&$T, &U) -> U) -> U {
f(self.index(0), &f(self.index(1), &f(self.index(2), &f(self.index(3), init))))
}
#[inline(always)]
pub fn foldr<U>(&self, init: &U, f: &fn(&$T, &U) -> U) -> U {
f(self.index(3), &f(self.index(2), &f(self.index(1), &f(self.index(0), init))))
}
}
)
)
macro_rules! impl_swap(
($Self:ident) => (
impl<T:Copy> $Self<T> {
#[inline]
pub fn swap(&mut self, a: uint, b: uint) {
let tmp = *self.index(a);
*self.index_mut(a) = *self.index(b);
*self.index_mut(b) = tmp;
}
}
)
)
macro_rules! impl_approx(
($Self:ident) => (
impl<T:Copy + Eq + ApproxEq<T>> ApproxEq<T> for $Self<T> {
#[inline]
pub fn approx_epsilon() -> T {
ApproxEq::approx_epsilon::<T,T>()
}
#[inline]
pub fn approx_eq(&self, other: &$Self<T>) -> bool {
self.approx_eq_eps(other, &ApproxEq::approx_epsilon::<T,T>())
}
#[inline]
pub fn approx_eq_eps(&self, other: &$Self<T>, epsilon: &T) -> bool {
self.zip(other, |a, b| a.approx_eq_eps(b, epsilon)).all(|&x| x)
}
}
)
)

View file

@ -32,209 +32,3 @@ macro_rules! one(
macro_rules! two( macro_rules! two(
($T:ty) => (one!(T) + one!(T)); ($T:ty) => (one!(T) + one!(T));
) )
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_dimensional_fns(
($Self:ident, $T:ty, 2) => (
impl<T> $Self<T> {
#[inline]
pub fn from_slice<'a>(slice: [$T,..2]) -> $Self<T> {
use std::cast::transmute;
unsafe { transmute(slice) }
}
#[inline(always)]
pub fn map<U>(&self, f: &fn(&$T) -> U) -> [U,..2] {
[f(self.index(0)),
f(self.index(1))]
}
#[inline(always)]
pub fn map_mut(&mut self, f: &fn(&mut $T)) {
f(self.index_mut(0));
f(self.index_mut(1));
}
#[inline(always)]
pub fn zip<U, SU: Dimensional<U,[U,..2]>, V>(&self, other: &SU, f: &fn(&$T, &U) -> V) -> [V,..2] {
[f(self.index(0), other.index(0)),
f(self.index(1), other.index(1))]
}
#[inline(always)]
pub fn zip_mut<U, SU: Dimensional<U,[U,..2]>>(&mut self, other: &SU, f: &fn(&mut $T, &U)) {
f(self.index_mut(0), other.index(0));
f(self.index_mut(1), other.index(1));
}
#[inline(always)]
pub fn foldl<U>(&self, init: &U, f: &fn(&$T, &U) -> U) -> U {
f(self.index(0), &f(self.index(1), init))
}
#[inline(always)]
pub fn foldr<U>(&self, init: &U, f: &fn(&$T, &U) -> U) -> U {
f(self.index(1), &f(self.index(0), init))
}
}
);
($Self:ident, $T:ty, 3) => (
impl<T> $Self<T> {
#[inline]
pub fn from_slice<'a>(slice: [$T,..3]) -> $Self<T> {
use std::cast::transmute;
unsafe { transmute(slice) }
}
#[inline(always)]
pub fn map<U>(&self, f: &fn(&$T) -> U) -> [U,..3] {
[f(self.index(0)),
f(self.index(1)),
f(self.index(2))]
}
#[inline(always)]
pub fn map_mut(&mut self, f: &fn(&mut $T)) {
f(self.index_mut(0));
f(self.index_mut(1));
f(self.index_mut(2));
}
#[inline(always)]
pub fn zip<U, SU: Dimensional<U,[U,..3]>, V>(&self, other: &SU, f: &fn(&$T, &U) -> V) -> [V,..3] {
[f(self.index(0), other.index(0)),
f(self.index(1), other.index(1)),
f(self.index(2), other.index(2))]
}
#[inline(always)]
pub fn zip_mut<U, SU: Dimensional<U,[U,..3]>>(&mut self, other: &SU, f: &fn(&mut $T, &U)) {
f(self.index_mut(0), other.index(0));
f(self.index_mut(1), other.index(1));
f(self.index_mut(2), other.index(2));
}
#[inline(always)]
pub fn foldl<U>(&self, init: &U, f: &fn(&$T, &U) -> U) -> U {
f(self.index(0), &f(self.index(1), &f(self.index(2), init)))
}
#[inline(always)]
pub fn foldr<U>(&self, init: &U, f: &fn(&$T, &U) -> U) -> U {
f(self.index(2), &f(self.index(1), &f(self.index(0), init)))
}
}
);
($Self:ident, $T:ty, 4) => (
impl<T> $Self<T> {
#[inline]
pub fn from_slice<'a>(slice: [$T,..4]) -> $Self<T> {
use std::cast::transmute;
unsafe { transmute(slice) }
}
#[inline(always)]
pub fn map<U>(&self, f: &fn(&$T) -> U) -> [U,..4] {
[f(self.index(0)),
f(self.index(1)),
f(self.index(2)),
f(self.index(3))]
}
#[inline(always)]
pub fn map_mut(&mut self, f: &fn(&mut $T)) {
f(self.index_mut(0));
f(self.index_mut(1));
f(self.index_mut(2));
f(self.index_mut(3));
}
#[inline(always)]
pub fn zip<U, SU: Dimensional<U,[U,..4]>, V>(&self, other: &SU, f: &fn(&$T, &U) -> V) -> [V,..4] {
[f(self.index(0), other.index(0)),
f(self.index(1), other.index(1)),
f(self.index(2), other.index(2)),
f(self.index(3), other.index(3))]
}
#[inline(always)]
pub fn zip_mut<U, SU: Dimensional<U,[U,..4]>>(&mut self, other: &SU, f: &fn(&mut $T, &U)) {
f(self.index_mut(0), other.index(0));
f(self.index_mut(1), other.index(1));
f(self.index_mut(2), other.index(2));
f(self.index_mut(3), other.index(3));
}
#[inline(always)]
pub fn foldl<U>(&self, init: &U, f: &fn(&$T, &U) -> U) -> U {
f(self.index(0), &f(self.index(1), &f(self.index(2), &f(self.index(3), init))))
}
#[inline(always)]
pub fn foldr<U>(&self, init: &U, f: &fn(&$T, &U) -> U) -> U {
f(self.index(3), &f(self.index(2), &f(self.index(1), &f(self.index(0), init))))
}
}
)
)
macro_rules! impl_swap(
($Self:ident) => (
impl<T:Copy> $Self<T> {
#[inline]
pub fn swap(&mut self, a: uint, b: uint) {
let tmp = *self.index(a);
*self.index_mut(a) = *self.index(b);
*self.index_mut(b) = tmp;
}
}
)
)
macro_rules! impl_approx(
($Self:ident) => (
impl<T:Copy + Eq + ApproxEq<T>> ApproxEq<T> for $Self<T> {
#[inline]
pub fn approx_epsilon() -> T {
ApproxEq::approx_epsilon::<T,T>()
}
#[inline]
pub fn approx_eq(&self, other: &$Self<T>) -> bool {
self.approx_eq_eps(other, &ApproxEq::approx_epsilon::<T,T>())
}
#[inline]
pub fn approx_eq_eps(&self, other: &$Self<T>, epsilon: &T) -> bool {
self.zip(other, |a, b| a.approx_eq_eps(b, epsilon)).all(|&x| x)
}
}
)
)

View file

@ -20,6 +20,7 @@ use mat::{Mat4, ToMat4};
use vec::Vec2; use vec::Vec2;
mod macros; mod macros;
mod dim_macros;
mod mat_macros; mod mat_macros;
#[deriving(Eq)] #[deriving(Eq)]

View file

@ -20,6 +20,7 @@ use quat::{Quat, ToQuat};
use vec::Vec3; use vec::Vec3;
mod macros; mod macros;
mod dim_macros;
mod mat_macros; mod mat_macros;
#[deriving(Eq)] #[deriving(Eq)]

View file

@ -19,6 +19,7 @@ use mat::Mat3;
use vec::Vec4; use vec::Vec4;
mod macros; mod macros;
mod dim_macros;
mod mat_macros; mod mat_macros;
#[deriving(Eq)] #[deriving(Eq)]

View file

@ -15,11 +15,11 @@
pub use super::Dimensional; pub use super::Dimensional;
use std::num::cast;
use mat::{Mat3, ToMat3}; use mat::{Mat3, ToMat3};
use vec::Vec3; use vec::Vec3;
mod macros; mod macros;
mod dim_macros;
// GLSL-style type aliases // GLSL-style type aliases
@ -285,6 +285,8 @@ impl<T:Copy + Float> Quat<T> {
/// - [Arcsynthesis OpenGL tutorial] /// - [Arcsynthesis OpenGL tutorial]
/// (http://www.arcsynthesis.org/gltut/Positioning/Tut08%20Interpolation.html) /// (http://www.arcsynthesis.org/gltut/Positioning/Tut08%20Interpolation.html)
pub fn slerp(&self, other: &Quat<T>, amount: T) -> Quat<T> { pub fn slerp(&self, other: &Quat<T>, amount: T) -> Quat<T> {
use std::num::cast;
let dot = self.dot(other); let dot = self.dot(other);
let dot_threshold = cast(0.9995); let dot_threshold = cast(0.9995);

View file

@ -16,6 +16,7 @@
pub use super::Dimensional; pub use super::Dimensional;
mod macros; mod macros;
mod dim_macros;
mod vec_macros; mod vec_macros;
#[deriving(Eq)] #[deriving(Eq)]

View file

@ -16,6 +16,7 @@
pub use super::Dimensional; pub use super::Dimensional;
mod macros; mod macros;
mod dim_macros;
mod vec_macros; mod vec_macros;
#[deriving(Eq)] #[deriving(Eq)]

View file

@ -16,6 +16,7 @@
pub use super::Dimensional; pub use super::Dimensional;
mod macros; mod macros;
mod dim_macros;
mod vec_macros; mod vec_macros;
#[deriving(Eq)] #[deriving(Eq)]