Removed occurences of copy/Copy + improved api.

Now, access to vector components are x, y, z, w, a, b, ... instead of at[i].
The method at(i) has the same (read only) effect as the old at[i].

Now, access to matrix components are m11, m12, ... instead of mij[offset(i, j)]...
The method at((i, j)) has the same effect as the old mij[offset(i, j)].

Automatic implementation of all traits the compiler supports has been added on the #[deriving]
clause for both matrices and vectors.
This commit is contained in:
Sébastien Crozet 2013-07-20 15:07:49 +02:00
parent 8918cb5d7e
commit cf216f9b90
17 changed files with 565 additions and 743 deletions

3
pkg.rs
View File

@ -1,3 +0,0 @@
#[pkg(id = "re.crozet.nalgebra", vers = "0.0.0")];
#[pkg_crate(file = "src/nalgebra.rc")];

View File

@ -30,10 +30,10 @@ pub fn rotmat2<N: Clone + Trigonometric + Neg<N>>(angle: N) -> Rotmat<Mat2<N>>
let (sia, coa) = angle.sin_cos();
Rotmat
{ submat: Mat2::new( [ coa.clone(), -sia, sia.clone(), coa ] ) }
{ submat: Mat2::new(coa.clone(), -sia, sia.clone(), coa) }
}
pub fn rotmat3<N: Clone + Copy + Trigonometric + DivisionRing + Algebraic>
pub fn rotmat3<N: Clone + Trigonometric + DivisionRing + Algebraic>
(axisangle: Vec3<N>) -> Rotmat<Mat3<N>>
{
if axisangle.sqnorm().is_zero()
@ -43,9 +43,9 @@ pub fn rotmat3<N: Clone + Copy + Trigonometric + DivisionRing + Algebraic>
let mut axis = axisangle;
let angle = axis.normalize();
let _1 = One::one::<N>();
let ux = axis.at[0].clone();
let uy = axis.at[1].clone();
let uz = axis.at[2].clone();
let ux = axis.x.clone();
let uy = axis.y.clone();
let uz = axis.z.clone();
let sqx = ux * ux;
let sqy = uy * uy;
let sqz = uz * uz;
@ -53,7 +53,7 @@ pub fn rotmat3<N: Clone + Copy + Trigonometric + DivisionRing + Algebraic>
let one_m_cos = _1 - cos;
Rotmat {
submat: Mat3::new( [
submat: Mat3::new(
(sqx + (_1 - sqx) * cos),
(ux * uy * one_m_cos - uz * sin),
(ux * uz * one_m_cos + uy * sin),
@ -64,7 +64,7 @@ pub fn rotmat3<N: Clone + Copy + Trigonometric + DivisionRing + Algebraic>
(ux * uz * one_m_cos - uy * sin),
(uy * uz * one_m_cos + ux * sin),
(sqz + (_1 - sqz) * cos) ] )
(sqz + (_1 - sqz) * cos))
}
}
}
@ -74,7 +74,7 @@ Rotation<Vec1<N>> for Rotmat<Mat2<N>>
{
#[inline]
fn rotation(&self) -> Vec1<N>
{ Vec1::new([ (-self.submat.at((0, 1))).atan2(&self.submat.at((0, 0))) ]) }
{ Vec1::new((-self.submat.at((0, 1))).atan2(&self.submat.at((0, 0)))) }
#[inline]
fn inv_rotation(&self) -> Vec1<N>
@ -90,10 +90,10 @@ Rotatable<Vec1<N>, Rotmat<Mat2<N>>> for Rotmat<Mat2<N>>
{
#[inline]
fn rotated(&self, rot: &Vec1<N>) -> Rotmat<Mat2<N>>
{ rotmat2(rot.at[0].clone()) * *self }
{ rotmat2(rot.x.clone()) * *self }
}
impl<N: Clone + Copy + Trigonometric + DivisionRing + Algebraic>
impl<N: Clone + Trigonometric + DivisionRing + Algebraic>
Rotation<Vec3<N>> for Rotmat<Mat3<N>>
{
#[inline]
@ -110,7 +110,7 @@ Rotation<Vec3<N>> for Rotmat<Mat3<N>>
{ *self = self.rotated(rot) }
}
impl<N: Clone + Copy + Trigonometric + DivisionRing + Algebraic>
impl<N: Clone + Trigonometric + DivisionRing + Algebraic>
Rotatable<Vec3<N>, Rotmat<Mat3<N>>> for Rotmat<Mat3<N>>
{
#[inline]
@ -147,7 +147,7 @@ impl<M: RMul<V> + LMul<V>, V> Transform<V> for Rotmat<M>
{ self.inv_rotate(v) }
}
impl<N: Clone + Copy + Rand + Trigonometric + DivisionRing + Algebraic>
impl<N: Clone + Rand + Trigonometric + DivisionRing + Algebraic>
Rand for Rotmat<Mat3<N>>
{
#[inline]

View File

@ -25,15 +25,15 @@ impl<M, V> Transform<M, V>
{ Transform { submat: mat, subtrans: trans } }
}
impl<M: Copy, V: Copy> Transform<M, V>
impl<M: Clone, V: Clone> Transform<M, V>
{
#[inline]
pub fn submat(&self) -> M
{ copy self.submat }
{ self.submat.clone() }
#[inline]
pub fn subtrans(&self) -> V
{ copy self.subtrans }
{ self.subtrans.clone() }
}
impl<M:Dim, V> Dim for Transform<M, V>
@ -112,12 +112,12 @@ impl<M: Translate<V>, V, _0> Translate<V> for Transform<M, _0>
{ self.submat.inv_translate(v) }
}
impl<M: Copy, V: Translatable<V, V> + Translation<V>>
impl<M: Clone, V: Translatable<V, V> + Translation<V>>
Translatable<V, Transform<M, V>> for Transform<M, V>
{
#[inline]
fn translated(&self, t: &V) -> Transform<M, V>
{ Transform::new(copy self.submat, self.subtrans.translated(t)) }
{ Transform::new(self.submat.clone(), self.subtrans.translated(t)) }
}
impl<M: Rotation<AV> + RMul<V> + One,
@ -172,11 +172,11 @@ Rotatable<AV, Transform<Res, V>> for Transform<M, V>
}
}
impl<M: Inv + RMul<V> + Mul<M, M> + Copy, V: Add<V, V> + Neg<V> + Copy>
impl<M: Inv + RMul<V> + Mul<M, M> + Clone, V: Add<V, V> + Neg<V> + Clone>
Transformation<Transform<M, V>> for Transform<M, V>
{
fn transformation(&self) -> Transform<M, V>
{ copy *self }
{ self.clone() }
fn inv_transformation(&self) -> Transform<M, V>
{
@ -207,14 +207,14 @@ transformation::Transform<V> for Transform<M, V>
// FIXME: constraints are too restrictive.
// Should be: Transformable<M2, // Transform<Res, V> ...
impl<M: RMul<V> + Mul<M, M> + Inv, V: Add<V, V> + Neg<V>>
impl<M: RMul<V> + Mul<M, M> + Inv + Clone, V: Add<V, V> + Neg<V> + Clone>
Transformable<Transform<M, V>, Transform<M, V>> for Transform<M, V>
{
fn transformed(&self, t: &Transform<M, V>) -> Transform<M, V>
{ t * *self }
}
impl<M: Copy + Inv + RMul<V>, V: Copy + Neg<V>>
impl<M: Inv + RMul<V> + Clone, V: Neg<V> + Clone>
Inv for Transform<M, V>
{
#[inline]
@ -232,7 +232,7 @@ Inv for Transform<M, V>
#[inline]
fn inverse(&self) -> Option<Transform<M, V>>
{
let mut res = copy *self;
let mut res = self.clone();
if res.inplace_inverse()
{ Some(res) }
@ -241,7 +241,7 @@ Inv for Transform<M, V>
}
}
impl<M: ToHomogeneous<M2>, M2: Dim + Column<V>, V: Copy>
impl<M: ToHomogeneous<M2>, M2: Dim + Column<V>, V: Clone>
ToHomogeneous<M2> for Transform<M, V>
{
fn to_homogeneous(&self) -> M2
@ -251,13 +251,13 @@ ToHomogeneous<M2> for Transform<M, V>
// copy the translation
let dim = Dim::dim::<M2>();
res.set_column(dim - 1, copy self.subtrans);
res.set_column(dim - 1, self.subtrans.clone());
res
}
}
impl<M: Column<V> + Dim, M2: FromHomogeneous<M>, V: Copy>
impl<M: Column<V> + Dim, M2: FromHomogeneous<M>, V>
FromHomogeneous<M> for Transform<M2, V>
{
fn from_homogeneous(m: &M) -> Transform<M2, V>

View File

@ -17,7 +17,7 @@ pub struct DMat<N>
}
#[inline]
pub fn zero_mat_with_dim<N: Zero + Copy>(dim: uint) -> DMat<N>
pub fn zero_mat_with_dim<N: Zero + Clone>(dim: uint) -> DMat<N>
{ DMat { dim: dim, mij: from_elem(dim * dim, Zero::zero()) } }
#[inline]
@ -25,7 +25,7 @@ pub fn is_zero_mat<N: Zero>(mat: &DMat<N>) -> bool
{ mat.mij.iter().all(|e| e.is_zero()) }
#[inline]
pub fn one_mat_with_dim<N: Copy + One + Zero>(dim: uint) -> DMat<N>
pub fn one_mat_with_dim<N: Clone + One + Zero>(dim: uint) -> DMat<N>
{
let mut res = zero_mat_with_dim(dim);
let _1 = One::one::<N>();
@ -36,7 +36,7 @@ pub fn one_mat_with_dim<N: Copy + One + Zero>(dim: uint) -> DMat<N>
res
}
impl<N: Copy> DMat<N>
impl<N: Clone> DMat<N>
{
#[inline]
pub fn offset(&self, i: uint, j: uint) -> uint
@ -47,7 +47,7 @@ impl<N: Copy> DMat<N>
{
assert!(i < self.dim);
assert!(j < self.dim);
self.mij[self.offset(i, j)] = copy *t
self.mij[self.offset(i, j)] = t.clone()
}
#[inline]
@ -55,18 +55,18 @@ impl<N: Copy> DMat<N>
{
assert!(i < self.dim);
assert!(j < self.dim);
copy self.mij[self.offset(i, j)]
self.mij[self.offset(i, j)].clone()
}
}
impl<N: Copy> Index<(uint, uint), N> for DMat<N>
impl<N: Clone> Index<(uint, uint), N> for DMat<N>
{
#[inline]
fn index(&self, &(i, j): &(uint, uint)) -> N
{ self.at(i, j) }
}
impl<N: Copy + Mul<N, N> + Add<N, N> + Zero>
impl<N: Clone + Mul<N, N> + Add<N, N> + Zero>
Mul<DMat<N>, DMat<N>> for DMat<N>
{
fn mul(&self, other: &DMat<N>) -> DMat<N>
@ -93,7 +93,7 @@ Mul<DMat<N>, DMat<N>> for DMat<N>
}
}
impl<N: Copy + Add<N, N> + Mul<N, N> + Zero>
impl<N: Clone + Add<N, N> + Mul<N, N> + Zero>
RMul<DVec<N>> for DMat<N>
{
fn rmul(&self, other: &DVec<N>) -> DVec<N>
@ -113,7 +113,7 @@ RMul<DVec<N>> for DMat<N>
}
}
impl<N: Copy + Add<N, N> + Mul<N, N> + Zero>
impl<N: Clone + Add<N, N> + Mul<N, N> + Zero>
LMul<DVec<N>> for DMat<N>
{
fn lmul(&self, other: &DVec<N>) -> DVec<N>
@ -133,13 +133,13 @@ LMul<DVec<N>> for DMat<N>
}
}
impl<N: Copy + Eq + DivisionRing>
impl<N: Clone + Eq + DivisionRing>
Inv for DMat<N>
{
#[inline]
fn inverse(&self) -> Option<DMat<N>>
{
let mut res : DMat<N> = copy *self;
let mut res : DMat<N> = self.clone();
if res.inplace_inverse()
{ Some(res) }
@ -227,12 +227,12 @@ Inv for DMat<N>
}
}
impl<N:Copy> Transpose for DMat<N>
impl<N: Clone> Transpose for DMat<N>
{
#[inline]
fn transposed(&self) -> DMat<N>
{
let mut res = copy *self;
let mut res = self.clone();
res.transpose();

View File

@ -13,14 +13,14 @@ use traits::norm::Norm;
use traits::translation::{Translation, Translatable};
use traits::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub};
#[deriving(Eq, Ord, ToStr)]
#[deriving(Eq, Ord, ToStr, Clone)]
pub struct DVec<N>
{
at: ~[N]
}
#[inline]
pub fn zero_vec_with_dim<N: Zero + Copy>(dim: uint) -> DVec<N>
pub fn zero_vec_with_dim<N: Zero + Clone>(dim: uint) -> DVec<N>
{ DVec { at: from_elem(dim, Zero::zero::<N>()) } }
#[inline]
@ -53,7 +53,7 @@ impl<N, Iter: Iterator<N>> FromIterator<N, Iter> for DVec<N>
}
// FIXME: is Clone needed?
impl<N: Copy + DivisionRing + Algebraic + ApproxEq<N>> DVec<N>
impl<N: Clone + DivisionRing + Algebraic + ApproxEq<N>> DVec<N>
{
pub fn canonical_basis_with_dim(dim: uint) -> ~[DVec<N>]
{
@ -87,7 +87,7 @@ impl<N: Copy + DivisionRing + Algebraic + ApproxEq<N>> DVec<N>
if res.len() == dim - 1
{ break; }
let mut elt = copy basis_element;
let mut elt = basis_element.clone();
elt = elt - self.scalar_mul(&basis_element.dot(self));
@ -104,7 +104,7 @@ impl<N: Copy + DivisionRing + Algebraic + ApproxEq<N>> DVec<N>
}
}
impl<N: Copy + Add<N,N>> Add<DVec<N>, DVec<N>> for DVec<N>
impl<N: Add<N,N>> Add<DVec<N>, DVec<N>> for DVec<N>
{
#[inline]
fn add(&self, other: &DVec<N>) -> DVec<N>
@ -116,7 +116,7 @@ impl<N: Copy + Add<N,N>> Add<DVec<N>, DVec<N>> for DVec<N>
}
}
impl<N: Copy + Sub<N,N>> Sub<DVec<N>, DVec<N>> for DVec<N>
impl<N: Sub<N,N>> Sub<DVec<N>, DVec<N>> for DVec<N>
{
#[inline]
fn sub(&self, other: &DVec<N>) -> DVec<N>
@ -227,11 +227,11 @@ ScalarSub<N> for DVec<N>
}
}
impl<N: Copy + Add<N, N> + Neg<N>> Translation<DVec<N>> for DVec<N>
impl<N: Add<N, N> + Neg<N> + Clone> Translation<DVec<N>> for DVec<N>
{
#[inline]
fn translation(&self) -> DVec<N>
{ copy *self }
{ self.clone() }
#[inline]
fn inv_translation(&self) -> DVec<N>
@ -242,14 +242,14 @@ impl<N: Copy + Add<N, N> + Neg<N>> Translation<DVec<N>> for DVec<N>
{ *self = *self + *t; }
}
impl<N: Add<N, N> + Neg<N> + Copy> Translatable<DVec<N>, DVec<N>> for DVec<N>
impl<N: Add<N, N> + Neg<N> + Clone> Translatable<DVec<N>, DVec<N>> for DVec<N>
{
#[inline]
fn translated(&self, t: &DVec<N>) -> DVec<N>
{ self + *t }
}
impl<N: Copy + DivisionRing + Algebraic>
impl<N: DivisionRing + Algebraic + Clone>
Norm<N> for DVec<N>
{
#[inline]
@ -263,7 +263,7 @@ Norm<N> for DVec<N>
#[inline]
fn normalized(&self) -> DVec<N>
{
let mut res : DVec<N> = copy *self;
let mut res : DVec<N> = self.clone();
res.normalize();

View File

View File

@ -1,8 +1,9 @@
use std::cast;
use std::uint::iterate;
use std::num::{One, Zero};
use std::cmp::ApproxEq;
use std::rand::{Rand, Rng, RngUtil};
use std::iterator::IteratorUtil;
use std::vec::{VecIterator, VecMutIterator};
use vec::{Vec1, Vec2, Vec3, Vec4, Vec5, Vec6};
use traits::dim::Dim;
use traits::ring::Ring;
@ -11,108 +12,112 @@ use traits::division_ring::DivisionRing;
use traits::transpose::Transpose;
use traits::rlmul::{RMul, LMul};
use traits::transformation::Transform;
use traits::homogeneous::{ToHomogeneous, FromHomogeneous};
use traits::homogeneous::{FromHomogeneous, ToHomogeneous};
use traits::indexable::Indexable;
use traits::column::Column;
use traits::iterable::{Iterable, IterableMut};
mod mat_impl;
#[deriving(ToStr)]
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
pub struct Mat1<N>
{ mij: [N, ..1 * 1] }
{ m11: N }
clone_impl!(Mat1)
mat_impl!(Mat1, 1)
one_impl!(Mat1, [ _1 ])
zero_impl!(Mat1, [ _0 ])
mat_impl!(Mat1, 1, m11)
one_impl!(Mat1, _1)
iterable_impl!(Mat1, 1)
iterable_mut_impl!(Mat1, 1)
dim_impl!(Mat1, 1)
mat_indexable_impl!(Mat1, 1)
indexable_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)
// (specialized) inv_impl!(Mat1, 1)
transpose_impl!(Mat1, 1)
approx_eq_impl!(Mat1)
rand_impl!(Mat1, rng, [ rng ])
to_homogeneous_impl!(Mat1, Mat2, 1)
from_homogeneous_impl!(Mat2, Mat1, 1)
column_impl!(Mat1, 1)
to_homogeneous_impl!(Mat1, Mat2, 1, 2)
from_homogeneous_impl!(Mat1, Mat2, 1, 2)
#[deriving(ToStr)]
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
pub struct Mat2<N>
{ mij: [N, ..2 * 2] }
{
m11: N, m12: N,
m21: N, m22: N
}
clone_impl!(Mat2)
mat_impl!(Mat2, 2)
one_impl!(Mat2, [ _1 | _0 |
_0 | _1 ])
zero_impl!(Mat2, [ _0 | _0 |
_0 | _0 ])
mat_impl!(Mat2, 2, m11, m12,
m21, m22)
one_impl!(Mat2, _1, _0,
_0, _1)
iterable_impl!(Mat2, 2)
iterable_mut_impl!(Mat2, 2)
dim_impl!(Mat2, 2)
mat_indexable_impl!(Mat2, 2)
indexable_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)
// (specialized) inv_impl!(Mat2, 2)
transpose_impl!(Mat2, 2)
approx_eq_impl!(Mat2)
rand_impl!(Mat2, rng, [ rng | rng |
rng | rng ])
to_homogeneous_impl!(Mat2, Mat3, 2)
from_homogeneous_impl!(Mat3, Mat2, 2)
column_impl!(Mat2, 2)
to_homogeneous_impl!(Mat2, Mat3, 2, 3)
from_homogeneous_impl!(Mat2, Mat3, 2, 3)
#[deriving(ToStr)]
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
pub struct Mat3<N>
{ mij: [N, ..3 * 3] }
{
m11: N, m12: N, m13: N,
m21: N, m22: N, m23: N,
m31: N, m32: N, m33: N
}
clone_impl!(Mat3)
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 ])
mat_impl!(Mat3, 3, m11, m12, m13,
m21, m22, m23,
m31, m32, m33)
one_impl!(Mat3, _1, _0, _0,
_0, _1, _0,
_0, _0, _1)
iterable_impl!(Mat3, 3)
iterable_mut_impl!(Mat3, 3)
dim_impl!(Mat3, 3)
mat_indexable_impl!(Mat3, 3)
indexable_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)
// (specialized) 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])
to_homogeneous_impl!(Mat3, Mat4, 3)
from_homogeneous_impl!(Mat4, Mat3, 3)
column_impl!(Mat3, 3)
to_homogeneous_impl!(Mat3, Mat4, 3, 4)
from_homogeneous_impl!(Mat3, Mat4, 3, 4)
#[deriving(ToStr)]
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
pub struct Mat4<N>
{ mij: [N, ..4 * 4] }
{
m11: N, m12: N, m13: N, m14: N,
m21: N, m22: N, m23: N, m24: N,
m31: N, m32: N, m33: N, m34: N,
m41: N, m42: N, m43: N, m44: N
}
clone_impl!(Mat4)
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
])
mat_impl!(Mat4, 4,
m11, m12, m13, m14,
m21, m22, m23, m24,
m31, m32, m33, m34,
m41, m42, m43, m44
)
one_impl!(Mat4, _1, _0, _0, _0,
_0, _1, _0, _0,
_0, _0, _1, _0,
_0, _0, _0, _1)
iterable_impl!(Mat4, 4)
iterable_mut_impl!(Mat4, 4)
dim_impl!(Mat4, 4)
mat_indexable_impl!(Mat4, 4)
indexable_impl!(Mat4, 4)
mul_impl!(Mat4, 4)
rmul_impl!(Mat4, Vec4, 4)
lmul_impl!(Mat4, Vec4, 4)
@ -120,38 +125,38 @@ 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
])
to_homogeneous_impl!(Mat4, Mat5, 4)
from_homogeneous_impl!(Mat5, Mat4, 4)
column_impl!(Mat4, 4)
to_homogeneous_impl!(Mat4, Mat5, 4, 5)
from_homogeneous_impl!(Mat4, Mat5, 4, 5)
#[deriving(ToStr)]
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
pub struct Mat5<N>
{ mij: [N, ..5 * 5] }
{
m11: N, m12: N, m13: N, m14: N, m15: N,
m21: N, m22: N, m23: N, m24: N, m25: N,
m31: N, m32: N, m33: N, m34: N, m35: N,
m41: N, m42: N, m43: N, m44: N, m45: N,
m51: N, m52: N, m53: N, m54: N, m55: N
}
clone_impl!(Mat5)
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
])
mat_impl!(Mat5, 5,
m11, m12, m13, m14, m15,
m21, m22, m23, m24, m25,
m31, m32, m33, m34, m35,
m41, m42, m43, m44, m45,
m51, m52, m53, m54, m55
)
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
)
iterable_impl!(Mat5, 5)
iterable_mut_impl!(Mat5, 5)
dim_impl!(Mat5, 5)
mat_indexable_impl!(Mat5, 5)
indexable_impl!(Mat5, 5)
mul_impl!(Mat5, 5)
rmul_impl!(Mat5, Vec5, 5)
lmul_impl!(Mat5, Vec5, 5)
@ -159,41 +164,41 @@ 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
])
to_homogeneous_impl!(Mat5, Mat6, 5)
from_homogeneous_impl!(Mat6, Mat5, 5)
column_impl!(Mat5, 5)
to_homogeneous_impl!(Mat5, Mat6, 5, 6)
from_homogeneous_impl!(Mat5, Mat6, 5, 6)
#[deriving(ToStr)]
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
pub struct Mat6<N>
{ mij: [N, ..6 * 6] }
{
m11: N, m12: N, m13: N, m14: N, m15: N, m16: N,
m21: N, m22: N, m23: N, m24: N, m25: N, m26: N,
m31: N, m32: N, m33: N, m34: N, m35: N, m36: N,
m41: N, m42: N, m43: N, m44: N, m45: N, m46: N,
m51: N, m52: N, m53: N, m54: N, m55: N, m56: N,
m61: N, m62: N, m63: N, m64: N, m65: N, m66: N
}
clone_impl!(Mat6)
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
])
mat_impl!(Mat6, 6,
m11, m12, m13, m14, m15, m16,
m21, m22, m23, m24, m25, m26,
m31, m32, m33, m34, m35, m36,
m41, m42, m43, m44, m45, m46,
m51, m52, m53, m54, m55, m56,
m61, m62, m63, m64, m65, m66
)
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
)
iterable_impl!(Mat6, 6)
iterable_mut_impl!(Mat6, 6)
dim_impl!(Mat6, 6)
mat_indexable_impl!(Mat6, 6)
indexable_impl!(Mat6, 6)
mul_impl!(Mat6, 6)
rmul_impl!(Mat6, Vec6, 6)
lmul_impl!(Mat6, Vec6, 6)
@ -201,12 +206,4 @@ 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
])
column_impl!(Mat6, 6)

