Struct Mat4

Source
#[repr(C)]
pub struct Mat4 { pub x_axis: Vec4, pub y_axis: Vec4, pub z_axis: Vec4, pub w_axis: Vec4, }
Expand description

Column-major 4x4 matrix acting on column vectors: translation * rotation * scale.

Fields§

§x_axis: Vec4§y_axis: Vec4§z_axis: Vec4§w_axis: Vec4

Implementations§

Source§

impl Mat4

Source

pub const IDENTITY: Mat4

Source

pub const ZERO: Mat4

Source

pub const NAN: Mat4

Source

pub const fn from_cols( x_axis: Vec4, y_axis: Vec4, z_axis: Vec4, w_axis: Vec4, ) -> Mat4

Source

pub const fn from_cols_array(v: &[f32; 16]) -> Mat4

Source

pub const fn from_cols_array_2d(v: &[[f32; 4]; 4]) -> Mat4

Source

pub const fn to_cols_array(self) -> [f32; 16]

Source

pub const fn to_cols_array_2d(self) -> [[f32; 4]; 4]

Source

pub fn col(self, index: usize) -> Vec4

Source

pub fn row(self, index: usize) -> Vec4

Source

pub fn transpose(self) -> Mat4

Source

pub const fn from_translation(v: Vec3) -> Mat4

Source

pub const fn from_scale(v: Vec3) -> Mat4

Source

pub const fn from_diagonal(v: Vec4) -> Mat4

Source

pub fn from_quat(rotation: Quat) -> Mat4

The quaternion is normalized to avoid embedding accidental scale.

Source

pub fn from_axis_angle(axis: Vec3, angle: f32) -> Mat4

Source

pub fn from_rotation_x(angle: f32) -> Mat4

Source

pub fn from_rotation_y(angle: f32) -> Mat4

Source

pub fn from_rotation_z(angle: f32) -> Mat4

Source

pub fn from_rotation_translation(rotation: Quat, translation: Vec3) -> Mat4

Source

pub fn from_scale_rotation_translation( scale: Vec3, rotation: Quat, translation: Vec3, ) -> Mat4

Source

pub fn transform_point3(self, v: Vec3) -> Vec3

Source

pub fn transform_vector3(self, v: Vec3) -> Vec3

Source

pub fn project_point3(self, v: Vec3) -> Vec3

Source

pub fn try_project_point3(self, v: Vec3) -> Result<Vec3, MatrixError>

Projects a finite point with f64 dot products and perspective division.

Rejects a non-finite matrix or point with MatrixError::NonFiniteInput. Returns MatrixError::ZeroHomogeneousW for exactly zero w, and MatrixError::ResultOutOfRange if a divided coordinate cannot round to finite f32. There is no absolute epsilon: tiny nonzero w is allowed when the divided result fits. Negative w and points outside the clip volume are allowed; this method does not test visibility or require an inverse. Output rounding may underflow coordinates to zero.

An inverse matrix projects normalized device coordinates back to world space (subject to floating-point error):

use ferravex_math::{Mat4, MatrixError, vec3};
let projection = Mat4::try_perspective_rh(1.0, 1.5, 0.1, 100.0)?;
let point = vec3(0.2, -0.1, -2.0);
let ndc = projection.try_project_point3(point)?;
let restored = projection.checked_inverse()?.try_project_point3(ndc)?;
assert!(restored.abs_diff_eq(point, 1.0e-5));
Source

pub fn mul_vec4(self, v: Vec4) -> Vec4

Source

pub fn mul_mat4(self, rhs: Mat4) -> Mat4

Source

pub fn is_finite(self) -> bool

Source

pub fn is_nan(self) -> bool

Source

pub fn abs_diff_eq(self, rhs: Mat4, tolerance: f32) -> bool

Source

pub fn determinant(self) -> f32

Partial-pivot Gaussian elimination with f64 intermediates.

Source

pub fn try_inverse(self) -> Option<Mat4>

