gemath 0.1.0

Type-safe game math with type-level units/spaces, typed angles, and explicit fallible ops (plus optional geometry/collision).
Documentation
#![cfg(all(feature = "aabb2", feature = "mat4"))]

use gemath::*;
use gemath::aabb2::*;
use gemath::vec2::*;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_aabb2_new() {
        let aabb: Aabb2<(),()> = Aabb2::new(Vec2::new(1.0, 2.0), Vec2::new(3.0, 4.0));
        assert_eq!(aabb.center, Vec2::new(1.0, 2.0));
        assert_eq!(aabb.half_size, Vec2::new(3.0, 4.0));
    }

    #[test]
    fn test_aabb2_default() {
        let aabb: Aabb2<(),()> = Default::default();
        assert_eq!(aabb.center, Vec2::ZERO);
        assert_eq!(aabb.half_size, Vec2::ZERO);
    }

    #[test]
    fn test_aabb2_min_max_size() {
        let aabb: Aabb2<(),()> = Aabb2::new(Vec2::new(5.0, 5.0), Vec2::new(2.0, 3.0));
        assert_eq!(aabb.min(), Vec2::new(3.0, 2.0));
        assert_eq!(aabb.max(), Vec2::new(7.0, 8.0));
        assert_eq!(aabb.size(), Vec2::new(4.0, 6.0));
    }

    #[test]
    fn test_aabb2_from_min_max() {
        let min: Vec2<(),()> = Vec2::new(1.0, 2.0);
        let max: Vec2<(),()> = Vec2::new(5.0, 6.0);
        let aabb = Aabb2::from_min_max(min, max);
        assert_eq!(aabb.min(), min);
        assert_eq!(aabb.max(), max);
    }

    #[test]
    fn test_aabb2_contains_point() {
        let aabb: Aabb2<(),()> = Aabb2::from_min_max(Vec2::new(0.0, 0.0), Vec2::new(2.0, 2.0));
        assert!(aabb.contains_point(Vec2::new(0.0, 0.0)));
        assert!(aabb.contains_point(Vec2::new(1.0, 1.0)));
        assert!(!aabb.contains_point(Vec2::new(2.0, 2.0)));
        assert!(!aabb.contains_point(Vec2::new(-1.0, 1.0)));
    }

    #[test]
    fn test_aabb2_intersects_and_intersection() {
        let a: Aabb2<(),()> = Aabb2::from_min_max(Vec2::new(0.0, 0.0), Vec2::new(2.0, 2.0));
        let b: Aabb2<(),()> = Aabb2::from_min_max(Vec2::new(1.0, 1.0), Vec2::new(3.0, 3.0));
        let c: Aabb2<(),()> = Aabb2::from_min_max(Vec2::new(3.0, 3.0), Vec2::new(4.0, 4.0));
        assert!(a.intersects(&b));
        assert!(!a.intersects(&c));
        let intersection = a.intersection(&b).unwrap();
        assert_eq!(intersection.min(), Vec2::new(1.0, 1.0));
        assert_eq!(intersection.max(), Vec2::new(2.0, 2.0));
        assert!(a.intersection(&c).is_none());
    }

    #[test]
    fn test_aabb2_expand_to_include() {
        let aabb: Aabb2<(),()> = Aabb2::from_min_max(Vec2::new(1.0, 1.0), Vec2::new(2.0, 2.0));
        let expanded = aabb.expand_to_include(Vec2::new(0.0, 3.0));
        assert_eq!(expanded.min(), Vec2::new(0.0, 1.0));
        assert_eq!(expanded.max(), Vec2::new(2.0, 3.0));
    }

    #[test]
    fn test_aabb2_area_is_empty() {
        let aabb: Aabb2<(),()> = Aabb2::from_min_max(Vec2::new(0.0, 0.0), Vec2::new(2.0, 3.0));
        assert_eq!(aabb.area(), 6.0);
        assert!(!aabb.is_empty());
        let empty: Aabb2<(),()> = Aabb2::from_min_max(Vec2::new(1.0, 1.0), Vec2::new(1.0, 1.0));
        assert!(empty.is_empty());
        assert_eq!(empty.area(), 0.0);
    }

    #[test]
    fn test_aabb2_union() {
        let a: Aabb2<(),()> = Aabb2::from_min_max(Vec2::new(0.0, 0.0), Vec2::new(2.0, 2.0));
        let b: Aabb2<(),()> = Aabb2::from_min_max(Vec2::new(1.0, -1.0), Vec2::new(3.0, 1.0));
        let u = a.union(&b);
        assert_eq!(u.min(), Vec2::new(0.0, -1.0));
        assert_eq!(u.max(), Vec2::new(3.0, 2.0));
    }

    #[test]
    fn test_aabb2_closest_point_and_distance() {
        let aabb: Aabb2<(),()> = Aabb2::from_min_max(Vec2::new(1.0, 1.0), Vec2::new(3.0, 4.0));
        // Inside
        assert_eq!(aabb.closest_point(Vec2::new(2.0, 2.0)), Vec2::new(2.0, 2.0));
        assert_eq!(aabb.distance(Vec2::new(2.0, 2.0)), 0.0);
        // Outside
        assert_eq!(aabb.closest_point(Vec2::new(0.0, 0.0)), Vec2::new(1.0, 1.0));
        assert!((aabb.distance(Vec2::new(0.0, 0.0)) - (2.0_f32).sqrt()).abs() < 1e-6);
        assert_eq!(aabb.closest_point(Vec2::new(4.0, 5.0)), Vec2::new(3.0, 4.0));
    }

    #[test]
    fn test_aabb2_intersect_ray() {
        let aabb: Aabb2<(),()> = Aabb2::from_min_max(Vec2::new(1.0, 1.0), Vec2::new(3.0, 4.0));
        // Ray from outside, hits
        let t = aabb.intersect_ray(Vec2::new(0.0, 2.0), Vec2::new(1.0, 0.0));
        assert!(t.is_some() && (t.unwrap() - 1.0).abs() < 1e-6);
        // Ray from inside
        let t2 = aabb.intersect_ray(Vec2::new(2.0, 2.0), Vec2::new(1.0, 0.0));
        assert!(t2.is_some() && (t2.unwrap() - 1.0).abs() < 1e-6);
        // Ray misses
        let t3 = aabb.intersect_ray(Vec2::new(0.0, 0.0), Vec2::new(-1.0, 0.0));
        assert!(t3.is_none());
    }

    #[test]
    fn test_aabb2_transform() {
        use crate::mat4::Mat4;
        let aabb: Aabb2<(),()> = Aabb2::from_min_max(Vec2::new(1.0, 2.0), Vec2::new(3.0, 4.0));
        let m = Mat4::from_translation(crate::vec3::Vec3::new(10.0, 20.0, 0.0));
        let t = aabb.transform(&m);
        assert_eq!(t.min(), Vec2::new(11.0, 22.0));
        assert_eq!(t.max(), Vec2::new(13.0, 24.0));
    }
}

