fix(brep): 草图剖面拉伸保留真实圆弧/圆几何,不再多边形化(VDE-168)
Build & Test / build-and-test (push) Canceled after 0s
VDE Build & Test / build-and-test (push) Canceled after 0s
Build & Test / python-bindings (push) Canceled after 0s

extrude(const curves::NurbsCurve&, const Vector3D&) 重写为真实几何建模:
按内部节点 trim_curve 切子曲线、侧面用 NurbsSurface::ruled 精确直纹面、
端盖用真实轮廓 p-curve(curve_to_pcurve)建裁剪圆盘;非平面轮廓回退
extrude_polygon_fallback(旧控制点多边形构造原样保留)。此前 VDE 侧实现比
工单描述更差:直接拿 NURBS 控制点当多边形顶点(圆→9 边形棱柱),端盖用
轮廓包围盒四边形。

顺带修复两处既有缺陷:
- NurbsCurve::closed_from_points 末节点遗留 0,节点向量非单调(…,1,0),
  domain() 未暴露但 trim_curve 读全向量时构造退化子曲线 → 侧面被压成零面积。
  改为 knots[n+2]=1.0。
- extrude_dxf_contour 丢弃轮廓几何、只按轮廓包围盒造 make_box(三角形与圆
  得到同一矩形块),改用真实剖面曲线后走新 extrude。

