- 新增 flattening.h/flattening.cpp:flattenMesh/flattenSurface/approximateDevelopable/generateSeamCuts - LSCM 初始化 + ARAP 迭代(SVD 局部 + 稀疏 CG 全局),9 项测试全部通过 - 文档复核:VDE-156~172/508/510 等已实现 API 状态 Deferred->Fixed
209 lines
8.1 KiB
C++
209 lines
8.1 KiB
C++
// VDE-152: 曲面展开 / 展平测试
|
||
#include <gtest/gtest.h>
|
||
#include "vde/mesh/flattening.h"
|
||
#include "vde/curves/nurbs_surface.h"
|
||
#include <cmath>
|
||
#include <vector>
|
||
|
||
using namespace vde::mesh;
|
||
using vde::curves::NurbsSurface;
|
||
|
||
namespace {
|
||
|
||
// 生成 nx×ny 的平面网格(Z=0 平面)
|
||
HalfedgeMesh make_planar_grid(int nx, int ny) {
|
||
std::vector<Point3D> verts;
|
||
std::vector<std::array<int, 3>> tris;
|
||
for (int j = 0; j < ny; ++j)
|
||
for (int i = 0; i < nx; ++i)
|
||
verts.push_back(Point3D(i, j, 0));
|
||
auto idx = [nx](int i, int j) { return j * nx + i; };
|
||
for (int j = 0; j < ny - 1; ++j)
|
||
for (int i = 0; i < nx - 1; ++i) {
|
||
int a = idx(i, j), b = idx(i + 1, j), c = idx(i + 1, j + 1), d = idx(i, j + 1);
|
||
tris.push_back({a, b, c});
|
||
tris.push_back({a, c, d});
|
||
}
|
||
HalfedgeMesh mesh;
|
||
mesh.build_from_triangles(verts, tris);
|
||
return mesh;
|
||
}
|
||
|
||
// 生成切开一条母线的开放圆柱面片(单连通、可展)
|
||
HalfedgeMesh make_cylinder_sheet(int nseg, int nring, double radius = 1.0, double height = 2.0) {
|
||
std::vector<Point3D> verts;
|
||
std::vector<std::array<int, 3>> tris;
|
||
for (int j = 0; j < nring; ++j)
|
||
for (int i = 0; i < nseg; ++i) {
|
||
double ang = 2.0 * M_PI * i / nseg;
|
||
verts.push_back(Point3D(radius * std::cos(ang), radius * std::sin(ang),
|
||
height * j / (nring - 1)));
|
||
}
|
||
auto idx = [nseg](int i, int j) { return j * nseg + i; };
|
||
for (int j = 0; j < nring - 1; ++j)
|
||
for (int i = 0; i < nseg - 1; ++i) {
|
||
int a = idx(i, j), b = idx(i + 1, j), c = idx(i + 1, j + 1), d = idx(i, j + 1);
|
||
tris.push_back({a, b, c});
|
||
tris.push_back({a, c, d});
|
||
}
|
||
HalfedgeMesh mesh;
|
||
mesh.build_from_triangles(verts, tris);
|
||
return mesh;
|
||
}
|
||
|
||
// 生成 UV 球(封闭、高曲率)
|
||
HalfedgeMesh make_uv_sphere(int nlat, int nlon, double radius = 1.0) {
|
||
std::vector<Point3D> verts;
|
||
std::vector<std::array<int, 3>> tris;
|
||
for (int j = 0; j <= nlat; ++j) {
|
||
double theta = M_PI * j / nlat;
|
||
for (int i = 0; i < nlon; ++i) {
|
||
double phi = 2.0 * M_PI * i / nlon;
|
||
verts.push_back(Point3D(radius * std::sin(theta) * std::cos(phi),
|
||
radius * std::sin(theta) * std::sin(phi),
|
||
radius * std::cos(theta)));
|
||
}
|
||
}
|
||
auto idx = [nlon](int i, int j) { return j * nlon + i; };
|
||
for (int j = 0; j < nlat; ++j) {
|
||
for (int i = 0; i < nlon; ++i) {
|
||
int a = idx(i, j), b = idx((i + 1) % nlon, j);
|
||
int c = idx((i + 1) % nlon, j + 1), d = idx(i, j + 1);
|
||
tris.push_back({a, b, c});
|
||
tris.push_back({a, c, d});
|
||
}
|
||
}
|
||
HalfedgeMesh mesh;
|
||
mesh.build_from_triangles(verts, tris);
|
||
return mesh;
|
||
}
|
||
|
||
} // namespace
|
||
|
||
// ── 平面网格:展开应为等距(无畸变、无翻转、快速收敛) ──
|
||
TEST(FlatteningTest, PlanarGrid_IsIsometric) {
|
||
auto mesh = make_planar_grid(5, 5);
|
||
FlattenOptions opts;
|
||
opts.maxIterations = 200;
|
||
auto r = flattenMesh(mesh, opts);
|
||
EXPECT_TRUE(r.isBijective) << "平面网格不应产生翻转三角形";
|
||
EXPECT_TRUE(r.converged) << "平面网格应在少量迭代内收敛";
|
||
EXPECT_NEAR(r.avgDistortion, 1.0, 1e-3) << "平面网格平均面积畸变应为 1";
|
||
EXPECT_NEAR(r.maxAngleErrorDeg, 0.0, 1e-2) << "平面网格角度误差应接近 0";
|
||
ASSERT_EQ(r.uvPositions.size(), mesh.num_vertices());
|
||
ASSERT_EQ(r.areaDistortion.size(), mesh.num_faces());
|
||
for (double d : r.areaDistortion)
|
||
EXPECT_NEAR(d, 1.0, 1e-3) << "平面网格逐面面积畸变应为 1";
|
||
}
|
||
|
||
// ── 面积保持:areaWeight=1 时总面积应精确保持 ──
|
||
TEST(FlatteningTest, PlanarGrid_AreaPreservation) {
|
||
auto mesh = make_planar_grid(4, 4);
|
||
FlattenOptions opts;
|
||
opts.areaWeight = 1.0;
|
||
opts.maxIterations = 100;
|
||
auto r = flattenMesh(mesh, opts);
|
||
EXPECT_NEAR(r.avgDistortion, 1.0, 1e-3);
|
||
}
|
||
|
||
// ── 开放圆柱面片(可展):展开后畸变应很小且无双射翻转 ──
|
||
TEST(FlatteningTest, CylinderSheet_Developable) {
|
||
auto mesh = make_cylinder_sheet(24, 5);
|
||
FlattenOptions opts;
|
||
opts.areaWeight = 1.0; // 可展面纯等距,面积权重取 1 检验精确性
|
||
opts.maxIterations = 500;
|
||
auto r = flattenMesh(mesh, opts);
|
||
EXPECT_TRUE(r.isBijective) << "圆柱面片展开不应产生翻转";
|
||
EXPECT_NEAR(r.avgDistortion, 1.0, 0.05) << "可展面片平均面积畸变应接近 1";
|
||
EXPECT_GT(r.maxStretch, 0.0);
|
||
EXPECT_LE(r.maxAngleErrorDeg, 60.0) << "可展面片角度误差应有界";
|
||
}
|
||
|
||
// ── 退化网格:应返回无效结果而非崩溃 ──
|
||
TEST(FlatteningTest, DegenerateMesh_ReturnsInvalid) {
|
||
HalfedgeMesh mesh;
|
||
mesh.build_from_triangles({Point3D(0, 0, 0), Point3D(1, 0, 0)}, {});
|
||
auto r = flattenMesh(mesh);
|
||
EXPECT_FALSE(r.isBijective);
|
||
}
|
||
|
||
// ── flattenSurface:平面 NURBS 曲面采样展开 ──
|
||
TEST(FlatteningTest, FlattenSurface_PlanarNurbs) {
|
||
// degree-1 平面 NURBS 面片(单位正方形,Z=0)
|
||
std::vector<std::vector<Point3D>> ctrl = {
|
||
{Point3D(0, 0, 0), Point3D(1, 0, 0)},
|
||
{Point3D(0, 1, 0), Point3D(1, 1, 0)}};
|
||
std::vector<double> ku = {0, 0, 1, 1}, kv = {0, 0, 1, 1};
|
||
std::vector<std::vector<double>> w = {{1, 1}, {1, 1}};
|
||
NurbsSurface surf(ctrl, ku, kv, w, 1, 1);
|
||
|
||
FlattenOptions opts;
|
||
opts.maxIterations = 100;
|
||
auto r = flattenSurface(surf, 5, 5, opts);
|
||
EXPECT_TRUE(r.isBijective);
|
||
EXPECT_NEAR(r.avgDistortion, 1.0, 1e-2);
|
||
EXPECT_EQ(r.uvPositions.size(), 6u * 6u); // (5+1)×(5+1)
|
||
EXPECT_EQ(r.uvCoords.size(), r.uvPositions.size()); // 参数空间坐标与 UV 同规模
|
||
// uvCoords 应为参数空间坐标 (u,v)∈[0,1]²(平面面片)
|
||
for (const auto& c : r.uvCoords) {
|
||
EXPECT_GE(c.first, 0.0);
|
||
EXPECT_LE(c.first, 1.0);
|
||
EXPECT_GE(c.second, 0.0);
|
||
EXPECT_LE(c.second, 1.0);
|
||
}
|
||
}
|
||
|
||
// ── approximateDevelopable:平面网格应近似为单一片 ──
|
||
TEST(FlatteningTest, ApproximateDevelopable_Planar_SingleGroup) {
|
||
auto mesh = make_planar_grid(6, 6);
|
||
DevelopableApproxOptions opts;
|
||
auto patches = approximateDevelopable(mesh, opts);
|
||
EXPECT_EQ(patches.size(), 1u) << "完全平面的网格应合并为单个可展片";
|
||
}
|
||
|
||
// ── approximateDevelopable:球面应分割为多个可展片 ──
|
||
TEST(FlatteningTest, ApproximateDevelopable_Sphere_MultiGroup) {
|
||
auto mesh = make_uv_sphere(12, 16);
|
||
DevelopableApproxOptions opts;
|
||
auto patches = approximateDevelopable(mesh, opts);
|
||
EXPECT_GT(patches.size(), 1u) << "高曲率球面应被分割为多个可展片";
|
||
// 每个片应包含至少一个三角形且顶点有效
|
||
for (const auto& p : patches) {
|
||
EXPECT_GE(p.num_faces(), 1u);
|
||
EXPECT_GE(p.num_vertices(), 3u);
|
||
}
|
||
}
|
||
|
||
// ── generateSeamCuts:封闭立方体应产生割缝链 ──
|
||
TEST(FlatteningTest, GenerateSeamCuts_ClosedCube_ProducesSeams) {
|
||
// 立方体 12 个三角形
|
||
std::vector<Point3D> v = {
|
||
Point3D(0, 0, 0), Point3D(1, 0, 0), Point3D(1, 1, 0), Point3D(0, 1, 0),
|
||
Point3D(0, 0, 1), Point3D(1, 0, 1), Point3D(1, 1, 1), Point3D(0, 1, 1)};
|
||
std::vector<std::array<int, 3>> tris = {
|
||
{0, 1, 2}, {0, 2, 3}, // bottom
|
||
{5, 4, 7}, {5, 7, 6}, // top
|
||
{0, 4, 5}, {0, 5, 1}, // front
|
||
{2, 6, 7}, {2, 7, 3}, // back
|
||
{1, 5, 6}, {1, 6, 2}, // right
|
||
{4, 0, 3}, {4, 3, 7}}; // left
|
||
HalfedgeMesh mesh;
|
||
mesh.build_from_triangles(v, tris);
|
||
|
||
auto seams = generateSeamCuts(mesh);
|
||
EXPECT_FALSE(seams.empty()) << "封闭网格必须割缝才能展开";
|
||
for (const auto& chain : seams) {
|
||
EXPECT_GE(chain.size(), 2u);
|
||
for (int vi : chain) {
|
||
EXPECT_GE(vi, 0);
|
||
EXPECT_LT(vi, static_cast<int>(mesh.num_vertices()));
|
||
}
|
||
}
|
||
}
|
||
|
||
// ── generateSeamCuts:空网格返回空 ──
|
||
TEST(FlatteningTest, GenerateSeamCuts_EmptyMesh_ReturnsEmpty) {
|
||
HalfedgeMesh mesh;
|
||
EXPECT_TRUE(generateSeamCuts(mesh).empty());
|
||
}
|