Expand description
gemath — a small, type-safe math library.
The library’s differentiators are:
- Type-level units and type-level coordinate spaces on core math types (e.g.
Vec2<Unit, Space>). - A clear
no_stdposture (via feature flags). - Typed angles (
Radians/Degrees) and “checked / fallible” APIs where appropriate.
§Getting started
§1) Vectors, typed angles, and rotation
use gemath::{Degrees, Vec2};
let v: Vec2<(), ()> = Vec2::new(1.0, 0.0);
let r = v.rotate_deg(Degrees(90.0));
assert!((r - Vec2::new(0.0, 1.0)).length() < 1e-5);§2) Units and spaces at the type level
use gemath::{Meters, Vec2, World};
// A position in world space, measured in meters.
let p: Vec2<Meters, World> = Vec2::new(10.0, 20.0);
// Operations preserve the tags.
let q: Vec2<Meters, World> = p + Vec2::new(1.0, 2.0);
let _ = q;§3) Mat4 TRS composition (translation / rotation / scale)
use gemath::{Mat4, Quat, Radians, Vec3};
let t = Vec3::new(1.0, 2.0, 3.0);
let r: Quat<(), ()> = Quat::from_axis_angle_radians(
Vec3::new(0.0, 0.0, 1.0),
Radians(core::f32::consts::FRAC_PI_2),
);
let s = Vec3::new(2.0, 2.0, 2.0);
let m: Mat4<(), ()> = Mat4::from_trs(t, r, s);
let p = m.transform_point(Vec3::new(1.0, 0.0, 0.0));
// After a 90° rotation around Z and uniform scale, the x-axis point ends up on +Y (plus translation).
assert!((p - Vec3::new(1.0, 4.0, 3.0)).length() < 1e-5);§4) Structured raycast hits (representative example)
use gemath::{ray2_circle_cast, Circle, Ray2, Vec2};
let circle: Circle<(), ()> = Circle::new(Vec2::new(0.0, 0.0), 1.0);
let ray: Ray2<(), ()> = Ray2::new(Vec2::new(-2.0, 0.0), Vec2::new(1.0, 0.0));
let hit = ray2_circle_cast(ray, circle).unwrap();
assert!((hit.t - 1.0).abs() < 1e-6);
assert!((hit.point - Vec2::new(-1.0, 0.0)).length() < 1e-6);
assert!((hit.normal - Vec2::new(-1.0, 0.0)).length() < 1e-6);§5) Allocation-backed primitives behind features (Polygon2)
use gemath::{Polygon2, Vec2};
let poly: Polygon2<(), ()> = Polygon2::new(vec![
Vec2::new(0.0, 0.0),
Vec2::new(1.0, 0.0),
Vec2::new(1.0, 1.0),
Vec2::new(0.0, 1.0),
]);
assert!(poly.contains_point(Vec2::new(0.5, 0.5)));§Feature docs
- Build/test matrix: see
docs/DEVELOPMENT.md - Feature dependency graph / minimal builds: see
docs/FEATURES.md
§Cargo features
serde: Enablesserde::Serialize/serde::Deserializefor core types.std(default): Use Rust standard library.libm: Use thelibmcrate for floating-point math when building withoutstd.alloc: Enables allocation-backed helpers (e.g.Vecconversions) when building withoutstd.math: Internal capability used bystd/libmbuilds (float math backend).
§Scalar type / precision
Core types are currently f32-based. The public alias Real documents the chosen scalar.
§“No-Std / No-Alloc / No-Macro” (precise)
- No-Std: Disable default features. In
no_stdmode, floating-point math requireslibm. - No-Alloc: In
no_stdmode, allocation is opt-in via theallocfeature. Modules that require allocation are gated accordingly (e.g.spatial). - No-Macro: The minimal
no_stdbuild (--no-default-features --features libm) does not require procedural macros. Enablingserdeusesserde_derive(proc macros) by design.
Re-exports§
pub use aabb2::Aabb2;pub use aabb3::Aabb3;pub use markers::Local;pub use markers::Meters;pub use markers::Pixels;pub use markers::Screen;pub use markers::World;pub use unit_vec::UnitVec2;pub use unit_vec::UnitVec3;pub use unit_vec::UnitVec4;pub use half_float::Half;pub use half_float::f16s_from_half_array;pub use half_float::f32s_from_half_array;pub use half_float::halfs_from_f16_array;pub use half_float::halfs_from_f32_array;pub use half_float::f16s_from_half_slice;pub use half_float::f32s_from_half_slice;pub use half_float::halfs_from_f16_slice;pub use half_float::halfs_from_f32_slice;pub use mat2::Mat2;pub use mat3::Mat3;pub use mat4::Mat4;pub use quat::Quat;pub use rect2::Rect2;pub use rect3::Rect3;pub use scalar::abs;pub use scalar::approx_eq;pub use scalar::clamp;pub use scalar::is_finite;pub use scalar::is_nan;pub use scalar::max;pub use scalar::min;pub use scalar::sign;pub use scalar::to_degrees;pub use scalar::to_radians;pub use numeric::GemathFloat;pub use vec2::Vec2;pub use vec3::Vec3;pub use vec4::Vec4;pub use angle::Degrees;pub use angle::Radians;pub use geometry::Capsule2;pub use geometry::Capsule3;pub use geometry::Circle;pub use geometry::Obb2;pub use geometry::Obb3;pub use geometry::Plane;pub use geometry::Ray2;pub use geometry::Ray3;pub use geometry::Segment2;pub use geometry::Segment3;pub use geometry::Sphere;pub use polygon2::Polygon2;pub use collision::circle_aabb2_intersects;pub use collision::circle_rect2_intersects;pub use collision::ray2_circle_cast;pub use collision::ray2_circle_intersect;pub use collision::ray3_sphere_cast;pub use collision::ray3_sphere_intersect;pub use collision::RayHit2;pub use collision::RayHit3;pub use collision::segment2_circle_intersects;pub use collision::segment2_intersection_point;pub use collision::segment2_intersects;pub use collision::segment3_sphere_intersects;pub use collision::sphere_aabb3_intersects;pub use collision::sphere_rect3_intersects;pub use spatial::Bvh3;pub use spatial::Quadtree2;pub use spatial::UniformGrid2;
Modules§
- aabb2
- aabb3
- angle
- collision
- Collision / intersection helpers for
geometryprimitives. - geometry
- half_
float - markers
- Shared marker (ZST) types used for type-level units and coordinate spaces.
- mat2
- mat3
- mat4
- math
- Floating-point math helpers.
- numeric
- Numeric traits for extensibility.
- polygon2
- 2D polygon primitive (allocation-backed).
- quat
- rect2
- rect3
- scalar
- spatial
- Spatial data structures (prototype-quality).
- unit_
vec - Unit-vector wrapper types.
- vec2
- vec3
- vec4
Type Aliases§
- Real
- The crate’s chosen scalar type.