fix(mesh): delaunay_2d CircleGrid 整数溢出防回绕 + CDT 翻转预算——OCC8228 heal 首次完整跑通(VDE-1193 §9.11.8 续)
Build & Test / build-and-test (push) Canceled after 0s
VDE Build & Test / build-and-test (push) Canceled after 0s
Build & Test / python-bindings (push) Canceled after 0s

sid=265 型 513 点近共线 sliver UV 环的退化外接圆(圆心 ±1e10/半径 1e7)
经 int 截断后 w*h 乘积回绕为负绕过 cap 检查 -> 亿级格循环 push bucket,
提交内存 GB 级爆炸 + 进程假死(heal 无法完成的总根因)。

- delaunay_2d register_circle:NaN/inf 防御 + w>cap||h>cap||w*h>cap
  单维度判超大圆(防乘法回绕),退化圆走 huge 例外表
- cdt_2d 约束恢复全局翻转预算 50n+5000(tris/edge_tris 随翻转无界
  增长的病态打转防御),超限放弃剩余约束(winding 过滤容错)
- 归因基建:VDE_TIMING_HEAL 下探针 stderr 无缓冲、面级 BEGIN 打印、
  to_mesh 阶段 commit 打印、delaunay 插入进度打印

效果:OCC8228 --heal --diag-only 首次完整跑通(2115s,内存 <0.2GB
稳定),卡死面 sid=265 40min+ 磨死 -> 5.4s;diag_err=0 / pen_pairs=0
(基线中 2 对真穿透由 heal 清零,语料 err_bodies 15 -> 14)。
12 套测试全绿(325 用例)
This commit is contained in:
hm
2026-09-14 11:11:24 +08:00
parent 2fc6aa7675
commit 311d5dc308
6 changed files with 173 additions and 13 deletions
+5 -1
View File
@@ -141,7 +141,11 @@ int wmain(int argc, wchar_t** wargv) {
const char* t = std::getenv("VDE_TRACE_IMPORT");
return t && *t && *t != '0';
}();
if ((t_en && *t_en && *t_en != '0') || trace_import)
const bool timing_heal = [] {
const char* t = std::getenv("VDE_TIMING_HEAL");
return t && *t && *t != '0';
}();
if ((t_en && *t_en && *t_en != '0') || trace_import || timing_heal)
setvbuf(stderr, nullptr, _IONBF, 0);
if (argc < 3) {
std::fprintf(stderr,
+23 -4
View File
@@ -1287,7 +1287,26 @@ det 在噪声底反复触发 `|det|<1e-20` break 或给出垃圾步,30 轮打
test_brep_validate 18、test_step_import 54、test_analytic_ssi 13、
test_brep_face_split 13、test_advanced_healing 31 全绿。
**新发现待查**:Orient 推进到 f≈230-260(sid≈253-264)之间出现
分配爆炸(0.13GB → 15.9GB,间隔 ~170 条链),进程在其中磨死——
与 §9.11.7 前两轮"卡死"同症状但位置更靠后,疑 ts=-1 面 uvline/
裁剪域重建对某类面无界细分或累积,需逐面探针归因(下一轮)。
**新发现待查 → 已修(同日续)**:Orient 推进到 f≈230-260 之间出现分配
爆炸(0.13GB → 15.9GB)磨死。逐层归因(面级 BEGIN 打印 + to_mesh 阶段
commit 打印 + delaunay 插入进度打印三层夹逼)定位到
`constrained_delaunay_2d` → Step 1 `delaunay_2d` 的 **CircleGrid::
register_circle 整数溢出**:sid=265 型面(513 点近共线 sliver UV 环)
的退化外接圆圆心 ~±1e10、半径 ~1e7,`(center±r-x0)/cell` 经 int 截断成
垃圾(±INT_MAX),`w*h` 乘积**回绕为负绕过 cap 检查** → 亿级格循环逐格
push bucket → 提交内存 GB 级 + 进程假死(huge 表以 +5/插入增长、
bkt_entries 冻结为佐证)。修复:①register_circle NaN/inf 防御 +
`w > cap || h > cap || w*h > cap` 单维度判超大圆(防乘法回绕);
②cdt_2d 约束恢复加全局翻转预算 `50n+5000`(翻转 tris 净增 2、
edge_tris 净增 6,病态约束集打转时同样无界);③VDE_TIMING_HEAL 下
探针 stderr 无缓冲 + 面级 BEGIN/to_mesh 阶段/delaunay 插入 commit
打印(归因基建)。
**效果(OCC8228 --heal --diag-only 首次完整跑通)**:总耗时 2115.4s
(~35min,此前任何版本都无法完成——95min 不收敛 → 30GB 抖动假死),
内存全程稳定(峰值 <0.2GB),卡死面 sid=265 从 40min+ 磨死 → 5.4s
完成,**diag_err=0 / diag_warn=6 / pen_pairs=0**——2026-09-13 基线中
"OCC8228 2 对真穿透"由 heal 自动清零,语料 err_bodies 15 → 14。
回归 12 套全绿(325 用例:trim 30/heal 41/measure 33/validate 18/
step_import 54/ssi 13/face_split 13/adv_heal 31/delaunay3d 6/
ssi_bool 51/brep_bool 21/step_export 14)。
+49
View File
@@ -12,6 +12,27 @@
#include <functional>
#include <cstdlib>
#include <cstdio>
#ifdef _WIN32
#include <windows.h>
// 与 brep_to_mesh.cpp 同款:kernel32 导出的 K32GetProcessMemoryInfo
// (Win7+ 恒有)+ 本地复刻 PROCESS_MEMORY_COUNTERS_EX,免链 psapi。
struct MeshPmcForTs {
unsigned long cb;
unsigned long PageFaultCount;
unsigned long long PeakWorkingSetSize;
unsigned long long WorkingSetSize;
unsigned long long QuotaPeakPagedPoolUsage;
unsigned long long QuotaPagedPoolUsage;
unsigned long long QuotaPeakNonPagedPoolUsage;
unsigned long long QuotaNonPagedPoolUsage;
unsigned long long PagefileUsage;
unsigned long long PeakPagefileUsage;
unsigned long long PrivateUsage; // 提交内存
};
extern "C" __declspec(dllimport) int __stdcall
K32GetProcessMemoryInfo(void* hProcess, MeshPmcForTs* ppsmemCounters,
unsigned long cb);
#endif
using vde::core::AABB3D;
using vde::core::Point2D;
@@ -488,6 +509,26 @@ mesh::HalfedgeMesh TrimmedSurface::to_mesh(double deflection,
const char* s = std::getenv("VDE_TRACE_TS");
return s && *s && *s != '0';
}();
// VDE_TIMING_HEAL=1:to_mesh 内阶段 commit 内存打印(爆炸面归因,
// VDE-1193 §9.11.8:sid=265 型 ts_to_mesh 513 点链面 commit 涨 GB)
static const bool timing_heal = [] {
const char* s = std::getenv("VDE_TIMING_HEAL");
return s && *s && *s != '0';
}();
auto commit_mb = []() -> long long {
#ifdef _WIN32
MeshPmcForTs pmc{};
if (K32GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
return static_cast<long long>(pmc.PrivateUsage / (1024 * 1024));
#endif
return -1;
};
auto mem_mark = [&](const char* stage) {
if (timing_heal)
std::fprintf(stderr, "[ts-mem] defl=%g loops=%zu stage=%s "
"commit=%lldMB\n",
deflection, loops.size(), stage, commit_mb());
};
struct TsTrace {
bool on;
const TrimmedSurface* self;
@@ -501,6 +542,7 @@ mesh::HalfedgeMesh TrimmedSurface::to_mesh(double deflection,
};
const char* ts_tag = "?";
TsTrace tstr{trace_ts, this, &ts_tag, deflection};
mem_mark("enter");
if (loops.empty()) {
ts_tag = "untrimmed";
@@ -640,6 +682,7 @@ mesh::HalfedgeMesh TrimmedSurface::to_mesh(double deflection,
// Get parameter domain from outer loop polygon
auto outer_poly = build_polygon(loops.front());
mem_mark("outer_poly");
double u_min = outer_poly[0].first, u_max = outer_poly[0].first;
double v_min = outer_poly[0].second, v_max = outer_poly[0].second;
for (const auto& [uu, vv] : outer_poly) {
@@ -710,6 +753,7 @@ mesh::HalfedgeMesh TrimmedSurface::to_mesh(double deflection,
loop_polys.push_back(
build_polygon_adaptive(loop, base_surface, deflection));
}
mem_mark("loop_polys");
// Generate grid vertices and index map (i, j) -> vertex index
std::vector<core::Point3D> all_verts;
@@ -841,7 +885,12 @@ mesh::HalfedgeMesh TrimmedSurface::to_mesh(double deflection,
}
}
if (cpts.size() >= 3 && ccons.size() >= 3) {
if (timing_heal)
std::fprintf(stderr, "[ts-mem] stage=cdt_enter "
"cpts=%zu ccons=%zu commit=%lldMB\n",
cpts.size(), ccons.size(), commit_mb());
auto cdt = mesh::constrained_delaunay_2d(cpts, ccons);
mem_mark("cdt_done");
std::vector<core::Point3D> cverts;
cverts.reserve(cpts.size());
for (const auto& p : cpts)
+50 -3
View File
@@ -16,6 +16,28 @@
#include <chrono>
#include <functional>
#include <iostream>
#ifdef _WIN32
#include <windows.h>
// GetProcessMemoryInfo 在 PSAPI_VERSION=2 时位于 psapi.dll(MinGW 需额外
// 链接 psapi);直接声明 kernel32 导出的 K32 变体(Win7+ 恒有),免改链接。
// PROCESS_MEMORY_COUNTERS_EX 同理在 psapi.h,这里按 Win64 布局本地复刻。
struct MeshPmcEx {
unsigned long cb;
unsigned long PageFaultCount;
unsigned long long PeakWorkingSetSize;
unsigned long long WorkingSetSize;
unsigned long long QuotaPeakPagedPoolUsage;
unsigned long long QuotaPagedPoolUsage;
unsigned long long QuotaPeakNonPagedPoolUsage;
unsigned long long QuotaNonPagedPoolUsage;
unsigned long long PagefileUsage;
unsigned long long PeakPagefileUsage;
unsigned long long PrivateUsage; // 提交内存
};
extern "C" __declspec(dllimport) int __stdcall
K32GetProcessMemoryInfo(void* hProcess, MeshPmcEx* ppsmemCounters,
unsigned long cb);
#endif
namespace vde::brep {
@@ -1873,16 +1895,41 @@ void BrepModel::mesh_body(double deflection,
}();
const auto ft0 = timing_heal ? std::chrono::steady_clock::now()
: std::chrono::steady_clock::time_point{};
if (timing_heal) {
// 面级 BEGIN 打印:卡死/爆炸面无需等它结束即可锁定(VDE-1193 §9.11.8)
unsigned long long commit_mb = 0;
#ifdef _WIN32
MeshPmcEx pmcb{};
if (K32GetProcessMemoryInfo(GetCurrentProcess(), &pmcb,
sizeof(pmcb)))
commit_mb = pmcb.PrivateUsage / (1024 * 1024);
#endif
std::fprintf(stderr,
"[mesh-timing] face f=%zu sid=%d ts=%d BEGIN "
"commit=%lluMB\n",
fi, face.surface_id, face.trimmed_surface_id,
commit_mb);
}
FaceMeshStatus st = mesh_face_trimmed(face, deflection, out_verts, out_tris);
if (timing_heal) {
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - ft0).count();
if (ms > 500)
if (ms > 500) {
// 提交内存(PrivateUsage):网格化无界细分/累积归因用
unsigned long long commit_mb = 0;
#ifdef _WIN32
MeshPmcEx pmc{};
if (K32GetProcessMemoryInfo(GetCurrentProcess(), &pmc,
sizeof(pmc)))
commit_mb = pmc.PrivateUsage / (1024 * 1024);
#endif
std::fprintf(stderr,
"[mesh-timing] SLOW face f=%zu sid=%d ts=%d "
"loops=%zu: %lld ms\n",
"loops=%zu: %lld ms commit=%lluMB\n",
fi, face.surface_id, face.trimmed_surface_id,
face.loops.size(), static_cast<long long>(ms));
face.loops.size(), static_cast<long long>(ms),
commit_mb);
}
}
// VDE_TRACE_MESH_ALL=1:逐面打印网格化路径与产出台数(共享边/路径诊断)
static const bool trace_all = [] {
+19 -1
View File
@@ -188,6 +188,16 @@ DelaunayResult constrained_delaunay_2d(
// 外层反复整表重扫。
const int MAX_ITERS = 10000;
const int guard_max = 4 * n + 16;
// 全局翻转预算:每次翻转 tris 净增 2(removed 惰性保留)、edge_tris
// 净增 6 条目,且单次翻转全表扫。近共线/自交约束集(OCC8228 sid=265
// 型:513 点近退化带面 UV 环,commit 175MB→GB 级)会让"合法翻转"
// 在守卫间长期打转——外层 10000 轮 × 每轮多次翻转把 tris/edge_tris
// 线性放大到 GB 级。预算超限即放弃剩余约束(下游 winding 过滤对未
// 恢复约束本就容错——"无可翻转边:该约束不可恢复"路径同口径),
// 内存/时间都有界。
const long long kMaxFlips = 50LL * n + 5000;
long long flips_done = 0;
bool budget_hit = false;
auto flip_one_crossing = [&](int v0, int v1) -> bool {
const Point2D pv0 = points[static_cast<size_t>(v0)];
const Point2D pv1 = points[static_cast<size_t>(v1)];
@@ -267,12 +277,20 @@ DelaunayResult constrained_delaunay_2d(
const int v0 = c.first, v1 = c.second;
if (has_edge(v0, v1)) continue;
for (int guard = 0; guard < guard_max && !has_edge(v0, v1); ++guard) {
if (flips_done >= kMaxFlips) { budget_hit = true; break; }
if (!flip_one_crossing(v0, v1)) break; // 无可翻转边:该约束不可恢复
++flips_done;
any_flip = true;
}
if (flips_done >= kMaxFlips) { budget_hit = true; break; }
}
if (!any_flip) break; // 本轮无翻转:所有可恢复约束边已恢复
if (!any_flip || flips_done >= kMaxFlips) break; // 本轮无翻转/预算尽:收敛或放弃
}
if (budget_hit)
std::fprintf(stderr,
"[cdt] flip budget exhausted n=%d constraints=%zu "
"flips=%lld — 剩余约束放弃(winding 过滤容错)\n",
n, constraints.size(), flips_done);
// Step 3: Collect final triangles
DelaunayResult result;
+27 -4
View File
@@ -106,7 +106,16 @@ DelaunayResult delaunay_2d(const std::vector<Point2D>& pts) {
(static_cast<uint64_t>(static_cast<uint32_t>(iy)) * 19349663ull);
}
void register_circle(const Tri& t, int idx) {
if (t.radius_sq >= std::numeric_limits<double>::max() * 0.5) {
// NaN/inf 防御 + 域宽防溢出:近共线点集(OCC8228 sid=265 型
// 513 点 sliver UV 环)的退化外接圆——圆心坐标 ~±1e10、半径
// ~1e7,(center±r-x0)/cell 经 int 截断成垃圾(±INT_MAX),
// w*h 乘积可回绕为负绕过 cap 检查 → 亿级格循环逐格 push
// bucket(提交内存 GB 级爆炸 + 进程假死)。任一维度超 cap 或
// 值非有限即按超大圆走 huge 例外表(线性扫)。
if (!(t.radius_sq >= 0.0) ||
t.radius_sq >= std::numeric_limits<double>::max() * 0.5 ||
!(t.center.x() == t.center.x()) ||
!(t.center.y() == t.center.y())) {
huge.push_back(idx);
return;
}
@@ -115,17 +124,31 @@ DelaunayResult delaunay_2d(const std::vector<Point2D>& pts) {
const int ix1 = static_cast<int>(std::floor((t.center.x() + r - x0) / cell));
const int iy0 = static_cast<int>(std::floor((t.center.y() - r - y0) / cell));
const int iy1 = static_cast<int>(std::floor((t.center.y() + r - y0) / cell));
const long long w = static_cast<long long>(ix1 - ix0 + 1);
const long long h = static_cast<long long>(iy1 - iy0 + 1);
if (w * h > cap) { huge.push_back(idx); return; }
const long long w = static_cast<long long>(ix1) - ix0 + 1;
const long long h = static_cast<long long>(iy1) - iy0 + 1;
if (w > cap || h > cap || w * h > cap) { huge.push_back(idx); return; }
total_entries += static_cast<size_t>(w * h);
for (int ix = ix0; ix <= ix1; ++ix)
for (int iy = iy0; iy <= iy1; ++iy)
buckets[key(ix, iy)].push_back(idx);
}
size_t total_entries = 0;
} cgrid{min_x - dmax, min_y - dmax, std::max(dmax, 1e-12) / 128.0};
cgrid.register_circle(tris[0], 0);
// VDE_TIMING_HEAL=1:插入进度打印(内存/卡死归因,VDE-1193 §9.11.8)
static const bool timing_heal = [] {
const char* s = std::getenv("VDE_TIMING_HEAL");
return s && *s && *s != '0';
}();
for (int i = 0; i < n0; ++i) {
if (timing_heal && i % 8 == 0)
std::fprintf(stderr,
"[delaunay] i=%d/%d tris=%zu huge=%zu "
"bkt_entries=%zu bkt_buckets=%zu\n",
i, n0, tris.size(), cgrid.huge.size(),
cgrid.total_entries, cgrid.buckets.size());
std::vector<int> bad;
// 点定位:只扫 p 所在格注册的三角形 + 超大圆例外表
{