ferravex
GitHub

ferravex-math

Original scalar Rust mathematics for Ferravex. No third-party packages, copied library implementations, platform FFI, unsafe code, or generated source. The library uses only Rust's standard library. Types have public fields and predictable #[repr(C)] layouts; no SIMD alignment or serialized memory-layout compatibility is promised.

use ferravex_math::{Mat4, Quat, Vec3, vec3};
let model = Mat4::from_scale_rotation_translation(
    Vec3::splat(2.0),
    Quat::from_rotation_y(0.5),
    vec3(4.0, 0.0, -2.0),
);
let world = model.transform_point3(Vec3::X);
let local = model.try_inverse().unwrap().transform_point3(world);
assert!(local.abs_diff_eq(Vec3::X, 0.00001));

Conventions

Degenerate inputs and precision

try_normalize returns None for zero or non-finite vectors. It accepts finite subnormal and extreme-magnitude vectors using wider intermediate arithmetic. normalize_or_zero / normalize_or provide explicit fallbacks. Unchecked normalize returns NaNs for invalid input. A zero vector remains zero when clamping to a minimum length because it has no direction.

Quat::try_inverse accepts any finite nonzero quaternion whose inverse fits in f32. Quaternion inverse is an algebraic inverse, not just conjugation. Slerp normalizes its endpoints and chooses the shortest rotation, including equivalent q/-q endpoints. A zero-length rotation direction or degenerate look-at basis propagates NaNs; it does not invent a camera orientation.

Mat4::try_inverse performs general Gauss-Jordan inversion with partial pivoting and f64 intermediates. Singular matrices, non-finite inputs, and results outside f32 range return None; inverse returns a NaN matrix in those cases. Ill-conditioned but invertible matrices are not rejected by an arbitrary epsilon, so applications must still account for floating-point error. try_to_scale_rotation_translation rejects zero scale, non-affine matrices, and shear; reflected TRS transforms are supported.

Ordinary vector arithmetic, dot/cross, matrix multiplication, and output storage use f32. No cross-platform bitwise determinism, SIMD speedup, complete glam API compatibility, or GPU ABI compatibility is claimed. Geometric tests use numerical tolerances.

Validation

cargo test -p ferravex-math
cargo clippy -p ferravex-math --all-targets -- -D warnings
cargo check -p ferravex-math --target wasm32-unknown-unknown

Tests cover vector arithmetic/swizzles, extreme normalization, cross-product orthogonality, reflection, quaternion composition/inverse and antipodal interpolation, column layout, affine point/vector semantics, general matrix inversion/pivoting, perspective and orthographic near/far planes, view transforms, degenerate inputs, strict rectangle contact, and 256 deterministic randomized TRS round trips including mirrored scale.