Refactor code for matrices.

This commit is contained in:
Sébastien Crozet 2013-06-28 22:55:09 +00:00
parent cd355dfb30
commit c54eb562ec
7 changed files with 615 additions and 610 deletions

View File

@ -1,6 +1,8 @@
use std::num::{One, Zero}; use std::num::{One, Zero};
use std::rand::{Rand, Rng, RngUtil}; use std::rand::{Rand, Rng, RngUtil};
use std::cmp::ApproxEq; use std::cmp::ApproxEq;
use traits::ring::Ring;
use traits::division_ring::DivisionRing;
use traits::rlmul::{RMul, LMul}; use traits::rlmul::{RMul, LMul};
use traits::dim::Dim; use traits::dim::Dim;
use traits::inv::Inv; use traits::inv::Inv;
@ -8,8 +10,7 @@ use traits::transpose::Transpose;
use traits::rotation::{Rotation, Rotate, Rotatable}; use traits::rotation::{Rotation, Rotate, Rotatable};
use traits::transformation::{Transform}; // FIXME: implement Transformation and Transformable use traits::transformation::{Transform}; // FIXME: implement Transformation and Transformable
use vec::Vec1; use vec::Vec1;
use dim2::mat2::Mat2; use mat::{Mat2, Mat3};
use dim3::mat3::Mat3;
use vec::Vec3; use vec::Vec3;
#[deriving(Eq, ToStr)] #[deriving(Eq, ToStr)]
@ -22,11 +23,10 @@ pub fn rotmat2<N: Copy + Trigonometric + Neg<N>>(angle: N) -> Rotmat<Mat2<N>>
let sia = angle.sin(); let sia = angle.sin();
Rotmat Rotmat
{ submat: Mat2::new(copy coa, -sia, copy sia, copy coa) } { submat: Mat2::new( [ copy coa, -sia, copy sia, copy coa ] ) }
} }
pub fn rotmat3<N: Copy + Trigonometric + Neg<N> + One + Sub<N, N> + Add<N, N> + pub fn rotmat3<N: Copy + Trigonometric + Ring>
Mul<N, N>>
(axis: &Vec3<N>, angle: N) -> Rotmat<Mat3<N>> (axis: &Vec3<N>, angle: N) -> Rotmat<Mat3<N>>
{ {
let _1 = One::one::<N>(); let _1 = One::one::<N>();
@ -41,7 +41,7 @@ pub fn rotmat3<N: Copy + Trigonometric + Neg<N> + One + Sub<N, N> + Add<N, N> +
let sin = angle.sin(); let sin = angle.sin();
Rotmat { Rotmat {
submat: Mat3::new( submat: Mat3::new( [
(sqx + (_1 - sqx) * cos), (sqx + (_1 - sqx) * cos),
(ux * uy * one_m_cos - uz * sin), (ux * uy * one_m_cos - uz * sin),
(ux * uz * one_m_cos + uy * sin), (ux * uz * one_m_cos + uy * sin),
@ -52,16 +52,16 @@ pub fn rotmat3<N: Copy + Trigonometric + Neg<N> + One + Sub<N, N> + Add<N, N> +
(ux * uz * one_m_cos - uy * sin), (ux * uz * one_m_cos - uy * sin),
(uy * uz * one_m_cos + ux * sin), (uy * uz * one_m_cos + ux * sin),
(sqz + (_1 - sqz) * cos)) (sqz + (_1 - sqz) * cos) ] )
} }
} }
impl<N: Div<N, N> + Trigonometric + Neg<N> + Mul<N, N> + Add<N, N> + Copy> impl<N: Trigonometric + DivisionRing + Copy>
Rotation<Vec1<N>> for Rotmat<Mat2<N>> Rotation<Vec1<N>> for Rotmat<Mat2<N>>
{ {
#[inline] #[inline]
fn rotation(&self) -> Vec1<N> fn rotation(&self) -> Vec1<N>
{ Vec1::new([-(self.submat.m12 / self.submat.m11).atan()]) } { Vec1::new([ -(self.submat.at(0, 1) / self.submat.at(0, 0)).atan() ]) }
#[inline] #[inline]
fn inv_rotation(&self) -> Vec1<N> fn inv_rotation(&self) -> Vec1<N>
@ -72,7 +72,7 @@ Rotation<Vec1<N>> for Rotmat<Mat2<N>>
{ *self = self.rotated(rot) } { *self = self.rotated(rot) }
} }
impl<N: Div<N, N> + Trigonometric + Neg<N> + Mul<N, N> + Add<N, N> + Copy> impl<N: Trigonometric + DivisionRing + Copy>
Rotatable<Vec1<N>, Rotmat<Mat2<N>>> for Rotmat<Mat2<N>> Rotatable<Vec1<N>, Rotmat<Mat2<N>>> for Rotmat<Mat2<N>>
{ {
#[inline] #[inline]
@ -80,8 +80,7 @@ Rotatable<Vec1<N>, Rotmat<Mat2<N>>> for Rotmat<Mat2<N>>
{ rotmat2(copy rot.at[0]) * *self } { rotmat2(copy rot.at[0]) * *self }
} }
impl<N: Div<N, N> + Trigonometric + Neg<N> + Mul<N, N> + Add<N, N> + Copy + impl<N: Copy + Trigonometric + DivisionRing>
One + Sub<N, N>>
Rotation<(Vec3<N>, N)> for Rotmat<Mat3<N>> Rotation<(Vec3<N>, N)> for Rotmat<Mat3<N>>
{ {
#[inline] #[inline]
@ -98,8 +97,7 @@ Rotation<(Vec3<N>, N)> for Rotmat<Mat3<N>>
{ *self = self.rotated(rot) } { *self = self.rotated(rot) }
} }
impl<N: Div<N, N> + Trigonometric + Neg<N> + Mul<N, N> + Add<N, N> + Copy + impl<N: Copy + Trigonometric + DivisionRing>
One + Sub<N, N>>
Rotatable<(Vec3<N>, N), Rotmat<Mat3<N>>> for Rotmat<Mat3<N>> Rotatable<(Vec3<N>, N), Rotmat<Mat3<N>>> for Rotmat<Mat3<N>>
{ {
#[inline] #[inline]
@ -136,8 +134,7 @@ impl<M: RMul<V> + LMul<V>, V> Transform<V> for Rotmat<M>
{ self.inv_rotate(v) } { self.inv_rotate(v) }
} }
impl<N: Copy + Rand + Trigonometric + Neg<N> + One + Sub<N, N> + Add<N, N> + impl<N: Copy + Rand + Trigonometric + Ring>
Mul<N, N>>
Rand for Rotmat<Mat3<N>> Rand for Rotmat<Mat3<N>>
{ {
#[inline] #[inline]

View File

@ -1,137 +0,0 @@
use std::num::{One, Zero};
use std::rand::{Rand, Rng, RngUtil};
use std::cmp::ApproxEq;
use traits::division_ring::DivisionRing;
use traits::dim::Dim;
use traits::inv::Inv;
use traits::transpose::Transpose;
use traits::transformation::Transform; // FIXME: implement Transformable, Transformation
use traits::rlmul::{RMul, LMul};
use vec::Vec1;
#[deriving(Eq, ToStr)]
pub struct Mat1<N>
{ m11: N }
impl<N> Mat1<N>
{
#[inline]
pub fn new(m11: N) -> Mat1<N>
{
Mat1
{ m11: m11 }
}
}
impl<N> Dim for Mat1<N>
{
#[inline]
fn dim() -> uint
{ 1 }
}
impl<N: One> One for Mat1<N>
{
#[inline]
fn one() -> Mat1<N>
{ return Mat1::new(One::one()) }
}
impl<N: Zero> Zero for Mat1<N>
{
#[inline]
fn zero() -> Mat1<N>
{ Mat1::new(Zero::zero()) }
#[inline]
fn is_zero(&self) -> bool
{ self.m11.is_zero() }
}
impl<N: Mul<N, N> + Add<N, N>> Mul<Mat1<N>, Mat1<N>> for Mat1<N>
{
#[inline]
fn mul(&self, other: &Mat1<N>) -> Mat1<N>
{ Mat1::new(self.m11 * other.m11) }
}
impl<N: Copy + DivisionRing>
Transform<Vec1<N>> for Mat1<N>
{
#[inline]
fn transform_vec(&self, v: &Vec1<N>) -> Vec1<N>
{ self.rmul(v) }
#[inline]
fn inv_transform(&self, v: &Vec1<N>) -> Vec1<N>
{ self.inverse().transform_vec(v) }
}
impl<N: Add<N, N> + Mul<N, N>> RMul<Vec1<N>> for Mat1<N>
{
#[inline]
fn rmul(&self, other: &Vec1<N>) -> Vec1<N>
{ Vec1::new([self.m11 * other.at[0]]) }
}
impl<N: Add<N, N> + Mul<N, N>> LMul<Vec1<N>> for Mat1<N>
{
#[inline]
fn lmul(&self, other: &Vec1<N>) -> Vec1<N>
{ Vec1::new([self.m11 * other.at[0]]) }
}
impl<N: Copy + DivisionRing>
Inv for Mat1<N>
{
#[inline]
fn inverse(&self) -> Mat1<N>
{
let mut res : Mat1<N> = copy *self;
res.invert();
res
}
#[inline]
fn invert(&mut self)
{
assert!(!self.m11.is_zero());
self.m11 = One::one::<N>() / self.m11
}
}
impl<N: Copy> Transpose for Mat1<N>
{
#[inline]
fn transposed(&self) -> Mat1<N>
{ copy *self }
#[inline]
fn transpose(&mut self)
{ }
}
impl<N: ApproxEq<N>> ApproxEq<N> for Mat1<N>
{
#[inline]
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
#[inline]
fn approx_eq(&self, other: &Mat1<N>) -> bool
{ self.m11.approx_eq(&other.m11) }
#[inline]
fn approx_eq_eps(&self, other: &Mat1<N>, epsilon: &N) -> bool
{ self.m11.approx_eq_eps(&other.m11, epsilon) }
}
impl<N: Rand> Rand for Mat1<N>
{
#[inline]
fn rand<R: Rng>(rng: &mut R) -> Mat1<N>
{ Mat1::new(rng.gen()) }
}

View File

@ -1,190 +0,0 @@
use std::num::{One, Zero};
use std::rand::{Rand, Rng, RngUtil};
use std::cmp::ApproxEq;
use std::util::swap;
use traits::transformation::Transform;
use traits::division_ring::DivisionRing;
use traits::dim::Dim;
use traits::inv::Inv;
use traits::transpose::Transpose;
use traits::rlmul::{RMul, LMul};
use vec::Vec2;
#[deriving(Eq, ToStr)]
pub struct Mat2<N>
{
m11: N, m12: N,
m21: N, m22: N
}
impl<N> Mat2<N>
{
#[inline]
pub fn new(m11: N, m12: N, m21: N, m22: N) -> Mat2<N>
{
Mat2
{
m11: m11, m12: m12,
m21: m21, m22: m22,
}
}
}
impl<N> Dim for Mat2<N>
{
#[inline]
fn dim() -> uint
{ 2 }
}
impl<N: Copy + One + Zero> One for Mat2<N>
{
#[inline]
fn one() -> Mat2<N>
{
let (_0, _1) = (Zero::zero(), One::one());
return Mat2::new(copy _1, copy _0,
_0, _1)
}
}
impl<N:Copy + Zero> Zero for Mat2<N>
{
#[inline]
fn zero() -> Mat2<N>
{
let _0 = Zero::zero();
return Mat2::new(copy _0, copy _0,
copy _0, _0)
}
#[inline]
fn is_zero(&self) -> bool
{
self.m11.is_zero() && self.m12.is_zero() &&
self.m21.is_zero() && self.m22.is_zero()
}
}
impl<N: Mul<N, N> + Add<N, N>> Mul<Mat2<N>, Mat2<N>> for Mat2<N>
{
#[inline]
fn mul(&self, other: &Mat2<N>) -> Mat2<N>
{
Mat2::new(
self.m11 * other.m11 + self.m12 * other.m21,
self.m11 * other.m12 + self.m12 * other.m22,
self.m21 * other.m11 + self.m22 * other.m21,
self.m21 * other.m12 + self.m22 * other.m22
)
}
}
impl<N: Copy + DivisionRing>
Transform<Vec2<N>> for Mat2<N>
{
#[inline]
fn transform_vec(&self, v: &Vec2<N>) -> Vec2<N>
{ self.rmul(v) }
#[inline]
fn inv_transform(&self, v: &Vec2<N>) -> Vec2<N>
{ self.inverse().transform_vec(v) }
}
impl<N: Add<N, N> + Mul<N, N>> RMul<Vec2<N>> for Mat2<N>
{
#[inline]
fn rmul(&self, other: &Vec2<N>) -> Vec2<N>
{
Vec2::new(
[ self.m11 * other.at[0] + self.m12 * other.at[1],
self.m21 * other.at[0] + self.m22 * other.at[1] ]
)
}
}
impl<N: Add<N, N> + Mul<N, N>> LMul<Vec2<N>> for Mat2<N>
{
#[inline]
fn lmul(&self, other: &Vec2<N>) -> Vec2<N>
{
Vec2::new(
[ self.m11 * other.at[0] + self.m21 * other.at[1],
self.m12 * other.at[0] + self.m22 * other.at[1] ]
)
}
}
impl<N: Copy + DivisionRing>
Inv for Mat2<N>
{
#[inline]
fn inverse(&self) -> Mat2<N>
{
let mut res : Mat2<N> = copy *self;
res.invert();
res
}
#[inline]
fn invert(&mut self)
{
let det = self.m11 * self.m22 - self.m21 * self.m12;
assert!(!det.is_zero());
*self = Mat2::new(self.m22 / det , -self.m12 / det,
-self.m21 / det, self.m11 / det)
}
}
impl<N: Copy> Transpose for Mat2<N>
{
#[inline]
fn transposed(&self) -> Mat2<N>
{
Mat2::new(copy self.m11, copy self.m21,
copy self.m12, copy self.m22)
}
#[inline]
fn transpose(&mut self)
{ swap(&mut self.m21, &mut self.m12); }
}
impl<N:ApproxEq<N>> ApproxEq<N> for Mat2<N>
{
#[inline]
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
#[inline]
fn approx_eq(&self, other: &Mat2<N>) -> bool
{
self.m11.approx_eq(&other.m11) &&
self.m12.approx_eq(&other.m12) &&
self.m21.approx_eq(&other.m21) &&
self.m22.approx_eq(&other.m22)
}
#[inline]
fn approx_eq_eps(&self, other: &Mat2<N>, epsilon: &N) -> bool
{
self.m11.approx_eq_eps(&other.m11, epsilon) &&
self.m12.approx_eq_eps(&other.m12, epsilon) &&
self.m21.approx_eq_eps(&other.m21, epsilon) &&
self.m22.approx_eq_eps(&other.m22, epsilon)
}
}
impl<N: Rand> Rand for Mat2<N>
{
#[inline]
fn rand<R: Rng>(rng: &mut R) -> Mat2<N>
{ Mat2::new(rng.gen(), rng.gen(), rng.gen(), rng.gen()) }
}

View File

@ -1,244 +0,0 @@
use std::num::{One, Zero};
use std::rand::{Rand, Rng, RngUtil};
use std::cmp::ApproxEq;
use std::util::swap;
use traits::dim::Dim;
use traits::inv::Inv;
use traits::transformation::Transform;
use traits::division_ring::DivisionRing;
use traits::transpose::Transpose;
use traits::rlmul::{RMul, LMul};
use vec::Vec3;
#[deriving(Eq, ToStr)]
pub struct Mat3<N>
{
m11: N, m12: N, m13: N,
m21: N, m22: N, m23: N,
m31: N, m32: N, m33: N
}
impl<N> Mat3<N>
{
#[inline]
pub fn new(m11: N, m12: N, m13: N,
m21: N, m22: N, m23: N,
m31: N, m32: N, m33: N) -> Mat3<N>
{
Mat3
{
m11: m11, m12: m12, m13: m13,
m21: m21, m22: m22, m23: m23,
m31: m31, m32: m32, m33: m33
}
}
}
impl<N> Dim for Mat3<N>
{
#[inline]
fn dim() -> uint
{ 3 }
}
impl<N: Copy + One + Zero> One for Mat3<N>
{
#[inline]
fn one() -> Mat3<N>
{
let (_0, _1) = (Zero::zero(), One::one());
return Mat3::new(copy _1, copy _0, copy _0,
copy _0, copy _1, copy _0,
copy _0, _0, _1)
}
}
impl<N: Copy + Zero> Zero for Mat3<N>
{
#[inline]
fn zero() -> Mat3<N>
{
let _0 = Zero::zero();
return Mat3::new(copy _0, copy _0, copy _0,
copy _0, copy _0, copy _0,
copy _0, copy _0, _0)
}
#[inline]
fn is_zero(&self) -> bool
{
self.m11.is_zero() && self.m12.is_zero() && self.m13.is_zero() &&
self.m21.is_zero() && self.m22.is_zero() && self.m23.is_zero() &&
self.m31.is_zero() && self.m32.is_zero() && self.m33.is_zero()
}
}
impl<N: Mul<N, N> + Add<N, N>> Mul<Mat3<N>, Mat3<N>> for Mat3<N>
{
#[inline]
fn mul(&self, other: &Mat3<N>) -> Mat3<N>
{
Mat3::new(
self.m11 * other.m11 + self.m12 * other.m21 + self.m13 * other.m31,
self.m11 * other.m12 + self.m12 * other.m22 + self.m13 * other.m32,
self.m11 * other.m13 + self.m12 * other.m23 + self.m13 * other.m33,
self.m21 * other.m11 + self.m22 * other.m21 + self.m23 * other.m31,
self.m21 * other.m12 + self.m22 * other.m22 + self.m23 * other.m32,
self.m21 * other.m13 + self.m22 * other.m23 + self.m23 * other.m33,
self.m31 * other.m11 + self.m32 * other.m21 + self.m33 * other.m31,
self.m31 * other.m12 + self.m32 * other.m22 + self.m33 * other.m32,
self.m31 * other.m13 + self.m32 * other.m23 + self.m33 * other.m33
)
}
}
impl<N: Copy + DivisionRing>
Transform<Vec3<N>> for Mat3<N>
{
#[inline]
fn transform_vec(&self, v: &Vec3<N>) -> Vec3<N>
{ self.rmul(v) }
#[inline]
fn inv_transform(&self, v: &Vec3<N>) -> Vec3<N>
{ self.inverse().transform_vec(v) }
}
impl<N: Add<N, N> + Mul<N, N>> RMul<Vec3<N>> for Mat3<N>
{
#[inline]
fn rmul(&self, other: &Vec3<N>) -> Vec3<N>
{
Vec3::new(
[self.m11 * other.at[0] + self.m12 * other.at[1] + self.m13 * other.at[2],
self.m21 * other.at[0] + self.m22 * other.at[1] + self.m33 * other.at[2],
self.m31 * other.at[0] + self.m32 * other.at[1] + self.m33 * other.at[2]]
)
}
}
impl<N: Add<N, N> + Mul<N, N>> LMul<Vec3<N>> for Mat3<N>
{
#[inline]
fn lmul(&self, other: &Vec3<N>) -> Vec3<N>
{
Vec3::new(
[self.m11 * other.at[0] + self.m21 * other.at[1] + self.m31 * other.at[2],
self.m12 * other.at[0] + self.m22 * other.at[1] + self.m32 * other.at[2],
self.m13 * other.at[0] + self.m23 * other.at[1] + self.m33 * other.at[2]]
)
}
}
impl<N: Copy + DivisionRing>
Inv for Mat3<N>
{
#[inline]
fn inverse(&self) -> Mat3<N>
{
let mut res = copy *self;
res.invert();
res
}
#[inline]
fn invert(&mut self)
{
let minor_m22_m33 = self.m22 * self.m33 - self.m32 * self.m23;
let minor_m21_m33 = self.m21 * self.m33 - self.m31 * self.m23;
let minor_m21_m32 = self.m21 * self.m32 - self.m31 * self.m22;
let det = self.m11 * minor_m22_m33
- self.m12 * minor_m21_m33
+ self.m13 * minor_m21_m32;
assert!(!det.is_zero());
*self = Mat3::new(
(minor_m22_m33 / det),
((self.m13 * self.m32 - self.m33 * self.m12) / det),
((self.m12 * self.m23 - self.m22 * self.m13) / det),
(-minor_m21_m33 / det),
((self.m11 * self.m33 - self.m31 * self.m13) / det),
((self.m13 * self.m21 - self.m23 * self.m11) / det),
(minor_m21_m32 / det),
((self.m12 * self.m31 - self.m32 * self.m11) / det),
((self.m11 * self.m22 - self.m21 * self.m12) / det)
)
}
}
impl<N:Copy> Transpose for Mat3<N>
{
#[inline]
fn transposed(&self) -> Mat3<N>
{
Mat3::new(copy self.m11, copy self.m21, copy self.m31,
copy self.m12, copy self.m22, copy self.m32,
copy self.m13, copy self.m23, copy self.m33)
}
#[inline]
fn transpose(&mut self)
{
swap(&mut self.m12, &mut self.m21);
swap(&mut self.m13, &mut self.m31);
swap(&mut self.m23, &mut self.m32);
}
}
impl<N:ApproxEq<N>> ApproxEq<N> for Mat3<N>
{
#[inline]
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
#[inline]
fn approx_eq(&self, other: &Mat3<N>) -> bool
{
self.m11.approx_eq(&other.m11) &&
self.m12.approx_eq(&other.m12) &&
self.m13.approx_eq(&other.m13) &&
self.m21.approx_eq(&other.m21) &&
self.m22.approx_eq(&other.m22) &&
self.m23.approx_eq(&other.m23) &&
self.m31.approx_eq(&other.m31) &&
self.m32.approx_eq(&other.m32) &&
self.m33.approx_eq(&other.m33)
}
#[inline]
fn approx_eq_eps(&self, other: &Mat3<N>, epsilon: &N) -> bool
{
self.m11.approx_eq_eps(&other.m11, epsilon) &&
self.m12.approx_eq_eps(&other.m12, epsilon) &&
self.m13.approx_eq_eps(&other.m13, epsilon) &&
self.m21.approx_eq_eps(&other.m21, epsilon) &&
self.m22.approx_eq_eps(&other.m22, epsilon) &&
self.m23.approx_eq_eps(&other.m23, epsilon) &&
self.m31.approx_eq_eps(&other.m31, epsilon) &&
self.m32.approx_eq_eps(&other.m32, epsilon) &&
self.m33.approx_eq_eps(&other.m33, epsilon)
}
}
impl<N: Rand> Rand for Mat3<N>
{
#[inline]
fn rand<R: Rng>(rng: &mut R) -> Mat3<N>
{
Mat3::new(rng.gen(), rng.gen(), rng.gen(),
rng.gen(), rng.gen(), rng.gen(),
rng.gen(), rng.gen(), rng.gen())
}
}

600
src/mat.rs Normal file
View File

@ -0,0 +1,600 @@
use std::uint::iterate;
use std::num::{One, Zero};
use std::vec::swap;
use std::cmp::ApproxEq;
use std::rand::{Rand, Rng, RngUtil};
use std::iterator::IteratorUtil;
use vec::{Vec1, Vec2, Vec3, Vec4, Vec5, Vec6};
use traits::dim::Dim;
use traits::ring::Ring;
use traits::inv::Inv;
use traits::division_ring::DivisionRing;
use traits::transpose::Transpose;
use traits::rlmul::{RMul, LMul};
use traits::transformation::Transform;
macro_rules! mat_impl(
($t: ident, $dim: expr) => (
impl<N> $t<N>
{
#[inline]
pub fn new(mij: [N, ..$dim * $dim]) -> $t<N>
{ $t { mij: mij } }
}
)
)
macro_rules! one_impl(
($t: ident, [ $($value: ident)|+ ] ) => (
impl<N: Copy + One + Zero> One for $t<N>
{
#[inline]
fn one() -> $t<N>
{
let (_0, _1) = (Zero::zero::<N>(), One::one::<N>());
return $t::new( [ $( copy $value, )+ ] )
}
}
)
)
macro_rules! zero_impl(
($t: ident, [ $($value: ident)|+ ] ) => (
impl<N: Copy + Zero> Zero for $t<N>
{
#[inline]
fn zero() -> $t<N>
{
let _0 = Zero::zero();
return $t::new( [ $( copy $value, )+ ] )
}
#[inline]
fn is_zero(&self) -> bool
{ self.mij.iter().all(|e| e.is_zero()) }
}
)
)
macro_rules! dim_impl(
($t: ident, $dim: expr) => (
impl<N> Dim for $t<N>
{
#[inline]
fn dim() -> uint
{ $dim }
}
)
)
macro_rules! mat_indexing_impl(
($t: ident, $dim: expr) => (
impl<N: Copy> $t<N>
{
#[inline]
pub fn offset(&self, i: uint, j: uint) -> uint
{ i * $dim + j }
#[inline]
pub fn set(&mut self, i: uint, j: uint, t: &N)
{
self.mij[self.offset(i, j)] = copy *t
}
#[inline]
pub fn at(&self, i: uint, j: uint) -> N
{
copy self.mij[self.offset(i, j)]
}
}
)
)
macro_rules! mul_impl(
($t: ident, $dim: expr) => (
impl<N: Copy + Ring>
Mul<$t<N>, $t<N>> for $t<N>
{
fn mul(&self, other: &$t<N>) -> $t<N>
{
let mut res: $t<N> = Zero::zero();
for iterate(0u, $dim) |i|
{
for iterate(0u, $dim) |j|
{
let mut acc = Zero::zero::<N>();
for iterate(0u, $dim) |k|
{ acc = acc + self.at(i, k) * other.at(k, j); }
res.set(i, j, &acc);
}
}
res
}
}
)
)
macro_rules! rmul_impl(
($t: ident, $v: ident, $dim: expr) => (
impl<N: Copy + Ring>
RMul<$v<N>> for $t<N>
{
fn rmul(&self, other: &$v<N>) -> $v<N>
{
let mut res : $v<N> = Zero::zero();
for iterate(0u, $dim) |i|
{
for iterate(0u, $dim) |j|
{ res.at[i] = res.at[i] + other.at[j] * self.at(i, j); }
}
res
}
}
)
)
macro_rules! lmul_impl(
($t: ident, $v: ident, $dim: expr) => (
impl<N: Copy + Ring>
LMul<$v<N>> for $t<N>
{
fn lmul(&self, other: &$v<N>) -> $v<N>
{
let mut res : $v<N> = Zero::zero();
for iterate(0u, $dim) |i|
{
for iterate(0u, $dim) |j|
{ res.at[i] = res.at[i] + other.at[j] * self.at(j, i); }
}
res
}
}
)
)
macro_rules! transform_impl(
($t: ident, $v: ident) => (
impl<N: Copy + DivisionRing + Eq>
Transform<$v<N>> for $t<N>
{
#[inline]
fn transform_vec(&self, v: &$v<N>) -> $v<N>
{ self.rmul(v) }
#[inline]
fn inv_transform(&self, v: &$v<N>) -> $v<N>
{ self.inverse().transform_vec(v) }
}
)
)
macro_rules! inv_impl(
($t: ident, $dim: expr) => (
impl<N: Copy + Eq + DivisionRing>
Inv for $t<N>
{
#[inline]
fn inverse(&self) -> $t<N>
{
let mut res : $t<N> = copy *self;
res.invert();
res
}
fn invert(&mut self)
{
let mut res: $t<N> = One::one();
let _0N: N = Zero::zero();
// inversion using Gauss-Jordan elimination
for iterate(0u, $dim) |k|
{
// search a non-zero value on the k-th column
// FIXME: would it be worth it to spend some more time searching for the
// max instead?
let mut n0 = k; // index of a non-zero entry
while (n0 != $dim)
{
if self.at(n0, k) != _0N
{ break; }
n0 = n0 + 1;
}
// swap pivot line
if n0 != k
{
for iterate(0u, $dim) |j|
{
let off_n0_j = self.offset(n0, j);
let off_k_j = self.offset(k, j);
swap(self.mij, off_n0_j, off_k_j);
swap(res.mij, off_n0_j, off_k_j);
}
}
let pivot = self.at(k, k);
for iterate(k, $dim) |j|
{
let selfval = &(self.at(k, j) / pivot);
self.set(k, j, selfval);
}
for iterate(0u, $dim) |j|
{
let resval = &(res.at(k, j) / pivot);
res.set(k, j, resval);
}
for iterate(0u, $dim) |l|
{
if l != k
{
let normalizer = self.at(l, k);
for iterate(k, $dim) |j|
{
let selfval = &(self.at(l, j) - self.at(k, j) * normalizer);
self.set(l, j, selfval);
}
for iterate(0u, $dim) |j|
{
let resval = &(res.at(l, j) - res.at(k, j) * normalizer);
res.set(l, j, resval);
}
}
}
}
*self = res;
}
}
)
)
macro_rules! transpose_impl(
($t: ident, $dim: expr) => (
impl<N: Copy> Transpose for $t<N>
{
#[inline]
fn transposed(&self) -> $t<N>
{
let mut res = copy *self;
res.transpose();
res
}
fn transpose(&mut self)
{
for iterate(1u, $dim) |i|
{
for iterate(0u, $dim - 1) |j|
{
let off_i_j = self.offset(i, j);
let off_j_i = self.offset(j, i);
swap(self.mij, off_i_j, off_j_i);
}
}
}
}
)
)
macro_rules! approx_eq_impl(
($t: ident) => (
impl<N: ApproxEq<N>> ApproxEq<N> for $t<N>
{
#[inline]
fn approx_epsilon() -> N
{ ApproxEq::approx_epsilon::<N, N>() }
#[inline]
fn approx_eq(&self, other: &$t<N>) -> bool
{
let mut zip = self.mij.iter().zip(other.mij.iter());
do zip.all |(a, b)| { a.approx_eq(b) }
}
#[inline]
fn approx_eq_eps(&self, other: &$t<N>, epsilon: &N) -> bool
{
let mut zip = self.mij.iter().zip(other.mij.iter());
do zip.all |(a, b)| { a.approx_eq_eps(b, epsilon) }
}
}
)
)
macro_rules! rand_impl(
($t: ident, $param: ident, [ $($elem: ident)|+ ]) => (
impl<N: Rand> Rand for $t<N>
{
#[inline]
fn rand<R: Rng>($param: &mut R) -> $t<N>
{ $t::new([ $( $elem.gen(), )+ ]) }
}
)
)
#[deriving(ToStr)]
pub struct Mat1<N>
{ mij: [N, ..1 * 1] }
mat_impl!(Mat1, 1)
one_impl!(Mat1, [ _1 ])
zero_impl!(Mat1, [ _0 ])
dim_impl!(Mat1, 1)
mat_indexing_impl!(Mat1, 1)
mul_impl!(Mat1, 1)
rmul_impl!(Mat1, Vec1, 1)
lmul_impl!(Mat1, Vec1, 1)
transform_impl!(Mat1, Vec1)
// inv_impl!(Mat1, 1)
transpose_impl!(Mat1, 1)
approx_eq_impl!(Mat1)
rand_impl!(Mat1, rng, [ rng ])
#[deriving(ToStr)]
pub struct Mat2<N>
{ mij: [N, ..2 * 2] }
mat_impl!(Mat2, 2)
one_impl!(Mat2, [ _1 | _0 |
_0 | _1 ])
zero_impl!(Mat2, [ _0 | _0 |
_0 | _0 ])
dim_impl!(Mat2, 2)
mat_indexing_impl!(Mat2, 2)
mul_impl!(Mat2, 2)
rmul_impl!(Mat2, Vec2, 2)
lmul_impl!(Mat2, Vec2, 2)
transform_impl!(Mat2, Vec2)
// inv_impl!(Mat2, 2)
transpose_impl!(Mat2, 2)
approx_eq_impl!(Mat2)
rand_impl!(Mat2, rng, [ rng | rng |
rng | rng ])
#[deriving(ToStr)]
pub struct Mat3<N>
{ mij: [N, ..3 * 3] }
mat_impl!(Mat3, 3)
one_impl!(Mat3, [ _1 | _0 | _0 |
_0 | _1 | _0 |
_0 | _0 | _1 ])
zero_impl!(Mat3, [ _0 | _0 | _0 |
_0 | _0 | _0 |
_0 | _0 | _0 ])
dim_impl!(Mat3, 3)
mat_indexing_impl!(Mat3, 3)
mul_impl!(Mat3, 3)
rmul_impl!(Mat3, Vec3, 3)
lmul_impl!(Mat3, Vec3, 3)
transform_impl!(Mat3, Vec3)
// inv_impl!(Mat3, 3)
transpose_impl!(Mat3, 3)
approx_eq_impl!(Mat3)
rand_impl!(Mat3, rng, [ rng | rng | rng |
rng | rng | rng |
rng | rng | rng])
#[deriving(ToStr)]
pub struct Mat4<N>
{ mij: [N, ..4 * 4] }
mat_impl!(Mat4, 4)
one_impl!(Mat4, [
_1 | _0 | _0 | _0 |
_0 | _1 | _0 | _0 |
_0 | _0 | _1 | _0 |
_0 | _0 | _0 | _1
])
zero_impl!(Mat4, [
_0 | _0 | _0 | _0 |
_0 | _0 | _0 | _0 |
_0 | _0 | _0 | _0 |
_0 | _0 | _0 | _0
])
dim_impl!(Mat4, 4)
mat_indexing_impl!(Mat4, 4)
mul_impl!(Mat4, 4)
rmul_impl!(Mat4, Vec4, 4)
lmul_impl!(Mat4, Vec4, 4)
transform_impl!(Mat4, Vec4)
inv_impl!(Mat4, 4)
transpose_impl!(Mat4, 4)
approx_eq_impl!(Mat4)
rand_impl!(Mat4, rng, [
rng | rng | rng | rng |
rng | rng | rng | rng |
rng | rng | rng | rng |
rng | rng | rng | rng
])
#[deriving(ToStr)]
pub struct Mat5<N>
{ mij: [N, ..5 * 5] }
mat_impl!(Mat5, 5)
one_impl!(Mat5, [
_1 | _0 | _0 | _0 | _0 |
_0 | _1 | _0 | _0 | _0 |
_0 | _0 | _1 | _0 | _0 |
_0 | _0 | _0 | _1 | _0 |
_0 | _0 | _0 | _0 | _1
])
zero_impl!(Mat5, [
_0 | _0 | _0 | _0 | _0 |
_0 | _0 | _0 | _0 | _0 |
_0 | _0 | _0 | _0 | _0 |
_0 | _0 | _0 | _0 | _0 |
_0 | _0 | _0 | _0 | _0
])
dim_impl!(Mat5, 5)
mat_indexing_impl!(Mat5, 5)
mul_impl!(Mat5, 5)
rmul_impl!(Mat5, Vec5, 5)
lmul_impl!(Mat5, Vec5, 5)
transform_impl!(Mat5, Vec5)
inv_impl!(Mat5, 5)
transpose_impl!(Mat5, 5)
approx_eq_impl!(Mat5)
rand_impl!(Mat5, rng, [
rng | rng | rng | rng | rng |
rng | rng | rng | rng | rng |
rng | rng | rng | rng | rng |
rng | rng | rng | rng | rng |
rng | rng | rng | rng | rng
])
#[deriving(ToStr)]
pub struct Mat6<N>
{ mij: [N, ..6 * 6] }
mat_impl!(Mat6, 6)
one_impl!(Mat6, [
_1 | _0 | _0 | _0 | _0 | _0 |
_0 | _1 | _0 | _0 | _0 | _0 |
_0 | _0 | _1 | _0 | _0 | _0 |
_0 | _0 | _0 | _1 | _0 | _0 |
_0 | _0 | _0 | _0 | _1 | _0 |
_0 | _0 | _0 | _0 | _0 | _1
])
zero_impl!(Mat6, [
_0 | _0 | _0 | _0 | _0 | _0 |
_0 | _0 | _0 | _0 | _0 | _0 |
_0 | _0 | _0 | _0 | _0 | _0 |
_0 | _0 | _0 | _0 | _0 | _0 |
_0 | _0 | _0 | _0 | _0 | _0 |
_0 | _0 | _0 | _0 | _0 | _0
])
dim_impl!(Mat6, 6)
mat_indexing_impl!(Mat6, 6)
mul_impl!(Mat6, 6)
rmul_impl!(Mat6, Vec6, 6)
lmul_impl!(Mat6, Vec6, 6)
transform_impl!(Mat6, Vec6)
inv_impl!(Mat6, 6)
transpose_impl!(Mat6, 6)
approx_eq_impl!(Mat6)
rand_impl!(Mat6, rng, [
rng | rng | rng | rng | rng | rng |
rng | rng | rng | rng | rng | rng |
rng | rng | rng | rng | rng | rng |
rng | rng | rng | rng | rng | rng |
rng | rng | rng | rng | rng | rng |
rng | rng | rng | rng | rng | rng
])
// some specializations:
impl<N: Copy + DivisionRing>
Inv for Mat1<N>
{
#[inline]
fn inverse(&self) -> Mat1<N>
{
let mut res : Mat1<N> = copy *self;
res.invert();
res
}
#[inline]
fn invert(&mut self)
{
assert!(!self.mij[0].is_zero());
self.mij[0] = One::one::<N>() / self.mij[0]
}
}
impl<N: Copy + DivisionRing>
Inv for Mat2<N>
{
#[inline]
fn inverse(&self) -> Mat2<N>
{
let mut res : Mat2<N> = copy *self;
res.invert();
res
}
#[inline]
fn invert(&mut self)
{
let det = self.mij[0 * 2 + 0] * self.mij[1 * 2 + 1] - self.mij[1 * 2 + 0] * self.mij[0 * 2 + 1];
assert!(!det.is_zero());
*self = Mat2::new([self.mij[1 * 2 + 1] / det , -self.mij[0 * 2 + 1] / det,
-self.mij[1 * 2 + 0] / det, self.mij[0 * 2 + 0] / det])
}
}
impl<N: Copy + DivisionRing>
Inv for Mat3<N>
{
#[inline]
fn inverse(&self) -> Mat3<N>
{
let mut res = copy *self;
res.invert();
res
}
#[inline]
fn invert(&mut self)
{
let minor_m12_m23 = self.mij[1 * 3 + 1] * self.mij[2 * 3 + 2] - self.mij[2 * 3 + 1] * self.mij[1 * 3 + 2];
let minor_m11_m23 = self.mij[1 * 3 + 0] * self.mij[2 * 3 + 2] - self.mij[2 * 3 + 0] * self.mij[1 * 3 + 2];
let minor_m11_m22 = self.mij[1 * 3 + 0] * self.mij[2 * 3 + 1] - self.mij[2 * 3 + 0] * self.mij[1 * 3 + 1];
let det = self.mij[0 * 3 + 0] * minor_m12_m23
- self.mij[0 * 3 + 1] * minor_m11_m23
+ self.mij[0 * 3 + 2] * minor_m11_m22;
assert!(!det.is_zero());
*self = Mat3::new( [
(minor_m12_m23 / det),
((self.mij[0 * 3 + 2] * self.mij[2 * 3 + 1] - self.mij[2 * 3 + 2] * self.mij[0 * 3 + 1]) / det),
((self.mij[0 * 3 + 1] * self.mij[1 * 3 + 2] - self.mij[1 * 3 + 1] * self.mij[0 * 3 + 2]) / det),
(-minor_m11_m23 / det),
((self.mij[0 * 3 + 0] * self.mij[2 * 3 + 2] - self.mij[2 * 3 + 0] * self.mij[0 * 3 + 2]) / det),
((self.mij[0 * 3 + 2] * self.mij[1 * 3 + 0] - self.mij[1 * 3 + 2] * self.mij[0 * 3 + 0]) / det),
(minor_m11_m22 / det),
((self.mij[0 * 3 + 1] * self.mij[2 * 3 + 0] - self.mij[2 * 3 + 1] * self.mij[0 * 3 + 0]) / det),
((self.mij[0 * 3 + 0] * self.mij[1 * 3 + 1] - self.mij[1 * 3 + 0] * self.mij[0 * 3 + 1]) / det)
] )
}
}

View File

@ -16,24 +16,7 @@
extern mod std; extern mod std;
pub mod vec; pub mod vec;
pub mod mat;
/// 1-dimensional linear algebra.
pub mod dim1
{
pub mod mat1;
}
/// 2-dimensional linear algebra.
pub mod dim2
{
pub mod mat2;
}
/// 3-dimensional linear algebra.
pub mod dim3
{
pub mod mat3;
}
/// n-dimensional linear algebra (slower). /// n-dimensional linear algebra (slower).
pub mod ndim pub mod ndim

View File

@ -11,11 +11,7 @@ use traits::rotation::{Rotation, Rotatable};
#[test] #[test]
use vec::Vec1; use vec::Vec1;
#[test] #[test]
use dim1::mat1::Mat1; use mat::{Mat1, Mat2, Mat3};
#[test]
use dim2::mat2::Mat2;
#[test]
use dim3::mat3::Mat3;
#[test] #[test]
use adaptors::rotmat::Rotmat; use adaptors::rotmat::Rotmat;