diff --git a/src/mat2.rs b/src/mat2.rs index 5b45e31..7de02c4 100644 --- a/src/mat2.rs +++ b/src/mat2.rs @@ -5,8 +5,9 @@ use core::sys::size_of; use core::util::swap; use core::vec::raw::buf_as_slice; -use std::cmp::FuzzyEq; +use std::cmp::{FuzzyEq, FUZZY_EPSILON}; use numeric::*; +use numeric::number::Number; use numeric::number::Number::{zero,one}; use vec::{ @@ -42,7 +43,7 @@ use mat::{ #[deriving_eq] pub struct Mat2 { x: Vec2, y: Vec2 } -pub impl Mat2: Matrix> { +pub impl> Mat2: Matrix> { #[inline(always)] pure fn col(&self, i: uint) -> Vec2 { self[i] } @@ -204,7 +205,7 @@ pub impl Mat2: Matrix> { } } -pub impl Mat2: MutableMatrix> { +pub impl> Mat2: MutableMatrix> { #[inline(always)] fn col_mut(&mut self, i: uint) -> &self/mut Vec2 { match i { @@ -274,7 +275,7 @@ pub impl Mat2: MutableMatrix> { } } -pub impl Mat2: Matrix2> { +pub impl> Mat2: Matrix2> { /** * Construct a 2 x 2 matrix * @@ -385,18 +386,23 @@ pub impl Mat2: Index> { } } -pub impl Mat2: Neg> { +pub impl> Mat2: Neg> { #[inline(always)] pure fn neg(&self) -> Mat2 { Matrix2::from_cols(-self[0], -self[1]) } } -pub impl Mat2: FuzzyEq { +pub impl> Mat2: FuzzyEq { #[inline(always)] pure fn fuzzy_eq(&self, other: &Mat2) -> bool { - self[0].fuzzy_eq(&other[0]) && - self[1].fuzzy_eq(&other[1]) + self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON)) + } + + #[inline(always)] + pure fn fuzzy_eq_eps(&self, other: &Mat2, epsilon: &T) -> bool { + self[0].fuzzy_eq_eps(&other[0], epsilon) && + self[1].fuzzy_eq_eps(&other[1], epsilon) } } diff --git a/src/mat3.rs b/src/mat3.rs index f292d16..bf91010 100644 --- a/src/mat3.rs +++ b/src/mat3.rs @@ -5,7 +5,7 @@ use core::util::swap; use core::sys::size_of; use core::vec::raw::buf_as_slice; -use std::cmp::FuzzyEq; +use std::cmp::{FuzzyEq, FUZZY_EPSILON}; use numeric::*; use numeric::number::Number; use numeric::number::Number::{zero,one}; @@ -43,7 +43,7 @@ use mat::{ #[deriving_eq] pub struct Mat3 { x: Vec3, y: Vec3, z: Vec3 } -pub impl Mat3: Matrix> { +pub impl> Mat3: Matrix> { #[inline(always)] pure fn col(&self, i: uint) -> Vec3 { self[i] } @@ -243,7 +243,7 @@ pub impl Mat3: Matrix> { } } -pub impl Mat3: Matrix3> { +pub impl> Mat3: Matrix3> { /** * Construct a 3 x 3 matrix * @@ -470,7 +470,7 @@ pub impl Mat3: Matrix3> { } } -pub impl Mat3: MutableMatrix> { +pub impl> Mat3: MutableMatrix> { #[inline(always)] fn col_mut(&mut self, i: uint) -> &self/mut Vec3 { match i { @@ -561,19 +561,24 @@ pub impl Mat3: Index> { } } -pub impl Mat3: Neg> { +pub impl> Mat3: Neg> { #[inline(always)] pure fn neg(&self) -> Mat3 { Matrix3::from_cols(-self[0], -self[1], -self[2]) } } -pub impl Mat3: FuzzyEq { +pub impl> Mat3: FuzzyEq { #[inline(always)] pure fn fuzzy_eq(&self, other: &Mat3) -> bool { - self[0].fuzzy_eq(&other[0]) && - self[1].fuzzy_eq(&other[1]) && - self[2].fuzzy_eq(&other[2]) + self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON)) + } + + #[inline(always)] + pure fn fuzzy_eq_eps(&self, other: &Mat3, epsilon: &T) -> bool { + self[0].fuzzy_eq_eps(&other[0], epsilon) && + self[1].fuzzy_eq_eps(&other[1], epsilon) && + self[2].fuzzy_eq_eps(&other[2], epsilon) } } diff --git a/src/mat4.rs b/src/mat4.rs index f51ae46..e374fa0 100644 --- a/src/mat4.rs +++ b/src/mat4.rs @@ -5,8 +5,9 @@ use core::util::swap; use core::sys::size_of; use core::vec::raw::buf_as_slice; -use std::cmp::FuzzyEq; +use std::cmp::{FuzzyEq, FUZZY_EPSILON}; use numeric::*; +use numeric::number::Number; use numeric::number::Number::{zero,one}; use vec::{ @@ -41,7 +42,7 @@ use mat::{ #[deriving_eq] pub struct Mat4 { x: Vec4, y: Vec4, z: Vec4, w: Vec4 } -pub impl Mat4: Matrix> { +pub impl> Mat4: Matrix> { #[inline(always)] pure fn col(&self, i: uint) -> Vec4 { self[i] } @@ -328,7 +329,7 @@ pub impl Mat4: Matrix> { } } -pub impl Mat4: Matrix4> { +pub impl> Mat4: Matrix4> { /** * Construct a 4 x 4 matrix * @@ -395,7 +396,7 @@ pub impl Mat4: Matrix4> { } } -pub impl Mat4: MutableMatrix> { +pub impl> Mat4: MutableMatrix> { #[inline(always)] fn col_mut(&mut self, i: uint) -> &self/mut Vec4 { match i { @@ -488,7 +489,7 @@ pub impl Mat4: MutableMatrix> { } } -pub impl Mat4: Neg> { +pub impl> Mat4: Neg> { #[inline(always)] pure fn neg(&self) -> Mat4 { Matrix4::from_cols(-self[0], -self[1], -self[2], -self[3]) @@ -505,13 +506,18 @@ pub impl Mat4: Index> { } } -pub impl Mat4: FuzzyEq { +pub impl> Mat4: FuzzyEq { #[inline(always)] pure fn fuzzy_eq(&self, other: &Mat4) -> bool { - self[0].fuzzy_eq(&other[0]) && - self[1].fuzzy_eq(&other[1]) && - self[2].fuzzy_eq(&other[2]) && - self[3].fuzzy_eq(&other[3]) + self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON)) + } + + #[inline(always)] + pure fn fuzzy_eq_eps(&self, other: &Mat4, epsilon: &T) -> bool { + self[0].fuzzy_eq_eps(&other[0], epsilon) && + self[1].fuzzy_eq_eps(&other[1], epsilon) && + self[2].fuzzy_eq_eps(&other[2], epsilon) && + self[3].fuzzy_eq_eps(&other[3], epsilon) } } diff --git a/src/projection.rs b/src/projection.rs index 498a750..c671f3d 100644 --- a/src/projection.rs +++ b/src/projection.rs @@ -1,6 +1,8 @@ use numeric::*; use numeric::number::Number; +use std::cmp::FuzzyEq; + use mat::{Mat4, Matrix4}; /** @@ -10,7 +12,7 @@ use mat::{Mat4, Matrix4}; * can be found [here](http://www.opengl.org/wiki/GluPerspective_code). */ #[inline(always)] -pub pure fn perspective(fovy: T, aspectRatio: T, near: T, far: T) -> Mat4 { +pub pure fn perspective>(fovy: T, aspectRatio: T, near: T, far: T) -> Mat4 { let ymax = near * tan(radians(fovy)); let xmax = ymax * aspectRatio; @@ -24,7 +26,7 @@ pub pure fn perspective(fovy: T, aspectRatio: T, near: T, far: T) * (http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml) function. */ #[inline(always)] -pub pure fn frustum(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4 { +pub pure fn frustum>(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4 { let _0: T = Number::from(0); let _1: T = Number::from(1); let _2: T = Number::from(2); diff --git a/src/quat.rs b/src/quat.rs index 87f61e4..9e90739 100644 --- a/src/quat.rs +++ b/src/quat.rs @@ -13,7 +13,7 @@ use core::ptr::to_unsafe_ptr; use core::sys::size_of; use core::vec::raw::buf_as_slice; -use std::cmp::FuzzyEq; +use std::cmp::{FuzzyEq, FUZZY_EPSILON}; use numeric::*; use numeric::number::Number; use numeric::number::Number::{zero,one}; @@ -45,7 +45,7 @@ use vec::{ #[deriving_eq] pub struct Quat { s: T, v: Vec3 } -pub impl Quat { +pub impl> Quat { /** * Construct the quaternion from one scalar component and three * imaginary components @@ -397,20 +397,25 @@ pub impl Quat: Index { } } -pub impl Quat: Neg> { +pub impl> Quat: Neg> { #[inline(always)] pure fn neg(&self) -> Quat { Quat::new(-self[0], -self[1], -self[2], -self[3]) } } -pub impl Quat: FuzzyEq { +pub impl> Quat: FuzzyEq { #[inline(always)] pure fn fuzzy_eq(&self, other: &Quat) -> bool { - self[0].fuzzy_eq(&other[0]) && - self[1].fuzzy_eq(&other[1]) && - self[2].fuzzy_eq(&other[2]) && - self[3].fuzzy_eq(&other[3]) + self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON)) + } + + #[inline(always)] + pure fn fuzzy_eq_eps(&self, other: &Quat, epsilon: &T) -> bool { + self[0].fuzzy_eq_eps(&other[0], epsilon) && + self[1].fuzzy_eq_eps(&other[1], epsilon) && + self[2].fuzzy_eq_eps(&other[2], epsilon) && + self[3].fuzzy_eq_eps(&other[3], epsilon) } } diff --git a/src/vec.rs b/src/vec.rs index c6af9a5..93e072b 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -407,4 +407,78 @@ pub trait BooleanVector: Vector { * the component-wise logical complement */ pure fn not(&self) -> Self; +} + +pub trait TrigVec: Vector { + pure fn radians(&self) -> Self; + pure fn degrees(&self) -> Self; + + // Triganometric functions + pure fn sin(&self) -> Self; + pure fn cos(&self) -> Self; + pure fn tan(&self) -> Self; + + // Inverse triganometric functions + pure fn asin(&self) -> Self; + pure fn acos(&self) -> Self; + pure fn atan(&self) -> Self; + pure fn atan2(&self, other: Self) -> Self; + + // Hyperbolic triganometric functions + pure fn sinh(&self) -> Self; + pure fn cosh(&self) -> Self; + pure fn tanh(&self) -> Self; + // pure fn asinh() -> Self; + // pure fn acosh() -> Self; + // pure fn atanh() -> Self; +} + +pub trait ExpVec: Vector { + // Exponential functions + pure fn pow_t(&self, n: Self) -> Self; + pure fn pow_v(&self, n: T) -> Self; + pure fn exp(&self) -> Self; + pure fn exp2(&self) -> Self; + pure fn ln(&self) -> Self; + pure fn ln2(&self) -> Self; + pure fn sqrt(&self) -> Self; + pure fn inv_sqrt(&self) -> Self; +} + +pub trait ApproxVec: Vector { + // Whole-number approximation functions + pure fn floor(&self) -> Self; + pure fn trunc(&self) -> Self; + pure fn round(&self) -> Self; + // pure fn round_even(&self) -> Self; + pure fn ceil(&self) -> Self; + pure fn fract(&self) -> Self; +} + +pub trait SignedVec: Vector { + pure fn is_positive(&self) -> BV; + pure fn is_negative(&self) -> BV; + pure fn is_nonpositive(&self) -> BV; + pure fn is_nonnegative(&self) -> BV; + + pure fn abs(&self) -> Self; + pure fn sign(&self) -> Self; + pure fn copysign(&self, other: Self) -> Self; +} + +pub trait ExtentVec: Vector { + 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; + + pure fn min_t(&self, other: T) -> Self; + pure fn max_t(&self, other: T) -> Self; + pure fn clamp_t(&self, mn: T, mx: T) -> Self; +} + +pub trait MixVec: Vector { + // Functions for blending numbers together + pure fn mix(&self, other: Self, value: Self) -> Self; + pure fn smooth_step(&self, edge0: Self, edge1: Self) -> Self; + pure fn step(&self, edge: Self) -> Self; } \ No newline at end of file diff --git a/src/vec2.rs b/src/vec2.rs index 65d1035..39125e7 100644 --- a/src/vec2.rs +++ b/src/vec2.rs @@ -5,8 +5,9 @@ use core::sys::size_of; use core::util::swap; use core::vec::raw::buf_as_slice; -use std::cmp::FuzzyEq; +use std::cmp::{FuzzyEq, FUZZY_EPSILON}; use numeric::*; +use numeric::number::Number; use numeric::number::Number::{zero,one}; use vec::{ @@ -284,11 +285,16 @@ pub impl Vec2: MutableEuclideanVector<&self/T> { } } -pub impl Vec2: FuzzyEq { +pub impl> Vec2: FuzzyEq { #[inline(always)] pure fn fuzzy_eq(&self, other: &Vec2) -> bool { - self[0].fuzzy_eq(&other[0]) && - self[1].fuzzy_eq(&other[1]) + self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON)) + } + + #[inline(always)] + pure fn fuzzy_eq_eps(&self, other: &Vec2, epsilon: &T) -> bool { + self[0].fuzzy_eq_eps(&other[0], epsilon) && + self[1].fuzzy_eq_eps(&other[1], epsilon) } } diff --git a/src/vec3.rs b/src/vec3.rs index 9b1aa08..e2b962b 100644 --- a/src/vec3.rs +++ b/src/vec3.rs @@ -5,8 +5,9 @@ use core::sys::size_of; use core::util::swap; use core::vec::raw::buf_as_slice; -use std::cmp::FuzzyEq; +use std::cmp::{FuzzyEq, FUZZY_EPSILON}; use numeric::*; +use numeric::number::Number; use numeric::number::Number::{zero,one}; use vec::{ @@ -316,12 +317,17 @@ pub impl Vec3: MutableEuclideanVector<&self/T> { } } -pub impl Vec3: FuzzyEq { +pub impl> Vec3: FuzzyEq { #[inline(always)] pure fn fuzzy_eq(&self, other: &Vec3) -> bool { - self[0].fuzzy_eq(&other[0]) && - self[1].fuzzy_eq(&other[1]) && - self[2].fuzzy_eq(&other[2]) + self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON)) + } + + #[inline(always)] + pure fn fuzzy_eq_eps(&self, other: &Vec3, epsilon: &T) -> bool { + self[0].fuzzy_eq_eps(&other[0], epsilon) && + self[1].fuzzy_eq_eps(&other[1], epsilon) && + self[2].fuzzy_eq_eps(&other[2], epsilon) } } diff --git a/src/vec4.rs b/src/vec4.rs index 5b5c1f2..7a545b7 100644 --- a/src/vec4.rs +++ b/src/vec4.rs @@ -5,8 +5,9 @@ use core::sys::size_of; use core::util::swap; use core::vec::raw::buf_as_slice; -use std::cmp::FuzzyEq; +use std::cmp::{FuzzyEq, FUZZY_EPSILON}; use numeric::*; +use numeric::number::Number; use numeric::number::Number::{zero,one}; use vec::{ @@ -314,13 +315,18 @@ pub impl Vec4: MutableEuclideanVector<&self/T> { } } -pub impl Vec4: FuzzyEq { +pub impl> Vec4: FuzzyEq { #[inline(always)] pure fn fuzzy_eq(&self, other: &Vec4) -> bool { - self[0].fuzzy_eq(&other[0]) && - self[1].fuzzy_eq(&other[1]) && - self[2].fuzzy_eq(&other[2]) && - self[3].fuzzy_eq(&other[3]) + self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON)) + } + + #[inline(always)] + pure fn fuzzy_eq_eps(&self, other: &Vec4, epsilon: &T) -> bool { + self[0].fuzzy_eq_eps(&other[0], epsilon) && + self[1].fuzzy_eq_eps(&other[1], epsilon) && + self[2].fuzzy_eq_eps(&other[2], epsilon) && + self[3].fuzzy_eq_eps(&other[3], epsilon) } }