Files
ViewDesignEngine/tests/collision/test_gjk.cpp
T
hm b23271b8d7 fix(mesh,sdf,spatial,collision): 网格算子修复 + 缺失算法补齐(EPA / mesh→SDF / 细分)
网格:
- repair_mesh 早退(remove_duplicates 后直接 return,补洞/定向/去退化从不执行)
  修复为完整流水线:cube 带孔 boundary 4→0、watertight 0→1、体积 0.833→1.0。
- automatic_hole_filling 扇形绕向与退化闭合三角形修正(boundary 4→0、体积恢复)。
- 曲率 cotan 权重错用中心角 → 改为对边角 cotan,并修正 H=|L|/4A
  (单位球 avg|H| 41.08 → 1.0082)。
- alpha_shapes_surface API 内焊接+统一朝向(boundary 28 → 0)。
- QEM 简化增加折叠合法性/体积守卫(旧实现 80% 简化体积漂移 60.4%;仍为部分
  修复,深度保持待拓扑重写,见路线图)。
- 测地热法符号/尺度/质量矩阵归一化重写 + 源点钉零(立方体对角 0.970 → 1.589,
  解析 2.236;仍列路线图)。
- 新增 include/vde/mesh/subdivision.h + loop_subdivide(Loop 细分:cube
  F 12→48、V=26、boundary=0、Euler=2)。

SDF:
- cone SDF 重写(底面中心 -2.0→0、顶点上方 2.828→2.0、侧面/内部符号正确)。
- 新增 include/vde/sdf/sdf_from_mesh.h + sdf_from_mesh(最近点 + 射线奇偶符号):
  单位立方体 -0.5/0/+1.5、球面 ±1.0 内。

空间/碰撞:
- BVH query_range 叶层补三角形 AABB 精确测试(33 → 14 命中与暴力一致);
  query_knn 改精确点-三角形距离(小规模精确,随机 300 三角形用例仍待查→路线图)。
- GJK 距离重写为最近点单纯形(球间隙 1.736→1.000000),gjk_full 正确填充
  point_a/point_b。
- 新增 include/vde/collision/epa.h + epa_penetration(EPA 扩展多面体):
  重叠盒 depth=0.5、球 depth=0.5、分离 valid=false。

测试:test_mesh_repair / test_alpha_shapes / test_smooth / test_mesh_curvature /
test_geodesic / test_mesh_lod / test_sdf_primitives / test_sdf_to_mesh /
test_bvh / test_gjk 以及既有 test_mesh_robustness 全部通过(mesh 探针
PASS 59→71、FAIL 17→6、CRASH 1→0)。
2026-10-25 16:12:44 +08:00

188 lines
5.8 KiB
C++

