fix(brep): extend adjacent cylinders for exact through-hole face offset
This commit is contained in:
@@ -539,15 +539,18 @@ BrepModel rebuild_with_offset(
|
||||
const auto& src_face = body.face(cur_fi);
|
||||
auto loop_idx = remap_loop_ids(loop_id_map, src_face);
|
||||
|
||||
bool is_affected = face_ids.count(cur_fi) != 0;
|
||||
const bool is_target = face_ids.count(cur_fi) != 0;
|
||||
bool is_affected = is_target;
|
||||
if (!is_affected) {
|
||||
for (int fid : face_ids)
|
||||
if (faces_share_edge(body, cur_fi, fid)) { is_affected = true; break; }
|
||||
}
|
||||
|
||||
if (is_affected) {
|
||||
if (is_target) {
|
||||
new_face_ids.push_back(
|
||||
rebuild_affected_face(dst, body, cur_fi, loop_idx, vertex_offsets));
|
||||
} else if (is_affected) {
|
||||
new_face_ids.push_back(copy_face_with_surface(dst, body, cur_fi, loop_idx));
|
||||
} else {
|
||||
new_face_ids.push_back(copy_face_with_surface(dst, body, cur_fi, loop_idx));
|
||||
}
|
||||
@@ -1284,8 +1287,39 @@ bool offset_analytic_body(const BrepModel& body, double delta, BrepModel& out,
|
||||
return false; // 非解析体 → 交给原有逻辑
|
||||
}
|
||||
|
||||
|
||||
void scale_adjacent_cylinder_surfaces(BrepModel& body, int target_face,
|
||||
const Vector3D& normal, double delta) {
|
||||
if (std::abs(delta) < 1e-15) return;
|
||||
for (int fi = 0; fi < static_cast<int>(body.num_faces()); ++fi) {
|
||||
if (fi == target_face) continue;
|
||||
if (!faces_share_edge(body, fi, target_face)) continue;
|
||||
const auto& s = body.face_surface(fi);
|
||||
const auto& cp = s.control_points();
|
||||
if (s.degree_u() != 2 || s.degree_v() != 1 || cp.size() < 2) continue;
|
||||
double lo = 1e300, hi = -1e300;
|
||||
for (const auto& row : cp)
|
||||
for (const auto& q : row) {
|
||||
const double d = q.dot(normal);
|
||||
lo = std::min(lo, d); hi = std::max(hi, d);
|
||||
}
|
||||
if (hi - lo < 1e-12) continue;
|
||||
const double scale = delta / (hi - lo);
|
||||
auto grid = cp;
|
||||
for (auto& row : grid)
|
||||
for (auto& q : row) {
|
||||
const double d = q.dot(normal);
|
||||
q = q + normal * ((d - lo) * scale);
|
||||
}
|
||||
curves::NurbsSurface ns(grid, s.knots_u(), s.knots_v(), s.weights(), 2, 1);
|
||||
body.set_face_surface(fi, body.add_surface(ns));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
|
||||
// ── 面等距偏移:解析曲面整组偏移(B-2)──
|
||||
|
||||
DirectModelingResult offset_face(const BrepModel& body, int face_id,
|
||||
@@ -1329,6 +1363,9 @@ DirectModelingResult offset_face(const BrepModel& body, int face_id,
|
||||
result = r;
|
||||
result.operation = "offset_face";
|
||||
// 目标面重建后可能丢失原 TrimmedSurface 的孔 p-curve;用新基底面保留裁剪环。
|
||||
scale_adjacent_cylinder_surfaces(result.body, face_id,
|
||||
compute_face_normal(body, face_id),
|
||||
offset_distance);
|
||||
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);
|
||||
|
||||
@@ -989,7 +989,8 @@ static double loop_area_xy(const BrepModel& b, int lid) {
|
||||
}
|
||||
|
||||
|
||||
TEST(BrepOffset, ThroughHoleTopFaceOffsetPreservesHoleArea) {
|
||||
|
||||
TEST(BrepOffset, ThroughHoleTopFaceOffsetPreservesHoleAreaAndVolume) {
|
||||
auto box = make_box(100, 100, 100);
|
||||
const auto bb = box.bounds();
|
||||
const Point3D c = bb.center();
|
||||
@@ -1008,12 +1009,13 @@ TEST(BrepOffset, ThroughHoleTopFaceOffsetPreservesHoleArea) {
|
||||
}
|
||||
}
|
||||
ASSERT_GE(top, 0);
|
||||
const double before = face_measure(body, top).area;
|
||||
const double expected_area = 10000.0 - 3.141592653589793 * 100.0;
|
||||
EXPECT_NEAR(face_measure(body, top).area, expected_area, 5.0);
|
||||
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);
|
||||
EXPECT_NEAR(face_measure(r.body, top).area, expected_area, 5.0);
|
||||
const double expected_volume =
|
||||
100.0 * 100.0 * 105.0 - 3.141592653589793 * 100.0 * 105.0;
|
||||
EXPECT_NEAR(analytic_measure(r.body, 0.01).volume, expected_volume, 1.0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user