Merge pull request #124 from aepsil0n/remove-ops-hacks

Remove operator hacks
This commit is contained in:
Sébastien Crozet 2015-05-08 23:44:35 +02:00
commit cea93fcad3
8 changed files with 10 additions and 124 deletions

View File

@ -122,7 +122,6 @@ pub use traits::{
Inv, Inv,
Iterable, Iterable,
IterableMut, IterableMut,
LMul,
Mat, Mat,
Mean, Mean,
Norm, Norm,
@ -133,11 +132,8 @@ pub use traits::{
POrd, POrd,
POrdering, POrdering,
PntAsVec, PntAsVec,
RMul,
Rotate, Rotation, RotationMatrix, RotationWithTranslation, Rotate, Rotation, RotationMatrix, RotationWithTranslation,
Row, Row,
ScalarAdd, ScalarSub,
ScalarMul, ScalarDiv,
Shape, Shape,
SquareMat, SquareMat,
ToHomogeneous, ToHomogeneous,

View File

@ -9,8 +9,7 @@ use std::iter::{Iterator, FromIterator, IntoIterator};
use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut}; use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut};
use rand::{Rand, Rng}; use rand::{Rand, Rng};
use num::{Zero, One}; use num::{Zero, One};
use traits::operations::{ApproxEq, POrd, POrdering, Axpy, ScalarAdd, ScalarSub, ScalarMul, use traits::operations::{ApproxEq, POrd, POrdering, Axpy};
ScalarDiv};
use traits::structure::{Cast, Dim, Indexable, Iterable, IterableMut, PntAsVec, Shape, use traits::structure::{Cast, Dim, Indexable, Iterable, IterableMut, PntAsVec, Shape,
NumPnt, FloatPnt, BaseFloat, BaseNum, Bounded}; NumPnt, FloatPnt, BaseFloat, BaseNum, Bounded};
use traits::geometry::{Orig, FromHomogeneous, ToHomogeneous}; use traits::geometry::{Orig, FromHomogeneous, ToHomogeneous};
@ -66,7 +65,6 @@ pnt_sub_impl!(Pnt1, Vec1);
neg_impl!(Pnt1, x); neg_impl!(Pnt1, x);
pnt_add_vec_impl!(Pnt1, Vec1, x); pnt_add_vec_impl!(Pnt1, Vec1, x);
pnt_sub_vec_impl!(Pnt1, Vec1, x); pnt_sub_vec_impl!(Pnt1, Vec1, x);
scalar_ops_impl!(Pnt1, x);
approx_eq_impl!(Pnt1, x); approx_eq_impl!(Pnt1, x);
from_iterator_impl!(Pnt1, iterator); from_iterator_impl!(Pnt1, iterator);
bounded_impl!(Pnt1, x); bounded_impl!(Pnt1, x);
@ -109,7 +107,6 @@ pnt_sub_impl!(Pnt2, Vec2);
neg_impl!(Pnt2, x, y); neg_impl!(Pnt2, x, y);
pnt_add_vec_impl!(Pnt2, Vec2, x, y); pnt_add_vec_impl!(Pnt2, Vec2, x, y);
pnt_sub_vec_impl!(Pnt2, Vec2, x, y); pnt_sub_vec_impl!(Pnt2, Vec2, x, y);
scalar_ops_impl!(Pnt2, x, y);
approx_eq_impl!(Pnt2, x, y); approx_eq_impl!(Pnt2, x, y);
from_iterator_impl!(Pnt2, iterator, iterator); from_iterator_impl!(Pnt2, iterator, iterator);
bounded_impl!(Pnt2, x, y); bounded_impl!(Pnt2, x, y);
@ -154,7 +151,6 @@ pnt_sub_impl!(Pnt3, Vec3);
neg_impl!(Pnt3, x, y, z); neg_impl!(Pnt3, x, y, z);
pnt_add_vec_impl!(Pnt3, Vec3, x, y, z); pnt_add_vec_impl!(Pnt3, Vec3, x, y, z);
pnt_sub_vec_impl!(Pnt3, Vec3, x, y, z); pnt_sub_vec_impl!(Pnt3, Vec3, x, y, z);
scalar_ops_impl!(Pnt3, x, y, z);
approx_eq_impl!(Pnt3, x, y, z); approx_eq_impl!(Pnt3, x, y, z);
from_iterator_impl!(Pnt3, iterator, iterator, iterator); from_iterator_impl!(Pnt3, iterator, iterator, iterator);
bounded_impl!(Pnt3, x, y, z); bounded_impl!(Pnt3, x, y, z);
@ -201,7 +197,6 @@ pnt_sub_impl!(Pnt4, Vec4);
neg_impl!(Pnt4, x, y, z, w); neg_impl!(Pnt4, x, y, z, w);
pnt_add_vec_impl!(Pnt4, Vec4, x, y, z, w); pnt_add_vec_impl!(Pnt4, Vec4, x, y, z, w);
pnt_sub_vec_impl!(Pnt4, Vec4, x, y, z, w); pnt_sub_vec_impl!(Pnt4, Vec4, x, y, z, w);
scalar_ops_impl!(Pnt4, x, y, z, w);
approx_eq_impl!(Pnt4, x, y, z, w); approx_eq_impl!(Pnt4, x, y, z, w);
from_iterator_impl!(Pnt4, iterator, iterator, iterator, iterator); from_iterator_impl!(Pnt4, iterator, iterator, iterator, iterator);
bounded_impl!(Pnt4, x, y, z, w); bounded_impl!(Pnt4, x, y, z, w);
@ -250,7 +245,6 @@ pnt_sub_impl!(Pnt5, Vec5);
neg_impl!(Pnt5, x, y, z, w, a); neg_impl!(Pnt5, x, y, z, w, a);
pnt_add_vec_impl!(Pnt5, Vec5, x, y, z, w, a); pnt_add_vec_impl!(Pnt5, Vec5, x, y, z, w, a);
pnt_sub_vec_impl!(Pnt5, Vec5, x, y, z, w, a); pnt_sub_vec_impl!(Pnt5, Vec5, x, y, z, w, a);
scalar_ops_impl!(Pnt5, x, y, z, w, a);
approx_eq_impl!(Pnt5, x, y, z, w, a); approx_eq_impl!(Pnt5, x, y, z, w, a);
from_iterator_impl!(Pnt5, iterator, iterator, iterator, iterator, iterator); from_iterator_impl!(Pnt5, iterator, iterator, iterator, iterator, iterator);
bounded_impl!(Pnt5, x, y, z, w, a); bounded_impl!(Pnt5, x, y, z, w, a);
@ -301,7 +295,6 @@ pnt_sub_impl!(Pnt6, Vec6);
neg_impl!(Pnt6, x, y, z, w, a, b); neg_impl!(Pnt6, x, y, z, w, a, b);
pnt_add_vec_impl!(Pnt6, Vec6, x, y, z, w, a, b); pnt_add_vec_impl!(Pnt6, Vec6, x, y, z, w, a, b);
pnt_sub_vec_impl!(Pnt6, Vec6, x, y, z, w, a, b); pnt_sub_vec_impl!(Pnt6, Vec6, x, y, z, w, a, b);
scalar_ops_impl!(Pnt6, x, y, z, w, a, b);
approx_eq_impl!(Pnt6, x, y, z, w, a, b); approx_eq_impl!(Pnt6, x, y, z, w, a, b);
from_iterator_impl!(Pnt6, iterator, iterator, iterator, iterator, iterator, iterator); from_iterator_impl!(Pnt6, iterator, iterator, iterator, iterator, iterator, iterator);
bounded_impl!(Pnt6, x, y, z, w, a, b); bounded_impl!(Pnt6, x, y, z, w, a, b);

