diff --git a/src/cgmath/aabb.rs b/src/cgmath/aabb.rs index fb2b063..68bd905 100644 --- a/src/cgmath/aabb.rs +++ b/src/cgmath/aabb.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The CGMath Developers. For a full listing of the authors, +// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors, // refer to the AUTHORS file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,14 +23,14 @@ use point::{Point, Point2, Point3}; use vector::{Vector, Vector2, Vector3}; use array::build; -use partial_ord::PartOrdPrim; +use num::BaseNum; use std::fmt; use std::num::{zero, one}; use std::iter::Iterator; pub trait Aabb < - S: PartOrdPrim, + S: BaseNum, V: Vector, P: Point, Slice @@ -65,8 +65,8 @@ pub trait Aabb /// Returns a new AABB that is grown to include the given point. fn grow(&self, p: &P) -> Self { - let min : P = build(|i| self.min().i(i).min(*p.i(i))); - let max : P = build(|i| self.max().i(i).max(*p.i(i))); + let min : P = build(|i| self.min().i(i).partial_min(*p.i(i))); + let max : P = build(|i| self.max().i(i).partial_max(*p.i(i))); Aabb::new(min, max) } @@ -95,26 +95,26 @@ pub struct Aabb2 { pub max: Point2, } -impl Aabb2 { +impl Aabb2 { /// Construct a new axis-aligned bounding box from two points. #[inline] pub fn new(p1: Point2, p2: Point2) -> Aabb2 { Aabb2 { - min: Point2::new(p1.x.min(p2.x), - p1.y.min(p2.y)), - max: Point2::new(p1.x.max(p2.x), - p1.y.max(p2.y)), + min: Point2::new(p1.x.partial_min(p2.x), + p1.y.partial_min(p2.y)), + max: Point2::new(p1.x.partial_max(p2.x), + p1.y.partial_max(p2.y)), } } } -impl Aabb, Point2, [S, ..2]> for Aabb2 { +impl Aabb, Point2, [S, ..2]> for Aabb2 { fn new(p1: Point2, p2: Point2) -> Aabb2 { Aabb2::new(p1, p2) } #[inline] fn min<'a>(&'a self) -> &'a Point2 { &self.min } #[inline] fn max<'a>(&'a self) -> &'a Point2 { &self.max } } -impl fmt::Show for Aabb2 { +impl fmt::Show for Aabb2 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "[{} - {}]", self.min, self.max) } @@ -127,28 +127,28 @@ pub struct Aabb3 { pub max: Point3, } -impl Aabb3 { +impl Aabb3 { /// Construct a new axis-aligned bounding box from two points. #[inline] pub fn new(p1: Point3, p2: Point3) -> Aabb3 { Aabb3 { - min: Point3::new(p1.x.min(p2.x), - p1.y.min(p2.y), - p1.z.min(p2.z)), - max: Point3::new(p1.x.max(p2.x), - p1.y.max(p2.y), - p1.z.max(p2.z)), + min: Point3::new(p1.x.partial_min(p2.x), + p1.y.partial_min(p2.y), + p1.z.partial_min(p2.z)), + max: Point3::new(p1.x.partial_max(p2.x), + p1.y.partial_max(p2.y), + p1.z.partial_max(p2.z)), } } } -impl Aabb, Point3, [S, ..3]> for Aabb3 { +impl Aabb, Point3, [S, ..3]> for Aabb3 { fn new(p1: Point3, p2: Point3) -> Aabb3 { Aabb3::new(p1, p2) } #[inline] fn min<'a>(&'a self) -> &'a Point3 { &self.min } #[inline] fn max<'a>(&'a self) -> &'a Point3 { &self.max } } -impl fmt::Show for Aabb3 { +impl fmt::Show for Aabb3 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "[{} - {}]", self.min, self.max) } diff --git a/src/cgmath/angle.rs b/src/cgmath/angle.rs index 9327cfb..657db1e 100644 --- a/src/cgmath/angle.rs +++ b/src/cgmath/angle.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The CGMath Developers. For a full listing of the authors, +// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors, // refer to the AUTHORS file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,6 +19,7 @@ use std::fmt; use std::num::{One, one, Zero, zero, cast}; use approx::ApproxEq; +use num::BaseFloat; /// An angle, in radians #[deriving(Clone, Eq, Ord, Hash)] pub struct Rad { pub s: S } @@ -26,27 +27,27 @@ use approx::ApproxEq; #[deriving(Clone, Eq, Ord, Hash)] pub struct Deg { pub s: S } /// Create a new angle, in radians -#[inline] pub fn rad(s: S) -> Rad { Rad { s: s } } +#[inline] pub fn rad(s: S) -> Rad { Rad { s: s } } /// Create a new angle, in degrees -#[inline] pub fn deg(s: S) -> Deg { Deg { s: s } } +#[inline] pub fn deg(s: S) -> Deg { Deg { s: s } } /// Represents types that can be converted to radians. -pub trait ToRad { +pub trait ToRad { /// Convert this value to radians. fn to_rad(&self) -> Rad; } /// Represents types that can be converted to degrees. -pub trait ToDeg { +pub trait ToDeg { /// Convert this value to degrees. fn to_deg(&self) -> Deg; } -impl ToRad for Rad { #[inline] fn to_rad(&self) -> Rad { self.clone() } } -impl ToRad for Deg { #[inline] fn to_rad(&self) -> Rad { rad(self.s.to_radians()) } } +impl ToRad for Rad { #[inline] fn to_rad(&self) -> Rad { self.clone() } } +impl ToRad for Deg { #[inline] fn to_rad(&self) -> Rad { rad(self.s.to_radians()) } } -impl ToDeg for Rad { #[inline] fn to_deg(&self) -> Deg { deg(self.s.to_degrees()) } } -impl ToDeg for Deg { #[inline] fn to_deg(&self) -> Deg { self.clone() } } +impl ToDeg for Rad { #[inline] fn to_deg(&self) -> Deg { deg(self.s.to_degrees()) } } +impl ToDeg for Deg { #[inline] fn to_deg(&self) -> Deg { self.clone() } } /// Private utility functions for converting to/from scalars trait ScalarConv { @@ -55,13 +56,13 @@ trait ScalarConv { fn mut_s<'a>(&'a mut self) -> &'a mut S; } -impl ScalarConv for Rad { +impl ScalarConv for Rad { #[inline] fn from(s: S) -> Rad { rad(s) } #[inline] fn s<'a>(&'a self) -> &'a S { &'a self.s } #[inline] fn mut_s<'a>(&'a mut self) -> &'a mut S { &'a mut self.s } } -impl ScalarConv for Deg { +impl ScalarConv for Deg { #[inline] fn from(s: S) -> Deg { deg(s) } #[inline] fn s<'a>(&'a self) -> &'a S { &'a self.s } #[inline] fn mut_s<'a>(&'a mut self) -> &'a mut S { &'a mut self.s } @@ -70,7 +71,7 @@ impl ScalarConv for Deg { /// Operations on angles. pub trait Angle < - S: Float + S: BaseFloat > : Clone + Zero + Eq + Equiv + Ord @@ -150,9 +151,9 @@ pub trait Angle #[inline] fn turn_div_6() -> Self { let full_turn: Self = Angle::full_turn(); full_turn.div_s(cast(6).unwrap()) } } -#[inline] pub fn bisect>(a: A, b: A) -> A { a.bisect(b) } +#[inline] pub fn bisect>(a: A, b: A) -> A { a.bisect(b) } -impl> +impl Rad { #[inline] pub fn zero() -> Rad { zero() } #[inline] pub fn full_turn() -> Rad { Angle::full_turn() } @@ -162,7 +163,7 @@ Rad { #[inline] pub fn turn_div_6() -> Rad { Angle::turn_div_6() } } -impl> +impl Deg { #[inline] pub fn zero() -> Deg { zero() } #[inline] pub fn full_turn() -> Deg { Angle::full_turn() } @@ -173,79 +174,79 @@ Deg { } -impl Add, Rad> for Rad { #[inline] fn add(&self, other: &Rad) -> Rad { rad(self.s + other.s) } } -impl Add, Deg> for Deg { #[inline] fn add(&self, other: &Deg) -> Deg { deg(self.s + other.s) } } +impl Add, Rad> for Rad { #[inline] fn add(&self, other: &Rad) -> Rad { rad(self.s + other.s) } } +impl Add, Deg> for Deg { #[inline] fn add(&self, other: &Deg) -> Deg { deg(self.s + other.s) } } -impl Sub, Rad> for Rad { #[inline] fn sub(&self, other: &Rad) -> Rad { rad(self.s - other.s) } } -impl Sub, Deg> for Deg { #[inline] fn sub(&self, other: &Deg) -> Deg { deg(self.s - other.s) } } +impl Sub, Rad> for Rad { #[inline] fn sub(&self, other: &Rad) -> Rad { rad(self.s - other.s) } } +impl Sub, Deg> for Deg { #[inline] fn sub(&self, other: &Deg) -> Deg { deg(self.s - other.s) } } -impl Neg> for Rad { #[inline] fn neg(&self) -> Rad { rad(-self.s) } } -impl Neg> for Deg { #[inline] fn neg(&self) -> Deg { deg(-self.s) } } +impl Neg> for Rad { #[inline] fn neg(&self) -> Rad { rad(-self.s) } } +impl Neg> for Deg { #[inline] fn neg(&self) -> Deg { deg(-self.s) } } -impl Zero for Rad { #[inline] fn zero() -> Rad { rad(zero()) } #[inline] fn is_zero(&self) -> bool { *self == zero() } } -impl Zero for Deg { #[inline] fn zero() -> Deg { deg(zero()) } #[inline] fn is_zero(&self) -> bool { *self == zero() } } +impl Zero for Rad { #[inline] fn zero() -> Rad { rad(zero()) } #[inline] fn is_zero(&self) -> bool { *self == zero() } } +impl Zero for Deg { #[inline] fn zero() -> Deg { deg(zero()) } #[inline] fn is_zero(&self) -> bool { *self == zero() } } -impl Mul, Rad> for Rad { #[inline] fn mul(&self, other: &Rad) -> Rad { rad(self.s * other.s) } } -impl Mul, Deg> for Deg { #[inline] fn mul(&self, other: &Deg) -> Deg { deg(self.s * other.s) } } +impl Mul, Rad> for Rad { #[inline] fn mul(&self, other: &Rad) -> Rad { rad(self.s * other.s) } } +impl Mul, Deg> for Deg { #[inline] fn mul(&self, other: &Deg) -> Deg { deg(self.s * other.s) } } -impl One for Rad { #[inline] fn one() -> Rad { rad(one()) } } -impl One for Deg { #[inline] fn one() -> Deg { deg(one()) } } +impl One for Rad { #[inline] fn one() -> Rad { rad(one()) } } +impl One for Deg { #[inline] fn one() -> Deg { deg(one()) } } -impl> +impl Equiv> for Rad { fn equiv(&self, other: &Rad) -> bool { self.normalize() == other.normalize() } } -impl> +impl Equiv> for Deg { fn equiv(&self, other: &Deg) -> bool { self.normalize() == other.normalize() } } -impl> +impl Angle for Rad { #[inline] fn from>(theta: A) -> Rad { theta.to_rad() } #[inline] fn full_turn() -> Rad { rad(Float::two_pi()) } } -impl> +impl Angle for Deg { #[inline] fn from>(theta: A) -> Deg { theta.to_deg() } #[inline] fn full_turn() -> Deg { deg(cast(360).unwrap()) } } -#[inline] pub fn sin(theta: Rad) -> S { theta.s.sin() } -#[inline] pub fn cos(theta: Rad) -> S { theta.s.cos() } -#[inline] pub fn tan(theta: Rad) -> S { theta.s.tan() } -#[inline] pub fn sin_cos(theta: Rad) -> (S, S) { theta.s.sin_cos() } +#[inline] pub fn sin(theta: Rad) -> S { theta.s.sin() } +#[inline] pub fn cos(theta: Rad) -> S { theta.s.cos() } +#[inline] pub fn tan(theta: Rad) -> S { theta.s.tan() } +#[inline] pub fn sin_cos(theta: Rad) -> (S, S) { theta.s.sin_cos() } -#[inline] pub fn cot(theta: Rad) -> S { tan(theta).recip() } -#[inline] pub fn sec(theta: Rad) -> S { cos(theta).recip() } -#[inline] pub fn csc(theta: Rad) -> S { sin(theta).recip() } +#[inline] pub fn cot(theta: Rad) -> S { tan(theta).recip() } +#[inline] pub fn sec(theta: Rad) -> S { cos(theta).recip() } +#[inline] pub fn csc(theta: Rad) -> S { sin(theta).recip() } -#[inline] pub fn asin(s: S) -> Rad { rad(s.asin()) } -#[inline] pub fn acos(s: S) -> Rad { rad(s.acos()) } -#[inline] pub fn atan(s: S) -> Rad { rad(s.atan()) } -#[inline] pub fn atan2(a: S, b: S) -> Rad { rad(a.atan2(b)) } +#[inline] pub fn asin(s: S) -> Rad { rad(s.asin()) } +#[inline] pub fn acos(s: S) -> Rad { rad(s.acos()) } +#[inline] pub fn atan(s: S) -> Rad { rad(s.atan()) } +#[inline] pub fn atan2(a: S, b: S) -> Rad { rad(a.atan2(b)) } -impl +impl fmt::Show for Rad { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{} rad", self.s) } } -impl +impl fmt::Show for Deg { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}°", self.s) } } -impl> +impl ApproxEq for Rad { #[inline] fn approx_eq_eps(&self, other: &Rad, epsilon: &S) -> bool { @@ -253,7 +254,7 @@ ApproxEq for Rad { } } -impl> +impl ApproxEq for Deg { #[inline] fn approx_eq_eps(&self, other: &Deg, epsilon: &S) -> bool { diff --git a/src/cgmath/approx.rs b/src/cgmath/approx.rs index 9f0b7e0..3827c80 100644 --- a/src/cgmath/approx.rs +++ b/src/cgmath/approx.rs @@ -74,4 +74,3 @@ approx_array!(impl Vector4) approx_array!(impl Point2) approx_array!(impl Point3) - diff --git a/src/cgmath/frustum.rs b/src/cgmath/frustum.rs index 3fbf2b8..a14f99b 100644 --- a/src/cgmath/frustum.rs +++ b/src/cgmath/frustum.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The CGMath Developers. For a full listing of the authors, +// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors, // refer to the AUTHORS file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,10 +16,10 @@ //! View frustum for visibility determination use matrix::{Matrix, Matrix4}; +use num::BaseFloat; use plane::Plane; use point::Point3; use vector::{Vector, EuclideanVector}; -use partial_ord::PartOrdFloat; #[deriving(Clone, Eq)] pub struct Frustum { @@ -31,7 +31,7 @@ pub struct Frustum { pub far: Plane, } -impl> +impl Frustum { /// Constructs a frustum pub fn new(left: Plane, right: Plane, diff --git a/src/cgmath/lib.rs b/src/cgmath/lib.rs index e0a18a0..00a222b 100644 --- a/src/cgmath/lib.rs +++ b/src/cgmath/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The CGMath Developers. For a full listing of the authors, +// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors, // refer to the AUTHORS file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -62,6 +62,6 @@ pub mod obb; pub mod sphere; pub mod approx; +pub mod num; pub mod ptr; -pub mod partial_ord; diff --git a/src/cgmath/line.rs b/src/cgmath/line.rs index 5359b42..72771f5 100644 --- a/src/cgmath/line.rs +++ b/src/cgmath/line.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The CGMath Developers. For a full listing of the authors, +// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors, // refer to the AUTHORS file at the top-level directionectory of this distribution. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,9 +17,9 @@ use std::num::{Zero, zero, One, one}; +use num::{BaseFloat, BaseNum}; use point::{Point, Point2, Point3}; use vector::{Vector, Vector2}; -use partial_ord::PartOrdFloat; use intersect::Intersect; /// A generic directed line segment from `origin` to `dest`. @@ -32,7 +32,7 @@ pub struct Line

