#[repr(C)]pub struct Transform2<T> {
pub a11: T,
pub a12: T,
pub a13: T,
pub a21: T,
pub a22: T,
pub a23: T,
}Expand description
2D affine transformation matrix.
Each field aij represents the i-th row and j-th column of the matrix.
The third row is implied to be [0, 0, 1] and is omitted.
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: TImplementations§
Source§impl<T> Transform2<T>
impl<T> Transform2<T>
Sourcepub const fn new(
a11: T,
a12: T,
a13: T,
a21: T,
a22: T,
a23: T,
) -> Transform2<T>
pub const fn new( a11: T, a12: T, a13: T, a21: T, a22: T, a23: T, ) -> Transform2<T>
Constructs a new matrix from components.
Source§impl<T: Zero> Transform2<T>
impl<T: Zero> Transform2<T>
Sourcepub const ZERO: Transform2<T>
pub const ZERO: Transform2<T>
Zero matrix.
Source§impl<T: Zero + One> Transform2<T>
impl<T: Zero + One> Transform2<T>
Sourcepub const IDENTITY: Transform2<T>
pub const IDENTITY: Transform2<T>
Identity matrix.
Source§impl<T: Float> Transform2<T>
impl<T: Float> Transform2<T>
Sourcepub fn translation(trans: Vec2<T>) -> Transform2<T>
pub fn translation(trans: Vec2<T>) -> Transform2<T>
Translation matrix.
let mat = cvmath::Transform2::translation(cvmath::Vec2(5.0, 6.0));
let value = mat * cvmath::Vec2(1.0, 2.0);
let expected = cvmath::Vec2(6.0, 8.0);
assert_eq!(expected, value);Sourcepub fn scaling(scale: Vec2<T>) -> Transform2<T>
pub fn scaling(scale: Vec2<T>) -> Transform2<T>
Scaling matrix.
Scales around the origin.
let mat = cvmath::Transform2::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>) -> Transform2<T>
pub fn rotation(angle: Angle<T>) -> Transform2<T>
Rotation matrix.
Rotates around the origin.
let mat = cvmath::Transform2::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>) -> Transform2<T>
pub fn rotation_between(from: Vec2<T>, to: Vec2<T>) -> Transform2<T>
Rotation matrix between two vectors. See Mat2::rotation_between.
let from = cvmath::Vec2(1.0, 1.0).norm();
let to = cvmath::Vec2(-1.0, 1.0).norm();
let mat = cvmath::Transform2::rotation_between(from, to);
let value = (mat * from).cast::<f32>();
let expected = to.cast::<f32>();
assert_eq!(expected, value);Sourcepub fn skewing(skew: Vec2<T>) -> Transform2<T>
pub fn skewing(skew: Vec2<T>) -> Transform2<T>
Skewing matrix.
let mat = cvmath::Transform2::skewing(cvmath::Vec2(2.0, 3.0));
let value = mat * cvmath::Vec2(4.0, 5.0);
let expected = cvmath::Vec2(14.0, 17.0);
assert_eq!(expected, value);Sourcepub fn reflection(line: Vec2<T>) -> Transform2<T>
pub fn reflection(line: Vec2<T>) -> Transform2<T>
Reflection matrix.
Reflects around the given axis. If axis is the zero vector, returns a point reflection around the origin.
let mat = cvmath::Transform2::reflection(cvmath::Vec2::<f64>::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(line: Vec2<T>) -> Transform2<T>
pub fn projection(line: Vec2<T>) -> Transform2<T>
Projection matrix.
Projects onto the given axis. If axis is the zero vector, returns the zero matrix.
let mat = cvmath::Transform2::projection(cvmath::Vec2::<f64>::X);
let value = mat * cvmath::Vec2(2.0, 3.0);
let expected = cvmath::Vec2(2.0, 0.0);
assert_eq!(expected, value);Sourcepub fn fit(source: Bounds2<T>, target: Bounds2<T>) -> Transform2<T>
pub fn fit(source: Bounds2<T>, target: Bounds2<T>) -> Transform2<T>
Fit matrix.
Fits coordinates from a source rect into a target rect.
let source = cvmath::Bounds2::c(10.0, 20.0, 30.0, 40.0);
let target = cvmath::Bounds2::c(100.0, 200.0, 200.0, 260.0);
let mat = cvmath::Transform2::fit(source, target);
let value = mat * cvmath::Point2(10.0, 20.0);
let expected = cvmath::Point2(100.0, 200.0);
assert_eq!(expected, value);Sourcepub fn ortho(rect: Bounds2<T>) -> Transform2<T>
pub fn ortho(rect: Bounds2<T>) -> Transform2<T>
Orthographic matrix.
Fits the coordinates from a rectangle to x = [-1, 1] and y = [1, -1].
let rect = cvmath::Bounds2::c(10.0, 20.0, 30.0, 40.0);
let mat = cvmath::Transform2::ortho(rect);
let value = mat * cvmath::Point2(10.0, 20.0);
let expected = cvmath::Point2(-1.0, 1.0);
assert_eq!(expected, value);Sourcepub fn screen(width: T, height: T) -> Transform2<T>
pub fn screen(width: T, height: T) -> Transform2<T>
Maps NDC coordinates to screen space.
let mat = cvmath::Transform2::screen(640.0, 480.0);
let value = mat * cvmath::Vec2(-1.0, -1.0);
let expected = cvmath::Vec2(0.0, 480.0);
assert_eq!(expected, value);Source§impl<T: Zero + One> Transform2<T>
impl<T: Zero + One> Transform2<T>
Source§impl<T> Transform2<T>
impl<T> Transform2<T>
Sourcepub fn from_row_major(mat: [[T; 3]; 2]) -> Transform2<T>
pub fn from_row_major(mat: [[T; 3]; 2]) -> Transform2<T>
Imports the matrix from a row-major layout.
let mat = cvmath::Transform2::from_row_major([[1, 2, 3], [4, 5, 6]]);
let expected = cvmath::Transform2(1, 2, 3, 4, 5, 6);
assert_eq!(expected, mat);Sourcepub fn from_column_major(mat: [[T; 2]; 3]) -> Transform2<T>
pub fn from_column_major(mat: [[T; 2]; 3]) -> Transform2<T>
Imports the matrix from a column-major layout.
let mat = cvmath::Transform2::from_column_major([[1, 4], [2, 5], [3, 6]]);
let expected = cvmath::Transform2(1, 2, 3, 4, 5, 6);
assert_eq!(expected, mat);Sourcepub fn into_row_major(self) -> [[T; 3]; 2]
pub fn into_row_major(self) -> [[T; 3]; 2]
Exports the matrix as a row-major array.
let value = cvmath::Transform2(1, 2, 3, 4, 5, 6).into_row_major();
let expected = [[1, 2, 3], [4, 5, 6]];
assert_eq!(expected, value);Sourcepub fn into_column_major(self) -> [[T; 2]; 3]
pub fn into_column_major(self) -> [[T; 2]; 3]
Exports the matrix as a column-major array.
let value = cvmath::Transform2(1, 2, 3, 4, 5, 6).into_column_major();
let expected = [[1, 4], [2, 5], [3, 6]];
assert_eq!(expected, value);Source§impl<T> Transform2<T>
impl<T> Transform2<T>
Sourcepub fn compose(x: Vec2<T>, y: Vec2<T>, t: Vec2<T>) -> Transform2<T>
pub fn compose(x: Vec2<T>, y: Vec2<T>, t: Vec2<T>) -> Transform2<T>
Composes the matrix from basis vectors.
let mat = cvmath::Transform2::compose(cvmath::Vec2(1, 2), cvmath::Vec2(3, 4), cvmath::Vec2(5, 6));
let value = mat.into_row_major();
let expected = [[1, 3, 5], [2, 4, 6]];
assert_eq!(expected, value);Sourcepub fn x(self) -> Vec2<T>
pub fn x(self) -> Vec2<T>
Gets the transformed X basis vector.
let value = cvmath::Transform2(1, 2, 3, 4, 5, 6).x();
let expected = cvmath::Vec2(1, 4);
assert_eq!(expected, value);Sourcepub fn y(self) -> Vec2<T>
pub fn y(self) -> Vec2<T>
Gets the transformed Y basis vector.
let value = cvmath::Transform2(1, 2, 3, 4, 5, 6).y();
let expected = cvmath::Vec2(2, 5);
assert_eq!(expected, value);Source§impl<T: Scalar> Transform2<T>
impl<T: Scalar> Transform2<T>
Sourcepub fn det(self) -> T
pub fn det(self) -> T
Computes the determinant.
let value = cvmath::Transform2(1, 2, 3, 4, 5, 6).det();
assert_eq!(-3, value);Sourcepub fn trace(self) -> T
pub fn trace(self) -> T
Computes the trace.
let value = cvmath::Transform2(1, 2, 3, 4, 5, 6).trace();
assert_eq!(7, 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::Transform2(1, 2, 3, 4, 5, 6).flat_norm_sqr();
assert_eq!(91, value);Sourcepub fn try_invert(self) -> Option<Transform2<T>>where
T: Float,
pub fn try_invert(self) -> Option<Transform2<T>>where
T: Float,
Attempts to invert the transform.
let mat = cvmath::Transform2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);
let point = cvmath::Vec2(7.0f64, 8.0);
let projected = mat * point;
let value = (mat.try_invert().unwrap() * projected).cast::<f32>();
let expected = point.cast::<f32>();
assert_eq!(expected, value);Sourcepub fn inverse(self) -> Transform2<T>where
T: Float,
pub fn inverse(self) -> Transform2<T>where
T: Float,
Computes the inverse matrix.
Returns the zero matrix if the determinant is exactly zero.
let mat = cvmath::Transform2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);
let point = cvmath::Vec2(7.0f64, 8.0);
let projected = mat * point;
let value = (mat.inverse() * projected).cast::<f32>();
let expected = point.cast::<f32>();
assert_eq!(expected, value);Sourcepub fn lerp(self, other: Transform2<T>, t: T) -> Transform2<T>where
T: Float,
pub fn lerp(self, other: Transform2<T>, t: T) -> Transform2<T>where
T: Float,
Linear interpolation between the matrix elements.
let source = cvmath::Transform2::IDENTITY;
let target = cvmath::Transform2::translation(cvmath::Vec2(8.0, 10.0));
let value = source.lerp(target, 0.5);
let expected = cvmath::Transform2(1.0, 0.0, 4.0, 0.0, 1.0, 5.0);
assert_eq!(expected, value);Sourcepub fn solve(self) -> Option<Vec2<T>>
pub fn solve(self) -> Option<Vec2<T>>
Solves a linear system of equations represented by the matrix.
Interprets the affine transform rows as the system:
- a11 x + a12 y + a13 = 0
- a21 x + a22 y + a23 = 0
Equivalently, this finds the vector v such that self * v == Vec2::ZERO.
Returns None if the determinant is zero (no unique solution).
let mat = cvmath::Transform2(
1.0, 1.0, -5.0,
1.0, -1.0, -1.0,
);
let value = mat.solve();
let expected = Some(cvmath::Vec2(3.0, 2.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::Transform2::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: Clone> Clone for Transform2<T>
impl<T: Clone> Clone for Transform2<T>
Source§fn clone(&self) -> Transform2<T>
fn clone(&self) -> Transform2<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Debug> Debug for Transform2<T>
impl<T: Debug> Debug for Transform2<T>
Source§impl<T: Default> Default for Transform2<T>
impl<T: Default> Default for Transform2<T>
Source§fn default() -> Transform2<T>
fn default() -> Transform2<T>
Source§impl<T: Display> Display for Transform2<T>
impl<T: Display> Display for Transform2<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> Mul<&T> for &Transform2<T>
impl<T> Mul<&T> for &Transform2<T>
Source§type Output = Transform2<T>
type Output = Transform2<T>
* operator.Source§impl<T> Mul<&T> for Transform2<T>
impl<T> Mul<&T> for Transform2<T>
Source§type Output = Transform2<T>
type Output = Transform2<T>
* operator.Source§impl<T> Mul<&Transform2<T>> for &Transform2<T>
impl<T> Mul<&Transform2<T>> for &Transform2<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> Mul<&Transform2<T>> for Transform2<T>
impl<T> Mul<&Transform2<T>> for Transform2<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> Mul<&Vec2<T>> for &Transform2<T>
impl<T> Mul<&Vec2<T>> for &Transform2<T>
Source§impl<T> Mul<&Vec2<T>> for Transform2<T>
impl<T> Mul<&Vec2<T>> for Transform2<T>
Source§impl<T> Mul<&Vec3<T>> for &Transform2<T>
impl<T> Mul<&Vec3<T>> for &Transform2<T>
Source§impl<T> Mul<&Vec3<T>> for Transform2<T>
impl<T> Mul<&Vec3<T>> for Transform2<T>
Source§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: Float> Mul<Ray2<T>> for Transform2<T>
impl<T: Float> Mul<Ray2<T>> for Transform2<T>
Source§impl<T> Mul<T> for &Transform2<T>
impl<T> Mul<T> for &Transform2<T>
Source§type Output = Transform2<T>
type Output = Transform2<T>
* operator.Source§fn mul(self, rhs: T) -> Transform2<T>
fn mul(self, rhs: T) -> Transform2<T>
* operation. Read moreSource§impl<T> Mul<Transform2<T>> for &Transform2<T>
impl<T> Mul<Transform2<T>> for &Transform2<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: 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> Mul<Vec2<T>> for &Transform2<T>
impl<T> Mul<Vec2<T>> for &Transform2<T>
Source§impl<T> Mul<Vec3<T>> for &Transform2<T>
impl<T> Mul<Vec3<T>> for &Transform2<T>
Source§impl<T: Copy + Add<Output = T> + Mul<Output = T>> Mul for Transform2<T>
impl<T: Copy + Add<Output = T> + Mul<Output = T>> Mul for Transform2<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<&T> for Transform2<T>
impl<T> MulAssign<&T> for Transform2<T>
Source§fn mul_assign(&mut self, rhs: &T)
fn mul_assign(&mut self, rhs: &T)
*= operation. Read moreSource§impl<T> MulAssign<&Transform2<T>> for Transform2<T>
impl<T> MulAssign<&Transform2<T>> for Transform2<T>
Source§fn mul_assign(&mut self, rhs: &Transform2<T>)
fn mul_assign(&mut self, rhs: &Transform2<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 + 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 moreSource§impl<T: Copy + Add<Output = T> + Mul<Output = T>> MulAssign for Transform2<T>
impl<T: Copy + Add<Output = T> + Mul<Output = T>> MulAssign for Transform2<T>
Source§fn mul_assign(&mut self, rhs: Transform2<T>)
fn mul_assign(&mut self, rhs: Transform2<T>)
*= operation. Read more