Files
ViewDesignEngine/dbg_h1_batch_probe.cpp
hm a07dc5028c chore(dbg): 探针硬边口径统一为 cb_f_n 质心翻转 + seam/cos-half 守卫
硬边成功率按 corner_blend 实际尝试口径统计;blend_corner add_planar fail 增加重建点列 dump
2026-08-29 08:52:36 +08:00

162 lines
7.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.
// 探针:realfile 全量模型的 corner_blend 硬边失败分布统计(H1 收尾)
// 对 ≤1500 面模型,每条硬边(内法向点积 < 0.995 且邻面=2)跑 corner_blend,
// 统计成功/回退/无效,并对非平滑边失败列出模型+边信息。
// 用法:dbg_h1_batch_probe.exe [dir] [max_faces] [max_edges_per_model]
// heal:INVALID 的 body 直接跳过(输入自交/伪闭合壳,corner_blend 必然回退,统计无意义)
#include "vde/brep/step_import.h"
#include "vde/brep/brep_heal.h"
#include "vde/brep/brep_validate.h"
#include "vde/brep/advanced_blend.h"
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#ifdef _WIN32
#include <windows.h>
#endif
using namespace vde::brep;
static void collect(const std::string& root, std::vector<std::string>& out) {
#ifdef _WIN32
std::string pattern = root + "/*";
WIN32_FIND_DATAA fd;
HANDLE h = FindFirstFileA(pattern.c_str(), &fd);
if (h == INVALID_HANDLE_VALUE) return;
do {
std::string name = fd.cFileName;
if (name == "." || name == "..") continue;
std::string full = root + "/" + name;
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
collect(full, out);
} else {
out.push_back(full);
}
} while (FindNextFileA(h, &fd));
FindClose(h);
#endif
}
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;
}
/// 与 corner_blend 内部 cb_f_n 一致的朝向判定:表面法向先按 reversed 取内法向,
/// 再用 bbox 质心启发式翻转(退化重合面/零厚双壁时把两壁统一为同向)。
/// 探针统计硬边须与 corner_blend 实际尝试口径一致,否则零厚 seam 对
/// (face_n_in dot=-1,cb_f_n dot=+1)会被误计为失败(NO-HARD-OK 假象)。
static Vector3D face_n_flip(const BrepModel& m, int f, const Point3D& cctr) {
const auto& surf = m.face_surface(f);
const auto& ku = surf.knots_u();
const auto& kv = surf.knots_v();
double uc = ku.empty() ? 0.5 : 0.5 * (ku.front() + ku.back());
double vc = kv.empty() ? 0.5 : 0.5 * (kv.front() + kv.back());
Vector3D n = surf.normal(uc, vc);
if (m.face(f).reversed) n = -n;
double l = n.norm();
if (l < 1e-12) return Vector3D::UnitZ();
n = n / l;
Point3D fc = surf.evaluate(uc, vc);
if ((fc - cctr).dot(n) < 0.0) n = -n;
return -n; // 内法向
}
int main(int argc, char** argv) {
setvbuf(stdout, nullptr, _IONBF, 0);
std::string dir = argc > 1 ? argv[1] : "tests/data/realfile";
int max_faces = argc > 2 ? std::atoi(argv[2]) : 1500;
int max_edges = argc > 3 ? std::atoi(argv[3]) : 1000000;
std::string filter = argc > 4 ? argv[4] : "";
int start_idx = argc > 5 ? std::atoi(argv[5]) : 0;
std::vector<std::string> files;
collect(dir, files);
int models = 0, models_hard = 0;
int hard_total = 0, hard_ok = 0, hard_fallback = 0, hard_invalid = 0, hard_except = 0;
int smooth_skipped = 0, seam_skipped = 0;
struct FailStat { std::string file; int fails; int hard; };
std::vector<FailStat> fail_stats;
int file_no = 0;
for (const auto& f : files) {
++file_no;
std::string ext = ext_lower(f);
if (ext != ".step" && ext != ".stp" && ext != ".p21") continue;
if (!filter.empty() && f.find(filter) == std::string::npos) continue;
if (file_no < start_idx) continue;
std::printf("[file] #%d %s\n", file_no, f.c_str());
std::vector<BrepModel> imported;
try { imported = import_step(f); } catch (...) { continue; }
if (imported.empty()) continue;
int body_no = 0;
for (auto& m : imported) {
++body_no;
std::printf("[body] %s #%d\n", f.c_str(), body_no);
try { (void)automatic_heal(m); } catch (...) { continue; }
auto vheal = validate(m);
if (!vheal.valid) {
std::printf("[skip-heal-invalid] %s\n", f.c_str());
continue; // 输入缺陷体,corner_blend 必然回退,跳过
}
if (static_cast<int>(m.num_faces()) > max_faces) continue;
if (m.num_edges() < 3) continue;
auto bb = m.bounds();
double scale = (bb.max() - bb.min()).norm();
if (!(scale > 1e-6)) continue;
double r = 0.01 * scale;
if (r > 0.5) r = 0.5;
++models;
int n_hard = 0, n_ok = 0, n_fb = 0, n_inv = 0, n_exc = 0;
int n_seam = 0;
Point3D cctr = (bb.min() + bb.max()) * 0.5;
int ne = static_cast<int>(m.num_edges());
for (int ei = 0; ei < ne && ei < max_edges; ++ei) {
auto efs = m.edge_faces(ei);
if (static_cast<int>(efs.size()) < 2) continue;
// 与 corner_blend 一致:cb_f_n 质心翻转 + cos-half 守卫。
// 零厚双壁 seam 对(内法向反向,ch≈0 或翻转后同向)不可圆角,
// corner_blend 会跳过/回退,不计入失败。
Vector3D na = face_n_flip(m, efs[0], cctr), nb = face_n_flip(m, efs[1], cctr);
double dp = na.dot(nb);
if (dp > 0.995) { ++smooth_skipped; continue; } // 平滑边
Vector3D s = na + nb;
double sl = s.norm();
if (sl < 1e-8) { ++seam_skipped; ++n_seam; continue; } // 反向 seam
double ch = s.dot(na) / sl;
if (std::abs(ch) < 1e-8) { ++seam_skipped; ++n_seam; continue; } // cos-half ~0
++n_hard;
++hard_total;
BrepModel res;
try { res = corner_blend(m, {ei}, r); }
catch (...) { ++n_exc; ++hard_except; continue; }
if (res.num_faces() <= m.num_faces()) { ++n_fb; ++hard_fallback; continue; }
auto vr = validate(res);
if (!vr.valid) { ++n_inv; ++hard_invalid; continue; }
++n_ok; ++hard_ok;
}
if (n_hard > 0) ++models_hard;
if (n_hard > 0 && n_ok == 0) {
fail_stats.push_back({f, n_hard, n_hard});
std::printf("[NO-HARD-OK] hard=%d (fb=%d inv=%d exc=%d seam=%d) %s\n",
n_hard, n_fb, n_inv, n_exc, n_seam, f.c_str());
} else if (n_hard > 0 && n_ok < n_hard) {
std::printf("[partial] ok=%d/%d hard (fb=%d inv=%d exc=%d seam=%d) %s\n",
n_ok, n_hard, n_fb, n_inv, n_exc, n_seam, f.c_str());
}
}
}
std::printf("[summary] models=%d hard_models=%d hard_edges=%d ok=%d fb=%d inv=%d exc=%d smooth_skipped=%d seam_skipped=%d\n",
models, models_hard, hard_total, hard_ok, hard_fallback, hard_invalid,
hard_except, smooth_skipped, seam_skipped);
std::printf("[summary] 硬边成功率 = %d/%d = %.1f%%\n",
hard_ok, hard_total, hard_total > 0 ? 100.0 * hard_ok / hard_total : 0.0);
return 0;
}