324 lines
15 KiB
C++
324 lines
15 KiB
C++
// VDE-1261:fair_surface 无效 NURBS 越界崩溃(0xC0000005)防御回归测试
|
||
//
|
||
// 背景:宿主 part.surface.fair 在普通实体(box 平面面)上调用 fair_surface
|
||
// 时进程崩溃。根因是 fair_surface 未校验控制点网格/节点向量与控制点数量一致,
|
||
// 直接按 nu/nv 下标访问 → 越界;conjugate_gradient_minimize/
|
||
// unflatten_control_points 也无布局防护(jagged 网格上越界写)。
|
||
//
|
||
// 本文件覆盖修复后的契约:
|
||
// 1. 合法平面(2×2 CP,degree 1)返回 ok=true、有限能量(宿主 smoothness=0.5);
|
||
// 2. 空曲面 / jagged 控制点网格 / 负阶次(短节点向量) / 零参数域 / 非降序节点 /
|
||
// 非有限控制点 / jagged 权重 / 非有限约束与数据点 → ok=false + error +
|
||
// 原曲面副本,不崩溃、不进入 CG;
|
||
// 3. legacy 包装保持返回(无效输入返回原曲面副本);
|
||
// 4. fair_region / interactive_fairing / per_cp_energy_contribution /
|
||
// energy_diagnostics / fair_surfaces_coupled 对无效输入不崩溃(同级防护)。
|
||
//
|
||
// 手工编译(不改共享 CMake;独立 build-fb-vde1261;Windows cmd 续行符 ^):
|
||
// g++ -std=c++20 -O2 -D_USE_MATH_DEFINES -I include -I eigen3 ^
|
||
// -I googletest/googletest/include -I googletest/googletest ^
|
||
// tests/curves/test_surface_fairing.cpp ^
|
||
// src/curves/surface_fairing.cpp src/curves/nurbs_surface.cpp ^
|
||
// src/curves/nurbs_curve.cpp src/curves/curve_surface_projection.cpp ^
|
||
// src/curves/bspline_curve.cpp src/curves/parameterization.cpp ^
|
||
// googletest/googletest/src/gtest-all.cc ^
|
||
// googletest/googletest/src/gtest_main.cc ^
|
||
// -o build-fb-vde1261/test_surface_fairing.exe
|
||
#include <gtest/gtest.h>
|
||
|
||
#include <cmath>
|
||
#include <limits>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
#include "vde/curves/nurbs_surface.h"
|
||
#include "vde/curves/surface_fairing.h"
|
||
|
||
using namespace vde::curves;
|
||
using namespace vde::core;
|
||
|
||
namespace {
|
||
|
||
constexpr double kNaN = std::numeric_limits<double>::quiet_NaN();
|
||
constexpr double kInf = std::numeric_limits<double>::infinity();
|
||
|
||
/// 宿主 part.surface.fair 的典型输入:2×2 控制点、degree 1 的平面。
|
||
NurbsSurface make_plane() {
|
||
return NurbsSurface::plane(Point3D(0, 0, 0), Vector3D(1, 0, 0),
|
||
Vector3D(0, 1, 0), 40.0, 20.0);
|
||
}
|
||
|
||
std::vector<std::vector<Point3D>> plane_grid() {
|
||
return {{Point3D(0, 0, 0), Point3D(0, 20, 0)},
|
||
{Point3D(40, 0, 0), Point3D(40, 20, 0)}};
|
||
}
|
||
|
||
/// 语义上合法的“平面棋盘”控制点网格(2×2)。
|
||
NurbsSurface make_valid_grid_surface() {
|
||
return NurbsSurface(plane_grid(), {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
|
||
}
|
||
|
||
/// 宿主侧 smoothness=0.5 的典型约束(VDE-1261 报障路径)。
|
||
FairingConstraints host_style_constraints() {
|
||
FairingConstraints c;
|
||
c.smoothness = 0.5;
|
||
c.lambda = 0.1;
|
||
return c;
|
||
}
|
||
|
||
bool finite(const Point3D& p) {
|
||
return std::isfinite(p.x()) && std::isfinite(p.y()) && std::isfinite(p.z());
|
||
}
|
||
|
||
} // namespace
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 1. 合法输入:行为不变(含宿主 smoothness=0.5 路径)
|
||
// ═══════════════════════════════════════════════════════════
|
||
TEST(VDE1261FairSurface, ValidPlaneReturnsFiniteEnergy) {
|
||
const NurbsSurface plane = make_valid_grid_surface();
|
||
const FairingResult r = fair_surface(plane, host_style_constraints());
|
||
ASSERT_TRUE(r.ok) << r.error;
|
||
EXPECT_TRUE(r.error.empty());
|
||
EXPECT_TRUE(std::isfinite(r.initial_energy));
|
||
EXPECT_TRUE(std::isfinite(r.final_energy));
|
||
EXPECT_GE(r.initial_energy, 0.0);
|
||
EXPECT_GE(r.final_energy, 0.0);
|
||
ASSERT_EQ(r.faired_surface.num_control_points()[0], 2);
|
||
ASSERT_EQ(r.faired_surface.num_control_points()[1], 2);
|
||
for (const auto& row : r.faired_surface.control_points())
|
||
for (const auto& p : row) EXPECT_TRUE(finite(p));
|
||
EXPECT_EQ(r.cp_displacements.size(), 4u);
|
||
EXPECT_TRUE(std::isfinite(r.max_cp_displacement));
|
||
}
|
||
|
||
TEST(VDE1261FairSurface, NurbsPlaneFactoryStillFairs) {
|
||
const NurbsSurface plane = make_plane();
|
||
const FairingResult r = fair_surface(plane, host_style_constraints());
|
||
ASSERT_TRUE(r.ok) << r.error;
|
||
EXPECT_TRUE(std::isfinite(r.final_energy));
|
||
EXPECT_FALSE(r.faired_surface.is_empty());
|
||
}
|
||
|
||
TEST(VDE1261FairSurface, ValidPlaneWithDataAndBoundaryConstraints) {
|
||
NurbsSurface plane = make_valid_grid_surface();
|
||
FairingConstraints c = host_style_constraints();
|
||
c.data_points = {{{0.25, 0.25}, Point3D(10, 5, 0.1)}};
|
||
BoundaryConstraint bc;
|
||
bc.edge_id = 2;
|
||
bc.type = BoundaryConstraintType::G0;
|
||
bc.u_params = {0.0, 1.0};
|
||
bc.target_positions = {Point3D(0, 0, 0), Point3D(40, 0, 0)};
|
||
c.boundary_constraints.push_back(bc);
|
||
|
||
const FairingResult r = fair_surface(plane, c);
|
||
ASSERT_TRUE(r.ok) << r.error;
|
||
EXPECT_TRUE(std::isfinite(r.data_fitting_error));
|
||
EXPECT_TRUE(std::isfinite(r.initial_energy));
|
||
EXPECT_TRUE(std::isfinite(r.final_energy));
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 2. 无效输入:显式失败(ok=false + error + 原曲面),不崩溃
|
||
// ═══════════════════════════════════════════════════════════
|
||
TEST(VDE1261FairSurface, EmptySurfaceFailsExplicitly) {
|
||
const NurbsSurface empty;
|
||
const FairingResult r = fair_surface(empty);
|
||
EXPECT_FALSE(r.ok);
|
||
EXPECT_FALSE(r.error.empty());
|
||
EXPECT_NE(r.error.find("fair_surface"), std::string::npos) << r.error;
|
||
EXPECT_TRUE(r.faired_surface.is_empty());
|
||
EXPECT_EQ(r.iterations, 0);
|
||
}
|
||
|
||
TEST(VDE1261FairSurface, RaggedControlPointGridFailsWithoutCrash) {
|
||
// 构造期只校验第一行:第二行少一个控制点 → num_control_points 仍报 2×2,
|
||
// 旧实现 unflatten/能量循环会越界读写(0xC0000005 源)。修复后必须显式失败。
|
||
std::vector<std::vector<Point3D>> ragged = {
|
||
{Point3D(0, 0, 0), Point3D(0, 20, 0)},
|
||
{Point3D(40, 0, 0)}};
|
||
const NurbsSurface surf(ragged, {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
|
||
const FairingResult r = fair_surface(surf, host_style_constraints());
|
||
EXPECT_FALSE(r.ok);
|
||
EXPECT_NE(r.error.find("ragged control point grid"), std::string::npos) << r.error;
|
||
EXPECT_EQ(r.faired_surface.control_points().size(), 2u);
|
||
// 证明确实未进入 CG:无迭代、无位移、无能量评估残留
|
||
EXPECT_EQ(r.iterations, 0);
|
||
EXPECT_TRUE(r.cp_displacements.empty());
|
||
EXPECT_EQ(r.initial_energy, 0.0);
|
||
EXPECT_EQ(r.final_energy, 0.0);
|
||
}
|
||
|
||
TEST(VDE1261FairSurface, NegativeDegreeAndShortKnotsFailsExplicitly) {
|
||
// degree_u=-2 + ku.size()=1 可绕过构造期尺寸校验:旧实现 ku[degree_u()]
|
||
// 负下标越界。修复后按负阶次拒绝。
|
||
const NurbsSurface surf(plane_grid(), {0.5}, {0, 0, 1, 1}, {}, -2, 1);
|
||
const FairingResult r = fair_surface(surf);
|
||
EXPECT_FALSE(r.ok);
|
||
EXPECT_NE(r.error.find("negative degree"), std::string::npos) << r.error;
|
||
EXPECT_FALSE(r.faired_surface.is_empty());
|
||
}
|
||
|
||
TEST(VDE1261FairSurface, EmptyParameterDomainFailsExplicitly) {
|
||
// 节点向量全相等 → 参数域 [ku[1], ku[2]] 为空(旧实现 cell=0、能量积分退化)。
|
||
const NurbsSurface surf(plane_grid(), {0, 0, 0, 0}, {0, 0, 1, 1}, {}, 1, 1);
|
||
const FairingResult r = fair_surface(surf);
|
||
EXPECT_FALSE(r.ok);
|
||
EXPECT_NE(r.error.find("parameter domain"), std::string::npos) << r.error;
|
||
}
|
||
|
||
TEST(VDE1261FairSurface, NonMonotonicKnotsFailExplicitly) {
|
||
const NurbsSurface surf(plane_grid(), {0, 0, 2, 1}, {0, 0, 1, 1}, {}, 1, 1);
|
||
const FairingResult r = fair_surface(surf);
|
||
EXPECT_FALSE(r.ok);
|
||
EXPECT_NE(r.error.find("not non-decreasing"), std::string::npos) << r.error;
|
||
}
|
||
|
||
TEST(VDE1261FairSurface, NonFiniteKnotsFailExplicitly) {
|
||
const NurbsSurface surf(plane_grid(), {0, 0, kNaN, 1}, {0, 0, 1, 1}, {}, 1, 1);
|
||
const FairingResult r = fair_surface(surf);
|
||
EXPECT_FALSE(r.ok);
|
||
EXPECT_NE(r.error.find("non-finite u knot"), std::string::npos) << r.error;
|
||
}
|
||
|
||
TEST(VDE1261FairSurface, NonFiniteControlPointFailsExplicitly) {
|
||
std::vector<std::vector<Point3D>> grid = plane_grid();
|
||
grid[1][1] = Point3D(kNaN, 0, 0);
|
||
const NurbsSurface surf(grid, {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
|
||
const FairingResult r = fair_surface(surf);
|
||
EXPECT_FALSE(r.ok);
|
||
EXPECT_NE(r.error.find("non-finite control point"), std::string::npos)
|
||
<< r.error;
|
||
}
|
||
|
||
TEST(VDE1261FairSurface, RaggedWeightsGridFailsExplicitly) {
|
||
// 构造期不校验 weights 布局;旧路径 evaluate 虽防御读取,但 fairing 应显式
|
||
// 拒绝不一致权重,避免后续 CG/能量语义错误。
|
||
std::vector<std::vector<double>> weights = {{1.0, 1.0}, {1.0}};
|
||
const NurbsSurface surf(plane_grid(), {0, 0, 1, 1}, {0, 0, 1, 1}, weights, 1, 1);
|
||
const FairingResult r = fair_surface(surf);
|
||
EXPECT_FALSE(r.ok);
|
||
EXPECT_NE(r.error.find("ragged weights grid"), std::string::npos) << r.error;
|
||
}
|
||
|
||
TEST(VDE1261FairSurface, NonPositiveWeightFailsExplicitly) {
|
||
std::vector<std::vector<double>> weights = {{1.0, 1.0}, {1.0, 0.0}};
|
||
const NurbsSurface surf(plane_grid(), {0, 0, 1, 1}, {0, 0, 1, 1}, weights, 1, 1);
|
||
const FairingResult r = fair_surface(surf);
|
||
EXPECT_FALSE(r.ok);
|
||
EXPECT_NE(r.error.find("non-finite or non-positive weight"),
|
||
std::string::npos)
|
||
<< r.error;
|
||
}
|
||
|
||
TEST(VDE1261FairSurface, NonFiniteDataPointFailsExplicitly) {
|
||
FairingConstraints c = host_style_constraints();
|
||
c.data_points = {{{0.5, 0.5}, Point3D(kNaN, 0, 0)}};
|
||
const FairingResult r = fair_surface(make_valid_grid_surface(), c);
|
||
EXPECT_FALSE(r.ok);
|
||
EXPECT_NE(r.error.find("non-finite data point"), std::string::npos) << r.error;
|
||
}
|
||
|
||
TEST(VDE1261FairSurface, NonFiniteBoundaryConstraintFailsExplicitly) {
|
||
FairingConstraints c = host_style_constraints();
|
||
BoundaryConstraint bc;
|
||
bc.edge_id = 0;
|
||
bc.u_params = {0.0};
|
||
bc.target_positions = {Point3D(kInf, 0, 0)};
|
||
c.boundary_constraints.push_back(bc);
|
||
const FairingResult r = fair_surface(make_valid_grid_surface(), c);
|
||
EXPECT_FALSE(r.ok);
|
||
EXPECT_NE(r.error.find("non-finite boundary constraint"), std::string::npos)
|
||
<< r.error;
|
||
}
|
||
|
||
TEST(VDE1261FairSurface, InvalidToleranceFailsExplicitly) {
|
||
FairingConstraints c = host_style_constraints();
|
||
c.tolerance = 0.0;
|
||
const FairingResult r = fair_surface(make_valid_grid_surface(), c);
|
||
EXPECT_FALSE(r.ok);
|
||
EXPECT_NE(r.error.find("tolerance"), std::string::npos) << r.error;
|
||
|
||
FairingConstraints nan_c = host_style_constraints();
|
||
nan_c.lambda = kNaN;
|
||
const FairingResult nan_r = fair_surface(make_valid_grid_surface(), nan_c);
|
||
EXPECT_FALSE(nan_r.ok);
|
||
EXPECT_NE(nan_r.error.find("non-finite fairing option"), std::string::npos)
|
||
<< nan_r.error;
|
||
}
|
||
|
||
TEST(VDE1261FairSurface, DegenerateButStructurallyValidPatchStillFairs) {
|
||
// 宿主 box 的三角形→退化 bilinear patch:结构合法(2×2、节点齐全)但
|
||
// ∂S/∂u≡0。这属于“合法布局、几何退化”,fair_surface 不应崩溃;能量为 0。
|
||
std::vector<std::vector<Point3D>> grid = {
|
||
{Point3D(0, 0, 0), Point3D(0, 1, 0)},
|
||
{Point3D(0, 0, 0), Point3D(0, 1, 0)}};
|
||
const NurbsSurface patch(grid, {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
|
||
const FairingResult r = fair_surface(patch);
|
||
ASSERT_TRUE(r.ok) << r.error;
|
||
EXPECT_TRUE(std::isfinite(r.initial_energy));
|
||
EXPECT_TRUE(std::isfinite(r.final_energy));
|
||
EXPECT_NEAR(r.initial_energy, 0.0, 1e-12);
|
||
EXPECT_NEAR(r.final_energy, 0.0, 1e-12);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 3. legacy 包装与同级 fairing API 的无效输入防护
|
||
// ═══════════════════════════════════════════════════════════
|
||
TEST(VDE1261FairSurface, LegacyWrapperReturnsInputCopyOnInvalid) {
|
||
const NurbsSurface empty;
|
||
const NurbsSurface legacy = fair_surface_legacy(empty);
|
||
EXPECT_TRUE(legacy.is_empty());
|
||
|
||
std::vector<std::vector<Point3D>> ragged = {
|
||
{Point3D(0, 0, 0), Point3D(0, 20, 0)},
|
||
{Point3D(40, 0, 0)}};
|
||
const NurbsSurface surf(ragged, {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
|
||
const NurbsSurface copy = fair_surface_legacy(surf);
|
||
EXPECT_EQ(copy.num_control_points()[0], 2);
|
||
}
|
||
|
||
TEST(VDE1261FairSurface, InteractiveFairingRejectsInvalidInput) {
|
||
const NurbsSurface empty;
|
||
const FairingResult r = interactive_fairing(empty, {0.0, 1.0, 0.0, 1.0});
|
||
EXPECT_FALSE(r.ok);
|
||
EXPECT_NE(r.error.find("interactive_fairing"), std::string::npos) << r.error;
|
||
EXPECT_TRUE(r.faired_surface.is_empty());
|
||
}
|
||
|
||
TEST(VDE1261FairSurface, FairRegionRejectsInvalidInput) {
|
||
const NurbsSurface empty;
|
||
const FairingResult r = fair_region(empty, FairingRegion{});
|
||
EXPECT_FALSE(r.ok);
|
||
EXPECT_NE(r.error.find("fair_region"), std::string::npos) << r.error;
|
||
}
|
||
|
||
TEST(VDE1261FairSurface, VoidReturningApisDoNotCrashOnInvalidInput) {
|
||
const NurbsSurface empty;
|
||
EXPECT_TRUE(per_cp_energy_contribution(empty).empty());
|
||
const EnergyDiagnostics diag = energy_diagnostics(empty);
|
||
EXPECT_EQ(diag.res_u, 64); // 与合法路径一致;density 不写入
|
||
EXPECT_TRUE(diag.density_grid.empty());
|
||
|
||
// jagged 网格:per_cp/energy_diagnostics 同样不得越界。
|
||
std::vector<std::vector<Point3D>> ragged = {
|
||
{Point3D(0, 0, 0), Point3D(0, 20, 0)},
|
||
{Point3D(40, 0, 0)}};
|
||
const NurbsSurface surf(ragged, {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
|
||
EXPECT_TRUE(per_cp_energy_contribution(surf).empty());
|
||
EXPECT_TRUE(energy_diagnostics(surf).density_grid.empty());
|
||
}
|
||
|
||
TEST(VDE1261FairSurface, CoupledFairingWithInvalidSurfaceDoesNotCrash) {
|
||
const NurbsSurface empty;
|
||
const NurbsSurface plane = make_valid_grid_surface();
|
||
const CoupledFairingResult r =
|
||
fair_surfaces_coupled({empty, plane}, {{0, 1}});
|
||
ASSERT_EQ(r.faired_surfaces.size(), 2u);
|
||
ASSERT_EQ(r.per_surface.size(), 2u);
|
||
EXPECT_FALSE(r.per_surface[0].ok);
|
||
EXPECT_TRUE(r.per_surface[1].ok) << r.per_surface[1].error;
|
||
EXPECT_TRUE(std::isfinite(r.global_energy_before));
|
||
EXPECT_TRUE(std::isfinite(r.global_energy_after));
|
||
}
|