Files
ViewDesignEngine/dbg_selfint_sweep.cpp
hm 24ea608038 feat(validate): 壳分解 + intra/inter 自交计数;Phase 2.1 量化:143 尾差全为壳内真实自交叉
- validate 预计算壳(共享边连通分量),自交对按壳内/壳间分类计数,
  valid 口径与错误文本不变(纯增量),realfile 三率/corner/boolean 回归逐字一致
- 新增 dbg_selfint_sweep 探针(容差扫描 + base-only 壳分类,tests/foundation 注册)
- 31 方案文档回填 Phase 2.1 结论:
  · 守护后权威 fail 清单 143 文件 = 验证率 96.2% 唯一尾差,自交为唯一致命项
  · 容差扫描 tol≤1e-3 清零 0/143 → 微穿透假设证伪
  · 壳分解 intra=143/inter=0 → 壳间装配干涉假设证伪(按壳语义收益 0)
  · 143 全部为单壳内面-面真实交叉(输入缺陷)→ 归 Phase 2.1/2.3 SSI/布尔种子库
2026-09-03 22:17:43 +08:00

161 lines
6.9 KiB
C++

// 探针:自交对容差扫描(Phase 2.1 自交攻坚)
// 读取失败清单(每行 [import]/[heal]/[valid] <path>,前缀自动剥离,也容忍裸路径),
// 对每个 body 的 raw 模型以 intersection tol ∈ {1e-6,1e-5,1e-4,1e-3,1e-2,1e-1}
// 各跑一次 validate,解析 "N face pairs have potential self-intersection" 的对数衰减。
// 目标:区分 147 个 raw 自交样本中——
// 数值级近接触/边界微叠(对数随容差放大快速清零 → 可在验证/合并层面合理收敛)
// 真实面交叉(对容差不敏感,即使 tol=0.1 仍高 → 属输入几何冲突,需 SSI/布尔级处理)
// 用法:dbg_selfint_sweep.exe <fail_list.txt>
#include "vde/brep/step_import.h"
#include "vde/brep/brep_validate.h"
#include "vde/brep/tolerance.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <string>
#include <vector>
#include <set>
#include <cmath>
using namespace vde::brep;
static std::string ext_lower(const std::string& f) {
auto dot = f.find_last_of('.');
if (dot == std::string::npos) return "";
std::string e;
for (size_t i = dot; i < f.size(); ++i)
e += static_cast<char>(std::tolower(static_cast<unsigned char>(f[i])));
return e;
}
/// 从 validate errors 中提取自交对数;返回 -1 表示无自交错误
static int selfint_count(const ValidationResult& v, int& other_errors) {
int self = -1;
other_errors = 0;
for (const auto& e : v.errors) {
int n = 0;
if (std::sscanf(e.c_str(), "%d face pairs", &n) == 1) self = n;
else ++other_errors;
}
return self;
}
int main(int argc, char** argv) {
setvbuf(stdout, nullptr, _IONBF, 0);
if (argc < 2) { std::printf("usage: dbg_selfint_sweep.exe <fail_list.txt> [1=base-only]\n"); return 1; }
const bool base_only = argc > 2 && std::atoi(argv[2]) == 1;
const double tols[] = { 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1 };
const int NT = static_cast<int>(sizeof(tols) / sizeof(tols[0]));
std::ifstream in(argv[1]);
if (!in) { std::printf("[open error] %s\n", argv[1]); return 1; }
std::string line;
int n_self_files = 0, n_self_bodies = 0, n_import_fail = 0;
// cleared[n] = 在该容差档(含)清零并转 valid 的 body 数
std::vector<int> cleared(NT, 0);
// persist = 即使 tol=1e-1 仍不自交清零(真实冲突候选)
int persist = 0;
// cleared_but_invalid = 自交清零但仍有其它错误/wt 不过
int cleared_but_invalid = 0;
int decayed_no_clear = 0; // 对数下降但未清零
std::set<std::string> cleared_files;
std::set<std::string> persist_files;
while (std::getline(in, line)) {
auto tab = line.find('\t');
std::string f = (tab == std::string::npos) ? line : line.substr(tab + 1);
if (f.empty()) continue;
if (f.front() == '\r' || f.front() == '\n') continue;
std::string tag = (tab == std::string::npos) ? "[file]" : line.substr(0, tab);
std::string ext = ext_lower(f);
if (ext != ".step" && ext != ".stp" && ext != ".p21") continue;
std::vector<BrepModel> imported;
try { imported = import_step(f); }
catch (const std::exception&) { ++n_import_fail; continue; }
if (imported.empty()) { ++n_import_fail; continue; }
bool file_has_self = false;
for (size_t bi = 0; bi < imported.size(); ++bi) {
BrepModel& m = imported[bi];
if (m.num_faces() == 0) continue;
// 基准档:确定是否为 raw 自交
ToleranceConfig base_cfg = ToleranceConfig::global();
{
ToleranceConfig cfg = base_cfg;
cfg.intersection = tols[0];
ToleranceConfig::set_global(cfg);
}
auto vbase = validate(m);
ToleranceConfig::set_global(base_cfg);
int other0 = 0;
int s0 = selfint_count(vbase, other0);
if (s0 <= 0) continue; // 非自交类,跳过
file_has_self = true;
++n_self_bodies;
auto bb = m.bounds();
double scale = (bb.max() - bb.min()).norm();
int counts[NT];
int clears_at = -1;
bool became_valid = false;
if (base_only) {
for (int t = 0; t < NT; ++t) counts[t] = (t == 0) ? s0 : -2; // -2 = 未扫描
} else {
for (int t = 0; t < NT; ++t) {
ToleranceConfig cfg = base_cfg;
cfg.intersection = tols[t];
ToleranceConfig::set_global(cfg);
auto v = validate(m);
int other = 0;
counts[t] = selfint_count(v, other);
if (counts[t] <= 0 && v.valid && v.watertightness > 0.99) {
if (clears_at < 0) clears_at = t;
became_valid = true;
}
}
}
ToleranceConfig::set_global(base_cfg);
std::printf("%s b%zu V=%zu E=%zu F=%zu scale=%.4g wt=%.4f ori=%zu eul=%d intra=%zu inter=%zu pairs=[",
tag.c_str(), bi, m.num_vertices(), m.num_edges(), m.num_faces(),
scale, vbase.watertightness, vbase.inconsistent_orientations,
vbase.euler_characteristic,
vbase.intra_shell_intersect_pairs,
vbase.inter_shell_intersect_pairs);
for (int t = 0; t < NT; ++t) {
if (t) std::printf(" ");
std::printf("%d", counts[t]);
}
std::printf("] other=%d %s\n", other0, f.c_str());
if (clears_at >= 0) {
for (int t = clears_at; t < NT; ++t) cleared[t]++;
cleared_files.insert(f);
if (!became_valid) ++cleared_but_invalid;
} else if (counts[NT - 1] == 0) {
// 清零但未满足 valid&&wt>0.99(例如 wt 同时跌破)
++cleared_but_invalid;
cleared_files.insert(f);
for (int t = 0; t < NT; ++t) if (counts[t] == 0) cleared[t]++;
} else {
if (counts[NT - 1] < s0) ++decayed_no_clear;
if (counts[NT - 1] >= s0) { ++persist; persist_files.insert(f); }
}
}
if (file_has_self) ++n_self_files;
}
std::printf("[summary] selfint_files=%d selfint_bodies=%d import_fail=%d\n",
n_self_files, n_self_bodies, n_import_fail);
if (base_only) return 0; // 仅基准档:容差分类数无意义,不打印
for (int t = 0; t < NT; ++t) {
std::printf("[cleared@tol=%.0e] bodies=%d\n", tols[t], cleared[t]);
}
std::printf("[persist@1e-1] bodies=%d files=%d\n", persist, static_cast<int>(persist_files.size()));
std::printf("[cleared_but_invalid] bodies=%d [decayed_no_clear] bodies=%d\n",
cleared_but_invalid, decayed_no_clear);
return 0;
}