Merge pull request #21 from kvark/try-approx

Reimplement ApproxEq that has been removed from the standard library
This commit is contained in:
Brendan Zabarauskas 2014-01-10 09:17:30 -08:00
commit 19d9c356fd
20 changed files with 227 additions and 179 deletions

View file

@ -10,13 +10,13 @@ The library provides:
- square matrices: `Mat2`, `Mat3`, `Mat4` - square matrices: `Mat2`, `Mat3`, `Mat4`
- a quaternion type: `Quat` - a quaternion type: `Quat`
- rotation matrices: `Rot2`, `Rot3` - rotation matrices: `Rot2`, `Rot3`
- rotations: `Euler`, `AxisAngle`
- angle units: `Rad`, `Deg` - angle units: `Rad`, `Deg`
- points: `Point2`, `Point3` - points: `Point2`, `Point3`
- rays: `Ray2`, `Ray3` - rays: `Ray2`, `Ray3`
- plane: `Plane` - a plane: `Plane`
- perspective projections: `Perspective`, `PerspectiveFov`, `Ortho` - perspective projections: `Perspective`, `PerspectiveFov`, `Ortho`
- a view frustum: `Frustrum` - a view frustum: `Frustrum`
- spatial transformations: `AffineMatrix3`, `Transform3D`
- axis-aligned bounding boxes: `Aabb2`, `Aabb3` - axis-aligned bounding boxes: `Aabb2`, `Aabb3`
- oriented bounding boxes: `Obb2`, `Obb3` - oriented bounding boxes: `Obb2`, `Obb3`
- collision primitives: `Sphere`, `Cylinder` - collision primitives: `Sphere`, `Cylinder`

View file

