Files
hm 3f2a4ac9d4 feat(mesh,spatial,collision): P1/P2 — mesh_boolean 精确 CSG + QEM 体积保持 + 测地标定 + BVH KNN 根因修复 + 球面自适应网格
mesh_boolean.cpp(重写,精确三角网格 CSG):
- 输入多边形面扇形三角化;B 施加 1e-3 rad 泛位置小旋转消除共面/共边退化;
- 逐对 tri-tri 求交(Möller 平面裁剪 + 内半平面裁剪,端点全局容差点池合并保证两侧交线坐标一致);
- 交线+三角形边界(含 T 形接缝点传给相邻面)投影 2D → constrained_delaunay_2d 共形重三角化(覆盖性校验,
  失败回退不分裂);子三角质心 3 方向射线奇偶投票分类;按 Union/Intersection/Difference 选择(difference
  反转 B 侧);焊接 + 边界环扇形回填 + BFS 朝向/腔体嵌套定向 + 非流形边定向修复。
- 验收:两盒(各 1、重叠 0.5)union 1.5001038(rel 6.9e-5)/intersection 0.4998238/inter 0.5(rel 3.5e-4)/
  difference 0.5001762,boundary=0、非流形边=0、Euler=2(修复前 4.0/0.333、boundary 72/24/12);
  错位盒/一大一小/全包含用例 rel~1e-6~1e-5 且水密。test_mesh_boolean 16/16。

mesh_simplify.cpp(QEM 重写):
- 根因:Quadric::optimal 3×3 逆矩阵第 2/3 行公式错(最优点飞远被 clamp)、塌缩位置用原始坐标(忽略已合并
  聚类当前位置)、无流形 link 条件/体积守卫、面表按原始面+代表点重建会静默丢非流形面。
- 修复:显式索引网格塌缩(位置/面表/邻接动态维护、惰性堆+版本失效)、修正 QEM 求解、link condition、
  翻转/退化守卫、逐塌缩体积线性校正 + 4% 累计预算 + 闭合网格体积归一。
- 验收(audit 口径 MC 球 2312 面 V=4.149678):70% 简化 644 面 V=0.92519 drift 77.704% → 692 面
  **drift 0.0000%**;80% 简化 416 面 drift 87.74% → 462 面 **0.0000%**;boundary=0、degen=0、Euler=2。

geodesic.cpp(热法重写):
- 根因:源点钉零只清列(列主序 InnerIterator(outer=si) 实际遍历列 si)→ 源点行仍耦合;梯度先面积加权平均到
  顶点再归一化(额外平滑);纯 t=h² 在直径≫50h 细网格下溢。
- 修复:逐面梯度归一化 X_f=-∇u/|∇u|(Crane 原文)、源点行+列真 Dirichlet 钉零(LDLT,LU 回退)、
  自适应 t=max(h²,(diam/24)²)、极粗网格 1→4 中点细分后求解(原顶点索引保留)。
- 验收:8 顶点立方体 d[opposite] 1.5891(-28.9%) → **2.1954(-1.82%,√5=2.2361,目标±5%)**;
  平面 20×20 角点 +0.69%/中心 +0.61%;UV 球 48×32 南极 +0.15%/最大大圆误差 0.25%;96×64 ≤0.06%。

collision/closest_point.cpp(审计根因纠错):
- BVH query_knn 本身已精确(与正确暴力解 worst=0),审计失败根因是参考实现 closest_point_triangle 的
  Ericson RTCD 5.1.5 抄写错误:edge BC 分支条件/基向量错(应为 edge AC: vb<=0 && d2>=0 && d6<=0 →
  a+ac·w),返回三角形外点且距离偏小。修正后 tri#203 与 core::closest_point 完全一致;
  300 三角形审计场景 worst 0.2056188993→0;1000×100 随机查询 ≤1e-12(0 mismatch)。

brep_to_mesh.cpp(球面自适应网格 + trim-domain):
- 球面 patch 专用自适应路径:最小二乘拟合球心/半径 + 3D 弦高倍增确定全局统一 u/v 段数(相邻 patch 公共边
  采样点逐位重合)+ 极点退化行按位置合并为单顶点扇面三角化(零面积三角形不输出)。
  make_sphere(5,24,12).to_mesh_adaptive(0.01):tris 831,744→**9,024**、verts 438,048→7,008、
  degenerate 1824→**0**、watertight 0→1。
- build_face_trim_domain:存储 TS 一致性校验(p-curve 端点 evaluate→3D 与 TopoLoop 顶点比对,仅平面面),
  不一致按环重建(修复 heal reversed 补片 UV 域错误),曲线边采样上限 256→自适应 12..2048。

测试:test_mesh_boolean 16/16、test_geodesic 8/8、test_mesh_lod 16/16、test_closest_point 8/8、
test_gjk 17/17、test_bvh 22/22、test_mesh_quality 22、test_mesh_robustness 145、test_brep_blend 14 等全绿。
2026-10-25 20:56:52 +08:00

186 lines
8.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <gtest/gtest.h>
#include "vde/collision/closest_point.h"
#include "vde/spatial/bvh.h"
#include <cmath>
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<Triangle3D> 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<Triangle3D> 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 <array>
#include <random>
namespace {
// 3D 点相对于三角形平面的重心坐标 (u,v,w),p = u*a + v*b + w*c,u+v+w=1。
std::array<double, 3> 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<Case> 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<double> u(-5.0, 5.0);
std::vector<Triangle3D> 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;
}