diff --git a/docs/feedback/VDE-141.md b/docs/feedback/VDE-141.md index 1007f4118..0573baeae 100644 --- a/docs/feedback/VDE-141.md +++ b/docs/feedback/VDE-141.md @@ -2,7 +2,7 @@ **Date**: 2026-07-31 **Severity**: Low -**Status**: Open — 2026-10-06 审计重开。原关闭理由张冠李戴:本工单所报对象是静态工厂 `SdfNode::hex_prism(double h)`(`include/vde/sdf/sdf_tree.h:178`,实现 `src/sdf/sdf_tree.cpp:97-103` 将 radius 硬绑等于 height),关闭却引用了另一自由函数 `hex_prism(const Point3D& p, double radius, double height)`(`sdf_primitives.h:333`)。缺陷仍成立——`SdfNode::hex_prism` 无法独立设半径。见 `docs/reference/audit-feedback.md`。 +**Status**: Fixed — 2026-10-06 审计重开并实现。原关闭理由张冠李戴:本工单所报对象是静态工厂 `SdfNode::hex_prism(double h)`(`include/vde/sdf/sdf_tree.h:178`,实现 `src/sdf/sdf_tree.cpp:97-103` 将 radius 硬绑等于 height),关闭却引用了另一自由函数 `hex_prism(const Point3D& p, double radius, double height)`(`sdf_primitives.h:333`)。**修复**:工厂签名改为 `SdfNode::hex_prism(double radius, double height, int sides = 6)`(`sdf_tree.h:178-182`),`SdfParams` 新增 `int sides`;新增通用 `regular_polygon_prism(p, radius, height, sides)` 精确 SDF(半平面/扇区折叠,sides=6 与旧 `hex_prism` 几何一致),`evaluate` 经其求值。判别性测试见 `tests/sdf/test_sdf_tree.cpp`(半径/高度独立、边数控截面、非法边数夹取)与 `test_sdf_primitives.cpp`(顶点共享、面法向精确距离)。见 `docs/reference/audit-feedback.md`。 ## Summary diff --git a/docs/reference/audit-feedback.md b/docs/reference/audit-feedback.md index fd428c372..2afeaadc7 100644 --- a/docs/reference/audit-feedback.md +++ b/docs/reference/audit-feedback.md @@ -87,7 +87,7 @@ CLOSED 的 Out of Scope 主体(约 601 份)为成批自动分诊 | 工单 | 问题 | |------|------| -| VDE-141 | 关闭理由张冠李戴:所报对象是 `SdfNode::hex_prism(double h)`(`sdf_tree.h:178`,半径被硬绑等于高度),关闭却引用了另一自由函数 `hex_prism(Point3D,radius,height)`(`sdf_primitives.h:333`)。**缺陷仍成立** | +| VDE-141 | 关闭理由张冠李戴:所报对象是 `SdfNode::hex_prism(double h)`(`sdf_tree.h:178`,半径被硬绑等于高度),关闭却引用了另一自由函数 `hex_prism(Point3D,radius,height)`(`sdf_primitives.h:333`)。**缺陷已修复**(2026-10-06 改为 `hex_prism(radius, height, sides=6)` + 通用 `regular_polygon_prism`),见 §4 | | VDE-749~754 | 6 份 P1 级领域功能(石化/半导体/运动场馆/轨道交通/船舶/选矿)原仅写 `Closed — Out of Scope`,无任何范围判定依据。**已补录关闭依据注**(应用层垂直领域整包,超出几何内核范围,与同批 601 份应用层 Out-of-Scope 口径一致);状态维持 closed | ### 3.4 既有审计文档的更正 @@ -106,7 +106,7 @@ CLOSED 的 Out of Scope 主体(约 601 份)为成批自动分诊 | VDE-316 | `visitParts` 携带 partIndex、`AssemblyNode::partIndex()` | brep/assembly | | VDE-441 | 精确消隐线、剖面线材质/标准、尺寸吸附策略、标题栏字段 | brep/drafting | | VDE-506 | 壁厚逐点样本、`SectionProperties.perimeter`、`DraftFaceResult::is_undercut()`、`face_count()` | brep/measure | -| VDE-141 | `SdfNode::hex_prism` 支持独立半径/边数 | sdf | +| VDE-141 | ✅ **已修复**(2026-10-06)`SdfNode::hex_prism(radius, height, sides=6)` 支持独立半径/边数;新增通用 `regular_polygon_prism`;判别性测试 5 项全绿 | sdf | | VDE-006/013/067 | 循环依赖解除、模块开关接线、裸 `mesh::` 前缀清理 | build/brep | > 注:VDE-749~754 属大颗粒领域模块(石化/半导体等),已补录关闭依据(应用层垂直领域,超出几何内核范围),如项目范围扩展需另立路线图票。 diff --git a/include/vde/sdf/sdf_primitives.h b/include/vde/sdf/sdf_primitives.h index 1bd1b0296..7e2db8075 100644 --- a/include/vde/sdf/sdf_primitives.h +++ b/include/vde/sdf/sdf_primitives.h @@ -309,14 +309,70 @@ using core::Vector3D; // ═══════════════════════════════════════════════ /** - * @brief 正六棱柱的 SDF + * @brief 正多边形棱柱的 SDF(任意边数) * - * 六边形位于 XZ 平面,尖顶沿 X 轴方向,沿 Y 轴挤出的正六棱柱。 + * 正 `sides` 边形截面位于 XZ 平面,第一个顶点沿 +X 轴方向,沿 Y 轴居中挤出。 + * 该函数是 `hex_prism` 及 `SdfNode::hex_prism` 的通用形式。 * * **算法概要:** - * 1. 将点对称映射到第一象限后,计算二维六边形的 SDF - * 2. 六边形 SDF 通过两组约束平面表示: max(px + 0.577*pz, 1.155*pz) - radius - * 3. 沿 Y 轴做 bounded extrusion + * 1. 截面角向对称,先按 2π/sides 的扇区折叠到单个「面法线角」局部坐标系((qx,qy)) + * 2. 边心距(内切圆半径)= radius·cos(π/sides),半边长 = radius·sin(π/sides) + * 3. 在局部坐标减去边心距/半边长并按边长夹取,得到精确的二维凸多边形距离 + * 4. 沿 Y 轴做 bounded extrusion + * + * 结果为真实欧氏距离(表面邻域 |∇f| ≈ 1),非缩放近似。 + * + * @param p 采样点 + * @param radius 正多边形外接圆半径(中心到顶点),必须 > 0 + * @param height 挤出总高度(±h/2 沿 Y 轴),必须 > 0 + * @param sides 边数,≥ 3(3=三角形截面,4=正方形,6=正六边形…);< 3 时按 3 处理 + * @return 有符号距离 + * + * @code{.cpp} + * // 外接圆半径 1.0、高 2.0 的正五棱柱 + * double d = regular_polygon_prism(Point3D(0.0, 0.0, 0.0), 1.0, 2.0, 5); + * @endcode + * + * @see hex_prism 六棱柱(sides = 6 的特例) + */ +[[nodiscard]] inline double regular_polygon_prism(const Point3D& p, double radius, + double height, int sides) { + constexpr double kPi = 3.14159265358979323846; + if (sides < 3) sides = 3; + const double n = static_cast(sides); + const double an = kPi / n; // half of the angular sector width + const double apothem = radius * std::cos(an); // face distance from centre + const double half_edge = radius * std::sin(an);// half of one edge length + + const double x = p.x(); + const double z = p.z(); + const double len = std::sqrt(x * x + z * z); + + // Fold the azimuth onto the nearest face-normal sector centred at an + k·2an. + double d_poly; + if (len < 1e-15) { + d_poly = -apothem; + } else { + const double theta = std::atan2(z, x); // measured from +X + const double bn = theta - (an + 2.0 * an * std::floor((theta - an) / (2.0 * an) + 0.5)); + double qx = len * std::cos(bn) - apothem; // along face normal + double qy = len * std::abs(std::sin(bn)) - half_edge; // along edge + // Clamp: nearest point on the edge segment (keeps the true Euclidean distance). + qy += std::clamp(-qy, 0.0, 2.0 * half_edge); + d_poly = std::sqrt(qx * qx + qy * qy) * (qx < 0.0 ? -1.0 : 1.0); + } + // Extrude along Y + return std::max(d_poly, std::abs(p.y()) - height * 0.5); +} + +/** + * @brief 正六棱柱的 SDF + * + * 六边形位于 XZ 平面,第一个顶点沿 +X 轴方向(尖顶朝 X),沿 Y 轴居中挤出。 + * 等价于 `regular_polygon_prism(p, radius, height, 6)`。 + * + * **算法概要:** 六个半平面(外法线 30°/90°/…/330°)的有符号距离取最大值, + * 再做 bounded extrusion;结果为精确欧氏距离。 * * @param p 采样点 * @param radius 六边形外接圆半径(中心到顶点),必须 > 0 @@ -328,15 +384,11 @@ using core::Vector3D; * double d = hex_prism(Point3D(0.0, 0.0, 0.0), 1.0, 2.0); * @endcode * + * @see regular_polygon_prism 任意边数的正多边形棱柱 * @see extrusion_bounded 通用 bounded extrusion 算子 */ [[nodiscard]] inline double hex_prism(const Point3D& p, double radius, double height) { - double px = std::abs(p.x()); - double pz = std::abs(p.z()); - // 2D hexagon SDF (pointy-top in XZ plane) - double d_hex = std::max(px + pz * 0.577350269189626, pz * 1.154700538379252) - radius; - // Extrude along Y - return std::max(d_hex, std::abs(p.y()) - height * 0.5); + return regular_polygon_prism(p, radius, height, 6); } /** diff --git a/include/vde/sdf/sdf_tree.h b/include/vde/sdf/sdf_tree.h index 4f0ed6b74..1b85c1491 100644 --- a/include/vde/sdf/sdf_tree.h +++ b/include/vde/sdf/sdf_tree.h @@ -110,6 +110,7 @@ struct SdfParams { double thickness = 0.1; ///< 壳层厚度(Onion) Vector3D normal = Vector3D(0, 1, 0); ///< 平面法向量 double offset = 0.0; ///< 平面偏移 / 通用偏移 + int sides = 6; ///< 正多边形棱柱截面边数(HexPrism,≥3) // ── Operation parameters ── double blend_k = 0.2; ///< 平滑 CSG 混合参数 @@ -174,8 +175,11 @@ public: /** @brief 创建三角棱柱节点 */ static SdfNodePtr triangular_prism(double h); - /** @brief 创建六棱柱节点 */ - static SdfNodePtr hex_prism(double h); + /** @brief 创建正多边形棱柱节点(默认六棱柱) + * @param radius 正多边形外接圆半径(中心到顶点) + * @param height 沿 Y 轴的总高度 + * @param sides 边数(3=三角形截面,4=正方形,6=正六边形…),默认 6 */ + static SdfNodePtr hex_prism(double radius, double height, int sides = 6); /** @brief 创建双环链接节点 */ static SdfNodePtr link(double r, double length, double thickness); diff --git a/python/src/bind_sdf.cpp b/python/src/bind_sdf.cpp index 90b0b9eb0..4e7c93f63 100644 --- a/python/src/bind_sdf.cpp +++ b/python/src/bind_sdf.cpp @@ -44,7 +44,8 @@ nodes with the factory functions. .def_static("triangular_prism", &SdfNode::triangular_prism, py::arg("height"), "Triangular prism along Y") .def_static("hex_prism", &SdfNode::hex_prism, - py::arg("height"), "Hexagonal prism along Y") + py::arg("radius"), py::arg("height"), py::arg("sides") = 6, + "Regular-polygon prism along Y (sides=6 → hexagon)") .def_static("link", &SdfNode::link, py::arg("radius"), py::arg("length"), py::arg("thickness"), "Link segment") .def_static("wedge", &SdfNode::wedge, diff --git a/python/src/vde_sdf.cpp b/python/src/vde_sdf.cpp index 2a4b2ff0e..4a661fa5a 100644 --- a/python/src/vde_sdf.cpp +++ b/python/src/vde_sdf.cpp @@ -77,7 +77,8 @@ Example: .def_static("triangular_prism", &SdfNode::triangular_prism, py::arg("height"), "Triangular prism along Y") .def_static("hex_prism", &SdfNode::hex_prism, - py::arg("height"), "Hexagonal prism along Y") + py::arg("radius"), py::arg("height"), py::arg("sides") = 6, + "Regular-polygon prism along Y (sides=6 → hexagon)") .def_static("link", &SdfNode::link, py::arg("radius"), py::arg("length"), py::arg("thickness"), "Link segment") diff --git a/src/sdf/sdf_tree.cpp b/src/sdf/sdf_tree.cpp index 61a761c7a..848632bfd 100644 --- a/src/sdf/sdf_tree.cpp +++ b/src/sdf/sdf_tree.cpp @@ -94,10 +94,11 @@ SdfNodePtr SdfNode::triangular_prism(double h) { return n; } -SdfNodePtr SdfNode::hex_prism(double h) { +SdfNodePtr SdfNode::hex_prism(double radius, double height, int sides) { auto n = make(SdfOp::HexPrism); - n->params.radius = h; // circumradius - n->params.height = h; // full height (= radius for equilateral) + n->params.radius = radius; // circumradius of the regular polygon + n->params.height = height; // full height along Y + n->params.sides = (sides < 3 ? 3 : sides); // 3 = triangle, 6 = hexagon, … n->name = "hex_prism"; return n; } @@ -242,7 +243,7 @@ double evaluate(const SdfNodePtr& node, const Point3D& p) { return triangular_prism(p, a, b, c, pr.height); } case SdfOp::HexPrism: - return hex_prism(p, pr.radius, pr.height); + return regular_polygon_prism(p, pr.radius, pr.height, pr.sides); case SdfOp::Link: return link(p, pr.height, pr.radius, pr.thickness); case SdfOp::Wedge: { @@ -337,8 +338,10 @@ static double estimate_radius(const SdfNodePtr& node) { node->params.extents.y(), node->params.extents.z()}); case SdfOp::TriangularPrism: - case SdfOp::HexPrism: return node->params.height; + case SdfOp::HexPrism: + return std::sqrt(node->params.radius * node->params.radius + + node->params.height * node->params.height * 0.25); case SdfOp::Link: return node->params.radius + node->params.height * 0.5 + node->params.thickness; case SdfOp::Wedge: diff --git a/tests/sdf/test_sdf_primitives.cpp b/tests/sdf/test_sdf_primitives.cpp index c4fb800bb..ba40e9410 100644 --- a/tests/sdf/test_sdf_primitives.cpp +++ b/tests/sdf/test_sdf_primitives.cpp @@ -331,6 +331,42 @@ TEST(SdfHexPrism, Degenerate) { EXPECT_NEAR(d, 0.0, 1e-6); } +// ═══════════════════════════════════════════════════ +// regular_polygon_prism (VDE-141) +// ═══════════════════════════════════════════════════ + +TEST(SdfRegularPolygonPrism, VertexSharedAcrossSideCounts) { + // First vertex lies on +X at distance = circumradius for every side count. + for (int n : {3, 4, 5, 6, 8}) + EXPECT_NEAR(regular_polygon_prism(Point3D(2.0, 0.0, 0.0), 2.0, 4.0, n), 0.0, EPS); +} + +TEST(SdfRegularPolygonPrism, ExactDistanceAlongFaceNormal) { + const double PI_6 = std::acos(-1.0) / 6.0; + // +Z is a face-normal direction for the hexagon → distance is exact. + EXPECT_NEAR(regular_polygon_prism(Point3D(0.0, 0.0, 1.2), 1.0, 4.0, 6), 1.2 - std::cos(PI_6), EPS); + double di = regular_polygon_prism(Point3D(0.0, 0.0, 0.3), 1.0, 4.0, 6); + EXPECT_NEAR(di, 0.3 - std::cos(PI_6), EPS); // inside → negative + EXPECT_LT(di, 0.0); + // Axial cap wins when the Y offset exceeds the half height. + EXPECT_NEAR(regular_polygon_prism(Point3D(0.0, 3.0, 0.0), 1.0, 4.0, 6), 1.0, EPS); +} + +TEST(SdfRegularPolygonPrism, SideCountChangesCrossSectionSign) { + // (0,0,0.9) is outside the triangle (apothem 0.5), inside the square + // (0.707) and outside the hexagon (apothem 0.866). + Point3D q(0.0, 0.0, 0.9); + EXPECT_GT(regular_polygon_prism(q, 1.0, 4.0, 3), 0.0); + EXPECT_LT(regular_polygon_prism(q, 1.0, 4.0, 4), 0.0); + EXPECT_GT(regular_polygon_prism(q, 1.0, 4.0, 6), 0.0); +} + +TEST(SdfRegularPolygonPrism, InvalidSideCountClampedToTriangle) { + Point3D q(0.0, 0.0, 0.9); + EXPECT_NEAR(regular_polygon_prism(q, 1.0, 4.0, 2), + regular_polygon_prism(q, 1.0, 4.0, 3), EPS); +} + // ═══════════════════════════════════════════════════ // link // ═══════════════════════════════════════════════════ diff --git a/tests/sdf/test_sdf_tree.cpp b/tests/sdf/test_sdf_tree.cpp index dde3c1995..53049d360 100644 --- a/tests/sdf/test_sdf_tree.cpp +++ b/tests/sdf/test_sdf_tree.cpp @@ -469,3 +469,70 @@ TEST(SdfTreeFactory, SmoothDifferenceMatchesIndependentBlend) { EXPECT_NEAR(evaluate(tree, bs), ref(bs), 1e-12); EXPECT_GT(evaluate(tree, bs), 0.0); } + +// ═══════════════════════════════════════════════════════════════════ +// VDE-141: SdfNode::hex_prism(radius, height, sides) +// The factory used to take only a height and hard-bound the cross-section +// circumradius to it. These tests pin the cross-section radius and the +// polygon side count as independent parameters. +// ═══════════════════════════════════════════════════════════════════ + +TEST(SdfTreeFactory, HexPrismRadiusIndependentOfHeight) { + const double R = 2.0; + auto short_p = SdfNode::hex_prism(R, 4.0); + auto tall_p = SdfNode::hex_prism(R, 20.0); + + // Factory stores radius/height as distinct fields (previously radius==height). + EXPECT_DOUBLE_EQ(short_p->params.radius, R); + EXPECT_DOUBLE_EQ(short_p->params.height, 4.0); + EXPECT_EQ(short_p->params.sides, 6); + EXPECT_DOUBLE_EQ(tall_p->params.height, 20.0); + + // Same circumradius → identical in-plane extent: (3,0,0) is 1.0 outside + // the +X vertex for both nodes, regardless of height. + EXPECT_NEAR(evaluate(short_p, Point3D(3, 0, 0)), 1.0, 1e-12); + EXPECT_NEAR(evaluate(tall_p, Point3D(3, 0, 0)), 1.0, 1e-12); + + // Different height → the Y caps differ: a point above the short prism is + // inside the tall one. + EXPECT_GT(evaluate(short_p, Point3D(0, 6, 0)), 0.0); // 6 > 4/2 + EXPECT_LT(evaluate(tall_p, Point3D(0, 6, 0)), 0.0); // 6 < 20/2 + + // Independent radius: same height, halved radius moves the surface inward. + auto narrow = SdfNode::hex_prism(1.0, 4.0); + EXPECT_NEAR(evaluate(narrow, Point3D(1.5, 0, 0)), 0.5, 1e-12); // just outside + EXPECT_LT(evaluate(short_p, Point3D(1.5, 0, 0)), 0.0); // deep inside +} + +TEST(SdfTreeFactory, HexPrismSidesControlsCrossSection) { + // Same circumradius (1.0) → every polygon shares the +X vertex exactly. + auto tri = SdfNode::hex_prism(1.0, 4.0, 3); + auto sq = SdfNode::hex_prism(1.0, 4.0, 4); + auto hex = SdfNode::hex_prism(1.0, 4.0, 6); + + EXPECT_EQ(tri->params.sides, 3); + EXPECT_EQ(sq->params.sides, 4); + EXPECT_EQ(hex->params.sides, 6); + + for (const auto& node : {tri, sq, hex}) + EXPECT_NEAR(evaluate(node, Point3D(1.0, 0, 0)), 0.0, 1e-12); // shared vertex + + // Probe (0,0,0.9) lies on the +Z face-normal axis. Its signed distance + // depends on the side count: outside the triangle, inside the square, + // outside the hexagon (whose apothem is cos30°≈0.866 < 0.9). + Point3D q(0.0, 0.0, 0.9); + EXPECT_GT(evaluate(tri, q), 0.0); + EXPECT_LT(evaluate(sq, q), 0.0); + EXPECT_GT(evaluate(hex, q), 0.0); + + // The triangle's +Z extent is smallest (apothem 0.5) → largest positive + // distance; the hexagon's is largest (apothem 0.866) → smallest positive. + EXPECT_GT(evaluate(tri, q), evaluate(hex, q)); +} + +TEST(SdfTreeFactory, HexPrismClampsInvalidSideCount) { + // sides < 3 is meaningless; the factory clamps to a triangle. + auto bad = SdfNode::hex_prism(1.0, 2.0, 2); + EXPECT_EQ(bad->params.sides, 3); + EXPECT_NEAR(evaluate(bad, Point3D(0, 0, 0.6)), evaluate(SdfNode::hex_prism(1.0, 2.0, 3), Point3D(0, 0, 0.6)), 1e-12); +}