Files
ViewDesignEngine/dbg_h1_1505_probe.cpp
hm 3e8902df82 fix(brep): 零宽缝隙/双壁输入 corner_blend 全面修复 — 全失败 body 43→19
heal 侧(brep_heal.cpp): remove_zero_width_slits 移除面环连续反向重合边对(零宽缝隙/T型接头伪影); step_stitch 重合边合并守卫(并集>2 跳过避免非流形); automatic_heal 安全网(raw VALID→heal INVALID 回退原体)

corner_blend 侧(blend_corner.cpp): find_edge_bidir→add_edge_shared_limited(邻接面<2 才复用, 双壁输入不再坍缩非流形边); face_edges 跟踪(复制面同面不得重复引用同一边, 跨环零宽缝隙强制新建边实例)

验证(realfile 全量): 硬边成功率 60.5%→73.8% 同口径; NO-HARD-OK 全失败 body 43→19(显控台 234/198/174/70、J29A 47、J30J-J装配体、HTW76、1305-微型编码器 全部救回); test_model_corpus 2/2 通过
2026-08-28 23:40:12 +08:00

191 lines
6.9 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.
// 探针:1505装配图.STEP corner_blend 失败原因分析(H1 收尾)
// 用法:dbg_h1_1505_probe.exe [step_path] [max_edges]
// 设置 VDE_CB_DEBUG=1 可同时捕获 corner_blend 内部 DBG_STEP 回退原因(stderr)。
#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 <cstring>
#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 Vector3D face_n_in(const BrepModel& m, int f) {
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();
return n / l;
}
static void dump_brep(const char* tag, const BrepModel& m) {
std::printf("==== %s ====\n", tag);
for (int vi = 0; vi < m.num_vertices(); ++vi) {
const auto& p = m.vertex(vi).point;
std::printf(" v%d (%.4f, %.4f, %.4f)\n", vi, p.x(), p.y(), p.z());
}
for (int ei = 0; ei < m.num_edges(); ++ei) {
const auto& e = m.edge(ei);
std::printf(" e%d v%d-v%d len=%.4f\n", ei, e.v_start, e.v_end,
(m.vertex(e.v_end).point - m.vertex(e.v_start).point).norm());
}
for (int fi = 0; fi < m.num_faces(); ++fi) {
const auto& f = m.face(fi);
Vector3D n = face_n_in(m, fi);
std::printf(" f%d surf=%d rev=%d loops=[", fi, f.surface_id, f.reversed ? 1 : 0);
for (size_t k = 0; k < f.loops.size(); ++k) {
if (k) std::printf(",");
const auto& lop = m.loop_by_id(f.loops[k]);
std::printf(" (");
for (size_t j = 0; j < lop.edges.size(); ++j) {
if (j) std::printf(",");
std::printf("e%d", lop.edges[j]);
}
std::printf(")");
}
std::printf("] n_in=(%.4f,%.4f,%.4f)\n", n.x(), n.y(), n.z());
}
std::printf("==== end %s ====\n", tag);
}
int main(int argc, char** argv) {
setvbuf(stdout, nullptr, _IONBF, 0);
int max_edges = argc > 2 ? std::atoi(argv[2]) : 1000000;
std::vector<std::string> files;
if (argc > 1) {
files.push_back(argv[1]);
} else {
// 无路径参数:遍历 realfile,过滤含 "1505" 的文件(避免中文硬编码编码问题)
collect("tests/data/realfile", files);
files.erase(std::remove_if(files.begin(), files.end(),
[](const std::string& s) {
return s.find("1505") == std::string::npos;
}),
files.end());
}
int global_ok = 0, global_tot = 0;
for (const auto& path : files) {
std::vector<BrepModel> imported;
try {
imported = import_step(path);
} catch (const std::exception& e) {
std::printf("[import error] %s\n", e.what());
return 1;
}
std::printf("imported bodies=%zu\n", imported.size());
int global_ok = 0, global_tot = 0;
double rfrac = argc > 3 ? std::atof(argv[3]) : 0.01; // 半径比例(默认 0.01*scale)
for (auto& m : imported) {
try {
automatic_heal(m);
} catch (...) {
std::printf("[heal error]\n");
continue;
}
auto bb = m.bounds();
double scale = (bb.max() - bb.min()).norm();
double r = rfrac * scale;
if (r > 0.5) r = 0.5;
if (!(scale > 1e-6)) {
std::printf("model degenerate scale=%.4f, skip\n", scale);
continue;
}
std::printf("model faces=%zu edges=%d verts=%d scale=%.4f r=%.4f\n",
m.num_faces(), m.num_edges(), m.num_vertices(), scale, r);
dump_brep("healed model", m);
int ne = static_cast<int>(m.num_edges());
int n = std::min(ne, max_edges);
int ok = 0, fallback = 0, invalid = 0, except = 0;
std::vector<int> fail_edges;
for (int ei = 0; ei < n; ++ei) {
BrepModel res;
try {
res = corner_blend(m, {ei}, r);
} catch (...) {
++except;
fail_edges.push_back(ei);
continue;
}
if (res.num_faces() <= m.num_faces()) {
++fallback;
fail_edges.push_back(ei);
continue;
}
auto vr = validate(res);
if (!vr.valid) {
++invalid;
fail_edges.push_back(ei);
continue;
}
++ok;
}
++global_tot;
if (ok > 0) ++global_ok;
std::printf("[stats] edges=%d ok=%d fallback=%d invalid=%d except=%d\n",
n, ok, fallback, invalid, except);
// 失败边分类详查(最多 30 条):打印边长/邻面/内法向点积,
// 可结合 VDE_CB_DEBUG 环境变量对照内部 DBG_STEP 回退原因。
int shown = 0;
for (int ei : fail_edges) {
if (shown >= 30) break;
const auto& ed = m.edge(ei);
double len = (m.vertex(ed.v_end).point - m.vertex(ed.v_start).point).norm();
auto efs = m.edge_faces(ei);
std::string ft;
for (int f : efs) {
const auto& sf = m.face_surface(f);
ft += m.face_type(f) == FaceType::Plane ? "P" :
m.face_type(f) == FaceType::Cylinder ? "C" : "S";
}
std::printf(" [fail] e%d len=%.4f nf=%zu faces=%s",
ei, len, efs.size(), ft.c_str());
if (efs.size() >= 2) {
Vector3D na = face_n_in(m, efs[0]), nb = face_n_in(m, efs[1]);
std::printf(" na·nb=%.4f", na.dot(nb));
}
std::printf("\n");
++shown;
}
if (fail_edges.size() > 30)
std::printf(" ... 另有 %zu 条失败边未列出\n", fail_edges.size() - 30);
} // for auto& m : imported
} // for files
std::printf("[summary] 模型级 %d/%d\n", global_ok, global_tot);
return 0;
}