#include <gtest/gtest.h>
#include "vde/collision/gjk.h"
#include <cmath>
using namespace vde::collision;
static SupportFunc make_sphere(const Point3D& center, double radius) {
return [=](const Vector3D& dir) -> Point3D {
double n = dir.norm();
if (n < 1e-12) return Point3D(center.x() + radius, center.y(), center.z());
Vector3D nd = dir / n;
return Point3D(center.x() + nd.x() * radius,
center.y() + nd.y() * radius,
center.z() + nd.z() * radius);
};
}
static SupportFunc make_box(const Point3D& center, double hx, double hy, double hz) {
return [=](const Vector3D& dir) -> Point3D {
return Point3D(
center.x() + (dir.x() > 0 ? hx : -hx),
center.y() + (dir.y() > 0 ? hy : -hy),
center.z() + (dir.z() > 0 ? hz : -hz)
);
};
}
// ── 原有测试 ────────────────────────────────────────
TEST(GJKTest, SeparatedSpheres) {
auto a = make_sphere({0,0,0}, 1.0);
auto b = make_sphere({5,0,0}, 1.0);
EXPECT_FALSE(gjk_intersect(a, b));
}
TEST(GJKTest, OverlappingSpheres) {
auto a = make_sphere({0,0,0}, 1.0);
auto b = make_sphere({1.5,0,0}, 1.0);
EXPECT_TRUE(gjk_intersect(a, b));
}
// ── gjk_distance 测试 ──────────────────────────────
TEST(GJKTest, DistanceSeparated) {
auto a = make_sphere({0,0,0}, 1.0);
auto b = make_sphere({5,0,0}, 1.0);
double d = gjk_distance(a, b);
// 两个半径=1, 中心距离=5 → 间隙 = 3
EXPECT_GT(d, 0.0);
EXPECT_NEAR(d, 3.0, 0.1);
}
TEST(GJKTest, DistanceOverlapping) {
auto a = make_sphere({0,0,0}, 1.0);
auto b = make_sphere({1.5,0,0}, 1.0);
double d = gjk_distance(a, b);
// 相交 → 距离 = 0
EXPECT_NEAR(d, 0.0, 1e-9);
}
TEST(GJKTest, DistancePoint) {
// 一个半径为0的球 = 点
auto a = make_sphere({0,0,0}, 0.0);
auto b = make_sphere({3,0,0}, 1.0);
double d = gjk_distance(a, b);
EXPECT_GT(d, 0.0);
EXPECT_NEAR(d, 2.0, 0.1);
}
// ── gjk_penetration / EPA 测试 ─────────────────────
TEST(GJKTest, EPAFullResultSeparated) {
auto a = make_sphere({0,0,0}, 1.0);
auto b = make_sphere({5,0,0}, 1.0);
auto result = gjk_full(a, b);
EXPECT_FALSE(result.intersect);
EXPECT_GT(result.distance, 0.0);
EXPECT_NEAR(result.distance, 3.0, 0.1);
}
TEST(GJKTest, EPAFullResultOverlapping) {
auto a = make_sphere({0,0,0}, 1.0);
auto b = make_sphere({1.0,0,0}, 1.0);
auto result = gjk_full(a, b);
EXPECT_TRUE(result.intersect);
EXPECT_NEAR(result.distance, 0.0, 1e-9);
}
TEST(GJKTest, EPABoxes) {
auto a = make_box({0,0,0}, 1, 1, 1);
auto b = make_box({1.5,0,0}, 1, 1, 1);
auto result = gjk_full(a, b);
EXPECT_TRUE(result.intersect);
EXPECT_NEAR(result.distance, 0.0, 1e-9);
}
// ── 边界情况测试 ───────────────────────────────────
TEST(GJKTest, IdenticalShapes) {
auto a = make_sphere({0,0,0}, 1.0);
auto b = make_sphere({0,0,0}, 1.0);
EXPECT_TRUE(gjk_intersect(a, b));
double d = gjk_distance(a, b);
EXPECT_NEAR(d, 0.0, 1e-9);
}
TEST(GJKTest, Touching) {
// 刚好接触: 中心距离 = 2r
auto a = make_sphere({0,0,0}, 1.0);
auto b = make_sphere({2.0,0,0}, 1.0);
bool intersect = gjk_intersect(a, b);
double d = gjk_distance(a, b);
// 刚好接触 (边界情况)
EXPECT_NEAR(d, 0.0, 1e-9);
EXPECT_TRUE(intersect);
}
TEST(GJKTest, VeryFarApart) {
auto a = make_sphere({0,0,0}, 0.1);
auto b = make_sphere({100,0,0}, 0.1);
double d = gjk_distance(a, b);
EXPECT_NEAR(d, 99.8, 0.2);
EXPECT_FALSE(gjk_intersect(a, b));
}
TEST(GJKTest, ZeroRadius) {
auto a = make_sphere({0,0,0}, 0.0);
auto b = make_sphere({1,0,0}, 0.0);
double d = gjk_distance(a, b);
EXPECT_NEAR(d, 1.0, 0.01);
}
// ═══════════════════════════════════════════════════════════
// Phase-2: exact GJK distance/nearest points + EPA penetration depth
// ═══════════════════════════════════════════════════════════
#include "vde/collision/epa.h"
TEST(Phase2GJK, DistanceSpheresExact) {
auto a = make_sphere({0,0,0}, 1.0);
auto b = make_sphere({3,0,0}, 1.0);
EXPECT_NEAR(gjk_distance(a, b), 1.0, 1e-6);
}
TEST(Phase2GJK, FullPopulatesNearestPoints) {
auto a = make_sphere({0,0,0}, 1.0);
auto b = make_sphere({3,0,0}, 1.0);
auto r = gjk_full(a, b);
EXPECT_FALSE(r.intersect);
EXPECT_NEAR(r.distance, 1.0, 1e-6);
EXPECT_NEAR((r.point_a - Point3D(1, 0, 0)).norm(), 0.0, 1e-6);
EXPECT_NEAR((r.point_b - Point3D(2, 0, 0)).norm(), 0.0, 1e-6);
}
TEST(Phase2GJK, EPABoxPenetrationDepth) {
auto a = make_box({0,0,0}, 1, 1, 1);
auto b = make_box({1.5,0,0}, 1, 1, 1); // overlap along x = 0.5
auto r = epa_penetration(a, b);
ASSERT_TRUE(r.valid);
EXPECT_NEAR(r.depth, 0.5, 1e-3);
EXPECT_NEAR(r.normal.norm(), 1.0, 1e-6);
EXPECT_NEAR((r.point_a - r.point_b).dot(r.normal), r.depth, 1e-3);
}
TEST(Phase2GJK, EPASpherePenetrationDepth) {
auto a = make_sphere({0,0,0}, 1.0);
auto b = make_sphere({1.5,0,0}, 1.0); // depth = 2 - 1.5 = 0.5
auto r = epa_penetration(a, b);
ASSERT_TRUE(r.valid);
EXPECT_NEAR(r.depth, 0.5, 0.02);
}
TEST(Phase2GJK, EPASeparatedIsInvalid) {
auto a = make_sphere({0,0,0}, 1.0);
auto b = make_sphere({5,0,0}, 1.0);
auto r = epa_penetration(a, b);
EXPECT_FALSE(r.valid);
EXPECT_NEAR(r.depth, 0.0, 1e-12);
}