diff --git a/src/funs/common.rs b/src/funs/common.rs index bc27d10..3fd1376 100644 --- a/src/funs/common.rs +++ b/src/funs/common.rs @@ -316,92 +316,268 @@ pub impl Vec4: Approx { } -pub trait MinMax { +pub trait Extent { pure fn min(other: &self) -> self; pure fn max(other: &self) -> self; + pure fn clamp(mn: &self, mx: &self) -> self; } -#[inline(always)] pub pure fn min(a: &T, b: &T) -> T { a.min(b) } -#[inline(always)] pub pure fn max(a: &T, b: &T) -> T { a.max(b) } +#[inline(always)] pub pure fn min(a: &T, b: &T) -> T { a.min(b) } +#[inline(always)] pub pure fn max(a: &T, b: &T) -> T { a.max(b) } +#[inline(always)] pub pure fn clamp(x: &T, mn: &T, mx: &T) -> T { x.clamp(mn, mx) } -pub impl u8: MinMax { - #[inline(always)] pure fn min(other: &u8) -> u8 { if self < *other { self } else { *other } } +pub impl u8: Extent { + #[inline(always)] + pure fn min(other: &u8) -> u8 { if self < *other { self } else { *other } } #[inline(always)] pure fn max(other: &u8) -> u8 { if self > *other { self } else { *other } } + + #[inline(always)] + pure fn clamp(mn: &u8, mx: &u8) -> u8 { + min(&max(&self, mn), mx) + } } -pub impl u16: MinMax { - #[inline(always)] pure fn min(other: &u16) -> u16 { if self < *other { self } else { *other } } - #[inline(always)] pure fn max(other: &u16) -> u16 { if self > *other { self } else { *other } } +pub impl u16: Extent { + #[inline(always)] + pure fn min(other: &u16) -> u16 { + if self < *other { self } else { *other } + } + + #[inline(always)] + pure fn max(other: &u16) -> u16 { + if self > *other { self } else { *other } + } + + #[inline(always)] + pure fn clamp(mn: &u16, mx: &u16) -> u16 { + min(&max(&self, mn), mx) + } } -pub impl u32: MinMax { - #[inline(always)] pure fn min(other: &u32) -> u32 { if self < *other { self } else { *other } } - #[inline(always)] pure fn max(other: &u32) -> u32 { if self > *other { self } else { *other } } +pub impl u32: Extent { + #[inline(always)] + pure fn min(other: &u32) -> u32 { + if self < *other { self } else { *other } + } + + #[inline(always)] + pure fn max(other: &u32) -> u32 { + if self > *other { self } else { *other } + } + + #[inline(always)] + pure fn clamp(mn: &u32, mx: &u32) -> u32 { + min(&max(&self, mn), mx) + } } -pub impl u64: MinMax { - #[inline(always)] pure fn min(other: &u64) -> u64 { if self < *other { self } else { *other } } - #[inline(always)] pure fn max(other: &u64) -> u64 { if self > *other { self } else { *other } } +pub impl u64: Extent { + #[inline(always)] + pure fn min(other: &u64) -> u64 { + if self < *other { self } else { *other } + } + + #[inline(always)] + pure fn max(other: &u64) -> u64 { + if self > *other { self } else { *other } + } + + #[inline(always)] + pure fn clamp(mn: &u64, mx: &u64) -> u64 { + min(&max(&self, mn), mx) + } } -pub impl uint: MinMax { - #[inline(always)] pure fn min(other: &uint) -> uint { if self < *other { self } else { *other } } - #[inline(always)] pure fn max(other: &uint) -> uint { if self > *other { self } else { *other } } +pub impl uint: Extent { + #[inline(always)] + pure fn min(other: &uint) -> uint { + if self < *other { self } else { *other } + } + + #[inline(always)] + pure fn max(other: &uint) -> uint { + if self > *other { self } else { *other } + } + + #[inline(always)] + pure fn clamp(mn: &uint, mx: &uint) -> uint { + min(&max(&self, mn), mx) + } } -pub impl i8: MinMax { - #[inline(always)] pure fn min(other: &i8) -> i8 { if self < *other { self } else { *other } } - #[inline(always)] pure fn max(other: &i8) -> i8 { if self > *other { self } else { *other } } +pub impl i8: Extent { + #[inline(always)] + pure fn min(other: &i8) -> i8 { + if self < *other { self } else { *other } + } + + #[inline(always)] + pure fn max(other: &i8) -> i8 { + if self > *other { self } else { *other } + } + + #[inline(always)] + pure fn clamp(mn: &i8, mx: &i8) -> i8 { + min(&max(&self, mn), mx) + } } -pub impl i16: MinMax { - #[inline(always)] pure fn min(other: &i16) -> i16 { if self < *other { self } else { *other } } - #[inline(always)] pure fn max(other: &i16) -> i16 { if self > *other { self } else { *other } } +pub impl i16: Extent { + #[inline(always)] + pure fn min(other: &i16) -> i16 { + if self < *other { self } else { *other } + } + + #[inline(always)] + pure fn max(other: &i16) -> i16 { + if self > *other { self } else { *other } + } + + #[inline(always)] + pure fn clamp(mn: &i16, mx: &i16) -> i16 { + min(&max(&self, mn), mx) + } } -pub impl i32: MinMax { - #[inline(always)] pure fn min(other: &i32) -> i32 { if self < *other { self } else { *other } } - #[inline(always)] pure fn max(other: &i32) -> i32 { if self > *other { self } else { *other } } +pub impl i32: Extent { + #[inline(always)] + pure fn min(other: &i32) -> i32 { + if self < *other { self } else { *other } + } + + #[inline(always)] + pure fn max(other: &i32) -> i32 { + if self > *other { self } else { *other } + } + + #[inline(always)] + pure fn clamp(mn: &i32, mx: &i32) -> i32 { + min(&max(&self, mn), mx) + } } -pub impl i64: MinMax { - #[inline(always)] pure fn min(other: &i64) -> i64 { if self < *other { self } else { *other } } - #[inline(always)] pure fn max(other: &i64) -> i64 { if self > *other { self } else { *other } } +pub impl i64: Extent { + #[inline(always)] + pure fn min(other: &i64) -> i64 { + if self < *other { self } else { *other } + } + + #[inline(always)] + pure fn max(other: &i64) -> i64 { + if self > *other { self } else { *other } + } + + #[inline(always)] + pure fn clamp(mn: &i64, mx: &i64) -> i64 { + min(&max(&self, mn), mx) + } } -pub impl int: MinMax { - #[inline(always)] pure fn min(other: &int) -> int { if self < *other { self } else { *other } } - #[inline(always)] pure fn max(other: &int) -> int { if self > *other { self } else { *other } } +pub impl int: Extent { + #[inline(always)] + pure fn min(other: &int) -> int { + if self < *other { self } else { *other } + } + + #[inline(always)] + pure fn max(other: &int) -> int { + if self > *other { self } else { *other } + } + + #[inline(always)] + pure fn clamp(mn: &int, mx: &int) -> int { + min(&max(&self, mn), mx) + } } -pub impl f32: MinMax { - #[inline(always)] pure fn min(other: &f32) -> f32 { if self < *other { self } else { *other } } - #[inline(always)] pure fn max(other: &f32) -> f32 { if self > *other { self } else { *other } } +pub impl f32: Extent { + #[inline(always)] + pure fn min(other: &f32) -> f32 { + if self < *other { self } else { *other } + } + + #[inline(always)] + pure fn max(other: &f32) -> f32 { + if self > *other { self } else { *other } + } + + #[inline(always)] + pure fn clamp(mn: &f32, mx: &f32) -> f32 { + min(&max(&self, mn), mx) + } } -pub impl f64: MinMax { - #[inline(always)] pure fn min(other: &f64) -> f64 { if self < *other { self } else { *other } } - #[inline(always)] pure fn max(other: &f64) -> f64 { if self > *other { self } else { *other } } +pub impl f64: Extent { + #[inline(always)] + pure fn min(other: &f64) -> f64 { + if self < *other { self } else { *other } + } + + #[inline(always)] + pure fn max(other: &f64) -> f64 { + if self > *other { self } else { *other } + } + + #[inline(always)] + pure fn clamp(mn: &f64, mx: &f64) -> f64 { + min(&max(&self, mn), mx) + } } -pub impl float: MinMax { - #[inline(always)] pure fn min(other: &float) -> float { if self < *other { self } else { *other } } - #[inline(always)] pure fn max(other: &float) -> float { if self > *other { self } else { *other } } +pub impl float: Extent { + #[inline(always)] + pure fn min(other: &float) -> float { + if self < *other { self } else { *other } + } + + #[inline(always)] + pure fn max(other: &float) -> float { + if self > *other { self } else { *other } + } + + #[inline(always)] + pure fn clamp(mn: &float, mx: &float) -> float { + min(&max(&self, mn), mx) + } } -pub impl Radians: MinMax{ - #[inline(always)] pure fn min(other: &Radians) -> Radians { Radians(min(&*self, &**other)) } - #[inline(always)] pure fn max(other: &Radians) -> Radians { Radians(max(&*self, &**other)) } +pub impl Radians: Extent{ + #[inline(always)] + pure fn min(other: &Radians) -> Radians { + Radians(min(&*self, &**other)) + } + + #[inline(always)] + pure fn max(other: &Radians) -> Radians { + Radians(max(&*self, &**other)) + } + + #[inline(always)] + pure fn clamp(mn: &Radians, mx: &Radians) -> Radians { + Radians((*self).clamp(&**mn, &**mx)) + } } -pub impl Degrees: MinMax{ - #[inline(always)] pure fn min(other: &Degrees) -> Degrees { Degrees(min(&*self, &**other)) } - #[inline(always)] pure fn max(other: &Degrees) -> Degrees { Degrees(max(&*self, &**other)) } +pub impl Degrees: Extent{ + #[inline(always)] + pure fn min(other: &Degrees) -> Degrees { + Degrees(min(&*self, &**other)) + } + + #[inline(always)] + pure fn max(other: &Degrees) -> Degrees { + Degrees(max(&*self, &**other)) + } + + #[inline(always)] + pure fn clamp(mn: &Degrees, mx: &Degrees) -> Degrees { + Degrees((*self).clamp(&**mn, &**mx)) + } } -pub impl Vec2: MinMax { +pub impl Vec2: Extent { #[inline(always)] pure fn min(other: &Vec2) -> Vec2 { Vec2::new(min(&self[0], &other[0]), @@ -413,9 +589,15 @@ pub impl Vec2: MinMax { Vec2::new(max(&self[0], &other[0]), max(&self[1], &other[1])) } + + #[inline(always)] + pure fn clamp(mn: &Vec2, mx: &Vec2) -> Vec2 { + Vec2::new(self[0].clamp(&mn[0], &mx[0]), + self[1].clamp(&mn[1], &mx[1])) + } } -pub impl Vec3: MinMax { +pub impl Vec3: Extent { #[inline(always)] pure fn min(other: &Vec3) -> Vec3 { Vec3::new(min(&self[0], &other[0]), @@ -429,9 +611,16 @@ pub impl Vec3: MinMax { max(&self[1], &other[1]), max(&self[2], &other[2])) } + + #[inline(always)] + pure fn clamp(mn: &Vec3, mx: &Vec3) -> Vec3 { + Vec3::new(self[0].clamp(&mn[0], &mx[0]), + self[1].clamp(&mn[1], &mx[1]), + self[2].clamp(&mn[2], &mx[2])) + } } -pub impl Vec4: MinMax { +pub impl Vec4: Extent { #[inline(always)] pure fn min(other: &Vec4) -> Vec4 { Vec4::new(min(&self[0], &other[0]), @@ -447,62 +636,7 @@ pub impl Vec4: MinMax { max(&self[2], &other[2]), max(&self[3], &other[3])) } -} - -pub trait Clamp { - pure fn clamp(mn: &self, mx: &self) -> self; -} - -#[inline(always)] pub pure fn clamp(x: &T, mn: &T, mx: &T) -> T { x.clamp(mn, mx) } - -pub impl u8: Clamp { #[inline(always)] pure fn clamp(mn: &u8, mx: &u8) -> u8 { min(&max(&self, mn), mx) } } -pub impl u16: Clamp { #[inline(always)] pure fn clamp(mn: &u16, mx: &u16) -> u16 { min(&max(&self, mn), mx) } } -pub impl u32: Clamp { #[inline(always)] pure fn clamp(mn: &u32, mx: &u32) -> u32 { min(&max(&self, mn), mx) } } -pub impl u64: Clamp { #[inline(always)] pure fn clamp(mn: &u64, mx: &u64) -> u64 { min(&max(&self, mn), mx) } } -pub impl uint: Clamp { #[inline(always)] pure fn clamp(mn: &uint, mx: &uint) -> uint { min(&max(&self, mn), mx) } } -pub impl i8: Clamp { #[inline(always)] pure fn clamp(mn: &i8, mx: &i8) -> i8 { min(&max(&self, mn), mx) } } -pub impl i16: Clamp { #[inline(always)] pure fn clamp(mn: &i16, mx: &i16) -> i16 { min(&max(&self, mn), mx) } } -pub impl i32: Clamp { #[inline(always)] pure fn clamp(mn: &i32, mx: &i32) -> i32 { min(&max(&self, mn), mx) } } -pub impl i64: Clamp { #[inline(always)] pure fn clamp(mn: &i64, mx: &i64) -> i64 { min(&max(&self, mn), mx) } } -pub impl int: Clamp { #[inline(always)] pure fn clamp(mn: &int, mx: &int) -> int { min(&max(&self, mn), mx) } } -pub impl f32: Clamp { #[inline(always)] pure fn clamp(mn: &f32, mx: &f32) -> f32 { min(&max(&self, mn), mx) } } -pub impl f64: Clamp { #[inline(always)] pure fn clamp(mn: &f64, mx: &f64) -> f64 { min(&max(&self, mn), mx) } } -pub impl float: Clamp { #[inline(always)] pure fn clamp(mn: &float, mx: &float) -> float { min(&max(&self, mn), mx) } } - - -pub impl Radians: Clamp { - #[inline(always)] - pure fn clamp(mn: &Radians, mx: &Radians) -> Radians { - Radians((*self).clamp(&**mn, &**mx)) - } -} - -pub impl Degrees: Clamp { - #[inline(always)] - pure fn clamp(mn: &Degrees, mx: &Degrees) -> Degrees { - Degrees((*self).clamp(&**mn, &**mx)) - } -} - - -pub impl Vec2: Clamp { - #[inline(always)] - pure fn clamp(mn: &Vec2, mx: &Vec2) -> Vec2 { - Vec2::new(self[0].clamp(&mn[0], &mx[0]), - self[1].clamp(&mn[1], &mx[1])) - } -} - -pub impl Vec3: Clamp { - #[inline(always)] - pure fn clamp(mn: &Vec3, mx: &Vec3) -> Vec3 { - Vec3::new(self[0].clamp(&mn[0], &mx[0]), - self[1].clamp(&mn[1], &mx[1]), - self[2].clamp(&mn[2], &mx[2])) - } -} - -pub impl Vec4: Clamp { + #[inline(always)] pure fn clamp(mn: &Vec4, mx: &Vec4) -> Vec4 { Vec4::new(self[0].clamp(&mn[0], &mx[0]), @@ -512,6 +646,7 @@ pub impl Vec4: Clamp { } } + pub trait Mix { pure fn mix(other: &self, value: &self) -> self; pure fn smooth_step(edge0: &self, edge1: &self) -> self; diff --git a/src/quat.rs b/src/quat.rs index 500d98c..b6c385a 100644 --- a/src/quat.rs +++ b/src/quat.rs @@ -70,7 +70,7 @@ pub impl Quat { } } -pub impl Quat: Quaternion { +pub impl Quat: Quaternion { #[inline(always)] static pure fn identity() -> Quat { Quat::new(NumCast::one(),