diff --git a/src/lib.rs b/src/lib.rs index 5a052a29..dac3a565 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -114,8 +114,8 @@ pub use traits::{PartialLess, PartialEqual, PartialGreater, NotComparable}; pub use traits::{ Absolute, AbsoluteRotate, - AnyPnt, - AnyVec, + NumPnt, + NumVec, ApproxEq, Axpy, Basis, diff --git a/src/structs/pnt.rs b/src/structs/pnt.rs index 3fd79c7f..c303b3fd 100644 --- a/src/structs/pnt.rs +++ b/src/structs/pnt.rs @@ -8,7 +8,8 @@ use std::slice::{Items, MutItems}; use std::iter::{Iterator, FromIterator}; use traits::operations::{ApproxEq, POrd, POrdering, PartialLess, PartialEqual, PartialGreater, NotComparable, Axpy}; -use traits::structure::{Cast, Dim, Indexable, Iterable, IterableMut, PntAsVec, Shape}; +use traits::structure::{Cast, Dim, Indexable, Iterable, IterableMut, PntAsVec, Shape, + NumPnt, FloatPnt}; use traits::geometry::{Orig, FromHomogeneous, ToHomogeneous}; use structs::vec::{Vec1, Vec2, Vec3, Vec4, Vec5, Vec6}; @@ -120,6 +121,7 @@ iterable_impl!(Pnt1, 1) iterable_mut_impl!(Pnt1, 1) pnt_to_homogeneous_impl!(Pnt1, Pnt2, y, x) pnt_from_homogeneous_impl!(Pnt1, Pnt2, y, x) +num_float_pnt_impl!(Pnt1, Vec1, Pnt1MulRhs, Pnt1DivRhs) /// Point of dimension 2. #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Show)] @@ -212,6 +214,7 @@ iterable_impl!(Pnt2, 2) iterable_mut_impl!(Pnt2, 2) pnt_to_homogeneous_impl!(Pnt2, Pnt3, z, x, y) pnt_from_homogeneous_impl!(Pnt2, Pnt3, z, x, y) +num_float_pnt_impl!(Pnt2, Vec2, Pnt2MulRhs, Pnt2DivRhs) /// Point of dimension 3. #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Show)] @@ -306,6 +309,7 @@ iterable_impl!(Pnt3, 3) iterable_mut_impl!(Pnt3, 3) pnt_to_homogeneous_impl!(Pnt3, Pnt4, w, x, y, z) pnt_from_homogeneous_impl!(Pnt3, Pnt4, w, x, y, z) +num_float_pnt_impl!(Pnt3, Vec3, Pnt3MulRhs, Pnt3DivRhs) /// Point of dimension 4. #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Show)] @@ -402,6 +406,7 @@ iterable_impl!(Pnt4, 4) iterable_mut_impl!(Pnt4, 4) pnt_to_homogeneous_impl!(Pnt4, Pnt5, a, x, y, z, w) pnt_from_homogeneous_impl!(Pnt4, Pnt5, a, x, y, z, w) +num_float_pnt_impl!(Pnt4, Vec4, Pnt4MulRhs, Pnt4DivRhs) /// Point of dimension 5. #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Show)] @@ -500,6 +505,7 @@ iterable_impl!(Pnt5, 5) iterable_mut_impl!(Pnt5, 5) pnt_to_homogeneous_impl!(Pnt5, Pnt6, b, x, y, z, w, a) pnt_from_homogeneous_impl!(Pnt5, Pnt6, b, x, y, z, w, a) +num_float_pnt_impl!(Pnt5, Vec5, Pnt5MulRhs, Pnt5DivRhs) /// Point of dimension 6. #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Show)] @@ -598,3 +604,4 @@ bounded_impl!(Pnt6, x, y, z, w, a, b) axpy_impl!(Pnt6, x, y, z, w, a, b) iterable_impl!(Pnt6, 6) iterable_mut_impl!(Pnt6, 6) +num_float_pnt_impl!(Pnt6, Vec6, Pnt6MulRhs, Pnt6DivRhs) diff --git a/src/structs/pnt_macros.rs b/src/structs/pnt_macros.rs index bc7a0b4c..69837575 100644 --- a/src/structs/pnt_macros.rs +++ b/src/structs/pnt_macros.rs @@ -128,3 +128,15 @@ macro_rules! pnt_from_homogeneous_impl( } ) ) + +macro_rules! num_float_pnt_impl( + ($t: ident, $tv: ident $(,$trhs: ident)*) => ( + impl NumPnt> for $t + where N: Num $(+ $trhs>)* { + } + + impl FloatPnt> for $t + where N: ApproxEq + Float $(+ $trhs>)* { + } + ) +) diff --git a/src/structs/spec/vec.rs b/src/structs/spec/vec.rs index 9e084eca..fa2c9220 100644 --- a/src/structs/spec/vec.rs +++ b/src/structs/spec/vec.rs @@ -91,7 +91,7 @@ impl> Basis for Vec2 { } } -impl Basis for Vec3 { +impl Basis for Vec3 { #[inline(always)] fn canonical_basis(f: |Vec3| -> bool) { if !f(Vec3::new(One::one(), Zero::zero(), Zero::zero())) { return }; diff --git a/src/structs/vec.rs b/src/structs/vec.rs index e3c2d69f..387ea26c 100644 --- a/src/structs/vec.rs +++ b/src/structs/vec.rs @@ -10,7 +10,8 @@ use traits::operations::{ApproxEq, POrd, POrdering, PartialLess, PartialEqual, PartialGreater, NotComparable, Axpy}; use traits::geometry::{Transform, Rotate, FromHomogeneous, ToHomogeneous, Dot, Norm, Translation, Translate}; -use traits::structure::{Basis, Cast, Dim, Indexable, Iterable, IterableMut, VecAsPnt, Shape}; +use traits::structure::{Basis, Cast, Dim, Indexable, Iterable, IterableMut, VecAsPnt, Shape, + NumVec, FloatVec}; use structs::pnt::{Pnt1, Pnt2, Pnt3, Pnt4, Pnt5, Pnt6}; @@ -131,6 +132,7 @@ rotate_impl!(Vec1) rotate_impl!(Pnt1) transform_impl!(Vec1, Pnt1) vec_as_pnt_impl!(Vec1, Pnt1, x) +num_float_vec_impl!(Vec1, Vec1MulRhs, Vec1DivRhs) /// Vector of dimension 2. #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)] @@ -233,6 +235,7 @@ rotate_impl!(Vec2) rotate_impl!(Pnt2) transform_impl!(Vec2, Pnt2) vec_as_pnt_impl!(Vec2, Pnt2, x, y) +num_float_vec_impl!(Vec2, Vec2MulRhs, Vec2DivRhs) /// Vector of dimension 3. #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)] @@ -340,6 +343,7 @@ rotate_impl!(Vec3) rotate_impl!(Pnt3) transform_impl!(Vec3, Pnt3) vec_as_pnt_impl!(Vec3, Pnt3, x, y, z) +num_float_vec_impl!(Vec3, Vec3MulRhs, Vec3DivRhs) /// Vector of dimension 4. @@ -447,6 +451,7 @@ rotate_impl!(Vec4) rotate_impl!(Pnt4) transform_impl!(Vec4, Pnt4) vec_as_pnt_impl!(Vec4, Pnt4, x, y, z, w) +num_float_vec_impl!(Vec4, Vec4MulRhs, Vec4DivRhs) /// Vector of dimension 5. #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)] @@ -555,6 +560,7 @@ rotate_impl!(Vec5) rotate_impl!(Pnt5) transform_impl!(Vec5, Pnt5) vec_as_pnt_impl!(Vec5, Pnt5, x, y, z, w, a) +num_float_vec_impl!(Vec5, Vec5MulRhs, Vec5DivRhs) /// Vector of dimension 6. #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)] @@ -663,3 +669,4 @@ rotate_impl!(Vec6) rotate_impl!(Pnt6) transform_impl!(Vec6, Pnt6) vec_as_pnt_impl!(Vec6, Pnt6, x, y, z, w, a, b) +num_float_vec_impl!(Vec6, Vec6MulRhs, Vec6DivRhs) diff --git a/src/structs/vec_macros.rs b/src/structs/vec_macros.rs index ec22533d..e35fd59d 100644 --- a/src/structs/vec_macros.rs +++ b/src/structs/vec_macros.rs @@ -719,3 +719,15 @@ macro_rules! vec_as_pnt_impl( } ) ) + +macro_rules! num_float_vec_impl( + ($t: ident $(,$trhs: ident)*) => ( + impl NumVec for $t + where N: Num $(+ $trhs>)* { + } + + impl FloatVec for $t + where N: ApproxEq + Float $(+ $trhs>)* { + } + ) +) diff --git a/src/traits/mod.rs b/src/traits/mod.rs index 74c3910c..5f71ac9d 100644 --- a/src/traits/mod.rs +++ b/src/traits/mod.rs @@ -5,7 +5,7 @@ pub use self::geometry::{AbsoluteRotate, Cross, CrossMatrix, Dot, FromHomogeneou Transform, Transformation, Translate, Translation, UniformSphereSample}; pub use self::structure::{FloatVec, FloatPnt, Basis, Cast, Col, Dim, Indexable, Iterable, - IterableMut, Mat, Row, AnyVec, AnyPnt, PntAsVec, VecAsPnt, ColSlice, + IterableMut, Mat, Row, NumVec, NumPnt, PntAsVec, VecAsPnt, ColSlice, RowSlice, Diag, Eye, Shape}; pub use self::operations::{Absolute, ApproxEq, Axpy, Cov, Det, Inv, LMul, Mean, Outer, POrd, diff --git a/src/traits/structure.rs b/src/traits/structure.rs index 599bb7ae..7c8a16da 100644 --- a/src/traits/structure.rs +++ b/src/traits/structure.rs @@ -157,21 +157,14 @@ pub trait VecAsPnt

{ } /// Trait grouping most common operations on vectors. -pub trait AnyVec: Dim + Sub + Add + Neg + Zero + PartialEq + - Mul + Div + Dot + Axpy + Basis + Index { +pub trait NumVec: Dim + Sub + Add + Neg + Zero + PartialEq + + Mul + Div + Dot + Axpy + Index { } /// Trait of vector with components implementing the `Float` trait. -pub trait FloatVec: AnyVec + Norm { +pub trait FloatVec: NumVec + Norm + Basis { } -impl AnyVec for V - where V: Dim + Sub + Add + Neg + Zero + PartialEq + Mul + Div + - Dot + Axpy + Basis + Index { -} - -impl + Norm> FloatVec for V { } - /* * Pnt related traits. */ @@ -192,13 +185,13 @@ pub trait PntAsVec { /// Trait grouping most common operations on points. // XXX: the vector space element `V` should be an associated type. Though this would prevent V from // having bounds (they are not supported yet). So, for now, we will just use a type parameter. -pub trait AnyPnt: +pub trait NumPnt: PntAsVec + Dim + Sub + Orig + Neg + PartialEq + Mul + Div + Add + Axpy + Index { // FIXME: + Sub } /// Trait of points with components implementing the `Float` trait. -pub trait FloatPnt>: AnyPnt { +pub trait FloatPnt>: NumPnt { /// Computes the square distance between two points. #[inline] fn sqdist(a: &Self, b: &Self) -> N { @@ -211,14 +204,3 @@ pub trait FloatPnt>: AnyPnt { Norm::norm(&(*a - *b)) } } - -impl AnyPnt for P - where P: PntAsVec + Dim + Sub + Add + Orig + Neg

+ PartialEq + Mul + - Div + Axpy + Index { -} - -impl FloatPnt for P - where N: Float, - V: Norm, - P: AnyPnt { -}