#include #include "vde/spatial/incremental_bvh.h" #include "vde/core/point.h" #include #include using namespace vde::spatial; using namespace vde::core; // ═══════════════════════════════════════════════════════════ // IncrementalBVH 测试: build / insert / remove / update / ray_query // ═══════════════════════════════════════════════════════════ static std::vector make_triangles() { return { Triangle3D({0, 0, 0}, {1, 0, 0}, {0, 1, 0}), Triangle3D({0, 0, 0}, {0, 1, 0}, {0, 0, 1}), Triangle3D({0, 0, 0}, {0, 0, 1}, {1, 0, 0}), Triangle3D({1, 1, 1}, {1, 0, 1}, {0, 1, 1}), }; } // ── build ──────────────────────────────────────────── TEST(IncrementalBVHTest, BuildWithInitialTriangles) { IncrementalBVH ibvh; auto tris = make_triangles(); ibvh.build(tris); EXPECT_EQ(ibvh.size(), tris.size()); } TEST(IncrementalBVHTest, BuildThenRayQuery) { IncrementalBVH ibvh; ibvh.build(make_triangles()); // 沿 +z 轴发射射线应命中三角形 Ray3Dd ray(Point3D(0.2, 0.2, -1), Vector3D(0, 0, 1)); auto results = ibvh.ray_query(ray); EXPECT_GE(results.size(), 1u); } TEST(IncrementalBVHTest, BuildEmpty) { IncrementalBVH ibvh; ibvh.build({}); EXPECT_EQ(ibvh.size(), 0u); } // ── insert ────────────────────────────────────────── TEST(IncrementalBVHTest, InsertIncreasesSize) { IncrementalBVH ibvh; ibvh.build({Triangle3D({0, 0, 0}, {1, 0, 0}, {0, 1, 0})}); size_t before = ibvh.size(); ibvh.insert(Triangle3D({1, 1, 1}, {2, 1, 1}, {1, 2, 1})); EXPECT_EQ(ibvh.size(), before + 1); } TEST(IncrementalBVHTest, InsertMultiple) { IncrementalBVH ibvh; ibvh.build({}); EXPECT_EQ(ibvh.size(), 0u); ibvh.insert(Triangle3D({0, 0, 0}, {1, 0, 0}, {0, 1, 0})); EXPECT_EQ(ibvh.size(), 1u); ibvh.insert(Triangle3D({1, 0, 0}, {1, 1, 0}, {0, 1, 0})); EXPECT_EQ(ibvh.size(), 2u); } // ── remove ────────────────────────────────────────── TEST(IncrementalBVHTest, RemoveReducesSize) { IncrementalBVH ibvh; auto tris = make_triangles(); ibvh.build(tris); size_t before = ibvh.size(); ibvh.remove(0); EXPECT_EQ(ibvh.size(), before - 1); } TEST(IncrementalBVHTest, RemoveLastElement) { IncrementalBVH ibvh; ibvh.build({Triangle3D({0, 0, 0}, {1, 0, 0}, {0, 1, 0})}); EXPECT_EQ(ibvh.size(), 1u); ibvh.remove(0); EXPECT_EQ(ibvh.size(), 0u); } // ── update ────────────────────────────────────────── TEST(IncrementalBVHTest, UpdateChangesGeometry) { IncrementalBVH ibvh; ibvh.build({ Triangle3D({0, 0, 0}, {1, 0, 0}, {0, 1, 0}), Triangle3D({5, 5, 5}, {6, 5, 5}, {5, 6, 5}), }); // 将第一个三角形移到远离原点的位置 Triangle3D new_tri({10, 10, 10}, {11, 10, 10}, {10, 11, 10}); ibvh.update(0, new_tri); // 原点射线不再命中(第一个三角形已移到远处) Ray3Dd ray_origin(Point3D(0.2, 0.2, -1), Vector3D(0, 0, 1)); auto results_origin = ibvh.ray_query(ray_origin); // 远处射线应命中更新后的三角形 Ray3Dd ray_far(Point3D(10.2, 10.2, 9), Vector3D(0, 0, 1)); auto results_far = ibvh.ray_query(ray_far); EXPECT_GE(results_far.size(), 1u); } TEST(IncrementalBVHTest, UpdatePreservesSize) { IncrementalBVH ibvh; ibvh.build(make_triangles()); size_t before = ibvh.size(); Triangle3D new_tri({2, 2, 2}, {3, 2, 2}, {2, 3, 2}); ibvh.update(0, new_tri); EXPECT_EQ(ibvh.size(), before); } // ── ray_query after insert ────────────────────────── TEST(IncrementalBVHTest, RayQueryAfterInsert) { IncrementalBVH ibvh; ibvh.build({}); // 插入一个在 z=0 平面上的三角形 Triangle3D tri({-1, -1, 0}, {1, -1, 0}, {0, 1, 0}); ibvh.insert(tri); // 沿 +z 轴发射射线应命中 Ray3Dd ray(Point3D(0, 0, -1), Vector3D(0, 0, 1)); auto results = ibvh.ray_query(ray); EXPECT_GE(results.size(), 1u); } // ── empty BVH ────────────────────────────────────── TEST(IncrementalBVHTest, EmptyBVHRayQueryReturnsEmpty) { IncrementalBVH ibvh; ibvh.build({}); Ray3Dd ray(Point3D(0, 0, -1), Vector3D(0, 0, 1)); auto results = ibvh.ray_query(ray); EXPECT_TRUE(results.empty()); } // ── single triangle ──────────────────────────────── TEST(IncrementalBVHTest, SingleTriangleRayQueryHits) { IncrementalBVH ibvh; Triangle3D tri({-1, -1, 0}, {1, -1, 0}, {0, 1, 0}); ibvh.build({tri}); // 射线穿过三角形中心 Ray3Dd ray(Point3D(0, 0, -1), Vector3D(0, 0, 1)); auto results = ibvh.ray_query(ray); EXPECT_EQ(results.size(), 1u); } TEST(IncrementalBVHTest, SingleTriangleRayQueryMisses) { IncrementalBVH ibvh; Triangle3D tri({-1, -1, 0}, {1, -1, 0}, {0, 1, 0}); ibvh.build({tri}); // 远离三角形的射线 Ray3Dd ray(Point3D(100, 100, -1), Vector3D(0, 0, 1)); auto results = ibvh.ray_query(ray); EXPECT_TRUE(results.empty()); } // ── multiple triangles ────────────────────────────── TEST(IncrementalBVHTest, MultipleTrianglesRayQueryFindsIntersections) { IncrementalBVH ibvh; auto tris = make_triangles(); ibvh.build(tris); // 沿 +z 轴在不同位置发射多条射线 int total_hits = 0; for (double x = 0.1; x < 1.0; x += 0.2) { for (double y = 0.1; y < 1.0; y += 0.2) { Ray3Dd ray(Point3D(x, y, -1), Vector3D(0, 0, 1)); auto results = ibvh.ray_query(ray); total_hits += static_cast(results.size()); } } EXPECT_GT(total_hits, 0); } TEST(IncrementalBVHTest, MultipleTrianglesAllHitResults) { IncrementalBVH ibvh; // 两个平行三角形在不同 z 高度 ibvh.build({ Triangle3D({-1, -1, 0}, {1, -1, 0}, {0, 1, 0}), Triangle3D({-1, -1, 2}, {1, -1, 2}, {0, 1, 2}), }); // 沿 +z 轴穿过两个三角形 Ray3Dd ray(Point3D(0, 0, -1), Vector3D(0, 0, 1)); auto results = ibvh.ray_query(ray); EXPECT_EQ(results.size(), 2u); } // ── combined workflow ─────────────────────────────── TEST(IncrementalBVHTest, InsertThenQueryFindsNewTriangle) { IncrementalBVH ibvh; // 初始:一个远离原点的三角形 ibvh.build({Triangle3D({10, 10, 0}, {11, 10, 0}, {10, 11, 0})}); // 原点射线不命中 Ray3Dd ray(Point3D(0, 0, -1), Vector3D(0, 0, 1)); auto before = ibvh.ray_query(ray); EXPECT_TRUE(before.empty()); // 插入一个覆盖原点的三角形 ibvh.insert(Triangle3D({-1, -1, 0}, {1, -1, 0}, {0, 1, 0})); // 现在射线应命中 auto after = ibvh.ray_query(ray); EXPECT_GE(after.size(), 1u); }