Files
ViewDesignEngine/tests/mesh/test_tessellation_parallel.cpp
hm 1ae5e94399 perf(brep,foundation): facet zero-alloc tessellation + export parallel dispatch (1.0.157)
VDE-1301: export COLLADA/AMF/VRML tessellate via io_detail::tessellate_body
(<256 faces serial, else to_mesh_parallel(deflection, min(hw,16))).

VDE-1302: mesh->B-Rep facet faces (single loop, 3 implicit-line edges,
2x2 degenerate triangle control net) emit triangles directly in
vde_fast_planar_trim_mesh, skipping ring sampling / 3D->2D projection /
constrained_delaunay_2d. to_mesh 100k facet 670.6->88.0ms (7.6x),
300k facet 3339.9->214.3ms (15.6x), parallel16 2806.3->169.3ms (16.6x).

VDE-1303: body_has_freeform_trim_evidence skips face_type for trivially
planar faces (1x1 bilinear + coplanar control points); edge scan first;
per-face getenv hoisted. 300k-body check 652-677ms -> ~10-20ms, DANGER=0
across primitives and 249MB real STEP (60029 faces), 32 body meshes
byte-identical (serial/parallel/soup).

Tests: +5 (facet tessellation x4, 800-face parallel match x1), 8190 total;
full CTest 8190/8190 PASS 0 failed 35 skipped 758.92s; build 389 targets
0 warning. Report 185; verifier report 184 (1.0.156) committed with this
release. Merge upstream origin/main 25adb7d0 (open-body diagnostic skip,
--dump-faces, diag error counting).
2026-11-05 04:32:22 +08:00

