Files
ViewDesignEngine/tests/curves/test_surface_extension.cpp
hm 495c8e2058 fix(curves): unify Continuity extend_surface to exact extension kernel (VDE-1475)
四参数 extend_surface(surface, edge, distance, Continuity) 的 G1/G2 分支自带「追加 1~2 行 + 节点」实现:接缝 C0 偏差 = 延伸长度(实测 0.5),与三参数版本同病。统一委托 1.0.277 精确内核(边界曲线 + 平均切向平移得精确直纹条带 + 精确 merge_surfaces 拼接;v 边经转置);语义:沿边界平均切向直线外推,distance 为几何长度。实测球面片 4 边 × {G1,G2} 接缝 C0 <=1e-12、原面复现 <=1e-12。4 个 surface_extension 用例断言由固定行/列数改为几何(非空、接缝、可求值);新增 SurfaceExtensionTest.Extend_SeamExactForAllEdgesAndContinuities;test_surface_extension 22 项全过;全量 8645/0;注册 8,649;报告 336;版本 1.0.278。
2026-11-11 05:51:59 +08:00

431 lines
17 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_extension.h"
#include "vde/curves/nurbs_operations.h"
#include "vde/curves/nurbs_curve.h"
#include "vde/curves/nurbs_surface.h"
#include "vde/brep/brep.h"
#include "vde/brep/modeling.h"
#include <cmath>
using namespace vde::curves;
using namespace vde::brep;
using namespace vde::core;
// ===========================================================================
// Helpers
// ===========================================================================
static NurbsCurve make_line_curve(const Point3D& a, const Point3D& b) {
return NurbsCurve({a, b}, {0, 0, 1, 1}, {1, 1}, 1);
}
static NurbsSurface make_planar_surface() {
std::vector<std::vector<Point3D>> grid = {
{Point3D(0, 0, 0), Point3D(0, 1, 0)},
{Point3D(1, 0, 0), Point3D(1, 1, 0)}
};
return NurbsSurface(grid, {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
}
static NurbsSurface make_degree2_surface() {
std::vector<std::vector<Point3D>> grid = {
{Point3D(0, 0, 0), Point3D(0, 1, 0.3), Point3D(0, 2, 0)},
{Point3D(1, 0, 0.3), Point3D(1, 1, 0.8), Point3D(1, 2, 0.3)},
{Point3D(2, 0, 0), Point3D(2, 1, 0.3), Point3D(2, 2, 0)}
};
return NurbsSurface(grid,
{0, 0, 0, 1, 1, 1}, {0, 0, 0, 1, 1, 1}, {}, 2, 2);
}
// ===========================================================================
// extend_surface — G1 延伸
// ===========================================================================
TEST(SurfaceExtensionTest, ExtendG1_Umax_AddsRow) {
auto plane = make_planar_surface();
auto extended = extend_surface(plane, 1, 0.5, Continuity::G1);
// VDE-1475:精确延伸不再保证「原样 +1 行」;断言几何(非空/接缝/可求值)。
auto [nu, nv] = extended.num_control_points();
EXPECT_GE(nu, 2);
EXPECT_GE(nv, 2);
ASSERT_FALSE(extended.control_points().empty());
// 原面边界应被精确复现(接缝 C0)
for (int k = 0; k <= 8; ++k) {
const double v = k / 8.0;
const auto pe = extended.evaluate(plane.knots_u()[plane.degree_u() + 1],
v);
const auto ps = plane.evaluate(plane.knots_u()[plane.degree_u() + 1], v);
EXPECT_LE((pe - ps).norm(), 1e-12);
}
// Extended surface should evaluate to finite values throughout
for (double u = 0.0; u <= 1.0; u += 0.25) {
for (double v = 0.0; v <= 1.0; v += 0.25) {
Point3D p = extended.evaluate(u, v);
EXPECT_TRUE(std::isfinite(p.x()));
EXPECT_TRUE(std::isfinite(p.y()));
EXPECT_TRUE(std::isfinite(p.z()));
}
}
}
TEST(SurfaceExtensionTest, ExtendG1_Vmin_AddsColumn) {
auto plane = make_planar_surface();
auto extended = extend_surface(plane, 2, 1.0, Continuity::G1);
auto [nu, nv] = extended.num_control_points();
EXPECT_GE(nu, 2);
EXPECT_GE(nv, 2);
ASSERT_FALSE(extended.control_points().empty());
// All points should be finite
Point3D p = extended.evaluate(0.5, 0.5);
EXPECT_TRUE(std::isfinite(p.x()));
EXPECT_TRUE(std::isfinite(p.y()));
EXPECT_TRUE(std::isfinite(p.z()));
}
TEST(SurfaceExtensionTest, ExtendG1_ZeroDistance) {
auto plane = make_planar_surface();
auto same = extend_surface(plane, 0, 0.0, Continuity::G1);
// Should return the same surface
Point3D p1 = plane.evaluate(0.5, 0.5);
Point3D p2 = same.evaluate(0.5, 0.5);
EXPECT_NEAR((p1 - p2).norm(), 0.0, 1e-10);
}
// ===========================================================================
// extend_surface — G2 延伸
// ===========================================================================
TEST(SurfaceExtensionTest, Extend_SeamExactForAllEdgesAndContinuities) {
// VDE-1475:四参数 extend_surface 与三参数精确内核统一后,
// 4 条边 × {G1,G2} 接缝 C0 精确、原面区域复现。
auto sph = make_sphere(2.0, 0, 0);
const auto& s =
sph.trimmed_surface(sph.face(0).trimmed_surface_id).base_surface;
const auto ku = s.knots_u();
const auto kv = s.knots_v();
const double d0 = ku[s.degree_u()], d1 = ku[ku.size() - 1 - s.degree_u()];
const double e0 = kv[s.degree_v()], e1 = kv[kv.size() - 1 - s.degree_v()];
const double span_u = d1 - d0, span_v = e1 - e0;
for (auto cont : {Continuity::G1, Continuity::G2}) {
for (int edge = 0; edge < 4; ++edge) {
auto ext = extend_surface(s, edge, 0.5, cont);
ASSERT_FALSE(ext.control_points().empty())
<< "cont=" << (int)cont << " edge=" << edge;
const auto eku = ext.knots_u();
const auto ekv = ext.knots_v();
const double f0 = eku[ext.degree_u()],
f1 = eku[eku.size() - 1 - ext.degree_u()];
const double g0 = ekv[ext.degree_v()],
g1v = ekv[ekv.size() - 1 - ext.degree_v()];
const bool uedge = (edge == 0 || edge == 1);
const double seam_u =
uedge ? ((edge == 1) ? d1 : d0 + span_u) : 0.0;
const double seam_v =
uedge ? 0.0 : ((edge == 3) ? e1 : e0 + span_v);
double dev = 0.0;
for (int k = 0; k <= 12; ++k) {
const double t = k / 12.0;
const double u =
uedge ? seam_u : d0 + span_u * t;
const double v =
uedge ? e0 + span_v * t : seam_v;
const auto p = ext.evaluate(u, v);
const auto q =
s.evaluate(uedge ? ((edge == 1) ? d1 : d0) : u,
uedge ? v : ((edge == 3) ? e1 : e0));
dev = std::max(dev, (p - q).norm());
}
EXPECT_LE(dev, 1e-12) << "cont=" << (int)cont << " edge=" << edge;
EXPECT_LT(f0, f1);
EXPECT_LT(g0, g1v);
}
}
}
TEST(SurfaceExtensionTest, ExtendG2_Umax_AddsTwoRows) {
auto plane = make_planar_surface();
auto extended = extend_surface(plane, 1, 0.5, Continuity::G2);
// VDE-1475:统一到精确延伸内核;断言几何而非固定行数。
auto [nu, nv] = extended.num_control_points();
EXPECT_GE(nu, 2);
EXPECT_GE(nv, 2);
ASSERT_FALSE(extended.control_points().empty());
}
TEST(SurfaceExtensionTest, ExtendG2_PlanarStaysPlanar) {
auto plane = make_planar_surface();
auto extended = extend_surface(plane, 1, 1.0, Continuity::G2);
// All points should have z=0 (planar extension of XY plane)
for (double u = 0.0; u <= 1.0; u += 0.25) {
for (double v = 0.0; v <= 1.0; v += 0.25) {
Point3D p = extended.evaluate(u, v);
EXPECT_NEAR(p.z(), 0.0, 1e-6);
}
}
}
TEST(SurfaceExtensionTest, ExtendG2_VmaxDegree2_AddsTwoCols) {
auto surf = make_degree2_surface();
auto extended = extend_surface(surf, 3, 0.5, Continuity::G2);
// VDE-1475:断言几何(非空/有限),不锁定固定列数。
auto [nu, nv] = extended.num_control_points();
EXPECT_GE(nu, 1);
EXPECT_GE(nv, 1);
ASSERT_FALSE(extended.control_points().empty());
// Evaluate at valid parameters
Point3D p = extended.evaluate(0.5, 0.5);
EXPECT_TRUE(std::isfinite(p.x()));
EXPECT_TRUE(std::isfinite(p.y()));
EXPECT_TRUE(std::isfinite(p.z()));
}
TEST(SurfaceExtensionTest, ExtendG1_PlanarPreservesFlatness) {
auto plane = make_planar_surface();
auto extended = extend_surface(plane, 1, 0.5, Continuity::G1);
// Extended surface should remain planar
for (double u = 0.0; u <= 1.0; u += 0.25) {
for (double v = 0.0; v <= 1.0; v += 0.25) {
Point3D p = extended.evaluate(u, v);
EXPECT_NEAR(p.z(), 0.0, 1e-6);
}
}
}
// ===========================================================================
// n_sided_fill — N 边填充
// ===========================================================================
TEST(SurfaceExtensionTest, FillNSided_TriangleG2) {
auto l1 = make_line_curve(Point3D(0, 0, 0), Point3D(2, 0, 0));
auto l2 = make_line_curve(Point3D(2, 0, 0), Point3D(1, 2, 0));
auto l3 = make_line_curve(Point3D(1, 2, 0), Point3D(0, 0, 0));
std::vector<NurbsCurve> boundaries = {l1, l2, l3};
auto fill = n_sided_fill(boundaries, Continuity::G2);
// Should produce a valid surface
Point3D mid = fill.evaluate(0.5, 0.5);
EXPECT_TRUE(std::isfinite(mid.x()));
EXPECT_TRUE(std::isfinite(mid.y()));
EXPECT_TRUE(std::isfinite(mid.z()));
// Center should be roughly inside the triangle
EXPECT_GT(mid.x(), 0.0);
EXPECT_LT(mid.x(), 2.0);
EXPECT_GT(mid.y(), 0.0);
EXPECT_LT(mid.y(), 2.0);
}
TEST(SurfaceExtensionTest, FillNSided_Quadrilateral) {
auto l1 = make_line_curve(Point3D(0, 0, 0), Point3D(3, 0, 0));
auto l2 = make_line_curve(Point3D(3, 0, 0), Point3D(3, 2, 0));
auto l3 = make_line_curve(Point3D(3, 2, 0), Point3D(0, 2, 0));
auto l4 = make_line_curve(Point3D(0, 2, 0), Point3D(0, 0, 0));
std::vector<NurbsCurve> boundaries = {l1, l2, l3, l4};
auto fill = n_sided_fill(boundaries, Continuity::G1);
// Center should be inside the rectangle
Point3D mid = fill.evaluate(0.5, 0.5);
EXPECT_GT(mid.x(), 0.5);
EXPECT_LT(mid.x(), 2.5);
EXPECT_GT(mid.y(), 0.0);
EXPECT_LT(mid.y(), 2.0);
EXPECT_NEAR(mid.z(), 0.0, 1e-6);
}
TEST(SurfaceExtensionTest, FillNSided_Pentagon) {
// Regular pentagon in XY plane
std::vector<NurbsCurve> boundaries;
std::vector<Point3D> pts;
for (int i = 0; i < 5; ++i) {
double angle = 2.0 * M_PI * i / 5.0 - M_PI / 2.0;
pts.push_back(Point3D(std::cos(angle), std::sin(angle), 0));
}
for (int i = 0; i < 5; ++i) {
boundaries.push_back(make_line_curve(pts[i], pts[(i + 1) % 5]));
}
auto fill = n_sided_fill(boundaries, Continuity::G2);
// Center should be near origin
Point3D mid = fill.evaluate(0.5, 0.5);
EXPECT_NEAR(mid.x(), 0.0, 0.5);
EXPECT_NEAR(mid.y(), 0.0, 0.5);
EXPECT_NEAR(mid.z(), 0.0, 1e-6);
}
TEST(SurfaceExtensionTest, FillNSided_TriangleG1) {
auto l1 = make_line_curve(Point3D(0, 0, 0), Point3D(2, 0, 0));
auto l2 = make_line_curve(Point3D(2, 0, 0), Point3D(0, 2, 0));
auto l3 = make_line_curve(Point3D(0, 2, 0), Point3D(0, 0, 0));
std::vector<NurbsCurve> boundaries = {l1, l2, l3};
auto fill = n_sided_fill(boundaries, Continuity::G1);
// Fill surface should be finite and pass through boundary
for (double u = 0.0; u <= 1.0; u += 0.25) {
for (double v = 0.0; v <= 1.0; v += 0.25) {
Point3D p = fill.evaluate(u, v);
EXPECT_TRUE(std::isfinite(p.x()));
EXPECT_TRUE(std::isfinite(p.y()));
EXPECT_TRUE(std::isfinite(p.z()));
}
}
}
TEST(SurfaceExtensionTest, FillNSided_DegenerateEdge) {
// Only 2 curves → should return degenerate
auto l1 = make_line_curve(Point3D(0, 0, 0), Point3D(1, 0, 0));
auto l2 = make_line_curve(Point3D(1, 0, 0), Point3D(0, 1, 0));
std::vector<NurbsCurve> boundaries = {l1, l2};
auto fill = n_sided_fill(boundaries, Continuity::G1);
auto [nu, nv] = fill.num_control_points();
// With < 3 curves, returns empty surface
EXPECT_TRUE(nu == 0 || nu == 1);
}
// ===========================================================================
// blend_surfaces — 两面过渡(自动检测公共边)
// ===========================================================================
TEST(SurfaceExtensionTest, BlendSurfacesAutoDetect_Adjacent) {
auto plane1 = make_planar_surface();
std::vector<std::vector<Point3D>> grid2 = {
{Point3D(1, 0, 0), Point3D(1, 1, 0)},
{Point3D(2, 0, 0), Point3D(2, 1, 0)}
};
NurbsSurface plane2(grid2, {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
auto blend = blend_surfaces(plane1, plane2, 0.3);
// Should produce a valid blend surface
auto [nu, nv] = blend.num_control_points();
EXPECT_GE(nu, 2);
EXPECT_GE(nv, 2);
// Blend surface should evaluate to finite values
Point3D p = blend.evaluate(0.5, 0.5);
EXPECT_TRUE(std::isfinite(p.x()));
EXPECT_TRUE(std::isfinite(p.y()));
EXPECT_TRUE(std::isfinite(p.z()));
}
TEST(SurfaceExtensionTest, BlendSurfacesNoCommonBoundary) {
auto plane1 = make_planar_surface();
std::vector<std::vector<Point3D>> grid2 = {
{Point3D(3, 0, 0), Point3D(3, 1, 0)},
{Point3D(4, 0, 0), Point3D(4, 1, 0)}
};
NurbsSurface plane2(grid2, {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
auto blend = blend_surfaces(plane1, plane2, 0.3);
auto [nu, nv] = blend.num_control_points();
// No common boundary → degenerate
EXPECT_TRUE(nu == 0 || nv == 0);
}
// ===========================================================================
// is_g1_continuous — G1 连续性检查
// ===========================================================================
TEST(SurfaceExtensionTest, G1Continuous_AdjacentPlanes) {
auto plane1 = make_planar_surface();
std::vector<std::vector<Point3D>> grid2 = {
{Point3D(1, 0, 0), Point3D(1, 1, 0)},
{Point3D(2, 0, 0), Point3D(2, 1, 0)}
};
NurbsSurface plane2(grid2, {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
// They share x=1 boundary: plane1 umax (edge=1) matches plane2 umin (edge=0)
EXPECT_TRUE(is_g1_continuous(plane1, plane2, 1));
}
TEST(SurfaceExtensionTest, G1Continuous_NonMatching) {
auto plane1 = make_planar_surface();
// plane2 is angled → normals won't match
std::vector<std::vector<Point3D>> grid2 = {
{Point3D(1, 0, 0), Point3D(1, 1, 1)},
{Point3D(2, 0, 0), Point3D(2, 1, 1)}
};
NurbsSurface plane2(grid2, {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
// Normals differ → not G1
EXPECT_FALSE(is_g1_continuous(plane1, plane2, 1));
}
// ═══════════════════════════════════════════════════════════
// VDE-345 提案 2/3:blend_surfaces_variable / extend_to_surface
// ═══════════════════════════════════════════════════════════
TEST(VDE345Test, BlendSurfacesVariable_ProducesSurface) {
// A:x∈[0,1]、y∈[0,1];B:x∈[0,1]、y∈[1,2],共享 y=1 边
auto a = NurbsSurface::plane({0, 0, 0}, {1, 0, 0}, {0, 1, 0}, 1.0, 1.0);
auto b = NurbsSurface::plane({0, 1, 0}, {1, 0, 0}, {0, 1, 0}, 1.0, 1.0);
auto r = blend_surfaces_variable(a, b, {0.1, 0.3}, {0.0, 1.0});
EXPECT_GT(r.num_control_points()[0], 0);
EXPECT_GT(r.degree_u(), 0);
}
TEST(VDE345Test, BlendSurfacesVariable_RadiusChangesGeometry) {
auto a = NurbsSurface::plane({0, 0, 0}, {1, 0, 0}, {0, 1, 0}, 1.0, 1.0);
auto b = NurbsSurface::plane({0, 1, 0}, {1, 0, 0}, {0, 1, 0}, 1.0, 1.0);
auto small = blend_surfaces_variable(a, b, {0.1, 0.1}, {0.0, 1.0});
auto big = blend_surfaces_variable(a, b, {0.4, 0.4}, {0.0, 1.0});
ASSERT_GT(small.num_control_points()[0], 0);
ASSERT_GT(big.num_control_points()[0], 0);
// 半径不同 ⇒ 几何不同
const auto& gs = small.control_points();
const auto& gb = big.control_points();
double diff = 0.0;
const size_t n = std::min(gs.size(), gb.size());
for (size_t i = 0; i < n; ++i) {
const size_t m = std::min(gs[i].size(), gb[i].size());
for (size_t j = 0; j < m; ++j) diff += (gs[i][j] - gb[i][j]).norm();
}
EXPECT_GT(diff, 1e-9);
}
TEST(VDE345Test, BlendSurfacesVariable_InvalidInputsReturnEmpty) {
auto a = NurbsSurface::plane({0, 0, 0}, {1, 0, 0}, {0, 1, 0}, 1.0, 1.0);
auto b = NurbsSurface::plane({0, 1, 0}, {1, 0, 0}, {0, 1, 0}, 1.0, 1.0);
EXPECT_EQ(blend_surfaces_variable(a, b, {}, {}).num_control_points()[0], 0);
EXPECT_EQ(blend_surfaces_variable(a, b, {0.1, 0.2}, {0.0}).num_control_points()[0],
0);
}
TEST(VDE345Test, ExtendToSurface_StopsAtTarget) {
// surf:x∈[0,1]、y∈[0,1](z=0)
auto surf = NurbsSurface::plane({0, 0, 0}, {1, 0, 0}, {0, 1, 0}, 1.0, 1.0);
// target:x∈[1.2,2.2](z=0),初始不与 surf 接触
auto target = NurbsSurface::plane({1.2, 0, 0}, {1, 0, 0}, {0, 1, 0}, 1.0, 1.0);
const auto n_before = surf.num_control_points()[0];
auto ext = extend_to_surface(surf, 1 /*umax*/, target, 100);
// 成功延伸 → umin..umax 方向控制点增加
EXPECT_GT(ext.num_control_points()[0], n_before);
}
TEST(VDE345Test, ExtendToSurface_UnreachableReturnsOriginal) {
auto surf = NurbsSurface::plane({0, 0, 0}, {1, 0, 0}, {0, 1, 0}, 1.0, 1.0);
// 目标远在 x∈[100,101],延伸上限(域宽 1)内不可达
auto target = NurbsSurface::plane({100, 0, 0}, {1, 0, 0}, {0, 1, 0}, 1.0, 1.0);
auto ext = extend_to_surface(surf, 1, target, 100);
EXPECT_EQ(ext.num_control_points()[0], surf.num_control_points()[0]);
}