From b04afd379cf1ef0f52cfe087ae0460c257e40dce Mon Sep 17 00:00:00 2001 From: hm-thinkbook14p Date: Sun, 8 Nov 2026 03:16:33 +0800 Subject: [PATCH] fix(brep): preserve arc wires and trimmed holes when offsetting faces --- src/brep/direct_modeling.cpp | 28 ++++++++++++++++++--- tests/brep/test_brep.cpp | 47 ++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/src/brep/direct_modeling.cpp b/src/brep/direct_modeling.cpp index acac6f1c..2eebde8d 100644 --- a/src/brep/direct_modeling.cpp +++ b/src/brep/direct_modeling.cpp @@ -508,10 +508,23 @@ BrepModel rebuild_with_offset( dst.add_edge(e.v_start, e.v_end); continue; } - const bool moved = vertex_offsets.count(e.v_start) != 0 || - vertex_offsets.count(e.v_end) != 0; - if (moved) dst.add_edge(e.v_start, e.v_end); - else dst.add_edge(e.v_start, e.v_end, *e.curve); + const auto i0 = vertex_offsets.find(e.v_start); + const auto i1 = vertex_offsets.find(e.v_end); + const bool moved = i0 != vertex_offsets.end() || i1 != vertex_offsets.end(); + if (!moved) { dst.add_edge(e.v_start, e.v_end, *e.curve); continue; } + if (i0 != vertex_offsets.end() && i1 != vertex_offsets.end()) { + const Vector3D d0 = i0->second - body.vertex(e.v_start).point; + const Vector3D d1 = i1->second - body.vertex(e.v_end).point; + if ((d0 - d1).norm() < 1e-12) { + auto cp = e.curve->control_points(); + for (auto& q : cp) q = q + d0; + curves::NurbsCurve nc(cp, e.curve->knots(), e.curve->weights(), + e.curve->degree()); + dst.add_edge(e.v_start, e.v_end, nc); + continue; + } + } + dst.add_edge(e.v_start, e.v_end); } for (size_t li = 0; li < body.all_loops().size(); ++li) { const auto& loop = body.all_loops()[li]; @@ -1315,6 +1328,13 @@ DirectModelingResult offset_face(const BrepModel& body, int face_id, DirectModelingResult r = tweak_face(body, face_id, offset_distance); result = r; result.operation = "offset_face"; + // 目标面重建后可能丢失原 TrimmedSurface 的孔 p-curve;用新基底面保留裁剪环。 + if (body.face(face_id).trimmed_surface_id >= 0) { + auto ts = body.trimmed_surface(body.face(face_id).trimmed_surface_id); + ts.base_surface = result.body.face_surface(face_id); + result.body.set_face_trimmed_surface( + face_id, result.body.add_trimmed_surface(ts)); + } auto edges = body.face_edges(face_id); if (edges.size() >= 4) { auto face_verts = collect_face_vertices(body, face_id); diff --git a/tests/brep/test_brep.cpp b/tests/brep/test_brep.cpp index 9464d5d0..2141346c 100644 --- a/tests/brep/test_brep.cpp +++ b/tests/brep/test_brep.cpp @@ -970,3 +970,50 @@ TEST(BrepOffset, TorusOffsetBeyondMinorRadiusFails) { auto r = offset_face(torus, 0, -3.0); EXPECT_FALSE(r.success); } + + +static double loop_area_xy(const BrepModel& b, int lid) { + const auto& loop = b.loop_by_id(lid); + std::vector p; + for (int eid : loop.edges) { + const auto& e = b.edge(eid); + p.push_back(b.vertex(e.v_start).point); + } + double a = 0.0; + for (std::size_t i = 0; i < p.size(); ++i) { + const auto& q = p[i]; + const auto& r = p[(i + 1) % p.size()]; + a += q.x() * r.y() - r.x() * q.y(); + } + return std::abs(a) * 0.5; +} + + +TEST(BrepOffset, ThroughHoleTopFaceOffsetPreservesHoleArea) { + auto box = make_box(100, 100, 100); + const auto bb = box.bounds(); + const Point3D c = bb.center(); + HoleSpec spec; + spec.diameter = 20.0; + spec.depth = 0.0; + auto body = make_hole(box, Point3D(c.x(), c.y(), bb.max().z()), + Vector3D(0, 0, -1), spec); + int top = -1; + double zmax = -1e300; + for (int fi = 0; fi < static_cast(body.num_faces()); ++fi) { + const auto b = body.face_bounds(fi); + if (body.face_normal(fi).z() > 0.99 && b.max().z() > zmax) { + zmax = b.max().z(); + top = fi; + } + } + ASSERT_GE(top, 0); + const double before = face_measure(body, top).area; + auto r = offset_face(body, top, 5.0); + EXPECT_TRUE(r.success); + EXPECT_TRUE(validate(r.body).valid); + // 100×100 减 Ø20 圆孔 = 10000 - π·10² + const double expected = 10000.0 - 3.141592653589793 * 100.0; + EXPECT_NEAR(face_measure(r.body, top).area, expected, 5.0); + EXPECT_NEAR(before, expected, 5.0); +}