Files
ViewDesignEngine/tests/curves/test_surface_quality.cpp
hm 5a5f1d127a
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
fix(test): 修复 test_surface_quality.CurvatureSignConsistency 既有失败
根因为测试自身缺陷(非内核缺陷):make_cyl_surf 中间控制点误取圆上点(√2/2,√2/2),有理二次圆弧须取切线交点(1,1)(权重 cos45°),原曲线半径浮动 0.83~1.0、法向非径向;断言方向亦反(to_center 指向轴心却要求外法向同向)。修复:helper 改为真·四分之一单位圆柱;断言改为几何正确且更强的逐点独立复算(径向半径恒=1、外法向与径向点积=1、沿 u 法曲率 κ_u=(n·S_uu)/|S_u|²=-1/r=-1)。test_surface_quality 10/10、test_v5_features 21/21(OffsetTest 经复核已不成立)。同步更新 audit-implementation.md。
2026-10-06 00:47:49 +08:00

207 lines
7.6 KiB
C++

#include <gtest/gtest.h>
#include "vde/curves/nurbs_surface.h"
#include "vde/curves/nurbs_curve.h"
#include "vde/curves/surface_continuity.h"
#include "vde/curves/surface_analysis.h"
#include <cmath>
using namespace vde::curves;
using namespace vde::core;
// ═══════════════════════════════════════════════
// 曲率连续性验证测试
// ═══════════════════════════════════════════════
// Helper: create a simple plane surface
NurbsSurface make_plane_surf() {
std::vector<std::vector<Point3D>> cp = {
{Point3D(0, 0, 0), Point3D(0, 1, 0)},
{Point3D(1, 0, 0), Point3D(1, 1, 0)}
};
return NurbsSurface(cp, {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
}
// Helper: create a simple cylindrical surface
NurbsSurface make_cyl_surf() {
// 真·四分之一单位圆柱面(轴 = z 轴过原点):有理二次圆弧。
// 有理二次 Bézier 表示圆弧时,中间控制点必须取两端切线的交点
// (此处为 (1,1))且权重 = cos(半角) = cos45° = √2/2,曲面才精确落在
// 半径 1 的圆上。(原实现把中间控制点放在圆上的 (√2/2,√2/2),
// 该曲线并非圆弧,半径在 ~0.83–1.0 间浮动。)
double r = 1.0;
std::vector<std::vector<Point3D>> cp(3);
cp[0] = {Point3D(r * std::cos(0.0), r * std::sin(0.0), 0.0),
Point3D(r * std::cos(0.0), r * std::sin(0.0), 1.0)};
cp[1] = {Point3D(1.0, 1.0, 0.0), Point3D(1.0, 1.0, 1.0)}; // 切线交点
cp[2] = {Point3D(r * std::cos(M_PI / 2.0), r * std::sin(M_PI / 2.0), 0.0),
Point3D(r * std::cos(M_PI / 2.0), r * std::sin(M_PI / 2.0), 1.0)};
std::vector<double> ku = {0, 0, 0, 1, 1, 1}; // degree 2
std::vector<double> kv = {0, 0, 1, 1}; // degree 1
std::vector<std::vector<double>> w = {{1, 1}, {M_SQRT2/2, M_SQRT2/2}, {1, 1}};
return NurbsSurface(cp, ku, kv, w, 2, 1);
}
// G0: position continuity
TEST(SurfaceQualityTest, G0PositionContinuity) {
auto s1 = make_plane_surf();
auto s2 = make_plane_surf();
// Same surface at same parameter → should be G0
Point3D p1 = s1.evaluate(0.5, 0.5);
Point3D p2 = s2.evaluate(0.5, 0.5);
double dist = Vector3D(p2.x()-p1.x(), p2.y()-p1.y(), p2.z()-p1.z()).norm();
EXPECT_LT(dist, 1e-10);
}
// G1: tangent continuity — plane surfaces
TEST(SurfaceQualityTest, G1TangentContinuity) {
auto s1 = make_plane_surf();
// Derivatives at same point on same surface → should be consistent
Vector3D du = s1.derivative_u(0.5, 0.5);
Vector3D dv = s1.derivative_v(0.5, 0.5);
// Plane has constant derivatives
EXPECT_GT(du.norm(), 0.0);
EXPECT_GT(dv.norm(), 0.0);
// Derivatives should be orthogonal (flat plane)
EXPECT_NEAR(du.dot(dv), 0.0, 1e-10);
}
// G2: curvature continuity — cylindrical surface
TEST(SurfaceQualityTest, G2CurvatureContinuity) {
auto s = make_cyl_surf();
// Check that curvature is constant along v-direction
Vector3D n1 = s.normal(0.1, 0.2);
Vector3D n2 = s.normal(0.1, 0.8);
// Normals should be similar (same u, different v on cylinder)
double dot = n1.dot(n2);
EXPECT_GT(dot, 0.99);
}
// G3: curvature rate continuity (torsion-like)
TEST(SurfaceQualityTest, G3CurvatureRateContinuity) {
auto s = make_plane_surf();
// For a plane, all higher-order derivatives are consistent
// G3 requires that the derivative of curvature is continuous
Vector3D du_a = s.derivative_u(0.2, 0.5);
Vector3D du_b = s.derivative_u(0.8, 0.5);
// Plane: derivative_u is constant
double diff = Vector3D(du_b.x()-du_a.x(), du_b.y()-du_a.y(), du_b.z()-du_a.z()).norm();
EXPECT_LT(diff, 1e-10);
}
// Surface normal consistency
TEST(SurfaceQualityTest, NormalConsistency) {
auto s = make_plane_surf();
Vector3D n1 = s.normal(0.1, 0.1);
Vector3D n2 = s.normal(0.5, 0.5);
Vector3D n3 = s.normal(0.9, 0.9);
// All normals should be parallel for a plane
EXPECT_GT(n1.dot(n2), 0.999);
EXPECT_GT(n2.dot(n3), 0.999);
}
// Surface evaluation boundaries
TEST(SurfaceQualityTest, EvaluationBoundaries) {
auto s = make_plane_surf();
// Boundary evaluations should not crash
Point3D p00 = s.evaluate(0.0, 0.0);
Point3D p01 = s.evaluate(0.0, 1.0);
Point3D p10 = s.evaluate(1.0, 0.0);
Point3D p11 = s.evaluate(1.0, 1.0);
// Corners should be at expected positions for unit plane
EXPECT_NEAR(p00.x(), 0.0, 1e-10);
EXPECT_NEAR(p00.y(), 0.0, 1e-10);
EXPECT_NEAR(p11.x(), 1.0, 1e-10);
EXPECT_NEAR(p11.y(), 1.0, 1e-10);
}
// Derivative validity (non-zero, non-NaN)
TEST(SurfaceQualityTest, DerivativeValidity) {
auto s = make_plane_surf();
auto c = make_cyl_surf();
// Plane derivatives
Vector3D pdu = s.derivative_u(0.3, 0.7);
Vector3D pdv = s.derivative_v(0.3, 0.7);
EXPECT_TRUE(std::isfinite(pdu.x()) && std::isfinite(pdu.y()) && std::isfinite(pdu.z()));
EXPECT_TRUE(std::isfinite(pdv.x()) && std::isfinite(pdv.y()) && std::isfinite(pdv.z()));
// Cylinder derivatives
Vector3D cdu = c.derivative_u(0.3, 0.7);
Vector3D cdv = c.derivative_v(0.3, 0.7);
EXPECT_TRUE(std::isfinite(cdu.x()) && std::isfinite(cdu.y()) && std::isfinite(cdu.z()));
EXPECT_TRUE(std::isfinite(cdv.x()) && std::isfinite(cdv.y()) && std::isfinite(cdv.z()));
}
// Surface parameterization validity
TEST(SurfaceQualityTest, ParameterizationDomain) {
auto s = make_cyl_surf();
// Evaluate at various (u,v) in [0,1]
for (double u : {0.0, 0.25, 0.5, 0.75, 1.0}) {
for (double v : {0.0, 0.5, 1.0}) {
Point3D p = s.evaluate(u, v);
EXPECT_TRUE(std::isfinite(p.x()));
EXPECT_TRUE(std::isfinite(p.y()));
EXPECT_TRUE(std::isfinite(p.z()));
}
}
}
// Curvature sign consistency
TEST(SurfaceQualityTest, CurvatureSignConsistency) {
auto s = make_cyl_surf();
const double u = 0.3, v = 0.5;
const Point3D p = s.evaluate(u, v);
const Vector3D n = s.normal(u, v);
// 逐点独立复算:四分之一单位圆柱(轴 = z 轴)上任意点径向半径恒为 1。
EXPECT_NEAR(std::hypot(p.x(), p.y()), 1.0, 1e-9);
// 凸面外法向应背离轴线(即与径向单位向量同向)。
Vector3D radial(p.x(), p.y(), 0.0);
ASSERT_GT(radial.norm(), 1e-12);
const Vector3D r_hat = radial.normalized();
EXPECT_NEAR(n.dot(r_hat), 1.0, 1e-9);
// 曲率符号一致性:沿 u 的法曲率 κ_u = (n·S_uu)/|S_u|²。
// 外法向 + 圆弧朝轴心弯曲 ⇒ κ_u < 0,且对半径 r=1 有 κ_u = -1/r = -1
// (该量是几何量,与参数化缩放无关)。
const Vector3D su = s.derivative_u(u, v);
const Vector3D suu = s.derivative_uu(u, v);
ASSERT_GT(su.squaredNorm(), 1e-12);
const double kappa_u = n.dot(suu) / su.squaredNorm();
EXPECT_LT(kappa_u, 0.0);
EXPECT_NEAR(kappa_u, -1.0, 1e-6);
}
// Multi patch continuity check
TEST(SurfaceQualityTest, MultiPatchEdgeMatching) {
// Two adjacent plane patches
auto s1 = make_plane_surf();
std::vector<std::vector<Point3D>> cp2 = {
{Point3D(1, 0, 0), Point3D(1, 1, 0)},
{Point3D(2, 0, 0), Point3D(2, 1, 0)}
};
NurbsSurface s2(cp2, {0, 0, 1, 1}, {0, 0, 1, 1}, {}, 1, 1);
// Check shared edge: s1(1.0, v) == s2(0.0, v)
for (double v : {0.0, 0.25, 0.5, 0.75, 1.0}) {
Point3D p1 = s1.evaluate(1.0, v);
Point3D p2 = s2.evaluate(0.0, v);
double dist = Vector3D(p2.x()-p1.x(), p2.y()-p1.y(), p2.z()-p1.z()).norm();
EXPECT_LT(dist, 1e-10);
}
}