Rename: Any{Vec,Pnt} to Num{Vec,Pnt} + do not impl them automatically.

Instead, implement them manually. This clarifies error messages for the users.
This commit is contained in:
Sébastien Crozet 2014-10-26 15:04:47 +01:00
parent 628aac0961
commit 6a194b2b62
8 changed files with 49 additions and 29 deletions

View File

@ -114,8 +114,8 @@ pub use traits::{PartialLess, PartialEqual, PartialGreater, NotComparable};
pub use traits::{ pub use traits::{
Absolute, Absolute,
AbsoluteRotate, AbsoluteRotate,
AnyPnt, NumPnt,
AnyVec, NumVec,
ApproxEq, ApproxEq,
Axpy, Axpy,
Basis, Basis,

View File

@ -8,7 +8,8 @@ use std::slice::{Items, MutItems};
use std::iter::{Iterator, FromIterator}; use std::iter::{Iterator, FromIterator};
use traits::operations::{ApproxEq, POrd, POrdering, PartialLess, PartialEqual, use traits::operations::{ApproxEq, POrd, POrdering, PartialLess, PartialEqual,
PartialGreater, NotComparable, Axpy}; 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 traits::geometry::{Orig, FromHomogeneous, ToHomogeneous};
use structs::vec::{Vec1, Vec2, Vec3, Vec4, Vec5, Vec6}; use structs::vec::{Vec1, Vec2, Vec3, Vec4, Vec5, Vec6};
@ -120,6 +121,7 @@ iterable_impl!(Pnt1, 1)
iterable_mut_impl!(Pnt1, 1) iterable_mut_impl!(Pnt1, 1)
pnt_to_homogeneous_impl!(Pnt1, Pnt2, y, x) pnt_to_homogeneous_impl!(Pnt1, Pnt2, y, x)
pnt_from_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. /// Point of dimension 2.
#[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Show)] #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Show)]
@ -212,6 +214,7 @@ iterable_impl!(Pnt2, 2)
iterable_mut_impl!(Pnt2, 2) iterable_mut_impl!(Pnt2, 2)
pnt_to_homogeneous_impl!(Pnt2, Pnt3, z, x, y) pnt_to_homogeneous_impl!(Pnt2, Pnt3, z, x, y)
pnt_from_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. /// Point of dimension 3.
#[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Show)] #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Show)]
@ -306,6 +309,7 @@ iterable_impl!(Pnt3, 3)
iterable_mut_impl!(Pnt3, 3) iterable_mut_impl!(Pnt3, 3)
pnt_to_homogeneous_impl!(Pnt3, Pnt4, w, x, y, z) pnt_to_homogeneous_impl!(Pnt3, Pnt4, w, x, y, z)
pnt_from_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. /// Point of dimension 4.
#[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Show)] #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Show)]
@ -402,6 +406,7 @@ iterable_impl!(Pnt4, 4)
iterable_mut_impl!(Pnt4, 4) iterable_mut_impl!(Pnt4, 4)
pnt_to_homogeneous_impl!(Pnt4, Pnt5, a, x, y, z, w) pnt_to_homogeneous_impl!(Pnt4, Pnt5, a, x, y, z, w)
pnt_from_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. /// Point of dimension 5.
#[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Show)] #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Show)]
@ -500,6 +505,7 @@ iterable_impl!(Pnt5, 5)
iterable_mut_impl!(Pnt5, 5) iterable_mut_impl!(Pnt5, 5)
pnt_to_homogeneous_impl!(Pnt5, Pnt6, b, x, y, z, w, a) pnt_to_homogeneous_impl!(Pnt5, Pnt6, b, x, y, z, w, a)
pnt_from_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. /// Point of dimension 6.
#[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Show)] #[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) axpy_impl!(Pnt6, x, y, z, w, a, b)
iterable_impl!(Pnt6, 6) iterable_impl!(Pnt6, 6)
iterable_mut_impl!(Pnt6, 6) iterable_mut_impl!(Pnt6, 6)
num_float_pnt_impl!(Pnt6, Vec6, Pnt6MulRhs, Pnt6DivRhs)

View File

@ -128,3 +128,15 @@ macro_rules! pnt_from_homogeneous_impl(
} }
) )
) )
macro_rules! num_float_pnt_impl(
($t: ident, $tv: ident $(,$trhs: ident)*) => (
impl<N> NumPnt<N, $tv<N>> for $t<N>
where N: Num $(+ $trhs<N, $t<N>>)* {
}
impl<N> FloatPnt<N, $tv<N>> for $t<N>
where N: ApproxEq<N> + Float $(+ $trhs<N, $t<N>>)* {
}
)
)

View File