View File

@ -1,69 +1,55 @@
#[macro_escape];
macro_rules! clone_impl(
// FIXME: use 'Clone' alone. For the moment, we need 'Copy' because the automatic
// implementation of Clone for [t, ..n] is badly typed.
($t: ident) => (
impl<N: Clone + Copy> Clone for $t<N>
macro_rules! mat_impl(
($t: ident, $dim: expr, $comp0: ident $(,$compN: ident)*) => (
impl<N> $t<N>
{
#[inline]
fn clone(&self) -> $t<N>
pub fn new($comp0: N $(, $compN: N )*) -> $t<N>
{
$t {
mij: copy self.mij
$comp0: $comp0
$(, $compN: $compN )*
}
}
}
)
)
macro_rules! mat_impl(
macro_rules! iterable_impl(
($t: ident, $dim: expr) => (
impl<N> $t<N>
impl<N> Iterable<N> for $t<N>
{
#[inline]
pub fn new(mij: [N, ..$dim * $dim]) -> $t<N>
{ $t { mij: mij } }
fn iter<'l>(&'l self) -> VecIterator<'l, N>
{ unsafe { cast::transmute::<&'l $t<N>, &'l [N, ..$dim * $dim]>(self).iter() } }
}
)
)
#[inline]
pub fn offset(&self, i: uint, j: uint) -> uint
{ i * $dim + j }
macro_rules! iterable_mut_impl(
($t: ident, $dim: expr) => (
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() } }
}
)
)
macro_rules! one_impl(
($t: ident, [ $($value: ident)|+ ] ) => (
($t: ident, $value0: ident $(, $valueN: ident)* ) => (
impl<N: Clone + One + Zero> One for $t<N>
{
#[inline]
fn one() -> $t<N>
{
let (_0, _1) = (Zero::zero::<N>(), One::one::<N>());
return $t::new( [ $( $value.clone(), )+ ] )
return $t::new($value0.clone() $(, $valueN.clone() )*)
}
}
)
)
macro_rules! zero_impl(
($t: ident, [ $($value: ident)|+ ] ) => (
impl<N: Clone + Zero> Zero for $t<N>
{
#[inline]
fn zero() -> $t<N>
{
let _0 = Zero::zero::<N>();
return $t::new( [ $( $value.clone(), )+ ] )
}
#[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>
@ -75,17 +61,26 @@ macro_rules! dim_impl(
)
)
macro_rules! mat_indexable_impl(
macro_rules! indexable_impl(
($t: ident, $dim: expr) => (
impl<N: Clone> Indexable<(uint, uint), N> for $t<N>
{
#[inline]
pub fn at(&self, (i, j): (uint, uint)) -> N
{ self.mij[self.offset(i, j)].clone() }
{ unsafe { cast::transmute::<&$t<N>, &[N, ..$dim * $dim]>(self)[i * $dim + j].clone() } }
#[inline]
pub fn set(&mut self, (i, j): (uint, uint), t: N)
{ self.mij[self.offset(i, j)] = t }
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 } }
#[inline]
pub fn swap(&mut self, (i1, j1): (uint, uint), (i2, j2): (uint, uint))
{
unsafe {
cast::transmute::<&mut $t<N>, &mut [N, ..$dim * $dim]>(self)
.swap(i1 * $dim + j1, i2 * $dim + j2)
}
}
}
)
)
@ -163,7 +158,10 @@ macro_rules! rmul_impl(
for iterate(0u, $dim) |i|
{
for iterate(0u, $dim) |j|
{ res.at[i] = res.at[i] + other.at[j] * self.at((i, j)); }
{
let val = res.at(i) + other.at(j) * self.at((i, j));
res.set(i, val)
}
}
res
@ -185,7 +183,10 @@ macro_rules! lmul_impl(
for iterate(0u, $dim) |i|
{
for iterate(0u, $dim) |j|
{ res.at[i] = res.at[i] + other.at[j] * self.at((j, i)); }
{
let val = res.at(i) + other.at(j) * self.at((j, i));
res.set(i, val)
}
}
res
@ -196,7 +197,7 @@ macro_rules! lmul_impl(
macro_rules! transform_impl(
($t: ident, $v: ident) => (
impl<N: Clone + Copy + DivisionRing + Eq>
impl<N: Clone + DivisionRing + Eq>
Transform<$v<N>> for $t<N>
{
#[inline]
@ -218,7 +219,7 @@ macro_rules! transform_impl(
macro_rules! inv_impl(
($t: ident, $dim: expr) => (
impl<N: Clone + Copy + Eq + DivisionRing>
impl<N: Clone + Eq + DivisionRing>
Inv for $t<N>
{
#[inline]
@ -262,11 +263,8 @@ macro_rules! inv_impl(
{
for iterate(0u, $dim) |j|
{
let off_n0_j = self.offset(n0, j);
let off_k_j = self.offset(k, j);
self.mij.swap(off_n0_j, off_k_j);
res.mij.swap(off_n0_j, off_k_j);
self.swap((n0, j), (k, j));
res.swap((n0, j), (k, j));
}
}
@ -315,7 +313,7 @@ macro_rules! inv_impl(
macro_rules! transpose_impl(
($t: ident, $dim: expr) => (
impl<N: Clone + Copy> Transpose for $t<N>
impl<N: Clone> Transpose for $t<N>
{
#[inline]
fn transposed(&self) -> $t<N>
@ -332,12 +330,7 @@ macro_rules! transpose_impl(
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);
self.mij.swap(off_i_j, off_j_i);
}
{ self.swap((i, j), (j, i)) }
}
}
}
@ -355,7 +348,7 @@ macro_rules! approx_eq_impl(
#[inline]
fn approx_eq(&self, other: &$t<N>) -> bool
{
let mut zip = self.mij.iter().zip(other.mij.iter());
let mut zip = self.iter().zip(other.iter());
do zip.all |(a, b)| { a.approx_eq(b) }
}
@ -363,7 +356,7 @@ macro_rules! approx_eq_impl(
#[inline]
fn approx_eq_eps(&self, other: &$t<N>, epsilon: &N) -> bool
{
let mut zip = self.mij.iter().zip(other.mij.iter());
let mut zip = self.iter().zip(other.iter());
do zip.all |(a, b)| { a.approx_eq_eps(b, epsilon) }
}
@ -371,19 +364,8 @@ macro_rules! approx_eq_impl(
)
)
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(), )+ ]) }
}
)
)
macro_rules! to_homogeneous_impl(
($t: ident, $t2: ident, $dim: expr) => (
($t: ident, $t2: ident, $dim: expr, $dim2: expr) => (
impl<N: One + Zero + Clone> ToHomogeneous<$t2<N>> for $t<N>
{
fn to_homogeneous(&self) -> $t2<N>
@ -403,7 +385,7 @@ macro_rules! to_homogeneous_impl(
)
macro_rules! from_homogeneous_impl(
($t: ident, $t2: ident, $dim2: expr) => (
($t: ident, $t2: ident, $dim: expr, $dim2: expr) => (
impl<N: One + Zero + Clone> FromHomogeneous<$t2<N>> for $t<N>
{
fn from_homogeneous(m: &$t2<N>) -> $t<N>

View File

@ -4,13 +4,13 @@ use traits::division_ring::DivisionRing;
use traits::inv::Inv;
// some specializations:
impl<N: Copy + DivisionRing>
impl<N: DivisionRing + Clone>
Inv for Mat1<N>
{
#[inline]
fn inverse(&self) -> Option<Mat1<N>>
{
let mut res : Mat1<N> = copy *self;
let mut res : Mat1<N> = self.clone();
if res.inplace_inverse()
{ Some(res) }
@ -21,23 +21,23 @@ Inv for Mat1<N>
#[inline]
fn inplace_inverse(&mut self) -> bool
{
if self.mij[0].is_zero()
if self.m11.is_zero()
{ false }
else
{
self.mij[0] = One::one::<N>() / self.mij[0];
self.m11 = One::one::<N>() / self.m11;
true
}
}
}
impl<N: Copy + DivisionRing>
impl<N: DivisionRing + Clone>
Inv for Mat2<N>
{
#[inline]
fn inverse(&self) -> Option<Mat2<N>>
{
let mut res : Mat2<N> = copy *self;
let mut res : Mat2<N> = self.clone();
if res.inplace_inverse()
{ Some(res) }
@ -48,27 +48,27 @@ Inv for Mat2<N>
#[inline]
fn inplace_inverse(&mut self) -> bool
{
let det = self.mij[0 * 2 + 0] * self.mij[1 * 2 + 1] - self.mij[1 * 2 + 0] * self.mij[0 * 2 + 1];
let det = self.m11 * self.m22 - self.m21 * self.m12;
if det.is_zero()
{ false }
else
{
*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]);
*self = Mat2::new(self.m22 / det , -self.m12 / det,
-self.m21 / det, self.m11 / det);
true
}
}
}
impl<N: Copy + DivisionRing>
impl<N: DivisionRing + Clone>
Inv for Mat3<N>
{
#[inline]
fn inverse(&self) -> Option<Mat3<N>>
{
let mut res = copy *self;
let mut res = self.clone();
if res.inplace_inverse()
{ Some(res) }
@ -79,31 +79,31 @@ Inv for Mat3<N>
#[inline]
fn inplace_inverse(&mut self) -> bool
{
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 minor_m12_m23 = self.m22 * self.m33 - self.m32 * self.m23;
let minor_m11_m23 = self.m21 * self.m33 - self.m31 * self.m23;
let minor_m11_m22 = self.m21 * self.m32 - self.m31 * self.m22;
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;
let det = self.m11 * minor_m12_m23
- self.m12 * minor_m11_m23
+ self.m13 * minor_m11_m22;
if det.is_zero()
{ false }
else
{
*self = Mat3::new( [
*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),
((self.m13 * self.m32 - self.m33 * self.m12) / det),
((self.m12 * self.m23 - self.m22 * self.m13) / 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),
((self.m11 * self.m33 - self.m31 * self.m13) / det),
((self.m13 * self.m21 - self.m23 * self.m11) / 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)
] );
((self.m12 * self.m31 - self.m32 * self.m11) / det),
((self.m11 * self.m22 - self.m21 * self.m12) / det)
);
true
}

View File

@ -1,19 +1,17 @@
/*!
# The n-dimensional linear algebra library.
# A n-dimensional linear algebra library.
*/
#[link(name = "nalgebra"
, vers = "0.0"
, vers = "0.1"
, author = "Sébastien Crozet"
, uuid = "1E96070F-4778-4EC1-B080-BF69F7048216")];
#[crate_type = "lib"];
#[warn(non_camel_case_types)]
extern mod std;
extern mod extra;
pub mod vec;
pub mod mat;

View File

@ -60,7 +60,7 @@ fn test_rotation2()
for 10000.times
{
let randmat = One::one::<Rotmat<Mat2<f64>>>();
let ang = &Vec1::new([abs::<f64>(random()) % Real::pi()]);
let ang = &Vec1::new(abs::<f64>(random()) % Real::pi());
assert!(randmat.rotated(ang).rotation().approx_eq(ang));
}

View File

@ -26,7 +26,7 @@ macro_rules! test_iterator_impl(
for 10000.times
{
let v: $t = random();
let mut mv: $t = copy v;
let mut mv: $t = v.clone();
let n: $n = random();
let nv: $t = v.iter().transform(|e| e * n).collect();
@ -64,7 +64,7 @@ macro_rules! test_scalar_op_impl(
assert!(v1.scalar_add(&n).scalar_sub(&n).approx_eq(&v1));
let mut v1 : $t = random();
let v0 : $t = copy v1;
let v0 : $t = v1.clone();
let n : $n = random();
v1.scalar_mul_inplace(&n);

View File

@ -6,4 +6,5 @@ pub trait Indexable<Index, Res>
{
fn at(&self, Index) -> Res;
fn set(&mut self, Index, Res);
fn swap(&mut self, Index, Index);
}

View File

@ -28,28 +28,3 @@ pub trait IterableMut<'self, N, I: Iterator<N>>
* For now, we oblige the iterator to be one specific type which works with
* everything on this lib.
*/
pub trait FromAnyIterator<N>
{
fn from_iterator<'l>(&mut vec::VecIterator<'l, N>) -> Self;
fn from_mut_iterator<'l>(&mut vec::VecMutIterator<'l, N>) -> Self;
}
/*
* FIXME: the previous trait is only a workaround.
* It should be something like:
pub trait FromAnyIterator<N>
{
fn from_iterator<I: Iterator<N>>(&mut I) -> Self;
}
* but this gives a wierd error message (the compile seems to mistake N with
* I).
* For now, we oblige the iterator to be one specific type which works with
* everything on this lib.
*
* Note that we dont use the standard std::iterator::FromIterator<N, I: Iterator<N>>
* because it is too hard to work with on generic code (as a type bound)
* because we have to name explicitly the type of the iterator.
*
*/

View File

@ -1,10 +1,11 @@
use std::cast;
use std::num::{Zero, One, Algebraic, Bounded};
use std::rand::{Rand, Rng, RngUtil};
use std::rand::Rng;
use std::vec::{VecIterator, VecMutIterator};
use std::iterator::{Iterator, IteratorUtil, FromIterator};
use std::cmp::ApproxEq;
use std::uint::iterate;
use traits::iterable::{Iterable, IterableMut, FromAnyIterator};
use traits::iterable::{Iterable, IterableMut};
use traits::basis::Basis;
use traits::dim::Dim;
use traits::dot::Dot;
@ -14,260 +15,249 @@ use traits::translation::{Translation, Translatable};
use traits::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub};
use traits::ring::Ring;
use traits::division_ring::DivisionRing;
use traits::homogeneous::{ToHomogeneous, FromHomogeneous};
use traits::homogeneous::{FromHomogeneous, ToHomogeneous};
use traits::indexable::Indexable;
mod vec_impl;
#[deriving(Ord, ToStr)]
pub struct Vec0<N>
{ at: [N, ..0] }
// #[deriving(Ord, ToStr)]
// pub struct Vec0<N>
// { at: [N, ..0] }
impl<N: Clone> Vec0<N>
{
#[inline]
pub fn new_repeat(_: N) -> Vec0<N>
{ Vec0 { at: [ ] } }
}
// impl<N: Clone> Vec0<N>
// {
// #[inline]
// pub fn new_repeat(_: N) -> Vec0<N>
// { Vec0 { at: [ ] } }
// }
clone_impl!(Vec0)
deep_clone_impl!(Vec0)
new_impl!(Vec0, 0)
indexable_impl!(Vec0)
dim_impl!(Vec0, 0)
eq_impl!(Vec0)
// (specialized) basis_impl!(Vec0, 0)
add_impl!(Vec0)
sub_impl!(Vec0)
neg_impl!(Vec0)
dot_impl!(Vec0, 0)
sub_dot_impl!(Vec0, 0)
scalar_mul_impl!(Vec0, 0)
scalar_div_impl!(Vec0, 0)
scalar_add_impl!(Vec0, 0)
scalar_sub_impl!(Vec0, 0)
translation_impl!(Vec0)
translatable_impl!(Vec0)
norm_impl!(Vec0, 0)
approx_eq_impl!(Vec0)
zero_impl!(Vec0)
one_impl!(Vec0)
bounded_impl!(Vec0)
iterable_impl!(Vec0)
iterable_mut_impl!(Vec0)
to_homogeneous_impl!(Vec0, Vec1)
from_homogeneous_impl!(Vec1, Vec0, 1)
// clone_impl!(Vec0)
// deep_clone_impl!(Vec0)
// new_impl!(Vec0, 0)
// indexable_impl!(Vec0)
// dim_impl!(Vec0, 0)
// eq_impl!(Vec0)
// // (specialized) basis_impl!(Vec0, 0)
// add_impl!(Vec0)
// sub_impl!(Vec0)
// neg_impl!(Vec0)
// dot_impl!(Vec0, 0)
// sub_dot_impl!(Vec0, 0)
// scalar_mul_impl!(Vec0, 0)
// scalar_div_impl!(Vec0, 0)
// scalar_add_impl!(Vec0, 0)
// scalar_sub_impl!(Vec0, 0)
// translation_impl!(Vec0)
// translatable_impl!(Vec0)
// norm_impl!(Vec0, 0)
// approx_eq_impl!(Vec0)
// zero_impl!(Vec0)
// one_impl!(Vec0)
// bounded_impl!(Vec0)
// iterable_impl!(Vec0)
// iterable_mut_impl!(Vec0)
// to_homogeneous_impl!(Vec0, Vec1)
// from_homogeneous_impl!(Vec1, Vec0, 1)
#[deriving(Ord, ToStr)]
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
pub struct Vec1<N>
{ at: [N, ..1] }
{ x: N }
clone_impl!(Vec1)
deep_clone_impl!(Vec1)
new_impl!(Vec1, 1)
new_repeat_impl!(Vec1, elem, [elem])
indexable_impl!(Vec1)
new_impl!(Vec1, x)
indexable_impl!(Vec1, 1)
new_repeat_impl!(Vec1, val, x)
dim_impl!(Vec1, 1)
eq_impl!(Vec1)
// (specialized) basis_impl!(Vec1, 1)
add_impl!(Vec1)
sub_impl!(Vec1)
neg_impl!(Vec1)
dot_impl!(Vec1, 1)
sub_dot_impl!(Vec1, 1)
scalar_mul_impl!(Vec1, 1)
scalar_div_impl!(Vec1, 1)
scalar_add_impl!(Vec1, 1)
scalar_sub_impl!(Vec1, 1)
add_impl!(Vec1, x)
sub_impl!(Vec1, x)
neg_impl!(Vec1, x)
dot_impl!(Vec1, x)
sub_dot_impl!(Vec1, x)
scalar_mul_impl!(Vec1, x)
scalar_div_impl!(Vec1, x)
scalar_add_impl!(Vec1, x)
scalar_sub_impl!(Vec1, x)
translation_impl!(Vec1)
translatable_impl!(Vec1)
norm_impl!(Vec1, 1)
approx_eq_impl!(Vec1)
zero_impl!(Vec1)
norm_impl!(Vec1)
approx_eq_impl!(Vec1, x)
one_impl!(Vec1)
rand_impl!(Vec1, rng, [rng])
from_iterator_impl!(Vec1, iterator, [iterator])
from_any_iterator_impl!(Vec1, iterator, [iterator])
from_iterator_impl!(Vec1, iterator)
bounded_impl!(Vec1)
iterable_impl!(Vec1)
iterable_mut_impl!(Vec1)
to_homogeneous_impl!(Vec1, Vec2)
from_homogeneous_impl!(Vec2, Vec1, 2)
iterable_impl!(Vec1, 1)
iterable_mut_impl!(Vec1, 1)
to_homogeneous_impl!(Vec1, Vec2, y, x)
from_homogeneous_impl!(Vec1, Vec2, y, x)
#[deriving(Ord, ToStr)]
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
pub struct Vec2<N>
{ at: [N, ..2] }
{
x: N,
y: N
}
clone_impl!(Vec2)
deep_clone_impl!(Vec2)
new_impl!(Vec2, 2)
new_repeat_impl!(Vec2, elem, [elem | elem])
indexable_impl!(Vec2)
new_impl!(Vec2, x, y)
indexable_impl!(Vec2, 2)
new_repeat_impl!(Vec2, val, x, y)
dim_impl!(Vec2, 2)
eq_impl!(Vec2)
// (specialized) basis_impl!(Vec2, 2)
add_impl!(Vec2)
sub_impl!(Vec2)
neg_impl!(Vec2)
dot_impl!(Vec2, 2)
sub_dot_impl!(Vec2, 2)
scalar_mul_impl!(Vec2, 2)
scalar_div_impl!(Vec2, 2)
scalar_add_impl!(Vec2, 2)
scalar_sub_impl!(Vec2, 2)
// (specialized) basis_impl!(Vec2, 1)
add_impl!(Vec2, x, y)
sub_impl!(Vec2, x, y)
neg_impl!(Vec2, x, y)
dot_impl!(Vec2, x, y)
sub_dot_impl!(Vec2, x, y)
scalar_mul_impl!(Vec2, x, y)
scalar_div_impl!(Vec2, x, y)
scalar_add_impl!(Vec2, x, y)
scalar_sub_impl!(Vec2, x, y)
translation_impl!(Vec2)
translatable_impl!(Vec2)
norm_impl!(Vec2, 2)
approx_eq_impl!(Vec2)
zero_impl!(Vec2)
norm_impl!(Vec2)
approx_eq_impl!(Vec2, x, y)
one_impl!(Vec2)
rand_impl!(Vec2, rng, [rng | rng])
from_iterator_impl!(Vec2, iterator, [iterator | iterator])
from_any_iterator_impl!(Vec2, iterator, [iterator | iterator])
from_iterator_impl!(Vec2, iterator, iterator)
bounded_impl!(Vec2)
iterable_impl!(Vec2)
iterable_mut_impl!(Vec2)
to_homogeneous_impl!(Vec2, Vec3)
from_homogeneous_impl!(Vec3, Vec2, 3)
iterable_impl!(Vec2, 2)
iterable_mut_impl!(Vec2, 2)
to_homogeneous_impl!(Vec2, Vec3, z, x, y)
from_homogeneous_impl!(Vec2, Vec3, z, x, y)
#[deriving(Ord, ToStr)]
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
pub struct Vec3<N>
{ at: [N, ..3] }
{
x: N,
y: N,
z: N
}
clone_impl!(Vec3)
deep_clone_impl!(Vec3)
new_impl!(Vec3, 3)
new_repeat_impl!(Vec3, elem, [elem | elem | elem])
indexable_impl!(Vec3)
new_impl!(Vec3, x, y, z)
indexable_impl!(Vec3, 3)
new_repeat_impl!(Vec3, val, x, y, z)
dim_impl!(Vec3, 3)
eq_impl!(Vec3)
// (specialized) basis_impl!(Vec3, 3)
add_impl!(Vec3)
sub_impl!(Vec3)
neg_impl!(Vec3)
dot_impl!(Vec3, 3)
sub_dot_impl!(Vec3, 3)
scalar_mul_impl!(Vec3, 3)
scalar_div_impl!(Vec3, 3)
scalar_add_impl!(Vec3, 3)
scalar_sub_impl!(Vec3, 3)
// (specialized) basis_impl!(Vec3, 1)
add_impl!(Vec3, x, y, z)
sub_impl!(Vec3, x, y, z)
neg_impl!(Vec3, x, y, z)
dot_impl!(Vec3, x, y, z)
sub_dot_impl!(Vec3, x, y, z)
scalar_mul_impl!(Vec3, x, y, z)
scalar_div_impl!(Vec3, x, y, z)
scalar_add_impl!(Vec3, x, y, z)
scalar_sub_impl!(Vec3, x, y, z)
translation_impl!(Vec3)
translatable_impl!(Vec3)
norm_impl!(Vec3, 3)
approx_eq_impl!(Vec3)
zero_impl!(Vec3)
norm_impl!(Vec3)
approx_eq_impl!(Vec3, x, y, z)
one_impl!(Vec3)
rand_impl!(Vec3, rng, [rng | rng | rng])
from_iterator_impl!(Vec3, iterator, [iterator | iterator | iterator])
from_any_iterator_impl!(Vec3, iterator, [iterator | iterator | iterator])
from_iterator_impl!(Vec3, iterator, iterator, iterator)
bounded_impl!(Vec3)
iterable_impl!(Vec3)
iterable_mut_impl!(Vec3)
to_homogeneous_impl!(Vec3, Vec4)
from_homogeneous_impl!(Vec4, Vec3, 4)
iterable_impl!(Vec3, 3)
iterable_mut_impl!(Vec3, 3)
to_homogeneous_impl!(Vec3, Vec4, w, x, y, z)
from_homogeneous_impl!(Vec3, Vec4, w, x, y, z)
#[deriving(Ord, ToStr)]
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
pub struct Vec4<N>
{ at: [N, ..4] }
{
x: N,
y: N,
z: N,
w: N
}
clone_impl!(Vec4)
deep_clone_impl!(Vec4)
new_impl!(Vec4, 4)
new_repeat_impl!(Vec4, elem, [elem | elem | elem | elem])
indexable_impl!(Vec4)
new_impl!(Vec4, x, y, z, w)
indexable_impl!(Vec4, 4)
new_repeat_impl!(Vec4, val, x, y, z, w)
dim_impl!(Vec4, 4)
eq_impl!(Vec4)
basis_impl!(Vec4, 4)
add_impl!(Vec4)
sub_impl!(Vec4)
neg_impl!(Vec4)
dot_impl!(Vec4, 4)
sub_dot_impl!(Vec4, 4)
scalar_mul_impl!(Vec4, 4)
scalar_div_impl!(Vec4, 4)
scalar_add_impl!(Vec4, 4)
scalar_sub_impl!(Vec4, 4)
add_impl!(Vec4, x, y, z, w)
sub_impl!(Vec4, x, y, z, w)
neg_impl!(Vec4, x, y, z, w)
dot_impl!(Vec4, x, y, z, w)
sub_dot_impl!(Vec4, x, y, z, w)
scalar_mul_impl!(Vec4, x, y, z, w)
scalar_div_impl!(Vec4, x, y, z, w)
scalar_add_impl!(Vec4, x, y, z, w)
scalar_sub_impl!(Vec4, x, y, z, w)
translation_impl!(Vec4)
translatable_impl!(Vec4)
norm_impl!(Vec4, 4)
approx_eq_impl!(Vec4)
zero_impl!(Vec4)
norm_impl!(Vec4)
approx_eq_impl!(Vec4, x, y, z, w)
one_impl!(Vec4)
rand_impl!(Vec4, rng, [rng | rng | rng | rng])
from_iterator_impl!(Vec4, iterator, [iterator | iterator | iterator | iterator])
from_any_iterator_impl!(Vec4, iterator, [iterator | iterator | iterator | iterator])
from_iterator_impl!(Vec4, iterator, iterator, iterator, iterator)
bounded_impl!(Vec4)
iterable_impl!(Vec4)
iterable_mut_impl!(Vec4)
to_homogeneous_impl!(Vec4, Vec5)
from_homogeneous_impl!(Vec5, Vec4, 5)
iterable_impl!(Vec4, 4)
iterable_mut_impl!(Vec4, 4)
to_homogeneous_impl!(Vec4, Vec5, a, x, y, z, w)
from_homogeneous_impl!(Vec4, Vec5, a, x, y, z, w)
#[deriving(Ord, ToStr)]
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
pub struct Vec5<N>
{ at: [N, ..5] }
{
x: N,
y: N,
z: N,
w: N,
a: N,
}
clone_impl!(Vec5)
deep_clone_impl!(Vec5)
new_impl!(Vec5, 5)
new_repeat_impl!(Vec5, elem, [elem | elem | elem | elem | elem])
indexable_impl!(Vec5)
new_impl!(Vec5, x, y, z, w, a)
indexable_impl!(Vec5, 5)
new_repeat_impl!(Vec5, val, x, y, z, w, a)
dim_impl!(Vec5, 5)
eq_impl!(Vec5)
basis_impl!(Vec5, 5)
add_impl!(Vec5)
sub_impl!(Vec5)
neg_impl!(Vec5)
dot_impl!(Vec5, 5)
sub_dot_impl!(Vec5, 5)
scalar_mul_impl!(Vec5, 5)
scalar_div_impl!(Vec5, 5)
scalar_add_impl!(Vec5, 5)
scalar_sub_impl!(Vec5, 5)
add_impl!(Vec5, x, y, z, w, a)
sub_impl!(Vec5, x, y, z, w, a)
neg_impl!(Vec5, x, y, z, w, a)
dot_impl!(Vec5, x, y, z, w, a)
sub_dot_impl!(Vec5, x, y, z, w, a)
scalar_mul_impl!(Vec5, x, y, z, w, a)
scalar_div_impl!(Vec5, x, y, z, w, a)
scalar_add_impl!(Vec5, x, y, z, w, a)
scalar_sub_impl!(Vec5, x, y, z, w, a)
translation_impl!(Vec5)
translatable_impl!(Vec5)
norm_impl!(Vec5, 5)
approx_eq_impl!(Vec5)
zero_impl!(Vec5)
norm_impl!(Vec5)
approx_eq_impl!(Vec5, x, y, z, w, a)
one_impl!(Vec5)
rand_impl!(Vec5, rng, [rng | rng | rng | rng | rng])
from_iterator_impl!(Vec5, iterator, [iterator | iterator | iterator | iterator | iterator])
from_any_iterator_impl!(Vec5, iterator, [iterator | iterator | iterator | iterator | iterator])
from_iterator_impl!(Vec5, iterator, iterator, iterator, iterator, iterator)
bounded_impl!(Vec5)
iterable_impl!(Vec5)
iterable_mut_impl!(Vec5)
to_homogeneous_impl!(Vec5, Vec6)
from_homogeneous_impl!(Vec6, Vec5, 6)
iterable_impl!(Vec5, 5)
iterable_mut_impl!(Vec5, 5)
to_homogeneous_impl!(Vec5, Vec6, b, x, y, z, w, a)
from_homogeneous_impl!(Vec5, Vec6, b, x, y, z, w, a)
#[deriving(Ord, ToStr)]
#[deriving(Eq, Ord, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
pub struct Vec6<N>
{ at: [N, ..6] }
{
x: N,
y: N,
z: N,
w: N,
a: N,
b: N
}
clone_impl!(Vec6)
deep_clone_impl!(Vec6)
new_impl!(Vec6, 6)
new_repeat_impl!(Vec6, elem, [elem | elem | elem | elem | elem | elem])
indexable_impl!(Vec6)
new_impl!(Vec6, x, y, z, w, a, b)
indexable_impl!(Vec6, 6)
new_repeat_impl!(Vec6, val, x, y, z, w, a, b)
dim_impl!(Vec6, 6)
eq_impl!(Vec6)
basis_impl!(Vec6, 6)
add_impl!(Vec6)
sub_impl!(Vec6)
neg_impl!(Vec6)
dot_impl!(Vec6, 6)
sub_dot_impl!(Vec6, 6)
scalar_mul_impl!(Vec6, 6)
scalar_div_impl!(Vec6, 6)
scalar_add_impl!(Vec6, 6)
scalar_sub_impl!(Vec6, 6)
add_impl!(Vec6, x, y, z, w, a, b)
sub_impl!(Vec6, x, y, z, w, a, b)
neg_impl!(Vec6, x, y, z, w, a, b)
dot_impl!(Vec6, x, y, z, w, a, b)
sub_dot_impl!(Vec6, x, y, z, w, a, b)
scalar_mul_impl!(Vec6, x, y, z, w, a, b)
scalar_div_impl!(Vec6, x, y, z, w, a, b)
scalar_add_impl!(Vec6, x, y, z, w, a, b)
scalar_sub_impl!(Vec6, x, y, z, w, a, b)
translation_impl!(Vec6)
translatable_impl!(Vec6)
norm_impl!(Vec6, 6)
approx_eq_impl!(Vec6)
zero_impl!(Vec6)
norm_impl!(Vec6)
approx_eq_impl!(Vec6, x, y, z, w, a, b)
one_impl!(Vec6)
rand_impl!(Vec6, rng, [rng | rng | rng | rng | rng | rng])
from_iterator_impl!(Vec6, iterator, [iterator | iterator | iterator | iterator | iterator | iterator])
from_any_iterator_impl!(Vec6, iterator, [iterator | iterator | iterator | iterator | iterator | iterator])
from_iterator_impl!(Vec6, iterator, iterator, iterator, iterator, iterator, iterator)
bounded_impl!(Vec6)
iterable_impl!(Vec6)
iterable_mut_impl!(Vec6)
iterable_impl!(Vec6, 6)
iterable_mut_impl!(Vec6, 6)

View File

@ -1,102 +1,72 @@
#[macro_escape];
macro_rules! clone_impl(
($t:ident) => (
// FIXME: use 'Clone' alone. For the moment, we need 'Copy' because the automatic
// implementation of Clone for [t, ..n] is badly typed.
impl<N: Clone + Copy> Clone for $t<N>
{
fn clone(&self) -> $t<N>
{
$t { at: copy self.at }
}
}
)
)
macro_rules! deep_clone_impl(
($t:ident) => (
// FIXME: use 'DeepClone' alone. For the moment, we need 'Copy' because the automatic
// implementation of DeepClone for [t, ..n] is badly typed.
// XXX: this does not do a real deep clone
impl<N: DeepClone + Copy> DeepClone for $t<N>
{
fn deep_clone(&self) -> $t<N>
{
$t { at: copy self.at }
}
}
)
)
macro_rules! new_impl(
($t: ident, $dim: expr) => (
($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N> $t<N>
{
#[inline]
pub fn new(at: [N, ..$dim]) -> $t<N>
{ $t { at: at } }
pub fn new($comp0: N $( , $compN: N )*) -> $t<N>
{
$t {
$comp0: $comp0
$(, $compN: $compN )*
}
}
}
)
)
macro_rules! indexable_impl(
($t: ident) => (
($t: ident, $dim: expr) => (
impl<N: Clone> Indexable<uint, N> for $t<N>
{
#[inline]
pub fn at(&self, i: uint) -> N
{ self.at[i].clone() }
{ unsafe { cast::transmute::<&$t<N>, &[N, ..$dim]>(self)[i].clone() } }
#[inline]
pub fn set(&mut self, i: uint, val: N)
{ self.at[i] = val }
{ 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) } }
}
)
)
macro_rules! new_repeat_impl(
($t: ident, $param: ident, [ $($elem: ident)|+ ]) => (
($t: ident, $param: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Clone> $t<N>
{
#[inline]
pub fn new_repeat($param: N) -> $t<N>
{ $t{ at: [ $( $elem.clone(), )+ ] } }
{
$t{
$comp0: $param.clone()
$(, $compN: $param.clone() )*
}
}
}
)
)
macro_rules! iterable_impl(
($t: ident) => (
($t: ident, $dim: expr) => (
impl<N> Iterable<N> for $t<N>
{
fn iter<'l>(&'l self) -> VecIterator<'l, N>
{ self.at.iter() }
{ unsafe { cast::transmute::<&'l $t<N>, &'l [N, ..$dim]>(self).iter() } }
}
)
)
macro_rules! iterable_mut_impl(
($t: ident) => (
($t: ident, $dim: expr) => (
impl<N> IterableMut<N> for $t<N>
{
fn mut_iter<'l>(&'l mut self) -> VecMutIterator<'l, N>
{ self.at.mut_iter() }
}
)
)
macro_rules! eq_impl(
($t: ident) => (
impl<N: Eq> Eq for $t<N>
{
#[inline]
fn eq(&self, other: &$t<N>) -> bool
{ self.at.iter().zip(other.at.iter()).all(|(a, b)| a == b) }
#[inline]
fn ne(&self, other: &$t<N>) -> bool
{ self.at.iter().zip(other.at.iter()).all(|(a, b)| a != b) }
{ unsafe { cast::transmute::<&'l mut $t<N>, &'l mut [N, ..$dim]>(self).mut_iter() } }
}
)
)
@ -115,7 +85,7 @@ macro_rules! dim_impl(
// FIXME: add the possibility to specialize that
macro_rules! basis_impl(
($t: ident, $dim: expr) => (
impl<N: Clone + Copy + DivisionRing + Algebraic + ApproxEq<N>> Basis for $t<N>
impl<N: Clone + DivisionRing + Algebraic + ApproxEq<N>> Basis for $t<N>
{
pub fn canonical_basis(f: &fn($t<N>))
{
@ -123,7 +93,7 @@ macro_rules! basis_impl(
{
let mut basis_element : $t<N> = Zero::zero();
basis_element.at[i] = One::one();
basis_element.set(i, One::one());
f(basis_element);
}
@ -139,7 +109,7 @@ macro_rules! basis_impl(
{
let mut basis_element : $t<N> = Zero::zero();
basis_element.at[i] = One::one();
basis_element.set(i, One::one());
if basis.len() == $dim - 1
{ break; }
@ -166,152 +136,127 @@ macro_rules! basis_impl(
)
macro_rules! add_impl(
($t: ident) => (
($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Clone + Add<N,N>> Add<$t<N>, $t<N>> for $t<N>
{
#[inline]
fn add(&self, other: &$t<N>) -> $t<N>
{
self.at.iter()
.zip(other.at.iter())
.transform(|(a, b)| { *a + *b })
.collect()
}
{ $t::new(self.$comp0 + other.$comp0 $(, self.$compN + other.$compN)*) }
}
)
)
macro_rules! sub_impl(
($t: ident) => (
($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Clone + Sub<N,N>> Sub<$t<N>, $t<N>> for $t<N>
{
#[inline]
fn sub(&self, other: &$t<N>) -> $t<N>
{
self.at.iter()
.zip(other.at.iter())
.transform(| (a, b) | { *a - *b })
.collect()
}
{ $t::new(self.$comp0 - other.$comp0 $(, self.$compN - other.$compN)*) }
}
)
)
macro_rules! neg_impl(
($t: ident) => (
($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Neg<N>> Neg<$t<N>> for $t<N>
{
#[inline]
fn neg(&self) -> $t<N>
{ self.at.iter().transform(|a| -a).collect() }
{ $t::new(-self.$comp0 $(, -self.$compN )*) }
}
)
)
macro_rules! dot_impl(
($t: ident, $dim: expr) => (
($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Ring> Dot<N> for $t<N>
{
#[inline]
fn dot(&self, other: &$t<N>) -> N
{
let mut res = Zero::zero::<N>();
for iterate(0u, $dim) |i|
{ res = res + self.at[i] * other.at[i]; }
res
}
{ self.$comp0 * other.$comp0 $(+ self.$compN * other.$compN )* }
}
)
)
macro_rules! sub_dot_impl(
($t: ident, $dim: expr) => (
($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Ring> SubDot<N> for $t<N>
{
#[inline]
fn sub_dot(&self, a: &$t<N>, b: &$t<N>) -> N
{
let mut res = Zero::zero::<N>();
for iterate(0u, $dim) |i|
{ res = res + (self.at[i] - a.at[i]) * b.at[i]; }
res
}
{ (self.$comp0 - a.$comp0) * b.$comp0 $(+ (self.$compN - a.$comp0) * b.$compN )* }
}
)
)
macro_rules! scalar_mul_impl(
($t: ident, $dim: expr) => (
($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Mul<N, N>> ScalarMul<N> for $t<N>
{
#[inline]
fn scalar_mul(&self, s: &N) -> $t<N>
{ self.at.iter().transform(|a| a * *s).collect() }
{ $t::new(self.$comp0 * *s $(, self.$compN * *s)*) }
#[inline]
fn scalar_mul_inplace(&mut self, s: &N)
{
for iterate(0u, $dim) |i|
{ self.at[i] = self.at[i] * *s; }
self.$comp0 = self.$comp0 * *s;
$(self.$compN = self.$compN * *s;)*
}
}
)
)
macro_rules! scalar_div_impl(
($t: ident, $dim: expr) => (
($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Div<N, N>> ScalarDiv<N> for $t<N>
{
#[inline]
fn scalar_div(&self, s: &N) -> $t<N>
{ self.at.iter().transform(|a| a / *s).collect() }
{ $t::new(self.$comp0 / *s $(, self.$compN / *s)*) }
#[inline]
fn scalar_div_inplace(&mut self, s: &N)
{
for iterate(0u, $dim) |i|
{ self.at[i] = self.at[i] / *s; }
self.$comp0 = self.$comp0 / *s;
$(self.$compN = self.$compN / *s;)*
}
}
)
)
macro_rules! scalar_add_impl(
($t: ident, $dim: expr) => (
($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Add<N, N>> ScalarAdd<N> for $t<N>
{
#[inline]
fn scalar_add(&self, s: &N) -> $t<N>
{ self.at.iter().transform(|a| a + *s).collect() }
{ $t::new(self.$comp0 + *s $(, self.$compN + *s)*) }
#[inline]
fn scalar_add_inplace(&mut self, s: &N)
{
for iterate(0u, $dim) |i|
{ self.at[i] = self.at[i] + *s; }
self.$comp0 = self.$comp0 + *s;
$(self.$compN = self.$compN + *s;)*
}
}
)
)
macro_rules! scalar_sub_impl(
($t: ident, $dim: expr) => (
($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Sub<N, N>> ScalarSub<N> for $t<N>
{
#[inline]
fn scalar_sub(&self, s: &N) -> $t<N>
{ self.at.iter().transform(|a| a - *s).collect() }
{ $t::new(self.$comp0 - *s $(, self.$compN - *s)*) }
#[inline]
fn scalar_sub_inplace(&mut self, s: &N)
{
for iterate(0u, $dim) |i|
{ self.at[i] = self.at[i] - *s; }
self.$comp0 = self.$comp0 - *s;
$(self.$compN = self.$compN - *s;)*
}
}
)
@ -319,7 +264,7 @@ macro_rules! scalar_sub_impl(
macro_rules! translation_impl(
($t: ident) => (
impl<N: Clone + Copy + Add<N, N> + Neg<N>> Translation<$t<N>> for $t<N>
impl<N: Clone + Add<N, N> + Neg<N>> Translation<$t<N>> for $t<N>
{
#[inline]
fn translation(&self) -> $t<N>
@ -348,8 +293,8 @@ macro_rules! translatable_impl(
)
macro_rules! norm_impl(
($t: ident, $dim: expr) => (
impl<N: Clone + Copy + DivisionRing + Algebraic> Norm<N> for $t<N>
($t: ident) => (
impl<N: Clone + DivisionRing + Algebraic> Norm<N> for $t<N>
{
#[inline]
fn sqnorm(&self) -> N
@ -374,8 +319,7 @@ macro_rules! norm_impl(
{
let l = self.norm();
for iterate(0u, $dim) |i|
{ self.at[i] = self.at[i] / l; }
self.scalar_div_inplace(&l);
l
}
@ -384,7 +328,7 @@ macro_rules! norm_impl(
)
macro_rules! approx_eq_impl(
($t: ident) => (
($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: ApproxEq<N>> ApproxEq<N> for $t<N>
{
#[inline]
@ -393,34 +337,11 @@ macro_rules! approx_eq_impl(
#[inline]
fn approx_eq(&self, other: &$t<N>) -> bool
{
let mut zip = self.at.iter().zip(other.at.iter());
do zip.all |(a, b)| { a.approx_eq(b) }
}
{ self.$comp0.approx_eq(&other.$comp0) $(&& self.$compN.approx_eq(&other.$compN))* }
#[inline]
fn approx_eq_eps(&self, other: &$t<N>, epsilon: &N) -> bool
{
let mut zip = self.at.iter().zip(other.at.iter());
do zip.all |(a, b)| { a.approx_eq_eps(b, epsilon) }
}
}
)
)
macro_rules! zero_impl(
($t: ident) => (
impl<N: Clone + Zero> Zero for $t<N>
{
#[inline]
fn zero() -> $t<N>
{ $t::new_repeat(Zero::zero()) }
#[inline]
fn is_zero(&self) -> bool
{ self.at.iter().all(|e| e.is_zero()) }
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))* }
}
)
)
@ -436,36 +357,12 @@ macro_rules! one_impl(
)
)
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(), )+ ]) }
}
)
)
macro_rules! from_any_iterator_impl(
($t: ident, $param: ident, [ $($elem: ident)|+ ]) => (
impl<N: Clone> FromAnyIterator<N> for $t<N>
{
fn from_iterator<'l>($param: &mut VecIterator<'l, N>) -> $t<N>
{ $t { at: [ $( $elem.next().unwrap().clone(), )+ ] } }
fn from_mut_iterator<'l>($param: &mut VecMutIterator<'l, N>) -> $t<N>
{ $t { at: [ $( $elem.next().unwrap().clone(), )+ ] } }
}
)
)
macro_rules! from_iterator_impl(
($t: ident, $param: ident, [ $($elem: ident)|+ ]) => (
($t: ident, $param0: ident $(, $paramN: ident)*) => (
impl<N, Iter: Iterator<N>> FromIterator<N, Iter> for $t<N>
{
fn from_iterator($param: &mut Iter) -> $t<N>
{ $t { at: [ $( $elem.next().unwrap(), )+ ] } }
fn from_iterator($param0: &mut Iter) -> $t<N>
{ $t::new($param0.next().unwrap() $(, $paramN.next().unwrap())*) }
}
)
)
@ -486,39 +383,37 @@ macro_rules! bounded_impl(
)
macro_rules! to_homogeneous_impl(
($t: ident, $t2: ident) =>
{
impl<N: Clone + One> ToHomogeneous<$t2<N>> for $t<N>
($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();
for self.iter().zip(res.mut_iter()).advance |(in, out)|
{ *out = in.clone() }
res.$comp0 = self.$comp0.clone();
$( res.$compN = self.$compN.clone(); )*
res
}
}
}
)
)
macro_rules! from_homogeneous_impl(
($t: ident, $t2: ident, $dim2: expr) =>
{
($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();
for v.iter().zip(res.mut_iter()).advance |(in, out)|
{ *out = in.clone() }
res.$comp0 = v.$comp0.clone();
$( res.$compN = v.$compN.clone(); )*
res.scalar_div(&v.at[$dim2 - 1]);
res.scalar_div(&v.$extra);
res
}
}
}
)
)

View File

@ -1,34 +1,22 @@
use std::vec::{VecIterator, VecMutIterator};
use std::iterator::FromIterator;
use std::num::{Zero, One};
use traits::basis::Basis;
use traits::cross::Cross;
use traits::division_ring::DivisionRing;
use traits::norm::Norm;
use traits::sample::UniformSphereSample;
use traits::iterable::FromAnyIterator;
use vec::{Vec0, Vec1, Vec2, Vec3};
use vec::{Vec1, Vec2, Vec3};
impl<N: Clone> FromAnyIterator<N> for Vec0<N>
{
fn from_iterator<'l>(_: &mut VecIterator<'l, N>) -> Vec0<N>
{ Vec0 { at: [ ] } }
fn from_mut_iterator<'l>(_: &mut VecMutIterator<'l, N>) -> Vec0<N>
{ Vec0 { at: [ ] } }
}
impl<N, Iter: Iterator<N>> FromIterator<N, Iter> for Vec0<N>
{
fn from_iterator(_: &mut Iter) -> Vec0<N>
{ Vec0 { at: [ ] } }
}
// FIXME: impl<N, Iter: Iterator<N>> FromIterator<N, Iter> for Vec0<N>
// FIXME: {
// FIXME: fn from_iterator(_: &mut Iter) -> Vec0<N>
// FIXME: { Vec0 { at: [ ] } }
// FIXME: }
impl<N: Mul<N, N> + Sub<N, N>> Cross<Vec1<N>> for Vec2<N>
{
#[inline]
fn cross(&self, other : &Vec2<N>) -> Vec1<N>
{ Vec1::new([self.at[0] * other.at[1] - self.at[1] * other.at[0]]) }
{ Vec1::new(self.x * other.y - self.y * other.x) }
}
impl<N: Mul<N, N> + Sub<N, N>> Cross<Vec3<N>> for Vec3<N>
@ -36,10 +24,9 @@ impl<N: Mul<N, N> + Sub<N, N>> Cross<Vec3<N>> for Vec3<N>
#[inline]
fn cross(&self, other : &Vec3<N>) -> Vec3<N>
{
Vec3::new(
[self.at[1] * other.at[2] - self.at[2] * other.at[1],
self.at[2] * other.at[0] - self.at[0] * other.at[2],
self.at[0] * other.at[1] - self.at[1] * other.at[0]]
Vec3::new(self.y * other.z - self.z * other.y,
self.z * other.x - self.x * other.z,
self.x * other.y - self.y * other.x
)
}
}
@ -48,7 +35,7 @@ impl<N: One> Basis for Vec1<N>
{
#[inline(always)]
fn canonical_basis(f: &fn(Vec1<N>))
{ f(Vec1::new([One::one()])) }
{ f(Vec1::new(One::one())) }
#[inline(always)]
fn orthonormal_subspace_basis(&self, _: &fn(Vec1<N>))
@ -60,34 +47,34 @@ impl<N: Clone + One + Zero + Neg<N>> Basis for Vec2<N>
#[inline]
fn canonical_basis(f: &fn(Vec2<N>))
{
f(Vec2::new([One::one(), Zero::zero()]));
f(Vec2::new([Zero::zero(), One::one()]));
f(Vec2::new(One::one(), Zero::zero()));
f(Vec2::new(Zero::zero(), One::one()));
}
#[inline]
fn orthonormal_subspace_basis(&self, f: &fn(Vec2<N>))
{ f(Vec2::new([-self.at[1], self.at[0].clone()])) }
{ f(Vec2::new(-self.y, self.x.clone())) }
}
impl<N: Clone + Copy + DivisionRing + Ord + Algebraic + Signed>
impl<N: Clone + DivisionRing + Ord + Algebraic + Signed>
Basis for Vec3<N>
{
#[inline(always)]
fn canonical_basis(f: &fn(Vec3<N>))
{
f(Vec3::new([One::one(), Zero::zero(), Zero::zero()]));
f(Vec3::new([Zero::zero(), One::one(), Zero::zero()]));
f(Vec3::new([Zero::zero(), Zero::zero(), One::one()]));
f(Vec3::new(One::one(), Zero::zero(), Zero::zero()));
f(Vec3::new(Zero::zero(), One::one(), Zero::zero()));
f(Vec3::new(Zero::zero(), Zero::zero(), One::one()));
}
#[inline(always)]
fn orthonormal_subspace_basis(&self, f: &fn(Vec3<N>))
{
let a =
if self.at[0].clone().abs() > self.at[1].clone().abs()
{ Vec3::new([self.at[2].clone(), Zero::zero(), -self.at[0]]).normalized() }
if self.x.clone().abs() > self.y.clone().abs()
{ Vec3::new(self.z.clone(), Zero::zero(), -self.x).normalized() }
else
{ Vec3::new([Zero::zero(), -self.at[2], self.at[1].clone()]).normalized() };
{ Vec3::new(Zero::zero(), -self.z, self.y.clone()).normalized() };
f(a.cross(self));
f(a);
@ -96,27 +83,27 @@ Basis for Vec3<N>
// FIXME: this bad: this fixes definitly the number of samples…
static SAMPLES_2_F64: [Vec2<f64>, ..21] = [
Vec2 { at: [1.0, 0.0] },
Vec2 { at: [0.95557281, 0.29475517] },
Vec2 { at: [0.82623877, 0.56332006] },
Vec2 { at: [0.6234898, 0.78183148] },
Vec2 { at: [0.36534102, 0.93087375] },
Vec2 { at: [0.07473009, 0.9972038] },
Vec2 { at: [-0.22252093, 0.97492791] },
Vec2 { at: [-0.5, 0.8660254] },
Vec2 { at: [-0.73305187, 0.68017274] },
Vec2 { at: [-0.90096887, 0.43388374] },
Vec2 { at: [-0.98883083, 0.14904227] },
Vec2 { at: [-0.98883083, -0.14904227] },
Vec2 { at: [-0.90096887, -0.43388374] },
Vec2 { at: [-0.73305187, -0.68017274] },
Vec2 { at: [-0.5, -0.8660254] },
Vec2 { at: [-0.22252093, -0.97492791] },
Vec2 { at: [0.07473009, -0.9972038] },
Vec2 { at: [0.36534102, -0.93087375] },
Vec2 { at: [0.6234898, -0.78183148] },
Vec2 { at: [0.82623877, -0.56332006] },
Vec2 { at: [0.95557281, -0.29475517] },
Vec2 { x: 1.0, y: 0.0 },
Vec2 { x: 0.95557281, y: 0.29475517 },
Vec2 { x: 0.82623877, y: 0.56332006 },
Vec2 { x: 0.6234898, y: 0.78183148 },
Vec2 { x: 0.36534102, y: 0.93087375 },
Vec2 { x: 0.07473009, y: 0.9972038 },
Vec2 { x: -0.22252093, y: 0.97492791 },
Vec2 { x: -0.5, y: 0.8660254 },
Vec2 { x: -0.73305187, y: 0.68017274 },
Vec2 { x: -0.90096887, y: 0.43388374 },
Vec2 { x: -0.98883083, y: 0.14904227 },
Vec2 { x: -0.98883083, y: -0.14904227 },
Vec2 { x: -0.90096887, y: -0.43388374 },
Vec2 { x: -0.73305187, y: -0.68017274 },
Vec2 { x: -0.5, y: -0.8660254 },
Vec2 { x: -0.22252093, y: -0.97492791 },
Vec2 { x: 0.07473009, y: -0.9972038 },
Vec2 { x: 0.36534102, y: -0.93087375 },
Vec2 { x: 0.6234898, y: -0.78183148 },
Vec2 { x: 0.82623877, y: -0.56332006 },
Vec2 { x: 0.95557281, y: -0.29475517 },
];
impl UniformSphereSample for Vec2<f64>