Returns None for singular/non-finite matrices or inverses outside f32 range. See Self::checked_inverse for error details and precision limits.

Source

pub fn checked_inverse(self) -> Result<Mat4, MatrixError>

General partial-pivot Gauss-Jordan inverse with diagnostic errors.

Uses a fixed stack buffer and f64 intermediates. A zero pivot is singular; there is no determinant/epsilon cutoff, so tiny finite scales are accepted when the inverse rounds to finite f32. Non-finite input and overflow are reported separately. Output rounding may underflow coefficients to zero. Singularity is tested on the computed pivots, not with exact arithmetic; cancellation can hide a dependency or erase a small nonzero pivot. This is not a condition-number test: nearly dependent axes can amplify rounding error, and success does not promise an accurate round trip. Self::try_inverse and Self::inverse retain their Option/NaN behavior.

use ferravex_math::{Mat4, MatrixError, vec3};
let tiny = Mat4::from_scale(vec3(1.0e-30, 2.0e-30, 3.0e-30));
let inverse = tiny.checked_inverse()?;
assert!((tiny * inverse).abs_diff_eq(Mat4::IDENTITY, 1.0e-6));
assert_eq!(Mat4::NAN.checked_inverse(),
           Err(MatrixError::NonFiniteInput { input: "matrix" }));
Source

pub fn inverse(self) -> Mat4

Source

pub fn look_at_rh(eye: Vec3, target: Vec3, up: Vec3) -> Mat4

Right-handed view: the camera looks along its negative Z axis.

Source

pub fn look_to_rh(eye: Vec3, direction: Vec3, up: Vec3) -> Mat4

Source

pub fn look_at_lh(eye: Vec3, target: Vec3, up: Vec3) -> Mat4

Source

pub fn look_to_lh(eye: Vec3, direction: Vec3, up: Vec3) -> Mat4

Source

pub fn try_look_at_rh( eye: Vec3, target: Vec3, up: Vec3, ) -> Result<Mat4, MatrixError>

Checked right-handed view looking from eye toward target along -Z.

Rejects non-finite inputs, coincident eye/target, zero or parallel up, and translations outside f32 range. Subtraction, cross products and normalization use f64, so tiny directions and extreme finite inputs do not fail merely because an f32 intermediate would underflow/overflow. No angular epsilon is imposed; almost parallel up can still be unstable.

use ferravex_math::{Mat4, MatrixError, Vec3, vec3};
let eye = vec3(0.0, 0.0, 5.0);
let view = Mat4::try_look_at_rh(eye, Vec3::ZERO, Vec3::Y)?;
assert_eq!(view.transform_point3(eye), Vec3::ZERO);
assert_eq!(Mat4::try_look_at_rh(eye, eye, Vec3::Y),
           Err(MatrixError::DegenerateDirection));
Source

pub fn try_look_to_rh( eye: Vec3, direction: Vec3, up: Vec3, ) -> Result<Mat4, MatrixError>

Checked right-handed view along direction (camera -Z). Uses the finite/degeneracy rules of Self::try_look_at_rh.

use ferravex_math::{Mat4, Vec3};
assert_eq!(Mat4::try_look_to_rh(Vec3::ZERO, Vec3::NEG_Z, Vec3::Y).unwrap(), Mat4::IDENTITY);
Source

pub fn try_look_at_lh( eye: Vec3, target: Vec3, up: Vec3, ) -> Result<Mat4, MatrixError>

Checked left-handed view toward target (camera +Z). Uses the finite/degeneracy rules of Self::try_look_at_rh.

use ferravex_math::{Mat4, Vec3};
assert_eq!(Mat4::try_look_at_lh(Vec3::ZERO, Vec3::Z, Vec3::Y).unwrap(), Mat4::IDENTITY);
Source

pub fn try_look_to_lh( eye: Vec3, direction: Vec3, up: Vec3, ) -> Result<Mat4, MatrixError>

Checked left-handed view along direction (camera +Z). Uses the finite/degeneracy rules of Self::try_look_at_rh.

