From f5aeeeb4246305d3c9db20149fdb77f494c47c19 Mon Sep 17 00:00:00 2001 From: hm-thinkbook14p Date: Mon, 16 Nov 2026 03:05:27 +0800 Subject: [PATCH] fix(curves): initialize plane normal before coplanarity test in is_circle_section is_circle_section used the caller's uninitialized Vector3D normal in the coplanarity sampling loop before assigning it at the end; suffix tests rewriting the stack flipped rational-circle sections to the generic resampling path, giving loft weight sum 26.414213562 instead of 23.485281374 and offset fit residuals 4.3e-2. Assign plane_normal before the loop and hand it back as the output. Regression: MeasureTest.LoftOfRationalCirclesStaysRational (rational Coons prefix + 3-circle loft; weight sum and v-boundary tolerance 1e-12). test_measure 160/160; test_generative_surfaces 33/33; test_curve_tools 52/52; test_brep 96/96; PR gate 918/918. --- docs/planning/remaining-gaps-2026-11-14.md | 37 ++++++++++-- ...圆截面检测修正与loft边界精确自验证报告.md | 1 + src/curves/generative_surfaces.cpp | 10 +++- tests/brep/test_measure.cpp | 56 +++++++++++++++++++ 4 files changed, 98 insertions(+), 6 deletions(-) diff --git a/docs/planning/remaining-gaps-2026-11-14.md b/docs/planning/remaining-gaps-2026-11-14.md index 10aae581..0fc86905 100644 --- a/docs/planning/remaining-gaps-2026-11-14.md +++ b/docs/planning/remaining-gaps-2026-11-14.md @@ -14,6 +14,7 @@ VDE-1346 薄板/双孔板/碎片板 中面+壳面积一致 VDE-1347 薄板/双孔板 tetra 闭合、体积校验通过 VDE-1276 真实语料 display 缝 open_edge 2412→0(plate)/ 576→0(PWB body2,2026-11-15) analytic_measure 逐面并行 PWB body2 body_volume 32.8 s→7.8 s(数值不变,2026-11-15) +test_measure 顺序 flake 有理 loft 权重退化(未初始化法向 UB)修复 160/160(2026-11-15) ~~~ ## 未关闭 @@ -78,8 +79,8 @@ test_fea_mesh 68/68(去掉 CorpusProbe.BatchSweep) test_vde1346_cae_preprocess 10/10 test_display_mesh 16/16 test_vde1239_display_quality 13/13 -test_measure 158/159(含 AnalyticMeasureParallel.MatchesSerialBitwise; - MeasureTest.OffsetSurfaceLoftIsHighAccuracy 全量顺序 flake) +test_measure 160/160(含 AnalyticMeasureParallel.MatchesSerialBitwise、 + LoftOfRationalCirclesStaysRational) PR gate 868/868(2026-11-15 实测 167.45 s;并行度量后复跑 159.19 s) ~~~ @@ -146,8 +147,36 @@ tests/brep/test_real_corpus_feedback.cpp 回归:`tests/brep/test_real_corpus_feedback.cpp` 的 `HostExportedSplineBodyDisplayMeshKeepsFaceTags`/`HostExportedStepRoundTripImports` -覆盖该体导入;`test_measure` 158 例(`MeasureTest.OffsetSurfaceLoftIsHighAccuracy` -全量跑既有顺序相关 flake,单跑 PASS,与本改动无关)。 +覆盖该体导入;`test_measure` 160 例(含 `LoftOfRationalCirclesStaysRational` 锁定理有 +loft 权重,替换此前标注的 `OffsetSurfaceLoftIsHighAccuracy` 顺序相关 flake)。 + + +## 关闭记录:test_measure 顺序相关 flake(2026-11-15) + +现象:`test_measure` 全量跑 158/159,`MeasureTest.OffsetSurfaceLoftIsHighAccuracy` +残差 4.35e-2(期望 1e-5);单跑 PASS。二分定位到 +`MeasureTest.CoonsPatchRationalBoundariesAreGeometricallyExact` 先跑即触发。 + +根因(UB):`src/curves/generative_surfaces.cpp` 的 `is_circle_section` 把法向当纯 +输出参数,却在赋值前用 `d.dot(normal)` 做共面判定 → 读取调用方未初始化的 +`Vector3D normal`(call site 是 `align_closed_section_starts` 的局部变量)。后缀用例 +改变栈内容后判定翻转:有理圆被判成非共面 → 落入 `rotate_generic_closed`(采样点直接 +当控制点的非有理折线)→ `loft_surface` 权重和 23.485281374→26.414213562,v 边界不再 +是原圆;offset 拟合残差由 2e-6 恶化到 4.3e-2。 + +~~~text +修复:先算 const Vector3D plane_normal = b0.cross(c0).normalized(); + 共面采样判定改用 plane_normal;末尾 normal = plane_normal; + +验证(A/B 同源码对比) + 修复前:test_measure 158/160;Loft 与新增 LoftOfRationalCirclesStaysRational 均 FAIL + 新增用例单跑即 FAIL,wsum=26.414213562373096(非有理退化的指纹) + 修复后:test_measure 160/160(62.7 s,含 AnalyticMeasureParallel 与新增用例) +~~~ + +回归:tests/brep/test_measure.cpp 的 `MeasureTest.LoftOfRationalCirclesStaysRational` +(先构造有理边界 Coons 曲面复现栈状态,再断言 loft 权重和 23.485281374 及 v=0/v=1 +边界与输入圆偏差 ≤1e-12)。 ## 整圆/圆弧统一采样原型(2026-11-14) diff --git a/docs/reference/340-VDE1.0.282-有理圆截面检测修正与loft边界精确自验证报告.md b/docs/reference/340-VDE1.0.282-有理圆截面检测修正与loft边界精确自验证报告.md index b75daeda..8d45fb61 100644 --- a/docs/reference/340-VDE1.0.282-有理圆截面检测修正与loft边界精确自验证报告.md +++ b/docs/reference/340-VDE1.0.282-有理圆截面检测修正与loft边界精确自验证报告.md @@ -87,3 +87,4 @@ | 日期 | 版本 | 变更 | |---|---|---| | 2026-11-09 | 1.0.282 | is_circle_section 采样验证 + loft 权重行 → 边界偏差 0;注册 8,653 | +| 2026-11-15 | — | 后续修复:§修复 1 的共面判定 `|d·n|` 在 `normal` 赋值前执行(读取调用方未初始化 `Vector3D`,UB;后缀用例可翻转判定 → 有理圆退化成非有理折线)。改为先算 `plane_normal = b0.cross(c0).normalized()` 再做采样判定;回归 `test_measure.cpp` 的 `MeasureTest.LoftOfRationalCirclesStaysRational` | diff --git a/src/curves/generative_surfaces.cpp b/src/curves/generative_surfaces.cpp index 13c3a4f3..4d247b6e 100644 --- a/src/curves/generative_surfaces.cpp +++ b/src/curves/generative_surfaces.cpp @@ -106,6 +106,12 @@ bool is_circle_section(const NurbsCurve& c, Point3D& center, double& radius, double beta = b0.squaredNorm() * c0.dot(c0 - b0) / d0; center = Point3D(a + b0 * alpha + c0 * beta); radius = (Vector3D(cps[0]) - Vector3D(center)).norm(); + // 平面法向必须先算出来:采样验证要检查点是否共面。旧实现把 normal 当作 + // 纯输出参数,却在赋值之前用 d.dot(normal) 做共面判定 → 读取调用方未初始化 + // 的 Vector3D(UB)。后缀测试栈内容不同时判定翻转:有理圆被判成非共面 → + // align 落入 rotate_generic_closed(重采样成非有理折线)→ loft 权重/控制点 + // 失真,offset 拟合残差从 2e-6 恶化到 4.3e-2(test_measure 的顺序相关 flake)。 + const Vector3D plane_normal = b0.cross(c0).normalized(); // VDE-1479:有理圆的控制点并不都落在圆上(奇数号控制点在切线交点, // 半径 = R/cos45°),旧检查会让每个有理圆都判失败 → align 走通用重排 // (重采样成非有理折线),loft 边界失真(实测 v=1 偏差 0.293)。 @@ -119,11 +125,11 @@ bool is_circle_section(const NurbsCurve& c, Point3D& center, double& radius, const Vector3D d = Vector3D(q) - Vector3D(center); if (std::abs(d.norm() - radius) > 1e-9 * std::max(1.0, radius)) return false; - if (std::abs(d.dot(normal)) > 1e-9 * std::max(1.0, radius)) + if (std::abs(d.dot(plane_normal)) > 1e-9 * std::max(1.0, radius)) return false; } } - normal = b0.cross(c0).normalized(); + normal = plane_normal; u_axis = (Vector3D(cps[0]) - Vector3D(center)).normalized(); // 在 NurbsCurve::arc 的参考系中计算起点角度 Vector3D uu, vv; diff --git a/tests/brep/test_measure.cpp b/tests/brep/test_measure.cpp index 12ae3246..48ec8fba 100644 --- a/tests/brep/test_measure.cpp +++ b/tests/brep/test_measure.cpp @@ -1798,6 +1798,62 @@ TEST(MeasureTest, OffsetSurfaceLoftIsHighAccuracy) { } } +// VDE-1514 回归:**有理圆截面的 loft 必须保持有理表示**。align_closed_section_starts +// 的共面判定曾读取调用方未初始化的法向(UB):后缀用例把栈内容改掉后,有理圆被判成 +// 非共面 → 落入 rotate_generic_closed(采样点直接当控制点的非有理折线)→ loft 权重和 +// 从 23.485281374 变成 26.414213562,v 边界不再是原圆;offset 残差由 2e-6 恶化到 +// 4.3e-2,表现为 test_measure 全量跑时 MeasureTest.OffsetSurfaceLoftIsHighAccuracy +// 的顺序相关 flake。此用例先构造有理 Coons 面(与触发 flake 的栈状态一致),再断言 +// 权重模式与 v 边界几何精确。 +TEST(MeasureTest, LoftOfRationalCirclesStaysRational) { + const double pi = 3.14159265358979323846; + { + // 先走一遍有理边界曲面运算,使后续 align 的栈槽带上有理运算残值。 + auto cu0 = vde::curves::NurbsCurve::arc(Point3D(1, 0, 0), 1.0, pi, 2.0 * pi, + Vector3D::UnitZ()); + auto cu1 = vde::curves::NurbsCurve::arc( + Point3D(1, 2, 0), 1.0, pi, 2.0 * pi, + Vector3D(-Vector3D::UnitZ())); + auto cv0 = vde::curves::NurbsCurve::arc( + Point3D(0, 1, 0), 1.0, -pi / 2.0, pi / 2.0, Vector3D::UnitZ()); + auto cv1 = vde::curves::NurbsCurve::arc( + Point3D(2, 1, 0), 1.0, -pi / 2.0, pi / 2.0, + Vector3D(-Vector3D::UnitZ())); + auto patch = vde::curves::coons_patch(cu0, cu1, cv0, cv1); + ASSERT_FALSE(patch.control_points().empty()); + (void)patch.evaluate(0.3, 0.7); + } + const std::vector secs = { + vde::curves::NurbsCurve::circle(Point3D(0, 0, 0), 1.0), + vde::curves::NurbsCurve::circle(Point3D(0, 0, 2), 2.0), + vde::curves::NurbsCurve::circle(Point3D(0, 0, 4), 1.5)}; + auto s = vde::curves::loft_surface(secs); + ASSERT_FALSE(s.control_points().empty()); + // 有理权重保留:每条截面圆权重和 = 5 + 4·(√2/2),3 个截面 → 23.485281374… + double wsum = 0.0; + for (const auto& row : s.weights()) + for (double w : row) wsum += w; + EXPECT_NEAR(wsum, 3.0 * (5.0 + 4.0 * std::sqrt(2.0) / 2.0), 1e-9) + << "wsum=" << wsum; + const auto ku = s.knots_u(); + const auto kv = s.knots_v(); + const double ku0 = ku[s.degree_u()], ku1 = ku[ku.size() - 1 - s.degree_u()]; + const double kv0 = kv[s.degree_v()], kv1 = kv[kv.size() - 1 - s.degree_v()]; + auto boundary_dev = [&](const vde::curves::NurbsCurve& c, double vfixed) { + const auto dom = c.domain(); + double maxd = 0.0; + for (int i = 0; i <= 64; ++i) { + const double t = i / 64.0; + const Point3D p = c.evaluate(dom.first + (dom.second - dom.first) * t); + const Point3D q = s.evaluate(ku0 + (ku1 - ku0) * t, vfixed); + maxd = std::max(maxd, (p - q).norm()); + } + return maxd; + }; + EXPECT_LE(boundary_dev(secs[0], kv0), 1e-12); + EXPECT_LE(boundary_dev(secs[2], kv1), 1e-12); +} + // VDE-1513:**斜线绕轴旋转**得到的锥面(u=母线 / v=旋转,degree_u==1)偏置 // 解析精确。旧实现只接受 degree_v==1 的锥面表示 → 该形式回退通用偏移 // (实测距离 [0.0849,0.100])。修复:转置后重试(−distance),结果再转置回。