#[repr(C)]pub struct Mat3<T> {
pub a11: T,
pub a12: T,
pub a13: T,
pub a21: T,
pub a22: T,
pub a23: T,
pub a31: T,
pub a32: T,
pub a33: T,
}Expand description
3D 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§a13: T§a21: T§a22: T§a23: T§a31: T§a32: T§a33: TImplementations§
Source§impl<T: Float> Mat3<T>
impl<T: Float> Mat3<T>
Sourcepub fn scaling(scale: Vec3<T>) -> Mat3<T>
pub fn scaling(scale: Vec3<T>) -> Mat3<T>
Scaling matrix.
let mat = cvmath::Mat3::scaling(cvmath::Vec3(2.0, 3.0, 4.0));
let value = mat * cvmath::Vec3(4.0, 5.0, 6.0);
let expected = cvmath::Vec3(8.0, 15.0, 24.0);
assert_eq!(expected, value);Sourcepub fn rotation(axis: Vec3<T>, angle: Angle<T>) -> Mat3<T>
pub fn rotation(axis: Vec3<T>, angle: Angle<T>) -> Mat3<T>
Rotation matrix around an axis.
let mat = cvmath::Mat3::rotation(cvmath::Vec3::Z, cvmath::deg(90.0));
let value = (mat * cvmath::Vec3(1.0f64, 1.0, 1.0)).cast::<f32>();
let expected = cvmath::Vec3(-1.0f32, 1.0, 1.0);
assert_eq!(expected, value);Sourcepub fn rotation_between(from: Vec3<T>, to: Vec3<T>) -> Mat3<T>
pub fn rotation_between(from: Vec3<T>, to: Vec3<T>) -> Mat3<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.
This produces the minimal rotation between the two directions.
The behavior is undefined if from and to are opposite (180° apart), or if either vector is zero-length,
since the rotation axis is not uniquely defined.
This is useful for constructing an orientation matrix that points one direction vector toward another.
let from = cvmath::Vec3(1.0, 0.0, 0.0);
let to = cvmath::Vec3(0.0, 1.0, 0.0);
let mat = cvmath::Mat3::rotation_between(from, to);
let value = (mat * from).cast::<f32>();
let expected = to.cast::<f32>();
assert_eq!(expected, value);Sourcepub fn outer_product(column: Vec3<T>, row: Vec3<T>) -> Mat3<T>
pub fn outer_product(column: Vec3<T>, row: Vec3<T>) -> Mat3<T>
Outer product of two vectors.
let column = cvmath::Vec3(1.0, 2.0, 3.0);
let row = cvmath::Vec3(4.0, 5.0, 6.0);
let mat = cvmath::Mat3::outer_product(column, row);
let expected = cvmath::Mat3(4.0, 5.0, 6.0, 8.0, 10.0, 12.0, 12.0, 15.0, 18.0);
assert_eq!(expected, mat);Sourcepub fn projection(normal: Vec3<T>) -> Mat3<T>
pub fn projection(normal: Vec3<T>) -> Mat3<T>
Projection matrix.
Projects onto the plane defined by the given normal, returning the zero matrix if the normal is zero.
let mat = cvmath::Mat3::projection(cvmath::Vec3(0.0f64, 0.0, 2.0));
let value = mat * cvmath::Vec3(2.0, 3.0, 4.0);
let expected = cvmath::Vec3(2.0, 3.0, 0.0);
assert_eq!(expected, value);Sourcepub fn reflection(normal: Vec3<T>) -> Mat3<T>
pub fn reflection(normal: Vec3<T>) -> Mat3<T>
Reflection matrix.
Reflects across the plane defined by the given normal, returning a point reflection around the origin if the normal is zero.
let mat = cvmath::Mat3::reflection(cvmath::Vec3(0.0f64, 0.0, 2.0));
let value = mat * cvmath::Vec3(2.0, 3.0, 4.0);
let expected = cvmath::Vec3(2.0, 3.0, -4.0);
assert_eq!(expected, value);Source§impl<T> Mat3<T>
impl<T> Mat3<T>
Sourcepub fn mat2(self) -> Mat2<T>
pub fn mat2(self) -> Mat2<T>
Converts to a Mat2 matrix.
let value = cvmath::Mat3(1, 2, 3, 4, 5, 6, 7, 8, 9).mat2();
let expected = cvmath::Mat2(1, 2, 4, 5);
assert_eq!(expected, value);Sourcepub fn transform3(self) -> Transform3<T>where
T: Zero,
pub fn transform3(self) -> Transform3<T>where
T: Zero,
Converts to a Transform3 matrix.
let mat = cvmath::Mat3(1, 2, 3, 4, 5, 6, 7, 8, 9).transform3();
let value = mat.into_row_major();
let expected = [[1, 2, 3, 0], [4, 5, 6, 0], [7, 8, 9, 0]];
assert_eq!(expected, value);Sourcepub fn translate(self, trans: Vec3<T>) -> Transform3<T>
pub fn translate(self, trans: Vec3<T>) -> Transform3<T>
Adds a translation to the matrix.
let mat = cvmath::Mat3::IDENTITY.translate(cvmath::Vec3(5, 6, 7));
let value = mat.into_row_major();
let expected = [[1, 0, 0, 5], [0, 1, 0, 6], [0, 0, 1, 7]];
assert_eq!(expected, value);Source§impl<T> Mat3<T>
impl<T> Mat3<T>
Sourcepub fn from_row_major(mat: [[T; 3]; 3]) -> Mat3<T>
pub fn from_row_major(mat: [[T; 3]; 3]) -> Mat3<T>
Imports the matrix from a row-major layout.
let mat = cvmath::Mat3::from_row_major([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
let expected = cvmath::Mat3(1, 2, 3, 4, 5, 6, 7, 8, 9);
assert_eq!(expected, mat);Sourcepub fn from_column_major(mat: [[T; 3]; 3]) -> Mat3<T>
pub fn from_column_major(mat: [[T; 3]; 3]) -> Mat3<T>
Imports the matrix from a column-major layout.
let mat = cvmath::Mat3::from_column_major([[1, 4, 7], [2, 5, 8], [3, 6, 9]]);
let expected = cvmath::Mat3(1, 2, 3, 4, 5, 6, 7, 8, 9);
assert_eq!(expected, mat);Sourcepub fn into_row_major(self) -> [[T; 3]; 3]
pub fn into_row_major(self) -> [[T; 3]; 3]
Exports the matrix as a row-major array.
let value = cvmath::Mat3(1, 2, 3, 4, 5, 6, 7, 8, 9).into_row_major();
let expected = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
assert_eq!(expected, value);Sourcepub fn into_column_major(self) -> [[T; 3]; 3]
pub fn into_column_major(self) -> [[T; 3]; 3]
Exports the matrix as a column-major array.
let value = cvmath::Mat3(1, 2, 3, 4, 5, 6, 7, 8, 9).into_column_major();
let expected = [[1, 4, 7], [2, 5, 8], [3, 6, 9]];
assert_eq!(expected, value);Source§impl<T> Mat3<T>
impl<T> Mat3<T>
Sourcepub fn compose(x: Vec3<T>, y: Vec3<T>, z: Vec3<T>) -> Mat3<T>
pub fn compose(x: Vec3<T>, y: Vec3<T>, z: Vec3<T>) -> Mat3<T>
Composes the matrix from basis vectors.
let mat = cvmath::Mat3::compose(cvmath::Vec3(1, 2, 3), cvmath::Vec3(4, 5, 6), cvmath::Vec3(7, 8, 9));
let value = mat.into_row_major();
let expected = [[1, 4, 7], [2, 5, 8], [3, 6, 9]];
assert_eq!(expected, value);Sourcepub fn x(self) -> Vec3<T>
pub fn x(self) -> Vec3<T>
Gets the transformed X basis vector.
let value = cvmath::Mat3(1, 2, 3, 4, 5, 6, 7, 8, 9).x();
let expected = cvmath::Vec3(1, 4, 7);
assert_eq!(expected, value);Source§impl<T: Scalar> Mat3<T>
impl<T: Scalar> Mat3<T>
Sourcepub fn det(self) -> T
pub fn det(self) -> T
Computes the determinant.
let value = cvmath::Mat3::scaling(cvmath::Vec3(2.0, 3.0, 4.0)).det();
assert_eq!(24.0, value);Sourcepub fn trace(self) -> T
pub fn trace(self) -> T
Computes the trace.
let value = cvmath::Mat3::scaling(cvmath::Vec3(2.0, 3.0, 4.0)).trace();
assert_eq!(9.0, 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::Mat3::scaling(cvmath::Vec3(2.0, 3.0, 4.0)).flat_norm_sqr();
assert_eq!(29.0, value);Sourcepub fn try_invert(self) -> Option<Mat3<T>>where
T: Float,
pub fn try_invert(self) -> Option<Mat3<T>>where
T: Float,
Attempts to invert the matrix.
let value = cvmath::Mat3::scaling(cvmath::Vec3(2.0, 4.0, 8.0)).try_invert();
let expected = Some(cvmath::Mat3::scaling(cvmath::Vec3(0.5, 0.25, 0.125)));
assert_eq!(expected, value);Sourcepub fn inverse(self) -> Mat3<T>where
T: Float,
pub fn inverse(self) -> Mat3<T>where
T: Float,
Computes the inverse matrix.
Returns the zero matrix if the determinant is exactly zero.
let value = cvmath::Mat3::scaling(cvmath::Vec3(2.0, 4.0, 8.0)).inverse();
let expected = cvmath::Mat3::scaling(cvmath::Vec3(0.5, 0.25, 0.125));
assert_eq!(expected, value);Sourcepub fn transpose(self) -> Mat3<T>
pub fn transpose(self) -> Mat3<T>
Returns the transposed matrix.
let mat = cvmath::Mat3(1, 2, 3, 4, 5, 6, 7, 8, 9);
let value = mat.transpose();
let expected = cvmath::Mat3(1, 4, 7, 2, 5, 8, 3, 6, 9);
assert_eq!(expected, value);Sourcepub fn adjugate(self) -> Mat3<T>
pub fn adjugate(self) -> Mat3<T>
Computes the adjugate matrix.
let value = cvmath::Mat3::scaling(cvmath::Vec3(2.0, 3.0, 4.0)).adjugate();
let expected = cvmath::Mat3::scaling(cvmath::Vec3(12.0, 8.0, 6.0));
assert_eq!(expected, value);Sourcepub fn around(self, origin: Vec3<T>) -> Transform3<T>where
T: Float,
pub fn around(self, origin: Vec3<T>) -> Transform3<T>where
T: Float,
Applies the transformation around a given origin.
let rotation = cvmath::Mat3::rotation(cvmath::Vec3::Z, cvmath::deg(90.0));
let mat = rotation.around(cvmath::Vec3(2.0f64, 3.0, 4.0));
let value = (mat * cvmath::Vec3(3.0, 3.0, 4.0)).cast::<f32>();
let expected = cvmath::Vec3(2.0f32, 4.0, 4.0);
assert_eq!(expected, value);Source§impl<T: Float> Mat3<T>
impl<T: Float> Mat3<T>
Sourcepub fn powi(self, exp: i32) -> Mat3<T>
pub fn powi(self, exp: i32) -> Mat3<T>
Raises the matrix to an integer power.
let axis = cvmath::Vec3(1.0, 2.0, 3.0).norm();
let mat = cvmath::Mat3::rotation(axis, cvmath::deg(60.0));
let value = mat.powi(-2).cast::<f32>();
let expected = cvmath::Mat3::rotation(axis, cvmath::deg(60.0) * -2.0).cast::<f32>();
assert_eq!(expected, value);Trait Implementations§
Source§impl<T: Copy + AddAssign> AddAssign for Mat3<T>
impl<T: Copy + AddAssign> AddAssign for Mat3<T>
Source§fn add_assign(&mut self, rhs: Mat3<T>)
fn add_assign(&mut self, rhs: Mat3<T>)
+= operation. Read moreSource§impl<T> AddAssign<&Mat3<T>> for Mat3<T>
impl<T> AddAssign<&Mat3<T>> for Mat3<T>
Source§fn add_assign(&mut self, rhs: &Mat3<T>)
fn add_assign(&mut self, rhs: &Mat3<T>)
+= operation. Read moreimpl<T: Copy> Copy for Mat3<T>
Source§impl<T: Zero + One> From<Transform2<T>> for Mat3<T>
impl<T: Zero + One> From<Transform2<T>> for Mat3<T>
Source§fn from(mat: Transform2<T>) -> Mat3<T>
fn from(mat: Transform2<T>) -> Mat3<T>
Source§impl<T: Copy + Add<Output = T> + Mul<Output = T>> Mul<Mat3<T>> for Transform3<T>
impl<T: Copy + Add<Output = T> + Mul<Output = T>> Mul<Mat3<T>> for Transform3<T>
Source§type Output = Transform3<T>
type Output = Transform3<T>
* operator.Source§impl<T: Copy + Add<Output = T> + Mul<Output = T>> MulAssign for Mat3<T>
impl<T: Copy + Add<Output = T> + Mul<Output = T>> MulAssign for Mat3<T>
Source§fn mul_assign(&mut self, rhs: Mat3<T>)
fn mul_assign(&mut self, rhs: Mat3<T>)
*= operation. Read moreSource§impl<T> MulAssign<&Mat3<T>> for Mat3<T>
impl<T> MulAssign<&Mat3<T>> for Mat3<T>
Source§fn mul_assign(&mut self, rhs: &Mat3<T>)
fn mul_assign(&mut self, rhs: &Mat3<T>)
*= operation. Read moreSource§impl<T> MulAssign<&T> for Mat3<T>
impl<T> MulAssign<&T> for Mat3<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<Mat3<T>> for Transform3<T>
impl<T: Copy + Add<Output = T> + Mul<Output = T>> MulAssign<Mat3<T>> for Transform3<T>
Source§fn mul_assign(&mut self, rhs: Mat3<T>)
fn mul_assign(&mut self, rhs: Mat3<T>)
*= operation. Read moreSource§impl<T: Copy + MulAssign> MulAssign<T> for Mat3<T>
impl<T: Copy + MulAssign> MulAssign<T> for Mat3<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<Transform2<T>> for Mat3<T>
impl<T: Copy + Add<Output = T> + Mul<Output = T>> MulAssign<Transform2<T>> for Mat3<T>
Source§fn mul_assign(&mut self, rhs: Transform2<T>)
fn mul_assign(&mut self, rhs: Transform2<T>)
*= operation. Read more