Files
ViewDesignEngine/dbg_g2_selfint_probe.cpp
hm b378b191da
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
build(io/brep): VDE-006/013/067 依赖环解除 + 模块开关接线 + 裸 mesh:: 清理
VDE-006:新增集成层 vde_io 归集跨层 TU,依赖图变严格单向
io→brep→{sdf,mesh}→{curves,collision}→spatial→core→foundation,删除
-Wl,--start-group;BrepModel 桥接(brep_to_mesh/mesh_bridge)归 vde_brep。
移除兜底后暴露并修复两处反向依赖:core/icp.cpp→vde_spatial,
brep/parallel_boolean.cpp→vde::boolean(VDE_DEPS_brep 增 boolean)。

VDE-013:顶层解析 VDE_BUILD_*→VDE_ENABLE_*,VDE_DEPS_* 级联关闭,
src/ 与 tests/ 按 VDE_ENABLE_* 门控,使模块开关真正生效。

VDE-067:全仓裸 mesh:: 清理为完全限定名 vde::mesh::(正则命中 0)。

回归:全量构建 EXIT 0;test_icp/test_parallel_boolean/test_geometry_completion
全绿。既有缺陷(FEA 球面网格挂起、CAM in_range 哨兵、3MF 装配、时序竞态)
经取证与本批无关,记入 docs/reference/audit-feedback.md §5。
2026-10-07 06:39:07 +08:00

