perf(curves): 投影链性能包——链 UV 外推+UV 窗口重试+Newton 早退+网格缓存+evaluate 免分配(VDE-1193 §9.11.7)
OCC8228 --heal 病态面 project_chain 28-38s/面 → 3-7.5s(3-6x): - NurbsSurface::evaluate 张量积双重和直接展开,消除每次调用的 row_h 大分配与 BSplineCurve 节点向量拷贝(Newton 每轮 11 次求值) - newton_project 增加 phys_exit2:链式温启动按调用方 tol 早退, 升级路径保持全精度语义 - GridCache 链级缓存:升级路径 24x24 全域网格每链只算一次, 种子与逐点 global_search 逐一相同 - project_polyline_on_surface 链 UV 线性外推(周期最短跨缝增量 + 非周期 10% 域宽伪跳守卫)+ 病态链 UV 窗口重试保底; 近退化带面链 warm 3/129 -> 79/129 - 投影语义守恒:验收门槛 resid<=tol 未动,15 套测试全绿
This commit is contained in:
@@ -1193,3 +1193,61 @@ OCC8228(761 面)--heal 超过 95 CPU 分钟不收敛——新增 `HealStageT
|
||||
②degenerate 投影初始值改进(1e6 参数域 Newton 初值不佳);③
|
||||
TrimmedSurface 存储复用(避免每次 heal 重建裁剪域)。本轮仅落计时工具,
|
||||
test_brep_heal 41/41、test_brep_validate 18/18 全绿。
|
||||
|
||||
#### 9.11.7 投影链性能包:链 UV 外推 + UV 窗口重试 + Newton 早退 + 网格缓存 + evaluate 免分配(2026-09-14)
|
||||
|
||||
Steiner 预算/CDT 索引(§9.11.6 续,294469c9/7e062414)落地后复测 OCC8228
|
||||
--heal,`VDE_TIMING_HEAL` 逐面计时显示剩余热点全部集中到 `project_chain`
|
||||
(环驱动裁剪域重建的边采样链投影):
|
||||
|
||||
| 面 | 改前 | 改后 | 加速 |
|
||||
|----|------|------|------|
|
||||
| f=0 sid=1 | 28.4s | 6.3s | 4.5× |
|
||||
| f=1 sid=2 | 38.2s | 7.5s | 5.1× |
|
||||
| f=2 sid=3 | 25.3s | 5.0s | 5.1× |
|
||||
| f=5 sid=6 | 30.7s | 5.2s | 5.9× |
|
||||
| f=6 sid=7 | 16.4s | 4.9s | 3.3× |
|
||||
| f=7 sid=8 | 11.8s | 3.0s | 3.9× |
|
||||
| f=8 sid=9 | 17.4s | 4.1s | 4.2× |
|
||||
| f=10 sid=11 | 24.4s | 6.5s | 3.8× |
|
||||
| f=9 sid=10(deg 2/11) | 10.2s | 7.3s | 1.4× |
|
||||
|
||||
根因(VDE_TRACE_PROJ_DETAIL 逐点归因,四层叠加):
|
||||
|
||||
1. **`NurbsSurface::evaluate` 每次调用堆分配**:row_h 向量(cp_.size()
|
||||
个 Eigen::Vector4d)+ 两个 BSplineCurve 临时对象(各拷贝整条节点向量
|
||||
+ 哑控制点数组);Newton 每轮 11 次求值(S + Su/Sv/Su+/Sv+/Svu 全部
|
||||
有限差分)× 每点 30 轮 → 单面 8.5 万次求值全带分配。修法:张量积
|
||||
双重和直接展开 `ΣΣ Nu[i]·Nv[j]·w_ij·P_ij`(与"先 v 后 u 齐次行组合"
|
||||
数学等价),O(deg_u·deg_v) 乘加、零分配(span/基函数内联复制
|
||||
BSplineCurve 同款算法含 denom 守卫与右端点修正;deg>63 兜底原路径)。
|
||||
2. **温启动 Newton 无调用方 tol 早退**:收敛判据只看噪声底
|
||||
r_th=1e-9·scale,链式温启动明明按 tol 验收却磨满 30 轮。修法:
|
||||
newton_project 增加 phys_exit2 参数(残差² < max(r_th2, phys_exit2)
|
||||
即返回),链温启动传 tol2;升级路径保持 0 = 全精度语义不变。
|
||||
3. **升级路径逐点重跑 24×24 全域网格**:global_search 的采样网格对同一
|
||||
曲面完全相同,129 点链 ~50 点满配升级 = 50×625 次重复求值,占该类
|
||||
链耗时 ~95%。修法:GridCache 链级缓存(首点升级时构建,后续只做 625
|
||||
次纯算术最近邻扫描),quick 与 multi-seed 共用;种子与逐点
|
||||
global_search 逐一相同(同网格同判据),投影语义不变。
|
||||
4. **病态参数化下温启动失效**(近退化带面 deg=2/1、u/v 域 [0,1],环点
|
||||
物理近但参数距离 10¹~10³,v 钉死 0.5、u 平滑漂移):warm 3/129、
|
||||
full 52/129。修法一:**链 UV 线性外推**——用前两点参数差预测下一点
|
||||
初值(周期方向取最短跨缝增量;非周期方向增量 > 域宽 10% 视为
|
||||
seam/pole 伪跳退回前一点),失效时退回前一点初值再试;改后同类链
|
||||
warm 79/129、full 22/129。修法二(保底):**UV 窗口重试**——首轮
|
||||
满配升级后仍 resid>tol 的点占比高时(nbad>3 且 8·nbad>n),用首轮
|
||||
全部 UV 构造外扩 25% 的窗口,全部搜索限制在窗口内重投影,逐点仍按
|
||||
resid≤tol 验收、两轮取残差小者(多种子语义不可裁剪,§7 教训)。
|
||||
|
||||
**语义守恒证据**:改前改后 [proj] 计数路径一致(warm/quick/full 判据
|
||||
未动)、验收门槛 resid≤tol 未动;test_brep_heal 41、test_trimmed_surface
|
||||
30、test_brep_validate 18、test_step_import 54、test_measure 33、
|
||||
test_analytic_ssi 13、test_brep_face_split 13、test_advanced_healing 31、
|
||||
test_ssi_boolean 51、test_brep_boolean 21、test_step_export 14、
|
||||
test_iges_import 27、test_iges_export 15 全绿。test_brep_modeling 的
|
||||
Fillet/Chamfer 2 例为既有失败(stash 后 HEAD 复现,与本轮无关)。
|
||||
|
||||
**遗留**:①sid=10 族 deg=2/11 高次面仅 1.4×(求值贵,Newton 迭代次数
|
||||
未降)——后续上解析导数(替代有限差分,11 次求值 → 3 次)再收;②
|
||||
OCC8228 全文件 --heal 总时长受同机负载干扰未取到干净数字,待独占复测。
|
||||
|
||||
@@ -112,10 +112,72 @@ PeriodFold detect_period(const NurbsSurface& surf) {
|
||||
return pf;
|
||||
}
|
||||
|
||||
/// UV 搜索窗口(病态链重试用):valid 时全局网格/多种子种子都限制在
|
||||
/// 窗口内——窗口由首轮真实投影点邻域构造,种子密度相对全域提高
|
||||
/// |domain|/|window| 倍,damping Newton 的种子可达性恢复。
|
||||
struct UVWindow {
|
||||
bool valid = false;
|
||||
double u_lo = 0, u_hi = 1, v_lo = 0, v_hi = 1;
|
||||
};
|
||||
|
||||
/// 全局搜索网格缓存(链级):global_search 的 24×24 采样点对同一曲面完全
|
||||
/// 相同,只有目标点在变。链式投影的升级路径逐点重跑全域网格(625 次曲面
|
||||
/// 求值/点),近退化带面(OCC8228 sid=1..11)129 点链 ~50 点满配升级时
|
||||
/// 网格求值占总耗时 ~95%。首点升级时构建一次,后续升级只做 625 次纯
|
||||
/// 算术最近邻扫描 + 局部 Newton——种子与逐点 global_search 逐一相同
|
||||
/// (同一网格、同一最近点判据),投影语义不变。
|
||||
struct GridCache {
|
||||
bool built = false;
|
||||
int res = 0; // 每轴采样段数(网格点数 = res+1)
|
||||
std::vector<double> us, vs; // res+1 个采样参数
|
||||
std::vector<core::Point3D> pts; // (res+1)² 个采样点 3D 坐标
|
||||
|
||||
void build(const NurbsSurface& surf, int res_u, int res_v) {
|
||||
const auto& ku = surf.knots_u();
|
||||
const auto& kv = surf.knots_v();
|
||||
const int pu = surf.degree_u(), pv = surf.degree_v();
|
||||
const double u_min = ku[pu], u_max = ku[ku.size() - 1 - pu];
|
||||
const double v_min = kv[pv], v_max = kv[kv.size() - 1 - pv];
|
||||
res = res_u;
|
||||
us.resize(res_u + 1);
|
||||
vs.resize(res_v + 1);
|
||||
pts.clear();
|
||||
pts.reserve(static_cast<size_t>(res_u + 1) * (res_v + 1));
|
||||
for (int i = 0; i <= res_u; ++i) {
|
||||
us[i] = u_min + (u_max - u_min) * static_cast<double>(i) / res_u;
|
||||
for (int j = 0; j <= res_v; ++j) {
|
||||
if (i == 0)
|
||||
vs[j] = v_min + (v_max - v_min) * static_cast<double>(j)
|
||||
/ res_v;
|
||||
pts.push_back(surf.evaluate(us[i], vs[j]));
|
||||
}
|
||||
}
|
||||
built = true;
|
||||
}
|
||||
|
||||
std::pair<double, double> best(const core::Point3D& target) const {
|
||||
double bd2 = std::numeric_limits<double>::max();
|
||||
size_t bi = 0;
|
||||
for (size_t k = 0; k < pts.size(); ++k) {
|
||||
const double dx = pts[k].x() - target.x();
|
||||
const double dy = pts[k].y() - target.y();
|
||||
const double dz = pts[k].z() - target.z();
|
||||
const double d2 = dx * dx + dy * dy + dz * dz;
|
||||
if (d2 < bd2) { bd2 = d2; bi = k; }
|
||||
}
|
||||
// pts 布局与 global_search 一致:u 外层、v 内层(严格小于比较,
|
||||
// 同距时先到的点胜出 → 与逐点 global_search 返回相同种子)。
|
||||
const size_t j = bi % vs.size();
|
||||
const size_t i = bi / vs.size();
|
||||
return { us[i], vs[j] };
|
||||
}
|
||||
};
|
||||
|
||||
/// 全局搜索:在采样网格上找距离 target 最近的点
|
||||
std::pair<double, double> global_search(const NurbsSurface& surf,
|
||||
const core::Point3D& target,
|
||||
int res_u = 20, int res_v = 20) {
|
||||
int res_u = 20, int res_v = 20,
|
||||
const UVWindow& w = UVWindow{}) {
|
||||
const auto& ku = surf.knots_u();
|
||||
const auto& kv = surf.knots_v();
|
||||
int pu = surf.degree_u();
|
||||
@@ -124,6 +186,13 @@ std::pair<double, double> global_search(const NurbsSurface& surf,
|
||||
double u_max = ku[ku.size() - 1 - pu];
|
||||
double v_min = kv[pv];
|
||||
double v_max = kv[kv.size() - 1 - pv];
|
||||
if (w.valid) {
|
||||
// 窗口钳制到参数域内(窗口由真实投影点邻域构造,必在域内)
|
||||
u_min = std::max(u_min, w.u_lo); u_max = std::min(u_max, w.u_hi);
|
||||
v_min = std::max(v_min, w.v_lo); v_max = std::min(v_max, w.v_hi);
|
||||
if (u_max <= u_min) { u_min = w.u_lo; u_max = w.u_hi; }
|
||||
if (v_max <= v_min) { v_min = w.v_lo; v_max = w.v_hi; }
|
||||
}
|
||||
|
||||
double best_dist = std::numeric_limits<double>::max();
|
||||
double best_u = u_min, best_v = v_min;
|
||||
@@ -149,12 +218,17 @@ std::pair<double, double> global_search(const NurbsSurface& surf,
|
||||
|
||||
/// Newton 迭代:找到曲面上离 target 最近的点
|
||||
/// 解方程: (S(u,v) - P) · Su(u,v) = 0, (S(u,v) - P) · Sv(u,v) = 0
|
||||
/// phys_exit2 > 0 时为调用方验收阈值(物理残差²):残差一旦低于该值即返回
|
||||
/// ——链式温启动按 tol 验收,磨到噪声底(1e-9·scale)只会白烧 30 轮迭代
|
||||
/// (OCC8228 sid=1 链 258 点全温启动仍 28s 首例)。升级路径(quick/
|
||||
/// multi-seed)保持 0 = 全精度语义不变。
|
||||
std::pair<double, double> newton_project(const NurbsSurface& surf,
|
||||
const core::Point3D& target,
|
||||
double u0, double v0,
|
||||
const PeriodFold& pf,
|
||||
int max_iter = 30,
|
||||
double tol = 1e-12) {
|
||||
double tol = 1e-12,
|
||||
double phys_exit2 = 0.0) {
|
||||
double u = u0, v = v0;
|
||||
|
||||
// 物理残差阈值随曲面尺度缩放:1e6 参数域曲面的求值噪声 ~1e-16×1e6 =
|
||||
@@ -174,6 +248,7 @@ std::pair<double, double> newton_project(const NurbsSurface& surf,
|
||||
}
|
||||
}
|
||||
const double r_th2 = 1e-18 * cp_scale * cp_scale;
|
||||
const double exit2 = std::max(r_th2, phys_exit2);
|
||||
|
||||
for (int iter = 0; iter < max_iter; ++iter) {
|
||||
core::Point3D S = pf.eval(surf, u, v);
|
||||
@@ -188,7 +263,7 @@ std::pair<double, double> newton_project(const NurbsSurface& surf,
|
||||
double rdx = S.x() - target.x();
|
||||
double rdy = S.y() - target.y();
|
||||
double rdz = S.z() - target.z();
|
||||
if (rdx * rdx + rdy * rdy + rdz * rdz < r_th2)
|
||||
if (rdx * rdx + rdy * rdy + rdz * rdz < exit2)
|
||||
break;
|
||||
}
|
||||
core::Vector3D Su = pf.eval_du(surf, u, v);
|
||||
@@ -273,9 +348,17 @@ std::pair<double, double> newton_project(const NurbsSurface& surf,
|
||||
/// 多起点投影:尝试多个种子,返回最接近的
|
||||
std::pair<double, double> multi_seed_project(const NurbsSurface& surf,
|
||||
const core::Point3D& target,
|
||||
int seeds = 5) {
|
||||
// 先全局搜索找最佳种子
|
||||
auto [u_best, v_best] = global_search(surf, target, seeds * 4, seeds * 4);
|
||||
int seeds = 5,
|
||||
const UVWindow& w = UVWindow{},
|
||||
GridCache* cache = nullptr) {
|
||||
// 先全局搜索找最佳种子(链式调用传缓存:网格求值只做一次/链)
|
||||
std::pair<double, double> best_seed;
|
||||
if (cache && !w.valid && cache->built && cache->res == seeds * 4) {
|
||||
best_seed = cache->best(target);
|
||||
} else {
|
||||
best_seed = global_search(surf, target, seeds * 4, seeds * 4, w);
|
||||
}
|
||||
const auto [u_best, v_best] = best_seed;
|
||||
|
||||
const PeriodFold pf = detect_period(surf);
|
||||
|
||||
@@ -295,6 +378,12 @@ std::pair<double, double> multi_seed_project(const NurbsSurface& surf,
|
||||
int pv = surf.degree_v();
|
||||
double u_min = ku[pu], u_max = ku[ku.size()-1-pu];
|
||||
double v_min = kv[pv], v_max = kv[kv.size()-1-pv];
|
||||
if (w.valid) {
|
||||
u_min = std::max(u_min, w.u_lo); u_max = std::min(u_max, w.u_hi);
|
||||
v_min = std::max(v_min, w.v_lo); v_max = std::min(v_max, w.v_hi);
|
||||
if (u_max <= u_min) { u_min = w.u_lo; u_max = w.u_hi; }
|
||||
if (v_max <= v_min) { v_min = w.v_lo; v_max = w.v_hi; }
|
||||
}
|
||||
|
||||
for (int i = 0; i < seeds; ++i) {
|
||||
double su = u_min + (u_max - u_min) * static_cast<double>(i) / (seeds - 1);
|
||||
@@ -385,11 +474,26 @@ bool surface_contains_point_fast(const core::Point3D& point,
|
||||
}
|
||||
|
||||
std::pair<double, double> project_point_on_surface_quick(
|
||||
const core::Point3D& point, const NurbsSurface& surface) {
|
||||
const core::Point3D& point, const NurbsSurface& surface,
|
||||
const UVWindow& w, GridCache* cache) {
|
||||
// 与 project_point_on_surface 相同的"全局网格最佳种子 + 局部 Newton",
|
||||
// 但省去 5×5 种子复核精化 → 单次调用约一个量级更快。
|
||||
auto [ub, vb] = global_search(surface, point, 24, 24);
|
||||
return newton_project(surface, point, ub, vb, detect_period(surface));
|
||||
std::pair<double, double> seed;
|
||||
if (cache && !w.valid) {
|
||||
if (!cache->built || cache->res != 24)
|
||||
cache->build(surface, 24, 24);
|
||||
seed = cache->best(point);
|
||||
} else {
|
||||
seed = global_search(surface, point, 24, 24, w);
|
||||
}
|
||||
return newton_project(surface, point, seed.first, seed.second,
|
||||
detect_period(surface));
|
||||
}
|
||||
|
||||
std::pair<double, double> project_point_on_surface_quick(
|
||||
const core::Point3D& point, const NurbsSurface& surface) {
|
||||
return project_point_on_surface_quick(point, surface, UVWindow{},
|
||||
nullptr);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════
|
||||
@@ -420,16 +524,66 @@ std::vector<std::pair<double, double>> project_polyline_on_surface(
|
||||
const double tol2 = tol * tol;
|
||||
long c1 = 0, c2 = 0, c3 = 0, cforce = 0;
|
||||
const PeriodFold pf = detect_period(surface);
|
||||
GridCache cache; // 链级全局网格缓存(升级路径共用,见下)
|
||||
std::vector<double> resid; // 每点 3D 残差²(病态链窗口重试判据)
|
||||
resid.reserve(n);
|
||||
if (std::getenv("VDE_TRACE_PROJ_DETAIL")) {
|
||||
const auto& kku = surface.knots_u();
|
||||
const auto& kkv = surface.knots_v();
|
||||
const int ppu = surface.degree_u(), ppv = surface.degree_v();
|
||||
std::fprintf(stderr,
|
||||
"[projd] n=%zu dom u=[%.6g..%.6g] v=[%.6g..%.6g] "
|
||||
"deg=%d/%d per=%d%d\n",
|
||||
n, kku[ppu], kku[kku.size() - 1 - ppu], kkv[ppv],
|
||||
kkv[kkv.size() - 1 - ppv], ppu, ppv,
|
||||
(int)pf.u_per, (int)pf.v_per);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
// 1) 链式温启动:以前一点已投影参数为初值做局部 Newton
|
||||
// 1) 链式温启动:沿链方向线性外推初值做局部 Newton。
|
||||
// 大 v 域 + 小 |Sv| 参数化(OCC8228 sid=1..11 族)下相邻环点
|
||||
// 物理邻近但参数距离可达 10¹~10³:以前一点 UV 为初值时 damping
|
||||
// Newton(步长 ≤0.5、≤30 轮)走不完该距离——要么打满迭代
|
||||
// (每点 ~30×6 次曲面求值,单链 258 点 22s),要么残差超 tol
|
||||
// 逐点满配升级(每次 multi-seed ~7k 次求值,单链 129 点 30s+)。
|
||||
// 链点参数随弧长近似匀速推进,用前两点参数差线性外推直接落在
|
||||
// 目标邻域,Newton 1~3 步收敛。周期方向增量取最短跨缝表示;
|
||||
// 非周期方向增量异常大(seam/pole 伪跳,>域宽 10%)时该增量
|
||||
// 不可信,退回前一点初值。
|
||||
if (i > 0) {
|
||||
auto [cu, cv] = newton_project(surface, pts[i],
|
||||
out[i - 1].first,
|
||||
out[i - 1].second, pf);
|
||||
double u0 = out[i - 1].first, v0 = out[i - 1].second;
|
||||
if (i > 1) {
|
||||
double du = out[i - 1].first - out[i - 2].first;
|
||||
double dv = out[i - 1].second - out[i - 2].second;
|
||||
if (pf.u_per && pf.u_hi > pf.u_lo) {
|
||||
const double p = pf.u_hi - pf.u_lo;
|
||||
du -= std::round(du / p) * p;
|
||||
}
|
||||
if (pf.v_per && pf.v_hi > pf.v_lo) {
|
||||
const double p = pf.v_hi - pf.v_lo;
|
||||
dv -= std::round(dv / p) * p;
|
||||
}
|
||||
if (pf.u_per || std::abs(du) < 0.1 * (pf.u_hi - pf.u_lo))
|
||||
u0 = out[i - 1].first + du;
|
||||
if (pf.v_per || std::abs(dv) < 0.1 * (pf.v_hi - pf.v_lo))
|
||||
v0 = out[i - 1].second + dv;
|
||||
}
|
||||
auto [cu, cv] = newton_project(surface, pts[i], u0, v0, pf,
|
||||
30, 1e-12, tol2);
|
||||
double r2 = resid2(cu, cv, pts[i]);
|
||||
if (r2 > tol2 && i > 1) {
|
||||
// 外推失手(环转折/伪跳):退回前一点初值再试一次,
|
||||
// 仍失败则走下方升级路径,语义与原实现一致。
|
||||
auto [cu2, cv2] = newton_project(surface, pts[i],
|
||||
out[i - 1].first,
|
||||
out[i - 1].second, pf,
|
||||
30, 1e-12, tol2);
|
||||
double r22 = resid2(cu2, cv2, pts[i]);
|
||||
if (r22 < r2) { cu = cu2; cv = cv2; r2 = r22; }
|
||||
}
|
||||
if (r2 <= tol2) {
|
||||
++c1;
|
||||
resid.push_back(r2);
|
||||
out.emplace_back(cu, cv);
|
||||
continue;
|
||||
}
|
||||
@@ -440,17 +594,92 @@ std::vector<std::pair<double, double>> project_polyline_on_surface(
|
||||
i, cu, cv, std::sqrt(r2), tol);
|
||||
}
|
||||
}
|
||||
// 2) 升级:全局网格最佳种子 + 单次 Newton
|
||||
auto [qu, qv] = project_point_on_surface_quick(pts[i], surface);
|
||||
// 2) 升级:全局网格最佳种子 + 单次 Newton(网格缓存链级复用)
|
||||
auto [qu, qv] = project_point_on_surface_quick(pts[i], surface,
|
||||
UVWindow{}, &cache);
|
||||
if (std::getenv("VDE_TRACE_PROJ_DETAIL") && cforce < 5) {
|
||||
std::fprintf(stderr,
|
||||
"[projq] i=%zu qu=%.6f qv=%.6f resid=%.3e\n",
|
||||
i, qu, qv, std::sqrt(resid2(qu, qv, pts[i])));
|
||||
}
|
||||
if (resid2(qu, qv, pts[i]) <= tol2) {
|
||||
++c2;
|
||||
resid.push_back(resid2(qu, qv, pts[i]));
|
||||
out.emplace_back(qu, qv);
|
||||
continue;
|
||||
}
|
||||
// 3) 兜底:全精度多种子复核(与 project_point_on_surface 一致)
|
||||
++c3;
|
||||
out.push_back(multi_seed_project(surface, pts[i], 6));
|
||||
out.push_back(multi_seed_project(surface, pts[i], 6, UVWindow{},
|
||||
&cache));
|
||||
resid.push_back(resid2(out.back().first, out.back().second, pts[i]));
|
||||
}
|
||||
|
||||
// ── 病态链 UV 窗口重试(VDE-1193 §9.11.6)──
|
||||
// 满配升级后仍 resid>tol 的点占比高(大 v 域 + 小 |Sv| 参数化:全域
|
||||
// 种子间物理距离近、damping Newton 步长受限到不了位——OCC8228
|
||||
// sid=28/29/77 首例,单链 13-19s)。用首轮全部 UV(好坏点都在真实
|
||||
// 裁剪环邻域)构造 UV 窗口,全部搜索限制在窗口内重投影——种子密度
|
||||
// 相对提高 |domain|/|window| 倍。逐点仍按 resid≤tol 验收、两轮取残差
|
||||
// 小者:多种子语义不可裁剪(bug24593 教训见上),此处只是把搜索约束
|
||||
// 到有据可依的窗口,不降低任何点的验收标准。
|
||||
size_t nbad = 0;
|
||||
for (double r2 : resid)
|
||||
if (r2 > tol2) ++nbad;
|
||||
if (nbad > 3 && nbad * 8 > n) {
|
||||
double ulo = 1e300, uhi = -1e300, vlo = 1e300, vhi = -1e300;
|
||||
for (const auto& uv : out) {
|
||||
ulo = std::min(ulo, uv.first); uhi = std::max(uhi, uv.first);
|
||||
vlo = std::min(vlo, uv.second); vhi = std::max(vhi, uv.second);
|
||||
}
|
||||
const auto& ku = surface.knots_u();
|
||||
const auto& kv = surface.knots_v();
|
||||
const int pu = surface.degree_u(), pv = surface.degree_v();
|
||||
const double du_dom = ku[ku.size() - 1 - pu] - ku[pu];
|
||||
const double dv_dom = kv[kv.size() - 1 - pv] - kv[pv];
|
||||
const double wu = uhi - ulo, wv = vhi - vlo;
|
||||
// 窗口外扩 25%;两轴都缩不下来(窗口≈全域)则重试无收益,跳过
|
||||
if (wu < 0.6 * du_dom || wv < 0.6 * dv_dom) {
|
||||
UVWindow w;
|
||||
w.u_lo = ulo - 0.25 * wu; w.u_hi = uhi + 0.25 * wu;
|
||||
w.v_lo = vlo - 0.25 * wv; w.v_hi = vhi + 0.25 * wv;
|
||||
w.valid = true;
|
||||
long w1 = 0, w2 = 0, w3 = 0;
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
auto best = out[i];
|
||||
double best_r2 = resid[i];
|
||||
auto accept = [&](const std::pair<double, double>& uv) {
|
||||
const double r2 = resid2(uv.first, uv.second, pts[i]);
|
||||
if (r2 < best_r2) { best_r2 = r2; best = uv; }
|
||||
};
|
||||
// 窗口内温启动 Newton(初值取首轮同点 UV)
|
||||
{
|
||||
auto [cu, cv] = newton_project(surface, pts[i],
|
||||
out[i].first,
|
||||
out[i].second, pf);
|
||||
if (resid2(cu, cv, pts[i]) <= tol2) { ++w1; accept({cu, cv}); }
|
||||
}
|
||||
if (best_r2 > tol2) {
|
||||
auto [qu, qv] = project_point_on_surface_quick(
|
||||
pts[i], surface, w, nullptr);
|
||||
if (resid2(qu, qv, pts[i]) <= tol2) { ++w2; accept({qu, qv}); }
|
||||
}
|
||||
if (best_r2 > tol2) {
|
||||
++w3;
|
||||
accept(multi_seed_project(surface, pts[i], 6, w));
|
||||
}
|
||||
if (best.first != out[i].first || best.second != out[i].second)
|
||||
out[i] = best;
|
||||
}
|
||||
if (std::getenv("VDE_TRACE_PROJ"))
|
||||
std::fprintf(stderr,
|
||||
"[proj] window-retry n=%zu bad=%zu win=(%.3g..%.3g)"
|
||||
"x(%.3g..%.3g) warm=%ld quick=%ld full=%ld\n",
|
||||
n, nbad, w.u_lo, w.u_hi, w.v_lo, w.v_hi,
|
||||
w1, w2, w3);
|
||||
}
|
||||
}
|
||||
|
||||
if (std::getenv("VDE_TRACE_PROJ"))
|
||||
std::fprintf(stderr,
|
||||
"[proj] n=%zu warm=%ld quick=%ld full=%ld tol=%.3e\n", n,
|
||||
|
||||
@@ -11,6 +11,57 @@
|
||||
|
||||
namespace vde::curves {
|
||||
|
||||
namespace {
|
||||
|
||||
// 供 NurbsSurface::evaluate 使用的轻量 span/基函数计算(与
|
||||
// BSplineCurve::find_span/basis_functions 同一算法,但不构造
|
||||
// BSplineCurve 临时对象——构造会拷贝整条节点向量并分配哑控制点
|
||||
// 数组,投影热循环每轮 11 次求值 × 每次两份拷贝成为热点)。
|
||||
int surf_span(const std::vector<double>& knots, int degree, int n_cp,
|
||||
double t) {
|
||||
const int n = n_cp - 1;
|
||||
if (t >= knots[n + 1]) return n;
|
||||
if (t <= knots[degree]) return degree;
|
||||
int low = degree, high = n + 1;
|
||||
int mid = (low + high) / 2;
|
||||
while (t < knots[mid] || t >= knots[mid + 1]) {
|
||||
if (t < knots[mid]) high = mid;
|
||||
else low = mid;
|
||||
mid = (low + high) / 2;
|
||||
}
|
||||
return mid;
|
||||
}
|
||||
|
||||
void surf_basis(const std::vector<double>& knots, int degree, int n_cp,
|
||||
double t, int span, double* N) {
|
||||
const int n = n_cp - 1;
|
||||
N[0] = 1.0;
|
||||
double left[33], right[33];
|
||||
const int d = degree <= 32 ? degree : 32; // 实际 CAD 阶次 ≤ 11;防御截断
|
||||
for (int j = 1; j <= d; ++j) {
|
||||
left[j] = t - knots[span + 1 - j];
|
||||
right[j] = knots[span + j] - t;
|
||||
double saved = 0.0;
|
||||
for (int r = 0; r < j; ++r) {
|
||||
double denom = right[r + 1] + left[j - r];
|
||||
double temp = (std::abs(denom) < 1e-15) ? 0.0 : N[r] / denom;
|
||||
N[r] = saved + right[r + 1] * temp;
|
||||
saved = left[j - r] * temp;
|
||||
}
|
||||
N[j] = saved;
|
||||
}
|
||||
// 与 BSplineCurve::basis_functions 相同的右端点修正:t 恰为末节点时
|
||||
// 递推可能全零,末基函数应为 1。
|
||||
if (span == n) {
|
||||
double sum = 0.0;
|
||||
for (int i = 0; i <= d; ++i) sum += N[i];
|
||||
if (std::abs(sum) < 1e-15) N[d] = 1.0;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
NurbsSurface::NurbsSurface(std::vector<std::vector<Point3D>> grid,
|
||||
std::vector<double> ku, std::vector<double> kv,
|
||||
std::vector<std::vector<double>> w, int du, int dv)
|
||||
@@ -49,6 +100,49 @@ Point3D NurbsSurface::evaluate(double u, double v) const {
|
||||
if (v < v_lo) v = v_lo; else if (v > v_hi) v = v_hi;
|
||||
}
|
||||
|
||||
// 张量积有理 B 样条:S(u,v) = ΣΣ Nu[i]·Nv[j]·w_ij·(P_ij, w_ij),
|
||||
// 双重求和直接展开(与"先 v 后 u 的齐次行组合再除权"数学等价),
|
||||
// 但每次求值只做 O(deg_u·deg_v) 次乘加、零堆分配——原实现的
|
||||
// row_h 向量(cp_.size() 个元素)+ 两个 BSplineCurve 临时对象
|
||||
// (各拷贝整条节点向量)在投影热循环(每 Newton 轮 11 次求值)
|
||||
// 中占掉单次求值 ~0.3ms 的绝大部分(OCC8228 sid=1..11 单面
|
||||
// project_chain 28-38s 首例)。
|
||||
if (degree_u_ <= 63 && degree_v_ <= 63) {
|
||||
const int n_u = static_cast<int>(cp_.size());
|
||||
const int span_u = surf_span(knots_u_, degree_u_, n_u, u);
|
||||
const int span_v = surf_span(knots_v_, degree_v_, nv, v);
|
||||
double Nu[64], Nv[64];
|
||||
surf_basis(knots_u_, degree_u_, n_u, u, span_u, Nu);
|
||||
surf_basis(knots_v_, degree_v_, nv, v, span_v, Nv);
|
||||
|
||||
Eigen::Vector4d acc(0, 0, 0, 0);
|
||||
for (int i = 0; i <= degree_u_; ++i) {
|
||||
const int iu = span_u - degree_u_ + i;
|
||||
if (iu < 0 || iu >= n_u) continue;
|
||||
const auto& row = cp_[iu];
|
||||
const std::vector<double>* wrow =
|
||||
(iu < static_cast<int>(weights_.size())) ? &weights_[iu]
|
||||
: nullptr;
|
||||
const double au = Nu[i];
|
||||
for (int j = 0; j <= degree_v_; ++j) {
|
||||
const int iv = span_v - degree_v_ + j;
|
||||
if (iv < 0 || iv >= nv) continue;
|
||||
const double wj = (wrow && iv < static_cast<int>(wrow->size()))
|
||||
? (*wrow)[iv] : 1.0;
|
||||
const double a = au * Nv[j] * wj;
|
||||
acc.x() += a * row[iv].x();
|
||||
acc.y() += a * row[iv].y();
|
||||
acc.z() += a * row[iv].z();
|
||||
acc.w() += a;
|
||||
}
|
||||
}
|
||||
if (std::abs(acc.w()) < 1e-12)
|
||||
return Point3D(0, 0, 0);
|
||||
return Point3D(acc.x() / acc.w(), acc.y() / acc.w(),
|
||||
acc.z() / acc.w());
|
||||
}
|
||||
|
||||
// 防御兜底:阶次 > 63 的病态面走原实现(BSplineCurve 动态向量)。
|
||||
// 先沿 v 方向求每行控制点的有理(齐次)B-spline 组合
|
||||
int nv_pts = static_cast<int>(knots_v_.size()) - degree_v_ - 1;
|
||||
BSplineCurve bs_v(std::vector<Point3D>(nv_pts > 0 ? nv_pts : 0), knots_v_, degree_v_);
|
||||
|
||||
Reference in New Issue
Block a user