Struct vek::mat::repr_simd::row_major::mat3::Mat3

source ·
#[repr(C)]
pub struct Mat3<T> { pub rows: CVec3<Vec3<T>>, }
Expand description

3x3 matrix.

Fields§

§rows: CVec3<Vec3<T>>

Implementations§

The identity matrix, which is also the default value for square matrices.

assert_eq!(Mat4::<f32>::default(), Mat4::<f32>::identity());

The matrix with all elements set to zero.

Applies the function f to each element of this matrix, in-place.

For an example, see the map() method.

Applies the function f to each element of this matrix, in-place.

For an example, see the map2() method.

Returns a memberwise-converted copy of this matrix, using NumCast.

let m = Mat4::<f32>::identity();
let m: Mat4<i32> = m.numcast().unwrap();
assert_eq!(m, Mat4::identity());

Initializes a new matrix with elements of the diagonal set to val and the other to zero.

In a way, this is the same as single-argument matrix constructors in GLSL and GLM.

assert_eq!(Mat4::broadcast_diagonal(0), Mat4::zero());
assert_eq!(Mat4::broadcast_diagonal(1), Mat4::identity());
assert_eq!(Mat4::broadcast_diagonal(2), Mat4::new(
    2,0,0,0,
    0,2,0,0,
    0,0,2,0,
    0,0,0,2,
));

Initializes a matrix by its diagonal, setting other elements to zero.

Gets the matrix’s diagonal into a vector.

assert_eq!(Mat4::<u32>::zero().diagonal(), Vec4::zero());
assert_eq!(Mat4::<u32>::identity().diagonal(), Vec4::one());

let mut m = Mat4::zero();
m[(0, 0)] = 1;
m[(1, 1)] = 2;
m[(2, 2)] = 3;
m[(3, 3)] = 4;
assert_eq!(m.diagonal(), Vec4::new(1, 2, 3, 4));
assert_eq!(m.diagonal(), Vec4::iota() + 1);

The sum of the diagonal’s elements.

assert_eq!(Mat4::<u32>::zero().trace(), 0);
assert_eq!(Mat4::<u32>::identity().trace(), 4);

Multiply elements of this matrix with another’s.


let m = Mat4::new(
    0, 1, 2, 3,
    1, 2, 3, 4,
    2, 3, 4, 5,
    3, 4, 5, 6,
);
let r = Mat4::new(
    0, 1, 4, 9,
    1, 4, 9, 16,
    4, 9, 16, 25,
    9, 16, 25, 36,
);
assert_eq!(m.mul_memberwise(m), r);

Convenience for getting the number of rows of this matrix.

Convenience for getting the number of columns of this matrix.

Convenience constant representing the number of rows for matrices of this type.

Convenience constant representing the number of columns for matrices of this type.

Are all elements of this matrix tightly packed together in memory ?

This might not be the case for matrices in the repr_simd module (it depends on the target architecture).

Returns a row-wise-converted copy of this matrix, using the given conversion closure.

use vek::mat::repr_c::row_major::Mat4;

let m = Mat4::<f32>::new(
    0.25, 1.25, 5.56, 8.66,
    8.53, 2.92, 3.86, 9.36,
    1.02, 0.28, 5.52, 6.06,
    6.20, 7.01, 4.90, 5.26
);
let m = m.map_rows(|row| row.map(|x| x.round() as i32));
assert_eq!(m, Mat4::new(
    0, 1, 6, 9,
    9, 3, 4, 9,
    1, 0, 6, 6,
    6, 7, 5, 5
));

Converts this matrix into a fixed-size array of elements.

use vek::mat::repr_c::row_major::Mat4;

let m = Mat4::<u32>::new(
     0,  1,  2,  3,
     4,  5,  6,  7,
     8,  9, 10, 11,
    12, 13, 14, 15
);
let array = [
     0,  1,  2,  3,
     4,  5,  6,  7,
     8,  9, 10, 11,
    12, 13, 14, 15
];
assert_eq!(m.into_row_array(), array);

Converts this matrix into a fixed-size array of fixed-size arrays of elements.

use vek::mat::repr_c::row_major::Mat4;