292 lines
13 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.
// 探针:网格自交修复实验 v2 — 切割 + 分类
#include "vde/brep/step_import.h"
#include "vde/brep/brep.h"
#include "vde/mesh/halfedge_mesh.h"
#include "vde/core/aabb.h"
#include <cstdio>
#include <array>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <unordered_map>
using namespace vde;
using namespace vde::brep;
using vde::core::Point3D;
using vde::core::Vector3D;
using vde::core::AABB3D;
// ── 三角形工具 ──────────────────────────────────────────────
static Point3D tri_centroid(const std::vector<Point3D>& v,
const std::array<int,3>& t) {
return (v[t[0]] + v[t[1]] + v[t[2]]) * (1.0/3.0);
}
static bool ray_tri_hit(const Point3D& o, const Vector3D& dir,
const Point3D& a, const Point3D& b, const Point3D& c,
double& t_out, double max_t, double eps = 1e-12) {
Vector3D e1 = b - a, e2 = c - a;
Vector3D pv = dir.cross(e2);
double det = e1.dot(pv);
if (std::abs(det) < eps) return false;
double inv = 1.0 / det;
Vector3D sv = o - a;
double u = sv.dot(pv) * inv;
if (u < -eps || u > 1.0 + eps) return false;
Vector3D qv = sv.cross(e1);
double vv = dir.dot(qv) * inv;
if (vv < -eps || u + vv > 1.0 + eps) return false;
double t = e2.dot(qv) * inv;
if (t <= eps || t > max_t) return false;
t_out = t;
return true;
}
/// 三角形-三角形交线段(返回 0/2 个点)
static std::vector<Point3D> tri_tri_seg(
const Point3D& a0, const Point3D& a1, const Point3D& a2,
const Point3D& b0, const Point3D& b1, const Point3D& b2) {
std::vector<Point3D> pts;
auto edges_a = std::vector<std::pair<Point3D,Point3D>>{{a0,a1},{a1,a2},{a2,a0}};
auto edges_b = std::vector<std::pair<Point3D,Point3D>>{{b0,b1},{b1,b2},{b2,b0}};
auto test_edge = [&](const Point3D& pa, const Point3D& pb,
const Point3D& q0, const Point3D& q1, const Point3D& q2) {
Vector3D dir = pb - pa;
double len = dir.norm();
if (len < 1e-15) return;
dir = dir / len;
double t = 0;
if (ray_tri_hit(pa, dir, q0, q1, q2, t, len + 1e-9)) {
Point3D ip = pa + dir * t;
for (auto& p : pts) if ((p - ip).squaredNorm() < 1e-14) return;
pts.push_back(ip);
}
};
for (auto& [pa,pb] : edges_a) test_edge(pa, pb, b0, b1, b2);
for (auto& [pa,pb] : edges_b) test_edge(pa, pb, a0, a1, a2);
return pts;
}
// ── 三角形平面 2D 投影 ─────────────────────────────────────
struct Tri2D {
Vector3D u_ax, v_ax, nrm; // 平面基
Point3D org; // 原点
std::vector<Point3D> pts3; // 顶点 3D(多边形,≥3)
double cross(const Point3D& x, const Point3D& y, const Point3D& z) const {
Point3D o = org;
Vector3D dx = x - o, dy = y - o, dz = z - o;
(void)dz;
double ux = dx.dot(u_ax), uy = dx.dot(v_ax);
double vx = dy.dot(u_ax), vy = dy.dot(v_ax);
return ux * vy - uy * vx;
}
};
/// 用直线(p,q)(在三角形平面内)分割多边形 pts3(顺序绕行)
/// 返回子多边形集(每个 ≥3 点)
static std::vector<std::vector<Point3D>> split_poly_by_line(
const Tri2D& t2d, const std::vector<Point3D>& poly,
const Point3D& p, const Point3D& q, double tol) {
std::vector<std::vector<Point3D>> out;
auto side = [&](const Point3D& x) {
Vector3D dx = x - p, dq = q - p;
return dx.dot(t2d.u_ax) * dq.dot(t2d.v_ax) - dx.dot(t2d.v_ax) * dq.dot(t2d.u_ax);
};
std::vector<double> s;
bool has_pos = false, has_neg = false;
for (auto& x : poly) {
double v = side(x);
s.push_back(v);
if (v > tol) has_pos = true;
if (v < -tol) has_neg = true;
}
if (!(has_pos && has_neg)) { out.push_back(poly); return out; }
// 沿边插入交点,构造两条链
std::vector<Point3D> chain_p, chain_n;
size_t n = poly.size();
for (size_t i = 0; i < n; ++i) {
size_t j = (i + 1) % n;
double si = s[i], sj = s[j];
if (si > tol) {
chain_p.push_back(poly[i]);
if (sj < -tol) {
double tt = si / (si - sj);
chain_p.push_back(poly[i] + (poly[j] - poly[i]) * tt);
chain_n.push_back(poly[i] + (poly[j] - poly[i]) * tt);
}
} else if (si < -tol) {
chain_n.push_back(poly[i]);
if (sj > tol) {
double tt = (-si) / (sj - si);
chain_n.push_back(poly[i] + (poly[j] - poly[i]) * tt);
chain_p.push_back(poly[i] + (poly[j] - poly[i]) * tt);
}
} else {
chain_p.push_back(poly[i]);
chain_n.push_back(poly[i]);
}
}
if (chain_p.size() >= 3) out.push_back(chain_p);
if (chain_n.size() >= 3) out.push_back(chain_n);
return out;
}
/// 对三角形递归应用多条切割线段,输出无穿透子三角形
static void cut_triangle(const Tri2D& t2d, const std::vector<Point3D>& poly,
const std::vector<std::pair<Point3D,Point3D>>& segs,
size_t si, double tol,
std::vector<std::vector<Point3D>>& out) {
if (si >= segs.size()) {
if (poly.size() >= 3) out.push_back(poly);
return;
}
// 检查该段是否穿过当前多边形(粗略:段端点是否在多边形内部/边上)
auto parts = split_poly_by_line(t2d, poly, segs[si].first, segs[si].second, tol);
if (parts.size() <= 1) { cut_triangle(t2d, poly, segs, si + 1, tol, out); return; }
for (auto& part : parts)
cut_triangle(t2d, part, segs, si + 1, tol, out);
}
int main(int argc, char** argv) {
setvbuf(stdout, nullptr, _IONBF, 0);
if (argc < 2) { std::printf("usage: dbg_g2_selfint_probe <stepfile> [deflection]\n"); return 1; }
double defl = argc > 2 ? std::atof(argv[2]) : 0.05;
auto models = import_step(argv[1]);
std::printf("bodies=%zu\n", models.size());
for (auto& m : models) {
auto msh = m.to_mesh(defl);
std::vector<Point3D> verts;
std::vector<std::array<int,3>> tris;
verts.reserve(msh.num_vertices());
for (size_t i = 0; i < msh.num_vertices(); ++i) verts.push_back(msh.vertex(i));
for (size_t fi = 0; fi < msh.num_faces(); ++fi) {
auto fv = msh.face_vertices(static_cast<int>(fi));
if (fv.size() >= 3) tris.push_back(std::array<int,3>{fv[0], fv[1], fv[2]});
}
std::printf("mesh: V=%zu T=%zu (deflection=%.3f)\n", verts.size(), tris.size(), defl);
// Step 1: 检测穿透对,收集每个三角形的切割段
std::vector<AABB3D> boxes(tris.size());
for (size_t i = 0; i < tris.size(); ++i)
for (int k = 0; k < 3; ++k) boxes[i].expand(verts[tris[i][k]]);
std::vector<std::vector<std::pair<Point3D,Point3D>>> segs_of(tris.size());
int pairs = 0;
for (size_t i = 0; i < tris.size(); ++i) {
for (size_t j = i + 1; j < tris.size(); ++j) {
if (!boxes[i].intersects(boxes[j])) continue;
auto& ti = tris[i]; auto& tj = tris[j];
// 共面/近平行三角形不构成"穿透"(facetted 平面细分的假相交)
Vector3D ni = (verts[ti[1]]-verts[ti[0]]).cross(verts[ti[2]]-verts[ti[0]]);
Vector3D nj = (verts[tj[1]]-verts[tj[0]]).cross(verts[tj[2]]-verts[tj[0]]);
if (ni.norm() < 1e-15 || nj.norm() < 1e-15) continue;
if (std::abs(ni.normalized().dot(nj.normalized())) > 0.9999) continue;
auto pts = tri_tri_seg(verts[ti[0]], verts[ti[1]], verts[ti[2]],
verts[tj[0]], verts[tj[1]], verts[tj[2]]);
if (pts.size() < 2) continue;
pairs++;
segs_of[i].push_back({pts[0], pts[1]});
segs_of[j].push_back({pts[0], pts[1]});
}
}
std::printf("penetrating pairs=%d\n", pairs);
// Step 2: 切割
std::vector<std::vector<Point3D>> cut_polys; // 每个原始三角形 → 子多边形
std::vector<int> cut_src; // 子多边形 → 源三角形索引
for (size_t i = 0; i < tris.size(); ++i) {
if (segs_of[i].empty()) {
cut_polys.push_back({verts[tris[i][0]], verts[tris[i][1]], verts[tris[i][2]]});
cut_src.push_back(static_cast<int>(i));
continue;
}
// 建立三角形平面 2D 基
Tri2D t2d;
t2d.org = verts[tris[i][0]];
Vector3D n = (verts[tris[i][1]]-t2d.org).cross(verts[tris[i][2]]-t2d.org);
double nl = n.norm();
if (nl < 1e-15) { cut_polys.push_back({verts[tris[i][0]], verts[tris[i][1]], verts[tris[i][2]]}); cut_src.push_back(static_cast<int>(i)); continue; }
t2d.nrm = n / nl;
Vector3D ref = std::abs(t2d.nrm.x()) < 0.9 ? Vector3D(1,0,0) : Vector3D(0,1,0);
t2d.u_ax = ref.cross(t2d.nrm).normalized();
t2d.v_ax = t2d.nrm.cross(t2d.u_ax);
std::vector<std::vector<Point3D>> pieces;
std::vector<Point3D> poly0 = {verts[tris[i][0]], verts[tris[i][1]], verts[tris[i][2]]};
cut_triangle(t2d, poly0, segs_of[i], 0, 1e-9, pieces);
if (pieces.empty()) pieces.push_back(poly0);
for (auto& pc : pieces) { cut_polys.push_back(pc); cut_src.push_back(static_cast<int>(i)); }
}
std::printf("cut: %zu polys\n", cut_polys.size());
// Step 3: 多边形三角化(fan)→ 全局三角形列表
std::vector<Point3D> cverts;
std::vector<std::array<int,3>> ctris;
std::unordered_map<int,int> src_count;
for (size_t pi = 0; pi < cut_polys.size(); ++pi) {
auto& poly = cut_polys[pi];
if (poly.size() < 3) continue;
// fan:以 poly[0] 为扇心
for (size_t k = 1; k + 1 < poly.size(); ++k) {
int base = static_cast<int>(cverts.size());
cverts.push_back(poly[0]);
cverts.push_back(poly[k]);
cverts.push_back(poly[k+1]);
ctris.push_back(std::array<int,3>{base, base+1, base+2});
src_count[cut_src[pi]]++;
}
}
std::printf("cut tris=%zu\n", ctris.size());
// Step 4: 分类(重心射线,跳过自身 + 共享顶点 + 共面)
AABB3D bb;
for (auto& p : cverts) bb.expand(p);
double diag = bb.extent().norm();
double teps = diag * 1e-9;
Vector3D ray(1, 0, 0);
double max_t = bb.extent().x() * 2 + 1.0;
std::vector<char> inside(ctris.size(), 0);
int removed = 0;
for (size_t i = 0; i < ctris.size(); ++i) {
Point3D c = tri_centroid(cverts, ctris[i]);
Vector3D ni = (cverts[ctris[i][1]]-cverts[ctris[i][0]]).cross(cverts[ctris[i][2]]-cverts[ctris[i][0]]);
double ni_len = ni.norm();
if (ni_len < 1e-15) continue;
ni = ni / ni_len;
int hits = 0;
for (size_t j = 0; j < ctris.size(); ++j) {
if (i == j) continue;
bool share = false;
for (int a : ctris[i]) for (int b : ctris[j]) if (a == b) { share = true; break; }
if (share) continue;
Vector3D nj = (cverts[ctris[j][1]]-cverts[ctris[j][0]]).cross(cverts[ctris[j][2]]-cverts[ctris[j][0]]);
double nj_len = nj.norm();
if (nj_len < 1e-15) continue;
nj = nj / nj_len;
if (std::abs(ni.dot(nj)) > 0.9999) {
Point3D cj = tri_centroid(cverts, ctris[j]);
if ((c - cj).norm() < diag * 1e-6) continue;
}
double t;
const auto& t3 = ctris[j];
if (ray_tri_hit(c, ray, cverts[t3[0]], cverts[t3[1]], cverts[t3[2]], t, max_t, teps))
hits++;
}
if (hits % 2 == 1) { inside[i] = 1; removed++; }
}
std::printf("removed=%d/%zu (%.1f%%)\n", removed, ctris.size(), 100.0*removed/ctris.size());
// Step 5: 重建 + 边界统计
std::vector<std::array<int,3>> keep;
for (size_t i = 0; i < ctris.size(); ++i) if (!inside[i]) keep.push_back(ctris[i]);
vde::mesh::HalfedgeMesh hm;
hm.build_from_triangles(cverts, keep);
int boundary_he = 0;
for (size_t he = 0; he < hm.num_edges() * 2; ++he)
if (hm.is_boundary_edge(static_cast<int>(he))) boundary_he++;
std::printf("after: faces=%zu boundary_edges=%d\n", keep.size(), boundary_he / 2);
}
return 0;
}