spatial
On this page
Fixed-size math for games and graphics: vectors, quaternions, column-major matrices, affine transforms and simple shapes.
Every type is generic over its component: Vec3<T>, Quat<T>,
Mat4<T>, with T = f32 when left out, so Vec3 is Vec3<f32>.
DVec3, DQuat, DMat4, ... name the f64 forms; IVec3 and
UVec3 the i32 and u32 vectors. Every type is #[repr(C)] with
its components in order, so it goes into a GPU buffer or across the C
ABI as it is.
Conventions: column vectors (m * v), matrices stored column by
column as GPUs read them, right-handed unless a name says _lh,
angles in radians. Projections come in four forms each: _rh / _lh
for the handedness of view space, and depth 0..1 (Metal, Direct3D,
Vulkan, WebGPU) or, with _gl, -1..1 (OpenGL).
Modules
shapeShapes for culling, picking and collision tests: boxes, rays, planes and spheres.
Re-exports
shape::{Aabb, Plane, Ray, Sphere}Source
pub use shape::{Aabb, Plane, Ray, Sphere}Structs
Affine2Source
pub struct Affine2<T = f32>A 2D affine transform: matrix * p + translation. Smaller and
cheaper than a Mat3 with a fixed last row. #[repr(C)]: the
Mat2 then the Vec2, six Ts.
use spatial::{Affine2, Vec2};
let a = Affine2::from_scale_angle_translation(Vec2::ONE, f32::PI / 2.0, Vec2::new(1.0, 0.0));
assert(a.transform_point(Vec2::X).approx_eq(Vec2::new(1.0, 1.0), 1e-6));
The transform of a matrix and a translation.
nabiuse spatial::{Affine2, Mat2, Vec2}; assert(Affine2::new(Mat2::IDENTITY, Vec2::ZERO) == Affine2::identity());No change.
nabiuse spatial::{Affine2, Vec2}; assert(Affine2::identity().transform_point(Vec2::ONE) == Vec2::ONE);The translation by
t.nabiuse spatial::{Affine2, Vec2}; assert(Affine2::from_translation(Vec2::X).transform_point(Vec2::ZERO) == Vec2::X);The scale by
s.nabiuse spatial::{Affine2, Vec2}; assert(Affine2::from_scale(Vec2::splat(2.0)).transform_point(Vec2::ONE) == Vec2::splat(2.0));The counterclockwise rotation by
angleradians.nabiuse spatial::{Affine2, Vec2}; assert(Affine2::from_angle(f32::PI).transform_point(Vec2::X).approx_eq(-Vec2::X, 1e-6));Scale, rotate by
angleradians, then move byt.nabiuse spatial::{Affine2, Vec2}; let a = Affine2::from_scale_angle_translation(Vec2::splat(3.0), 0.0, Vec2::Y); assert(a.transform_point(Vec2::X) == Vec2::new(3.0, 1.0));The affine part of a 2D homogeneous
Mat3(its last row ignored).nabiuse spatial::{Affine2, Mat3, Vec2}; assert(Affine2::from_mat3(Mat3::from_translation(Vec2::X)) == Affine2::from_translation(Vec2::X));The same transform as a homogeneous
Mat3.nabiuse spatial::{Affine2, Mat3}; assert(Affine2::<f32>::identity().to_mat3() == Mat3::IDENTITY);The point
ptransformed.nabiuse spatial::{Affine2, Vec2}; assert(Affine2::from_translation(Vec2::X).transform_point(Vec2::Y) == Vec2::ONE);The direction
vtransformed, translation left out.nabiuse spatial::{Affine2, Vec2}; assert(Affine2::from_translation(Vec2::X).transform_vector(Vec2::Y) == Vec2::Y);The inverse transform. A singular matrix gives infinite or NaN entries.
nabiuse spatial::{DAffine2, DVec2}; let a = DAffine2::from_scale_angle_translation(DVec2::splat(2.0), 0.5, DVec2::ONE); assert((a * a.inverse()).transform_point(DVec2::X).approx_eq(DVec2::X, 1e-12));Whether the matrix and translation are within
epsofo's.nabiuse spatial::Affine2; assert(Affine2::identity().approx_eq(Affine2::from_angle(1e-7), 1e-6));
Trait implementations 1
impl<T: Real> Mul for Affine2<T>
Affine3Source
pub struct Affine3<T = f32>A 3D affine transform: matrix * p + translation, any mix of
rotation, scale, shear and translation without the projective row of
a Mat4. #[repr(C)]: the Mat3 then the Vec3, twelve Ts.
use spatial::{Affine3, Quat, Vec3};
let a = Affine3::from_scale_rotation_translation(Vec3::ONE, Quat::from_rotation_y(f32::PI / 2.0), Vec3::Y);
assert(a.transform_point(Vec3::Z).approx_eq(Vec3::new(1.0, 1.0, 0.0), 1e-6));
The transform of a matrix and a translation.
nabiuse spatial::{Affine3, Mat3, Vec3}; assert(Affine3::new(Mat3::IDENTITY, Vec3::ZERO) == Affine3::identity());No change.
nabiuse spatial::{Affine3, Vec3}; assert(Affine3::identity().transform_point(Vec3::ONE) == Vec3::ONE);The translation by
t.nabiuse spatial::{Affine3, Vec3}; assert(Affine3::from_translation(Vec3::X).transform_point(Vec3::ZERO) == Vec3::X);The scale by
s.nabiuse spatial::{Affine3, Vec3}; assert(Affine3::from_scale(Vec3::splat(2.0)).transform_point(Vec3::ONE) == Vec3::splat(2.0));The rotation of a unit quaternion.
nabiuse spatial::{Affine3, Quat, Vec3}; let q = Quat::from_rotation_z(0.3); assert(Affine3::from_quat(q).transform_point(Vec3::X).approx_eq(q * Vec3::X, 1e-6));The rotation by
angleradians about the unit vectoraxis.nabiuse spatial::{Affine3, Vec3}; let a = Affine3::from_axis_angle(Vec3::Z, f32::PI / 2.0); assert(a.transform_point(Vec3::X).approx_eq(Vec3::Y, 1e-6));
Sourcepub fn from_scale_rotation_translation(§scale: Vec3<T>, rotation: Quat<T>, translation: Vec3<T>) -> Affine3<T> Scale, rotate, then move.
nabiuse spatial::{Affine3, Quat, Vec3}; let a = Affine3::from_scale_rotation_translation(Vec3::splat(2.0), Quat::IDENTITY, Vec3::X); assert(a.transform_point(Vec3::ONE) == Vec3::new(3.0, 2.0, 2.0));The affine part of
m(its last row ignored).nabiuse spatial::{Affine3, Mat4, Vec3}; assert(Affine3::from_mat4(Mat4::from_translation(Vec3::X)) == Affine3::from_translation(Vec3::X));The same transform as a
Mat4.nabiuse spatial::{Affine3, Mat4}; assert(Affine3::<f32>::identity().to_mat4() == Mat4::IDENTITY);The point
ptransformed.nabiuse spatial::{Affine3, Vec3}; assert(Affine3::from_translation(Vec3::X).transform_point(Vec3::Y) == Vec3::new(1.0, 1.0, 0.0));The direction
vtransformed, translation left out.nabiuse spatial::{Affine3, Vec3}; assert(Affine3::from_translation(Vec3::X).transform_vector(Vec3::Y) == Vec3::Y);The inverse transform. A singular matrix gives infinite or NaN entries.
nabiuse spatial::{DAffine3, DQuat, DVec3}; let a = DAffine3::from_scale_rotation_translation(DVec3::splat(2.0), DQuat::from_rotation_x(1.0), DVec3::ONE); assert((a.inverse() * a).approx_eq(DAffine3::identity(), 1e-12));Whether the matrix and translation are within
epsofo's.nabiuse spatial::{Affine3, Vec3}; assert(Affine3::identity().approx_eq(Affine3::from_translation(Vec3::splat(1e-7)), 1e-6));
Trait implementations 1
impl<T: Real> Mul for Affine3<T>
Mat2Source
pub struct Mat2<T = f32>A 2x2 matrix of two column vectors, for 2D rotation and scale.
Mat2 alone is Mat2<f32>. #[repr(C)]: x_axis then y_axis,
four Ts column by column.
use spatial::{Mat2, Vec2};
let m = Mat2::from_cols(Vec2::new(1.0, 2.0), Vec2::new(3.0, 4.0));
assert(m * Vec2::new(1.0, 0.0) == Vec2::new(1.0, 2.0));
assert(m.determinant() == -2.0);
The matrix of its columns.
nabiuse spatial::{Mat2, Vec2}; assert(Mat2::from_cols(Vec2::X, Vec2::Y) == Mat2::IDENTITY);From four values, column by column.
nabiassert(spatial::DMat2::from_cols_array([1.0, 0.0, 0.0, 1.0]) == spatial::DMat2::IDENTITY);The four values column by column, as a GPU reads them.
nabiassert(spatial::Mat2::IDENTITY.to_cols_array() == [1.0, 0.0, 0.0, 1.0]);The identity.
nabiassert(spatial::Mat2::<f32>::identity() == spatial::Mat2::IDENTITY);All zero.
nabiassert(spatial::Mat2::<f32>::zero().determinant() == 0.0);The diagonal matrix of
d, a scale.nabiuse spatial::{Mat2, Vec2}; assert(Mat2::from_diagonal(Vec2::new(2.0, 3.0)) * Vec2::ONE == Vec2::new(2.0, 3.0));The counterclockwise rotation by
angleradians.nabiuse spatial::{Mat2, Vec2}; assert((Mat2::from_angle(f32::PI / 2.0) * Vec2::X).approx_eq(Vec2::Y, 1e-6));Scale by
scale, then rotate byangleradians.nabiuse spatial::{Mat2, Vec2}; let m = Mat2::from_scale_angle(Vec2::splat(2.0), f32::PI / 2.0); assert((m * Vec2::X).approx_eq(Vec2::new(0.0, 2.0), 1e-6));Column
i(0 or 1).nabiassert(spatial::Mat2::IDENTITY.col(1) == spatial::Vec2::Y);Row
i(0 or 1).nabiuse spatial::{Mat2, Vec2}; let m = Mat2::from_cols_array([1.0, 2.0, 3.0, 4.0]); assert(m.row(0) == Vec2::new(1.0, 3.0));The transpose.
nabilet m = spatial::Mat2::from_cols_array([1.0, 2.0, 3.0, 4.0]); assert(m.transpose().to_cols_array() == [1.0, 3.0, 2.0, 4.0]);The determinant.
nabiassert(spatial::Mat2::from_cols_array([1.0, 2.0, 3.0, 4.0]).determinant() == -2.0);The inverse. A singular matrix gives infinite or NaN entries;
try_inversegivesnonefor it instead.nabiuse spatial::DMat2; let m = DMat2::from_cols_array([1.0, 2.0, 3.0, 4.0]); assert((m * m.inverse()).approx_eq(DMat2::IDENTITY, 1e-12));The inverse, or
nonewhen the determinant is zero or the result is not finite.nabiassert(spatial::Mat2::<f32>::zero().try_inverse() == none);Whether every entry is within
epsofo's.nabiuse spatial::Mat2; assert(Mat2::IDENTITY.approx_eq(Mat2::from_angle(1e-7), 1e-6));Whether no entry is NaN or infinite.
nabiassert(spatial::Mat2::IDENTITY.is_finite());
Mat3
pub struct Mat3<T = f32>Mat4
pub struct Mat4<T = f32>Quat
pub struct Quat<T = f32>Transform
pub struct Transform<T = f32>Vec2
pub struct Vec2<T = f32>Vec3
pub struct Vec3<T = f32>Vec3ASource
pub struct Vec3A<T = f32>A Vec3 stored in 16 bytes, aligned to 16, as a vec3<f32> sits in
a WGSL uniform or std140 / std430 buffer: a struct of Vec3As
matches the shader's layout with no hand-written padding. Convert
with Vec3A::from(v) and .to_vec3(); the arithmetic is Vec3's.
use spatial::{Vec3, Vec3A};
let a = Vec3A::from(Vec3::new(1.0, 2.0, 3.0));
assert(Vec3A::<f32>::size() == 16 && Vec3A::<f32>::align() == 16);
assert((a + a).to_vec3() == Vec3::new(2.0, 4.0, 6.0));
The vector of its components.
nabiassert(spatial::Vec3A::new(1.0, 2.0, 3.0).z == 3.0);The padded form of
v.nabiuse spatial::{Vec3, Vec3A}; assert(Vec3A::from(Vec3::<f32>::X).x == 1.0);The unpadded
Vec3.nabiuse spatial::{Vec3, Vec3A}; assert(Vec3A::new(1.0, 2.0, 3.0).to_vec3() == Vec3::new(1.0, 2.0, 3.0));The dot product.
nabiassert(spatial::Vec3A::new(1.0, 2.0, 3.0).dot(spatial::Vec3A::new(1.0, 1.0, 1.0)) == 6.0);The cross product, as
Vec3::cross.nabiuse spatial::{Vec3, Vec3A}; assert(Vec3A::from(Vec3::<f32>::X).cross(Vec3A::from(Vec3::Y)).to_vec3() == Vec3::Z);The length.
nabiassert(spatial::Vec3A::new(3.0, 4.0, 0.0).length() == 5.0);The unit vector, as
Vec3::normalize.nabiassert(spatial::Vec3A::new(0.0, 0.0, 2.0).normalize().z == 1.0);
Vec4
pub struct Vec4<T = f32>Enums
EulerOrderSource
pub enum EulerOrderThe axes of three Euler angles a, b, c, first to last. With
XYZ the rotation is R_x(a) * R_y(b) * R_z(c): on a column vector
c about z applies first, then b about y, then a about x. Read
as moving axes, it is a about x, then b about the turned y, then
c about the twice-turned z (intrinsic X-Y'-Z'').
For a y-up, -z-forward camera or character, YXZ is yaw, pitch,
roll; for z-up worlds (aircraft, robotics), ZYX is.
use spatial::{DMat3, EulerOrder};
let m = DMat3::from_euler(EulerOrder::XYZ, 0.1, 0.2, 0.3);
let want = DMat3::from_rotation_x(0.1) * DMat3::from_rotation_y(0.2) * DMat3::from_rotation_z(0.3);
assert(m.approx_eq(want, 1e-12));
The axis indices (0 = x, 1 = y, 2 = z), first to last.
nabiassert(spatial::EulerOrder::YXZ.axes() == (1, 0, 2));
Traits
RealSource
pub trait Real: Scalar + FloatingPointA float component type, f32 or f64: what lengths, normals,
rotations, matrices and shapes take.
use spatial::{DVec3, Real, Vec3, vec3};
fn unit<T: Real>(v: Vec3<T>) -> Vec3<T> { v.normalize() }
assert(unit(vec3(0.0, 0.0, 2.0)) == Vec3::Z);
assert(unit(DVec3::new(0.0, 3.0, 0.0)) == DVec3::Y);
ScalarSource
pub trait Scalar: Num + SimdElement + Debug + DisplayA component type: f32, f64, i32 or u32. The arithmetic of
IVec3 and UVec3 is the integer type's, so u32 components that
go below zero panic as u32 subtraction does.
use spatial::{IVec2, Scalar, Vec2};
fn area<T: Scalar>(size: Vec2<T>) -> T { size.x * size.y }
assert(area(IVec2::new(3, 4)) == 12);
assert(area(Vec2::new(1.5, 2.0)) == 3.0);
Type aliases
DAffine2Source
pub type DAffine2 = Affine2<f64>Affine2<f64>.
assert(spatial::DAffine2::identity().translation == spatial::DVec2::ZERO);
DAffine3Source
pub type DAffine3 = Affine3<f64>Affine3<f64>.
assert(spatial::DAffine3::identity().translation == spatial::DVec3::ZERO);
DMat2Source
pub type DMat2 = Mat2<f64>Mat2<f64>.
assert(spatial::DMat2::IDENTITY.determinant() == 1.0);
DMat3Source
pub type DMat3 = Mat3<f64>Mat3<f64>.
assert(spatial::DMat3::IDENTITY.determinant() == 1.0);
DMat4Source
pub type DMat4 = Mat4<f64>Mat4<f64>.
assert(spatial::DMat4::IDENTITY.determinant() == 1.0);
DQuatSource
pub type DQuat = Quat<f64>Quat<f64>.
assert(spatial::DQuat::IDENTITY.w == 1.0);
DTransformSource
pub type DTransform = Transform<f64>Transform<f64>.
assert(spatial::DTransform::identity().scale == spatial::DVec3::ONE);
DVec2Source
pub type DVec2 = Vec2<f64>Vec2<f64>.
assert(spatial::DVec2::new(1.0, 2.0).length_squared() == 5.0);
DVec3Source
pub type DVec3 = Vec3<f64>Vec3<f64>.
assert(spatial::DVec3::X.cross(spatial::DVec3::Y) == spatial::DVec3::Z);
DVec3ASource
pub type DVec3A = Vec3A<f64>Vec3A<f64>, 32 bytes.
assert(spatial::DVec3A::size() == 32);
DVec4Source
pub type DVec4 = Vec4<f64>Vec4<f64>.
assert(spatial::DVec4::ONE.element_sum() == 4.0);
IVec2Source
pub type IVec2 = Vec2<i32>Vec2<i32>, a grid position or a pixel size.
assert(spatial::IVec2::new(3, -4).dot(spatial::IVec2::ONE) == -1);
IVec3Source
pub type IVec3 = Vec3<i32>Vec3<i32>, a voxel or chunk coordinate.
assert(spatial::IVec3::new(1, 2, 3) * 2 == spatial::IVec3::new(2, 4, 6));
IVec4Source
pub type IVec4 = Vec4<i32>Vec4<i32>.
assert(spatial::IVec4::splat(2).element_product() == 16);
UVec2Source
pub type UVec2 = Vec2<u32>Vec2<u32>, a texture or window size.
assert(spatial::UVec2::new(1920, 1080).element_product() == 2073600);
UVec3Source
pub type UVec3 = Vec3<u32>Vec3<u32>, a compute dispatch size.
assert(spatial::UVec3::new(8, 8, 1).element_product() == 64);
UVec4Source
pub type UVec4 = Vec4<u32>Vec4<u32>, an RGBA8 colour widened, or four indices.
assert(spatial::UVec4::splat(255).max_element() == 255);
Functions
dvec2Source
pub const fn dvec2(x: f64, y: f64) -> DVec2 DVec2::new(x, y) with f64 components, whatever the literals.
use spatial::{DVec2, dvec2};
assert(dvec2(1.0, 2.0) == DVec2::new(1.0, 2.0));
dvec3Source
pub const fn dvec3(x: f64, y: f64, z: f64) -> DVec3 DVec3::new(x, y, z) with f64 components, whatever the literals.
use spatial::{DVec3, dvec3};
assert(dvec3(1.0, 2.0, 3.0) == DVec3::new(1.0, 2.0, 3.0));
dvec4Source
pub const fn dvec4(x: f64, y: f64, z: f64, w: f64) -> DVec4 DVec4::new(x, y, z, w) with f64 components, whatever the literals.
use spatial::{DVec4, dvec4};
assert(dvec4(1.0, 2.0, 3.0, 4.0) == DVec4::new(1.0, 2.0, 3.0, 4.0));
ivec2Source
pub const fn ivec2(x: i32, y: i32) -> IVec2 IVec2::new(x, y) with i32 components, whatever the literals.
use spatial::{IVec2, ivec2};
assert(ivec2(1, -2) == IVec2::new(1, -2));
ivec3Source
pub const fn ivec3(x: i32, y: i32, z: i32) -> IVec3 IVec3::new(x, y, z) with i32 components, whatever the literals.
use spatial::{IVec3, ivec3};
assert(ivec3(1, -2, 3) == IVec3::new(1, -2, 3));
ivec4Source
pub const fn ivec4(x: i32, y: i32, z: i32, w: i32) -> IVec4 IVec4::new(x, y, z, w) with i32 components, whatever the literals.
use spatial::{IVec4, ivec4};
assert(ivec4(1, -2, 3, 4) == IVec4::new(1, -2, 3, 4));
uvec2Source
pub const fn uvec2(x: u32, y: u32) -> UVec2 UVec2::new(x, y) with u32 components, whatever the literals.
use spatial::{UVec2, uvec2};
assert(uvec2(1, 2) == UVec2::new(1, 2));
uvec3Source
pub const fn uvec3(x: u32, y: u32, z: u32) -> UVec3 UVec3::new(x, y, z) with u32 components, whatever the literals.
use spatial::{UVec3, uvec3};
assert(uvec3(1, 2, 3) == UVec3::new(1, 2, 3));
uvec4Source
pub const fn uvec4(x: u32, y: u32, z: u32, w: u32) -> UVec4 UVec4::new(x, y, z, w) with u32 components, whatever the literals.
use spatial::{UVec4, uvec4};
assert(uvec4(1, 2, 3, 4) == UVec4::new(1, 2, 3, 4));
vec2Source
pub const fn vec2(x: f32, y: f32) -> Vec2 Vec2::new(x, y) with f32 components, whatever the literals.
use spatial::{Vec2, vec2};
assert(vec2(1.0, 2.0) == Vec2::<f32>::new(1.0, 2.0));
vec3Source
pub const fn vec3(x: f32, y: f32, z: f32) -> Vec3 Vec3::new(x, y, z) with f32 components, whatever the literals.
use spatial::{Vec3, vec3};
assert(vec3(1.0, 2.0, 3.0) == Vec3::<f32>::new(1.0, 2.0, 3.0));
vec4Source
pub const fn vec4(x: f32, y: f32, z: f32, w: f32) -> Vec4 Vec4::new(x, y, z, w) with f32 components, whatever the literals.
use spatial::{Vec4, vec4};
assert(vec4(1.0, 2.0, 3.0, 4.0) == Vec4::<f32>::new(1.0, 2.0, 3.0, 4.0));