Files
ViewDesignEngine/tests/mesh/test_facet_tessellation.cpp
hm 23f9467721 feat(1.0.179): 网格水密指标、shell_multi validate 门控与网格焊接入口一致性
VDE-1350 网格水密指标:mesh_repair.h 新增 MeshWatertightness 与
analyze_mesh_watertightness 两条重载(焊接顶点/边界环/非流形边/重复面/
有向体积/共享边比率);mesh_healing_report 的 hole_count 由 boundary_edges/3
启发式改为边界顶点 DSU 连通分量。新增 test_mesh_watertight 8 例 +
双孔洞 1 例。

VDE-1351 shell_multi 门控:独立验证 229 P2-1 证实 is_valid() 只做基本检查,
L 形/圆柱/球坏体仍非空返回且旧库数值相同。新增 shell_result_acceptable()
(validate() + 水密率=1 + 无悬挂边/非流形边/夹边),等厚与不等厚两路径统一;
测试改名 InvalidShellReturnsEmptyModel 并断言空模型。

VDE-1352 恢复 topo_naming.h BOM;勘误 228 报告行数/BOM 口径与 CHANGELOG
1.0.178 的回归编号、告警口径。

VDE-1353 网格焊接入口一致性:合并上游 71 提交后 20 项 CTest 失败,根因是
上游只给串行 to_mesh 加容差焊接,tagged/parallel(两分支)/soup/可见性重载
均未焊接,同一体各入口顶点数分叉;另原容差用 max|coord|*1e-6,1e15 远体
eps=1e6 致整球塌缩。焊接容差改为包围盒相对口径 eps=max(1e-9*diag,1e-12)、
格点以包围盒最小角为原点;五个入口全部补齐焊接;to_mesh_parallel 大网格
焊接后改用确定性串行 build_from_triangles;过期断言更新(box 24→8、facet
与 mesh→brep 唯一顶点数、ctypes smoke)。曾失败 5 组 46 项修复后 46/46。

测试:注册 8,381→8,414(合并上游 +33,本版新增 9)。
全量 CTest(Release -j8 三段 1-2805/2806-5610/5611-8414):8,414 执行、
8,379 passed、0 failed、35 SKIP、583.30 s;build 0 error。
release --check 14/14 PASS、--report-check PASS(latest=230)。
报告:docs/reference/230-VDE1.0.179-…自验证报告.md;229(1.0.178 独立验证)
随版入库。版本链 6 处 bump 1.0.179。
2026-11-07 14:40:21 +08:00