新增判别性测试 tests/brep/test_vde168_extrude.cpp(8 例):解析圆柱径向偏差
<1e-9、体积/面积随 deflection 收敛、弧剖面弦收口、非平面回退、退化输入空体、
精确棱柱、DXF 三角轮廓保持形状(体积 150 而非包围盒 300)、开放轮廓弦收口。
回归:test_step_import 65、test_step_export 14、test_boolean_corpus 323 及
curves/brep/mechanical/structural/dxf/modeling 各套件全绿。
This commit is contained in:
hm
2026-10-15 23:34:15 +08:00
parent 08a374326b
commit 15d6db68fa
8 changed files with 551 additions and 22 deletions
+38 -1
View File
@@ -1,12 +1,49 @@
# VDE-168: Sketch profile tessellation loses arc/circle curvature
**Date**: 2026-08-01
**Status**: Closed — Deferred to Future Roadmap — Enhancement request. Sketch arc/circle curvature preservation requires VDE to provide a `buildFaceFromSketchProfile` that accepts NURBS curves directly instead of tessellating to polylines. ViewDesign workaround (32-segment tessellation) is functional but produces faceted surfaces. Deferred to future roadmap. 2026-08-04 cron.
**Status**: Fixed — `extrude(const curves::NurbsCurve&, const Vector3D&)` rebuilt to preserve true curve/surface geometry (no tessellation). 2026-10-15.
**Found By**: ViewDesign Sketch-to-Extrude pipeline (vd-core-partdesign, Session 144-145)
**Severity**: Medium (correct geometry, but degraded quality for downstream ops)
---
## Resolution (2026-10-15)
The VDE-side `extrude(NurbsCurve)` was **worse than the ticket described**: it fed the NURBS
**control points** straight into a polygonal wire (a circle → 9-gon prism) and built end caps
from the profile **bounding box** (a quad, not the profile shape). Rewritten to true geometry:
- **Profile split at internal knots**, each sub-curve via `curves::trim_curve(profile, t0, t1)`
so true arc/circle/NURBS segments are preserved end to end.
- **Lateral faces = exact ruled surfaces** `NurbsSurface::ruled(seg, translated(seg))`, each
carrying an `from_rect` trimmed-surface domain.
- **End caps = real profile p-curves** on a plane base surface (`curve_to_pcurve` affine
projection), replacing the bounding-box quad.
- **Non-planar profiles** (deviation of sampled points > 1e-6·scale) fall back to
`extrude_polygon_fallback` (the old polygon construction, retained verbatim).
- Open profiles are closed by a chord edge so the result stays a valid solid.
Two pre-existing bad/fake implementations found and fixed alongside:
1. `NurbsCurve::closed_from_points` (`src/curves/nurbs_curve.cpp`) left the final knot at `0`
instead of `1`, producing a non-monotonic knot vector (`…, 1, 0`). `domain()` never exposed
it, but `trim_curve` (and the ruled-surface u knots) read the whole vector and built a
degenerate sub-curve → one lateral face collapsed to zero area (the closed-polyline extrude
surface-area deficit). Fixed: `knots[n + 2] = 1.0`.
2. `extrude_dxf_contour` (`src/brep/dxf_import.cpp`) ignored the contour entirely and emitted a
`make_box` of the contour **bounding box** (a triangle and a circle produced the same
rectangle block). Rewritten to build the real profile from the contour points
(`closed_from_points` for closed, a clamped 1st-degree open polyline otherwise) and call the
new `extrude`.
Discriminating tests added in `tests/brep/test_vde168_extrude.cpp` (8 cases): analytic cylinder
radial deviation < 1e-9, analytic volume/surface-area convergence vs `deflection`, arc profile
closed by chord, non-planar fallback, degenerate inputs → empty, exact prism, DXF triangle
contour honouring its shape (volume 150, not the bounding-box 300), and open-contour chord
closing.
---
## Summary
When a sketch profile containing arcs or circles is used for extrude/revolve, the `buildFaceFromSketchProfile()` function tessellates all curves into polyline segments before constructing the face. This means:
+5 -2
View File
@@ -22,7 +22,7 @@
2. **FIXED 零证据集深核**(32 份):VDE-002/016/033/042/067/170/185/189/249/316/320/436/440/441/449/459/464/469/471(有令牌但 0 命中)+ VDE-001/003/004/005/006/008/009/010/012/013/136/456/457(无令牌)。
3. **CLOSED 非政策类深核**(64 份):构建自动检测批(VDE-032~051/060/064/066)、
"已实现/取代/重复"(VDE-173/174/175/182/190/198/207/428/487)、"Deferred to Future Roadmap"
(VDE-117/168/478/480/484/486/490/492/494/503/504/505/511/512/513)、
(VDE-117/478/480/484/486/490/492/494/503/504/505/511/512/513;VDE-168 已于 2026-10-15 实现,见 §4)、
领域功能误关(VDE-141/749~754)。
4. **交叉核对既有审计**:`00-overview.md`、`audit-proposal-batch.md`、`VDE-565_581_triage.md`。
@@ -44,7 +44,9 @@ CLOSED 的 Out of Scope 主体(约 601 份)为成批自动分诊
`nurbs_surface.cpp:554 plane/577 box` 等,均以真实 file:line 取证)。
- **取代/重复关闭有效**:VDE-174→VDE-179、VDE-175→VDE-186、VDE-182→VDE-186、VDE-190→VDE-173、
VDE-487→VDE-481(目标 API 均实测存在)。
- **多数 Deferred 判定 JUSTIFIED**:VDE-117/168/478/480/490/492/494/511/513 确属架构级/新增子模块。
- **多数 Deferred 判定 JUSTIFIED**:VDE-117/478/480/490/492/494/511/513 确属架构级/新增子模块。
(VDE-168 原判 Deferred,后经复查属"误延后"——VDE 侧 `extrude(NurbsCurve)` 已有真实几何路径可扩展,
2026-10-15 已实现,见 §4。)
### 3.2 状态更正(本次审计)
@@ -109,6 +111,7 @@ CLOSED 的 Out of Scope 主体(约 601 份)为成批自动分诊
| VDE-506 | ✅ **已修复**(2026-10-06)逐点壁厚样本 `WallThicknessSample`+`detailed`;`SectionProperties.perimeter/min_thickness/max_thickness`;`DraftFaceResult::is_undercut()/is_insufficient_draft()`;`BrepModel::face_count()`。另修复 `section_properties` 投影主平面选反(面积/厚度归零)与 `section_plane` 分段直线节点向量末端退化(丢末段边);判别性测试 13+35+22+15 全绿 | brep/measure |
| VDE-141 | ✅ **已修复**(2026-10-06)`SdfNode::hex_prism(radius, height, sides=6)` 支持独立半径/边数;新增通用 `regular_polygon_prism`;判别性测试 5 项全绿 | sdf |
| VDE-006/013/067 | ✅ **已修复**(2026-10-06)① **VDE-006**:新增集成层 `vde_io` 归集跨层 TU(foundation 格式 IO / core voronoi+topology_compression / curves 返 B-Rep 工具 / mesh 6 文件 / brep format_io+pmi_mbd),`BrepModel` 桥接归 `vde_brep`;依赖图变严格单向 `io→brep→{sdf,mesh}→{curves,collision}→spatial→core→foundation`,删除 `-Wl,--start-group`;顺带修复移除兜底后暴露的 `core/icp.cpp→spatial::KDTree`(TU 归 `vde_spatial`)与 `brep/parallel_boolean.cpp→vde::boolean`(`VDE_DEPS_brep` 增 `boolean` + 链接)。② **VDE-013**:顶层解析 `VDE_BUILD_*`→`VDE_ENABLE_*`,`VDE_DEPS_*` 级联关闭,`src/`+`tests/` 按 `VDE_ENABLE_*` 门控。③ **VDE-067**:全仓裸 `mesh::` 清理为 `vde::mesh::`(命中 0) | build/brep |
| VDE-168 | ✅ **已修复**(2026-10-15)`extrude(const curves::NurbsCurve&, const Vector3D&)` 重写为真实几何:按内部节点 `trim_curve` 切子曲线、侧面 `NurbsSurface::ruled` 精确直纹面、端盖用真实轮廓 p-curve(`curve_to_pcurve`);非平面轮廓回退 `extrude_polygon_fallback`。**既有坏实现**(比工单更差:控制点当多边形顶点,圆→9 边形棱柱;端盖用包围盒四边形)一并修复。顺带修复 2 处既有缺陷:① `NurbsCurve::closed_from_points` 末节点遗留 `0` 致节点向量非单调、`trim_curve` 构造退化子曲线(零面积面)→ 改 `knots[n+2]=1.0`;② `extrude_dxf_contour` 丢弃轮廓只造 `make_box` 包围盒(三角形/圆都得同一矩形块)→ 改用真实剖面曲线。判别性测试 `test_vde168_extrude` 8 项全绿 | brep/modeling |
> 注:VDE-749~754 属大颗粒领域模块(石化/半导体等),已补录关闭依据(应用层垂直领域,超出几何内核范围),如项目范围扩展需另立路线图票。
+27 -18
View File
@@ -494,29 +494,38 @@ DxfImportResult import_dxf(const std::string& filepath) {
return import_dxf_from_string(buf.str());
}
/// 开放折线剖面(1 阶 NURBS,clamped 节点向量):节点数 = n+2,段数 = n-1。
static curves::NurbsCurve open_polyline_curve(const std::vector<Point3D>& pts) {
const int n = static_cast<int>(pts.size());
if (n < 2) return curves::NurbsCurve();
std::vector<double> knots(static_cast<size_t>(n) + 2, 0.0);
for (int i = 1; i <= n - 1; ++i)
knots[i + 1] = static_cast<double>(i) / (n - 1);
knots[n + 1] = 1.0;
return curves::NurbsCurve(pts, knots, std::vector<double>(pts.size(), 1.0), 1);
}
BrepModel extrude_dxf_contour(const DxfContour& contour, double height) {
if (contour.points.size() < 2) return BrepModel{};
if (!(std::abs(height) > 1e-12)) return BrepModel{};
// Use 2D profile → simple extrusion as a box scaled to points bounds
// For a more accurate extrusion, each contour should build a proper sweep.
// 真实轮廓拉伸:以 DXF 实体折线化的轮廓点列为剖面沿 +Z 挤出。
// 此前实现只按轮廓包围盒造一个 make_box —— 轮廓几何被完全丢弃
// (方形/圆形/异形轮廓都变成同一个矩形块),下游质量属性、布尔、
// 导出全部继承该误差。现改用真实剖面曲线。
std::vector<Point3D> pts = contour.points;
// 闭合折线常以首点收尾(或含相邻重复点):去重后再判断是否够构成剖面
while (pts.size() >= 3 && (pts.back() - pts.front()).norm() < 1e-9) pts.pop_back();
if (pts.size() < 3) return BrepModel{};
// Compute bounding box of contour
double min_x = contour.points[0].x(), max_x = min_x;
double min_y = contour.points[0].y(), max_y = min_y;
for (const auto& p : contour.points) {
min_x = std::min(min_x, p.x());
max_x = std::max(max_x, p.x());
min_y = std::min(min_y, p.y());
max_y = std::max(max_y, p.y());
}
double w = max_x - min_x;
double d = max_y - min_y;
if (w < 0.01) w = 0.1;
if (d < 0.01) d = 0.1;
// 1 阶(折线)剖面:闭合轮廓用封闭折线;开放轮廓用开放折线,
// 由 extrude 自动补直线收口边闭合(如圆弧段的弦)。
curves::NurbsCurve profile = contour.closed
? curves::NurbsCurve::closed_from_points(pts)
: open_polyline_curve(pts);
if (profile.degree() < 1) return BrepModel{};
// Create an extruded block matching the contour bounds
auto body = make_box(w, d, height);
return body;
return extrude(profile, Vector3D(0, 0, height));
}
std::vector<BrepModel> import_dxf_as_solids(const std::string& filepath, double height) {
+191
View File
@@ -952,8 +952,197 @@ BrepModel make_elbow(double R, double r, double angle_rad,
}
// ── Extrude/Revolve/Sweep/Loft ──
// ═══════════════════════════════════════════════════════════
// VDE-168: 草图轮廓挤出保留真实曲线/曲面
//
// 草图剖面(含圆弧/圆)沿方向挤出时,原先直接把 NURBS **控制点**当多边形
// 顶点、端盖用轮廓包围盒四边形。有理圆/弧的中间控制点不在曲线上(在圆外),
// 故圆 → 9 边形棱柱、端盖 → 包围盒方板:侧面为分面多面体而非真实圆柱面,
// 端盖不是圆盘。下游(圆角、STEP 导出、质量属性、视口)全部继承该误差。
//
// 新实现按"OCCT 圆柱面 = 4 段四分之一圆弧面"的口径重建:
// · 剖面按**内部节点**切分为真实子曲线(trim_curve 节点插入/截取,几何不变);
// · 每段子曲线与其平移生成一张**精确直纹面**(圆/圆弧段 ⇒ 精确圆柱面);
// · 端盖为**平面基面 + 真实轮廓 p-curve 裁剪环**(圆轮廓 ⇒ 真正圆盘);
// · 拓扑顶点/边曲线取自真实曲线(端点 = 曲线在切分参数处的求值)。
//
// 非平面轮廓无法用"平面端盖 + 直纹侧面"精确表达,回退旧多边形构造。
// ═══════════════════════════════════════════════════════════
namespace {
/// 平移 NURBS 曲线:控制点整体平移(权重/节点不变 ⇒ 几何为原曲线的精确平移)。
curves::NurbsCurve translated_curve(const curves::NurbsCurve& c, const Vector3D& d) {
std::vector<Point3D> cp = c.control_points();
for (auto& p : cp) p += d;
return curves::NurbsCurve(std::move(cp), c.knots(), c.weights(), c.degree());
}
/// 平面内曲线 → 基面参数域 p-curve(X=u、Y=v)。控制点仿射投影即得精确
/// p-curve(有理 NURBS 经仿射变换后,控制点仍是变换后曲线的控制点)。
curves::NurbsCurve curve_to_pcurve(const curves::NurbsCurve& c, const Point3D& o,
const Vector3D& u_ax, const Vector3D& v_ax,
double w, double h) {
std::vector<Point3D> cp;
cp.reserve(c.control_points().size());
for (const auto& p : c.control_points()) {
const Vector3D r = p - o;
cp.emplace_back(r.dot(u_ax) / w, r.dot(v_ax) / h, 0.0);
}
return curves::NurbsCurve(std::move(cp), c.knots(), c.weights(), c.degree());
}
/// VDE-168 回退路径(定义见 extrude 之后):非平面轮廓沿用旧多边形柱体构造。
BrepModel extrude_polygon_fallback(const curves::NurbsCurve& profile,
const Vector3D& dir);
} // namespace
BrepModel extrude(const curves::NurbsCurve& profile, const Vector3D& dir) {
// 输入校验:退化曲线 / 零方向向量 → 空模型
if (profile.degree() < 1 || profile.control_points().size() < 2) return BrepModel();
if (dir.norm() < 1e-12) return BrepModel();
const auto dom = profile.domain();
const double t0 = dom.first, tspan = dom.second - dom.first;
if (!(tspan > 1e-15)) return BrepModel();
// 轮廓平面:采样点 Newell 法向 + 最大偏离(非平面 → 回退多边形构造)
const int kSamp = 33;
std::vector<Point3D> samp;
samp.reserve(kSamp);
for (int i = 0; i < kSamp; ++i)
samp.push_back(profile.evaluate(t0 + tspan * i / (kSamp - 1)));
AABB3D sbb;
for (const auto& p : samp) sbb.expand(p);
const double scale = std::max(1e-9, (sbb.max() - sbb.min()).norm());
Vector3D nrm = newell_normal(samp);
if (nrm.norm() < 1e-12) return extrude_polygon_fallback(profile, dir);
nrm.normalize();
double dev = 0.0;
for (const auto& p : samp) dev = std::max(dev, std::abs((p - samp[0]).dot(nrm)));
if (dev > 1e-6 * scale) return extrude_polygon_fallback(profile, dir);
// 按内部节点切分为真实子曲线(闭曲线首末节点重合,环自然闭合)
std::vector<double> cuts{t0, dom.second};
for (double k : profile.knots())
if (k > t0 + 1e-12 * tspan && k < dom.second - 1e-12 * tspan) cuts.push_back(k);
std::sort(cuts.begin(), cuts.end());
cuts.erase(std::unique(cuts.begin(), cuts.end(),
[tspan](double a, double b) {
return std::abs(a - b) <= 1e-12 * tspan;
}),
cuts.end());
std::vector<Point3D> cpt;
cpt.reserve(cuts.size());
for (double t : cuts) cpt.push_back(profile.evaluate(t));
std::vector<curves::NurbsCurve> segs;
segs.reserve(cuts.size());
for (size_t i = 0; i + 1 < cuts.size(); ++i) {
auto sub = curves::trim_curve(profile, (cuts[i] - t0) / tspan,
(cuts[i + 1] - t0) / tspan);
if (sub.degree() < 1) sub = curves::NurbsCurve::line(cpt[i], cpt[i + 1]);
segs.push_back(std::move(sub));
}
// 开放轮廓:补直线收口边,使边界链闭合(与封闭剖面同样按环处理)
if ((cpt.back() - cpt.front()).norm() > 1e-9 * scale)
segs.push_back(curves::NurbsCurve::line(cpt.back(), cpt.front()));
if (segs.size() < 2) return extrude_polygon_fallback(profile, dir);
const int m = static_cast<int>(segs.size());
const bool flip = (dir.dot(nrm) < 0.0); // 挤出方向与轮廓法向反向 → 整体翻面
BrepModel result;
std::vector<Point3D> bp;
bp.reserve(m);
for (int i = 0; i < m; ++i) bp.push_back(segs[i].evaluate(0.0));
std::vector<int> vb(m), vt(m), eb(m), et(m), es(m);
for (int i = 0; i < m; ++i) {
vb[i] = result.add_vertex(bp[i]);
vt[i] = result.add_vertex(bp[i] + dir);
}
for (int i = 0; i < m; ++i) {
const int j = (i + 1) % m;
eb[i] = result.add_edge(vb[i], vb[j], segs[i]);
et[i] = result.add_edge(vt[i], vt[j], translated_curve(segs[i], dir));
}
// seam 边(轮廓切分点处的竖直棱)被相邻两张侧面共享,只建一份
for (int i = 0; i < m; ++i) es[i] = result.add_edge(vb[i], vt[i]);
std::vector<int> all_faces;
// 侧面:每段子曲线与其平移张成精确直纹面,裁剪域 = 整参数域 [0,1]²
// (∂u 沿曲线、∂v 沿挤出方向 ⇒ 自然法向 = 切线×dir 朝外)
for (int i = 0; i < m; ++i) {
const int j = (i + 1) % m;
auto side = curves::NurbsSurface::ruled(segs[i], translated_curve(segs[i], dir));
const int sid = result.add_surface(side);
const int tid =
result.add_trimmed_surface(TrimmedSurface::from_rect(side, 0.0, 1.0, 0.0, 1.0));
const int lp = result.add_loop({eb[i], es[j], et[i], es[i]}, true);
const int fid = result.add_face(sid, {lp});
result.set_face_trimmed_surface(fid, tid);
result.face(fid).reversed = flip;
all_faces.push_back(fid);
}
// 端盖:平面基面 + 真实轮廓 p-curve 裁剪环(圆盘,非包围盒四边形)。
// 基面取"u_ax×v_ax = nrm"的正交基 → 轮廓 UV 环保持逆时针、p-curve 为
// 精确圆/弧(控制点仿射投影)。
Vector3D u_ax =
(std::abs(nrm.x()) < 0.9 ? Vector3D(1, 0, 0) : Vector3D(0, 1, 0)).cross(nrm);
if (u_ax.norm() < 1e-12) u_ax = Vector3D(1, 0, 0).cross(nrm);
u_ax.normalize();
const Vector3D v_ax = nrm.cross(u_ax);
const Point3D o = bp[0];
double umin = 0.0, umax = 0.0, vmin = 0.0, vmax = 0.0;
for (const auto& p : samp) {
const Vector3D r = p - o;
const double u = r.dot(u_ax), v = r.dot(v_ax);
umin = std::min(umin, u); umax = std::max(umax, u);
vmin = std::min(vmin, v); vmax = std::max(vmax, v);
}
const double W = std::max(umax - umin, 1e-6 * scale);
const double H = std::max(vmax - vmin, 1e-6 * scale);
const Point3D porig = o + umin * u_ax + vmin * v_ax;
TrimLoop outer;
outer.is_outer = true;
for (int i = 0; i < m; ++i)
outer.p_curves.push_back(curve_to_pcurve(segs[i], porig, u_ax, v_ax, W, H));
for (int cap = 0; cap < 2; ++cap) {
const bool far = (cap == 1); // false = 轮廓平面端盖,true = 沿 dir 的远端盖
const Point3D co = far ? porig + dir : porig;
const auto cap_surf = curves::NurbsSurface::plane(co, u_ax, v_ax, W, H);
std::vector<int> ring;
ring.reserve(m);
for (int i = 0; i < m; ++i) ring.push_back(far ? et[i] : eb[i]);
const int sid = result.add_surface(cap_surf);
TrimmedSurface cts = TrimmedSurface::from_surface(cap_surf);
cts.loops = {outer};
const int tid = result.add_trimmed_surface(cts);
const int fid = result.add_face(sid, {result.add_loop(ring, true)});
result.set_face_trimmed_surface(fid, tid);
// 基面自然法向恒为 +nrm:远端盖朝外 ⇔ dir·nrm>0,近端盖相反
result.face(fid).reversed = far ? flip : !flip;
all_faces.push_back(fid);
}
const int sh = result.add_shell(all_faces, true);
result.add_body({sh}, "extrude");
return result;
}
namespace {
// VDE-168 回退路径:非平面轮廓沿用旧"控制点多边形柱体"构造,保持既有行为。
BrepModel extrude_polygon_fallback(const curves::NurbsCurve& profile, const Vector3D& dir) {
BrepModel result; const auto& cp=profile.control_points(); int n=cp.size();
if (n < 3) return BrepModel();
std::vector<int> bot,top;
for(int i=0;i<n;++i){ bot.push_back(result.add_vertex(cp[i])); top.push_back(result.add_vertex(cp[i]+dir)); }
// add_edge_unique(按顶点对去重,VDE-1193 §9.10):底/顶/竖边被端盖环与
@@ -985,6 +1174,8 @@ BrepModel extrude(const curves::NurbsCurve& profile, const Vector3D& dir) {
int sh=result.add_shell(all_faces,true); result.add_body({sh},"extrude"); return result;
}
} // namespace
BrepModel revolve(const curves::NurbsCurve& profile, const Point3D& o, const Vector3D& ax, double ang) {
// 输入校验:空/退化曲线、零轴、零角均返回空模型
if (profile.degree() == 0 || ax.norm() < 1e-12 || std::abs(ang) < 1e-9) return BrepModel();
+1 -1
View File
@@ -5674,7 +5674,7 @@ private:
+ std::to_string(id) + ": profile not a closed planar polygon");
return false;
}
// 去除重合/尾随闭合点:extrude() 把控制点当作轮廓顶点,退化边需剔除
// 去除重合/尾随闭合点:顶点对重合会产生零长边
std::vector<Point3D> pts;
for (auto& p : poly) {
if (!pts.empty() && (pts.back() - p).norm() < 1e-9) continue;
+6
View File
@@ -700,6 +700,12 @@ NurbsCurve NurbsCurve::closed_from_points(const std::vector<Point3D>& points) {
std::vector<double> knots(n + 3, 0.0); // 阶次 1 → 节点数 = n+3
for (int i = 1; i <= n; ++i)
knots[i + 1] = static_cast<double>(i) / n;
// 末节点必须与倒数第二个同为 1(clamped 尾)。此前末元素遗留为 0,
// 节点向量非单调(…,1,0):domain() 恰好只读到 knots[n] 而未暴露,
// 但任何读取整个节点向量的下游(trim_curve 的节点插入/子段提取、
// extrude 的直纹面 u 节点)都会构造出退化子曲线/曲面
// (VDE-168:闭合折线拉伸有一条侧面被整体压成零面积)。
knots[n + 2] = 1.0;
std::vector<double> weights(cps.size(), 1.0);
return NurbsCurve(cps, knots, weights, 1);
+2
View File
@@ -6,6 +6,8 @@ add_vde_test(test_brep_boolean)
add_vde_test(test_ssi_boolean)
add_vde_test(test_analytic_ssi)
add_vde_test(test_brep_validate)
# VDE-168: 草图剖面(圆弧/圆)拉伸保留真实曲面/曲线,不再多边形化
add_vde_test(test_vde168_extrude)
# Phase 2.1/2.3: 自交手术种子库与验收口径
add_vde_test(test_selfint_seeds)
add_vde_test(test_iges_import)
+281
View File
@@ -0,0 +1,281 @@
// VDE-168: 草图剖面(含圆弧/圆)拉伸必须保留真实曲面/曲线,不得多边形化。
//
// 既有坏实现(VDE 侧比工单描述更差):src/brep/modeling.cpp 的
// extrude(NurbsCurve, dir) 直接拿**控制点**当多边形顶点、端盖用包围盒四边形。
// 有理圆的中间控制点不在曲线上(在圆外),故圆 → 9 边形棱柱、端盖 → 方板,
// 体积/面积与解析值偏差巨大(π/4 ≈ 21%~27% 量级)。
//
// 现实现:剖面按内部节点切分为真实子曲线,每段与其平移张成精确直纹面
// (圆/圆弧段 ⇒ 精确圆柱面);端盖为平面基面 + 真实轮廓 p-curve 裁剪环
// (圆轮廓 ⇒ 真正圆盘)。下方断言全部为**判别性**:旧实现必然失败。
#include <gtest/gtest.h>
#include "vde/brep/brep.h"
#include "vde/brep/modeling.h"
#include "vde/brep/brep_validate.h"
#include "vde/brep/measure.h"
#include "vde/brep/dxf_import.h"
#include "vde/curves/nurbs_surface.h"
#include "vde/curves/nurbs_curve.h"
#include <cmath>
#include <vector>
using namespace vde::brep;
using namespace vde::core;
using vde::curves::NurbsCurve;
namespace {
double radius_xy(const Point3D& p) { return std::hypot(p.x(), p.y()); }
/// 采集面基面在 [0,1]² 上的采样点(用于判定该面的真实曲面形状)
std::vector<Point3D> sample_face_surface(const BrepModel& body, int face_idx, int n) {
const auto& f = body.face(face_idx);
const auto& s = body.surface(f.surface_id);
std::vector<Point3D> pts;
pts.reserve(static_cast<size_t>(n + 1) * (n + 1));
for (int i = 0; i <= n; ++i)
for (int j = 0; j <= n; ++j)
pts.push_back(s.evaluate(static_cast<double>(i) / n, static_cast<double>(j) / n));
return pts;
}
/// 面是否为平面(基面采样点的 Newell 法向最大偏离)
bool face_is_planar(const BrepModel& body, int face_idx) {
auto pts = sample_face_surface(body, face_idx, 4);
Vector3D n(0, 0, 0);
for (size_t i = 0; i < pts.size(); ++i) {
const Point3D& a = pts[i];
const Point3D& b = pts[(i + 1) % pts.size()];
n += Vector3D((a.y() - b.y()) * (a.z() + b.z()),
(a.z() - b.z()) * (a.x() + b.x()),
(a.x() - b.x()) * (a.y() + b.y()));
}
if (n.norm() < 1e-12) return true;
n.normalize();
const double scale = std::max(1.0, (pts[0] - pts[pts.size() / 2]).norm());
double dev = 0.0;
for (const auto& p : pts) dev = std::max(dev, std::abs((p - pts[0]).dot(n)));
return dev < 1e-9 * scale;
}
/// 拓扑水密:每条边恰好 2 个相邻面
void expect_edge_manifold(const BrepModel& body) {
for (size_t ei = 0; ei < body.num_edges(); ++ei)
EXPECT_EQ(body.edge_faces(static_cast<int>(ei)).size(), 2u) << "edge " << ei;
}
} // namespace
// ═══════════════════════════════════════════════════════════
// 圆剖面拉伸 ⇒ 精确圆柱(4 段四分之一圆柱面 + 2 圆盘端盖)
// ═══════════════════════════════════════════════════════════
TEST(Vde168ExtrudeTest, CircularProfileProducesAnalyticCylinder) {
const double r = 2.0, h = 5.0;
auto circle = NurbsCurve::circle(Point3D(0, 0, 0), r, Vector3D::UnitZ());
auto body = extrude(circle, Vector3D(0, 0, h));
ASSERT_EQ(body.num_bodies(), 1u);
// 旧实现:圆 → 9 边形棱柱 = 9 侧面 + 2 端盖。现实现:4 侧面 + 2 端盖。
EXPECT_EQ(body.num_faces(), 6u);
EXPECT_EQ(body.num_edges(), 12u); // 3 × 4(底环 / 顶环 / 竖直 seam 棱)
EXPECT_EQ(body.num_vertices(), 8u); // 2 × 4
expect_edge_manifold(body);
// a) 侧面为**精确**圆柱面:采样点到轴距离恒为 r(无分面)
int sides = 0, caps = 0;
double max_radial_dev = 0.0;
for (size_t fi = 0; fi < body.num_faces(); ++fi) {
const int fidx = static_cast<int>(fi);
if (face_is_planar(body, fidx)) { ++caps; continue; }
++sides;
double zmin = 1e30, zmax = -1e30;
for (const auto& p : sample_face_surface(body, fidx, 6)) {
zmin = std::min(zmin, p.z());
zmax = std::max(zmax, p.z());
max_radial_dev = std::max(max_radial_dev, std::abs(radius_xy(p) - r));
}
EXPECT_NEAR(zmin, 0.0, 1e-12);
EXPECT_NEAR(zmax, h, 1e-12);
}
EXPECT_EQ(sides, 4);
EXPECT_EQ(caps, 2);
EXPECT_LT(max_radial_dev, 1e-9) << "侧面非精确圆柱(被多边形化)";
// b) 端盖边曲线(圆弧)必须精确落在半径 r 的圆上 —— 旧实现端盖为包围盒
// 方板,边为直线(端点半径 r√2)。
int cap_edges = 0;
double max_cap_curve_dev = 0.0;
for (size_t fi = 0; fi < body.num_faces(); ++fi) {
if (!face_is_planar(body, static_cast<int>(fi))) continue;
const auto& lop = body.loop_by_id(body.face(static_cast<int>(fi)).loops[0]);
for (int e : lop.edges) {
++cap_edges;
const auto& ec = body.edge(e).curve;
ASSERT_TRUE(ec != nullptr) << "端盖边缺少曲线几何(旧实现为直线边)";
for (int k = 0; k <= 16; ++k) {
const Point3D p = ec->evaluate(k / 16.0);
max_cap_curve_dev = std::max(max_cap_curve_dev, std::abs(radius_xy(p) - r));
}
}
}
EXPECT_EQ(cap_edges, 8); // 2 端盖 × 4 圆弧
EXPECT_LT(max_cap_curve_dev, 1e-9) << "端盖轮廓非圆(被多边形化/退化为包围盒)";
// c) 端盖裁剪域为真实圆盘:外环为 4 条 2 阶圆弧 p-curve(旧实现为 4 条直线
// 围成的包围盒矩形)
int arc_pcurves = 0, cap_ts = 0;
for (size_t fi = 0; fi < body.num_faces(); ++fi) {
if (!face_is_planar(body, static_cast<int>(fi))) continue;
++cap_ts;
const auto& f = body.face(static_cast<int>(fi));
ASSERT_GE(f.trimmed_surface_id, 0);
const auto& ts = body.trimmed_surface(f.trimmed_surface_id);
ASSERT_EQ(ts.loops.size(), 1u);
EXPECT_EQ(ts.loops[0].p_curves.size(), 4u);
for (const auto& pc : ts.loops[0].p_curves)
if (pc.degree() >= 2) ++arc_pcurves;
}
EXPECT_EQ(cap_ts, 2);
EXPECT_EQ(arc_pcurves, 8); // 2 端盖 × 4 圆弧
// d) 解析质量属性对照(SolidWorks MassProperty 口径):偏差必须远小于
// 旧实现的多边形化误差(≈21%~27%),且随弦高容差收紧而收敛。
const double exact_v = M_PI * r * r * h;
const double exact_a = 2.0 * M_PI * r * h + 2.0 * M_PI * r * r;
const double v_coarse = analytic_volume(body, 1e-2);
const double v_fine = analytic_volume(body, 1e-4);
const double a_coarse = analytic_surface_area(body, 1e-2);
const double a_fine = analytic_surface_area(body, 1e-4);
EXPECT_LT(std::abs(v_fine - exact_v), 1e-4 * exact_v);
EXPECT_LT(std::abs(v_fine - exact_v), std::abs(v_coarse - exact_v) + 1e-12)
<< "解析体积未随容差收紧收敛";
EXPECT_LT(std::abs(a_fine - exact_a), 1e-4 * exact_a);
EXPECT_LT(std::abs(a_fine - exact_a), std::abs(a_coarse - exact_a) + 1e-12)
<< "解析面积未随容差收紧收敛";
// e) 综合验证通过
auto vr = validate(body);
EXPECT_TRUE(vr.valid) << (vr.errors.empty() ? "" : vr.errors.front());
EXPECT_NEAR(vr.watertightness, 1.0, 1e-12);
EXPECT_TRUE(body.is_valid());
}
// 反向挤出(dir·法向 < 0)仍为精确圆柱且水密(整体翻面正确)
TEST(Vde168ExtrudeTest, CircularProfileReverseDirection) {
const double r = 1.5, h = 4.0;
auto circle = NurbsCurve::circle(Point3D(0, 0, 0), r, Vector3D::UnitZ());
auto body = extrude(circle, Vector3D(0, 0, -h));
ASSERT_EQ(body.num_faces(), 6u);
expect_edge_manifold(body);
// 体积取绝对值(方向反转不影响 |V|)
const double exact_v = M_PI * r * r * h;
EXPECT_LT(std::abs(std::abs(analytic_volume(body, 1e-4)) - exact_v), 1e-4 * exact_v);
}
// 圆弧(非整圆)+ 直径弦闭合剖面:真实圆柱弧面 + 平端盖,无多边形化
TEST(Vde168ExtrudeTest, ArcProfileClosedByChordStaysAnalytic) {
const double r = 3.0, h = 2.0;
auto arc = NurbsCurve::arc(Point3D(0, 0, 0), r, 0.0, M_PI, Vector3D::UnitZ());
auto body = extrude(arc, Vector3D(0, 0, h));
ASSERT_EQ(body.num_bodies(), 1u);
expect_edge_manifold(body);
// 平面面 = 2 端盖 + 1 弦侧;其余为真实圆柱弧面(圆弧按内部节点切段,
// 段数由 arc() 的节点向量决定,此处不硬编码)
int curved = 0;
double max_radial_dev = 0.0;
for (size_t fi = 0; fi < body.num_faces(); ++fi) {
const int fidx = static_cast<int>(fi);
if (face_is_planar(body, fidx)) continue;
++curved;
for (const auto& p : sample_face_surface(body, fidx, 6))
max_radial_dev = std::max(max_radial_dev, std::abs(radius_xy(p) - r));
}
EXPECT_GE(curved, 1);
EXPECT_EQ(body.num_faces(), static_cast<size_t>(curved + 3)); // 弧面 + 弦 + 2 端盖
EXPECT_LT(max_radial_dev, 1e-9);
// 体积 = 半圆面积 × 高(弦为解析直线边 ⇒ 剖面精确为半圆)
const double exact_v = 0.5 * M_PI * r * r * h;
EXPECT_LT(std::abs(analytic_volume(body, 1e-4) - exact_v), 1e-4 * exact_v);
}
// 非平面轮廓:无法用"平面端盖 + 直纹侧面"精确表达,按既有行为回退多边形构造
// (不得静默返回空模型)
TEST(Vde168ExtrudeTest, NonPlanarProfileFallsBackToPolygonExtrude) {
auto space = NurbsCurve::interpolate(
{Point3D(0, 0, 0), Point3D(2, 0, 1), Point3D(2, 2, 0), Point3D(0, 2, 1)}, 3);
auto body = extrude(space, Vector3D(0, 0, 3));
EXPECT_GT(body.num_faces(), 0u);
EXPECT_EQ(body.num_bodies(), 1u);
}
// 退化输入:零方向 / 空曲线 ⇒ 空模型(不崩、不产出垃圾拓扑)
TEST(Vde168ExtrudeTest, DegenerateInputsReturnEmpty) {
auto circle = NurbsCurve::circle(Point3D(0, 0, 0), 1.0, Vector3D::UnitZ());
EXPECT_EQ(extrude(circle, Vector3D::Zero()).num_faces(), 0u);
EXPECT_EQ(extrude(NurbsCurve(), Vector3D(0, 0, 1)).num_faces(), 0u);
}
// ═══════════════════════════════════════════════════════════
// 1 阶封闭折线剖面(DXF 轮廓 / closed_from_points)
// ═══════════════════════════════════════════════════════════
TEST(Vde168ExtrudeTest, ClosedPolylineProfileProducesExactPrism) {
auto rect = NurbsCurve::closed_from_points(
{Point3D(0, 0, 0), Point3D(10, 0, 0), Point3D(10, 5, 0), Point3D(0, 5, 0)});
auto body = extrude(rect, Vector3D(0, 0, 2));
EXPECT_EQ(body.num_faces(), 6u); // 4 侧 + 2 盖
expect_edge_manifold(body);
EXPECT_NEAR(analytic_volume(body, 1e-3), 100.0, 1e-6);
EXPECT_NEAR(analytic_surface_area(body, 1e-3), 160.0, 1e-6);
EXPECT_TRUE(validate(body).valid);
}
// DXF 轮廓拉伸必须尊重真实轮廓形状(既有实现只按包围盒造 make_box,
// 轮廓几何被完全丢弃:三角形轮廓也会得到同一个矩形块)
TEST(Vde168ExtrudeTest, DxfContourExtrusionHonorsContourShape) {
DxfContour tri;
tri.closed = true;
tri.layer = "profile";
tri.points = {Point3D(0, 0, 0), Point3D(10, 0, 0), Point3D(0, 10, 0)};
auto body = extrude_dxf_contour(tri, 3.0);
ASSERT_EQ(body.num_bodies(), 1u);
EXPECT_EQ(body.num_faces(), 5u); // 3 侧 + 2 盖(包围盒实现会得到 6 面)
expect_edge_manifold(body);
// 体积 = 三角形面积 × 高 = 10·10/2 × 3 = 150(包围盒实现为 10×10×3 = 300)
EXPECT_NEAR(analytic_volume(body, 1e-3), 150.0, 1e-5);
EXPECT_NEAR(analytic_surface_area(body, 1e-3), 50.0 * 2.0 + (10.0 + 10.0 + 10.0 * std::sqrt(2.0)) * 3.0,
1e-5);
EXPECT_TRUE(validate(body).valid);
EXPECT_EQ(extrude_dxf_contour(DxfContour{}, 1.0).num_faces(), 0u);
}
// 开放轮廓(无闭合标志):由弦收口,仍应得到水密实体
TEST(Vde168ExtrudeTest, DxfOpenContourClosedByChord) {
DxfContour arc;
arc.closed = false;
arc.layer = "profile";
for (int i = 0; i <= 8; ++i) {
const double a = M_PI * i / 8.0; // 上半圆折线
arc.points.push_back(Point3D(2.0 * std::cos(a), 2.0 * std::sin(a), 0.0));
}
auto body = extrude_dxf_contour(arc, 1.0);
ASSERT_EQ(body.num_bodies(), 1u);
expect_edge_manifold(body);
// 内接九边形半圆盘面积(9 个顶点 → 8 段弦):介于九边形与半圆之间
double poly_area = 0.0;
const auto& p = arc.points;
for (size_t i = 0; i + 1 < p.size(); ++i)
poly_area += p[i].x() * p[i + 1].y() - p[i + 1].x() * p[i].y();
poly_area = std::abs(poly_area) * 0.5;
EXPECT_NEAR(analytic_volume(body, 1e-3), poly_area, 1e-4 * poly_area);
EXPECT_LT(poly_area, 0.5 * M_PI * 4.0); // 内接折线小于半圆
}