209 lines
8.8 KiB
C++
209 lines
8.8 KiB
C++
// NURBS 曲面求值/法向防御回归(VDE-1373)
|
|
//
|
|
// 设计约定(VDE-1261):NurbsSurface 构造期保持宽容——畸形输入由 fair_surface/
|
|
// validate 等消费方显式报错。因此这里的防线在**求值端**:
|
|
// · 负阶次 + 短节点向量(旧实现 knots[-1] 负下标,span 二分不收敛 → normal()
|
|
// 永不返回,探针 15 s 超时);
|
|
// · 参差控制网格(行宽不一致,旧实现 row[iv] 越界读);
|
|
// · 非有限/非单调/空域节点、非正权重、非有限控制点。
|
|
// 要求:任何畸形对象 evaluate/normal 都必须在有限时间内返回,且法向为有限单位向量。
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <cmath>
|
|
#include <limits>
|
|
#include <vector>
|
|
|
|
#include "vde/core/point.h"
|
|
#include "vde/curves/nurbs_surface.h"
|
|
#include "vde/curves/curve_surface_projection.h"
|
|
|
|
using namespace vde;
|
|
using namespace vde::curves;
|
|
using namespace vde::core;
|
|
|
|
namespace {
|
|
|
|
const double kNaN = std::numeric_limits<double>::quiet_NaN();
|
|
const double kInf = std::numeric_limits<double>::infinity();
|
|
|
|
std::vector<std::vector<Point3D>> plane_grid() {
|
|
return {{Point3D(0, 0, 0), Point3D(0, 1, 0)},
|
|
{Point3D(1, 0, 0), Point3D(1, 1, 0)}};
|
|
}
|
|
|
|
bool finite_point(const Point3D& p) {
|
|
return std::isfinite(p.x()) && std::isfinite(p.y()) && std::isfinite(p.z());
|
|
}
|
|
|
|
/// 畸形对象:反复求值/法向必须快速返回(回归旧实现的 span 二分不收敛)。
|
|
void expect_safe_repeated_evaluation(const NurbsSurface& s) {
|
|
for (int i = 0; i < 1000; ++i) {
|
|
const double u = (i % 7) / 6.0;
|
|
const double v = (i % 5) / 4.0;
|
|
const Point3D p = s.evaluate(u, v);
|
|
ASSERT_TRUE(std::isfinite(p.x()) || std::isinf(p.x())) << "evaluate crashed";
|
|
const Vector3D n = s.normal(u, v);
|
|
EXPECT_TRUE(finite_point(Point3D(n.x(), n.y(), n.z())))
|
|
<< "normal 非有限 at i=" << i;
|
|
EXPECT_NEAR(n.norm(), 1.0, 1e-9) << "normal 非单位 at i=" << i;
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST(NurbsSurfaceValidation, NegativeDegreeEvaluationDoesNotHang) {
|
|
// degree_u=-2 + ku.size()=1 可通过尺寸校验(cp = 1 - (-2) - 1 = 2)。
|
|
const NurbsSurface s(plane_grid(), {0.5}, {0, 0, 1, 1}, {}, -2, 1);
|
|
expect_safe_repeated_evaluation(s);
|
|
EXPECT_TRUE(finite_point(s.evaluate(0.5, 0.5)));
|
|
EXPECT_FALSE(s.is_valid_layout());
|
|
}
|
|
|
|
TEST(NurbsSurfaceValidation, RaggedControlGridEvaluationIsSafe) {
|
|
std::vector<std::vector<Point3D>> ragged = {
|
|
{Point3D(0, 0, 0), Point3D(0, 1, 0)}, {Point3D(1, 0, 0)}};
|
|
const NurbsSurface s(ragged, {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
|
|
expect_safe_repeated_evaluation(s);
|
|
EXPECT_FALSE(s.is_valid_layout());
|
|
}
|
|
|
|
TEST(NurbsSurfaceValidation, NonFiniteKnotsEvaluationIsSafe) {
|
|
const NurbsSurface s(plane_grid(), {0, 0, kNaN, 1}, {0, 0, 1, 1}, {}, 1, 1);
|
|
expect_safe_repeated_evaluation(s);
|
|
EXPECT_TRUE(finite_point(s.evaluate(0.5, 0.5)));
|
|
}
|
|
|
|
TEST(NurbsSurfaceValidation, NonMonotonicKnotsEvaluationIsSafe) {
|
|
const NurbsSurface s(plane_grid(), {0, 0, 2, 1}, {0, 0, 1, 1}, {}, 1, 1);
|
|
expect_safe_repeated_evaluation(s);
|
|
EXPECT_TRUE(finite_point(s.evaluate(0.5, 0.5)));
|
|
}
|
|
|
|
TEST(NurbsSurfaceValidation, EmptyParameterDomainEvaluationIsSafe) {
|
|
const NurbsSurface s(plane_grid(), {0, 0, 0, 0}, {0, 0, 1, 1}, {}, 1, 1);
|
|
expect_safe_repeated_evaluation(s);
|
|
EXPECT_TRUE(finite_point(s.evaluate(0.5, 0.5)));
|
|
}
|
|
|
|
TEST(NurbsSurfaceValidation, NonFiniteControlPointEvaluationIsSafe) {
|
|
auto g = plane_grid();
|
|
g[1][1] = Point3D(kNaN, 0, 0);
|
|
const NurbsSurface s(g, {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
|
|
// 位置可含 NaN(由消费方 validate 检出),但法向必须有限且不挂死。
|
|
for (int i = 0; i < 100; ++i) {
|
|
const Vector3D n = s.normal(0.3, 0.7);
|
|
EXPECT_TRUE(finite_point(Point3D(n.x(), n.y(), n.z())));
|
|
}
|
|
}
|
|
|
|
TEST(NurbsSurfaceValidation, RaggedWeightsEvaluationIsSafe) {
|
|
const NurbsSurface s(plane_grid(), {0, 0, 1, 1}, {0, 0, 1, 1},
|
|
{{1.0, 1.0}, {1.0}}, 1, 1);
|
|
expect_safe_repeated_evaluation(s);
|
|
EXPECT_TRUE(finite_point(s.evaluate(0.5, 0.5)));
|
|
}
|
|
|
|
TEST(NurbsSurfaceValidation, NonPositiveWeightEvaluationIsSafe) {
|
|
const NurbsSurface zero(plane_grid(), {0, 0, 1, 1}, {0, 0, 1, 1},
|
|
{{1.0, 1.0}, {1.0, 0.0}}, 1, 1);
|
|
expect_safe_repeated_evaluation(zero);
|
|
const NurbsSurface neg(plane_grid(), {0, 0, 1, 1}, {0, 0, 1, 1},
|
|
{{1.0, 1.0}, {1.0, -1.0}}, 1, 1);
|
|
expect_safe_repeated_evaluation(neg);
|
|
EXPECT_TRUE(finite_point(neg.evaluate(0.5, 0.5)));
|
|
}
|
|
|
|
TEST(NurbsSurfaceValidation, AcceptsMinimalBilinearSurface) {
|
|
NurbsSurface s(plane_grid(), {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
|
|
const Point3D p = s.evaluate(0.5, 0.5);
|
|
EXPECT_NEAR(p.x(), 0.5, 1e-12);
|
|
EXPECT_NEAR(p.y(), 0.5, 1e-12);
|
|
const Vector3D n = s.normal(0.5, 0.5);
|
|
EXPECT_NEAR(n.norm(), 1.0, 1e-12);
|
|
EXPECT_TRUE(s.is_valid_layout());
|
|
EXPECT_EQ(s.degree_u(), 1);
|
|
EXPECT_EQ(s.degree_v(), 1);
|
|
}
|
|
|
|
TEST(NurbsSurfaceValidation, DefaultConstructedSurfaceIsSafe) {
|
|
NurbsSurface empty;
|
|
const Point3D p = empty.evaluate(0.3, 0.7);
|
|
EXPECT_DOUBLE_EQ(p.x(), 0.0);
|
|
EXPECT_DOUBLE_EQ(p.y(), 0.0);
|
|
EXPECT_DOUBLE_EQ(p.z(), 0.0);
|
|
const Vector3D n = empty.normal(0.3, 0.7);
|
|
EXPECT_NEAR(n.norm(), 1.0, 1e-12);
|
|
EXPECT_FALSE(empty.is_valid_layout());
|
|
}
|
|
|
|
TEST(NurbsSurfaceValidation, FactorySurfacesStillConstruct) {
|
|
const NurbsSurface sphere = NurbsSurface::sphere(0, 0, 0, 2.0);
|
|
const Point3D ps = sphere.evaluate(0.25, 0.75);
|
|
EXPECT_NEAR(ps.norm(), 2.0, 1e-9);
|
|
EXPECT_NEAR(sphere.normal(0.25, 0.75).norm(), 1.0, 1e-9);
|
|
|
|
const NurbsSurface torus = NurbsSurface::torus(0, 0, 0, 5.0, 2.0);
|
|
EXPECT_TRUE(std::isfinite(torus.evaluate(0.3, 0.4).norm()));
|
|
EXPECT_NEAR(torus.normal(0.3, 0.4).norm(), 1.0, 1e-9);
|
|
|
|
const NurbsSurface plane = NurbsSurface::plane(
|
|
Point3D(1, 2, 3), Vector3D(1, 0, 0), Vector3D(0, 1, 0), 4.0, 5.0);
|
|
EXPECT_TRUE(std::isfinite(plane.evaluate(0.5, 0.5).x()));
|
|
EXPECT_TRUE(plane.is_valid_layout());
|
|
}
|
|
|
|
TEST(NurbsSurfaceValidation, ClosestPointOnMalformedSurfacesIsSafe) {
|
|
// 默认构造(空节点)曾使 closest_point() 100% 崩溃:global_search 直接索引
|
|
// knots[degree]。空/参差/负阶次曲面都必须返回有限参数与有限距离。
|
|
NurbsSurface empty;
|
|
const Point3D c0 = empty.closest_point(Point3D(1, 2, 3));
|
|
EXPECT_TRUE(finite_point(c0));
|
|
EXPECT_TRUE(std::isfinite(empty.distance_to(Point3D(1, 2, 3))));
|
|
|
|
std::vector<std::vector<Point3D>> ragged = {
|
|
{Point3D(0, 0, 0), Point3D(0, 1, 0)}, {Point3D(1, 0, 0)}};
|
|
NurbsSurface r(ragged, {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
|
|
EXPECT_TRUE(finite_point(r.closest_point(Point3D(1, 2, 3))));
|
|
EXPECT_TRUE(std::isfinite(r.distance_to(Point3D(1, 2, 3))));
|
|
const auto uv_r = project_point_on_surface_quick(Point3D(1, 2, 3), r);
|
|
EXPECT_TRUE(std::isfinite(uv_r.first) && std::isfinite(uv_r.second));
|
|
|
|
NurbsSurface neg(plane_grid(), {0.5}, {0, 0, 1, 1}, {}, -2, 1);
|
|
EXPECT_TRUE(finite_point(neg.closest_point(Point3D(1, 2, 3))));
|
|
const auto uv_n = project_point_on_surface_quick(Point3D(1, 2, 3), neg);
|
|
EXPECT_TRUE(std::isfinite(uv_n.first) && std::isfinite(uv_n.second));
|
|
}
|
|
|
|
// VDE-1378:空/畸形曲面上 project_polyline_on_surface 曾在取参数域端点时崩溃;
|
|
// project_line/circle/adaptive 逐点投影也须返回有限结果而非崩溃。
|
|
TEST(NurbsSurfaceValidation, ProjectionApisOnMalformedSurfacesAreSafe) {
|
|
const NurbsSurface empty;
|
|
const NurbsCurve line({Point3D(0, 0, 0), Point3D(1, 0, 0)}, {0, 0, 1, 1},
|
|
{1, 1}, 1);
|
|
const NurbsCurve line_pc = project_line_on_surface(
|
|
Point3D(0, 0, 0), Point3D(1, 0, 0), empty);
|
|
EXPECT_GT(line_pc.control_points().size(), 0u);
|
|
for (const auto& c : line_pc.control_points())
|
|
EXPECT_TRUE(std::isfinite(c.x()) && std::isfinite(c.y()));
|
|
|
|
const NurbsCurve circle_pc = project_circle_on_surface(
|
|
Point3D(0, 0, 0), Vector3D(0, 0, 1), 1.0, empty);
|
|
EXPECT_GT(circle_pc.control_points().size(), 0u);
|
|
|
|
const NurbsCurve adaptive = project_curve_on_surface_adaptive(line, empty, 0.01);
|
|
EXPECT_GT(adaptive.control_points().size(), 0u);
|
|
|
|
const std::vector<Point3D> pts = {Point3D(0, 0, 0), Point3D(1, 0, 0),
|
|
Point3D(2, 1, 0)};
|
|
const auto uv = project_polyline_on_surface(pts, empty, 0.01, true);
|
|
ASSERT_EQ(uv.size(), pts.size());
|
|
for (const auto& p : uv)
|
|
EXPECT_TRUE(std::isfinite(p.first) && std::isfinite(p.second));
|
|
|
|
std::vector<std::vector<Point3D>> ragged = {
|
|
{Point3D(0, 0, 0), Point3D(0, 1, 0)}, {Point3D(1, 0, 0)}};
|
|
const NurbsSurface r(ragged, {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
|
|
const auto uv_r = project_polyline_on_surface(pts, r, 0.01, true);
|
|
EXPECT_EQ(uv_r.size(), pts.size());
|
|
}
|