From 11b5b12d0a8319f2f0583d2cde13f6c8d32cf7f8 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Mon, 26 Nov 2012 16:48:46 +1000 Subject: [PATCH] Reorganise funs module to mirror chapter 8 of the GLSL spec --- src/funs/approx.rs | 180 ------ src/funs/boolv.rs | 53 -- src/funs/common.rs | 583 ++++++++++++++++++ src/funs/{exp.rs => exponential.rs} | 2 +- src/funs/extent.rs | 170 ----- src/funs/mix.rs | 137 ---- src/funs/projection.rs | 4 +- src/funs/{relv.rs => relational.rs} | 54 +- src/funs/sign.rs | 103 ---- .../test/{test_extent.rs => test_common.rs} | 36 +- .../{test_boolv.rs => test_relational.rs} | 2 +- src/funs/test/test_sign.rs | 35 -- src/funs/transform.rs | 7 +- src/funs/{trig.rs => triganomic.rs} | 4 +- src/lmath.rc | 17 +- src/mat.rs | 5 +- src/quat.rs | 6 +- src/vec.rs | 2 +- 18 files changed, 693 insertions(+), 707 deletions(-) delete mode 100644 src/funs/approx.rs delete mode 100644 src/funs/boolv.rs create mode 100644 src/funs/common.rs rename src/funs/{exp.rs => exponential.rs} (99%) delete mode 100644 src/funs/extent.rs delete mode 100644 src/funs/mix.rs rename src/funs/{relv.rs => relational.rs} (78%) delete mode 100644 src/funs/sign.rs rename src/funs/test/{test_extent.rs => test_common.rs} (86%) rename src/funs/test/{test_boolv.rs => test_relational.rs} (98%) delete mode 100644 src/funs/test/test_sign.rs rename src/funs/{trig.rs => triganomic.rs} (99%) diff --git a/src/funs/approx.rs b/src/funs/approx.rs deleted file mode 100644 index aa4fcd3..0000000 --- a/src/funs/approx.rs +++ /dev/null @@ -1,180 +0,0 @@ -use num::cast::cast; -use vec::{Vec2, Vec3, Vec4}; - -pub trait Approx { - pure fn floor() -> self; - pure fn trunc() -> self; - pure fn round() -> self; - // pure fn roundEven() -> self; - pure fn ceil() -> 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() } - -pub impl f32: Approx { - #[inline(always)] pure fn floor() -> f32 { cast(cmath::c_float_utils::floor(self)) } - #[inline(always)] pure fn trunc() -> f32 { cast(cmath::c_float_utils::trunc(self)) } - #[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 { self - floor(&self) } -} - -pub impl f64: Approx { - #[inline(always)] pure fn floor() -> f64 { cast(cmath::c_double_utils::floor(self)) } - #[inline(always)] pure fn trunc() -> f64 { cast(cmath::c_double_utils::trunc(self)) } - #[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 { self - floor(&self) } -} - -pub impl float: Approx { - #[inline(always)] pure fn floor() -> float { cast(cmath::c_float_utils::floor(cast(self))) } - #[inline(always)] pure fn trunc() -> float { cast(cmath::c_float_utils::trunc(cast(self))) } - #[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 { 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/boolv.rs b/src/funs/boolv.rs deleted file mode 100644 index f5a48a7..0000000 --- a/src/funs/boolv.rs +++ /dev/null @@ -1,53 +0,0 @@ -use vec::{Vector, Vec2, Vec3, Vec4}; - -pub trait BooleanVector: Vector { - pure fn any() -> bool; - pure fn all() -> bool; - pure fn not() -> self; -} - -#[inline(always)] pub pure fn any(x: &T) -> bool { x.any() } -#[inline(always)] pub pure fn all(x: &T) -> bool { x.all() } -#[inline(always)] pub pure fn not(x: &T) -> T { x.not() } - -pub impl Vec2: BooleanVector { - pure fn any() -> bool { - self[0] || self[1] - } - - pure fn all() -> bool { - self[0] && self[1] - } - - pure fn not() -> Vec2 { - Vec2::new(!self[0], !self[1]) - } -} - -pub impl Vec3: BooleanVector { - pure fn any() -> bool { - self[0] || self[1] || self[2] - } - - pure fn all() -> bool { - self[0] && self[1] && self[2] - } - - pure fn not() -> Vec3 { - Vec3::new(!self[0], !self[1], !self[2]) - } -} - -pub impl Vec4: BooleanVector { - pure fn any() -> bool { - self[0] || self[1] || self[2] || self[3] - } - - pure fn all() -> bool { - self[0] && self[1] && self[2] && self[3] - } - - pure fn not() -> Vec4 { - Vec4::new(!self[0], !self[1], !self[2], !self[3]) - } -} \ No newline at end of file diff --git a/src/funs/common.rs b/src/funs/common.rs new file mode 100644 index 0000000..bc227c0 --- /dev/null +++ b/src/funs/common.rs @@ -0,0 +1,583 @@ +use num::cast::cast; +use vec::{Vec2, Vec3, Vec4}; + +/// Should only be implemented on signed types. +pub trait Sign { + pure fn abs() -> self; + + /// returns `-1` if the number is negative, `0` if the number is equal to 0, + /// and `1` if the number is positive + pure fn sign() -> self; +} + +#[inline(always)] pub pure fn abs(x: &T) -> T { x.abs() } +#[inline(always)] pub pure fn sign(x: &T) -> T { x.sign() } + +pub impl i8: Sign { + #[inline(always)] pure fn abs() -> i8 { if self >= 0 { self } else {-self } } + #[inline(always)] pure fn sign() -> i8 { if self > 0 { 1 } else if self == 0 { 0 } else { -1 } } +} + +pub impl i16: Sign { + #[inline(always)] pure fn abs() -> i16 { if self >= 0 { self } else {-self } } + #[inline(always)] pure fn sign() -> i16 { if self > 0 { 1 } else if self == 0 { 0 } else { -1 } } +} + +pub impl i32: Sign { + #[inline(always)] pure fn abs() -> i32 { if self >= 0 { self } else {-self } } + #[inline(always)] pure fn sign() -> i32 { if self > 0 { 1 } else if self == 0 { 0 } else { -1 } } +} + +pub impl i64: Sign { + #[inline(always)] pure fn abs() -> i64 { if self >= 0 { self } else {-self } } + #[inline(always)] pure fn sign() -> i64 { if self > 0 { 1 } else if self == 0 { 0 } else { -1 } } +} + +pub impl int: Sign { + #[inline(always)] pure fn abs() -> int { if self >= 0 { self } else {-self } } + #[inline(always)] pure fn sign() -> int { if self > 0 { 1 } else if self == 0 { 0 } else { -1 } } +} + +pub impl f32: Sign { + #[inline(always)] pure fn abs() -> f32 { if self >= 0f32 { self } else {-self } } + #[inline(always)] pure fn sign() -> f32 { if self > 0f32 { 1f32 } else if self == 0f32 { 0f32 } else { -1f32 } } +} + +pub impl f64: Sign { + #[inline(always)] pure fn abs() -> f64 { if self >= 0f64 { self } else {-self } } + #[inline(always)] pure fn sign() -> f64 { if self > 0f64 { 1f64 } else if self == 0f64 { 0f64 } else { -1f64 } } +} + +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])) + } +} + +pub trait Approx { + pure fn floor() -> self; + pure fn trunc() -> self; + pure fn round() -> self; + // pure fn roundEven() -> self; + pure fn ceil() -> 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() } + +pub impl f32: Approx { + #[inline(always)] pure fn floor() -> f32 { cast(cmath::c_float_utils::floor(self)) } + #[inline(always)] pure fn trunc() -> f32 { cast(cmath::c_float_utils::trunc(self)) } + #[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 { self - floor(&self) } +} + +pub impl f64: Approx { + #[inline(always)] pure fn floor() -> f64 { cast(cmath::c_double_utils::floor(self)) } + #[inline(always)] pure fn trunc() -> f64 { cast(cmath::c_double_utils::trunc(self)) } + #[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 { self - floor(&self) } +} + +pub impl float: Approx { + #[inline(always)] pure fn floor() -> float { cast(cmath::c_float_utils::floor(cast(self))) } + #[inline(always)] pure fn trunc() -> float { cast(cmath::c_float_utils::trunc(cast(self))) } + #[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 { 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])) + } + +} + +pub trait MinMax { + pure fn min(other: &self) -> self; + pure fn max(other: &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) } + +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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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; +} + +#[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 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]), + self[1].clamp(&mn[1], &mx[1]), + self[2].clamp(&mn[2], &mx[2]), + self[3].clamp(&mn[3], &mx[3])) + } +} + +pub trait Mix { + pure fn mix(other: &self, value: &self) -> self; + pure fn smooth_step(edge0: &self, edge1: &self) -> self; + pure fn step(edge: &self) -> self; +} + +#[inline(always)] pub pure fn mix(a: &T, b: &T, value: &T) -> T { a.mix(b, value) } +#[inline(always)] pub pure fn smooth_step(x: &T, edge0: &T, edge1: &T) -> T { x.smooth_step(edge0, edge1) } +#[inline(always)] pub pure fn step(x: &T, edge: &T) -> T { x.step(edge) } + +pub impl f32: Mix { + #[inline(always)] + pure fn mix(other: &f32, value: &f32) -> f32 { + self * (1.0 - (*value)) + (*other) * (*value) + } + + #[inline(always)] + pure fn smooth_step(edge0: &f32, edge1: &f32) -> f32 { + let t = clamp(&((self - *edge0) / (*edge1 - *edge0)), &0.0, &1.0); + return t * t * (3.0 - 2.0 * t); + } + + #[inline(always)] + pure fn step(edge: &f32) -> f32 { + if self < *edge { 0.0 } else { 1.0 } + } +} + +pub impl f64: Mix { + #[inline(always)] + pure fn mix(other: &f64, value: &f64) -> f64 { + self * (1.0 - (*value)) + (*other) * (*value) + } + + #[inline(always)] + pure fn smooth_step(edge0: &f64, edge1: &f64) -> f64 { + let t = clamp(&((self - *edge0) / (*edge1 - *edge0)), &0.0, &1.0); + return t * t * (3.0 - 2.0 * t); + } + + #[inline(always)] + pure fn step(edge: &f64) -> f64 { + if self < *edge { 0.0 } else { 1.0 } + } +} + +pub impl float: Mix { + #[inline(always)] + pure fn mix(other: &float, value: &float) -> float { + self * (1.0 - (*value)) + (*other) * (*value) + } + + #[inline(always)] + pure fn smooth_step(edge0: &float, edge1: &float) -> float { + let t = clamp(&((self - *edge0) / (*edge1 - *edge0)), &0.0, &1.0); + return t * t * (3.0 - 2.0 * t); + } + + #[inline(always)] + pure fn step(edge: &float) -> float { + if self < *edge { 0.0 } else { 1.0 } + } +} + +pub impl Vec2: Mix { + #[inline(always)] + pure fn mix(other: &Vec2, value: &Vec2) -> Vec2 { + Vec2::new(self[0].mix(&other[0], &value[0]), + self[1].mix(&other[1], &value[1])) + } + + #[inline(always)] + pure fn smooth_step(edge0: &Vec2, edge1: &Vec2) -> Vec2 { + Vec2::new(self[0].smooth_step(&edge0[0], &edge1[0]), + self[1].smooth_step(&edge0[1], &edge1[1])) + } + + #[inline(always)] + pure fn step(edge: &Vec2) -> Vec2 { + Vec2::new(self[0].step(&edge[0]), + self[1].step(&edge[1])) + } +} + +pub impl Vec3: Mix { + #[inline(always)] + pure fn mix(other: &Vec3, value: &Vec3) -> Vec3 { + Vec3::new(self[0].mix(&other[0], &value[0]), + self[1].mix(&other[1], &value[1]), + self[2].mix(&other[2], &value[2])) + } + + #[inline(always)] + pure fn smooth_step(edge0: &Vec3, edge1: &Vec3) -> Vec3 { + Vec3::new(self[0].smooth_step(&edge0[0], &edge1[0]), + self[1].smooth_step(&edge0[1], &edge1[1]), + self[2].smooth_step(&edge0[2], &edge1[2])) + } + + #[inline(always)] + pure fn step(edge: &Vec3) -> Vec3 { + Vec3::new(self[0].step(&edge[0]), + self[1].step(&edge[1]), + self[2].step(&edge[2])) + } +} + +pub impl Vec4: Mix { + #[inline(always)] + pure fn mix(other: &Vec4, value: &Vec4) -> Vec4 { + Vec4::new(self[0].mix(&other[0], &value[0]), + self[1].mix(&other[1], &value[1]), + self[2].mix(&other[2], &value[2]), + self[3].mix(&other[3], &value[3])) + } + + #[inline(always)] + pure fn smooth_step(edge0: &Vec4, edge1: &Vec4) -> Vec4 { + Vec4::new(self[0].smooth_step(&edge0[0], &edge1[0]), + self[1].smooth_step(&edge0[1], &edge1[1]), + self[2].smooth_step(&edge0[2], &edge1[2]), + self[3].smooth_step(&edge0[3], &edge1[3])) + } + + #[inline(always)] + pure fn step(edge: &Vec4) -> Vec4 { + Vec4::new(self[0].step(&edge[0]), + self[1].step(&edge[1]), + self[2].step(&edge[2]), + self[3].step(&edge[3])) + } +} \ No newline at end of file diff --git a/src/funs/exp.rs b/src/funs/exponential.rs similarity index 99% rename from src/funs/exp.rs rename to src/funs/exponential.rs index 179f5ec..9587771 100644 --- a/src/funs/exp.rs +++ b/src/funs/exponential.rs @@ -1,7 +1,7 @@ /** * Exponential Functions */ -use num::cast::*; +use num::cast::{NumCast, cast}; use vec::{Vec2, Vec3, Vec4}; pub trait Exp { diff --git a/src/funs/extent.rs b/src/funs/extent.rs deleted file mode 100644 index 9f3251e..0000000 --- a/src/funs/extent.rs +++ /dev/null @@ -1,170 +0,0 @@ -use num::cast::cast; -use vec::{Vec2, Vec3, Vec4}; - -pub trait MinMax { - pure fn min(other: &self) -> self; - pure fn max(other: &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) } - -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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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; -} - -#[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 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]), - 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 deleted file mode 100644 index fa59ece..0000000 --- a/src/funs/mix.rs +++ /dev/null @@ -1,137 +0,0 @@ -use num::cast::*; -use vec::{Vec2, Vec3, Vec4}; - -use extent::{Clamp, clamp}; - -pub trait Mix { - pure fn mix(other: &self, value: &self) -> self; - pure fn smooth_step(edge0: &self, edge1: &self) -> self; - pure fn step(edge: &self) -> self; -} - -#[inline(always)] pub pure fn mix(a: &T, b: &T, value: &T) -> T { a.mix(b, value) } -#[inline(always)] pub pure fn smooth_step(x: &T, edge0: &T, edge1: &T) -> T { x.smooth_step(edge0, edge1) } -#[inline(always)] pub pure fn step(x: &T, edge: &T) -> T { x.step(edge) } - -pub impl f32: Mix { - #[inline(always)] - pure fn mix(other: &f32, value: &f32) -> f32 { - self * (1.0 - (*value)) + (*other) * (*value) - } - - #[inline(always)] - pure fn smooth_step(edge0: &f32, edge1: &f32) -> f32 { - let t = clamp(&((self - *edge0) / (*edge1 - *edge0)), &0.0, &1.0); - return t * t * (3.0 - 2.0 * t); - } - - #[inline(always)] - pure fn step(edge: &f32) -> f32 { - if self < *edge { 0.0 } else { 1.0 } - } -} - -pub impl f64: Mix { - #[inline(always)] - pure fn mix(other: &f64, value: &f64) -> f64 { - self * (1.0 - (*value)) + (*other) * (*value) - } - - #[inline(always)] - pure fn smooth_step(edge0: &f64, edge1: &f64) -> f64 { - let t = clamp(&((self - *edge0) / (*edge1 - *edge0)), &0.0, &1.0); - return t * t * (3.0 - 2.0 * t); - } - - #[inline(always)] - pure fn step(edge: &f64) -> f64 { - if self < *edge { 0.0 } else { 1.0 } - } -} - -pub impl float: Mix { - #[inline(always)] - pure fn mix(other: &float, value: &float) -> float { - self * (1.0 - (*value)) + (*other) * (*value) - } - - #[inline(always)] - pure fn smooth_step(edge0: &float, edge1: &float) -> float { - let t = clamp(&((self - *edge0) / (*edge1 - *edge0)), &0.0, &1.0); - return t * t * (3.0 - 2.0 * t); - } - - #[inline(always)] - pure fn step(edge: &float) -> float { - if self < *edge { 0.0 } else { 1.0 } - } -} - -pub impl Vec2: Mix { - #[inline(always)] - pure fn mix(other: &Vec2, value: &Vec2) -> Vec2 { - Vec2::new(self[0].mix(&other[0], &value[0]), - self[1].mix(&other[1], &value[1])) - } - - #[inline(always)] - pure fn smooth_step(edge0: &Vec2, edge1: &Vec2) -> Vec2 { - Vec2::new(self[0].smooth_step(&edge0[0], &edge1[0]), - self[1].smooth_step(&edge0[1], &edge1[1])) - } - - #[inline(always)] - pure fn step(edge: &Vec2) -> Vec2 { - Vec2::new(self[0].step(&edge[0]), - self[1].step(&edge[1])) - } -} - -pub impl Vec3: Mix { - #[inline(always)] - pure fn mix(other: &Vec3, value: &Vec3) -> Vec3 { - Vec3::new(self[0].mix(&other[0], &value[0]), - self[1].mix(&other[1], &value[1]), - self[2].mix(&other[2], &value[2])) - } - - #[inline(always)] - pure fn smooth_step(edge0: &Vec3, edge1: &Vec3) -> Vec3 { - Vec3::new(self[0].smooth_step(&edge0[0], &edge1[0]), - self[1].smooth_step(&edge0[1], &edge1[1]), - self[2].smooth_step(&edge0[2], &edge1[2])) - } - - #[inline(always)] - pure fn step(edge: &Vec3) -> Vec3 { - Vec3::new(self[0].step(&edge[0]), - self[1].step(&edge[1]), - self[2].step(&edge[2])) - } -} - -pub impl Vec4: Mix { - #[inline(always)] - pure fn mix(other: &Vec4, value: &Vec4) -> Vec4 { - Vec4::new(self[0].mix(&other[0], &value[0]), - self[1].mix(&other[1], &value[1]), - self[2].mix(&other[2], &value[2]), - self[3].mix(&other[3], &value[3])) - } - - #[inline(always)] - pure fn smooth_step(edge0: &Vec4, edge1: &Vec4) -> Vec4 { - Vec4::new(self[0].smooth_step(&edge0[0], &edge1[0]), - self[1].smooth_step(&edge0[1], &edge1[1]), - self[2].smooth_step(&edge0[2], &edge1[2]), - self[3].smooth_step(&edge0[3], &edge1[3])) - } - - #[inline(always)] - pure fn step(edge: &Vec4) -> Vec4 { - Vec4::new(self[0].step(&edge[0]), - self[1].step(&edge[1]), - self[2].step(&edge[2]), - self[3].step(&edge[3])) - } -} \ No newline at end of file diff --git a/src/funs/projection.rs b/src/funs/projection.rs index 870ddd3..6b45c0a 100644 --- a/src/funs/projection.rs +++ b/src/funs/projection.rs @@ -1,5 +1,5 @@ -use funs::trig::*; -use angle::Angle; +use funs::triganomic::tan; +use angle::Radians; use mat::Mat4; use num::cast::cast; use num::consts::pi; diff --git a/src/funs/relv.rs b/src/funs/relational.rs similarity index 78% rename from src/funs/relv.rs rename to src/funs/relational.rs index 0ed2587..d6b0eb9 100644 --- a/src/funs/relv.rs +++ b/src/funs/relational.rs @@ -4,7 +4,7 @@ use core::cmp::{Eq, Ord}; -use vec::{Vec2, Vec3, Vec4}; +use vec::{Vector, Vec2, Vec3, Vec4}; pub trait RelVector { pure fn less_than(y: &self) -> BVec; @@ -152,4 +152,56 @@ pub impl Vec4: RelVector> { self[2] != y[2], self[3] != y[3]) } +} + +pub trait BooleanVector: Vector { + pure fn any() -> bool; + pure fn all() -> bool; + pure fn not() -> self; +} + +#[inline(always)] pub pure fn any(x: &T) -> bool { x.any() } +#[inline(always)] pub pure fn all(x: &T) -> bool { x.all() } +#[inline(always)] pub pure fn not(x: &T) -> T { x.not() } + +pub impl Vec2: BooleanVector { + pure fn any() -> bool { + self[0] || self[1] + } + + pure fn all() -> bool { + self[0] && self[1] + } + + pure fn not() -> Vec2 { + Vec2::new(!self[0], !self[1]) + } +} + +pub impl Vec3: BooleanVector { + pure fn any() -> bool { + self[0] || self[1] || self[2] + } + + pure fn all() -> bool { + self[0] && self[1] && self[2] + } + + pure fn not() -> Vec3 { + Vec3::new(!self[0], !self[1], !self[2]) + } +} + +pub impl Vec4: BooleanVector { + pure fn any() -> bool { + self[0] || self[1] || self[2] || self[3] + } + + pure fn all() -> bool { + self[0] && self[1] && self[2] && self[3] + } + + pure fn not() -> Vec4 { + Vec4::new(!self[0], !self[1], !self[2], !self[3]) + } } \ No newline at end of file diff --git a/src/funs/sign.rs b/src/funs/sign.rs deleted file mode 100644 index 34de154..0000000 --- a/src/funs/sign.rs +++ /dev/null @@ -1,103 +0,0 @@ -use vec::{Vec2, Vec3, Vec4}; - -/// Should only be implemented on signed types. -pub trait Sign { - pure fn abs() -> self; - - /// returns `-1` if the number is negative, `0` if the number is equal to 0, - /// and `1` if the number is positive - pure fn sign() -> self; -} - -#[inline(always)] pub pure fn abs(x: &T) -> T { x.abs() } -#[inline(always)] pub pure fn sign(x: &T) -> T { x.sign() } - -pub impl i8: Sign { - #[inline(always)] pure fn abs() -> i8 { if self >= 0 { self } else {-self } } - #[inline(always)] pure fn sign() -> i8 { if self > 0 { 1 } else if self == 0 { 0 } else { -1 } } -} - -pub impl i16: Sign { - #[inline(always)] pure fn abs() -> i16 { if self >= 0 { self } else {-self } } - #[inline(always)] pure fn sign() -> i16 { if self > 0 { 1 } else if self == 0 { 0 } else { -1 } } -} - -pub impl i32: Sign { - #[inline(always)] pure fn abs() -> i32 { if self >= 0 { self } else {-self } } - #[inline(always)] pure fn sign() -> i32 { if self > 0 { 1 } else if self == 0 { 0 } else { -1 } } -} - -pub impl i64: Sign { - #[inline(always)] pure fn abs() -> i64 { if self >= 0 { self } else {-self } } - #[inline(always)] pure fn sign() -> i64 { if self > 0 { 1 } else if self == 0 { 0 } else { -1 } } -} - -pub impl int: Sign { - #[inline(always)] pure fn abs() -> int { if self >= 0 { self } else {-self } } - #[inline(always)] pure fn sign() -> int { if self > 0 { 1 } else if self == 0 { 0 } else { -1 } } -} - -pub impl f32: Sign { - #[inline(always)] pure fn abs() -> f32 { if self >= 0f32 { self } else {-self } } - #[inline(always)] pure fn sign() -> f32 { if self > 0f32 { 1f32 } else if self == 0f32 { 0f32 } else { -1f32 } } -} - -pub impl f64: Sign { - #[inline(always)] pure fn abs() -> f64 { if self >= 0f64 { self } else {-self } } - #[inline(always)] pure fn sign() -> f64 { if self > 0f64 { 1f64 } else if self == 0f64 { 0f64 } else { -1f64 } } -} - -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/funs/test/test_extent.rs b/src/funs/test/test_common.rs similarity index 86% rename from src/funs/test/test_extent.rs rename to src/funs/test/test_common.rs index aba1152..95a1c8e 100644 --- a/src/funs/test/test_extent.rs +++ b/src/funs/test/test_common.rs @@ -1,6 +1,40 @@ -use extent::*; +use common::*; use vec::{Vec2, Vec3, Vec4}; +#[test] +fn test_abs() { + assert 0.abs() == 0; + assert 2.abs() == 2; + assert (-2).abs() == 2; + assert abs(&0) == 0; + assert abs(&2) == 2; + assert abs(&-2) == 2; + + assert 0.0.abs() == 0.0; + assert 2.5.abs() == 2.5; + assert (-2.5).abs() == 2.5; + assert abs(&0.0) == 0.0; + assert abs(&2.5) == 2.5; + assert abs(&-2.5) == 2.5; +} + +#[test] +fn test_sign() { + assert 0.sign() == 0; + assert 2.sign() == 1; + assert (-2).sign() == -1; + assert sign(&0) == 0; + assert sign(&2) == 1; + assert sign(&-2) == -1; + + assert 0.0.sign() == 0.0; + assert 2.5.sign() == 1.0; + assert (-2.5).sign()== -1.0; + assert sign(&0.0) == 0.0; + assert sign(&2.5) == 1.0; + assert sign(&-2.5) == -1.0; +} + #[test] fn test_min() { assert 1u.min(&2u) == 1u; diff --git a/src/funs/test/test_boolv.rs b/src/funs/test/test_relational.rs similarity index 98% rename from src/funs/test/test_boolv.rs rename to src/funs/test/test_relational.rs index fcafb70..b78289c 100644 --- a/src/funs/test/test_boolv.rs +++ b/src/funs/test/test_relational.rs @@ -1,5 +1,5 @@ use vec::*; -use boolv::*; +use relational::*; #[test] fn test_boolv2() { diff --git a/src/funs/test/test_sign.rs b/src/funs/test/test_sign.rs deleted file mode 100644 index 15e3efc..0000000 --- a/src/funs/test/test_sign.rs +++ /dev/null @@ -1,35 +0,0 @@ -use sign::*; - -#[test] -fn test_abs() { - assert 0.abs() == 0; - assert 2.abs() == 2; - assert (-2).abs() == 2; - assert abs(&0) == 0; - assert abs(&2) == 2; - assert abs(&-2) == 2; - - assert 0.0.abs() == 0.0; - assert 2.5.abs() == 2.5; - assert (-2.5).abs() == 2.5; - assert abs(&0.0) == 0.0; - assert abs(&2.5) == 2.5; - assert abs(&-2.5) == 2.5; -} - -#[test] -fn test_sign() { - assert 0.sign() == 0; - assert 2.sign() == 1; - assert (-2).sign() == -1; - assert sign(&0) == 0; - assert sign(&2) == 1; - assert sign(&-2) == -1; - - assert 0.0.sign() == 0.0; - assert 2.5.sign() == 1.0; - assert (-2.5).sign()== -1.0; - assert sign(&0.0) == 0.0; - assert sign(&2.5) == 1.0; - assert sign(&-2.5) == -1.0; -} \ No newline at end of file diff --git a/src/funs/transform.rs b/src/funs/transform.rs index 7eb35a8..462520f 100644 --- a/src/funs/transform.rs +++ b/src/funs/transform.rs @@ -1,7 +1,8 @@ -use funs::trig::*; -use angle::Angle; +use funs::triganomic::{cos, sin}; +use angle::Radians; use mat::{Mat3, Mat4}; -use num::cast::*; +use num::cast::{NumCast, cast}; +use vec::Vec3; pub pure fn mat3_from_rotation(theta: Radians, axis: Vec3) -> Mat3 { let c: T = cos(&theta); diff --git a/src/funs/trig.rs b/src/funs/triganomic.rs similarity index 99% rename from src/funs/trig.rs rename to src/funs/triganomic.rs index b9836e6..5489b0c 100644 --- a/src/funs/trig.rs +++ b/src/funs/triganomic.rs @@ -1,5 +1,5 @@ -use num::cast::*; -use angle::*; +use num::cast::{NumCast, cast}; +use angle::Radians; use vec::{Vec3, Vec2, Vec4}; /// diff --git a/src/lmath.rc b/src/lmath.rc index bcd9da0..a3a6d47 100644 --- a/src/lmath.rc +++ b/src/lmath.rc @@ -35,22 +35,17 @@ pub mod num { } pub mod funs { - pub mod approx; - pub mod boolv; - pub mod exp; - pub mod extent; - pub mod mix; + pub mod common; + pub mod exponential; pub mod projection; - pub mod relv; - pub mod sign; + pub mod relational; pub mod transform; - pub mod trig; + pub mod triganomic; #[test] mod test { - mod test_boolv; - mod test_extent; - mod test_sign; + mod test_common; + mod test_relational; mod test_transform; } } \ No newline at end of file diff --git a/src/mat.rs b/src/mat.rs index 5e1c28f..2c16e3d 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -7,9 +7,8 @@ use core::vec::raw::buf_as_slice; use std::cmp::FuzzyEq; use dim::Dimensional; -use funs::exp::*; -use funs::extent::*; -use funs::sign::*; +use funs::common::*; +use funs::exponential::*; use num::cast::*; use num::default_eq::DefaultEq; use quat::{Quat, ToQuat}; diff --git a/src/quat.rs b/src/quat.rs index 8b66969..55f0549 100644 --- a/src/quat.rs +++ b/src/quat.rs @@ -7,9 +7,9 @@ use core::vec::raw::buf_as_slice; use std::cmp::FuzzyEq; use dim::Dimensional; -use funs::exp::*; -use funs::extent::*; -use funs::trig::*; +use funs::common::*; +use funs::exponential::*; +use funs::triganomic::*; use mat::{Mat3, Mat4}; use num::cast::*; use num::default_eq::DefaultEq; diff --git a/src/vec.rs b/src/vec.rs index aa3e516..17eccda 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -7,7 +7,7 @@ use core::vec::raw::buf_as_slice; use std::cmp::FuzzyEq; use dim::Dimensional; -use funs::exp::Exp; +use funs::exponential::Exp; use num::cast::*; use num::default_eq::DefaultEq;