2013-07-22 16:26:20 +08:00
|
|
|
#[macro_escape];
|
|
|
|
|
|
|
|
macro_rules! mat_impl(
|
2013-07-23 05:42:35 +08:00
|
|
|
($t: ident, $comp0: ident $(,$compN: ident)*) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N> $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
pub fn new($comp0: N $(, $compN: N )*) -> $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
$t {
|
|
|
|
$comp0: $comp0
|
|
|
|
$(, $compN: $compN )*
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-07-23 05:42:35 +08:00
|
|
|
macro_rules! mat_cast_impl(
|
|
|
|
($t: ident, $comp0: ident $(,$compN: ident)*) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<Nin: NumCast + Clone, Nout: NumCast> MatCast<$t<Nout>> for $t<Nin> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
pub fn from(m: $t<Nin>) -> $t<Nout> {
|
|
|
|
$t::new(NumCast::from(m.$comp0.clone()) $(, NumCast::from(m.$compN.clone()) )*)
|
|
|
|
}
|
2013-07-23 05:42:35 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-07-22 16:26:20 +08:00
|
|
|
macro_rules! iterable_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N> Iterable<N> for $t<N> {
|
|
|
|
fn iter<'l>(&'l self) -> VecIterator<'l, N> {
|
|
|
|
unsafe {
|
|
|
|
cast::transmute::<&'l $t<N>, &'l [N, ..$dim * $dim]>(self).iter()
|
|
|
|
}
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! iterable_mut_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N> IterableMut<N> for $t<N> {
|
|
|
|
fn mut_iter<'l>(&'l mut self) -> VecMutIterator<'l, N> {
|
|
|
|
unsafe {
|
|
|
|
cast::transmute::<&'l mut $t<N>, &'l mut [N, ..$dim * $dim]>(self).mut_iter()
|
|
|
|
}
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! one_impl(
|
|
|
|
($t: ident, $value0: ident $(, $valueN: ident)* ) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: Clone + One + Zero> One for $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn one() -> $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
let (_0, _1) = (Zero::zero::<N>(), One::one::<N>());
|
|
|
|
return $t::new($value0.clone() $(, $valueN.clone() )*)
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! dim_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N> Dim for $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn dim() -> uint {
|
|
|
|
$dim
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! indexable_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: Clone> Indexable<(uint, uint), N> for $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
pub fn at(&self, (i, j): (uint, uint)) -> N {
|
|
|
|
unsafe {
|
|
|
|
cast::transmute::<&$t<N>, &[N, ..$dim * $dim]>(self)[i * $dim + j].clone()
|
|
|
|
}
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
|
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
pub fn set(&mut self, (i, j): (uint, uint), val: N) {
|
|
|
|
unsafe {
|
|
|
|
cast::transmute::<&mut $t<N>, &mut [N, ..$dim * $dim]>(self)[i * $dim + j] = val
|
|
|
|
}
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
|
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
pub fn swap(&mut self, (i1, j1): (uint, uint), (i2, j2): (uint, uint)) {
|
2013-08-05 15:44:56 +08:00
|
|
|
unsafe {
|
|
|
|
cast::transmute::<&mut $t<N>, &mut [N, ..$dim * $dim]>(self)
|
|
|
|
.swap(i1 * $dim + j1, i2 * $dim + j2)
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! column_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: Clone, V: Zero + Iterable<N> + IterableMut<N>> Column<V> for $t<N> {
|
|
|
|
fn set_column(&mut self, col: uint, v: V) {
|
|
|
|
for (i, e) in v.iter().enumerate() {
|
|
|
|
if i == Dim::dim::<$t<N>>() {
|
|
|
|
break
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
self.set((i, col), e.clone());
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
fn column(&self, col: uint) -> V {
|
2013-08-05 15:44:56 +08:00
|
|
|
let mut res = Zero::zero::<V>();
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for (i, e) in res.mut_iter().enumerate() {
|
|
|
|
if i >= Dim::dim::<$t<N>>() {
|
|
|
|
break
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
|
|
|
|
*e = self.at((i, col));
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
res
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! mul_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: Clone + Ring> Mul<$t<N>, $t<N>> for $t<N> {
|
|
|
|
fn mul(&self, other: &$t<N>) -> $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
let mut res: $t<N> = Zero::zero();
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for i in range(0u, $dim) {
|
|
|
|
for j in range(0u, $dim) {
|
2013-08-05 15:44:56 +08:00
|
|
|
let mut acc = Zero::zero::<N>();
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for k in range(0u, $dim) {
|
|
|
|
acc = acc + self.at((i, k)) * other.at((k, j));
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
|
|
|
|
res.set((i, j), acc);
|
|
|
|
}
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
res
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! rmul_impl(
|
|
|
|
($t: ident, $v: ident, $dim: expr) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: Clone + Ring> RMul<$v<N>> for $t<N> {
|
|
|
|
fn rmul(&self, other: &$v<N>) -> $v<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
let mut res : $v<N> = Zero::zero();
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for i in range(0u, $dim) {
|
|
|
|
for j in range(0u, $dim) {
|
2013-08-05 15:44:56 +08:00
|
|
|
let val = res.at(i) + other.at(j) * self.at((i, j));
|
|
|
|
res.set(i, val)
|
|
|
|
}
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
res
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! lmul_impl(
|
|
|
|
($t: ident, $v: ident, $dim: expr) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: Clone + Ring> LMul<$v<N>> for $t<N> {
|
|
|
|
fn lmul(&self, other: &$v<N>) -> $v<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
let mut res : $v<N> = Zero::zero();
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for i in range(0u, $dim) {
|
|
|
|
for j in range(0u, $dim) {
|
2013-08-05 15:44:56 +08:00
|
|
|
let val = res.at(i) + other.at(j) * self.at((j, i));
|
|
|
|
res.set(i, val)
|
|
|
|
}
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
res
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! transform_impl(
|
|
|
|
($t: ident, $v: ident) => (
|
|
|
|
impl<N: Clone + DivisionRing + Eq>
|
2013-08-05 16:13:44 +08:00
|
|
|
Transform<$v<N>> for $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn transform_vec(&self, v: &$v<N>) -> $v<N> {
|
|
|
|
self.rmul(v)
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn inv_transform(&self, v: &$v<N>) -> $v<N> {
|
|
|
|
match self.inverse() {
|
2013-08-05 15:44:56 +08:00
|
|
|
Some(t) => t.transform_vec(v),
|
|
|
|
None => fail!("Cannot use inv_transform on a non-inversible matrix.")
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! inv_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
|
|
|
impl<N: Clone + Eq + DivisionRing>
|
2013-08-05 16:13:44 +08:00
|
|
|
Inv for $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn inverse(&self) -> Option<$t<N>> {
|
2013-08-05 15:44:56 +08:00
|
|
|
let mut res : $t<N> = self.clone();
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
if res.inplace_inverse() {
|
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
None
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
fn inplace_inverse(&mut self) -> bool {
|
2013-08-05 15:44:56 +08:00
|
|
|
let mut res: $t<N> = One::one();
|
|
|
|
let _0N: N = Zero::zero();
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
// inversion using Gauss-Jordan elimination
|
2013-08-05 16:13:44 +08:00
|
|
|
for k in range(0u, $dim) {
|
2013-08-05 15:44:56 +08:00
|
|
|
// 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
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
while (n0 != $dim) {
|
|
|
|
if self.at((n0, k)) != _0N {
|
|
|
|
break;
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
|
|
|
|
n0 = n0 + 1;
|
|
|
|
}
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
if n0 == $dim {
|
|
|
|
return false
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
|
|
|
|
// swap pivot line
|
2013-08-05 16:13:44 +08:00
|
|
|
if n0 != k {
|
|
|
|
for j in range(0u, $dim) {
|
2013-08-05 15:44:56 +08:00
|
|
|
self.swap((n0, j), (k, j));
|
|
|
|
res.swap((n0, j), (k, j));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let pivot = self.at((k, k));
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for j in range(k, $dim) {
|
2013-08-05 15:44:56 +08:00
|
|
|
let selfval = self.at((k, j)) / pivot;
|
|
|
|
self.set((k, j), selfval);
|
|
|
|
}
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for j in range(0u, $dim) {
|
2013-08-05 15:44:56 +08:00
|
|
|
let resval = res.at((k, j)) / pivot;
|
|
|
|
res.set((k, j), resval);
|
|
|
|
}
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for l in range(0u, $dim) {
|
|
|
|
if l != k {
|
2013-08-05 15:44:56 +08:00
|
|
|
let normalizer = self.at((l, k));
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for j in range(k, $dim) {
|
2013-08-05 15:44:56 +08:00
|
|
|
let selfval = self.at((l, j)) - self.at((k, j)) * normalizer;
|
|
|
|
self.set((l, j), selfval);
|
|
|
|
}
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for j in range(0u, $dim) {
|
2013-08-05 15:44:56 +08:00
|
|
|
let resval = res.at((l, j)) - res.at((k, j)) * normalizer;
|
|
|
|
res.set((l, j), resval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
*self = res;
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
true
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! transpose_impl(
|
|
|
|
($t: ident, $dim: expr) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: Clone> Transpose for $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn transposed(&self) -> $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
let mut res = self.clone();
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
res.transpose();
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
res
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
fn transpose(&mut self) {
|
|
|
|
for i in range(1u, $dim) {
|
|
|
|
for j in range(0u, i) {
|
|
|
|
self.swap((i, j), (j, i))
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! approx_eq_impl(
|
|
|
|
($t: ident) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: ApproxEq<N>> ApproxEq<N> for $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn approx_epsilon() -> N {
|
|
|
|
ApproxEq::approx_epsilon::<N, N>()
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn approx_eq(&self, other: &$t<N>) -> bool {
|
2013-08-05 15:44:56 +08:00
|
|
|
let mut zip = self.iter().zip(other.iter());
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
do zip.all |(a, b)| {
|
|
|
|
a.approx_eq(b)
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn approx_eq_eps(&self, other: &$t<N>, epsilon: &N) -> bool {
|
2013-08-05 15:44:56 +08:00
|
|
|
let mut zip = self.iter().zip(other.iter());
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
do zip.all |(a, b)| {
|
|
|
|
a.approx_eq_eps(b, epsilon)
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! to_homogeneous_impl(
|
|
|
|
($t: ident, $t2: ident, $dim: expr, $dim2: expr) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: One + Zero + Clone> ToHomogeneous<$t2<N>> for $t<N> {
|
|
|
|
fn to_homogeneous(&self) -> $t2<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
let mut res: $t2<N> = One::one();
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for i in range(0u, $dim) {
|
|
|
|
for j in range(0u, $dim) {
|
|
|
|
res.set((i, j), self.at((i, j)))
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! from_homogeneous_impl(
|
|
|
|
($t: ident, $t2: ident, $dim: expr, $dim2: expr) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
impl<N: One + Zero + Clone> FromHomogeneous<$t2<N>> for $t<N> {
|
|
|
|
fn from(m: &$t2<N>) -> $t<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
let mut res: $t<N> = One::one();
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for i in range(0u, $dim2) {
|
|
|
|
for j in range(0u, $dim2) {
|
|
|
|
res.set((i, j), m.at((i, j)))
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
// FIXME: do we have to deal the lost components
|
|
|
|
// (like if the 1 is not a 1… do we have to divide?)
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
2013-07-22 16:26:20 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|