Files
ViewDesignEngine/tests/curves/test_surface_continuity.cpp
hm 2777933e94
Build & Test / build-and-test (push) Canceled after 0s
VDE Build & Test / build-and-test (push) Canceled after 0s
Build & Test / python-bindings (push) Canceled after 0s
feat(proposal): 落地 VDE-394/386 提案 API(ABF 参数化 + UV 打包;双曲面曲率梳 + 自动 G1/G2 匹配)
VDE-394 (mesh_parameterize):
- abf_parameterization:以 Tutte 为初值的 Gauss-Seidel 坐标下降,最小化角畸变能量
  Σ(θ_uv−θ_3d)²,含翻转三角形守卫,保角性优于 Tutte。
- UVChart/UVAtlas + pack_uv_charts:归一化 + 高度降序 shelf 装箱,输出单位正方形
  内无重叠布局与利用率。

VDE-386 (surface_continuity / surface_analysis):
- match_surface_continuity:委托 surface_editing::match_surface 自动调整待匹配面
  控制点达目标连续性,返回 MatchResult(达到等级 + 最大控制点位移)。
- curvature_comb(a,b,samples):双曲面沿公共边自动探测,取面内垂直边方向的截面
  法曲率,梳齿 = 两侧曲率向量之差,即 G2 失配可视化。

测试:
- test_mesh_parameterize 16/16:ABF 角畸变能量严格下降(可展圆柱弧网格、独立复算),
  UV 打包全落 [0,1]² 且包围盒两两不重叠。
- test_surface_continuity 17/17:匹配后共享边切平面夹角从 ≈45° 降至 <1e-3 rad
  (独立复算法线夹角);平面×抛物面梳齿非零、同面自比梳齿为 0。

文档:VDE-394.md / VDE-386.md 状态更正为 Fixed;核验表计数 34/43/0/241。
2026-10-05 02:50:04 +08:00

