#[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: Vec4Implementations§
Source§impl Mat4
impl Mat4
pub const IDENTITY: Mat4
pub const ZERO: Mat4
pub const NAN: Mat4
pub const fn from_cols( x_axis: Vec4, y_axis: Vec4, z_axis: Vec4, w_axis: Vec4, ) -> Mat4
pub const fn from_cols_array(v: &[f32; 16]) -> Mat4
pub const fn from_cols_array_2d(v: &[[f32; 4]; 4]) -> Mat4
pub const fn to_cols_array(self) -> [f32; 16]
pub const fn to_cols_array_2d(self) -> [[f32; 4]; 4]
pub fn col(self, index: usize) -> Vec4
pub fn row(self, index: usize) -> Vec4
pub fn transpose(self) -> Mat4
pub const fn from_translation(v: Vec3) -> Mat4
pub const fn from_scale(v: Vec3) -> Mat4
pub const fn from_diagonal(v: Vec4) -> Mat4
Sourcepub fn from_quat(rotation: Quat) -> Mat4
pub fn from_quat(rotation: Quat) -> Mat4
The quaternion is normalized to avoid embedding accidental scale.
pub fn from_axis_angle(axis: Vec3, angle: f32) -> Mat4
pub fn from_rotation_x(angle: f32) -> Mat4
pub fn from_rotation_y(angle: f32) -> Mat4
pub fn from_rotation_z(angle: f32) -> Mat4
pub fn from_rotation_translation(rotation: Quat, translation: Vec3) -> Mat4
pub fn from_scale_rotation_translation( scale: Vec3, rotation: Quat, translation: Vec3, ) -> Mat4
pub fn transform_point3(self, v: Vec3) -> Vec3
pub fn transform_vector3(self, v: Vec3) -> Vec3
pub fn project_point3(self, v: Vec3) -> Vec3
Sourcepub fn try_project_point3(self, v: Vec3) -> Result<Vec3, MatrixError>
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));pub fn mul_vec4(self, v: Vec4) -> Vec4
pub fn mul_mat4(self, rhs: Mat4) -> Mat4
pub fn is_finite(self) -> bool
pub fn is_nan(self) -> bool
pub fn abs_diff_eq(self, rhs: Mat4, tolerance: f32) -> bool
Sourcepub fn determinant(self) -> f32
pub fn determinant(self) -> f32
Partial-pivot Gaussian elimination with f64 intermediates.
Sourcepub fn try_inverse(self) -> Option<Mat4>
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.
Sourcepub fn checked_inverse(self) -> Result<Mat4, MatrixError>
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" }));pub fn inverse(self) -> Mat4
Sourcepub fn look_at_rh(eye: Vec3, target: Vec3, up: Vec3) -> Mat4
pub fn look_at_rh(eye: Vec3, target: Vec3, up: Vec3) -> Mat4
Right-handed view: the camera looks along its negative Z axis.
pub fn look_to_rh(eye: Vec3, direction: Vec3, up: Vec3) -> Mat4
pub fn look_at_lh(eye: Vec3, target: Vec3, up: Vec3) -> Mat4
pub fn look_to_lh(eye: Vec3, direction: Vec3, up: Vec3) -> Mat4
Sourcepub fn try_look_at_rh(
eye: Vec3,
target: Vec3,
up: Vec3,
) -> Result<Mat4, MatrixError>
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));Sourcepub fn try_look_to_rh(
eye: Vec3,
direction: Vec3,
up: Vec3,
) -> Result<Mat4, MatrixError>
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);Sourcepub fn try_look_at_lh(
eye: Vec3,
target: Vec3,
up: Vec3,
) -> Result<Mat4, MatrixError>
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);Sourcepub fn try_look_to_lh(
eye: Vec3,
direction: Vec3,
up: Vec3,
) -> Result<Mat4, MatrixError>
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);Sourcepub fn perspective_rh_gl(fov_y: f32, aspect: f32, near: f32, far: f32) -> Mat4
pub fn perspective_rh_gl(fov_y: f32, aspect: f32, near: f32, far: f32) -> Mat4
Right-handed perspective with OpenGL depth [-1, 1].
Sourcepub fn perspective_rh(fov_y: f32, aspect: f32, near: f32, far: f32) -> Mat4
pub fn perspective_rh(fov_y: f32, aspect: f32, near: f32, far: f32) -> Mat4
Right-handed perspective with WebGPU/Vulkan/Metal depth [0, 1].
pub fn perspective_lh(fov_y: f32, aspect: f32, near: f32, far: f32) -> Mat4
pub fn perspective_infinite_rh(fov_y: f32, aspect: f32, near: f32) -> Mat4
pub fn perspective_infinite_reverse_rh( fov_y: f32, aspect: f32, near: f32, ) -> Mat4
pub fn orthographic_rh_gl( left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32, ) -> Mat4
pub fn orthographic_rh( left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32, ) -> Mat4
pub fn orthographic_lh( left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32, ) -> Mat4
Sourcepub fn try_perspective_rh_gl(
fov_y: f32,
aspect: f32,
near: f32,
far: f32,
) -> Result<Mat4, MatrixError>
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);Sourcepub fn try_perspective_rh(
fov_y: f32,
aspect: f32,
near: f32,
far: f32,
) -> Result<Mat4, MatrixError>
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" }));Sourcepub fn try_perspective_lh(
fov_y: f32,
aspect: f32,
near: f32,
far: f32,
) -> Result<Mat4, MatrixError>
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);Sourcepub fn try_perspective_infinite_rh(
fov_y: f32,
aspect: f32,
near: f32,
) -> Result<Mat4, MatrixError>
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());Sourcepub fn try_perspective_infinite_reverse_rh(
fov_y: f32,
aspect: f32,
near: f32,
) -> Result<Mat4, MatrixError>
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);Sourcepub fn try_orthographic_rh_gl(
left: f32,
right: f32,
bottom: f32,
top: f32,
near: f32,
far: f32,
) -> Result<Mat4, MatrixError>
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));Sourcepub fn try_orthographic_rh(
left: f32,
right: f32,
bottom: f32,
top: f32,
near: f32,
far: f32,
) -> Result<Mat4, MatrixError>
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" }));Sourcepub fn try_orthographic_lh(
left: f32,
right: f32,
bottom: f32,
top: f32,
near: f32,
far: f32,
) -> Result<Mat4, MatrixError>
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);Sourcepub fn try_to_scale_rotation_translation(self) -> Option<(Vec3, Quat, Vec3)>
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.
pub fn to_scale_rotation_translation(self) -> (Vec3, Quat, Vec3)
Trait Implementations§
Source§impl MulAssign for Mat4
impl MulAssign for Mat4
Source§fn mul_assign(&mut self, rhs: Mat4)
fn mul_assign(&mut self, rhs: Mat4)
*= operation. Read more