Struct cgl_rs::math::Matrix4x4

source ·
#[repr(C)]
pub struct Matrix4x4 { /* private fields */ }
Expand description

A 4x4 matrix struct used for graphics programming with OpenGL.

Implementations§

source§

impl Matrix4x4

source

pub fn new( m00: f32, m01: f32, m02: f32, m03: f32, m10: f32, m11: f32, m12: f32, m13: f32, m20: f32, m21: f32, m22: f32, m23: f32, m30: f32, m31: f32, m32: f32, m33: f32 ) -> Matrix4x4

Creates a new matrix with the given values.

Arguments
  • m00 .. m33 - The values of the matrix.
Returns

A new identity matrix.

Example
use cgl_rs::math::Matrix4x4;
 
let m = Matrix4x4::new(
   1.0, 0.0, 0.0, 0.0,
   0.0, 1.0, 0.0, 0.0,
   0.0, 0.0, 1.0, 0.0,
   0.0, 0.0, 0.0, 1.0
);
 
println!("{}", m);
source

pub fn from_mat3(m: &Matrix3x3) -> Matrix4x4

Creates a new 4x4 matrix from a 3x3 matrix.

Arguments
  • m - The 3x3 matrix to convert to a 4x4 matrix.
Returns

A new 4x4 matrix with the same values as the input 3x3 matrix, with the fourth row and column set to zero.

Example
use cgl_rs::math::{Matrix3x3, Matrix4x4};

let m3 = Matrix3x3::new(
    1.0, 2.0, 3.0,
    4.0, 5.0, 6.0,
    7.0, 8.0, 9.0
);

let m4 = Matrix4x4::from_mat3(&m3);
source

pub fn zero() -> Matrix4x4

Returns a new matrix with all elements set to zero.

Returns

A new matrix with all elements set to zero.

Example
use cgl_rs::math::Matrix4x4;

let m = Matrix4x4::zero();
source

pub fn identity() -> Matrix4x4

Returns a new identity matrix.

Returns

A new identity matrix.

Example
use cgl_rs::math::Matrix4x4;

let m = Matrix4x4::identity();
source

pub fn scale(x: f32, y: f32, z: f32) -> Matrix4x4

Returns a new scaling matrix.

Arguments
  • x - The scaling factor along the x-axis.
  • y - The scaling factor along the y-axis.
  • z - The scaling factor along the z-axis.
Returns

A new scaling matrix.

Example
use cgl_rs::math::Matrix4x4;

let m = Matrix4x4::scale(2.0, 3.0, 4.0);
source

pub fn translate(x: f32, y: f32, z: f32) -> Matrix4x4

Returns a new translation matrix.

Arguments
  • x - The translation along the x-axis.
  • y - The translation along the y-axis.
  • z - The translation along the z-axis.
Returns

A new translation matrix.

Example
use cgl_rs::math::Matrix4x4;

let m = Matrix4x4::translate(1.0, 2.0, 3.0);
source

pub fn rotate_x(angle: f32) -> Matrix4x4

Returns a new rotation matrix around the x-axis.

Arguments
  • angle - The angle of rotation in radians.
Returns

A new rotation matrix around the x-axis.

Example
use cgl_rs::math::Matrix4x4;

let m = Matrix4x4::rotate_x(0.5);
source

pub fn rotate_y(angle: f32) -> Matrix4x4

Returns a new rotation matrix around the y-axis.

Arguments
  • angle - The angle of rotation in radians.
Returns

A new rotation matrix around the y-axis.

Example
use cgl_rs::math::Matrix4x4;

let m = Matrix4x4::rotate_y(0.5);
source

pub fn rotate_z(angle: f32) -> Matrix4x4

Returns a new rotation matrix around the z-axis.

Arguments
  • angle - The angle of rotation in radians.
Returns

A new rotation matrix around the z-axis.

Example
use cgl_rs::math::Matrix4x4;

let m = Matrix4x4::rotate_z(0.5);
source

pub fn rotate_about_axis(axis: &Vector3, angle: f32) -> Matrix4x4

Returns a new rotation matrix that rotates around the given axis by the specified angle.

Arguments
  • axis - The axis to rotate around.
  • angle - The angle of rotation in radians.
Returns

A new rotation matrix that rotates around the given axis by the specified angle.

Example
use cgl_rs::math::{Matrix4x4, Vector3};

let axis = Vector3::new(1.0, 0.0, 0.0);
let m = Matrix4x4::rotate_about_axis(&axis, 0.5);
source

pub fn perspective(fov: f32, aspect: f32, near: f32, far: f32) -> Matrix4x4

Returns a new perspective projection matrix.

Arguments
  • fov - The field of view angle in radians.
  • aspect - The aspect ratio of the projection.
  • near - The distance to the near clipping plane.
  • far - The distance to the far clipping plane.
Returns

A new perspective projection matrix.

Example
use cgl_rs::math::Matrix4x4;
 
let m = Matrix4x4::perspective(cgl_rs::math::constants::PI_2, 16.0/9.0, 0.1, 100.0);
source

pub fn orthographic( left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32 ) -> Matrix4x4

Returns a new orthographic projection matrix.

Arguments
  • left - The coordinate for the left vertical clipping plane.
  • right - The coordinate for the right vertical clipping plane.
  • bottom - The coordinate for the bottom horizontal clipping plane.
  • top - The coordinate for the top horizontal clipping plane.
  • near - The distance to the near clipping plane.
  • far - The distance to the far clipping plane.
Returns

A new orthographic projection matrix.

Example
use cgl_rs::math::Matrix4x4;

