Files
ViewDesignEngine/tests/collision/test_gjk.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

140 lines
4.0 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);
}