View File

@ -9,8 +9,7 @@ use std::iter::{FromIterator, IntoIterator};
use rand::{Rand, Rng}; use rand::{Rand, Rng};
use num::{Zero, One}; use num::{Zero, One};
use structs::{Vec3, Pnt3, Rot3, Mat3}; use structs::{Vec3, Pnt3, Rot3, Mat3};
use traits::operations::{ApproxEq, Inv, POrd, POrdering, Axpy, ScalarAdd, ScalarSub, ScalarMul, use traits::operations::{ApproxEq, Inv, POrd, POrdering, Axpy};
ScalarDiv};
use traits::structure::{Cast, Indexable, Iterable, IterableMut, Dim, Shape, BaseFloat, BaseNum, use traits::structure::{Cast, Indexable, Iterable, IterableMut, Dim, Shape, BaseFloat, BaseNum,
Bounded}; Bounded};
use traits::geometry::{Norm, Rotation, Rotate, Transform}; use traits::geometry::{Norm, Rotation, Rotate, Transform};
@ -506,7 +505,6 @@ scalar_sub_impl!(Quat, w, i, j, k);
scalar_mul_impl!(Quat, w, i, j, k); scalar_mul_impl!(Quat, w, i, j, k);
scalar_div_impl!(Quat, w, i, j, k); scalar_div_impl!(Quat, w, i, j, k);
neg_impl!(Quat, w, i, j, k); neg_impl!(Quat, w, i, j, k);
scalar_ops_impl!(Quat, w, i, j, k);
zero_one_impl!(Quat, w, i, j, k); zero_one_impl!(Quat, w, i, j, k);
approx_eq_impl!(Quat, w, i, j, k); approx_eq_impl!(Quat, w, i, j, k);
from_iterator_impl!(Quat, iterator, iterator, iterator, iterator); from_iterator_impl!(Quat, iterator, iterator, iterator, iterator);

