packages

spatial::Mat3

On this page

Mat3Source

pub struct Mat3<T = f32>

A 3x3 matrix of three column vectors: a rotation and scale in 3D, or a 2D transform in homogeneous coordinates (transform_point2). Mat3 alone is Mat3<f32>. #[repr(C)]: x_axis, y_axis, z_axis, nine Ts column by column. A WGSL mat3x3<f32> pads each column to 16 bytes: upload Mat4::from_mat3(m) or three Vec3As.

nabi
use spatial::{Mat3, Vec3};

let m = Mat3::from_rotation_z(f32::PI / 2.0);
assert((m * Vec3::X).approx_eq(Vec3::Y, 1e-6));
  • pub x_axis: Vec3<T>§

    Source
  • pub y_axis: Vec3<T>§

    Source
  • pub z_axis: Vec3<T>§

    Source
  • pub const fn from_cols(x_axis: Vec3<T>, y_axis: Vec3<T>, z_axis: Vec3<T>) -> Mat3<T>§

    Source

    The matrix of its columns.

    nabi
    use spatial::{Mat3, Vec3};
    
    assert(Mat3::from_cols(Vec3::X, Vec3::Y, Vec3::Z) == Mat3::IDENTITY);
    
  • pub fn from_cols_array(a: [T; 9]) -> Mat3<T>§

    Source

    From nine values, column by column.

    nabi
    let m = spatial::DMat3::from_cols_array([1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]);
    assert(m == spatial::DMat3::IDENTITY);
    
  • pub fn to_cols_array(self) -> [T; 9]§

    Source

    The nine values column by column.

    nabi
    assert(spatial::Mat3::IDENTITY.to_cols_array()[4] == 1.0);
    
  • pub fn identity() -> Mat3<T>§

    Source

    The identity.

    nabi
    assert(spatial::Mat3::<f32>::identity() == spatial::Mat3::IDENTITY);
    
  • pub fn zero() -> Mat3<T>§

    Source

    All zero.

    nabi
    assert(spatial::Mat3::<f32>::zero().determinant() == 0.0);
    
  • pub fn from_diagonal(d: Vec3<T>) -> Mat3<T>§

    Source

    The diagonal matrix of d, a 3D scale.

    nabi
    use spatial::{Mat3, Vec3};
    
    assert(Mat3::from_diagonal(Vec3::new(1.0, 2.0, 3.0)) * Vec3::ONE == Vec3::new(1.0, 2.0, 3.0));
    
  • pub fn from_quat(q: Quat<T>) -> Mat3<T>§

    Source

    The rotation of a unit quaternion.

    nabi
    use spatial::{Mat3, Quat, Vec3};
    
    let q = Quat::from_rotation_y(0.5);
    assert((Mat3::from_quat(q) * Vec3::X).approx_eq(q * Vec3::X, 1e-6));
    
  • pub fn from_axis_angle(axis: Vec3<T>, angle: T) -> Mat3<T>§

    Source

    The rotation by angle radians about the unit vector axis.

    nabi
    use spatial::{Mat3, Vec3};
    
    assert((Mat3::from_axis_angle(Vec3::X, f32::PI / 2.0) * Vec3::Y).approx_eq(Vec3::Z, 1e-6));
    
  • pub fn from_rotation_x(angle: T) -> Mat3<T>§

    Source

    The rotation by angle radians about +x.

    nabi
    use spatial::{Mat3, Vec3};
    
    assert((Mat3::from_rotation_x(f32::PI / 2.0) * Vec3::Y).approx_eq(Vec3::Z, 1e-6));
    
  • pub fn from_rotation_y(angle: T) -> Mat3<T>§

    Source

    The rotation by angle radians about +y.

    nabi
    use spatial::{Mat3, Vec3};
    
    assert((Mat3::from_rotation_y(f32::PI / 2.0) * Vec3::Z).approx_eq(Vec3::X, 1e-6));
    
  • pub fn from_rotation_z(angle: T) -> Mat3<T>§

    Source

    The rotation by angle radians about +z.

    nabi
    use spatial::{Mat3, Vec3};
    
    assert((Mat3::from_rotation_z(f32::PI / 2.0) * Vec3::X).approx_eq(Vec3::Y, 1e-6));
    
  • pub fn from_euler(order: EulerOrder, a: T, b: T, c: T) -> Mat3<T>§

    Source

    The rotation of Euler angles in radians: see EulerOrder.

    nabi
    use spatial::{EulerOrder, Mat3};
    
    let m = Mat3::from_euler(EulerOrder::ZYX, 0.0, 0.0, 0.5);
    assert(m.approx_eq(Mat3::from_rotation_x(0.5), 1e-6));
    
  • pub fn to_euler(self, order: EulerOrder) -> (T, T, T)§

    Source

    The Euler angles of a rotation matrix: see Quat::to_euler.

    nabi
    use spatial::{EulerOrder, Mat3};
    
    let (a, b, c) = Mat3::<f64>::from_rotation_y(0.25).to_euler(EulerOrder::YXZ);
    assert((a - 0.25).abs() < 1e-12 && b.abs() < 1e-12 && c.abs() < 1e-12);
    
  • pub fn from_mat4(m: Mat4<T>) -> Mat3<T>§

    Source

    The upper-left 3x3 of m: its rotation and scale.

    nabi
    use spatial::{Mat3, Mat4, Vec3};
    
    assert(Mat3::from_mat4(Mat4::from_translation(Vec3::ONE)) == Mat3::IDENTITY);
    
  • pub fn from_mat2(m: Mat2<T>) -> Mat3<T>§

    Source

    m as the upper-left 2x2, 1 in the corner: a 2D linear map in homogeneous coordinates.

    nabi
    use spatial::{Mat2, Mat3};
    
    assert(Mat3::from_mat2(Mat2::IDENTITY) == Mat3::IDENTITY);
    
  • pub fn from_translation(t: Vec2<T>) -> Mat3<T>§

    Source

    The 2D translation by t, in homogeneous coordinates.

    nabi
    use spatial::{Mat3, Vec2};
    
    assert(Mat3::from_translation(Vec2::new(1.0, 2.0)).transform_point2(Vec2::ZERO) == Vec2::new(1.0, 2.0));
    
  • pub fn from_scale_angle_translation(scale: Vec2<T>, angle: T, t: Vec2<T>) -> Mat3<T>§

    Source

    The 2D transform that scales by scale, rotates by angle radians, then moves by t.

    nabi
    use spatial::{Mat3, Vec2};
    
    let m = Mat3::from_scale_angle_translation(Vec2::splat(2.0), f32::PI / 2.0, Vec2::new(1.0, 0.0));
    assert(m.transform_point2(Vec2::X).approx_eq(Vec2::new(1.0, 2.0), 1e-6));
    
  • pub fn transform_point2(self, p: Vec2<T>) -> Vec2<T>§

    Source

    The 2D point p transformed, translation included (z = 1, no perspective divide).

    nabi
    use spatial::{Mat3, Vec2};
    
    assert(Mat3::from_translation(Vec2::X).transform_point2(Vec2::Y) == Vec2::ONE);
    
  • pub fn transform_vector2(self, v: Vec2<T>) -> Vec2<T>§

    Source

    The 2D direction v transformed, translation left out (z = 0).

    nabi
    use spatial::{Mat3, Vec2};
    
    assert(Mat3::from_translation(Vec2::X).transform_vector2(Vec2::Y) == Vec2::Y);
    
  • pub fn col(self, i: usize) -> Vec3<T>§

    Source

    Column i (0, 1 or 2).

    nabi
    assert(spatial::Mat3::IDENTITY.col(2) == spatial::Vec3::Z);
    
  • pub fn row(self, i: usize) -> Vec3<T>§

    Source

    Row i (0, 1 or 2).

    nabi
    assert(spatial::Mat3::IDENTITY.row(0) == spatial::Vec3::X);
    
  • pub fn transpose(self) -> Mat3<T>§

    Source

    The transpose; the inverse of a rotation.

    nabi
    use spatial::DMat3;
    
    let r = DMat3::from_rotation_x(0.3);
    assert((r * r.transpose()).approx_eq(DMat3::IDENTITY, 1e-12));
    
  • pub fn determinant(self) -> T§

    Source

    The determinant, x_axis . (y_axis x z_axis).

    nabi
    use spatial::{Mat3, Vec3};
    
    assert(Mat3::from_diagonal(Vec3::new(1.0, 2.0, 3.0)).determinant() == 6.0);
    
  • pub fn inverse(self) -> Mat3<T>§

    Source

    The inverse. A singular matrix gives infinite or NaN entries; try_inverse gives none for it instead.

    nabi
    use spatial::{DMat3, DVec3};
    
    let m = DMat3::from_cols(DVec3::new(2.0, 0.0, 1.0), DVec3::new(0.0, 1.0, 0.0), DVec3::new(1.0, 0.0, 3.0));
    assert((m.inverse() * m).approx_eq(DMat3::IDENTITY, 1e-12));
    
  • pub fn try_inverse(self) -> Mat3<T> | none§

    Source

    The inverse, or none when the determinant is zero or the result is not finite.

    nabi
    assert(spatial::Mat3::<f32>::zero().try_inverse() == none);
    
  • pub fn transform_vector(self, v: Vec3<T>) -> Vec3<T>§

    Source

    The vector v transformed, the same as self * v.

    nabi
    use spatial::{Mat3, Vec3};
    
    assert(Mat3::IDENTITY.transform_vector(Vec3::ONE) == Vec3::ONE);
    
  • pub fn approx_eq(self, o: Mat3<T>, eps: T) -> bool§

    Source

    Whether every entry is within eps of o's.

    nabi
    use spatial::Mat3;
    
    assert(Mat3::IDENTITY.approx_eq(Mat3::from_rotation_z(1e-7), 1e-6));
    
  • pub fn is_finite(self) -> bool§

    Source

    Whether no entry is NaN or infinite.

    nabi
    assert(spatial::Mat3::IDENTITY.is_finite());
    
Trait implementations 7
  • impl<T: Real> Mul for Mat3<T>
  • impl<T: Real> Mul<Vec3<T>> for Mat3<T>
  • impl<T: Real> Mul<T> for Mat3<T>
  • impl<T: Real> Add for Mat3<T>
  • impl<T: Real> Sub for Mat3<T>
  • impl<T: Real> MulAssign for Mat3<T>
  • impl<T: Real> Display for Mat3<T>