commit
c4e8d8ae69
9 changed files with 66 additions and 66 deletions
|
@ -49,7 +49,7 @@ pub trait Aabb<S: BaseNum, V: Vector<S>, P: Point<S, V>>: Sized {
|
|||
|
||||
/// Return the volume this AABB encloses.
|
||||
#[inline]
|
||||
fn volume(&self) -> S { self.dim().comp_mul() }
|
||||
fn volume(&self) -> S { self.dim().product() }
|
||||
|
||||
/// Return the center point of this AABB.
|
||||
#[inline]
|
||||
|
|
|
@ -271,7 +271,7 @@ pub trait Matrix<S: BaseFloat, V: Vector<S> + 'static>: Array2<V, V, S> + Approx
|
|||
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(S::one()) }
|
||||
fn one() -> Self { Self::from_value(S::one()) }
|
||||
|
||||
/// Multiply this matrix by a scalar, returning the new matrix.
|
||||
#[must_use]
|
||||
|
@ -327,7 +327,7 @@ pub trait Matrix<S: BaseFloat, V: Vector<S> + 'static>: Array2<V, V, S> + Approx
|
|||
|
||||
/// Return the trace of this matrix. That is, the sum of the diagonal.
|
||||
#[inline]
|
||||
fn trace(&self) -> S { self.diagonal().comp_add() }
|
||||
fn trace(&self) -> S { self.diagonal().sum() }
|
||||
|
||||
/// Invert this matrix, returning a new matrix. `m.mul_m(m.invert())` is
|
||||
/// the identity matrix. Returns `None` if this matrix is not invertible
|
||||
|
@ -348,7 +348,7 @@ pub trait Matrix<S: BaseFloat, V: Vector<S> + 'static>: Array2<V, V, S> + Approx
|
|||
/// Test if this matrix is the identity matrix. That is, it is diagonal
|
||||
/// and every element in the diagonal is one.
|
||||
#[inline]
|
||||
fn is_identity(&self) -> bool { self.approx_eq(&Self::identity()) }
|
||||
fn is_one(&self) -> bool { self.approx_eq(&Self::one()) }
|
||||
|
||||
/// Test if this is a diagonal matrix. That is, every element outside of
|
||||
/// the diagonal is 0.
|
||||
|
|
|
@ -64,7 +64,7 @@ impl<S: BaseFloat> Quaternion<S> {
|
|||
|
||||
/// The multiplicative identity, ie: `q = 1 + 0i + 0j + 0i`
|
||||
#[inline]
|
||||
pub fn identity() -> Quaternion<S> {
|
||||
pub fn one() -> Quaternion<S> {
|
||||
Quaternion::from_sv(S::one(), Vector3::zero())
|
||||
}
|
||||
|
||||
|
@ -341,7 +341,7 @@ impl<S: BaseFloat> From<Quaternion<S>> for Basis3<S> {
|
|||
|
||||
impl<S: BaseFloat + 'static> Rotation<S, Vector3<S>, Point3<S>> for Quaternion<S> {
|
||||
#[inline]
|
||||
fn identity() -> Quaternion<S> { Quaternion::identity() }
|
||||
fn one() -> Quaternion<S> { Quaternion::one() }
|
||||
|
||||
#[inline]
|
||||
fn look_at(dir: &Vector3<S>, up: &Vector3<S>) -> Quaternion<S> {
|
||||
|
|
|
@ -28,7 +28,7 @@ use vector::{Vector, Vector2, Vector3};
|
|||
/// creates a circular motion, and preserves at least one point in the space.
|
||||
pub trait Rotation<S: BaseFloat, V: Vector<S>, P: Point<S, V>>: PartialEq + ApproxEq<S> + Sized {
|
||||
/// Create the identity transform (causes no transformation).
|
||||
fn identity() -> Self;
|
||||
fn one() -> Self;
|
||||
|
||||
/// Create a rotation to a given direction with an 'up' vector
|
||||
fn look_at(dir: &V, up: &V) -> Self;
|
||||
|
@ -181,7 +181,7 @@ impl<S: BaseFloat> From<Basis2<S>> for Matrix2<S> {
|
|||
|
||||
impl<S: BaseFloat + 'static> Rotation<S, Vector2<S>, Point2<S>> for Basis2<S> {
|
||||
#[inline]
|
||||
fn identity() -> Basis2<S> { Basis2{ mat: Matrix2::identity() } }
|
||||
fn one() -> Basis2<S> { Basis2 { mat: Matrix2::one() } }
|
||||
|
||||
#[inline]
|
||||
fn look_at(dir: &Vector2<S>, up: &Vector2<S>) -> Basis2<S> {
|
||||
|
@ -262,7 +262,7 @@ impl<S: BaseFloat + 'static> From<Basis3<S>> for Quaternion<S> {
|
|||
|
||||
impl<S: BaseFloat + 'static> Rotation<S, Vector3<S>, Point3<S>> for Basis3<S> {
|
||||
#[inline]
|
||||
fn identity() -> Basis3<S> { Basis3{ mat: Matrix3::identity() } }
|
||||
fn one() -> Basis3<S> { Basis3 { mat: Matrix3::one() } }
|
||||
|
||||
#[inline]
|
||||
fn look_at(dir: &Vector3<S>, up: &Vector3<S>) -> Basis3<S> {
|
||||
|
|
|
@ -31,7 +31,7 @@ use vector::*;
|
|||
pub trait Transform<S: BaseNum, V: Vector<S>, P: Point<S, V>>: Sized {
|
||||
/// Create an identity transformation. That is, a transformation which
|
||||
/// does nothing.
|
||||
fn identity() -> Self;
|
||||
fn one() -> Self;
|
||||
|
||||
/// Create a transformation that rotates a vector to look at `center` from
|
||||
/// `eye`, using `up` for orientation.
|
||||
|
@ -92,10 +92,10 @@ impl<
|
|||
R: Rotation<S, V, P>,
|
||||
> Transform<S, V, P> for Decomposed<S, V, R> {
|
||||
#[inline]
|
||||
fn identity() -> Decomposed<S, V, R> {
|
||||
fn one() -> Decomposed<S, V, R> {
|
||||
Decomposed {
|
||||
scale: S::one(),
|
||||
rot: R::identity(),
|
||||
rot: R::one(),
|
||||
disp: V::zero(),
|
||||
}
|
||||
}
|
||||
|
@ -200,8 +200,8 @@ pub struct AffineMatrix3<S> {
|
|||
|
||||
impl<S: BaseFloat + 'static> Transform<S, Vector3<S>, Point3<S>> for AffineMatrix3<S> {
|
||||
#[inline]
|
||||
fn identity() -> AffineMatrix3<S> {
|
||||
AffineMatrix3 { mat: Matrix4::identity() }
|
||||
fn one() -> AffineMatrix3<S> {
|
||||
AffineMatrix3 { mat: Matrix4::one() }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -262,7 +262,7 @@ impl<
|
|||
R: Rotation<S, V, P> + Clone,
|
||||
> ToComponents<S, V, P, R> for Decomposed<S, V, R> {
|
||||
fn decompose(&self) -> (V, R, V) {
|
||||
(V::identity().mul_s(self.scale), self.rot.clone(), self.disp.clone())
|
||||
(V::one().mul_s(self.scale), self.rot.clone(), self.disp.clone())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
//! ## Working with Vectors
|
||||
//!
|
||||
//! Vectors can be created in several different ways. There is, of course, the
|
||||
//! traditional `new()` method, but unit vectors, zero vectors, and an identity
|
||||
//! traditional `new()` method, but unit vectors, zero vectors, and an one
|
||||
//! vector are also provided:
|
||||
//!
|
||||
//! ```rust
|
||||
|
@ -41,7 +41,7 @@
|
|||
//!
|
||||
//! assert_eq!(&a + &b, Vector2::zero());
|
||||
//! assert_eq!(-(&a * &b), Vector2::new(9.0f64, 16.0f64));
|
||||
//! assert_eq!(&a / &Vector2::identity(), a);
|
||||
//! assert_eq!(&a / &Vector2::one(), a);
|
||||
//!
|
||||
//! // As with Rust's `int` and `f32` types, Vectors of different types cannot
|
||||
//! // be added and so on with impunity. The following will fail to compile:
|
||||
|
@ -135,7 +135,7 @@ pub trait Vector<S: BaseNum>: Array1<S> + Clone // where
|
|||
fn zero() -> Self { Self::from_value(S::zero()) }
|
||||
/// The identity vector (with all components set to one)
|
||||
#[inline]
|
||||
fn identity() -> Self { Self::from_value(S::one()) }
|
||||
fn one() -> Self { Self::from_value(S::one()) }
|
||||
|
||||
/// Add a scalar to this vector, returning a new vector.
|
||||
#[must_use]
|
||||
|
@ -191,14 +191,14 @@ pub trait Vector<S: BaseNum>: Array1<S> + Clone // where
|
|||
/// Take the remainder of this vector by another, in-place.
|
||||
fn rem_self_v(&mut self, v: &Self);
|
||||
|
||||
/// The sum of each component of the vector.
|
||||
fn comp_add(&self) -> S;
|
||||
/// The product of each component of the vector.
|
||||
fn comp_mul(&self) -> S;
|
||||
/// The sum of the components of the vector.
|
||||
fn sum(&self) -> S;
|
||||
/// The product of the components of the vector.
|
||||
fn product(&self) -> S;
|
||||
|
||||
/// Vector dot product.
|
||||
#[inline]
|
||||
fn dot(&self, v: &Self) -> S { self.mul_v(v).comp_add() }
|
||||
fn dot(&self, v: &Self) -> S { self.mul_v(v).sum() }
|
||||
|
||||
/// The minimum component of the vector.
|
||||
fn comp_min(&self) -> S;
|
||||
|
@ -274,8 +274,8 @@ macro_rules! vec {
|
|||
#[inline] fn div_self_v(&mut self, v: &$VectorN<S>) { *self = &*self / v; }
|
||||
#[inline] fn rem_self_v(&mut self, v: &$VectorN<S>) { *self = &*self % v; }
|
||||
|
||||
#[inline] fn comp_add(&self) -> S { fold!(add, { $(self.$field),+ }) }
|
||||
#[inline] fn comp_mul(&self) -> S { fold!(mul, { $(self.$field),+ }) }
|
||||
#[inline] fn sum(&self) -> S { fold!(add, { $(self.$field),+ }) }
|
||||
#[inline] fn product(&self) -> S { fold!(mul, { $(self.$field),+ }) }
|
||||
#[inline] fn comp_min(&self) -> S { fold!(partial_min, { $(self.$field),+ }) }
|
||||
#[inline] fn comp_max(&self) -> S { fold!(partial_max, { $(self.$field),+ }) }
|
||||
}
|
||||
|
|
|
@ -265,7 +265,7 @@ fn test_transpose() {
|
|||
#[test]
|
||||
fn test_invert() {
|
||||
// Matrix2
|
||||
assert!(Matrix2::<f64>::identity().invert().unwrap().is_identity());
|
||||
assert!(Matrix2::<f64>::one().invert().unwrap().is_one());
|
||||
|
||||
assert_eq!(matrix2::A.invert().unwrap(),
|
||||
Matrix2::new(-2.0f64, 1.5f64,
|
||||
|
@ -277,7 +277,7 @@ fn test_invert() {
|
|||
assert_eq!(mut_a, matrix2::A.invert().unwrap());
|
||||
|
||||
// Matrix3
|
||||
assert!(Matrix3::<f64>::identity().invert().unwrap().is_identity());
|
||||
assert!(Matrix3::<f64>::one().invert().unwrap().is_one());
|
||||
|
||||
assert_eq!(matrix3::A.invert(), None);
|
||||
|
||||
|
@ -290,7 +290,7 @@ fn test_invert() {
|
|||
assert_eq!(mut_c, matrix3::C.invert().unwrap());
|
||||
|
||||
// Matrix4
|
||||
assert!(Matrix4::<f64>::identity().invert().unwrap().is_identity());
|
||||
assert!(Matrix4::<f64>::one().invert().unwrap().is_one());
|
||||
|
||||
assert!(matrix4::C.invert().unwrap().approx_eq(&
|
||||
Matrix4::new( 5.0f64, -4.0f64, 1.0f64, 0.0f64,
|
||||
|
@ -305,25 +305,25 @@ fn test_invert() {
|
|||
-0., 0.631364f64, 0.775487f64, 0.0f64,
|
||||
-0.991261f64, 0.1023f64, -0.083287f64, 0.0f64,
|
||||
0., -1.262728f64, -1.550973f64, 1.0f64);
|
||||
assert!(mat_c.invert().unwrap().mul_m(&mat_c).is_identity());
|
||||
assert!(mat_c.invert().unwrap().mul_m(&mat_c).is_one());
|
||||
|
||||
let mat_d = Matrix4::new( 0.065455f64, -0.720002f64, 0.690879f64, 0.0f64,
|
||||
-0., 0.692364f64, 0.721549f64, 0.0f64,
|
||||
-0.997856f64, -0.047229f64, 0.045318f64, 0.0f64,
|
||||
0., -1.384727f64, -1.443098f64, 1.0f64);
|
||||
assert!(mat_d.invert().unwrap().mul_m(&mat_d).is_identity());
|
||||
assert!(mat_d.invert().unwrap().mul_m(&mat_d).is_one());
|
||||
|
||||
let mat_e = Matrix4::new( 0.409936f64, 0.683812f64, -0.603617f64, 0.0f64,
|
||||
0., 0.661778f64, 0.7497f64, 0.0f64,
|
||||
0.912114f64, -0.307329f64, 0.271286f64, 0.0f64,
|
||||
-0., -1.323555f64, -1.499401f64, 1.0f64);
|
||||
assert!(mat_e.invert().unwrap().mul_m(&mat_e).is_identity());
|
||||
assert!(mat_e.invert().unwrap().mul_m(&mat_e).is_one());
|
||||
|
||||
let mat_f = Matrix4::new(-0.160691f64, -0.772608f64, 0.614211f64, 0.0f64,
|
||||
-0., 0.622298f64, 0.78278f64, 0.0f64,
|
||||
-0.987005f64, 0.125786f64, -0.099998f64, 0.0f64,
|
||||
0., -1.244597f64, -1.565561f64, 1.0f64);
|
||||
assert!(mat_f.invert().unwrap().mul_m(&mat_f).is_identity());
|
||||
assert!(mat_f.invert().unwrap().mul_m(&mat_f).is_one());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -338,17 +338,17 @@ fn test_from_translation() {
|
|||
fn test_predicates() {
|
||||
// Matrix2
|
||||
|
||||
assert!(Matrix2::<f64>::identity().is_identity());
|
||||
assert!(Matrix2::<f64>::identity().is_symmetric());
|
||||
assert!(Matrix2::<f64>::identity().is_diagonal());
|
||||
assert!(Matrix2::<f64>::identity().is_invertible());
|
||||
assert!(Matrix2::<f64>::one().is_one());
|
||||
assert!(Matrix2::<f64>::one().is_symmetric());
|
||||
assert!(Matrix2::<f64>::one().is_diagonal());
|
||||
assert!(Matrix2::<f64>::one().is_invertible());
|
||||
|
||||
assert!(!matrix2::A.is_identity());
|
||||
assert!(!matrix2::A.is_one());
|
||||
assert!(!matrix2::A.is_symmetric());
|
||||
assert!(!matrix2::A.is_diagonal());
|
||||
assert!(matrix2::A.is_invertible());
|
||||
|
||||
assert!(!matrix2::C.is_identity());
|
||||
assert!(!matrix2::C.is_one());
|
||||
assert!(matrix2::C.is_symmetric());
|
||||
assert!(!matrix2::C.is_diagonal());
|
||||
assert!(matrix2::C.is_invertible());
|
||||
|
@ -357,17 +357,17 @@ fn test_predicates() {
|
|||
|
||||
// Matrix3
|
||||
|
||||
assert!(Matrix3::<f64>::identity().is_identity());
|
||||
assert!(Matrix3::<f64>::identity().is_symmetric());
|
||||
assert!(Matrix3::<f64>::identity().is_diagonal());
|
||||
assert!(Matrix3::<f64>::identity().is_invertible());
|
||||
assert!(Matrix3::<f64>::one().is_one());
|
||||
assert!(Matrix3::<f64>::one().is_symmetric());
|
||||
assert!(Matrix3::<f64>::one().is_diagonal());
|
||||
assert!(Matrix3::<f64>::one().is_invertible());
|
||||
|
||||
assert!(!matrix3::A.is_identity());
|
||||
assert!(!matrix3::A.is_one());
|
||||
assert!(!matrix3::A.is_symmetric());
|
||||
assert!(!matrix3::A.is_diagonal());
|
||||
assert!(!matrix3::A.is_invertible());
|
||||
|
||||
assert!(!matrix3::D.is_identity());
|
||||
assert!(!matrix3::D.is_one());
|
||||
assert!(matrix3::D.is_symmetric());
|
||||
assert!(!matrix3::D.is_diagonal());
|
||||
assert!(matrix3::D.is_invertible());
|
||||
|
@ -376,17 +376,17 @@ fn test_predicates() {
|
|||
|
||||
// Matrix4
|
||||
|
||||
assert!(Matrix4::<f64>::identity().is_identity());
|
||||
assert!(Matrix4::<f64>::identity().is_symmetric());
|
||||
assert!(Matrix4::<f64>::identity().is_diagonal());
|
||||
assert!(Matrix4::<f64>::identity().is_invertible());
|
||||
assert!(Matrix4::<f64>::one().is_one());
|
||||
assert!(Matrix4::<f64>::one().is_symmetric());
|
||||
assert!(Matrix4::<f64>::one().is_diagonal());
|
||||
assert!(Matrix4::<f64>::one().is_invertible());
|
||||
|
||||
assert!(!matrix4::A.is_identity());
|
||||
assert!(!matrix4::A.is_one());
|
||||
assert!(!matrix4::A.is_symmetric());
|
||||
assert!(!matrix4::A.is_diagonal());
|
||||
assert!(!matrix4::A.is_invertible());
|
||||
|
||||
assert!(!matrix4::D.is_identity());
|
||||
assert!(!matrix4::D.is_one());
|
||||
assert!(matrix4::D.is_symmetric());
|
||||
assert!(!matrix4::D.is_diagonal());
|
||||
assert!(matrix4::D.is_invertible());
|
||||
|
|
|
@ -35,7 +35,7 @@ fn test_invert_basis2() {
|
|||
let a: Basis2<_> = rotation::a2();
|
||||
let a = a.concat(&a.invert());
|
||||
let a: &Matrix2<_> = a.as_ref();
|
||||
assert!(a.is_identity());
|
||||
assert!(a.is_one());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -43,5 +43,5 @@ fn test_invert_basis3() {
|
|||
let a: Basis3<_> = rotation::a3();
|
||||
let a = a.concat(&a.invert());
|
||||
let a: &Matrix3<_> = a.as_ref();
|
||||
assert!(a.is_identity());
|
||||
assert!(a.is_one());
|
||||
}
|
||||
|
|
|
@ -41,25 +41,25 @@ fn test_dot() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_comp_add() {
|
||||
assert_eq!(Vector2::new(1isize, 2isize).comp_add(), 3isize);
|
||||
assert_eq!(Vector3::new(1isize, 2isize, 3isize).comp_add(), 6isize);
|
||||
assert_eq!(Vector4::new(1isize, 2isize, 3isize, 4isize).comp_add(), 10isize);
|
||||
fn test_sum() {
|
||||
assert_eq!(Vector2::new(1isize, 2isize).sum(), 3isize);
|
||||
assert_eq!(Vector3::new(1isize, 2isize, 3isize).sum(), 6isize);
|
||||
assert_eq!(Vector4::new(1isize, 2isize, 3isize, 4isize).sum(), 10isize);
|
||||
|
||||
assert_eq!(Vector2::new(3.0f64, 4.0f64).comp_add(), 7.0f64);
|
||||
assert_eq!(Vector3::new(4.0f64, 5.0f64, 6.0f64).comp_add(), 15.0f64);
|
||||
assert_eq!(Vector4::new(5.0f64, 6.0f64, 7.0f64, 8.0f64).comp_add(), 26.0f64);
|
||||
assert_eq!(Vector2::new(3.0f64, 4.0f64).sum(), 7.0f64);
|
||||
assert_eq!(Vector3::new(4.0f64, 5.0f64, 6.0f64).sum(), 15.0f64);
|
||||
assert_eq!(Vector4::new(5.0f64, 6.0f64, 7.0f64, 8.0f64).sum(), 26.0f64);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_comp_mul() {
|
||||
assert_eq!(Vector2::new(1isize, 2isize).comp_mul(), 2isize);
|
||||
assert_eq!(Vector3::new(1isize, 2isize, 3isize).comp_mul(), 6isize);
|
||||
assert_eq!(Vector4::new(1isize, 2isize, 3isize, 4isize).comp_mul(), 24isize);
|
||||
fn test_product() {
|
||||
assert_eq!(Vector2::new(1isize, 2isize).product(), 2isize);
|
||||
assert_eq!(Vector3::new(1isize, 2isize, 3isize).product(), 6isize);
|
||||
assert_eq!(Vector4::new(1isize, 2isize, 3isize, 4isize).product(), 24isize);
|
||||
|
||||
assert_eq!(Vector2::new(3.0f64, 4.0f64).comp_mul(), 12.0f64);
|
||||
assert_eq!(Vector3::new(4.0f64, 5.0f64, 6.0f64).comp_mul(), 120.0f64);
|
||||
assert_eq!(Vector4::new(5.0f64, 6.0f64, 7.0f64, 8.0f64).comp_mul(), 1680.0f64);
|
||||
assert_eq!(Vector2::new(3.0f64, 4.0f64).product(), 12.0f64);
|
||||
assert_eq!(Vector3::new(4.0f64, 5.0f64, 6.0f64).product(), 120.0f64);
|
||||
assert_eq!(Vector4::new(5.0f64, 6.0f64, 7.0f64, 8.0f64).product(), 1680.0f64);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Reference in a new issue