285 lines
12 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.
// Phase5-TessPerf:真正并行三角化(to_mesh_parallel)正确性回归。
//
// 目标:serial `to_mesh(deflection)` 与 OpenMP 按面并行 `to_mesh_parallel(deflection, n)`
// 输出一致(顶点/三角形/面积/bbox/朝向),并覆盖边界与裁剪面模型。
#include <gtest/gtest.h>
#include "vde/brep/brep.h"
#include "vde/brep/modeling.h"
#include "vde/brep/mesh_to_brep.h"
#include "vde/mesh/halfedge_mesh.h"
#include <algorithm>
#include <array>
#include <cmath>
#include <string>
#include <utility>
#include <vector>
using namespace vde::brep;
using namespace vde::mesh;
using namespace vde::core;
namespace {
struct MeshSummary {
size_t verts = 0;
size_t tris = 0;
size_t degenerate = 0;
double area = 0.0;
Point3D lo{0, 0, 0};
Point3D hi{0, 0, 0};
};
MeshSummary summarize(const HalfedgeMesh& m) {
MeshSummary s;
s.verts = m.num_vertices();
s.tris = m.num_faces();
if (s.verts == 0) return s;
s.lo = m.vertex(0);
s.hi = m.vertex(0);
for (size_t vi = 1; vi < s.verts; ++vi) {
const auto& p = m.vertex(vi);
s.lo = s.lo.cwiseMin(p);
s.hi = s.hi.cwiseMax(p);
}
for (size_t fi = 0; fi < s.tris; ++fi) {
auto fv = m.face_vertices(static_cast<int>(fi));
if (fv.size() < 3) continue;
const Point3D a = m.vertex(fv[0]);
const Point3D b = m.vertex(fv[1]);
const Point3D c = m.vertex(fv[2]);
const double area = 0.5 * (b - a).cross(c - a).norm();
if (area < 1e-15) ++s.degenerate;
s.area += area;
}
return s;
}
void expect_mesh_equal(const HalfedgeMesh& serial, const HalfedgeMesh& par,
const std::string& label) {
ASSERT_EQ(par.num_vertices(), serial.num_vertices()) << label;
ASSERT_EQ(par.num_faces(), serial.num_faces()) << label;
for (size_t vi = 0; vi < serial.num_vertices(); ++vi) {
const Point3D a = serial.vertex(static_cast<int>(vi));
const Point3D b = par.vertex(static_cast<int>(vi));
ASSERT_NEAR(a.x(), b.x(), 1e-12) << label << " v" << vi;
ASSERT_NEAR(a.y(), b.y(), 1e-12) << label << " v" << vi;
ASSERT_NEAR(a.z(), b.z(), 1e-12) << label << " v" << vi;
}
for (size_t fi = 0; fi < serial.num_faces(); ++fi) {
auto fa = serial.face_vertices(static_cast<int>(fi));
auto fb = par.face_vertices(static_cast<int>(fi));
ASSERT_EQ(fb.size(), fa.size()) << label << " f" << fi;
for (size_t k = 0; k < fa.size(); ++k)
ASSERT_EQ(fa[k], fb[k]) << label << " f" << fi << " k" << k;
}
const MeshSummary ss = summarize(serial);
const MeshSummary ps = summarize(par);
EXPECT_EQ(ps.degenerate, ss.degenerate) << label;
EXPECT_GT(ss.tris, 0u) << label;
const double rel_area =
std::abs(ps.area - ss.area) / std::max(1.0e-300, ss.area);
EXPECT_LE(rel_area, 1e-12) << label << " area rel=" << rel_area;
EXPECT_NEAR(ps.lo.x(), ss.lo.x(), 1e-12) << label;
EXPECT_NEAR(ps.lo.y(), ss.lo.y(), 1e-12) << label;
EXPECT_NEAR(ps.lo.z(), ss.lo.z(), 1e-12) << label;
EXPECT_NEAR(ps.hi.x(), ss.hi.x(), 1e-12) << label;
EXPECT_NEAR(ps.hi.y(), ss.hi.y(), 1e-12) << label;
EXPECT_NEAR(ps.hi.z(), ss.hi.z(), 1e-12) << label;
// 朝向抽样:同一三角形的法向完全一致(串行/并行同源)
const size_t step = std::max<size_t>(1, serial.num_faces() / 64);
for (size_t fi = 0; fi < serial.num_faces(); fi += step) {
auto f = serial.face_vertices(static_cast<int>(fi));
if (f.size() < 3) continue;
const Point3D a = serial.vertex(f[0]);
const Vector3D n = (serial.vertex(f[1]) - a).cross(serial.vertex(f[2]) - a);
auto g = par.face_vertices(static_cast<int>(fi));
const Vector3D m = (par.vertex(g[1]) - par.vertex(g[0])).cross(
par.vertex(g[2]) - par.vertex(g[0]));
EXPECT_NEAR(n.norm(), m.norm(), 1e-12) << label << " f" << fi;
if (n.norm() < 1e-15 || m.norm() < 1e-15) continue; // 退化三角形两侧一致
EXPECT_NEAR(n.normalized().dot(m.normalized()), 1.0, 1e-12)
<< label << " f" << fi;
}
}
} // namespace
TEST(TessellationParallel, MatchesSerialOnPrimitives) {
const std::pair<std::string, BrepModel> cases[] = {
{"box", make_box(2.0, 1.0, 3.0)},
{"sphere", make_sphere(1.0, 16, 8)},
{"cylinder_exact", make_cylinder(1.0, 2.0, 0)},
{"cylinder_seg32", make_cylinder(1.0, 2.0, 32)},
{"cone", make_cone(1.0, 0.2, 2.0, 16)},
{"torus", make_torus(3.0, 1.0, 16, 8)},
};
for (const auto& [name, body] : cases) {
for (double deflection : {0.2, 0.05}) {
if (body.num_faces() == 0) continue;
const auto serial = body.to_mesh(deflection);
const auto par = body.to_mesh_parallel(deflection, 8);
expect_mesh_equal(serial, par, name + "/def" + std::to_string(deflection));
// 线程数不影响结果(1/2/8 一致)
const auto p1 = body.to_mesh_parallel(deflection, 1);
const auto p2 = body.to_mesh_parallel(deflection, 2);
expect_mesh_equal(serial, p1, name + "/threads1");
expect_mesh_equal(serial, p2, name + "/threads2");
}
}
}
TEST(TessellationParallel, MatchesSerialOnManyFaceMesh) {
// VDE-1301:>256 面走分块并行路径(小面数模型走串行快速路径)。
// 800 个三角 facet 面 → 验证块内顺序网格化 + 按块确定性合并与串行
// to_mesh 逐顶点/逐面一致(导出器网格生成的分派门禁)。
const int seg = 20; // 20×20 四边形 → 800 三角形面
std::vector<Point3D> verts;
std::vector<std::array<int, 3>> tris;
verts.reserve(static_cast<size_t>((seg + 1) * (seg + 1)));
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; };
tris.reserve(static_cast<size_t>(seg * seg * 2));
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)});
}
}
HalfedgeMesh mesh;
mesh.build_from_triangles(verts, tris);
const BrepModel body = mesh_to_brep_model(mesh);
ASSERT_EQ(body.num_faces(), tris.size());
ASSERT_GT(body.num_faces(), 256u) << "需覆盖分块并行路径";
const auto serial = body.to_mesh(0.2);
ASSERT_EQ(serial.num_faces(), tris.size());
for (int threads : {2, 4, 8}) {
const auto par = body.to_mesh_parallel(0.2, threads);
expect_mesh_equal(serial, par,
"grid800/t" + std::to_string(threads));
}
}
TEST(TessellationParallel, TrimmedHoleModelMatchesSerial) {
// 平面上打穿一个孔 → 裁剪面 + 多环,覆盖 mesh_face_trimmed 复用路径
BrepModel box = make_box(4.0, 4.0, 2.0);
HoleSpec spec;
spec.diameter = 1.0;
spec.depth = 0.0; // through hole
BrepModel holed = make_hole(box, Point3D(0.0, 0.0, 1.0),
Vector3D(0.0, 0.0, -1.0), spec);
ASSERT_GT(holed.num_faces(), 0u);
const auto serial = holed.to_mesh(0.2);
const auto par = holed.to_mesh_parallel(0.2, 8);
ASSERT_GT(serial.num_faces(), 0u);
expect_mesh_equal(serial, par, "box_with_through_hole");
}
TEST(TessellationParallel, SafetyEdgeCases) {
// 空模型
BrepModel empty;
EXPECT_EQ(empty.to_mesh_parallel(8).num_faces(), 0u);
EXPECT_EQ(empty.to_mesh_parallel(0.1, 0).num_faces(), 0u);
// 非法线程数 clamp:1 / 0 / 负数 不崩且结果一致
BrepModel box = make_box(1.0, 1.0, 1.0);
const auto serial = box.to_mesh(0.1);
for (int threads : {1, 0, -3, 100}) {
const auto par = box.to_mesh_parallel(0.1, threads);
EXPECT_EQ(par.num_faces(), serial.num_faces()) << "threads=" << threads;
EXPECT_EQ(par.num_vertices(), serial.num_vertices()) << "threads=" << threads;
}
// 旧接口(int 重载)与显式 deflection=0.1 等价
const auto legacy = box.to_mesh_parallel(4);
const auto explicit_def = box.to_mesh_parallel(0.1, 4);
EXPECT_EQ(legacy.num_faces(), explicit_def.num_faces());
EXPECT_EQ(legacy.num_vertices(), explicit_def.num_vertices());
}
TEST(TessellationParallel, TriangleSoupMatchesMeshGeometry) {
const std::pair<std::string, BrepModel> cases[] = {
{"box", make_box(2.0, 1.0, 3.0)},
{"sphere", make_sphere(1.0, 16, 8)},
{"cylinder_exact", make_cylinder(1.0, 2.0, 0)},
{"torus", make_torus(3.0, 1.0, 16, 8)},
};
for (const auto& [name, body] : cases) {
const double def = 0.1;
const auto mesh = body.to_mesh(def);
const auto soup = body.to_triangle_soup(def, true, 8);
ASSERT_EQ(soup.num_triangles(), mesh.num_faces()) << name;
ASSERT_EQ(soup.num_vertices(), mesh.num_vertices()) << name;
ASSERT_EQ(soup.normals.size(), soup.num_vertices()) << name;
// 顶点位置逐点一致(与 to_mesh 同源逐面网格化)
for (size_t vi = 0; vi < soup.num_vertices(); ++vi) {
const Point3D a = soup.positions[vi];
const Point3D b = mesh.vertex(vi);
ASSERT_NEAR(a.x(), b.x(), 1e-12) << name << " v" << vi;
ASSERT_NEAR(a.y(), b.y(), 1e-12) << name << " v" << vi;
ASSERT_NEAR(a.z(), b.z(), 1e-12) << name << " v" << vi;
}
// 几何:面积/bbox/退化数一致
const MeshSummary sm = summarize(mesh);
double soup_area = 0.0;
size_t soup_degenerate = 0;
Point3D lo = soup.positions[0], hi = soup.positions[0];
for (const auto& p : soup.positions) {
lo = lo.cwiseMin(p);
hi = hi.cwiseMax(p);
}
for (size_t ti = 0; ti < soup.num_triangles(); ++ti) {
const auto& idx = soup.indices[ti];
const Point3D a = soup.positions[static_cast<size_t>(idx[0])];
const Point3D b = soup.positions[static_cast<size_t>(idx[1])];
const Point3D c = soup.positions[static_cast<size_t>(idx[2])];
const double area = 0.5 * (b - a).cross(c - a).norm();
if (area < 1e-15) ++soup_degenerate;
soup_area += area;
}
ASSERT_GT(soup_area, 0.0) << name;
EXPECT_NEAR(soup_area / sm.area, 1.0, 1e-12) << name;
EXPECT_EQ(soup_degenerate, sm.degenerate) << name;
EXPECT_NEAR(lo.x(), sm.lo.x(), 1e-12) << name;
EXPECT_NEAR(hi.z(), sm.hi.z(), 1e-12) << name;
// 法向:逐顶点、单位化,且与相邻三角形几何法向同向(抽样)
for (size_t vi = 0; vi < soup.normals.size(); vi += 97)
EXPECT_NEAR(soup.normals[vi].norm(), 1.0, 1e-12) << name;
for (size_t ti = 0; ti < soup.num_triangles(); ti += 997) {
const auto& idx = soup.indices[ti];
const Point3D p0 = soup.positions[static_cast<size_t>(idx[0])];
const Point3D p1 = soup.positions[static_cast<size_t>(idx[1])];
const Point3D p2 = soup.positions[static_cast<size_t>(idx[2])];
const Vector3D gn = (p1 - p0).cross(p2 - p0);
if (gn.norm() < 1e-15) continue;
EXPECT_GT(soup.normals[static_cast<size_t>(idx[0])].dot(gn.normalized()),
0.0) << name;
}
}
}
TEST(TessellationParallel, TriangleSoupSafety) {
BrepModel empty;
const auto empty_soup = empty.to_triangle_soup(0.1, true, 8);
EXPECT_EQ(empty_soup.num_vertices(), 0u);
EXPECT_EQ(empty_soup.num_triangles(), 0u);
BrepModel box = make_box(1.0, 1.0, 1.0);
const auto soup0 = box.to_triangle_soup(0.1, false, 0); // auto threads, no normals
EXPECT_GT(soup0.num_triangles(), 0u);
EXPECT_TRUE(soup0.normals.empty());
const auto soup8 = box.to_triangle_soup(0.1, true, 8);
EXPECT_EQ(soup8.num_triangles(), soup0.num_triangles());
EXPECT_EQ(soup8.normals.size(), soup8.num_vertices());
}