From b6f6c37ce38eae8c0c2315c5f3b02ae34522da62 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sun, 25 Nov 2012 21:26:40 +1000 Subject: [PATCH] Add trait implementations for vector structs --- src/funs/approx.rs | 156 +++++++++++++++++++++++++++++-- src/funs/extent.rs | 223 +++++++++++++++++++++++++++++++++------------ src/funs/mix.rs | 90 +++++++++++++++--- src/funs/sign.rs | 52 +++++++++++ src/quat.rs | 2 +- 5 files changed, 438 insertions(+), 85 deletions(-) diff --git a/src/funs/approx.rs b/src/funs/approx.rs index 3be1555..aa4fcd3 100644 --- a/src/funs/approx.rs +++ b/src/funs/approx.rs @@ -1,4 +1,5 @@ use num::cast::cast; +use vec::{Vec2, Vec3, Vec4}; pub trait Approx { pure fn floor() -> self; @@ -6,15 +7,15 @@ pub trait Approx { pure fn round() -> self; // pure fn roundEven() -> self; pure fn ceil() -> self; - // pure fn fract() -> self; + pure fn fract() -> self; } -#[inline(always)] pub pure fn floor(x: T) -> T { x.floor() } -#[inline(always)] pub pure fn trunc(x: T) -> T { x.trunc() } -#[inline(always)] pub pure fn round(x: T) -> T { x.round() } -// #[inline(always)] pub pure fn roundEven(x: T) -> T { x.roundEven() } -#[inline(always)] pub pure fn ceil(x: T) -> T { x.ceil() } -// #[inline(always)] pub pure fn fract(x: T) -> T { x.fract() } +#[inline(always)] pub pure fn floor(x: &T) -> T { x.floor() } +#[inline(always)] pub pure fn trunc(x: &T) -> T { x.trunc() } +#[inline(always)] pub pure fn round(x: &T) -> T { x.round() } +// #[inline(always)] pub pure fn roundEven(x: &T) -> T { x.roundEven() } +#[inline(always)] pub pure fn ceil(x: &T) -> T { x.ceil() } +#[inline(always)] pub pure fn fract(x: &T) -> T { x.fract() } pub impl f32: Approx { #[inline(always)] pure fn floor() -> f32 { cast(cmath::c_float_utils::floor(self)) } @@ -22,7 +23,7 @@ pub impl f32: Approx { #[inline(always)] pure fn round() -> f32 { cast(cmath::c_float_utils::round(self)) } // #[inline(always)] pure fn roundEven() -> f32 {} #[inline(always)] pure fn ceil() -> f32 { cast(cmath::c_float_utils::ceil(self)) } - // #[inline(always)] pure fn fract() -> f32 {} + #[inline(always)] pure fn fract() -> f32 { self - floor(&self) } } pub impl f64: Approx { @@ -31,7 +32,7 @@ pub impl f64: Approx { #[inline(always)] pure fn round() -> f64 { cast(cmath::c_double_utils::round(self)) } // #[inline(always)] pure fn roundEven() -> f64 {} #[inline(always)] pure fn ceil() -> f64 { cast(cmath::c_double_utils::ceil(self)) } - // #[inline(always)] pure fn fract() -> f64 {} + #[inline(always)] pure fn fract() -> f64 { self - floor(&self) } } pub impl float: Approx { @@ -40,5 +41,140 @@ pub impl float: Approx { #[inline(always)] pure fn round() -> float { cast(cmath::c_float_utils::round(cast(self))) } // #[inline(always)] pure fn roundEven() -> float {} #[inline(always)] pure fn ceil() -> float { cast(cmath::c_float_utils::ceil(cast(self))) } - // #[inline(always)] pure fn fract() -> float {} + #[inline(always)] pure fn fract() -> float { self - floor(&self) } +} + +pub impl Vec2: Approx { + #[inline(always)] + pure fn floor() -> Vec2 { + Vec2::new(floor(&self[0]), + floor(&self[1])) + } + + #[inline(always)] + pure fn trunc() -> Vec2 { + Vec2::new(trunc(&self[0]), + trunc(&self[1])) + } + + #[inline(always)] + pure fn round() -> Vec2 { + Vec2::new(round(&self[0]), + round(&self[1])) + } + + // #[inline(always)] + // pure fn ceil() -> Vec2 { + // Vec2::new(roundEven(&self[0]), + // roundEven(&self[1])) + // } + + #[inline(always)] + pure fn ceil() -> Vec2 { + Vec2::new(ceil(&self[0]), + ceil(&self[1])) + } + + #[inline(always)] + pure fn fract() -> Vec2 { + Vec2::new(fract(&self[0]), + fract(&self[1])) + } + +} + +pub impl Vec3: Approx { + #[inline(always)] + pure fn floor() -> Vec3 { + Vec3::new(floor(&self[0]), + floor(&self[1]), + floor(&self[2])) + } + + #[inline(always)] + pure fn trunc() -> Vec3 { + Vec3::new(trunc(&self[0]), + trunc(&self[1]), + trunc(&self[2])) + } + + #[inline(always)] + pure fn round() -> Vec3 { + Vec3::new(round(&self[0]), + round(&self[1]), + round(&self[2])) + } + + // #[inline(always)] + // pure fn ceil() -> Vec3 { + // Vec3::new(roundEven(&self[0]), + // roundEven(&self[1]), + // roundEven(&self[2])) + // } + + #[inline(always)] + pure fn ceil() -> Vec3 { + Vec3::new(ceil(&self[0]), + ceil(&self[1]), + ceil(&self[2])) + } + + #[inline(always)] + pure fn fract() -> Vec3 { + Vec3::new(fract(&self[0]), + fract(&self[1]), + fract(&self[2])) + } + +} + +pub impl Vec4: Approx { + #[inline(always)] + pure fn floor() -> Vec4 { + Vec4::new(floor(&self[0]), + floor(&self[1]), + floor(&self[2]), + floor(&self[3])) + } + + #[inline(always)] + pure fn trunc() -> Vec4 { + Vec4::new(trunc(&self[0]), + trunc(&self[1]), + trunc(&self[2]), + trunc(&self[3])) + } + + #[inline(always)] + pure fn round() -> Vec4 { + Vec4::new(round(&self[0]), + round(&self[1]), + round(&self[2]), + round(&self[3])) + } + + // #[inline(always)] + // pure fn ceil() -> Vec4 { + // Vec4::new(roundEven(&self[0]), + // roundEven(&self[1]), + // roundEven(&self[2]), + // roundEven(&self[3])) + // } + + #[inline(always)] + pure fn ceil() -> Vec4 { + Vec4::new(ceil(&self[0]), + ceil(&self[1]), + ceil(&self[2]), + ceil(&self[3])) + } + + #[inline(always)] + pure fn fract() -> Vec4 { + Vec4::new(fract(&self[0]), + fract(&self[1]), + fract(&self[2]), + fract(&self[3])) + } + } \ No newline at end of file diff --git a/src/funs/extent.rs b/src/funs/extent.rs index e8aab9f..00dcb7f 100644 --- a/src/funs/extent.rs +++ b/src/funs/extent.rs @@ -1,89 +1,194 @@ use num::cast::cast; +use vec::{Vec2, Vec3, Vec4}; -pub trait Extent { - pure fn min(y: &self) -> self; - pure fn max(y: &self) -> self; - pure fn clamp(minv: &self, maxv: &self) -> self; +pub trait MinMax { + pure fn min(other: &self) -> self; + pure fn max(other: &self) -> self; } -#[inline(always)] pub pure fn min(x: &T, y: &T) -> T { x.min(y) } -#[inline(always)] pub pure fn max(x: &T, y: &T) -> T { x.max(y) } -#[inline(always)] pub pure fn clamp(x: &T, minv: &T, maxv: &T) -> T { x.clamp(minv, maxv) } +#[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) } -pub impl u8: Extent { - #[inline(always)] pure fn min(y: &u8) -> u8 { if self < *y { self } else { *y } } - #[inline(always)] pure fn max(y: &u8) -> u8 { if self > *y { self } else { *y } } - #[inline(always)] pure fn clamp(minv: &u8, maxv: &u8) -> u8 { min(&max(&self, minv), maxv) } +pub impl u8: MinMax { + #[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 } } } -pub impl u16: Extent { - #[inline(always)] pure fn min(y: &u16) -> u16 { if self < *y { self } else { *y } } - #[inline(always)] pure fn max(y: &u16) -> u16 { if self > *y { self } else { *y } } - #[inline(always)] pure fn clamp(minv: &u16, maxv: &u16) -> u16 { min(&max(&self, minv), maxv) } +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 u32: Extent { - #[inline(always)] pure fn min(y: &u32) -> u32 { if self < *y { self } else { *y } } - #[inline(always)] pure fn max(y: &u32) -> u32 { if self > *y { self } else { *y } } - #[inline(always)] pure fn clamp(minv: &u32, maxv: &u32) -> u32 { min(&max(&self, minv), maxv) } +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 u64: Extent { - #[inline(always)] pure fn min(y: &u64) -> u64 { if self < *y { self } else { *y } } - #[inline(always)] pure fn max(y: &u64) -> u64 { if self > *y { self } else { *y } } - #[inline(always)] pure fn clamp(minv: &u64, maxv: &u64) -> u64 { min(&max(&self, minv), maxv) } +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 uint: Extent { - #[inline(always)] pure fn min(y: &uint) -> uint { if self < *y { self } else { *y } } - #[inline(always)] pure fn max(y: &uint) -> uint { if self > *y { self } else { *y } } - #[inline(always)] pure fn clamp(minv: &uint, maxv: &uint) -> uint { min(&max(&self, minv), maxv) } +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 i8: Extent { - #[inline(always)] pure fn min(y: &i8) -> i8 { if self < *y { self } else { *y } } - #[inline(always)] pure fn max(y: &i8) -> i8 { if self > *y { self } else { *y } } - #[inline(always)] pure fn clamp(minv: &i8, maxv: &i8) -> i8 { min(&max(&self, minv), maxv) } +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 i16: Extent { - #[inline(always)] pure fn min(y: &i16) -> i16 { if self < *y { self } else { *y } } - #[inline(always)] pure fn max(y: &i16) -> i16 { if self > *y { self } else { *y } } - #[inline(always)] pure fn clamp(minv: &i16, maxv: &i16) -> i16 { min(&max(&self, minv), maxv) } +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 i32: Extent { - #[inline(always)] pure fn min(y: &i32) -> i32 { if self < *y { self } else { *y } } - #[inline(always)] pure fn max(y: &i32) -> i32 { if self > *y { self } else { *y } } - #[inline(always)] pure fn clamp(minv: &i32, maxv: &i32) -> i32 { min(&max(&self, minv), maxv) } +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 i64: Extent { - #[inline(always)] pure fn min(y: &i64) -> i64 { if self < *y { self } else { *y } } - #[inline(always)] pure fn max(y: &i64) -> i64 { if self > *y { self } else { *y } } - #[inline(always)] pure fn clamp(minv: &i64, maxv: &i64) -> i64 { min(&max(&self, minv), maxv) } +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 int: Extent { - #[inline(always)] pure fn min(y: &int) -> int { if self < *y { self } else { *y } } - #[inline(always)] pure fn max(y: &int) -> int { if self > *y { self } else { *y } } - #[inline(always)] pure fn clamp(minv: &int, maxv: &int) -> int { min(&max(&self, minv), maxv) } +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 f32: Extent { - #[inline(always)] pure fn min(y: &f32) -> f32 { if self < *y { self } else { *y } } - #[inline(always)] pure fn max(y: &f32) -> f32 { if self > *y { self } else { *y } } - #[inline(always)] pure fn clamp(minv: &f32, maxv: &f32) -> f32 { min(&max(&self, minv), maxv) } +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 f64: Extent { - #[inline(always)] pure fn min(y: &f64) -> f64 { if self < *y { self } else { *y } } - #[inline(always)] pure fn max(y: &f64) -> f64 { if self > *y { self } else { *y } } - #[inline(always)] pure fn clamp(minv: &f64, maxv: &f64) -> f64 { min(&max(&self, minv), maxv) } +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 float: Extent { - #[inline(always)] pure fn min(y: &float) -> float { if self < *y { self } else { *y } } - #[inline(always)] pure fn max(y: &float) -> float { if self > *y { self } else { *y } } - #[inline(always)] pure fn clamp(minv: &float, maxv: &float) -> float { min(&max(&self, minv), maxv) } +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 Vec2: MinMax { + #[inline(always)] + pure fn min(other: &Vec2) -> Vec2 { + Vec2::new(min(&self[0], &other[0]), + min(&self[1], &other[1])) + } + + #[inline(always)] + pure fn max(other: &Vec2) -> Vec2 { + Vec2::new(max(&self[0], &other[0]), + max(&self[1], &other[1])) + } +} + +pub impl Vec3: MinMax { + #[inline(always)] + pure fn min(other: &Vec3) -> Vec3 { + Vec3::new(min(&self[0], &other[0]), + min(&self[1], &other[1]), + min(&self[2], &other[2])) + } + + #[inline(always)] + pure fn max(other: &Vec3) -> Vec3 { + Vec3::new(max(&self[0], &other[0]), + max(&self[1], &other[1]), + max(&self[2], &other[2])) + } +} + +pub impl Vec4: MinMax { + #[inline(always)] + pure fn min(other: &Vec4) -> Vec4 { + Vec4::new(min(&self[0], &other[0]), + min(&self[1], &other[1]), + min(&self[2], &other[2]), + min(&self[3], &other[3])) + } + + #[inline(always)] + pure fn max(other: &Vec4) -> Vec4 { + Vec4::new(max(&self[0], &other[0]), + max(&self[1], &other[1]), + max(&self[2], &other[2]), + max(&self[3], &other[3])) + } +} + +pub trait Clamp { + pure fn clamp(mn: &self, mx: &self) -> self; +} + +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 trait ClampV { + pure fn clamp(mn: &T, mx: &T) -> self; + pure fn clamp_v(mn: &self, mx: &self) -> self; +} + +pub impl Vec2: ClampV { + #[inline(always)] + pure fn clamp(mn: &T, mx: &T) -> Vec2 { + Vec2::new(self[0].clamp(mn, mx), + self[1].clamp(mn, mx)) + } + + #[inline(always)] + pure fn clamp_v(mn: &Vec2, mx: &Vec2) -> Vec2 { + Vec2::new(self[0].clamp(&mn[0], &mx[0]), + self[1].clamp(&mn[1], &mx[1])) + } +} + +pub impl Vec3: ClampV { + #[inline(always)] + pure fn clamp(mn: &T, mx: &T) -> Vec3 { + Vec3::new(self[0].clamp(mn, mx), + self[1].clamp(mn, mx), + self[2].clamp(mn, mx)) + } + + #[inline(always)] + pure fn clamp_v(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: ClampV { + #[inline(always)] + pure fn clamp(mn: &T, mx: &T) -> Vec4 { + Vec4::new(self[0].clamp(mn, mx), + self[1].clamp(mn, mx), + self[2].clamp(mn, mx), + self[3].clamp(mn, mx)) + } + + #[inline(always)] + pure fn clamp_v(mn: &Vec4, mx: &Vec4) -> Vec4 { + Vec4::new(self[0].clamp(&mn[0], &mx[0]), + self[1].clamp(&mn[1], &mx[1]), + self[2].clamp(&mn[2], &mx[2]), + self[3].clamp(&mn[3], &mx[3])) + } } \ No newline at end of file diff --git a/src/funs/mix.rs b/src/funs/mix.rs index b17e41d..6e94580 100644 --- a/src/funs/mix.rs +++ b/src/funs/mix.rs @@ -1,22 +1,82 @@ -pub trait Mix { - pure fn mix(y: &self, a: &self) -> self; - pure fn mixb(y: &self, a: &B) -> self; +use num::cast::*; +use vec::{Vec2, Vec3, Vec4}; + +pub trait Mix { + pure fn mix(other: &self, value: &self) -> self; } -#[inline(always)] pub pure fn mix, B>(x: &T, y: &T, a: &T) -> T { x.mix(y, a) } -#[inline(always)] pub pure fn mixb, B>(x: &T, y: &T, a: &B) -> T { x.mixb(y, a) } - -pub impl f32: Mix { - #[inline(always)] pure fn mix(y: &f32, a: &f32) -> f32 { self * (1f32 - (*a)) + (*y) * (*a) } - #[inline(always)] pure fn mixb(y: &f32, a: &bool) -> f32 { if *a { *y } else { self } } +pub impl f32: Mix { + #[inline(always)] + pure fn mix(other: &f32, value: &f32) -> f32 { + self * (1f32 - (*value)) + (*other) * (*value) + } } -pub impl f64: Mix { - #[inline(always)] pure fn mix(y: &f64, a: &f64) -> f64 { self * (1f64 - (*a)) + (*y) * (*a) } - #[inline(always)] pure fn mixb(y: &f64, a: &bool) -> f64 { if *a { *y } else { self } } +pub impl f64: Mix { + #[inline(always)] + pure fn mix(other: &f64, value: &f64) -> f64 { + self * (1f64 - (*value)) + (*other) * (*value) + } } -pub impl float: Mix { - #[inline(always)] pure fn mix(y: &float, a: &float) -> float { self * (1f - (*a)) + (*y) * (*a) } - #[inline(always)] pure fn mixb(y: &float, a: &bool) -> float { if *a { *y } else { self } } +pub impl float: Mix { + #[inline(always)] + pure fn mix(other: &float, value: &float) -> float { + self * (1f - (*value)) + (*other) * (*value) + } +} + + + +pub trait MixV { + pure fn mix(other: &self, value: &T) -> self; + pure fn mix_v(other: &self, values: &self) -> self; +} + +pub impl Vec2: MixV { + #[inline(always)] + pure fn mix(other: &Vec2, value: &T) -> Vec2 { + Vec2::new(self[0].mix(&other[0], value), + self[1].mix(&other[1], value)) + } + + #[inline(always)] + pure fn mix_v(other: &Vec2, values: &Vec2) -> Vec2 { + Vec2::new(self[0].mix(&other[0], &values[0]), + self[1].mix(&other[1], &values[1])) + } +} + +pub impl Vec3: MixV { + #[inline(always)] + pure fn mix(other: &Vec3, value: &T) -> Vec3 { + Vec3::new(self[0].mix(&other[0], value), + self[1].mix(&other[1], value), + self[2].mix(&other[2], value)) + } + + #[inline(always)] + pure fn mix_v(other: &Vec3, values: &Vec3) -> Vec3 { + Vec3::new(self[0].mix(&other[0], &values[0]), + self[1].mix(&other[1], &values[1]), + self[2].mix(&other[2], &values[2])) + } +} + +pub impl Vec4: MixV { + #[inline(always)] + pure fn mix(other: &Vec4, value: &T) -> Vec4 { + Vec4::new(self[0].mix(&other[0], value), + self[1].mix(&other[1], value), + self[2].mix(&other[2], value), + self[3].mix(&other[3], value)) + } + + #[inline(always)] + pure fn mix_v(other: &Vec4, values: &Vec4) -> Vec4 { + Vec4::new(self[0].mix(&other[0], &values[0]), + self[1].mix(&other[1], &values[1]), + self[2].mix(&other[2], &values[2]), + self[3].mix(&other[3], &values[3])) + } } \ No newline at end of file diff --git a/src/funs/sign.rs b/src/funs/sign.rs index eea7733..34de154 100644 --- a/src/funs/sign.rs +++ b/src/funs/sign.rs @@ -1,3 +1,5 @@ +use vec::{Vec2, Vec3, Vec4}; + /// Should only be implemented on signed types. pub trait Sign { pure fn abs() -> self; @@ -48,4 +50,54 @@ pub impl f64: Sign { pub impl float: Sign { #[inline(always)] pure fn abs() -> float { if self >= 0f { self } else {-self } } #[inline(always)] pure fn sign() -> float { if self > 0f { 1f } else if self == 0f { 0f } else { -1f } } +} + + + +pub impl Vec2: Sign { + #[inline(always)] + pure fn abs() -> Vec2 { + Vec2::new(abs(&self[0]), + abs(&self[1])) + } + + #[inline(always)] + pure fn sign() -> Vec2 { + Vec2::new(sign(&self[0]), + sign(&self[1])) + } +} + +pub impl Vec3: Sign { + #[inline(always)] + pure fn abs() -> Vec3 { + Vec3::new(abs(&self[0]), + abs(&self[1]), + abs(&self[2])) + } + + #[inline(always)] + pure fn sign() -> Vec3 { + Vec3::new(sign(&self[0]), + sign(&self[1]), + sign(&self[2])) + } +} + +pub impl Vec4: Sign { + #[inline(always)] + pure fn abs() -> Vec4 { + Vec4::new(abs(&self[0]), + abs(&self[1]), + abs(&self[2]), + abs(&self[3])) + } + + #[inline(always)] + pure fn sign() -> Vec4 { + Vec4::new(sign(&self[0]), + sign(&self[1]), + sign(&self[2]), + sign(&self[3])) + } } \ No newline at end of file diff --git a/src/quat.rs b/src/quat.rs index 016a048..1e52aa0 100644 --- a/src/quat.rs +++ b/src/quat.rs @@ -69,7 +69,7 @@ pub impl Quat { } } -pub impl Quat: Quaternion { +pub impl Quat: Quaternion { #[inline(always)] static pure fn identity() -> Quat { Quat::new(NumCast::one(),