// --- Compile-time (const) tests/examples for Aabb2 ---
const _CONST_AABB0: Aabb2f32 = Aabb2f32::new(Vec2f32::new(1.0, 2.0), Vec2f32::new(3.0, 4.0));
const _CONST_AABB1: Aabb2f32 = Aabb2f32::new(Vec2f32::new(5.0, 6.0), Vec2f32::new(7.0, 8.0));
const _CONST_AABB_METERS: Aabb2Meters = Aabb2Meters::new(Vec2Meters::new(1.0, 2.0), Vec2Meters::new(3.0, 4.0));
const _CONST_AABB_WORLD: Aabb2World = Aabb2World::new(Vec2World::new(1.0, 2.0), Vec2World::new(3.0, 4.0));

const _: () = {
    // Compile-time assertions for const-everything
    assert!(_CONST_AABB0.center.x == 1.0 && _CONST_AABB0.center.y == 2.0);
    assert!(_CONST_AABB0.half_size.x == 3.0 && _CONST_AABB0.half_size.y == 4.0);
};

// Compile-time type safety: the following lines would fail to compile if uncommented
// const _FAIL: Aabb2Meters = Aabb2Pixels::new(Vec2Pixels::new(1.0, 2.0), Vec2Pixels::new(3.0, 4.0)); // error: mismatched types
// const _FAIL2: Aabb2World = Aabb2Local::new(Vec2Local::new(1.0, 2.0), Vec2Local::new(3.0, 4.0)); // error: mismatched types