From 449705b760b40b4b7031bd4bce44dd9b44a4bf4d Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Tue, 2 Apr 2013 11:25:05 +1100 Subject: [PATCH] Use core::num::{Zero, One, NumCast} traits for 'zero', 'one' and 'cast' methods --- src/mat.rs | 39 ++++++++++++++++++++------------------- src/projection.rs | 12 ++++++------ src/quat.rs | 26 ++++++++++++++------------ src/vec.rs | 44 +++++++++++++++++++++++--------------------- 4 files changed, 63 insertions(+), 58 deletions(-) diff --git a/src/mat.rs b/src/mat.rs index c4abfe1..12fe973 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -1,7 +1,8 @@ +use core::num::{Zero, One}; +use core::num::Zero::zero; +use core::num::One::one; use std::cmp::{FuzzyEq, FUZZY_EPSILON}; use numeric::*; -use numeric::number::Number; -use numeric::number::Number::{zero,one}; use vec::*; use quat::Quat; @@ -312,7 +313,7 @@ pub trait Matrix4: Matrix { #[deriving(Eq)] pub struct Mat2 { x: Vec2, y: Vec2 } -impl + Add + Sub + Mul + Div + Neg> Matrix> for Mat2 { +impl + Add + Sub + Mul + Div + Neg> Matrix> for Mat2 { #[inline(always)] fn col(&self, i: uint) -> Vec2 { self[i] } @@ -537,7 +538,7 @@ impl + Add + Sub + Mul + Div + N } } -impl + Add + Sub + Mul + Div + Neg> Matrix2> for Mat2 { +impl + Add + Sub + Mul + Div + Neg> Matrix2> for Mat2 { /** * Construct a 2 x 2 matrix * @@ -645,17 +646,17 @@ impl Index> for Mat2 { } } -impl + Add + Sub + Mul + Div + Neg> Neg> for Mat2 { +impl + Add + Sub + Mul + Div + Neg> Neg> for Mat2 { #[inline(always)] fn neg(&self) -> Mat2 { Matrix2::from_cols(-self[0], -self[1]) } } -impl> FuzzyEq for Mat2 { +impl> FuzzyEq for Mat2 { #[inline(always)] fn fuzzy_eq(&self, other: &Mat2) -> bool { - self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON)) + self.fuzzy_eq_eps(other, &num::cast(FUZZY_EPSILON)) } #[inline(always)] @@ -723,7 +724,7 @@ mat2_type!(Mat2f64) #[deriving(Eq)] pub struct Mat3 { x: Vec3, y: Vec3, z: Vec3 } -impl + Add + Sub + Mul + Div + Neg> Matrix> for Mat3 { +impl + Add + Sub + Mul + Div + Neg> Matrix> for Mat3 { #[inline(always)] fn col(&self, i: uint) -> Vec3 { self[i] } @@ -997,7 +998,7 @@ impl + Add + Sub + Mul + Div + N } } -impl + Add + Sub + Mul + Div + Neg> Matrix3> for Mat3 { +impl + Add + Sub + Mul + Div + Neg> Matrix3> for Mat3 { /** * Construct a 3 x 3 matrix * @@ -1187,8 +1188,8 @@ impl + Add + Sub + Mul + Div + N let w, x, y, z; let trace = self.trace(); - let _1: T = Number::from(1.0); - let half: T = Number::from(0.5); + let _1: T = num::cast(1.0); + let half: T = num::cast(0.5); if trace >= zero() { s = (_1 + trace).sqrt(); @@ -1231,17 +1232,17 @@ impl Index> for Mat3 { } } -impl + Add + Sub + Mul + Div + Neg> Neg> for Mat3 { +impl + Add + Sub + Mul + Div + Neg> Neg> for Mat3 { #[inline(always)] fn neg(&self) -> Mat3 { Matrix3::from_cols(-self[0], -self[1], -self[2]) } } -impl + Add + Sub + Mul + Div + Neg> FuzzyEq for Mat3 { +impl + Add + Sub + Mul + Div + Neg> FuzzyEq for Mat3 { #[inline(always)] fn fuzzy_eq(&self, other: &Mat3) -> bool { - self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON)) + self.fuzzy_eq_eps(other, &num::cast(FUZZY_EPSILON)) } #[inline(always)] @@ -1314,7 +1315,7 @@ mat3_type!(Mat3f64) #[deriving(Eq)] pub struct Mat4 { x: Vec4, y: Vec4, z: Vec4, w: Vec4 } -impl + Add + Sub + Mul + Div + Neg> Matrix> for Mat4 { +impl + Add + Sub + Mul + Div + Neg> Matrix> for Mat4 { #[inline(always)] fn col(&self, i: uint) -> Vec4 { self[i] } @@ -1687,7 +1688,7 @@ impl + Add + Sub + Mul + Div + N } } -impl + Add + Sub + Mul + Div + Neg> Matrix4> for Mat4 { +impl + Add + Sub + Mul + Div + Neg> Matrix4> for Mat4 { /** * Construct a 4 x 4 matrix * @@ -1754,7 +1755,7 @@ impl + Add + Sub + Mul + Div + N } } -impl + Add + Sub + Mul + Div + Neg> Neg> for Mat4 { +impl + Add + Sub + Mul + Div + Neg> Neg> for Mat4 { #[inline(always)] fn neg(&self) -> Mat4 { Matrix4::from_cols(-self[0], -self[1], -self[2], -self[3]) @@ -1768,10 +1769,10 @@ impl Index> for Mat4 { } } -impl> FuzzyEq for Mat4 { +impl> FuzzyEq for Mat4 { #[inline(always)] fn fuzzy_eq(&self, other: &Mat4) -> bool { - self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON)) + self.fuzzy_eq_eps(other, &num::cast(FUZZY_EPSILON)) } #[inline(always)] diff --git a/src/projection.rs b/src/projection.rs index df1faca..c5803ba 100644 --- a/src/projection.rs +++ b/src/projection.rs @@ -1,5 +1,5 @@ +use core::num::{Zero, One}; use numeric::*; -use numeric::number::Number; use std::cmp::FuzzyEq; @@ -12,7 +12,7 @@ use mat::{Mat4, Matrix4}; * can be found [here](http://www.opengl.org/wiki/GluPerspective_code). */ #[inline(always)] -pub fn perspective + Add + Sub + Mul + Div + Neg>(fovy: T, aspectRatio: T, near: T, far: T) -> Mat4 { +pub fn perspective + Add + Sub + Mul + Div + Neg>(fovy: T, aspectRatio: T, near: T, far: T) -> Mat4 { let ymax = near * tan(radians(fovy)); let xmax = ymax * aspectRatio; @@ -26,10 +26,10 @@ pub fn perspective + Add + Sub + Mul * (http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml) function. */ #[inline(always)] -pub fn frustum + Add + Sub + Mul + Div + Neg>(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); +pub fn frustum + Add + Sub + Mul + Div + Neg>(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4 { + let _0: T = num::cast(0); + let _1: T = num::cast(1); + let _2: T = num::cast(2); let c0r0 = (_2 * near) / (right - left); let c0r1 = _0; diff --git a/src/quat.rs b/src/quat.rs index 69704e7..eafd131 100644 --- a/src/quat.rs +++ b/src/quat.rs @@ -7,10 +7,12 @@ * Sir William Hamilton */ +use core::num::{Zero, One}; +use core::num::Zero::zero; +use core::num::One::one; use std::cmp::{FuzzyEq, FUZZY_EPSILON}; use numeric::*; use numeric::number::Number; -use numeric::number::Number::{zero,one}; use mat::{Mat3, Matrix3}; use vec::{Vec3, Vector3, EuclideanVector}; @@ -32,7 +34,7 @@ use vec::{vec3, dvec3, Vec3f, Vec3f32, Vec3f64}; #[deriving(Eq)] pub struct Quat { s: T, v: Vec3 } -pub impl + Add + Sub + Mul + Div + Neg> Quat { +pub impl + Add + Sub + Mul + Div + Neg> Quat { /** * Construct the quaternion from one scalar component and three * imaginary components @@ -84,26 +86,26 @@ pub impl + Add + Sub + Mul + Div #[inline(always)] fn from_angle_x(radians: T) -> Quat { - let _2 = Number::from(2); + let _2 = num::cast(2); Quat::new(cos(radians / _2), sin(radians), zero(), zero()) } #[inline(always)] fn from_angle_y(radians: T) -> Quat { - let _2 = Number::from(2); + let _2 = num::cast(2); Quat::new(cos(radians / _2), zero(), sin(radians), zero()) } #[inline(always)] fn from_angle_z(radians: T) -> Quat { - let _2 = Number::from(2); + let _2 = num::cast(2); Quat::new(cos(radians / _2), zero(), zero(), sin(radians)) } #[inline(always)] fn from_angle_xyz(radians_x: T, radians_y: T, radians_z: T) -> Quat { // http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles#Conversion - let _2 = Number::from(2); + let _2 = num::cast(2); let xdiv2 = radians_x / _2; let ydiv2 = radians_y / _2; let zdiv2 = radians_z / _2; @@ -115,7 +117,7 @@ pub impl + Add + Sub + Mul + Div #[inline(always)] fn from_angle_axis(radians: T, axis: &Vec3) -> Quat { - let half = radians / Number::from(2); + let half = radians / num::cast(2); Quat::from_sv(cos(half), axis.mul_t(sin(half))) } @@ -167,7 +169,7 @@ pub impl + Add + Sub + Mul + Div #[inline(always)] fn mul_v(&self, vec: &Vec3) -> Vec3 { let tmp = self.v.cross(vec).add_v(&vec.mul_t(self.s)); - self.v.cross(&tmp).mul_t(Number::from(2)).add_v(vec) + self.v.cross(&tmp).mul_t(num::cast(2)).add_v(vec) } /** @@ -314,7 +316,7 @@ pub impl + Add + Sub + Mul + Div fn slerp(&self, other: &Quat, amount: T) -> Quat { let dot = self.dot(other); - let dot_threshold = Number::from(0.9995); + let dot_threshold = num::cast(0.9995); if dot > dot_threshold { return self.nlerp(other, amount); // if quaternions are close together use `nlerp` @@ -377,17 +379,17 @@ impl Index for Quat { } } -impl + Add + Sub + Mul + Div + Neg> Neg> for Quat { +impl + Add + Sub + Mul + Div + Neg> Neg> for Quat { #[inline(always)] fn neg(&self) -> Quat { Quat::new(-self[0], -self[1], -self[2], -self[3]) } } -impl> FuzzyEq for Quat { +impl> FuzzyEq for Quat { #[inline(always)] fn fuzzy_eq(&self, other: &Quat) -> bool { - self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON)) + self.fuzzy_eq_eps(other, &num::cast(FUZZY_EPSILON)) } #[inline(always)] diff --git a/src/vec.rs b/src/vec.rs index 9e9ecb5..ca94fb6 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -1,7 +1,9 @@ +use core::num::{Zero, One}; +use core::num::Zero::zero; +use core::num::One::one; use std::cmp::{FuzzyEq, FUZZY_EPSILON}; use numeric::*; use numeric::number::Number; -use numeric::number::Number::{zero,one}; /** * The base generic vector trait. @@ -561,7 +563,7 @@ impl Index for Vec2 { } } -impl + Sub + Mul + Div + Neg> NumericVector for Vec2 { +impl + Sub + Mul + Div + Neg> NumericVector for Vec2 { #[inline(always)] fn identity() -> Vec2 { Vector2::new(one::(), one::()) @@ -651,14 +653,14 @@ impl + Sub + Mul + Div + Neg> Numer } } -impl + Sub + Mul + Div + Neg> Neg> for Vec2 { +impl + Sub + Mul + Div + Neg> Neg> for Vec2 { #[inline(always)] fn neg(&self) -> Vec2 { Vector2::new(-self[0], -self[1]) } } -impl + Sub + Mul + Div + Neg> NumericVector2 for Vec2 { +impl + Sub + Mul + Div + Neg> NumericVector2 for Vec2 { #[inline(always)] fn unit_x() -> Vec2 { Vector2::new(one::(), zero::()) @@ -675,14 +677,14 @@ impl + Sub + Mul + Div + Neg> Numer } } -impl ToHomogeneous> for Vec2 { +impl ToHomogeneous> for Vec2 { #[inline(always)] fn to_homogeneous(&self) -> Vec3 { Vector3::new(self.x, self.y, zero()) } } -impl + Sub + Mul + Div + Neg> EuclideanVector for Vec2 { +impl + Sub + Mul + Div + Neg> EuclideanVector for Vec2 { #[inline(always)] fn length2(&self) -> T { self.dot(self) @@ -741,10 +743,10 @@ impl + Sub + Mul + Div + Neg> Euclid } } -impl> FuzzyEq for Vec2 { +impl> FuzzyEq for Vec2 { #[inline(always)] fn fuzzy_eq(&self, other: &Vec2) -> bool { - self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON)) + self.fuzzy_eq_eps(other, &num::cast(FUZZY_EPSILON)) } #[inline(always)] @@ -940,7 +942,7 @@ impl Index for Vec3 { } } -impl + Sub + Mul + Div + Neg> NumericVector for Vec3 { +impl + Sub + Mul + Div + Neg> NumericVector for Vec3 { #[inline(always)] fn identity() -> Vec3 { Vector3::new(one::(), one::(), one::()) @@ -1033,14 +1035,14 @@ impl + Sub + Mul + Div + Neg> Numer } } -impl + Sub + Mul + Div + Neg> Neg> for Vec3 { +impl + Sub + Mul + Div + Neg> Neg> for Vec3 { #[inline(always)] fn neg(&self) -> Vec3 { Vector3::new(-self[0], -self[1], -self[2]) } } -impl + Sub + Mul + Div + Neg> NumericVector3 for Vec3 { +impl + Sub + Mul + Div + Neg> NumericVector3 for Vec3 { #[inline(always)] fn unit_x() -> Vec3 { Vector3::new(one::(), zero::(), zero::()) @@ -1069,14 +1071,14 @@ impl + Sub + Mul + Div + Neg> Numer } } -impl + Sub + Mul + Div + Neg> ToHomogeneous> for Vec3 { +impl + Sub + Mul + Div + Neg> ToHomogeneous> for Vec3 { #[inline(always)] fn to_homogeneous(&self) -> Vec4 { Vector4::new(self.x, self.y, self.z, zero()) } } -impl + Sub + Mul + Div + Neg> EuclideanVector for Vec3 { +impl + Sub + Mul + Div + Neg> EuclideanVector for Vec3 { #[inline(always)] fn length2(&self) -> T { self.dot(self) @@ -1135,10 +1137,10 @@ impl + Sub + Mul + Div + Neg> Euclid } } -impl> FuzzyEq for Vec3 { +impl> FuzzyEq for Vec3 { #[inline(always)] fn fuzzy_eq(&self, other: &Vec3) -> bool { - self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON)) + self.fuzzy_eq_eps(other, &num::cast(FUZZY_EPSILON)) } #[inline(always)] @@ -1338,7 +1340,7 @@ impl Index for Vec4 { } } -impl + Sub + Mul + Div + Neg> NumericVector for Vec4 { +impl + Sub + Mul + Div + Neg> NumericVector for Vec4 { #[inline(always)] fn identity() -> Vec4 { Vector4::new(one::(), one::(), one::(), one::()) @@ -1434,14 +1436,14 @@ impl + Sub + Mul + Div + Neg> Numer } } -impl + Sub + Mul + Div + Neg> Neg> for Vec4 { +impl + Sub + Mul + Div + Neg> Neg> for Vec4 { #[inline(always)] fn neg(&self) -> Vec4 { Vector4::new(-self[0], -self[1], -self[2], -self[3]) } } -impl NumericVector4 for Vec4 { +impl NumericVector4 for Vec4 { #[inline(always)] fn unit_x() -> Vec4 { Vector4::new(one::(), zero::(), zero::(), zero::()) @@ -1463,7 +1465,7 @@ impl NumericVector4 for Vec4 { } } -impl + Sub + Mul + Div + Neg> EuclideanVector for Vec4 { +impl + Sub + Mul + Div + Neg> EuclideanVector for Vec4 { #[inline(always)] fn length2(&self) -> T { self.dot(self) @@ -1522,10 +1524,10 @@ impl + Sub + Mul + Div + Neg> Euclid } } -impl> FuzzyEq for Vec4 { +impl> FuzzyEq for Vec4 { #[inline(always)] fn fuzzy_eq(&self, other: &Vec4) -> bool { - self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON)) + self.fuzzy_eq_eps(other, &num::cast(FUZZY_EPSILON)) } #[inline(always)]