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。
296 lines
12 KiB
C++
296 lines
12 KiB
C++
#include <gtest/gtest.h>
|
|
#include "vde/mesh/mesh_parameterize.h"
|
|
#include "vde/mesh/halfedge_mesh.h"
|
|
#include <cmath>
|
|
#include <array>
|
|
#include <vector>
|
|
#include <algorithm>
|
|
|
|
using namespace vde::mesh;
|
|
using namespace vde::core;
|
|
|
|
// ═══════════════════════════════════════════════════════════
|
|
// Helper: 拓扑圆盘网格(带单一边界的平面网格)
|
|
// ═══════════════════════════════════════════════════════════
|
|
|
|
static HalfedgeMesh make_disk_mesh(int rows, int cols) {
|
|
std::vector<Point3D> verts;
|
|
std::vector<std::array<int, 3>> tris;
|
|
for (int i = 0; i < rows; ++i) {
|
|
for (int j = 0; j < cols; ++j) {
|
|
verts.push_back(Point3D(j, i, 0.0));
|
|
}
|
|
}
|
|
for (int i = 0; i < rows - 1; ++i) {
|
|
for (int j = 0; j < cols - 1; ++j) {
|
|
int v00 = i * cols + j;
|
|
int v10 = (i + 1) * cols + j;
|
|
int v11 = (i + 1) * cols + j + 1;
|
|
int v01 = i * cols + j + 1;
|
|
tris.push_back({v00, v10, v11});
|
|
tris.push_back({v00, v11, v01});
|
|
}
|
|
}
|
|
HalfedgeMesh m;
|
|
m.build_from_triangles(verts, tris);
|
|
return m;
|
|
}
|
|
|
|
static HalfedgeMesh make_single_tri() {
|
|
HalfedgeMesh m;
|
|
m.build_from_triangles(
|
|
{Point3D(0, 0, 0), Point3D(1, 0, 0), Point3D(0.5, 0.866, 0)},
|
|
{{0, 1, 2}});
|
|
return m;
|
|
}
|
|
|
|
// ═══════════════════════════════════════════════════════════
|
|
// tutte_parameterization
|
|
// ═══════════════════════════════════════════════════════════
|
|
|
|
TEST(MeshParameterizeTest, Tutte_Disk_UVCount) {
|
|
auto mesh = make_disk_mesh(4, 4);
|
|
auto uv = tutte_parameterization(mesh);
|
|
EXPECT_EQ(uv.size(), mesh.num_vertices());
|
|
for (const auto& p : uv) {
|
|
EXPECT_TRUE(std::isfinite(p.x()));
|
|
EXPECT_TRUE(std::isfinite(p.y()));
|
|
}
|
|
}
|
|
|
|
TEST(MeshParameterizeTest, Tutte_Disk_WithinUnitRange) {
|
|
auto mesh = make_disk_mesh(5, 5);
|
|
auto uv = tutte_parameterization(mesh);
|
|
// Tutte 边界固定为单位圆 → 坐标应在 [-1,1] 附近
|
|
for (const auto& p : uv) {
|
|
EXPECT_GE(p.x(), -1.01);
|
|
EXPECT_LE(p.x(), 1.01);
|
|
EXPECT_GE(p.y(), -1.01);
|
|
EXPECT_LE(p.y(), 1.01);
|
|
}
|
|
}
|
|
|
|
TEST(MeshParameterizeTest, Tutte_SingleTriangle) {
|
|
auto mesh = make_single_tri();
|
|
auto uv = tutte_parameterization(mesh);
|
|
EXPECT_EQ(uv.size(), 3u);
|
|
}
|
|
|
|
TEST(MeshParameterizeTest, Tutte_Deterministic) {
|
|
auto mesh = make_disk_mesh(3, 3);
|
|
auto uv1 = tutte_parameterization(mesh);
|
|
auto uv2 = tutte_parameterization(mesh);
|
|
for (size_t i = 0; i < uv1.size(); ++i) {
|
|
EXPECT_NEAR(uv1[i].x(), uv2[i].x(), 1e-12);
|
|
EXPECT_NEAR(uv1[i].y(), uv2[i].y(), 1e-12);
|
|
}
|
|
}
|
|
|
|
// ═══════════════════════════════════════════════════════════
|
|
// lscm_parameterization
|
|
// ═══════════════════════════════════════════════════════════
|
|
|
|
TEST(MeshParameterizeTest, LSCM_Disk_UVCount) {
|
|
auto mesh = make_disk_mesh(4, 4);
|
|
auto uv = lscm_parameterization(mesh);
|
|
EXPECT_EQ(uv.size(), mesh.num_vertices());
|
|
}
|
|
|
|
TEST(MeshParameterizeTest, LSCM_SingleTriangle) {
|
|
auto mesh = make_single_tri();
|
|
auto uv = lscm_parameterization(mesh);
|
|
EXPECT_EQ(uv.size(), 3u);
|
|
}
|
|
|
|
// ═══════════════════════════════════════════════════════════
|
|
// arap_parameterization + boundary_free_param
|
|
// ═══════════════════════════════════════════════════════════
|
|
|
|
TEST(MeshParameterizeTest, ARAP_Disk_UVCount) {
|
|
auto mesh = make_disk_mesh(4, 4);
|
|
auto uv = arap_parameterization(mesh, 20);
|
|
EXPECT_EQ(uv.size(), mesh.num_vertices());
|
|
}
|
|
|
|
TEST(MeshParameterizeTest, ARAP_SingleTriangle) {
|
|
auto mesh = make_single_tri();
|
|
auto uv = arap_parameterization(mesh);
|
|
EXPECT_EQ(uv.size(), 3u);
|
|
}
|
|
|
|
TEST(MeshParameterizeTest, BoundaryFree_Disk_UVCount) {
|
|
auto mesh = make_disk_mesh(4, 4);
|
|
auto uv = boundary_free_param(mesh);
|
|
EXPECT_EQ(uv.size(), mesh.num_vertices());
|
|
}
|
|
|
|
// ═══════════════════════════════════════════════════════════
|
|
// parameterization_quality
|
|
// ═══════════════════════════════════════════════════════════
|
|
|
|
TEST(MeshParameterizeTest, Quality_ValidOutput) {
|
|
auto mesh = make_disk_mesh(4, 4);
|
|
auto uv = tutte_parameterization(mesh);
|
|
auto q = parameterization_quality(mesh, uv);
|
|
EXPECT_EQ(q.total_triangles, mesh.num_faces());
|
|
EXPECT_GT(q.max_triangle_area, 0.0);
|
|
}
|
|
|
|
TEST(MeshParameterizeTest, Quality_ZeroUV_ZeroArea) {
|
|
auto mesh = make_disk_mesh(3, 3);
|
|
// 全零 UV → 三角形面积全 0 → 最大面积与面积保持度均为 0
|
|
std::vector<Point2D> bad_uv(mesh.num_vertices(), Point2D(0.0, 0.0));
|
|
auto q = parameterization_quality(mesh, bad_uv);
|
|
EXPECT_EQ(q.total_triangles, mesh.num_faces());
|
|
EXPECT_EQ(q.max_triangle_area, 0.0);
|
|
EXPECT_EQ(q.area_preservation, 0.0);
|
|
}
|
|
|
|
// ═══════════════════════════════════════════════════════════
|
|
// VDE-394: abf_parameterization
|
|
// ═══════════════════════════════════════════════════════════
|
|
|
|
/// 将规则网格等距映射到圆柱面(可展曲面 ⇒ 3D 角度 = 平面角度)
|
|
static HalfedgeMesh make_cylinder_arc_mesh(int rows, int cols) {
|
|
const double R = 2.0;
|
|
std::vector<Point3D> verts;
|
|
std::vector<std::array<int, 3>> tris;
|
|
for (int i = 0; i < rows; ++i) {
|
|
for (int j = 0; j < cols; ++j) {
|
|
const double s = j * 0.5; // 弧长
|
|
const double y = i * 0.5;
|
|
verts.push_back(Point3D(R * std::sin(s / R), y, R * (1.0 - std::cos(s / R))));
|
|
}
|
|
}
|
|
for (int i = 0; i < rows - 1; ++i) {
|
|
for (int j = 0; j < cols - 1; ++j) {
|
|
int v00 = i * cols + j, v10 = (i + 1) * cols + j;
|
|
int v11 = (i + 1) * cols + j + 1, v01 = i * cols + j + 1;
|
|
tris.push_back({v00, v10, v11});
|
|
tris.push_back({v00, v11, v01});
|
|
}
|
|
}
|
|
HalfedgeMesh m;
|
|
m.build_from_triangles(verts, tris);
|
|
return m;
|
|
}
|
|
|
|
/// 角畸变能量 E = Σ (θ_uv − θ_3d)²(测试侧独立复算,用于验证 ABF 确实下降)
|
|
static double angle_energy(const HalfedgeMesh& mesh, const std::vector<Point2D>& uv) {
|
|
double E = 0.0;
|
|
for (int fi = 0; fi < static_cast<int>(mesh.num_faces()); ++fi) {
|
|
auto vs = mesh.face_vertices(fi);
|
|
const int n = static_cast<int>(vs.size());
|
|
for (int k = 0; k < n; ++k) {
|
|
const int v0 = vs[(k + n - 1) % n], v1 = vs[k], v2 = vs[(k + 1) % n];
|
|
const Point3D p1 = mesh.vertex(v1);
|
|
const Vector3D e0 = mesh.vertex(v0) - p1, e2 = mesh.vertex(v2) - p1;
|
|
if (e0.norm() < 1e-12 || e2.norm() < 1e-12) continue;
|
|
const double a3 = std::acos(std::clamp(e0.dot(e2) / (e0.norm() * e2.norm()), -1.0, 1.0));
|
|
const double a0x = uv[v0].x() - uv[v1].x(), a0y = uv[v0].y() - uv[v1].y();
|
|
const double a2x = uv[v2].x() - uv[v1].x(), a2y = uv[v2].y() - uv[v1].y();
|
|
const double m0 = std::hypot(a0x, a0y), m2 = std::hypot(a2x, a2y);
|
|
if (m0 < 1e-15 || m2 < 1e-15) { E += a3 * a3; continue; }
|
|
const double au = std::acos(std::clamp((a0x * a2x + a0y * a2y) / (m0 * m2), -1.0, 1.0));
|
|
E += (au - a3) * (au - a3);
|
|
}
|
|
}
|
|
return E;
|
|
}
|
|
|
|
TEST(MeshParameterizeTest, ABF_Disk_UVCount) {
|
|
auto mesh = make_disk_mesh(4, 4);
|
|
auto uv = abf_parameterization(mesh, 20);
|
|
EXPECT_EQ(uv.size(), mesh.num_vertices());
|
|
for (const auto& p : uv) {
|
|
EXPECT_TRUE(std::isfinite(p.x()));
|
|
EXPECT_TRUE(std::isfinite(p.y()));
|
|
}
|
|
}
|
|
|
|
TEST(MeshParameterizeTest, ABF_SingleTriangle_NoFreeVertex) {
|
|
auto mesh = make_single_tri();
|
|
auto uv = abf_parameterization(mesh, 20);
|
|
EXPECT_EQ(uv.size(), 3u);
|
|
}
|
|
|
|
TEST(MeshParameterizeTest, ABF_ReducesAngleDistortion) {
|
|
auto mesh = make_cylinder_arc_mesh(5, 5);
|
|
auto tutte = tutte_parameterization(mesh);
|
|
auto abf = abf_parameterization(mesh, 100);
|
|
ASSERT_EQ(abf.size(), mesh.num_vertices());
|
|
for (const auto& p : abf) {
|
|
EXPECT_TRUE(std::isfinite(p.x()));
|
|
EXPECT_TRUE(std::isfinite(p.y()));
|
|
}
|
|
// ABF 直接最小化角畸变:能量必须不增,且在本例中严格下降
|
|
const double e_t = angle_energy(mesh, tutte);
|
|
const double e_a = angle_energy(mesh, abf);
|
|
EXPECT_LE(e_a, e_t + 1e-12);
|
|
EXPECT_LT(e_a, e_t);
|
|
}
|
|
|
|
// ═══════════════════════════════════════════════════════════
|
|
// VDE-394: pack_uv_charts
|
|
// ═══════════════════════════════════════════════════════════
|
|
|
|
static UVChart make_rect_chart(double w, double h) {
|
|
UVChart c;
|
|
c.uv_coords = {Point2D(0, 0), Point2D(w, 0), Point2D(w, h), Point2D(0, h)};
|
|
c.triangles = {{0, 1, 2}, {0, 2, 3}};
|
|
return c;
|
|
}
|
|
|
|
TEST(MeshParameterizeTest, PackUVCharts_InUnitSquare_NoOverlap) {
|
|
std::vector<UVChart> charts = {
|
|
make_rect_chart(1.0, 0.5),
|
|
make_rect_chart(0.3, 1.0),
|
|
make_rect_chart(0.5, 0.5),
|
|
};
|
|
auto atlas = pack_uv_charts(charts, 0.01);
|
|
ASSERT_EQ(atlas.charts.size(), 3u);
|
|
|
|
// 顶点数与三角形保留
|
|
for (size_t i = 0; i < charts.size(); ++i) {
|
|
EXPECT_EQ(atlas.charts[i].uv_coords.size(), charts[i].uv_coords.size());
|
|
EXPECT_EQ(atlas.charts[i].triangles.size(), charts[i].triangles.size());
|
|
}
|
|
|
|
// 全部 UV 落在单位正方形内
|
|
for (const auto& c : atlas.charts) {
|
|
for (const auto& p : c.uv_coords) {
|
|
EXPECT_GE(p.x(), -1e-9); EXPECT_LE(p.x(), 1.0 + 1e-9);
|
|
EXPECT_GE(p.y(), -1e-9); EXPECT_LE(p.y(), 1.0 + 1e-9);
|
|
}
|
|
}
|
|
|
|
// 包围盒两两不重叠(允许相邻共边)
|
|
auto bbox = [](const UVChart& c) {
|
|
double minx = c.uv_coords[0].x(), maxx = minx;
|
|
double miny = c.uv_coords[0].y(), maxy = miny;
|
|
for (const auto& p : c.uv_coords) {
|
|
minx = std::min(minx, p.x()); maxx = std::max(maxx, p.x());
|
|
miny = std::min(miny, p.y()); maxy = std::max(maxy, p.y());
|
|
}
|
|
return std::array<double, 4>{minx, miny, maxx, maxy};
|
|
};
|
|
for (size_t i = 0; i < atlas.charts.size(); ++i) {
|
|
for (size_t j = i + 1; j < atlas.charts.size(); ++j) {
|
|
auto a = bbox(atlas.charts[i]);
|
|
auto b = bbox(atlas.charts[j]);
|
|
const bool overlap = (a[0] < b[2] - 1e-6) && (b[0] < a[2] - 1e-6) &&
|
|
(a[1] < b[3] - 1e-6) && (b[1] < a[3] - 1e-6);
|
|
EXPECT_FALSE(overlap) << "charts " << i << " and " << j << " overlap";
|
|
}
|
|
}
|
|
|
|
EXPECT_GT(atlas.utilization, 0.0);
|
|
EXPECT_LE(atlas.utilization, 1.0);
|
|
}
|
|
|
|
TEST(MeshParameterizeTest, PackUVCharts_EmptyInput) {
|
|
auto atlas = pack_uv_charts({}, 0.005);
|
|
EXPECT_TRUE(atlas.charts.empty());
|
|
EXPECT_EQ(atlas.utilization, 0.0);
|
|
}
|