Files
hm c922fb6996 feat(curves/collision): B2 NURBS Trim/Untrim + B3 连续碰撞检测
- B2: trim_curve/untrim_curve — Boehm 节点插入 + 齐次控制点子段提取
  * 归一化参数语义 (t0/t1 为 [0,1] 比例),自动 clamp 与交换
  * 13 个测试覆盖端点匹配/中点几何/圆几何保持/边界/退化/折线
- B3: conservative_advancement + swept_sphere_triangle
  * GJK 距离查询保守推进,保证不穿透
  * 8 个测试覆盖命中时间/远离/重叠/正交/对撞/扫掠球
2026-08-08 23:57:35 +08:00

112 lines
4.0 KiB
C++
Raw Permalink 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/collision/ccd.h"
#include "vde/collision/gjk.h"
#include <cmath>
using namespace vde::collision;
using namespace vde::core;
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)
);
};
}
// ═══════════════════════════════════════════════════════════
// B3: conservative_advancement — 保守推进 CCD
// ═══════════════════════════════════════════════════════════
TEST(CCDTest, SphereApproach_HitAtExpectedTime) {
// 球 A 从 (0,0,0) 以 10 m/s 沿 +X 运动,球 B 静止在 (5,0,0)
// 中心距 5,碰撞距离 2(半径各 1)→ TOI = (5-2)/10 = 0.3
auto a = make_sphere({0, 0, 0}, 1.0);
auto b = make_sphere({5, 0, 0}, 1.0);
auto hit = conservative_advancement(a, {10, 0, 0}, b, {0, 0, 0});
EXPECT_TRUE(hit.hit);
EXPECT_NEAR(hit.time_of_impact, 0.3, 0.05);
EXPECT_NEAR(hit.initial_distance, 3.0, 0.1);
}
TEST(CCDTest, SphereMoveAway_NoHit) {
// A 向远离 B 的方向运动 → 永不碰撞
auto a = make_sphere({0, 0, 0}, 1.0);
auto b = make_sphere({5, 0, 0}, 1.0);
auto hit = conservative_advancement(a, {-10, 0, 0}, b, {0, 0, 0});
EXPECT_FALSE(hit.hit);
}
TEST(CCDTest, SphereTooFar_NoHitWithinStep) {
// 中心距 10,碰撞距离 2,速度 10,步内最多走 10 → 恰好到达临界
// 用更大的距离确保不碰撞
auto a = make_sphere({0, 0, 0}, 1.0);
auto b = make_sphere({20, 0, 0}, 1.0);
auto hit = conservative_advancement(a, {10, 0, 0}, b, {0, 0, 0});
EXPECT_FALSE(hit.hit);
}
TEST(CCDTest, StaticOverlap_ImmediateHit) {
// 初始即相交 → TOI = 0
auto a = make_sphere({0, 0, 0}, 1.0);
auto b = make_sphere({1.0, 0, 0}, 1.0);
auto hit = conservative_advancement(a, {0, 0, 0}, b, {0, 0, 0});
EXPECT_TRUE(hit.hit);
EXPECT_NEAR(hit.time_of_impact, 0.0, 1e-9);
}
TEST(CCDTest, BoxApproach_OrthogonalHit) {
// 盒 A 从 (-5,0,0) 以 5 m/s 沿 +X,盒 B 静止在 (0,0,0)
// 盒尺寸半边长 1 → 表面距离:(-5+1) 到 (-1) = 3,TOI = 3/5 = 0.6
auto a = make_box({-5, 0, 0}, 1.0, 1.0, 1.0);
auto b = make_box({0, 0, 0}, 1.0, 1.0, 1.0);
auto hit = conservative_advancement(a, {5, 0, 0}, b, {0, 0, 0});
EXPECT_TRUE(hit.hit);
EXPECT_NEAR(hit.time_of_impact, 0.6, 0.05);
}
TEST(CCDTest, HeadOnCollision_Midpoint) {
// 两球相向而行:A 从 (-4,0,0) 以 +10,B 从 (4,0,0) 以 -10
// 中心距 8,碰撞距离 2,相对速度 20 → TOI = 6/20 = 0.3
auto a = make_sphere({-4, 0, 0}, 1.0);
auto b = make_sphere({4, 0, 0}, 1.0);
auto hit = conservative_advancement(a, {10, 0, 0}, b, {-10, 0, 0});
EXPECT_TRUE(hit.hit);
EXPECT_NEAR(hit.time_of_impact, 0.3, 0.05);
}
TEST(CCDTest, SweptSphereTriangle_Intersects) {
// 球从 (0,0,0) 移动到 (0,0,5),三角形在 z=1 平面 XY
bool hit = swept_sphere_triangle(
{0, 0, 0}, {0, 0, 5}, 0.5,
{-1, -1, 1}, {1, -1, 1}, {0, 1, 1});
EXPECT_TRUE(hit);
}
TEST(CCDTest, SweptSphereTriangle_Miss) {
// 球远离三角形
bool hit = swept_sphere_triangle(
{10, 10, 0}, {10, 10, 5}, 0.5,
{-1, -1, 1}, {1, -1, 1}, {0, 1, 1});
EXPECT_FALSE(hit);
}