diff --git a/src/funs/common.rs b/src/funs/common.rs index 7a75cf3..7bdf905 100644 --- a/src/funs/common.rs +++ b/src/funs/common.rs @@ -8,7 +8,7 @@ use numeric::funs::*; use numeric::traits::*; -use vec::{Vec2, Vec3, Vec4}; +use vec::{Vector, Vec2, Vec3, Vec4}; // Approx @@ -146,52 +146,125 @@ pub impl Vec4: Approx { // Extent -pub impl Vec2: Extent { +pub trait ExtentVector: Vector { + pure fn min_t(&self, value: T) -> self; + pure fn max_t(&self, value: T) -> self; + pure fn clamp_t(&self, mn: T, mx: T) -> self; + + pure fn min_v(&self, other: &self) -> self; + pure fn max_v(&self, other: &self) -> self; + pure fn clamp_v(&self, mn: &self, mx: &self) -> self; +} + +pub impl Vec2: ExtentVector { #[inline(always)] - pure fn min(&self, other: &Vec2) -> Vec2 { + pure fn min_t(&self, value: T) -> Vec2 { + Vec2::new(min(&self[0], &value), + min(&self[1], &value)) + } + + #[inline(always)] + pure fn max_t(&self, value: T) -> Vec2 { + Vec2::new(max(&self[0], &value), + max(&self[1], &value)) + } + + #[inline(always)] + pure fn clamp_t(&self, mn: T, mx: T) -> Vec2 { + Vec2::new(self[0].clamp(&mn, &mx), + self[1].clamp(&mn, &mx)) + } + + #[inline(always)] + pure fn min_v(&self, other: &Vec2) -> Vec2 { Vec2::new(min(&self[0], &other[0]), min(&self[1], &other[1])) } #[inline(always)] - pure fn max(&self, other: &Vec2) -> Vec2 { + pure fn max_v(&self, other: &Vec2) -> Vec2 { Vec2::new(max(&self[0], &other[0]), max(&self[1], &other[1])) } #[inline(always)] - pure fn clamp(&self, mn: &Vec2, mx: &Vec2) -> Vec2 { + pure fn clamp_v(&self, mn: &Vec2, mx: &Vec2) -> Vec2 { Vec2::new(self[0].clamp(&mn[0], &mx[0]), self[1].clamp(&mn[1], &mx[1])) } } -pub impl Vec3: Extent { +pub impl Vec3: ExtentVector { #[inline(always)] - pure fn min(&self, other: &Vec3) -> Vec3 { + pure fn min_t(&self, value: T) -> Vec3 { + Vec3::new(min(&self[0], &value), + min(&self[1], &value), + min(&self[2], &value)) + } + + #[inline(always)] + pure fn max_t(&self, value: T) -> Vec3 { + Vec3::new(max(&self[0], &value), + max(&self[1], &value), + max(&self[2], &value)) + } + + #[inline(always)] + pure fn clamp_t(&self, 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 min_v(&self, 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(&self, other: &Vec3) -> Vec3 { + pure fn max_v(&self, other: &Vec3) -> Vec3 { Vec3::new(max(&self[0], &other[0]), max(&self[1], &other[1]), max(&self[2], &other[2])) } #[inline(always)] - pure fn clamp(&self, mn: &Vec3, mx: &Vec3) -> Vec3 { + pure fn clamp_v(&self, 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: Extent { +pub impl Vec4: ExtentVector { #[inline(always)] - pure fn min(&self, other: &Vec4) -> Vec4 { + pure fn min_t(&self, value: T) -> Vec4 { + Vec4::new(min(&self[0], &value), + min(&self[1], &value), + min(&self[2], &value), + min(&self[3], &value)) + } + + #[inline(always)] + pure fn max_t(&self, value: T) -> Vec4 { + Vec4::new(max(&self[0], &value), + max(&self[1], &value), + max(&self[2], &value), + max(&self[3], &value)) + } + + #[inline(always)] + pure fn clamp_t(&self, 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 min_v(&self, other: &Vec4) -> Vec4 { Vec4::new(min(&self[0], &other[0]), min(&self[1], &other[1]), min(&self[2], &other[2]), @@ -199,7 +272,7 @@ pub impl Vec4: Extent { } #[inline(always)] - pure fn max(&self, other: &Vec4) -> Vec4 { + pure fn max_v(&self, other: &Vec4) -> Vec4 { Vec4::new(max(&self[0], &other[0]), max(&self[1], &other[1]), max(&self[2], &other[2]), @@ -207,7 +280,7 @@ pub impl Vec4: Extent { } #[inline(always)] - pure fn clamp(&self, mn: &Vec4, mx: &Vec4) -> Vec4 { + pure fn clamp_v(&self, 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]), @@ -217,52 +290,125 @@ pub impl Vec4: Extent { // Mix -pub impl Vec2: Mix { +pub trait MixVector: Vector { + pure fn mix_t(&self, other: T, value: T) -> self; + pure fn smooth_step_t(&self, edge0: T, edge1: T) -> self; + pure fn step_t(&self, edge: T) -> self; + + pure fn mix_v(&self, other: &self, value: &self) -> self; + pure fn smooth_step_v(&self, edge0: &self, edge1: &self) -> self; + pure fn step_v(&self, edge: &self) -> self; +} + +pub impl Vec2: MixVector { #[inline(always)] - pure fn mix(&self, other: &Vec2, value: &Vec2) -> Vec2 { + pure fn mix_t(&self, other: T, value: T) -> Vec2 { + Vec2::new(self[0].mix(&other, &value), + self[1].mix(&other, &value)) + } + + #[inline(always)] + pure fn smooth_step_t(&self, edge0: T, edge1: T) -> Vec2 { + Vec2::new(self[0].smooth_step(&edge0, &edge1), + self[1].smooth_step(&edge0, &edge1)) + } + + #[inline(always)] + pure fn step_t(&self, edge: T) -> Vec2 { + Vec2::new(self[0].step(&edge), + self[1].step(&edge)) + } + + #[inline(always)] + pure fn mix_v(&self, 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(&self, edge0: &Vec2, edge1: &Vec2) -> Vec2 { + pure fn smooth_step_v(&self, 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(&self, edge: &Vec2) -> Vec2 { + pure fn step_v(&self, edge: &Vec2) -> Vec2 { Vec2::new(self[0].step(&edge[0]), self[1].step(&edge[1])) } } -pub impl Vec3: Mix { +pub impl Vec3: MixVector { #[inline(always)] - pure fn mix(&self, other: &Vec3, value: &Vec3) -> Vec3 { + pure fn mix_t(&self, other: T, value: T) -> Vec3 { + Vec3::new(self[0].mix(&other, &value), + self[1].mix(&other, &value), + self[2].mix(&other, &value)) + } + + #[inline(always)] + pure fn smooth_step_t(&self, edge0: T, edge1: T) -> Vec3 { + Vec3::new(self[0].smooth_step(&edge0, &edge1), + self[1].smooth_step(&edge0, &edge1), + self[2].smooth_step(&edge0, &edge1)) + } + + #[inline(always)] + pure fn step_t(&self, edge: T) -> Vec3 { + Vec3::new(self[0].step(&edge), + self[1].step(&edge), + self[2].step(&edge)) + } + + #[inline(always)] + pure fn mix_v(&self, 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(&self, edge0: &Vec3, edge1: &Vec3) -> Vec3 { + pure fn smooth_step_v(&self, 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(&self, edge: &Vec3) -> Vec3 { + pure fn step_v(&self, edge: &Vec3) -> Vec3 { Vec3::new(self[0].step(&edge[0]), self[1].step(&edge[1]), self[2].step(&edge[2])) } } -pub impl Vec4: Mix { +pub impl Vec4: MixVector { #[inline(always)] - pure fn mix(&self, other: &Vec4, value: &Vec4) -> Vec4 { + pure fn mix_t(&self, other: T, value: T) -> Vec4 { + Vec4::new(self[0].mix(&other, &value), + self[1].mix(&other, &value), + self[2].mix(&other, &value), + self[3].mix(&other, &value)) + } + + #[inline(always)] + pure fn smooth_step_t(&self, edge0: T, edge1: T) -> Vec4 { + Vec4::new(self[0].smooth_step(&edge0, &edge1), + self[1].smooth_step(&edge0, &edge1), + self[2].smooth_step(&edge0, &edge1), + self[3].smooth_step(&edge0, &edge1)) + } + + #[inline(always)] + pure fn step_t(&self, edge: T) -> Vec4 { + Vec4::new(self[0].step(&edge), + self[1].step(&edge), + self[2].step(&edge), + self[3].step(&edge)) + } + + #[inline(always)] + pure fn mix_v(&self, 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]), @@ -270,7 +416,7 @@ pub impl Vec4: Mix { } #[inline(always)] - pure fn smooth_step(&self, edge0: &Vec4, edge1: &Vec4) -> Vec4 { + pure fn smooth_step_v(&self, 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]), @@ -278,7 +424,7 @@ pub impl Vec4: Mix { } #[inline(always)] - pure fn step(&self, edge: &Vec4) -> Vec4 { + pure fn step_v(&self, edge: &Vec4) -> Vec4 { Vec4::new(self[0].step(&edge[0]), self[1].step(&edge[1]), self[2].step(&edge[2]), diff --git a/src/funs/exponential.rs b/src/funs/exponential.rs index cb3888a..03088f7 100644 --- a/src/funs/exponential.rs +++ b/src/funs/exponential.rs @@ -8,15 +8,33 @@ use numeric::funs::*; use numeric::traits::*; -use vec::{Vec2, Vec3, Vec4}; +use vec::{Vector, Vec2, Vec3, Vec4}; // Exp +pub trait ExpVector: Vector { + pure fn pow_t(&self, n: T) -> self; + pure fn pow_v(&self, n: &self) -> self; +} + +pub impl Vec2: ExpVector { + #[inline(always)] + pure fn pow_t(&self, n: T) -> Vec2 { + Vec2::new(pow(&self[0], &n), + pow(&self[1], &n)) + } + + #[inline(always)] + pure fn pow_v(&self, n: &Vec2) -> Vec2 { + Vec2::new(pow(&self[0], &n[0]), + pow(&self[1], &n[1])) + } +} + pub impl Vec2: Exp { #[inline(always)] pure fn pow(&self, n: &Vec2) -> Vec2 { - Vec2::new(pow(&self[0], &n[0]), - pow(&self[1], &n[1])) + self.pow_v(n) } #[inline(always)] @@ -56,13 +74,27 @@ pub impl Vec2: Exp { } } -pub impl Vec3: Exp { +pub impl Vec3: ExpVector { #[inline(always)] - pure fn pow(&self, n: &Vec3) -> Vec3 { + pure fn pow_t(&self, n: T) -> Vec3 { + Vec3::new(pow(&self[0], &n), + pow(&self[1], &n), + pow(&self[2], &n)) + } + + #[inline(always)] + pure fn pow_v(&self, n: &Vec3) -> Vec3 { Vec3::new(pow(&self[0], &n[0]), pow(&self[1], &n[1]), pow(&self[2], &n[2])) } +} + +pub impl Vec3: Exp { + #[inline(always)] + pure fn pow(&self, n: &Vec3) -> Vec3 { + self.pow_v(n) + } #[inline(always)] pure fn exp(&self) -> Vec3 { @@ -107,14 +139,29 @@ pub impl Vec3: Exp { } } -pub impl Vec4: Exp { +pub impl Vec4: ExpVector { #[inline(always)] - pure fn pow(&self, n: &Vec4) -> Vec4 { + pure fn pow_t(&self, n: T) -> Vec4 { + Vec4::new(pow(&self[0], &n), + pow(&self[1], &n), + pow(&self[2], &n), + pow(&self[3], &n)) + } + + #[inline(always)] + pure fn pow_v(&self, n: &Vec4) -> Vec4 { Vec4::new(pow(&self[0], &n[0]), pow(&self[1], &n[1]), pow(&self[2], &n[2]), pow(&self[3], &n[3])) } +} + +pub impl Vec4: Exp { + #[inline(always)] + pure fn pow(&self, n: &Vec4) -> Vec4 { + self.pow_v(n) + } #[inline(always)] pure fn exp(&self) -> Vec4 {