let m = Mat4::<u32>::new(
     0,  1,  2,  3,
     4,  5,  6,  7,
     8,  9, 10, 11,
    12, 13, 14, 15
);
let array = [
    [  0,  1,  2,  3, ],
    [  4,  5,  6,  7, ],
    [  8,  9, 10, 11, ],
    [ 12, 13, 14, 15, ],
];
assert_eq!(m.into_row_arrays(), array);

Converts a fixed-size array of elements into a matrix.

use vek::mat::repr_c::row_major::Mat4;

let m = Mat4::<u32>::new(
     0,  1,  2,  3,
     4,  5,  6,  7,
     8,  9, 10, 11,
    12, 13, 14, 15
);
let array = [
     0,  1,  2,  3,
     4,  5,  6,  7,
     8,  9, 10, 11,
    12, 13, 14, 15
];
assert_eq!(m, Mat4::from_row_array(array));

Converts a fixed-size array of fixed-size array of elements into a matrix.

use vek::mat::repr_c::row_major::Mat4;

let m = Mat4::<u32>::new(
     0,  1,  2,  3,
     4,  5,  6,  7,
     8,  9, 10, 11,
    12, 13, 14, 15
);
let array = [
    [  0,  1,  2,  3, ],
    [  4,  5,  6,  7, ],
    [  8,  9, 10, 11, ],
    [ 12, 13, 14, 15, ],
];
assert_eq!(m, Mat4::from_row_arrays(array));

Converts this matrix into a fixed-size array of elements.

use vek::mat::repr_c::row_major::Mat4;

let m = Mat4::<u32>::new(
     0,  1,  2,  3,
     4,  5,  6,  7,
     8,  9, 10, 11,
    12, 13, 14, 15
);
let array = [
    0, 4, 8, 12,
    1, 5, 9, 13,
    2, 6, 10, 14,
    3, 7, 11, 15
];
assert_eq!(m.into_col_array(), array);

Converts this matrix into a fixed-size array of fixed-size arrays of elements.

use vek::mat::repr_c::row_major::Mat4;

let m = Mat4::<u32>::new(
     0,  1,  2,  3,
     4,  5,  6,  7,
     8,  9, 10, 11,
    12, 13, 14, 15
);
let array = [
    [ 0, 4,  8, 12, ],
    [ 1, 5,  9, 13, ],
    [ 2, 6, 10, 14, ],
    [ 3, 7, 11, 15, ],
];
assert_eq!(m.into_col_arrays(), array);

Converts a fixed-size array of elements into a matrix.

use vek::mat::repr_c::row_major::Mat4;

let m = Mat4::<u32>::new(
     0,  1,  2,  3,
     4,  5,  6,  7,
     8,  9, 10, 11,
    12, 13, 14, 15
);
let array = [
    0, 4, 8, 12,
    1, 5, 9, 13,
    2, 6, 10, 14,
    3, 7, 11, 15
];
assert_eq!(m, Mat4::from_col_array(array));

Converts a fixed-size array of fixed-size arrays of elements into a matrix.

use vek::mat::repr_c::row_major::Mat4;

let m = Mat4::<u32>::new(
     0,  1,  2,  3,
     4,  5,  6,  7,
     8,  9, 10, 11,
    12, 13, 14, 15
);
let array = [
    [ 0, 4,  8, 12, ],
    [ 1, 5,  9, 13, ],
    [ 2, 6, 10, 14, ],
    [ 3, 7, 11, 15, ],
];
assert_eq!(m, Mat4::from_col_arrays(array));

Gets a const pointer to this matrix’s elements.

Panics

Panics if the matrix’s elements are not tightly packed in memory, which may be the case for matrices in the repr_simd module. You may check this with the is_packed() method.

Gets a mut pointer to this matrix’s elements.

Panics

Panics if the matrix’s elements are not tightly packed in memory, which may be the case for matrices in the repr_simd module. You may check this with the is_packed() method.

View this matrix as an immutable slice.

Panics

Panics if the matrix’s elements are not tightly packed in memory, which may be the case for matrices in the repr_simd module. You may check this with the is_packed() method.

View this matrix as a mutable slice.

Panics

Panics if the matrix’s elements are not tightly packed in memory, which may be the case for matrices in the repr_simd module. You may check this with the is_packed() method.

Gets the transpose parameter to pass to OpenGL glUniformMatrix*() functions.

The return value is a plain bool which you may directly cast to a GLboolean.

