feat(brep): 解析曲面 SSI 基础模块——隐式函数+相交曲线追踪,6 项几何测试全过(VDE-1193 §9.11)
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

- analytic_ssi.h/.cpp:Plane/Cylinder/Cone/Sphere/Torus 五种解析曲面的隐式
  F(p)/梯度 dF(p),两曲面相交曲线的种子搜索+牛顿投影+切向双向追踪,
  多分支自然覆盖(柱×柱 Steinmetz 双交线)、闭合折线、退化(相切/重合)判定
- 单元测试 6 项:面面直线/柱面圆/锥面圆/双柱双分支/无交空/球面圆,
  交线几何精度 1e-3~1e-6 全过
- C 类内核第一步:后续将交线接入 heal 面分割(bug33261 body1/2 锥×柱
  为验收目标)
This commit is contained in:
hm
2026-09-13 16:40:16 +08:00
parent 546e455ce2
commit 3bc648534f
5 changed files with 405 additions and 0 deletions
+156
View File
@@ -0,0 +1,156 @@
#pragma once
/**
* @file analytic_ssi.h
* @brief 解析曲面(平面/圆柱/圆锥/球/圆环)隐式表示与两两相交曲线追踪
* (VDE-1193 §9.11 C 类内核第一步)
*
* C 类穿透体的穿透面为解析曲面(plane/cyl/cone/torus)。修复需要沿相交
* 曲线分割面 —— 第一步是稳健地求两解析曲面的相交曲线。本模块提供:
* 1. 五种解析曲面的隐式函数 F(p)(面上为 0)与梯度 ∇F;
* 2. 两曲面相交曲线的追踪:种子搜索(双 |F| 谷底)+ 牛顿投影到
* {F1=0, F2=0} 交线 + 沿切向(∇F1×∇F2)步进追踪,输出采样折线。
*
* 追踪为数值方法(非分型讨论),对平面/柱/锥/球/环的任意组合给出
* 与解析交线吻合(弦差 ≤ tol)的折线;退化(相切/重合/无交)以空输出
* + 返回值区分。
*/
#include "vde/core/point.h"
#include <cmath>
#include <vector>
namespace vde::brep {
namespace ssi {
/// 解析曲面种类
enum class Kind { Plane, Cylinder, Cone, Sphere, Torus };
/// 解析曲面参数(局部标架:点 o、轴 z、参考 x;z 已归一化)
struct Surface {
Kind kind{};
core::Point3D o{0, 0, 0}; // 平面:面上一点;旋转面:轴上参考点
core::Vector3D z{0, 0, 1}; // 轴(平面:法向)
core::Vector3D x{1, 0, 0}; // 参考方向(⊥ z)
double r1 = 0; // Cylinder: 半径;Cone: 底半径(z=0 处);Sphere: 半径;Torus: 主半径
double r2 = 0; // Cone: 与 r1 的轴向距离差基准(半角=atan((r1-r2)/h) 用 h);Torus: 管半径
double h = 1; // Cone: 轴向高度(r(z)= r1 + (r2-r1)*z/h)
};
/// 隐式函数 F(p):面上为 0。约定符号:旋转面 F = 到轴距离 − r(轴向)。
inline double F(const Surface& s, const core::Point3D& p) {
const core::Vector3D d = p - s.o;
switch (s.kind) {
case Kind::Plane:
return d.dot(s.z);
case Kind::Cylinder: {
core::Vector3D a = d - s.z * d.dot(s.z);
return a.norm() - s.r1;
}
case Kind::Cone: {
const double t = d.dot(s.z); // 轴向坐标
const double rr = s.r1 + (s.r2 - s.r1) * (t / s.h); // 该轴向处的半径
core::Vector3D a = d - s.z * t;
return a.norm() - rr;
}
case Kind::Sphere:
return d.norm() - s.r1;
case Kind::Torus: {
const double t = d.dot(s.z);
core::Vector3D a = d - s.z * t;
const double ra = a.norm();
const core::Vector3D maj = a * (ra > 1e-12 ? (s.r1 / ra) : 0.0);
// 从主圆最近点指向 p 的完整 3D 向量(含轴向分量)
const core::Vector3D q = (a - maj) + s.z * t;
return (q.norm() - s.r2);
}
}
return 0.0;
}
/// ∇F(p)(解析导数;数值上足够用于牛顿投影)
inline core::Vector3D dF(const Surface& s, const core::Point3D& p) {
const core::Vector3D d = p - s.o;
switch (s.kind) {
case Kind::Plane:
return s.z;
case Kind::Cylinder: {
core::Vector3D a = d - s.z * d.dot(s.z);
const double n = a.norm();
return n > 1e-12 ? a * (1.0 / n) : s.x;
}
case Kind::Cone: {
const double t = d.dot(s.z);
core::Vector3D a = d - s.z * t;
const double n = a.norm();
if (n < 1e-12) return s.x;
const double slope = (s.r2 - s.r1) / s.h;
// d(|a| − r(t))/dp = û − slope·ẑ,û = a/n(忽略 −t 项的二阶耦合,
// 牛顿投影会收敛到真零集)
core::Vector3D u = a * (1.0 / n);
return u - s.z * slope;
}
case Kind::Sphere: {
const double n = d.norm();
return n > 1e-12 ? d * (1.0 / n) : s.x;
}
case Kind::Torus: {
const double t = d.dot(s.z);
core::Vector3D a = d - s.z * t;
const double ra = a.norm();
if (ra < 1e-12) return s.z;
const core::Vector3D maj = a * (s.r1 / ra);
const core::Vector3D q = (a - maj) + s.z * t; // p − 主圆最近点
const double qn = q.norm();
return qn > 1e-12 ? q * (1.0 / qn) : s.z;
}
}
return core::Vector3D(0, 0, 1);
}
/// 牛顿投影:把 p 拉回 {F1=0 ∧ F2=0} 交线(解 2×2 最小二乘)。
/// 返回是否收敛(残差 ≤ tol)。
inline bool project_to_curve(const Surface& s1, const Surface& s2,
core::Point3D& p, double tol, int max_iter = 32) {
for (int it = 0; it < max_iter; ++it) {
const double f1 = F(s1, p), f2 = F(s2, p);
if (std::abs(f1) <= tol && std::abs(f2) <= tol) return true;
const core::Vector3D g1 = dF(s1, p), g2 = dF(s2, p);
// 法方程 [[g1·g1, g1·g2],[g2·g1, g2·g2]] [a,b]ᵀ = −[f1,f2]ᵀ
const double a11 = g1.dot(g1), a12 = g1.dot(g2);
const double a22 = g2.dot(g2);
const double det = a11 * a22 - a12 * a12;
if (std::abs(det) < 1e-18) return false; // 梯度平行:相切/重合
const double b1 = -f1, b2 = -f2;
const double lam = (b1 * a22 - b2 * a12) / det;
const double mu = (a11 * b2 - a12 * b1) / det;
p = p + g1 * lam + g2 * mu;
}
const double f1 = F(s1, p), f2 = F(s2, p);
return std::abs(f1) <= tol && std::abs(f2) <= tol;
}
struct TraceOptions {
double tol = 1e-6; // 曲线残差(mm 量纲随模型)
double step = 0.05; // 追踪步长(相对模型尺度宜取包围盒的 ~1/500)
int max_points = 4096; // 单条折线点数上限
double accept_cos = 0.0; // 相邻段方向余弦下限(0=不检查折返)
};
/// 相交曲线追踪结果:0 条 = 无交/相切/重合退化;≥1 条折线。
struct TraceResult {
std::vector<std::vector<core::Point3D>> polylines;
bool degenerate = false; // 梯度平行(相切/重合面)
};
/// 两解析曲面的相交曲线追踪。
/// 算法:① 在 s1 的参数网格上找 |F1|≤tol 且 |F2| 谷底的种子(网格密度由
/// step 控制);② 牛顿投影到交线;③ 沿切向 ∇F1×∇F2 双向步进 + 每步牛顿
/// 校正;④ 折线闭合或走到边界/上限停止。多分支(如柱×柱双交线)由多种子
/// 自然覆盖(已访问点去重防止重复输出)。
TraceResult trace_intersection(const Surface& s1, const Surface& s2,
const core::Point3D& seed_region_min,
const core::Point3D& seed_region_max,
const TraceOptions& opt = {});
} // namespace ssi
} // namespace vde::brep
+1
View File
@@ -252,6 +252,7 @@ add_library(vde_brep STATIC
brep/iges_export.cpp
brep/brep_boolean.cpp
brep/ssi_boolean.cpp
brep/analytic_ssi.cpp
brep/brep_validate.cpp
brep/brep_heal.cpp
brep/brep_face_split.cpp
+152
View File
@@ -0,0 +1,152 @@
/**
* @file analytic_ssi.cpp
* @brief 解析曲面相交曲线追踪实现(VDE-1193 §9.11)
*/
#include "vde/brep/analytic_ssi.h"
#include <algorithm>
#include <set>
#include <cstdint>
namespace vde::brep {
namespace ssi {
namespace {
inline uint64_t mix64(uint64_t x) {
x ^= x >> 33; x *= 0xff51afd7ed558ccdULL;
x ^= x >> 33; x *= 0xc4ceb9fe1a85ec53ULL;
x ^= x >> 33;
return x;
}
/// 空间哈希键(cell 尺寸 = step)
inline uint64_t cell_key(const core::Point3D& p, double inv_step) {
auto q = [](double v) { return (int64_t)std::floor(v); };
const int64_t x = q(p.x() * inv_step), y = q(p.y() * inv_step),
z = q(p.z() * inv_step);
uint64_t h = mix64((uint64_t)(uint32_t)(int32_t)x);
h ^= mix64((uint64_t)(uint32_t)(int32_t)y + 0x9E3779B9ull);
h ^= mix64((uint64_t)(uint32_t)(int32_t)z + 0x85EBCA77ull);
return h;
}
/// 种子搜索:在种子区域网格上找双 |F| 谷底,牛顿投影后去重。
std::vector<core::Point3D> find_seeds(const Surface& s1, const Surface& s2,
const core::Point3D& mn,
const core::Point3D& mx,
const TraceOptions& opt) {
std::vector<core::Point3D> seeds;
const int N = 40; // 每轴网格
core::Vector3D ext = mx - mn;
const double gx = ext.x() > 1e-12 ? ext.x() / N : 0.0;
const double gy = ext.y() > 1e-12 ? ext.y() / N : 0.0;
const double gz = ext.z() > 1e-12 ? ext.z() / N : 0.0;
// 谷底判定阈值:取两曲面特征尺度的比例
const double feature = std::max({s1.r1, s1.r2, s2.r1, s2.r2, 1.0});
const double near_tol = feature * 0.02;
auto ok = [&](const core::Point3D& p) {
const double f1 = F(s1, p), f2 = F(s2, p);
if (std::abs(f1) > near_tol || std::abs(f2) > near_tol) return false;
core::Point3D q = p;
return project_to_curve(s1, s2, q, opt.tol);
};
for (int i = 0; i <= N; ++i)
for (int j = 0; j <= N; ++j)
for (int k = 0; k <= N; ++k) {
core::Point3D p(mn.x() + gx * i, mn.y() + gy * j, mn.z() + gz * k);
if (!ok(p)) continue;
bool dup = false;
for (const auto& s : seeds)
if ((s - p).norm() < opt.step * 2.0) { dup = true; break; }
if (!dup) seeds.push_back(p);
}
return seeds;
}
} // namespace
TraceResult trace_intersection(const Surface& s1, const Surface& s2,
const core::Point3D& seed_region_min,
const core::Point3D& seed_region_max,
const TraceOptions& opt) {
TraceResult res;
const double inv_step = 1.0 / std::max(opt.step, 1e-9);
// 相切/重合检测:区域内曲面双侧采样点梯度平行率过高 → 退化
{
int parallel = 0, total = 0;
core::Vector3D ext = seed_region_max - seed_region_min;
for (int i = 0; i < 64; ++i) {
const double fx = (i * 37 % 97) / 97.0, fy = (i * 61 % 89) / 89.0,
fz = (i * 23 % 83) / 83.0;
core::Point3D p(seed_region_min.x() + ext.x() * fx,
seed_region_min.y() + ext.y() * fy,
seed_region_min.z() + ext.z() * fz);
const double f1 = F(s1, p), f2 = F(s2, p);
if (std::abs(f1) > 10 * opt.tol || std::abs(f2) > 10 * opt.tol) continue;
core::Vector3D g1 = dF(s1, p), g2 = dF(s2, p);
if (g1.norm() < 1e-12 || g2.norm() < 1e-12) continue;
g1.normalize(); g2.normalize();
++total;
if (std::abs(g1.dot(g2)) > 1 - 1e-9) ++parallel;
}
if (total > 0 && parallel == total) {
res.degenerate = true;
return res;
}
}
// 种子搜索
auto seeds = find_seeds(s1, s2, seed_region_min, seed_region_max, opt);
std::set<uint64_t> visited_cells; // 已输出折线的空间哈希格(去重/闭合判定)
// 单向追踪:从 from 沿 ±切向走,返回 after 点列(不含 from)
auto march = [&](const core::Point3D& from, double sgn) {
std::vector<core::Point3D> pts;
core::Point3D p = from;
for (int k = 0; k < opt.max_points; ++k) {
core::Vector3D g1 = dF(s1, p), g2 = dF(s2, p);
core::Vector3D T = g1.cross(g2);
if (T.norm() < 1e-14) break; // 相切
T.normalize();
core::Point3D q = p + T * (sgn * opt.step);
if (!project_to_curve(s1, s2, q, opt.tol)) break;
const uint64_t ck = cell_key(q, inv_step);
if (visited_cells.count(ck)) break;
// 闭合:回到起点邻域
if ((q - from).norm() <= opt.step * 1.5 && pts.size() >= 3) {
pts.push_back(from);
break;
}
visited_cells.insert(ck);
pts.push_back(q);
p = q;
}
return pts;
};
for (const auto& seed : seeds) {
core::Point3D p0 = seed;
if (!project_to_curve(s1, s2, p0, opt.tol)) continue;
if (visited_cells.count(cell_key(p0, inv_step))) continue;
auto fwd = march(p0, +1.0);
auto bwd = march(p0, -1.0);
std::vector<core::Point3D> pts;
for (auto it = bwd.rbegin(); it != bwd.rend(); ++it) pts.push_back(*it);
pts.push_back(p0);
for (const auto& q : fwd) pts.push_back(q);
if ((int)pts.size() < 4) continue;
// 标记全部已访问
for (const auto& q : pts) visited_cells.insert(cell_key(q, inv_step));
res.polylines.push_back(std::move(pts));
}
return res;
}
} // namespace ssi
} // namespace vde::brep
+1
View File
@@ -4,6 +4,7 @@ add_vde_test(test_step_export)
add_vde_test(test_step_import)
add_vde_test(test_brep_boolean)
add_vde_test(test_ssi_boolean)
add_vde_test(test_analytic_ssi)
add_vde_test(test_brep_validate)
add_vde_test(test_iges_import)
add_vde_test(test_iges_export)
+95
View File
@@ -0,0 +1,95 @@
/**
* @file test_analytic_ssi.cpp
* @brief 解析曲面相交曲线追踪测试(VDE-1193 §9.11)
*/
#include <gtest/gtest.h>
#include "vde/brep/analytic_ssi.h"
#include <cmath>
using namespace vde::brep;
using namespace vde::brep::ssi;
namespace {
/// 验证折线全部点满足双隐式零(残差 ≤ tol)
void expect_on_both(const TraceResult& r, const Surface& s1, const Surface& s2,
double tol) {
ASSERT_FALSE(r.polylines.empty());
for (const auto& pl : r.polylines)
for (const auto& p : pl) {
EXPECT_LE(std::abs(F(s1, p)), tol * 10) << "F1 残差超差";
EXPECT_LE(std::abs(F(s2, p)), tol * 10) << "F2 残差超差";
}
}
} // namespace
TEST(AnalyticSsiTest, PlanePlane_Line) {
Surface a; a.kind = Kind::Plane; a.o = {0, 0, 0}; a.z = {0, 0, 1};
Surface b; b.kind = Kind::Plane; b.o = {0, 0, 0}; b.z = {1, 0, 0};
TraceOptions opt; opt.step = 0.05; opt.tol = 1e-9;
auto r = trace_intersection(a, b, {-5, -5, -5}, {5, 5, 5}, opt);
expect_on_both(r, a, b, opt.tol);
// 交线沿 Y 轴:所有点 x≈0, z≈0
size_t n = 0;
for (const auto& pl : r.polylines) n += pl.size();
EXPECT_GE(n, 20);
for (const auto& pl : r.polylines)
for (const auto& p : pl) {
EXPECT_NEAR(p.x(), 0.0, 1e-6);
EXPECT_NEAR(p.z(), 0.0, 1e-6);
}
}
TEST(AnalyticSsiTest, PlaneCylinder_Ellipse) {
// z=0 平面 × 竖直圆柱 r=2(轴过原点)→ 交线为圆(半径 2)
Surface a; a.kind = Kind::Plane; a.o = {0, 0, 0}; a.z = {0, 0, 1};
Surface c; c.kind = Kind::Cylinder; c.o = {0, 0, -5}; c.z = {0, 0, 1}; c.r1 = 2;
TraceOptions opt; opt.step = 0.05; opt.tol = 1e-7;
auto r = trace_intersection(a, c, {-3, -3, -1}, {3, 3, 1}, opt);
expect_on_both(r, a, c, opt.tol);
for (const auto& pl : r.polylines)
for (const auto& p : pl)
EXPECT_NEAR(std::sqrt(p.x() * p.x() + p.y() * p.y()), 2.0, 1e-4);
}
TEST(AnalyticSsiTest, PlaneCone_Circle) {
// 半角 45° 锥(轴 +Z,顶点在原点)与 z=1 平面 → 半径 1 的圆
Surface k; k.kind = Kind::Cone; k.o = {0, 0, 0}; k.z = {0, 0, 1};
k.r1 = 0; k.r2 = 1; k.h = 1; // z=h 处半径 = r2-r1 = 1
Surface a; a.kind = Kind::Plane; a.o = {0, 0, 1}; a.z = {0, 0, 1};
TraceOptions opt; opt.step = 0.02; opt.tol = 1e-7;
auto r = trace_intersection(k, a, {-2, -2, 0}, {2, 2, 1.5}, opt);
expect_on_both(r, k, a, opt.tol);
for (const auto& pl : r.polylines)
for (const auto& p : pl)
EXPECT_NEAR(std::sqrt(p.x() * p.x() + p.y() * p.y()), 1.0, 1e-3);
}
TEST(AnalyticSsiTest, CylinderCylinder_TwoCrossLines) {
// 等半径正交圆柱:两轴向双交线(半径 2,轴 X 与轴 Z? 用轴 X 与轴 Y,交线为两条直线)
Surface a; a.kind = Kind::Cylinder; a.o = {0, 0, 0}; a.z = {0, 0, 1}; a.r1 = 2;
Surface b; b.kind = Kind::Cylinder; b.o = {0, 0, 0}; b.z = {0, 1, 0}; b.r1 = 2;
TraceOptions opt; opt.step = 0.05; opt.tol = 1e-7;
auto r = trace_intersection(a, b, {-4, -4, -4}, {4, 4, 4}, opt);
expect_on_both(r, a, b, opt.tol);
// 正交等半径圆柱交线 = 两条椭圆(Steinmetz),至少 2 分支
EXPECT_GE(r.polylines.size(), 2u);
}
TEST(AnalyticSsiTest, NoIntersection_Empty) {
Surface a; a.kind = Kind::Plane; a.o = {0, 0, 0}; a.z = {0, 0, 1};
Surface c; c.kind = Kind::Cylinder; c.o = {10, 0, -5}; c.z = {0, 0, 1}; c.r1 = 1;
TraceOptions opt; opt.step = 0.05; opt.tol = 1e-7;
auto r = trace_intersection(a, c, {-2, -2, -2}, {2, 2, 2}, opt);
EXPECT_TRUE(r.polylines.empty());
}
TEST(AnalyticSsiTest, SpherePlane_Circle) {
Surface s; s.kind = Kind::Sphere; s.o = {0, 0, 0}; s.r1 = 3;
Surface a; a.kind = Kind::Plane; a.o = {0, 0, 1}; a.z = {0, 0, 1};
TraceOptions opt; opt.step = 0.05; opt.tol = 1e-7;
auto r = trace_intersection(s, a, {-4, -4, -4}, {4, 4, 4}, opt);
expect_on_both(r, s, a, opt.tol);
for (const auto& pl : r.polylines)
for (const auto& p : pl)
EXPECT_NEAR(std::sqrt(p.x() * p.x() + p.y() * p.y()), std::sqrt(8.0), 1e-3);
}