|
| 1 | +// |
| 2 | +// Created by Vlad on 3/25/2025. |
| 3 | +// |
| 4 | +#include "omath/collision/line_tracer.hpp" |
| 5 | +#include "omath/linear_algebra/aabb.hpp" |
| 6 | +#include "omath/linear_algebra/vector3.hpp" |
| 7 | +#include <gtest/gtest.h> |
| 8 | + |
| 9 | +using Vec3 = omath::Vector3<float>; |
| 10 | +using Ray = omath::collision::Ray<>; |
| 11 | +using LineTracer = omath::collision::LineTracer<>; |
| 12 | +using AABB = omath::AABB<Vec3>; |
| 13 | + |
| 14 | +static Ray make_ray(Vec3 start, Vec3 end, bool infinite = false) |
| 15 | +{ |
| 16 | + Ray r; |
| 17 | + r.start = start; |
| 18 | + r.end = end; |
| 19 | + r.infinite_length = infinite; |
| 20 | + return r; |
| 21 | +} |
| 22 | + |
| 23 | +// Ray passing straight through the center along Z axis |
| 24 | +TEST(LineTracerAABBTests, HitCenterAlongZ) |
| 25 | +{ |
| 26 | + const AABB box{{-1.f, -1.f, -1.f}, {1.f, 1.f, 1.f}}; |
| 27 | + const auto ray = make_ray({0.f, 0.f, -5.f}, {0.f, 0.f, 5.f}); |
| 28 | + |
| 29 | + const auto hit = LineTracer::get_ray_hit_point(ray, box); |
| 30 | + EXPECT_NE(hit, ray.end); |
| 31 | + EXPECT_NEAR(hit.z, -1.f, 1e-4f); |
| 32 | + EXPECT_NEAR(hit.x, 0.f, 1e-4f); |
| 33 | + EXPECT_NEAR(hit.y, 0.f, 1e-4f); |
| 34 | +} |
| 35 | + |
| 36 | +// Ray passing straight through the center along X axis |
| 37 | +TEST(LineTracerAABBTests, HitCenterAlongX) |
| 38 | +{ |
| 39 | + const AABB box{{-1.f, -1.f, -1.f}, {1.f, 1.f, 1.f}}; |
| 40 | + const auto ray = make_ray({-5.f, 0.f, 0.f}, {5.f, 0.f, 0.f}); |
| 41 | + |
| 42 | + const auto hit = LineTracer::get_ray_hit_point(ray, box); |
| 43 | + EXPECT_NE(hit, ray.end); |
| 44 | + EXPECT_NEAR(hit.x, -1.f, 1e-4f); |
| 45 | +} |
| 46 | + |
| 47 | +// Ray that misses entirely (too far in Y) |
| 48 | +TEST(LineTracerAABBTests, MissReturnsEnd) |
| 49 | +{ |
| 50 | + const AABB box{{-1.f, -1.f, -1.f}, {1.f, 1.f, 1.f}}; |
| 51 | + const auto ray = make_ray({0.f, 5.f, -5.f}, {0.f, 5.f, 5.f}); |
| 52 | + |
| 53 | + const auto hit = LineTracer::get_ray_hit_point(ray, box); |
| 54 | + EXPECT_EQ(hit, ray.end); |
| 55 | +} |
| 56 | + |
| 57 | +// Ray that stops short before reaching the box |
| 58 | +TEST(LineTracerAABBTests, RayTooShortReturnsEnd) |
| 59 | +{ |
| 60 | + const AABB box{{3.f, -1.f, -1.f}, {5.f, 1.f, 1.f}}; |
| 61 | + const auto ray = make_ray({0.f, 0.f, 0.f}, {2.f, 0.f, 0.f}); |
| 62 | + |
| 63 | + const auto hit = LineTracer::get_ray_hit_point(ray, box); |
| 64 | + EXPECT_EQ(hit, ray.end); |
| 65 | +} |
| 66 | + |
| 67 | +// Infinite ray that starts before the box should hit |
| 68 | +TEST(LineTracerAABBTests, InfiniteRayHits) |
| 69 | +{ |
| 70 | + const AABB box{{3.f, -1.f, -1.f}, {5.f, 1.f, 1.f}}; |
| 71 | + const auto ray = make_ray({0.f, 0.f, 0.f}, {2.f, 0.f, 0.f}, true); |
| 72 | + |
| 73 | + const auto hit = LineTracer::get_ray_hit_point(ray, box); |
| 74 | + EXPECT_NE(hit, ray.end); |
| 75 | + EXPECT_NEAR(hit.x, 3.f, 1e-4f); |
| 76 | +} |
| 77 | + |
| 78 | +// Ray starting inside the box — t_min=0, so hit point equals ray.start |
| 79 | +TEST(LineTracerAABBTests, RayStartsInsideBox) |
| 80 | +{ |
| 81 | + const AABB box{{-1.f, -1.f, -1.f}, {1.f, 1.f, 1.f}}; |
| 82 | + const auto ray = make_ray({0.f, 0.f, 0.f}, {0.f, 0.f, 5.f}); |
| 83 | + |
| 84 | + const auto hit = LineTracer::get_ray_hit_point(ray, box); |
| 85 | + EXPECT_NE(hit, ray.end); |
| 86 | + // t_min is clamped to 0, so hit == start |
| 87 | + EXPECT_NEAR(hit.x, 0.f, 1e-4f); |
| 88 | + EXPECT_NEAR(hit.y, 0.f, 1e-4f); |
| 89 | + EXPECT_NEAR(hit.z, 0.f, 1e-4f); |
| 90 | +} |
| 91 | + |
| 92 | +// Ray parallel to XY plane, pointing along X, at Z outside the box |
| 93 | +TEST(LineTracerAABBTests, ParallelRayOutsideSlabMisses) |
| 94 | +{ |
| 95 | + const AABB box{{-1.f, -1.f, -1.f}, {1.f, 1.f, 1.f}}; |
| 96 | + // Z component of ray is 3.0 — outside box's Z slab |
| 97 | + const auto ray = make_ray({-5.f, 0.f, 3.f}, {5.f, 0.f, 3.f}); |
| 98 | + |
| 99 | + const auto hit = LineTracer::get_ray_hit_point(ray, box); |
| 100 | + EXPECT_EQ(hit, ray.end); |
| 101 | +} |
| 102 | + |
| 103 | +// Ray parallel to XY plane, pointing along X, at Z inside the box |
| 104 | +TEST(LineTracerAABBTests, ParallelRayInsideSlabHits) |
| 105 | +{ |
| 106 | + const AABB box{{-1.f, -1.f, -1.f}, {1.f, 1.f, 1.f}}; |
| 107 | + const auto ray = make_ray({-5.f, 0.f, 0.f}, {5.f, 0.f, 0.f}); |
| 108 | + |
| 109 | + const auto hit = LineTracer::get_ray_hit_point(ray, box); |
| 110 | + EXPECT_NE(hit, ray.end); |
| 111 | + EXPECT_NEAR(hit.x, -1.f, 1e-4f); |
| 112 | +} |
| 113 | + |
| 114 | +// Diagonal ray hitting a corner region |
| 115 | +TEST(LineTracerAABBTests, DiagonalRayHits) |
| 116 | +{ |
| 117 | + const AABB box{{0.f, 0.f, 0.f}, {2.f, 2.f, 2.f}}; |
| 118 | + const auto ray = make_ray({-1.f, -1.f, -1.f}, {3.f, 3.f, 3.f}); |
| 119 | + |
| 120 | + const auto hit = LineTracer::get_ray_hit_point(ray, box); |
| 121 | + EXPECT_NE(hit, ray.end); |
| 122 | + // Entry point should be at (0,0,0) |
| 123 | + EXPECT_NEAR(hit.x, 0.f, 1e-4f); |
| 124 | + EXPECT_NEAR(hit.y, 0.f, 1e-4f); |
| 125 | + EXPECT_NEAR(hit.z, 0.f, 1e-4f); |
| 126 | +} |
0 commit comments