From f20eca5cc5f260661d8c03d74a92a07f5aa80cb8 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sun, 16 Jun 2013 15:21:32 +1000 Subject: [PATCH] Use macros and higher-order functions to reduce code repetition --- src/lmath.rs | 2 - src/macros.rs | 240 ++++++++++++++++++++++ src/mat2.rs | 376 +++------------------------------ src/mat3.rs | 443 +++------------------------------------ src/mat4.rs | 513 ++-------------------------------------------- src/mat_macros.rs | 508 +++++++++++++++++++++++++++++++++++++++++++++ src/projection.rs | 63 +++--- src/quat.rs | 149 +++----------- src/vec2.rs | 408 ++---------------------------------- src/vec3.rs | 440 ++------------------------------------- src/vec4.rs | 465 ++--------------------------------------- src/vec_macros.rs | 245 ++++++++++++++++++++++ 12 files changed, 1182 insertions(+), 2670 deletions(-) create mode 100644 src/macros.rs create mode 100644 src/mat_macros.rs create mode 100644 src/vec_macros.rs diff --git a/src/lmath.rs b/src/lmath.rs index e653b6a..c44e0c9 100644 --- a/src/lmath.rs +++ b/src/lmath.rs @@ -34,6 +34,4 @@ pub trait Dimensional { 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 fn map(&self, f: &fn(&T) -> T) -> Self; - pub fn map_mut(&mut self, f: &fn(&mut T)); } diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..88e3d48 --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,240 @@ +// 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! zero( + ($T:ty) => ({ + use std::num::Zero; + Zero::zero::<$T>() + }); +) + +macro_rules! one( + ($T:ty) => ({ + use std::num::One; + One::one::<$T>() + }); +) + +macro_rules! two( + ($T:ty) => (one!(T) + one!(T)); +) + +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_dimensional_fns( + ($Self:ident, $T:ty, 2) => ( + impl $Self { + #[inline] + pub fn from_slice<'a>(slice: [$T,..2]) -> $Self { + use std::cast::transmute; + unsafe { transmute(slice) } + } + + #[inline(always)] + pub fn map(&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, 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>(&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(&self, init: &U, f: &fn(&$T, &U) -> U) -> U { + f(self.index(0), &f(self.index(1), init)) + } + + #[inline(always)] + pub fn foldr(&self, init: &U, f: &fn(&$T, &U) -> U) -> U { + f(self.index(1), &f(self.index(0), init)) + } + } + ); + ($Self:ident, $T:ty, 3) => ( + impl $Self { + #[inline] + pub fn from_slice<'a>(slice: [$T,..3]) -> $Self { + use std::cast::transmute; + unsafe { transmute(slice) } + } + + #[inline(always)] + pub fn map(&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, 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>(&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(&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(&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 $Self { + #[inline] + pub fn from_slice<'a>(slice: [$T,..4]) -> $Self { + use std::cast::transmute; + unsafe { transmute(slice) } + } + + #[inline(always)] + pub fn map(&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, 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>(&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(&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(&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 $Self { + #[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> ApproxEq for $Self { + #[inline] + pub fn approx_epsilon() -> T { + ApproxEq::approx_epsilon::() + } + + #[inline] + pub fn approx_eq(&self, other: &$Self) -> bool { + self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) + } + + #[inline] + pub fn approx_eq_eps(&self, other: &$Self, epsilon: &T) -> bool { + self.zip(other, |a, b| a.approx_eq_eps(b, epsilon)).all(|&x| x) + } + } + ) +) diff --git a/src/mat2.rs b/src/mat2.rs index 1727c80..f9d1909 100644 --- a/src/mat2.rs +++ b/src/mat2.rs @@ -13,14 +13,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::cast::transmute; -use std::cmp::ApproxEq; -use std::num::{Zero, One}; +pub use super::Dimensional; -use super::Dimensional; +use mat::{Mat3, ToMat3}; +use mat::{Mat4, ToMat4}; use vec::Vec2; -use super::{Mat3, ToMat3}; -use super::{Mat4, ToMat4}; + +mod macros; +mod mat_macros; #[deriving(Eq)] pub struct Mat2 { @@ -28,26 +28,21 @@ pub struct Mat2 { y: Vec2, } +impl_dimensional!(Mat2, Vec2, 2) +impl_dimensional_fns!(Mat2, Vec2, 2) +impl_approx!(Mat2) + +impl_mat!(Mat2, Vec2) +impl_mat_copyable!(Mat2, Vec2) +impl_mat_numeric!(Mat2, Vec2) +impl_mat_approx_numeric!(Mat2) +impl_mat_neg!(Mat2) + pub trait ToMat2 { pub fn to_mat2(&self) -> Mat2; } impl Mat2 { - /// Construct a 2 x 2 matrix - /// - /// # Arguments - /// - /// - `c0r0`, `c0r1`: the first column of the matrix - /// - `c1r0`, `c1r1`: the second column of the matrix - /// - /// ~~~ - /// c0 c1 - /// +------+------+ - /// r0 | c0r0 | c1r0 | - /// +------+------+ - /// r1 | c0r1 | c1r1 | - /// +------+------+ - /// ~~~ #[inline] pub fn new(c0r0: T, c0r1: T, c1r0: T, c1r1: T) -> Mat2 { @@ -55,287 +50,29 @@ impl Mat2 { Vec2::new(c1r0, c1r1)) } - /// Construct a 2 x 2 matrix from column vectors - /// - /// # Arguments - /// - /// - `c0`: the first column vector of the matrix - /// - `c1`: the second column vector of the matrix - /// - /// ~~~ - /// c0 c1 - /// +------+------+ - /// r0 | c0.x | c1.x | - /// +------+------+ - /// r1 | c0.y | c1.y | - /// +------+------+ - /// ~~~ #[inline] pub fn from_cols(c0: Vec2, c1: Vec2) -> Mat2 { Mat2 { x: c0, y: c1 } } - - #[inline] - pub fn col<'a>(&'a self, i: uint) -> &'a Vec2 { - self.index(i) - } - - #[inline] - pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec2 { - self.index_mut(i) - } - - #[inline] - pub fn elem<'a>(&'a self, i: uint, j: uint) -> &'a T { - self.index(i).index(j) - } - - #[inline] - pub fn elem_mut<'a>(&'a mut self, i: uint, j: uint) -> &'a mut T { - self.index_mut(i).index_mut(j) - } -} - -impl Dimensional,[Vec2,..2]> for Mat2 { - #[inline] - pub fn index<'a>(&'a self, i: uint) -> &'a Vec2 { - &'a self.as_slice()[i] - } - - #[inline] - pub fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec2 { - &'a mut self.as_mut_slice()[i] - } - - #[inline] - pub fn as_slice<'a>(&'a self) -> &'a [Vec2,..2] { - unsafe { transmute(self) } - } - - #[inline] - pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec2,..2] { - unsafe { transmute(self) } - } - - #[inline(always)] - pub fn map(&self, f: &fn(&Vec2) -> Vec2) -> Mat2 { - Mat2::from_cols(f(self.index(0)), - f(self.index(1))) - } - - #[inline(always)] - pub fn map_mut(&mut self, f: &fn(&mut Vec2)) { - f(self.index_mut(0)); - f(self.index_mut(1)); - } -} - -impl Mat2 { - #[inline] - pub fn row(&self, i: uint) -> Vec2 { - Vec2::new(*self.elem(0, i), - *self.elem(1, i)) - } - - #[inline] - pub fn swap_cols(&mut self, a: uint, b: uint) { - let tmp = *self.col(a); - *self.col_mut(a) = *self.col(b); - *self.col_mut(b) = tmp; - } - - #[inline] - pub fn swap_rows(&mut self, a: uint, b: uint) { - self.x.swap(a, b); - self.y.swap(a, b); - } - - #[inline] - pub fn transpose(&self) -> Mat2 { - Mat2::new(*self.elem(0, 0), *self.elem(1, 0), - *self.elem(0, 1), *self.elem(1, 1)) - } - - #[inline] - pub fn transpose_self(&mut self) { - let tmp01 = *self.elem(0, 1); - let tmp10 = *self.elem(1, 0); - - *self.elem_mut(0, 1) = *self.elem(1, 0); - *self.elem_mut(1, 0) = *self.elem(0, 1); - - *self.elem_mut(1, 0) = tmp01; - *self.elem_mut(0, 1) = tmp10; - } -} - -impl Mat2 { - /// Construct a 2 x 2 diagonal matrix with the major diagonal set to `value`. - /// ~~~ - /// c0 c1 - /// +-----+-----+ - /// r0 | val | 0 | - /// +-----+-----+ - /// r1 | 0 | val | - /// +-----+-----+ - /// ~~~ - #[inline] - pub fn from_value(value: T) -> Mat2 { - Mat2::new(value, Zero::zero(), - Zero::zero(), value) - } - - /// Returns the multiplicative identity matrix - /// ~~~ - /// c0 c1 - /// +----+----+ - /// r0 | 1 | 0 | - /// +----+----+ - /// r1 | 0 | 1 | - /// +----+----+ - /// ~~~ - #[inline] - pub fn identity() -> Mat2 { - Mat2::new(One::one::(), Zero::zero::(), - Zero::zero::(), One::one::()) - } - - /// Returns the additive identity matrix - /// ~~~ - /// c0 c1 - /// +----+----+ - /// r0 | 0 | 0 | - /// +----+----+ - /// r1 | 0 | 0 | - /// +----+----+ - /// ~~~ - #[inline] - pub fn zero() -> Mat2 { - Mat2::new(Zero::zero::(), Zero::zero::(), - Zero::zero::(), Zero::zero::()) - } - - #[inline] - pub fn mul_t(&self, value: T) -> Mat2 { - self.map(|&c| c.mul_t(value)) - } - - #[inline] - pub fn mul_v(&self, vec: &Vec2) -> Vec2 { - Vec2::new(self.row(0).dot(vec), - self.row(1).dot(vec)) - } - - #[inline] - pub fn add_m(&self, other: &Mat2) -> Mat2 { - Mat2::from_cols(self.col(0).add_v(other.col(0)), - self.col(1).add_v(other.col(1))) - } - - #[inline] - pub fn sub_m(&self, other: &Mat2) -> Mat2 { - Mat2::from_cols(self.col(0).sub_v(other.col(0)), - self.col(1).sub_v(other.col(1))) - } - - #[inline] - pub fn mul_m(&self, other: &Mat2) -> Mat2 { - Mat2::new(self.row(0).dot(other.col(0)), self.row(1).dot(other.col(0)), - self.row(0).dot(other.col(1)), self.row(1).dot(other.col(1))) - } - - #[inline] - pub fn mul_self_t(&mut self, value: T) { - self.map_mut(|x| x.mul_self_t(value)) - } - - #[inline] - pub fn add_self_m(&mut self, other: &Mat2) { - self.x.add_self_v(other.col(0)); - self.y.add_self_v(other.col(1)); - } - - #[inline] - pub fn sub_self_m(&mut self, other: &Mat2) { - self.x.sub_self_v(other.col(0)); - self.y.sub_self_v(other.col(1)); - } - - pub fn dot(&self, other: &Mat2) -> T { - other.transpose().mul_m(self).trace() - } - - pub fn determinant(&self) -> T { - *self.col(0).index(0) * - *self.col(1).index(1) - - *self.col(1).index(0) * - *self.col(0).index(1) - } - - pub fn trace(&self) -> T { - *self.col(0).index(0) + - *self.col(1).index(1) - } - - #[inline] - pub fn to_identity(&mut self) { - *self = Mat2::identity(); - } - - #[inline] - pub fn to_zero(&mut self) { - *self = Mat2::zero(); - } } impl ToMat3 for Mat2 { - /// Returns the the matrix with an extra row and column added - /// ~~~ - /// c0 c1 c0 c1 c2 - /// +----+----+ +----+----+----+ - /// r0 | a | b | r0 | a | b | 0 | - /// +----+----+ +----+----+----+ - /// r1 | c | d | => r1 | c | d | 0 | - /// +----+----+ +----+----+----+ - /// r2 | 0 | 0 | 1 | - /// +----+----+----+ - /// ~~~ #[inline] pub fn to_mat3(&self) -> Mat3 { - Mat3::new(*self.elem(0, 0), *self.elem(0, 1), Zero::zero(), - *self.elem(1, 0), *self.elem(1, 1), Zero::zero(), - Zero::zero(), Zero::zero(), One::one()) + Mat3::new(*self.elem(0, 0), *self.elem(0, 1), zero!(T), + *self.elem(1, 0), *self.elem(1, 1), zero!(T), + zero!(T), zero!(T), one!(T)) } } impl ToMat4 for Mat2 { - /// Returns the the matrix with an extra two rows and columns added - /// ~~~ - /// c0 c1 c0 c1 c2 c3 - /// +----+----+ +----+----+----+----+ - /// r0 | a | b | r0 | a | b | 0 | 0 | - /// +----+----+ +----+----+----+----+ - /// r1 | c | d | => r1 | c | d | 0 | 0 | - /// +----+----+ +----+----+----+----+ - /// r2 | 0 | 0 | 1 | 0 | - /// +----+----+----+----+ - /// r3 | 0 | 0 | 0 | 1 | - /// +----+----+----+----+ - /// ~~~ #[inline] pub fn to_mat4(&self) -> Mat4 { - Mat4::new(*self.elem(0, 0), *self.elem(0, 1), Zero::zero(), Zero::zero(), - *self.elem(1, 0), *self.elem(1, 1), Zero::zero(), Zero::zero(), - Zero::zero(), Zero::zero(), One::one(), Zero::zero(), - Zero::zero(), Zero::zero(), Zero::zero(), One::one()) - } -} - -impl Neg> for Mat2 { - #[inline] - pub fn neg(&self) -> Mat2 { - self.map(|&x| -x) + Mat4::new(*self.elem(0, 0), *self.elem(0, 1), zero!(T), zero!(T), + *self.elem(1, 0), *self.elem(1, 1), zero!(T), zero!(T), + zero!(T), zero!(T), one!(T), zero!(T), + zero!(T), zero!(T), zero!(T), one!(T)) } } @@ -350,69 +87,6 @@ impl Mat2 { } } -impl> Mat2 { - #[inline] - pub fn inverse(&self) -> Option> { - let d = self.determinant(); - if d.approx_eq(&Zero::zero()) { - None - } else { - Some(Mat2::new(self.elem(1, 1) / d, -self.elem(0, 1) / d, - -self.elem(1, 0) / d, self.elem(0, 0) / d)) - } - } - - #[inline] - pub fn invert_self(&mut self) { - *self = self.inverse().expect("Couldn't invert the matrix!"); - } - - #[inline] - pub fn is_identity(&self) -> bool { - self.approx_eq(&Mat2::identity()) - } - - #[inline] - pub fn is_diagonal(&self) -> bool { - self.elem(0, 1).approx_eq(&Zero::zero()) && - self.elem(1, 0).approx_eq(&Zero::zero()) - } - - #[inline] - pub fn is_rotated(&self) -> bool { - !self.approx_eq(&Mat2::identity()) - } - - #[inline] - pub fn is_symmetric(&self) -> bool { - self.elem(0, 1).approx_eq(self.elem(1, 0)) && - self.elem(1, 0).approx_eq(self.elem(0, 1)) - } - - #[inline] - pub fn is_invertible(&self) -> bool { - !self.determinant().approx_eq(&Zero::zero()) - } -} - -impl> ApproxEq for Mat2 { - #[inline] - pub fn approx_epsilon() -> T { - ApproxEq::approx_epsilon::() - } - - #[inline] - pub fn approx_eq(&self, other: &Mat2) -> bool { - self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) - } - - #[inline] - pub fn approx_eq_eps(&self, other: &Mat2, epsilon: &T) -> bool { - self.col(0).approx_eq_eps(other.col(0), epsilon) && - self.col(1).approx_eq_eps(other.col(1), epsilon) - } -} - #[cfg(test)] mod tests{ use mat::*; @@ -576,8 +250,8 @@ mod tests{ #[test] fn test_mat2_approx_eq() { assert!(!Mat2::new::(0.000001, 0.000001, - 0.000001, 0.000001).approx_eq(&Mat2::zero::())); + 0.000001, 0.000001).approx_eq(&Mat2::zero::())); assert!(Mat2::new::(0.0000001, 0.0000001, - 0.0000001, 0.0000001).approx_eq(&Mat2::zero::())); + 0.0000001, 0.0000001).approx_eq(&Mat2::zero::())); } } diff --git a/src/mat3.rs b/src/mat3.rs index 02e4492..04fb91a 100644 --- a/src/mat3.rs +++ b/src/mat3.rs @@ -13,14 +13,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::cast::transmute; -use std::cmp::ApproxEq; -use std::num::{Zero, One}; +pub use super::Dimensional; -use super::Dimensional; -use vec::Vec3; +use mat::{Mat4, ToMat4}; use quat::{Quat, ToQuat}; -use super::{Mat4, ToMat4}; +use vec::Vec3; + +mod macros; +mod mat_macros; #[deriving(Eq)] pub struct Mat3 { @@ -29,29 +29,21 @@ pub struct Mat3 { z: Vec3, } +impl_dimensional!(Mat3, Vec3, 3) +impl_dimensional_fns!(Mat3, Vec3, 3) +impl_approx!(Mat3) + +impl_mat!(Mat3, Vec3) +impl_mat_copyable!(Mat3, Vec3) +impl_mat_numeric!(Mat3, Vec3) +impl_mat_approx_numeric!(Mat3) +impl_mat_neg!(Mat3) + pub trait ToMat3 { pub fn to_mat3(&self) -> Mat3; } impl Mat3 { - /// Construct a 3 x 3 matrix - /// - /// # Arguments - /// - /// - `c0r0`, `c0r1`, `c0r2`: the first column of the matrix - /// - `c1r0`, `c1r1`, `c1r2`: the second column of the matrix - /// - `c2r0`, `c2r1`, `c2r2`: the third column of the matrix - /// - /// ~~~ - /// c0 c1 c2 - /// +------+------+------+ - /// r0 | c0r0 | c1r0 | c2r0 | - /// +------+------+------+ - /// r1 | c0r1 | c1r1 | c2r1 | - /// +------+------+------+ - /// r2 | c0r2 | c1r2 | c2r2 | - /// +------+------+------+ - /// ~~~ #[inline] pub fn new(c0r0:T, c0r1:T, c0r2:T, c1r0:T, c1r1:T, c1r2:T, @@ -61,314 +53,21 @@ impl Mat3 { Vec3::new(c2r0, c2r1, c2r2)) } - /// Construct a 3 x 3 matrix from column vectors - /// - /// # Arguments - /// - /// - `c0`: the first column vector of the matrix - /// - `c1`: the second column vector of the matrix - /// - `c2`: the third column vector of the matrix - /// - /// ~~~ - /// c0 c1 c2 - /// +------+------+------+ - /// r0 | c0.x | c1.x | c2.x | - /// +------+------+------+ - /// r1 | c0.y | c1.y | c2.y | - /// +------+------+------+ - /// r2 | c0.z | c1.z | c2.z | - /// +------+------+------+ - /// ~~~ #[inline] pub fn from_cols(c0: Vec3, c1: Vec3, c2: Vec3) -> Mat3 { Mat3 { x: c0, y: c1, z: c2 } } - - #[inline] - pub fn col<'a>(&'a self, i: uint) -> &'a Vec3 { - self.index(i) - } - - #[inline] - pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec3 { - self.index_mut(i) - } - - #[inline] - pub fn elem<'a>(&'a self, i: uint, j: uint) -> &'a T { - self.index(i).index(j) - } - - #[inline] - pub fn elem_mut<'a>(&'a mut self, i: uint, j: uint) -> &'a mut T { - self.index_mut(i).index_mut(j) - } -} - -impl Dimensional,[Vec3,..3]> for Mat3 { - #[inline] - pub fn index<'a>(&'a self, i: uint) -> &'a Vec3 { - &'a self.as_slice()[i] - } - - #[inline] - pub fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec3 { - &'a mut self.as_mut_slice()[i] - } - - #[inline] - pub fn as_slice<'a>(&'a self) -> &'a [Vec3,..3] { - unsafe { transmute(self) } - } - - #[inline] - pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec3,..3] { - unsafe { transmute(self) } - } - - #[inline(always)] - pub fn map(&self, f: &fn(&Vec3) -> Vec3) -> Mat3 { - Mat3::from_cols(f(self.index(0)), - f(self.index(1)), - f(self.index(2))) - } - - #[inline(always)] - pub fn map_mut(&mut self, f: &fn(&mut Vec3)) { - f(self.index_mut(0)); - f(self.index_mut(1)); - f(self.index_mut(2)); - } -} - -impl Mat3 { - #[inline] - pub fn row(&self, i: uint) -> Vec3 { - Vec3::new(*self.elem(0, i), - *self.elem(1, i), - *self.elem(2, i)) - } - - #[inline] - pub fn swap_cols(&mut self, a: uint, b: uint) { - let tmp = *self.col(a); - *self.col_mut(a) = *self.col(b); - *self.col_mut(b) = tmp; - } - - #[inline] - pub fn swap_rows(&mut self, a: uint, b: uint) { - self.x.swap(a, b); - self.y.swap(a, b); - self.z.swap(a, b); - } - - #[inline] - pub fn transpose(&self) -> Mat3 { - Mat3::new(*self.elem(0, 0), *self.elem(1, 0), *self.elem(2, 0), - *self.elem(0, 1), *self.elem(1, 1), *self.elem(2, 1), - *self.elem(0, 2), *self.elem(1, 2), *self.elem(2, 2)) - } - - #[inline] - pub fn transpose_self(&mut self) { - let tmp01 = *self.elem(0, 1); - let tmp02 = *self.elem(0, 2); - let tmp10 = *self.elem(1, 0); - let tmp12 = *self.elem(1, 2); - let tmp20 = *self.elem(2, 0); - let tmp21 = *self.elem(2, 1); - - *self.elem_mut(0, 1) = *self.elem(1, 0); - *self.elem_mut(0, 2) = *self.elem(2, 0); - *self.elem_mut(1, 0) = *self.elem(0, 1); - *self.elem_mut(1, 2) = *self.elem(2, 1); - *self.elem_mut(2, 0) = *self.elem(0, 2); - *self.elem_mut(2, 1) = *self.elem(1, 2); - - *self.elem_mut(1, 0) = tmp01; - *self.elem_mut(2, 0) = tmp02; - *self.elem_mut(0, 1) = tmp10; - *self.elem_mut(2, 1) = tmp12; - *self.elem_mut(0, 2) = tmp20; - *self.elem_mut(1, 2) = tmp21; - } -} - -impl Mat3 { - /// Construct a 3 x 3 diagonal matrix with the major diagonal set to `value` - /// - /// # Arguments - /// - /// - `value`: the value to set the major diagonal to - /// - /// ~~~ - /// c0 c1 c2 - /// +-----+-----+-----+ - /// r0 | val | 0 | 0 | - /// +-----+-----+-----+ - /// r1 | 0 | val | 0 | - /// +-----+-----+-----+ - /// r2 | 0 | 0 | val | - /// +-----+-----+-----+ - /// ~~~ - #[inline] - pub fn from_value(value: T) -> Mat3 { - Mat3::new(value, Zero::zero(), Zero::zero(), - Zero::zero(), value, Zero::zero(), - Zero::zero(), Zero::zero(), value) - } - - /// Returns the multiplicative identity matrix - /// ~~~ - /// c0 c1 c2 - /// +----+----+----+ - /// r0 | 1 | 0 | 0 | - /// +----+----+----+ - /// r1 | 0 | 1 | 0 | - /// +----+----+----+ - /// r2 | 0 | 0 | 1 | - /// +----+----+----+ - /// ~~~ - #[inline] - pub fn identity() -> Mat3 { - Mat3::new(One::one::(), Zero::zero::(), Zero::zero::(), - Zero::zero::(), One::one::(), Zero::zero::(), - Zero::zero::(), Zero::zero::(), One::one::()) - } - - /// Returns the additive identity matrix - /// ~~~ - /// c0 c1 c2 - /// +----+----+----+ - /// r0 | 0 | 0 | 0 | - /// +----+----+----+ - /// r1 | 0 | 0 | 0 | - /// +----+----+----+ - /// r2 | 0 | 0 | 0 | - /// +----+----+----+ - /// ~~~ - #[inline] - pub fn zero() -> Mat3 { - Mat3::new(Zero::zero::(), Zero::zero::(), Zero::zero::(), - Zero::zero::(), Zero::zero::(), Zero::zero::(), - Zero::zero::(), Zero::zero::(), Zero::zero::()) - } - - #[inline] - pub fn mul_t(&self, value: T) -> Mat3 { - self.map(|&c| c.mul_t(value)) - } - - #[inline] - pub fn mul_v(&self, vec: &Vec3) -> Vec3 { - Vec3::new(self.row(0).dot(vec), - self.row(1).dot(vec), - self.row(2).dot(vec)) - } - - #[inline] - pub fn add_m(&self, other: &Mat3) -> Mat3 { - Mat3::from_cols(self.col(0).add_v(other.col(0)), - self.col(1).add_v(other.col(1)), - self.col(2).add_v(other.col(2))) - } - - #[inline] - pub fn sub_m(&self, other: &Mat3) -> Mat3 { - Mat3::from_cols(self.col(0).sub_v(other.col(0)), - self.col(1).sub_v(other.col(1)), - self.col(2).sub_v(other.col(2))) - } - - #[inline] - pub fn mul_m(&self, other: &Mat3) -> Mat3 { - Mat3::new(self.row(0).dot(other.col(0)), - self.row(1).dot(other.col(0)), - self.row(2).dot(other.col(0)), - - self.row(0).dot(other.col(1)), - self.row(1).dot(other.col(1)), - self.row(2).dot(other.col(1)), - - self.row(0).dot(other.col(2)), - self.row(1).dot(other.col(2)), - self.row(2).dot(other.col(2))) - } - - #[inline] - pub fn mul_self_t(&mut self, value: T) { - self.map_mut(|x| x.mul_self_t(value)) - } - - #[inline] - pub fn add_self_m(&mut self, other: &Mat3) { - self.col_mut(0).add_self_v(other.col(0)); - self.col_mut(1).add_self_v(other.col(1)); - self.col_mut(2).add_self_v(other.col(2)); - } - - #[inline] - pub fn sub_self_m(&mut self, other: &Mat3) { - self.col_mut(0).sub_self_v(other.col(0)); - self.col_mut(1).sub_self_v(other.col(1)); - self.col_mut(2).sub_self_v(other.col(2)); - } - - pub fn dot(&self, other: &Mat3) -> T { - other.transpose().mul_m(self).trace() - } - - pub fn determinant(&self) -> T { - self.col(0).dot(&self.col(1).cross(self.col(2))) - } - - pub fn trace(&self) -> T { - *self.elem(0, 0) + - *self.elem(1, 1) + - *self.elem(2, 2) - } - - #[inline] - pub fn to_identity(&mut self) { - *self = Mat3::identity(); - } - - #[inline] - pub fn to_zero(&mut self) { - *self = Mat3::zero(); - } } impl ToMat4 for Mat3 { - /// Returns the the matrix with an extra row and column added - /// ~~~ - /// c0 c1 c2 c0 c1 c2 c3 - /// +----+----+----+ +----+----+----+----+ - /// r0 | a | b | c | r0 | a | b | c | 0 | - /// +----+----+----+ +----+----+----+----+ - /// r1 | d | e | f | => r1 | d | e | f | 0 | - /// +----+----+----+ +----+----+----+----+ - /// r2 | g | h | i | r2 | g | h | i | 0 | - /// +----+----+----+ +----+----+----+----+ - /// r3 | 0 | 0 | 0 | 1 | - /// +----+----+----+----+ - /// ~~~ #[inline] pub fn to_mat4(&self) -> Mat4 { - Mat4::new(*self.elem(0, 0), *self.elem(0, 1), *self.elem(0, 2), Zero::zero(), - *self.elem(1, 0), *self.elem(1, 1), *self.elem(1, 2), Zero::zero(), - *self.elem(2, 0), *self.elem(2, 1), *self.elem(2, 2), Zero::zero(), - Zero::zero(), Zero::zero(), Zero::zero(), One::one()) - } -} - -impl Neg> for Mat3 { - #[inline] - pub fn neg(&self) -> Mat3 { - self.map(|&x| -x) + Mat4::new(*self.elem(0, 0), *self.elem(0, 1), *self.elem(0, 2), zero!(T), + *self.elem(1, 0), *self.elem(1, 1), *self.elem(1, 2), zero!(T), + *self.elem(2, 0), *self.elem(2, 1), *self.elem(2, 2), zero!(T), + zero!(T), zero!(T), zero!(T), one!(T)) } } @@ -379,9 +78,9 @@ impl Mat3 { let cos_theta = radians.cos(); let sin_theta = radians.sin(); - Mat3::new(One::one(), Zero::zero(), Zero::zero(), - Zero::zero(), cos_theta, sin_theta, - Zero::zero(), -sin_theta, cos_theta) + Mat3::new(one!(T), zero!(T), zero!(T), + zero!(T), cos_theta, sin_theta, + zero!(T), -sin_theta, cos_theta) } /// Construct a matrix from an angular rotation around the `y` axis @@ -390,9 +89,9 @@ impl Mat3 { let cos_theta = radians.cos(); let sin_theta = radians.sin(); - Mat3::new(cos_theta, Zero::zero(), -sin_theta, - Zero::zero(), One::one(), Zero::zero(), - sin_theta, Zero::zero(), cos_theta) + Mat3::new(cos_theta, zero!(T), -sin_theta, + zero!(T), one!(T), zero!(T), + sin_theta, zero!(T), cos_theta) } /// Construct a matrix from an angular rotation around the `z` axis @@ -401,9 +100,9 @@ impl Mat3 { let cos_theta = radians.cos(); let sin_theta = radians.sin(); - Mat3::new(cos_theta, sin_theta, Zero::zero(), - -sin_theta, cos_theta, Zero::zero(), - Zero::zero(), Zero::zero(), One::one()) + Mat3::new(cos_theta, sin_theta, zero!(T), + -sin_theta, cos_theta, zero!(T), + zero!(T), zero!(T), one!(T)) } /// Construct a matrix from Euler angles @@ -431,7 +130,7 @@ impl Mat3 { pub fn from_angle_axis(radians: T, axis: &Vec3) -> Mat3 { let c = radians.cos(); let s = radians.sin(); - let _1_c = One::one::() - c; + let _1_c = one!(T) - c; let x = axis.x; let y = axis.y; @@ -467,11 +166,11 @@ impl ToQuat for Mat3 { let trace = self.trace(); // FIXME: We don't have any numeric conversions in std yet :P - let half = One::one::() / (One::one::() + One::one::()); + let half = one!(T) / two!(T); cond! ( - (trace >= Zero::zero()) { - s = (One::one::() + trace).sqrt(); + (trace >= zero!(T)) { + s = (one!(T) + trace).sqrt(); w = half * s; s = half / s; x = (*self.elem(1, 2) - *self.elem(2, 1)) * s; @@ -508,82 +207,6 @@ impl ToQuat for Mat3 { } } -impl> Mat3 { - pub fn inverse(&self) -> Option> { - let d = self.determinant(); - if d.approx_eq(&Zero::zero()) { - None - } else { - Some(Mat3::from_cols(self.col(1).cross(self.col(2)).div_t(d), - self.col(2).cross(self.col(0)).div_t(d), - self.col(0).cross(self.col(1)).div_t(d)).transpose()) - } - } - - #[inline] - pub fn invert_self(&mut self) { - *self = self.inverse().expect("Couldn't invert the matrix!"); - } - - #[inline] - pub fn is_identity(&self) -> bool { - self.approx_eq(&Mat3::identity()) - } - - #[inline] - pub fn is_diagonal(&self) -> bool { - self.elem(0, 1).approx_eq(&Zero::zero()) && - self.elem(0, 2).approx_eq(&Zero::zero()) && - - self.elem(1, 0).approx_eq(&Zero::zero()) && - self.elem(1, 2).approx_eq(&Zero::zero()) && - - self.elem(2, 0).approx_eq(&Zero::zero()) && - self.elem(2, 1).approx_eq(&Zero::zero()) - } - - #[inline] - pub fn is_rotated(&self) -> bool { - !self.approx_eq(&Mat3::identity()) - } - - #[inline] - pub fn is_symmetric(&self) -> bool { - self.elem(0, 1).approx_eq(self.elem(1, 0)) && - self.elem(0, 2).approx_eq(self.elem(2, 0)) && - - self.elem(1, 0).approx_eq(self.elem(0, 1)) && - self.elem(1, 2).approx_eq(self.elem(2, 1)) && - - self.elem(2, 0).approx_eq(self.elem(0, 2)) && - self.elem(2, 1).approx_eq(self.elem(1, 2)) - } - - #[inline] - pub fn is_invertible(&self) -> bool { - !self.determinant().approx_eq(&Zero::zero()) - } -} - -impl> ApproxEq for Mat3 { - #[inline] - pub fn approx_epsilon() -> T { - ApproxEq::approx_epsilon::() - } - - #[inline] - pub fn approx_eq(&self, other: &Mat3) -> bool { - self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) - } - - #[inline] - pub fn approx_eq_eps(&self, other: &Mat3, epsilon: &T) -> bool { - self.col(0).approx_eq_eps(other.col(0), epsilon) && - self.col(1).approx_eq_eps(other.col(1), epsilon) && - self.col(2).approx_eq_eps(other.col(2), epsilon) - } -} - #[cfg(test)] mod tests{ use mat::*; diff --git a/src/mat4.rs b/src/mat4.rs index 7c493b4..4487a35 100644 --- a/src/mat4.rs +++ b/src/mat4.rs @@ -13,14 +13,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::cast::transmute; -use std::cmp::ApproxEq; -use std::num::{Zero, One}; -use std::uint; +pub use super::Dimensional; -use super::Dimensional; +use mat::Mat3; use vec::Vec4; -use super::Mat3; + +mod macros; +mod mat_macros; #[deriving(Eq)] pub struct Mat4 { @@ -30,32 +29,21 @@ pub struct Mat4 { w: Vec4, } +impl_dimensional!(Mat4, Vec4, 4) +impl_dimensional_fns!(Mat4, Vec4, 4) +impl_approx!(Mat4) + +impl_mat!(Mat4, Vec4) +impl_mat_copyable!(Mat4, Vec4) +impl_mat_numeric!(Mat4, Vec4) +impl_mat_approx_numeric!(Mat4) +impl_mat_neg!(Mat4) + pub trait ToMat4 { pub fn to_mat4(&self) -> Mat4; } impl Mat4 { - /// Construct a 4 x 4 matrix - /// - /// # Arguments - /// - /// - `c0r0`, `c0r1`, `c0r2`, `c0r3`: the first column of the matrix - /// - `c1r0`, `c1r1`, `c1r2`, `c1r3`: the second column of the matrix - /// - `c2r0`, `c2r1`, `c2r2`, `c2r3`: the third column of the matrix - /// - `c3r0`, `c3r1`, `c3r2`, `c3r3`: the fourth column of the matrix - /// - /// ~~~ - /// c0 c1 c2 c3 - /// +------+------+------+------+ - /// r0 | c0r0 | c1r0 | c2r0 | c3r0 | - /// +------+------+------+------+ - /// r1 | c0r1 | c1r1 | c2r1 | c3r1 | - /// +------+------+------+------+ - /// r2 | c0r2 | c1r2 | c2r2 | c3r2 | - /// +------+------+------+------+ - /// r3 | c0r3 | c1r3 | c2r3 | c3r3 | - /// +------+------+------+------+ - /// ~~~ #[inline] pub fn new(c0r0: T, c0r1: T, c0r2: T, c0r3: T, c1r0: T, c1r1: T, c1r2: T, c1r3: T, @@ -67,27 +55,6 @@ impl Mat4 { Vec4::new(c3r0, c3r1, c3r2, c3r3)) } - /// Construct a 4 x 4 matrix from column vectors - /// - /// # Arguments - /// - /// - `c0`: the first column vector of the matrix - /// - `c1`: the second column vector of the matrix - /// - `c2`: the third column vector of the matrix - /// - `c3`: the fourth column vector of the matrix - /// - /// ~~~ - /// c0 c1 c2 c3 - /// +------+------+------+------+ - /// r0 | c0.x | c1.x | c2.x | c3.x | - /// +------+------+------+------+ - /// r1 | c0.y | c1.y | c2.y | c3.y | - /// +------+------+------+------+ - /// r2 | c0.z | c1.z | c2.z | c3.z | - /// +------+------+------+------+ - /// r3 | c0.w | c1.w | c2.w | c3.w | - /// +------+------+------+------+ - /// ~~~ #[inline] pub fn from_cols(c0: Vec4, c1: Vec4, @@ -95,456 +62,6 @@ impl Mat4 { c3: Vec4) -> Mat4 { Mat4 { x: c0, y: c1, z: c2, w: c3 } } - - #[inline] - pub fn col<'a>(&'a self, i: uint) -> &'a Vec4 { - self.index(i) - } - - #[inline] - pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec4 { - self.index_mut(i) - } - - #[inline] - pub fn elem<'a>(&'a self, i: uint, j: uint) -> &'a T { - self.index(i).index(j) - } - - #[inline] - pub fn elem_mut<'a>(&'a mut self, i: uint, j: uint) -> &'a mut T { - self.index_mut(i).index_mut(j) - } -} - -impl Dimensional,[Vec4,..4]> for Mat4 { - #[inline] - pub fn index<'a>(&'a self, i: uint) -> &'a Vec4 { - &'a self.as_slice()[i] - } - - #[inline] - pub fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec4 { - &'a mut self.as_mut_slice()[i] - } - - #[inline] - pub fn as_slice<'a>(&'a self) -> &'a [Vec4,..4] { - unsafe { transmute(self) } - } - - #[inline] - pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec4,..4] { - unsafe { transmute(self) } - } - - #[inline(always)] - pub fn map(&self, f: &fn(&Vec4) -> Vec4) -> Mat4 { - Mat4::from_cols(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 Vec4)) { - f(self.index_mut(0)); - f(self.index_mut(1)); - f(self.index_mut(2)); - f(self.index_mut(3)); - } -} - -impl Mat4 { - #[inline] - pub fn row(&self, i: uint) -> Vec4 { - Vec4::new(*self.elem(0, i), - *self.elem(1, i), - *self.elem(2, i), - *self.elem(3, i)) - } - - #[inline] - pub fn swap_cols(&mut self, a: uint, b: uint) { - let tmp = *self.col(a); - *self.col_mut(a) = *self.col(b); - *self.col_mut(b) = tmp; - } - - #[inline] - pub fn swap_rows(&mut self, a: uint, b: uint) { - self.x.swap(a, b); - self.y.swap(a, b); - self.z.swap(a, b); - self.w.swap(a, b); - } - - #[inline] - pub fn transpose(&self) -> Mat4 { - Mat4::new(*self.elem(0, 0), *self.elem(1, 0), *self.elem(2, 0), *self.elem(3, 0), - *self.elem(0, 1), *self.elem(1, 1), *self.elem(2, 1), *self.elem(3, 1), - *self.elem(0, 2), *self.elem(1, 2), *self.elem(2, 2), *self.elem(3, 2), - *self.elem(0, 3), *self.elem(1, 3), *self.elem(2, 3), *self.elem(3, 3)) - } - - #[inline] - pub fn transpose_self(&mut self) { - let tmp01 = *self.elem(0, 1); - let tmp02 = *self.elem(0, 2); - let tmp03 = *self.elem(0, 3); - let tmp10 = *self.elem(1, 0); - let tmp12 = *self.elem(1, 2); - let tmp13 = *self.elem(1, 3); - let tmp20 = *self.elem(2, 0); - let tmp21 = *self.elem(2, 1); - let tmp23 = *self.elem(2, 3); - let tmp30 = *self.elem(3, 0); - let tmp31 = *self.elem(3, 1); - let tmp32 = *self.elem(3, 2); - - *self.elem_mut(0, 1) = *self.elem(1, 0); - *self.elem_mut(0, 2) = *self.elem(2, 0); - *self.elem_mut(0, 3) = *self.elem(3, 0); - *self.elem_mut(1, 0) = *self.elem(0, 1); - *self.elem_mut(1, 2) = *self.elem(2, 1); - *self.elem_mut(1, 3) = *self.elem(3, 1); - *self.elem_mut(2, 0) = *self.elem(0, 2); - *self.elem_mut(2, 1) = *self.elem(1, 2); - *self.elem_mut(2, 3) = *self.elem(3, 2); - *self.elem_mut(3, 0) = *self.elem(0, 3); - *self.elem_mut(3, 1) = *self.elem(1, 3); - *self.elem_mut(3, 2) = *self.elem(2, 3); - - *self.elem_mut(1, 0) = tmp01; - *self.elem_mut(2, 0) = tmp02; - *self.elem_mut(3, 0) = tmp03; - *self.elem_mut(0, 1) = tmp10; - *self.elem_mut(2, 1) = tmp12; - *self.elem_mut(3, 1) = tmp13; - *self.elem_mut(0, 2) = tmp20; - *self.elem_mut(1, 2) = tmp21; - *self.elem_mut(3, 2) = tmp23; - *self.elem_mut(0, 3) = tmp30; - *self.elem_mut(1, 3) = tmp31; - *self.elem_mut(2, 3) = tmp32; - } -} - -impl Mat4 { - /// Construct a 4 x 4 diagonal matrix with the major diagonal set to `value` - /// - /// # Arguments - /// - /// - `value`: the value to set the major diagonal to - /// - /// ~~~ - /// c0 c1 c2 c3 - /// +-----+-----+-----+-----+ - /// r0 | val | 0 | 0 | 0 | - /// +-----+-----+-----+-----+ - /// r1 | 0 | val | 0 | 0 | - /// +-----+-----+-----+-----+ - /// r2 | 0 | 0 | val | 0 | - /// +-----+-----+-----+-----+ - /// r3 | 0 | 0 | 0 | val | - /// +-----+-----+-----+-----+ - /// ~~~ - #[inline] - pub fn from_value(value: T) -> Mat4 { - Mat4::new(value, Zero::zero(), Zero::zero(), Zero::zero(), - Zero::zero(), value, Zero::zero(), Zero::zero(), - Zero::zero(), Zero::zero(), value, Zero::zero(), - Zero::zero(), Zero::zero(), Zero::zero(), value) - } - - /// Returns the multiplicative identity matrix - /// ~~~ - /// c0 c1 c2 c3 - /// +----+----+----+----+ - /// r0 | 1 | 0 | 0 | 0 | - /// +----+----+----+----+ - /// r1 | 0 | 1 | 0 | 0 | - /// +----+----+----+----+ - /// r2 | 0 | 0 | 1 | 0 | - /// +----+----+----+----+ - /// r3 | 0 | 0 | 0 | 1 | - /// +----+----+----+----+ - /// ~~~ - #[inline] - pub fn identity() -> Mat4 { - Mat4::new(One::one::(), Zero::zero::(), Zero::zero::(), Zero::zero::(), - Zero::zero::(), One::one::(), Zero::zero::(), Zero::zero::(), - Zero::zero::(), Zero::zero::(), One::one::(), Zero::zero::(), - Zero::zero::(), Zero::zero::(), Zero::zero::(), One::one::()) - } - - /// Returns the additive identity matrix - /// ~~~ - /// c0 c1 c2 c3 - /// +----+----+----+----+ - /// r0 | 0 | 0 | 0 | 0 | - /// +----+----+----+----+ - /// r1 | 0 | 0 | 0 | 0 | - /// +----+----+----+----+ - /// r2 | 0 | 0 | 0 | 0 | - /// +----+----+----+----+ - /// r3 | 0 | 0 | 0 | 0 | - /// +----+----+----+----+ - /// ~~~ - #[inline] - pub fn zero() -> Mat4 { - Mat4::new(Zero::zero::(), Zero::zero::(), Zero::zero::(), Zero::zero::(), - Zero::zero::(), Zero::zero::(), Zero::zero::(), Zero::zero::(), - Zero::zero::(), Zero::zero::(), Zero::zero::(), Zero::zero::(), - Zero::zero::(), Zero::zero::(), Zero::zero::(), Zero::zero::()) - } - - #[inline] - pub fn mul_t(&self, value: T) -> Mat4 { - self.map(|&c| c.mul_t(value)) - } - - #[inline] - pub fn mul_v(&self, vec: &Vec4) -> Vec4 { - Vec4::new(self.row(0).dot(vec), - self.row(1).dot(vec), - self.row(2).dot(vec), - self.row(3).dot(vec)) - } - - #[inline] - pub fn add_m(&self, other: &Mat4) -> Mat4 { - Mat4::from_cols(self.col(0).add_v(other.col(0)), - self.col(1).add_v(other.col(1)), - self.col(2).add_v(other.col(2)), - self.col(3).add_v(other.col(3))) - } - - #[inline] - pub fn sub_m(&self, other: &Mat4) -> Mat4 { - Mat4::from_cols(self.col(0).sub_v(other.col(0)), - self.col(1).sub_v(other.col(1)), - self.col(2).sub_v(other.col(2)), - self.col(3).sub_v(other.col(3))) - } - - #[inline] - pub fn mul_m(&self, other: &Mat4) -> Mat4 { - Mat4::new(self.row(0).dot(other.col(0)), - self.row(1).dot(other.col(0)), - self.row(2).dot(other.col(0)), - self.row(3).dot(other.col(0)), - - self.row(0).dot(other.col(1)), - self.row(1).dot(other.col(1)), - self.row(2).dot(other.col(1)), - self.row(3).dot(other.col(1)), - - self.row(0).dot(other.col(2)), - self.row(1).dot(other.col(2)), - self.row(2).dot(other.col(2)), - self.row(3).dot(other.col(2)), - - self.row(0).dot(other.col(3)), - self.row(1).dot(other.col(3)), - self.row(2).dot(other.col(3)), - self.row(3).dot(other.col(3))) - } - - #[inline] - pub fn mul_self_t(&mut self, value: T) { - self.map_mut(|x| x.mul_self_t(value)) - } - - #[inline] - pub fn add_self_m(&mut self, other: &Mat4) { - self.col_mut(0).add_self_v(other.col(0)); - self.col_mut(1).add_self_v(other.col(1)); - self.col_mut(2).add_self_v(other.col(2)); - self.col_mut(3).add_self_v(other.col(3)); - } - - #[inline] - pub fn sub_self_m(&mut self, other: &Mat4) { - self.col_mut(0).sub_self_v(other.col(0)); - self.col_mut(1).sub_self_v(other.col(1)); - self.col_mut(2).sub_self_v(other.col(2)); - self.col_mut(3).sub_self_v(other.col(3)); - } - - pub fn dot(&self, other: &Mat4) -> T { - other.transpose().mul_m(self).trace() - } - - pub fn determinant(&self) -> T { - let m0 = Mat3::new(*self.elem(1, 1), *self.elem(2, 1), *self.elem(3, 1), - *self.elem(1, 2), *self.elem(2, 2), *self.elem(3, 2), - *self.elem(1, 3), *self.elem(2, 3), *self.elem(3, 3)); - let m1 = Mat3::new(*self.elem(0, 1), *self.elem(2, 1), *self.elem(3, 1), - *self.elem(0, 2), *self.elem(2, 2), *self.elem(3, 2), - *self.elem(0, 3), *self.elem(2, 3), *self.elem(3, 3)); - let m2 = Mat3::new(*self.elem(0, 1), *self.elem(1, 1), *self.elem(3, 1), - *self.elem(0, 2), *self.elem(1, 2), *self.elem(3, 2), - *self.elem(0, 3), *self.elem(1, 3), *self.elem(3, 3)); - let m3 = Mat3::new(*self.elem(0, 1), *self.elem(1, 1), *self.elem(2, 1), - *self.elem(0, 2), *self.elem(1, 2), *self.elem(2, 2), - *self.elem(0, 3), *self.elem(1, 3), *self.elem(2, 3)); - - self.elem(0, 0) * m0.determinant() - - self.elem(1, 0) * m1.determinant() + - self.elem(2, 0) * m2.determinant() - - self.elem(3, 0) * m3.determinant() - } - - pub fn trace(&self) -> T { - *self.elem(0, 0) + - *self.elem(1, 1) + - *self.elem(2, 2) + - *self.elem(3, 3) - } - - #[inline] - pub fn to_identity(&mut self) { - *self = Mat4::identity(); - } - - #[inline] - pub fn to_zero(&mut self) { - *self = Mat4::zero(); - } -} - -impl Neg> for Mat4 { - #[inline] - pub fn neg(&self) -> Mat4 { - self.map(|&x| -x) - } -} - -impl> Mat4 { - pub fn inverse(&self) -> Option> { - let d = self.determinant(); - if d.approx_eq(&Zero::zero()) { - None - } else { - // Gauss Jordan Elimination with partial pivoting - // So take this matrix, A, augmented with the identity - // and essentially reduce [A|I] - - let mut A = *self; - let mut I = Mat4::identity::(); - - for uint::range(0, 4) |j| { - // Find largest element in col j - let mut i1 = j; - for uint::range(j + 1, 4) |i| { - if A.elem(j, i).abs() > A.elem(j, i1).abs() { - i1 = i; - } - } - - // Swap columns i1 and j in A and I to - // put pivot on diagonal - A.swap_cols(i1, j); - I.swap_cols(i1, j); - - // Scale col j to have a unit diagonal - let ajj = *A.elem(j, j); - I.col_mut(j).div_self_t(ajj); - A.col_mut(j).div_self_t(ajj); - - // Eliminate off-diagonal elems in col j of A, - // doing identical ops to I - for uint::range(0, 4) |i| { - if i != j { - let ij_mul_aij = I.col(j).mul_t(*A.elem(i, j)); - let aj_mul_aij = A.col(j).mul_t(*A.elem(i, j)); - I.col_mut(i).sub_self_v(&ij_mul_aij); - A.col_mut(i).sub_self_v(&aj_mul_aij); - } - } - } - Some(I) - } - } - - #[inline] - pub fn invert_self(&mut self) { - *self = self.inverse().expect("Couldn't invert the matrix!"); - } - - #[inline] - pub fn is_identity(&self) -> bool { - self.approx_eq(&Mat4::identity()) - } - - #[inline] - pub fn is_diagonal(&self) -> bool { - self.elem(0, 1).approx_eq(&Zero::zero()) && - self.elem(0, 2).approx_eq(&Zero::zero()) && - self.elem(0, 3).approx_eq(&Zero::zero()) && - - self.elem(1, 0).approx_eq(&Zero::zero()) && - self.elem(1, 2).approx_eq(&Zero::zero()) && - self.elem(1, 3).approx_eq(&Zero::zero()) && - - self.elem(2, 0).approx_eq(&Zero::zero()) && - self.elem(2, 1).approx_eq(&Zero::zero()) && - self.elem(2, 3).approx_eq(&Zero::zero()) && - - self.elem(3, 0).approx_eq(&Zero::zero()) && - self.elem(3, 1).approx_eq(&Zero::zero()) && - self.elem(3, 2).approx_eq(&Zero::zero()) - } - - #[inline] - pub fn is_rotated(&self) -> bool { - !self.approx_eq(&Mat4::identity()) - } - - #[inline] - pub fn is_symmetric(&self) -> bool { - self.elem(0, 1).approx_eq(self.elem(1, 0)) && - self.elem(0, 2).approx_eq(self.elem(2, 0)) && - self.elem(0, 3).approx_eq(self.elem(3, 0)) && - - self.elem(1, 0).approx_eq(self.elem(0, 1)) && - self.elem(1, 2).approx_eq(self.elem(2, 1)) && - self.elem(1, 3).approx_eq(self.elem(3, 1)) && - - self.elem(2, 0).approx_eq(self.elem(0, 2)) && - self.elem(2, 1).approx_eq(self.elem(1, 2)) && - self.elem(2, 3).approx_eq(self.elem(3, 2)) && - - self.elem(3, 0).approx_eq(self.elem(0, 3)) && - self.elem(3, 1).approx_eq(self.elem(1, 3)) && - self.elem(3, 2).approx_eq(self.elem(2, 3)) - } - - #[inline] - pub fn is_invertible(&self) -> bool { - !self.determinant().approx_eq(&Zero::zero()) - } -} - -impl> ApproxEq for Mat4 { - #[inline] - pub fn approx_epsilon() -> T { - ApproxEq::approx_epsilon::() - } - - #[inline] - pub fn approx_eq(&self, other: &Mat4) -> bool { - self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) - } - - #[inline] - pub fn approx_eq_eps(&self, other: &Mat4, epsilon: &T) -> bool { - self.col(0).approx_eq_eps(other.col(0), epsilon) && - self.col(1).approx_eq_eps(other.col(1), epsilon) && - self.col(2).approx_eq_eps(other.col(2), epsilon) && - self.col(3).approx_eq_eps(other.col(3), epsilon) - } } #[cfg(test)] diff --git a/src/mat_macros.rs b/src/mat_macros.rs new file mode 100644 index 0000000..a9de7da --- /dev/null +++ b/src/mat_macros.rs @@ -0,0 +1,508 @@ +// 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_mat( + ($Mat:ident, $Vec:ident) => ( + impl $Mat { + #[inline] + pub fn col<'a>(&'a self, i: uint) -> &'a $Vec { + self.index(i) + } + + #[inline] + pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut $Vec { + self.index_mut(i) + } + + #[inline] + pub fn elem<'a>(&'a self, i: uint, j: uint) -> &'a T { + self.index(i).index(j) + } + + #[inline] + pub fn elem_mut<'a>(&'a mut self, i: uint, j: uint) -> &'a mut T { + self.index_mut(i).index_mut(j) + } + } + ) +) + +macro_rules! impl_mat_copyable( + ($Mat:ident, $Vec:ident) => ( + impl $Mat { + #[inline] + pub fn row(&self, i: uint) -> $Vec { + $Vec::from_slice(self.map(|c| *c.index(i))) + } + + #[inline] + pub fn swap_cols(&mut self, a: uint, b: uint) { + let tmp = *self.col(a); + *self.col_mut(a) = *self.col(b); + *self.col_mut(b) = tmp; + } + + #[inline] + pub fn swap_rows(&mut self, a: uint, b: uint) { + self.map_mut(|x| x.swap(a, b)) + } + + #[inline] + pub fn swap_elem(&mut self, (ai, aj): (uint, uint), (bi, bj): (uint, uint)) { + let tmp = *self.elem(ai, aj); + *self.elem_mut(ai, aj) = *self.elem(bi, bj); + *self.elem_mut(bi, bj) = tmp; + } + + #[inline] pub fn transpose(&self) -> $Mat { mat_transpose!($Mat) } + + #[inline] pub fn transpose_self(&mut self) { mat_transpose_self!($Mat) } + } + ) +) + +macro_rules! mat_transpose( + (Mat2) => ( + Mat2::new(*self.elem(0, 0), *self.elem(1, 0), + *self.elem(0, 1), *self.elem(1, 1)) + ); + (Mat3) => ( + Mat3::new(*self.elem(0, 0), *self.elem(1, 0), *self.elem(2, 0), + *self.elem(0, 1), *self.elem(1, 1), *self.elem(2, 1), + *self.elem(0, 2), *self.elem(1, 2), *self.elem(2, 2)) + ); + (Mat4) => ( + Mat4::new(*self.elem(0, 0), *self.elem(1, 0), *self.elem(2, 0), *self.elem(3, 0), + *self.elem(0, 1), *self.elem(1, 1), *self.elem(2, 1), *self.elem(3, 1), + *self.elem(0, 2), *self.elem(1, 2), *self.elem(2, 2), *self.elem(3, 2), + *self.elem(0, 3), *self.elem(1, 3), *self.elem(2, 3), *self.elem(3, 3)) + ); +) + +macro_rules! mat_transpose_self( + (Mat2) => ( + self.swap_elem((0, 1), (1, 0)); + self.swap_elem((1, 0), (0, 1)); + ); + (Mat3) => ( + self.swap_elem((0, 1), (1, 0)); + self.swap_elem((0, 2), (2, 0)); + + self.swap_elem((1, 0), (0, 1)); + self.swap_elem((1, 2), (2, 1)); + + self.swap_elem((2, 0), (0, 2)); + self.swap_elem((2, 1), (1, 2)); + ); + (Mat4) => ( + self.swap_elem((0, 1), (1, 0)); + self.swap_elem((0, 2), (2, 0)); + self.swap_elem((0, 3), (3, 0)); + + self.swap_elem((1, 0), (0, 1)); + self.swap_elem((1, 2), (2, 1)); + self.swap_elem((1, 3), (3, 1)); + + self.swap_elem((2, 0), (0, 2)); + self.swap_elem((2, 1), (1, 2)); + self.swap_elem((2, 3), (3, 2)); + + self.swap_elem((3, 0), (0, 3)); + self.swap_elem((3, 1), (1, 3)); + self.swap_elem((3, 2), (2, 3)); + ); +) + +macro_rules! impl_mat_numeric( + ($Mat:ident, $Vec:ident) => ( + impl $Mat { + #[inline] + pub fn from_value(value: T) -> $Mat { mat_from_value!($Mat) } + + #[inline] + pub fn identity() -> $Mat { $Mat::from_value(one!(T)) } + + #[inline] + pub fn zero() -> $Mat { $Mat::from_value(zero!(T)) } + + #[inline] + pub fn mul_t(&self, value: T) -> $Mat { + $Mat::from_slice(self.map(|&c| c.mul_t(value))) + } + + #[inline] + pub fn mul_v(&self, vec: &$Vec) -> $Vec { + mat_mul_v!($Mat) + } + + #[inline] + pub fn mul_m(&self, other: &$Mat) -> $Mat { + mat_mul_m!($Mat) + } + + #[inline] + pub fn add_m(&self, other: &$Mat) -> $Mat { + $Mat::from_slice(self.zip(other, |a, b| a.add_v(b))) + } + + #[inline] + pub fn sub_m(&self, other: &$Mat) -> $Mat { + $Mat::from_slice(self.zip(other, |a, b| a.sub_v(b))) + } + + #[inline] + pub fn mul_self_t(&mut self, value: T) { + self.map_mut(|x| x.mul_self_t(value)) + } + + #[inline] + pub fn add_self_m(&mut self, other: &$Mat) { + self.zip_mut(other, |a, b| a.add_self_v(b)) + } + + #[inline] + pub fn sub_self_m(&mut self, other: &$Mat) { + self.zip_mut(other, |a, b| a.sub_self_v(b)) + } + + pub fn dot(&self, other: &$Mat) -> T { + other.transpose().mul_m(self).trace() + } + + pub fn determinant(&self) -> T { + mat_determinant!($Mat) + } + + pub fn trace(&self) -> T { + mat_trace!($Mat) + } + + #[inline] + pub fn to_identity(&mut self) { + *self = $Mat::identity(); + } + + #[inline] + pub fn to_zero(&mut self) { + *self = $Mat::zero(); + } + } + ) +) + +macro_rules! mat_from_value( + (Mat2) => ( + Mat2::new(value, zero!(T), + zero!(T), value) + ); + (Mat3) => ( + Mat3::new(value, zero!(T), zero!(T), + zero!(T), value, zero!(T), + zero!(T), zero!(T), value) + ); + (Mat4) => ( + Mat4::new(value, zero!(T), zero!(T), zero!(T), + zero!(T), value, zero!(T), zero!(T), + zero!(T), zero!(T), value, zero!(T), + zero!(T), zero!(T), zero!(T), value) + ); +) + +macro_rules! mat_mul_v( + (Mat2) => ( + Vec2::new(self.row(0).dot(vec), + self.row(1).dot(vec)) + ); + (Mat3) => ( + Vec3::new(self.row(0).dot(vec), + self.row(1).dot(vec), + self.row(2).dot(vec)) + ); + (Mat4) => ( + Vec4::new(self.row(0).dot(vec), + self.row(1).dot(vec), + self.row(2).dot(vec), + self.row(3).dot(vec)) + ); +) + +macro_rules! mat_mul_m( + (Mat2) => ( + Mat2::new(self.row(0).dot(other.col(0)), self.row(1).dot(other.col(0)), + self.row(0).dot(other.col(1)), self.row(1).dot(other.col(1))) + ); + (Mat3) => ( + Mat3::new(self.row(0).dot(other.col(0)), + self.row(1).dot(other.col(0)), + self.row(2).dot(other.col(0)), + + self.row(0).dot(other.col(1)), + self.row(1).dot(other.col(1)), + self.row(2).dot(other.col(1)), + + self.row(0).dot(other.col(2)), + self.row(1).dot(other.col(2)), + self.row(2).dot(other.col(2))) + ); + (Mat4) => ( + Mat4::new(self.row(0).dot(other.col(0)), + self.row(1).dot(other.col(0)), + self.row(2).dot(other.col(0)), + self.row(3).dot(other.col(0)), + + self.row(0).dot(other.col(1)), + self.row(1).dot(other.col(1)), + self.row(2).dot(other.col(1)), + self.row(3).dot(other.col(1)), + + self.row(0).dot(other.col(2)), + self.row(1).dot(other.col(2)), + self.row(2).dot(other.col(2)), + self.row(3).dot(other.col(2)), + + self.row(0).dot(other.col(3)), + self.row(1).dot(other.col(3)), + self.row(2).dot(other.col(3)), + self.row(3).dot(other.col(3))) + ); +) + +macro_rules! mat_determinant( + (Mat2) => ( + *self.elem(0, 0) * + *self.elem(1, 1) - + *self.elem(1, 0) * + *self.elem(0, 1) + ); + (Mat3) => ( + self.col(0).dot(&self.col(1).cross(self.col(2))) + ); + (Mat4) => ({ + let m0 = Mat3::new(*self.elem(1, 1), *self.elem(2, 1), *self.elem(3, 1), + *self.elem(1, 2), *self.elem(2, 2), *self.elem(3, 2), + *self.elem(1, 3), *self.elem(2, 3), *self.elem(3, 3)); + let m1 = Mat3::new(*self.elem(0, 1), *self.elem(2, 1), *self.elem(3, 1), + *self.elem(0, 2), *self.elem(2, 2), *self.elem(3, 2), + *self.elem(0, 3), *self.elem(2, 3), *self.elem(3, 3)); + let m2 = Mat3::new(*self.elem(0, 1), *self.elem(1, 1), *self.elem(3, 1), + *self.elem(0, 2), *self.elem(1, 2), *self.elem(3, 2), + *self.elem(0, 3), *self.elem(1, 3), *self.elem(3, 3)); + let m3 = Mat3::new(*self.elem(0, 1), *self.elem(1, 1), *self.elem(2, 1), + *self.elem(0, 2), *self.elem(1, 2), *self.elem(2, 2), + *self.elem(0, 3), *self.elem(1, 3), *self.elem(2, 3)); + + self.elem(0, 0) * m0.determinant() - + self.elem(1, 0) * m1.determinant() + + self.elem(2, 0) * m2.determinant() - + self.elem(3, 0) * m3.determinant() + }); +) + +macro_rules! mat_trace( + (Mat2) => (*self.elem(0, 0) + *self.elem(1, 1)); + (Mat3) => (*self.elem(0, 0) + *self.elem(1, 1) + *self.elem(2, 2)); + (Mat4) => (*self.elem(0, 0) + *self.elem(1, 1) + *self.elem(2, 2) + *self.elem(3, 3)); +) + +macro_rules! impl_mat_approx_numeric( + ($Mat:ident) => ( + impl> $Mat { + #[inline] + pub fn inverse(&self) -> Option<$Mat> { + mat_inverse!($Mat) + } + + #[inline] + pub fn invert_self(&mut self) { + *self = self.inverse().expect("Couldn't invert the matrix!"); + } + + #[inline] + pub fn is_identity(&self) -> bool { + self.approx_eq(&$Mat::identity()) + } + + #[inline] + pub fn is_diagonal(&self) -> bool { + mat_is_diagonal!($Mat) + } + + #[inline] + pub fn is_rotated(&self) -> bool { + !self.approx_eq(&$Mat::identity()) + } + + #[inline] + pub fn is_symmetric(&self) -> bool { + mat_is_symmetric!($Mat) + } + + #[inline] + pub fn is_invertible(&self) -> bool { + !self.determinant().approx_eq(&zero!(T)) + } + } + ) +) + +macro_rules! mat_inverse( + (Mat2) => ({ + let d = self.determinant(); + if d.approx_eq(&zero!(T)) { + None + } else { + Some(Mat2::new(self.elem(1, 1) / d, -self.elem(0, 1) / d, + -self.elem(1, 0) / d, self.elem(0, 0) / d)) + } + }); + (Mat3) => ({ + let d = self.determinant(); + if d.approx_eq(&zero!(T)) { + None + } else { + Some(Mat3::from_cols(self.col(1).cross(self.col(2)).div_t(d), + self.col(2).cross(self.col(0)).div_t(d), + self.col(0).cross(self.col(1)).div_t(d)).transpose()) + } + }); + (Mat4) => ({ + use std::uint; + + let d = self.determinant(); + if d.approx_eq(&zero!(T)) { + None + } else { + // Gauss Jordan Elimination with partial pivoting + // So take this matrix, A, augmented with the identity + // and essentially reduce [A|I] + + let mut A = *self; + let mut I = Mat4::identity::(); + + for uint::range(0, 4) |j| { + // Find largest element in col j + let mut i1 = j; + for uint::range(j + 1, 4) |i| { + if A.elem(j, i).abs() > A.elem(j, i1).abs() { + i1 = i; + } + } + + // Swap columns i1 and j in A and I to + // put pivot on diagonal + A.swap_cols(i1, j); + I.swap_cols(i1, j); + + // Scale col j to have a unit diagonal + let ajj = *A.elem(j, j); + I.col_mut(j).div_self_t(ajj); + A.col_mut(j).div_self_t(ajj); + + // Eliminate off-diagonal elems in col j of A, + // doing identical ops to I + for uint::range(0, 4) |i| { + if i != j { + let ij_mul_aij = I.col(j).mul_t(*A.elem(i, j)); + let aj_mul_aij = A.col(j).mul_t(*A.elem(i, j)); + I.col_mut(i).sub_self_v(&ij_mul_aij); + A.col_mut(i).sub_self_v(&aj_mul_aij); + } + } + } + Some(I) + } + }); +) + +macro_rules! mat_is_diagonal( + (Mat2) => ( + self.elem(0, 1).approx_eq(&zero!(T)) && + self.elem(1, 0).approx_eq(&zero!(T)) + ); + (Mat3) => ( + self.elem(0, 1).approx_eq(&zero!(T)) && + self.elem(0, 2).approx_eq(&zero!(T)) && + + self.elem(1, 0).approx_eq(&zero!(T)) && + self.elem(1, 2).approx_eq(&zero!(T)) && + + self.elem(2, 0).approx_eq(&zero!(T)) && + self.elem(2, 1).approx_eq(&zero!(T)) + ); + (Mat4) => ( + self.elem(0, 1).approx_eq(&zero!(T)) && + self.elem(0, 2).approx_eq(&zero!(T)) && + self.elem(0, 3).approx_eq(&zero!(T)) && + + self.elem(1, 0).approx_eq(&zero!(T)) && + self.elem(1, 2).approx_eq(&zero!(T)) && + self.elem(1, 3).approx_eq(&zero!(T)) && + + self.elem(2, 0).approx_eq(&zero!(T)) && + self.elem(2, 1).approx_eq(&zero!(T)) && + self.elem(2, 3).approx_eq(&zero!(T)) && + + self.elem(3, 0).approx_eq(&zero!(T)) && + self.elem(3, 1).approx_eq(&zero!(T)) && + self.elem(3, 2).approx_eq(&zero!(T)) + ); +) + +macro_rules! mat_is_symmetric( + (Mat2) => ( + self.elem(0, 1).approx_eq(self.elem(1, 0)) && + self.elem(1, 0).approx_eq(self.elem(0, 1)) + ); + (Mat3) => ( + self.elem(0, 1).approx_eq(self.elem(1, 0)) && + self.elem(0, 2).approx_eq(self.elem(2, 0)) && + + self.elem(1, 0).approx_eq(self.elem(0, 1)) && + self.elem(1, 2).approx_eq(self.elem(2, 1)) && + + self.elem(2, 0).approx_eq(self.elem(0, 2)) && + self.elem(2, 1).approx_eq(self.elem(1, 2)) + ); + (Mat4) => ( + self.elem(0, 1).approx_eq(self.elem(1, 0)) && + self.elem(0, 2).approx_eq(self.elem(2, 0)) && + self.elem(0, 3).approx_eq(self.elem(3, 0)) && + + self.elem(1, 0).approx_eq(self.elem(0, 1)) && + self.elem(1, 2).approx_eq(self.elem(2, 1)) && + self.elem(1, 3).approx_eq(self.elem(3, 1)) && + + self.elem(2, 0).approx_eq(self.elem(0, 2)) && + self.elem(2, 1).approx_eq(self.elem(1, 2)) && + self.elem(2, 3).approx_eq(self.elem(3, 2)) && + + self.elem(3, 0).approx_eq(self.elem(0, 3)) && + self.elem(3, 1).approx_eq(self.elem(1, 3)) && + self.elem(3, 2).approx_eq(self.elem(2, 3)) + ); +) + +macro_rules! impl_mat_neg( + ($Mat:ident) => ( + impl Neg<$Mat> for $Mat { + #[inline] + pub fn neg(&self) -> $Mat { + $Mat::from_slice(self.map(|&x| -x)) + } + } + ) +) diff --git a/src/projection.rs b/src/projection.rs index ee966ec..ca3c761 100644 --- a/src/projection.rs +++ b/src/projection.rs @@ -13,9 +13,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::num::{Zero, One}; use mat::Mat4; +mod macros; + /// /// Create a perspective projection matrix /// @@ -25,7 +26,7 @@ use mat::Mat4; /// can be found [here](http://www.opengl.org/wiki/GluPerspective_code). /// pub fn perspective(fovy: T, aspectRatio: T, near: T, far: T) -> Mat4 { - let ymax = near * (fovy / two::()).to_radians().tan(); + let ymax = near * (fovy / two!(T)).to_radians().tan(); let xmax = ymax * aspectRatio; frustum(-xmax, xmax, -ymax, ymax, near, far) @@ -38,25 +39,25 @@ pub fn perspective(fovy: T, aspectRatio: T, near: T, far: T) -> M /// (http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml) function. /// pub fn frustum(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4 { - let c0r0 = (two::() * near) / (right - left); - let c0r1 = Zero::zero(); - let c0r2 = Zero::zero(); - let c0r3 = Zero::zero(); + let c0r0 = (two!(T) * near) / (right - left); + let c0r1 = zero!(T); + let c0r2 = zero!(T); + let c0r3 = zero!(T); - let c1r0 = Zero::zero(); - let c1r1 = (two::() * near) / (top - bottom); - let c1r2 = Zero::zero(); - let c1r3 = Zero::zero(); + let c1r0 = zero!(T); + let c1r1 = (two!(T) * near) / (top - bottom); + let c1r2 = zero!(T); + let c1r3 = zero!(T); let c2r0 = (right + left) / (right - left); let c2r1 = (top + bottom) / (top - bottom); let c2r2 = -(far + near) / (far - near); - let c2r3 = -One::one::(); + let c2r3 = -one!(T); - let c3r0 = Zero::zero(); - let c3r1 = Zero::zero(); - let c3r2 = -(two::() * far * near) / (far - near); - let c3r3 = Zero::zero(); + let c3r0 = zero!(T); + let c3r1 = zero!(T); + let c3r2 = -(two!(T) * far * near) / (far - near); + let c3r3 = zero!(T); Mat4::new(c0r0, c0r1, c0r2, c0r3, c1r0, c1r1, c1r2, c1r3, @@ -71,34 +72,28 @@ pub fn frustum(left: T, right: T, bottom: T, top: T, near: T, far /// (http://www.opengl.org/sdk/docs/man2/xhtml/glOrtho.xml) function. /// pub fn ortho(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4 { - let c0r0 = two::() / (right - left); - let c0r1 = Zero::zero(); - let c0r2 = Zero::zero(); - let c0r3 = Zero::zero(); + let c0r0 = two!(T) / (right - left); + let c0r1 = zero!(T); + let c0r2 = zero!(T); + let c0r3 = zero!(T); - let c1r0 = Zero::zero(); - let c1r1 = two::() / (top - bottom); - let c1r2 = Zero::zero(); - let c1r3 = Zero::zero(); + let c1r0 = zero!(T); + let c1r1 = two!(T) / (top - bottom); + let c1r2 = zero!(T); + let c1r3 = zero!(T); - let c2r0 = Zero::zero(); - let c2r1 = Zero::zero(); - let c2r2 = -two::() / (far - near); - let c2r3 = Zero::zero(); + let c2r0 = zero!(T); + let c2r1 = zero!(T); + let c2r2 = -two!(T) / (far - near); + let c2r3 = zero!(T); let c3r0 = -(right + left) / (right - left); let c3r1 = -(top + bottom) / (top - bottom); let c3r2 = -(far + near) / (far - near); - let c3r3 = One::one(); + let c3r3 = one!(T); Mat4::new(c0r0, c0r1, c0r2, c0r3, c1r0, c1r1, c1r2, c1r3, c2r0, c2r1, c2r2, c2r3, c3r0, c3r1, c3r2, c3r3) } - -// FIXME: We can remove this once we have numeric conversions in std -#[inline] -priv fn two() -> T { - One::one::() + One::one::() -} diff --git a/src/quat.rs b/src/quat.rs index 82f94cc..55171c8 100644 --- a/src/quat.rs +++ b/src/quat.rs @@ -15,13 +15,12 @@ pub use super::Dimensional; -use std::cast::transmute; -use std::cmp::ApproxEq; -use std::num::{Zero, One, cast}; - +use std::num::cast; use mat::{Mat3, ToMat3}; use vec::Vec3; +mod macros; + // GLSL-style type aliases pub type quat = Quat; @@ -34,18 +33,14 @@ pub type Quatf32 = Quat; pub type Quatf64 = Quat; /// A quaternion in scalar/vector form -/// -/// # Type parameters -/// -/// - `T`: The type of the components. Should be a floating point type. -/// -/// # Fields -/// -/// - `s`: the scalar component -/// - `v`: a vector containing the three imaginary components #[deriving(Eq)] pub struct Quat { s: T, v: Vec3 } +impl_dimensional!(Quat, T, 4) +impl_dimensional_fns!(Quat, T, 4) +impl_swap!(Quat) +impl_approx!(Quat) + pub trait ToQuat { pub fn to_quat(&self) -> Quat; } @@ -77,98 +72,39 @@ impl Quat { } } -impl Dimensional for Quat { - #[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,..4] { - unsafe { transmute(self) } - } - - #[inline] - pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T,..4] { - unsafe { transmute(self) } - } - - #[inline] - pub fn map(&self, f: &fn(&T) -> T) -> Quat { - Quat::new(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)); - } -} - -impl Quat { - #[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; - } -} - impl Quat { /// The multiplicative identity, ie: `q = 1 + 0i + 0j + 0i` #[inline] pub fn identity() -> Quat { - Quat::from_sv(One::one(), Vec3::zero()) + Quat::from_sv(one!(T), Vec3::zero()) } /// The additive identity, ie: `q = 0 + 0i + 0j + 0i` #[inline] pub fn zero() -> Quat { - Quat::new(Zero::zero(), - Zero::zero(), - Zero::zero(), - Zero::zero()) + Quat::new(zero!(T), zero!(T), zero!(T), zero!(T)) } #[inline] pub fn from_angle_x(radians: T) -> Quat { - Quat::new((radians / two()).cos(), - radians.sin(), - Zero::zero(), - Zero::zero()) + Quat::new((radians / two!(T)).cos(), radians.sin(), zero!(T), zero!(T)) } #[inline] pub fn from_angle_y(radians: T) -> Quat { - Quat::new((radians / two()).cos(), - Zero::zero(), - radians.sin(), - Zero::zero()) + Quat::new((radians / two!(T)).cos(), zero!(T), radians.sin(), zero!(T)) } #[inline] pub fn from_angle_z(radians: T) -> Quat { - Quat::new((radians / two()).cos(), - Zero::zero(), - Zero::zero(), - radians.sin()) + Quat::new((radians / two!(T)).cos(), zero!(T), zero!(T), radians.sin()) } pub fn from_angle_xyz(radians_x: T, radians_y: T, radians_z: T) -> Quat { // http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles#Conversion - let xdiv2 = radians_x / two(); - let ydiv2 = radians_y / two(); - let zdiv2 = radians_z / two(); + let xdiv2 = radians_x / two!(T); + let ydiv2 = radians_y / two!(T); + let zdiv2 = radians_z / two!(T); Quat::new(zdiv2.cos() * xdiv2.cos() * ydiv2.cos() + zdiv2.sin() * xdiv2.sin() * ydiv2.sin(), zdiv2.sin() * xdiv2.cos() * ydiv2.cos() - zdiv2.cos() * xdiv2.sin() * ydiv2.sin(), zdiv2.cos() * xdiv2.sin() * ydiv2.cos() + zdiv2.sin() * xdiv2.cos() * ydiv2.sin(), @@ -177,7 +113,7 @@ impl Quat { #[inline] pub fn from_angle_axis(radians: T, axis: &Vec3) -> Quat { - let half = radians / two(); + let half = radians / two!(T); Quat::from_sv(half.cos(), axis.mul_t(half.sin())) } @@ -188,20 +124,20 @@ impl Quat { /// The result of multiplying the quaternion a scalar #[inline] pub fn mul_t(&self, value: T) -> Quat { - self.map(|&x| x * value) + Quat::from_slice(self.map(|&x| x * value)) } /// The result of dividing the quaternion a scalar #[inline] pub fn div_t(&self, value: T) -> Quat { - self.map(|&x| x / value) + Quat::from_slice(self.map(|&x| x / value)) } /// The result of multiplying the quaternion by a vector #[inline] pub fn mul_v(&self, vec: &Vec3) -> Vec3 { let tmp = self.v.cross(vec).add_v(&vec.mul_t(self.s)); - self.v.cross(&tmp).mul_t(two()).add_v(vec) + self.v.cross(&tmp).mul_t(two!(T)).add_v(vec) } /// The sum of this quaternion and `other` @@ -271,7 +207,7 @@ impl Quat { /// The normalized quaternion #[inline] pub fn normalize(&self) -> Quat { - self.mul_t(One::one::() / self.magnitude()) + self.mul_t(one!(T) / self.magnitude()) } /// Normalised linear interpolation @@ -280,7 +216,7 @@ impl Quat { /// /// The intoperlated quaternion pub fn nlerp(&self, other: &Quat, amount: T) -> Quat { - self.mul_t(One::one::() - amount).add_q(&other.mul_t(amount)).normalize() + self.mul_t(one!(T) - amount).add_q(&other.mul_t(amount)).normalize() } } @@ -303,7 +239,7 @@ impl ToMat3 for Quat { let sz2 = z2 * self.s; let sx2 = x2 * self.s; - let _1: T = One::one(); + let _1: T = one!(T); Mat3::new(_1 - yy2 - zz2, xy2 + sz2, xz2 - sy2, xy2 - sz2, _1 - xx2 - zz2, yz2 + sx2, @@ -314,7 +250,7 @@ impl ToMat3 for Quat { impl Neg> for Quat { #[inline] pub fn neg(&self) -> Quat { - self.map(|&x| -x) + Quat::from_slice(self.map(|&x| -x)) } } @@ -352,14 +288,15 @@ impl Quat { let dot = self.dot(other); let dot_threshold = cast(0.9995); + // if quaternions are close together use `nlerp` if dot > dot_threshold { - self.nlerp(other, amount) // if quaternions are close together use `nlerp` + self.nlerp(other, amount) } else { - let robust_dot = dot.clamp(&-One::one::(), - &One::one()); // stay within the domain of acos() + // stay within the domain of acos() + let robust_dot = dot.clamp(&-one!(T), &one!(T)); - let theta_0 = robust_dot.acos(); // the angle between the quaternions - let theta = theta_0 * amount; // the fraction of theta specified by `amount` + let theta_0 = robust_dot.acos(); // the angle between the quaternions + let theta = theta_0 * amount; // the fraction of theta specified by `amount` let q = other.sub_q(&self.mul_t(robust_dot)) .normalize(); @@ -370,32 +307,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.index(0).approx_eq_eps(other.index(0), epsilon) && - self.index(1).approx_eq_eps(other.index(1), epsilon) && - self.index(2).approx_eq_eps(other.index(2), epsilon) && - self.index(3).approx_eq_eps(other.index(3), epsilon) - } -} - -// FIXME: We can remove this once we have numeric conversions in std -#[inline] -priv fn two() -> T { - One::one::() + One::one::() -} - #[cfg(test)] mod tests { use mat::*; diff --git a/src/vec2.rs b/src/vec2.rs index 5694d2d..1d9dfa6 100644 --- a/src/vec2.rs +++ b/src/vec2.rs @@ -13,407 +13,38 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::cast::transmute; -use std::cmp::ApproxEq; -use std::num::{Zero, One}; +pub use super::Dimensional; -use super::Dimensional; - -use super::Vec3; +mod macros; +mod vec_macros; #[deriving(Eq)] pub struct Vec2 { x: T, y: T } -impl Vec2 { - #[inline] - pub fn new(x: T, y: T ) -> Vec2 { - Vec2 { x: x, y: y } - } -} +impl_dimensional!(Vec2, T, 2) +impl_dimensional_fns!(Vec2, T, 2) +impl_swap!(Vec2) +impl_approx!(Vec2) -impl Dimensional for Vec2 { - #[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,..2] { - unsafe { transmute(self) } - } - - #[inline] - pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T,..2] { - unsafe { transmute(self) } - } - - #[inline(always)] - pub fn map(&self, f: &fn(&T) -> T) -> Vec2 { - Vec2::new(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)); - } -} - -impl Vec2 { - #[inline] - pub fn from_value(value: T) -> Vec2 { - Vec2::new(value, value) - } - - #[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; - } -} +impl_vec!(Vec2 { x, y }) +impl_vec_copyable!(Vec2) +impl_vec_numeric!(Vec2) +impl_vec_neg!(Vec2) +impl_vec_euclidean!(Vec2) +impl_vec_ord!(Vec2) +impl_vec_eq!(Vec2) +impl_vec_bool!(Vec2) +impl_vec_not!(Vec2) impl Vec2 { - #[inline] - pub fn identity() -> Vec2 { - Vec2::new(One::one::(), One::one::()) - } - - #[inline] - pub fn zero() -> Vec2 { - Vec2::new(Zero::zero::(), Zero::zero::()) - } - - #[inline] - pub fn unit_x() -> Vec2 { - Vec2::new(One::one::(), Zero::zero::()) - } - - #[inline] - pub fn unit_y() -> Vec2 { - Vec2::new(Zero::zero::(), One::one::()) - } - - #[inline] - pub fn is_zero(&self) -> bool { - *self.index(0) == Zero::zero() && - *self.index(1) == Zero::zero() - } - - #[inline] - pub fn add_t(&self, value: T) -> Vec2 { - self.map(|&x| x + value) - } - - #[inline] - pub fn sub_t(&self, value: T) -> Vec2 { - self.map(|&x| x - value) - } - - #[inline] - pub fn mul_t(&self, value: T) -> Vec2 { - self.map(|&x| x * value) - } - - #[inline] - pub fn div_t(&self, value: T) -> Vec2 { - self.map(|&x| x / value) - } - - #[inline] - pub fn rem_t(&self, value: T) -> Vec2 { - self.map(|&x| x % value) - } - - #[inline] - pub fn add_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) + *other.index(0), - *self.index(1) + *other.index(1)) - } - - #[inline] - pub fn sub_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) - *other.index(0), - *self.index(1) - *other.index(1)) - } - - #[inline] - pub fn mul_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) * *other.index(0), - *self.index(1) * *other.index(1)) - } - - #[inline] - pub fn div_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) / *other.index(0), - *self.index(1) / *other.index(1)) - } - - #[inline] - pub fn rem_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) % *other.index(0), - *self.index(1) % *other.index(1)) - } - - #[inline] - pub fn neg_self(&mut self) { - self.map_mut(|x| *x = -*x) - } - - #[inline] - pub fn add_self_t(&mut self, value: T) { - self.map_mut(|x| *x += value) - } - - #[inline] - pub fn sub_self_t(&mut self, value: T) { - self.map_mut(|x| *x -= value) - } - - #[inline] - pub fn mul_self_t(&mut self, value: T) { - self.map_mut(|x| *x *= value) - } - - #[inline] - pub fn div_self_t(&mut self, value: T) { - self.map_mut(|x| *x /= value) - } - - #[inline] - pub fn rem_self_t(&mut self, value: T) { - self.map_mut(|x| *x %= value) - } - - #[inline] - pub fn add_self_v(&mut self, other: &Vec2) { - *self.index_mut(0) += *other.index(0); - *self.index_mut(1) += *other.index(1); - } - - #[inline] - pub fn sub_self_v(&mut self, other: &Vec2) { - *self.index_mut(0) -= *other.index(0); - *self.index_mut(1) -= *other.index(1); - } - - #[inline] - pub fn mul_self_v(&mut self, other: &Vec2) { - *self.index_mut(0) *= *other.index(0); - *self.index_mut(1) *= *other.index(1); - } - - #[inline] - pub fn div_self_v(&mut self, other: &Vec2) { - *self.index_mut(0) /= *other.index(0); - *self.index_mut(1) /= *other.index(1); - } - - #[inline] - pub fn rem_self_v(&mut self, other: &Vec2) { - *self.index_mut(0) /= *other.index(0); - *self.index_mut(1) /= *other.index(1); - } - - #[inline] - pub fn dot(&self, other: &Vec2) -> T { - *self.index(0) * *other.index(0) + - *self.index(1) * *other.index(1) - } + #[inline] pub fn unit_x() -> Vec2 { Vec2::new(one!(T), zero!(T)) } + #[inline] pub fn unit_y() -> Vec2 { Vec2::new(zero!(T), one!(T)) } #[inline] pub fn perp_dot(&self, other: &Vec2) -> T { (*self.index(0) * *other.index(1)) - (*self.index(1) * *other.index(0)) } - - #[inline] - pub fn to_homogeneous(&self) -> Vec3 { - Vec3::new(self.x, self.y, Zero::zero()) - } -} - -impl Neg> for Vec2 { - #[inline] - pub fn neg(&self) -> Vec2 { - self.map(|&x| -x) - } -} - -impl Vec2 { - #[inline] - pub fn length2(&self) -> T { - self.dot(self) - } - - #[inline] - pub fn length(&self) -> T { - self.length2().sqrt() - } - - #[inline] - pub fn distance2(&self, other: &Vec2) -> T { - other.sub_v(self).length2() - } - - #[inline] - pub fn distance(&self, other: &Vec2) -> T { - other.distance2(self).sqrt() - } - - #[inline] - pub fn angle(&self, other: &Vec2) -> T { - self.perp_dot(other).atan2(&self.dot(other)) - } - - #[inline] - pub fn normalize(&self) -> Vec2 { - self.mul_t(One::one::()/self.length()) - } - - #[inline] - pub fn normalize_to(&self, length: T) -> Vec2 { - self.mul_t(length / self.length()) - } - - #[inline] - pub fn lerp(&self, other: &Vec2, amount: T) -> Vec2 { - self.add_v(&other.sub_v(self).mul_t(amount)) - } - - #[inline] - pub fn normalize_self(&mut self) { - let n = One::one::() / self.length(); - self.mul_self_t(n); - } - - #[inline] - pub fn normalize_self_to(&mut self, length: T) { - let n = length / self.length(); - self.mul_self_t(n); - } - - pub fn lerp_self(&mut self, other: &Vec2, amount: T) { - let v = other.sub_v(self).mul_t(amount); - self.add_self_v(&v); - } -} - -impl> ApproxEq for Vec2 { - #[inline] - pub fn approx_epsilon() -> T { - ApproxEq::approx_epsilon::() - } - - #[inline] - pub fn approx_eq(&self, other: &Vec2) -> bool { - self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) - } - - #[inline] - pub fn approx_eq_eps(&self, other: &Vec2, epsilon: &T) -> bool { - self.index(0).approx_eq_eps(other.index(0), epsilon) && - self.index(1).approx_eq_eps(other.index(1), epsilon) - } -} - -impl Vec2 { - #[inline] - pub fn lt_t(&self, value: T) -> Vec2 { - Vec2::new(*self.index(0) < value, - *self.index(1) < value) - } - - #[inline] - pub fn le_t(&self, value: T) -> Vec2 { - Vec2::new(*self.index(0) <= value, - *self.index(1) <= value) - } - - #[inline] - pub fn ge_t(&self, value: T) -> Vec2 { - Vec2::new(*self.index(0) >= value, - *self.index(1) >= value) - } - - #[inline] - pub fn gt_t(&self, value: T) -> Vec2 { - Vec2::new(*self.index(0) > value, - *self.index(1) > value) - } - - #[inline] - pub fn lt_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) < *other.index(0), - *self.index(1) < *other.index(1)) - } - - #[inline] - pub fn le_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) <= *other.index(0), - *self.index(1) <= *other.index(1)) - } - - #[inline] - pub fn ge_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) >= *other.index(0), - *self.index(1) >= *other.index(1)) - } - - #[inline] - pub fn gt_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) > *other.index(0), - *self.index(1) > *other.index(1)) - } -} - -impl Vec2 { - #[inline] - pub fn eq_t(&self, value: T) -> Vec2 { - Vec2::new(*self.index(0) == value, - *self.index(1) == value) - } - - #[inline] - pub fn ne_t(&self, value: T) -> Vec2 { - Vec2::new(*self.index(0) != value, - *self.index(1) != value) - } - - #[inline] - pub fn eq_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) == *other.index(0), - *self.index(1) == *other.index(1)) - } - - #[inline] - pub fn ne_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) != *other.index(0), - *self.index(1) != *other.index(1)) - } -} - -impl Vec2 { - #[inline] - pub fn any(&self) -> bool { - *self.index(0) || *self.index(1) - } - - #[inline] - pub fn all(&self) -> bool { - *self.index(0) && *self.index(1) - } - - #[inline] - pub fn not(&self) -> Vec2 { - self.map(|&x| !x) - } } #[cfg(test)] @@ -455,9 +86,6 @@ mod tests { assert_eq!(-a, Vec2::new::(-1.0, -2.0)); assert_eq!(a.neg(), Vec2::new::(-1.0, -2.0)); - assert!(Vec2::new::(0.0, 0.0).is_zero()); - assert!(!Vec2::new::(1.0, 1.0).is_zero()); - assert_eq!(a.mul_t(f1), Vec2::new::( 1.5, 3.0)); assert_eq!(a.div_t(f2), Vec2::new::( 2.0, 4.0)); diff --git a/src/vec3.rs b/src/vec3.rs index bda3878..3712428 100644 --- a/src/vec3.rs +++ b/src/vec3.rs @@ -13,237 +13,33 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::cast::transmute; -use std::cmp::ApproxEq; -use std::num::{Zero, One}; +pub use super::Dimensional; -use super::Dimensional; -use super::Vec4; +mod macros; +mod vec_macros; #[deriving(Eq)] pub struct Vec3 { x: T, y: T, z: T } -impl Vec3 { - #[inline] - pub fn new(x: T, y: T, z: T ) -> Vec3 { - Vec3 { x: x, y: y, z: z } - } -} +impl_dimensional!(Vec3, T, 3) +impl_dimensional_fns!(Vec3, T, 3) +impl_swap!(Vec3) +impl_approx!(Vec3) -impl Dimensional for Vec3 { - #[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,..3] { - unsafe { transmute(self) } - } - - #[inline] - pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T,..3] { - unsafe { transmute(self) } - } - - #[inline(always)] - pub fn map(&self, f: &fn(&T) -> T) -> Vec3 { - Vec3::new(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)); - } -} - -impl Vec3 { - #[inline] - pub fn from_value(value: T) -> Vec3 { - Vec3::new(value, value, value) - } - - #[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; - } -} +impl_vec!(Vec3 { x, y, z }) +impl_vec_copyable!(Vec3) +impl_vec_numeric!(Vec3) +impl_vec_neg!(Vec3) +impl_vec_euclidean!(Vec3) +impl_vec_ord!(Vec3) +impl_vec_eq!(Vec3) +impl_vec_bool!(Vec3) +impl_vec_not!(Vec3) impl Vec3 { - #[inline] - pub fn identity() -> Vec3 { - Vec3::new(One::one::(), One::one::(), One::one::()) - } - - #[inline] - pub fn zero() -> Vec3 { - Vec3::new(Zero::zero::(), Zero::zero::(), Zero::zero::()) - } - - #[inline] - pub fn unit_x() -> Vec3 { - Vec3::new(One::one::(), Zero::zero::(), Zero::zero::()) - } - - #[inline] - pub fn unit_y() -> Vec3 { - Vec3::new(Zero::zero::(), One::one::(), Zero::zero::()) - } - - #[inline] - pub fn unit_z() -> Vec3 { - Vec3::new(Zero::zero::(), Zero::zero::(), One::one::()) - } - - #[inline] - pub fn is_zero(&self) -> bool { - *self.index(0) == Zero::zero() && - *self.index(1) == Zero::zero() && - *self.index(2) == Zero::zero() - } - - #[inline] - pub fn add_t(&self, value: T) -> Vec3 { - self.map(|&x| x + value) - } - - #[inline] - pub fn sub_t(&self, value: T) -> Vec3 { - self.map(|&x| x - value) - } - - #[inline] - pub fn mul_t(&self, value: T) -> Vec3 { - self.map(|&x| x * value) - } - - #[inline] - pub fn div_t(&self, value: T) -> Vec3 { - self.map(|&x| x / value) - } - - #[inline] - pub fn rem_t(&self, value: T) -> Vec3 { - self.map(|&x| x % value) - } - - #[inline] - pub fn add_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) + *other.index(0), - *self.index(1) + *other.index(1), - *self.index(2) + *other.index(2)) - } - - #[inline] - pub fn sub_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) - *other.index(0), - *self.index(1) - *other.index(1), - *self.index(2) - *other.index(2)) - } - - #[inline] - pub fn mul_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) * *other.index(0), - *self.index(1) * *other.index(1), - *self.index(2) * *other.index(2)) - } - - #[inline] - pub fn div_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) / *other.index(0), - *self.index(1) / *other.index(1), - *self.index(2) / *other.index(2)) - } - - #[inline] - pub fn rem_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) % *other.index(0), - *self.index(1) % *other.index(1), - *self.index(2) % *other.index(2)) - } - - #[inline] - pub fn neg_self(&mut self) { - self.map_mut(|x| *x = -*x) - } - - #[inline] - pub fn add_self_t(&mut self, value: T) { - self.map_mut(|x| *x += value) - } - - #[inline] - pub fn sub_self_t(&mut self, value: T) { - self.map_mut(|x| *x -= value) - } - - #[inline] - pub fn mul_self_t(&mut self, value: T) { - self.map_mut(|x| *x *= value) - } - - #[inline] - pub fn div_self_t(&mut self, value: T) { - self.map_mut(|x| *x /= value) - } - - #[inline] - pub fn rem_self_t(&mut self, value: T) { - self.map_mut(|x| *x %= value) - } - - #[inline] - pub fn add_self_v(&mut self, other: &Vec3) { - *self.index_mut(0) += *other.index(0); - *self.index_mut(1) += *other.index(1); - *self.index_mut(2) += *other.index(2); - } - - #[inline] - pub fn sub_self_v(&mut self, other: &Vec3) { - *self.index_mut(0) -= *other.index(0); - *self.index_mut(1) -= *other.index(1); - *self.index_mut(2) -= *other.index(2); - } - - #[inline] - pub fn mul_self_v(&mut self, other: &Vec3) { - *self.index_mut(0) *= *other.index(0); - *self.index_mut(1) *= *other.index(1); - *self.index_mut(2) *= *other.index(2); - } - - #[inline] - pub fn div_self_v(&mut self, other: &Vec3) { - *self.index_mut(0) /= *other.index(0); - *self.index_mut(1) /= *other.index(1); - *self.index_mut(2) /= *other.index(2); - } - - #[inline] - pub fn rem_self_v(&mut self, other: &Vec3) { - *self.index_mut(0) /= *other.index(0); - *self.index_mut(1) /= *other.index(1); - *self.index_mut(2) /= *other.index(2); - } - - #[inline] - pub fn dot(&self, other: &Vec3) -> T { - *self.index(0) * *other.index(0) + - *self.index(1) * *other.index(1) + - *self.index(2) * *other.index(2) - } + #[inline] pub fn unit_x() -> Vec3 { Vec3::new(one!(T), zero!(T), zero!(T)) } + #[inline] pub fn unit_y() -> Vec3 { Vec3::new(zero!(T), one!(T), zero!(T)) } + #[inline] pub fn unit_z() -> Vec3 { Vec3::new(zero!(T), zero!(T), one!(T)) } #[inline] pub fn cross(&self, other: &Vec3) -> Vec3 { @@ -256,201 +52,6 @@ impl Vec3 { pub fn cross_self(&mut self, other: &Vec3) { *self = self.cross(other) } - - #[inline] - pub fn to_homogeneous(&self) -> Vec4 { - Vec4::new(self.x, self.y, self.z, Zero::zero()) - } -} - -impl Neg> for Vec3 { - #[inline] - pub fn neg(&self) -> Vec3 { - self.map(|&x| -x) - } -} - -impl Vec3 { - #[inline] - pub fn length2(&self) -> T { - self.dot(self) - } - - #[inline] - pub fn length(&self) -> T { - self.length2().sqrt() - } - - #[inline] - pub fn distance2(&self, other: &Vec3) -> T { - other.sub_v(self).length2() - } - - #[inline] - pub fn distance(&self, other: &Vec3) -> T { - other.distance2(self).sqrt() - } - - #[inline] - pub fn angle(&self, other: &Vec3) -> T { - self.cross(other).length().atan2(&self.dot(other)) - } - - #[inline] - pub fn normalize(&self) -> Vec3 { - self.mul_t(One::one::()/self.length()) - } - - #[inline] - pub fn normalize_to(&self, length: T) -> Vec3 { - self.mul_t(length / self.length()) - } - - #[inline] - pub fn lerp(&self, other: &Vec3, amount: T) -> Vec3 { - self.add_v(&other.sub_v(self).mul_t(amount)) - } - - #[inline] - pub fn normalize_self(&mut self) { - let n = One::one::() / self.length(); - self.mul_self_t(n); - } - - #[inline] - pub fn normalize_self_to(&mut self, length: T) { - let n = length / self.length(); - self.mul_self_t(n); - } - - pub fn lerp_self(&mut self, other: &Vec3, amount: T) { - let v = other.sub_v(self).mul_t(amount); - self.add_self_v(&v); - } -} - -impl> ApproxEq for Vec3 { - #[inline] - pub fn approx_epsilon() -> T { - ApproxEq::approx_epsilon::() - } - - #[inline] - pub fn approx_eq(&self, other: &Vec3) -> bool { - self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) - } - - #[inline] - pub fn approx_eq_eps(&self, other: &Vec3, epsilon: &T) -> bool { - self.index(0).approx_eq_eps(other.index(0), epsilon) && - self.index(1).approx_eq_eps(other.index(1), epsilon) && - self.index(2).approx_eq_eps(other.index(2), epsilon) - } -} - -impl Vec3 { - #[inline] - pub fn lt_t(&self, value: T) -> Vec3 { - Vec3::new(*self.index(0) < value, - *self.index(1) < value, - *self.index(2) < value) - } - - #[inline] - pub fn le_t(&self, value: T) -> Vec3 { - Vec3::new(*self.index(0) <= value, - *self.index(1) <= value, - *self.index(2) <= value) - } - - #[inline] - pub fn ge_t(&self, value: T) -> Vec3 { - Vec3::new(*self.index(0) >= value, - *self.index(1) >= value, - *self.index(2) >= value) - } - - #[inline] - pub fn gt_t(&self, value: T) -> Vec3 { - Vec3::new(*self.index(0) > value, - *self.index(1) > value, - *self.index(2) > value) - } - - #[inline] - pub fn lt_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) < *other.index(0), - *self.index(1) < *other.index(1), - *self.index(2) < *other.index(2)) - } - - #[inline] - pub fn le_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) <= *other.index(0), - *self.index(1) <= *other.index(1), - *self.index(2) <= *other.index(2)) - } - - #[inline] - pub fn ge_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) >= *other.index(0), - *self.index(1) >= *other.index(1), - *self.index(2) >= *other.index(2)) - } - - #[inline] - pub fn gt_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) > *other.index(0), - *self.index(1) > *other.index(1), - *self.index(2) > *other.index(2)) - } -} - -impl Vec3 { - #[inline] - pub fn eq_t(&self, value: T) -> Vec3 { - Vec3::new(*self.index(0) == value, - *self.index(1) == value, - *self.index(2) == value) - } - - #[inline] - pub fn ne_t(&self, value: T) -> Vec3 { - Vec3::new(*self.index(0) != value, - *self.index(1) != value, - *self.index(2) != value) - } - - #[inline] - pub fn eq_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) == *other.index(0), - *self.index(1) == *other.index(1), - *self.index(2) == *other.index(2)) - } - - #[inline] - pub fn ne_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) != *other.index(0), - *self.index(1) != *other.index(1), - *self.index(2) != *other.index(2)) - } -} - -impl Vec3 { - #[inline] - pub fn any(&self) -> bool { - *self.index(0) || *self.index(1) || *self.index(2) - } - - #[inline] - pub fn all(&self) -> bool { - *self.index(0) && *self.index(1) && *self.index(2) - } - - #[inline] - pub fn not(&self) -> Vec3 { - self.map(|&x| !x) - } } #[cfg(test)] @@ -507,9 +108,6 @@ mod tests{ assert_eq!(-a, Vec3::new::(-1.0, -2.0, -3.0)); assert_eq!(a.neg(), Vec3::new::(-1.0, -2.0, -3.0)); - assert!(Vec3::new::(0.0, 0.0, 0.0).is_zero()); - assert!(!Vec3::new::(1.0, 1.0, 1.0).is_zero()); - assert_eq!(a.mul_t(f1), Vec3::new::( 1.5, 3.0, 4.5)); assert_eq!(a.div_t(f2), Vec3::new::( 2.0, 4.0, 6.0)); diff --git a/src/vec4.rs b/src/vec4.rs index 9954090..c15c4cc 100644 --- a/src/vec4.rs +++ b/src/vec4.rs @@ -13,456 +13,34 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::cast::transmute; -use std::cmp::ApproxEq; -use std::num::{Zero, One}; +pub use super::Dimensional; -use super::Dimensional; +mod macros; +mod vec_macros; #[deriving(Eq)] pub struct Vec4 { x: T, y: T, z: T, w: T } -impl Vec4 { - #[inline] - pub fn new(x: T, y: T, z: T, w: T ) -> Vec4 { - Vec4 { x: x, y: y, z: z, w: w } - } -} +impl_dimensional!(Vec4, T, 4) +impl_dimensional_fns!(Vec4, T, 4) +impl_approx!(Vec4) +impl_swap!(Vec4) -impl Dimensional for Vec4 { - #[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,..4] { - unsafe { transmute(self) } - } - - #[inline] - pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T,..4] { - unsafe { transmute(self) } - } - - #[inline(always)] - pub fn map(&self, f: &fn(&T) -> T) -> Vec4 { - Vec4::new(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)); - } -} - -impl Vec4 { - #[inline] - pub fn from_value(value: T) -> Vec4 { - Vec4::new(value, value, value, value) - } - - #[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; - } -} +impl_vec!(Vec4 { x, y, z, w }) +impl_vec_copyable!(Vec4) +impl_vec_numeric!(Vec4) +impl_vec_neg!(Vec4) +impl_vec_euclidean!(Vec4) +impl_vec_ord!(Vec4) +impl_vec_eq!(Vec4) +impl_vec_bool!(Vec4) +impl_vec_not!(Vec4) impl Vec4 { - #[inline] - pub fn identity() -> Vec4 { - Vec4::new(One::one::(), One::one::(), One::one::(), One::one::()) - } - - #[inline] - pub fn zero() -> Vec4 { - Vec4::new(Zero::zero::(), Zero::zero::(), Zero::zero::(), Zero::zero::()) - } - - #[inline] - pub fn unit_x() -> Vec4 { - Vec4::new(One::one::(), Zero::zero::(), Zero::zero::(), Zero::zero::()) - } - - #[inline] - pub fn unit_y() -> Vec4 { - Vec4::new(Zero::zero::(), One::one::(), Zero::zero::(), Zero::zero::()) - } - - #[inline] - pub fn unit_z() -> Vec4 { - Vec4::new(Zero::zero::(), Zero::zero::(), One::one::(), Zero::zero::()) - } - - #[inline] - pub fn unit_w() -> Vec4 { - Vec4::new(Zero::zero::(), Zero::zero::(), Zero::zero::(), One::one::()) - } - - #[inline] - pub fn is_zero(&self) -> bool { - *self.index(0) == Zero::zero() && - *self.index(1) == Zero::zero() && - *self.index(2) == Zero::zero() && - *self.index(3) == Zero::zero() - } - - #[inline] - pub fn add_t(&self, value: T) -> Vec4 { - self.map(|&x| x + value) - } - - #[inline] - pub fn sub_t(&self, value: T) -> Vec4 { - self.map(|&x| x - value) - } - - #[inline] - pub fn mul_t(&self, value: T) -> Vec4 { - self.map(|&x| x * value) - } - - #[inline] - pub fn div_t(&self, value: T) -> Vec4 { - self.map(|&x| x / value) - } - - #[inline] - pub fn rem_t(&self, value: T) -> Vec4 { - self.map(|&x| x % value) - } - - #[inline] - pub fn add_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) + *other.index(0), - *self.index(1) + *other.index(1), - *self.index(2) + *other.index(2), - *self.index(3) + *other.index(3)) - } - - #[inline] - pub fn sub_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) - *other.index(0), - *self.index(1) - *other.index(1), - *self.index(2) - *other.index(2), - *self.index(3) - *other.index(3)) - } - - #[inline] - pub fn mul_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) * *other.index(0), - *self.index(1) * *other.index(1), - *self.index(2) * *other.index(2), - *self.index(3) * *other.index(3)) - } - - #[inline] - pub fn div_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) / *other.index(0), - *self.index(1) / *other.index(1), - *self.index(2) / *other.index(2), - *self.index(3) / *other.index(3)) - } - - #[inline] - pub fn rem_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) % *other.index(0), - *self.index(1) % *other.index(1), - *self.index(2) % *other.index(2), - *self.index(3) % *other.index(3)) - } - - #[inline] - pub fn neg_self(&mut self) { - self.map_mut(|x| *x = -*x) - } - - #[inline] - pub fn add_self_t(&mut self, value: T) { - self.map_mut(|x| *x += value) - } - - #[inline] - pub fn sub_self_t(&mut self, value: T) { - self.map_mut(|x| *x -= value) - } - - #[inline] - pub fn mul_self_t(&mut self, value: T) { - self.map_mut(|x| *x *= value) - } - - #[inline] - pub fn div_self_t(&mut self, value: T) { - self.map_mut(|x| *x /= value) - } - - #[inline] - pub fn rem_self_t(&mut self, value: T) { - self.map_mut(|x| *x %= value) - } - - #[inline] - pub fn add_self_v(&mut self, other: &Vec4) { - *self.index_mut(0) += *other.index(0); - *self.index_mut(1) += *other.index(1); - *self.index_mut(2) += *other.index(2); - *self.index_mut(3) += *other.index(3); - } - - #[inline] - pub fn sub_self_v(&mut self, other: &Vec4) { - *self.index_mut(0) -= *other.index(0); - *self.index_mut(1) -= *other.index(1); - *self.index_mut(2) -= *other.index(2); - *self.index_mut(3) -= *other.index(3); - } - - #[inline] - pub fn mul_self_v(&mut self, other: &Vec4) { - *self.index_mut(0) *= *other.index(0); - *self.index_mut(1) *= *other.index(1); - *self.index_mut(2) *= *other.index(2); - *self.index_mut(3) *= *other.index(3); - } - - #[inline] - pub fn div_self_v(&mut self, other: &Vec4) { - *self.index_mut(0) /= *other.index(0); - *self.index_mut(1) /= *other.index(1); - *self.index_mut(2) /= *other.index(2); - *self.index_mut(3) /= *other.index(3); - } - - #[inline] - pub fn rem_self_v(&mut self, other: &Vec4) { - *self.index_mut(0) /= *other.index(0); - *self.index_mut(1) /= *other.index(1); - *self.index_mut(2) /= *other.index(2); - *self.index_mut(3) /= *other.index(3); - } - - #[inline] - pub fn dot(&self, other: &Vec4) -> T { - *self.index(0) * *other.index(0) + - *self.index(1) * *other.index(1) + - *self.index(2) * *other.index(2) + - *self.index(3) * *other.index(3) - } -} - -impl Neg> for Vec4 { - #[inline] - pub fn neg(&self) -> Vec4 { - self.map(|&x| -x) - } -} - -impl Vec4 { - #[inline] - pub fn length2(&self) -> T { - self.dot(self) - } - - #[inline] - pub fn length(&self) -> T { - self.length2().sqrt() - } - - #[inline] - pub fn distance2(&self, other: &Vec4) -> T { - other.sub_v(self).length2() - } - - #[inline] - pub fn distance(&self, other: &Vec4) -> T { - other.distance2(self).sqrt() - } - - #[inline] - pub fn angle(&self, other: &Vec4) -> T { - (self.dot(other) / (self.length() * other.length())).acos() - } - - #[inline] - pub fn normalize(&self) -> Vec4 { - self.mul_t(One::one::()/self.length()) - } - - #[inline] - pub fn normalize_to(&self, length: T) -> Vec4 { - self.mul_t(length / self.length()) - } - - #[inline] - pub fn lerp(&self, other: &Vec4, amount: T) -> Vec4 { - self.add_v(&other.sub_v(self).mul_t(amount)) - } - - #[inline] - pub fn normalize_self(&mut self) { - let n = One::one::() / self.length(); - self.mul_self_t(n); - } - - #[inline] - pub fn normalize_self_to(&mut self, length: T) { - let n = length / self.length(); - self.mul_self_t(n); - } - - pub fn lerp_self(&mut self, other: &Vec4, amount: T) { - let v = other.sub_v(self).mul_t(amount); - self.add_self_v(&v); - } -} - -impl> ApproxEq for Vec4 { - #[inline] - pub fn approx_epsilon() -> T { - ApproxEq::approx_epsilon::() - } - - #[inline] - pub fn approx_eq(&self, other: &Vec4) -> bool { - self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) - } - - #[inline] - pub fn approx_eq_eps(&self, other: &Vec4, epsilon: &T) -> bool { - self.index(0).approx_eq_eps(other.index(0), epsilon) && - self.index(1).approx_eq_eps(other.index(1), epsilon) && - self.index(2).approx_eq_eps(other.index(2), epsilon) && - self.index(3).approx_eq_eps(other.index(3), epsilon) - } -} - -impl Vec4 { - #[inline] - pub fn lt_t(&self, value: T) -> Vec4 { - Vec4::new(*self.index(0) < value, - *self.index(1) < value, - *self.index(2) < value, - *self.index(3) < value) - } - - #[inline] - pub fn le_t(&self, value: T) -> Vec4 { - Vec4::new(*self.index(0) <= value, - *self.index(1) <= value, - *self.index(2) <= value, - *self.index(3) <= value) - } - - #[inline] - pub fn ge_t(&self, value: T) -> Vec4 { - Vec4::new(*self.index(0) >= value, - *self.index(1) >= value, - *self.index(2) >= value, - *self.index(3) >= value) - } - - #[inline] - pub fn gt_t(&self, value: T) -> Vec4 { - Vec4::new(*self.index(0) > value, - *self.index(1) > value, - *self.index(2) > value, - *self.index(3) > value) - } - - #[inline] - pub fn lt_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) < *other.index(0), - *self.index(1) < *other.index(1), - *self.index(2) < *other.index(2), - *self.index(3) < *other.index(3)) - } - - #[inline] - pub fn le_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) <= *other.index(0), - *self.index(1) <= *other.index(1), - *self.index(2) <= *other.index(2), - *self.index(3) <= *other.index(3)) - } - - #[inline] - pub fn ge_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) >= *other.index(0), - *self.index(1) >= *other.index(1), - *self.index(2) >= *other.index(2), - *self.index(3) >= *other.index(3)) - } - - #[inline] - pub fn gt_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) > *other.index(0), - *self.index(1) > *other.index(1), - *self.index(2) > *other.index(2), - *self.index(3) > *other.index(3)) - } -} - -impl Vec4 { - #[inline] - pub fn eq_t(&self, value: T) -> Vec4 { - Vec4::new(*self.index(0) == value, - *self.index(1) == value, - *self.index(2) == value, - *self.index(3) == value) - } - - #[inline] - pub fn ne_t(&self, value: T) -> Vec4 { - Vec4::new(*self.index(0) != value, - *self.index(1) != value, - *self.index(2) != value, - *self.index(3) != value) - } - - #[inline] - pub fn eq_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) == *other.index(0), - *self.index(1) == *other.index(1), - *self.index(2) == *other.index(2), - *self.index(3) == *other.index(3)) - } - - #[inline] - pub fn ne_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) != *other.index(0), - *self.index(1) != *other.index(1), - *self.index(2) != *other.index(2), - *self.index(3) != *other.index(3)) - } -} - -impl Vec4 { - #[inline] - pub fn any(&self) -> bool { - *self.index(0) || *self.index(1) || *self.index(2) || *self.index(3) - } - - #[inline] - pub fn all(&self) -> bool { - *self.index(0) && *self.index(1) && *self.index(2) && *self.index(3) - } - - #[inline] - pub fn not(&self) -> Vec4 { self.map(|&x| !x) } + #[inline] pub fn unit_x() -> Vec4 { Vec4::new(one!(T), zero!(T), zero!(T), zero!(T)) } + #[inline] pub fn unit_y() -> Vec4 { Vec4::new(zero!(T), one!(T), zero!(T), zero!(T)) } + #[inline] pub fn unit_z() -> Vec4 { Vec4::new(zero!(T), zero!(T), one!(T), zero!(T)) } + #[inline] pub fn unit_w() -> Vec4 { Vec4::new(zero!(T), zero!(T), zero!(T), one!(T)) } } #[cfg(test)] @@ -517,9 +95,6 @@ mod tests { assert_eq!(-a, Vec4::new::(-1.0, -2.0, -3.0, -4.0)); assert_eq!(a.neg(), Vec4::new::(-1.0, -2.0, -3.0, -4.0)); - assert!(Vec4::new::(0.0, 0.0, 0.0, 0.0).is_zero()); - assert!(!Vec4::new::(1.0, 1.0, 1.0, 1.0).is_zero()); - assert_eq!(a.mul_t(f1), Vec4::new::( 1.5, 3.0, 4.5, 6.0)); assert_eq!(a.div_t(f2), Vec4::new::( 2.0, 4.0, 6.0, 8.0)); diff --git a/src/vec_macros.rs b/src/vec_macros.rs new file mode 100644 index 0000000..b70c3dd --- /dev/null +++ b/src/vec_macros.rs @@ -0,0 +1,245 @@ +// 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_vec( + ($Vec:ident { $($field:ident),+ }) => ( + impl $Vec { + #[inline] + pub fn new($($field: T),+) -> $Vec { + $Vec { $($field: $field),+ } + } + } + ) +) + +macro_rules! impl_vec_copyable( + ($Vec:ident) => ( + impl $Vec { + #[inline] + pub fn from_value(value: T) -> $Vec { + vec_from_value!($Vec) + } + } + ) +) + +macro_rules! vec_from_value( + (Vec2) => (Vec2::new(value, value)); + (Vec3) => (Vec3::new(value, value, value)); + (Vec4) => (Vec4::new(value, value, value, value)); +) + +macro_rules! impl_vec_numeric( + ($Vec:ident) => ( + impl $Vec { + #[inline] pub fn identity() -> $Vec { $Vec::from_value(one!(T)) } + #[inline] pub fn zero() -> $Vec { $Vec::from_value(zero!(T)) } + + #[inline] pub fn add_t(&self, value: T) -> $Vec { $Vec::from_slice(self.map(|&x| x + value)) } + #[inline] pub fn sub_t(&self, value: T) -> $Vec { $Vec::from_slice(self.map(|&x| x - value)) } + #[inline] pub fn mul_t(&self, value: T) -> $Vec { $Vec::from_slice(self.map(|&x| x * value)) } + #[inline] pub fn div_t(&self, value: T) -> $Vec { $Vec::from_slice(self.map(|&x| x / value)) } + #[inline] pub fn rem_t(&self, value: T) -> $Vec { $Vec::from_slice(self.map(|&x| x % value)) } + + #[inline] pub fn add_v(&self, other: &$Vec) -> $Vec { $Vec::from_slice(self.zip(other, |&a, &b| a + b)) } + #[inline] pub fn sub_v(&self, other: &$Vec) -> $Vec { $Vec::from_slice(self.zip(other, |&a, &b| a - b)) } + #[inline] pub fn mul_v(&self, other: &$Vec) -> $Vec { $Vec::from_slice(self.zip(other, |&a, &b| a * b)) } + #[inline] pub fn div_v(&self, other: &$Vec) -> $Vec { $Vec::from_slice(self.zip(other, |&a, &b| a / b)) } + #[inline] pub fn rem_v(&self, other: &$Vec) -> $Vec { $Vec::from_slice(self.zip(other, |&a, &b| a % b)) } + + #[inline] pub fn neg_self(&mut self) { self.map_mut(|x| *x = -*x) } + #[inline] pub fn add_self_t(&mut self, value: T) { self.map_mut(|x| *x += value) } + #[inline] pub fn sub_self_t(&mut self, value: T) { self.map_mut(|x| *x -= value) } + #[inline] pub fn mul_self_t(&mut self, value: T) { self.map_mut(|x| *x *= value) } + #[inline] pub fn div_self_t(&mut self, value: T) { self.map_mut(|x| *x /= value) } + #[inline] pub fn rem_self_t(&mut self, value: T) { self.map_mut(|x| *x %= value) } + + #[inline] pub fn add_self_v(&mut self, other: &$Vec) { self.zip_mut(other, |a, &b| *a += b) } + #[inline] pub fn sub_self_v(&mut self, other: &$Vec) { self.zip_mut(other, |a, &b| *a -= b) } + #[inline] pub fn mul_self_v(&mut self, other: &$Vec) { self.zip_mut(other, |a, &b| *a *= b) } + #[inline] pub fn div_self_v(&mut self, other: &$Vec) { self.zip_mut(other, |a, &b| *a /= b) } + #[inline] pub fn rem_self_v(&mut self, other: &$Vec) { self.zip_mut(other, |a, &b| *a %= b) } + + #[inline] pub fn dot(&self, other: &$Vec) -> T { vec_dot!($Vec) } + } + ) +) + +macro_rules! vec_dot( + (Vec2) => ( + *self.index(0) * *other.index(0) + + *self.index(1) * *other.index(1) + ); + (Vec3) => ( + *self.index(0) * *other.index(0) + + *self.index(1) * *other.index(1) + + *self.index(2) * *other.index(2) + ); + (Vec4) => ( + *self.index(0) * *other.index(0) + + *self.index(1) * *other.index(1) + + *self.index(2) * *other.index(2) + + *self.index(3) * *other.index(3) + ); +) + +macro_rules! impl_vec_neg( + ($Vec:ident) => ( + impl Neg<$Vec> for $Vec { + #[inline] + pub fn neg(&self) -> $Vec { + $Vec::from_slice(self.map(|&x| -x)) + } + } + ) +) + +macro_rules! impl_vec_euclidean( + ($Vec:ident) => ( + impl $Vec { + #[inline] + pub fn length2(&self) -> T { + self.dot(self) + } + + #[inline] + pub fn length(&self) -> T { + self.length2().sqrt() + } + + #[inline] + pub fn distance2(&self, other: &$Vec) -> T { + other.sub_v(self).length2() + } + + #[inline] + pub fn distance(&self, other: &$Vec) -> T { + other.distance2(self).sqrt() + } + + #[inline] + pub fn angle(&self, other: &$Vec) -> T { + vec_angle!($Vec) + } + + #[inline] + pub fn normalize(&self) -> $Vec { + self.mul_t(one!(T)/self.length()) + } + + #[inline] + pub fn normalize_to(&self, length: T) -> $Vec { + self.mul_t(length / self.length()) + } + + #[inline] + pub fn lerp(&self, other: &$Vec, amount: T) -> $Vec { + self.add_v(&other.sub_v(self).mul_t(amount)) + } + + #[inline] + pub fn normalize_self(&mut self) { + let rlen = self.length().recip(); + self.mul_self_t(rlen); + } + + #[inline] + pub fn normalize_self_to(&mut self, length: T) { + let n = length / self.length(); + self.mul_self_t(n); + } + + pub fn lerp_self(&mut self, other: &$Vec, amount: T) { + let v = other.sub_v(self).mul_t(amount); + self.add_self_v(&v); + } + } + ) +) + +macro_rules! vec_angle( + (Vec2) => (self.perp_dot(other).atan2(&self.dot(other))); + (Vec3) => (self.cross(other).length().atan2(&self.dot(other))); + (Vec4) => ((self.dot(other) / (self.length() * other.length())).acos()); +) + +macro_rules! impl_vec_ord( + ($Vec:ident) => ( + impl $Vec { + #[inline] pub fn lt_t(&self, value: T) -> $Vec { $Vec::from_slice(self.map(|&x| x < value)) } + #[inline] pub fn le_t(&self, value: T) -> $Vec { $Vec::from_slice(self.map(|&x| x <= value)) } + #[inline] pub fn ge_t(&self, value: T) -> $Vec { $Vec::from_slice(self.map(|&x| x >= value)) } + #[inline] pub fn gt_t(&self, value: T) -> $Vec { $Vec::from_slice(self.map(|&x| x > value)) } + + #[inline] pub fn lt_v(&self, other: &$Vec) -> $Vec { $Vec::from_slice(self.zip(other, |&a, &b| a < b)) } + #[inline] pub fn le_v(&self, other: &$Vec) -> $Vec { $Vec::from_slice(self.zip(other, |&a, &b| a <= b)) } + #[inline] pub fn ge_v(&self, other: &$Vec) -> $Vec { $Vec::from_slice(self.zip(other, |&a, &b| a >= b)) } + #[inline] pub fn gt_v(&self, other: &$Vec) -> $Vec { $Vec::from_slice(self.zip(other, |&a, &b| a > b)) } + } + ) +) + +macro_rules! impl_vec_eq( + ($Vec:ident) => ( + impl $Vec { + #[inline] pub fn eq_t(&self, value: T) -> $Vec { $Vec::from_slice(self.map(|&x| x == value)) } + #[inline] pub fn ne_t(&self, value: T) -> $Vec { $Vec::from_slice(self.map(|&x| x != value)) } + + #[inline] pub fn eq_v(&self, other: &$Vec) -> $Vec { $Vec::from_slice(self.zip(other, |&a, &b| a == b)) } + #[inline] pub fn ne_v(&self, other: &$Vec) -> $Vec { $Vec::from_slice(self.zip(other, |&a, &b| a != b)) } + } + ) +) + +macro_rules! impl_vec_bool( + ($Vec:ident) => ( + impl $Vec { + #[inline] + pub fn any(&self) -> bool { vec_any!($Vec) } + + #[inline] + pub fn all(&self) -> bool { vec_all!($Vec) } + + #[inline] + pub fn not(&self) -> $Vec { + $Vec::from_slice(self.map(|&x| !x)) + } + } + ) +) + +macro_rules! vec_any( + (Vec2) => (*self.index(0) || *self.index(1)); + (Vec3) => (*self.index(0) || *self.index(1) || *self.index(2)); + (Vec4) => (*self.index(0) || *self.index(1) || *self.index(2) || *self.index(3)); +) + +macro_rules! vec_all( + (Vec2) => (*self.index(0) && *self.index(1)); + (Vec3) => (*self.index(0) && *self.index(1) && *self.index(2)); + (Vec4) => (*self.index(0) && *self.index(1) && *self.index(2) && *self.index(3)); +) + +macro_rules! impl_vec_not( + ($Vec:ident) => ( + impl> Not<$Vec> for $Vec { + pub fn not(&self) -> $Vec { + $Vec::from_slice(self.map(|&x| !x)) + } + } + ) +)