Files
ViewDesignEngine/dbg_nm_edge.cpp
T
hm a4dbd78ad6
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
fix(import): BREP_WITH_VOIDS 整件静默丢失修复 + validate 边邻接按相异面计数(VDE-1193 §9.11.14)
step_import:
- 枚举入口追加 BREP_WITH_VOIDS(带内腔实体):此前只认 MSB/SBSM/
  CLOSED_SHELL/FBSM,该类型无任何匹配,外壳+内腔壳被整体静默丢弃且
  无降级报告(grabcad agrobee flaps_down 的 'STICK' 体:源 60 壳/1097 面
  只建成 56 体/1070 面,丢失 4 壳 27 面)。
- convert_solid 解析 args=(name, outer, voids):void 壳经
  ORIENTED_CLOSED_SHELL 取 closed_shell_element,orientation=.F. 时
  对该壳所有面翻转 reversed(内腔法向指向空腔),并入同一闭合壳。
- referenced_by_solid 展开 ORIENTED_CLOSED_SHELL→CLOSED_SHELL,避免
  AP203 直壳通道把内腔壳二次建成重复体;walk_product_tree 同步认该类型。
- 新增 VDE_TRACE_SHELL 追踪开关(体→壳列表 / 逐壳 refs vs added /
  逐面早退原因),用于定位整壳与逐面静默丢失。

brep_validate:
- 边邻接计数改按「相异面」口径(同一面重复引用不累加);周期曲面
  seam 边(nfac==1 && nocc>=2)计为水密闭合边。消除 seam 边被"同面两次
  引用 + 邻面"误判为 >=3 面非流形。

验证:agrobee flaps_down 57 体/1097 面(60 壳全数建成),新增 body
F=27 E=53 V=34 nm3=0,V-E+F=8 = 外壳+3 内腔四个闭合分量;其 10 条
单面引用边经源侧核对全为周期面 seam 边,诊断 err=0。新增回归测试
StepImport.BrepWithVoids(外壳 6 面+内腔 6 面,内腔 reversed 取反);
test_step_import 55/55 通过。
2026-09-15 23:03:34 +08:00

