Files

1037 lines
43 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.
#include <gtest/gtest.h>
#include "vde/curves/surface_tools.h"
#include "vde/curves/nurbs_surface.h"
#include "vde/curves/nurbs_curve.h"
#include "vde/brep/modeling.h"
#include <cmath>
#include <stdexcept>
using namespace vde::curves;
using namespace vde::core;
// Helper: create a simple planar NURBS surface (unit square in XY)
static NurbsSurface make_planar_surface() {
std::vector<std::vector<Point3D>> grid = {
{Point3D(0, 0, 0), Point3D(5, 0, 0)},
{Point3D(0, 5, 0), Point3D(5, 5, 0)},
};
std::vector<double> knots_u = {0, 0, 1, 1};
std::vector<double> knots_v = {0, 0, 1, 1};
std::vector<std::vector<double>> weights = {
{1.0, 1.0},
{1.0, 1.0},
};
return NurbsSurface(grid, knots_u, knots_v, weights, 1, 1);
}
// Helper: create a perturbed surface (like a curved patch)
static NurbsSurface make_curved_surface() {
std::vector<std::vector<Point3D>> grid = {
{Point3D(0, 0, 0), Point3D(3, 0, 1), Point3D(6, 0, 0)},
{Point3D(0, 3, 1), Point3D(3, 3, 2), Point3D(6, 3, 1)},
{Point3D(0, 6, 0), Point3D(3, 6, 1), Point3D(6, 6, 0)},
};
std::vector<double> knots_u = {0, 0, 0, 1, 1, 1};
std::vector<double> knots_v = {0, 0, 0, 1, 1, 1};
std::vector<std::vector<double>> weights = {
{1.0, 1.0, 1.0},
{1.0, 1.0, 1.0},
{1.0, 1.0, 1.0},
};
return NurbsSurface(grid, knots_u, knots_v, weights, 2, 2);
}
// ═══════════════════════════════════════════════════════════
// thicken_surface
// ═══════════════════════════════════════════════════════════
TEST(SurfaceToolsTest, ThickenSurface_Basic) {
auto surf = make_planar_surface();
auto solid = thicken_surface(surf, 2.0);
EXPECT_TRUE(solid.is_valid());
EXPECT_GT(solid.num_faces(), 0u);
// Thickened surface should produce a solid with volume
EXPECT_GT(solid.bounds().volume(), 0.0);
}
TEST(SurfaceToolsTest, ThickenSurface_BothSides) {
auto surf = make_planar_surface();
auto solid = thicken_surface(surf, 4.0, true, true);
EXPECT_TRUE(solid.is_valid());
EXPECT_GT(solid.num_faces(), 0u);
}
TEST(SurfaceToolsTest, ThickenSurface_NoCaps) {
auto surf = make_planar_surface();
auto solid = thicken_surface(surf, 1.0, false, false);
EXPECT_TRUE(solid.is_valid());
EXPECT_GE(solid.num_faces(), 1u);
}
TEST(SurfaceToolsTest, ThickenSurface_CurvedPatch) {
auto surf = make_curved_surface();
auto solid = thicken_surface(surf, 2.0, true, true);
EXPECT_TRUE(solid.is_valid());
EXPECT_GT(solid.num_faces(), 0u);
}
// ═══════════════════════════════════════════════════════════
// sew_surfaces
// ═══════════════════════════════════════════════════════════
TEST(SurfaceToolsTest, SewSurfaces_Basic) {
auto s1 = make_planar_surface();
// Create a second surface offset slightly
std::vector<std::vector<Point3D>> grid2 = {
{Point3D(5, 0, 0), Point3D(10, 0, 0)},
{Point3D(5, 5, 0), Point3D(10, 5, 0)},
};
std::vector<double> ku = {0, 0, 1, 1};
std::vector<double> kv = {0, 0, 1, 1};
std::vector<std::vector<double>> w2 = {{1, 1}, {1, 1}};
NurbsSurface s2(grid2, ku, kv, w2, 1, 1);
std::vector<NurbsSurface> patches = {s1, s2};
auto result = sew_surfaces(patches, 1e-3);
EXPECT_TRUE(result.is_valid());
EXPECT_GT(result.num_faces(), 0u);
}
TEST(SurfaceToolsTest, SewSurfaces_SingleSurface) {
auto s1 = make_planar_surface();
std::vector<NurbsSurface> patches = {s1};
auto result = sew_surfaces(patches);
EXPECT_TRUE(result.is_valid());
EXPECT_GE(result.num_faces(), 1u);
}
TEST(SurfaceToolsTest, SewSurfaces_EmptyInput) {
std::vector<NurbsSurface> patches;
auto result = sew_surfaces(patches);
// Empty input: should still return a valid (but empty) model
// Or at minimum not crash
SUCCEED();
}
// 缝合的真实语义:相邻曲面的公共边界必须焊成同一顶点/同一条边。
// 回归"只把顶点/边按索引偏移拼进同一模型、不检测重合"的伪缝合——那种实现
// 两个共享边的四边形会有 8 顶点/8 边(应 6 顶点/7 边),壳必然漏水。
TEST(SurfaceToolsTest, SewSurfaces_WeldsSharedBoundary) {
auto s1 = make_planar_surface(); // x∈[0,5], y∈[0,5]
std::vector<std::vector<Point3D>> grid2 = {
{Point3D(5, 0, 0), Point3D(10, 0, 0)},
{Point3D(5, 5, 0), Point3D(10, 5, 0)},
};
std::vector<double> ku = {0, 0, 1, 1};
std::vector<double> kv = {0, 0, 1, 1};
std::vector<std::vector<double>> w2 = {{1, 1}, {1, 1}};
NurbsSurface s2(grid2, ku, kv, w2, 1, 1);
auto result = sew_surfaces({s1, s2}, 1e-6);
// 6 个唯一角点(共享边上的 (5,0)/(5,5) 各只保留一份)
EXPECT_EQ(result.num_vertices(), 6u);
// 7 条唯一边(两条各自 4 条,公共边界 1 条重合)
EXPECT_EQ(result.num_edges(), 7u);
// 共用边必须被两个面共享(缝合成功的判定)。
// 注意 edge_faces 取的是边的**数组下标**(环里存下标),非 TopoEdge::id。
int shared_edges = 0;
for (size_t ei = 0; ei < result.num_edges(); ++ei) {
if (result.edge_faces(static_cast<int>(ei)).size() == 2) ++shared_edges;
}
EXPECT_EQ(shared_edges, 1);
}
// ═══════════════════════════════════════════════════════════
// boundary_surface
// ═══════════════════════════════════════════════════════════
TEST(SurfaceToolsTest, BoundarySurface_Quad) {
// Create 4 bounding curves forming a rectangle
auto c0 = NurbsCurve::line(Point3D(0, 0, 0), Point3D(10, 0, 0));
auto c1 = NurbsCurve::line(Point3D(10, 0, 0), Point3D(10, 6, 0));
auto c2 = NurbsCurve::line(Point3D(10, 6, 0), Point3D(0, 6, 0));
auto c3 = NurbsCurve::line(Point3D(0, 6, 0), Point3D(0, 0, 0));
std::vector<NurbsCurve> curves = {c0, c1, c2, c3};
auto surf = boundary_surface(curves);
// Should produce a valid surface
EXPECT_GT(surf.evaluate(0.5, 0.5).z(), -1e-6); // Surface stays in XY plane
}
TEST(SurfaceToolsTest, BoundarySurface_Triangle) {
auto c0 = NurbsCurve::line(Point3D(0, 0, 0), Point3D(5, 0, 0));
auto c1 = NurbsCurve::line(Point3D(5, 0, 0), Point3D(0, 5, 0));
auto c2 = NurbsCurve::line(Point3D(0, 5, 0), Point3D(0, 0, 0));
std::vector<NurbsCurve> curves = {c0, c1, c2};
auto surf = boundary_surface(curves);
// 3-sided boundary should also work
auto mid = surf.evaluate(0.5, 0.5);
EXPECT_FALSE(std::isnan(mid.x()));
}
// ═══════════════════════════════════════════════════════════
// fill_surface
// ═══════════════════════════════════════════════════════════
TEST(SurfaceToolsTest, FillSurface_Quad) {
auto c0 = NurbsCurve::line(Point3D(0, 0, 0), Point3D(5, 0, 0));
auto c1 = NurbsCurve::line(Point3D(5, 0, 0), Point3D(5, 5, 1));
auto c2 = NurbsCurve::line(Point3D(5, 5, 1), Point3D(0, 5, 1));
auto c3 = NurbsCurve::line(Point3D(0, 5, 1), Point3D(0, 0, 0));
std::vector<NurbsCurve> boundary = {c0, c1, c2, c3};
auto fill = fill_surface(boundary);
// Fill should produce a C² continuous surface
auto center = fill.evaluate(0.5, 0.5);
EXPECT_FALSE(std::isnan(center.x()));
EXPECT_FALSE(std::isnan(center.y()));
EXPECT_FALSE(std::isnan(center.z()));
}
TEST(SurfaceToolsTest, FillSurface_Triangle) {
auto c0 = NurbsCurve::line(Point3D(0, 0, 0), Point3D(4, 0, 0));
auto c1 = NurbsCurve::line(Point3D(4, 0, 0), Point3D(0, 4, 0));
auto c2 = NurbsCurve::line(Point3D(0, 4, 0), Point3D(0, 0, 0));
std::vector<NurbsCurve> boundary = {c0, c1, c2};
auto fill = fill_surface(boundary);
auto center = fill.evaluate(0.5, 0.5);
EXPECT_FALSE(std::isnan(center.x()));
}
TEST(SurfaceToolsTest, FillSurface_Pentagon) {
// 5-sided boundary for N-sided fill
auto c0 = NurbsCurve::line(Point3D(0, 0, 0), Point3D(3, 0, 0));
auto c1 = NurbsCurve::line(Point3D(3, 0, 0), Point3D(4, 2, 0));
auto c2 = NurbsCurve::line(Point3D(4, 2, 0), Point3D(2, 4, 0));
auto c3 = NurbsCurve::line(Point3D(2, 4, 0), Point3D(0, 3, 0));
auto c4 = NurbsCurve::line(Point3D(0, 3, 0), Point3D(0, 0, 0));
std::vector<NurbsCurve> boundary = {c0, c1, c2, c3, c4};
auto fill = fill_surface(boundary);
auto mid = fill.evaluate(0.5, 0.5);
EXPECT_FALSE(std::isnan(mid.x()));
EXPECT_FALSE(std::isnan(mid.y()));
}
// ═══════════════════════════════════════════════════════════
// developable_unwrap (F7)
// ═══════════════════════════════════════════════════════════
/// NURBS 圆柱(沿 Z 轴,半径 r,高度 h,中心在原点;u 为圆周方向,v 为母线方向)
static NurbsSurface make_cyl(double r = 1.0, double h = 2.0) {
double hh = h * 0.5;
double w = std::sqrt(2.0) / 2.0;
std::vector<Point3D> bottom = {
{ r, 0, -hh}, { r, r, -hh}, { 0, r, -hh}, {-r, r, -hh},
{-r, 0, -hh}, {-r,-r, -hh}, { 0,-r, -hh}, { r,-r, -hh},
{ r, 0, -hh}
};
std::vector<Point3D> top = {
{ r, 0, hh}, { r, r, hh}, { 0, r, hh}, {-r, r, hh},
{-r, 0, hh}, {-r,-r, hh}, { 0,-r, hh}, { r,-r, hh},
{ r, 0, hh}
};
std::vector<std::vector<Point3D>> grid;
std::vector<std::vector<double>> weights;
std::vector<double> uw = {1, w, 1, w, 1, w, 1, w, 1};
for (int i = 0; i < 9; ++i) {
grid.push_back({bottom[i], top[i]});
weights.push_back({uw[i], uw[i]});
}
return NurbsSurface(
grid,
{0,0,0, 0.25,0.25, 0.5,0.5, 0.75,0.75, 1,1,1},
{0,0,1,1},
weights,
2, 1);
}
/// NURBS 直圆锥(顶点在 v=0 原点,底圆半径 r,高 h;u 为圆周方向,v 为母线方向)
static NurbsSurface make_cone(double r = 1.0, double h = 1.0) {
double w = std::sqrt(2.0) / 2.0;
std::vector<Point3D> apex(9, Point3D(0, 0, 0));
std::vector<Point3D> base = {
{ r, 0, h}, { r, r, h}, { 0, r, h}, {-r, r, h},
{-r, 0, h}, {-r,-r, h}, { 0,-r, h}, { r,-r, h},
{ r, 0, h}
};
std::vector<std::vector<Point3D>> grid;
std::vector<std::vector<double>> weights;
std::vector<double> uw = {1, w, 1, w, 1, w, 1, w, 1};
for (int i = 0; i < 9; ++i) {
grid.push_back({apex[i], base[i]});
weights.push_back({uw[i], uw[i]});
}
return NurbsSurface(
grid,
{0,0,0, 0.25,0.25, 0.5,0.5, 0.75,0.75, 1,1,1},
{0,0,1,1},
weights,
2, 1);
}
TEST(SurfaceToolsTest, DevelopableUnwrap_CylinderToRectangle) {
auto cyl = make_cyl(1.0, 2.0);
auto res = developable_unwrap(cyl, 32);
ASSERT_TRUE(res.is_developable);
ASSERT_EQ(res.boundary_a.size(), 33u);
ASSERT_EQ(res.boundary_b.size(), 33u);
// 全部落在平面 z=0
for (const auto& p : res.boundary_a) EXPECT_NEAR(p.z(), 0.0, 1e-12);
for (const auto& p : res.boundary_b) EXPECT_NEAR(p.z(), 0.0, 1e-12);
// 边界 A 沿 +x 直线(母线底端),边界 B 平行于 A 且 y = 母线长 = 2
EXPECT_NEAR(res.boundary_a[0].x(), 0.0, 1e-12);
for (size_t i = 0; i < res.boundary_a.size(); ++i) {
EXPECT_NEAR(res.boundary_a[i].y(), 0.0, 1e-9);
EXPECT_NEAR(res.boundary_b[i].y(), 2.0, 1e-9);
}
// 宽度 = 周长(2πr),误差 < 1e-6
double width = res.boundary_a.back().x() - res.boundary_a.front().x();
EXPECT_NEAR(width, 2.0 * M_PI, 1e-6);
// 母线长度保持 = 高 2
for (size_t i = 0; i < res.boundary_a.size(); ++i) {
EXPECT_NEAR((res.boundary_b[i] - res.boundary_a[i]).norm(), 2.0, 1e-9);
}
// 圆柱母线方向恒定 → 无展开角度误差
EXPECT_LT(res.max_angle_error, 1e-12);
// guideline 为第一条母线(起点/终点)
ASSERT_EQ(res.guideline.size(), 2u);
EXPECT_NEAR((res.guideline[1] - res.guideline[0]).norm(), 2.0, 1e-9);
}
TEST(SurfaceToolsTest, DevelopableUnwrap_ConeToSector) {
auto cone = make_cone(1.0, 1.0); // 底半径 1,高 1 → 母线长 √2
int n = 256;
auto res = developable_unwrap(cone, n);
ASSERT_TRUE(res.is_developable);
ASSERT_EQ(res.boundary_a.size(), static_cast<size_t>(n) + 1);
ASSERT_EQ(res.boundary_b.size(), static_cast<size_t>(n) + 1);
double slant = std::sqrt(2.0);
// A 链 = 顶点:展开后集中在原点
for (size_t i = 0; i < res.boundary_a.size(); ++i) {
EXPECT_NEAR(res.boundary_a[i].norm(), 0.0, 1e-9);
}
// 半径 = 母线长 √2
for (size_t i = 0; i < res.boundary_b.size(); ++i) {
EXPECT_NEAR(res.boundary_b[i].norm(), slant, 1e-6);
}
// 母线长度保持
for (size_t i = 0; i < res.boundary_a.size(); ++i) {
EXPECT_NEAR((res.boundary_b[i] - res.boundary_a[i]).norm(), slant, 1e-9);
}
// 逐段保长:展开弧段长 = 3D 底圆弦段长(等距性,离散误差仅浮点级)
double arc2d = 0.0;
for (int i = 1; i <= n; ++i) {
double d2 = (res.boundary_b[i] - res.boundary_b[i - 1]).norm();
double u0 = static_cast<double>(i - 1) / n;
double u1 = static_cast<double>(i) / n;
double d3 = (cone.evaluate(u1, 1.0) - cone.evaluate(u0, 1.0)).norm();
EXPECT_NEAR(d2, d3, 1e-9);
arc2d += d2;
}
// 展开弧长 ≈ 底圆周长 2πr(弦长求和,误差 < 1e-3)
EXPECT_NEAR(arc2d, 2.0 * M_PI, 1e-3);
// 圆锥母线方向连续旋转 → 角度误差 ≈ 0
EXPECT_LT(res.max_angle_error, 1e-12);
}
TEST(SurfaceToolsTest, DevelopableUnwrap_RejectsCurvedSurface) {
// 非可展曲面(有高斯曲率)→ is_developable = false
auto surf = make_curved_surface();
auto res = developable_unwrap(surf, 16);
EXPECT_FALSE(res.is_developable);
}
// ═══════════════════════════════════════════════════════════
// surface_to_brep / thicken_surface_ex (VDE-341)
// ═══════════════════════════════════════════════════════════
TEST(SurfaceToolsTest, SurfaceToBrep_MinimalTopology) {
auto surf = make_planar_surface();
auto m = surface_to_brep(surf);
EXPECT_TRUE(m.is_valid());
EXPECT_EQ(m.num_faces(), 1u);
EXPECT_EQ(m.num_edges(), 4u);
EXPECT_EQ(m.num_vertices(), 4u);
}
TEST(SurfaceToolsTest, ThickenSurfaceEx_ZeroDraftMatchesBasic) {
auto surf = make_planar_surface();
auto base = thicken_surface(surf, 2.0, false, true);
ThickenOptions opts;
opts.thickness = 2.0;
opts.both_sides = false;
opts.cap_ends = true;
opts.draft_angle_deg = 0.0; // 无拔模 → 直接用法向偏移面(等价 thicken_surface)
auto ex = thicken_surface_ex(surf, opts);
EXPECT_TRUE(ex.is_valid());
EXPECT_EQ(ex.num_faces(), 6u);
EXPECT_EQ(ex.num_faces(), base.num_faces());
}
TEST(SurfaceToolsTest, ThickenSurfaceEx_NoCaps) {
auto surf = make_planar_surface();
ThickenOptions opts;
opts.thickness = 2.0;
opts.cap_ends = false; // 不封闭侧壁 → 只留上下两面
auto ex = thicken_surface_ex(surf, opts);
EXPECT_TRUE(ex.is_valid());
EXPECT_EQ(ex.num_faces(), 2u);
}
TEST(SurfaceToolsTest, ThickenSurfaceEx_DraftTaperExpandsFootprint) {
auto surf = make_planar_surface(); // 5×5 平面,z=0
ThickenOptions opts;
opts.thickness = 2.0;
opts.cap_ends = true;
opts.draft_angle_deg = 45.0;
opts.draft_direction = Vector3D(0, 0, 1);
ThickenOptions base_opts = opts;
base_opts.draft_angle_deg = 0.0;
auto with_draft = thicken_surface_ex(surf, opts);
auto no_draft = thicken_surface_ex(surf, base_opts);
EXPECT_TRUE(with_draft.is_valid());
EXPECT_EQ(with_draft.num_faces(), 6u);
// 拔模后壳体仍水密:每条边恰有 2 个相邻面(Coons 顶面与侧壁严格共享边界)
for (size_t ei = 0; ei < with_draft.num_edges(); ++ei)
EXPECT_EQ(with_draft.edge_faces(static_cast<int>(ei)).size(), 2u);
// 拔模使顶面边界沿横向外移 2·tan45° = 2 → 平面内投影严格变大
const auto ed = with_draft.bounds().extent();
const auto en = no_draft.bounds().extent();
EXPECT_GT(ed.x(), en.x() + 1.0);
EXPECT_GT(ed.y(), en.y() + 1.0);
}
TEST(SurfaceToolsTest, ThickenSurfaceEx_InvalidInput) {
auto surf = make_planar_surface();
ThickenOptions bad;
bad.thickness = 0.0;
EXPECT_THROW(thicken_surface_ex(surf, bad), std::invalid_argument);
ThickenOptions bad2;
bad2.thickness = 1.0;
bad2.draft_angle_deg = 90.0;
EXPECT_THROW(thicken_surface_ex(surf, bad2), std::invalid_argument);
}
// ═══════════════════════════════════════════════════════════
// extract_sub_surface / split_surface — 参数域真实裁剪内核 (VDE-893)
// ═══════════════════════════════════════════════════════════
namespace {
/// 曲面某方向的有效参数域 [knots[p], knots[n-p-1]]
std::pair<double, double> domain_of(const NurbsSurface& s, bool is_u) {
const auto& K = is_u ? s.knots_u() : s.knots_v();
const int p = is_u ? s.degree_u() : s.degree_v();
if (K.size() < static_cast<size_t>(p) + 2) return {0.0, 0.0};
return {K[static_cast<size_t>(p)], K[K.size() - p - 1]};
}
/// 方向是否在域两端各以 p+1 重钳位
bool is_clamped(const NurbsSurface& s, bool is_u) {
const auto& K = is_u ? s.knots_u() : s.knots_v();
const int p = is_u ? s.degree_u() : s.degree_v();
if (K.size() < static_cast<size_t>(2 * (p + 1))) return false;
for (int i = 0; i < p + 1; ++i) {
if (K[static_cast<size_t>(i)] != K[0]) return false;
if (K[K.size() - 1 - i] != K.back()) return false;
}
return true;
}
} // namespace
TEST(SurfaceToolsTest, ExtractSubSurface_MatchesOriginalPointwise) {
auto s = make_curved_surface(); // 双二次、单跨
auto sub = extract_sub_surface(s, 0.25, 0.75, 0.25, 0.75);
auto ncp = sub.num_control_points();
ASSERT_GT(ncp[0], 0);
ASSERT_GT(ncp[1], 0);
// 子域边界 = 请求值,且两端 p+1 重钳位
auto [u0, u1] = domain_of(sub, true);
auto [v0, v1] = domain_of(sub, false);
EXPECT_NEAR(u0, 0.25, 1e-12);
EXPECT_NEAR(u1, 0.75, 1e-12);
EXPECT_NEAR(v0, 0.25, 1e-12);
EXPECT_NEAR(v1, 0.75, 1e-12);
EXPECT_TRUE(is_clamped(sub, true));
EXPECT_TRUE(is_clamped(sub, false));
// 逐点独立复算:子曲面在整个子域上与源曲面严格一致(节点插入保形)
double max_diff = 0.0;
for (int i = 0; i <= 24; ++i)
for (int j = 0; j <= 24; ++j) {
const double u = 0.25 + 0.5 * i / 24.0;
const double v = 0.25 + 0.5 * j / 24.0;
max_diff = std::max(max_diff,
(sub.evaluate(u, v) - s.evaluate(u, v)).norm());
}
EXPECT_LT(max_diff, 1e-12);
}
TEST(SurfaceToolsTest, ExtractSubSurface_ReducesControlPointsToSingleSpan) {
auto cyl = make_cyl(1.0, 2.0); // u: 9 CP / 4 跨;v: 2 CP / 1 跨
auto n0 = cyl.num_control_points();
ASSERT_EQ(n0[0], 9);
// 取 u∈[0,0.25](恰好一个跨)→ u 向控制点应降为 p+1 = 3
auto sub = extract_sub_surface(cyl, 0.0, 0.25, 0.0, 1.0);
auto ncp = sub.num_control_points();
EXPECT_EQ(ncp[0], 3);
EXPECT_EQ(ncp[1], 2);
double max_diff = 0.0;
for (int i = 0; i <= 16; ++i)
for (int j = 0; j <= 4; ++j) {
const double u = 0.25 * i / 16.0;
const double v = static_cast<double>(j) / 4.0;
max_diff = std::max(max_diff,
(sub.evaluate(u, v) - cyl.evaluate(u, v)).norm());
}
EXPECT_LT(max_diff, 1e-12);
}
TEST(SurfaceToolsTest, ExtractSubSurface_FullDomainEqualsOriginal) {
auto s = make_curved_surface();
auto sub = extract_sub_surface(s, 0.0, 1.0, 0.0, 1.0);
auto a = s.num_control_points();
auto b = sub.num_control_points();
EXPECT_EQ(a[0], b[0]);
EXPECT_EQ(a[1], b[1]);
for (int i = 0; i <= 8; ++i)
for (int j = 0; j <= 8; ++j) {
const double u = i / 8.0, v = j / 8.0;
EXPECT_LT((sub.evaluate(u, v) - s.evaluate(u, v)).norm(), 1e-12);
}
}
TEST(SurfaceToolsTest, ExtractSubSurface_DegenerateAndInvalidRange) {
NurbsSurface empty;
EXPECT_EQ(extract_sub_surface(empty, 0.1, 0.9, 0.1, 0.9).num_control_points()[0],
0);
auto s = make_curved_surface();
// 零宽区间(u0 == u1)→ 退化返回
EXPECT_EQ(extract_sub_surface(s, 0.5, 0.5, 0.1, 0.9).num_control_points()[0], 0);
// 零宽区间(v0 == v1)→ 退化返回
EXPECT_EQ(extract_sub_surface(s, 0.1, 0.9, 0.5, 0.5).num_control_points()[0], 0);
// 完全越域(钳制后宽度为 0)→ 退化返回
EXPECT_EQ(extract_sub_surface(s, 2.0, 3.0, 0.1, 0.9).num_control_points()[0], 0);
// 反向区间 (u1 < u0) 视为无序输入:交换后按合法子域提取,结果非空且与正向一致
auto rev = extract_sub_surface(s, 0.7, 0.3, 0.1, 0.9);
auto fwd = extract_sub_surface(s, 0.3, 0.7, 0.1, 0.9);
EXPECT_GT(rev.num_control_points()[0], 0);
auto [r0, r1] = domain_of(rev, true);
auto [f0, f1] = domain_of(fwd, true);
EXPECT_NEAR(r0, f0, 1e-12);
EXPECT_NEAR(r1, f1, 1e-12);
}
// ── split_surface:等参线真实分割(替换原"仅重标定节点向量、控制点原封不动"的假实现)
TEST(SurfaceToolsTest, SplitSurface_ConstantVLine_ProducesTwoExactHalves) {
auto s = make_planar_surface();
// p-curve:参数域内的常 v = 0.5 横线((u,v,0):u 从 0 到 1)
NurbsCurve cut({Point3D(0.0, 0.5, 0.0), Point3D(1.0, 0.5, 0.0)},
{0, 0, 1, 1}, {1, 1}, 1);
auto [lo, hi] = split_surface(s, cut);
ASSERT_GT(lo.num_control_points()[0], 0);
ASSERT_GT(hi.num_control_points()[0], 0);
// 两片 v 域互补:[v0, 0.5] 与 [0.5, v1];u 域保持全域
auto [lu0, lu1] = domain_of(lo, true);
auto [lv0, lv1] = domain_of(lo, false);
auto [hu0, hu1] = domain_of(hi, true);
auto [hv0, hv1] = domain_of(hi, false);
EXPECT_NEAR(lu0, 0.0, 1e-12);
EXPECT_NEAR(lu1, 1.0, 1e-12);
EXPECT_NEAR(lv0, 0.0, 1e-12);
EXPECT_NEAR(lv1, 0.5, 1e-12);
EXPECT_NEAR(hu0, 0.0, 1e-12);
EXPECT_NEAR(hu1, 1.0, 1e-12);
EXPECT_NEAR(hv0, 0.5, 1e-12);
EXPECT_NEAR(hv1, 1.0, 1e-12);
// 逐点独立复算:各片在其子域上与源曲面严格一致(真分割,非重标定)
for (int i = 0; i <= 10; ++i)
for (int j = 0; j <= 10; ++j) {
const double u = i / 10.0;
const double vl = 0.5 * j / 10.0;
const double vh = 0.5 + 0.5 * j / 10.0;
EXPECT_LT((lo.evaluate(u, vl) - s.evaluate(u, vl)).norm(), 1e-12);
EXPECT_LT((hi.evaluate(u, vh) - s.evaluate(u, vh)).norm(), 1e-12);
}
// 公共分割线:两片在 v=0.5 处与源曲面重合(G0 缝合)
for (int i = 0; i <= 10; ++i) {
const double u = i / 10.0;
EXPECT_LT((lo.evaluate(u, 0.5) - s.evaluate(u, 0.5)).norm(), 1e-12);
EXPECT_LT((hi.evaluate(u, 0.5) - s.evaluate(u, 0.5)).norm(), 1e-12);
}
}
TEST(SurfaceToolsTest, SplitSurface_ConstantULine_ProducesTwoExactHalves) {
auto s = make_curved_surface();
NurbsCurve cut({Point3D(0.4, 0.0, 0.0), Point3D(0.4, 1.0, 0.0)},
{0, 0, 1, 1}, {1, 1}, 1); // 常 u = 0.4
auto [lo, hi] = split_surface(s, cut);
auto [lu0, lu1] = domain_of(lo, true);
auto [hu0, hu1] = domain_of(hi, true);
EXPECT_NEAR(lu0, 0.0, 1e-12);
EXPECT_NEAR(lu1, 0.4, 1e-12);
EXPECT_NEAR(hu0, 0.4, 1e-12);
EXPECT_NEAR(hu1, 1.0, 1e-12);
for (int i = 0; i <= 10; ++i)
for (int j = 0; j <= 10; ++j) {
const double v = j / 10.0;
const double ul = 0.4 * i / 10.0;
const double uh = 0.4 + 0.6 * i / 10.0;
EXPECT_LT((lo.evaluate(ul, v) - s.evaluate(ul, v)).norm(), 1e-12);
EXPECT_LT((hi.evaluate(uh, v) - s.evaluate(uh, v)).norm(), 1e-12);
}
}
TEST(SurfaceToolsTest, SplitSurface_NonIsoCurve_Rejected) {
auto s = make_planar_surface();
// 对角线(u、v 均变化)→ 非等参,NurbsSurface 无法表示裁剪子域
NurbsCurve diag({Point3D(0.0, 0.0, 0.0), Point3D(1.0, 1.0, 0.0)},
{0, 0, 1, 1}, {1, 1}, 1);
EXPECT_THROW(split_surface(s, diag), std::invalid_argument);
}
// ═══════════════════════════════════════════════════════════
// Wave2-B4:A 级曲面深度增强回归
// (多面片 G2/G3 拼接 / 能量光顺 / 质量度量 / 公差化匹配)
// ═══════════════════════════════════════════════════════════
#include "vde/curves/surface_fairing.h"
#include "vde/curves/surface_analysis.h"
#include <array>
#include <vector>
namespace {
std::vector<double> b4_knots(int ncp, int deg) {
std::vector<double> k(static_cast<size_t>(ncp + deg + 1), 0.0);
const int inner = ncp - deg - 1;
for (int i = 0; i <= deg; ++i) k[static_cast<size_t>(i)] = 0.0;
for (int i = ncp; i < ncp + deg + 1; ++i) k[static_cast<size_t>(i)] = 1.0;
for (int i = 1; i <= inner; ++i)
k[static_cast<size_t>(deg + i)] = static_cast<double>(i) / (inner + 1);
return k;
}
NurbsSurface b4_wavy(int n = 8, int deg = 3) {
const auto k = b4_knots(n, deg);
std::vector<std::vector<Point3D>> cp(static_cast<size_t>(n),
std::vector<Point3D>(static_cast<size_t>(n)));
std::vector<std::vector<double>> w(static_cast<size_t>(n),
std::vector<double>(static_cast<size_t>(n), 1.0));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) {
const double u = static_cast<double>(i) / (n - 1);
const double v = static_cast<double>(j) / (n - 1);
cp[static_cast<size_t>(i)][static_cast<size_t>(j)] =
Point3D(u, v, 0.35 * std::sin(2.0 * M_PI * u) * std::sin(M_PI * v) +
0.10 * u * v);
}
return NurbsSurface(cp, k, k, w, deg, deg);
}
NurbsSurface b4_planar(Point3D o, Vector3D du, Vector3D dv) {
const std::vector<double> k = {0, 0, 0, 0, 1, 1, 1, 1};
std::vector<std::vector<Point3D>> cp(4, std::vector<Point3D>(4));
std::vector<std::vector<double>> w(4, std::vector<double>(4, 1.0));
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j) {
const double a = i / 3.0, b = j / 3.0;
cp[static_cast<size_t>(i)][static_cast<size_t>(j)] =
Point3D(o.x() + a * du.x() + b * dv.x(),
o.y() + a * du.y() + b * dv.y(),
o.z() + a * du.z() + b * dv.z());
}
return NurbsSurface(cp, k, k, w, 3, 3);
}
NurbsSurface b4_transpose(const NurbsSurface& s) {
const int nu = s.num_control_points()[0], nv = s.num_control_points()[1];
std::vector<std::vector<Point3D>> cp(static_cast<size_t>(nv),
std::vector<Point3D>(static_cast<size_t>(nu)));
std::vector<std::vector<double>> w(static_cast<size_t>(nv),
std::vector<double>(static_cast<size_t>(nu), 1.0));
for (int i = 0; i < nu; ++i)
for (int j = 0; j < nv; ++j) {
cp[static_cast<size_t>(j)][static_cast<size_t>(i)] =
s.control_points()[static_cast<size_t>(i)][static_cast<size_t>(j)];
if (!s.weights().empty())
w[static_cast<size_t>(j)][static_cast<size_t>(i)] =
s.weights()[static_cast<size_t>(i)][static_cast<size_t>(j)];
}
return NurbsSurface(cp, s.knots_v(), s.knots_u(), w, s.degree_v(), s.degree_u());
}
void b4_expect_seam_le(const SeamContinuityMetrics& m, double tol) {
EXPECT_LE(m.position_error, tol);
EXPECT_LE(m.tangent_angle, tol);
EXPECT_LE(m.normal_angle, tol);
EXPECT_LE(m.cross_curvature_error, tol);
EXPECT_LE(m.along_curvature_error, tol);
EXPECT_LE(m.twist_error, tol);
EXPECT_TRUE(m.g0);
EXPECT_TRUE(m.g1);
EXPECT_TRUE(m.g2);
}
bool b4_no_fold(const NurbsSurface& s, int n = 20) {
const auto& ku = s.knots_u();
const auto& kv = s.knots_v();
if (ku.empty() || kv.empty()) return false;
const double u0 = ku[static_cast<size_t>(s.degree_u())];
const double u1 = ku[static_cast<size_t>(s.num_control_points()[0])];
const double v0 = kv[static_cast<size_t>(s.degree_v())];
const double v1 = kv[static_cast<size_t>(s.num_control_points()[1])];
Vector3D ref;
bool have = false;
for (int i = 0; i <= n; ++i)
for (int j = 0; j <= n; ++j) {
const double u = u0 + (u1 - u0) * i / n;
const double v = v0 + (v1 - v0) * j / n;
Vector3D nrm = s.derivative_u(u, v).cross(s.derivative_v(u, v));
if (nrm.norm() < 1e-12) return false;
nrm.normalize();
if (!have) { ref = nrm; have = true; }
if (nrm.dot(ref) < 0.0) return false;
}
return true;
}
void b4_model_area_volume(const vde::brep::BrepModel& m, double& area, double& vol) {
area = 0.0;
vol = 0.0;
std::vector<Point3D> vs;
std::vector<std::array<int, 3>> ts;
std::vector<std::array<Point3D, 3>> tris;
for (int f = 0; f < static_cast<int>(m.num_faces()); ++f) {
vs.clear();
ts.clear();
m.mesh_face_diag(f, 0.02, vs, ts);
for (const auto& t : ts)
tris.push_back({vs[static_cast<size_t>(t[0])], vs[static_cast<size_t>(t[1])],
vs[static_cast<size_t>(t[2])]});
}
Point3D c(0, 0, 0);
for (const auto& t : tris) c += (t[0] + t[1] + t[2]) / 3.0;
if (!tris.empty()) c /= static_cast<double>(tris.size());
double v = 0.0;
for (auto t : tris) {
area += 0.5 * (t[1] - t[0]).cross(t[2] - t[0]).norm();
const Vector3D nrm = (t[1] - t[0]).cross(t[2] - t[0]);
const Point3D tc = (t[0] + t[1] + t[2]) / 3.0;
if (nrm.dot(tc - c) < 0.0) std::swap(t[1], t[2]);
v += t[0].dot(Vector3D(t[1].cross(t[2]))) / 6.0;
}
vol = std::abs(v);
}
NurbsSurface b4_perturbed_continuation(const NurbsSurface& a, const NurbsSurface& b) {
auto cp = b.control_points();
const size_t nv = cp[1].size();
for (size_t j = 0; j < nv; ++j) {
const double t = static_cast<double>(j) / (nv - 1);
cp[1][j] += Vector3D(0.02 * std::sin(M_PI * t), 0.0, 0.05 * std::sin(M_PI * t));
}
(void)a;
return NurbsSurface(cp, b.knots_u(), b.knots_v(), b.weights(), b.degree_u(),
b.degree_v());
}
} // namespace
TEST(SurfaceB4Stitch, G2AndG3ExactSeam) {
const NurbsSurface parent = b4_wavy();
const NurbsSurface a = extract_sub_surface(parent, 0.0, 0.5, 0.0, 1.0);
const NurbsSurface b = extract_sub_surface(parent, 0.5, 1.0, 0.0, 1.0);
// 未扰动的 split 面片本身是 G2/G3
b4_expect_seam_le(measure_seam_continuity(a, 1, b, 0, 41, 1e-6), 1e-6);
// 扰动后 G1/G2 破坏
const NurbsSurface bad = b4_perturbed_continuation(a, b);
const auto m_bad = measure_seam_continuity(a, 1, bad, 0, 41, 1e-6);
EXPECT_GT(m_bad.tangent_angle, 1e-3);
EXPECT_FALSE(m_bad.g2);
// G2 拼接:公共边切向/曲率差 ≤1e-6,内部偏差 = 0
SurfaceStitchOptions opt2;
opt2.order = 2;
const auto st2 = stitch_surfaces_continuous(bad, 0, a, 1, opt2);
ASSERT_TRUE(st2.ok) << st2.message;
EXPECT_EQ(st2.rows_modified, 3);
EXPECT_LE(st2.interior_deviation, 1e-12);
b4_expect_seam_le(st2.seam, 1e-6);
// G3(尽力):4 排控制点,仍满足 ≤1e-6
SurfaceStitchOptions opt3;
opt3.order = 3;
const auto st3 = stitch_surfaces_continuous(bad, 0, a, 1, opt3);
ASSERT_TRUE(st3.ok) << st3.message;
EXPECT_EQ(st3.rows_modified, 4);
EXPECT_LE(st3.interior_deviation, 1e-12);
b4_expect_seam_le(st3.seam, 1e-6);
}
TEST(SurfaceB4Stitch, InteriorShapePreserved) {
const NurbsSurface parent = b4_wavy();
const NurbsSurface a = extract_sub_surface(parent, 0.0, 0.5, 0.0, 1.0);
const NurbsSurface b = extract_sub_surface(parent, 0.5, 1.0, 0.0, 1.0);
const NurbsSurface bad = b4_perturbed_continuation(a, b);
SurfaceStitchOptions opt;
opt.order = 2;
const auto st = stitch_surfaces_continuous(bad, 0, a, 1, opt);
ASSERT_TRUE(st.ok);
// 未修改排(第 3 排及以后)控制点逐位相同
const auto cp_old = bad.control_points();
const auto cp_new = st.matched_surface.control_points();
for (size_t i = 3; i < cp_old.size(); ++i)
for (size_t j = 0; j < cp_old[i].size(); ++j)
EXPECT_LE((cp_new[i][j] - cp_old[i][j]).norm(), 1e-15);
// 未修改区域逐点一致
EXPECT_LE(st.interior_deviation, 1e-12);
}
TEST(SurfaceB4Quality, ContinuityReportVerdicts) {
const NurbsSurface parent = b4_wavy();
const NurbsSurface a = extract_sub_surface(parent, 0.0, 0.5, 0.0, 1.0);
const NurbsSurface b = extract_sub_surface(parent, 0.5, 1.0, 0.0, 1.0);
// 精确 split 面片:G0/G1/G2 全通过,斑马连续,曲率梳无跳变
const auto exact = surface_quality_report(a, 1, b, 0, 1e-6, 41);
EXPECT_TRUE(exact.seam.g0);
EXPECT_TRUE(exact.seam.g1);
EXPECT_TRUE(exact.seam.g2);
EXPECT_TRUE(exact.zebra_continuous);
EXPECT_LE(exact.curvature_comb_max_jump, 1e-6);
EXPECT_LE(exact.patch_a.gaussian_min, exact.patch_a.gaussian_max);
EXPECT_LE(exact.patch_a.mean_min, exact.patch_a.mean_max);
// 扰动面片:G1/G2 不通过,曲率梳跳变明显
const NurbsSurface bad = b4_perturbed_continuation(a, b);
const auto qbad = surface_quality_report(a, 1, bad, 0, 1e-6, 41);
EXPECT_TRUE(qbad.seam.g0);
EXPECT_FALSE(qbad.seam.g1);
EXPECT_FALSE(qbad.seam.g2);
EXPECT_FALSE(qbad.zebra_continuous);
EXPECT_GT(qbad.curvature_comb_max_jump, 1e-3);
// 拼接后:G2 + 斑马连续
SurfaceStitchOptions opt;
opt.order = 2;
const auto st = stitch_surfaces_continuous(bad, 0, a, 1, opt);
ASSERT_TRUE(st.ok);
const auto qfix = surface_quality_report(a, 1, st.matched_surface, 0, 1e-6, 41);
EXPECT_TRUE(qfix.seam.g1);
EXPECT_TRUE(qfix.seam.g2);
EXPECT_TRUE(qfix.zebra_continuous);
EXPECT_LE(qfix.curvature_comb_max_jump, 1e-6);
}
TEST(SurfaceB4Quality, MixedAxisSeamAndMultiPatchReport) {
// 混合轴(u-edge ↔ v-edge)公共边:转置面片后 G2 仍成立
const NurbsSurface parent = b4_planar(Point3D(0, 0, 0), Vector3D(2, 0, 0),
Vector3D(0, 2, 0));
const NurbsSurface a = extract_sub_surface(parent, 0.0, 0.5, 0.0, 1.0);
const NurbsSurface b = extract_sub_surface(parent, 0.5, 1.0, 0.0, 1.0);
const NurbsSurface bt = b4_transpose(b); // 公共边变为 v-edge
const auto mixed = surface_quality_report(a, 1, bt, 2, 1e-6, 41);
EXPECT_TRUE(mixed.seam.g0);
EXPECT_TRUE(mixed.seam.g1);
EXPECT_TRUE(mixed.seam.g2);
SurfaceStitchOptions opt;
opt.order = 2;
const auto st = stitch_surfaces_continuous(bt, 2, a, 1, opt);
ASSERT_TRUE(st.ok) << st.message;
b4_expect_seam_le(st.seam, 1e-6);
// 多面片报告:box 平面面片仅有 G0(90° 折角 → G1 不通过)
const double W = 2, H = 3, D = 4;
std::vector<NurbsSurface> box = {
b4_planar(Point3D(0, 0, 0), Vector3D(W, 0, 0), Vector3D(0, H, 0)),
b4_planar(Point3D(0, 0, D), Vector3D(W, 0, 0), Vector3D(0, H, 0)),
b4_planar(Point3D(0, 0, 0), Vector3D(W, 0, 0), Vector3D(0, 0, D)),
b4_planar(Point3D(0, H, 0), Vector3D(W, 0, 0), Vector3D(0, 0, D)),
b4_planar(Point3D(0, 0, 0), Vector3D(0, H, 0), Vector3D(0, 0, D)),
b4_planar(Point3D(W, 0, 0), Vector3D(0, H, 0), Vector3D(0, 0, D)),
};
const std::vector<PatchSeamRef> seams = {
{0, 2, 2, 2}, {0, 3, 3, 2}, {0, 0, 4, 2}, {0, 1, 5, 2},
{1, 2, 2, 3}, {1, 3, 3, 3}, {1, 0, 4, 3}, {1, 1, 5, 3},
};
const auto report = multi_patch_quality_report(box, seams, 1e-6, 33);
ASSERT_EQ(report.seams.size(), seams.size());
EXPECT_TRUE(report.all_g0);
EXPECT_FALSE(report.all_g1); // 90° 折角
EXPECT_LE(report.max_position_error, 1e-6);
EXPECT_NEAR(report.max_tangent_angle, M_PI / 2, 1e-6);
}
TEST(SurfaceB4Fairing, EnergyDropWithG2SeamAndG0Boundary) {
const NurbsSurface parent = b4_wavy();
const NurbsSurface a = extract_sub_surface(parent, 0.0, 0.5, 0.0, 1.0);
const NurbsSurface b = extract_sub_surface(parent, 0.5, 1.0, 0.0, 1.0);
// 先把 b 拼到 a,得 G2 缝;再加内部噪声
SurfaceStitchOptions opt;
opt.order = 2;
const auto st0 = stitch_surfaces_continuous(b, 0, a, 1, opt);
ASSERT_TRUE(st0.ok);
auto cp = st0.matched_surface.control_points();
for (int i = 2; i + 2 < static_cast<int>(cp.size()); ++i)
for (int j = 1; j + 1 < static_cast<int>(cp[0].size()); ++j) {
const double s = ((i * 7 + j * 13) % 11 - 5) / 5.0;
cp[static_cast<size_t>(i)][static_cast<size_t>(j)] += Vector3D(0, 0, 0.03 * s);
}
const NurbsSurface noisy(cp, st0.matched_surface.knots_u(),
st0.matched_surface.knots_v(),
st0.matched_surface.weights(), 3, 3);
FairingConstraints fc;
fc.max_iterations = 400;
fc.tolerance = 1e-7;
for (int e = 0; e < 4; ++e) {
BoundaryConstraint bc;
bc.edge_id = e;
bc.type = BoundaryConstraintType::G0;
fc.boundary_constraints.push_back(bc);
}
const auto fr = fair_surface(noisy, fc);
ASSERT_GT(fr.initial_energy, 0.0);
// 约束投影:恢复外侧边界(G0 精确),公共边重新 stitch(G2 精确)
auto cp2 = fr.faired_surface.control_points();
const auto cp_orig = noisy.control_points();
auto restore_edge = [&](int edge) {
const bool uax = edge < 2, ismax = (edge == 1 || edge == 3);
const int n = uax ? static_cast<int>(cp2.size())
: static_cast<int>(cp2[0].size());
const int idx = ismax ? n - 1 : 0;
if (uax) cp2[static_cast<size_t>(idx)] = cp_orig[static_cast<size_t>(idx)];
else
for (size_t i = 0; i < cp2.size(); ++i)
cp2[i][static_cast<size_t>(idx)] = cp_orig[i][static_cast<size_t>(idx)];
};
restore_edge(0);
restore_edge(2);
restore_edge(3);
const NurbsSurface projected(cp2, noisy.knots_u(), noisy.knots_v(), noisy.weights(), 3, 3);
const auto st = stitch_surfaces_continuous(projected, 0, a, 1, opt);
ASSERT_TRUE(st.ok) << st.message;
// 能量下降 ≥30%
const auto ed0 = energy_diagnostics(noisy);
const auto ed1 = energy_diagnostics(st.matched_surface);
ASSERT_GT(ed0.bending_energy, 0.0);
const double drop = 100.0 * (ed0.bending_energy - ed1.bending_energy) / ed0.bending_energy;
EXPECT_GE(drop, 30.0) << "drop=" << drop;
// 公共边 G2 约束保持
b4_expect_seam_le(st.seam, 1e-6);
// 外侧边界 G0 精确保持(edge 0/2/3;edge1 为公共缝)
for (int e : {0, 2, 3})
for (int i = 0; i <= 16; ++i) {
const double t = i / 16.0;
const auto p0 = [&] {
const auto& ku = noisy.knots_u();
const auto& kv = noisy.knots_v();
const double u0 = ku[static_cast<size_t>(noisy.degree_u())];
const double u1 = ku[static_cast<size_t>(noisy.num_control_points()[0])];
const double v0 = kv[static_cast<size_t>(noisy.degree_v())];
const double v1 = kv[static_cast<size_t>(noisy.num_control_points()[1])];
const double along = (e < 2) ? v0 + (v1 - v0) * t : u0 + (u1 - u0) * t;
const double uu = (e == 0) ? u0 : (e == 1) ? u1 : along;
const double vv = (e == 2) ? v0 : (e == 3) ? v1 : along;
return std::pair<double, double>(uu, vv);
}();
EXPECT_LE((noisy.evaluate(p0.first, p0.second) -
st.matched_surface.evaluate(p0.first, p0.second)).norm(), 1e-12);
}
// 无自交(无法向翻转/折叠)
EXPECT_TRUE(b4_no_fold(noisy));
EXPECT_TRUE(b4_no_fold(fr.faired_surface));
EXPECT_TRUE(b4_no_fold(st.matched_surface));
}
TEST(SurfaceB4Sew, TolerantSnapAndAssemble) {
const double W = 2, H = 3, D = 4;
const double tol = 1e-2;
std::vector<NurbsSurface> box = {
b4_planar(Point3D(0, 0, 0), Vector3D(W, 0, 0), Vector3D(0, H, 0)),
b4_planar(Point3D(0, 0, D), Vector3D(W, 0, 0), Vector3D(0, H, 0)),
b4_planar(Point3D(0, 0, 0), Vector3D(W, 0, 0), Vector3D(0, 0, D)),
b4_planar(Point3D(0, H, 0), Vector3D(W, 0, 0), Vector3D(0, 0, D)),
b4_planar(Point3D(0, 0, 0), Vector3D(0, H, 0), Vector3D(0, 0, D)),
b4_planar(Point3D(W, 0, 0), Vector3D(0, H, 0), Vector3D(0, 0, D)),
};
auto top = box[1].control_points();
for (auto& row : top)
for (auto& p : row) p += Vector3D(0, 0, 1e-3);
box[1] = NurbsSurface(top, box[1].knots_u(), box[1].knots_v(), box[1].weights(), 3, 3);
const auto before = patch_network_area_volume(box, 24);
EXPECT_FALSE(before.closed); // 有 1e-3 间隙
const auto snap = snap_patch_gaps(box, tol, 41);
EXPECT_EQ(snap.pairs_snapped, 12);
EXPECT_GE(snap.max_gap_before, 9e-4);
EXPECT_LE(snap.max_gap_after, tol);
EXPECT_TRUE(snap.all_within_tolerance);
const auto after = patch_network_area_volume(snap.patches, 24);
EXPECT_TRUE(after.closed);
// 面积/体积不跳变:面积 52,体积 24
EXPECT_NEAR(before.area, 52.0, 1e-2);
EXPECT_NEAR(after.area, 52.0, 1e-2);
EXPECT_NEAR(after.volume, 24.0, 1e-1);
// 缝合为有效 B-Rep:6 个面片、顶点被焊接、面积/体积连续
const auto sewn = sew_surfaces(snap.patches, tol);
EXPECT_EQ(sewn.num_faces(), 6u);
EXPECT_GT(sewn.num_vertices(), 0u);
EXPECT_LT(sewn.num_vertices(), 6u * 16u); // 边界顶点已共享
double area = 0, vol = 0;
b4_model_area_volume(sewn, area, vol);
EXPECT_NEAR(area, 52.0, 1e-1);
EXPECT_NEAR(vol, 24.0, 1e-1);
}