diff --git a/src/mat2.rs b/src/mat2.rs index 00be834..be939f1 100644 --- a/src/mat2.rs +++ b/src/mat2.rs @@ -7,7 +7,7 @@ use std::cmp::FuzzyEq; use numeric::funs::*; use numeric::types::angle::Angle; use numeric::types::float::Float; -use numeric::types::number::Number; +use numeric::types::number::Number::{one, zero}; use vec::Vec2; @@ -93,9 +93,8 @@ pub impl Mat2 { */ #[inline(always)] static pure fn from_value(value: T) -> Mat2 { - let _0 = Number::from(0); - Mat2::new(value, _0, - _0, value) + Mat2::new(value, zero(), + zero(), value) } // FIXME: An interim solution to the issues with static functions @@ -140,10 +139,8 @@ pub impl Mat2: Matrix> { */ #[inline(always)] static pure fn identity() -> Mat2 { - let _0 = Number::from(0); - let _1 = Number::from(1); - Mat2::new(_1, _0, - _0, _1) + Mat2::new(one(), zero(), + zero(), one()) } /** @@ -159,9 +156,8 @@ pub impl Mat2: Matrix> { */ #[inline(always)] static pure fn zero() -> Mat2 { - let _0 = Number::from(0); - Mat2::new(_0, _0, - _0, _0) + Mat2::new(zero(), zero(), + zero(), zero()) } #[inline(always)] @@ -209,7 +205,7 @@ pub impl Mat2: Matrix> { #[inline(always)] pure fn inverse(&self) -> Option> { let d = self.determinant(); - if d.fuzzy_eq(&Number::from(0)) { + if d.fuzzy_eq(&zero()) { None } else { Some(Mat2::new( self[1][1]/d, -self[0][1]/d, @@ -231,9 +227,8 @@ pub impl Mat2: Matrix> { #[inline(always)] pure fn is_diagonal(&self) -> bool { - let _0 = Number::from(0); - self[0][1].fuzzy_eq(&_0) && - self[1][0].fuzzy_eq(&_0) + self[0][1].fuzzy_eq(&zero()) && + self[1][0].fuzzy_eq(&zero()) } #[inline(always)] @@ -250,7 +245,7 @@ pub impl Mat2: Matrix> { #[inline(always)] pure fn is_invertible(&self) -> bool { - !self.determinant().fuzzy_eq(&Number::from(0)) + !self.determinant().fuzzy_eq(&zero()) } #[inline(always)] @@ -336,21 +331,17 @@ pub impl Mat2: MutableMatrix> { pub impl Mat2: Matrix2> { #[inline(always)] pure fn to_mat3(&self) -> Mat3 { - let _0 = Number::from(0); - let _1 = Number::from(1); - Mat3::new(self[0][0], self[0][1], _0, - self[1][0], self[1][1], _0, - _0, _0, _1) + Mat3::new(self[0][0], self[0][1], zero(), + self[1][0], self[1][1], zero(), + zero(), zero(), one()) } #[inline(always)] pure fn to_mat4(&self) -> Mat4 { - let _0 = Number::from(0); - let _1 = Number::from(1); - Mat4::new(self[0][0], self[0][1], _0, _0, - self[1][0], self[1][1], _0, _0, - _0, _0, _1, _0, - _0, _0, _0, _1) + Mat4::new(self[0][0], self[0][1], zero(), zero(), + self[1][0], self[1][1], zero(), zero(), + zero(), zero(), one(), zero(), + zero(), zero(), zero(), one()) } } diff --git a/src/mat3.rs b/src/mat3.rs index b118492..0fe4893 100644 --- a/src/mat3.rs +++ b/src/mat3.rs @@ -8,6 +8,7 @@ use numeric::funs::*; use numeric::types::angle::Angle; use numeric::types::float::Float; use numeric::types::number::Number; +use numeric::types::number::Number::{one, zero}; use quat::Quat; use rot::Rotation; @@ -106,10 +107,9 @@ pub impl Mat3 { */ #[inline(always)] static pure fn from_value(value: T) -> Mat3 { - let _0 = Number::from(0); - Mat3::new(value, _0, _0, - _0, value, _0, - _0, _0, value) + Mat3::new(value, zero(), zero(), + zero(), value, zero(), + zero(), zero(), value) } /// Wrapper method for `Matrix::identity` @@ -129,12 +129,10 @@ pub impl Mat3 { // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations let cos_theta = cos(&theta.to_radians()); let sin_theta = sin(&theta.to_radians()); - let _0 = Number::from(0); - let _1 = Number::from(1); - Mat3::new(_1, _0, _0, - _0, cos_theta, sin_theta, - _0, -sin_theta, cos_theta) + Mat3::new( one(), zero(), zero(), + zero(), cos_theta, sin_theta, + zero(), -sin_theta, cos_theta) } /** @@ -146,12 +144,10 @@ pub impl Mat3 { // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations let cos_theta = cos(&theta.to_radians()); let sin_theta = sin(&theta.to_radians()); - let _0 = Number::from(0); - let _1 = Number::from(1); - Mat3::new(cos_theta, _0, -sin_theta, - _0, _1, _0, - sin_theta, _0, cos_theta) + Mat3::new(cos_theta, zero(), -sin_theta, + zero(), one(), zero(), + sin_theta, zero(), cos_theta) } /** @@ -163,12 +159,10 @@ pub impl Mat3 { // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations let cos_theta = cos(&theta.to_radians()); let sin_theta = sin(&theta.to_radians()); - let _0 = Number::from(0); - let _1 = Number::from(1); - Mat3::new( cos_theta, sin_theta, _0, - -sin_theta, cos_theta, _0, - _0, _0, _1) + Mat3::new( cos_theta, sin_theta, zero(), + -sin_theta, cos_theta, zero(), + zero(), zero(), one()) } /** @@ -202,11 +196,9 @@ pub impl Mat3 { // TODO: Move to Rotation implementation. See: https://github.com/mozilla/rust/issues/4306 #[inline(always)] static pure fn from_angle_axis>(theta: A, axis: &Vec3) -> Mat3 { - let c: T = cos(&theta.to_radians()); - let s: T = sin(&theta.to_radians()); - let _0: T = Number::from(0); - let _1: T = Number::from(1); - let _1_c: T = _1 - c; + let c = cos(&theta.to_radians()); + let s = sin(&theta.to_radians()); + let _1_c = one::() - c; let x = axis.x; let y = axis.y; @@ -261,7 +253,7 @@ pub impl Mat3 { let _1: T = Number::from(1.0); let half: T = Number::from(0.5); - if trace >= Number::from(0) { + if trace >= zero() { s = (_1 + trace).sqrt(); w = half * s; s = half / s; @@ -321,11 +313,9 @@ pub impl Mat3: Matrix> { */ #[inline(always)] static pure fn identity() -> Mat3 { - let _0 = Number::from(0); - let _1 = Number::from(1); - Mat3::new(_1, _0, _0, - _0, _1, _0, - _0, _0, _1) + Mat3::new( one(), zero(), zero(), + zero(), one(), zero(), + zero(), zero(), one()) } /** @@ -343,10 +333,9 @@ pub impl Mat3: Matrix> { */ #[inline(always)] static pure fn zero() -> Mat3 { - let _0 = Number::from(0); - Mat3::new(_0, _0, _0, - _0, _0, _0, - _0, _0, _0) + Mat3::new(zero(), zero(), zero(), + zero(), zero(), zero(), + zero(), zero(), zero()) } #[inline(always)] @@ -407,7 +396,7 @@ pub impl Mat3: Matrix> { // #[inline(always)] pure fn inverse(&self) -> Option> { let d = self.determinant(); - if d.fuzzy_eq(&Number::from(0)) { + if d.fuzzy_eq(&zero()) { None } else { Some(Mat3::from_cols(self[1].cross(&self[2]).div_t(d), @@ -431,15 +420,14 @@ pub impl Mat3: Matrix> { #[inline(always)] pure fn is_diagonal(&self) -> bool { - let _0 = Number::from(0); - self[0][1].fuzzy_eq(&_0) && - self[0][2].fuzzy_eq(&_0) && + self[0][1].fuzzy_eq(&zero()) && + self[0][2].fuzzy_eq(&zero()) && - self[1][0].fuzzy_eq(&_0) && - self[1][2].fuzzy_eq(&_0) && + self[1][0].fuzzy_eq(&zero()) && + self[1][2].fuzzy_eq(&zero()) && - self[2][0].fuzzy_eq(&_0) && - self[2][1].fuzzy_eq(&_0) + self[2][0].fuzzy_eq(&zero()) && + self[2][1].fuzzy_eq(&zero()) } #[inline(always)] @@ -462,7 +450,7 @@ pub impl Mat3: Matrix> { #[inline(always)] pure fn is_invertible(&self) -> bool { - !self.determinant().fuzzy_eq(&Number::zero()) + !self.determinant().fuzzy_eq(&zero()) } #[inline(always)] @@ -559,12 +547,10 @@ pub impl Mat3: MutableMatrix> { pub impl Mat3: Matrix3> { #[inline(always)] pure fn to_mat4(&self) -> Mat4 { - let _0 = Number::from(0); - let _1 = Number::from(1); - Mat4::new(self[0][0], self[0][1], self[0][2], _0, - self[1][0], self[1][1], self[1][2], _0, - self[2][0], self[2][1], self[2][2], _0, - _0, _0, _0, _1) + Mat4::new(self[0][0], self[0][1], self[0][2], zero(), + self[1][0], self[1][1], self[1][2], zero(), + self[2][0], self[2][1], self[2][2], zero(), + zero(), zero(), zero(), one()) } } diff --git a/src/mat4.rs b/src/mat4.rs index 15ff6ad..315e21a 100644 --- a/src/mat4.rs +++ b/src/mat4.rs @@ -7,7 +7,7 @@ use std::cmp::FuzzyEq; use numeric::funs::*; use numeric::types::angle::Angle; use numeric::types::float::Float; -use numeric::types::number::Number; +use numeric::types::number::Number::{one, zero}; use vec::Vec4; @@ -116,11 +116,10 @@ pub impl Mat4 { */ #[inline(always)] static pure fn from_value(value: T) -> Mat4 { - let _0 = Number::from(0); - Mat4::new(value, _0, _0, _0, - _0, value, _0, _0, - _0, _0, value, _0, - _0, _0, _0, value) + Mat4::new(value, zero(), zero(), zero(), + zero(), value, zero(), zero(), + zero(), zero(), value, zero(), + zero(), zero(), zero(), value) } /// Wrapper method for `Matrix::identity` @@ -161,12 +160,10 @@ pub impl Mat4: Matrix> { */ #[inline(always)] static pure fn identity() -> Mat4 { - let _0 = Number::from(0); - let _1 = Number::from(1); - Mat4::new(_1, _0, _0, _0, - _0, _1, _0, _0, - _0, _0, _1, _0, - _0, _0, _0, _1) + Mat4::new( one(), zero(), zero(), zero(), + zero(), one(), zero(), zero(), + zero(), zero(), one(), zero(), + zero(), zero(), zero(), one()) } /** @@ -186,11 +183,10 @@ pub impl Mat4: Matrix> { */ #[inline(always)] static pure fn zero() -> Mat4 { - let _0 = Number::from(0); - Mat4::new(_0, _0, _0, _0, - _0, _0, _0, _0, - _0, _0, _0, _0, - _0, _0, _0, _0) + Mat4::new(zero(), zero(), zero(), zero(), + zero(), zero(), zero(), zero(), + zero(), zero(), zero(), zero(), + zero(), zero(), zero(), zero()) } #[inline(always)] @@ -274,7 +270,7 @@ pub impl Mat4: Matrix> { pure fn inverse(&self) -> Option> { let d = self.determinant(); - if d.fuzzy_eq(&Number::from(0)) { + if d.fuzzy_eq(&zero()) { None } else { @@ -335,22 +331,21 @@ pub impl Mat4: Matrix> { #[inline(always)] pure fn is_diagonal(&self) -> bool { - let _0 = Number::from(0); - self[0][1].fuzzy_eq(&_0) && - self[0][2].fuzzy_eq(&_0) && - self[0][3].fuzzy_eq(&_0) && + self[0][1].fuzzy_eq(&zero()) && + self[0][2].fuzzy_eq(&zero()) && + self[0][3].fuzzy_eq(&zero()) && - self[1][0].fuzzy_eq(&_0) && - self[1][2].fuzzy_eq(&_0) && - self[1][3].fuzzy_eq(&_0) && + self[1][0].fuzzy_eq(&zero()) && + self[1][2].fuzzy_eq(&zero()) && + self[1][3].fuzzy_eq(&zero()) && - self[2][0].fuzzy_eq(&_0) && - self[2][1].fuzzy_eq(&_0) && - self[2][3].fuzzy_eq(&_0) && + self[2][0].fuzzy_eq(&zero()) && + self[2][1].fuzzy_eq(&zero()) && + self[2][3].fuzzy_eq(&zero()) && - self[3][0].fuzzy_eq(&_0) && - self[3][1].fuzzy_eq(&_0) && - self[3][2].fuzzy_eq(&_0) + self[3][0].fuzzy_eq(&zero()) && + self[3][1].fuzzy_eq(&zero()) && + self[3][2].fuzzy_eq(&zero()) } #[inline(always)] @@ -380,7 +375,7 @@ pub impl Mat4: Matrix> { #[inline(always)] pure fn is_invertible(&self) -> bool { - !self.determinant().fuzzy_eq(&Number::zero()) + !self.determinant().fuzzy_eq(&zero()) } #[inline(always)] diff --git a/src/quat.rs b/src/quat.rs index e3bf2cf..549488d 100644 --- a/src/quat.rs +++ b/src/quat.rs @@ -18,6 +18,7 @@ use numeric::funs::*; use numeric::types::angle::Angle; use numeric::types::float::Float; use numeric::types::number::Number; +use numeric::types::number::Number::{one, zero}; use mat::{Mat3, Mat4}; use vec::Vec3; @@ -74,10 +75,7 @@ pub impl Quat { */ #[inline(always)] static pure fn identity() -> Quat { - Quat::new(Number::one(), - Number::zero(), - Number::zero(), - Number::zero()) + Quat::new(one(), zero(), zero(), zero()) } /** @@ -87,10 +85,7 @@ pub impl Quat { */ #[inline(always)] static pure fn zero() -> Quat { - Quat::new(Number::zero(), - Number::zero(), - Number::zero(), - Number::zero()) + Quat::new(zero(), zero(), zero(), zero()) } /** @@ -234,9 +229,7 @@ pub impl Quat { */ #[inline(always)] pure fn normalize(&self) -> Quat { - let mut n: T = Number::from(1); - n /= self.magnitude(); - return self.mul_t(n); + self.mul_t(one::()/self.magnitude()) } /** @@ -248,8 +241,7 @@ pub impl Quat { */ #[inline(always)] pure fn nlerp(&self, other: &Quat, amount: T) -> Quat { - let _1: T = Number::from(1); - self.mul_t(_1 - amount).add_q(&other.mul_t(amount)).normalize() + self.mul_t(one::() - amount).add_q(&other.mul_t(amount)).normalize() } /** @@ -282,8 +274,7 @@ pub impl Quat { if dot > dot_threshold { return self.nlerp(other, amount); // if quaternions are close together use `nlerp` } else { - let robust_dot = dot.clamp(&-Number::from(1), - &Number::from(1)); // stay within the domain of acos() + let robust_dot = dot.clamp(&-one(), &one()); // stay within the domain of acos() let theta_0 = acos(&robust_dot); // the angle between the quaternions let theta = theta_0 * amount; // the fraction of theta specified by `amount` @@ -312,28 +303,25 @@ pub impl Quat { // TODO: Move to Rotation implementation. See: https://github.com/mozilla/rust/issues/4306 #[inline(always)] static pure fn from_angle_x>(theta: A) -> Quat { - let _0 = Number::from(0); let _2 = Number::from(2); let rad = theta.to_radians(); - Quat::new((rad / _2).cos(), rad.sin(), _0, _0) + Quat::new((rad / _2).cos(), rad.sin(), zero(), zero()) } // TODO: Move to Rotation implementation. See: https://github.com/mozilla/rust/issues/4306 #[inline(always)] static pure fn from_angle_y>(theta: A) -> Quat { - let _0 = Number::from(0); let _2 = Number::from(2); let rad = theta.to_radians(); - Quat::new((rad / _2).cos(), _0, rad.sin(), _0) + Quat::new((rad / _2).cos(), zero(), rad.sin(), zero()) } // TODO: Move to Rotation implementation. See: https://github.com/mozilla/rust/issues/4306 #[inline(always)] static pure fn from_angle_z>(theta: A) -> Quat { - let _0 = Number::from(0); let _2 = Number::from(2); let rad = theta.to_radians(); - Quat::new((rad / _2).cos(), _0, _0, rad.sin()) + Quat::new((rad / _2).cos(), zero(), zero(), rad.sin()) } // TODO: Move to Rotation implementation. See: https://github.com/mozilla/rust/issues/4306 @@ -399,7 +387,7 @@ pub impl Quat { let sz2 = z2 * self.s; let sx2 = x2 * self.s; - let _1: T = Number::from(1); + let _1: T = one(); Mat3::new(_1 - yy2 - zz2, xy2 + sz2, xz2 - sy2, xy2 - sz2, _1 - xx2 - zz2, yz2 + sx2, diff --git a/src/vec2.rs b/src/vec2.rs index 26280ef..4bff30e 100644 --- a/src/vec2.rs +++ b/src/vec2.rs @@ -8,6 +8,7 @@ use numeric::funs::*; use numeric::types::angle::Radians; use numeric::types::float::Float; use numeric::types::number::Number; +use numeric::types::number::Number::{one, zero}; /** * A 2-dimensional vector @@ -75,20 +76,18 @@ pub impl Vec2: MutableVector { pub impl Vec2: NumericVector { #[inline(always)] static pure fn identity() -> Vec2 { - Vec2::new(Number::one(), - Number::one()) + Vec2::new(one(), one()) } #[inline(always)] static pure fn zero() -> Vec2 { - Vec2::new(Number::zero(), - Number::zero()) + Vec2::new(zero(), zero()) } #[inline(always)] pure fn is_zero(&self) -> bool { - self[0] == Number::zero() && - self[1] == Number::zero() + self[0] == zero() && + self[1] == zero() } #[inline(always)] @@ -220,15 +219,12 @@ pub impl Vec2: EuclideanVector { #[inline(always)] pure fn normalize(&self) -> Vec2 { - let mut n: T = Number::from(1); - n /= self.length(); - return self.mul_t(n); + self.mul_t(one::()/self.length()) } #[inline(always)] pure fn normalize_to(&self, length: T) -> Vec2 { - let mut n: T = length / self.length(); - return self.mul_t(n); + self.mul_t(length / self.length()) } #[inline(always)] @@ -240,14 +236,13 @@ pub impl Vec2: EuclideanVector { pub impl Vec2: MutableEuclideanVector<&self/T> { #[inline(always)] fn normalize_self(&mut self) { - let mut n: T = Number::from(1); - n /= self.length(); + let n = one::() / self.length(); self.mul_self_t(&n); } #[inline(always)] fn normalize_self_to(&mut self, length: &T) { - let mut n: T = length / self.length(); + let n = length / self.length(); self.mul_self_t(&n); } diff --git a/src/vec3.rs b/src/vec3.rs index 90593e4..1afaa93 100644 --- a/src/vec3.rs +++ b/src/vec3.rs @@ -8,6 +8,7 @@ use numeric::funs::*; use numeric::types::angle::Radians; use numeric::types::float::Float; use numeric::types::number::Number; +use numeric::types::number::Number::{one, zero}; /** * A 3-dimensional vector @@ -77,23 +78,19 @@ pub impl Vec3: MutableVector { pub impl Vec3: NumericVector { #[inline(always)] static pure fn identity() -> Vec3 { - Vec3::new(Number::one(), - Number::one(), - Number::one()) + Vec3::new(one(), one(), one()) } #[inline(always)] static pure fn zero() -> Vec3 { - Vec3::new(Number::zero(), - Number::zero(), - Number::zero()) + Vec3::new(zero(), zero(), zero()) } #[inline(always)] pure fn is_zero(&self) -> bool { - self[0] == Number::zero() && - self[1] == Number::zero() && - self[2] == Number::zero() + self[0] == zero() && + self[1] == zero() && + self[2] == zero() } #[inline(always)] @@ -248,15 +245,12 @@ pub impl Vec3: EuclideanVector { #[inline(always)] pure fn normalize(&self) -> Vec3 { - let mut n: T = Number::from(1); - n /= self.length(); - return self.mul_t(n); + self.mul_t(one::()/self.length()) } #[inline(always)] pure fn normalize_to(&self, length: T) -> Vec3 { - let mut n: T = length / self.length(); - return self.mul_t(n); + self.mul_t(length / self.length()) } #[inline(always)] @@ -268,14 +262,13 @@ pub impl Vec3: EuclideanVector { pub impl Vec3: MutableEuclideanVector<&self/T> { #[inline(always)] fn normalize_self(&mut self) { - let mut n: T = Number::from(1); - n /= self.length(); + let n = one::() / self.length(); self.mul_self_t(&n); } #[inline(always)] fn normalize_self_to(&mut self, length: &T) { - let mut n: T = length / self.length(); + let n = length / self.length(); self.mul_self_t(&n); } diff --git a/src/vec4.rs b/src/vec4.rs index 9dad13a..bd21fca 100644 --- a/src/vec4.rs +++ b/src/vec4.rs @@ -8,6 +8,7 @@ use numeric::funs::*; use numeric::types::angle::Radians; use numeric::types::float::Float; use numeric::types::number::Number; +use numeric::types::number::Number::{one, zero}; /** * A 4-dimensional vector @@ -79,18 +80,12 @@ pub impl Vec4: MutableVector { pub impl Vec4: NumericVector { #[inline(always)] static pure fn identity() -> Vec4 { - Vec4::new(Number::one(), - Number::one(), - Number::one(), - Number::one()) + Vec4::new(one(), one(), one(), one()) } #[inline(always)] static pure fn zero() -> Vec4 { - Vec4::new(Number::zero(), - Number::zero(), - Number::zero(), - Number::zero()) + Vec4::new(zero(), zero(), zero(), zero()) } #[inline(always)] @@ -251,15 +246,12 @@ pub impl Vec4: EuclideanVector { #[inline(always)] pure fn normalize(&self) -> Vec4 { - let mut n: T = Number::from(1); - n /= self.length(); - return self.mul_t(n); + self.mul_t(one::()/self.length()) } #[inline(always)] pure fn normalize_to(&self, length: T) -> Vec4 { - let mut n: T = length / self.length(); - return self.mul_t(n); + self.mul_t(length / self.length()) } #[inline(always)] @@ -271,14 +263,13 @@ pub impl Vec4: EuclideanVector { pub impl Vec4: MutableEuclideanVector<&self/T> { #[inline(always)] fn normalize_self(&mut self) { - let mut n: T = Number::from(1); - n /= self.length(); + let n = one::() / self.length(); self.mul_self_t(&n); } #[inline(always)] fn normalize_self_to(&mut self, length: &T) { - let mut n: T = length / self.length(); + let n = length / self.length(); self.mul_self_t(&n); }