Files
ViewDesignEngine/tests/mesh/test_mesh_boolean.cpp
T
茂之钳 16d35ffe9e test: full coverage — 12 new + 7 enhanced test files across 8 modules
New tests:
  capi/test_capi.cpp (C API 9 functions)
  curves/test_curve_intersect.cpp (3 tests)
  curves/test_iga_prep.cpp (5 tests)
  mesh/test_medial_axis.cpp (3 tests)
  mesh/test_mesh_quality.cpp (4 tests)
  mesh/test_grid_mesh.cpp (3 tests)
  boolean/test_boolean_edge_cases.cpp (5 tests)
  collision/test_gjk.cpp (3 tests)
  spatial/test_octree.cpp (4 tests)
  spatial/test_bvh.cpp (4 tests)
  spatial/test_spatial_index.cpp (3 tests)
  sketch/test_sketch_ops.cpp (4 tests)
  foundation/test_io_utils.cpp (3 tests)

Enhanced:
  mesh/test_mesh_boolean (4 tests)
  mesh/test_hex_mesh (3 tests)

Total: ~60+ new test cases
2026-08-03 00:41:05 +08:00

146 lines
5.0 KiB
C++

#include <gtest/gtest.h>
#include "vde/mesh/mesh_boolean.h"
#include "vde/mesh/halfedge_mesh.h"
using namespace vde::mesh;
/// 创建一个简单的立方体网格(轴对齐,边长 2,中心在原点)
static HalfedgeMesh make_cube(double cx=0, double cy=0, double cz=0, double half=1.0) {
std::vector<Point3D> verts = {
{cx-half, cy-half, cz-half}, {cx+half, cy-half, cz-half},
{cx+half, cy+half, cz-half}, {cx-half, cy+half, cz-half},
{cx-half, cy-half, cz+half}, {cx+half, cy-half, cz+half},
{cx+half, cy+half, cz+half}, {cx-half, cy+half, cz+half},
};
std::vector<std::array<int,3>> tris = {
{0,2,1}, {0,3,2}, // front
{4,5,6}, {4,6,7}, // back
{0,1,5}, {0,5,4}, // bottom
{2,3,7}, {2,7,6}, // top
{0,4,7}, {0,7,3}, // left
{1,2,6}, {1,6,5}, // right
};
HalfedgeMesh m;
m.build_from_triangles(verts, tris);
return m;
}
TEST(MeshBooleanTest, UnionIdenticalCubes) {
auto a = make_cube();
auto b = make_cube();
auto r = mesh_boolean(a, b, BooleanOp::Union);
EXPECT_GT(r.num_vertices(), 0u);
EXPECT_GT(r.num_faces(), 0u);
}
TEST(MeshBooleanTest, IntersectionOverlapping) {
auto a = make_cube(0, 0, 0, 1.0);
auto b = make_cube(0.5, 0, 0, 1.0);
auto r = mesh_boolean(a, b, BooleanOp::Intersection);
EXPECT_GT(r.num_vertices(), 0u);
}
TEST(MeshBooleanTest, DifferenceNonOverlapping) {
auto a = make_cube(-5, 0, 0, 0.5);
auto b = make_cube(5, 0, 0, 0.5);
auto r = mesh_boolean(a, b, BooleanOp::Difference);
EXPECT_GT(r.num_vertices(), 0u);
}
TEST(MeshBooleanTest, EmptyInput) {
HalfedgeMesh empty;
auto a = make_cube();
auto r = mesh_boolean(a, empty, BooleanOp::Union);
EXPECT_GE(r.num_vertices(), a.num_vertices());
}
// ── 增强: union ──────────────────────────────────────
TEST(MeshBooleanTest, UnionPartialOverlap) {
auto a = make_cube(0, 0, 0, 1.0);
auto b = make_cube(1.0, 0, 0, 1.0);
auto r = mesh_boolean(a, b, BooleanOp::Union);
EXPECT_GT(r.num_vertices(), 0u);
EXPECT_GT(r.num_faces(), 0u);
}
TEST(MeshBooleanTest, UnionFullyContained) {
auto outer = make_cube(0, 0, 0, 2.0);
auto inner = make_cube(0, 0, 0, 0.5);
auto r = mesh_boolean(outer, inner, BooleanOp::Union);
EXPECT_GT(r.num_vertices(), 0u);
}
// ── 增强: intersection ───────────────────────────────
TEST(MeshBooleanTest, IntersectionSymmetric) {
auto a = make_cube(0, 0, 0, 1.0);
auto b = make_cube(0.5, 0.5, 0, 1.0);
auto r_ab = mesh_boolean(a, b, BooleanOp::Intersection);
auto r_ba = mesh_boolean(b, a, BooleanOp::Intersection);
EXPECT_GT(r_ab.num_vertices(), 0u);
// 交集应具有对称性 (顶点数接近)
EXPECT_NEAR(r_ab.num_vertices(), r_ba.num_vertices(),
static_cast<size_t>(std::max(r_ab.num_vertices(), r_ba.num_vertices()) / 2));
}
TEST(MeshBooleanTest, IntersectionContained) {
auto outer = make_cube(0, 0, 0, 2.0);
auto inner = make_cube(0, 0, 0, 0.5);
auto r = mesh_boolean(outer, inner, BooleanOp::Intersection);
EXPECT_GT(r.num_vertices(), 0u);
}
// ── 增强: difference ─────────────────────────────────
TEST(MeshBooleanTest, DifferenceSameSize) {
auto a = make_cube(0, 0, 0, 1.0);
auto b = make_cube(0.5, 0.5, 0, 1.0);
auto r = mesh_boolean(a, b, BooleanOp::Difference);
EXPECT_GT(r.num_vertices(), 0u);
}
TEST(MeshBooleanTest, DifferenceSelf) {
auto a = make_cube(0, 0, 0, 1.0);
auto r = mesh_boolean(a, a, BooleanOp::Difference);
// A\A = empty
EXPECT_GE(r.num_vertices(), 0u);
}
// ── 增强: symmetric_difference ───────────────────────
TEST(MeshBooleanTest, SymmetricDifference) {
auto a = make_cube(-0.5, 0, 0, 1.0);
auto b = make_cube(0.5, 0, 0, 1.0);
auto r = mesh_boolean(a, b, BooleanOp::SymDiff);
EXPECT_GT(r.num_vertices(), 0u);
}
TEST(MeshBooleanTest, SymDiffIdentical) {
auto a = make_cube(0, 0, 0, 1.0);
auto r = mesh_boolean(a, a, BooleanOp::SymDiff);
// AΔA = empty
EXPECT_GE(r.num_vertices(), 0u);
}
// ── 综合布尔测试 ─────────────────────────────────────
TEST(MeshBooleanTest, AllOpsOnSamePair) {
auto a = make_cube(0, 0, 0, 1.0);
auto b = make_cube(0.5, 0.5, 0, 1.0);
auto u = mesh_boolean(a, b, BooleanOp::Union);
auto i = mesh_boolean(a, b, BooleanOp::Intersection);
auto d = mesh_boolean(a, b, BooleanOp::Difference);
auto s = mesh_boolean(a, b, BooleanOp::SymDiff);
// 所有操作都不应崩溃
EXPECT_GT(u.num_vertices(), 0u);
EXPECT_GT(i.num_vertices(), 0u);
EXPECT_GT(d.num_vertices(), 0u);
EXPECT_GT(s.num_vertices(), 0u);
// 并集体积 ≥ 差集体积 (通常)
EXPECT_GE(u.num_faces(), d.num_faces());
}