From cf216f9b9036507cd829c05a4414c6bcfcbe9771 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Sat, 20 Jul 2013 15:07:49 +0200 Subject: [PATCH] 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. --- pkg.rs | 3 - src/adaptors/rotmat.rs | 24 +-- src/adaptors/transform.rs | 26 +-- src/dmat.rs | 26 +-- src/dvec.rs | 22 +-- src/lib.rs | 0 src/mat.rs | 249 ++++++++++++------------ src/mat_impl.rs | 122 +++++------- src/mat_spec.rs | 50 ++--- src/nalgebra.rc | 8 +- src/tests/mat.rs | 2 +- src/tests/vec.rs | 4 +- src/traits/indexable.rs | 1 + src/traits/iterable.rs | 25 --- src/vec.rs | 392 +++++++++++++++++++------------------- src/vec_impl.rs | 257 ++++++++----------------- src/vec_spec.rs | 97 ++++------ 17 files changed, 565 insertions(+), 743 deletions(-) delete mode 100644 pkg.rs delete mode 100644 src/lib.rs diff --git a/pkg.rs b/pkg.rs deleted file mode 100644 index 8bd591f5..00000000 --- a/pkg.rs +++ /dev/null @@ -1,3 +0,0 @@ -#[pkg(id = "re.crozet.nalgebra", vers = "0.0.0")]; - -#[pkg_crate(file = "src/nalgebra.rc")]; diff --git a/src/adaptors/rotmat.rs b/src/adaptors/rotmat.rs index 9b927d6c..955ebe14 100644 --- a/src/adaptors/rotmat.rs +++ b/src/adaptors/rotmat.rs @@ -30,10 +30,10 @@ pub fn rotmat2>(angle: N) -> Rotmat> 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 +pub fn rotmat3 (axisangle: Vec3) -> Rotmat> { if axisangle.sqnorm().is_zero() @@ -43,9 +43,9 @@ pub fn rotmat3 let mut axis = axisangle; let angle = axis.normalize(); let _1 = One::one::(); - 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 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 (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> for Rotmat> { #[inline] fn rotation(&self) -> Vec1 - { 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 @@ -90,10 +90,10 @@ Rotatable, Rotmat>> for Rotmat> { #[inline] fn rotated(&self, rot: &Vec1) -> Rotmat> - { rotmat2(rot.at[0].clone()) * *self } + { rotmat2(rot.x.clone()) * *self } } -impl +impl Rotation> for Rotmat> { #[inline] @@ -110,7 +110,7 @@ Rotation> for Rotmat> { *self = self.rotated(rot) } } -impl +impl Rotatable, Rotmat>> for Rotmat> { #[inline] @@ -147,7 +147,7 @@ impl + LMul, V> Transform for Rotmat { self.inv_rotate(v) } } -impl +impl Rand for Rotmat> { #[inline] diff --git a/src/adaptors/transform.rs b/src/adaptors/transform.rs index 2285117e..4430641c 100644 --- a/src/adaptors/transform.rs +++ b/src/adaptors/transform.rs @@ -25,15 +25,15 @@ impl Transform { Transform { submat: mat, subtrans: trans } } } -impl Transform +impl Transform { #[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 Dim for Transform @@ -112,12 +112,12 @@ impl, V, _0> Translate for Transform { self.submat.inv_translate(v) } } -impl + Translation> +impl + Translation> Translatable> for Transform { #[inline] fn translated(&self, t: &V) -> Transform - { Transform::new(copy self.submat, self.subtrans.translated(t)) } + { Transform::new(self.submat.clone(), self.subtrans.translated(t)) } } impl + RMul + One, @@ -172,11 +172,11 @@ Rotatable> for Transform } } -impl + Mul + Copy, V: Add + Neg + Copy> +impl + Mul + Clone, V: Add + Neg + Clone> Transformation> for Transform { fn transformation(&self) -> Transform - { copy *self } + { self.clone() } fn inv_transformation(&self) -> Transform { @@ -207,14 +207,14 @@ transformation::Transform for Transform // FIXME: constraints are too restrictive. // Should be: Transformable ... -impl + Mul + Inv, V: Add + Neg> +impl + Mul + Inv + Clone, V: Add + Neg + Clone> Transformable, Transform> for Transform { fn transformed(&self, t: &Transform) -> Transform { t * *self } } -impl, V: Copy + Neg> +impl + Clone, V: Neg + Clone> Inv for Transform { #[inline] @@ -232,7 +232,7 @@ Inv for Transform #[inline] fn inverse(&self) -> Option> { - let mut res = copy *self; + let mut res = self.clone(); if res.inplace_inverse() { Some(res) } @@ -241,7 +241,7 @@ Inv for Transform } } -impl, M2: Dim + Column, V: Copy> +impl, M2: Dim + Column, V: Clone> ToHomogeneous for Transform { fn to_homogeneous(&self) -> M2 @@ -251,13 +251,13 @@ ToHomogeneous for Transform // copy the translation let dim = Dim::dim::(); - res.set_column(dim - 1, copy self.subtrans); + res.set_column(dim - 1, self.subtrans.clone()); res } } -impl + Dim, M2: FromHomogeneous, V: Copy> +impl + Dim, M2: FromHomogeneous, V> FromHomogeneous for Transform { fn from_homogeneous(m: &M) -> Transform diff --git a/src/dmat.rs b/src/dmat.rs index eb4d32a5..2572f316 100644 --- a/src/dmat.rs +++ b/src/dmat.rs @@ -17,7 +17,7 @@ pub struct DMat } #[inline] -pub fn zero_mat_with_dim(dim: uint) -> DMat +pub fn zero_mat_with_dim(dim: uint) -> DMat { DMat { dim: dim, mij: from_elem(dim * dim, Zero::zero()) } } #[inline] @@ -25,7 +25,7 @@ pub fn is_zero_mat(mat: &DMat) -> bool { mat.mij.iter().all(|e| e.is_zero()) } #[inline] -pub fn one_mat_with_dim(dim: uint) -> DMat +pub fn one_mat_with_dim(dim: uint) -> DMat { let mut res = zero_mat_with_dim(dim); let _1 = One::one::(); @@ -36,7 +36,7 @@ pub fn one_mat_with_dim(dim: uint) -> DMat res } -impl DMat +impl DMat { #[inline] pub fn offset(&self, i: uint, j: uint) -> uint @@ -47,7 +47,7 @@ impl DMat { 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 DMat { assert!(i < self.dim); assert!(j < self.dim); - copy self.mij[self.offset(i, j)] + self.mij[self.offset(i, j)].clone() } } -impl Index<(uint, uint), N> for DMat +impl Index<(uint, uint), N> for DMat { #[inline] fn index(&self, &(i, j): &(uint, uint)) -> N { self.at(i, j) } } -impl + Add + Zero> +impl + Add + Zero> Mul, DMat> for DMat { fn mul(&self, other: &DMat) -> DMat @@ -93,7 +93,7 @@ Mul, DMat> for DMat } } -impl + Mul + Zero> +impl + Mul + Zero> RMul> for DMat { fn rmul(&self, other: &DVec) -> DVec @@ -113,7 +113,7 @@ RMul> for DMat } } -impl + Mul + Zero> +impl + Mul + Zero> LMul> for DMat { fn lmul(&self, other: &DVec) -> DVec @@ -133,13 +133,13 @@ LMul> for DMat } } -impl +impl Inv for DMat { #[inline] fn inverse(&self) -> Option> { - let mut res : DMat = copy *self; + let mut res : DMat = self.clone(); if res.inplace_inverse() { Some(res) } @@ -227,12 +227,12 @@ Inv for DMat } } -impl Transpose for DMat +impl Transpose for DMat { #[inline] fn transposed(&self) -> DMat { - let mut res = copy *self; + let mut res = self.clone(); res.transpose(); diff --git a/src/dvec.rs b/src/dvec.rs index 4d9f5a35..74fde06b 100644 --- a/src/dvec.rs +++ b/src/dvec.rs @@ -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 { at: ~[N] } #[inline] -pub fn zero_vec_with_dim(dim: uint) -> DVec +pub fn zero_vec_with_dim(dim: uint) -> DVec { DVec { at: from_elem(dim, Zero::zero::()) } } #[inline] @@ -53,7 +53,7 @@ impl> FromIterator for DVec } // FIXME: is Clone needed? -impl> DVec +impl> DVec { pub fn canonical_basis_with_dim(dim: uint) -> ~[DVec] { @@ -87,7 +87,7 @@ impl> DVec 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> DVec } } -impl> Add, DVec> for DVec +impl> Add, DVec> for DVec { #[inline] fn add(&self, other: &DVec) -> DVec @@ -116,7 +116,7 @@ impl> Add, DVec> for DVec } } -impl> Sub, DVec> for DVec +impl> Sub, DVec> for DVec { #[inline] fn sub(&self, other: &DVec) -> DVec @@ -227,11 +227,11 @@ ScalarSub for DVec } } -impl + Neg> Translation> for DVec +impl + Neg + Clone> Translation> for DVec { #[inline] fn translation(&self) -> DVec - { copy *self } + { self.clone() } #[inline] fn inv_translation(&self) -> DVec @@ -242,14 +242,14 @@ impl + Neg> Translation> for DVec { *self = *self + *t; } } -impl + Neg + Copy> Translatable, DVec> for DVec +impl + Neg + Clone> Translatable, DVec> for DVec { #[inline] fn translated(&self, t: &DVec) -> DVec { self + *t } } -impl +impl Norm for DVec { #[inline] @@ -263,7 +263,7 @@ Norm for DVec #[inline] fn normalized(&self) -> DVec { - let mut res : DVec = copy *self; + let mut res : DVec = self.clone(); res.normalize(); diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index e69de29b..00000000 diff --git a/src/mat.rs b/src/mat.rs index 2cf6b695..0f3e10b6 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -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 -{ 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 -{ 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 -{ 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 -{ 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 -{ 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 -{ 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) diff --git a/src/mat_impl.rs b/src/mat_impl.rs index 2df49151..f5a996c6 100644 --- a/src/mat_impl.rs +++ b/src/mat_impl.rs @@ -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 Clone for $t +macro_rules! mat_impl( + ($t: ident, $dim: expr, $comp0: ident $(,$compN: ident)*) => ( + impl $t { #[inline] - fn clone(&self) -> $t + pub fn new($comp0: N $(, $compN: N )*) -> $t { $t { - mij: copy self.mij + $comp0: $comp0 + $(, $compN: $compN )* } } } ) ) -macro_rules! mat_impl( +macro_rules! iterable_impl( ($t: ident, $dim: expr) => ( - impl $t + impl Iterable for $t { - #[inline] - pub fn new(mij: [N, ..$dim * $dim]) -> $t - { $t { mij: mij } } + fn iter<'l>(&'l self) -> VecIterator<'l, N> + { unsafe { cast::transmute::<&'l $t, &'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 IterableMut for $t + { + fn mut_iter<'l>(&'l mut self) -> VecMutIterator<'l, N> + { unsafe { cast::transmute::<&'l mut $t, &'l mut [N, ..$dim * $dim]>(self).mut_iter() } } } ) ) macro_rules! one_impl( - ($t: ident, [ $($value: ident)|+ ] ) => ( + ($t: ident, $value0: ident $(, $valueN: ident)* ) => ( impl One for $t { #[inline] fn one() -> $t { let (_0, _1) = (Zero::zero::(), One::one::()); - return $t::new( [ $( $value.clone(), )+ ] ) + return $t::new($value0.clone() $(, $valueN.clone() )*) } } ) ) -macro_rules! zero_impl( - ($t: ident, [ $($value: ident)|+ ] ) => ( - impl Zero for $t - { - #[inline] - fn zero() -> $t - { - let _0 = Zero::zero::(); - 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 Dim for $t @@ -75,17 +61,26 @@ macro_rules! dim_impl( ) ) -macro_rules! mat_indexable_impl( +macro_rules! indexable_impl( ($t: ident, $dim: expr) => ( impl Indexable<(uint, uint), N> for $t { #[inline] pub fn at(&self, (i, j): (uint, uint)) -> N - { self.mij[self.offset(i, j)].clone() } + { unsafe { cast::transmute::<&$t, &[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, &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, &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 + impl Transform<$v> for $t { #[inline] @@ -218,7 +219,7 @@ macro_rules! transform_impl( macro_rules! inv_impl( ($t: ident, $dim: expr) => ( - impl + impl Inv for $t { #[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 Transpose for $t + impl Transpose for $t { #[inline] fn transposed(&self) -> $t @@ -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) -> 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, 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 Rand for $t - { - #[inline] - fn rand($param: &mut R) -> $t - { $t::new([ $( $elem.gen(), )+ ]) } - } - ) -) - macro_rules! to_homogeneous_impl( - ($t: ident, $t2: ident, $dim: expr) => ( + ($t: ident, $t2: ident, $dim: expr, $dim2: expr) => ( impl ToHomogeneous<$t2> for $t { fn to_homogeneous(&self) -> $t2 @@ -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 FromHomogeneous<$t2> for $t { fn from_homogeneous(m: &$t2) -> $t diff --git a/src/mat_spec.rs b/src/mat_spec.rs index 229b1896..298389d7 100644 --- a/src/mat_spec.rs +++ b/src/mat_spec.rs @@ -4,13 +4,13 @@ use traits::division_ring::DivisionRing; use traits::inv::Inv; // some specializations: -impl +impl Inv for Mat1 { #[inline] fn inverse(&self) -> Option> { - let mut res : Mat1 = copy *self; + let mut res : Mat1 = self.clone(); if res.inplace_inverse() { Some(res) } @@ -21,23 +21,23 @@ Inv for Mat1 #[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::() / self.mij[0]; + self.m11 = One::one::() / self.m11; true } } } -impl +impl Inv for Mat2 { #[inline] fn inverse(&self) -> Option> { - let mut res : Mat2 = copy *self; + let mut res : Mat2 = self.clone(); if res.inplace_inverse() { Some(res) } @@ -48,27 +48,27 @@ Inv for Mat2 #[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 +impl Inv for Mat3 { #[inline] fn inverse(&self) -> Option> { - let mut res = copy *self; + let mut res = self.clone(); if res.inplace_inverse() { Some(res) } @@ -79,31 +79,31 @@ Inv for Mat3 #[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 } diff --git a/src/nalgebra.rc b/src/nalgebra.rc index 078ee818..f60ac384 100644 --- a/src/nalgebra.rc +++ b/src/nalgebra.rc @@ -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; diff --git a/src/tests/mat.rs b/src/tests/mat.rs index f7bff18d..dd9e9937 100644 --- a/src/tests/mat.rs +++ b/src/tests/mat.rs @@ -60,7 +60,7 @@ fn test_rotation2() for 10000.times { let randmat = One::one::>>(); - let ang = &Vec1::new([abs::(random()) % Real::pi()]); + let ang = &Vec1::new(abs::(random()) % Real::pi()); assert!(randmat.rotated(ang).rotation().approx_eq(ang)); } diff --git a/src/tests/vec.rs b/src/tests/vec.rs index e395a97b..27619d03 100644 --- a/src/tests/vec.rs +++ b/src/tests/vec.rs @@ -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); diff --git a/src/traits/indexable.rs b/src/traits/indexable.rs index 98b5a3aa..a2f7a29b 100644 --- a/src/traits/indexable.rs +++ b/src/traits/indexable.rs @@ -6,4 +6,5 @@ pub trait Indexable { fn at(&self, Index) -> Res; fn set(&mut self, Index, Res); + fn swap(&mut self, Index, Index); } diff --git a/src/traits/iterable.rs b/src/traits/iterable.rs index 930370fa..2ac6eceb 100644 --- a/src/traits/iterable.rs +++ b/src/traits/iterable.rs @@ -28,28 +28,3 @@ pub trait IterableMut<'self, N, I: Iterator> * For now, we oblige the iterator to be one specific type which works with * everything on this lib. */ - -pub trait FromAnyIterator -{ - 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 -{ - fn from_iterator>(&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> - * 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. - * - */ diff --git a/src/vec.rs b/src/vec.rs index b19731df..4651f5e7 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -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 -{ at: [N, ..0] } +// #[deriving(Ord, ToStr)] +// pub struct Vec0 +// { at: [N, ..0] } -impl Vec0 -{ - #[inline] - pub fn new_repeat(_: N) -> Vec0 - { Vec0 { at: [ ] } } -} +// impl Vec0 +// { +// #[inline] +// pub fn new_repeat(_: N) -> Vec0 +// { 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 -{ 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 -{ 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 -{ 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 -{ 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 -{ 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 -{ 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) diff --git a/src/vec_impl.rs b/src/vec_impl.rs index 95bb27d8..36422217 100644 --- a/src/vec_impl.rs +++ b/src/vec_impl.rs @@ -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 Clone for $t - { - fn clone(&self) -> $t - { - $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 DeepClone for $t - { - fn deep_clone(&self) -> $t - { - $t { at: copy self.at } - } - } - ) -) - macro_rules! new_impl( - ($t: ident, $dim: expr) => ( + ($t: ident, $comp0: ident $(,$compN: ident)*) => ( impl $t { #[inline] - pub fn new(at: [N, ..$dim]) -> $t - { $t { at: at } } + pub fn new($comp0: N $( , $compN: N )*) -> $t + { + $t { + $comp0: $comp0 + $(, $compN: $compN )* + } + } } ) ) macro_rules! indexable_impl( - ($t: ident) => ( + ($t: ident, $dim: expr) => ( impl Indexable for $t { #[inline] pub fn at(&self, i: uint) -> N - { self.at[i].clone() } + { unsafe { cast::transmute::<&$t, &[N, ..$dim]>(self)[i].clone() } } #[inline] pub fn set(&mut self, i: uint, val: N) - { self.at[i] = val } + { unsafe { cast::transmute::<&mut $t, &mut [N, ..$dim]>(self)[i] = val } } + + #[inline] + pub fn swap(&mut self, i1: uint, i2: uint) + { unsafe { cast::transmute::<&mut $t, &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 $t { #[inline] pub fn new_repeat($param: N) -> $t - { $t{ at: [ $( $elem.clone(), )+ ] } } + { + $t{ + $comp0: $param.clone() + $(, $compN: $param.clone() )* + } + } } ) ) macro_rules! iterable_impl( - ($t: ident) => ( + ($t: ident, $dim: expr) => ( impl Iterable for $t { fn iter<'l>(&'l self) -> VecIterator<'l, N> - { self.at.iter() } + { unsafe { cast::transmute::<&'l $t, &'l [N, ..$dim]>(self).iter() } } } ) ) macro_rules! iterable_mut_impl( - ($t: ident) => ( + ($t: ident, $dim: expr) => ( impl IterableMut for $t { fn mut_iter<'l>(&'l mut self) -> VecMutIterator<'l, N> - { self.at.mut_iter() } - } - ) -) - -macro_rules! eq_impl( - ($t: ident) => ( - impl Eq for $t - { - #[inline] - fn eq(&self, other: &$t) -> bool - { self.at.iter().zip(other.at.iter()).all(|(a, b)| a == b) } - - #[inline] - fn ne(&self, other: &$t) -> bool - { self.at.iter().zip(other.at.iter()).all(|(a, b)| a != b) } + { unsafe { cast::transmute::<&'l mut $t, &'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> Basis for $t + impl> Basis for $t { pub fn canonical_basis(f: &fn($t)) { @@ -123,7 +93,7 @@ macro_rules! basis_impl( { let mut basis_element : $t = 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 = 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> Add<$t, $t> for $t { #[inline] fn add(&self, other: &$t) -> $t - { - 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> Sub<$t, $t> for $t { #[inline] fn sub(&self, other: &$t) -> $t - { - 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> Neg<$t> for $t { #[inline] fn neg(&self) -> $t - { 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 Dot for $t { #[inline] fn dot(&self, other: &$t) -> N - { - let mut res = Zero::zero::(); - - 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 SubDot for $t { #[inline] fn sub_dot(&self, a: &$t, b: &$t) -> N - { - let mut res = Zero::zero::(); - - 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> ScalarMul for $t { #[inline] fn scalar_mul(&self, s: &N) -> $t - { 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> ScalarDiv for $t { #[inline] fn scalar_div(&self, s: &N) -> $t - { 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> ScalarAdd for $t { #[inline] fn scalar_add(&self, s: &N) -> $t - { 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> ScalarSub for $t { #[inline] fn scalar_sub(&self, s: &N) -> $t - { 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 + Neg> Translation<$t> for $t + impl + Neg> Translation<$t> for $t { #[inline] fn translation(&self) -> $t @@ -348,8 +293,8 @@ macro_rules! translatable_impl( ) macro_rules! norm_impl( - ($t: ident, $dim: expr) => ( - impl Norm for $t + ($t: ident) => ( + impl Norm for $t { #[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> ApproxEq for $t { #[inline] @@ -393,34 +337,11 @@ macro_rules! approx_eq_impl( #[inline] fn approx_eq(&self, other: &$t) -> 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, 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 Zero for $t - { - #[inline] - fn zero() -> $t - { $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, 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 Rand for $t - { - #[inline] - fn rand($param: &mut R) -> $t - { $t::new([ $( $elem.gen(), )+ ]) } - } - ) -) - -macro_rules! from_any_iterator_impl( - ($t: ident, $param: ident, [ $($elem: ident)|+ ]) => ( - impl FromAnyIterator for $t - { - fn from_iterator<'l>($param: &mut VecIterator<'l, N>) -> $t - { $t { at: [ $( $elem.next().unwrap().clone(), )+ ] } } - - fn from_mut_iterator<'l>($param: &mut VecMutIterator<'l, N>) -> $t - { $t { at: [ $( $elem.next().unwrap().clone(), )+ ] } } - } - ) -) - macro_rules! from_iterator_impl( - ($t: ident, $param: ident, [ $($elem: ident)|+ ]) => ( + ($t: ident, $param0: ident $(, $paramN: ident)*) => ( impl> FromIterator for $t { - fn from_iterator($param: &mut Iter) -> $t - { $t { at: [ $( $elem.next().unwrap(), )+ ] } } + fn from_iterator($param0: &mut Iter) -> $t + { $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 ToHomogeneous<$t2> for $t + ($t: ident, $t2: ident, $extra: ident, $comp0: ident $(,$compN: ident)*) => ( + impl ToHomogeneous<$t2> for $t { fn to_homogeneous(&self) -> $t2 { let mut res: $t2 = 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 + One + Zero> FromHomogeneous<$t2> for $t { fn from_homogeneous(v: &$t2) -> $t { let mut res: $t = 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 } } - } + ) ) diff --git a/src/vec_spec.rs b/src/vec_spec.rs index 9b4962c0..8d73b795 100644 --- a/src/vec_spec.rs +++ b/src/vec_spec.rs @@ -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 FromAnyIterator for Vec0 -{ - fn from_iterator<'l>(_: &mut VecIterator<'l, N>) -> Vec0 - { Vec0 { at: [ ] } } - - fn from_mut_iterator<'l>(_: &mut VecMutIterator<'l, N>) -> Vec0 - { Vec0 { at: [ ] } } -} - -impl> FromIterator for Vec0 -{ - fn from_iterator(_: &mut Iter) -> Vec0 - { Vec0 { at: [ ] } } -} +// FIXME: impl> FromIterator for Vec0 +// FIXME: { +// FIXME: fn from_iterator(_: &mut Iter) -> Vec0 +// FIXME: { Vec0 { at: [ ] } } +// FIXME: } impl + Sub> Cross> for Vec2 { #[inline] fn cross(&self, other : &Vec2) -> Vec1 - { 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 + Sub> Cross> for Vec3 @@ -36,10 +24,9 @@ impl + Sub> Cross> for Vec3 #[inline] fn cross(&self, other : &Vec3) -> Vec3 { - 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 Basis for Vec1 { #[inline(always)] fn canonical_basis(f: &fn(Vec1)) - { f(Vec1::new([One::one()])) } + { f(Vec1::new(One::one())) } #[inline(always)] fn orthonormal_subspace_basis(&self, _: &fn(Vec1)) @@ -60,34 +47,34 @@ impl> Basis for Vec2 #[inline] fn canonical_basis(f: &fn(Vec2)) { - 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)) - { f(Vec2::new([-self.at[1], self.at[0].clone()])) } + { f(Vec2::new(-self.y, self.x.clone())) } } -impl +impl Basis for Vec3 { #[inline(always)] fn canonical_basis(f: &fn(Vec3)) { - 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)) { 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 // FIXME: this bad: this fixes definitly the number of samples… static SAMPLES_2_F64: [Vec2, ..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