Add `Orderable` implementation for vectors.

The `min`, `max` and `clamp` methods are component-wise.
This commit is contained in:
Sébastien Crozet 2013-08-04 11:06:23 +02:00
parent 9af1cac45d
commit 53a5dbb6e3
3 changed files with 40 additions and 0 deletions

View File

@ -285,3 +285,15 @@ fn test_ord_vec3()
assert!(!(Vec3::new(0.0, 3.0, 0.0) <= Vec3::new(1.0, 2.0, 1.0)));
assert!(!(Vec3::new(0.0, 3.0, 0.0) >= Vec3::new(1.0, 2.0, 1.0)));
}
#[test]
fn test_min_max_vec3()
{
assert_eq!(Vec3::new(1, 2, 3).max(&Vec3::new(3, 2, 1)), Vec3::new(3, 2, 3));
assert_eq!(Vec3::new(1, 2, 3).min(&Vec3::new(3, 2, 1)), Vec3::new(1, 2, 1));
assert_eq!(
Vec3::new(0, 2, 4).clamp(
&Vec3::new(1, 1, 1), &Vec3::new(3, 3, 3)
), Vec3::new(1, 2, 3)
);
}

View File

@ -40,6 +40,7 @@ pub struct Vec1<N>
new_impl!(Vec1, x)
ord_impl!(Vec1, x)
orderable_impl!(Vec1, x)
vec_axis_impl!(Vec1, x)
vec_cast_impl!(Vec1, x)
indexable_impl!(Vec1, 1)
@ -79,6 +80,7 @@ pub struct Vec2<N>
new_impl!(Vec2, x, y)
ord_impl!(Vec2, x, y)
orderable_impl!(Vec2, x, y)
vec_axis_impl!(Vec2, x, y)
vec_cast_impl!(Vec2, x, y)
indexable_impl!(Vec2, 2)
@ -121,6 +123,7 @@ pub struct Vec3<N>
new_impl!(Vec3, x, y, z)
ord_impl!(Vec3, x, y, z)
orderable_impl!(Vec3, x, y, z)
vec_axis_impl!(Vec3, x, y, z)
vec_cast_impl!(Vec3, x, y, z)
indexable_impl!(Vec3, 3)
@ -164,6 +167,7 @@ pub struct Vec4<N>
new_impl!(Vec4, x, y, z, w)
ord_impl!(Vec4, x, y, z, w)
orderable_impl!(Vec4, x, y, z, w)
vec_axis_impl!(Vec4, x, y, z, w)
vec_cast_impl!(Vec4, x, y, z, w)
indexable_impl!(Vec4, 4)
@ -209,6 +213,7 @@ pub struct Vec5<N>
new_impl!(Vec5, x, y, z, w, a)
ord_impl!(Vec5, x, y, z, w, a)
orderable_impl!(Vec5, x, y, z, w, a)
vec_axis_impl!(Vec5, x, y, z, w, a)
vec_cast_impl!(Vec5, x, y, z, w, a)
indexable_impl!(Vec5, 5)
@ -256,6 +261,7 @@ pub struct Vec6<N>
new_impl!(Vec6, x, y, z, w, a, b)
ord_impl!(Vec6, x, y, z, w, a, b)
orderable_impl!(Vec6, x, y, z, w, a, b)
vec_axis_impl!(Vec6, x, y, z, w, a, b)
vec_cast_impl!(Vec6, x, y, z, w, a, b)
indexable_impl!(Vec6, 6)

View File

@ -40,6 +40,28 @@ macro_rules! ord_impl(
)
)
macro_rules! orderable_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Orderable> Orderable for $t<N>
{
#[inline]
fn max(&self, other: &$t<N>) -> $t<N>
{ $t::new(self.$comp0.max(&other.$comp0) $(, self.$compN.max(&other.$compN))*) }
#[inline]
fn min(&self, other: &$t<N>) -> $t<N>
{ $t::new(self.$comp0.min(&other.$comp0) $(, self.$compN.min(&other.$compN))*) }
#[inline]
fn clamp(&self, min: &$t<N>, max: &$t<N>) -> $t<N>
{
$t::new(self.$comp0.clamp(&min.$comp0, &max.$comp0)
$(, self.$compN.clamp(&min.$comp0, &max.$comp0))*)
}
}
)
)
macro_rules! vec_axis_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Zero + One> $t<N>