fix(brep): preserve arc wires and trimmed holes when offsetting faces

This commit is contained in:
hm
2026-11-08 03:16:33 +08:00
parent f0cd3ceb60
commit b04afd379c
2 changed files with 71 additions and 4 deletions
+24 -4
View File
@@ -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);
+47
View File
@@ -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<Point3D> 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<int>(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);
}