diff --git a/src/tests/vec.rs b/src/tests/vec.rs index 7a1c6c5c..46075591 100644 --- a/src/tests/vec.rs +++ b/src/tests/vec.rs @@ -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) + ); +} diff --git a/src/vec.rs b/src/vec.rs index 823dc0c6..6a00a109 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -40,6 +40,7 @@ pub struct Vec1 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 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 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 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 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 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) diff --git a/src/vec_macros.rs b/src/vec_macros.rs index afbea1d7..99244c1a 100644 --- a/src/vec_macros.rs +++ b/src/vec_macros.rs @@ -40,6 +40,28 @@ macro_rules! ord_impl( ) ) +macro_rules! orderable_impl( + ($t: ident, $comp0: ident $(,$compN: ident)*) => ( + impl Orderable for $t + { + #[inline] + fn max(&self, other: &$t) -> $t + { $t::new(self.$comp0.max(&other.$comp0) $(, self.$compN.max(&other.$compN))*) } + + #[inline] + fn min(&self, other: &$t) -> $t + { $t::new(self.$comp0.min(&other.$comp0) $(, self.$compN.min(&other.$compN))*) } + + #[inline] + fn clamp(&self, min: &$t, max: &$t) -> $t + { + $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 $t