Skip to main content

Mat4

Struct Mat4 

Source
pub struct Mat4<T>(pub Vec4<T>, pub Vec4<T>, pub Vec4<T>, pub Vec4<T>);

Tuple Fields§

§0: Vec4<T>§1: Vec4<T>§2: Vec4<T>§3: Vec4<T>

Implementations§

Source§

impl<T> Mat4<T>
where T: NumAssign + Copy,

Source

pub fn new( col0: &Vec4<T>, col1: &Vec4<T>, col2: &Vec4<T>, col3: &Vec4<T>, ) -> Self

Creates a new Mat4 instance from four column vectors.

§Parameters
  • col0: The first column vector.
  • col1: The second column vector.
  • col2: The third column vector.
  • col3: The fourth column vector.
§Returns

Returns a new Mat4 with the specified column vectors.

§Examples
let col0 = Vec4::new(1.0, 0.0, 0.0, 0.0);
let col1 = Vec4::new(0.0, 1.0, 0.0, 0.0);
let col2 = Vec4::new(0.0, 0.0, 1.0, 0.0);
let col3 = Vec4::new(0.0, 0.0, 0.0, 1.0);
let mat = Mat4::new(&col0, &col1, &col2, &col3);
Source

pub fn zero() -> Self

Returns the zero matrix (all elements are zero).

§Returns

Returns a new Mat4 where all components are zero.

§Examples
let mat = Mat4::zero();
Source

pub fn identity() -> Self

Returns the identity matrix (diagonal with ones).

§Returns

Returns a new Mat4 with ones on the diagonal and zeros elsewhere.

§Examples
let mat = Mat4::identity();
Source

pub fn transpose(&self) -> Self

Computes the transpose of the matrix.

The transpose of a matrix is obtained by swapping rows with columns.

§Returns

Returns a new Mat4 which is the transpose of the original matrix.

§Examples
let mat = Mat4::identity();
let transposed = mat.transpose();
assert_eq!(transposed, mat);
Source

pub fn determinant(&self) -> T

Computes the determinant of the matrix.

The determinant is a scalar value that can be used to determine if the matrix is invertible.

§Returns

Returns the determinant as a T.

§Examples
let mat = Mat4::identity();
let determinant = mat.determinant();
assert_eq!(determinant, 1.0);
Source

pub fn trace(&self) -> T

Computes the trace of the matrix.

The trace is the sum of the diagonal elements.

§Returns

Returns the trace as a T.

§Examples
let mat = Mat4::identity();
let trace = mat.trace();
assert_eq!(trace, 4.0);
Source

pub fn mul(&self, other: &Self) -> Self

Multiplies this matrix by another matrix.

§Parameters
  • other: The matrix to multiply with.
§Returns

Returns the resulting matrix after multiplication.

§Examples
let mat1 = Mat4::identity();
let mat2 = Mat4::identity();
let result = mat1.mul(&mat2);
assert_eq!(result, mat1);
Source

pub fn translate(&mut self, translate: &Vec3<T>)

Applies a translation transformation to the matrix.

This method modifies the matrix to include the translation vector.

§Parameters
  • translate: The translation vector to apply.
§Examples
let mut mat = Mat4::identity();
let translate = Vec3::new(1.0, 2.0, 3.0);
mat.translate(&translate);
Source

pub fn scale(&mut self, scale: &Vec3<T>)

Applies a scale transformation to the matrix.

This method modifies the matrix to include the scaling vector.

§Parameters
  • scale: The scaling vector to apply.
§Examples
let mut mat = Mat4::identity();
let scale = Vec3::new(2.0, 3.0, 4.0);
mat.scale(&scale);
Source§

impl<T> Mat4<T>
where T: NumAssign + Float,

Source

pub fn invert(&self) -> Option<Self>

Computes the inverse of the matrix.

§Returns

Returns Some(Self) if the matrix is invertible, None otherwise. The inverse is computed using the adjugate method and determinant.

§Examples
use std::f32::consts::PI;
let mat = Mat4::identity();
let inv = mat.invert();
assert!(inv.is_some());
Source