328 lines
13 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_continuity.h"
#include "vde/curves/surface_analysis.h"
#include "vde/curves/nurbs_curve.h"
#include "vde/curves/nurbs_surface.h"
#include <cmath>
using namespace vde::curves;
using namespace vde::core;
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/// Create a degree-1 NURBS line from a to b
static NurbsCurve line(const Point3D& a, const Point3D& b) {
return NurbsCurve({a, b}, {0, 0, 1, 1}, {1, 1}, 1);
}
/// Create a planar NURBS surface on XY plane over [0,1]×[0,1]
static NurbsSurface plane_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},{1,1}}, 1, 1);
}
/// Create a simple NURBS surface from a 2×2 grid
static NurbsSurface simple_surface(const std::vector<std::vector<Point3D>>& grid,
int du=1, int dv=1) {
int rows = static_cast<int>(grid.size());
int cols = static_cast<int>(grid.empty() ? 0 : grid[0].size());
std::vector<double> ku(rows + du + 1, 0.0);
std::vector<double> kv(cols + dv + 1, 0.0);
for (int i = 0; i <= du; ++i) ku[i] = 0.0;
for (size_t i = du + 1; i < ku.size() - du - 1; ++i)
ku[i] = static_cast<double>(i - du) / (ku.size() - 2*du - 1);
for (size_t i = ku.size() - du - 1; i < ku.size(); ++i) ku[i] = 1.0;
for (int i = 0; i <= dv; ++i) kv[i] = 0.0;
for (size_t i = dv + 1; i < kv.size() - dv - 1; ++i)
kv[i] = static_cast<double>(i - dv) / (kv.size() - 2*dv - 1);
for (size_t i = kv.size() - dv - 1; i < kv.size(); ++i) kv[i] = 1.0;
std::vector<std::vector<double>> w(rows, std::vector<double>(cols, 1.0));
return NurbsSurface(grid, ku, kv, w, du, dv);
}
// ---------------------------------------------------------------------------
// Test: to_string
// ---------------------------------------------------------------------------
TEST(ContinuityTest, ToString) {
EXPECT_STREQ("G0", to_string(ContinuityLevel::G0));
EXPECT_STREQ("G1", to_string(ContinuityLevel::G1));
EXPECT_STREQ("G2", to_string(ContinuityLevel::G2));
EXPECT_STREQ("G3", to_string(ContinuityLevel::G3));
EXPECT_STREQ("None", to_string(ContinuityLevel::None));
}
// ---------------------------------------------------------------------------
// Test: G0 — Position continuity on lines
// ---------------------------------------------------------------------------
TEST(ContinuityTest, G0_SameCurveContinuous) {
// Two identical line segments should be G1 (same tangent direction)
auto a = line(Point3D(0, 0, 0), Point3D(1, 0, 0));
auto b = line(Point3D(1, 0, 0), Point3D(2, 0, 0));
auto level = continuity_type(a, b);
EXPECT_GE(static_cast<int>(level), static_cast<int>(ContinuityLevel::G1));
}
TEST(ContinuityTest, G0_EndpointsMatch) {
auto a = line(Point3D(0, 0, 0), Point3D(1, 0, 0));
auto b = line(Point3D(1, 0, 0), Point3D(1, 1, 0));
auto level = continuity_type(a, b);
EXPECT_GE(static_cast<int>(level), static_cast<int>(ContinuityLevel::G0));
}
TEST(ContinuityTest, G0_NoConnection) {
auto a = line(Point3D(0, 0, 0), Point3D(1, 0, 0));
auto b = line(Point3D(5, 5, 5), Point3D(6, 6, 6));
auto level = continuity_type(a, b);
EXPECT_EQ(ContinuityLevel::None, level);
}
// ---------------------------------------------------------------------------
// Test: G1 — Tangent continuity
// ---------------------------------------------------------------------------
TEST(ContinuityTest, G1_CollinearLines) {
// Two collinear lines → G0 position, G1 tangent
auto a = line(Point3D(0, 0, 0), Point3D(1, 0, 0));
auto b = line(Point3D(1, 0, 0), Point3D(2, 0, 0));
// Check at the connection point with explicit t values
auto level = continuity_type(a, 1.0, b, 0.0);
EXPECT_GE(static_cast<int>(level), static_cast<int>(ContinuityLevel::G1));
}
TEST(ContinuityTest, G1_SharpCornerG0Only) {
// Two lines meeting at a right angle → only G0
auto a = line(Point3D(0, 0, 0), Point3D(1, 0, 0));
auto b = line(Point3D(1, 0, 0), Point3D(1, 1, 0));
auto level = continuity_type(a, 1.0, b, 0.0);
EXPECT_EQ(ContinuityLevel::G0, level);
}
// ---------------------------------------------------------------------------
// Test: G2 — Curvature continuity on curves
// ---------------------------------------------------------------------------
TEST(ContinuityTest, G2_StraightLinesG3) {
// Two collinear straight lines have zero curvature everywhere → G3
auto a = line(Point3D(0, 0, 0), Point3D(1, 0, 0));
auto b = line(Point3D(1, 0, 0), Point3D(3, 0, 0));
auto level = continuity_type(a, 1.0, b, 0.0);
// Straight lines have d2=0, curvature=0, so they trivially match → G3
EXPECT_EQ(ContinuityLevel::G3, level);
}
TEST(ContinuityTest, G0_DisconnectedReturnsNone) {
auto a = line(Point3D(0, 0, 0), Point3D(1, 0, 0));
auto b = line(Point3D(10, 10, 10), Point3D(11, 11, 11));
auto level = continuity_type(a, 0.5, b, 0.5);
EXPECT_EQ(ContinuityLevel::None, level);
}
// ---------------------------------------------------------------------------
// Test: Surface Continuity
// ---------------------------------------------------------------------------
TEST(SurfaceContinuityTest, IdenticalPlanes) {
// Two identical flat planes → should be G3
auto s1 = plane_surface();
auto s2 = plane_surface();
auto level = surface_continuity(s1, s2, 10);
EXPECT_GE(static_cast<int>(level), static_cast<int>(ContinuityLevel::G3));
}
TEST(SurfaceContinuityTest, CoplanarSurfacesEdgeMatch) {
// Two adjacent coplanar surfaces sharing an edge
std::vector<std::vector<Point3D>> g1 = {
{Point3D(0,0,0), Point3D(0,1,0)},
{Point3D(1,0,0), Point3D(1,1,0)}
};
std::vector<std::vector<Point3D>> g2 = {
{Point3D(1,0,0), Point3D(1,1,0)},
{Point3D(2,0,0), Point3D(2,1,0)}
};
auto s1 = simple_surface(g1);
auto s2 = simple_surface(g2);
auto level = surface_continuity(s1, s2, 10);
EXPECT_GE(static_cast<int>(level), static_cast<int>(ContinuityLevel::G3));
}
TEST(SurfaceContinuityTest, DisconnectedSurfaces) {
std::vector<std::vector<Point3D>> g1 = {
{Point3D(0,0,0), Point3D(0,1,0)},
{Point3D(1,0,0), Point3D(1,1,0)}
};
std::vector<std::vector<Point3D>> g2 = {
{Point3D(10,0,0), Point3D(10,1,0)},
{Point3D(11,0,0), Point3D(11,1,0)}
};
auto s1 = simple_surface(g1);
auto s2 = simple_surface(g2);
auto level = surface_continuity(s1, s2, 10);
EXPECT_EQ(ContinuityLevel::None, level);
}
TEST(SurfaceContinuityTest, ContinuityReport) {
auto s1 = plane_surface();
auto s2 = plane_surface();
auto report = surface_continuity_report(s1, s2, 20);
EXPECT_GE(static_cast<int>(report.worst), static_cast<int>(ContinuityLevel::G3));
EXPECT_NEAR(report.g1_ratio, 1.0, 0.01);
EXPECT_NEAR(report.g2_ratio, 1.0, 0.01);
}
// ---------------------------------------------------------------------------
// Test: Edge parameter overload
// ---------------------------------------------------------------------------
TEST(SurfaceContinuityTest, ExplicitEdgeParametersPlane) {
auto s1 = plane_surface();
auto s2 = plane_surface();
// Same physical edge (u=1) on both identical surfaces
auto level = surface_continuity(
s1, s2,
{{1.0, 0.0}, {1.0, 1.0}}, // s1: u=1 edge, v 0→1
{{1.0, 0.0}, {1.0, 1.0}}, // s2: same u=1 edge
20);
EXPECT_GE(static_cast<int>(level), static_cast<int>(ContinuityLevel::G3));
}
// ---------------------------------------------------------------------------
// VDE-1202: G3 判据须参数化无关(几何 d³r/ds³)
// ---------------------------------------------------------------------------
/// 单段 Bezier 曲线:degree p,p+1 个控制点,clamped 节点 {0…,1…}
static NurbsCurve bezier(const std::vector<Point3D>& cps) {
const int p = static_cast<int>(cps.size()) - 1;
std::vector<double> knots(2 * (p + 1), 0.0);
for (int i = p + 1; i < 2 * (p + 1); ++i) knots[i] = 1.0;
std::vector<double> w(cps.size(), 1.0);
return NurbsCurve(cps, knots, w, p);
}
TEST(ContinuityTest, VDE1202_GeometricG3DifferentParameterization) {
// 同一条几何抛物线 (t, t², 0),两种参数化:
// C1 = 度 2 Bezier(x=t, y=t²);
// C2 = 度 4 Bezier(x=u², y=u⁴,即 u→u² 的非线性重参数化)。
// 在接点 (1,1,0) 处 G0/G1/G2 均成立;旧判据因参数化相关误判 G2,
// 几何正确判据须给 G3。
auto c1 = bezier({Point3D(0, 0, 0), Point3D(0.5, 0, 0), Point3D(1, 1, 0)});
auto c2 = bezier({Point3D(0, 0, 0), Point3D(0, 0, 0),
Point3D(1.0 / 6.0, 0, 0), Point3D(0.5, 0, 0),
Point3D(1, 1, 0)});
EXPECT_EQ(ContinuityLevel::G3, continuity_type(c1, 1.0, c2, 1.0));
}
TEST(ContinuityTest, VDE1202_G2PairStaysG2) {
// 直线与 (u, u³, 0) 在原点相接:κ 两侧均为 0(G2),但 dκ/ds
// 侧向非零((0,6,0) vs 0)⇒ 应准确判为 G2,不能被新判据抬成 G3。
auto a = line(Point3D(-1, 0, 0), Point3D(0, 0, 0));
auto b = bezier({Point3D(0, 0, 0), Point3D(1.0 / 3.0, 0, 0),
Point3D(2.0 / 3.0, 0, 0), Point3D(1, 1, 0)});
EXPECT_EQ(ContinuityLevel::G2, continuity_type(a, 1.0, b, 0.0));
}
// ---------------------------------------------------------------------------
// VDE-386: match_surface_continuity(自动 G1/G2 匹配)
// ---------------------------------------------------------------------------
/// 平面 (u, v, 0),u,v ∈ [0,1]
static NurbsSurface uv_plane() {
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}, {1, 1}}, 1, 1);
}
/// 与 uv_plane 共享 u=umax 边((1,v,0))但沿 u 倾斜的面:z = slope·(u−1)
static NurbsSurface uv_tilted(double slope) {
std::vector<std::vector<Point3D>> grid = {
{Point3D(0, 0, -slope), Point3D(0, 1, -slope)},
{Point3D(1, 0, 0), Point3D(1, 1, 0)}};
return NurbsSurface(grid, {0, 0, 1, 1}, {0, 0, 1, 1}, {{1, 1}, {1, 1}}, 1, 1);
}
/// 共享边 u=umax 处的最大切平面夹角(= G1 失配量的独立度量)
static double edge_plane_angle(const NurbsSurface& a, const NurbsSurface& b, int s = 20) {
double worst = 0.0;
for (int i = 0; i <= s; ++i) {
const double v = static_cast<double>(i) / s;
const Vector3D na = a.normal(1.0, v);
const Vector3D nb = b.normal(1.0, v);
const double d = std::max(-1.0, std::min(1.0, std::abs(na.dot(nb))));
worst = std::max(worst, std::acos(d));
}
return worst;
}
TEST(ContinuityTest, VDE386_MatchSurfaceContinuity_AchievesG1) {
auto ref = uv_plane();
auto tgt = uv_tilted(1.0);
// 初始:共享边位置重合但切平面不连续(≈45° 夹角)
const double before_ang = edge_plane_angle(ref, tgt);
EXPECT_GT(before_ang, 0.1);
const auto res = match_surface_continuity(tgt, ref, 1 /*umax*/, ContinuityLevel::G1);
// 控制点确实被调整
EXPECT_GT(res.max_cp_displacement, 1e-9);
EXPECT_EQ(res.matched_surface.num_control_points()[0], tgt.num_control_points()[0]);
EXPECT_EQ(res.matched_surface.num_control_points()[1], tgt.num_control_points()[1]);
// 达到等级 ≥ G1
EXPECT_GE(static_cast<int>(res.achieved_level), static_cast<int>(ContinuityLevel::G1));
// 独立复核:匹配后共享边切平面夹角显著减小(≈0)
const double after_ang = edge_plane_angle(ref, res.matched_surface);
EXPECT_LT(after_ang, 1e-3);
EXPECT_LT(after_ang, before_ang);
}
// ---------------------------------------------------------------------------
// VDE-386: 两张曲面沿公共边的曲率梳
// ---------------------------------------------------------------------------
/// 与 uv_plane 共享 u=umax 边、二次弯曲的面:z = k·(u²−1)
static NurbsSurface uv_paraboloid(double k) {
std::vector<std::vector<Point3D>> grid = {
{Point3D(0.0, 0, -k), Point3D(0.0, 1, -k)},
{Point3D(0.5, 0, -k), Point3D(0.5, 1, -k)},
{Point3D(1.0, 0, 0.0), Point3D(1.0, 1, 0.0)}};
return NurbsSurface(grid, {0, 0, 0, 1, 1, 1}, {0, 0, 1, 1},
{{1, 1}, {1, 1}, {1, 1}}, 2, 1);
}
TEST(ContinuityTest, VDE386_CurvatureComb_SurfaceToSurface) {
auto plane = uv_plane();
auto quad = uv_paraboloid(0.2);
// 平面 × 抛物面:平面侧跨界曲率为 0,抛物面侧非 0 → 梳齿非零
auto comb = curvature_comb(plane, quad, 20);
ASSERT_EQ(comb.points.size(), 21u);
ASSERT_EQ(comb.samples.size(), 21u);
EXPECT_LT(comb.max_curvature, 1e-6);
double max_comb = 0.0;
for (const auto& p : comb.points) {
EXPECT_TRUE(std::isfinite(p.curvature_vec.norm()));
max_comb = std::max(max_comb, p.curvature_vec.norm());
}
EXPECT_GT(max_comb, 1e-3);
// 同名曲面自比 → G2 完美 → 梳齿全 0
auto self = curvature_comb(plane, plane, 20);
ASSERT_EQ(self.points.size(), 21u);
for (const auto& p : self.points) {
EXPECT_LT(p.curvature_vec.norm(), 1e-9);
}
}