use ferravex_math::{Mat4, Vec3};
assert_eq!(Mat4::try_look_to_lh(Vec3::ZERO, Vec3::Z, Vec3::Y).unwrap(), Mat4::IDENTITY);
Source

pub fn perspective_rh_gl(fov_y: f32, aspect: f32, near: f32, far: f32) -> Mat4

Right-handed perspective with OpenGL depth [-1, 1].

Source

pub fn perspective_rh(fov_y: f32, aspect: f32, near: f32, far: f32) -> Mat4

Right-handed perspective with WebGPU/Vulkan/Metal depth [0, 1].

Source

pub fn perspective_lh(fov_y: f32, aspect: f32, near: f32, far: f32) -> Mat4

Source

pub fn perspective_infinite_rh(fov_y: f32, aspect: f32, near: f32) -> Mat4

Source

pub fn perspective_infinite_reverse_rh( fov_y: f32, aspect: f32, near: f32, ) -> Mat4

Source

pub fn orthographic_rh_gl( left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32, ) -> Mat4

Source

pub fn orthographic_rh( left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32, ) -> Mat4

Source

pub fn orthographic_lh( left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32, ) -> Mat4

Source

pub fn try_perspective_rh_gl( fov_y: f32, aspect: f32, near: f32, far: f32, ) -> Result<Mat4, MatrixError>

Checked right-handed perspective with OpenGL depth [-1, 1].

All arguments must be finite, 0 < fov_y < PI, aspect > 0, and 0 < near < far. Uses f64 intermediates; returns a diagnostic error instead of panicking. Rejects non-finite output and required scale/depth coefficients that round to zero. Success does not guarantee good conditioning or that the inverse is representable in f32.

use ferravex_math::{Mat4, MatrixError, vec3};
let p = Mat4::try_perspective_rh_gl(1.0, 1.5, 0.5, 100.0)?;
assert!((p.try_project_point3(vec3(0.0, 0.0, -0.5))?.z + 1.0).abs() < 1.0e-6);
Source

pub fn try_perspective_rh( fov_y: f32, aspect: f32, near: f32, far: f32, ) -> Result<Mat4, MatrixError>

Checked right-handed perspective with depth [0, 1]. Uses the input/range rules of Self::try_perspective_rh_gl.

use ferravex_math::{Mat4, MatrixError};
assert_eq!(Mat4::try_perspective_rh(1.0, 0.0, 0.1, 100.0),
           Err(MatrixError::InvalidPerspective { parameter: "aspect" }));
Source

pub fn try_perspective_lh( fov_y: f32, aspect: f32, near: f32, far: f32, ) -> Result<Mat4, MatrixError>

Checked left-handed perspective with depth [0, 1]. Uses the input/range rules of Self::try_perspective_rh_gl.

use ferravex_math::{Mat4, MatrixError, vec3};
let p = Mat4::try_perspective_lh(1.0, 1.0, 0.5, 10.0)?;
assert!(p.try_project_point3(vec3(0.0, 0.0, 0.5))?.z.abs() < 1.0e-6);
Source

pub fn try_perspective_infinite_rh( fov_y: f32, aspect: f32, near: f32, ) -> Result<Mat4, MatrixError>

Checked right-handed infinite perspective: near maps to 0, infinity to 1. Uses Self::try_perspective_rh_gl’s rules without a far argument. A finite positive near is validated directly, without computing near * 2.

use ferravex_math::Mat4;
assert!(Mat4::try_perspective_infinite_rh(1.0, 1.0, f32::MAX).is_ok());
Source

pub fn try_perspective_infinite_reverse_rh( fov_y: f32, aspect: f32, near: f32, ) -> Result<Mat4, MatrixError>

Checked right-handed infinite reverse depth: near maps to 1, infinity to 0. Uses the input/range rules of Self::try_perspective_infinite_rh.

use ferravex_math::{Mat4, MatrixError, vec3};
let p = Mat4::try_perspective_infinite_reverse_rh(1.0, 1.0, 0.5)?;
assert_eq!(p.try_project_point3(vec3(0.0, 0.0, -0.5))?.z, 1.0);
Source