pub fn rotate(&mut self, axis: Vec3<T>, angle: T)

Applies a rotation transformation to the matrix around an arbitrary axis.

§Parameters
  • axis: The axis of rotation as a Vec3<T>. It should be normalized.
  • angle: The angle of rotation in radians.
§Examples
use std::f32::consts::PI;
let mut mat = Mat4::identity();
let axis = Vec3::new(0.0, 1.0, 0.0);
let angle = PI / 4.0; // 45 degrees
mat.rotate(&axis, angle);
Source

pub fn rotate_x(&mut self, angle: T)

Rotates the matrix around the X-axis by a given angle.

§Parameters
  • angle: The angle of rotation in radians.
§Examples
use std::f32::consts::PI;
let mut mat = Mat4::identity();
let angle = PI / 4.0; // 45 degrees
mat.rotate_x(angle);
Source

pub fn rotate_y(&mut self, angle: T)

Rotates the matrix around the Y-axis by a given angle.

§Parameters
  • angle: The angle of rotation in radians.
§Examples
use std::f32::consts::PI;
let mut mat = Mat4::identity();
let angle = PI / 4.0; // 45 degrees
mat.rotate_y(angle);
Source

pub fn rotate_z(&mut self, angle: T)

Rotates the matrix around the Z-axis by a given angle.

§Parameters
  • angle: The angle of rotation in radians.
§Examples
use std::f32::consts::PI;
let mut mat = Mat4::identity();
let angle = PI / 4.0; // 45 degrees
mat.rotate_z(angle);
Source

pub fn rotate_xyz(&mut self, angles: Vec3<T>)

Applies a rotation transformation to the matrix around the X, Y, and Z axes sequentially.

The rotations are applied in the order X -> Y -> Z, meaning that:

  • First, the matrix is rotated around the X-axis by angles.x.
  • Then, the resulting matrix is rotated around the Y-axis by angles.y.
  • Finally, the resulting matrix is rotated around the Z-axis by angles.z.
§Parameters
  • angles: A Vec3<T> containing the rotation angles (in radians) for the X, Y, and Z axes respectively.
§Examples
use std::f32::consts::PI;
let mut mat = Mat4::identity();
let angles = Vec3::new(PI / 4.0, PI / 6.0, PI / 3.0);
mat.rotate_xyz(angles);
Source

pub fn rotate_zyx(&mut self, angles: Vec3<T>)

Applies a rotation transformation to the matrix around the Z, Y, and X axes sequentially.

The rotations are applied in the order Z -> Y -> X, meaning that:

  • First, the matrix is rotated around the Z-axis by angles.z.
  • Then, the resulting matrix is rotated around the Y-axis by angles.y.
  • Finally, the resulting matrix is rotated around the X-axis by angles.x.
§Parameters
  • angles: A Vec3<T> containing the rotation angles (in radians) for the Z, Y, and X axes respectively.
§Examples
use std::f32::consts::PI;
let mut mat = Mat4::identity();
let angles = Vec3::new(PI / 3.0, PI / 6.0, PI / 4.0);
mat.rotate_zyx(angles);
Source

pub fn frustum( left: T, right: T, bottom: T, top: T, near_plane: T, far_plane: T, ) -> Self

Creates a frustum matrix for a perspective projection.

The frustum matrix transforms coordinates from the view frustum to normalized device coordinates.

§Parameters
  • left, right, bottom, top: Coordinates of the clipping planes of the view frustum.
  • near_plane: The distance to the near clipping plane.
  • far_plane: The distance to the far clipping plane.
§Returns

A Mat4<T> representing the frustum projection matrix.

§Examples
let frustum = Mat4::frustum(-1.0, 1.0, -1.0, 1.0, 0.1, 100.0);
Source

pub fn perspective(fov_y: T, aspect: T, near_plane: T, far_plane: T) -> Self

Creates a perspective projection matrix.

This matrix is used to transform coordinates from a perspective projection view to normalized device coordinates.

§Parameters
  • fov_y: Field of view in the Y direction (in radians).
  • aspect: Aspect ratio of the viewport (width / height).
  • near_plane: The distance to the near clipping plane.
  • far_plane: The distance to the far clipping plane.
