#[repr(C)]pub struct Mat2<T> {
pub a11: T,
pub a12: T,
pub a21: T,
pub a22: T,
}Expand description
2D transformation matrix.
Each field aij represents the i-th row and j-th column of the matrix.
Row-major storage with column-major semantics.
Stored in row-major order (fields appear in reading order),
but interpreted as column-major: each column is a transformed basis vector,
and matrices are applied to column vectors via mat * vec.
Fields§
§a11: T§a12: T§a21: T§a22: TImplementations§
Source§impl<T: Float> Mat2<T>
impl<T: Float> Mat2<T>
Sourcepub fn scaling(scale: Vec2<T>) -> Mat2<T>
pub fn scaling(scale: Vec2<T>) -> Mat2<T>
Scaling matrix.
Scales around the origin.
let mat = cvmath::Mat2::scaling(cvmath::Vec2(2.0, 3.0));
let value = mat * cvmath::Vec2(4.0, 5.0);
let expected = cvmath::Vec2(8.0, 15.0);
assert_eq!(expected, value);Sourcepub fn rotation(angle: Angle<T>) -> Mat2<T>
pub fn rotation(angle: Angle<T>) -> Mat2<T>
Rotation matrix.
Rotates around the origin.
let mat = cvmath::Mat2::rotation(cvmath::Angle::deg(90.0));
let value = (mat * cvmath::Vec2(1.0f64, 1.0)).cast::<f32>();
let expected = cvmath::Vec2(-1.0f32, 1.0);
assert_eq!(expected, value);Sourcepub fn rotation_between(from: Vec2<T>, to: Vec2<T>) -> Mat2<T>
pub fn rotation_between(from: Vec2<T>, to: Vec2<T>) -> Mat2<T>
Returns the shortest rotation that aligns vector from with vector to.
The resulting matrix R satisfies:
R * from = toBoth vectors are expected to be normalized. The implementation avoids trigonometric functions.
The 2D rotation is uniquely defined even when the vectors are opposite, so this returns a 180° rotation in that case.
This is useful for constructing an orientation matrix that points one direction vector toward another.
let from = cvmath::Vec2(1.0, 1.0).norm();
let to = cvmath::Vec2(-1.0, 1.0).norm();
let mat = cvmath::Mat2::rotation_between(from, to);
let expected = to.cast::<f32>();
let value = (mat * from).cast::<f32>();
assert_eq!(expected, value);Sourcepub fn reflection(axis: Vec2<T>) -> Mat2<T>
pub fn reflection(axis: Vec2<T>) -> Mat2<T>
Reflection matrix.
Reflects around the given axis. If axis is the zero vector, returns a point reflection around the origin.
let mat = cvmath::Mat2::reflection(cvmath::Vec2d::Y);
let value = mat * cvmath::Vec2(2.0, 3.0);
let expected = cvmath::Vec2(-2.0, 3.0);
assert_eq!(expected, value);Sourcepub fn projection(axis: Vec2<T>) -> Mat2<T>
pub fn projection(axis: Vec2<T>) -> Mat2<T>
Projection matrix.
Projects onto the given axis. If axis is the zero vector, returns the zero matrix.
let mat = cvmath::Mat2::projection(cvmath::Vec2d::X);
let value = mat * cvmath::Vec2(2.0, 3.0);
let expected = cvmath::Vec2(2.0, 0.0);
assert_eq!(expected, value);Source§impl<T> Mat2<T>
impl<T> Mat2<T>
Sourcepub fn transform2(self) -> Transform2<T>where
T: Zero,
pub fn transform2(self) -> Transform2<T>where
T: Zero,
Converts to a Transform2 matrix.
let mat = cvmath::Mat2(1, 2, 3, 4).transform2();
let value = mat.into_row_major();
let expected = [[1, 2, 0], [3, 4, 0]];
assert_eq!(expected, value);Sourcepub fn translate(self, trans: Vec2<T>) -> Transform2<T>
pub fn translate(self, trans: Vec2<T>) -> Transform2<T>
Adds a translation to the matrix.
let mat = cvmath::Mat2::IDENTITY.translate(cvmath::Vec2(5, 6));
let value = mat.into_row_major();
let expected = [[1, 0, 5], [0, 1, 6]];
assert_eq!(expected, value);Source§impl<T> Mat2<T>
impl<T> Mat2<T>
Sourcepub fn from_row_major(mat: [[T; 2]; 2]) -> Mat2<T>
pub fn from_row_major(mat: [[T; 2]; 2]) -> Mat2<T>
Imports the matrix from a row-major layout.
let mat = cvmath::Mat2::from_row_major([[1, 2], [3, 4]]);
let expected = cvmath::Mat2(1, 2, 3, 4);
assert_eq!(expected, mat);Sourcepub fn from_column_major(mat: [[T; 2]; 2]) -> Mat2<T>
pub fn from_column_major(mat: [[T; 2]; 2]) -> Mat2<T>
Imports the matrix from a column-major layout.
let mat = cvmath::Mat2::from_column_major([[1, 3], [2, 4]]);
let expected = cvmath::Mat2(1, 2, 3, 4);
assert_eq!(expected, mat);Sourcepub fn into_row_major(self) -> [[T; 2]; 2]
pub fn into_row_major(self) -> [[T; 2]; 2]
Exports the matrix as a row-major array.
let mat = cvmath::Mat2(1, 2, 3, 4).into_row_major();
let expected = [[1, 2], [3, 4]];
assert_eq!(expected, mat);Sourcepub fn into_column_major(self) -> [[T; 2]; 2]
pub fn into_column_major(self) -> [[T; 2]; 2]
Exports the matrix as a column-major array.
let mat = cvmath::Mat2(1, 2, 3, 4).into_column_major();
let expected = [[1, 3], [2, 4]];
assert_eq!(expected, mat);Source§impl<T> Mat2<T>
impl<T> Mat2<T>
Sourcepub fn compose(x: Vec2<T>, y: Vec2<T>) -> Mat2<T>
pub fn compose(x: Vec2<T>, y: Vec2<T>) -> Mat2<T>
Composes the matrix from basis vectors.
let mat = cvmath::Mat2::compose(cvmath::Vec2(1, 2), cvmath::Vec2(3, 4));
let value = mat.into_row_major();
let expected = [[1, 3], [2, 4]];
assert_eq!(expected, value);Source§impl<T: Scalar> Mat2<T>
impl<T: Scalar> Mat2<T>
Sourcepub fn det(self) -> T
pub fn det(self) -> T
Computes the determinant.
let value = cvmath::Mat2(1, 2, 3, 4).det();
assert_eq!(-2, value);Sourcepub fn trace(self) -> T
pub fn trace(self) -> T
Computes the trace.
let value = cvmath::Mat2(1, 2, 3, 4).trace();
assert_eq!(5, value);Sourcepub fn flat_norm_sqr(self) -> T
pub fn flat_norm_sqr(self) -> T
Computes the squared Frobenius norm (sum of squares of all matrix elements).
This measure is useful for quickly checking matrix magnitude or comparing matrices without the cost of a square root operation.
To check if a matrix is effectively zero, test if flat_norm_sqr() is below a small epsilon threshold.
let value = cvmath::Mat2(1, 2, 3, 4).flat_norm_sqr();
assert_eq!(30, value);Sourcepub fn try_invert(self) -> Option<Mat2<T>>where
T: Float,
pub fn try_invert(self) -> Option<Mat2<T>>where
T: Float,
Attempts to invert the matrix.
let mat = cvmath::Mat2(1.0, 2.0, 3.0, 4.0);
let value = mat.try_invert();
let expected = Some(cvmath::Mat2(-2.0, 1.0, 1.5, -0.5));
assert_eq!(expected, value);Sourcepub fn inverse(self) -> Mat2<T>where
T: Float,
pub fn inverse(self) -> Mat2<T>where
T: Float,
Computes the inverse matrix.
Returns the zero matrix if the determinant is exactly zero.
let mat = cvmath::Mat2(1.0, 2.0, 3.0, 4.0);
let value = mat.inverse();
let expected = cvmath::Mat2(-2.0, 1.0, 1.5, -0.5);
assert_eq!(expected, value);Sourcepub fn transpose(self) -> Mat2<T>
pub fn transpose(self) -> Mat2<T>
Returns the transposed matrix.
let mat = cvmath::Mat2(1, 2, 3, 4);
assert_eq!(cvmath::Mat2(1, 3, 2, 4), mat.transpose());Sourcepub fn adjugate(self) -> Mat2<T>
pub fn adjugate(self) -> Mat2<T>
Computes the adjugate matrix.
let value = cvmath::Mat2(1, 2, 3, 4).adjugate();
let expected = cvmath::Mat2(4, -2, -3, 1);
assert_eq!(expected, value);Sourcepub fn lerp(self, rhs: Mat2<T>, t: T) -> Mat2<T>where
T: Float,
pub fn lerp(self, rhs: Mat2<T>, t: T) -> Mat2<T>where
T: Float,
Linear interpolation between the matrix elements.
let source = cvmath::Mat2::IDENTITY;
let target = cvmath::Mat2::scaling(cvmath::Vec2(3.0, 5.0));
let value = source.lerp(target, 0.5);
let expected = cvmath::Mat2(2.0, 0.0, 0.0, 3.0);
assert_eq!(expected, value);Sourcepub fn around(self, origin: Vec2<T>) -> Transform2<T>where
T: Float,
pub fn around(self, origin: Vec2<T>) -> Transform2<T>where
T: Float,
Applies the transformation around a given origin.
let rotation = cvmath::Mat2::rotation(cvmath::Angle::deg(90.0));
let mat = rotation.around(cvmath::Vec2(2.0f64, 3.0));
let value = (mat * cvmath::Vec2(3.0, 3.0)).cast::<f32>();
let expected = cvmath::Vec2(2.0f32, 4.0);
assert_eq!(expected, value);Trait Implementations§
Source§impl<T> AddAssign<&Mat2<T>> for Mat2<T>
impl<T> AddAssign<&Mat2<T>> for Mat2<T>
Source§fn add_assign(&mut self, rhs: &Mat2<T>)
fn add_assign(&mut self, rhs: &Mat2<T>)
+= operation. Read moreSource§impl<T: Copy + AddAssign> AddAssign for Mat2<T>
impl<T: Copy + AddAssign> AddAssign for Mat2<T>
Source§fn add_assign(&mut self, rhs: Mat2<T>)
fn add_assign(&mut self, rhs: Mat2<T>)
+= operation. Read moreSource§impl<T: Copy + Add<Output = T> + Mul<Output = T>> Mul<Mat2<T>> for Transform2<T>
impl<T: Copy + Add<Output = T> + Mul<Output = T>> Mul<Mat2<T>> for Transform2<T>
Source§type Output = Transform2<T>
type Output = Transform2<T>
* operator.Source§impl<T: Copy + Add<Output = T> + Mul<Output = T>> Mul<Transform2<T>> for Mat2<T>
impl<T: Copy + Add<Output = T> + Mul<Output = T>> Mul<Transform2<T>> for Mat2<T>
Source§type Output = Transform2<T>
type Output = Transform2<T>
* operator.Source§fn mul(self, rhs: Transform2<T>) -> Transform2<T>
fn mul(self, rhs: Transform2<T>) -> Transform2<T>
* operation. Read moreSource§impl<T> MulAssign<&Mat2<T>> for Mat2<T>
impl<T> MulAssign<&Mat2<T>> for Mat2<T>
Source§fn mul_assign(&mut self, rhs: &Mat2<T>)
fn mul_assign(&mut self, rhs: &Mat2<T>)
*= operation. Read moreSource§impl<T> MulAssign<&T> for Mat2<T>
impl<T> MulAssign<&T> for Mat2<T>
Source§fn mul_assign(&mut self, rhs: &T)
fn mul_assign(&mut self, rhs: &T)
*= operation. Read moreSource§impl<T: Copy + Add<Output = T> + Mul<Output = T>> MulAssign<Mat2<T>> for Transform2<T>
impl<T: Copy + Add<Output = T> + Mul<Output = T>> MulAssign<Mat2<T>> for Transform2<T>
Source§fn mul_assign(&mut self, rhs: Mat2<T>)
fn mul_assign(&mut self, rhs: Mat2<T>)
*= operation. Read moreSource§impl<T: Copy + MulAssign> MulAssign<T> for Mat2<T>
impl<T: Copy + MulAssign> MulAssign<T> for Mat2<T>
Source§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
*= operation. Read moreSource§impl<T: Copy + Add<Output = T> + Mul<Output = T>> MulAssign for Mat2<T>
impl<T: Copy + Add<Output = T> + Mul<Output = T>> MulAssign for Mat2<T>
Source§fn mul_assign(&mut self, rhs: Mat2<T>)
fn mul_assign(&mut self, rhs: Mat2<T>)
*= operation. Read moreSource§impl<T> SubAssign<&Mat2<T>> for Mat2<T>
impl<T> SubAssign<&Mat2<T>> for Mat2<T>
Source§fn sub_assign(&mut self, rhs: &Mat2<T>)
fn sub_assign(&mut self, rhs: &Mat2<T>)
-= operation. Read moreSource§impl<T: Copy + SubAssign> SubAssign for Mat2<T>
impl<T: Copy + SubAssign> SubAssign for Mat2<T>
Source§fn sub_assign(&mut self, rhs: Mat2<T>)
fn sub_assign(&mut self, rhs: Mat2<T>)
-= operation. Read more