pub fn try_orthographic_rh_gl( left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32, ) -> Result<Mat4, MatrixError>

Checked right-handed orthographic projection with OpenGL depth [-1, 1].

Requires finite bounds and unequal endpoints on each axis. Reversed ranges and negative near/far are allowed. Uses f64 intermediates and reports output overflow or a scale rounded to zero instead of panicking. Success does not guarantee that the inverse fits in f32: even a scale rounded to a nonzero subnormal can have an overflowing reciprocal.

use ferravex_math::{Mat4, MatrixError, vec3};
let p = Mat4::try_orthographic_rh_gl(-2.0, 2.0, -1.0, 1.0, 0.5, 10.0)?;
assert!(p.try_project_point3(vec3(-2.0, -1.0, -0.5))?
            .abs_diff_eq(vec3(-1.0, -1.0, -1.0), 1.0e-6));
let wide = Mat4::try_orthographic_rh_gl(
    -f32::MAX, f32::MAX, -1.0, 1.0, 0.0, 1.0)?;
assert_eq!(wide.checked_inverse(), Err(MatrixError::ResultOutOfRange));
Source

pub fn try_orthographic_rh( left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32, ) -> Result<Mat4, MatrixError>

Checked right-handed orthographic projection with depth [0, 1]. Uses the input/range rules of Self::try_orthographic_rh_gl.

use ferravex_math::{Mat4, MatrixError};
assert_eq!(Mat4::try_orthographic_rh(0.0, 0.0, -1.0, 1.0, 0.1, 10.0),
           Err(MatrixError::DegenerateOrthographic { axis: "x" }));
Source

pub fn try_orthographic_lh( left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32, ) -> Result<Mat4, MatrixError>

Checked left-handed orthographic projection with depth [0, 1]. Uses the input/range rules of Self::try_orthographic_rh_gl.

use ferravex_math::{Mat4, MatrixError, vec3};
let p = Mat4::try_orthographic_lh(-1.0, 1.0, -1.0, 1.0, 0.5, 10.0)?;
assert!(p.try_project_point3(vec3(0.0, 0.0, 0.5))?.z.abs() < 1.0e-6);
Source

pub fn try_to_scale_rotation_translation(self) -> Option<(Vec3, Quat, Vec3)>

Extracts TRS from an affine matrix with orthogonal, nonzero scale axes. Shear and perspective are rejected rather than approximated.

Source

pub fn to_scale_rotation_translation(self) -> (Vec3, Quat, Vec3)

Trait Implementations§

Source§

impl Add for Mat4

Source§

type Output = Mat4

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Clone for Mat4

Source§

fn clone(&self) -> Mat4

Returns a duplicate 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 Mat4

Source§

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

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

impl Default for Mat4

Source§

fn default() -> Mat4

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

impl Index<usize> for Mat4

Source§

type Output = Vec4

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Vec4

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

impl IndexMut<usize> for Mat4

Source§

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

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

impl Mul<Vec4> for Mat4

Source§

type Output = Vec4

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Vec4) -> Vec4

Performs the * operation. Read more
Source§

impl Mul<f32> for Mat4

Source§

type Output = Mat4

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul for Mat4

Source§

type Output = Mat4

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Mat4) -> Mat4

Performs the * operation. Read more
Source§

impl MulAssign for Mat4

Source§

fn mul_assign(&mut self, rhs: Mat4)

Performs the *= operation. Read more
Source§

impl PartialEq for Mat4

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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 Sub for Mat4

Source§

type Output = Mat4

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Copy for Mat4

Source§

impl StructuralPartialEq for Mat4

Auto Trait Implementations§

§

impl Freeze for Mat4

§

impl RefUnwindSafe for Mat4

§

impl Send for Mat4

§

impl Sync for Mat4

§

impl Unpin for Mat4

§

impl UnwindSafe for Mat4

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, 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.