169 lines
7.9 KiB
C++
Raw 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.
/**
* @file dbg_nm_edge.cpp
* @brief 非流形边(≥3 面共边)结构归因工具(VDE-1193 §9.11 残余错误件归因)
*
* 背景:语料残余 err_bodies 绝大多数为 k=2「非流形边」。文件级扫描
* (build/sweep-heal914/step_nm_scan.py)证明这些 STEP 的 CLOSED_SHELL /
* OPEN_SHELL 自身不含 ≥3 面共边(按面去重口径与逐引用口径都为 0)→
* 非流形边由导入器/修复流水线构造产生,需逐边看清结构再定修法。
*
* 本工具输出每条 ≥3 面共边的:
* - 环引用次数 occ(同一面在同一环里重复引用同一 ID 会推高 occ);
* - 去重后的相异面数 nfac;
* - 两端点坐标、边长;
* - 每个关联面的 surface_id / 次数 / 度数与控制点规模 / 环规模 /
* 该面在本边上出现次数 / 包围盒对角 / 平面法向。
* 判读:
* - nfac<3 而 occ≥3 → 同一面重复引用(环内重复边,非真非流形);
* - nfac≥3 且两面 surface_id 相同 / 面积与法向一致 → 同域双墙面;
* - nfac≥3 且三面几何各异 → 真三面共边(壳内结构)。
*
* 用法: dbg_nm_edge [--heal] [--diag] [--seams] <file.stp> [...] (ASCII 路径)
* --heal 导入后先跑 automatic_heal(复现 sweep 的口径),可对比 raw/heal 结构
* --diag 额外跑 diagnose_body 打印错误 kind(0=壳内穿透 2=非流形 3=水密性 ...)
* --seams 同时打印「周期面 seam 边」(同面正反引用两次,nfac==1 而 occ==2)
*/
#include "vde/brep/brep.h"
#include "vde/brep/step_import.h"
#include "vde/brep/brep_heal.h"
#include "vde/brep/brep_validate.h"
#include <cmath>
#include <cstdio>
#include <map>
#include <set>
#include <string>
#include <vector>
using vde::brep::BrepModel;
using vde::core::Point3D;
using vde::core::Vector3D;
static void dump_body(const BrepModel& b, size_t bi, bool diag, bool seams) {
const int F = static_cast<int>(b.num_faces());
const int E = static_cast<int>(b.num_edges());
std::vector<std::vector<int>> inc(E); // edge -> distinct faces
std::vector<std::vector<int>> occ(E); // edge -> face occurrences (with dup)
for (int fi = 0; fi < F; ++fi) {
std::set<int> seen;
for (int ei : b.face_edges(fi)) {
if (ei < 0 || ei >= E) continue;
occ[ei].push_back(fi);
if (seen.insert(ei).second) inc[ei].push_back(fi);
}
}
// 汇总:≥3 相异面(真非流形候选)、≥3 逐引用(含同面重复)、0/1 面(悬边/边界)
size_t n_nm = 0, n_dup = 0, n_open = 0, n_two = 0;
for (int ei = 0; ei < E; ++ei) {
const size_t nfac = inc[ei].size(), nocc = occ[ei].size();
if (nfac >= 3) ++n_nm;
else if (nocc >= 3) ++n_dup;
if (nfac == 0 || nfac == 1) ++n_open;
else if (nfac == 2) ++n_two;
}
const double wt = E ? static_cast<double>(n_two) / E : 1.0;
std::printf(" body%zu: F=%d E=%d V=%zu nm3(diff-face)=%zu occ3(same-face-dup)=%zu "
"open<=1=%zu wt=%.4f\n",
bi, F, E, b.num_vertices(), n_nm, n_dup, n_open, wt);
if (diag) {
const vde::brep::BodyDiagnostics d = vde::brep::diagnose_body(b);
for (const auto& it : d.errors) {
std::printf(" ERR kind=%d nfaces=%zu: %s\n", it.kind, it.faces.size(),
it.message.c_str());
}
}
for (int ei = 0; ei < E; ++ei) {
const size_t nfac = inc[ei].size();
const size_t nocc = occ[ei].size();
const bool nm_case = (nfac >= 3 || nocc >= 3);
const bool seam_case = (nfac == 1 && nocc >= 2); // 周期面 seam(同面正反两次)
if (!(nm_case || (seams && seam_case))) continue;
const auto& e = b.edge(ei);
const Point3D p0 = b.vertex_by_id(e.v_start).point;
const Point3D p1 = b.vertex_by_id(e.v_end).point;
std::printf(" body%zu F=%d E=%d edge#%d %s occ=%zu nfac=%zu len=%.6g "
"v0=(%.5g,%.5g,%.5g) v1=(%.5g,%.5g,%.5g)\n",
bi, F, E, ei, seam_case ? "SEAM" : "NM", nocc, nfac, (p1 - p0).norm(),
p0.x(), p0.y(), p0.z(), p1.x(), p1.y(), p1.z());
for (int fi : inc[ei]) {
const auto& f = b.face(fi);
const auto& ns = b.face_surface(fi);
int this_face_occ = 0;
for (int x : occ[ei]) if (x == fi) ++this_face_occ;
size_t nloop_edges = 0;
for (int li : f.loops) nloop_edges += b.loop_by_id(li).edges.size();
double mn[3] = {1e300, 1e300, 1e300}, mx[3] = {-1e300, -1e300, -1e300};
for (int li : f.loops) {
for (int le : b.loop_by_id(li).edges) {
if (le < 0 || le >= E) continue;
const auto& ee = b.edge(le);
for (int v : {ee.v_start, ee.v_end}) {
const Point3D& q = b.vertex_by_id(v).point;
double c[3] = {q.x(), q.y(), q.z()};
for (int k = 0; k < 3; ++k) {
mn[k] = std::min(mn[k], c[k]);
mx[k] = std::max(mx[k], c[k]);
}
}
}
}
const double diag = std::sqrt((mx[0]-mn[0])*(mx[0]-mn[0]) +
(mx[1]-mn[1])*(mx[1]-mn[1]) +
(mx[2]-mn[2])*(mx[2]-mn[2]));
std::printf(" f%d sid=%d occ_in_face=%d deg=%dx%d cp=%zux%zu "
"loops=%zu loop_edges=%zu diag=%.6g rev=%d",
fi, f.surface_id, this_face_occ, ns.degree_u(), ns.degree_v(),
ns.control_points().size(),
ns.control_points().empty() ? 0 : ns.control_points()[0].size(),
f.loops.size(), nloop_edges, diag, f.reversed ? 1 : 0);
if (ns.degree_u() == 1 && ns.degree_v() == 1 &&
ns.control_points().size() >= 2) {
const auto& c0 = ns.control_points()[0][0];
const auto& c1 = ns.control_points()[0][1];
const auto& c2 = ns.control_points()[1][0];
Vector3D n = Vector3D(c1 - c0).cross(Vector3D(c2 - c0));
if (n.norm() > 1e-12) {
n = n.normalized();
std::printf(" plane_n=(%.6g,%.6g,%.6g) d=%.6g",
n.x(), n.y(), n.z(), n.dot(Vector3D(c0)));
}
}
std::printf("\n");
}
}
}
int main(int argc, char** argv) {
bool heal = false, diag = false, seams = false;
std::vector<std::string> files;
for (int i = 1; i < argc; ++i) {
const std::string a = argv[i];
if (a == "--heal") { heal = true; continue; }
if (a == "--diag") { diag = true; continue; }
if (a == "--seams") { seams = true; continue; }
files.push_back(a);
}
for (const std::string& path : files) {
std::printf("=== %s%s\n", path.c_str(), heal ? " [heal]" : "");
std::vector<BrepModel> bodies;
try {
bodies = vde::brep::import_step(path, {});
} catch (const std::exception& ex) {
std::printf(" import failed: %s\n", ex.what());
continue;
}
std::printf(" bodies=%zu\n", bodies.size());
for (size_t bi = 0; bi < bodies.size(); ++bi) {
if (heal) {
const auto hr = vde::brep::automatic_heal(bodies[bi]);
std::printf(" [heal body%zu] st=%d si=%d re=%d or=%d fi=%d ch=%d score=%d\n",
bi, hr.stitch.items_fixed, hr.simplify.items_fixed,
hr.regularize.items_fixed, hr.orient.items_fixed,
hr.fill.items_fixed, hr.check.items_fixed, hr.overall_score);
}
dump_body(bodies[bi], bi, diag, seams);
}
}
return 0;
}