216 lines
9.3 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.
// VDE-1302:mesh_to_brep 三角面片(facet)零分配快速路径回归。
//
// facet 面 = 单环 3 条隐式直线边 + degree 1×1 的退化 2×2 控制网。此类面在
// to_mesh 中不再走 UV 投影 + constrained_delaunay_2d(每次都分配 std::map/
// vector),而是按环序直接输出一个三角形。几何与旧 CDT 路径完全一致(同一
// 三角形、法向同向),仅顶点顺序定义为「环序相对面法向按要求反向」。
#include <gtest/gtest.h>
#include "vde/brep/brep.h"
#include "vde/brep/mesh_to_brep.h"
#include "vde/mesh/halfedge_mesh.h"
#include <algorithm>
#include <array>
#include <cmath>
#include <string>
#include <vector>
using namespace vde::brep;
using namespace vde::mesh;
using namespace vde::core;
namespace {
BrepModel facet_body(const std::vector<Point3D>& verts,
const std::vector<std::array<int, 3>>& tris) {
HalfedgeMesh mesh;
mesh.build_from_triangles(verts, tris);
return mesh_to_brep_model(mesh);
}
// 复刻 vde_fast_loop_ring 对 facet 环的走法:从 loop.edges[0] 的 v_start
// 起沿环边逐条前进,每个直线边贡献它的起点;结果即快速路径的三角顶点序。
std::vector<int> facet_ring_vertices(const BrepModel& body, int face_index) {
const auto& face = body.face(face_index);
if (face.loops.size() != 1) return {};
const auto& loop = body.loop_by_id(face.loops[0]);
if (loop.edges.size() != 3) return {};
std::vector<int> ring;
ring.reserve(3);
int cur = body.edge(loop.edges[0]).v_start;
for (int eid : loop.edges) {
const auto& e = body.edge(eid);
if (e.v_start != cur && e.v_end != cur) return {};
ring.push_back(cur);
cur = (e.v_start == cur) ? e.v_end : e.v_start;
}
if (cur != ring[0]) return {};
return ring;
}
// 快速路径的输出顶点序:环序相对面法向为 CW 时反向,否则保持环序。
std::vector<Point3D> expected_facet_order(const BrepModel& body, int face_index,
Vector3D normal) {
const std::vector<int> ring = facet_ring_vertices(body, face_index);
EXPECT_EQ(ring.size(), 3u) << "face " << face_index;
if (ring.size() != 3) return {};
const Point3D p0 = body.vertex(ring[0]).point;
const Point3D p1 = body.vertex(ring[1]).point;
const Point3D p2 = body.vertex(ring[2]).point;
const double signed_area2 = (p1 - p0).cross(p2 - p0).dot(normal);
if (signed_area2 < 0.0) return {p2, p1, p0};
return {p0, p1, p2};
}
void expect_same_point(const Point3D& actual, const Point3D& expected,
const std::string& label) {
EXPECT_DOUBLE_EQ(actual.x(), expected.x()) << label;
EXPECT_DOUBLE_EQ(actual.y(), expected.y()) << label;
EXPECT_DOUBLE_EQ(actual.z(), expected.z()) << label;
}
} // namespace
TEST(FacetTessellation, FastPathKeepsReversedLoopOrder) {
const Point3D a(0.0, 0.0, 0.0);
const Point3D b(1.0, 0.0, 0.0);
const Point3D c(0.0, 1.0, 0.0);
const BrepModel body = facet_body({a, b, c}, {{{0, 1, 2}}});
ASSERT_EQ(body.num_faces(), 1u);
ASSERT_EQ(body.num_edges(), 3u);
ASSERT_EQ(body.num_vertices(), 3u);
Vector3D n = body.face_normal(0);
ASSERT_GT(n.norm(), 0.0);
n = n.normalized();
const std::vector<Point3D> expected = expected_facet_order(body, 0, n);
ASSERT_EQ(expected.size(), 3u);
const HalfedgeMesh mesh = body.to_mesh(0.01);
ASSERT_EQ(mesh.num_faces(), 1u);
ASSERT_EQ(mesh.num_vertices(), 3u);
for (size_t k = 0; k < 3; ++k)
expect_same_point(mesh.vertex(static_cast<int>(k)), expected[k],
"vertex " + std::to_string(k));
// HalfedgeMesh::face_vertices 返回的是各半边终点,故三角形顶点集合是
// 同一循环的一个旋转;这里只要求恰好覆盖 0/1/2(几何顺序已在上方逐点校验)。
const auto fv = mesh.face_vertices(0);
ASSERT_EQ(fv.size(), 3u);
std::vector<int> sorted_fv(fv.begin(), fv.end());
std::sort(sorted_fv.begin(), sorted_fv.end());
EXPECT_EQ(sorted_fv, (std::vector<int>{0, 1, 2}));
// 输出三角形朝向与面法向同向(与旧 CDT 路径一致)
const Vector3D tn =
(mesh.vertex(1) - mesh.vertex(0)).cross(mesh.vertex(2) - mesh.vertex(0));
EXPECT_GT(tn.norm(), 0.0);
EXPECT_GT(tn.normalized().dot(n), 0.999999);
}
TEST(FacetTessellation, GridFacetsKeepReversedLoopOrderPerFace) {
// 3×3 顶点 / 2×2 四边形 → 8 个 facet 面;逐面校验顶点序与朝向。
const int seg = 2;
std::vector<Point3D> verts;
for (int j = 0; j <= seg; ++j)
for (int i = 0; i <= seg; ++i)
verts.emplace_back(i * 0.1, j * 0.1, 0.0);
auto vid = [&](int i, int j) { return j * (seg + 1) + i; };
std::vector<std::array<int, 3>> tris;
for (int j = 0; j < seg; ++j)
for (int i = 0; i < seg; ++i) {
tris.push_back({vid(i, j), vid(i + 1, j), vid(i + 1, j + 1)});
tris.push_back({vid(i, j), vid(i + 1, j + 1), vid(i, j + 1)});
}
const BrepModel body = facet_body(verts, tris);
ASSERT_EQ(body.num_faces(), tris.size());
const HalfedgeMesh mesh = body.to_mesh(0.2);
ASSERT_EQ(mesh.num_faces(), tris.size());
// VDE-1353:to_mesh 按容差焊接重合顶点(上游 76786237/dbe2cfd2 语义)。
// 2×2 四边形共享 3×3=9 个网格点,不再每面复制 3 个顶点。
ASSERT_EQ(mesh.num_vertices(), static_cast<size_t>((seg + 1) * (seg + 1)));
double area = 0.0;
for (size_t fi = 0; fi < tris.size(); ++fi) {
Vector3D n = body.face_normal(static_cast<int>(fi));
ASSERT_GT(n.norm(), 0.0) << "face " << fi;
n = n.normalized();
const std::vector<Point3D> expected =
expected_facet_order(body, static_cast<int>(fi), n);
ASSERT_EQ(expected.size(), 3u) << "face " << fi;
const std::vector<int> fv = mesh.face_vertices(static_cast<int>(fi));
ASSERT_EQ(fv.size(), 3u) << "face " << fi;
// 焊接后 face_vertices 是同一循环的某个旋转起点,按循环序逐点比较
// (旋转不改变绕向;绕向随后用三角法向单独校验)。
std::size_t start = 3;
for (std::size_t j = 0; j < 3; ++j) {
const Point3D& p = mesh.vertex(fv[j]);
if (p.x() == expected[0].x() && p.y() == expected[0].y() &&
p.z() == expected[0].z()) {
start = j;
break;
}
}
ASSERT_LT(start, 3u) << "face " << fi;
for (std::size_t k = 0; k < 3; ++k)
expect_same_point(mesh.vertex(fv[(start + k) % 3]), expected[k],
"face " + std::to_string(fi) + " vertex " +
std::to_string(k));
const Point3D p0 = mesh.vertex(fv[start]);
const Point3D p1 = mesh.vertex(fv[(start + 1) % 3]);
const Point3D p2 = mesh.vertex(fv[(start + 2) % 3]);
const Vector3D tn = (p1 - p0).cross(p2 - p0);
EXPECT_GT(tn.norm(), 0.0) << "face " << fi;
EXPECT_GT(tn.normalized().dot(n), 0.999999) << "face " << fi;
area += 0.5 * tn.norm();
}
// 2×2 个 0.1×0.1 四边形 = 0.04
EXPECT_NEAR(area, 0.04, 1e-12);
}
TEST(FacetTessellation, CollinearFacetIsFilteredNotCrashing) {
// 共线三角面片:不满足快速路径的非退化门禁,交回旧路径并最终由公共
// 退化三角形过滤器丢弃,不应崩溃或输出 NaN 顶点。
const BrepModel body = facet_body(
{Point3D(0.0, 0.0, 0.0), Point3D(1.0, 0.0, 0.0), Point3D(2.0, 0.0, 0.0)},
{{{0, 1, 2}}});
const HalfedgeMesh mesh = body.to_mesh(0.1);
EXPECT_EQ(mesh.num_faces(), 0u);
}
TEST(FacetTessellation, ParallelMatchesSerialOnFacetGrid) {
const int seg = 12; // 12×12 四边形 → 288 个 facet 面
std::vector<Point3D> verts;
for (int j = 0; j <= seg; ++j)
for (int i = 0; i <= seg; ++i)
verts.emplace_back(i * 0.05, j * 0.05, 0.0);
auto vid = [&](int i, int j) { return j * (seg + 1) + i; };
std::vector<std::array<int, 3>> tris;
for (int j = 0; j < seg; ++j)
for (int i = 0; i < seg; ++i) {
tris.push_back({vid(i, j), vid(i + 1, j), vid(i + 1, j + 1)});
tris.push_back({vid(i, j), vid(i + 1, j + 1), vid(i, j + 1)});
}
const BrepModel body = facet_body(verts, tris);
const HalfedgeMesh serial = body.to_mesh(0.2);
ASSERT_EQ(serial.num_faces(), tris.size());
for (int threads : {2, 8}) {
const HalfedgeMesh par = body.to_mesh_parallel(0.2, threads);
ASSERT_EQ(par.num_faces(), serial.num_faces()) << "threads=" << threads;
ASSERT_EQ(par.num_vertices(), serial.num_vertices())
<< "threads=" << threads;
for (size_t vi = 0; vi < serial.num_vertices(); ++vi)
expect_same_point(par.vertex(static_cast<int>(vi)),
serial.vertex(static_cast<int>(vi)),
"threads " + std::to_string(threads) + " v" +
std::to_string(vi));
for (size_t fi = 0; fi < serial.num_faces(); ++fi) {
const auto fa = serial.face_vertices(static_cast<int>(fi));
const auto fb = par.face_vertices(static_cast<int>(fi));
ASSERT_EQ(fb, fa) << "threads " << threads << " face " << fi;
}
}
}