@ -21,6 +21,8 @@ pub use std::num::{asinh, acosh, atanh};
use std::fmt; use std::fmt;
use std::num::{Zero, zero, cast}; use std::num::{Zero, zero, cast};
use approx::ApproxEq;
#[deriving(Clone, Eq, Ord, Zero)] pub struct Rad<S> { s: S } #[deriving(Clone, Eq, Ord, Zero)] pub struct Rad<S> { s: S }
#[deriving(Clone, Eq, Ord, Zero)] pub struct Deg<S> { s: S } #[deriving(Clone, Eq, Ord, Zero)] pub struct Deg<S> { s: S }
@ -126,7 +128,8 @@ pub trait Angle
#[inline] pub fn bisect<S: Float, A: Angle<S>>(a: A, b: A) -> A { a.bisect(b) } #[inline] pub fn bisect<S: Float, A: Angle<S>>(a: A, b: A) -> A { a.bisect(b) }
impl<S: Float> Rad<S> { impl<S: Float + ApproxEq<S>>
Rad<S> {
#[inline] pub fn zero() -> Rad<S> { zero() } #[inline] pub fn zero() -> Rad<S> { zero() }
#[inline] pub fn full_turn() -> Rad<S> { Angle::full_turn() } #[inline] pub fn full_turn() -> Rad<S> { Angle::full_turn() }
#[inline] pub fn turn_div_2() -> Rad<S> { Angle::turn_div_2() } #[inline] pub fn turn_div_2() -> Rad<S> { Angle::turn_div_2() }
@ -135,7 +138,8 @@ impl<S: Float> Rad<S> {
#[inline] pub fn turn_div_6() -> Rad<S> { Angle::turn_div_6() } #[inline] pub fn turn_div_6() -> Rad<S> { Angle::turn_div_6() }
} }
impl<S: Float> Deg<S> { impl<S: Float + ApproxEq<S>>
Deg<S> {
#[inline] pub fn zero() -> Deg<S> { zero() } #[inline] pub fn zero() -> Deg<S> { zero() }
#[inline] pub fn full_turn() -> Deg<S> { Angle::full_turn() } #[inline] pub fn full_turn() -> Deg<S> { Angle::full_turn() }
#[inline] pub fn turn_div_2() -> Deg<S> { Angle::turn_div_2() } #[inline] pub fn turn_div_2() -> Deg<S> { Angle::turn_div_2() }
@ -144,24 +148,28 @@ impl<S: Float> Deg<S> {
#[inline] pub fn turn_div_6() -> Deg<S> { Angle::turn_div_6() } #[inline] pub fn turn_div_6() -> Deg<S> { Angle::turn_div_6() }
} }
impl<S: Float> Equiv<Rad<S>> for Rad<S> { impl<S: Float + ApproxEq<S>>
Equiv<Rad<S>> for Rad<S> {
fn equiv(&self, other: &Rad<S>) -> bool { fn equiv(&self, other: &Rad<S>) -> bool {
self.normalize() == other.normalize() self.normalize() == other.normalize()
} }
} }
impl<S: Float> Equiv<Deg<S>> for Deg<S> { impl<S: Float + ApproxEq<S>>
Equiv<Deg<S>> for Deg<S> {
fn equiv(&self, other: &Deg<S>) -> bool { fn equiv(&self, other: &Deg<S>) -> bool {
self.normalize() == other.normalize() self.normalize() == other.normalize()
} }
} }
impl<S: Float> Angle<S> for Rad<S> { impl<S: Float + ApproxEq<S>>
Angle<S> for Rad<S> {
#[inline] fn from<A: Angle<S>>(theta: A) -> Rad<S> { theta.to_rad() } #[inline] fn from<A: Angle<S>>(theta: A) -> Rad<S> { theta.to_rad() }
#[inline] fn full_turn() -> Rad<S> { rad(Real::two_pi()) } #[inline] fn full_turn() -> Rad<S> { rad(Real::two_pi()) }
} }
impl<S: Float> Angle<S> for Deg<S> { impl<S: Float + ApproxEq<S>>
Angle<S> for Deg<S> {
#[inline] fn from<A: Angle<S>>(theta: A) -> Deg<S> { theta.to_deg() } #[inline] fn from<A: Angle<S>>(theta: A) -> Deg<S> { theta.to_deg() }
#[inline] fn full_turn() -> Deg<S> { deg(cast(360).unwrap()) } #[inline] fn full_turn() -> Deg<S> { deg(cast(360).unwrap()) }
} }
@ -183,38 +191,18 @@ impl<S: Float> Angle<S> for Deg<S> {
impl<S: Float + fmt::Default> ToStr for Rad<S> { fn to_str(&self) -> ~str { format!("{} rad", self.s) } } impl<S: Float + fmt::Default> ToStr for Rad<S> { fn to_str(&self) -> ~str { format!("{} rad", self.s) } }
impl<S: Float + fmt::Default> ToStr for Deg<S> { fn to_str(&self) -> ~str { format!("{}°", self.s) } } impl<S: Float + fmt::Default> ToStr for Deg<S> { fn to_str(&self) -> ~str { format!("{}°", self.s) } }
impl<S: Float> ApproxEq<S> for Rad<S> { impl<S: Float + ApproxEq<S>>
ApproxEq<S> for Rad<S> {
#[inline] #[inline]
fn approx_epsilon() -> S { fn approx_eq_eps(&self, other: &Rad<S>, epsilon: &S) -> bool {
// TODO: fix this after static methods are fixed in rustc self.s.approx_eq_eps(&other.s, epsilon)
fail!(~"Doesn't work!");
}
#[inline]
fn approx_eq(&self, other: &Rad<S>) -> bool {
self.s.approx_eq(&other.s)
}
#[inline]
fn approx_eq_eps(&self, other: &Rad<S>, approx_epsilon: &S) -> bool {
self.s.approx_eq_eps(&other.s, approx_epsilon)
} }
} }
impl<S: Float> ApproxEq<S> for Deg<S> { impl<S: Float + ApproxEq<S>>
ApproxEq<S> for Deg<S> {
#[inline] #[inline]
fn approx_epsilon() -> S { fn approx_eq_eps(&self, other: &Deg<S>, epsilon: &S) -> bool {
// TODO: fix this after static methods are fixed in rustc self.s.approx_eq_eps(&other.s, epsilon)
fail!(~"Doesn't work!");
}
#[inline]
fn approx_eq(&self, other: &Deg<S>) -> bool {
self.s.approx_eq(&other.s)
}
#[inline]
fn approx_eq_eps(&self, other: &Deg<S>, approx_epsilon: &S) -> bool {
self.s.approx_eq_eps(&other.s, approx_epsilon)
} }
} }

77
src/cgmath/approx.rs Normal file
View file

@ -0,0 +1,77 @@
// 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 std::num;
use array::Array;
use matrix::{Mat2, Mat3, Mat4};
use point::{Point2, Point3};
use quaternion::Quat;
use vector::{Vec2, Vec3, Vec4};
pub trait ApproxEq<T: Float> {
fn approx_epsilon(_hack: Option<Self>) -> T {
num::cast(1.0e-5).unwrap()
}
fn approx_eq(&self, other: &Self) -> bool {
let eps: T = ApproxEq::approx_epsilon(None::<Self>);
self.approx_eq_eps(other, &eps)
}
fn approx_eq_eps(&self, other: &Self, epsilon: &T) -> bool;
}
macro_rules! approx_simple(
($S:ident) => (
impl ApproxEq<$S> for $S {
#[inline]
fn approx_eq_eps(&self, other: &$S, epsilon: &$S) -> bool {
num::abs(*self - *other) < *epsilon
}
}
)
)
approx_simple!(f32)
approx_simple!(f64)
macro_rules! approx_array(
(impl<$S:ident> $Self:ty) => (
impl<$S: Float + Clone + ApproxEq<$S>> ApproxEq<$S> for $Self {
#[inline]
fn approx_eq_eps(&self, other: &$Self, epsilon: &$S) -> bool {
self.iter().zip(other.iter())
.all(|(a, b)| a.approx_eq_eps(b, epsilon))
}
}
)
)
approx_array!(impl<S> Mat2<S>)
approx_array!(impl<S> Mat3<S>)
approx_array!(impl<S> Mat4<S>)
approx_array!(impl<S> Quat<S>)
approx_array!(impl<S> Vec2<S>)
approx_array!(impl<S> Vec3<S>)
approx_array!(impl<S> Vec4<S>)
approx_array!(impl<S> Point2<S>)
approx_array!(impl<S> Point3<S>)

View file

@ -123,26 +123,3 @@ macro_rules! gen_each_mut(
(_4) => ({ f(0, self.mut_i(0)); f(1, self.mut_i(1)); f(2, self.mut_i(2)); f(3, self.mut_i(3)); }); (_4) => ({ f(0, self.mut_i(0)); f(1, self.mut_i(1)); f(2, self.mut_i(2)); f(3, self.mut_i(3)); });
) )
macro_rules! approx_eq(
(impl<$S:ident> $Self:ty) => (
impl<$S: Clone + ApproxEq<$S>> ApproxEq<$S> for $Self {
#[inline]
fn approx_epsilon() -> $S {
// TODO: fix this after static methods are fixed in rustc
fail!(~"Doesn't work!");
}
#[inline]
fn approx_eq(&self, other: &$Self) -> bool {
self.iter().zip(other.iter())
.all(|(a, b)| a.approx_eq(b))
}
#[inline]
fn approx_eq_eps(&self, other: &$Self, approx_epsilon: &$S) -> bool {
self.iter().zip(other.iter())
.all(|(a, b)| a.approx_eq_eps(b, approx_epsilon))
}
}
)
)

View file

@ -15,6 +15,7 @@
//! View frustum for visibility determination //! View frustum for visibility determination
use approx::ApproxEq;
use matrix::{Matrix, Mat4}; use matrix::{Matrix, Mat4};
use plane::Plane; use plane::Plane;
use point::Point3; use point::Point3;
@ -30,7 +31,8 @@ pub struct Frustum<S> {
far: Plane<S>, far: Plane<S>,
} }
impl<S: Float> Frustum<S> { impl<S: Float + ApproxEq<S>>
Frustum<S> {
/// Constructs a frustum /// Constructs a frustum
pub fn new(left: Plane<S>, right: Plane<S>, pub fn new(left: Plane<S>, right: Plane<S>,
bottom: Plane<S>, top: Plane<S>, bottom: Plane<S>, top: Plane<S>,

View file

@ -13,10 +13,10 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#[pkgid="cgmath#0.1"]; #[crate_id="cgmath#0.1"];
#[crate_type = "lib"];
#[comment = "A mathematics library for computer graphics."]; #[comment = "A mathematics library for computer graphics."];
#[license = "ASL2"]; #[license = "ASL2"];
#[crate_type = "lib"];
#[feature(globs)]; #[feature(globs)];
#[feature(macro_rules)]; #[feature(macro_rules)];
@ -42,4 +42,5 @@ pub mod intersect;
pub mod obb; pub mod obb;
pub mod sphere; pub mod sphere;
pub mod approx;
pub mod ptr; pub mod ptr;

View file

@ -18,6 +18,7 @@
use std::num::{Zero, zero, One, one, cast, sqrt}; use std::num::{Zero, zero, One, one, cast, sqrt};
use angle::{Rad, sin, cos, sin_cos}; use angle::{Rad, sin, cos, sin_cos};
use approx::ApproxEq;
use array::{Array, build}; use array::{Array, build};
use point::{Point, Point3}; use point::{Point, Point3};
use quaternion::{Quat, ToQuat}; use quaternion::{Quat, ToQuat};
@ -36,9 +37,6 @@ pub struct Mat3<S> { x: Vec3<S>, y: Vec3<S>, z: Vec3<S> }
#[deriving(Clone, Eq, Zero)] #[deriving(Clone, Eq, Zero)]
pub struct Mat4<S> { x: Vec4<S>, y: Vec4<S>, z: Vec4<S>, w: Vec4<S> } pub struct Mat4<S> { x: Vec4<S>, y: Vec4<S>, z: Vec4<S>, w: Vec4<S> }
approx_eq!(impl<S> Mat2<S>)
approx_eq!(impl<S> Mat3<S>)
approx_eq!(impl<S> Mat4<S>)
impl<S: Primitive> Mat2<S> { impl<S: Primitive> Mat2<S> {
#[inline] #[inline]
@ -114,7 +112,8 @@ impl<S: Primitive> Mat3<S> {
} }
} }
impl<S: Float> Mat3<S> { impl<S: Float + ApproxEq<S>>
Mat3<S> {
pub fn look_at(dir: &Vec3<S>, up: &Vec3<S>) -> Mat3<S> { pub fn look_at(dir: &Vec3<S>, up: &Vec3<S>) -> Mat3<S> {
let dir = dir.normalize(); let dir = dir.normalize();
let side = dir.cross(&up.normalize()); let side = dir.cross(&up.normalize());
@ -223,7 +222,8 @@ impl<S: Primitive> Mat4<S> {
} }
} }
impl<S: Float> Mat4<S> { impl<S: Float + ApproxEq<S>>
Mat4<S> {
pub fn look_at(eye: &Point3<S>, center: &Point3<S>, up: &Vec3<S>) -> Mat4<S> { pub fn look_at(eye: &Point3<S>, center: &Point3<S>, up: &Vec3<S>) -> Mat4<S> {
let f = center.sub_p(eye).normalize(); let f = center.sub_p(eye).normalize();
let s = f.cross(up).normalize(); let s = f.cross(up).normalize();
@ -246,7 +246,7 @@ array!(impl<S> Mat4<S> -> [Vec4<S>, ..4] _4)
pub trait Matrix pub trait Matrix
< <
S: Float, Slice, S: Float + ApproxEq<S>, Slice,
V: Clone + Vector<S, VSlice> + Array<S, VSlice>, VSlice V: Clone + Vector<S, VSlice> + Array<S, VSlice>, VSlice
> >
: Array<V, Slice> : Array<V, Slice>
@ -362,7 +362,7 @@ impl<S: Float> Neg<Mat2<S>> for Mat2<S> { #[inline] fn neg(&self) -> Mat2<S> { b
impl<S: Float> Neg<Mat3<S>> for Mat3<S> { #[inline] fn neg(&self) -> Mat3<S> { build(|i| self.i(i).neg()) } } impl<S: Float> Neg<Mat3<S>> for Mat3<S> { #[inline] fn neg(&self) -> Mat3<S> { build(|i| self.i(i).neg()) } }
impl<S: Float> Neg<Mat4<S>> for Mat4<S> { #[inline] fn neg(&self) -> Mat4<S> { build(|i| self.i(i).neg()) } } impl<S: Float> Neg<Mat4<S>> for Mat4<S> { #[inline] fn neg(&self) -> Mat4<S> { build(|i| self.i(i).neg()) } }
impl<S: Float> impl<S: Float + ApproxEq<S>>
Matrix<S, [Vec2<S>, ..2], Vec2<S>, [S, ..2]> Matrix<S, [Vec2<S>, ..2], Vec2<S>, [S, ..2]>
for Mat2<S> for Mat2<S>
{ {
@ -411,7 +411,7 @@ for Mat2<S>
} }
} }
impl<S: Float> impl<S: Float + ApproxEq<S>>
Matrix<S, [Vec3<S>, ..3], Vec3<S>, [S, ..3]> Matrix<S, [Vec3<S>, ..3], Vec3<S>, [S, ..3]>
for Mat3<S> for Mat3<S>
{ {
@ -484,7 +484,7 @@ macro_rules! dot_mat4(
(*$A.cr(3, $I)) * (*$B.cr($J, 3)) (*$A.cr(3, $I)) * (*$B.cr($J, 3))
)) ))
impl<S: Float> impl<S: Float + ApproxEq<S>>
Matrix<S, [Vec4<S>, ..4], Vec4<S>, [S, ..4]> Matrix<S, [Vec4<S>, ..4], Vec4<S>, [S, ..4]>
for Mat4<S> for Mat4<S>
{ {
@ -617,7 +617,8 @@ pub trait ToMat2<S: Primitive> { fn to_mat2(&self) -> Mat2<S>; }
pub trait ToMat3<S: Primitive> { fn to_mat3(&self) -> Mat3<S>; } pub trait ToMat3<S: Primitive> { fn to_mat3(&self) -> Mat3<S>; }
pub trait ToMat4<S: Primitive> { fn to_mat4(&self) -> Mat4<S>; } pub trait ToMat4<S: Primitive> { fn to_mat4(&self) -> Mat4<S>; }
impl<S: Float> ToMat3<S> for Mat2<S> { impl<S: Float + ApproxEq<S>>
ToMat3<S> for Mat2<S> {
/// Clone the elements of a 2-dimensional matrix into the top corner of a /// Clone the elements of a 2-dimensional matrix into the top corner of a
/// 3-dimensional identity matrix. /// 3-dimensional identity matrix.
fn to_mat3(&self) -> Mat3<S> { fn to_mat3(&self) -> Mat3<S> {
@ -627,7 +628,8 @@ impl<S: Float> ToMat3<S> for Mat2<S> {
} }
} }
impl<S: Float> ToMat4<S> for Mat2<S> { impl<S: Float + ApproxEq<S>>
ToMat4<S> for Mat2<S> {
/// Clone the elements of a 2-dimensional matrix into the top corner of a /// Clone the elements of a 2-dimensional matrix into the top corner of a
/// 4-dimensional identity matrix. /// 4-dimensional identity matrix.
fn to_mat4(&self) -> Mat4<S> { fn to_mat4(&self) -> Mat4<S> {
@ -638,7 +640,8 @@ impl<S: Float> ToMat4<S> for Mat2<S> {
} }
} }
impl<S: Float> ToMat4<S> for Mat3<S> { impl<S: Float + ApproxEq<S>>
ToMat4<S> for Mat3<S> {
/// Clone the elements of a 3-dimensional matrix into the top corner of a /// Clone the elements of a 3-dimensional matrix into the top corner of a
/// 4-dimensional identity matrix. /// 4-dimensional identity matrix.
fn to_mat4(&self) -> Mat4<S> { fn to_mat4(&self) -> Mat4<S> {
@ -649,7 +652,8 @@ impl<S: Float> ToMat4<S> for Mat3<S> {
} }
} }
impl<S:Float> ToQuat<S> for Mat3<S> { impl<S: Float + ApproxEq<S>>
ToQuat<S> for Mat3<S> {
/// Convert the matrix to a quaternion /// Convert the matrix to a quaternion
fn to_quat(&self) -> Quat<S> { fn to_quat(&self) -> Quat<S> {
// http://www.cs.ucr.edu/~vbz/resources/Quatut.pdf // http://www.cs.ucr.edu/~vbz/resources/Quatut.pdf

View file

@ -17,6 +17,7 @@ use std::cast::transmute;
use std::fmt; use std::fmt;
use std::num::Zero; use std::num::Zero;
use approx::ApproxEq;
use intersect::Intersect; use intersect::Intersect;
use point::{Point, Point3}; use point::{Point, Point3};
use ray::Ray3; use ray::Ray3;
@ -45,7 +46,8 @@ pub struct Plane<S> {
d: S, d: S,
} }
impl<S: Float> Plane<S> { impl<S: Float + ApproxEq<S>>
Plane<S> {
/// Construct a plane from a normal vector and a scalar distance /// Construct a plane from a normal vector and a scalar distance
pub fn new(n: Vec3<S>, d: S) -> Plane<S> { pub fn new(n: Vec3<S>, d: S) -> Plane<S> {
Plane { n: n, d: d } Plane { n: n, d: d }
@ -110,23 +112,12 @@ impl<S: Float> Intersect<Option<Point3<S>>> for (Plane<S>, Plane<S>, Plane<S>) {
} }
} }
impl<S: Float> ApproxEq<S> for Plane<S> { impl<S: Float + ApproxEq<S>>
ApproxEq<S> for Plane<S> {
#[inline] #[inline]
fn approx_epsilon() -> S { fn approx_eq_eps(&self, other: &Plane<S>, epsilon: &S) -> bool {
// TODO: fix this after static methods are fixed in rustc self.n.approx_eq_eps(&other.n, epsilon) &&
fail!(~"Doesn't work!"); self.d.approx_eq_eps(&other.d, epsilon)
}
#[inline]
fn approx_eq(&self, other: &Plane<S>) -> bool {
self.n.approx_eq(&other.n) &&
self.d.approx_eq(&other.d)
}
#[inline]
fn approx_eq_eps(&self, other: &Plane<S>, approx_epsilon: &S) -> bool {
self.n.approx_eq_eps(&other.n, approx_epsilon) &&
self.d.approx_eq_eps(&other.d, approx_epsilon)
} }
} }

View file

@ -31,8 +31,6 @@ pub struct Point2<S> { x: S, y: S }
#[deriving(Eq, Zero, Clone)] #[deriving(Eq, Zero, Clone)]
pub struct Point3<S> { x: S, y: S, z: S } pub struct Point3<S> { x: S, y: S, z: S }
approx_eq!(impl<S> Point2<S>)
approx_eq!(impl<S> Point3<S>)
impl<S: Num> Point2<S> { impl<S: Num> Point2<S> {
#[inline] #[inline]

View file

@ -16,6 +16,7 @@
use std::num::{zero, one, cast}; use std::num::{zero, one, cast};
use angle::{Angle, tan, cot}; use angle::{Angle, tan, cot};
use approx::ApproxEq;
use frustum::Frustum; use frustum::Frustum;
use matrix::{Mat4, ToMat4}; use matrix::{Mat4, ToMat4};
use plane::Plane; use plane::Plane;
@ -93,7 +94,8 @@ impl<S: Float, A: Angle<S>> PerspectiveFov<S, A> {
} }
} }
impl<S: Float, A: Angle<S>> Projection<S> for PerspectiveFov<S, A> { impl<S: Float + ApproxEq<S>, A: Angle<S>>
Projection<S> for PerspectiveFov<S, A> {
fn to_frustum(&self) -> Frustum<S> { fn to_frustum(&self) -> Frustum<S> {
// TODO: Could this be faster? // TODO: Could this be faster?
Frustum::from_mat4(self.to_mat4()) Frustum::from_mat4(self.to_mat4())
@ -149,7 +151,8 @@ pub struct Perspective<S> {
near: S, far: S, near: S, far: S,
} }
impl<S: Float> Projection<S> for Perspective<S> { impl<S: Float + ApproxEq<S>>
Projection<S> for Perspective<S> {
fn to_frustum(&self) -> Frustum<S> { fn to_frustum(&self) -> Frustum<S> {
// TODO: Could this be faster? // TODO: Could this be faster?
Frustum::from_mat4(self.to_mat4()) Frustum::from_mat4(self.to_mat4())
@ -199,7 +202,8 @@ pub struct Ortho<S> {
near: S, far: S, near: S, far: S,
} }
impl<S: Float> Projection<S> for Ortho<S> { impl<S: Float + ApproxEq<S>>
Projection<S> for Ortho<S> {
fn to_frustum(&self) -> Frustum<S> { fn to_frustum(&self) -> Frustum<S> {
Frustum { Frustum {
left: Plane::from_abcd( one::<S>(), zero::<S>(), zero::<S>(), self.left.clone()), left: Plane::from_abcd( one::<S>(), zero::<S>(), zero::<S>(), self.left.clone()),

View file

@ -17,6 +17,7 @@ use std::fmt;
use std::num::{zero, one, cast, sqrt}; use std::num::{zero, one, cast, sqrt};
use angle::{Angle, Rad, acos, cos, sin, sin_cos}; use angle::{Angle, Rad, acos, cos, sin, sin_cos};
use approx::ApproxEq;
use array::{Array, build}; use array::{Array, build};
use matrix::{Mat3, ToMat3}; use matrix::{Mat3, ToMat3};
use vector::{Vec3, Vector, EuclideanVector}; use vector::{Vec3, Vector, EuclideanVector};
@ -26,13 +27,13 @@ use vector::{Vec3, Vector, EuclideanVector};
pub struct Quat<S> { s: S, v: Vec3<S> } pub struct Quat<S> { s: S, v: Vec3<S> }
array!(impl<S> Quat<S> -> [S, ..4] _4) array!(impl<S> Quat<S> -> [S, ..4] _4)
approx_eq!(impl<S> Quat<S>)
pub trait ToQuat<S: Float> { pub trait ToQuat<S: Float> {
fn to_quat(&self) -> Quat<S>; fn to_quat(&self) -> Quat<S>;
} }
impl<S: Float> Quat<S> { impl<S: Float + ApproxEq<S>>
Quat<S> {
/// Construct a new quaternion from one scalar component and three /// Construct a new quaternion from one scalar component and three
/// imaginary components /// imaginary components
#[inline] #[inline]
@ -220,7 +221,8 @@ impl<S: Float> Quat<S> {
} }
} }
impl<S: Float> Quat<S> { impl<S: Float + ApproxEq<S>>
Quat<S> {
/// Spherical Linear Intoperlation /// Spherical Linear Intoperlation
/// ///
/// Perform a spherical linear interpolation between the quaternion and /// Perform a spherical linear interpolation between the quaternion and
@ -265,7 +267,8 @@ impl<S: Float> Quat<S> {
} }
} }
impl<S: Float> ToMat3<S> for Quat<S> { impl<S: Float + ApproxEq<S>>
ToMat3<S> for Quat<S> {
/// Convert the quaternion to a 3 x 3 rotation matrix /// Convert the quaternion to a 3 x 3 rotation matrix
fn to_mat3(&self) -> Mat3<S> { fn to_mat3(&self) -> Mat3<S> {
let x2 = self.v.x + self.v.x; let x2 = self.v.x + self.v.x;
@ -290,7 +293,8 @@ impl<S: Float> ToMat3<S> for Quat<S> {
} }
} }
impl<S: Float> Neg<Quat<S>> for Quat<S> { impl<S: Float + ApproxEq<S>>
Neg<Quat<S>> for Quat<S> {
#[inline] #[inline]
fn neg(&self) -> Quat<S> { fn neg(&self) -> Quat<S> {
Quat::from_sv(-self.s, -self.v) Quat::from_sv(-self.s, -self.v)

View file

@ -14,6 +14,7 @@
// limitations under the License. // limitations under the License.
use angle::Rad; use angle::Rad;
use approx::ApproxEq;
use array::Array; use array::Array;
use matrix::Matrix; use matrix::Matrix;
use matrix::{Mat2, ToMat2}; use matrix::{Mat2, ToMat2};
@ -115,7 +116,8 @@ impl<S: Float> ToMat2<S> for Basis2<S> {
fn to_mat2(&self) -> Mat2<S> { self.mat.clone() } fn to_mat2(&self) -> Mat2<S> { self.mat.clone() }
} }
impl<S: Float> Rotation<S, [S, ..2], Vec2<S>, Point2<S>> for Basis2<S> { impl<S: Float + ApproxEq<S>>
Rotation<S, [S, ..2], Vec2<S>, Point2<S>> for Basis2<S> {
#[inline] #[inline]
fn identity() -> Basis2<S> { Basis2{ mat: Mat2::identity() } } fn identity() -> Basis2<S> { Basis2{ mat: Mat2::identity() } }
@ -139,25 +141,16 @@ impl<S: Float> Rotation<S, [S, ..2], Vec2<S>, Point2<S>> for Basis2<S> {
fn invert_self(&mut self) { self.mat.invert_self(); } fn invert_self(&mut self) { self.mat.invert_self(); }
} }
impl<S: Float> ApproxEq<S> for Basis2<S> { impl<S: Float + ApproxEq<S>>
ApproxEq<S> for Basis2<S> {
#[inline] #[inline]
fn approx_epsilon() -> S { fn approx_eq_eps(&self, other: &Basis2<S>, epsilon: &S) -> bool {
// TODO: fix this after static methods are fixed in rustc self.mat.approx_eq_eps(&other.mat, epsilon)
fail!(~"Doesn't work!");
}
#[inline]
fn approx_eq(&self, other: &Basis2<S>) -> bool {
self.mat.approx_eq(&other.mat)
}
#[inline]
fn approx_eq_eps(&self, other: &Basis2<S>, approx_epsilon: &S) -> bool {
self.mat.approx_eq_eps(&other.mat, approx_epsilon)
} }
} }
impl<S: Float> Rotation2<S> for Basis2<S> {} impl<S: Float + ApproxEq<S>>
Rotation2<S> for Basis2<S> {}
/// A three-dimensional rotation matrix. /// A three-dimensional rotation matrix.
/// ///
@ -170,7 +163,8 @@ pub struct Basis3<S> {
priv mat: Mat3<S> priv mat: Mat3<S>
} }
impl<S: Float> Basis3<S> { impl<S: Float + ApproxEq<S>>
Basis3<S> {
#[inline] #[inline]
pub fn look_at(dir: &Vec3<S>, up: &Vec3<S>) -> Basis3<S> { pub fn look_at(dir: &Vec3<S>, up: &Vec3<S>) -> Basis3<S> {
Basis3 { mat: Mat3::look_at(dir, up) } Basis3 { mat: Mat3::look_at(dir, up) }
@ -225,12 +219,14 @@ impl<S: Float> ToMat3<S> for Basis3<S> {
fn to_mat3(&self) -> Mat3<S> { self.mat.clone() } fn to_mat3(&self) -> Mat3<S> { self.mat.clone() }
} }
impl<S: Float> ToQuat<S> for Basis3<S> { impl<S: Float + ApproxEq<S>>
ToQuat<S> for Basis3<S> {
#[inline] #[inline]
fn to_quat(&self) -> Quat<S> { self.mat.to_quat() } fn to_quat(&self) -> Quat<S> { self.mat.to_quat() }
} }
impl<S: Float> Rotation<S, [S, ..3], Vec3<S>, Point3<S>> for Basis3<S> { impl<S: Float + ApproxEq<S>>
Rotation<S, [S, ..3], Vec3<S>, Point3<S>> for Basis3<S> {
#[inline] #[inline]
fn identity() -> Basis3<S> { Basis3{ mat: Mat3::identity() } } fn identity() -> Basis3<S> { Basis3{ mat: Mat3::identity() } }
@ -254,29 +250,21 @@ impl<S: Float> Rotation<S, [S, ..3], Vec3<S>, Point3<S>> for Basis3<S> {
fn invert_self(&mut self) { self.mat.invert_self(); } fn invert_self(&mut self) { self.mat.invert_self(); }
} }
impl<S: Float> ApproxEq<S> for Basis3<S> { impl<S: Float + ApproxEq<S>>
ApproxEq<S> for Basis3<S> {
#[inline] #[inline]
fn approx_epsilon() -> S { fn approx_eq_eps(&self, other: &Basis3<S>, epsilon: &S) -> bool {
// TODO: fix this after static methods are fixed in rustc self.mat.approx_eq_eps(&other.mat, epsilon)
fail!(~"Doesn't work!");
}
#[inline]
fn approx_eq(&self, other: &Basis3<S>) -> bool {
self.mat.approx_eq(&other.mat)
}
#[inline]
fn approx_eq_eps(&self, other: &Basis3<S>, approx_epsilon: &S) -> bool {
self.mat.approx_eq_eps(&other.mat, approx_epsilon)
} }
} }
impl<S: Float> Rotation3<S> for Basis3<S> {} impl<S: Float + ApproxEq<S>>
Rotation3<S> for Basis3<S> {}
// Quaternion Rotation impls // Quaternion Rotation impls
impl<S: Float> ToBasis3<S> for Quat<S> { impl<S: Float + ApproxEq<S>>
ToBasis3<S> for Quat<S> {
#[inline] #[inline]
fn to_rot3(&self) -> Basis3<S> { Basis3 { mat: self.to_mat3() } } fn to_rot3(&self) -> Basis3<S> { Basis3 { mat: self.to_mat3() } }
} }
@ -286,7 +274,8 @@ impl<S: Float> ToQuat<S> for Quat<S> {
fn to_quat(&self) -> Quat<S> { self.clone() } fn to_quat(&self) -> Quat<S> { self.clone() }
} }
impl<S: Float> Rotation<S, [S, ..3], Vec3<S>, Point3<S>> for Quat<S> { impl<S: Float + ApproxEq<S>>
Rotation<S, [S, ..3], Vec3<S>, Point3<S>> for Quat<S> {
#[inline] #[inline]
fn identity() -> Quat<S> { Quat::identity() } fn identity() -> Quat<S> { Quat::identity() }
@ -306,4 +295,5 @@ impl<S: Float> Rotation<S, [S, ..3], Vec3<S>, Point3<S>> for Quat<S> {
fn invert_self(&mut self) { *self = self.invert() } fn invert_self(&mut self) { *self = self.invert() }
} }
impl<S: Float> Rotation3<S> for Quat<S> {} impl<S: Float + ApproxEq<S>>
Rotation3<S> for Quat<S> {}

View file

@ -15,6 +15,7 @@
use std::{fmt,num}; use std::{fmt,num};
use approx::ApproxEq;
use matrix::{Matrix, Mat4, ToMat4}; use matrix::{Matrix, Mat4, ToMat4};
use point::{Point, Point3}; use point::{Point, Point3};
use ray::Ray; use ray::Ray;
@ -73,7 +74,7 @@ pub struct Decomposed<S,V,R> {
impl impl
< <
S: Float, S: Float + ApproxEq<S>,
Slice, Slice,
V: Vector<S, Slice>, V: Vector<S, Slice>,
P: Point<S, V, Slice>, P: Point<S, V, Slice>,
@ -129,7 +130,7 @@ pub trait Transform3<S>
+ ToMat4<S> + ToMat4<S>
{} {}
impl<S: Float + Clone, R: Rotation3<S>> impl<S: Float + Clone + ApproxEq<S>, R: Rotation3<S>>
ToMat4<S> for Decomposed<S, Vec3<S>, R> { ToMat4<S> for Decomposed<S, Vec3<S>, R> {
fn to_mat4(&self) -> Mat4<S> { fn to_mat4(&self) -> Mat4<S> {
let mut m = self.rot.to_mat3().mul_s( self.scale.clone() ).to_mat4(); let mut m = self.rot.to_mat3().mul_s( self.scale.clone() ).to_mat4();
@ -138,7 +139,7 @@ ToMat4<S> for Decomposed<S, Vec3<S>, R> {
} }
} }
impl<S: Float, R: Rotation3<S>> impl<S: Float + ApproxEq<S>, R: Rotation3<S>>
Transform3<S> for Decomposed<S,Vec3<S>,R> {} Transform3<S> for Decomposed<S,Vec3<S>,R> {}
impl<S: fmt::Default + Float, R: ToStr + Rotation3<S>> impl<S: fmt::Default + Float, R: ToStr + Rotation3<S>>
@ -155,7 +156,7 @@ pub struct AffineMatrix3<S> {
mat: Mat4<S>, mat: Mat4<S>,
} }
impl<S : Clone + Float> impl<S : Clone + Float + ApproxEq<S>>
Transform<S, [S, ..3], Vec3<S>, Point3<S>> for AffineMatrix3<S> { Transform<S, [S, ..3], Vec3<S>, Point3<S>> for AffineMatrix3<S> {
#[inline] #[inline]
fn identity() -> AffineMatrix3<S> { fn identity() -> AffineMatrix3<S> {
@ -188,7 +189,7 @@ ToMat4<S> for AffineMatrix3<S> {
#[inline] fn to_mat4(&self) -> Mat4<S> { self.mat.clone() } #[inline] fn to_mat4(&self) -> Mat4<S> { self.mat.clone() }
} }
impl<S: Float> impl<S: Float + ApproxEq<S>>
Transform3<S> for AffineMatrix3<S> {} Transform3<S> for AffineMatrix3<S> {}
@ -201,4 +202,9 @@ impl<S: Float> Transform3D<S> {
pub fn new(scale: S, rot: Quat<S>, disp: Vec3<S>) -> Transform3D<S> { pub fn new(scale: S, rot: Quat<S>, disp: Vec3<S>) -> Transform3D<S> {
Transform3D( Decomposed { scale: scale, rot: rot, disp: disp }) Transform3D( Decomposed { scale: scale, rot: rot, disp: disp })
} }
#[inline]
pub fn get<'a>(&'a self) -> &'a Decomposed<S,Vec3<S>,Quat<S>> {
let &Transform3D(ref d) = self;
d
}
} }

View file

@ -17,6 +17,7 @@ use std::fmt;
use std::num::{Zero, zero, One, one, sqrt}; use std::num::{Zero, zero, One, one, sqrt};
use angle::{Rad, atan2, acos}; use angle::{Rad, atan2, acos};
use approx::ApproxEq;
use array::{Array, build}; use array::{Array, build};
/// A 2-dimensional vector. /// A 2-dimensional vector.
@ -112,10 +113,6 @@ array!(impl<S> Vec2<S> -> [S, ..2] _2)
array!(impl<S> Vec3<S> -> [S, ..3] _3) array!(impl<S> Vec3<S> -> [S, ..3] _3)
array!(impl<S> Vec4<S> -> [S, ..4] _4) array!(impl<S> Vec4<S> -> [S, ..4] _4)
approx_eq!(impl<S> Vec2<S>)
approx_eq!(impl<S> Vec3<S>)
approx_eq!(impl<S> Vec4<S>)
/// A trait that specifies a range of numeric operations for vectors. Not all /// 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 /// of these make sense from a linear algebra point of view, but are included
/// for pragmatic reasons. /// for pragmatic reasons.
@ -215,7 +212,7 @@ impl<S: Primitive> Vec3<S> {
/// 2-dimensional and 3-dimensional vectors. /// 2-dimensional and 3-dimensional vectors.
pub trait EuclideanVector pub trait EuclideanVector
< <
S: Float, S: Float + ApproxEq<S>,
Slice Slice
> >
: Vector<S, Slice> : Vector<S, Slice>
@ -286,21 +283,24 @@ pub trait EuclideanVector
} }
} }
impl<S: Float> EuclideanVector<S, [S, ..2]> for Vec2<S> { impl<S: Float + ApproxEq<S>>
EuclideanVector<S, [S, ..2]> for Vec2<S> {
#[inline] #[inline]
fn angle(&self, other: &Vec2<S>) -> Rad<S> { fn angle(&self, other: &Vec2<S>) -> Rad<S> {
atan2(self.perp_dot(other), self.dot(other)) atan2(self.perp_dot(other), self.dot(other))
} }
} }
impl<S: Float> EuclideanVector<S, [S, ..3]> for Vec3<S> { impl<S: Float + ApproxEq<S>>
EuclideanVector<S, [S, ..3]> for Vec3<S> {
#[inline] #[inline]
fn angle(&self, other: &Vec3<S>) -> Rad<S> { fn angle(&self, other: &Vec3<S>) -> Rad<S> {
atan2(self.cross(other).length(), self.dot(other)) atan2(self.cross(other).length(), self.dot(other))
} }
} }
impl<S: Float> EuclideanVector<S, [S, ..4]> for Vec4<S> { impl<S: Float + ApproxEq<S>>
EuclideanVector<S, [S, ..4]> for Vec4<S> {
#[inline] #[inline]
fn angle(&self, other: &Vec4<S>) -> Rad<S> { fn angle(&self, other: &Vec4<S>) -> Rad<S> {
acos(self.dot(other) / (self.length() * other.length())) acos(self.dot(other) / (self.length() * other.length()))

View file

@ -14,14 +14,15 @@
// limitations under the License. // limitations under the License.
use cgmath::angle::*; use cgmath::angle::*;
use cgmath::approx::ApproxEq;
#[test] #[test]
fn conv() { fn conv() {
assert_approx_eq!(deg(-5.0).to_rad().to_deg(), deg(-5.0)); assert!(deg(-5.0).to_rad().to_deg().approx_eq( &deg(-5.0) ));
assert_approx_eq!(deg(30.0).to_rad().to_deg(), deg(30.0)); assert!(deg(30.0).to_rad().to_deg().approx_eq( &deg(30.0) ));
assert_approx_eq!(rad(-5.0).to_deg().to_rad(), rad(-5.0)); assert!(rad(-5.0).to_deg().to_rad().approx_eq( &rad(-5.0) ));
assert_approx_eq!(rad(30.0).to_deg().to_rad(), rad(30.0)); assert!(rad(30.0).to_deg().to_rad().approx_eq( &rad(30.0) ));
} }
#[test] #[test]

View file

@ -15,6 +15,7 @@
use cgmath::matrix::*; use cgmath::matrix::*;
use cgmath::vector::*; use cgmath::vector::*;
use cgmath::approx::ApproxEq;
type float = f64; type float = f64;
pub mod mat2 { pub mod mat2 {
@ -287,11 +288,11 @@ fn test_invert() {
// Mat4 // Mat4
assert!(Mat4::<f64>::identity().invert().unwrap().is_identity()); assert!(Mat4::<f64>::identity().invert().unwrap().is_identity());
assert_approx_eq!(mat4::C.invert().unwrap(), assert!(mat4::C.invert().unwrap().approx_eq(&
Mat4::new( 5.0, -4.0, 1.0, 0.0, Mat4::new( 5.0, -4.0, 1.0, 0.0,
-4.0, 8.0, -4.0, 0.0, -4.0, 8.0, -4.0, 0.0,
4.0, -8.0, 4.0, 8.0, 4.0, -8.0, 4.0, 8.0,
-3.0, 4.0, 1.0, -8.0).mul_s(0.125)); -3.0, 4.0, 1.0, -8.0).mul_s(0.125)));
let mut mut_c = mat4::C; let mut mut_c = mat4::C;
mut_c.invert_self(); mut_c.invert_self();
assert_eq!(mut_c, mat4::C.invert().unwrap()); assert_eq!(mut_c, mat4::C.invert().unwrap());

View file

@ -14,9 +14,10 @@
// limitations under the License. // limitations under the License.
use cgmath::point::*; use cgmath::point::*;
use cgmath::approx::ApproxEq;
#[test] #[test]
fn test_homogeneous() { fn test_homogeneous() {
let p = Point3::new(1.0, 2.0, 3.0); let p = Point3::new(1.0, 2.0, 3.0);
assert_approx_eq!(p, Point3::from_homogeneous( &p.to_homogeneous() )); assert!(p.approx_eq( &Point3::from_homogeneous( &p.to_homogeneous() ) ));
} }

View file

@ -2,6 +2,7 @@ use cgmath::sphere::*;
use cgmath::point::*; use cgmath::point::*;
use cgmath::vector::*; use cgmath::vector::*;
use cgmath::ray::*; use cgmath::ray::*;
use cgmath::approx::ApproxEq;
use cgmath::intersect::Intersect; use cgmath::intersect::Intersect;
use std::num; use std::num;
@ -13,7 +14,7 @@ fn test_intersection() {
let r2 = Ray::new(Point3::new(1f64, 0f64, 5f64), Vec3::new(0f64, 0f64, -5f64).normalize()); let r2 = Ray::new(Point3::new(1f64, 0f64, 5f64), Vec3::new(0f64, 0f64, -5f64).normalize());
let r3 = Ray::new(Point3::new(2f64, 0f64, 5f64), Vec3::new(0f64, 0f64, -5f64).normalize()); let r3 = Ray::new(Point3::new(2f64, 0f64, 5f64), Vec3::new(0f64, 0f64, -5f64).normalize());
assert_eq!((sphere,r0).intersection(), Some(Point3::new(0f64, 0f64, 1f64))); assert_eq!((sphere,r0).intersection(), Some(Point3::new(0f64, 0f64, 1f64)));
assert_approx_eq!((sphere,r1).intersection().unwrap(), Point3::new(num::cos(1f64), 0f64, num::sin(1f64))); assert!((sphere,r1).intersection().unwrap().approx_eq( &Point3::new(num::cos(1f64), 0f64, num::sin(1f64)) ));
assert_eq!((sphere,r2).intersection(), Some(Point3::new(1f64, 0f64, 0f64))); assert_eq!((sphere,r2).intersection(), Some(Point3::new(1f64, 0f64, 0f64)));
assert_eq!((sphere,r3).intersection(), None); assert_eq!((sphere,r3).intersection(), None);
} }

View file

@ -16,12 +16,13 @@
use cgmath::quaternion::*; use cgmath::quaternion::*;
use cgmath::transform::*; use cgmath::transform::*;
use cgmath::vector::*; use cgmath::vector::*;
use cgmath::approx::ApproxEq;
#[test] #[test]
fn test_invert() { fn test_invert() {
let v = Vec3::new(1.0, 2.0, 3.0); let v = Vec3::new(1.0, 2.0, 3.0);
let t = Transform3D::new(1.5, Quat::new(0.5,0.5,0.5,0.5), Vec3::new(6.0,-7.0,8.0)); let t = Transform3D::new(1.5, Quat::new(0.5,0.5,0.5,0.5), Vec3::new(6.0,-7.0,8.0));
let ti = t.invert().expect("Expected successful inversion"); let ti = t.get().invert().expect("Expected successful inversion");
let vt = t.transform_vec( &v ); let vt = t.get().transform_vec( &v );
assert_approx_eq!(v, ti.transform_vec( &vt )); assert!(v.approx_eq( &ti.transform_vec( &vt ) ));
} }

View file

@ -15,6 +15,7 @@
use cgmath::angle::*; use cgmath::angle::*;
use cgmath::vector::*; use cgmath::vector::*;
use cgmath::approx::ApproxEq;
#[test] #[test]
fn test_from_value() { fn test_from_value() {
@ -136,23 +137,23 @@ mod test_length {
#[test] #[test]
fn test_angle() { fn test_angle() {
assert_approx_eq!(Vec2::new(1.0, 0.0).angle(&Vec2::new(0.0, 1.0)), rad(Real::frac_pi_2())); assert!(Vec2::new(1.0, 0.0).angle(&Vec2::new(0.0, 1.0)).approx_eq( &rad(Real::frac_pi_2()) ));
assert_approx_eq!(Vec2::new(10.0, 0.0).angle(&Vec2::new(0.0, 5.0)), rad(Real::frac_pi_2())); assert!(Vec2::new(10.0, 0.0).angle(&Vec2::new(0.0, 5.0)).approx_eq( &rad(Real::frac_pi_2()) ));
assert_approx_eq!(Vec2::new(-1.0, 0.0).angle(&Vec2::new(0.0, 1.0)), -rad(Real::frac_pi_2())); assert!(Vec2::new(-1.0, 0.0).angle(&Vec2::new(0.0, 1.0)).approx_eq( &-rad(Real::frac_pi_2()) ));
assert_approx_eq!(Vec3::new(1.0, 0.0, 1.0).angle(&Vec3::new(1.0, 1.0, 0.0)), rad(Real::frac_pi_3())); assert!(Vec3::new(1.0, 0.0, 1.0).angle(&Vec3::new(1.0, 1.0, 0.0)).approx_eq( &rad(Real::frac_pi_3()) ));
assert_approx_eq!(Vec3::new(10.0, 0.0, 10.0).angle(&Vec3::new(5.0, 5.0, 0.0)), rad(Real::frac_pi_3())); assert!(Vec3::new(10.0, 0.0, 10.0).angle(&Vec3::new(5.0, 5.0, 0.0)).approx_eq( &rad(Real::frac_pi_3()) ));
assert_approx_eq!(Vec3::new(-1.0, 0.0, -1.0).angle(&Vec3::new(1.0, -1.0, 0.0)), rad(2.0 * Real::frac_pi_3())); assert!(Vec3::new(-1.0, 0.0, -1.0).angle(&Vec3::new(1.0, -1.0, 0.0)).approx_eq( &rad(2.0 * Real::frac_pi_3()) ));
assert_approx_eq!(Vec4::new(1.0, 0.0, 1.0, 0.0).angle(&Vec4::new(0.0, 1.0, 0.0, 1.0)), rad(Real::frac_pi_2())); assert!(Vec4::new(1.0, 0.0, 1.0, 0.0).angle(&Vec4::new(0.0, 1.0, 0.0, 1.0)).approx_eq( &rad(Real::frac_pi_2()) ));
assert_approx_eq!(Vec4::new(10.0, 0.0, 10.0, 0.0).angle(&Vec4::new(0.0, 5.0, 0.0, 5.0)), rad(Real::frac_pi_2())); assert!(Vec4::new(10.0, 0.0, 10.0, 0.0).angle(&Vec4::new(0.0, 5.0, 0.0, 5.0)).approx_eq( &rad(Real::frac_pi_2()) ));
assert_approx_eq!(Vec4::new(-1.0, 0.0, -1.0, 0.0).angle(&Vec4::new(0.0, 1.0, 0.0, 1.0)), rad(Real::frac_pi_2())); assert!(Vec4::new(-1.0, 0.0, -1.0, 0.0).angle(&Vec4::new(0.0, 1.0, 0.0, 1.0)).approx_eq( &rad(Real::frac_pi_2()) ));
} }
#[test] #[test]
fn test_normalize() { fn test_normalize() {
// TODO: test normalize_to, normalize_sel.0, and normalize_self_to // TODO: test normalize_to, normalize_sel.0, and normalize_self_to
assert_approx_eq!(Vec2::new(3.0, 4.0).normalize(), Vec2::new(3.0/5.0, 4.0/5.0)); assert!(Vec2::new(3.0, 4.0).normalize().approx_eq( &Vec2::new(3.0/5.0, 4.0/5.0) ));
assert_approx_eq!(Vec3::new(2.0, 3.0, 6.0).normalize(), Vec3::new(2.0/7.0, 3.0/7.0, 6.0/7.0)); assert!(Vec3::new(2.0, 3.0, 6.0).normalize().approx_eq( &Vec3::new(2.0/7.0, 3.0/7.0, 6.0/7.0) ));
assert_approx_eq!(Vec4::new(1.0, 2.0, 4.0, 10.0).normalize(), Vec4::new(1.0/11.0, 2.0/11.0, 4.0/11.0, 10.0/11.0)); assert!(Vec4::new(1.0, 2.0, 4.0, 10.0).normalize().approx_eq( &Vec4::new(1.0/11.0, 2.0/11.0, 4.0/11.0, 10.0/11.0) ));
} }