impl < - S: Primitive, + S: BaseNum, Slice, V: Vector, P: Point @@ -49,7 +49,7 @@ pub type Line3 = Line>; /// Determines if an intersection between two line segments is found. If the segments are /// collinear and overlapping, the intersection point that will be returned will be the first /// intersection point found by traversing the first line segment, starting at its origin. -impl> Intersect>> for (Line2, Line2) { +impl Intersect>> for (Line2, Line2) { fn intersection(&self) -> Option> { match *self { (ref l1, ref l2) => { diff --git a/src/cgmath/matrix.rs b/src/cgmath/matrix.rs index 2e7d4d7..02ba5e1 100644 --- a/src/cgmath/matrix.rs +++ b/src/cgmath/matrix.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The CGMath Developers. For a full listing of the authors, +// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors, // refer to the AUTHORS file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,11 +21,11 @@ use std::num::{Zero, zero, One, one, cast}; use angle::{Rad, sin, cos, sin_cos}; use approx::ApproxEq; use array::{Array, build}; +use num::{BaseFloat, BaseNum}; use point::{Point, Point3}; use quaternion::{Quaternion, ToQuaternion}; use vector::{Vector, EuclideanVector}; use vector::{Vector2, Vector3, Vector4}; -use partial_ord::PartOrdFloat; /// A 2 x 2, column major matrix #[deriving(Clone, Eq)] @@ -40,7 +40,7 @@ pub struct Matrix3 { pub x: Vector3, pub y: Vector3, pub z: Vector3 pub struct Matrix4 { pub x: Vector4, pub y: Vector4, pub z: Vector4, pub w: Vector4 } -impl Matrix2 { +impl Matrix2 { /// Create a new matrix, providing values for each index. #[inline] pub fn new(c0r0: S, c0r1: S, @@ -76,7 +76,7 @@ impl Matrix2 { } } -impl> Matrix2 { +impl Matrix2 { /// Create a transformation matrix that will cause a vector to point at /// `dir`, using `up` for orientation. pub fn look_at(dir: &Vector2, up: &Vector2) -> Matrix2 { @@ -94,7 +94,7 @@ impl> Matrix2 { } } -impl Matrix3 { +impl Matrix3 { /// Create a new matrix, providing values for each index. #[inline] pub fn new(c0r0:S, c0r1:S, c0r2:S, @@ -133,7 +133,7 @@ impl Matrix3 { } } -impl> +impl Matrix3 { /// Create a transformation matrix that will cause a vector to point at /// `dir`, using `up` for orientation. @@ -209,7 +209,7 @@ Matrix3 { } } -impl Matrix4 { +impl Matrix4 { /// Create a new matrix, providing values for each index. #[inline] pub fn new(c0r0: S, c0r1: S, c0r2: S, c0r3: S, @@ -251,7 +251,7 @@ impl Matrix4 { } } -impl> +impl Matrix4 { /// Create a transformation matrix that will cause a vector to point at /// `dir`, using `up` for orientation. @@ -273,7 +273,7 @@ array!(impl Matrix4 -> [Vector4, ..4] _4) pub trait Matrix < - S: PartOrdFloat, Slice, + S: BaseFloat, Slice, V: Clone + Vector + Array, VSlice > : Array @@ -417,31 +417,31 @@ pub trait Matrix fn is_symmetric(&self) -> bool; } -impl> Add, Matrix2> for Matrix2 { #[inline] fn add(&self, other: &Matrix2) -> Matrix2 { build(|i| self.i(i).add_v(other.i(i))) } } -impl> Add, Matrix3> for Matrix3 { #[inline] fn add(&self, other: &Matrix3) -> Matrix3 { build(|i| self.i(i).add_v(other.i(i))) } } -impl> Add, Matrix4> for Matrix4 { #[inline] fn add(&self, other: &Matrix4) -> Matrix4 { build(|i| self.i(i).add_v(other.i(i))) } } +impl Add, Matrix2> for Matrix2 { #[inline] fn add(&self, other: &Matrix2) -> Matrix2 { build(|i| self.i(i).add_v(other.i(i))) } } +impl Add, Matrix3> for Matrix3 { #[inline] fn add(&self, other: &Matrix3) -> Matrix3 { build(|i| self.i(i).add_v(other.i(i))) } } +impl Add, Matrix4> for Matrix4 { #[inline] fn add(&self, other: &Matrix4) -> Matrix4 { build(|i| self.i(i).add_v(other.i(i))) } } -impl> Sub, Matrix2> for Matrix2 { #[inline] fn sub(&self, other: &Matrix2) -> Matrix2 { build(|i| self.i(i).sub_v(other.i(i))) } } -impl> Sub, Matrix3> for Matrix3 { #[inline] fn sub(&self, other: &Matrix3) -> Matrix3 { build(|i| self.i(i).sub_v(other.i(i))) } } -impl> Sub, Matrix4> for Matrix4 { #[inline] fn sub(&self, other: &Matrix4) -> Matrix4 { build(|i| self.i(i).sub_v(other.i(i))) } } +impl Sub, Matrix2> for Matrix2 { #[inline] fn sub(&self, other: &Matrix2) -> Matrix2 { build(|i| self.i(i).sub_v(other.i(i))) } } +impl Sub, Matrix3> for Matrix3 { #[inline] fn sub(&self, other: &Matrix3) -> Matrix3 { build(|i| self.i(i).sub_v(other.i(i))) } } +impl Sub, Matrix4> for Matrix4 { #[inline] fn sub(&self, other: &Matrix4) -> Matrix4 { build(|i| self.i(i).sub_v(other.i(i))) } } -impl> Neg> for Matrix2 { #[inline] fn neg(&self) -> Matrix2 { build(|i| self.i(i).neg()) } } -impl> Neg> for Matrix3 { #[inline] fn neg(&self) -> Matrix3 { build(|i| self.i(i).neg()) } } -impl> Neg> for Matrix4 { #[inline] fn neg(&self) -> Matrix4 { build(|i| self.i(i).neg()) } } +impl Neg> for Matrix2 { #[inline] fn neg(&self) -> Matrix2 { build(|i| self.i(i).neg()) } } +impl Neg> for Matrix3 { #[inline] fn neg(&self) -> Matrix3 { build(|i| self.i(i).neg()) } } +impl Neg> for Matrix4 { #[inline] fn neg(&self) -> Matrix4 { build(|i| self.i(i).neg()) } } -impl> Zero for Matrix2 { #[inline] fn zero() -> Matrix2 { Matrix2::zero() } #[inline] fn is_zero(&self) -> bool { *self == zero() } } -impl> Zero for Matrix3 { #[inline] fn zero() -> Matrix3 { Matrix3::zero() } #[inline] fn is_zero(&self) -> bool { *self == zero() } } -impl> Zero for Matrix4 { #[inline] fn zero() -> Matrix4 { Matrix4::zero() } #[inline] fn is_zero(&self) -> bool { *self == zero() } } +impl Zero for Matrix2 { #[inline] fn zero() -> Matrix2 { Matrix2::zero() } #[inline] fn is_zero(&self) -> bool { *self == zero() } } +impl Zero for Matrix3 { #[inline] fn zero() -> Matrix3 { Matrix3::zero() } #[inline] fn is_zero(&self) -> bool { *self == zero() } } +impl Zero for Matrix4 { #[inline] fn zero() -> Matrix4 { Matrix4::zero() } #[inline] fn is_zero(&self) -> bool { *self == zero() } } -impl> Mul, Matrix2> for Matrix2 { #[inline] fn mul(&self, other: &Matrix2) -> Matrix2 { build(|i| self.i(i).mul_v(other.i(i))) } } -impl> Mul, Matrix3> for Matrix3 { #[inline] fn mul(&self, other: &Matrix3) -> Matrix3 { build(|i| self.i(i).mul_v(other.i(i))) } } -impl> Mul, Matrix4> for Matrix4 { #[inline] fn mul(&self, other: &Matrix4) -> Matrix4 { build(|i| self.i(i).mul_v(other.i(i))) } } +impl Mul, Matrix2> for Matrix2 { #[inline] fn mul(&self, other: &Matrix2) -> Matrix2 { build(|i| self.i(i).mul_v(other.i(i))) } } +impl Mul, Matrix3> for Matrix3 { #[inline] fn mul(&self, other: &Matrix3) -> Matrix3 { build(|i| self.i(i).mul_v(other.i(i))) } } +impl Mul, Matrix4> for Matrix4 { #[inline] fn mul(&self, other: &Matrix4) -> Matrix4 { build(|i| self.i(i).mul_v(other.i(i))) } } -impl> One for Matrix2 { #[inline] fn one() -> Matrix2 { Matrix2::identity() } } -impl> One for Matrix3 { #[inline] fn one() -> Matrix3 { Matrix3::identity() } } -impl> One for Matrix4 { #[inline] fn one() -> Matrix4 { Matrix4::identity() } } +impl One for Matrix2 { #[inline] fn one() -> Matrix2 { Matrix2::identity() } } +impl One for Matrix3 { #[inline] fn one() -> Matrix3 { Matrix3::identity() } } +impl One for Matrix4 { #[inline] fn one() -> Matrix4 { Matrix4::identity() } } -impl> +impl Matrix, ..2], Vector2, [S, ..2]> for Matrix2 { @@ -490,7 +490,7 @@ for Matrix2 } } -impl> +impl Matrix, ..3], Vector3, [S, ..3]> for Matrix3 { @@ -563,7 +563,7 @@ macro_rules! dot_matrix4( (*$A.cr(3, $I)) * (*$B.cr($J, 3)) )) -impl> +impl Matrix, ..4], Vector4, [S, ..4]> for Matrix4 { @@ -694,24 +694,24 @@ for Matrix4 // Conversion traits /// Represents types which can be converted to a Matrix2 -pub trait ToMatrix2 { +pub trait ToMatrix2 { /// Convert this value to a Matrix2 fn to_matrix2(&self) -> Matrix2; } /// Represents types which can be converted to a Matrix3 -pub trait ToMatrix3 { +pub trait ToMatrix3 { /// Convert this value to a Matrix3 fn to_matrix3(&self) -> Matrix3; } /// Represents types which can be converted to a Matrix4 -pub trait ToMatrix4 { +pub trait ToMatrix4 { /// Convert this value to a Matrix4 fn to_matrix4(&self) -> Matrix4; } -impl> +impl ToMatrix3 for Matrix2 { /// Clone the elements of a 2-dimensional matrix into the top-left corner /// of a 3-dimensional identity matrix. @@ -722,7 +722,7 @@ ToMatrix3 for Matrix2 { } } -impl> +impl ToMatrix4 for Matrix2 { /// Clone the elements of a 2-dimensional matrix into the top-left corner /// of a 4-dimensional identity matrix. @@ -734,7 +734,7 @@ ToMatrix4 for Matrix2 { } } -impl> +impl ToMatrix4 for Matrix3 { /// Clone the elements of a 3-dimensional matrix into the top-left corner /// of a 4-dimensional identity matrix. @@ -746,7 +746,7 @@ ToMatrix4 for Matrix3 { } } -impl> +impl ToQuaternion for Matrix3 { /// Convert the matrix to a quaternion fn to_quaternion(&self) -> Quaternion { @@ -794,7 +794,7 @@ ToQuaternion for Matrix3 { } } -impl + fmt::Show> fmt::Show for Matrix2 { +impl fmt::Show for Matrix2 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "[[{}, {}], [{}, {}]]", self.cr(0, 0), self.cr(0, 1), @@ -802,7 +802,7 @@ impl + fmt::Show> fmt::Show for Matrix2 { } } -impl + fmt::Show> fmt::Show for Matrix3 { +impl fmt::Show for Matrix3 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "[[{}, {}, {}], [{}, {}, {}], [{}, {}, {}]]", self.cr(0, 0), self.cr(0, 1), self.cr(0, 2), @@ -811,7 +811,7 @@ impl + fmt::Show> fmt::Show for Matrix3 { } } -impl + fmt::Show> fmt::Show for Matrix4 { +impl fmt::Show for Matrix4 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "[[{}, {}, {}, {}], [{}, {}, {}, {}], [{}, {}, {}, {}], [{}, {}, {}, {}]]", self.cr(0, 0), self.cr(0, 1), self.cr(0, 2), self.cr(0, 3), diff --git a/src/cgmath/num.rs b/src/cgmath/num.rs new file mode 100644 index 0000000..4527f6b --- /dev/null +++ b/src/cgmath/num.rs @@ -0,0 +1,95 @@ +// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use approx::ApproxEq; + +use std::cmp; +use std::fmt; + +/// A trait providing a [partial ordering][po] +/// +/// [po]: http://mathworld.wolfram.com/PartialOrder.html +pub trait PartialOrd { + fn partial_min(self, other: Self) -> Self; + fn partial_max(self, other: Self) -> Self; +} + +macro_rules! partial_ord_int ( + ($T:ident) => ( + impl PartialOrd for $T { + fn partial_min(self, other: $T) -> $T { cmp::min(self, other) } + fn partial_max(self, other: $T) -> $T { cmp::max(self, other) } + } + ) +) + +partial_ord_int!(int) +partial_ord_int!(i8) +partial_ord_int!(i16) +partial_ord_int!(i32) +partial_ord_int!(i64) +partial_ord_int!(uint) +partial_ord_int!(u8) +partial_ord_int!(u16) +partial_ord_int!(u32) +partial_ord_int!(u64) + +macro_rules! partial_ord_float ( + ($T:ident) => ( + impl PartialOrd for $T { + fn partial_min(self, other: $T) -> $T { self.min(other) } + fn partial_max(self, other: $T) -> $T { self.max(other) } + } + ) +) + +partial_ord_float!(f32) +partial_ord_float!(f64) + +/// Base numeric types with partial ordering +pub trait BaseNum: Primitive + PartialOrd + fmt::Show {} + +impl BaseNum for i8 {} +impl BaseNum for i16 {} +impl BaseNum for i32 {} +impl BaseNum for i64 {} +impl BaseNum for int {} +impl BaseNum for u8 {} +impl BaseNum for u16 {} +impl BaseNum for u32 {} +impl BaseNum for u64 {} +impl BaseNum for uint {} +impl BaseNum for f32 {} +impl BaseNum for f64 {} + +/// Base integer types +pub trait BaseInt : BaseNum + Int {} + +impl BaseInt for i8 {} +impl BaseInt for i16 {} +impl BaseInt for i32 {} +impl BaseInt for i64 {} +impl BaseInt for int {} +impl BaseInt for u8 {} +impl BaseInt for u16 {} +impl BaseInt for u32 {} +impl BaseInt for u64 {} +impl BaseInt for uint {} + +/// Base floating point types +pub trait BaseFloat : BaseNum + FloatMath + ApproxEq + fmt::Float {} + +impl BaseFloat for f32 {} +impl BaseFloat for f64 {} diff --git a/src/cgmath/partial_ord.rs b/src/cgmath/partial_ord.rs deleted file mode 100644 index 1040d76..0000000 --- a/src/cgmath/partial_ord.rs +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2013 The CGMath Developers. For a full listing of the authors, -// refer to the AUTHORS file at the top-level directory of this distribution. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use approx::ApproxEq; -use std::cmp; - -/// A trait providing a [partial order][po] over a primitive type. -/// -/// [po]: http://mathworld.wolfram.com/PartialOrder.html -pub trait PartOrdPrim : Primitive { - fn min(&self, b: Self) -> Self; - fn max(&self, b: Self) -> Self; -} - -macro_rules! gen_minmax_for_floats ( - ( $($T:ident),+ ) => ( - $( - impl PartOrdPrim for $T { - fn min(&self, b: $T) -> $T { (*self).min(b) } - fn max(&self, b: $T) -> $T { (*self).max(b) } - } - )+ - ) -) - -macro_rules! gen_minmax_for_not_floats ( - ( $($T:ident),+ ) => ( - $( - impl PartOrdPrim for $T { - fn min(&self, b: $T) -> $T { cmp::min((*self), b) } - fn max(&self, b: $T) -> $T { cmp::max((*self), b) } - } - )+ - ) -) - -gen_minmax_for_floats!(f32, f64) -gen_minmax_for_not_floats!(int, i8, i16, i32, i64, uint, u8, u16, u32, u64) - -pub trait PartOrdFloat : FloatMath + ApproxEq + PartOrdPrim {} -impl PartOrdFloat for f32 {} -impl PartOrdFloat for f64 {} - diff --git a/src/cgmath/plane.rs b/src/cgmath/plane.rs index adf9cd0..0e28cf1 100644 --- a/src/cgmath/plane.rs +++ b/src/cgmath/plane.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The CGMath Developers. For a full listing of the authors, +// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors, // refer to the AUTHORS file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,11 +19,11 @@ use std::num::Zero; use approx::ApproxEq; use intersect::Intersect; +use num::BaseFloat; use point::{Point, Point3}; use ray::Ray3; use vector::{Vector3, Vector4}; use vector::{Vector, EuclideanVector}; -use partial_ord::PartOrdFloat; /// A 3-dimensional plane formed from the equation: `A*x + B*y + C*z - D = 0`. @@ -47,8 +47,7 @@ pub struct Plane { pub d: S, } -impl> -Plane { +impl Plane { /// Construct a plane from a normal vector and a scalar distance. The /// plane will be perpendicular to `n`, and `d` units offset from the /// origin. @@ -91,7 +90,7 @@ Plane { } } -impl> Intersect>> for (Plane, Ray3) { +impl Intersect>> for (Plane, Ray3) { fn intersection(&self) -> Option> { match *self { (ref p, ref r) => { @@ -103,19 +102,19 @@ impl> Intersect>> for (Plane, Ray3) { } } -impl Intersect>> for (Plane, Plane) { +impl Intersect>> for (Plane, Plane) { fn intersection(&self) -> Option> { fail!("Not yet implemented"); } } -impl Intersect>> for (Plane, Plane, Plane) { +impl Intersect>> for (Plane, Plane, Plane) { fn intersection(&self) -> Option> { fail!("Not yet implemented"); } } -impl> +impl> ApproxEq for Plane { #[inline] fn approx_eq_eps(&self, other: &Plane, epsilon: &S) -> bool { @@ -124,7 +123,7 @@ ApproxEq for Plane { } } -impl fmt::Show for Plane { +impl fmt::Show for Plane { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:f}x + {:f}y + {:f}z - {:f} = 0", self.n.x, self.n.y, self.n.z, self.d) diff --git a/src/cgmath/point.rs b/src/cgmath/point.rs index 32635f0..8769859 100644 --- a/src/cgmath/point.rs +++ b/src/cgmath/point.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The CGMath Developers. For a full listing of the authors, +// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors, // refer to the AUTHORS file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,8 +21,8 @@ use std::fmt; use std::num::{one, zero}; use array::*; +use num::BaseNum; use vector::*; -use partial_ord::PartOrdPrim; /// A point in 2-dimensional space. #[deriving(Eq, Clone, Hash)] @@ -33,21 +33,21 @@ pub struct Point2 { pub x: S, pub y: S } pub struct Point3 { pub x: S, pub y: S, pub z: S } -impl Point2 { +impl Point2 { #[inline] pub fn new(x: S, y: S) -> Point2 { Point2 { x: x, y: y } } } -impl Point3 { +impl Point3 { #[inline] pub fn new(x: S, y: S, z: S) -> Point3 { Point3 { x: x, y: y, z: z } } } -impl Point3 { +impl Point3 { #[inline] pub fn from_homogeneous(v: &Vector4) -> Point3 { let e = v.truncate().mul_s(one::() / v.w); @@ -63,7 +63,7 @@ impl Point3 { /// Specifies the numeric operations for point types. pub trait Point < - S: PartOrdPrim, + S: BaseNum, V: Vector, Slice > @@ -109,16 +109,16 @@ pub trait Point array!(impl Point2 -> [S, ..2] _2) array!(impl Point3 -> [S, ..3] _3) -impl Point, [S, ..2]> for Point2 {} -impl Point, [S, ..3]> for Point3 {} +impl Point, [S, ..2]> for Point2 {} +impl Point, [S, ..3]> for Point3 {} -impl fmt::Show for Point2 { +impl fmt::Show for Point2 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "[{}, {}]", self.x, self.y) } } -impl fmt::Show for Point3 { +impl fmt::Show for Point3 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "[{}, {}, {}]", self.x, self.y, self.z) } diff --git a/src/cgmath/projection.rs b/src/cgmath/projection.rs index 5cdf168..7daee8e 100644 --- a/src/cgmath/projection.rs +++ b/src/cgmath/projection.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The CGMath Developers. For a full listing of the authors, +// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors, // refer to the AUTHORS file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,14 +18,14 @@ use std::num::{zero, one, cast}; use angle::{Angle, tan, cot}; use frustum::Frustum; use matrix::{Matrix4, ToMatrix4}; +use num::BaseFloat; use plane::Plane; -use partial_ord::PartOrdFloat; /// Create a perspective projection matrix. /// /// This is the equivalent to the [gluPerspective] /// (http://www.opengl.org/sdk/docs/man2/xhtml/gluPerspective.xml) function. -pub fn perspective>(fovy: A, aspect: S, near: S, far: S) -> Matrix4 { +pub fn perspective>(fovy: A, aspect: S, near: S, far: S) -> Matrix4 { PerspectiveFov { fovy: fovy, aspect: aspect, @@ -38,7 +38,7 @@ pub fn perspective>(fovy: A, aspect: S, near: S, far: /// /// This is the equivalent of the now deprecated [glFrustrum] /// (http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml) function. -pub fn frustum(left: S, right: S, bottom: S, top: S, near: S, far: S) -> Matrix4 { +pub fn frustum(left: S, right: S, bottom: S, top: S, near: S, far: S) -> Matrix4 { Perspective { left: left, right: right, @@ -53,7 +53,7 @@ pub fn frustum(left: S, right: S, bottom: S, top: S, near: S, far: S) /// /// This is the equivalent of the now deprecated [glOrtho] /// (http://www.opengl.org/sdk/docs/man2/xhtml/glOrtho.xml) function. -pub fn ortho(left: S, right: S, bottom: S, top: S, near: S, far: S) -> Matrix4 { +pub fn ortho(left: S, right: S, bottom: S, top: S, near: S, far: S) -> Matrix4 { Ortho { left: left, right: right, @@ -77,7 +77,7 @@ pub struct PerspectiveFov { pub far: S, } -impl> PerspectiveFov { +impl> PerspectiveFov { pub fn to_perspective(&self) -> Perspective { let angle = self.fovy.div_s(cast(2).unwrap()); let ymax = self.near * tan(angle.to_rad()); @@ -94,15 +94,14 @@ impl> PerspectiveFov { } } -impl, A: Angle> -Projection for PerspectiveFov { +impl> Projection for PerspectiveFov { fn to_frustum(&self) -> Frustum { // TODO: Could this be faster? Frustum::from_matrix4(self.to_matrix4()) } } -impl> ToMatrix4 for PerspectiveFov { +impl> ToMatrix4 for PerspectiveFov { fn to_matrix4(&self) -> Matrix4 { let half_turn: A = Angle::turn_div_2(); @@ -151,15 +150,14 @@ pub struct Perspective { pub near: S, far: S, } -impl> -Projection for Perspective { +impl Projection for Perspective { fn to_frustum(&self) -> Frustum { // TODO: Could this be faster? Frustum::from_matrix4(self.to_matrix4()) } } -impl ToMatrix4 for Perspective { +impl ToMatrix4 for Perspective { fn to_matrix4(&self) -> Matrix4 { assert!(self.left > self.right, "`left` cannot be greater than `right`, found: left: {:?} right: {:?}", self.left, self.right); assert!(self.bottom > self.top, "`bottom` cannot be greater than `top`, found: bottom: {:?} top: {:?}", self.bottom, self.top); @@ -202,8 +200,7 @@ pub struct Ortho { pub near: S, far: S, } -impl> -Projection for Ortho { +impl Projection for Ortho { fn to_frustum(&self) -> Frustum { Frustum { left: Plane::from_abcd( one::(), zero::(), zero::(), self.left.clone()), @@ -216,7 +213,7 @@ Projection for Ortho { } } -impl ToMatrix4 for Ortho { +impl ToMatrix4 for Ortho { fn to_matrix4(&self) -> Matrix4 { assert!(self.left < self.right, "`left` cannot be greater than `right`, found: left: {:?} right: {:?}", self.left, self.right); assert!(self.bottom < self.top, "`bottom` cannot be greater than `top`, found: bottom: {:?} top: {:?}", self.bottom, self.top); diff --git a/src/cgmath/quaternion.rs b/src/cgmath/quaternion.rs index 4d71623..9783859 100644 --- a/src/cgmath/quaternion.rs +++ b/src/cgmath/quaternion.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The CGMath Developers. For a full listing of the authors, +// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors, // refer to the AUTHORS file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,13 +17,12 @@ use std::fmt; use std::num::{zero, one, cast}; use angle::{Angle, Rad, acos, sin, sin_cos}; -use approx::ApproxEq; use array::{Array, build}; use matrix::{Matrix3, ToMatrix3, ToMatrix4, Matrix4}; +use num::BaseFloat; use point::{Point3}; use rotation::{Rotation, Rotation3, Basis3, ToBasis3}; use vector::{Vector3, Vector, EuclideanVector}; -use partial_ord::PartOrdFloat; /// A [quaternion](https://en.wikipedia.org/wiki/Quaternion) in scalar/vector /// form. @@ -33,13 +32,12 @@ pub struct Quaternion { pub s: S, pub v: Vector3 } array!(impl Quaternion -> [S, ..4] _4) /// Represents types which can be expressed as a quaternion. -pub trait ToQuaternion { +pub trait ToQuaternion { /// Convert this value to a quaternion. fn to_quaternion(&self) -> Quaternion; } -impl> -Quaternion { +impl Quaternion { /// Construct a new quaternion from one scalar component and three /// imaginary components #[inline] @@ -178,8 +176,7 @@ Quaternion { } } -impl> -Quaternion { +impl Quaternion { /// Spherical Linear Intoperlation /// /// Return the spherical linear interpolation between the quaternion and @@ -228,8 +225,7 @@ Quaternion { } } -impl> -ToMatrix3 for Quaternion { +impl ToMatrix3 for Quaternion { /// Convert the quaternion to a 3 x 3 rotation matrix fn to_matrix3(&self) -> Matrix3 { let x2 = self.v.x + self.v.x; @@ -254,8 +250,7 @@ ToMatrix3 for Quaternion { } } -impl> -ToMatrix4 for Quaternion { +impl ToMatrix4 for Quaternion { /// Convert the quaternion to a 4 x 4 rotation matrix fn to_matrix4(&self) -> Matrix4 { let x2 = self.v.x + self.v.x; @@ -281,15 +276,14 @@ ToMatrix4 for Quaternion { } } -impl> -Neg> for Quaternion { +impl Neg> for Quaternion { #[inline] fn neg(&self) -> Quaternion { Quaternion::from_sv(-self.s, -self.v) } } -impl fmt::Show for Quaternion { +impl fmt::Show for Quaternion { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{} + {}i + {}j + {}k", self.s, @@ -301,19 +295,17 @@ impl fmt::Show for Quaternion { // Quaternion Rotation impls -impl> -ToBasis3 for Quaternion { +impl ToBasis3 for Quaternion { #[inline] fn to_rot3(&self) -> Basis3 { Basis3::from_quaternion(self) } } -impl ToQuaternion for Quaternion { +impl ToQuaternion for Quaternion { #[inline] fn to_quaternion(&self) -> Quaternion { self.clone() } } -impl> -Rotation, Point3> for Quaternion { +impl Rotation, Point3> for Quaternion { #[inline] fn identity() -> Quaternion { Quaternion::identity() } @@ -345,9 +337,7 @@ Rotation, Point3> for Quaternion { fn invert_self(&mut self) { *self = self.invert() } } -impl> -Rotation3 for Quaternion -{ +impl Rotation3 for Quaternion { #[inline] fn from_axis_angle(axis: &Vector3, angle: Rad) -> Quaternion { let (s, c) = sin_cos(angle.mul_s(cast(0.5).unwrap())); diff --git a/src/cgmath/ray.rs b/src/cgmath/ray.rs index 69cb118..9e1d028 100644 --- a/src/cgmath/ray.rs +++ b/src/cgmath/ray.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The CGMath Developers. For a full listing of the authors, +// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors, // refer to the AUTHORS file at the top-level directionectory of this distribution. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,30 +13,23 @@ // See the License for the specific language governing permissions and // limitations under the License. +use num::BaseNum; use point::{Point, Point2, Point3}; use vector::{Vector, Vector2, Vector3}; /// A generic ray starting at `origin` and extending infinitely in /// `direction`. #[deriving(Clone, Eq)] -pub struct Ray -{ +pub struct Ray { pub origin: P, pub direction: V, } -impl -< - S: Primitive, - Slice, - V: Vector, - P: Point -> Ray -{ +impl, P: Point> Ray { pub fn new(origin: P, direction: V) -> Ray { - Ray { origin:origin, direction:direction } + Ray { origin: origin, direction: direction } } } -pub type Ray2 = Ray,Vector2>; -pub type Ray3 = Ray,Vector3>; +pub type Ray2 = Ray, Vector2>; +pub type Ray3 = Ray, Vector3>; diff --git a/src/cgmath/rotation.rs b/src/cgmath/rotation.rs index 5fe8b47..b80f0ca 100644 --- a/src/cgmath/rotation.rs +++ b/src/cgmath/rotation.rs @@ -19,17 +19,17 @@ use array::Array; use matrix::Matrix; use matrix::{Matrix2, ToMatrix2}; use matrix::{Matrix3, ToMatrix3}; +use num::{BaseNum, BaseFloat}; use point::{Point, Point2, Point3}; use quaternion::{Quaternion, ToQuaternion}; use ray::Ray; use vector::{Vector, Vector2, Vector3}; -use partial_ord::{PartOrdPrim, PartOrdFloat}; /// A trait for a generic rotation. A rotation is a transformation that /// creates a circular motion, and preserves at least one point in the space. pub trait Rotation < - S: PartOrdPrim, + S: BaseNum, Slice, V: Vector, P: Point @@ -102,7 +102,7 @@ pub trait Rotation2 /// A three-dimensional rotation. pub trait Rotation3 < - S: Primitive + S: BaseNum > : Rotation, Point3> + ToMatrix3 @@ -152,30 +152,29 @@ pub struct Basis2 { mat: Matrix2 } -impl Basis2 { +impl Basis2 { /// Coerce to a `Matrix2` #[inline] pub fn as_matrix2<'a>(&'a self) -> &'a Matrix2 { &'a self.mat } } /// Represents types which can be converted to a rotation matrix. -pub trait ToBasis2 { +pub trait ToBasis2 { /// Convert this type to a rotation matrix. fn to_rot2(&self) -> Basis2; } -impl ToBasis2 for Basis2 { +impl ToBasis2 for Basis2 { #[inline] fn to_rot2(&self) -> Basis2 { self.clone() } } -impl ToMatrix2 for Basis2 { +impl ToMatrix2 for Basis2 { #[inline] fn to_matrix2(&self) -> Matrix2 { self.mat.clone() } } -impl> -Rotation, Point2> for Basis2 { +impl Rotation, Point2> for Basis2 { #[inline] fn identity() -> Basis2 { Basis2{ mat: Matrix2::identity() } } @@ -209,16 +208,14 @@ Rotation, Point2> for Basis2 { fn invert_self(&mut self) { self.mat.invert_self(); } } -impl> -ApproxEq for Basis2 { +impl ApproxEq for Basis2 { #[inline] fn approx_eq_eps(&self, other: &Basis2, epsilon: &S) -> bool { self.mat.approx_eq_eps(&other.mat, epsilon) } } -impl> -Rotation2 for Basis2 { +impl Rotation2 for Basis2 { fn from_angle(theta: Rad) -> Basis2 { Basis2 { mat: Matrix2::from_angle(theta) } } } @@ -233,8 +230,7 @@ pub struct Basis3 { mat: Matrix3 } -impl> -Basis3 { +impl Basis3 { /// Create a new rotation matrix from a quaternion. #[inline] pub fn from_quaternion(quaternion: &Quaternion) -> Basis3 { @@ -247,29 +243,27 @@ Basis3 { } /// Represents types which can be converted to a rotation matrix. -pub trait ToBasis3 { +pub trait ToBasis3 { /// Convert this type to a rotation matrix. fn to_rot3(&self) -> Basis3; } -impl ToBasis3 for Basis3 { +impl ToBasis3 for Basis3 { #[inline] fn to_rot3(&self) -> Basis3 { self.clone() } } -impl ToMatrix3 for Basis3 { +impl ToMatrix3 for Basis3 { #[inline] fn to_matrix3(&self) -> Matrix3 { self.mat.clone() } } -impl> -ToQuaternion for Basis3 { +impl ToQuaternion for Basis3 { #[inline] fn to_quaternion(&self) -> Quaternion { self.mat.to_quaternion() } } -impl> -Rotation, Point3> for Basis3 { +impl Rotation, Point3> for Basis3 { #[inline] fn identity() -> Basis3 { Basis3{ mat: Matrix3::identity() } } @@ -304,16 +298,14 @@ Rotation, Point3> for Basis3 { fn invert_self(&mut self) { self.mat.invert_self(); } } -impl> -ApproxEq for Basis3 { +impl ApproxEq for Basis3 { #[inline] fn approx_eq_eps(&self, other: &Basis3, epsilon: &S) -> bool { self.mat.approx_eq_eps(&other.mat, epsilon) } } -impl> -Rotation3 for Basis3 { +impl Rotation3 for Basis3 { fn from_axis_angle(axis: &Vector3, angle: Rad) -> Basis3 { Basis3 { mat: Matrix3::from_axis_angle(axis, angle) } } diff --git a/src/cgmath/sphere.rs b/src/cgmath/sphere.rs index 191e1fd..446491a 100644 --- a/src/cgmath/sphere.rs +++ b/src/cgmath/sphere.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The CGMath Developers. For a full listing of the authors, +// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors, // refer to the AUTHORS file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,17 +16,12 @@ //! Bounding sphere use intersect::Intersect; +use num::BaseFloat; use point::{Point, Point3}; use ray::Ray3; use vector::Vector; -use partial_ord::PartOrdFloat; -use std::num::NumCast; -use std::num; - -fn cast(n: T) -> U { - num::cast(n).unwrap() -} +use std::num::zero; #[deriving(Clone, Eq)] pub struct Sphere { @@ -34,13 +29,13 @@ pub struct Sphere { pub radius: S, } -impl> Intersect>> for (Sphere, Ray3) { +impl Intersect>> for (Sphere, Ray3) { fn intersection(&self) -> Option> { match *self { (ref s, ref r) => { let l = s.center.sub_p(&r.origin); let tca = l.dot(&r.direction); - if tca < cast(0.0) { return None; } + if tca < 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/cgmath/transform.rs b/src/cgmath/transform.rs index e7df504..7c91d81 100644 --- a/src/cgmath/transform.rs +++ b/src/cgmath/transform.rs @@ -19,19 +19,19 @@ use std::num::one; use approx::ApproxEq; use matrix::{Matrix, Matrix4, ToMatrix4}; +use num::{BaseNum, BaseFloat}; use point::{Point, Point3}; use ray::Ray; use rotation::{Rotation, Rotation3}; use quaternion::Quaternion; use vector::{Vector, Vector3}; -use partial_ord::{PartOrdPrim, PartOrdFloat}; /// A trait representing an [affine /// transformation](https://en.wikipedia.org/wiki/Affine_transformation) that /// can be applied to points or vectors. An affine transformation is one which pub trait Transform < - S: PartOrdPrim, + S: BaseNum, Slice, V: Vector, P: Point @@ -94,7 +94,7 @@ pub struct Decomposed { impl < - S: PartOrdFloat, + S: BaseFloat, Slice, V: Vector, P: Point, @@ -162,8 +162,7 @@ pub trait Transform3 + ToMatrix4 {} -impl, R: Rotation3> -ToMatrix4 for Decomposed, R> { +impl> ToMatrix4 for Decomposed, R> { fn to_matrix4(&self) -> Matrix4 { let mut m = self.rot.to_matrix3().mul_s( self.scale.clone() ).to_matrix4(); m.w = self.disp.extend( num::one() ); @@ -171,11 +170,9 @@ ToMatrix4 for Decomposed, R> { } } -impl, R: Rotation3> -Transform3 for Decomposed,R> {} +impl> Transform3 for Decomposed,R> {} -impl> -fmt::Show for Decomposed,R> { +impl> fmt::Show for Decomposed,R> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "(scale({}), rot({}), disp{})", self.scale, self.rot, self.disp) @@ -188,8 +185,7 @@ pub struct AffineMatrix3 { pub mat: Matrix4, } -impl> -Transform, Point3> for AffineMatrix3 { +impl Transform, Point3> for AffineMatrix3 { #[inline] fn identity() -> AffineMatrix3 { AffineMatrix3 { mat: Matrix4::identity() } @@ -221,20 +217,18 @@ Transform, Point3> for AffineMatrix3 { } } -impl -ToMatrix4 for AffineMatrix3 { +impl ToMatrix4 for AffineMatrix3 { #[inline] fn to_matrix4(&self) -> Matrix4 { self.mat.clone() } } -impl> -Transform3 for AffineMatrix3 {} +impl Transform3 for AffineMatrix3 {} /// A transformation in three dimensions consisting of a rotation, /// displacement vector and scale amount. pub struct Transform3D( Decomposed,Quaternion> ); -impl> Transform3D { +impl Transform3D { #[inline] pub fn new(scale: S, rot: Quaternion, disp: Vector3) -> Transform3D { Transform3D( Decomposed { scale: scale, rot: rot, disp: disp }) @@ -252,6 +246,6 @@ impl> Transform3D { } } -impl> ToMatrix4 for Transform3D { +impl ToMatrix4 for Transform3D { fn to_matrix4(&self) -> Matrix4 { self.get().to_matrix4() } } diff --git a/src/cgmath/vector.rs b/src/cgmath/vector.rs index 95b689a..14dfb7e 100644 --- a/src/cgmath/vector.rs +++ b/src/cgmath/vector.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The CGMath Developers. For a full listing of the authors, +// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors, // refer to the AUTHORS file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,14 +19,14 @@ use std::num::{Zero, zero, One, one}; use angle::{Rad, atan2, acos}; use approx::ApproxEq; use array::{Array, build}; -use partial_ord::{PartOrdPrim, PartOrdFloat}; +use num::{BaseNum, BaseFloat, PartialOrd}; /// A trait that specifies a range of numeric operations for vectors. Not all /// of these make sense from a linear algebra point of view, but are included /// for pragmatic reasons. pub trait Vector < - S: PartOrdPrim, + S: BaseNum, Slice > : Array @@ -90,14 +90,14 @@ pub trait Vector #[inline] fn dot(&self, other: &Self) -> S { self.mul_v(other).comp_add() } /// The minimum component of the vector. - #[inline] fn comp_min(&self) -> S { self.fold(|a, b| if *a < *b { *a } else {*b }) } + #[inline] fn comp_min(&self) -> S { self.fold(|a, b| a.partial_min(*b)) } /// The maximum component of the vector. - #[inline] fn comp_max(&self) -> S { self.fold(|a, b| if *a > *b { *a } else {*b }) } + #[inline] fn comp_max(&self) -> S { self.fold(|a, b| a.partial_max(*b)) } } /// Dot product of two vectors. -#[inline] pub fn dot>(a: V, b: V) -> S { a.dot(&b) } +#[inline] pub fn dot>(a: V, b: V) -> S { a.dot(&b) } // Utility macro for generating associated functions for the vectors macro_rules! vec( @@ -105,7 +105,7 @@ macro_rules! vec( #[deriving(Eq, TotalEq, Clone, Hash)] pub struct $Self { $(pub $field: S),+ } - impl<$S: Primitive> $Self<$S> { + impl<$S: BaseNum> $Self<$S> { /// Construct a new vector, using the provided values. #[inline] pub fn new($($field: $S),+) -> $Self<$S> { @@ -127,32 +127,32 @@ macro_rules! vec( pub fn ident() -> $Self<$S> { $Self::from_value(one()) } } - impl Add<$Self, $Self> for $Self { + impl Add<$Self, $Self> for $Self { #[inline] fn add(&self, other: &$Self) -> $Self { self.add_v(other) } } - impl Sub<$Self, $Self> for $Self { + impl Sub<$Self, $Self> for $Self { #[inline] fn sub(&self, other: &$Self) -> $Self { self.sub_v(other) } } - impl Zero for $Self { + impl Zero for $Self { #[inline] fn zero() -> $Self { $Self::from_value(zero()) } #[inline] fn is_zero(&self) -> bool { *self == zero() } } - impl Neg<$Self> for $Self { + impl Neg<$Self> for $Self { #[inline] fn neg(&self) -> $Self { build(|i| self.i(i).neg()) } } - impl Mul<$Self, $Self> for $Self { + impl Mul<$Self, $Self> for $Self { #[inline] fn mul(&self, other: &$Self) -> $Self { self.mul_v(other) } } - impl One for $Self { + impl One for $Self { #[inline] fn one() -> $Self { $Self::from_value(one()) } } - impl Vector for $Self {} + impl Vector for $Self {} ) ) @@ -165,7 +165,7 @@ array!(impl Vector3 -> [S, ..3] _3) array!(impl Vector4 -> [S, ..4] _4) /// Operations specific to numeric two-dimensional vectors. -impl Vector2 { +impl Vector2 { /// A unit vector in the `x` direction. #[inline] pub fn unit_x() -> Vector2 { Vector2::new(one(), zero()) } /// A unit vector in the `y` direction. @@ -186,7 +186,7 @@ impl Vector2 { } /// Operations specific to numeric three-dimensional vectors. -impl Vector3 { +impl Vector3 { /// A unit vector in the `x` direction. #[inline] pub fn unit_x() -> Vector3 { Vector3::new(one(), zero(), zero()) } /// A unit vector in the `y` direction. @@ -224,7 +224,7 @@ impl Vector3 { } /// Operations specific to numeric four-dimensional vectors. -impl Vector4 { +impl Vector4 { /// A unit vector in the `x` direction. #[inline] pub fn unit_x() -> Vector4 { Vector4::new(one(), zero(), zero(), zero()) } /// A unit vector in the `y` direction. @@ -245,7 +245,7 @@ impl Vector4 { /// 2-dimensional and 3-dimensional vectors. pub trait EuclideanVector < - S: PartOrdFloat, + S: BaseFloat, Slice > : Vector @@ -316,7 +316,7 @@ pub trait EuclideanVector } } -impl> +impl EuclideanVector for Vector2 { #[inline] fn angle(&self, other: &Vector2) -> Rad { @@ -324,7 +324,7 @@ EuclideanVector for Vector2 { } } -impl> +impl EuclideanVector for Vector3 { #[inline] fn angle(&self, other: &Vector3) -> Rad { @@ -332,7 +332,7 @@ EuclideanVector for Vector3 { } } -impl> +impl EuclideanVector for Vector4 { #[inline] fn angle(&self, other: &Vector4) -> Rad {