This takes &self to prevent surprises when changing the type of matrix you plan to send.

The transpose parameter to pass to OpenGL glUniformMatrix*() functions.

Returns an element-wise-converted copy of this matrix, using the given conversion closure.

use vek::mat::repr_c::row_major::Mat4;

let m = Mat4::<f32>::new(
    0.25, 1.25, 5.56, 8.66,
    8.53, 2.92, 3.86, 9.36,
    1.02, 0.28, 5.52, 6.06,
    6.20, 7.01, 4.90, 5.26
);
let m = m.map(|x| x.round() as i32);
assert_eq!(m, Mat4::new(
    0, 1, 6, 9,
    9, 3, 4, 9,
    1, 0, 6, 6,
    6, 7, 5, 5
));

Returns a memberwise-converted copy of this matrix, using AsPrimitive.

Examples
let v = Vec4::new(0_f32, 1., 2., 3.);
let i: Vec4<i32> = v.as_();
assert_eq!(i, Vec4::new(0, 1, 2, 3));
Safety

In Rust versions before 1.45.0, some uses of the as operator were not entirely safe. In particular, it was undefined behavior if a truncated floating point value could not fit in the target integer type (#10184);

let x: u8 = (1.04E+17).as_(); // UB

Applies the function f to each element of two matrices, pairwise, and returns the result.

use vek::mat::repr_c::row_major::Mat4;

let a = Mat4::<f32>::new(
    0.25, 1.25, 2.52, 2.99,
    0.25, 1.25, 2.52, 2.99,
    0.25, 1.25, 2.52, 2.99,
    0.25, 1.25, 2.52, 2.99
);
let b = Mat4::<i32>::new(
    0, 1, 0, 0,
    1, 0, 0, 0,
    0, 0, 1, 0,
    0, 0, 0, 1
);
let m = a.map2(b, |a, b| a.round() as i32 + b);
assert_eq!(m, Mat4::new(
    0, 2, 3, 3,
    1, 1, 3, 3,
    0, 1, 4, 3,
    0, 1, 3, 4
));

The matrix’s transpose.

For orthogonal matrices, the transpose is the same as the inverse. All pure rotation matrices are orthogonal, and therefore can be inverted faster by simply computing their transpose.

use std::f32::consts::PI;

let m = Mat3::new(
    0, 1, 2,
    4, 5, 6,
    8, 9, 0
);
let t = Mat3::new(
    0, 4, 8,
    1, 5, 9,
    2, 6, 0
);
assert_eq!(m.transposed(), t);
assert_eq!(m, m.transposed().transposed());

let m = Mat3::rotation_x(PI/7.);
assert_relative_eq!(m * m.transposed(), Mat3::identity());
assert_relative_eq!(m.transposed() * m, Mat3::identity());

Transpose this matrix.


let mut m = Mat3::new(
    0, 1, 2,
    4, 5, 6,
    8, 9, 0
);
let t = Mat3::new(
    0, 4, 8,
    1, 5, 9,
    2, 6, 0
);
m.transpose();
assert_eq!(m, t);

Get this matrix’s determinant.

A matrix is invertible if its determinant is non-zero.

Shortcut for self * Vec3::from_point_2d(rhs).

Shortcut for self * Vec3::from_direction_2d(rhs).

Translates this matrix in 2D.

Returns this matrix translated in 2D.

Creates a 2D translation matrix.

Scales this matrix in 3D.

Returns this matrix scaled in 3D.

Creates a 3D scaling matrix.

Rotates this matrix around the X axis.

Returns this matrix rotated around the X axis.

Creates a matrix that rotates around the X axis.

Rotates this matrix around the Y axis.

Returns this matrix rotated around the Y axis.

Creates a matrix that rotates around the Y axis.

Rotates this matrix around the Z axis.

Returns this matrix rotated around the Z axis.

Creates a matrix that rotates around the Z axis.

Rotates this matrix around a 3D axis. The axis is not required to be normalized.

Returns this matrix rotated around a 3D axis. The axis is not required to be normalized.

Creates a matrix that rotates around a 3D axis. The axis is not required to be normalized.

use std::f32::consts::PI;

let angles = 32;
for i in 0..angles {
    let theta = PI * 2. * (i as f32) / (angles as f32);

    assert_relative_eq!(Mat3::rotation_x(theta), Mat3::from(Quaternion::rotation_x(theta)), epsilon = 0.000001);
    assert_relative_eq!(Mat3::rotation_y(theta), Mat3::from(Quaternion::rotation_y(theta)), epsilon = 0.000001);
    assert_relative_eq!(Mat3::rotation_z(theta), Mat3::from(Quaternion::rotation_z(theta)), epsilon = 0.000001);

    assert_relative_eq!(Mat3::rotation_x(theta), Mat3::rotation_3d(theta, Vec3::unit_x()));
    assert_relative_eq!(Mat3::rotation_y(theta), Mat3::rotation_3d(theta, Vec3::unit_y()));
    assert_relative_eq!(Mat3::rotation_z(theta), Mat3::rotation_3d(theta, Vec3::unit_z()));

    assert_relative_eq!(Mat3::rotation_x(theta), Mat3::from(Mat4::rotation_3d(theta, Vec3::unit_x())));
    assert_relative_eq!(Mat3::rotation_y(theta), Mat3::from(Mat4::rotation_3d(theta, Vec3::unit_y())));
    assert_relative_eq!(Mat3::rotation_z(theta), Mat3::from(Mat4::rotation_3d(theta, Vec3::unit_z())));

    assert_relative_eq!(Mat4::rotation_x(theta), Mat4::from(Mat3::rotation_3d(theta, Vec3::unit_x())));
    assert_relative_eq!(Mat4::rotation_y(theta), Mat4::from(Mat3::rotation_3d(theta, Vec3::unit_y())));
    assert_relative_eq!(Mat4::rotation_z(theta), Mat4::from(Mat3::rotation_3d(theta, Vec3::unit_z())));

    // See what rotating unit vectors do for most angles between 0 and 2*PI.
    // It's helpful to picture this as a right-handed coordinate system.

    let v = Vec3::unit_y();
    let m = Mat3::rotation_x(theta);
    assert_relative_eq!(m * v, Vec3::new(0., theta.cos(), theta.sin()));

    let v = Vec3::unit_z();
    let m = Mat3::rotation_y(theta);
    assert_relative_eq!(m * v, Vec3::new(theta.sin(), 0., theta.cos()));

    let v = Vec3::unit_x();
    let m = Mat3::rotation_z(theta);
    assert_relative_eq!(m * v, Vec3::new(theta.cos(), theta.sin(), 0.));
}

Creates a matrix that would rotate a from direction to to.


let (from, to) = (Vec3::<f32>::unit_x(), Vec3::<f32>::unit_z());
let m = Mat3::<f32>::rotation_from_to_3d(from, to);
assert_relative_eq!(m * from, to);

let (from, to) = (Vec3::<f32>::unit_x(), -Vec3::<f32>::unit_x());
let m = Mat3::<f32>::rotation_from_to_3d(from, to);
assert_relative_eq!(m * from, to);

Creates a new 3x3 matrix from elements in a layout-agnostic way.

The parameters are named mij where i is the row index and j the column index. Their order is always the same regardless of the matrix’s layout.

Trait Implementations§

Used for specifying relative comparisons.
The default tolerance to use when testing values that are close together. Read more
A test for equality that uses the absolute difference to compute the approximate equality of two numbers.
The inverse of AbsDiffEq::abs_diff_eq.
The resulting type after applying the + operator.
Performs the + operation. Read more
The resulting type after applying the + operator.
Performs the + operation. Read more
Performs the += operation. Read more
Performs the += operation. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

The default value for a square matrix is the identity.

assert_eq!(Mat4::<f32>::default(), Mat4::<f32>::identity());
Returns the “default value” for a type. Read more
Deserialize this value from the given Serde deserializer. Read more

Displays this matrix using the following format:

(i being the number of rows and j the number of columns)

( m00 ... m0j
  ... ... ...
  mi0 ... mij )

Note that elements are not comma-separated. This format doesn’t depend on the matrix’s storage layout.

Formats the value using the given formatter. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
Performs the /= operation. Read more
Performs the /= operation. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more

Index this matrix in a layout-agnostic way with an (i, j) (row index, column index) tuple.

Matrices cannot be indexed by Vec2s because that would be likely to cause confusion: should x be the row index (because it’s the first element) or the column index (because it’s a horizontal position) ?

The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more

Multiplies a column-major matrix with a row-major matrix.

use vek::mat::row_major::Mat4 as Rows4;
use vek::mat::column_major::Mat4 as Cols4;

let m = Cols4::<u32>::new(
    0, 1, 2, 3,
    4, 5, 6, 7,
    8, 9, 0, 1,
    2, 3, 4, 5
);
let b = Rows4::from(m);
let r = Rows4::<u32>::new(
    26, 32, 18, 24,
    82, 104, 66, 88,
    38, 56, 74, 92,
    54, 68, 42, 56
);
assert_eq!(m * b, r);
assert_eq!(m * Rows4::<u32>::identity(), m.into());
assert_eq!(Cols4::<u32>::identity() * b, m.into());
The resulting type after applying the * operator.
Performs the * operation. Read more

Multiplies a row-major matrix with another.

use vek::mat::row_major::Mat4;

let m = Mat4::<u32>::new(
    0, 1, 2, 3,
    4, 5, 6, 7,
    8, 9, 0, 1,
    2, 3, 4, 5
);
let r = Mat4::<u32>::new(
    26, 32, 18, 24,
    82, 104, 66, 88,
    38, 56, 74, 92,
    54, 68, 42, 56
);
assert_eq!(m * m, r);
assert_eq!(m, m * Mat4::<u32>::identity());
assert_eq!(m, Mat4::<u32>::identity() * m);
The resulting type after applying the * operator.
Performs the * operation. Read more

Multiplies a row-major matrix with a column-major matrix, producing a column-major matrix.

use vek::mat::row_major::Mat4 as Rows4;
use vek::mat::column_major::Mat4 as Cols4;

let m = Rows4::<u32>::new(
    0, 1, 2, 3,
    4, 5, 6, 7,
    8, 9, 0, 1,
    2, 3, 4, 5
);
let b = Cols4::from(m);
let r = Cols4::<u32>::new(
    26, 32, 18, 24,
    82, 104, 66, 88,
    38, 56, 74, 92,
    54, 68, 42, 56
);
assert_eq!(m * b, r);
assert_eq!(m * Cols4::<u32>::identity(), m.into());
assert_eq!(Rows4::<u32>::identity() * b, m.into());
The resulting type after applying the * operator.
Performs the * operation. Read more

Multiplies a row vector with a row-major matrix, giving a row vector.

With SIMD vectors, this is the most efficient way.

use vek::mat::row_major::Mat4;
use vek::vec::Vec4;

let m = Mat4::new(
    0, 1, 2, 3,
    4, 5, 6, 7,
    8, 9, 0, 1,
    2, 3, 4, 5
);
let v = Vec4::new(0, 1, 2, 3);
let r = Vec4::new(26, 32, 18, 24);
assert_eq!(v * m, r);
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more

Multiplies a row-major matrix with a column vector, giving a column vector.

use vek::mat::row_major::Mat4;
use vek::vec::Vec4;

let m = Mat4::new(
    0, 1, 2, 3,
    4, 5, 6, 7,
    8, 9, 0, 1,
    2, 3, 4, 5
);
let v = Vec4::new(0, 1, 2, 3);
let r = Vec4::new(14, 38, 12, 26);
assert_eq!(m * v, r);
The resulting type after applying the * operator.
Performs the * operation. Read more
Performs the *= operation. Read more
Performs the *= operation. Read more
The resulting type after applying the - operator.
Performs the unary - operation. Read more
Returns the multiplicative identity element of Self, 1. Read more
Sets self to the multiplicative identity element of Self, 1.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
The default relative tolerance for testing values that are far-apart. Read more
A test for equality that uses a relative comparison if the values are far apart.
The inverse of RelativeEq::relative_eq.
The resulting type after applying the % operator.
Performs the % operation. Read more
The resulting type after applying the % operator.
Performs the % operation. Read more
Performs the %= operation. Read more
Performs the %= operation. Read more
Serialize this value into the given Serde serializer. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more
Performs the -= operation. Read more
Performs the -= operation. Read more
The default ULPs to tolerate when testing values that are far-apart. Read more
A test for equality that uses units in the last place (ULP) if the values are far apart.
The inverse of UlpsEq::ulps_eq.
Returns the additive identity element of Self, 0. Read more
Returns true if self is equal to the additive identity.
Sets self to the additive identity element of Self, 0.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.