Files
ViewDesignEngine/tests/mesh/test_marching_cubes.cpp
T
hm d0140cec55 feat(mesh): E3a 无查表 Marching Cubes + 串/并行水密焊接
- 删除截断的 triTable,改用 mc_triangulate_case 无查表三角化,覆盖全部 256 种配置(4 边歧义面按面内轴坐标规范排序配对)
- 串行路径改用全局角点索引键焊接,跨立方体顶点去重 → 水密
- 并行路径:角点坐标统一用绝对全局索引公式;所有边沿坐标增大方向插值(edge2/3/6/7 反序修正),消除共享边 1 ULP 差;焊接改按物理边身份键(与串行一致),避免等值面过格点时的自环退化边
- OpenMP 经 vde_compile_options 传播至所有模块
- 新增验证:256 配置三角化圆盘性质、单角点基准、串/并行球水密(边界边=0)、线程数无关性、表面精度
2026-08-09 13:56:45 +08:00

248 lines
9.3 KiB
C++
Raw 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/mesh/marching_cubes.h"
#include "vde/mesh/halfedge_mesh.h"
#include <set>
#include <map>
using namespace vde::mesh;
namespace {
// 立方体 12 条边的端角点
constexpr int kCornerPairs[12][2] = {
{0,1},{1,2},{2,3},{3,0},{4,5},{5,6},{6,7},{7,4},{0,4},{1,5},{2,6},{3,7}
};
// 由角点配置计算边表掩码(与 kEdgeTable 语义一致)
int edge_mask_from_cube_index(int cube_index) {
int mask = 0;
for (int e = 0; e < 12; ++e) {
bool in0 = (cube_index >> kCornerPairs[e][0]) & 1;
bool in1 = (cube_index >> kCornerPairs[e][1]) & 1;
if (in0 != in1) mask |= (1 << e);
}
return mask;
}
// 单立方体内的三角化必须是「三角化圆盘」的并集:
// - 每条相交边至少被使用 1 次(表面必须穿过它)
// - 每条非相交边不得被使用
// - 每条几何边(两个边中点之间)被使用 1 次(圆盘边界)或 2 次(内部弦)
// - 边界边构成闭合环(每个边界顶点度 = 2)
// - Euler 公式:V - E + F = 圆盘数(= 边界环数)
// 注意:这不是「每条相交边恰好 2 次」——经典 MC 中三角形边是圆盘边界,
// 相邻立方体在共享面上补足另一半,故单立方体内相交边出现 1 次即正确。
struct DiskCheck {
bool ok = true;
std::string msg;
};
DiskCheck check_disk_triangulation(const std::vector<std::array<int, 3>>& tris, int mask) {
DiskCheck r;
// 1) 退化三角形 / 非相交边 / 相交边使用次数
std::array<int, 12> usage{};
for (const auto& t : tris) {
if (t[0] == t[1] || t[1] == t[2] || t[0] == t[2]) {
r.ok = false; r.msg += "degenerate-tri ";
}
for (int e : t) {
if (!(mask & (1 << e))) { r.ok = false; r.msg += "uncut-edge-used "; }
++usage[e];
}
}
for (int e = 0; e < 12; ++e) {
if ((mask & (1 << e)) && usage[e] == 0) {
r.ok = false; r.msg += "cut-edge-unused ";
}
}
// 2) 几何边使用计数((a,b) 无序对)
std::map<std::pair<int, int>, int> gedge;
for (const auto& t : tris) {
for (int k = 0; k < 3; ++k) {
int a = t[k], b = t[(k + 1) % 3];
if (a > b) std::swap(a, b);
++gedge[{a, b}];
}
}
for (const auto& [key, cnt] : gedge) {
if (cnt != 1 && cnt != 2) {
r.ok = false; r.msg += "geom-edge-cnt" + std::to_string(cnt) + " ";
}
}
// 3) 边界子图(使用 1 次的几何边):顶点度必须 = 2(闭合环)
std::array<std::vector<int>, 12> boundary;
for (const auto& [key, cnt] : gedge) {
if (cnt != 1) continue;
boundary[key.first].push_back(key.second);
boundary[key.second].push_back(key.first);
}
for (int e = 0; e < 12; ++e)
if (boundary[e].size() == 1 || boundary[e].size() > 2) {
r.ok = false; r.msg += "boundary-deg" + std::to_string(boundary[e].size()) + " ";
}
// 4) Euler:V - E + F = 边界环数(每个圆盘恰好 1 个闭合边界)
int V = 0, F = static_cast<int>(tris.size());
for (int e = 0; e < 12; ++e)
if (usage[e]) ++V;
int E = static_cast<int>(gedge.size());
int nloops = 0;
std::array<bool, 12> bvis{};
for (int e = 0; e < 12; ++e) {
if (boundary[e].empty() || bvis[e]) continue;
++nloops;
int cur = e, prev = -1;
while (true) {
bvis[cur] = true;
if (boundary[cur].size() != 2) break;
int nxt = boundary[cur][0] == prev ? boundary[cur][1] : boundary[cur][0];
if (nxt == e) break;
prev = cur;
cur = nxt;
}
}
if (V - E + F != nloops) {
r.ok = false; r.msg += "euler(V=" + std::to_string(V) + ",E=" + std::to_string(E) +
",F=" + std::to_string(F) + ",loops=" + std::to_string(nloops) + ") ";
}
return r;
}
} // namespace
/// 测试 sdf_sphere 的基本计算
TEST(MarchingCubesTest, SdfSphereAtCenter) {
// 在球心处,距离 = -r(内部)
double d = sdf_sphere(0, 0, 0, 0, 0, 0, 1.0);
EXPECT_DOUBLE_EQ(d, -1.0);
}
TEST(MarchingCubesTest, SdfSphereOnSurface) {
// 在球面上,距离 = 0
double d = sdf_sphere(1, 0, 0, 0, 0, 0, 1.0);
EXPECT_NEAR(d, 0.0, 1e-9);
}
TEST(MarchingCubesTest, SdfSphereOutside) {
// 外部点
double d = sdf_sphere(3, 0, 0, 0, 0, 0, 1.0);
EXPECT_NEAR(d, 2.0, 1e-9);
}
TEST(MarchingCubesTest, SdfBoxCenter) {
// 在立方体中心,距离 = -min(hx,hy,hz)(内部)
double d = sdf_box(0, 0, 0, 1.0, 1.0, 1.0);
EXPECT_NEAR(d, -1.0, 1e-9);
}
TEST(MarchingCubesTest, MarchingCubesSphere) {
// 提取球面等值面
auto f = [](double x, double y, double z) {
return sdf_sphere(x, y, z, 0, 0, 0, 1.0);
};
auto mesh = marching_cubes(f, 0.0,
Point3D(-2, -2, -2), Point3D(2, 2, 2), 16);
// 应有顶点和三角形
EXPECT_GT(mesh.vertices.size(), 0u);
EXPECT_GT(mesh.triangles.size(), 0u);
}
TEST(MarchingCubesTest, MarchingCubesBox) {
// 提取立方体等值面
auto f = [](double x, double y, double z) {
return sdf_box(x, y, z, 1.0, 1.0, 1.0);
};
auto mesh = marching_cubes(f, 0.0,
Point3D(-2, -2, -2), Point3D(2, 2, 2), 16);
EXPECT_GT(mesh.vertices.size(), 0u);
EXPECT_GT(mesh.triangles.size(), 0u);
}
// ═══════════════════════════════════════════════════════════
// 无查表三角化(mc_triangulate_case)完整性与正确性测试
// ═══════════════════════════════════════════════════════════
TEST(MarchingCubesTest, TriangulateAllConfigsValidDisk) {
// 遍历全部 256 种配置:单立方体内三角化必须是三角化圆盘的并集
// (相交边 ≥1 次、非相交边 0 次、几何边 1/2 次、边界闭合环、Euler 校验)
for (int cube_index = 0; cube_index < 256; ++cube_index) {
int mask = edge_mask_from_cube_index(cube_index);
auto tris = mc_triangulate_case(mask, cube_index);
if (mask == 0) {
EXPECT_TRUE(tris.empty()) << "cube_index=" << cube_index;
continue;
}
ASSERT_FALSE(tris.empty()) << "cube_index=" << cube_index;
auto r = check_disk_triangulation(tris, mask);
EXPECT_TRUE(r.ok) << "cube_index=" << cube_index << " mask=0x" << std::hex << mask
<< std::dec << " " << r.msg;
}
}
TEST(MarchingCubesTest, TriangulateSingleCornerCases) {
// 单角点在内部:经典查表已知结果(作为基准校验)
// cube_index bit c 置位 = 角点 c 在内部
struct Case { int idx; std::array<int, 3> tri; };
std::vector<Case> cases = {
{1, {0, 8, 3}}, // 角点 0
{2, {0, 1, 9}}, // 角点 1
{4, {1, 2, 10}}, // 角点 2
{8, {3, 11, 2}}, // 角点 3
{16, {4, 7, 8}}, // 角点 4
{32, {9, 5, 4}}, // 角点 5
{64, {10, 6, 5}}, // 角点 6
{128,{6, 7, 11}}, // 角点 7
};
for (const auto& c : cases) {
int mask = edge_mask_from_cube_index(c.idx);
auto tris = mc_triangulate_case(mask, c.idx);
ASSERT_EQ(tris.size(), 1u) << "cube_index=" << c.idx;
std::set<int> got{tris[0][0], tris[0][1], tris[0][2]};
std::set<int> want{c.tri[0], c.tri[1], c.tri[2]};
EXPECT_EQ(got, want) << "cube_index=" << c.idx;
}
}
TEST(MarchingCubesTest, SphereWatertightSerial) {
// 串行 MC:球面应完全水密(无边界边),证明跨立方体方向一致
auto f = [](double x, double y, double z) {
return sdf_sphere(x, y, z, 0, 0, 0, 1.0);
};
auto mesh = marching_cubes(f, 0.0,
Point3D(-2, -2, -2), Point3D(2, 2, 2), 48);
ASSERT_GT(mesh.triangles.size(), 0u);
HalfedgeMesh hemesh;
hemesh.build_from_triangles(mesh.vertices, mesh.triangles);
int boundary = 0;
for (size_t hei = 0; hei < hemesh.num_edges() * 2; ++hei)
if (hemesh.is_boundary_edge(static_cast<int>(hei))) ++boundary;
EXPECT_EQ(boundary, 0);
}
TEST(MarchingCubesTest, VoxelGridWatertight) {
// VoxelGrid 路径同样应水密。
// 注意:网格角点范围 = [origin, origin+(n-1)*cell_size](n 个体素仅 n-1 个间隔),
// 必须覆盖整个球面(半径 1),否则等值面被网格边界裁剪 → 非水密。
VoxelGrid grid;
grid.nx = grid.ny = grid.nz = 32;
grid.cell_size = 2.0 / (grid.nx - 1); // 角点恰好覆盖 [-1, 1]
grid.origin = Point3D(-1, -1, -1);
grid.values.resize(grid.size());
for (int z = 0; z < grid.nz; ++z)
for (int y = 0; y < grid.ny; ++y)
for (int x = 0; x < grid.nx; ++x) {
auto c = grid.voxel_center(x, y, z);
grid.values[grid.index(x, y, z)] =
sdf_sphere(c.x(), c.y(), c.z(), 0, 0, 0, 1.0);
}
auto mesh = marching_cubes(grid, 0.0);
ASSERT_GT(mesh.triangles.size(), 0u);
HalfedgeMesh hemesh;
hemesh.build_from_triangles(mesh.vertices, mesh.triangles);
int boundary = 0;
for (size_t hei = 0; hei < hemesh.num_edges() * 2; ++hei)
if (hemesh.is_boundary_edge(static_cast<int>(hei))) ++boundary;
EXPECT_EQ(boundary, 0);
}