#include #include "vde/spatial/bvh.h" #include "vde/core/triangle.h" #include "vde/core/line.h" #include "vde/core/point.h" #include #include #include #include using namespace vde::spatial; using namespace vde::core; // ═══════════════════════════════════════════════════════════ // Wave 8 补测: BVH 空树查询 / 深度截断 / KNN 边界 / 射线场 // // 覆盖 src/spatial/bvh.cpp 中此前未执行的分支: // - 空节点数组上的 query_range/query_knn/query_ray/query_ray_nearest // - build_recursive 的 depth >= max_depth 提前叶子化分支 // - query_knn 的 k == 0 / k > size 边界与最近点三角区域分类 // - query_ray / query_ray_nearest / *_indices 的场数据一致性 // - insert / remove 重复图元路径 // ═══════════════════════════════════════════════════════════ namespace { std::vector make_flat_grid(int count, double spacing) { std::vector tris; tris.reserve(static_cast(count)); for (int i = 0; i < count; ++i) { double x = static_cast(i) * spacing; tris.push_back(Triangle3D({x, 0.0, 0.0}, {x + 1.0, 0.0, 0.0}, {x, 0.5, 0.0})); } return tris; } // 点到三角形的最近距离平方(Ericson RTCD 区域法),独立于被测实现, // 用于交叉验证 query_knn 的索引排序。 double point_tri_dist_sq(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; const Vector3D ac = c - a; const Vector3D ap = p - a; const double d1 = ab.dot(ap); const double d2 = ac.dot(ap); if (d1 <= 0.0 && d2 <= 0.0) return ap.dot(ap); const Vector3D bp = p - b; const double d3 = ab.dot(bp); const double d4 = ac.dot(bp); if (d3 >= 0.0 && d4 <= d3) return bp.dot(bp); const double vc = d1 * d4 - d3 * d2; if (vc <= 0.0 && d1 >= 0.0 && d3 <= 0.0) { const double v = d1 / (d1 - d3); const Point3D q(a[0] + ab[0] * v, a[1] + ab[1] * v, a[2] + ab[2] * v); return (p - q).dot(p - q); } const Vector3D cp = p - c; const double d5 = ab.dot(cp); const double d6 = ac.dot(cp); if (d6 >= 0.0 && d5 <= d6) return cp.dot(cp); const double vb = d5 * d2 - d1 * d6; if (vb <= 0.0 && d2 >= 0.0 && d6 <= 0.0) { const double w = d2 / (d2 - d6); const Point3D q(a[0] + ac[0] * w, a[1] + ac[1] * w, a[2] + ac[2] * w); return (p - q).dot(p - q); } 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)); const Vector3D bc = c - b; const Point3D q(b[0] + bc[0] * w, b[1] + bc[1] * w, b[2] + bc[2] * w); return (p - q).dot(p - q); } const double denom = 1.0 / (va + vb + vc); const double v = vb * denom; const double w = vc * denom; const Point3D q(a[0] + ab[0] * v + ac[0] * w, a[1] + ab[1] * v + ac[1] * w, a[2] + ab[2] * v + ac[2] * w); return (p - q).dot(p - q); } int brute_force_nearest_index(const std::vector& tris, const Point3D& p) { int best = -1; double best_d = std::numeric_limits::infinity(); for (size_t i = 0; i < tris.size(); ++i) { const double d = point_tri_dist_sq(p, tris[i]); if (d < best_d) { best_d = d; best = static_cast(i); } } return best; } } // namespace // ── 空树: 所有查询的安全空返回 ────────────────────────── TEST(CovSpatialBvh, EmptyTreeQueriesAreSafe) { BVH bvh; bvh.build({}); ASSERT_EQ(bvh.size(), 0u); const AABB3D whole(Point3D(-10, -10, -10), Point3D(10, 10, 10)); EXPECT_TRUE(bvh.query_range(whole).empty()); EXPECT_TRUE(bvh.query_range_indices(whole).empty()); EXPECT_TRUE(bvh.query_knn(Point3D(0, 0, 0), 4).empty()); EXPECT_TRUE(bvh.query_knn_indices(Point3D(0, 0, 0), 4).empty()); const Ray3Dd ray(Point3D(0, 0, -1), Vector3D(0, 0, 1)); EXPECT_TRUE(bvh.query_ray(ray).empty()); EXPECT_TRUE(bvh.query_ray_indices(ray).empty()); EXPECT_FALSE(bvh.query_ray_nearest(ray).has_value()); const Triangle3D tri({0, 0, 0}, {1, 0, 0}, {0, 1, 0}); EXPECT_FALSE(bvh.remove(tri)); bvh.clear(); EXPECT_EQ(bvh.size(), 0u); EXPECT_EQ(bvh.index_of(tri), -1); } // ── 深度截断 + 全部拆分策略 (Median 实现一致性) ───────── TEST(CovSpatialBvh, MaxDepthCutoffAndStrategies) { auto tris = make_flat_grid(64, 4.0); for (auto strategy : {BVHSplitStrategy::Middle, BVHSplitStrategy::Equal, BVHSplitStrategy::SAH}) { BVHBuildOptions opts; opts.strategy = strategy; opts.leaf_size = 1; opts.max_depth = 1; // 根节点一次拆分后立即叶子化 BVH bvh(opts); bvh.build(tris); EXPECT_EQ(bvh.size(), 64u); const AABB3D whole(Point3D(-2, -2, -2), Point3D(300, 2, 2)); EXPECT_EQ(bvh.query_range(whole).size(), 64u); // 射线从下方竖直向上, 应命中最后一块网格 (x=252..253) const Ray3Dd up(Point3D(252.5, 0.25, -1), Vector3D(0, 0, 1)); auto nearest = bvh.query_ray_nearest(up); ASSERT_TRUE(nearest.has_value()); EXPECT_NEAR(nearest->t, 1.0, 1e-9); EXPECT_NEAR(nearest->point[2], 0.0, 1e-9); } } // ── KNN: k 边界 + 与暴力最近点交叉验证 ─────────────────── TEST(CovSpatialBvh, KnnBoundsAndBruteForceOrdering) { auto tris = make_flat_grid(8, 10.0); // x = 0,10,...,70 BVH bvh; bvh.build(tris); ASSERT_EQ(bvh.size(), 8u); EXPECT_TRUE(bvh.query_knn(Point3D(0, 0, 0), 0).empty()); EXPECT_TRUE(bvh.query_knn_indices(Point3D(0, 0, 0), 0).empty()); const Point3D q(63.2, 0.25, 4.0); auto knn = bvh.query_knn(q, 100); // k > size, 返回全部 ASSERT_EQ(knn.size(), tris.size()); auto knn_idx = bvh.query_knn_indices(q, 100); ASSERT_EQ(knn_idx.size(), tris.size()); // 距离必须单调递增, 且与暴力最近点结果一致 double prev = -1.0; for (size_t i = 0; i < knn.size(); ++i) { const double d = point_tri_dist_sq(q, knn[i]); EXPECT_GE(d, prev - 1e-12) << "knn must be sorted at i=" << i; prev = d; EXPECT_EQ(brute_force_nearest_index(tris, q), knn_idx[0].first); } EXPECT_EQ(knn_idx[0].first, 6); // 最近网格 x=60..61 // 多采样点覆盖最近点三角的顶点/边/面区域分支 const std::vector probes = { Point3D(0.5, 0.25, 0.0), // 面区域 Point3D(35.0, 2.0, 0.0), // 边区域 (y 超出三角形) Point3D(-3.0, -2.0, 0.0), // 顶点区域 Point3D(80.0, 0.1, 0.0), // 超越最后一格 Point3D(31.5, 0.1, 3.0), // 面之上 }; for (const auto& p : probes) { auto got = bvh.query_knn_indices(p, 1); ASSERT_EQ(got.size(), 1u); EXPECT_EQ(got[0].first, brute_force_nearest_index(tris, p)) << "probe mismatch at (" << p[0] << "," << p[1] << "," << p[2] << ")"; } } // ── 射线: 最近命中场 / 全命中 / 索引 / 重复插入删除 ───── TEST(CovSpatialBvh, RayFieldDataAndDuplicatePrimitives) { const Triangle3D lower({0, 0, 1}, {1, 0, 1}, {0, 1, 1}); const Triangle3D upper({0, 0, 3}, {1, 0, 3}, {0, 1, 3}); BVH bvh; bvh.insert(lower); bvh.insert(upper); bvh.insert(lower); // 重复图元 ASSERT_EQ(bvh.size(), 3u); const Ray3Dd up(Point3D(0.2, 0.2, 0), Vector3D(0, 0, 1)); auto hits = bvh.query_ray(up); EXPECT_EQ(hits.size(), 3u); auto hit_idx = bvh.query_ray_indices(up); ASSERT_EQ(hit_idx.size(), hits.size()); auto nearest = bvh.query_ray_nearest(up); ASSERT_TRUE(nearest.has_value()); EXPECT_NEAR(nearest->t, 1.0, 1e-9); EXPECT_NEAR(nearest->point[2], 1.0, 1e-9); EXPECT_DOUBLE_EQ(nearest->tri.v(0)[2], 1.0); // 反向轴向射线: 最近命中上层 const Ray3Dd down(Point3D(0.2, 0.2, 4), Vector3D(0, 0, -1)); auto nearest_down = bvh.query_ray_nearest(down); ASSERT_TRUE(nearest_down.has_value()); EXPECT_NEAR(nearest_down->point[2], 3.0, 1e-9); // 偏离射线的未命中路径 const Ray3Dd miss(Point3D(5, 5, 0), Vector3D(0, 0, 1)); EXPECT_TRUE(bvh.query_ray(miss).empty()); EXPECT_FALSE(bvh.query_ray_nearest(miss).has_value()); // remove: 每个重复图元各删一次, 第三次失败 EXPECT_TRUE(bvh.remove(lower)); EXPECT_EQ(bvh.size(), 2u); EXPECT_TRUE(bvh.remove(lower)); EXPECT_EQ(bvh.size(), 1u); EXPECT_FALSE(bvh.remove(lower)); EXPECT_EQ(bvh.size(), 1u); EXPECT_TRUE(bvh.remove(upper)); EXPECT_EQ(bvh.size(), 0u); }