diff --git a/src/matrix.rs b/src/matrix.rs index 9bb96c0..d40c9cb 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -351,8 +351,9 @@ pub trait SquareMatrix where /// Create a matrix from a non-uniform scale fn from_diagonal(diagonal: Self::Column) -> Self; - /// Create a matrix where the each element of the diagonal is equal to one. - fn one() -> Self; + /// The [identity matrix](https://en.wikipedia.org/wiki/Identity_matrix). Multiplying this + /// matrix with another has no effect. + fn identity() -> Self; /// Add this matrix with another matrix, returning the new metrix. fn add_m(&self, m: &Self) -> Self; @@ -398,7 +399,7 @@ pub trait SquareMatrix where /// Test if this matrix is the identity matrix. That is, it is diagonal /// and every element in the diagonal is one. #[inline] - fn is_one(&self) -> bool { self.approx_eq(&Self::one()) } + fn is_identity(&self) -> bool { self.approx_eq(&Self::identity()) } /// Test if this is a diagonal matrix. That is, every element outside of /// the diagonal is 0. @@ -491,9 +492,8 @@ impl SquareMatrix for Matrix2 { } #[inline] - fn one() -> Matrix2 { - Matrix2::new(S::one(), S::zero(), - S::zero(), S::one()) + fn identity() -> Matrix2 { + Matrix2::from_value(S::one()) } #[inline] @@ -645,10 +645,8 @@ impl SquareMatrix for Matrix3 { } #[inline] - fn one() -> Matrix3 { - Matrix3::new(S::one(), S::zero(), S::zero(), - S::zero(), S::one(), S::zero(), - S::zero(), S::zero(), S::one()) + fn identity() -> Matrix3 { + Matrix3::from_value(S::one()) } #[inline] @@ -821,11 +819,8 @@ impl SquareMatrix for Matrix4 { } #[inline] - fn one() -> Matrix4 { - Matrix4::new(S::one(), S::zero(), S::zero(), S::zero(), - S::zero(), S::one(), S::zero(), S::zero(), - S::zero(), S::zero(), S::one(), S::zero(), - S::zero(), S::zero(), S::zero(), S::one()) + fn identity() -> Matrix4 { + Matrix4::from_value(S::one()) } #[inline] diff --git a/src/rotation.rs b/src/rotation.rs index c13eefb..d30e556 100644 --- a/src/rotation.rs +++ b/src/rotation.rs @@ -177,7 +177,7 @@ impl From> for Matrix2 { impl Rotation> for Basis2 { #[inline] - fn one() -> Basis2 { Basis2 { mat: Matrix2::one() } } + fn one() -> Basis2 { Basis2 { mat: Matrix2::identity() } } #[inline] fn look_at(dir: Vector2, up: Vector2) -> Basis2 { @@ -260,7 +260,7 @@ impl From> for Quaternion { impl Rotation> for Basis3 { #[inline] - fn one() -> Basis3 { Basis3 { mat: Matrix3::one() } } + fn one() -> Basis3 { Basis3 { mat: Matrix3::identity() } } #[inline] fn look_at(dir: Vector3, up: Vector3) -> Basis3 { diff --git a/src/transform.rs b/src/transform.rs index 36abb9b..32669fe 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -177,7 +177,7 @@ pub struct AffineMatrix3 { impl Transform> for AffineMatrix3 { #[inline] fn one() -> AffineMatrix3 { - AffineMatrix3 { mat: Matrix4::one() } + AffineMatrix3 { mat: Matrix4::identity() } } #[inline] diff --git a/tests/matrix.rs b/tests/matrix.rs index d02f744..2a931d2 100644 --- a/tests/matrix.rs +++ b/tests/matrix.rs @@ -265,7 +265,7 @@ fn test_transpose() { #[test] fn test_invert() { // Matrix2 - assert!(Matrix2::::one().invert().unwrap().is_one()); + assert!(Matrix2::::identity().invert().unwrap().is_identity()); 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::::one().invert().unwrap().is_one()); + assert!(Matrix3::::identity().invert().unwrap().is_identity()); assert_eq!(matrix3::A.invert(), None); @@ -290,7 +290,7 @@ fn test_invert() { assert_eq!(mut_c, matrix3::C.invert().unwrap()); // Matrix4 - assert!(Matrix4::::one().invert().unwrap().is_one()); + assert!(Matrix4::::identity().invert().unwrap().is_identity()); 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_one()); + assert!(mat_c.invert().unwrap().mul_m(&mat_c).is_identity()); 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_one()); + assert!(mat_d.invert().unwrap().mul_m(&mat_d).is_identity()); 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_one()); + assert!(mat_e.invert().unwrap().mul_m(&mat_e).is_identity()); 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_one()); + assert!(mat_f.invert().unwrap().mul_m(&mat_f).is_identity()); } #[test] @@ -338,17 +338,17 @@ fn test_from_translation() { fn test_predicates() { // Matrix2 - assert!(Matrix2::::one().is_one()); - assert!(Matrix2::::one().is_symmetric()); - assert!(Matrix2::::one().is_diagonal()); - assert!(Matrix2::::one().is_invertible()); + assert!(Matrix2::::identity().is_identity()); + assert!(Matrix2::::identity().is_symmetric()); + assert!(Matrix2::::identity().is_diagonal()); + assert!(Matrix2::::identity().is_invertible()); - assert!(!matrix2::A.is_one()); + assert!(!matrix2::A.is_identity()); assert!(!matrix2::A.is_symmetric()); assert!(!matrix2::A.is_diagonal()); assert!(matrix2::A.is_invertible()); - assert!(!matrix2::C.is_one()); + assert!(!matrix2::C.is_identity()); 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::::one().is_one()); - assert!(Matrix3::::one().is_symmetric()); - assert!(Matrix3::::one().is_diagonal()); - assert!(Matrix3::::one().is_invertible()); + assert!(Matrix3::::identity().is_identity()); + assert!(Matrix3::::identity().is_symmetric()); + assert!(Matrix3::::identity().is_diagonal()); + assert!(Matrix3::::identity().is_invertible()); - assert!(!matrix3::A.is_one()); + assert!(!matrix3::A.is_identity()); assert!(!matrix3::A.is_symmetric()); assert!(!matrix3::A.is_diagonal()); assert!(!matrix3::A.is_invertible()); - assert!(!matrix3::D.is_one()); + assert!(!matrix3::D.is_identity()); 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::::one().is_one()); - assert!(Matrix4::::one().is_symmetric()); - assert!(Matrix4::::one().is_diagonal()); - assert!(Matrix4::::one().is_invertible()); + assert!(Matrix4::::identity().is_identity()); + assert!(Matrix4::::identity().is_symmetric()); + assert!(Matrix4::::identity().is_diagonal()); + assert!(Matrix4::::identity().is_invertible()); - assert!(!matrix4::A.is_one()); + assert!(!matrix4::A.is_identity()); assert!(!matrix4::A.is_symmetric()); assert!(!matrix4::A.is_diagonal()); assert!(!matrix4::A.is_invertible()); - assert!(!matrix4::D.is_one()); + assert!(!matrix4::D.is_identity()); assert!(matrix4::D.is_symmetric()); assert!(!matrix4::D.is_diagonal()); assert!(matrix4::D.is_invertible()); diff --git a/tests/rotation.rs b/tests/rotation.rs index 2ad4ba0..2ebe1a3 100644 --- a/tests/rotation.rs +++ b/tests/rotation.rs @@ -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_one()); + assert!(a.is_identity()); } #[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_one()); + assert!(a.is_identity()); }