nalgebra/src/vec_impl.rs

420 lines
9.5 KiB
Rust
Raw Normal View History

2013-06-29 08:34:45 +08:00
#[macro_escape];
macro_rules! new_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => (
2013-06-29 08:34:45 +08:00
impl<N> $t<N>
{
#[inline]
pub fn new($comp0: N $( , $compN: N )*) -> $t<N>
{
$t {
$comp0: $comp0
$(, $compN: $compN )*
}
}
2013-06-29 08:34:45 +08:00
}
)
)
macro_rules! indexable_impl(
($t: ident, $dim: expr) => (
impl<N: Clone> Indexable<uint, N> for $t<N>
{
#[inline]
pub fn at(&self, i: uint) -> N
{ unsafe { cast::transmute::<&$t<N>, &[N, ..$dim]>(self)[i].clone() } }
#[inline]
pub fn set(&mut self, i: uint, val: N)
{ unsafe { cast::transmute::<&mut $t<N>, &mut [N, ..$dim]>(self)[i] = val } }
#[inline]
pub fn swap(&mut self, i1: uint, i2: uint)
{ unsafe { cast::transmute::<&mut $t<N>, &mut [N, ..$dim]>(self).swap(i1, i2) } }
}
)
)
2013-06-29 08:34:45 +08:00
macro_rules! new_repeat_impl(
($t: ident, $param: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Clone> $t<N>
2013-06-29 08:34:45 +08:00
{
#[inline]
pub fn new_repeat($param: N) -> $t<N>
{
$t{
$comp0: $param.clone()
$(, $compN: $param.clone() )*
}
}
2013-06-29 08:34:45 +08:00
}
)
)
macro_rules! iterable_impl(
($t: ident, $dim: expr) => (
2013-06-29 08:34:45 +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]>(self).iter() } }
2013-06-29 08:34:45 +08:00
}
)
)
macro_rules! iterable_mut_impl(
($t: ident, $dim: expr) => (
2013-06-29 08:34:45 +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]>(self).mut_iter() } }
2013-06-29 08:34:45 +08:00
}
)
)
macro_rules! dim_impl(
($t: ident, $dim: expr) => (
impl<N> Dim for $t<N>
{
#[inline]
fn dim() -> uint
{ $dim }
}
)
)
// FIXME: add the possibility to specialize that
macro_rules! basis_impl(
($t: ident, $dim: expr) => (
impl<N: Clone + DivisionRing + Algebraic + ApproxEq<N>> Basis for $t<N>
2013-06-29 08:34:45 +08:00
{
pub fn canonical_basis(f: &fn($t<N>))
2013-06-29 08:34:45 +08:00
{
for iterate(0u, $dim) |i|
{
let mut basis_element : $t<N> = Zero::zero();
basis_element.set(i, One::one());
2013-06-29 08:34:45 +08:00
f(basis_element);
2013-06-29 08:34:45 +08:00
}
}
pub fn orthonormal_subspace_basis(&self, f: &fn($t<N>))
2013-06-29 08:34:45 +08:00
{
// compute the basis of the orthogonal subspace using Gram-Schmidt
// orthogonalization algorithm
let mut basis: ~[$t<N>] = ~[];
2013-06-29 08:34:45 +08:00
for iterate(0u, $dim) |i|
{
let mut basis_element : $t<N> = Zero::zero();
basis_element.set(i, One::one());
2013-06-29 08:34:45 +08:00
if basis.len() == $dim - 1
2013-06-29 08:34:45 +08:00
{ break; }
let mut elt = basis_element.clone();
2013-06-29 08:34:45 +08:00
elt = elt - self.scalar_mul(&basis_element.dot(self));
for basis.iter().advance |v|
2013-06-29 08:34:45 +08:00
{ elt = elt - v.scalar_mul(&elt.dot(v)) };
if !elt.sqnorm().approx_eq(&Zero::zero())
{
let new_element = elt.normalized();
f(new_element.clone());
basis.push(new_element);
}
2013-06-29 08:34:45 +08:00
}
}
}
)
)
macro_rules! add_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Clone + Add<N,N>> Add<$t<N>, $t<N>> for $t<N>
2013-06-29 08:34:45 +08:00
{
#[inline]
fn add(&self, other: &$t<N>) -> $t<N>
{ $t::new(self.$comp0 + other.$comp0 $(, self.$compN + other.$compN)*) }
2013-06-29 08:34:45 +08:00
}
)
)
macro_rules! sub_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Clone + Sub<N,N>> Sub<$t<N>, $t<N>> for $t<N>
2013-06-29 08:34:45 +08:00
{
#[inline]
fn sub(&self, other: &$t<N>) -> $t<N>
{ $t::new(self.$comp0 - other.$comp0 $(, self.$compN - other.$compN)*) }
2013-06-29 08:34:45 +08:00
}
)
)
macro_rules! neg_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => (
2013-06-29 08:34:45 +08:00
impl<N: Neg<N>> Neg<$t<N>> for $t<N>
{
#[inline]
fn neg(&self) -> $t<N>
{ $t::new(-self.$comp0 $(, -self.$compN )*) }
2013-06-29 08:34:45 +08:00
}
)
)
macro_rules! dot_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => (
2013-06-29 08:34:45 +08:00
impl<N: Ring> Dot<N> for $t<N>
{
#[inline]
fn dot(&self, other: &$t<N>) -> N
{ self.$comp0 * other.$comp0 $(+ self.$compN * other.$compN )* }
2013-06-29 08:34:45 +08:00
}
)
)
macro_rules! sub_dot_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => (
2013-06-29 08:34:45 +08:00
impl<N: Ring> SubDot<N> for $t<N>
{
#[inline]
fn sub_dot(&self, a: &$t<N>, b: &$t<N>) -> N
{ (self.$comp0 - a.$comp0) * b.$comp0 $(+ (self.$compN - a.$comp0) * b.$compN )* }
2013-06-29 08:34:45 +08:00
}
)
)
macro_rules! scalar_mul_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => (
2013-06-29 08:34:45 +08:00
impl<N: Mul<N, N>> ScalarMul<N> for $t<N>
{
#[inline]
fn scalar_mul(&self, s: &N) -> $t<N>
{ $t::new(self.$comp0 * *s $(, self.$compN * *s)*) }
2013-06-29 08:34:45 +08:00
#[inline]
fn scalar_mul_inplace(&mut self, s: &N)
{
self.$comp0 = self.$comp0 * *s;
$(self.$compN = self.$compN * *s;)*
2013-06-29 08:34:45 +08:00
}
}
)
)
macro_rules! scalar_div_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => (
2013-06-29 08:34:45 +08:00
impl<N: Div<N, N>> ScalarDiv<N> for $t<N>
{
#[inline]
fn scalar_div(&self, s: &N) -> $t<N>
{ $t::new(self.$comp0 / *s $(, self.$compN / *s)*) }
2013-06-29 08:34:45 +08:00
#[inline]
fn scalar_div_inplace(&mut self, s: &N)
{
self.$comp0 = self.$comp0 / *s;
$(self.$compN = self.$compN / *s;)*
2013-06-29 08:34:45 +08:00
}
}
)
)
macro_rules! scalar_add_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => (
2013-06-29 08:34:45 +08:00
impl<N: Add<N, N>> ScalarAdd<N> for $t<N>
{
#[inline]
fn scalar_add(&self, s: &N) -> $t<N>
{ $t::new(self.$comp0 + *s $(, self.$compN + *s)*) }
2013-06-29 08:34:45 +08:00
#[inline]
fn scalar_add_inplace(&mut self, s: &N)
{
self.$comp0 = self.$comp0 + *s;
$(self.$compN = self.$compN + *s;)*
2013-06-29 08:34:45 +08:00
}
}
)
)
macro_rules! scalar_sub_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => (
2013-06-29 08:34:45 +08:00
impl<N: Sub<N, N>> ScalarSub<N> for $t<N>
{
#[inline]
fn scalar_sub(&self, s: &N) -> $t<N>
{ $t::new(self.$comp0 - *s $(, self.$compN - *s)*) }
2013-06-29 08:34:45 +08:00
#[inline]
fn scalar_sub_inplace(&mut self, s: &N)
{
self.$comp0 = self.$comp0 - *s;
$(self.$compN = self.$compN - *s;)*
2013-06-29 08:34:45 +08:00
}
}
)
)
macro_rules! translation_impl(
($t: ident) => (
impl<N: Clone + Add<N, N> + Neg<N>> Translation<$t<N>> for $t<N>
2013-06-29 08:34:45 +08:00
{
#[inline]
fn translation(&self) -> $t<N>
{ self.clone() }
2013-06-29 08:34:45 +08:00
#[inline]
fn inv_translation(&self) -> $t<N>
{ -self }
#[inline]
fn translate_by(&mut self, t: &$t<N>)
{ *self = *self + *t; }
}
)
)
macro_rules! translatable_impl(
($t: ident) => (
impl<N: Add<N, N> + Neg<N> + Clone> Translatable<$t<N>, $t<N>> for $t<N>
2013-06-29 08:34:45 +08:00
{
#[inline]
fn translated(&self, t: &$t<N>) -> $t<N>
{ self + *t }
}
)
)
macro_rules! norm_impl(
($t: ident) => (
impl<N: Clone + DivisionRing + Algebraic> Norm<N> for $t<N>
2013-06-29 08:34:45 +08:00
{
#[inline]
fn sqnorm(&self) -> N
{ self.dot(self) }
#[inline]
fn norm(&self) -> N
{ self.sqnorm().sqrt() }
#[inline]
fn normalized(&self) -> $t<N>
{
let mut res : $t<N> = self.clone();
2013-06-29 08:34:45 +08:00
res.normalize();
res
}
#[inline]
fn normalize(&mut self) -> N
{
let l = self.norm();
self.scalar_div_inplace(&l);
2013-06-29 08:34:45 +08:00
l
}
}
)
)
macro_rules! approx_eq_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => (
2013-06-29 08:34:45 +08:00
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
{ self.$comp0.approx_eq(&other.$comp0) $(&& self.$compN.approx_eq(&other.$compN))* }
2013-06-29 08:34:45 +08:00
#[inline]
fn approx_eq_eps(&self, other: &$t<N>, eps: &N) -> bool
{ self.$comp0.approx_eq_eps(&other.$comp0, eps) $(&& self.$compN.approx_eq_eps(&other.$compN, eps))* }
2013-06-29 08:34:45 +08:00
}
)
)
macro_rules! one_impl(
($t: ident) => (
impl<N: Clone + One> One for $t<N>
{
#[inline]
fn one() -> $t<N>
{ $t::new_repeat(One::one()) }
}
)
)
2013-06-29 08:34:45 +08:00
macro_rules! from_iterator_impl(
($t: ident, $param0: ident $(, $paramN: ident)*) => (
2013-06-29 08:34:45 +08:00
impl<N, Iter: Iterator<N>> FromIterator<N, Iter> for $t<N>
{
fn from_iterator($param0: &mut Iter) -> $t<N>
{ $t::new($param0.next().unwrap() $(, $paramN.next().unwrap())*) }
2013-06-29 08:34:45 +08:00
}
)
)
macro_rules! bounded_impl(
($t: ident) => (
impl<N: Bounded + Clone> Bounded for $t<N>
2013-06-29 08:34:45 +08:00
{
#[inline]
fn max_value() -> $t<N>
{ $t::new_repeat(Bounded::max_value()) }
#[inline]
fn min_value() -> $t<N>
{ $t::new_repeat(Bounded::min_value()) }
}
)
)
macro_rules! to_homogeneous_impl(
($t: ident, $t2: ident, $extra: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Clone + One + Zero> ToHomogeneous<$t2<N>> for $t<N>
{
fn to_homogeneous(&self) -> $t2<N>
{
let mut res: $t2<N> = One::one();
res.$comp0 = self.$comp0.clone();
$( res.$compN = self.$compN.clone(); )*
res
}
}
)
)
macro_rules! from_homogeneous_impl(
($t: ident, $t2: ident, $extra: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Clone + Div<N, N> + One + Zero> FromHomogeneous<$t2<N>> for $t<N>
{
fn from_homogeneous(v: &$t2<N>) -> $t<N>
{
let mut res: $t<N> = Zero::zero();
res.$comp0 = v.$comp0.clone();
$( res.$compN = v.$compN.clone(); )*
res.scalar_div(&v.$extra);
res
}
}
)
)