feat(brep): cap boundary-only cycles without shared-edge reuse
This commit is contained in:
@@ -25,4 +25,8 @@ int cap_boundary_loops(BrepModel& body, double tol = 1e-6);
|
||||
/// 返回被裁剪的面数;失败返回 0。
|
||||
int trim_corner_notch(BrepModel& body, const Point3D& corner, double radius);
|
||||
|
||||
/// 仅用 use==1 的边界边构成闭环封口;不新建边、不复用共享边。
|
||||
/// 返回新增面数;开放链/非平面环跳过。
|
||||
int cap_boundary_cycles(BrepModel& body, double tol = 1e-6);
|
||||
|
||||
} // namespace vde::brep
|
||||
|
||||
@@ -322,4 +322,92 @@ BrepModel compact_used_topology(const BrepModel& in) {
|
||||
}
|
||||
} // namespace
|
||||
|
||||
|
||||
int cap_boundary_cycles(BrepModel& body, double tol) {
|
||||
std::map<int, int> use;
|
||||
std::map<int, std::vector<int>> adj; // vertex -> boundary edge
|
||||
for (int fi = 0; fi < static_cast<int>(body.num_faces()); ++fi)
|
||||
for (int lid : body.face(fi).loops)
|
||||
for (int eid : body.loop_by_id(lid).edges) ++use[eid];
|
||||
for (const auto& kv : use) {
|
||||
if (kv.second != 1) continue;
|
||||
const auto& e = body.edge(kv.first);
|
||||
adj[e.v_start].push_back(kv.first);
|
||||
adj[e.v_end].push_back(kv.first);
|
||||
}
|
||||
int capped = 0;
|
||||
std::set<int> visited;
|
||||
std::set<int> used_edges;
|
||||
for (const auto& kv : adj) {
|
||||
if (kv.second.size() != 2) continue;
|
||||
const int start_v = kv.first;
|
||||
if (visited.count(start_v)) continue;
|
||||
visited.insert(start_v);
|
||||
std::vector<int> ev;
|
||||
std::vector<int> vv{start_v};
|
||||
int cur = start_v;
|
||||
int prev_e = -1;
|
||||
bool closed = false;
|
||||
while (true) {
|
||||
int next_e = -1;
|
||||
for (int eid : adj[cur])
|
||||
if (eid != prev_e && !used_edges.count(eid)) { next_e = eid; break; }
|
||||
if (next_e < 0) break;
|
||||
const auto& e = body.edge(next_e);
|
||||
const int nxt = (e.v_start == cur) ? e.v_end : e.v_start;
|
||||
ev.push_back(next_e);
|
||||
if (nxt == start_v) { closed = true; break; }
|
||||
if (visited.count(nxt)) break;
|
||||
vv.push_back(nxt);
|
||||
visited.insert(nxt);
|
||||
prev_e = next_e;
|
||||
cur = nxt;
|
||||
if (ev.size() > adj.size() + 2) break;
|
||||
}
|
||||
if (!closed || ev.size() < 3) continue;
|
||||
for (int eid : ev) used_edges.insert(eid);
|
||||
for (int v : vv) visited.insert(v);
|
||||
std::vector<Point3D> pts;
|
||||
for (int v : vv) pts.push_back(body.vertex(v).point);
|
||||
Vector3D n(0, 0, 0);
|
||||
for (std::size_t i = 0; i < pts.size(); ++i) {
|
||||
const Vector3D a = pts[i] - Point3D(0, 0, 0);
|
||||
const Vector3D b = pts[(i + 1) % pts.size()] - Point3D(0, 0, 0);
|
||||
n = n + a.cross(b);
|
||||
}
|
||||
if (n.norm() <= 1e-12) continue;
|
||||
const Vector3D nh = n.normalized();
|
||||
const Point3D origin = pts[0];
|
||||
double scale = 1.0;
|
||||
for (const auto& q : pts) scale = std::max(scale, (q - origin).norm());
|
||||
bool planar = true;
|
||||
for (const auto& q : pts)
|
||||
if (std::abs((q - origin).dot(nh)) > tol * scale) { planar = false; break; }
|
||||
if (!planar) continue;
|
||||
Vector3D u = pts[1] - pts[0];
|
||||
if (u.norm() <= 1e-12) continue;
|
||||
u = u.normalized();
|
||||
const Vector3D v = nh.cross(u);
|
||||
double umin = 0, umax = 0, vmin = 0, vmax = 0;
|
||||
for (std::size_t i = 0; i < pts.size(); ++i) {
|
||||
const Vector3D d = pts[i] - origin;
|
||||
const double du = d.dot(u), dv = d.dot(v);
|
||||
if (i == 0) { umin = umax = du; vmin = vmax = dv; }
|
||||
else { umin = std::min(umin, du); umax = std::max(umax, du);
|
||||
vmin = std::min(vmin, dv); vmax = std::max(vmax, dv); }
|
||||
}
|
||||
std::vector<std::vector<Point3D>> grid = {
|
||||
{origin + u * umin + v * vmin, origin + u * umin + v * vmax},
|
||||
{origin + u * umax + v * vmin, origin + u * umax + v * vmax}};
|
||||
curves::NurbsSurface surf(grid, {0, 0, 1, 1}, {0, 0, 1, 1},
|
||||
{{1, 1}, {1, 1}}, 1, 1);
|
||||
const int sid = body.add_surface(surf);
|
||||
const int lid = body.add_loop(ev, true);
|
||||
body.add_face(sid, {lid});
|
||||
++capped;
|
||||
}
|
||||
if (capped > 0) body.rebuild_single_shell(true);
|
||||
return capped;
|
||||
}
|
||||
|
||||
} // namespace vde::brep
|
||||
|
||||
@@ -279,3 +279,14 @@ TEST(FaceRegionsTest, CapBoxMissingFaceRepeatedIsDeterministic) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
TEST(FaceRegionsTest, CapBoundaryCyclesBox) {
|
||||
auto box = make_box(10, 10, 10);
|
||||
BrepModel open = delete_face(box, 0);
|
||||
const int capped = cap_boundary_cycles(open);
|
||||
const auto v = validate(open);
|
||||
EXPECT_EQ(capped, 1);
|
||||
EXPECT_TRUE(v.valid);
|
||||
EXPECT_DOUBLE_EQ(v.watertightness, 1.0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user