Files
hm 35baae1555 test(coverage): add core-plus-v2 profile with plugin/capi/spatial/sdf/compute coverage + 20 boundary cases
Wave 8 W48 (task-123).

Profile: tools/coverage/target_profiles.json gains core-plus-v2 = core-plus 59 targets
+ 37 plugin/capi/spatial/sdf/compute targets (96 total; >=85 required). Excludes
test_curved_boolean_baseline (gcov wall-clock sensitive, same as core-plus) and
test_plugin_ecosystem (asserts all official plugin kinds, needs full plugin build).
line_pct_gate = 52.0 derived from the measured 54.01% (floor - 2pp).

Tests: 5 new files / 20 cases covering plugin lifecycle + capability registry
semantics, C API export boundaries, BVH edge cases, SDF field nodes and compute
SIMD lane boundaries. All 20 pass; module-subset ctest (Plugin|Capi|Spatial|Sdf|
Compute|Brep|Curves|Foundation) = 721/721 passed.

Measurement (build-cov8, RelWithDebInfo + VDE_ENABLE_COVERAGE, MinGW gcc 16.1.0):
before core-plus 50.91% (63374/124474), after core-plus-v2 54.01% (70324/130198),
func 62.88% (3568/5674), branch 33.55%; 2418/2418 cases pass. --update-baseline
wrote tools/coverage/baseline.json core-plus-v2 entry and
--min-line-pct baseline --strict-baseline PASSed (status PASS, line gate 54.01%).

Plugin fixture fix: test_plugin_extensions build deps now include vde_service_runner /
vde_service_stl_convert / vde_service_gcs_solve (3 external-service cases failed when
only the target was built; 9/9 pass after the fix).

CI: nightly coverage-nightly adds a core-plus-v2 report-only archive step and an
opt-in hard gate gated by vars.VDE_ENABLE_CORE_PLUS_V2=1; pr-quality dry-run plan
adds --profile core-plus-v2. Evidence: docs/planning/wave8/cov-notes.md and
build/kcp8_evidence/cov/ (before/after/after-gate reports, ctest logs).

Validation: validate_workflows.py --strict PASS 2/2, vde_release.py --check PASS
(14 checks, 0 fail/0 warn), no product (src/) changes.
2026-11-20 14:55:34 +08:00

240 lines
9.0 KiB
C++

#include <gtest/gtest.h>
#include "vde/spatial/bvh.h"
#include "vde/core/triangle.h"
#include "vde/core/line.h"
#include "vde/core/point.h"
#include <algorithm>
#include <cmath>
#include <limits>
#include <vector>
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<Triangle3D> make_flat_grid(int count, double spacing) {
std::vector<Triangle3D> tris;
tris.reserve(static_cast<size_t>(count));
for (int i = 0; i < count; ++i) {
double x = static_cast<double>(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<Triangle3D>& tris, const Point3D& p) {
int best = -1;
double best_d = std::numeric_limits<double>::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<int>(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<Point3D> 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);
}