View File

@ -9,8 +9,7 @@ use std::slice::{Iter, IterMut};
use std::iter::{Iterator, FromIterator, IntoIterator}; use std::iter::{Iterator, FromIterator, IntoIterator};
use rand::{Rand, Rng}; use rand::{Rand, Rng};
use num::{Zero, One}; use num::{Zero, One};
use traits::operations::{ApproxEq, POrd, POrdering, Axpy, ScalarAdd, ScalarSub, ScalarMul, use traits::operations::{ApproxEq, POrd, POrdering, Axpy, Absolute};
ScalarDiv, Absolute};
use traits::geometry::{Transform, Rotate, FromHomogeneous, ToHomogeneous, Dot, Norm, use traits::geometry::{Transform, Rotate, FromHomogeneous, ToHomogeneous, Dot, Norm,
Translation, Translate}; Translation, Translate};
use traits::structure::{Basis, Cast, Dim, Indexable, Iterable, IterableMut, Shape, NumVec, use traits::structure::{Basis, Cast, Dim, Indexable, Iterable, IterableMut, Shape, NumVec,
@ -70,7 +69,6 @@ scalar_mul_impl!(Vec1, x);
scalar_div_impl!(Vec1, x); scalar_div_impl!(Vec1, x);
neg_impl!(Vec1, x); neg_impl!(Vec1, x);
dot_impl!(Vec1, x); dot_impl!(Vec1, x);
scalar_ops_impl!(Vec1, x);
translation_impl!(Vec1); translation_impl!(Vec1);
norm_impl!(Vec1, x); norm_impl!(Vec1, x);
approx_eq_impl!(Vec1, x); approx_eq_impl!(Vec1, x);
@ -124,7 +122,6 @@ scalar_mul_impl!(Vec2, x, y);
scalar_div_impl!(Vec2, x, y); scalar_div_impl!(Vec2, x, y);
neg_impl!(Vec2, x, y); neg_impl!(Vec2, x, y);
dot_impl!(Vec2, x, y); dot_impl!(Vec2, x, y);
scalar_ops_impl!(Vec2, x, y);
translation_impl!(Vec2); translation_impl!(Vec2);
norm_impl!(Vec2, x, y); norm_impl!(Vec2, x, y);
approx_eq_impl!(Vec2, x, y); approx_eq_impl!(Vec2, x, y);
@ -180,7 +177,6 @@ scalar_mul_impl!(Vec3, x, y, z);
scalar_div_impl!(Vec3, x, y, z); scalar_div_impl!(Vec3, x, y, z);
neg_impl!(Vec3, x, y, z); neg_impl!(Vec3, x, y, z);
dot_impl!(Vec3, x, y, z); dot_impl!(Vec3, x, y, z);
scalar_ops_impl!(Vec3, x, y, z);
translation_impl!(Vec3); translation_impl!(Vec3);
norm_impl!(Vec3, x, y ,z); norm_impl!(Vec3, x, y ,z);
approx_eq_impl!(Vec3, x, y, z); approx_eq_impl!(Vec3, x, y, z);
@ -239,7 +235,6 @@ scalar_mul_impl!(Vec4, x, y, z, w);
scalar_div_impl!(Vec4, x, y, z, w); scalar_div_impl!(Vec4, x, y, z, w);
neg_impl!(Vec4, x, y, z, w); neg_impl!(Vec4, x, y, z, w);
dot_impl!(Vec4, x, y, z, w); dot_impl!(Vec4, x, y, z, w);
scalar_ops_impl!(Vec4, x, y, z, w);
translation_impl!(Vec4); translation_impl!(Vec4);
norm_impl!(Vec4, x, y, z, w); norm_impl!(Vec4, x, y, z, w);
approx_eq_impl!(Vec4, x, y, z, w); approx_eq_impl!(Vec4, x, y, z, w);
@ -299,7 +294,6 @@ scalar_mul_impl!(Vec5, x, y, z, w, a);
scalar_div_impl!(Vec5, x, y, z, w, a); scalar_div_impl!(Vec5, x, y, z, w, a);
neg_impl!(Vec5, x, y, z, w, a); neg_impl!(Vec5, x, y, z, w, a);
dot_impl!(Vec5, x, y, z, w, a); dot_impl!(Vec5, x, y, z, w, a);
scalar_ops_impl!(Vec5, x, y, z, w, a);
translation_impl!(Vec5); translation_impl!(Vec5);
norm_impl!(Vec5, x, y, z, w, a); norm_impl!(Vec5, x, y, z, w, a);
approx_eq_impl!(Vec5, x, y, z, w, a); approx_eq_impl!(Vec5, x, y, z, w, a);
@ -361,7 +355,6 @@ scalar_mul_impl!(Vec6, x, y, z, w, a, b);
scalar_div_impl!(Vec6, x, y, z, w, a, b); scalar_div_impl!(Vec6, x, y, z, w, a, b);
neg_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); dot_impl!(Vec6, x, y, z, w, a, b);
scalar_ops_impl!(Vec6, x, y, z, w, a, b);
translation_impl!(Vec6); translation_impl!(Vec6);
norm_impl!(Vec6, x, y, z, w, a, b); norm_impl!(Vec6, x, y, z, w, a, b);
approx_eq_impl!(Vec6, x, y, z, w, a, b); approx_eq_impl!(Vec6, x, y, z, w, a, b);

View File

@ -504,38 +504,6 @@ macro_rules! dot_impl(
) )
); );
macro_rules! scalar_ops_impl(
($t: ident, $($compN: ident),+) => (
impl<N: Copy + Mul<N, Output = N>> ScalarMul<N> for $t<N> {
#[inline]
fn mul_s(&self, other: &N) -> $t<N> {
$t::new($(self.$compN * *other),+)
}
}
impl<N: Copy + Div<N, Output = N>> ScalarDiv<N> for $t<N> {
#[inline]
fn div_s(&self, other: &N) -> $t<N> {
$t::new($(self.$compN / *other),+)
}
}
impl<N: Copy + Add<N, Output = N>> ScalarAdd<N> for $t<N> {
#[inline]
fn add_s(&self, other: &N) -> $t<N> {
$t::new($(self.$compN + *other),+)
}
}
impl<N: Copy + Sub<N, Output = N>> ScalarSub<N> for $t<N> {
#[inline]
fn sub_s(&self, other: &N) -> $t<N> {
$t::new($(self.$compN - *other),+)
}
}
)
);
macro_rules! translation_impl( macro_rules! translation_impl(
($t: ident) => ( ($t: ident) => (
impl<N: Copy + Add<N, Output = N> + Neg<Output = N>> Translation<$t<N>> for $t<N> { impl<N: Copy + Add<N, Output = N> + Neg<Output = N>> Translation<$t<N>> for $t<N> {

View File

@ -8,8 +8,8 @@ pub use traits::structure::{FloatVec, FloatPnt, Basis, Cast, Col, Dim, Indexable
IterableMut, Mat, SquareMat, Row, NumVec, NumPnt, PntAsVec, ColSlice, IterableMut, Mat, SquareMat, Row, NumVec, NumPnt, PntAsVec, ColSlice,
RowSlice, Diag, Eye, Shape, BaseFloat, BaseNum, Bounded}; RowSlice, Diag, Eye, Shape, BaseFloat, BaseNum, Bounded};
pub use traits::operations::{Absolute, ApproxEq, Axpy, Cov, Det, Inv, LMul, Mean, Outer, POrd, pub use traits::operations::{Absolute, ApproxEq, Axpy, Cov, Det, Inv, Mean, Outer, POrd, Transpose,
RMul, ScalarAdd, ScalarSub, ScalarMul, ScalarDiv, Transpose, EigenQR}; EigenQR};
pub use traits::operations::POrdering; pub use traits::operations::POrdering;
pub mod geometry; pub mod geometry;

View File

@ -1,7 +1,6 @@
//! Low level operations on vectors and matrices. //! Low level operations on vectors and matrices.
use num::{Float, Signed}; use num::{Float, Signed};
use std::ops::Mul;
use std::cmp::Ordering; use std::cmp::Ordering;
use traits::structure::SquareMat; use traits::structure::SquareMat;
@ -337,68 +336,6 @@ pub trait EigenQR<N, V>: SquareMat<N, V> {
fn eigen_qr(&self, eps: &N, niter: usize) -> (Self, V); fn eigen_qr(&self, eps: &N, niter: usize) -> (Self, V);
} }
// XXX: those two traits should not exist since there is generalized operator overloading of Add
// and Sub.
// However, using the same trait multiple time as a trait bound (ex: impl<T: Add<N, V> + Add<V, V>)
// does not work properly, mainly because the way we are doing generalized operator overloading is
// verry hacky.
//
// Hopefully, this will be fixed on a future version of the language!
/// Trait of objects having a right multiplication with another element.
pub trait RMul<V> {
/// Computes `self * v`
fn rmul(&self, v: &V) -> V;
}
impl<M: Copy + Mul<T, Output = T>, T: Copy> RMul<T> for M {
fn rmul(&self, v: &T) -> T {
*self * *v
}
}
/// Trait of objects having a left multiplication with another element.
pub trait LMul<V> {
/// Computes `v * self`
fn lmul(&self, &V) -> V;
}
impl<T: Copy + Mul<M, Output = T>, M: Copy> LMul<T> for M {
fn lmul(&self, v: &T) -> T {
*v * *self
}
}
// XXX: those traits should not exist since there is generalized operator overloading of Add and
// Sub.
// However, using the same trait multiple time as a trait bound (ex: impl<T: Add<N, V> + Add<V, V>)
// does not work properly, mainly because the way we are doing generalized operator overloading is
// verry hacky.
//
// Hopefully, this will be fixed on a future version of the language!
/// Trait of objects having an addition with a scalar.
pub trait ScalarAdd<N> {
/// Gets the result of `self + n`.
fn add_s(&self, n: &N) -> Self;
}
/// Trait of objects having a subtraction with a scalar.
pub trait ScalarSub<N> {
/// Gets the result of `self - n`.
fn sub_s(&self, n: &N) -> Self;
}
/// Trait of objects having a multiplication with a scalar.
pub trait ScalarMul<N> {
/// Gets the result of `self * n`.
fn mul_s(&self, n: &N) -> Self;
}
/// Trait of objects having a division by a scalar.
pub trait ScalarDiv<N> {
/// Gets the result of `self / n`.
fn div_s(&self, n: &N) -> Self;
}
/// Trait of objects implementing the `y = ax + y` operation. /// Trait of objects implementing the `y = ax + y` operation.
pub trait Axpy<N> { pub trait Axpy<N> {
/// Adds $$a * x$$ to `self`. /// Adds $$a * x$$ to `self`.

View File

@ -4,7 +4,7 @@ use std::{f32, f64, i8, i16, i32, i64, u8, u16, u32, u64, isize, usize};
use std::slice::{Iter, IterMut}; use std::slice::{Iter, IterMut};
use std::ops::{Add, Sub, Mul, Div, Rem, Index, IndexMut}; use std::ops::{Add, Sub, Mul, Div, Rem, Index, IndexMut};
use num::{Float, Zero, One}; use num::{Float, Zero, One};
use traits::operations::{RMul, LMul, Axpy, Transpose, Inv, Absolute}; use traits::operations::{Axpy, Transpose, Inv, Absolute};
use traits::geometry::{Dot, Norm, Orig}; use traits::geometry::{Dot, Norm, Orig};
/// Basic integral numeric trait. /// Basic integral numeric trait.
@ -59,11 +59,12 @@ pub trait Cast<T> {
/// Trait of matrices. /// Trait of matrices.
/// ///
/// A matrix has rows and columns and are able to multiply them. /// A matrix has rows and columns and are able to multiply them.
pub trait Mat<N, R, C>: Row<R> + Col<C> + RMul<R> + LMul<C> + Index<(usize, usize), Output = N> { } pub trait Mat<N, R, C: Mul<Self>>: Row<R> + Col<C> + Mul<R> + Index<(usize, usize), Output = N> { }
impl<N, M, R, C> Mat<N, R, C> for M impl<N, M, R, C> Mat<N, R, C> for M
where M: Row<R> + Col<C> + RMul<R> + LMul<C> + Index<(usize, usize), Output = N> { where M: Row<R> + Col<C> + Mul<R> + Index<(usize, usize), Output = N>,
} C: Mul<M>,
{}
/// Trait implemented by square matrices. /// Trait implemented by square matrices.
pub trait SquareMat<N, V>: Mat<N, V, V> + pub trait SquareMat<N, V>: Mat<N, V, V> +