- fit_surface_to_grid: chord parameterization, tensor-product LSQ (separable QR solve), closest-point error, per-row/col parameter correction, alternating control-point growth; aligns N_FitSrfApproxTol - measured: bicubic polynomial 9x9 tol 1e-6 -> dev 3.589e-7 CP 8x7; sphere octant 11x11 tol 1e-3 -> dev 7.548e-4 CP 6x5 - degenerate grid / unreachable tolerance return ok=false with best fit and error - A2-5 (knot-removal error upper bound) deferred: measured-deviation acceptance already prevents degradation - tests: test_smlib_nurbs_alignment 22/22 (+4); target subset 659/659 (32.21 s) - version 1.0.404; report 463; plan A2-2 done, A2-5 deferred
79 lines
3.3 KiB
C++
79 lines
3.3 KiB
C++
#pragma once
|
||
/**
|
||
* @file surface_fitting.h
|
||
* @brief 点网格 → NURBS 曲面的容差驱动拟合(SMLib N_FitSrfApproxTol 对齐)
|
||
*
|
||
* 对齐 SMLib `NL_SrfFit` 的"按容差自动定规模"主循环:
|
||
* 参数化 → 张量积最小二乘 → 最近点误差 → 参数修正 → 扩控制点,
|
||
* 直到达到容差或控制点数上限。
|
||
*
|
||
* 与 `vde::mesh::fit_nurbs_least_squares`(点云、固定基、单次 LSQ)不同,
|
||
* 本模块面向**结构化点网格**(扫描/放样/网格采样),带参数修正与
|
||
* 控制点数自适应,且误差以点到曲面的最近点距离度量。
|
||
*
|
||
* @ingroup curves
|
||
*/
|
||
#include "vde/curves/nurbs_surface.h"
|
||
#include "vde/core/point.h"
|
||
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
namespace vde::curves {
|
||
|
||
/**
|
||
* @brief 曲面拟合结果
|
||
*/
|
||
struct SurfaceFitResult {
|
||
bool ok = false; ///< 是否达到 tolerance
|
||
NurbsSurface surface; ///< 最佳拟合曲面(未达标时为当前最佳)
|
||
double max_deviation = 0.0; ///< 网格点到曲面的最大最近点距离
|
||
double tolerance = 0.0; ///< 请求容差
|
||
int control_points_u = 0; ///< 最终 u 向控制点数
|
||
int control_points_v = 0; ///< 最终 v 向控制点数
|
||
int iterations = 0; ///< 参数修正迭代总次数
|
||
std::string error; ///< 未达标原因(ok=false 时非空)
|
||
};
|
||
|
||
/**
|
||
* @brief 用结构化点网格拟合 NURBS 曲面(容差闭环)
|
||
*
|
||
* @param grid 点网格:`grid[i][j]` = (u_i, v_j) 处采样点;
|
||
* 行数/列数 ≥ 2,各行长度一致
|
||
* @param tolerance 允许的最大最近点距离(>0)
|
||
* @param degree_u u 向阶次(默认 3;会被点网格尺寸与上限收敛)
|
||
* @param degree_v v 向阶次(默认 3)
|
||
* @param max_control_points 单方向控制点数上限(0 = 不额外限制,不超过采样点数)
|
||
* @param max_iterations 每个规模下的参数修正轮数上限(默认 6)
|
||
* @return 拟合结果(ok / surface / max_deviation / 控制点数 / iterations / error)
|
||
*
|
||
* 算法:
|
||
* ```text
|
||
* 参数化:u 向 = 相邻行平均弦长归一;v 向同理
|
||
* ncu = min(nu, du+1), ncv = min(nv, dv+1)
|
||
* while 未达标:
|
||
* 张量积 LSQ(uniform clamped 节点;法方程 + 微小 ridge)
|
||
* 误差 = max_{i,j} dist(P_ij, S)(最近点)
|
||
* 参数修正(最多 max_iterations 轮):
|
||
* u_i = mean_j u*_ij, v_j = mean_i v*_ij(单调化)
|
||
* 同规模重拟合;误差严格下降才接受
|
||
* 未达标 -> ncu/ncv 交替 +1(到上限为止)
|
||
* ```
|
||
*
|
||
* @note 网格点顺序必须与参数方向一致(i 对应 u 增大、j 对应 v 增大)。
|
||
* 输入退化(空行/尺寸不足/共线)由调用方保证;本函数返回 ok=false。
|
||
* @note 拟合使用多项式张量积基(权重全 1);有理曲面(球/环)以容差内
|
||
* 多项式逼近,非线性误差随控制点数收敛。
|
||
* @code{.cpp}
|
||
* auto r = fit_surface_to_grid(points, 1e-3, 3, 3, 8);
|
||
* if (r.ok) use(r.surface);
|
||
* @endcode
|
||
* @ingroup curves
|
||
*/
|
||
[[nodiscard]] SurfaceFitResult fit_surface_to_grid(
|
||
const std::vector<std::vector<core::Point3D>>& grid, double tolerance,
|
||
int degree_u = 3, int degree_v = 3, int max_control_points = 0,
|
||
int max_iterations = 6);
|
||
|
||
} // namespace vde::curves
|