#include #include "vde/spatial/bvh.h" #include "vde/core/triangle.h" #include "vde/core/line.h" #include #include using namespace vde::spatial; using namespace vde::core; // ═══════════════════════════════════════════════════════════ // BVH 测试: build / intersect / update / query_ray // ═══════════════════════════════════════════════════════════ 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(BVHTest, BuildEmpty) { BVH bvh; bvh.build({}); EXPECT_EQ(bvh.size(), 0u); } TEST(BVHTest, BuildSingle) { BVH bvh; Triangle3D tri({0,0,0}, {1,0,0}, {0,1,0}); bvh.build({tri}); EXPECT_EQ(bvh.size(), 1u); } TEST(BVHTest, BuildMultiple) { BVH bvh; auto tris = make_triangles(); bvh.build(tris); EXPECT_EQ(bvh.size(), tris.size()); } TEST(BVHTest, BuildWithStrategy) { BVHBuildOptions opts; opts.strategy = BVHSplitStrategy::Middle; opts.leaf_size = 2; BVH bvh(opts); auto tris = make_triangles(); bvh.build(tris); EXPECT_EQ(bvh.size(), tris.size()); } TEST(BVHTest, BuildSAHStrategy) { BVHBuildOptions opts; opts.strategy = BVHSplitStrategy::SAH; opts.leaf_size = 1; BVH bvh(opts); bvh.build(make_triangles()); EXPECT_GT(bvh.size(), 0u); } // ── intersect (query_range) ────────────────────────── TEST(BVHTest, IntersectRange) { BVH bvh; bvh.build(make_triangles()); // 范围查询 AABB3D range(Point3D(0, 0, 0), Point3D(0.5, 0.5, 0.5)); auto results = bvh.query_range(range); EXPECT_GE(results.size(), 1u); } TEST(BVHTest, IntersectRangeEmpty) { BVH bvh; bvh.build(make_triangles()); // 范围远离三角形 AABB3D range(Point3D(100, 100, 100), Point3D(101, 101, 101)); auto results = bvh.query_range(range); EXPECT_EQ(results.size(), 0u); } TEST(BVHTest, IntersectRangeFull) { BVH bvh; auto tris = make_triangles(); bvh.build(tris); // 全覆盖 AABB3D range(Point3D(-1, -1, -1), Point3D(2, 2, 2)); auto results = bvh.query_range(range); EXPECT_EQ(results.size(), tris.size()); } // ── update (insert/remove) ─────────────────────────── TEST(BVHTest, UpdateInsert) { BVH bvh; bvh.insert(Triangle3D({0,0,0}, {1,0,0}, {0,1,0})); EXPECT_EQ(bvh.size(), 1u); bvh.insert(Triangle3D({1,0,0}, {1,1,0}, {0,1,0})); EXPECT_EQ(bvh.size(), 2u); } TEST(BVHTest, UpdateRemove) { BVH bvh; Triangle3D tri({0,0,0}, {1,0,0}, {0,1,0}); bvh.insert(tri); EXPECT_EQ(bvh.size(), 1u); bool removed = bvh.remove(tri); EXPECT_TRUE(removed); EXPECT_EQ(bvh.size(), 0u); } TEST(BVHTest, UpdateRemoveNonExistent) { BVH bvh; Triangle3D tri1({0,0,0}, {1,0,0}, {0,1,0}); bvh.insert(tri1); EXPECT_EQ(bvh.size(), 1u); Triangle3D tri2({100,100,100}, {200,100,100}, {100,200,100}); bool removed = bvh.remove(tri2); EXPECT_FALSE(removed); EXPECT_EQ(bvh.size(), 1u); } // ── query_ray ──────────────────────────────────────── TEST(BVHTest, QueryRayHit) { BVH bvh; bvh.build(make_triangles()); // 沿 +z 轴发射射线应命中三角形 Ray3Dd ray(Point3D(0.2, 0.2, -1), Vector3D(0, 0, 1)); auto results = bvh.query_ray(ray); EXPECT_GE(results.size(), 1u); } TEST(BVHTest, QueryRayMiss) { BVH bvh; bvh.build(make_triangles()); // 远离的射线 Ray3Dd ray(Point3D(100, 100, 100), Vector3D(0, 0, 1)); auto results = bvh.query_ray(ray); EXPECT_EQ(results.size(), 0u); } TEST(BVHTest, QueryRayNearest) { BVH bvh; bvh.build(make_triangles()); Ray3Dd ray(Point3D(0.2, 0.2, -1), Vector3D(0, 0, 1)); auto hit = bvh.query_ray_nearest(ray); if (hit.has_value()) { EXPECT_GT(hit->t, 0.0); EXPECT_GE(hit->tri_index, 0); } } // ── KNN ────────────────────────────────────────────── TEST(BVHTest, QueryKNN) { BVH bvh; bvh.build(make_triangles()); auto results = bvh.query_knn(Point3D(0, 0, 0), 2); EXPECT_GE(results.size(), 1u); EXPECT_LE(results.size(), 2u); } // ── clear ──────────────────────────────────────────── TEST(BVHTest, ClearBVH) { BVH bvh; bvh.build(make_triangles()); EXPECT_GT(bvh.size(), 0u); bvh.clear(); EXPECT_EQ(bvh.size(), 0u); } // ═══════════════════════════════════════════════════════════ // index_of / query_*_indices (VDE-893) — 索引必须能回指原始输入三角形 // ═══════════════════════════════════════════════════════════ TEST(BVHIndexVDE893, IndexOfMatchesBuildInsertionOrder) { // 第 i 个三角形位于 x = i*10,其插入下标可由几何唯一还原。 std::vector tris; for (int i = 0; i < 8; ++i) { double x = i * 10.0; tris.push_back(Triangle3D({x, 0, 0}, {x + 1, 0, 0}, {x, 1, 0})); } BVH bvh; bvh.build(tris); AABB3D all(Point3D(-1, -1, -1), Point3D(100, 100, 100)); auto hits = bvh.query_range(all); ASSERT_EQ(hits.size(), 8u); for (const auto& tri : hits) { int idx = bvh.index_of(tri); ASSERT_GE(idx, 0); ASSERT_LT(idx, static_cast(tris.size())); // index_of 必须指回“同一颗”原始三角形(用 x 起点判别)。 EXPECT_DOUBLE_EQ(tris[idx].vertices()[0].x(), tri.vertices()[0].x()); // 且与由几何推得的插入下标一致。 EXPECT_EQ(idx, static_cast(std::lround(tri.vertices()[0].x() / 10.0))); } } TEST(BVHIndexVDE893, QueryRangeIndicesPairsIndexWithTriangle) { std::vector tris; for (int i = 0; i < 6; ++i) { double x = i * 5.0; // 0,5,10,15,20,25 tris.push_back(Triangle3D({x, 0, 0}, {x + 1, 0, 0}, {x, 1, 0})); } BVH bvh; bvh.build(tris); // 全覆盖:每对 (index, triangle) 的下标都必须能回指同一颗原始三角形。 // 注意 query_range 是保守的(返回相交叶内的整批图元),因此只断言配对 // 不变量,不断言精确数量。 AABB3D all(Point3D(-1, -1, -1), Point3D(100, 100, 100)); auto pairs = bvh.query_range_indices(all); ASSERT_EQ(pairs.size(), 6u); for (const auto& [idx, tri] : pairs) { ASSERT_GE(idx, 0); ASSERT_LT(idx, static_cast(tris.size())); EXPECT_EQ(bvh.index_of(tri), idx); EXPECT_DOUBLE_EQ(tris[idx].vertices()[0].x(), tri.vertices()[0].x()); } // 远离的范围:无配对。 AABB3D far(Point3D(1000, 1000, 1000), Point3D(1001, 1001, 1001)); EXPECT_TRUE(bvh.query_range_indices(far).empty()); } TEST(BVHIndexVDE893, QueryRayIndicesPairsIndexWithTriangle) { BVH bvh; bvh.build(make_triangles()); // +z 射线命中 z=0 平面上的三角形 0。 Ray3Dd ray(Point3D(0.2, 0.2, -1), Vector3D(0, 0, 1)); auto pairs = bvh.query_ray_indices(ray); ASSERT_GE(pairs.size(), 1u); for (const auto& [idx, tri] : pairs) { ASSERT_GE(idx, 0); ASSERT_LT(idx, static_cast(bvh.size())); EXPECT_EQ(bvh.index_of(tri), idx); } // 命中的索引 0 对应的原始三角形与返回三角形几何一致。 auto orig = make_triangles(); EXPECT_EQ(bvh.index_of(pairs[0].second), pairs[0].first); const auto& rv = pairs[0].second.vertices()[0]; const auto& ov = orig[pairs[0].first].vertices()[0]; EXPECT_DOUBLE_EQ(rv.x(), ov.x()); EXPECT_DOUBLE_EQ(rv.y(), ov.y()); EXPECT_DOUBLE_EQ(rv.z(), ov.z()); } // ═══════════════════════════════════════════════════════════ // Phase-2: query_range exact primitive test + query_knn exact distance // ═══════════════════════════════════════════════════════════ #include #include #include namespace { Point3D p2_closest_on_tri(const Point3D& p, const Triangle3D& tri) { const Point3D& a = tri.v(0); const Point3D& b = tri.v(1); const Point3D& c = tri.v(2); const Vector3D ab = b - a, ac = c - a, ap = p - a; const double d1 = ab.dot(ap), d2 = ac.dot(ap); if (d1 <= 0.0 && d2 <= 0.0) return a; const Vector3D bp = p - b; const double d3 = ab.dot(bp), d4 = ac.dot(bp); if (d3 >= 0.0 && d4 <= d3) return b; const double vc = d1 * d4 - d3 * d2; if (vc <= 0.0 && d1 >= 0.0 && d3 <= 0.0) { const double v = d1 / (d1 - d3); return a + ab * v; } const Vector3D cp = p - c; const double d5 = ab.dot(cp), d6 = ac.dot(cp); if (d6 >= 0.0 && d5 <= d6) return c; const double vb = d5 * d2 - d1 * d6; if (vb <= 0.0 && d2 >= 0.0 && d6 <= 0.0) { const double w = d2 / (d2 - d6); return a + ac * w; } const double va = d3 * d6 - d5 * d4; if (va <= 0.0 && (d4 - d3) >= 0.0 && (d5 - d6) >= 0.0) { const double w = (d4 - d3) / ((d4 - d3) + (d5 - d6)); return b + (c - b) * w; } const double denom = va + vb + vc; if (std::abs(denom) < 1e-30) return a; const double v = vb / denom, w = vc / denom; return a + ab * v + ac * w; } } // namespace TEST(Phase2BVH, QueryRangeMatchesBruteForceTriangleAABB) { std::mt19937 gen(4242); std::uniform_real_distribution u(-1.0, 1.0); std::vector tris; for (int i = 0; i < 300; ++i) { const Point3D c(u(gen), u(gen), u(gen)); const double s = 0.08; tris.push_back(Triangle3D(c, c + Vector3D(s, 0, 0), c + Vector3D(0, s, 0))); } BVH bvh; bvh.build(tris); const AABB3D range(Point3D(-0.3, -0.3, -0.3), Point3D(0.3, 0.3, 0.3)); int brute = 0; for (const auto& t : tris) { AABB3D tb; tb.expand(t.v(0)); tb.expand(t.v(1)); tb.expand(t.v(2)); if (tb.intersects(range)) ++brute; } auto got = bvh.query_range(range); EXPECT_EQ(static_cast(got.size()), brute); EXPECT_GT(brute, 0); } TEST(Phase2BVH, QueryKnnMatchesBruteForceSurfaceDistance) { std::mt19937 gen(99); std::uniform_real_distribution u(-1.0, 1.0); std::vector tris; for (int i = 0; i < 200; ++i) { const Point3D c(u(gen), u(gen), u(gen)); const double s = 0.1; tris.push_back(Triangle3D(c, c + Vector3D(s, 0, 0.03), c + Vector3D(0.02, s, 0))); } BVH bvh; bvh.build(tris); const Point3D q(0.1, 0.2, 0.3); std::vector d; d.reserve(tris.size()); for (const auto& t : tris) d.push_back((p2_closest_on_tri(q, t) - q).norm()); std::sort(d.begin(), d.end()); auto got = bvh.query_knn(q, 10); ASSERT_EQ(got.size(), 10u); std::vector gotd; for (const auto& t : got) gotd.push_back((p2_closest_on_tri(q, t) - q).norm()); std::sort(gotd.begin(), gotd.end()); for (size_t i = 0; i < gotd.size(); ++i) EXPECT_NEAR(gotd[i], d[i], 1e-9) << "rank " << i; } // P2 (task-38) 回归:与 audit probe 相同的 300 小三角形场景,k=10、100 个随机 // 查询点,逐查询与暴力最近距离比较;要求 worst_distance_err <= 1e-9,且返回 // 顺序本身按距离升序(不能只靠排序掩盖乱序)。 TEST(Phase2BVH, QueryKnnAuditScenarioExactAcrossQueries) { std::mt19937 gen(4242); std::uniform_real_distribution u01(0.0, 1.0); std::vector tris; for (int i = 0; i < 300; ++i) { const Point3D c(u01(gen) * 2 - 1, u01(gen) * 2 - 1, u01(gen) * 2 - 1); const double s = 0.08; tris.push_back(Triangle3D(c, c + Vector3D(s, 0, 0), c + Vector3D(0, s, 0))); } BVH bvh; bvh.build(tris); double worst = 0.0; for (int qi = 0; qi < 100; ++qi) { const Point3D q(u01(gen) * 2 - 1, u01(gen) * 2 - 1, u01(gen) * 2 - 1); std::vector d; d.reserve(tris.size()); for (const auto& t : tris) d.push_back((p2_closest_on_tri(q, t) - q).norm()); std::sort(d.begin(), d.end()); auto got = bvh.query_knn(q, 10); ASSERT_EQ(got.size(), 10u) << "query " << qi; double prev = -1.0; for (size_t i = 0; i < got.size(); ++i) { const double gotd = (p2_closest_on_tri(q, got[i]) - q).norm(); worst = std::max(worst, std::abs(gotd - d[i])); EXPECT_GE(gotd + 1e-12, prev) << "query " << qi << " rank " << i << " 结果应按距离升序"; prev = gotd; } } EXPECT_LE(worst, 1e-9) << "worst_distance_err=" << worst; }