diff --git a/src/lib.rs b/src/lib.rs index 9c82709a..6f12cb01 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,8 +69,8 @@ extern crate num_traits; pub use length::Length; pub use scale_factor::ScaleFactor; -pub use matrix2d::{Matrix2D, TypedMatrix2D}; -pub use matrix4d::{Matrix4D, TypedMatrix4D}; +pub use transform2d::{Transform2D, TypedTransform2D}; +pub use transform3d::{Transform3D, TypedTransform3D}; pub use point::{ Point2D, TypedPoint2D, Point3D, TypedPoint3D, @@ -85,8 +85,8 @@ pub mod approxeq; pub mod length; #[macro_use] mod macros; -pub mod matrix2d; -pub mod matrix4d; +pub mod transform2d; +pub mod transform3d; pub mod num; pub mod point; pub mod rect; @@ -110,3 +110,20 @@ pub type Radians = Length; /// A value in Degrees. pub type Degrees = Length; + +/// Temporary alias to facilitate the transition to the new naming scheme +#[deprecated] +pub type Matrix2D = Transform2D; + +/// Temporary alias to facilitate the transition to the new naming scheme +#[deprecated] +pub type TypedMatrix2D = TypedTransform2D; + +/// Temporary alias to facilitate the transition to the new naming scheme +#[deprecated] +pub type Matrix4D = Transform3D; + +/// Temporary alias to facilitate the transition to the new naming scheme +#[deprecated] +pub type TypedMatrix4D = TypedTransform3D; + diff --git a/src/matrix2d.rs b/src/transform2d.rs similarity index 70% rename from src/matrix2d.rs rename to src/transform2d.rs index 5c86a7ee..127533e1 100644 --- a/src/matrix2d.rs +++ b/src/transform2d.rs @@ -18,32 +18,31 @@ use trig::Trig; use std::fmt; define_matrix! { - /// A 2d transform stored as a 2 by 3 matrix in row-major order in memory, - /// useful to represent 2d transformations. + /// A 2d transform stored as a 2 by 3 matrix in row-major order in memory. /// - /// Matrices can be parametrized over the source and destination units, to describe a + /// Transforms can be parametrized over the source and destination units, to describe a /// transformation from a space to another. - /// For example, `TypedMatrix2D::transform_point4d` + /// For example, `TypedTransform2D::transform_point4d` /// takes a `TypedPoint2D` and returns a `TypedPoint2D`. /// - /// Matrices expose a set of convenience methods for pre- and post-transformations. + /// Transforms expose a set of convenience methods for pre- and post-transformations. /// A pre-transformation corresponds to adding an operation that is applied before /// the rest of the transformation, while a post-transformation adds an operation /// that is applied after. - pub struct TypedMatrix2D { + pub struct TypedTransform2D { pub m11: T, pub m12: T, pub m21: T, pub m22: T, pub m31: T, pub m32: T, } } -/// The default 2d matrix type with no units. -pub type Matrix2D = TypedMatrix2D; +/// The default 2d transform type with no units. +pub type Transform2D = TypedTransform2D; -impl TypedMatrix2D { - /// Create a matrix specifying its components in row-major order. - pub fn row_major(m11: T, m12: T, m21: T, m22: T, m31: T, m32: T) -> TypedMatrix2D { - TypedMatrix2D { +impl TypedTransform2D { + /// Create a transform specifying its matrix elements in row-major order. + pub fn row_major(m11: T, m12: T, m21: T, m22: T, m31: T, m32: T) -> TypedTransform2D { + TypedTransform2D { m11: m11, m12: m12, m21: m21, m22: m22, m31: m31, m32: m32, @@ -51,9 +50,9 @@ impl TypedMatrix2D { } } - /// Create a matrix specifying its components in column-major order. - pub fn column_major(m11: T, m21: T, m31: T, m12: T, m22: T, m32: T) -> TypedMatrix2D { - TypedMatrix2D { + /// Create a transform specifying its matrix elements in column-major order. + pub fn column_major(m11: T, m21: T, m31: T, m12: T, m22: T, m32: T) -> TypedTransform2D { + TypedTransform2D { m11: m11, m12: m12, m21: m21, m22: m22, m31: m31, m32: m32, @@ -61,8 +60,8 @@ impl TypedMatrix2D { } } - /// Returns an array containing this matrix's terms in row-major order (the order - /// in which the matrix is actually laid out in memory). + /// Returns an array containing this transform's terms in row-major order (the order + /// in which the transform is actually laid out in memory). pub fn to_row_major_array(&self) -> [T; 6] { [ self.m11, self.m12, @@ -71,7 +70,7 @@ impl TypedMatrix2D { ] } - /// Returns an array containing this matrix's terms in column-major order. + /// Returns an array containing this transform's terms in column-major order. pub fn to_column_major_array(&self) -> [T; 6] { [ self.m11, self.m21, self.m31, @@ -80,8 +79,8 @@ impl TypedMatrix2D { } /// Drop the units, preserving only the numeric value. - pub fn to_untyped(&self) -> Matrix2D { - Matrix2D::row_major( + pub fn to_untyped(&self) -> Transform2D { + Transform2D::row_major( self.m11, self.m12, self.m21, self.m22, self.m31, self.m32 @@ -89,8 +88,8 @@ impl TypedMatrix2D { } /// Tag a unitless value with units. - pub fn from_untyped(p: &Matrix2D) -> TypedMatrix2D { - TypedMatrix2D::row_major( + pub fn from_untyped(p: &Transform2D) -> TypedTransform2D { + TypedTransform2D::row_major( p.m11, p.m12, p.m21, p.m22, p.m31, p.m32 @@ -98,13 +97,13 @@ impl TypedMatrix2D { } } -impl TypedMatrix2D +impl TypedTransform2D where T: Copy + PartialEq + One + Zero { - pub fn identity() -> TypedMatrix2D { + pub fn identity() -> TypedTransform2D { let (_0, _1) = (Zero::zero(), One::one()); - TypedMatrix2D::row_major( + TypedTransform2D::row_major( _1, _0, _0, _1, _0, _0 @@ -115,11 +114,11 @@ where T: Copy + // while most consumers will probably want some sort of approximate // equivalence to deal with floating-point errors. fn is_identity(&self) -> bool { - *self == TypedMatrix2D::identity() + *self == TypedTransform2D::identity() } } -impl TypedMatrix2D +impl TypedTransform2D where T: Copy + Clone + Add + Mul + @@ -131,8 +130,8 @@ where T: Copy + Clone + /// Returns the multiplication of the two matrices such that mat's transformation /// applies after self's transformation. - pub fn post_mul(&self, mat: &TypedMatrix2D) -> TypedMatrix2D { - TypedMatrix2D::row_major( + pub fn post_mul(&self, mat: &TypedTransform2D) -> TypedTransform2D { + TypedTransform2D::row_major( self.m11 * mat.m11 + self.m12 * mat.m21, self.m11 * mat.m12 + self.m12 * mat.m22, self.m21 * mat.m11 + self.m22 * mat.m21, @@ -144,77 +143,77 @@ where T: Copy + Clone + /// Returns the multiplication of the two matrices such that mat's transformation /// applies before self's transformation. - pub fn pre_mul(&self, mat: &TypedMatrix2D) -> TypedMatrix2D { + pub fn pre_mul(&self, mat: &TypedTransform2D) -> TypedTransform2D { mat.post_mul(self) } - /// Returns a translation matrix. - pub fn create_translation(x: T, y: T) -> TypedMatrix2D { + /// Returns a translation transform. + pub fn create_translation(x: T, y: T) -> TypedTransform2D { let (_0, _1): (T, T) = (Zero::zero(), One::one()); - TypedMatrix2D::row_major( + TypedTransform2D::row_major( _1, _0, _0, _1, x, y ) } - /// Applies a translation after self's transformation and returns the resulting matrix. - pub fn post_translated(&self, x: T, y: T) -> TypedMatrix2D { - self.post_mul(&TypedMatrix2D::create_translation(x, y)) + /// Applies a translation after self's transformation and returns the resulting transform. + pub fn post_translated(&self, x: T, y: T) -> TypedTransform2D { + self.post_mul(&TypedTransform2D::create_translation(x, y)) } - /// Applies a translation before self's transformation and returns the resulting matrix. - pub fn pre_translated(&self, x: T, y: T) -> TypedMatrix2D { - self.pre_mul(&TypedMatrix2D::create_translation(x, y)) + /// Applies a translation before self's transformation and returns the resulting transform. + pub fn pre_translated(&self, x: T, y: T) -> TypedTransform2D { + self.pre_mul(&TypedTransform2D::create_translation(x, y)) } - /// Returns a scale matrix. - pub fn create_scale(x: T, y: T) -> TypedMatrix2D { + /// Returns a scale transform. + pub fn create_scale(x: T, y: T) -> TypedTransform2D { let _0 = Zero::zero(); - TypedMatrix2D::row_major( + TypedTransform2D::row_major( x, _0, _0, y, _0, _0 ) } - /// Applies a scale after self's transformation and returns the resulting matrix. - pub fn post_scaled(&self, x: T, y: T) -> TypedMatrix2D { - self.post_mul(&TypedMatrix2D::create_scale(x, y)) + /// Applies a scale after self's transformation and returns the resulting transform. + pub fn post_scaled(&self, x: T, y: T) -> TypedTransform2D { + self.post_mul(&TypedTransform2D::create_scale(x, y)) } - /// Applies a scale before self's transformation and returns the resulting matrix. - pub fn pre_scaled(&self, x: T, y: T) -> TypedMatrix2D { - TypedMatrix2D::row_major( + /// Applies a scale before self's transformation and returns the resulting transform. + pub fn pre_scaled(&self, x: T, y: T) -> TypedTransform2D { + TypedTransform2D::row_major( self.m11 * x, self.m12, self.m21, self.m22 * y, self.m31, self.m32 ) } - /// Returns a rotation matrix. - pub fn create_rotation(theta: Radians) -> TypedMatrix2D { + /// Returns a rotation transform. + pub fn create_rotation(theta: Radians) -> TypedTransform2D { let _0 = Zero::zero(); let cos = theta.get().cos(); let sin = theta.get().sin(); - TypedMatrix2D::row_major( + TypedTransform2D::row_major( cos, _0 - sin, sin, cos, _0, _0 ) } - /// Applies a rotation after self's transformation and returns the resulting matrix. - pub fn post_rotated(&self, theta: Radians) -> TypedMatrix2D { - self.post_mul(&TypedMatrix2D::create_rotation(theta)) + /// Applies a rotation after self's transformation and returns the resulting transform. + pub fn post_rotated(&self, theta: Radians) -> TypedTransform2D { + self.post_mul(&TypedTransform2D::create_rotation(theta)) } - /// Applies a rotation after self's transformation and returns the resulting matrix. - pub fn pre_rotated(&self, theta: Radians) -> TypedMatrix2D { - self.pre_mul(&TypedMatrix2D::create_rotation(theta)) + /// Applies a rotation after self's transformation and returns the resulting transform. + pub fn pre_rotated(&self, theta: Radians) -> TypedTransform2D { + self.pre_mul(&TypedTransform2D::create_rotation(theta)) } - /// Returns the given point transformed by this matrix. + /// Returns the given point transformed by this transform. #[inline] pub fn transform_point(&self, point: &TypedPoint2D) -> TypedPoint2D { TypedPoint2D::new(point.x * self.m11 + point.y * self.m21 + self.m31, @@ -222,7 +221,7 @@ where T: Copy + Clone + } /// Returns a rectangle that encompasses the result of transforming the given rectangle by this - /// matrix. + /// transform. #[inline] pub fn transform_rect(&self, rect: &TypedRect) -> TypedRect { TypedRect::from_points(&[ @@ -233,13 +232,13 @@ where T: Copy + Clone + ]) } - /// Computes and returns the determinant of this matrix. + /// Computes and returns the determinant of this transform. pub fn determinant(&self) -> T { self.m11 * self.m22 - self.m12 * self.m21 } - /// Returns the inverse matrix if possible. - pub fn inverse(&self) -> Option> { + /// Returns the inverse transform if possible. + pub fn inverse(&self) -> Option> { let det = self.determinant(); let _0: T = Zero::zero(); @@ -250,7 +249,7 @@ where T: Copy + Clone + } let inv_det = _1 / det; - Some(TypedMatrix2D::row_major( + Some(TypedTransform2D::row_major( inv_det * self.m22, inv_det * (_0 - self.m12), inv_det * (_0 - self.m21), @@ -260,20 +259,20 @@ where T: Copy + Clone + )) } - /// Returns the same matrix with a different destination unit. + /// Returns the same transform with a different destination unit. #[inline] - pub fn with_destination(&self) -> TypedMatrix2D { - TypedMatrix2D::row_major( + pub fn with_destination(&self) -> TypedTransform2D { + TypedTransform2D::row_major( self.m11, self.m12, self.m21, self.m22, self.m31, self.m32, ) } - /// Returns the same matrix with a different source unit. + /// Returns the same transform with a different source unit. #[inline] - pub fn with_source(&self) -> TypedMatrix2D { - TypedMatrix2D::row_major( + pub fn with_source(&self) -> TypedTransform2D { + TypedTransform2D::row_major( self.m11, self.m12, self.m21, self.m22, self.m31, self.m32, @@ -281,7 +280,7 @@ where T: Copy + Clone + } } -impl, Src, Dst> TypedMatrix2D { +impl, Src, Dst> TypedTransform2D { pub fn approx_eq(&self, other: &Self) -> bool { self.m11.approx_eq(&other.m11) && self.m12.approx_eq(&other.m12) && self.m21.approx_eq(&other.m21) && self.m22.approx_eq(&other.m22) && @@ -289,7 +288,7 @@ impl, Src, Dst> TypedMatrix2D { } } -impl fmt::Debug for TypedMatrix2D +impl fmt::Debug for TypedTransform2D where T: Copy + fmt::Debug + PartialEq + One + Zero { @@ -311,7 +310,7 @@ mod test { use std::f32::consts::FRAC_PI_2; - type Mat = Matrix2D; + type Mat = Transform2D; fn rad(v: f32) -> Radians { Radians::new(v) } @@ -396,8 +395,8 @@ mod test { #[test] pub fn test_pre_post() { - let m1 = Matrix2D::identity().post_scaled(1.0, 2.0).post_translated(1.0, 2.0); - let m2 = Matrix2D::identity().pre_translated(1.0, 2.0).pre_scaled(1.0, 2.0); + let m1 = Transform2D::identity().post_scaled(1.0, 2.0).post_translated(1.0, 2.0); + let m2 = Transform2D::identity().pre_translated(1.0, 2.0).pre_scaled(1.0, 2.0); assert!(m1.approx_eq(&m2)); let r = Mat::create_rotation(rad(FRAC_PI_2)); @@ -417,13 +416,13 @@ mod test { #[test] fn test_size_of() { use std::mem::size_of; - assert_eq!(size_of::>(), 6*size_of::()); - assert_eq!(size_of::>(), 6*size_of::()); + assert_eq!(size_of::>(), 6*size_of::()); + assert_eq!(size_of::>(), 6*size_of::()); } #[test] pub fn test_is_identity() { - let m1 = Matrix2D::identity(); + let m1 = Transform2D::identity(); assert!(m1.is_identity()); let m2 = m1.post_translated(0.1, 0.0); assert!(!m2.is_identity()); diff --git a/src/matrix4d.rs b/src/transform3d.rs similarity index 79% rename from src/matrix4d.rs rename to src/transform3d.rs index 2bccf8de..a42216ef 100644 --- a/src/matrix4d.rs +++ b/src/transform3d.rs @@ -12,7 +12,7 @@ use approxeq::ApproxEq; use trig::Trig; use point::{TypedPoint2D, TypedPoint3D, TypedPoint4D}; use rect::TypedRect; -use matrix2d::TypedMatrix2D; +use transform2d::TypedTransform2D; use scale_factor::ScaleFactor; use num::{One, Zero}; use std::ops::{Add, Mul, Sub, Div, Neg}; @@ -20,19 +20,18 @@ use std::marker::PhantomData; use std::fmt; define_matrix! { - /// A 4 by 4 matrix stored in row-major order in memory, useful to represent - /// 3d transformations. + /// A 3d transform stored as a 4 by 4 matrix in row-major order in memory. /// - /// Matrices can be parametrized over the source and destination units, to describe a + /// Transforms can be parametrized over the source and destination units, to describe a /// transformation from a space to another. - /// For example, `TypedMatrix4D::transform_point4d` + /// For example, `TypedTransform3D::transform_point4d` /// takes a `TypedPoint4D` and returns a `TypedPoint4D`. /// - /// Matrices expose a set of convenience methods for pre- and post-transformations. + /// Transforms expose a set of convenience methods for pre- and post-transformations. /// A pre-transformation corresponds to adding an operation that is applied before /// the rest of the transformation, while a post-transformation adds an operation /// that is applied after. - pub struct TypedMatrix4D { + pub struct TypedTransform3D { pub m11: T, pub m12: T, pub m13: T, pub m14: T, pub m21: T, pub m22: T, pub m23: T, pub m24: T, pub m31: T, pub m32: T, pub m33: T, pub m34: T, @@ -40,11 +39,11 @@ define_matrix! { } } -/// The default 4d matrix type with no units. -pub type Matrix4D = TypedMatrix4D; +/// The default 4d transform type with no units. +pub type Transform3D = TypedTransform3D; -impl TypedMatrix4D { - /// Create a matrix specifying its components in row-major order. +impl TypedTransform3D { + /// Create a transform specifying its components in row-major order. /// /// For example, the translation terms m41, m42, m43 on the last row with the /// row-major convention) are the 13rd, 14th and 15th parameters. @@ -54,8 +53,8 @@ impl TypedMatrix4D { m21: T, m22: T, m23: T, m24: T, m31: T, m32: T, m33: T, m34: T, m41: T, m42: T, m43: T, m44: T) - -> TypedMatrix4D { - TypedMatrix4D { + -> TypedTransform3D { + TypedTransform3D { m11: m11, m12: m12, m13: m13, m14: m14, m21: m21, m22: m22, m23: m23, m24: m24, m31: m31, m32: m32, m33: m33, m34: m34, @@ -64,7 +63,7 @@ impl TypedMatrix4D { } } - /// Create a matrix specifying its components in column-major order. + /// Create a transform specifying its components in column-major order. /// /// For example, the translation terms m41, m42, m43 on the last column with the /// column-major convention) are the 4th, 8th and 12nd parameters. @@ -74,8 +73,8 @@ impl TypedMatrix4D { m12: T, m22: T, m32: T, m42: T, m13: T, m23: T, m33: T, m43: T, m14: T, m24: T, m34: T, m44: T) - -> TypedMatrix4D { - TypedMatrix4D { + -> TypedTransform3D { + TypedTransform3D { m11: m11, m12: m12, m13: m13, m14: m14, m21: m21, m22: m22, m23: m23, m24: m24, m31: m31, m32: m32, m33: m33, m34: m34, @@ -85,14 +84,14 @@ impl TypedMatrix4D { } } -impl TypedMatrix4D +impl TypedTransform3D where T: Copy + Clone + PartialEq + One + Zero { #[inline] - pub fn identity() -> TypedMatrix4D { + pub fn identity() -> TypedTransform3D { let (_0, _1): (T, T) = (Zero::zero(), One::one()); - TypedMatrix4D::row_major( + TypedTransform3D::row_major( _1, _0, _0, _0, _0, _1, _0, _0, _0, _0, _1, _0, @@ -105,11 +104,11 @@ where T: Copy + Clone + // equivalence to deal with floating-point errors. #[inline] fn is_identity(&self) -> bool { - *self == TypedMatrix4D::identity() + *self == TypedTransform3D::identity() } } -impl TypedMatrix4D +impl TypedTransform3D where T: Copy + Clone + Add + Sub + @@ -121,12 +120,12 @@ where T: Copy + Clone + Trig + One + Zero { - /// Create a 4 by 4 matrix representing a 2d transformation, specifying its components + /// Create a 4 by 4 transform representing a 2d transformation, specifying its components /// in row-major order. #[inline] - pub fn row_major_2d(m11: T, m12: T, m21: T, m22: T, m41: T, m42: T) -> TypedMatrix4D { + pub fn row_major_2d(m11: T, m12: T, m21: T, m22: T, m41: T, m42: T) -> TypedTransform3D { let (_0, _1): (T, T) = (Zero::zero(), One::one()); - TypedMatrix4D::row_major( + TypedTransform3D::row_major( m11, m12, _0, _0, m21, m22, _0, _0, _0, _0, _1, _0, @@ -134,17 +133,17 @@ where T: Copy + Clone + ) } - /// Create an orthogonal projection matrix. + /// Create an orthogonal projection transform. pub fn ortho(left: T, right: T, bottom: T, top: T, - near: T, far: T) -> TypedMatrix4D { + near: T, far: T) -> TypedTransform3D { let tx = -((right + left) / (right - left)); let ty = -((top + bottom) / (top - bottom)); let tz = -((far + near) / (far - near)); let (_0, _1): (T, T) = (Zero::zero(), One::one()); let _2 = _1 + _1; - TypedMatrix4D::row_major( + TypedTransform3D::row_major( _2 / (right - left), _0 , _0 , _0, _0 , _2 / (top - bottom), _0 , _0, _0 , _0 , -_2 / (far - near), _0, @@ -152,9 +151,9 @@ where T: Copy + Clone + ) } - /// Returns true if this matrix can be represented with a TypedMatrix2D. + /// Returns true if this transform can be represented with a TypedTransform2D. /// - /// See https://drafts.csswg.org/css-transforms/#2d-matrix + /// See https://drafts.csswg.org/css-transforms/#2d-transform #[inline] pub fn is_2d(&self) -> bool { let (_0, _1): (T, T) = (Zero::zero(), One::one()); @@ -165,19 +164,19 @@ where T: Copy + Clone + self.m33 == _1 && self.m44 == _1 } - /// Create a 2D matrix picking the relevent terms from this matrix. + /// Create a 2D transform picking the relevent terms from this transform. /// /// This method assumes that self represents a 2d transformation, callers /// should check that self.is_2d() returns true beforehand. - pub fn to_2d(&self) -> TypedMatrix2D { - TypedMatrix2D::row_major( + pub fn to_2d(&self) -> TypedTransform2D { + TypedTransform2D::row_major( self.m11, self.m12, self.m21, self.m22, self.m41, self.m42 ) } - pub fn approx_eq(&self, other: &TypedMatrix4D) -> bool { + pub fn approx_eq(&self, other: &TypedTransform3D) -> bool { self.m11.approx_eq(&other.m11) && self.m12.approx_eq(&other.m12) && self.m13.approx_eq(&other.m13) && self.m14.approx_eq(&other.m14) && self.m21.approx_eq(&other.m21) && self.m22.approx_eq(&other.m22) && @@ -188,10 +187,10 @@ where T: Copy + Clone + self.m43.approx_eq(&other.m43) && self.m44.approx_eq(&other.m44) } - /// Returns the same matrix with a different destination unit. + /// Returns the same transform with a different destination unit. #[inline] - pub fn with_destination(&self) -> TypedMatrix4D { - TypedMatrix4D::row_major( + pub fn with_destination(&self) -> TypedTransform3D { + TypedTransform3D::row_major( self.m11, self.m12, self.m13, self.m14, self.m21, self.m22, self.m23, self.m24, self.m31, self.m32, self.m33, self.m34, @@ -199,10 +198,10 @@ where T: Copy + Clone + ) } - /// Returns the same matrix with a different source unit. + /// Returns the same transform with a different source unit. #[inline] - pub fn with_source(&self) -> TypedMatrix4D { - TypedMatrix4D::row_major( + pub fn with_source(&self) -> TypedTransform3D { + TypedTransform3D::row_major( self.m11, self.m12, self.m13, self.m14, self.m21, self.m22, self.m23, self.m24, self.m31, self.m32, self.m33, self.m34, @@ -212,8 +211,8 @@ where T: Copy + Clone + /// Drop the units, preserving only the numeric value. #[inline] - pub fn to_untyped(&self) -> Matrix4D { - Matrix4D::row_major( + pub fn to_untyped(&self) -> Transform3D { + Transform3D::row_major( self.m11, self.m12, self.m13, self.m14, self.m21, self.m22, self.m23, self.m24, self.m31, self.m32, self.m33, self.m34, @@ -223,8 +222,8 @@ where T: Copy + Clone + /// Tag a unitless value with units. #[inline] - pub fn from_untyped(m: &Matrix4D) -> Self { - TypedMatrix4D::row_major( + pub fn from_untyped(m: &Transform3D) -> Self { + TypedTransform3D::row_major( m.m11, m.m12, m.m13, m.m14, m.m21, m.m22, m.m23, m.m24, m.m31, m.m32, m.m33, m.m34, @@ -234,8 +233,8 @@ where T: Copy + Clone + /// Returns the multiplication of the two matrices such that mat's transformation /// applies after self's transformation. - pub fn post_mul(&self, mat: &TypedMatrix4D) -> TypedMatrix4D { - TypedMatrix4D::row_major( + pub fn post_mul(&self, mat: &TypedTransform3D) -> TypedTransform3D { + TypedTransform3D::row_major( self.m11 * mat.m11 + self.m12 * mat.m21 + self.m13 * mat.m31 + self.m14 * mat.m41, self.m11 * mat.m12 + self.m12 * mat.m22 + self.m13 * mat.m32 + self.m14 * mat.m42, self.m11 * mat.m13 + self.m12 * mat.m23 + self.m13 * mat.m33 + self.m14 * mat.m43, @@ -257,12 +256,12 @@ where T: Copy + Clone + /// Returns the multiplication of the two matrices such that mat's transformation /// applies before self's transformation. - pub fn pre_mul(&self, mat: &TypedMatrix4D) -> TypedMatrix4D { + pub fn pre_mul(&self, mat: &TypedTransform3D) -> TypedTransform3D { mat.post_mul(self) } - /// Returns the inverse matrix if possible. - pub fn inverse(&self) -> Option> { + /// Returns the inverse transform if possible. + pub fn inverse(&self) -> Option> { let det = self.determinant(); if det == Zero::zero() { @@ -270,8 +269,8 @@ where T: Copy + Clone + } // todo(gw): this could be made faster by special casing - // for simpler matrix types. - let m = TypedMatrix4D::row_major( + // for simpler transform types. + let m = TypedTransform3D::row_major( self.m23*self.m34*self.m42 - self.m24*self.m33*self.m42 + self.m24*self.m32*self.m43 - self.m22*self.m34*self.m43 - self.m23*self.m32*self.m44 + self.m22*self.m33*self.m44, @@ -341,7 +340,7 @@ where T: Copy + Clone + Some(m.mul_s(_1 / det)) } - /// Compute the determinant of the matrix. + /// Compute the determinant of the transform. pub fn determinant(&self) -> T { self.m14 * self.m23 * self.m32 * self.m41 - self.m13 * self.m24 * self.m32 * self.m41 - @@ -369,9 +368,9 @@ where T: Copy + Clone + self.m11 * self.m22 * self.m33 * self.m44 } - /// Multiplies all of the matrix's component by a scalar and returns the result. - pub fn mul_s(&self, x: T) -> TypedMatrix4D { - TypedMatrix4D::row_major( + /// Multiplies all of the transform's component by a scalar and returns the result. + pub fn mul_s(&self, x: T) -> TypedTransform3D { + TypedTransform3D::row_major( self.m11 * x, self.m12 * x, self.m13 * x, self.m14 * x, self.m21 * x, self.m22 * x, self.m23 * x, self.m24 * x, self.m31 * x, self.m32 * x, self.m33 * x, self.m34 * x, @@ -379,12 +378,12 @@ where T: Copy + Clone + ) } - /// Convenience function to create a scale matrix from a ScaleFactor. - pub fn from_scale_factor(scale: ScaleFactor) -> TypedMatrix4D { - TypedMatrix4D::create_scale(scale.get(), scale.get(), scale.get()) + /// Convenience function to create a scale transform from a ScaleFactor. + pub fn from_scale_factor(scale: ScaleFactor) -> TypedTransform3D { + TypedTransform3D::create_scale(scale.get(), scale.get(), scale.get()) } - /// Returns the given 2d point transformed by this matrix. + /// Returns the given 2d point transformed by this transform. /// /// The input point must be use the unit Src, and the returned point has the unit Dst. #[inline] @@ -392,7 +391,7 @@ where T: Copy + Clone + self.transform_point4d(&TypedPoint4D::new(p.x, p.y, Zero::zero(), One::one())).to_2d() } - /// Returns the given 3d point transformed by this matrix. + /// Returns the given 3d point transformed by this transform. /// /// The input point must be use the unit Src, and the returned point has the unit Dst. #[inline] @@ -400,7 +399,7 @@ where T: Copy + Clone + self.transform_point4d(&TypedPoint4D::new(p.x, p.y, p.z, One::one())).to_3d() } - /// Returns the given 4d point transformed by this matrix. + /// Returns the given 4d point transformed by this transform. /// /// The input point must be use the unit Src, and the returned point has the unit Dst. #[inline] @@ -413,7 +412,7 @@ where T: Copy + Clone + } /// Returns a rectangle that encompasses the result of transforming the given rectangle by this - /// matrix. + /// transform. pub fn transform_rect(&self, rect: &TypedRect) -> TypedRect { TypedRect::from_points(&[ self.transform_point(&rect.origin), @@ -423,10 +422,10 @@ where T: Copy + Clone + ]) } - /// Create a 3d translation matrix - pub fn create_translation(x: T, y: T, z: T) -> TypedMatrix4D { + /// Create a 3d translation transform + pub fn create_translation(x: T, y: T, z: T) -> TypedTransform3D { let (_0, _1): (T, T) = (Zero::zero(), One::one()); - TypedMatrix4D::row_major( + TypedTransform3D::row_major( _1, _0, _0, _0, _0, _1, _0, _0, _0, _0, _1, _0, @@ -434,20 +433,20 @@ where T: Copy + Clone + ) } - /// Returns a matrix with a translation applied before self's transformation. - pub fn pre_translated(&self, x: T, y: T, z: T) -> TypedMatrix4D { - self.pre_mul(&TypedMatrix4D::create_translation(x, y, z)) + /// Returns a transform with a translation applied before self's transformation. + pub fn pre_translated(&self, x: T, y: T, z: T) -> TypedTransform3D { + self.pre_mul(&TypedTransform3D::create_translation(x, y, z)) } - /// Returns a matrix with a translation applied after self's transformation. - pub fn post_translated(&self, x: T, y: T, z: T) -> TypedMatrix4D { - self.post_mul(&TypedMatrix4D::create_translation(x, y, z)) + /// Returns a transform with a translation applied after self's transformation. + pub fn post_translated(&self, x: T, y: T, z: T) -> TypedTransform3D { + self.post_mul(&TypedTransform3D::create_translation(x, y, z)) } - /// Create a 3d scale matrix - pub fn create_scale(x: T, y: T, z: T) -> TypedMatrix4D { + /// Create a 3d scale transform + pub fn create_scale(x: T, y: T, z: T) -> TypedTransform3D { let (_0, _1): (T, T) = (Zero::zero(), One::one()); - TypedMatrix4D::row_major( + TypedTransform3D::row_major( x, _0, _0, _0, _0, y, _0, _0, _0, _0, z, _0, @@ -455,9 +454,9 @@ where T: Copy + Clone + ) } - /// Returns a matrix with a scale applied before self's transformation. - pub fn pre_scaled(&self, x: T, y: T, z: T) -> TypedMatrix4D { - TypedMatrix4D::row_major( + /// Returns a transform with a scale applied before self's transformation. + pub fn pre_scaled(&self, x: T, y: T, z: T) -> TypedTransform3D { + TypedTransform3D::row_major( self.m11 * x, self.m12, self.m13, self.m14, self.m21 , self.m22 * y, self.m23, self.m24, self.m31 , self.m32, self.m33 * z, self.m34, @@ -465,14 +464,14 @@ where T: Copy + Clone + ) } - /// Returns a matrix with a scale applied after self's transformation. - pub fn post_scaled(&self, x: T, y: T, z: T) -> TypedMatrix4D { - self.post_mul(&TypedMatrix4D::create_scale(x, y, z)) + /// Returns a transform with a scale applied after self's transformation. + pub fn post_scaled(&self, x: T, y: T, z: T) -> TypedTransform3D { + self.post_mul(&TypedTransform3D::create_scale(x, y, z)) } - /// Create a 3d rotation matrix from an angle / axis. + /// Create a 3d rotation transform from an angle / axis. /// The supplied axis must be normalized. - pub fn create_rotation(x: T, y: T, z: T, theta: Radians) -> TypedMatrix4D { + pub fn create_rotation(x: T, y: T, z: T, theta: Radians) -> TypedTransform3D { let (_0, _1): (T, T) = (Zero::zero(), One::one()); let _2 = _1 + _1; @@ -484,7 +483,7 @@ where T: Copy + Clone + let sc = half_theta.sin() * half_theta.cos(); let sq = half_theta.sin() * half_theta.sin(); - TypedMatrix4D::row_major( + TypedTransform3D::row_major( _1 - _2 * (yy + zz) * sq, _2 * (x * y * sq - z * sc), _2 * (x * z * sq + y * sc), @@ -507,23 +506,23 @@ where T: Copy + Clone + ) } - /// Returns a matrix with a rotation applied after self's transformation. - pub fn post_rotated(&self, x: T, y: T, z: T, theta: Radians) -> TypedMatrix4D { - self.post_mul(&TypedMatrix4D::create_rotation(x, y, z, theta)) + /// Returns a transform with a rotation applied after self's transformation. + pub fn post_rotated(&self, x: T, y: T, z: T, theta: Radians) -> TypedTransform3D { + self.post_mul(&TypedTransform3D::create_rotation(x, y, z, theta)) } - /// Returns a matrix with a rotation applied before self's transformation. - pub fn pre_rotated(&self, x: T, y: T, z: T, theta: Radians) -> TypedMatrix4D { - self.pre_mul(&TypedMatrix4D::create_rotation(x, y, z, theta)) + /// Returns a transform with a rotation applied before self's transformation. + pub fn pre_rotated(&self, x: T, y: T, z: T, theta: Radians) -> TypedTransform3D { + self.pre_mul(&TypedTransform3D::create_rotation(x, y, z, theta)) } - /// Create a 2d skew matrix. + /// Create a 2d skew transform. /// /// See https://drafts.csswg.org/css-transforms/#funcdef-skew - pub fn create_skew(alpha: Radians, beta: Radians) -> TypedMatrix4D { + pub fn create_skew(alpha: Radians, beta: Radians) -> TypedTransform3D { let (_0, _1): (T, T) = (Zero::zero(), One::one()); let (sx, sy) = (beta.get().tan(), alpha.get().tan()); - TypedMatrix4D::row_major( + TypedTransform3D::row_major( _1, sx, _0, _0, sy, _1, _0, _0, _0, _0, _1, _0, @@ -531,10 +530,10 @@ where T: Copy + Clone + ) } - /// Create a simple perspective projection matrix - pub fn create_perspective(d: T) -> TypedMatrix4D { + /// Create a simple perspective projection transform + pub fn create_perspective(d: T) -> TypedTransform3D { let (_0, _1): (T, T) = (Zero::zero(), One::one()); - TypedMatrix4D::row_major( + TypedTransform3D::row_major( _1, _0, _0, _0, _0, _1, _0, _0, _0, _0, _1, -_1 / d, @@ -543,9 +542,9 @@ where T: Copy + Clone + } } -impl TypedMatrix4D { - /// Returns an array containing this matrix's terms in row-major order (the order - /// in which the matrix is actually laid out in memory). +impl TypedTransform3D { + /// Returns an array containing this transform's terms in row-major order (the order + /// in which the transform is actually laid out in memory). pub fn to_row_major_array(&self) -> [T; 16] { [ self.m11, self.m12, self.m13, self.m14, @@ -555,7 +554,7 @@ impl TypedMatrix4D { ] } - /// Returns an array containing this matrix's terms in column-major order. + /// Returns an array containing this transform's terms in column-major order. pub fn to_column_major_array(&self) -> [T; 16] { [ self.m11, self.m21, self.m31, self.m41, @@ -565,7 +564,7 @@ impl TypedMatrix4D { ] } - /// Returns an array containing this matrix's 4 rows in (in row-major order) + /// Returns an array containing this transform's 4 rows in (in row-major order) /// as arrays. /// /// This is a convenience method to interface with other libraries like glium. @@ -578,7 +577,7 @@ impl TypedMatrix4D { ] } - /// Returns an array containing this matrix's 4 columns in (in row-major order, + /// Returns an array containing this transform's 4 columns in (in row-major order, /// or 4 rows in column-major order) as arrays. /// /// This is a convenience method to interface with other libraries like glium. @@ -592,7 +591,7 @@ impl TypedMatrix4D { } } -impl fmt::Debug for TypedMatrix4D +impl fmt::Debug for TypedTransform3D where T: Copy + fmt::Debug + PartialEq + One + Zero { @@ -608,14 +607,14 @@ where T: Copy + fmt::Debug + #[cfg(test)] mod tests { use approxeq::ApproxEq; - use matrix2d::Matrix2D; + use transform2d::Transform2D; use point::{Point2D, Point3D, Point4D}; use Radians; use super::*; use std::f32::consts::FRAC_PI_2; - type Mf32 = Matrix4D; + type Mf32 = Transform3D; // For convenience. fn rad(v: f32) -> Radians { Radians::new(v) } @@ -634,7 +633,7 @@ mod tests { assert_eq!(t1.post_mul(&t1), Mf32::create_translation(2.0, 4.0, 6.0)); assert!(!t1.is_2d()); - assert_eq!(Mf32::create_translation(1.0, 2.0, 3.0).to_2d(), Matrix2D::create_translation(1.0, 2.0)); + assert_eq!(Mf32::create_translation(1.0, 2.0, 3.0).to_2d(), Transform2D::create_translation(1.0, 2.0)); } #[test] @@ -651,7 +650,7 @@ mod tests { assert!(r1.post_mul(&r1).approx_eq(&Mf32::create_rotation(0.0, 0.0, 1.0, rad(FRAC_PI_2*2.0)))); assert!(r1.is_2d()); - assert!(r1.to_2d().approx_eq(&Matrix2D::create_rotation(rad(FRAC_PI_2)))); + assert!(r1.to_2d().approx_eq(&Transform2D::create_rotation(rad(FRAC_PI_2)))); } #[test] @@ -668,7 +667,7 @@ mod tests { assert_eq!(s1.post_mul(&s1), Mf32::create_scale(4.0, 9.0, 16.0)); assert!(!s1.is_2d()); - assert_eq!(Mf32::create_scale(2.0, 3.0, 0.0).to_2d(), Matrix2D::create_scale(2.0, 3.0)); + assert_eq!(Mf32::create_scale(2.0, 3.0, 0.0).to_2d(), Transform2D::create_scale(2.0, 3.0)); } #[test] @@ -773,8 +772,8 @@ mod tests { #[test] pub fn test_pre_post() { - let m1 = Matrix4D::identity().post_scaled(1.0, 2.0, 3.0).post_translated(1.0, 2.0, 3.0); - let m2 = Matrix4D::identity().pre_translated(1.0, 2.0, 3.0).pre_scaled(1.0, 2.0, 3.0); + let m1 = Transform3D::identity().post_scaled(1.0, 2.0, 3.0).post_translated(1.0, 2.0, 3.0); + let m2 = Transform3D::identity().pre_translated(1.0, 2.0, 3.0).pre_scaled(1.0, 2.0, 3.0); assert!(m1.approx_eq(&m2)); let r = Mf32::create_rotation(0.0, 0.0, 1.0, rad(FRAC_PI_2)); @@ -794,8 +793,8 @@ mod tests { #[test] fn test_size_of() { use std::mem::size_of; - assert_eq!(size_of::>(), 16*size_of::()); - assert_eq!(size_of::>(), 16*size_of::()); + assert_eq!(size_of::>(), 16*size_of::()); + assert_eq!(size_of::>(), 16*size_of::()); } #[test] @@ -817,7 +816,7 @@ mod tests { #[test] pub fn test_is_identity() { - let m1 = Matrix4D::identity(); + let m1 = Transform3D::identity(); assert!(m1.is_identity()); let m2 = m1.post_translated(0.1, 0.0, 0.0); assert!(!m2.is_identity());