Use macros and higher-order functions to reduce code repetition

This commit is contained in:
Brendan Zabarauskas 2013-06-16 15:21:32 +10:00
parent 3d5a7ed7ef
commit f20eca5cc5
12 changed files with 1182 additions and 2670 deletions

View file

@ -34,6 +34,4 @@ pub trait Dimensional<T,Slice> {
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));
}

240
src/macros.rs Normal file
View file

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

View file

@ -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<T> {
@ -28,26 +28,21 @@ pub struct Mat2<T> {
y: Vec2<T>,
}
impl_dimensional!(Mat2, Vec2<T>, 2)
impl_dimensional_fns!(Mat2, Vec2<T>, 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<T> {
pub fn to_mat2(&self) -> Mat2<T>;
}
impl<T> Mat2<T> {
/// 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<T> {
@ -55,287 +50,29 @@ impl<T> Mat2<T> {
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<T>,
c1: Vec2<T>) -> Mat2<T> {
Mat2 { x: c0, y: c1 }
}
#[inline]
pub fn col<'a>(&'a self, i: uint) -> &'a Vec2<T> {
self.index(i)
}
#[inline]
pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec2<T> {
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<T> Dimensional<Vec2<T>,[Vec2<T>,..2]> for Mat2<T> {
#[inline]
pub fn index<'a>(&'a self, i: uint) -> &'a Vec2<T> {
&'a self.as_slice()[i]
}
#[inline]
pub fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec2<T> {
&'a mut self.as_mut_slice()[i]
}
#[inline]
pub fn as_slice<'a>(&'a self) -> &'a [Vec2<T>,..2] {
unsafe { transmute(self) }
}
#[inline]
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec2<T>,..2] {
unsafe { transmute(self) }
}
#[inline(always)]
pub fn map(&self, f: &fn(&Vec2<T>) -> Vec2<T>) -> Mat2<T> {
Mat2::from_cols(f(self.index(0)),
f(self.index(1)))
}
#[inline(always)]
pub fn map_mut(&mut self, f: &fn(&mut Vec2<T>)) {
f(self.index_mut(0));
f(self.index_mut(1));
}
}
impl<T:Copy> Mat2<T> {
#[inline]
pub fn row(&self, i: uint) -> Vec2<T> {
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<T> {
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<T:Copy + Num> Mat2<T> {
/// 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<T> {
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<T> {
Mat2::new(One::one::<T>(), Zero::zero::<T>(),
Zero::zero::<T>(), One::one::<T>())
}
/// Returns the additive identity matrix
/// ~~~
/// c0 c1
/// +----+----+
/// r0 | 0 | 0 |
/// +----+----+
/// r1 | 0 | 0 |
/// +----+----+
/// ~~~
#[inline]
pub fn zero() -> Mat2<T> {
Mat2::new(Zero::zero::<T>(), Zero::zero::<T>(),
Zero::zero::<T>(), Zero::zero::<T>())
}
#[inline]
pub fn mul_t(&self, value: T) -> Mat2<T> {
self.map(|&c| c.mul_t(value))
}
#[inline]
pub fn mul_v(&self, vec: &Vec2<T>) -> Vec2<T> {
Vec2::new(self.row(0).dot(vec),
self.row(1).dot(vec))
}
#[inline]
pub fn add_m(&self, other: &Mat2<T>) -> Mat2<T> {
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<T>) -> Mat2<T> {
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<T>) -> Mat2<T> {
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<T>) {
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<T>) {
self.x.sub_self_v(other.col(0));
self.y.sub_self_v(other.col(1));
}
pub fn dot(&self, other: &Mat2<T>) -> 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<T:Copy + Num> ToMat3<T> for Mat2<T> {
/// 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<T> {
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<T:Copy + Num> ToMat4<T> for Mat2<T> {
/// 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<T> {
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<T:Copy + Num> Neg<Mat2<T>> for Mat2<T> {
#[inline]
pub fn neg(&self) -> Mat2<T> {
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<T:Copy + Real> Mat2<T> {
}
}
impl<T:Copy + Real + ApproxEq<T>> Mat2<T> {
#[inline]
pub fn inverse(&self) -> Option<Mat2<T>> {
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<T:Copy + Eq + ApproxEq<T>> ApproxEq<T> for Mat2<T> {
#[inline]
pub fn approx_epsilon() -> T {
ApproxEq::approx_epsilon::<T,T>()
}
#[inline]
pub fn approx_eq(&self, other: &Mat2<T>) -> bool {
self.approx_eq_eps(other, &ApproxEq::approx_epsilon::<T,T>())
}
#[inline]
pub fn approx_eq_eps(&self, other: &Mat2<T>, 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::<float>(0.000001, 0.000001,
0.000001, 0.000001).approx_eq(&Mat2::zero::<float>()));
0.000001, 0.000001).approx_eq(&Mat2::zero::<float>()));
assert!(Mat2::new::<float>(0.0000001, 0.0000001,
0.0000001, 0.0000001).approx_eq(&Mat2::zero::<float>()));
0.0000001, 0.0000001).approx_eq(&Mat2::zero::<float>()));
}
}

View file

@ -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<T> {
@ -29,29 +29,21 @@ pub struct Mat3<T> {
z: Vec3<T>,
}
impl_dimensional!(Mat3, Vec3<T>, 3)
impl_dimensional_fns!(Mat3, Vec3<T>, 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<T> {
pub fn to_mat3(&self) -> Mat3<T>;
}
impl<T> Mat3<T> {
/// 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<T> Mat3<T> {
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<T>,
c1: Vec3<T>,
c2: Vec3<T>) -> Mat3<T> {
Mat3 { x: c0, y: c1, z: c2 }
}
#[inline]
pub fn col<'a>(&'a self, i: uint) -> &'a Vec3<T> {
self.index(i)
}
#[inline]
pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec3<T> {
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<T> Dimensional<Vec3<T>,[Vec3<T>,..3]> for Mat3<T> {
#[inline]
pub fn index<'a>(&'a self, i: uint) -> &'a Vec3<T> {
&'a self.as_slice()[i]
}
#[inline]
pub fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec3<T> {
&'a mut self.as_mut_slice()[i]
}
#[inline]
pub fn as_slice<'a>(&'a self) -> &'a [Vec3<T>,..3] {
unsafe { transmute(self) }
}
#[inline]
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec3<T>,..3] {
unsafe { transmute(self) }
}
#[inline(always)]
pub fn map(&self, f: &fn(&Vec3<T>) -> Vec3<T>) -> Mat3<T> {
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<T>)) {
f(self.index_mut(0));
f(self.index_mut(1));
f(self.index_mut(2));
}
}
impl<T:Copy> Mat3<T> {
#[inline]
pub fn row(&self, i: uint) -> Vec3<T> {
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<T> {
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<T:Copy + Num> Mat3<T> {
/// 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<T> {
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<T> {
Mat3::new(One::one::<T>(), Zero::zero::<T>(), Zero::zero::<T>(),
Zero::zero::<T>(), One::one::<T>(), Zero::zero::<T>(),
Zero::zero::<T>(), Zero::zero::<T>(), One::one::<T>())
}
/// 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<T> {
Mat3::new(Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(),
Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(),
Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>())
}
#[inline]
pub fn mul_t(&self, value: T) -> Mat3<T> {
self.map(|&c| c.mul_t(value))
}
#[inline]
pub fn mul_v(&self, vec: &Vec3<T>) -> Vec3<T> {
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<T>) -> Mat3<T> {
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<T>) -> Mat3<T> {
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<T>) -> Mat3<T> {
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<T>) {
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<T>) {
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>) -> 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<T:Copy + Num> ToMat4<T> for Mat3<T> {
/// 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<T> {
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<T:Copy + Num> Neg<Mat3<T>> for Mat3<T> {
#[inline]
pub fn neg(&self) -> Mat3<T> {
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<T:Copy + Real> Mat3<T> {
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<T:Copy + Real> Mat3<T> {
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<T:Copy + Real> Mat3<T> {
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<T:Copy + Real> Mat3<T> {
pub fn from_angle_axis(radians: T, axis: &Vec3<T>) -> Mat3<T> {
let c = radians.cos();
let s = radians.sin();
let _1_c = One::one::<T>() - c;
let _1_c = one!(T) - c;
let x = axis.x;
let y = axis.y;
@ -467,11 +166,11 @@ impl<T:Copy + Real> ToQuat<T> for Mat3<T> {
let trace = self.trace();
// FIXME: We don't have any numeric conversions in std yet :P
let half = One::one::<T>() / (One::one::<T>() + One::one::<T>());
let half = one!(T) / two!(T);
cond! (
(trace >= Zero::zero()) {
s = (One::one::<T>() + 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<T:Copy + Real> ToQuat<T> for Mat3<T> {
}
}
impl<T:Copy + Real + ApproxEq<T>> Mat3<T> {
pub fn inverse(&self) -> Option<Mat3<T>> {
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<T:Copy + Eq + ApproxEq<T>> ApproxEq<T> for Mat3<T> {
#[inline]
pub fn approx_epsilon() -> T {
ApproxEq::approx_epsilon::<T,T>()
}
#[inline]
pub fn approx_eq(&self, other: &Mat3<T>) -> bool {
self.approx_eq_eps(other, &ApproxEq::approx_epsilon::<T,T>())
}
#[inline]
pub fn approx_eq_eps(&self, other: &Mat3<T>, 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::*;

View file

@ -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<T> {
@ -30,32 +29,21 @@ pub struct Mat4<T> {
w: Vec4<T>,
}
impl_dimensional!(Mat4, Vec4<T>, 4)
impl_dimensional_fns!(Mat4, Vec4<T>, 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<T> {
pub fn to_mat4(&self) -> Mat4<T>;
}
impl<T> Mat4<T> {
/// 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<T> Mat4<T> {
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<T>,
c1: Vec4<T>,
@ -95,456 +62,6 @@ impl<T> Mat4<T> {
c3: Vec4<T>) -> Mat4<T> {
Mat4 { x: c0, y: c1, z: c2, w: c3 }
}
#[inline]
pub fn col<'a>(&'a self, i: uint) -> &'a Vec4<T> {
self.index(i)
}
#[inline]
pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec4<T> {
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<T> Dimensional<Vec4<T>,[Vec4<T>,..4]> for Mat4<T> {
#[inline]
pub fn index<'a>(&'a self, i: uint) -> &'a Vec4<T> {
&'a self.as_slice()[i]
}
#[inline]
pub fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec4<T> {
&'a mut self.as_mut_slice()[i]
}
#[inline]
pub fn as_slice<'a>(&'a self) -> &'a [Vec4<T>,..4] {
unsafe { transmute(self) }
}
#[inline]
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec4<T>,..4] {
unsafe { transmute(self) }
}
#[inline(always)]
pub fn map(&self, f: &fn(&Vec4<T>) -> Vec4<T>) -> Mat4<T> {
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<T>)) {
f(self.index_mut(0));
f(self.index_mut(1));
f(self.index_mut(2));
f(self.index_mut(3));
}
}
impl<T:Copy> Mat4<T> {
#[inline]
pub fn row(&self, i: uint) -> Vec4<T> {
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<T> {
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<T:Copy + Num> Mat4<T> {
/// 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<T> {
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<T> {
Mat4::new(One::one::<T>(), Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(),
Zero::zero::<T>(), One::one::<T>(), Zero::zero::<T>(), Zero::zero::<T>(),
Zero::zero::<T>(), Zero::zero::<T>(), One::one::<T>(), Zero::zero::<T>(),
Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(), One::one::<T>())
}
/// 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<T> {
Mat4::new(Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(),
Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(),
Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(),
Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>())
}
#[inline]
pub fn mul_t(&self, value: T) -> Mat4<T> {
self.map(|&c| c.mul_t(value))
}
#[inline]
pub fn mul_v(&self, vec: &Vec4<T>) -> Vec4<T> {
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<T>) -> Mat4<T> {
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<T>) -> Mat4<T> {
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<T>) -> Mat4<T> {
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<T>) {
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<T>) {
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>) -> 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<T:Copy + Num> Neg<Mat4<T>> for Mat4<T> {
#[inline]
pub fn neg(&self) -> Mat4<T> {
self.map(|&x| -x)
}
}
impl<T:Copy + Real + ApproxEq<T>> Mat4<T> {
pub fn inverse(&self) -> Option<Mat4<T>> {
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::<T>();
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<T:Copy + Eq + ApproxEq<T>> ApproxEq<T> for Mat4<T> {
#[inline]
pub fn approx_epsilon() -> T {
ApproxEq::approx_epsilon::<T,T>()
}
#[inline]
pub fn approx_eq(&self, other: &Mat4<T>) -> bool {
self.approx_eq_eps(other, &ApproxEq::approx_epsilon::<T,T>())
}
#[inline]
pub fn approx_eq_eps(&self, other: &Mat4<T>, 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)]

508
src/mat_macros.rs Normal file
View file

@ -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<T> $Mat<T> {
#[inline]
pub fn col<'a>(&'a self, i: uint) -> &'a $Vec<T> {
self.index(i)
}
#[inline]
pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut $Vec<T> {
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<T:Copy> $Mat<T> {
#[inline]
pub fn row(&self, i: uint) -> $Vec<T> {
$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<T> { 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<T:Copy + Num> $Mat<T> {
#[inline]
pub fn from_value(value: T) -> $Mat<T> { mat_from_value!($Mat) }
#[inline]
pub fn identity() -> $Mat<T> { $Mat::from_value(one!(T)) }
#[inline]
pub fn zero() -> $Mat<T> { $Mat::from_value(zero!(T)) }
#[inline]
pub fn mul_t(&self, value: T) -> $Mat<T> {
$Mat::from_slice(self.map(|&c| c.mul_t(value)))
}
#[inline]
pub fn mul_v(&self, vec: &$Vec<T>) -> $Vec<T> {
mat_mul_v!($Mat)
}
#[inline]
pub fn mul_m(&self, other: &$Mat<T>) -> $Mat<T> {
mat_mul_m!($Mat)
}
#[inline]
pub fn add_m(&self, other: &$Mat<T>) -> $Mat<T> {
$Mat::from_slice(self.zip(other, |a, b| a.add_v(b)))
}
#[inline]
pub fn sub_m(&self, other: &$Mat<T>) -> $Mat<T> {
$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<T>) {
self.zip_mut(other, |a, b| a.add_self_v(b))
}
#[inline]
pub fn sub_self_m(&mut self, other: &$Mat<T>) {
self.zip_mut(other, |a, b| a.sub_self_v(b))
}
pub fn dot(&self, other: &$Mat<T>) -> 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<T:Copy + Real + ApproxEq<T>> $Mat<T> {
#[inline]
pub fn inverse(&self) -> Option<$Mat<T>> {
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::<T>();
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<T:Copy + Num> Neg<$Mat<T>> for $Mat<T> {
#[inline]
pub fn neg(&self) -> $Mat<T> {
$Mat::from_slice(self.map(|&x| -x))
}
}
)
)

View file

@ -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<T:Copy + Real>(fovy: T, aspectRatio: T, near: T, far: T) -> Mat4<T> {
let ymax = near * (fovy / two::<T>()).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<T:Copy + Real>(fovy: T, aspectRatio: T, near: T, far: T) -> M
/// (http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml) function.
///
pub fn frustum<T:Copy + Real>(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4<T> {
let c0r0 = (two::<T>() * 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::<T>() * 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::<T>();
let c2r3 = -one!(T);
let c3r0 = Zero::zero();
let c3r1 = Zero::zero();
let c3r2 = -(two::<T>() * 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<T:Copy + Real>(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<T:Copy + Real>(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4<T> {
let c0r0 = two::<T>() / (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::<T>() / (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::<T>() / (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:Num>() -> T {
One::one::<T>() + One::one::<T>()
}

View file

@ -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<f32>;
@ -34,18 +33,14 @@ pub type Quatf32 = Quat<f32>;
pub type Quatf64 = Quat<f64>;
/// 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<T> { s: T, v: Vec3<T> }
impl_dimensional!(Quat, T, 4)
impl_dimensional_fns!(Quat, T, 4)
impl_swap!(Quat)
impl_approx!(Quat)
pub trait ToQuat<T> {
pub fn to_quat(&self) -> Quat<T>;
}
@ -77,98 +72,39 @@ impl<T> Quat<T> {
}
}
impl<T> Dimensional<T,[T,..4]> for Quat<T> {
#[inline]
pub fn index<'a>(&'a self, i: uint) -> &'a T {
&'a self.as_slice()[i]
}
#[inline]
pub fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T {
&'a mut self.as_mut_slice()[i]
}
#[inline]
pub fn as_slice<'a>(&'a self) -> &'a [T,..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<T> {
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<T:Copy> Quat<T> {
#[inline]
pub fn swap(&mut self, a: uint, b: uint) {
let tmp = *self.index(a);
*self.index_mut(a) = *self.index(b);
*self.index_mut(b) = tmp;
}
}
impl<T:Copy + Real> Quat<T> {
/// The multiplicative identity, ie: `q = 1 + 0i + 0j + 0i`
#[inline]
pub fn identity() -> Quat<T> {
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<T> {
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<T> {
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<T> {
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<T> {
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<T> {
// 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<T:Copy + Real> Quat<T> {
#[inline]
pub fn from_angle_axis(radians: T, axis: &Vec3<T>) -> Quat<T> {
let half = radians / two();
let half = radians / two!(T);
Quat::from_sv(half.cos(), axis.mul_t(half.sin()))
}
@ -188,20 +124,20 @@ impl<T:Copy + Real> Quat<T> {
/// The result of multiplying the quaternion a scalar
#[inline]
pub fn mul_t(&self, value: T) -> Quat<T> {
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<T> {
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<T>) -> Vec3<T> {
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<T:Copy + Real> Quat<T> {
/// The normalized quaternion
#[inline]
pub fn normalize(&self) -> Quat<T> {
self.mul_t(One::one::<T>() / self.magnitude())
self.mul_t(one!(T) / self.magnitude())
}
/// Normalised linear interpolation
@ -280,7 +216,7 @@ impl<T:Copy + Real> Quat<T> {
///
/// The intoperlated quaternion
pub fn nlerp(&self, other: &Quat<T>, amount: T) -> Quat<T> {
self.mul_t(One::one::<T>() - 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<T:Copy + Num> ToMat3<T> for Quat<T> {
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<T:Copy + Num> ToMat3<T> for Quat<T> {
impl<T:Copy + Float> Neg<Quat<T>> for Quat<T> {
#[inline]
pub fn neg(&self) -> Quat<T> {
self.map(|&x| -x)
Quat::from_slice(self.map(|&x| -x))
}
}
@ -352,14 +288,15 @@ impl<T:Copy + Float> Quat<T> {
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::<T>(),
&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<T:Copy + Float> Quat<T> {
}
}
impl<T:Copy + Eq + ApproxEq<T>> ApproxEq<T> for Quat<T> {
#[inline]
pub fn approx_epsilon() -> T {
ApproxEq::approx_epsilon::<T,T>()
}
#[inline]
pub fn approx_eq(&self, other: &Quat<T>) -> bool {
self.approx_eq_eps(other, &ApproxEq::approx_epsilon::<T,T>())
}
#[inline]
pub fn approx_eq_eps(&self, other: &Quat<T>, epsilon: &T) -> bool {
self.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:Num>() -> T {
One::one::<T>() + One::one::<T>()
}
#[cfg(test)]
mod tests {
use mat::*;

View file

@ -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<T> { x: T, y: T }
impl<T> Vec2<T> {
#[inline]
pub fn new(x: T, y: T ) -> Vec2<T> {
Vec2 { x: x, y: y }
}
}
impl_dimensional!(Vec2, T, 2)
impl_dimensional_fns!(Vec2, T, 2)
impl_swap!(Vec2)
impl_approx!(Vec2)
impl<T> Dimensional<T,[T,..2]> for Vec2<T> {
#[inline]
pub fn index<'a>(&'a self, i: uint) -> &'a T {
&'a self.as_slice()[i]
}
#[inline]
pub fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T {
&'a mut self.as_mut_slice()[i]
}
#[inline]
pub fn as_slice<'a>(&'a self) -> &'a [T,..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<T> {
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<T:Copy> Vec2<T> {
#[inline]
pub fn from_value(value: T) -> Vec2<T> {
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<T:Copy + Num> Vec2<T> {
#[inline]
pub fn identity() -> Vec2<T> {
Vec2::new(One::one::<T>(), One::one::<T>())
}
#[inline]
pub fn zero() -> Vec2<T> {
Vec2::new(Zero::zero::<T>(), Zero::zero::<T>())
}
#[inline]
pub fn unit_x() -> Vec2<T> {
Vec2::new(One::one::<T>(), Zero::zero::<T>())
}
#[inline]
pub fn unit_y() -> Vec2<T> {
Vec2::new(Zero::zero::<T>(), One::one::<T>())
}
#[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<T> {
self.map(|&x| x + value)
}
#[inline]
pub fn sub_t(&self, value: T) -> Vec2<T> {
self.map(|&x| x - value)
}
#[inline]
pub fn mul_t(&self, value: T) -> Vec2<T> {
self.map(|&x| x * value)
}
#[inline]
pub fn div_t(&self, value: T) -> Vec2<T> {
self.map(|&x| x / value)
}
#[inline]
pub fn rem_t(&self, value: T) -> Vec2<T> {
self.map(|&x| x % value)
}
#[inline]
pub fn add_v(&self, other: &Vec2<T>) -> Vec2<T> {
Vec2::new(*self.index(0) + *other.index(0),
*self.index(1) + *other.index(1))
}
#[inline]
pub fn sub_v(&self, other: &Vec2<T>) -> Vec2<T> {
Vec2::new(*self.index(0) - *other.index(0),
*self.index(1) - *other.index(1))
}
#[inline]
pub fn mul_v(&self, other: &Vec2<T>) -> Vec2<T> {
Vec2::new(*self.index(0) * *other.index(0),
*self.index(1) * *other.index(1))
}
#[inline]
pub fn div_v(&self, other: &Vec2<T>) -> Vec2<T> {
Vec2::new(*self.index(0) / *other.index(0),
*self.index(1) / *other.index(1))
}
#[inline]
pub fn rem_v(&self, other: &Vec2<T>) -> Vec2<T> {
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<T>) {
*self.index_mut(0) += *other.index(0);
*self.index_mut(1) += *other.index(1);
}
#[inline]
pub fn sub_self_v(&mut self, other: &Vec2<T>) {
*self.index_mut(0) -= *other.index(0);
*self.index_mut(1) -= *other.index(1);
}
#[inline]
pub fn mul_self_v(&mut self, other: &Vec2<T>) {
*self.index_mut(0) *= *other.index(0);
*self.index_mut(1) *= *other.index(1);
}
#[inline]
pub fn div_self_v(&mut self, other: &Vec2<T>) {
*self.index_mut(0) /= *other.index(0);
*self.index_mut(1) /= *other.index(1);
}
#[inline]
pub fn rem_self_v(&mut self, other: &Vec2<T>) {
*self.index_mut(0) /= *other.index(0);
*self.index_mut(1) /= *other.index(1);
}
#[inline]
pub fn dot(&self, other: &Vec2<T>) -> T {
*self.index(0) * *other.index(0) +
*self.index(1) * *other.index(1)
}
#[inline] pub fn unit_x() -> Vec2<T> { Vec2::new(one!(T), zero!(T)) }
#[inline] pub fn unit_y() -> Vec2<T> { Vec2::new(zero!(T), one!(T)) }
#[inline]
pub fn perp_dot(&self, other: &Vec2<T>) -> T {
(*self.index(0) * *other.index(1)) -
(*self.index(1) * *other.index(0))
}
#[inline]
pub fn to_homogeneous(&self) -> Vec3<T> {
Vec3::new(self.x, self.y, Zero::zero())
}
}
impl<T:Copy + Num> Neg<Vec2<T>> for Vec2<T> {
#[inline]
pub fn neg(&self) -> Vec2<T> {
self.map(|&x| -x)
}
}
impl<T:Copy + Real> Vec2<T> {
#[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>) -> T {
other.sub_v(self).length2()
}
#[inline]
pub fn distance(&self, other: &Vec2<T>) -> T {
other.distance2(self).sqrt()
}
#[inline]
pub fn angle(&self, other: &Vec2<T>) -> T {
self.perp_dot(other).atan2(&self.dot(other))
}
#[inline]
pub fn normalize(&self) -> Vec2<T> {
self.mul_t(One::one::<T>()/self.length())
}
#[inline]
pub fn normalize_to(&self, length: T) -> Vec2<T> {
self.mul_t(length / self.length())
}
#[inline]
pub fn lerp(&self, other: &Vec2<T>, amount: T) -> Vec2<T> {
self.add_v(&other.sub_v(self).mul_t(amount))
}
#[inline]
pub fn normalize_self(&mut self) {
let n = One::one::<T>() / 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<T>, amount: T) {
let v = other.sub_v(self).mul_t(amount);
self.add_self_v(&v);
}
}
impl<T:Copy + Eq + ApproxEq<T>> ApproxEq<T> for Vec2<T> {
#[inline]
pub fn approx_epsilon() -> T {
ApproxEq::approx_epsilon::<T,T>()
}
#[inline]
pub fn approx_eq(&self, other: &Vec2<T>) -> bool {
self.approx_eq_eps(other, &ApproxEq::approx_epsilon::<T,T>())
}
#[inline]
pub fn approx_eq_eps(&self, other: &Vec2<T>, epsilon: &T) -> bool {
self.index(0).approx_eq_eps(other.index(0), epsilon) &&
self.index(1).approx_eq_eps(other.index(1), epsilon)
}
}
impl<T:Copy + Ord> Vec2<T> {
#[inline]
pub fn lt_t(&self, value: T) -> Vec2<bool> {
Vec2::new(*self.index(0) < value,
*self.index(1) < value)
}
#[inline]
pub fn le_t(&self, value: T) -> Vec2<bool> {
Vec2::new(*self.index(0) <= value,
*self.index(1) <= value)
}
#[inline]
pub fn ge_t(&self, value: T) -> Vec2<bool> {
Vec2::new(*self.index(0) >= value,
*self.index(1) >= value)
}
#[inline]
pub fn gt_t(&self, value: T) -> Vec2<bool> {
Vec2::new(*self.index(0) > value,
*self.index(1) > value)
}
#[inline]
pub fn lt_v(&self, other: &Vec2<T>) -> Vec2<bool> {
Vec2::new(*self.index(0) < *other.index(0),
*self.index(1) < *other.index(1))
}
#[inline]
pub fn le_v(&self, other: &Vec2<T>) -> Vec2<bool> {
Vec2::new(*self.index(0) <= *other.index(0),
*self.index(1) <= *other.index(1))
}
#[inline]
pub fn ge_v(&self, other: &Vec2<T>) -> Vec2<bool> {
Vec2::new(*self.index(0) >= *other.index(0),
*self.index(1) >= *other.index(1))
}
#[inline]
pub fn gt_v(&self, other: &Vec2<T>) -> Vec2<bool> {
Vec2::new(*self.index(0) > *other.index(0),
*self.index(1) > *other.index(1))
}
}
impl<T:Copy + Eq> Vec2<T> {
#[inline]
pub fn eq_t(&self, value: T) -> Vec2<bool> {
Vec2::new(*self.index(0) == value,
*self.index(1) == value)
}
#[inline]
pub fn ne_t(&self, value: T) -> Vec2<bool> {
Vec2::new(*self.index(0) != value,
*self.index(1) != value)
}
#[inline]
pub fn eq_v(&self, other: &Vec2<T>) -> Vec2<bool> {
Vec2::new(*self.index(0) == *other.index(0),
*self.index(1) == *other.index(1))
}
#[inline]
pub fn ne_v(&self, other: &Vec2<T>) -> Vec2<bool> {
Vec2::new(*self.index(0) != *other.index(0),
*self.index(1) != *other.index(1))
}
}
impl Vec2<bool> {
#[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<bool> {
self.map(|&x| !x)
}
}
#[cfg(test)]
@ -455,9 +86,6 @@ mod tests {
assert_eq!(-a, Vec2::new::<float>(-1.0, -2.0));
assert_eq!(a.neg(), Vec2::new::<float>(-1.0, -2.0));
assert!(Vec2::new::<float>(0.0, 0.0).is_zero());
assert!(!Vec2::new::<float>(1.0, 1.0).is_zero());
assert_eq!(a.mul_t(f1), Vec2::new::<float>( 1.5, 3.0));
assert_eq!(a.div_t(f2), Vec2::new::<float>( 2.0, 4.0));

View file

@ -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<T> { x: T, y: T, z: T }
impl<T> Vec3<T> {
#[inline]
pub fn new(x: T, y: T, z: T ) -> Vec3<T> {
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<T> Dimensional<T,[T,..3]> for Vec3<T> {
#[inline]
pub fn index<'a>(&'a self, i: uint) -> &'a T {
&'a self.as_slice()[i]
}
#[inline]
pub fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T {
&'a mut self.as_mut_slice()[i]
}
#[inline]
pub fn as_slice<'a>(&'a self) -> &'a [T,..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<T> {
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<T:Copy> Vec3<T> {
#[inline]
pub fn from_value(value: T) -> Vec3<T> {
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<T:Copy + Num> Vec3<T> {
#[inline]
pub fn identity() -> Vec3<T> {
Vec3::new(One::one::<T>(), One::one::<T>(), One::one::<T>())
}
#[inline]
pub fn zero() -> Vec3<T> {
Vec3::new(Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>())
}
#[inline]
pub fn unit_x() -> Vec3<T> {
Vec3::new(One::one::<T>(), Zero::zero::<T>(), Zero::zero::<T>())
}
#[inline]
pub fn unit_y() -> Vec3<T> {
Vec3::new(Zero::zero::<T>(), One::one::<T>(), Zero::zero::<T>())
}
#[inline]
pub fn unit_z() -> Vec3<T> {
Vec3::new(Zero::zero::<T>(), Zero::zero::<T>(), One::one::<T>())
}
#[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<T> {
self.map(|&x| x + value)
}
#[inline]
pub fn sub_t(&self, value: T) -> Vec3<T> {
self.map(|&x| x - value)
}
#[inline]
pub fn mul_t(&self, value: T) -> Vec3<T> {
self.map(|&x| x * value)
}
#[inline]
pub fn div_t(&self, value: T) -> Vec3<T> {
self.map(|&x| x / value)
}
#[inline]
pub fn rem_t(&self, value: T) -> Vec3<T> {
self.map(|&x| x % value)
}
#[inline]
pub fn add_v(&self, other: &Vec3<T>) -> Vec3<T> {
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<T>) -> Vec3<T> {
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<T>) -> Vec3<T> {
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<T>) -> Vec3<T> {
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<T>) -> Vec3<T> {
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<T>) {
*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<T>) {
*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<T>) {
*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<T>) {
*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<T>) {
*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>) -> 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<T> { Vec3::new(one!(T), zero!(T), zero!(T)) }
#[inline] pub fn unit_y() -> Vec3<T> { Vec3::new(zero!(T), one!(T), zero!(T)) }
#[inline] pub fn unit_z() -> Vec3<T> { Vec3::new(zero!(T), zero!(T), one!(T)) }
#[inline]
pub fn cross(&self, other: &Vec3<T>) -> Vec3<T> {
@ -256,201 +52,6 @@ impl<T:Copy + Num> Vec3<T> {
pub fn cross_self(&mut self, other: &Vec3<T>) {
*self = self.cross(other)
}
#[inline]
pub fn to_homogeneous(&self) -> Vec4<T> {
Vec4::new(self.x, self.y, self.z, Zero::zero())
}
}
impl<T:Copy + Num> Neg<Vec3<T>> for Vec3<T> {
#[inline]
pub fn neg(&self) -> Vec3<T> {
self.map(|&x| -x)
}
}
impl<T:Copy + Real> Vec3<T> {
#[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>) -> T {
other.sub_v(self).length2()
}
#[inline]
pub fn distance(&self, other: &Vec3<T>) -> T {
other.distance2(self).sqrt()
}
#[inline]
pub fn angle(&self, other: &Vec3<T>) -> T {
self.cross(other).length().atan2(&self.dot(other))
}
#[inline]
pub fn normalize(&self) -> Vec3<T> {
self.mul_t(One::one::<T>()/self.length())
}
#[inline]
pub fn normalize_to(&self, length: T) -> Vec3<T> {
self.mul_t(length / self.length())
}
#[inline]
pub fn lerp(&self, other: &Vec3<T>, amount: T) -> Vec3<T> {
self.add_v(&other.sub_v(self).mul_t(amount))
}
#[inline]
pub fn normalize_self(&mut self) {
let n = One::one::<T>() / 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<T>, amount: T) {
let v = other.sub_v(self).mul_t(amount);
self.add_self_v(&v);
}
}
impl<T:Copy + Eq + ApproxEq<T>> ApproxEq<T> for Vec3<T> {
#[inline]
pub fn approx_epsilon() -> T {
ApproxEq::approx_epsilon::<T,T>()
}
#[inline]
pub fn approx_eq(&self, other: &Vec3<T>) -> bool {
self.approx_eq_eps(other, &ApproxEq::approx_epsilon::<T,T>())
}
#[inline]
pub fn approx_eq_eps(&self, other: &Vec3<T>, 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<T:Copy + Ord> Vec3<T> {
#[inline]
pub fn lt_t(&self, value: T) -> Vec3<bool> {
Vec3::new(*self.index(0) < value,
*self.index(1) < value,
*self.index(2) < value)
}
#[inline]
pub fn le_t(&self, value: T) -> Vec3<bool> {
Vec3::new(*self.index(0) <= value,
*self.index(1) <= value,
*self.index(2) <= value)
}
#[inline]
pub fn ge_t(&self, value: T) -> Vec3<bool> {
Vec3::new(*self.index(0) >= value,
*self.index(1) >= value,
*self.index(2) >= value)
}
#[inline]
pub fn gt_t(&self, value: T) -> Vec3<bool> {
Vec3::new(*self.index(0) > value,
*self.index(1) > value,
*self.index(2) > value)
}
#[inline]
pub fn lt_v(&self, other: &Vec3<T>) -> Vec3<bool> {
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<T>) -> Vec3<bool> {
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<T>) -> Vec3<bool> {
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<T>) -> Vec3<bool> {
Vec3::new(*self.index(0) > *other.index(0),
*self.index(1) > *other.index(1),
*self.index(2) > *other.index(2))
}
}
impl<T:Copy + Eq> Vec3<T> {
#[inline]
pub fn eq_t(&self, value: T) -> Vec3<bool> {
Vec3::new(*self.index(0) == value,
*self.index(1) == value,
*self.index(2) == value)
}
#[inline]
pub fn ne_t(&self, value: T) -> Vec3<bool> {
Vec3::new(*self.index(0) != value,
*self.index(1) != value,
*self.index(2) != value)
}
#[inline]
pub fn eq_v(&self, other: &Vec3<T>) -> Vec3<bool> {
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<T>) -> Vec3<bool> {
Vec3::new(*self.index(0) != *other.index(0),
*self.index(1) != *other.index(1),
*self.index(2) != *other.index(2))
}
}
impl Vec3<bool> {
#[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<bool> {
self.map(|&x| !x)
}
}
#[cfg(test)]
@ -507,9 +108,6 @@ mod tests{
assert_eq!(-a, Vec3::new::<float>(-1.0, -2.0, -3.0));
assert_eq!(a.neg(), Vec3::new::<float>(-1.0, -2.0, -3.0));
assert!(Vec3::new::<float>(0.0, 0.0, 0.0).is_zero());
assert!(!Vec3::new::<float>(1.0, 1.0, 1.0).is_zero());
assert_eq!(a.mul_t(f1), Vec3::new::<float>( 1.5, 3.0, 4.5));
assert_eq!(a.div_t(f2), Vec3::new::<float>( 2.0, 4.0, 6.0));

View file

@ -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<T> { x: T, y: T, z: T, w: T }
impl<T> Vec4<T> {
#[inline]
pub fn new(x: T, y: T, z: T, w: T ) -> Vec4<T> {
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<T> Dimensional<T,[T,..4]> for Vec4<T> {
#[inline]
pub fn index<'a>(&'a self, i: uint) -> &'a T {
&'a self.as_slice()[i]
}
#[inline]
pub fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T {
&'a mut self.as_mut_slice()[i]
}
#[inline]
pub fn as_slice<'a>(&'a self) -> &'a [T,..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<T> {
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<T:Copy> Vec4<T> {
#[inline]
pub fn from_value(value: T) -> Vec4<T> {
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<T:Copy + Num> Vec4<T> {
#[inline]
pub fn identity() -> Vec4<T> {
Vec4::new(One::one::<T>(), One::one::<T>(), One::one::<T>(), One::one::<T>())
}
#[inline]
pub fn zero() -> Vec4<T> {
Vec4::new(Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>())
}
#[inline]
pub fn unit_x() -> Vec4<T> {
Vec4::new(One::one::<T>(), Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>())
}
#[inline]
pub fn unit_y() -> Vec4<T> {
Vec4::new(Zero::zero::<T>(), One::one::<T>(), Zero::zero::<T>(), Zero::zero::<T>())
}
#[inline]
pub fn unit_z() -> Vec4<T> {
Vec4::new(Zero::zero::<T>(), Zero::zero::<T>(), One::one::<T>(), Zero::zero::<T>())
}
#[inline]
pub fn unit_w() -> Vec4<T> {
Vec4::new(Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(), One::one::<T>())
}
#[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<T> {
self.map(|&x| x + value)
}
#[inline]
pub fn sub_t(&self, value: T) -> Vec4<T> {
self.map(|&x| x - value)
}
#[inline]
pub fn mul_t(&self, value: T) -> Vec4<T> {
self.map(|&x| x * value)
}
#[inline]
pub fn div_t(&self, value: T) -> Vec4<T> {
self.map(|&x| x / value)
}
#[inline]
pub fn rem_t(&self, value: T) -> Vec4<T> {
self.map(|&x| x % value)
}
#[inline]
pub fn add_v(&self, other: &Vec4<T>) -> Vec4<T> {
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<T>) -> Vec4<T> {
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<T>) -> Vec4<T> {
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<T>) -> Vec4<T> {
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<T>) -> Vec4<T> {
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<T>) {
*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<T>) {
*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<T>) {
*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<T>) {
*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<T>) {
*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>) -> 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<T:Copy + Num> Neg<Vec4<T>> for Vec4<T> {
#[inline]
pub fn neg(&self) -> Vec4<T> {
self.map(|&x| -x)
}
}
impl<T:Copy + Real> Vec4<T> {
#[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>) -> T {
other.sub_v(self).length2()
}
#[inline]
pub fn distance(&self, other: &Vec4<T>) -> T {
other.distance2(self).sqrt()
}
#[inline]
pub fn angle(&self, other: &Vec4<T>) -> T {
(self.dot(other) / (self.length() * other.length())).acos()
}
#[inline]
pub fn normalize(&self) -> Vec4<T> {
self.mul_t(One::one::<T>()/self.length())
}
#[inline]
pub fn normalize_to(&self, length: T) -> Vec4<T> {
self.mul_t(length / self.length())
}
#[inline]
pub fn lerp(&self, other: &Vec4<T>, amount: T) -> Vec4<T> {
self.add_v(&other.sub_v(self).mul_t(amount))
}
#[inline]
pub fn normalize_self(&mut self) {
let n = One::one::<T>() / 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<T>, amount: T) {
let v = other.sub_v(self).mul_t(amount);
self.add_self_v(&v);
}
}
impl<T:Copy + Eq + ApproxEq<T>> ApproxEq<T> for Vec4<T> {
#[inline]
pub fn approx_epsilon() -> T {
ApproxEq::approx_epsilon::<T,T>()
}
#[inline]
pub fn approx_eq(&self, other: &Vec4<T>) -> bool {
self.approx_eq_eps(other, &ApproxEq::approx_epsilon::<T,T>())
}
#[inline]
pub fn approx_eq_eps(&self, other: &Vec4<T>, 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<T:Copy + Ord> Vec4<T> {
#[inline]
pub fn lt_t(&self, value: T) -> Vec4<bool> {
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<bool> {
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<bool> {
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<bool> {
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<T>) -> Vec4<bool> {
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<T>) -> Vec4<bool> {
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<T>) -> Vec4<bool> {
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<T>) -> Vec4<bool> {
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<T:Copy + Eq> Vec4<T> {
#[inline]
pub fn eq_t(&self, value: T) -> Vec4<bool> {
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<bool> {
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<T>) -> Vec4<bool> {
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<T>) -> Vec4<bool> {
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<bool> {
#[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<bool> { self.map(|&x| !x) }
#[inline] pub fn unit_x() -> Vec4<T> { Vec4::new(one!(T), zero!(T), zero!(T), zero!(T)) }
#[inline] pub fn unit_y() -> Vec4<T> { Vec4::new(zero!(T), one!(T), zero!(T), zero!(T)) }
#[inline] pub fn unit_z() -> Vec4<T> { Vec4::new(zero!(T), zero!(T), one!(T), zero!(T)) }
#[inline] pub fn unit_w() -> Vec4<T> { Vec4::new(zero!(T), zero!(T), zero!(T), one!(T)) }
}
#[cfg(test)]
@ -517,9 +95,6 @@ mod tests {
assert_eq!(-a, Vec4::new::<float>(-1.0, -2.0, -3.0, -4.0));
assert_eq!(a.neg(), Vec4::new::<float>(-1.0, -2.0, -3.0, -4.0));
assert!(Vec4::new::<float>(0.0, 0.0, 0.0, 0.0).is_zero());
assert!(!Vec4::new::<float>(1.0, 1.0, 1.0, 1.0).is_zero());
assert_eq!(a.mul_t(f1), Vec4::new::<float>( 1.5, 3.0, 4.5, 6.0));
assert_eq!(a.div_t(f2), Vec4::new::<float>( 2.0, 4.0, 6.0, 8.0));

245
src/vec_macros.rs Normal file
View file

@ -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<T> $Vec<T> {
#[inline]
pub fn new($($field: T),+) -> $Vec<T> {
$Vec { $($field: $field),+ }
}
}
)
)
macro_rules! impl_vec_copyable(
($Vec:ident) => (
impl<T:Copy> $Vec<T> {
#[inline]
pub fn from_value(value: T) -> $Vec<T> {
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<T:Copy + Num> $Vec<T> {
#[inline] pub fn identity() -> $Vec<T> { $Vec::from_value(one!(T)) }
#[inline] pub fn zero() -> $Vec<T> { $Vec::from_value(zero!(T)) }
#[inline] pub fn add_t(&self, value: T) -> $Vec<T> { $Vec::from_slice(self.map(|&x| x + value)) }
#[inline] pub fn sub_t(&self, value: T) -> $Vec<T> { $Vec::from_slice(self.map(|&x| x - value)) }
#[inline] pub fn mul_t(&self, value: T) -> $Vec<T> { $Vec::from_slice(self.map(|&x| x * value)) }
#[inline] pub fn div_t(&self, value: T) -> $Vec<T> { $Vec::from_slice(self.map(|&x| x / value)) }
#[inline] pub fn rem_t(&self, value: T) -> $Vec<T> { $Vec::from_slice(self.map(|&x| x % value)) }
#[inline] pub fn add_v(&self, other: &$Vec<T>) -> $Vec<T> { $Vec::from_slice(self.zip(other, |&a, &b| a + b)) }
#[inline] pub fn sub_v(&self, other: &$Vec<T>) -> $Vec<T> { $Vec::from_slice(self.zip(other, |&a, &b| a - b)) }
#[inline] pub fn mul_v(&self, other: &$Vec<T>) -> $Vec<T> { $Vec::from_slice(self.zip(other, |&a, &b| a * b)) }
#[inline] pub fn div_v(&self, other: &$Vec<T>) -> $Vec<T> { $Vec::from_slice(self.zip(other, |&a, &b| a / b)) }
#[inline] pub fn rem_v(&self, other: &$Vec<T>) -> $Vec<T> { $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<T>) { self.zip_mut(other, |a, &b| *a += b) }
#[inline] pub fn sub_self_v(&mut self, other: &$Vec<T>) { self.zip_mut(other, |a, &b| *a -= b) }
#[inline] pub fn mul_self_v(&mut self, other: &$Vec<T>) { self.zip_mut(other, |a, &b| *a *= b) }
#[inline] pub fn div_self_v(&mut self, other: &$Vec<T>) { self.zip_mut(other, |a, &b| *a /= b) }
#[inline] pub fn rem_self_v(&mut self, other: &$Vec<T>) { self.zip_mut(other, |a, &b| *a %= b) }
#[inline] pub fn dot(&self, other: &$Vec<T>) -> 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<T:Copy + Num> Neg<$Vec<T>> for $Vec<T> {
#[inline]
pub fn neg(&self) -> $Vec<T> {
$Vec::from_slice(self.map(|&x| -x))
}
}
)
)
macro_rules! impl_vec_euclidean(
($Vec:ident) => (
impl<T:Copy + Real> $Vec<T> {
#[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>) -> T {
other.sub_v(self).length2()
}
#[inline]
pub fn distance(&self, other: &$Vec<T>) -> T {
other.distance2(self).sqrt()
}
#[inline]
pub fn angle(&self, other: &$Vec<T>) -> T {
vec_angle!($Vec)
}
#[inline]
pub fn normalize(&self) -> $Vec<T> {
self.mul_t(one!(T)/self.length())
}
#[inline]
pub fn normalize_to(&self, length: T) -> $Vec<T> {
self.mul_t(length / self.length())
}
#[inline]
pub fn lerp(&self, other: &$Vec<T>, amount: T) -> $Vec<T> {
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<T>, 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<T:Copy + Ord> $Vec<T> {
#[inline] pub fn lt_t(&self, value: T) -> $Vec<bool> { $Vec::from_slice(self.map(|&x| x < value)) }
#[inline] pub fn le_t(&self, value: T) -> $Vec<bool> { $Vec::from_slice(self.map(|&x| x <= value)) }
#[inline] pub fn ge_t(&self, value: T) -> $Vec<bool> { $Vec::from_slice(self.map(|&x| x >= value)) }
#[inline] pub fn gt_t(&self, value: T) -> $Vec<bool> { $Vec::from_slice(self.map(|&x| x > value)) }
#[inline] pub fn lt_v(&self, other: &$Vec<T>) -> $Vec<bool> { $Vec::from_slice(self.zip(other, |&a, &b| a < b)) }
#[inline] pub fn le_v(&self, other: &$Vec<T>) -> $Vec<bool> { $Vec::from_slice(self.zip(other, |&a, &b| a <= b)) }
#[inline] pub fn ge_v(&self, other: &$Vec<T>) -> $Vec<bool> { $Vec::from_slice(self.zip(other, |&a, &b| a >= b)) }
#[inline] pub fn gt_v(&self, other: &$Vec<T>) -> $Vec<bool> { $Vec::from_slice(self.zip(other, |&a, &b| a > b)) }
}
)
)
macro_rules! impl_vec_eq(
($Vec:ident) => (
impl<T:Copy + Eq> $Vec<T> {
#[inline] pub fn eq_t(&self, value: T) -> $Vec<bool> { $Vec::from_slice(self.map(|&x| x == value)) }
#[inline] pub fn ne_t(&self, value: T) -> $Vec<bool> { $Vec::from_slice(self.map(|&x| x != value)) }
#[inline] pub fn eq_v(&self, other: &$Vec<T>) -> $Vec<bool> { $Vec::from_slice(self.zip(other, |&a, &b| a == b)) }
#[inline] pub fn ne_v(&self, other: &$Vec<T>) -> $Vec<bool> { $Vec::from_slice(self.zip(other, |&a, &b| a != b)) }
}
)
)
macro_rules! impl_vec_bool(
($Vec:ident) => (
impl $Vec<bool> {
#[inline]
pub fn any(&self) -> bool { vec_any!($Vec) }
#[inline]
pub fn all(&self) -> bool { vec_all!($Vec) }
#[inline]
pub fn not(&self) -> $Vec<bool> {
$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<T:Copy + Not<T>> Not<$Vec<T>> for $Vec<T> {
pub fn not(&self) -> $Vec<T> {
$Vec::from_slice(self.map(|&x| !x))
}
}
)
)