Skip to main content

Crate algebrix

Crate algebrix 

Source
Expand description

Math for game engines: vectors, matrices, quaternions, geometry.

SIMD is used when the simd feature is on and the target is x86_64 or aarch64; otherwise scalar. Column vectors; multiply by matrices on the left. See README for feature flags.

§Modules

Vectors: vec2, vec3, vec4, ivec2ivec4, uvec2uvec4, bvec, dvec3. Matrices: mat2, mat3, mat4, dmat4. Quaternions: quat, dquat. Transforms: affine2, affine3. Geometry: geometry (plane, ray, AABB, frustum). Angles: angle (Rad, Deg, EulerRot). Utilities: utils. Optional interop: compat.

§Examples

Vectors and normalization:

use algebrix::{Vec3, Rad, Deg};

let v = Vec3::new(3.0, 4.0, 0.0);
assert!((v.length() - 5.0).abs() < 1e-5);
let u = v.normalize();
let half_turn = Rad::from(Deg::new(90.0));

Transform and quaternion:

use algebrix::{Mat4, Quat, Vec3};

let t = Mat4::from_translation(Vec3::new(1.0, 0.0, 0.0));
let p = t.transform_point3(Vec3::ZERO);
assert_eq!(p, Vec3::new(1.0, 0.0, 0.0));

let q = Quat::from_axis_angle(Vec3::Z, std::f32::consts::FRAC_PI_2);
let x_rotated = q * Vec3::X;
assert!((x_rotated - Vec3::Y).length() < 1e-5);

Re-exports§

pub use vec2::Vec2;
pub use vec3::Vec3;
pub use vec4::Vec4;
pub use ivec2::IVec2;
pub use ivec3::IVec3;
pub use ivec4::IVec4;
pub use uvec2::UVec2;
pub use uvec3::UVec3;
pub use uvec4::UVec4;
pub use bvec::BVec2;
pub use bvec::BVec3;
pub use bvec::BVec4;
pub use swizzles::Vec2Swizzles;
pub use swizzles::Vec3Swizzles;
pub use swizzles::Vec4Swizzles;
pub use mat2::Mat2;
pub use mat3::Mat3;
pub use mat4::Mat4;
pub use dmat4::DMat4;
pub use quat::Quat;
pub use dquat::DQuat;
pub use dvec3::DVec3;
pub use affine2::Affine2;
pub use affine3::Affine3;
pub use geometry::Plane;
pub use geometry::Ray;
pub use geometry::Aabb2;
pub use geometry::Aabb3;
pub use geometry::Frustum;
pub use angle::Rad;
pub use angle::Deg;
pub use angle::EulerRot;
pub use utils::*;

Modules§

affine2
2D affine transform: scale/rotation plus translation. No perspective; cheaper than a full 3x3 for 2D.
affine3
3D affine transform: scale/rotation plus translation. No perspective; cheaper than a full 4x4 for 3D.
angle
Type-safe angles: Rad and Deg, plus EulerRot for Euler order.
bvec
Boolean vectors (BVec2/3/4): component-wise masks for select, all, any.
compat
Optional interop with glam and nalgebra.
dmat4
4x4 column-major matrix in double precision (f64). Same API as Mat4; use as_mat4 to convert to float.
dquat
Unit quaternion in double precision (f64). Same API as Quat; use from_quat / as_quat to convert.
dvec3
3D float vector in double precision (f64). Use when you need more precision than Vec3; convert to/from Vec3 with from_vec3 / as_vec3.
geometry
Planes, rays, AABBs, and frustums for culling and spatial queries.
ivec2
2D signed integer vector (IVec2). Same layout as Vec2 but i32; use as_vec2 to convert to float.
ivec3
3D signed integer vector (IVec3). Same layout as Vec3 but i32; use as_vec3 to convert to float.
ivec4
4D signed integer vector (IVec4). Same layout as Vec4 but i32; use as_vec4 to convert to float.
mat2
2x2 column-major matrix. Used for 2D rotation, scale, or combined linear transform.
mat3
3x3 column-major matrix: 3D rotation, scale, or combined linear transform.
mat4
4x4 column-major matrix: 3D affine or projective transform.
quat
Unit quaternion for 3D rotation. Layout (x, y, z, w); multiply quats for combined rotation.
swizzles
Component reordering (swizzling): .xy(), .xyz(), .xx(), etc. on Vec2/3/4.
utils
Scalar math: constants (PI, TAU, etc.), lerp, clamp, clamp01, smoothstep, angle conversion, min/max, and f32_rsqrt (approximate 1/sqrt(x), SIMD when feature is on).
uvec2
2D unsigned integer vector (UVec2). Same layout as Vec2 but u32; use as_vec2 to convert to float.
uvec3
3D unsigned integer vector (UVec3). Same layout as Vec3 but u32; use as_vec3 to convert to float.
uvec4
4D unsigned integer vector (UVec4). Same layout as Vec4 but u32; use as_vec4 to convert to float.
vec2
2D float vector: positions, directions, UVs.
vec3
3D float vector: positions, directions, normals.
vec4
4D float vector: homogenous coords, quaternion-like (x,y,z,w), colors (RGBA).