#include #include "vde/collision/ray_intersect.h" #include "vde/core/triangle.h" #include "vde/core/line.h" #include "vde/core/aabb.h" #include "vde/core/point.h" #include using namespace vde::collision; using vde::core::Point3D; using vde::core::Vector3D; using vde::core::Triangle3D; using vde::core::Ray3Dd; using vde::core::AABB3D; TEST(RayIntersectTest, RayTriangle_Hit) { Triangle3D tri(Point3D(0,0,0), Point3D(1,0,0), Point3D(0,1,0)); Point3D origin(0.25, 0.25, 1); Vector3D dir(0, 0, -1); auto hit = ray_triangle_intersect(origin, dir, tri); ASSERT_TRUE(hit.has_value()); EXPECT_NEAR(hit->t, 1.0, 1e-6); } TEST(RayIntersectTest, RayTriangle_Miss) { Triangle3D tri(Point3D(0,0,0), Point3D(1,0,0), Point3D(0,1,0)); Point3D origin(0.25, 0.25, 1); Vector3D dir(1, 0, 0); // parallel to triangle plane auto hit = ray_triangle_intersect(origin, dir, tri); EXPECT_FALSE(hit.has_value()); } TEST(RayIntersectTest, RayTriangle_BehindOrigin) { Triangle3D tri(Point3D(0,0,0), Point3D(1,0,0), Point3D(0,1,0)); Point3D origin(0.25, 0.25, 1); Vector3D dir(0, 0, 1); // pointing away from triangle auto hit = ray_triangle_intersect(origin, dir, tri); EXPECT_FALSE(hit.has_value()); } TEST(RayIntersectTest, RayAABB_Hit) { Ray3Dd ray(Point3D(-1, 0.5, 0.5), Vector3D(1, 0, 0)); AABB3D box(Point3D(0,0,0), Point3D(2,2,2)); double tmin, tmax; EXPECT_TRUE(ray_aabb_intersect(ray, box, tmin, tmax)); EXPECT_NEAR(tmin, 1.0, 1e-6); } TEST(RayIntersectTest, RayAABB_Miss) { Ray3Dd ray(Point3D(-1, 3, 3), Vector3D(1, 0, 0)); AABB3D box(Point3D(0,0,0), Point3D(2,2,2)); double tmin, tmax; EXPECT_FALSE(ray_aabb_intersect(ray, box, tmin, tmax)); } TEST(RayIntersectTest, RayAABB_Inside) { Ray3Dd ray(Point3D(0.5, 0.5, 0.5), Vector3D(1, 0, 0)); AABB3D box(Point3D(0,0,0), Point3D(2,2,2)); double tmin, tmax; EXPECT_TRUE(ray_aabb_intersect(ray, box, tmin, tmax)); } TEST(RayIntersectTest, RaySphere_Hit) { Point3D origin(0, 0, 5); Vector3D dir(0, 0, -1); auto hit = ray_sphere_intersect(origin, dir, Point3D(0,0,0), 1.0); ASSERT_TRUE(hit.has_value()); EXPECT_NEAR(hit->t, 4.0, 1e-6); } TEST(RayIntersectTest, RaySphere_Miss) { Point3D origin(0, 0, 5); Vector3D dir(1, 0, 0); auto hit = ray_sphere_intersect(origin, dir, Point3D(0,0,0), 1.0); EXPECT_FALSE(hit.has_value()); } TEST(RayIntersectTest, RayPlane_Hit) { auto t = ray_plane_intersect(Point3D(0,0,5), Vector3D(0,0,-1), Point3D(0,0,0), Vector3D(0,0,1)); ASSERT_TRUE(t.has_value()); EXPECT_NEAR(*t, 5.0, 1e-6); } TEST(RayIntersectTest, RayPlane_Parallel) { auto t = ray_plane_intersect(Point3D(0,0,5), Vector3D(1,0,0), Point3D(0,0,0), Vector3D(0,0,1)); EXPECT_FALSE(t.has_value()); } TEST(RayIntersectTest, RayMesh_Hit) { std::vector tris = { Triangle3D(Point3D(0,0,0), Point3D(2,0,0), Point3D(0,2,0)), }; auto hit = ray_mesh_intersect(Point3D(0.5, 0.5, 5), Vector3D(0,0,-1), tris); ASSERT_TRUE(hit.has_value()); EXPECT_NEAR(hit->t, 5.0, 1e-6); }