@ -91,7 +91,7 @@ impl<N: Clone + One + Zero + Neg<N>> Basis for Vec2<N> {
} }
} }
impl<N: Clone + PartialOrd + Float + Signed> Basis for Vec3<N> { impl<N: Float> Basis for Vec3<N> {
#[inline(always)] #[inline(always)]
fn canonical_basis(f: |Vec3<N>| -> bool) { fn canonical_basis(f: |Vec3<N>| -> bool) {
if !f(Vec3::new(One::one(), Zero::zero(), Zero::zero())) { return }; if !f(Vec3::new(One::one(), Zero::zero(), Zero::zero())) { return };

View File

@ -10,7 +10,8 @@ use traits::operations::{ApproxEq, POrd, POrdering, PartialLess, PartialEqual,
PartialGreater, NotComparable, Axpy}; PartialGreater, NotComparable, Axpy};
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, VecAsPnt, Shape}; use traits::structure::{Basis, Cast, Dim, Indexable, Iterable, IterableMut, VecAsPnt, Shape,
NumVec, FloatVec};
use structs::pnt::{Pnt1, Pnt2, Pnt3, Pnt4, Pnt5, Pnt6}; use structs::pnt::{Pnt1, Pnt2, Pnt3, Pnt4, Pnt5, Pnt6};
@ -131,6 +132,7 @@ rotate_impl!(Vec1)
rotate_impl!(Pnt1) rotate_impl!(Pnt1)
transform_impl!(Vec1, Pnt1) transform_impl!(Vec1, Pnt1)
vec_as_pnt_impl!(Vec1, Pnt1, x) vec_as_pnt_impl!(Vec1, Pnt1, x)
num_float_vec_impl!(Vec1, Vec1MulRhs, Vec1DivRhs)
/// Vector of dimension 2. /// Vector of dimension 2.
#[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)] #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)]
@ -233,6 +235,7 @@ rotate_impl!(Vec2)
rotate_impl!(Pnt2) rotate_impl!(Pnt2)
transform_impl!(Vec2, Pnt2) transform_impl!(Vec2, Pnt2)
vec_as_pnt_impl!(Vec2, Pnt2, x, y) vec_as_pnt_impl!(Vec2, Pnt2, x, y)
num_float_vec_impl!(Vec2, Vec2MulRhs, Vec2DivRhs)
/// Vector of dimension 3. /// Vector of dimension 3.
#[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)] #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)]
@ -340,6 +343,7 @@ rotate_impl!(Vec3)
rotate_impl!(Pnt3) rotate_impl!(Pnt3)
transform_impl!(Vec3, Pnt3) transform_impl!(Vec3, Pnt3)
vec_as_pnt_impl!(Vec3, Pnt3, x, y, z) vec_as_pnt_impl!(Vec3, Pnt3, x, y, z)
num_float_vec_impl!(Vec3, Vec3MulRhs, Vec3DivRhs)
/// Vector of dimension 4. /// Vector of dimension 4.
@ -447,6 +451,7 @@ rotate_impl!(Vec4)
rotate_impl!(Pnt4) rotate_impl!(Pnt4)
transform_impl!(Vec4, Pnt4) transform_impl!(Vec4, Pnt4)
vec_as_pnt_impl!(Vec4, Pnt4, x, y, z, w) vec_as_pnt_impl!(Vec4, Pnt4, x, y, z, w)
num_float_vec_impl!(Vec4, Vec4MulRhs, Vec4DivRhs)
/// Vector of dimension 5. /// Vector of dimension 5.
#[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)] #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)]
@ -555,6 +560,7 @@ rotate_impl!(Vec5)
rotate_impl!(Pnt5) rotate_impl!(Pnt5)
transform_impl!(Vec5, Pnt5) transform_impl!(Vec5, Pnt5)
vec_as_pnt_impl!(Vec5, Pnt5, x, y, z, w, a) vec_as_pnt_impl!(Vec5, Pnt5, x, y, z, w, a)
num_float_vec_impl!(Vec5, Vec5MulRhs, Vec5DivRhs)
/// Vector of dimension 6. /// Vector of dimension 6.
#[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)] #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)]
@ -663,3 +669,4 @@ rotate_impl!(Vec6)
rotate_impl!(Pnt6) rotate_impl!(Pnt6)
transform_impl!(Vec6, Pnt6) transform_impl!(Vec6, Pnt6)
vec_as_pnt_impl!(Vec6, Pnt6, x, y, z, w, a, b) vec_as_pnt_impl!(Vec6, Pnt6, x, y, z, w, a, b)
num_float_vec_impl!(Vec6, Vec6MulRhs, Vec6DivRhs)

View File

@ -719,3 +719,15 @@ macro_rules! vec_as_pnt_impl(
} }
) )
) )
macro_rules! num_float_vec_impl(
($t: ident $(,$trhs: ident)*) => (
impl<N> NumVec<N> for $t<N>
where N: Num $(+ $trhs<N, $t<N>>)* {
}
impl<N> FloatVec<N> for $t<N>
where N: ApproxEq<N> + Float $(+ $trhs<N, $t<N>>)* {
}
)
)

View File

@ -5,7 +5,7 @@ pub use self::geometry::{AbsoluteRotate, Cross, CrossMatrix, Dot, FromHomogeneou
Transform, Transformation, Translate, Translation, UniformSphereSample}; Transform, Transformation, Translate, Translation, UniformSphereSample};
pub use self::structure::{FloatVec, FloatPnt, Basis, Cast, Col, Dim, Indexable, Iterable, 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}; RowSlice, Diag, Eye, Shape};
pub use self::operations::{Absolute, ApproxEq, Axpy, Cov, Det, Inv, LMul, Mean, Outer, POrd, pub use self::operations::{Absolute, ApproxEq, Axpy, Cov, Det, Inv, LMul, Mean, Outer, POrd,

View File

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