§Returns

A Mat4<T> representing the perspective projection matrix.

§Examples
let perspective = Mat4::perspective(std::f32::consts::PI / 4.0, 16.0 / 9.0, 0.1, 100.0);
Source

pub fn orthographic( left: T, right: T, bottom: T, top: T, near_plane: T, far_plane: T, ) -> Self

Creates an orthographic projection matrix.

This matrix maps 3D coordinates to a 2D view with no perspective distortion. It is useful for 2D games or certain types of 3D projections where depth perception is not needed.

§Parameters
  • left, right, bottom, top: Coordinates of the clipping planes of the orthographic view.
  • near_plane: The distance to the near clipping plane.
  • far_plane: The distance to the far clipping plane.
§Returns

A Mat4<T> representing the orthographic projection matrix.

§Examples
let ortho = Mat4::orthographic(-1.0, 1.0, -1.0, 1.0, 0.1, 100.0);
Source

pub fn look_at(eye: Vec3<T>, target: Vec3<T>, up: Vec3<T>) -> Self

Creates a view matrix that orients the camera to look at a specific target.

The view matrix transforms coordinates from world space to camera (or view) space.

§Parameters
  • eye: The position of the camera.
  • target: The position the camera is looking at.
  • up: The up direction of the camera.
§Returns

A Mat4<T> representing the view matrix.

§Examples
let eye = Vec3::new(0.0, 0.0, 5.0);
let target = Vec3::new(0.0, 0.0, 0.0);
let up = Vec3::new(0.0, 1.0, 0.0);
let view_matrix = Mat4::look_at(eye, target, up);

Trait Implementations§

Source§

impl<T> Add for Mat4<T>
where T: NumAssign + Copy,

Source§

type Output = Mat4<T>

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
Source§

impl<T: Clone> Clone for Mat4<T>

Source§

fn clone(&self) -> Mat4<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Copy> Copy for Mat4<T>

Source§

impl<T: Debug> Debug for Mat4<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for Mat4<T>

Source§

fn default() -> Mat4<T>

Returns the “default value” for a type. Read more
Source§

impl<T> Display for Mat4<T>
where T: Display,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Div<T> for Mat4<T>
where T: NumAssign + Copy,

Source§

type Output = Mat4<T>

The resulting type after applying the / operator.
Source§

fn div(self, scalar: T) -> Self

Performs the / operation. Read more
Source§

impl<T> Index<usize> for Mat4<T>

Source§

type Output = Vec4<T>

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<usize> for Mat4<T>

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a, T> IntoIterator for &'a Mat4<T>
where T: NumAssign + Copy,

Source§

type Item = &'a Vec4<T>

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, Vec4<T>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a mut Mat4<T>
where T: NumAssign + Copy,

Source§

type Item = &'a mut Vec4<T>

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, Vec4<T>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> Mul for Mat4<T>
where T: NumAssign + Copy,

Source§

type Output = Mat4<T>

The resulting type after applying the * operator.
Source§

fn mul(self, other: Mat4<T>) -> Mat4<T>

Performs the * operation. Read more
Source§

impl<T> Mul<T> for Mat4<T>
where T: NumAssign + Copy,

Source§

type Output = Mat4<T>

The resulting type after applying the * operator.
Source§

fn mul(self, scalar: T) -> Self

Performs the * operation. Read more
Source§

impl<T> Neg for Mat4<T>
where T: NumAssign + Copy + Neg<Output = T>,

Source§

type Output = Mat4<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<T: PartialEq> PartialEq for Mat4<T>

Source§

fn eq(&self, other: &Mat4<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> StructuralPartialEq for Mat4<T>

Source§

impl<T> Sub for Mat4<T>
where T: NumAssign + Copy,

Source§

type Output = Mat4<T>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self

Performs the - operation. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Mat4<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Mat4<T>
where T: RefUnwindSafe,

§

impl<T> Send for Mat4<T>
where T: Send,

§

impl<T> Sync for Mat4<T>
where T: Sync,

§

impl<T> Unpin for Mat4<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Mat4<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Mat4<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.