diff --git a/src/aabb.rs b/src/aabb.rs index ebdeb35..307c02a 100644 --- a/src/aabb.rs +++ b/src/aabb.rs @@ -22,7 +22,7 @@ use std::fmt; -use rust_num::{Float, zero, one}; +use rust_num::{Float, Zero, One}; use bound::*; use point::{Point, Point2, Point3}; @@ -54,7 +54,7 @@ pub trait Aabb, P: Point>: Sized { /// Return the center point of this AABB. #[inline] fn center(&self) -> P { - let two = one::() + one::(); + let two = S::one() + S::one(); self.min().add_v(&self.dim().div_s(two)) } @@ -131,8 +131,8 @@ impl Aabb, Point2> for Aabb2 { fn contains(&self, p: &Point2) -> bool { let v_min = p.sub_p(self.min()); let v_max = self.max().sub_p(p); - v_min.x >= zero() && v_min.y >= zero() && - v_max.x > zero() && v_max.y > zero() + v_min.x >= S::zero() && v_min.y >= S::zero() && + v_max.x > S::zero() && v_max.y > S::zero() } } @@ -191,8 +191,8 @@ impl Aabb, Point3> for Aabb3 { fn contains(&self, p: &Point3) -> bool { let v_min = p.sub_p(self.min()); let v_max = self.max().sub_p(p); - v_min.x >= zero() && v_min.y >= zero() && v_min.z >= zero() && - v_max.x > zero() && v_max.y > zero() && v_max.z > zero() + v_min.x >= S::zero() && v_min.y >= S::zero() && v_min.z >= S::zero() && + v_max.x > S::zero() && v_max.y > S::zero() && v_max.z > S::zero() } } @@ -206,28 +206,28 @@ impl Intersect>> for (Ray2, Aabb2) { fn intersection(&self) -> Option> { let (ref ray, ref aabb) = *self; - let mut tmin: S = Float::neg_infinity(); - let mut tmax: S = Float::infinity(); + let mut tmin = S::neg_infinity(); + let mut tmax = S::infinity(); - if ray.direction.x != zero() { + if ray.direction.x != S::zero() { let tx1 = (aabb.min.x - ray.origin.x) / ray.direction.x; let tx2 = (aabb.max.x - ray.origin.x) / ray.direction.x; tmin = tmin.max(tx1.min(tx2)); tmax = tmax.min(tx1.max(tx2)); } - if ray.direction.y != zero() { + if ray.direction.y != S::zero() { let ty1 = (aabb.min.y - ray.origin.y) / ray.direction.y; let ty2 = (aabb.max.y - ray.origin.y) / ray.direction.y; tmin = tmin.max(ty1.min(ty2)); tmax = tmax.min(ty1.max(ty2)); } - if tmin < zero() && tmax < zero() { + if tmin < S::zero() && tmax < S::zero() { None } else if tmax >= tmin { - if tmin >= zero() { + if tmin >= S::zero() { Some(Point2::new(ray.origin.x + ray.direction.x * tmin, ray.origin.y + ray.direction.y * tmin)) } diff --git a/src/angle.rs b/src/angle.rs index 4b05b3e..0a0075b 100644 --- a/src/angle.rs +++ b/src/angle.rs @@ -22,7 +22,7 @@ use std::ops::*; use rand::{Rand, Rng}; use rand::distributions::range::SampleRange; -use rust_num::{Float, One, Zero, one, zero}; +use rust_num::{Float, One, Zero}; use rust_num::traits::cast; use approx::ApproxEq; @@ -132,15 +132,15 @@ pub trait Angle /// Normalize the angle to the range `[0, full_turn)`. #[inline] fn normalize_self(&mut self) { - let full_turn: Self = Angle::full_turn(); + let full_turn = Self::full_turn(); self.rem_self_s(full_turn.s().clone()); - if *self < zero() { self.add_self_a(full_turn) }; + if *self < Self::zero() { self.add_self_a(full_turn) }; } /// Return the angle rotated by half a turn #[inline] fn opposite(&self) -> Self { - self.add_a(Angle::turn_div_2()).normalize() + self.add_a(Self::turn_div_2()).normalize() } /// Returns the interior bisector of the two angles @@ -151,37 +151,16 @@ pub trait Angle fn full_turn() -> Self; - #[inline] fn turn_div_2() -> Self { let full_turn: Self = Angle::full_turn(); full_turn.div_s(cast(2i8).unwrap()) } - #[inline] fn turn_div_3() -> Self { let full_turn: Self = Angle::full_turn(); full_turn.div_s(cast(3i8).unwrap()) } - #[inline] fn turn_div_4() -> Self { let full_turn: Self = Angle::full_turn(); full_turn.div_s(cast(4i8).unwrap()) } - #[inline] fn turn_div_6() -> Self { let full_turn: Self = Angle::full_turn(); full_turn.div_s(cast(6i8).unwrap()) } + #[inline] fn turn_div_2() -> Self { Self::full_turn().div_s(cast(2i8).unwrap()) } + #[inline] fn turn_div_3() -> Self { Self::full_turn().div_s(cast(3i8).unwrap()) } + #[inline] fn turn_div_4() -> Self { Self::full_turn().div_s(cast(4i8).unwrap()) } + #[inline] fn turn_div_6() -> Self { Self::full_turn().div_s(cast(6i8).unwrap()) } #[inline] fn equiv(&self, other: &Self) -> bool { self.normalize() == other.normalize() } } #[inline] pub fn bisect>(a: A, b: A) -> A { a.bisect(b) } -impl -Rad { - #[inline] pub fn zero() -> Rad { zero() } - #[inline] pub fn full_turn() -> Rad { Angle::full_turn() } - #[inline] pub fn turn_div_2() -> Rad { Angle::turn_div_2() } - #[inline] pub fn turn_div_3() -> Rad { Angle::turn_div_3() } - #[inline] pub fn turn_div_4() -> Rad { Angle::turn_div_4() } - #[inline] pub fn turn_div_6() -> Rad { Angle::turn_div_6() } -} - -impl -Deg { - #[inline] pub fn zero() -> Deg { zero() } - #[inline] pub fn full_turn() -> Deg { Angle::full_turn() } - #[inline] pub fn turn_div_2() -> Deg { Angle::turn_div_2() } - #[inline] pub fn turn_div_3() -> Deg { Angle::turn_div_3() } - #[inline] pub fn turn_div_4() -> Deg { Angle::turn_div_4() } - #[inline] pub fn turn_div_6() -> Deg { Angle::turn_div_6() } -} - - impl>, S: BaseFloat> Add for Rad { type Output = Rad; @@ -225,15 +204,15 @@ impl Neg for Deg { impl Zero for Rad { #[inline] - fn zero() -> Rad { rad(zero()) } + fn zero() -> Rad { rad(S::zero()) } #[inline] - fn is_zero(&self) -> bool { *self == zero() } + fn is_zero(&self) -> bool { *self == Self::zero() } } impl Zero for Deg { #[inline] - fn zero() -> Deg { deg(zero()) } + fn zero() -> Deg { deg(S::zero()) } #[inline] - fn is_zero(&self) -> bool { *self == zero() } + fn is_zero(&self) -> bool { *self == Self::zero() } } impl>, S: BaseFloat> Mul for Rad { @@ -252,11 +231,11 @@ impl>, S: BaseFloat> Mul for Deg { impl One for Rad { #[inline] - fn one() -> Rad { rad(one()) } + fn one() -> Rad { rad(S::one()) } } impl One for Deg { #[inline] - fn one() -> Deg { deg(one()) } + fn one() -> Deg { deg(S::one()) } } const PI_2: f64 = f64::consts::PI * 2f64; diff --git a/src/line.rs b/src/line.rs index 3d20e45..5d8d2e1 100644 --- a/src/line.rs +++ b/src/line.rs @@ -17,7 +17,7 @@ use std::marker::PhantomData; -use rust_num::{Zero, zero, One, one}; +use rust_num::{Zero, One}; use num::{BaseNum, BaseFloat}; use point::{Point, Point2, Point3}; @@ -57,14 +57,13 @@ impl Intersect>> for (Ray2, Line2) { let q = line.origin; let r = ray.direction; let s = Vector2::new(line.dest.x - line.origin.x, line.dest.y - line.origin.y); - let zero: S = Zero::zero(); let cross_1 = r.perp_dot(&s); let qmp = Vector2::new(q.x - p.x, q.y - p.y); let cross_2 = qmp.perp_dot(&r); - if cross_1 == zero { - if cross_2 != zero { + if cross_1 == S::zero() { + if cross_2 != S::zero() { // parallel return None; } @@ -73,10 +72,10 @@ impl Intersect>> for (Ray2, Line2) { let q2mp = Vector2::new(line.dest.x - p.x, line.dest.y - p.y); let dot_1 = qmp.dot(&r); let dot_2 = q2mp.dot(&r); - if (dot_1 <= zero && dot_2 >= zero) || (dot_1 >= zero && dot_2 <= zero) { + if (dot_1 <= S::zero() && dot_2 >= S::zero()) || (dot_1 >= S::zero() && dot_2 <= S::zero()) { return Some(p); } - else if dot_1 >= zero && dot_2 >= zero { + else if dot_1 >= S::zero() && dot_2 >= S::zero() { if dot_1 <= dot_2 { return Some(q); } @@ -92,7 +91,7 @@ impl Intersect>> for (Ray2, Line2) { let t = qmp.perp_dot(&s) / cross_1; let u = cross_2 / cross_1; - if zero <= t && u >= zero && u <= One::one() { + if S::zero() <= t && u >= S::zero() && u <= S::one() { return Some(Point2::new(p.x + t*r.x, p.y + t*r.y)); } diff --git a/src/matrix.rs b/src/matrix.rs index 9eb9d8d..5d7f290 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -21,7 +21,7 @@ use std::ops::*; use rand::{Rand, Rng}; -use rust_num::{zero, one}; +use rust_num::{Zero, One}; use rust_num::traits::cast; use angle::{Rad, sin, cos, sin_cos}; @@ -122,27 +122,27 @@ impl Matrix3 { pub fn from_angle_x(theta: Rad) -> Matrix3 { // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations let (s, c) = sin_cos(theta); - Matrix3::new( one(), zero(), zero(), - zero(), c.clone(), s.clone(), - zero(), -s.clone(), c.clone()) + Matrix3::new(S::one(), S::zero(), S::zero(), + S::zero(), c.clone(), s.clone(), + S::zero(), -s.clone(), c.clone()) } /// Create a matrix from a rotation around the `y` axis (yaw). pub fn from_angle_y(theta: Rad) -> Matrix3 { // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations let (s, c) = sin_cos(theta); - Matrix3::new(c.clone(), zero(), -s.clone(), - zero(), one(), zero(), - s.clone(), zero(), c.clone()) + Matrix3::new(c.clone(), S::zero(), -s.clone(), + S::zero(), S::one(), S::zero(), + s.clone(), S::zero(), c.clone()) } /// Create a matrix from a rotation around the `z` axis (roll). pub fn from_angle_z(theta: Rad) -> Matrix3 { // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations let (s, c) = sin_cos(theta); - Matrix3::new( c.clone(), s.clone(), zero(), - -s.clone(), c.clone(), zero(), - zero(), zero(), one()) + Matrix3::new( c.clone(), s.clone(), S::zero(), + -s.clone(), c.clone(), S::zero(), + S::zero(), S::zero(), S::one()) } /// Create a matrix from a set of euler angles. @@ -166,7 +166,7 @@ impl Matrix3 { /// Create a matrix from a rotation around an arbitrary axis pub fn from_axis_angle(axis: &Vector3, angle: Rad) -> Matrix3 { let (s, c) = sin_cos(angle); - let _1subc = one::() - c; + let _1subc = S::one() - c; Matrix3::new(_1subc * axis.x * axis.x + c, _1subc * axis.x * axis.y + s * axis.z, @@ -216,10 +216,10 @@ impl Matrix4 { /// Create a translation matrix from a Vector3 #[inline] pub fn from_translation(v: &Vector3) -> Matrix4 { - Matrix4::new(one(), zero(), zero(), zero(), - zero(), one(), zero(), zero(), - zero(), zero(), one(), zero(), - v.x, v.y, v.z, one()) + Matrix4::new(S::one(), S::zero(), S::zero(), S::zero(), + S::zero(), S::one(), S::zero(), S::zero(), + S::zero(), S::zero(), S::one(), S::zero(), + v.x, v.y, v.z, S::one()) } } @@ -231,10 +231,10 @@ impl Matrix4 { let s = f.cross(up).normalize(); let u = s.cross(&f); - Matrix4::new( s.x.clone(), u.x.clone(), -f.x.clone(), zero(), - s.y.clone(), u.y.clone(), -f.y.clone(), zero(), - s.z.clone(), u.z.clone(), -f.z.clone(), zero(), - -eye.dot(&s), -eye.dot(&u), eye.dot(&f), one()) + Matrix4::new(s.x.clone(), u.x.clone(), -f.x.clone(), S::zero(), + s.y.clone(), u.y.clone(), -f.y.clone(), S::zero(), + s.z.clone(), u.z.clone(), -f.z.clone(), S::zero(), + -eye.dot(&s), -eye.dot(&u), eye.dot(&f), S::one()) } } @@ -259,10 +259,10 @@ pub trait Matrix + 'static>: Array2 /// Create a matrix with all elements equal to zero. #[inline] - fn zero() -> Self { Self::from_value(zero()) } + fn zero() -> Self { Self::from_value(S::zero()) } /// Create a matrix where the each element of the diagonal is equal to one. #[inline] - fn identity() -> Self { Self::from_value(one()) } + fn identity() -> Self { Self::from_value(S::one()) } /// Multiply this matrix by a scalar, returning the new matrix. #[must_use] @@ -334,7 +334,7 @@ pub trait Matrix + 'static>: Array2 /// Test if this matrix is invertible. #[inline] - fn is_invertible(&self) -> bool { !self.determinant().approx_eq(&zero()) } + fn is_invertible(&self) -> bool { !self.determinant().approx_eq(&S::zero()) } /// Test if this matrix is the identity matrix. That is, it is diagonal /// and every element in the diagonal is one. @@ -512,14 +512,14 @@ impl Array2, Vector4, S> for Matrix4 { impl Matrix> for Matrix2 { #[inline] fn from_value(value: S) -> Matrix2 { - Matrix2::new(value, zero(), - zero(), value) + Matrix2::new(value, S::zero(), + S::zero(), value) } #[inline] fn from_diagonal(value: &Vector2) -> Matrix2 { - Matrix2::new(value.x, zero(), - zero(), value.y) + Matrix2::new(value.x, S::zero(), + S::zero(), value.y) } #[inline] @@ -617,7 +617,7 @@ impl Matrix> for Matrix2 { #[inline] fn invert(&self) -> Option> { let det = self.determinant(); - if det.approx_eq(&zero()) { + if det.approx_eq(&S::zero()) { None } else { Some(Matrix2::new( self[1][1] / det, -self[0][1] / det, @@ -627,8 +627,8 @@ impl Matrix> for Matrix2 { #[inline] fn is_diagonal(&self) -> bool { - (&self[0][1]).approx_eq(&zero()) && - (&self[1][0]).approx_eq(&zero()) + (&self[0][1]).approx_eq(&S::zero()) && + (&self[1][0]).approx_eq(&S::zero()) } @@ -642,16 +642,16 @@ impl Matrix> for Matrix2 { impl Matrix> for Matrix3 { #[inline] fn from_value(value: S) -> Matrix3 { - Matrix3::new(value, zero(), zero(), - zero(), value, zero(), - zero(), zero(), value) + Matrix3::new(value, S::zero(), S::zero(), + S::zero(), value, S::zero(), + S::zero(), S::zero(), value) } #[inline] fn from_diagonal(value: &Vector3) -> Matrix3 { - Matrix3::new(value.x, zero(), zero(), - zero(), value.y, zero(), - zero(), zero(), value.z) + Matrix3::new(value.x, S::zero(), S::zero(), + S::zero(), value.y, S::zero(), + S::zero(), S::zero(), value.z) } #[inline] @@ -765,7 +765,7 @@ impl Matrix> for Matrix3 { fn invert(&self) -> Option> { let det = self.determinant(); - if det.approx_eq(&zero()) { None } else { + if det.approx_eq(&S::zero()) { None } else { Some(Matrix3::from_cols(self[1].cross(&self[2]).div_s(det), self[2].cross(&self[0]).div_s(det), self[0].cross(&self[1]).div_s(det)).transpose()) @@ -773,14 +773,14 @@ impl Matrix> for Matrix3 { } fn is_diagonal(&self) -> bool { - (&self[0][1]).approx_eq(&zero()) && - (&self[0][2]).approx_eq(&zero()) && + (&self[0][1]).approx_eq(&S::zero()) && + (&self[0][2]).approx_eq(&S::zero()) && - (&self[1][0]).approx_eq(&zero()) && - (&self[1][2]).approx_eq(&zero()) && + (&self[1][0]).approx_eq(&S::zero()) && + (&self[1][2]).approx_eq(&S::zero()) && - (&self[2][0]).approx_eq(&zero()) && - (&self[2][1]).approx_eq(&zero()) + (&self[2][0]).approx_eq(&S::zero()) && + (&self[2][1]).approx_eq(&S::zero()) } fn is_symmetric(&self) -> bool { @@ -810,18 +810,18 @@ macro_rules! dot_matrix4( impl Matrix> for Matrix4 { #[inline] fn from_value(value: S) -> Matrix4 { - Matrix4::new(value, zero(), zero(), zero(), - zero(), value, zero(), zero(), - zero(), zero(), value, zero(), - zero(), zero(), zero(), value) + Matrix4::new(value, S::zero(), S::zero(), S::zero(), + S::zero(), value, S::zero(), S::zero(), + S::zero(), S::zero(), value, S::zero(), + S::zero(), S::zero(), S::zero(), value) } #[inline] fn from_diagonal(value: &Vector4) -> Matrix4 { - Matrix4::new(value.x, zero(), zero(), zero(), - zero(), value.y, zero(), zero(), - zero(), zero(), value.z, zero(), - zero(), zero(), zero(), value.w) + Matrix4::new(value.x, S::zero(), S::zero(), S::zero(), + S::zero(), value.y, S::zero(), S::zero(), + S::zero(), S::zero(), value.z, S::zero(), + S::zero(), S::zero(), S::zero(), value.w) } #[inline] @@ -965,9 +965,8 @@ impl Matrix> for Matrix4 { fn invert(&self) -> Option> { let det = self.determinant(); - if !det.approx_eq(&zero()) { - let one: S = one(); - let inv_det = one / det; + if !det.approx_eq(&S::zero()) { + let inv_det = S::one() / det; let t = self.transpose(); let cf = |i, j| { let mat = match i { @@ -985,7 +984,7 @@ impl Matrix> for Matrix4 { t.z.truncate_n(j)), _ => panic!("out of range") }; - let sign = if (i+j) & 1 == 1 {-one} else {one}; + let sign = if (i + j) & 1 == 1 { -S::one() } else { S::one() }; mat.determinant() * sign * inv_det }; @@ -1000,21 +999,21 @@ impl Matrix> for Matrix4 { } fn is_diagonal(&self) -> bool { - (&self[0][1]).approx_eq(&zero()) && - (&self[0][2]).approx_eq(&zero()) && - (&self[0][3]).approx_eq(&zero()) && + (&self[0][1]).approx_eq(&S::zero()) && + (&self[0][2]).approx_eq(&S::zero()) && + (&self[0][3]).approx_eq(&S::zero()) && - (&self[1][0]).approx_eq(&zero()) && - (&self[1][2]).approx_eq(&zero()) && - (&self[1][3]).approx_eq(&zero()) && + (&self[1][0]).approx_eq(&S::zero()) && + (&self[1][2]).approx_eq(&S::zero()) && + (&self[1][3]).approx_eq(&S::zero()) && - (&self[2][0]).approx_eq(&zero()) && - (&self[2][1]).approx_eq(&zero()) && - (&self[2][3]).approx_eq(&zero()) && + (&self[2][0]).approx_eq(&S::zero()) && + (&self[2][1]).approx_eq(&S::zero()) && + (&self[2][3]).approx_eq(&S::zero()) && - (&self[3][0]).approx_eq(&zero()) && - (&self[3][1]).approx_eq(&zero()) && - (&self[3][2]).approx_eq(&zero()) + (&self[3][0]).approx_eq(&S::zero()) && + (&self[3][1]).approx_eq(&S::zero()) && + (&self[3][2]).approx_eq(&S::zero()) } fn is_symmetric(&self) -> bool { @@ -1200,9 +1199,9 @@ impl From> for Matrix3 { /// Clone the elements of a 2-dimensional matrix into the top-left corner /// of a 3-dimensional identity matrix. fn from(m: Matrix2) -> Matrix3 { - Matrix3::new(m[0][0], m[0][1], zero(), - m[1][0], m[1][1], zero(), - zero(), zero(), one()) + Matrix3::new(m[0][0], m[0][1], S::zero(), + m[1][0], m[1][1], S::zero(), + S::zero(), S::zero(), S::one()) } } @@ -1210,10 +1209,10 @@ impl From> for Matrix4 { /// Clone the elements of a 2-dimensional matrix into the top-left corner /// of a 4-dimensional identity matrix. fn from(m: Matrix2) -> Matrix4 { - Matrix4::new(m[0][0], m[0][1], zero(), zero(), - m[1][0], m[1][1], zero(), zero(), - zero(), zero(), one(), zero(), - zero(), zero(), zero(), one()) + Matrix4::new(m[0][0], m[0][1], S::zero(), S::zero(), + m[1][0], m[1][1], S::zero(), S::zero(), + S::zero(), S::zero(), S::one(), S::zero(), + S::zero(), S::zero(), S::zero(), S::one()) } } @@ -1221,10 +1220,10 @@ impl From> for Matrix4 { /// Clone the elements of a 3-dimensional matrix into the top-left corner /// of a 4-dimensional identity matrix. fn from(m: Matrix3) -> Matrix4 { - Matrix4::new(m[0][0], m[0][1], m[0][2], zero(), - m[1][0], m[1][1], m[1][2], zero(), - m[2][0], m[2][1], m[2][2], zero(), - zero(), zero(), zero(), one()) + Matrix4::new(m[0][0], m[0][1], m[0][2], S::zero(), + m[1][0], m[1][1], m[1][2], S::zero(), + m[2][0], m[2][1], m[2][2], S::zero(), + S::zero(), S::zero(), S::zero(), S::one()) } } @@ -1235,8 +1234,8 @@ impl From> for Quaternion { let trace = mat.trace(); let half: S = cast(0.5f64).unwrap(); - if trace >= zero::() { - let s = (one::() + trace).sqrt(); + if trace >= S::zero() { + let s = (S::one() + trace).sqrt(); let w = half * s; let s = half / s; let x = (mat[1][2] - mat[2][1]) * s; diff --git a/src/plane.rs b/src/plane.rs index 2c3c102..0db0e28 100644 --- a/src/plane.rs +++ b/src/plane.rs @@ -15,7 +15,7 @@ use std::fmt; -use rust_num::{one, Zero, zero}; +use rust_num::{One, Zero}; use approx::ApproxEq; use intersect::Intersect; @@ -85,7 +85,7 @@ impl Plane { // find the normal vector that is perpendicular to v1 and v2 let mut n = v0.cross(&v1); - if n.approx_eq(&zero()) { None } + if n.approx_eq(&Vector3::zero()) { None } else { // compute the normal and the distance to the plane n.normalize_self(); @@ -103,9 +103,9 @@ impl Plane { /// Normalize a plane. pub fn normalize(&self) -> Option> { - if self.n.approx_eq(&zero()) { None } + if self.n.approx_eq(&Vector3::zero()) { None } else { - let denom = one::() / self.n.length(); + let denom = S::one() / self.n.length(); Some(Plane::new(self.n.mul_s(denom), self.d*denom)) } } diff --git a/src/point.rs b/src/point.rs index 2d6251a..e9ebf96 100644 --- a/src/point.rs +++ b/src/point.rs @@ -21,7 +21,7 @@ use std::fmt; use std::mem; use std::ops::*; -use rust_num::{one, zero}; +use rust_num::{One, Zero}; use approx::ApproxEq; use array::Array1; @@ -57,13 +57,13 @@ impl Point3 { impl Point3 { #[inline] pub fn from_homogeneous(v: &Vector4) -> Point3 { - let e = v.truncate().mul_s(one::() / v.w); + let e = v.truncate().mul_s(S::one() / v.w); Point3::new(e.x, e.y, e.z) //FIXME } #[inline] pub fn to_homogeneous(&self) -> Vector4 { - Vector4::new(self.x, self.y, self.z, one()) + Vector4::new(self.x, self.y, self.z, S::one()) } } @@ -118,7 +118,7 @@ impl Array1 for Point2 {} impl Point> for Point2 { #[inline] fn origin() -> Point2 { - Point2::new(zero(), zero()) + Point2::new(S::zero(), S::zero()) } #[inline] @@ -218,7 +218,7 @@ impl Array1 for Point3 {} impl Point> for Point3 { #[inline] fn origin() -> Point3 { - Point3::new(zero(), zero(), zero()) + Point3::new(S::zero(), S::zero(), S::zero()) } #[inline] diff --git a/src/projection.rs b/src/projection.rs index 792548f..d67189e 100644 --- a/src/projection.rs +++ b/src/projection.rs @@ -13,7 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use rust_num::{zero, one}; +use rust_num::{Zero, One}; use rust_num::traits::cast; use angle::{Angle, Rad, tan, cot}; @@ -107,11 +107,11 @@ impl> From> for Matrix4 { fn from(persp: PerspectiveFov) -> Matrix4 { let half_turn: A = Angle::turn_div_2(); - assert!(persp.fovy > zero(), "The vertical field of view cannot be below zero, found: {:?}", persp.fovy); + assert!(persp.fovy > A::zero(), "The vertical field of view cannot be below zero, found: {:?}", persp.fovy); assert!(persp.fovy < half_turn, "The vertical field of view cannot be greater than a half turn, found: {:?}", persp.fovy); - assert!(persp.aspect > zero(), "The aspect ratio cannot be below zero, found: {:?}", persp.aspect); - assert!(persp.near > zero(), "The near plane distance cannot be below zero, found: {:?}", persp.near); - assert!(persp.far > zero(), "The far plane distance cannot be below zero, found: {:?}", persp.far); + assert!(persp.aspect > S::zero(), "The aspect ratio cannot be below zero, found: {:?}", persp.aspect); + assert!(persp.near > S::zero(), "The near plane distance cannot be below zero, found: {:?}", persp.near); + assert!(persp.far > S::zero(), "The far plane distance cannot be below zero, found: {:?}", persp.far); assert!(persp.far > persp.near, "The far plane cannot be closer than the near plane, found: far: {:?}, near: {:?}", persp.far, persp.near); let f: Rad<_> = persp.fovy.div_s(cast(2i8).unwrap()).into(); @@ -119,24 +119,24 @@ impl> From> for Matrix4 { let two: S = cast(2i8).unwrap(); let c0r0 = f / persp.aspect; - let c0r1 = zero(); - let c0r2 = zero(); - let c0r3 = zero(); + let c0r1 = S::zero(); + let c0r2 = S::zero(); + let c0r3 = S::zero(); - let c1r0 = zero(); + let c1r0 = S::zero(); let c1r1 = f; - let c1r2 = zero(); - let c1r3 = zero(); + let c1r2 = S::zero(); + let c1r3 = S::zero(); - let c2r0 = zero(); - let c2r1 = zero(); + let c2r0 = S::zero(); + let c2r1 = S::zero(); let c2r2 = (persp.far + persp.near) / (persp.near - persp.far); - let c2r3 = -one::(); + let c2r3 = -S::one(); - let c3r0 = zero(); - let c3r1 = zero(); + let c3r0 = S::zero(); + let c3r1 = S::zero(); let c3r2 = (two * persp.far * persp.near) / (persp.near - persp.far); - let c3r3 = zero(); + let c3r3 = S::zero(); Matrix4::new(c0r0, c0r1, c0r2, c0r3, c1r0, c1r1, c1r2, c1r3, @@ -172,24 +172,24 @@ impl From> for Matrix4 { let two: S = cast(2i8).unwrap(); let c0r0 = (two * persp.near) / (persp.right - persp.left); - let c0r1 = zero(); - let c0r2 = zero(); - let c0r3 = zero(); + let c0r1 = S::zero(); + let c0r2 = S::zero(); + let c0r3 = S::zero(); - let c1r0 = zero(); + let c1r0 = S::zero(); let c1r1 = (two * persp.near) / (persp.top - persp.bottom); - let c1r2 = zero(); - let c1r3 = zero(); + let c1r2 = S::zero(); + let c1r3 = S::zero(); let c2r0 = (persp.right + persp.left) / (persp.right - persp.left); let c2r1 = (persp.top + persp.bottom) / (persp.top - persp.bottom); let c2r2 = -(persp.far + persp.near) / (persp.far - persp.near); - let c2r3 = -one::(); + let c2r3 = -S::one(); - let c3r0 = zero(); - let c3r1 = zero(); + let c3r0 = S::zero(); + let c3r1 = S::zero(); let c3r2 = -(two * persp.far * persp.near) / (persp.far - persp.near); - let c3r3 = zero(); + let c3r3 = S::zero(); Matrix4::new(c0r0, c0r1, c0r2, c0r3, c1r0, c1r1, c1r2, c1r3, @@ -212,12 +212,12 @@ pub struct Ortho { impl Projection for Ortho { fn to_frustum(&self) -> Frustum { Frustum { - left: Plane::from_abcd( one::(), zero::(), zero::(), self.left.clone()), - right: Plane::from_abcd(-one::(), zero::(), zero::(), self.right.clone()), - bottom: Plane::from_abcd(zero::(), one::(), zero::(), self.bottom.clone()), - top: Plane::from_abcd(zero::(), -one::(), zero::(), self.top.clone()), - near: Plane::from_abcd(zero::(), zero::(), -one::(), self.near.clone()), - far: Plane::from_abcd(zero::(), zero::(), one::(), self.far.clone()), + left: Plane::from_abcd( S::one(), S::zero(), S::zero(), self.left.clone()), + right: Plane::from_abcd(-S::one(), S::zero(), S::zero(), self.right.clone()), + bottom: Plane::from_abcd(S::zero(), S::one(), S::zero(), self.bottom.clone()), + top: Plane::from_abcd(S::zero(), -S::one(), S::zero(), self.top.clone()), + near: Plane::from_abcd(S::zero(), S::zero(), -S::one(), self.near.clone()), + far: Plane::from_abcd(S::zero(), S::zero(), S::one(), self.far.clone()), } } } @@ -227,24 +227,24 @@ impl From> for Matrix4 { let two: S = cast(2i8).unwrap(); let c0r0 = two / (ortho.right - ortho.left); - let c0r1 = zero(); - let c0r2 = zero(); - let c0r3 = zero(); + let c0r1 = S::zero(); + let c0r2 = S::zero(); + let c0r3 = S::zero(); - let c1r0 = zero(); + let c1r0 = S::zero(); let c1r1 = two / (ortho.top - ortho.bottom); - let c1r2 = zero(); - let c1r3 = zero(); + let c1r2 = S::zero(); + let c1r3 = S::zero(); - let c2r0 = zero(); - let c2r1 = zero(); + let c2r0 = S::zero(); + let c2r1 = S::zero(); let c2r2 = -two / (ortho.far - ortho.near); - let c2r3 = zero(); + let c2r3 = S::zero(); let c3r0 = -(ortho.right + ortho.left) / (ortho.right - ortho.left); let c3r1 = -(ortho.top + ortho.bottom) / (ortho.top - ortho.bottom); let c3r2 = -(ortho.far + ortho.near) / (ortho.far - ortho.near); - let c3r3 = one::(); + let c3r3 = S::one(); Matrix4::new(c0r0, c0r1, c0r2, c0r3, c1r0, c1r1, c1r2, c1r3, diff --git a/src/quaternion.rs b/src/quaternion.rs index 76c59ca..dceec36 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -19,7 +19,7 @@ use std::mem; use std::ops::*; use rand::{Rand, Rng}; -use rust_num::{Float, one, zero}; +use rust_num::{Float, One, Zero}; use rust_num::traits::cast; use angle::{Angle, Rad, acos, sin, sin_cos, rad}; @@ -59,13 +59,13 @@ impl Quaternion { /// The additive identity, ie: `q = 0 + 0i + 0j + 0i` #[inline] pub fn zero() -> Quaternion { - Quaternion::new(zero(), zero(), zero(), zero()) + Quaternion::new(S::zero(), S::zero(), S::zero(), S::zero()) } /// The multiplicative identity, ie: `q = 1 + 0i + 0j + 0i` #[inline] pub fn identity() -> Quaternion { - Quaternion::from_sv(one::(), zero()) + Quaternion::from_sv(S::one(), Vector3::zero()) } /// The result of multiplying the quaternion a scalar @@ -176,12 +176,12 @@ impl Quaternion { /// Normalize this quaternion, returning the new quaternion. #[inline] pub fn normalize(&self) -> Quaternion { - self.mul_s(one::() / self.magnitude()) + self.mul_s(S::one() / self.magnitude()) } /// Do a normalized linear interpolation with `other`, by `amount`. pub fn nlerp(&self, other: &Quaternion, amount: S) -> Quaternion { - self.mul_s(one::() - amount).add_q(&other.mul_s(amount)).normalize() + self.mul_s(S::one() - amount).add_q(&other.mul_s(amount)).normalize() } } @@ -220,17 +220,17 @@ impl Quaternion { } else { // stay within the domain of acos() // TODO REMOVE WHEN https://github.com/mozilla/rust/issues/12068 IS RESOLVED - let robust_dot = if dot > one::() { - one::() - } else if dot < -one::() { - -one::() + let robust_dot = if dot > S::one() { + S::one() + } else if dot < -S::one() { + -S::one() } else { dot }; let theta: Rad = acos(robust_dot.clone()); - let scale1 = sin(theta.mul_s(one::() - amount)); + let scale1 = sin(theta.mul_s(S::one() - amount)); let scale2 = sin(theta.mul_s(amount)); self.mul_s(scale1) @@ -258,14 +258,14 @@ impl Quaternion { if test > sig * unit { ( - rad(zero::()), + rad(S::zero()), rad(cast(f64::consts::FRAC_PI_2).unwrap()), rad(two * qx.atan2(qw)), ) } else if test < -sig * unit { let y: S = cast(f64::consts::FRAC_PI_2).unwrap(); ( - rad(zero::()), + rad(S::zero()), rad(-y), rad(two * qx.atan2(qw)), ) @@ -298,9 +298,9 @@ impl From> for Matrix3 { let sz2 = z2 * quat.s; let sx2 = x2 * quat.s; - Matrix3::new(one::() - yy2 - zz2, xy2 + sz2, xz2 - sy2, - xy2 - sz2, one::() - xx2 - zz2, yz2 + sx2, - xz2 + sy2, yz2 - sx2, one::() - xx2 - yy2) + Matrix3::new(S::one() - yy2 - zz2, xy2 + sz2, xz2 - sy2, + xy2 - sz2, S::one() - xx2 - zz2, yz2 + sx2, + xz2 + sy2, yz2 - sx2, S::one() - xx2 - yy2) } } @@ -323,10 +323,10 @@ impl From> for Matrix4 { let sz2 = z2 * quat.s; let sx2 = x2 * quat.s; - Matrix4::new(one::() - yy2 - zz2, xy2 + sz2, xz2 - sy2, zero::(), - xy2 - sz2, one::() - xx2 - zz2, yz2 + sx2, zero::(), - xz2 + sy2, yz2 - sx2, one::() - xx2 - yy2, zero::(), - zero::(), zero::(), zero::(), one::()) + Matrix4::new(S::one() - yy2 - zz2, xy2 + sz2, xz2 - sy2, S::zero(), + xy2 - sz2, S::one() - xx2 - zz2, yz2 + sx2, S::zero(), + xz2 + sy2, yz2 - sx2, S::one() - xx2 - yy2, S::zero(), + S::zero(), S::zero(), S::zero(), S::one()) } } @@ -369,7 +369,7 @@ impl Rotation, Point3> for Quaternion, b: &Vector3) -> Quaternion { //http://stackoverflow.com/questions/1171849/ //finding-quaternion-representing-the-rotation-from-one-vector-to-another - Quaternion::from_sv(one::() + a.dot(b), a.cross(b)).normalize() + Quaternion::from_sv(S::one() + a.dot(b), a.cross(b)).normalize() } #[inline] diff --git a/src/rotation.rs b/src/rotation.rs index 9b7541d..0aa8d4c 100644 --- a/src/rotation.rs +++ b/src/rotation.rs @@ -44,7 +44,7 @@ pub trait Rotation, P: Point>: PartialEq + Appr /// representation as a vector. #[inline] fn rotate_point(&self, point: &P) -> P { - Point::from_vec( &self.rotate_vector( &point.to_vec() ) ) + P::from_vec(&self.rotate_vector(&point.to_vec())) } /// Rotate a ray using this rotation. @@ -63,7 +63,7 @@ pub trait Rotation, P: Point>: PartialEq + Appr /// Modify this rotation in-place by combining it with another. #[inline] fn concat_self(&mut self, other: &Self) { - *self = Rotation::concat(self, other); + *self = Self::concat(self, other); } /// Invert this rotation in-place. diff --git a/src/sphere.rs b/src/sphere.rs index 28deb77..f960b02 100644 --- a/src/sphere.rs +++ b/src/sphere.rs @@ -15,7 +15,7 @@ //! Bounding sphere -use rust_num::zero; +use rust_num::Zero; use bound::*; use intersect::Intersect; @@ -37,7 +37,7 @@ impl Intersect>> for (Sphere, Ray3) { let l = s.center.sub_p(&r.origin); let tca = l.dot(&r.direction); - if tca < zero() { return None; } + if tca < S::zero() { return None; } let d2 = l.dot(&l) - tca*tca; if d2 > s.radius*s.radius { return None; } let thc = (s.radius*s.radius - d2).sqrt(); diff --git a/src/transform.rs b/src/transform.rs index b94a0f6..8ef9ee8 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -15,7 +15,7 @@ use std::fmt; -use rust_num::{zero, one}; +use rust_num::{Zero, One}; use approx::ApproxEq; use matrix::*; @@ -52,7 +52,7 @@ pub trait Transform, P: Point>: Sized { /// Transform a vector as a point using this transform. #[inline] fn transform_as_point(&self, vec: &V) -> V { - self.transform_point(&Point::from_vec(vec)).to_vec() + self.transform_point(&P::from_vec(vec)).to_vec() } /// Combine this transform with another, yielding a new transformation @@ -65,7 +65,7 @@ pub trait Transform, P: Point>: Sized { /// Combine this transform with another, in-place. #[inline] fn concat_self(&mut self, other: &Self) { - *self = Transform::concat(self, other); + *self = Self::concat(self, other); } /// Invert this transform in-place, failing if the transformation is not @@ -94,19 +94,18 @@ impl< #[inline] fn identity() -> Decomposed { Decomposed { - scale: one(), - rot: Rotation::identity(), - disp: zero(), + scale: S::one(), + rot: R::identity(), + disp: V::zero(), } } #[inline] fn look_at(eye: &P, center: &P, up: &V) -> Decomposed { - let origin: P = Point::origin(); - let rot: R = Rotation::look_at(¢er.sub_p(eye), up); - let disp: V = rot.rotate_vector(&origin.sub_p(eye)); + let rot = R::look_at(¢er.sub_p(eye), up); + let disp = rot.rotate_vector(&P::origin().sub_p(eye)); Decomposed { - scale: one(), + scale: S::one(), rot: rot, disp: disp, } @@ -131,10 +130,10 @@ impl< } fn invert(&self) -> Option> { - if self.scale.approx_eq(&zero()) { + if self.scale.approx_eq(&S::zero()) { None } else { - let s = one::() / self.scale; + let s = S::one() / self.scale; let r = self.rot.invert(); let d = r.rotate_vector(&self.disp).mul_s(-s); Some(Decomposed { @@ -156,7 +155,7 @@ impl< fn from(dec: Decomposed, R>) -> Matrix3 { let m: Matrix2<_> = dec.rot.into(); let mut m: Matrix3<_> = m.mul_s(dec.scale).into(); - m.z = dec.disp.extend(one()); + m.z = dec.disp.extend(S::one()); m } } @@ -168,7 +167,7 @@ impl< fn from(dec: Decomposed, R>) -> Matrix4 { let m: Matrix3<_> = dec.rot.into(); let mut m: Matrix4<_> = m.mul_s(dec.scale).into(); - m.w = dec.disp.extend(one()); + m.w = dec.disp.extend(S::one()); m } } @@ -212,7 +211,7 @@ impl Transform, Point3> for AffineMatri #[inline] fn transform_vector(&self, vec: &Vector3) -> Vector3 { - self.mat.mul_v(&vec.extend(zero())).truncate() + self.mat.mul_v(&vec.extend(S::zero())).truncate() } #[inline] @@ -263,8 +262,7 @@ impl< R: Rotation + Clone, > ToComponents for Decomposed { fn decompose(&self) -> (V, R, V) { - let v: V = one(); - (v.mul_s(self.scale), self.rot.clone(), self.disp.clone()) + (V::one().mul_s(self.scale), self.rot.clone(), self.disp.clone()) } } diff --git a/src/vector.rs b/src/vector.rs index d5b0392..8a10b8d 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -102,7 +102,7 @@ use std::ops::*; use rand::{Rand, Rng}; -use rust_num::{NumCast, Zero, One, zero, one}; +use rust_num::{NumCast, Zero, One}; use angle::{Rad, atan2, acos}; use approx::ApproxEq; @@ -217,7 +217,7 @@ macro_rules! vec { impl<$S: Zero + BaseNum> Zero for $Self_<$S> { #[inline] - fn zero() -> $Self_ { $Self_ { $($field: zero()),+ } } + fn zero() -> $Self_ { $Self_ { $($field: $S::zero()),+ } } #[inline] fn is_zero(&self) -> bool { $((self.$field.is_zero()) )&&+ } @@ -225,7 +225,7 @@ macro_rules! vec { impl<$S: One + BaseNum> One for $Self_<$S> { #[inline] - fn one() -> $Self_<$S> { $Self_ { $($field: one()),+ } } + fn one() -> $Self_<$S> { $Self_ { $($field: S::one()),+ } } } impl<$S: NumCast + Copy> $Self_<$S> { @@ -482,9 +482,16 @@ index_operators!(Vector4, 4, [S], RangeFull); /// Operations specific to numeric two-dimensional vectors. impl Vector2 { /// A unit vector in the `x` direction. - #[inline] pub fn unit_x() -> Vector2 { Vector2::new(one(), zero()) } + #[inline] + pub fn unit_x() -> Vector2 { + Vector2::new(S::one(), S::zero()) + } + /// A unit vector in the `y` direction. - #[inline] pub fn unit_y() -> Vector2 { Vector2::new(zero(), one()) } + #[inline] + pub fn unit_y() -> Vector2 { + Vector2::new(S::zero(), S::one()) + } /// The perpendicular dot product of the vector and `other`. #[inline] @@ -503,11 +510,22 @@ impl Vector2 { /// Operations specific to numeric three-dimensional vectors. impl Vector3 { /// A unit vector in the `x` direction. - #[inline] pub fn unit_x() -> Vector3 { Vector3::new(one(), zero(), zero()) } + #[inline] + pub fn unit_x() -> Vector3 { + Vector3::new(S::one(), S::zero(), S::zero()) + } + /// A unit vector in the `y` direction. - #[inline] pub fn unit_y() -> Vector3 { Vector3::new(zero(), one(), zero()) } + #[inline] + pub fn unit_y() -> Vector3 { + Vector3::new(S::zero(), S::one(), S::zero()) + } + /// A unit vector in the `w` direction. - #[inline] pub fn unit_z() -> Vector3 { Vector3::new(zero(), zero(), one()) } + #[inline] + pub fn unit_z() -> Vector3 { + Vector3::new(S::zero(), S::zero(), S::one()) + } /// Returns the cross product of the vector and `other`. #[inline] @@ -542,13 +560,28 @@ impl Vector3 { /// Operations specific to numeric four-dimensional vectors. impl Vector4 { /// A unit vector in the `x` direction. - #[inline] pub fn unit_x() -> Vector4 { Vector4::new(one(), zero(), zero(), zero()) } + #[inline] + pub fn unit_x() -> Vector4 { + Vector4::new(S::one(), S::zero(), S::zero(), S::zero()) + } + /// A unit vector in the `y` direction. - #[inline] pub fn unit_y() -> Vector4 { Vector4::new(zero(), one(), zero(), zero()) } + #[inline] + pub fn unit_y() -> Vector4 { + Vector4::new(S::zero(), S::one(), S::zero(), S::zero()) + } + /// A unit vector in the `z` direction. - #[inline] pub fn unit_z() -> Vector4 { Vector4::new(zero(), zero(), one(), zero()) } + #[inline] + pub fn unit_z() -> Vector4 { + Vector4::new(S::zero(), S::zero(), S::one(), S::zero()) + } + /// A unit vector in the `w` direction. - #[inline] pub fn unit_w() -> Vector4 { Vector4::new(zero(), zero(), zero(), one()) } + #[inline] + pub fn unit_w() -> Vector4 { + Vector4::new(S::zero(), S::zero(), S::zero(), S::one()) + } /// Create a `Vector3`, dropping the `w` value. #[inline] @@ -577,7 +610,7 @@ pub trait EuclideanVector: Vector /// Returns `true` if the vector is perpendicular (at right angles) to the /// other vector. fn is_perpendicular(&self, other: &Self) -> bool { - self.dot(other).approx_eq(&zero()) + self.dot(other).approx_eq(&S::zero()) } /// Returns the squared length of the vector. This does not perform an @@ -602,7 +635,7 @@ pub trait EuclideanVector: Vector #[inline] #[must_use] fn normalize(&self) -> Self { - self.normalize_to(one::()) + self.normalize_to(S::one()) } /// Returns a vector with the same direction and a given `length`.