let m = Matrix4x4::orthographic(-1.0, 1.0, -1.0, 1.0, 0.1, 100.0);
source

pub fn look_at(eye: Vector3, target: Vector3, up: Vector3) -> Matrix4x4

Returns a new view matrix that looks from the eye position towards the target position.

Arguments
  • eye - The position of the camera.
  • target - The position to look at.
  • up - The up direction of the camera.
Returns

A new view matrix.

Example
use cgl_rs::math::{Matrix4x4, Vector3};

let mat = Matrix4x4::look_at(
   Vector3::new(0.0, 0.0, 0.0),
   Vector3::new(0.0, 0.0, -1.0),
   Vector3::new(0.0, 1.0, 0.0)
);
source

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

Multiplies this matrix by another matrix and returns the result.

Arguments
  • other - The matrix to multiply by.
Returns

The resulting matrix of the multiplication.

Example
use cgl_rs::math::Matrix4x4;

let m1 = Matrix4x4::identity();
let m2 = Matrix4x4::rotate_z(0.5);
let m3 = m1.mul(&m2);
source

pub fn determinant(&self) -> f32

Calculates the determinant of this matrix.

Returns

The determinant of this matrix.

Example
use cgl_rs::math::Matrix4x4;

let m = Matrix4x4::identity();
let det = m.determinant();
source

pub fn mul_vec4(&self, other: &Vector4) -> Vector4

Multiplies this matrix by a vector and returns the result.

Arguments
  • other - The vector to multiply by.
Returns

The resulting vector of the multiplication.

Example
use cgl_rs::math::{Matrix4x4, Vector4};

let m = Matrix4x4::identity();
let v = Vector4::new(1.0, 2.0, 3.0, 1.0);
let result = m.mul_vec4(&v);
source

pub fn inverse(&self) -> Matrix4x4

Calculates the inverse of this matrix.

Returns

The inverse of this matrix.

Example
use cgl_rs::math::Matrix4x4;

let m = Matrix4x4::identity();
let inv = m.inverse();
source

pub fn transpose(&self) -> Matrix4x4

Transposes this matrix.

Returns

The transposed matrix.

Example
use cgl_rs::math::Matrix4x4;

let m = Matrix4x4::identity();
let transposed = m.transpose();
source

pub fn adjoint(&self) -> Matrix4x4

Calculates the adjoint of this matrix.

Returns

The adjoint of this matrix.

Example
use cgl_rs::math::Matrix4x4;

let m = Matrix4x4::identity();
let adj = m.adjoint();
source

pub fn gaussian_elimination(&self) -> Matrix4x4

Performs Gaussian elimination on this matrix.

Returns

The matrix in row echelon form.

Example
use cgl_rs::math::Matrix4x4;

let m = Matrix4x4::identity();
let row_echelon = m.gaussian_elimination();
source

pub fn rank(&self) -> i32

Calculates the rank of this matrix.

Returns

The rank of this matrix.

Example
use cgl_rs::math::Matrix4x4;

let m = Matrix4x4::identity();
let rank = m.rank();
source

pub fn trace(&self) -> f32

Calculates the trace of this matrix.

Returns

The trace of this matrix.

Example
use cgl_rs::math::Matrix4x4;

let m = Matrix4x4::identity();
let trace = m.trace();
source

pub fn to_mat3(&self) -> Matrix3x3

Converts this Matrix4x4 to a Matrix3x3 by discarding the last row and column.

Example
use cgl_rs::math::{Matrix3x3, Matrix4x4};

let m = Matrix4x4::identity();
let m3 = m.to_mat3();
source

pub fn decompose_lu(&self) -> (Matrix4x4, Matrix4x4)

Decomposes this matrix into lower and upper triangular matrices using LU decomposition.

Returns

A tuple containing the lower and upper triangular matrices.

Example
use cgl_rs::math::Matrix4x4;

let m = Matrix4x4::identity();
let (l, u) = m.decompose_lu();
source

pub fn lerp(&self, other: &Matrix4x4, t: f32) -> Matrix4x4

Performs a linear interpolation between this matrix and another matrix.

Arguments
  • other - The other matrix to interpolate with.
  • t - The interpolation factor. Should be between 0.0 and 1.0.
Returns

The interpolated matrix.

Example
use cgl_rs::math::Matrix4x4;

let m1 = Matrix4x4::identity();
let m2 = Matrix4x4::scale(1.0, 2.0, 3.0);
let m3 = m1.lerp(&m2, 0.5);

Trait Implementations§

source§

impl Add<Matrix4x4> for Matrix4x4

§

type Output = Matrix4x4

The resulting type after applying the + operator.
source§

fn add(self, rhs: Matrix4x4) -> Matrix4x4

Performs the + operation. Read more
source§

impl Clone for Matrix4x4

source§

fn clone(&self) -> Matrix4x4

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for Matrix4x4

source§

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

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

impl Display for Matrix4x4

source§

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

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

impl Mul<f32> for Matrix4x4

§

type Output = Matrix4x4

The resulting type after applying the * operator.
source§

fn mul(self, rhs: f32) -> Matrix4x4

Performs the * operation. Read more
source§

impl PartialEq<Matrix4x4> for Matrix4x4

source§

fn eq(&self, other: &Matrix4x4) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Sub<Matrix4x4> for Matrix4x4

§

type Output = Matrix4x4

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Matrix4x4) -> Matrix4x4

Performs the - operation. Read more
source§

impl Copy for Matrix4x4

source§

impl StructuralPartialEq for Matrix4x4

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

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

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

§

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 Twhere U: TryFrom<T>,

§

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.