feat(brep): A-5b 凸台解析圆柱化 — 4×90° 解析 patch + 精确圆内环,体积 rel 7.2e-4→2.2e-11、面数 103→11、双凸台 -12.1% bug 修复
This commit is contained in:
@@ -13,10 +13,11 @@
|
||||
* |----------|-------------------------------------------------------|-------------------------|
|
||||
* | 孔 Hole | 孔径(radius)/ 孔深(depth)→ 填旧孔 + 新尺寸重钻 | 沿 offset 复制同尺寸孔 |
|
||||
* | 凸台 Boss | **半径(radius)+ 高度(depth)**:半径变化走拓扑手术 | 沿 offset 复制凸台并缝合 |
|
||||
* | | (删旧凸台侧面/顶盖+承载面底环 → 直缝重建新尺寸; | |
|
||||
* | | 仅高度变化沿用顶面 push_pull) | |
|
||||
* | 槽 Slot / | **深度(depth)**:face_ids 定位底平面 → push_pull | 需槽长/宽/圆角参数; |
|
||||
* | 腔 Pocket | 沿轴调整(加深 = 切除更多材料) | 当前给可行动诊断 |
|
||||
* | | (删旧凸台侧面/顶盖+承载面底环 → **解析圆柱 4 patch** | |
|
||||
* | | 重建:4×90° 精确圆柱面 + 解析圆边 + seam 焊接; | |
|
||||
* | | 体积相对 πr²h 误差 ≤1e-6,面数稳定 = 承载体+5 面) | |
|
||||
* | 槽 Slot / | **深度(depth)**:face_ids 定位底平面 → 沿轴偏移 | 需槽长/宽/圆角参数; |
|
||||
* | 腔 Pocket | 调整(加深 = 切除更多材料) | 当前给可行动诊断 |
|
||||
* | 圆角 Fillet | 半径(radius)→ 脊线重建(平面邻面) | 需目标边/邻面约束; |
|
||||
* | | | 当前给可行动诊断 |
|
||||
* | 倒角 Chamfer | 距离(radius)→ 脊线重建(平面邻面) | 同上 |
|
||||
@@ -25,7 +26,9 @@
|
||||
* 设计约束:
|
||||
* - 凸台半径编辑**不使用圆柱布尔差集**(差集会保留旧底环、切出浅坑并可能产生
|
||||
* 非流形);改为 remove_boss_direct(删除侧面+顶盖、承载面去掉底环、只复制
|
||||
* 被引用拓扑)+ add_boss_direct(96 边直缝重建,体积与 πr²h 相对误差 <1e-3)。
|
||||
* 被引用拓扑)+ add_boss_direct(**解析圆柱 4 patch**:4×90° 精确圆柱面 +
|
||||
* 解析圆边 + seam 端点焊接,体积相对 πr²h 误差 ≤1e-6;承载面裁剪基从拓扑环
|
||||
* + 边曲线重建,保证多次顺序缝合时裁剪域与拓扑一致)。
|
||||
* - 删除/重建任一阶段失败都返回 `errors` 诊断且 `success=false`(结果 body 为
|
||||
* 输入副本),绝不返回静默错误几何。
|
||||
*
|
||||
|
||||
+352
-151
@@ -627,159 +627,350 @@ int find_boss_base_face(const BrepModel& body, const Point3D& c, const Vector3D&
|
||||
|
||||
/// 直接在 body 上缝合一个凸台(返回新模型):
|
||||
/// 1) 复制全部面;2) 承载面加 24 边形内环;3) 加 24 个侧面 + 顶盖(带裁剪环)。
|
||||
BrepModel add_boss_direct(const BrepModel& body, const Point3D& c, const Vector3D& axis,
|
||||
double r, double height, std::string& err) {
|
||||
// P1-A5:直缝凸台侧面由 24 段提升到 96 段。凸台体积 = A_n(r)·h
|
||||
// (A_n 为正 n 边形面积),24 段时 A_24/πr² = 0.9886(相对误差 1.14%),
|
||||
// 无法满足"体积 = plate + πr²h 且 rel<1e-3"的验收;96 段误差 7.2e-4。
|
||||
constexpr int SEG = 96;
|
||||
// ── 解析圆辅助(P1-A5b:凸台侧壁升级为解析圆柱 4 patch) ──
|
||||
|
||||
/// 90° 有理二次圆弧(平面内,e1×e2 = 轴;θ ∈ [i·90°, (i+1)·90°] 逆时针)
|
||||
curves::NurbsCurve quarter_arc(const Point3D& center, double r,
|
||||
const Vector3D& e1, const Vector3D& e2, int i) {
|
||||
static const double cp[4][3][2] = {
|
||||
{{1.0, 0.0}, {1.0, 1.0}, {0.0, 1.0}},
|
||||
{{0.0, 1.0}, {-1.0, 1.0}, {-1.0, 0.0}},
|
||||
{{-1.0, 0.0}, {-1.0, -1.0}, {0.0, -1.0}},
|
||||
{{0.0, -1.0}, {1.0, -1.0}, {1.0, 0.0}},
|
||||
};
|
||||
static const double w[3] = {1.0, 0.70710678118654752440, 1.0};
|
||||
std::vector<Point3D> cps;
|
||||
cps.reserve(3);
|
||||
for (int k = 0; k < 3; ++k) {
|
||||
cps.push_back(center + e1 * (r * cp[i][k][0]) + e2 * (r * cp[i][k][1]));
|
||||
}
|
||||
return curves::NurbsCurve(std::move(cps), {0.0, 0.0, 0.0, 1.0, 1.0, 1.0},
|
||||
{w[0], w[1], w[2]}, 2);
|
||||
}
|
||||
|
||||
/// 有理 NURBS 曲线反向参数化(控制点/权重反转,节点镜像)
|
||||
curves::NurbsCurve reverse_pcurve(const curves::NurbsCurve& c) {
|
||||
std::vector<Point3D> cps(c.control_points().rbegin(), c.control_points().rend());
|
||||
std::vector<double> w(c.weights().rbegin(), c.weights().rend());
|
||||
const std::vector<double>& k = c.knots();
|
||||
std::vector<double> nk(k.size());
|
||||
const double kmax = k.empty() ? 0.0 : k.back();
|
||||
for (size_t i = 0; i < k.size(); ++i) nk[i] = kmax - k[k.size() - 1 - i];
|
||||
return curves::NurbsCurve(std::move(cps), std::move(nk), std::move(w),
|
||||
c.degree());
|
||||
}
|
||||
|
||||
/// 双线性平面片参数域框架:S(u,v) = org + u·eu + v·ev
|
||||
struct PlaneUvFrame {
|
||||
Point3D org;
|
||||
Vector3D eu, ev;
|
||||
double eu2 = 0.0, ev2 = 0.0, eudotev = 0.0, det = 1.0;
|
||||
};
|
||||
|
||||
bool plane_uv_frame(const curves::NurbsSurface& s, PlaneUvFrame& f) {
|
||||
const auto& cp = s.control_points();
|
||||
if (cp.size() < 2 || cp[0].size() < 2) return false;
|
||||
f.org = cp[0][0];
|
||||
f.eu = cp[1][0] - f.org;
|
||||
f.ev = cp[0][1] - f.org;
|
||||
f.eu2 = f.eu.dot(f.eu);
|
||||
f.ev2 = f.ev.dot(f.ev);
|
||||
f.eudotev = f.eu.dot(f.ev);
|
||||
f.det = f.eu2 * f.ev2 - f.eudotev * f.eudotev;
|
||||
return f.det > 1e-24;
|
||||
}
|
||||
|
||||
void world_to_uv(const PlaneUvFrame& f, const Point3D& p, double& u, double& v) {
|
||||
const Vector3D d = p - f.org;
|
||||
const double de = d.dot(f.eu);
|
||||
const double dv = d.dot(f.ev);
|
||||
u = (de * f.ev2 - dv * f.eudotev) / f.det;
|
||||
v = (dv * f.eu2 - de * f.eudotev) / f.det;
|
||||
}
|
||||
|
||||
/// 双线性平面片张量积曲面(与 modeling.cpp make_plane_surface 同构)
|
||||
curves::NurbsSurface make_plane_surface4(const Point3D& p0, const Point3D& p1,
|
||||
const Point3D& p2, const Point3D& p3) {
|
||||
std::vector<std::vector<Point3D>> grid = {{p0, p3}, {p1, p2}};
|
||||
return curves::NurbsSurface(grid, {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
|
||||
}
|
||||
|
||||
/// 参数域裁剪环面积符号(控制点多边形;仅用于判定内环绕向)
|
||||
double trim_loop_signed_area(const TrimLoop& lp) {
|
||||
std::vector<Point3D> pts;
|
||||
for (const auto& pc : lp.p_curves)
|
||||
for (const auto& q : pc.control_points()) pts.push_back(q);
|
||||
double a = 0.0;
|
||||
for (size_t i = 0; i < pts.size(); ++i) {
|
||||
const auto& p = pts[i];
|
||||
const auto& q = pts[(i + 1) % pts.size()];
|
||||
a += p.x() * q.y() - q.x() * p.y();
|
||||
}
|
||||
return 0.5 * a;
|
||||
}
|
||||
|
||||
/// 解析圆盘端盖:平面基 + 真实 1/4 圆弧 p-curve 外环(顶盖)
|
||||
int add_disk_cap_analytic(BrepModel& m, const Point3D& center, double r,
|
||||
const Vector3D& e1, const Vector3D& e2,
|
||||
const std::vector<curves::NurbsCurve>& arcs_ccw,
|
||||
const std::vector<int>& arc_edges, bool reversed) {
|
||||
const Point3D p0 = center - e1 * r - e2 * r;
|
||||
const Point3D p1 = center + e1 * r - e2 * r;
|
||||
const Point3D p2 = center + e1 * r + e2 * r;
|
||||
const Point3D p3 = center - e1 * r + e2 * r;
|
||||
const auto surf = make_plane_surface4(p0, p1, p2, p3);
|
||||
TrimmedSurface ts = TrimmedSurface::from_surface(surf);
|
||||
TrimLoop outer;
|
||||
outer.is_outer = true;
|
||||
for (const auto& c : arcs_ccw) {
|
||||
std::vector<Point3D> cps;
|
||||
cps.reserve(c.control_points().size());
|
||||
for (const auto& q : c.control_points()) {
|
||||
const Vector3D d = q - p0;
|
||||
cps.emplace_back(d.dot(e1) / (2.0 * r), d.dot(e2) / (2.0 * r), 0.0);
|
||||
}
|
||||
outer.p_curves.emplace_back(std::move(cps), c.knots(), c.weights(),
|
||||
c.degree());
|
||||
}
|
||||
ts.loops.push_back(std::move(outer));
|
||||
const int sid = m.add_surface(surf);
|
||||
const int tid = m.add_trimmed_surface(ts);
|
||||
const int fid = m.add_face(sid, {m.add_loop(arc_edges, true)});
|
||||
m.set_face_trimmed_surface(fid, tid);
|
||||
m.face(fid).reversed = reversed;
|
||||
return fid;
|
||||
}
|
||||
|
||||
// ── 凸台直接缝合(P1-A5b:解析圆柱 4 patch,体积 = πr²h 解析精度)──
|
||||
//
|
||||
// 侧壁:4 个 90° 解析圆柱直纹 patch(ruled(底弧, 顶弧),v 向 = +axis),
|
||||
// 法向天然朝外;seam 边按端点焊接(add_edge_unique)。
|
||||
// 顶盖:平面基 + 4 段真实 1/4 圆弧 p-curve 外环(解析圆边)。
|
||||
// 承载面:沿用原平面基/裁剪基,追加 4 段圆弧内环(p-curve 精确圆)——若沿用
|
||||
// 多边形内环则体积会退化到 n 边形精度(96 段仍有 7.2e-4 误差)。
|
||||
BrepModel add_boss_direct(const BrepModel& body, const Point3D& c,
|
||||
const Vector3D& axis, double r, double height,
|
||||
std::string& err) {
|
||||
if (!(r > 0.0) || !(height > 0.0)) {
|
||||
err = "凸台半径/高度必须 > 0";
|
||||
return {};
|
||||
}
|
||||
const Vector3D a = axis.normalized();
|
||||
const Point3D foot = c - a * (height * 0.5);
|
||||
const Point3D top_c = foot + a * height;
|
||||
const double tol = geo_tol(body);
|
||||
const int base = find_boss_base_face(body, c, a, height, r, tol);
|
||||
if (base < 0) {
|
||||
err = "无法定位凸台底座承载面";
|
||||
return {};
|
||||
}
|
||||
|
||||
Vector3D ref = std::abs(a.x()) < 0.9 ? Vector3D::UnitX() : Vector3D::UnitZ();
|
||||
const Vector3D u = a.cross(ref).normalized();
|
||||
const Vector3D v = a.cross(u).normalized();
|
||||
|
||||
std::vector<Point3D> ring(SEG), top(SEG);
|
||||
for (int i = 0; i < SEG; ++i) {
|
||||
const double ang = 2.0 * M_PI * i / SEG;
|
||||
const double dx = r * std::cos(ang), dy = r * std::sin(ang);
|
||||
ring[i] = Point3D(foot.x() + u.x() * dx + v.x() * dy,
|
||||
foot.y() + u.y() * dx + v.y() * dy,
|
||||
foot.z() + u.z() * dx + v.z() * dy);
|
||||
top[i] = Point3D(ring[i].x() + a.x() * height,
|
||||
ring[i].y() + a.y() * height,
|
||||
ring[i].z() + a.z() * height);
|
||||
}
|
||||
|
||||
// 外环有向面积 → 判定绕向(B-Rep:内环与外环反向)
|
||||
double area2 = 0.0;
|
||||
for (int i = 0; i < SEG; ++i) {
|
||||
const int j = (i + 1) % SEG;
|
||||
area2 += u.dot(ring[i]) * v.dot(ring[j]) - u.dot(ring[j]) * v.dot(ring[i]);
|
||||
}
|
||||
const bool inner_cw = area2 > 0.0; // 外环 CCW → 内环须 CW
|
||||
const Vector3D e1 = ref.cross(a).normalized(); // e1 × e2 = a
|
||||
const Vector3D e2 = a.cross(e1);
|
||||
|
||||
BrepModel dst;
|
||||
std::vector<int> vmap(body.num_vertices(), -1);
|
||||
for (size_t i = 0; i < body.num_vertices(); ++i)
|
||||
vmap[i] = dst.add_vertex(body.vertex(static_cast<int>(i)).point);
|
||||
std::vector<int> smap(body.num_surfaces(), -1);
|
||||
for (size_t i = 0; i < body.num_surfaces(); ++i)
|
||||
smap[i] = dst.add_surface(body.surface(static_cast<int>(i)));
|
||||
std::map<int, int> vmap, smap;
|
||||
std::map<std::pair<int, int>, int> ecache;
|
||||
auto eid = [&](int x, int y) -> int {
|
||||
auto nv = [&](int vid) -> int {
|
||||
const auto it = vmap.find(vid);
|
||||
if (it != vmap.end()) return it->second;
|
||||
const int id = dst.add_vertex(body.vertex_by_id(vid).point);
|
||||
vmap[vid] = id;
|
||||
return id;
|
||||
};
|
||||
auto ne = [&](int x, int y,
|
||||
const curves::NurbsCurve* curve) -> int {
|
||||
const auto key = std::make_pair(std::min(x, y), std::max(x, y));
|
||||
const auto it = ecache.find(key);
|
||||
if (it != ecache.end()) return it->second;
|
||||
const int e = dst.add_edge(x, y);
|
||||
ecache[key] = e;
|
||||
return e;
|
||||
const int id = curve != nullptr ? dst.add_edge(x, y, *curve)
|
||||
: dst.add_edge(x, y);
|
||||
ecache[key] = id;
|
||||
return id;
|
||||
};
|
||||
std::vector<int> rv(SEG), tv(SEG);
|
||||
for (int i = 0; i < SEG; ++i) {
|
||||
rv[i] = dst.add_vertex(ring[i]);
|
||||
tv[i] = dst.add_vertex(top[i]);
|
||||
auto ns = [&](int sid) -> int {
|
||||
if (sid < 0 || sid >= static_cast<int>(body.num_surfaces())) return -1;
|
||||
const auto it = smap.find(sid);
|
||||
if (it != smap.end()) return it->second;
|
||||
const int id = dst.add_surface(body.surface(sid));
|
||||
smap[sid] = id;
|
||||
return id;
|
||||
};
|
||||
|
||||
// 凸台 8 顶点 + 12 边(4 底弧 + 4 顶弧 + 4 seam)
|
||||
std::vector<int> vb(4), vt(4), eb(4), et(4), seam(4);
|
||||
std::vector<curves::NurbsCurve> ab(4), at(4);
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
const double ang = 0.5 * M_PI * i;
|
||||
const Vector3D off =
|
||||
e1 * (r * std::cos(ang)) + e2 * (r * std::sin(ang));
|
||||
vb[i] = dst.add_vertex(foot + off);
|
||||
vt[i] = dst.add_vertex(top_c + off);
|
||||
}
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
const int j = (i + 1) % 4;
|
||||
ab[i] = quarter_arc(foot, r, e1, e2, i);
|
||||
at[i] = quarter_arc(top_c, r, e1, e2, i);
|
||||
eb[i] = dst.add_edge(vb[i], vb[j], ab[i]);
|
||||
et[i] = dst.add_edge(vt[i], vt[j], at[i]);
|
||||
seam[i] = dst.add_edge_unique(vb[i], vt[i]);
|
||||
}
|
||||
|
||||
// 复制全部面;承载面追加内环
|
||||
// 复制其余面(承载面单独重建);顶点/边/曲面/裁剪面/朝向按原样保留
|
||||
std::vector<int> all_faces;
|
||||
for (size_t fi = 0; fi < body.num_faces(); ++fi) {
|
||||
const int g = static_cast<int>(fi);
|
||||
const auto& f = body.face(g);
|
||||
std::vector<int> loops;
|
||||
const int fid = static_cast<int>(fi);
|
||||
if (fid == base) continue;
|
||||
const auto& f = body.face(fid);
|
||||
std::vector<int> new_loops;
|
||||
for (int li : f.loops) {
|
||||
const auto& lp = body.loop_by_id(li);
|
||||
std::vector<int> es;
|
||||
es.reserve(lp.edges.size());
|
||||
for (int ei : lp.edges) {
|
||||
const auto& e = body.edge(ei);
|
||||
es.push_back(eid(vmap[e.v_start], vmap[e.v_end]));
|
||||
es.push_back(ne(nv(e.v_start), nv(e.v_end), e.curve.get()));
|
||||
}
|
||||
loops.push_back(dst.add_loop(es, lp.is_outer));
|
||||
new_loops.push_back(dst.add_loop(es, lp.is_outer));
|
||||
}
|
||||
if (g == base) {
|
||||
std::vector<int> es;
|
||||
for (int i = 0; i < SEG; ++i)
|
||||
es.push_back(inner_cw ? eid(rv[(i + 1) % SEG], rv[i])
|
||||
: eid(rv[i], rv[(i + 1) % SEG]));
|
||||
loops.push_back(dst.add_loop(es, false));
|
||||
const int sid = ns(f.surface_id);
|
||||
if (sid < 0) {
|
||||
err = "复制面缺少有效曲面";
|
||||
return {};
|
||||
}
|
||||
const int nf = dst.add_face(smap[f.surface_id], loops);
|
||||
// 复制原面须保留朝向标志(STEP same_sense=.F. 导入的面 reversed=true)。
|
||||
dst.set_face_reversed(nf, f.reversed);
|
||||
all_faces.push_back(nf);
|
||||
const int nfid = dst.add_face(sid, new_loops);
|
||||
dst.set_face_reversed(nfid, f.reversed);
|
||||
if (f.trimmed_surface_id >= 0 &&
|
||||
f.trimmed_surface_id <
|
||||
static_cast<int>(body.num_trimmed_surfaces())) {
|
||||
const int tid = dst.add_trimmed_surface(
|
||||
body.trimmed_surface(f.trimmed_surface_id));
|
||||
dst.set_face_trimmed_surface(nfid, tid);
|
||||
}
|
||||
all_faces.push_back(nfid);
|
||||
}
|
||||
|
||||
// 24 个侧面(平面四边形,镜像 make_cylinder 的构造/绕向)
|
||||
for (int i = 0; i < SEG; ++i) {
|
||||
const int j = (i + 1) % SEG;
|
||||
std::vector<std::vector<Point3D>> grid = {
|
||||
{ring[i], top[i]},
|
||||
{ring[j], top[j]}};
|
||||
curves::NurbsSurface surf(grid, {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
|
||||
const int sid = dst.add_surface(surf);
|
||||
auto ts = TrimmedSurface::from_rect(surf, 0.0, 1.0, 0.0, 1.0);
|
||||
const int tid = dst.add_trimmed_surface(ts);
|
||||
std::vector<int> es = {eid(rv[i], tv[i]), eid(tv[i], tv[j]),
|
||||
eid(tv[j], rv[j]), eid(rv[j], rv[i])};
|
||||
const int nf = dst.add_face(sid, {dst.add_loop(es, true)});
|
||||
dst.set_face_trimmed_surface(nf, tid);
|
||||
all_faces.push_back(nf);
|
||||
// 承载面:从拓扑环 + 边曲线**重建精确裁剪基**(外环 CCW、既有孔 CW),再追加
|
||||
// 新凸台底环(4 段圆弧 p-curve)。不复制旧 TS——多凸台顺序缝合时复制旧 TS 再
|
||||
// 追加内环会累积环集合不一致(实测双凸台体积少 1.2%)。
|
||||
const auto& bf = body.face(base);
|
||||
const curves::NurbsSurface& bsurf = body.surface(bf.surface_id);
|
||||
PlaneUvFrame uv;
|
||||
if (!plane_uv_frame(bsurf, uv)) {
|
||||
err = "凸台承载面参数域不是平面";
|
||||
return {};
|
||||
}
|
||||
|
||||
// 顶盖:2x2 平面补丁 + 24 边形裁剪环(flip_v 约定同 make_cylinder 顶盖)。
|
||||
// 补丁必须以外轴中心(foot + a*height = top[0] − u*r)为中心:原实现以
|
||||
// top[0](环上 0 号顶点,本身偏出轴线一个半径 u*r)为锚点,使存储基面整体
|
||||
// 偏移 r,面几何与裁剪环/顶点不一致。平面 TS 面的网格化改走"环驱动"重建
|
||||
// (VDE-1193)因而掩盖了该缺陷,但解析度量(build_face_trim_domain 直接返回
|
||||
// 存储 TS)的一阶矩/质心随之外移一个半径(凸台顶盖质心 (−0,3,11) 而非
|
||||
// (0,0,11),FeatureEditTest.EditBoss_IncreaseHeight/CopyBoss_Offset_AddsBoss)。
|
||||
{
|
||||
const Point3D ctop(top[0].x() - u.x() * r, top[0].y() - u.y() * r,
|
||||
top[0].z() - u.z() * r);
|
||||
std::vector<std::vector<Point3D>> grid = {
|
||||
{Point3D(ctop.x() - u.x() * r + v.x() * r, ctop.y() - u.y() * r + v.y() * r, ctop.z() - u.z() * r + v.z() * r),
|
||||
Point3D(ctop.x() + u.x() * r + v.x() * r, ctop.y() + u.y() * r + v.y() * r, ctop.z() + u.z() * r + v.z() * r)},
|
||||
{Point3D(ctop.x() - u.x() * r - v.x() * r, ctop.y() - u.y() * r - v.y() * r, ctop.z() - u.z() * r - v.z() * r),
|
||||
Point3D(ctop.x() + u.x() * r - v.x() * r, ctop.y() + u.y() * r - v.y() * r, ctop.z() + u.z() * r - v.z() * r)}};
|
||||
curves::NurbsSurface surf(grid, {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
|
||||
TrimLoop trim;
|
||||
trim.is_outer = true;
|
||||
for (int i = 0; i < SEG; ++i) {
|
||||
const double a0 = 2.0 * M_PI * i / SEG, a1 = 2.0 * M_PI * ((i + 1) % SEG) / SEG;
|
||||
const double u0 = 0.5 + 0.5 * std::cos(a0), u1 = 0.5 + 0.5 * std::cos(a1);
|
||||
const double v0 = 0.5 - 0.5 * std::sin(a0), v1 = 0.5 - 0.5 * std::sin(a1);
|
||||
std::vector<Point3D> cps = {Point3D(u0, v0, 0.0), Point3D(u1, v1, 0.0)};
|
||||
std::vector<double> knots = {0.0, 0.0, 1.0, 1.0};
|
||||
std::vector<double> weights = {1.0, 1.0};
|
||||
trim.p_curves.push_back(PCurve(cps, knots, weights, 1));
|
||||
const auto edge_to_pcurve = [&](const TopoEdge& e) -> PCurve {
|
||||
if (e.curve) {
|
||||
const auto& c = *e.curve;
|
||||
std::vector<Point3D> cps;
|
||||
cps.reserve(c.control_points().size());
|
||||
for (const auto& q : c.control_points()) {
|
||||
double u = 0.0, v = 0.0;
|
||||
world_to_uv(uv, q, u, v);
|
||||
cps.emplace_back(u, v, 0.0);
|
||||
}
|
||||
return PCurve(std::move(cps), c.knots(), c.weights(), c.degree());
|
||||
}
|
||||
TrimmedSurface ts = TrimmedSurface::from_surface(surf);
|
||||
ts.loops = {trim};
|
||||
const int sid = dst.add_surface(surf);
|
||||
const int tid = dst.add_trimmed_surface(ts);
|
||||
double u0 = 0.0, v0 = 0.0, u1 = 0.0, v1 = 0.0;
|
||||
world_to_uv(uv, body.vertex_by_id(e.v_start).point, u0, v0);
|
||||
world_to_uv(uv, body.vertex_by_id(e.v_end).point, u1, v1);
|
||||
std::vector<Point3D> cps = {Point3D(u0, v0, 0.0),
|
||||
Point3D(u1, v1, 0.0)};
|
||||
return PCurve(std::move(cps), {0.0, 0.0, 1.0, 1.0}, {1.0, 1.0}, 1);
|
||||
};
|
||||
const auto reverse_loop_dir = [](const TrimLoop& lp) {
|
||||
TrimLoop out;
|
||||
out.is_outer = lp.is_outer;
|
||||
for (auto it = lp.p_curves.rbegin(); it != lp.p_curves.rend(); ++it)
|
||||
out.p_curves.push_back(reverse_pcurve(*it));
|
||||
return out;
|
||||
};
|
||||
TrimmedSurface bts = TrimmedSurface::from_surface(bsurf);
|
||||
for (int lid : bf.loops) {
|
||||
const auto& lp = body.loop_by_id(lid);
|
||||
TrimLoop loop;
|
||||
loop.is_outer = lp.is_outer;
|
||||
for (int ei : lp.edges)
|
||||
loop.p_curves.push_back(edge_to_pcurve(body.edge(ei)));
|
||||
const double area = trim_loop_signed_area(loop);
|
||||
if (loop.is_outer) {
|
||||
if (area < 0.0) loop = reverse_loop_dir(loop); // 外环逆时针
|
||||
} else {
|
||||
if (area > 0.0) loop = reverse_loop_dir(loop); // 内环顺时针
|
||||
}
|
||||
bts.loops.push_back(std::move(loop));
|
||||
}
|
||||
const auto make_hole_loop = [&](bool reversed_dir) {
|
||||
TrimLoop loop;
|
||||
loop.is_outer = false;
|
||||
for (int k = 0; k < 4; ++k) {
|
||||
const int i = reversed_dir ? (3 - k) : k;
|
||||
curves::NurbsCurve cc = ab[i];
|
||||
if (reversed_dir) cc = reverse_pcurve(cc);
|
||||
std::vector<Point3D> cps;
|
||||
cps.reserve(cc.control_points().size());
|
||||
for (const auto& q : cc.control_points()) {
|
||||
double u = 0.0, v = 0.0;
|
||||
world_to_uv(uv, q, u, v);
|
||||
cps.emplace_back(u, v, 0.0);
|
||||
}
|
||||
loop.p_curves.emplace_back(std::move(cps), cc.knots(), cc.weights(),
|
||||
cc.degree());
|
||||
}
|
||||
return loop;
|
||||
};
|
||||
TrimLoop hole = make_hole_loop(false);
|
||||
if (trim_loop_signed_area(hole) > 0.0)
|
||||
hole = make_hole_loop(true); // 内环必须顺时针
|
||||
bts.loops.push_back(std::move(hole));
|
||||
|
||||
std::vector<int> base_loops;
|
||||
for (int li : bf.loops) {
|
||||
const auto& lp = body.loop_by_id(li);
|
||||
std::vector<int> es;
|
||||
for (int i = 0; i < SEG; ++i) es.push_back(eid(tv[(i + 1) % SEG], tv[i]));
|
||||
const int nf = dst.add_face(sid, {dst.add_loop(es, true)});
|
||||
dst.set_face_trimmed_surface(nf, tid);
|
||||
all_faces.push_back(nf);
|
||||
es.reserve(lp.edges.size());
|
||||
for (int ei : lp.edges) {
|
||||
const auto& e = body.edge(ei);
|
||||
es.push_back(ne(nv(e.v_start), nv(e.v_end), e.curve.get()));
|
||||
}
|
||||
base_loops.push_back(dst.add_loop(es, lp.is_outer));
|
||||
}
|
||||
base_loops.push_back(
|
||||
dst.add_loop({eb[3], eb[2], eb[1], eb[0]}, false)); // 底环(顺时针)
|
||||
const int bsid = ns(bf.surface_id);
|
||||
const int btid = dst.add_trimmed_surface(bts);
|
||||
const int bfid = dst.add_face(bsid, base_loops);
|
||||
dst.set_face_trimmed_surface(bfid, btid);
|
||||
dst.set_face_reversed(bfid, bf.reversed);
|
||||
all_faces.push_back(bfid);
|
||||
|
||||
// 4 个解析圆柱侧壁 patch(ruled(底弧, 顶弧) 为精确圆柱面,法向朝外)
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
const int j = (i + 1) % 4;
|
||||
const auto wall = curves::NurbsSurface::ruled(ab[i], at[i]);
|
||||
const int sid = dst.add_surface(wall);
|
||||
const int tid = dst.add_trimmed_surface(
|
||||
TrimmedSurface::from_rect(wall, 0.0, 1.0, 0.0, 1.0));
|
||||
const int lp = dst.add_loop({eb[i], seam[j], et[i], seam[i]}, true);
|
||||
const int nfid = dst.add_face(sid, {lp});
|
||||
dst.set_face_trimmed_surface(nfid, tid);
|
||||
all_faces.push_back(nfid);
|
||||
}
|
||||
// 顶盖:平面基 + 解析圆弧外环
|
||||
all_faces.push_back(
|
||||
add_disk_cap_analytic(dst, top_c, r, e1, e2, at, et, false));
|
||||
|
||||
const int sh = dst.add_shell(all_faces, true);
|
||||
dst.add_body({sh}, "boss");
|
||||
dst.add_body({sh}, body.label());
|
||||
if (!dst.is_valid()) {
|
||||
err = "解析凸台重建后模型无效(拓扑/朝向异常)";
|
||||
return {};
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
// ── 孔编辑 ──
|
||||
|
||||
/// 从识别特征定位解析圆柱孔壁(VDE-1225 make_hole 直拓扑):
|
||||
/// make_hole 的孔壁是多片同轴同半径的解析圆柱面 patch,detect_faceted_holes
|
||||
/// 只认面片化平面墙,故用 feature.face_ids 的顶点拟合轴线点/半径/轴向范围。
|
||||
/// 要求:face_ids 合法、顶点径向距离一致(整圈圆柱壳)、轴向跨度 > 0。
|
||||
static bool locate_analytic_hole(const BrepModel& body, const RecognizedFeature& f,
|
||||
FacetedHole& out) {
|
||||
@@ -1001,18 +1192,49 @@ bool locate_boss_geometry(const BrepModel& body, const RecognizedFeature& f,
|
||||
const Vector3D axis = f.axis.norm() > 0.5 ? f.axis.normalized() : Vector3D::UnitZ();
|
||||
const int nf = static_cast<int>(body.num_faces());
|
||||
|
||||
// 侧面:face_ids 中平面且法向 ⊥ axis 的面;face_ids 缺失时按中心/半径回退
|
||||
std::vector<int> sides;
|
||||
// P1-A5b:侧壁可能是解析圆柱 patch(非平面),按“同时触及底线与顶线”识别,
|
||||
// 兼容旧直缝四边形。先用 face_ids 全部顶点求轴向范围。
|
||||
std::vector<int> candidates;
|
||||
for (int fid : f.face_ids) {
|
||||
if (fid < 0 || fid >= nf) continue;
|
||||
Vector3D n;
|
||||
double nd = 0.0;
|
||||
if (!face_plane(body, fid, n, nd)) continue;
|
||||
if (std::abs(n.dot(axis)) > 0.5) continue;
|
||||
sides.push_back(fid);
|
||||
candidates.push_back(fid);
|
||||
}
|
||||
std::set<int> cand_vids;
|
||||
for (int fid : candidates) {
|
||||
for (int ei : body.face_edges(fid)) {
|
||||
const auto& e = body.edge(ei);
|
||||
cand_vids.insert(e.v_start);
|
||||
cand_vids.insert(e.v_end);
|
||||
}
|
||||
}
|
||||
if (cand_vids.size() < 6) {
|
||||
err = "凸台侧面顶点不足(feature.face_ids 缺失或无效,请先 recognize_features)";
|
||||
return false;
|
||||
}
|
||||
double zlo = 1e30, zhi = -1e30;
|
||||
for (int vid : cand_vids) {
|
||||
const double z = body.vertex_by_id(vid).point.dot(axis);
|
||||
zlo = std::min(zlo, z);
|
||||
zhi = std::max(zhi, z);
|
||||
}
|
||||
const double height = zhi - zlo;
|
||||
if (height < tol) { err = "凸台高度退化"; return false; }
|
||||
|
||||
std::vector<int> sides;
|
||||
for (int fid : candidates) {
|
||||
bool touches_low = false, touches_high = false;
|
||||
for (int ei : body.face_edges(fid)) {
|
||||
const auto& e = body.edge(ei);
|
||||
for (int vid : {e.v_start, e.v_end}) {
|
||||
const double z = body.vertex_by_id(vid).point.dot(axis);
|
||||
if (z <= zlo + 0.05 * height) touches_low = true;
|
||||
if (z >= zhi - 0.05 * height) touches_high = true;
|
||||
}
|
||||
}
|
||||
if (touches_low && touches_high) sides.push_back(fid);
|
||||
}
|
||||
if (sides.size() < 3) {
|
||||
err = "凸台侧面不足 3 片(feature.face_ids 缺失或无效,请先 recognize_features)";
|
||||
err = "凸台侧面不足 3 片(face_ids 未覆盖完整侧壁)";
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1025,14 +1247,7 @@ bool locate_boss_geometry(const BrepModel& body, const RecognizedFeature& f,
|
||||
}
|
||||
}
|
||||
if (vids.size() < 6) { err = "凸台侧面顶点不足"; return false; }
|
||||
double zmin = 1e30, zmax = -1e30;
|
||||
for (int vid : vids) {
|
||||
const double z = body.vertex_by_id(vid).point.dot(axis);
|
||||
zmin = std::min(zmin, z);
|
||||
zmax = std::max(zmax, z);
|
||||
}
|
||||
const double height = zmax - zmin;
|
||||
if (height < tol) { err = "凸台高度退化"; return false; }
|
||||
const double zmin = zlo, zmax = zhi;
|
||||
|
||||
// 底线/顶线中心
|
||||
Point3D bot_c(0, 0, 0), top_c(0, 0, 0);
|
||||
@@ -1130,11 +1345,12 @@ BrepModel remove_boss_direct(const BrepModel& body, const BossGeometry& g,
|
||||
vmap[vid] = id;
|
||||
return id;
|
||||
};
|
||||
auto ne = [&](int a, int b) -> int {
|
||||
auto ne = [&](int a, int b, const curves::NurbsCurve* curve) -> int {
|
||||
const auto key = std::make_pair(std::min(a, b), std::max(a, b));
|
||||
const auto it = emap.find(key);
|
||||
if (it != emap.end()) return it->second;
|
||||
const int id = dst.add_edge(a, b);
|
||||
const int id = curve != nullptr ? dst.add_edge(a, b, *curve)
|
||||
: dst.add_edge(a, b);
|
||||
emap[key] = id;
|
||||
return id;
|
||||
};
|
||||
@@ -1160,7 +1376,7 @@ BrepModel remove_boss_direct(const BrepModel& body, const BossGeometry& g,
|
||||
es.reserve(lp.edges.size());
|
||||
for (int ei : lp.edges) {
|
||||
const auto& e = body.edge(ei);
|
||||
es.push_back(ne(nv(e.v_start), nv(e.v_end)));
|
||||
es.push_back(ne(nv(e.v_start), nv(e.v_end), e.curve.get()));
|
||||
}
|
||||
if (es.size() < 3) {
|
||||
err = "删除凸台后出现退化环";
|
||||
@@ -1235,24 +1451,9 @@ DirectModelingResult edit_boss(const BrepModel& body, const RecognizedFeature& f
|
||||
return r;
|
||||
}
|
||||
|
||||
// 仅高度变化:沿用顶面 push_pull(原拓扑最稳)
|
||||
if (std::abs(new_r - old_r) <= tol) {
|
||||
auto pr = push_pull(body, g.top_face, new_h - old_h);
|
||||
if (!pr.success || !pr.body.is_valid()) {
|
||||
r.errors.push_back("push_pull 调整凸台高度失败");
|
||||
for (const auto& e : pr.errors) r.errors.push_back(e);
|
||||
return r;
|
||||
}
|
||||
r.success = true;
|
||||
r.body = std::move(pr.body);
|
||||
char buf[160];
|
||||
std::snprintf(buf, sizeof(buf), "凸台高度 %.3f -> %.3f", old_h, new_h);
|
||||
r.warnings.push_back(buf);
|
||||
return r;
|
||||
}
|
||||
|
||||
// 半径变化(可与高度组合):删除旧凸台(拓扑手术)→ 直缝重建新半径/高度。
|
||||
// 不使用圆柱布尔差集——差集会保留旧凸台底环切出浅坑并产生非流形。
|
||||
// 任意尺寸变化(半径/高度/组合):删除旧凸台(拓扑手术)→ 解析圆柱 4 patch
|
||||
// 重建新半径/高度。不使用圆柱布尔差集(会切出浅坑/非流形);也不再走
|
||||
// push_pull 改高度(平面顶盖推拉会生成重复侧面条带,破坏解析体积口径)。
|
||||
BrepModel removed = remove_boss_direct(body, g, err);
|
||||
if (removed.num_faces() == 0) {
|
||||
r.errors.push_back("删除旧凸台失败: " + err);
|
||||
|
||||
@@ -766,3 +766,149 @@ TEST(FeatureEditTest, CopyFeature_FilletChamferSlotPocketRib_HonestDiagnostics)
|
||||
EXPECT_EQ(r.body.num_faces(), box.num_faces());
|
||||
}
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
// P1-A5b: 凸台解析圆柱 4 patch 重建(体积 vs π 解析 rel ≤ 1e-6)
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
namespace {
|
||||
|
||||
/// π 解析板+凸台体积:plate(10×10×10) + πr²h
|
||||
double analytic_plate_boss_volume(double r, double h) {
|
||||
return 1000.0 + M_PI * r * r * h;
|
||||
}
|
||||
|
||||
/// 由解析圆柱 patch 顶点反算凸台半径(顶点严格落在圆上)
|
||||
double analytic_boss_radius(const BrepModel& body, double z_lo, double z_hi) {
|
||||
const auto sides = boss_side_faces(body, z_lo, z_hi);
|
||||
std::set<int> vids;
|
||||
for (int fid : sides) {
|
||||
for (int ei : body.face_edges(fid)) {
|
||||
const auto& e = body.edge(ei);
|
||||
vids.insert(e.v_start);
|
||||
vids.insert(e.v_end);
|
||||
}
|
||||
}
|
||||
double rsum = 0.0;
|
||||
int n = 0;
|
||||
for (int vid : vids) {
|
||||
const Point3D p = body.vertex_by_id(vid).point;
|
||||
rsum += std::sqrt(p.x() * p.x() + p.y() * p.y());
|
||||
++n;
|
||||
}
|
||||
return n > 0 ? rsum / static_cast<double>(n) : 0.0;
|
||||
}
|
||||
|
||||
/// 网格体积(散度定理),用于 to_mesh 后的独立质量积分核对
|
||||
double mesh_volume_of(const BrepModel& body, double deflection) {
|
||||
const auto mesh = body.to_mesh(deflection);
|
||||
double vol = 0.0;
|
||||
for (size_t fi = 0; fi < mesh.num_faces(); ++fi) {
|
||||
const auto fv = mesh.face_vertices(static_cast<int>(fi));
|
||||
for (size_t k = 1; k + 1 < fv.size(); ++k) {
|
||||
const auto& a = mesh.vertex(static_cast<size_t>(fv[0]));
|
||||
const auto& b = mesh.vertex(static_cast<size_t>(fv[k]));
|
||||
const auto& c = mesh.vertex(static_cast<size_t>(fv[k + 1]));
|
||||
vol += a.dot(b.cross(c)) / 6.0;
|
||||
}
|
||||
}
|
||||
return vol;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(FeatureEditTest, AnalyticBossConstruction_TopologyAndVolume) {
|
||||
auto body = box_with_boss(3.0, 4.0, {0, 0, 7});
|
||||
ASSERT_TRUE(body.is_valid());
|
||||
const auto vr = validate(body);
|
||||
EXPECT_TRUE(vr.valid);
|
||||
EXPECT_GE(vr.watertightness, 0.999);
|
||||
// 板 6 面 + 4 解析圆柱 patch + 1 解析圆盘顶盖 = 11 面
|
||||
EXPECT_EQ(body.num_faces(), 11u);
|
||||
EXPECT_EQ(body.num_vertices(), 16u);
|
||||
EXPECT_EQ(body.num_edges(), 24u);
|
||||
// 4 片 90° 解析圆柱侧壁
|
||||
EXPECT_EQ(boss_side_faces(body, 5.0, 9.0).size(), 4u);
|
||||
// 解析体积 = 1000 + π·3²·4(rel ≤ 1e-6)
|
||||
const double expected = analytic_plate_boss_volume(3.0, 4.0);
|
||||
const double rel = std::abs(volume(body) - expected) / expected;
|
||||
EXPECT_LT(rel, 1e-6) << "volume=" << volume(body)
|
||||
<< " expected=" << expected << " rel=" << rel;
|
||||
}
|
||||
|
||||
TEST(FeatureEditTest, EditBoss_AnalyticRadius_VolumeRel1e6) {
|
||||
const double r0 = 3.0, h0 = 4.0;
|
||||
auto body = box_with_boss(r0, h0, {0, 0, 7});
|
||||
ASSERT_TRUE(body.is_valid());
|
||||
ASSERT_EQ(body.num_faces(), 11u);
|
||||
auto f = boss_feature_of(body, r0, h0);
|
||||
ASSERT_GE(f.face_ids.size(), 3u);
|
||||
|
||||
auto r = edit_feature(body, f, FeatureEditParams{4.0, 0.0, 0.0});
|
||||
ASSERT_TRUE(r.success) << (r.errors.empty() ? "" : r.errors[0]);
|
||||
ASSERT_TRUE(r.body.is_valid());
|
||||
EXPECT_GE(validate(r.body).watertightness, 0.999);
|
||||
// 体积 = 1000 + π·4²·4,rel ≤ 1e-6
|
||||
const double expected = analytic_plate_boss_volume(4.0, h0);
|
||||
const double rel = std::abs(volume(r.body) - expected) / expected;
|
||||
EXPECT_LT(rel, 1e-6) << "rel=" << rel;
|
||||
// 面数稳定(4 解析 patch + 1 顶盖)
|
||||
EXPECT_EQ(r.body.num_faces(), 11u);
|
||||
EXPECT_EQ(boss_side_faces(r.body, 5.0, 9.0).size(), 4u);
|
||||
// 反算半径 = 新值(≤1e-9)
|
||||
EXPECT_NEAR(analytic_boss_radius(r.body, 5.0, 9.0), 4.0, 1e-9);
|
||||
// to_mesh / 独立质量积分正常:网格非空、无 NaN、网格体积在解析值 2% 内
|
||||
const auto mesh = r.body.to_mesh(0.005);
|
||||
EXPECT_GT(mesh.num_faces(), 0u);
|
||||
const double mesh_vol = mesh_volume_of(r.body, 0.005);
|
||||
EXPECT_TRUE(std::isfinite(mesh_vol));
|
||||
EXPECT_NEAR(mesh_vol, expected, 0.02 * expected);
|
||||
EXPECT_FALSE(r.warnings.empty());
|
||||
}
|
||||
|
||||
TEST(FeatureEditTest, EditBoss_AnalyticRadiusAndDepth_VolumeRel1e6) {
|
||||
const double r0 = 3.0, h0 = 4.0;
|
||||
auto body = box_with_boss(r0, h0, {0, 0, 7});
|
||||
ASSERT_TRUE(body.is_valid());
|
||||
auto f = boss_feature_of(body, r0, h0);
|
||||
// 组合:r 3→2.5、h 4→6
|
||||
auto r = edit_feature(body, f, FeatureEditParams{2.5, 6.0, 0.0});
|
||||
ASSERT_TRUE(r.success) << (r.errors.empty() ? "" : r.errors[0]);
|
||||
ASSERT_TRUE(r.body.is_valid());
|
||||
EXPECT_GE(validate(r.body).watertightness, 0.999);
|
||||
const double expected = analytic_plate_boss_volume(2.5, 6.0);
|
||||
const double rel = std::abs(volume(r.body) - expected) / expected;
|
||||
EXPECT_LT(rel, 1e-6) << "rel=" << rel;
|
||||
EXPECT_EQ(r.body.num_faces(), 11u);
|
||||
EXPECT_NEAR(analytic_boss_radius(r.body, 5.0, 11.0), 2.5, 1e-9);
|
||||
EXPECT_TRUE(boss_top_exists(r.body, {0, 0, 8}, Vector3D::UnitZ(), 2.5, 6.0));
|
||||
}
|
||||
|
||||
TEST(FeatureEditTest, EditBoss_AnalyticHeightOnly_VolumeRel1e6) {
|
||||
const double r0 = 3.0, h0 = 4.0;
|
||||
auto body = box_with_boss(r0, h0, {0, 0, 7});
|
||||
auto f = boss_feature_of(body, r0, h0);
|
||||
auto r = edit_feature(body, f, FeatureEditParams{0.0, 6.0, 0.0}); // h 4→6
|
||||
ASSERT_TRUE(r.success) << (r.errors.empty() ? "" : r.errors[0]);
|
||||
EXPECT_GE(validate(r.body).watertightness, 0.999);
|
||||
const double expected = analytic_plate_boss_volume(r0, 6.0);
|
||||
const double rel = std::abs(volume(r.body) - expected) / expected;
|
||||
EXPECT_LT(rel, 1e-6) << "rel=" << rel;
|
||||
EXPECT_EQ(r.body.num_faces(), 11u);
|
||||
EXPECT_NEAR(analytic_boss_radius(r.body, 5.0, 11.0), r0, 1e-9);
|
||||
}
|
||||
|
||||
TEST(FeatureEditTest, CopyBoss_Analytic_VolumeRel1e6) {
|
||||
// 20×10×10 板 + 凸台 r3 h4 → 复制到 x=6:体积 = 2000 + 2πr²h(rel ≤ 1e-6)
|
||||
auto body = box_with_boss(3.0, 4.0, {0, 0, 7}, 20.0);
|
||||
ASSERT_TRUE(body.is_valid());
|
||||
auto r = copy_feature(body, boss_feature(3.0, 4.0, {0, 0, 7}),
|
||||
Vector3D(6, 0, 0));
|
||||
ASSERT_TRUE(r.success) << (r.errors.empty() ? "" : r.errors[0]);
|
||||
ASSERT_TRUE(r.body.is_valid());
|
||||
EXPECT_GE(validate(r.body).watertightness, 0.999);
|
||||
const double expected = 2000.0 + 2.0 * M_PI * 9.0 * 4.0;
|
||||
const double rel = std::abs(volume(r.body) - expected) / expected;
|
||||
EXPECT_LT(rel, 1e-6) << "two-boss volume rel=" << rel;
|
||||
EXPECT_EQ(r.body.num_faces(), 16u); // 板 6 + 2×(4 patch + 1 顶盖)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user