Files
ViewDesignEngine/tests/curves/test_nurbs_certification.cpp
hm e9e63833af test(curves): random rational surface first/second derivative certification (VDE-1431)
新增 NurbsCertification.RandomRationalSurfaceDerivativesMatchFiniteDifferences:40 个随机有理曲面(deg 2-5、权 0.3-3),一阶 du/dv rel 4.34e-10、二阶 duu/duv/dvv rel 1.20e-9(阈值 1e-5)。定向 1153/1153、全量 8604/0;注册 8,609;报告 293;版本 1.0.235。
2026-11-10 14:14:30 +08:00

340 lines
14 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// NURBS 认证套件(VDE-1379)—— 对照 analytic 几何锁定工厂曲面/投影/三角化的精度与安全性。
//
// 覆盖计划文档 production-exact-brep-nurbs.md §6 P2-1「NURBS 认证套件」:
// · 球/环/柱/锥/平面工厂曲面位于闭式解上;
// · 极端尺度 1e-9 / 1e9 的相对精度;
// · 球极点单侧极限法向;
// · 解析导数 vs 中心差分;
// · 投影/反投影与 p-curve 往返精度;
// · 三角化顶点贴面与零面积三角形剔除;
// · is_closed / bbox 一致性。
#include <gtest/gtest.h>
#include <cmath>
#include <random>
#include <limits>
#include <vector>
#include "vde/core/point.h"
#include "vde/curves/curve_surface_projection.h"
#include "vde/curves/nurbs_curve.h"
#include "vde/curves/nurbs_surface.h"
using namespace vde;
using namespace vde::curves;
using namespace vde::core;
namespace {
constexpr double kPi = 3.14159265358979323846;
double rel_err(double a, double b) {
return std::abs(a - b) / std::max(1.0, std::abs(b));
}
} // namespace
TEST(NurbsCertification, FactorySurfacesLieOnAnalyticGeometry) {
// 球面:所有采样点半径 == r。
{
const double r = 5.0;
const NurbsSurface s = NurbsSurface::sphere(1.0, -2.0, 3.0, r);
for (int i = 0; i <= 8; ++i)
for (int j = 0; j <= 8; ++j) {
const Point3D p = s.evaluate(i / 8.0, j / 8.0);
EXPECT_NEAR((p - Point3D(1, -2, 3)).norm(), r, 1e-9)
<< "uv=" << i << "," << j;
}
}
// 环面:(sqrt(x²+y²)-R)² + z² == r²。
{
const double R = 5.0, r = 2.0;
const NurbsSurface t = NurbsSurface::torus(0, 0, 0, R, r);
for (int i = 0; i <= 16; ++i)
for (int j = 0; j <= 16; ++j) {
const Point3D p = t.evaluate(i / 16.0, j / 16.0);
const double rho = std::hypot(p.x(), p.y());
EXPECT_NEAR(std::hypot(rho - R, p.z()), r, 1e-9);
}
}
// 圆柱:(sqrt(x²+y²)==R,0<=z<=h)。
{
const double R = 3.0, h = 10.0;
const NurbsSurface c = NurbsSurface::cylinder(0, 0, 0, R, h);
for (int i = 0; i <= 8; ++i)
for (int j = 0; j <= 8; ++j) {
const Point3D p = c.evaluate(i / 8.0, j / 8.0);
EXPECT_NEAR(std::hypot(p.x(), p.y()), R, 1e-9);
// 工厂约定:以 cz 为中心,向 ±h/2 延伸。
EXPECT_GE(p.z(), -h / 2 - 1e-9);
EXPECT_LE(p.z(), h / 2 + 1e-9);
}
}
// 圆锥台:(rho - (rb + (rt-rb) z/h)) == 0。
{
const double rb = 4.0, rt = 2.0, h = 6.0;
const NurbsSurface c = NurbsSurface::cone(0, 0, 0, rb, rt, h);
for (int i = 0; i <= 8; ++i)
for (int j = 0; j <= 8; ++j) {
const Point3D p = c.evaluate(i / 8.0, j / 8.0);
// 工厂约定:z 以 cz 为中心 ∈[-h/2,h/2],底面半径 rb、顶面 rt。
const double z = std::clamp(p.z(), -h / 2, h / 2);
EXPECT_GE(p.z(), -h / 2 - 1e-9);
EXPECT_LE(p.z(), h / 2 + 1e-9);
EXPECT_NEAR(std::hypot(p.x(), p.y()),
rb + (rt - rb) * (z + h / 2) / h, 1e-7)
<< "uv=" << i << "," << j << " z=" << p.z();
}
}
// 平面:z == 0(宽高 10×10)。
{
const NurbsSurface pl = NurbsSurface::plane(
Point3D(0, 0, 0), Vector3D(1, 0, 0), Vector3D(0, 1, 0), 10, 10);
for (int i = 0; i <= 4; ++i)
for (int j = 0; j <= 4; ++j) {
const Point3D p = pl.evaluate(i / 4.0, j / 4.0);
EXPECT_NEAR(p.z(), 0.0, 1e-12);
EXPECT_GE(p.x(), -1e-9);
EXPECT_LE(p.x(), 10 + 1e-9);
}
}
}
TEST(NurbsCertification, ExtremeScalesKeepRelativeAccuracy) {
for (double r : {1e-9, 1.0, 1e9}) {
const NurbsSurface s = NurbsSurface::sphere(0, 0, 0, r);
double max_radial = 0.0, max_normal = 0.0;
for (int i = 0; i <= 8; ++i)
for (int j = 0; j <= 8; ++j) {
max_radial = std::max(
max_radial, std::abs(s.evaluate(i / 8.0, j / 8.0).norm() - r) / r);
max_normal = std::max(
max_normal, std::abs(s.normal(i / 8.0, j / 8.0).norm() - 1.0));
}
EXPECT_LT(max_radial, 1e-12) << "r=" << r;
EXPECT_LT(max_normal, 1e-12) << "r=" << r;
const auto t = s.tessellate_checked(8, 8);
EXPECT_TRUE(t.ok) << "r=" << r;
for (const auto& p : t.vertices)
EXPECT_TRUE(std::isfinite(p.x()) && std::isfinite(p.y()) &&
std::isfinite(p.z()));
}
}
TEST(NurbsCertification, SpherePoleNormalsAreLimitingNormals) {
const double r = 2.0;
const NurbsSurface s = NurbsSurface::sphere(0, 0, 0, r);
for (double v : {0.0, 1.0}) {
for (double u : {0.0, 0.25, 0.5, 0.75}) {
const Vector3D n = s.normal(u, v);
EXPECT_NEAR(n.norm(), 1.0, 1e-9);
const Point3D p = s.evaluate(u, v);
const double dot = n.dot(p) / (p.norm() > 0 ? p.norm() : 1.0);
// 球面外法向在极点取径向:两侧都应与 p/|p| 同向(+1)。
EXPECT_NEAR(dot, 1.0, 1e-6) << "u=" << u << " v=" << v;
}
}
}
TEST(NurbsCertification, AnalyticDerivativesMatchFiniteDifferences) {
const NurbsSurface sphere = NurbsSurface::sphere(0, 0, 0, 2.0);
const NurbsSurface torus = NurbsSurface::torus(0, 0, 0, 5.0, 2.0);
const double h = 1e-6;
for (const NurbsSurface* s : {&sphere, &torus}) {
for (int i = 1; i <= 4; ++i)
for (int j = 1; j <= 4; ++j) {
const double u = i / 5.0, v = j / 5.0;
const Vector3D su = s->derivative_u(u, v);
const Vector3D fd_u =
(s->evaluate(u + h, v) - s->evaluate(u - h, v)) / (2 * h);
const Vector3D sv = s->derivative_v(u, v);
const Vector3D fd_v =
(s->evaluate(u, v + h) - s->evaluate(u, v - h)) / (2 * h);
EXPECT_LT((su - fd_u).norm() / std::max(1.0, su.norm()), 1e-4)
<< "Su uv=" << u << "," << v;
EXPECT_LT((sv - fd_v).norm() / std::max(1.0, sv.norm()), 1e-4)
<< "Sv uv=" << u << "," << v;
}
}
}
TEST(NurbsCertification, ClosestPointProjectsBackToSurface) {
const double r = 3.0;
const NurbsSurface s = NurbsSurface::sphere(1, 2, 3, r);
const Point3D queries[] = {Point3D(10, 2, 3), Point3D(1, 2, 20),
Point3D(-5, -4, 1), Point3D(1, 2, 3)};
for (const auto& q : queries) {
const Point3D p = s.closest_point(q);
EXPECT_NEAR((p - Point3D(1, 2, 3)).norm(), r, 1e-7) << q.transpose();
EXPECT_NEAR(s.distance_to(q), (p - q).norm(), 1e-9);
EXPECT_TRUE(std::isfinite(s.distance_to(q)));
}
}
TEST(NurbsCertification, ProjectedLineOnPlaneRoundTrips) {
const NurbsSurface pl = NurbsSurface::plane(
Point3D(0, 0, 0), Vector3D(1, 0, 0), Vector3D(0, 1, 0), 10, 10);
const NurbsCurve line({Point3D(0, 0, 0), Point3D(5, 5, 0)}, {0, 0, 1, 1},
{1, 1}, 1);
const NurbsCurve pc = project_curve_on_surface(line, pl, 64);
ASSERT_GT(pc.control_points().size(), 1u);
// p-curve 采样点反投影回平面:x≈u、y≈v(平面参数域 [0,1] 对应 10×10 区域)。
const auto pts = evaluate_pcurve_on_surface(pc, pl, 32);
ASSERT_EQ(pts.size(), 32u);
for (const auto& p : pts) {
EXPECT_TRUE(std::isfinite(p.x()) && std::isfinite(p.y()));
EXPECT_NEAR(p.z(), 0.0, 1e-9);
// 投影点应落在线段附近(x≈y,且在 [0,5])。
EXPECT_NEAR(p.x(), p.y(), 1e-6) << "p=" << p.transpose();
EXPECT_GE(p.x(), -1e-6);
EXPECT_LE(p.x(), 5.0 + 1e-6);
}
}
TEST(NurbsCertification, TessellationStaysOnSurfaceAndFiltersDegenerate) {
const double r = 2.0;
const NurbsSurface s = NurbsSurface::sphere(0, 0, 0, r);
const auto t = s.tessellate_checked(16, 8);
ASSERT_TRUE(t.ok);
EXPECT_GT(t.triangles.size(), 0u);
EXPECT_LE(t.degenerate_triangles, t.total_triangles);
for (const auto& p : t.vertices) {
EXPECT_NEAR(p.norm(), r, 1e-9);
EXPECT_TRUE(std::isfinite(p.x()));
}
// 参数方向塌缩的退化 bilinear patch:零面积三角形必须被剔除(VDE-1240)。
std::vector<std::vector<Point3D>> deg = {
{Point3D(0, 0, 0), Point3D(0, 0, 0)},
{Point3D(1, 0, 0), Point3D(1, 0, 0)}};
const NurbsSurface collapse(deg, {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
const auto td = collapse.tessellate_checked(8, 8);
ASSERT_TRUE(td.ok);
EXPECT_EQ(td.triangles.size(), 0u);
EXPECT_EQ(td.degenerate_triangles, td.total_triangles);
}
TEST(NurbsCertification, ClosednessAndBBoxAreConsistent) {
const NurbsSurface sphere = NurbsSurface::sphere(0, 0, 0, 2.0);
EXPECT_TRUE(sphere.is_closed(true)); // 经度方向整周闭合
EXPECT_FALSE(sphere.is_closed(false)); // 纬度方向两端为极点边界,不闭合
const NurbsSurface plane = NurbsSurface::plane(
Point3D(0, 0, 0), Vector3D(1, 0, 0), Vector3D(0, 1, 0), 4, 3);
EXPECT_FALSE(plane.is_closed(true));
EXPECT_FALSE(plane.is_closed(false));
const NurbsSurface cyl = NurbsSurface::cylinder(0, 0, 0, 2.0, 5.0);
EXPECT_TRUE(cyl.is_closed(true));
EXPECT_FALSE(cyl.is_closed(false));
const auto box = sphere.bbox();
for (int i = 0; i <= 16; ++i)
for (int j = 0; j <= 16; ++j) {
const Point3D p = sphere.evaluate(i / 16.0, j / 16.0);
for (int k = 0; k < 3; ++k) {
EXPECT_GE(p[k], box.min()[k] - 1e-12);
EXPECT_LE(p[k], box.max()[k] + 1e-12);
}
}
}
// VDE-1403:随机有理曲线(固定种子)一阶导数 vs 中心差分、二阶导数 vs 一阶差分;
// 阈值 1e-5 远大于实测 ~3.5e-9(差分噪声)。
// VDE-1431:随机有理曲面(deg 2..5、正权)一阶/二阶导数 vs 中心差分;
// 阈值 1e-5 远大于实测 ~1.2e-9(差分噪声)。
TEST(NurbsCertification, RandomRationalSurfaceDerivativesMatchFiniteDifferences) {
std::mt19937 rng(9901);
std::uniform_real_distribution<double> U(-3, 3), W(0.3, 3.0), T(0.02, 0.98);
auto knot_vec = [](int n, int p) {
std::vector<double> k(n + p + 1, 0.0);
for (size_t i = 0; i < k.size(); ++i) {
if (static_cast<int>(i) <= p) k[i] = 0.0;
else if (static_cast<int>(i) >= n) k[i] = 1.0;
else k[i] = static_cast<double>(static_cast<int>(i) - p) / (n - p);
}
return k;
};
auto rel = [](const Vector3D& a, const Vector3D& b) {
return (a - b).norm() / std::max(b.norm(), 1.0);
};
double worst1 = 0.0, worst2 = 0.0;
for (int iter = 0; iter < 40; ++iter) {
const int pu = 2 + (iter % 4); // 2..5
const int pv = 2 + ((iter / 4) % 4); // 2..5
const int nu = pu + 1 + (iter % 3);
const int nv = pv + 1 + ((iter / 3) % 3);
std::vector<std::vector<Point3D>> grid(nu, std::vector<Point3D>(nv));
std::vector<std::vector<double>> w(nu, std::vector<double>(nv, 1.0));
for (int i = 0; i < nu; ++i)
for (int j = 0; j < nv; ++j) {
grid[i][j] = Point3D(U(rng), U(rng), U(rng));
w[i][j] = W(rng);
}
NurbsSurface s(grid, knot_vec(nu, pu), knot_vec(nv, pv), w, pu, pv);
for (int k = 0; k < 12; ++k) {
const double u = T(rng), v = T(rng);
const double h = 1e-6;
worst1 = std::max(worst1, rel(s.derivative_u(u, v),
(s.evaluate(u + h, v) - s.evaluate(u - h, v)) * (0.5 / h)));
worst1 = std::max(worst1, rel(s.derivative_v(u, v),
(s.evaluate(u, v + h) - s.evaluate(u, v - h)) * (0.5 / h)));
worst2 = std::max(worst2, rel(s.derivative_uu(u, v),
(s.derivative_u(u + h, v) - s.derivative_u(u - h, v)) * (0.5 / h)));
worst2 = std::max(worst2, rel(s.derivative_uv(u, v),
(s.derivative_u(u, v + h) - s.derivative_u(u, v - h)) * (0.5 / h)));
worst2 = std::max(worst2, rel(s.derivative_vv(u, v),
(s.derivative_v(u, v + h) - s.derivative_v(u, v - h)) * (0.5 / h)));
}
}
EXPECT_LE(worst1, 1e-5) << "worst first-derivative rel err=" << worst1;
EXPECT_LE(worst2, 1e-5) << "worst second-derivative rel err=" << worst2;
}
TEST(NurbsCertification, RandomRationalDerivativesMatchFiniteDifferences) {
std::mt19937 rng(12345);
std::uniform_real_distribution<double> U(-5, 5), W(0.2, 5.0), T(0.0, 1.0);
auto knot_vec = [](int n, int p) {
std::vector<double> k(n + p + 1, 0.0);
for (size_t i = 0; i < k.size(); ++i) {
if (static_cast<int>(i) <= p) k[i] = 0.0;
else if (static_cast<int>(i) >= n) k[i] = 1.0;
else k[i] = static_cast<double>(static_cast<int>(i) - p) / (n - p);
}
return k;
};
double worst = 0.0;
for (int iter = 0; iter < 200; ++iter) {
const int deg = 2 + (iter % 3);
const int ncp = deg + 1 + (iter % 4);
std::vector<Point3D> cps;
std::vector<double> w;
for (int i = 0; i < ncp; ++i) {
cps.push_back(Point3D(U(rng), U(rng), U(rng)));
w.push_back(W(rng));
}
NurbsCurve c(cps, knot_vec(ncp, deg), w, deg);
for (int k = 0; k < 9; ++k) {
const double t = T(rng);
const Vector3D d = c.derivative(t, 1);
const double h = 1e-6;
const Vector3D fd =
(c.evaluate(t + h) - c.evaluate(t - h)) * (1.0 / (2.0 * h));
worst = std::max(worst, (fd - d).norm() / std::max(d.norm(), 1e-9));
}
}
EXPECT_LE(worst, 1e-5);
{
std::vector<Point3D> cps = {Point3D(0, 0, 0), Point3D(1, 2, 0),
Point3D(3, -1, 0), Point3D(4, 0, 0)};
std::vector<double> w = {1.0, 2.0, 0.5, 1.0};
NurbsCurve c(cps, {0, 0, 0, 0, 1, 1, 1, 1}, w, 3);
double e2 = 0.0;
for (int k = 1; k < 9; ++k) {
const double t = k / 10.0, h = 1e-5;
const Vector3D d2 = c.derivative(t, 2);
const Vector3D fd = (c.derivative(t + h, 1) - c.derivative(t - h, 1)) *
(1.0 / (2.0 * h));
e2 = std::max(e2, (fd - d2).norm() / std::max(d2.norm(), 1e-9));
}
EXPECT_LE(e2, 1e-5);
}
}