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 = "spatial", any(feature = "std", feature = "alloc")))]

use gemath::*;

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

    #[test]
    fn uniform_grid2_query_dedups_and_finds_candidates() {
        let bounds_min = Vec2::<(), ()>::new(0.0, 0.0);
        let bounds_max = Vec2::<(), ()>::new(10.0, 10.0);
        let mut grid: UniformGrid2<(), ()> = UniformGrid2::new(bounds_min, bounds_max, 1.0);

        let a0: Aabb2<(), ()> = Aabb2::from_min_max(Vec2::new(1.0, 1.0), Vec2::new(2.0, 2.0));
        let a1: Aabb2<(), ()> = Aabb2::from_min_max(Vec2::new(5.0, 5.0), Vec2::new(6.0, 6.0));
        // Spans multiple cells.
        let a2: Aabb2<(), ()> = Aabb2::from_min_max(Vec2::new(2.2, 2.2), Vec2::new(4.8, 4.8));

        grid.insert_aabb(0, &a0);
        grid.insert_aabb(1, &a1);
        grid.insert_aabb(2, &a2);

        let q: Aabb2<(), ()> = Aabb2::from_min_max(Vec2::new(0.0, 0.0), Vec2::new(3.0, 3.0));
        let mut out = Vec::<usize>::new();
        grid.query_aabb(&q, &mut out);

        // Should include 0 and 2, but not 1.
        assert_eq!(out, vec![0, 2]);
    }

    #[test]
    fn quadtree2_query_returns_expected() {
        let root: Aabb2<(), ()> = Aabb2::from_min_max(Vec2::new(0.0, 0.0), Vec2::new(8.0, 8.0));
        let mut qt: Quadtree2<(), ()> = Quadtree2::new(root, 6, 2);

        let a0: Aabb2<(), ()> = Aabb2::from_min_max(Vec2::new(0.5, 0.5), Vec2::new(1.0, 1.0));
        let a1: Aabb2<(), ()> = Aabb2::from_min_max(Vec2::new(7.0, 7.0), Vec2::new(7.5, 7.5));
        let a2: Aabb2<(), ()> = Aabb2::from_min_max(Vec2::new(3.9, 3.9), Vec2::new(4.1, 4.1)); // near center

        qt.insert(0, a0);
        qt.insert(1, a1);
        qt.insert(2, a2);

        let q: Aabb2<(), ()> = Aabb2::from_min_max(Vec2::new(0.0, 0.0), Vec2::new(4.0, 4.0));
        let mut out = Vec::<usize>::new();
        qt.query_aabb(&q, &mut out);
        assert_eq!(out, vec![0, 2]);
    }

    #[test]
    fn bvh3_query_returns_expected() {
        let a0: Aabb3<(), ()> = Aabb3::from_min_max(Vec3::new(-1.0, -1.0, -1.0), Vec3::new(0.0, 0.0, 0.0));
        let a1: Aabb3<(), ()> = Aabb3::from_min_max(Vec3::new(10.0, 10.0, 10.0), Vec3::new(11.0, 11.0, 11.0));
        let a2: Aabb3<(), ()> = Aabb3::from_min_max(Vec3::new(0.5, 0.5, 0.5), Vec3::new(1.5, 1.5, 1.5));

        let bvh: Bvh3<(), ()> = Bvh3::build(&[a0, a1, a2]);

        let q: Aabb3<(), ()> = Aabb3::from_min_max(Vec3::new(-2.0, -2.0, -2.0), Vec3::new(2.0, 2.0, 2.0));
        let mut out = Vec::<usize>::new();
        bvh.query_aabb(&q, &mut out);
        assert_eq!(out, vec![0, 2]);
    }
}