Files
ViewDesignEngine/tests/boolean/test_boolean_mesh.cpp
T
hm 281ca1d394
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(mesh,boolean): VDE-893 修复 sdf_smooth_subtraction/smooth_hc_laplacian 坏实现并补判别性测试
sdf_smooth_subtraction: 参数顺序反转(等价 op_smooth_difference(d2,d1,k),与文档 d1-d2 及规范相反)+ k<=0 除零/NaN;改为 op_smooth_difference(d1,d2,k) 一致式并加 k<=0 锐利退化守卫。sdf_smooth_union 同补守卫。

smooth_hc_laplacian: 回推项误用顶点上一轮自身位移(契约应为 avg(neighbor_b)),单尖峰 5 轮由 1.0 放大到 1.99(发散);改为标准 Vollmer HC 两步,修复后 0.68 且不越界放大。

补 fit_nurbs_least_squares(平面点云拟合逐点复算)与 clip_mesh(裁剪保留面独立质心分类复算)判别性测试;复核 collision/sketch/mesh/boolean 清单假阳性并更新审计(未测试 160->140)。
2026-10-06 09:28:30 +08:00

320 lines
13 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/boolean/boolean_mesh.h"
#include "vde/mesh/halfedge_mesh.h"
#include <cmath>
using namespace vde::boolean;
using namespace vde::mesh;
using namespace vde::core;
// ── Helper: build a box mesh (12 triangles, 8 vertices) ──
// Face normals point outward (CCW winding from outside)
static HalfedgeMesh make_box(double w, double h, double d) {
double hw = w * 0.5, hh = h * 0.5, hd = d * 0.5;
std::vector<Point3D> verts = {
{-hw, -hh, -hd}, { hw, -hh, -hd}, { hw, hh, -hd}, {-hw, hh, -hd}, // 0-3: front
{-hw, -hh, hd}, { hw, -hh, hd}, { hw, hh, hd}, {-hw, hh, hd}, // 4-7: back
};
// CCW winding viewed from outside
std::vector<std::array<int,3>> tris = {
{0, 1, 2}, {0, 2, 3}, // front (-z)
{5, 4, 6}, {6, 4, 7}, // back (+z)
{1, 5, 6}, {1, 6, 2}, // right (+x)
{4, 0, 3}, {4, 3, 7}, // left (-x)
{0, 4, 5}, {0, 5, 1}, // bottom (-y)
{3, 2, 6}, {3, 6, 7}, // top (+y)
};
HalfedgeMesh mesh;
mesh.build_from_triangles(verts, tris);
return mesh;
}
// ── Helper: build a translated box ──
static HalfedgeMesh make_box_at(double cx, double cy, double cz,
double w, double h, double d) {
double hw = w * 0.5, hh = h * 0.5, hd = d * 0.5;
std::vector<Point3D> verts = {
{cx-hw, cy-hh, cz-hd}, {cx+hw, cy-hh, cz-hd},
{cx+hw, cy+hh, cz-hd}, {cx-hw, cy+hh, cz-hd},
{cx-hw, cy-hh, cz+hd}, {cx+hw, cy-hh, cz+hd},
{cx+hw, cy+hh, cz+hd}, {cx-hw, cy+hh, cz+hd},
};
std::vector<std::array<int,3>> tris = {
{0, 1, 2}, {0, 2, 3}, // front (-z)
{5, 4, 6}, {6, 4, 7}, // back (+z)
{1, 5, 6}, {1, 6, 2}, // right (+x)
{4, 0, 3}, {4, 3, 7}, // left (-x)
{0, 4, 5}, {0, 5, 1}, // bottom (-y)
{3, 2, 6}, {3, 6, 7}, // top (+y)
};
HalfedgeMesh mesh;
mesh.build_from_triangles(verts, tris);
return mesh;
}
// ═══════════════════════════════════════════════════════════
// Union tests
// ═══════════════════════════════════════════════════════════
TEST(BooleanMeshTest, Union_IntersectingBoxes_HasResult) {
auto a = make_box(2, 2, 2); // centered at origin
auto b = make_box_at(1, 0, 0, 2, 2, 2); // shifted +x by 1
auto result = mesh_union(a, b);
EXPECT_GT(result.num_faces(), 0u);
// Union should have at least as many faces as the individual boxes
// (since they partially overlap, the exterior faces are kept)
EXPECT_GE(result.num_faces(), a.num_faces() / 2);
}
TEST(BooleanMeshTest, Union_DisjointBoxes_HasAllFaces) {
auto a = make_box(1, 1, 1); // centered at origin
auto b = make_box_at(3, 0, 0, 1, 1, 1); // far away
auto result = mesh_union(a, b);
// Disjoint: both meshes should be fully retained
EXPECT_EQ(result.num_faces(), a.num_faces() + b.num_faces());
}
// ═══════════════════════════════════════════════════════════
// Intersection tests
// ═══════════════════════════════════════════════════════════
TEST(BooleanMeshTest, Intersection_OverlappingBoxes_HasResult) {
auto a = make_box(2, 2, 2); // [-1,1]³
auto b = make_box_at(1, 0, 0, 2, 2, 2); // [0,2]×[-1,1]×[-1,1]
auto result = mesh_intersection(a, b);
// Overlap region = [0,1]×[-1,1]×[-1,1] — should have faces
EXPECT_GT(result.num_faces(), 0u);
}
TEST(BooleanMeshTest, Intersection_Disjoint_ReturnsEmpty) {
auto a = make_box(1, 1, 1);
auto b = make_box_at(3, 0, 0, 1, 1, 1);
auto result = mesh_intersection(a, b);
EXPECT_EQ(result.num_faces(), 0u);
}
TEST(BooleanMeshTest, Intersection_BContainsA_ReturnsA) {
auto a = make_box(1, 1, 1); // small cube at origin
auto b = make_box(4, 4, 4); // large cube containing a
auto result = mesh_intersection(a, b);
// All faces of A are inside B → intersection should have 12 faces
EXPECT_EQ(result.num_faces(), a.num_faces());
}
// ═══════════════════════════════════════════════════════════
// Difference tests
// ═══════════════════════════════════════════════════════════
TEST(BooleanMeshTest, Difference_Overlapping_HasResult) {
auto a = make_box(2, 2, 2);
auto b = make_box_at(1, 0, 0, 2, 2, 2);
auto result = mesh_difference(a, b);
// A-b: faces of A whose centroids are outside B
EXPECT_GT(result.num_faces(), 0u);
// Should be fewer faces than original A (some faces are clipped)
EXPECT_LT(result.num_faces(), a.num_faces());
}
TEST(BooleanMeshTest, Difference_Disjoint_ReturnsAllA) {
auto a = make_box(1, 1, 1);
auto b = make_box_at(3, 0, 0, 1, 1, 1);
auto result = mesh_difference(a, b);
EXPECT_EQ(result.num_faces(), a.num_faces());
}
TEST(BooleanMeshTest, Difference_BContainsA_ReturnsEmpty) {
auto a = make_box(1, 1, 1); // small inside
auto b = make_box(4, 4, 4); // large container
auto result = mesh_difference(a, b);
// A is fully inside B → all faces are clipped
EXPECT_EQ(result.num_faces(), 0u);
}
// ═══════════════════════════════════════════════════════════
// Symmetric difference tests
// ═══════════════════════════════════════════════════════════
TEST(BooleanMeshTest, SymDiff_Overlapping_HasResult) {
auto a = make_box(2, 2, 2);
auto b = make_box_at(1, 0, 0, 2, 2, 2);
auto result = mesh_sym_diff(a, b);
EXPECT_GT(result.num_faces(), 0u);
}
// ═══════════════════════════════════════════════════════════
// Empty mesh input tests
// ═══════════════════════════════════════════════════════════
TEST(BooleanMeshTest, Union_EmptyA_ReturnsB) {
HalfedgeMesh empty;
auto b = make_box(2, 2, 2);
auto result = mesh_union(empty, b);
EXPECT_EQ(result.num_faces(), b.num_faces());
}
TEST(BooleanMeshTest, Union_EmptyB_ReturnsA) {
auto a = make_box(2, 2, 2);
HalfedgeMesh empty;
auto result = mesh_union(a, empty);
EXPECT_EQ(result.num_faces(), a.num_faces());
}
TEST(BooleanMeshTest, Intersection_EmptyA_ReturnsEmpty) {
HalfedgeMesh empty;
auto b = make_box(2, 2, 2);
auto result = mesh_intersection(empty, b);
EXPECT_EQ(result.num_faces(), 0u);
}
TEST(BooleanMeshTest, Difference_EmptyA_ReturnsEmpty) {
HalfedgeMesh empty;
auto b = make_box(2, 2, 2);
auto result = mesh_difference(empty, b);
EXPECT_EQ(result.num_faces(), 0u);
}
TEST(BooleanMeshTest, Difference_EmptyB_ReturnsA) {
auto a = make_box(2, 2, 2);
HalfedgeMesh empty;
auto result = mesh_difference(a, empty);
EXPECT_EQ(result.num_faces(), a.num_faces());
}
// ═══════════════════════════════════════════════════════════
// BooleanOp dispatcher
// ═══════════════════════════════════════════════════════════
TEST(BooleanMeshTest, Dispatch_Union) {
auto a = make_box(2, 2, 2);
auto b = make_box_at(3, 0, 0, 2, 2, 2);
auto result = mesh_boolean(a, b, BooleanOp::Union);
EXPECT_EQ(result.num_faces(), a.num_faces() + b.num_faces());
}
TEST(BooleanMeshTest, Dispatch_Intersection_Disjoint) {
auto a = make_box(1, 1, 1);
auto b = make_box_at(3, 0, 0, 1, 1, 1);
auto result = mesh_boolean(a, b, BooleanOp::Intersection);
EXPECT_EQ(result.num_faces(), 0u);
}
// ═══════════════════════════════════════════════════════════
// Point-in-mesh classification
// ═══════════════════════════════════════════════════════════
TEST(BooleanMeshTest, PointInside_OriginInCenteredBox) {
auto box = make_box(2, 2, 2);
Point3D origin(0, 0, 0);
bool inside = is_point_inside_mesh(origin, box);
EXPECT_TRUE(inside);
}
TEST(BooleanMeshTest, PointInside_FarPointOutsideBox) {
auto box = make_box(2, 2, 2);
Point3D far(10, 0, 0);
bool inside = is_point_inside_mesh(far, box);
EXPECT_FALSE(inside);
}
// ═══════════════════════════════════════════════════════════
// Invert / merge helpers
// ═══════════════════════════════════════════════════════════
TEST(BooleanMeshTest, Invert_PreservesFaceCount) {
auto box = make_box(2, 2, 2);
auto inverted = invert_mesh(box);
EXPECT_EQ(inverted.num_faces(), box.num_faces());
EXPECT_EQ(inverted.num_vertices(), box.num_vertices());
}
TEST(BooleanMeshTest, Merge_TwoBoxes) {
auto a = make_box(1, 1, 1);
auto b = make_box_at(3, 0, 0, 1, 1, 1);
auto merged = merge_meshes(a, b);
EXPECT_EQ(merged.num_faces(), a.num_faces() + b.num_faces());
EXPECT_EQ(merged.num_vertices(), a.num_vertices() + b.num_vertices());
}
// ═══════════════════════════════════════════════════════════
// VDE-893: clip_mesh 判别性测试
// ═══════════════════════════════════════════════════════════
namespace {
/// 逐面质心(用于独立判定面是否位于裁剪体内)
Point3D face_centroid(const HalfedgeMesh& m, int fi) {
auto vis = m.face_vertices(fi);
return (m.vertex(vis[0]) + m.vertex(vis[1]) + m.vertex(vis[2])) * (1.0 / 3.0);
}
} // namespace
TEST(BooleanMeshClipVDE893Test, KeepsOnlyFacesOutsideClipper) {
// A = [-1,1]^3 立方体(12 三角面);B = 以 (1,0,0) 为中心、边长 1 的立方体。
// B 只覆盖 A 的 +x 面(x=1,y/z∈[-0.5,0.5] 部分)→ 应剔除 +x 的 2 个三角面。
auto a = make_box(2, 2, 2);
auto b = make_box_at(1, 0, 0, 1, 1, 1);
HalfedgeMesh out = clip_mesh(a, b, /*invert_b=*/false);
EXPECT_EQ(out.num_faces(), 10u) << "应剔除 +x 面的 2 个三角面";
// 独立复算:保留面数 == A 中质心位于 B 外的面数
int outside_count = 0;
for (int fi = 0; fi < static_cast<int>(a.num_faces()); ++fi)
if (!is_point_inside_mesh(face_centroid(a, fi), b)) ++outside_count;
EXPECT_EQ(static_cast<int>(out.num_faces()), outside_count);
// 结果中不得残留任何质心位于 B 内部的面
for (int fi = 0; fi < static_cast<int>(out.num_faces()); ++fi)
EXPECT_FALSE(is_point_inside_mesh(face_centroid(out, fi), b))
<< "输出面 " << fi << " 仍在裁剪体内";
}
TEST(BooleanMeshClipVDE893Test, InvertKeepsOnlyFacesInsideClipper) {
auto a = make_box(2, 2, 2);
auto b = make_box_at(1, 0, 0, 1, 1, 1);
HalfedgeMesh out = clip_mesh(a, b, /*invert_b=*/true);
EXPECT_EQ(out.num_faces(), 2u) << "取反后应只保留 +x 面进入 B 的 2 个三角面";
for (int fi = 0; fi < static_cast<int>(out.num_faces()); ++fi)
EXPECT_TRUE(is_point_inside_mesh(face_centroid(out, fi), b))
<< "取反输出面 " << fi << " 应位于裁剪体内";
}
TEST(BooleanMeshClipVDE893Test, EmptyClipperEdgeCases) {
auto a = make_box(2, 2, 2);
HalfedgeMesh empty;
// 空裁剪体:正向保留全部 A
HalfedgeMesh keep_all = clip_mesh(a, empty, false);
EXPECT_EQ(keep_all.num_faces(), a.num_faces());
// 空裁剪体 + 取反:交集恒空
HalfedgeMesh keep_none = clip_mesh(a, empty, true);
EXPECT_EQ(keep_none.num_faces(), 0u);
// 空待裁剪网格:恒返回空
EXPECT_EQ(clip_mesh(empty, make_box(1, 1, 1), false).num_faces(), 0u);
}