#include #include "vde/collision/closest_point.h" #include "vde/spatial/bvh.h" #include using namespace vde::collision; using namespace vde::core; using namespace vde::spatial; // ═══════════════════════════════════════════════════════════ // VDE-116: closest_point 测试 // ═══════════════════════════════════════════════════════════ TEST(ClosestPointTest, TriangleVertexRegion) { // 三角形在 XY 平面 Triangle3D tri(Point3D(0, 0, 0), Point3D(10, 0, 0), Point3D(0, 10, 0)); // 查询点在 v0 的 Voronoi 区域(投影落在 v0 附近外侧) Point3D query(-1.0, -1.0, 0.0); Point3D cp = closest_point_triangle(query, tri); EXPECT_NEAR(cp.x(), 0.0, 1e-9); EXPECT_NEAR(cp.y(), 0.0, 1e-9); EXPECT_NEAR(cp.z(), 0.0, 1e-9); } TEST(ClosestPointTest, TriangleEdgeRegion) { // 查询点投影落在边 AB 的外侧 Triangle3D tri(Point3D(0, 0, 0), Point3D(10, 0, 0), Point3D(0, 10, 0)); Point3D query(5.0, -2.0, 0.0); Point3D cp = closest_point_triangle(query, tri); // 最近点应在边 AB 上 (5,0,0) EXPECT_NEAR(cp.x(), 5.0, 1e-9); EXPECT_NEAR(cp.y(), 0.0, 1e-9); EXPECT_NEAR(cp.z(), 0.0, 1e-9); } TEST(ClosestPointTest, TriangleFaceRegion) { // 查询点投影落在三角形内部 Triangle3D tri(Point3D(0, 0, 0), Point3D(10, 0, 0), Point3D(0, 10, 0)); Point3D query(2.0, 2.0, 5.0); Point3D cp = closest_point_triangle(query, tri); // 最近点应为垂直投影到平面的点 EXPECT_NEAR(cp.x(), 2.0, 1e-9); EXPECT_NEAR(cp.y(), 2.0, 1e-9); EXPECT_NEAR(cp.z(), 0.0, 1e-9); } TEST(ClosestPointTest, MeshBruteForce) { // 两个分离的三角形 std::vector tris = { Triangle3D(Point3D(0, 0, 0), Point3D(1, 0, 0), Point3D(0, 1, 0)), Triangle3D(Point3D(10, 0, 0), Point3D(11, 0, 0), Point3D(10, 1, 0)), }; // 查询点靠近第二个三角形 Point3D query(10.5, 0.2, 0.5); Point3D cp = closest_point_mesh(query, tris); EXPECT_NEAR(cp.x(), 10.5, 1e-6); EXPECT_NEAR(cp.y(), 0.2, 1e-6); EXPECT_NEAR(cp.z(), 0.0, 1e-6); // distance_to_mesh 一致性 double d = distance_to_mesh(query, tris); EXPECT_NEAR(d, 0.5, 1e-9); } TEST(ClosestPointTest, MeshBVHMatchesBruteForce) { // 构建一个 2×2 三角形网格(4 个三角形组成正方形) std::vector tris = { Triangle3D(Point3D(0, 0, 0), Point3D(1, 0, 0), Point3D(1, 1, 0)), Triangle3D(Point3D(0, 0, 0), Point3D(1, 1, 0), Point3D(0, 1, 0)), Triangle3D(Point3D(1, 0, 0), Point3D(2, 0, 0), Point3D(2, 1, 0)), Triangle3D(Point3D(1, 0, 0), Point3D(2, 1, 0), Point3D(1, 1, 0)), }; BVH bvh; bvh.build(tris); // 多个查询点验证 BVH 与暴力结果一致 for (int i = 0; i <= 10; ++i) { for (int j = 0; j <= 10; ++j) { Point3D query(i * 0.2, j * 0.2, 1.5); Point3D cp_brute = closest_point_mesh(query, tris); Point3D cp_bvh = closest_point_mesh(query, tris, bvh); double dist_diff = (cp_brute - cp_bvh).norm(); EXPECT_LT(dist_diff, 1e-6) << "BVH/brute mismatch at (" << i * 0.2 << "," << j * 0.2 << ")"; } } } // ═══════════════════════════════════════════════════════════════════════ // task-38 回归:collision::closest_point_triangle 参考实现缺陷 // 旧实现把 edge-AC 分支写错(条件/基向量都不对),当三角形在查询点方向 // 近乎退化(va+vb+vc 很小时)还会走 face 分支得到爆炸的重心坐标,返回 // 三角形外的点、距离比真实最近距离更小。audit_mesh 的 spatial.bvh_knn // 正是拿它当参考,导致 BVH 被误判 FAIL(BVH 本身是精确的)。 // ═══════════════════════════════════════════════════════════════════════ #include "vde/core/distance.h" #include #include namespace { // 3D 点相对于三角形平面的重心坐标 (u,v,w),p = u*a + v*b + w*c,u+v+w=1。 std::array tri_bary(const Point3D& p, const Triangle3D& t) { const Vector3D v0 = t.v(1) - t.v(0); const Vector3D v1 = t.v(2) - t.v(0); const Vector3D v2 = p - t.v(0); const double d00 = v0.dot(v0), d01 = v0.dot(v1), d11 = v1.dot(v1); const double d20 = v2.dot(v0), d21 = v2.dot(v1); const double den = d00 * d11 - d01 * d01; if (std::abs(den) < 1e-300) return {1.0, 0.0, 0.0}; const double v = (d11 * d20 - d01 * d21) / den; const double w = (d00 * d21 - d01 * d20) / den; return {1.0 - v - w, v, w}; } } // namespace // Ericson 标准区域用例:顶点/三条边/面区域都必须返回解析最近点。 TEST(ClosestPointTest, EricsonRegionsEdgeACIncluded) { const Triangle3D tri(Point3D(0, 0, 0), Point3D(1, 0, 0), Point3D(0, 1, 0)); struct Case { Point3D q; Point3D expect; }; const std::vector cases = { {Point3D(-1, -1, 0), Point3D(0, 0, 0)}, // vertex A {Point3D(2, -1, 0), Point3D(1, 0, 0)}, // vertex B {Point3D(-1, 2, 0), Point3D(0, 1, 0)}, // vertex C {Point3D(0.5, -1, 0), Point3D(0.5, 0, 0)}, // edge AB {Point3D(-1, 0.5, 0), Point3D(0, 0.5, 0)}, // edge AC (旧 bug 分支) {Point3D(1, 1, 0), Point3D(0.5, 0.5, 0)}, // edge BC {Point3D(0.2, 0.2, 1), Point3D(0.2, 0.2, 0)} // face }; for (const auto& c : cases) { const Point3D got = closest_point_triangle(c.q, tri); EXPECT_LE((got - c.expect).norm(), 1e-12) << "q=(" << c.q.x() << "," << c.q.y() << "," << c.q.z() << ")"; } } // 具体反例 tri#203(audit rng 第 203 颗三角形):旧实现返回 (0.1,0.2,0.260) // 这种 x 完全在三角形外的点;修复后必须落在三角形内/边上且与 core 参考一致。 TEST(ClosestPointTest, RegressionTri203ClosestPointOnTriangle) { const Triangle3D tri(Point3D(0.992612, 0.147741, 0.260454), Point3D(1.07261, 0.147741, 0.260454), Point3D(0.992612, 0.227741, 0.260454)); const Point3D q(0.1, 0.2, 0.3); const Point3D got = closest_point_triangle(q, tri); const Point3D ref = vde::core::closest_point(q, tri); EXPECT_LE((got - ref).norm(), 1e-12) << "closest point 必须与 core 参考一致"; // 必须落在三角形内/边上(重心坐标均落在 [0,1]) const auto bar = tri_bary(got, tri); for (double c : bar) { EXPECT_GE(c, -1e-9); EXPECT_LE(c, 1.0 + 1e-9); } // 距离必须与 vde::core::distance 一致(旧实现会偏小) EXPECT_NEAR((got - q).norm(), vde::core::distance(q, tri), 1e-12); // 且旧 bug 的 x 坐标错误不可能重现(x 应落在 [0.9926, 1.0726]) EXPECT_GE(got.x(), 0.992612 - 1e-9); } // 随机 1000 三角形 × 100 查询点:与 vde::core::closest_point(独立实现)逐点一致。 TEST(ClosestPointTest, Random1000Triangles100QueriesMatchesCore) { std::mt19937 gen(20261025u); std::uniform_real_distribution u(-5.0, 5.0); std::vector tris; tris.reserve(1000); for (int i = 0; i < 1000; ++i) { tris.push_back(Triangle3D(Point3D(u(gen), u(gen), u(gen)), Point3D(u(gen), u(gen), u(gen)), Point3D(u(gen), u(gen), u(gen)))); } int mismatches = 0; double worst = 0.0; for (int qi = 0; qi < 100; ++qi) { const Point3D q(u(gen), u(gen), u(gen)); for (const auto& t : tris) { const Point3D got = closest_point_triangle(q, t); const Point3D ref = vde::core::closest_point(q, t); const double diff = (got - ref).norm(); worst = std::max(worst, diff); if (diff > 1e-12) ++mismatches; } } EXPECT_EQ(mismatches, 0) << "worst=" << worst; }