- D2: tests/data/models 按格式分目录模型库 + test_model_corpus 遍历测试(空库自动跳过) - D3: tests/fuzz/fuzz_ccd(保守推进/扫掠球/GJK 一致性)+ Gitea CI Fuzz Smoketest - D4/D6/D7: GitHub CI 新增 valgrind / sanitizer(ASan+UBSan) / coverage 三 job - D8: tools/perf_regression.sh 性能回归脚本 + CI perf-regression job(退化>15% 报警) - D10: include/vde/foundation/vde_log.h + src/foundation/vde_log.cpp 自研结构化日志 - D11: version.h 新增 VDE_API_VERSION_* / VDE_API_SINCE / VDE_DEPRECATED 宏 - 路线图 D 阶段状态同步
153 lines
5.4 KiB
C++
153 lines
5.4 KiB
C++
/// @file fuzz_ccd.cpp
|
||
/// @brief D3: 连续碰撞检测(CCD)随机参数模糊测试 + 持续运行模式
|
||
///
|
||
/// 测试策略:
|
||
/// 1. 随机球体运动对做保守推进 CCD
|
||
/// 2. 随机扫掠球 vs 随机三角形
|
||
/// 3. 随机相对速度与方向
|
||
/// 4. 验证结果有限、时间单调、不产生 NaN/Inf
|
||
/// 5. 持续运行模式(VDE_FUZZ_FOREVER=1)
|
||
|
||
#include <gtest/gtest.h>
|
||
#include "vde/collision/ccd.h"
|
||
#include "vde/collision/gjk.h"
|
||
#include "vde/core/point.h"
|
||
|
||
#include <cmath>
|
||
#include <cstdlib>
|
||
#include <random>
|
||
#include <string>
|
||
|
||
using namespace vde::collision;
|
||
using namespace vde::core;
|
||
|
||
static std::mt19937 s_rng(2026);
|
||
|
||
namespace {
|
||
|
||
double rand_range(double lo, double hi) {
|
||
return std::uniform_real_distribution<double>(lo, hi)(s_rng);
|
||
}
|
||
|
||
Point3D rand_point(double lo, double hi) {
|
||
return Point3D(rand_range(lo, hi), rand_range(lo, hi), rand_range(lo, hi));
|
||
}
|
||
|
||
Point3D rand_dir() {
|
||
Point3D d = rand_point(-1.0, 1.0);
|
||
if (d.norm() < 1e-9) d = Point3D(1, 0, 0);
|
||
return d.normalized();
|
||
}
|
||
|
||
/// 球体支撑函数(保守推进需要凸体支撑函数)
|
||
SupportFunc make_sphere(const Point3D& center, double radius) {
|
||
return [center, radius](const Vector3D& d) -> Point3D {
|
||
Vector3D dir = d;
|
||
if (dir.norm() < 1e-12) dir = Vector3D(1, 0, 0);
|
||
return center + radius * dir.normalized();
|
||
};
|
||
}
|
||
|
||
} // anonymous
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 随机 CCD 测试
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(FuzzCcd, RandomSphereMotion) {
|
||
for (int i = 0; i < 200; ++i) {
|
||
const double ra = rand_range(0.1, 3.0);
|
||
const double rb = rand_range(0.1, 3.0);
|
||
Point3D pa = rand_point(-10, 10);
|
||
Point3D pb = rand_point(-10, 10);
|
||
Vector3D va = rand_dir() * rand_range(0.1, 20.0);
|
||
Vector3D vb = rand_dir() * rand_range(0.1, 20.0);
|
||
|
||
EXPECT_NO_THROW({
|
||
auto hit = conservative_advancement(
|
||
make_sphere(pa, ra), va,
|
||
make_sphere(pb, rb), vb, 1.0, 1e-6);
|
||
if (hit.hit) {
|
||
EXPECT_TRUE(std::isfinite(hit.time_of_impact));
|
||
EXPECT_GE(hit.time_of_impact, 0.0);
|
||
EXPECT_LE(hit.time_of_impact, 1.0 + 1e-9);
|
||
EXPECT_TRUE(std::isfinite(hit.initial_distance));
|
||
EXPECT_GE(hit.initial_distance, 0.0);
|
||
}
|
||
}) << "sphere CCD crashed at iteration " << i;
|
||
}
|
||
}
|
||
|
||
TEST(FuzzCcd, RandomSweptSphereTriangle) {
|
||
for (int i = 0; i < 200; ++i) {
|
||
Point3D from = rand_point(-10, 10);
|
||
Point3D to = from + rand_dir() * rand_range(0.1, 20.0);
|
||
double radius = rand_range(0.05, 2.0);
|
||
Point3D v0 = rand_point(-5, 5);
|
||
Point3D v1 = rand_point(-5, 5);
|
||
Point3D v2 = rand_point(-5, 5);
|
||
|
||
EXPECT_NO_THROW({
|
||
// 返回值是 bool(扫掠是否命中)
|
||
bool hit = swept_sphere_triangle(from, to, radius, v0, v1, v2);
|
||
(void)hit;
|
||
}) << "swept sphere vs triangle crashed at iteration " << i;
|
||
}
|
||
}
|
||
|
||
TEST(FuzzCcd, RandomGjkConsistency) {
|
||
// GJK 距离查询与保守推进初值一致性
|
||
for (int i = 0; i < 100; ++i) {
|
||
Point3D pa = rand_point(-5, 5);
|
||
Point3D pb = rand_point(-5, 5);
|
||
double ra = rand_range(0.5, 2.0);
|
||
double rb = rand_range(0.5, 2.0);
|
||
|
||
EXPECT_NO_THROW({
|
||
auto hit = conservative_advancement(
|
||
make_sphere(pa, ra), Vector3D::Zero(),
|
||
make_sphere(pb, rb), Vector3D::Zero(), 1.0, 1e-6);
|
||
// 静止重叠应立即命中
|
||
double center_dist = (pa - pb).norm();
|
||
bool overlap = center_dist < ra + rb + 1e-9;
|
||
if (overlap) EXPECT_TRUE(hit.hit);
|
||
});
|
||
}
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 持续运行模式
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(FuzzCcd, ContinuousFuzz) {
|
||
const char* forever = std::getenv("VDE_FUZZ_FOREVER");
|
||
if (!forever || std::string(forever) != "1") {
|
||
GTEST_SKIP() << "设置 VDE_FUZZ_FOREVER=1 启用持续模糊测试";
|
||
}
|
||
|
||
int iteration = 0;
|
||
while (true) {
|
||
++iteration;
|
||
const double ra = rand_range(0.1, 3.0);
|
||
const double rb = rand_range(0.1, 3.0);
|
||
Point3D pa = rand_point(-10, 10);
|
||
Point3D pb = rand_point(-10, 10);
|
||
Vector3D va = rand_dir() * rand_range(0.1, 20.0);
|
||
Vector3D vb = rand_dir() * rand_range(0.1, 20.0);
|
||
|
||
auto hit = conservative_advancement(
|
||
make_sphere(pa, ra), va,
|
||
make_sphere(pb, rb), vb, 1.0, 1e-6);
|
||
if (hit.hit) {
|
||
ASSERT_TRUE(std::isfinite(hit.time_of_impact))
|
||
<< "iteration=" << iteration;
|
||
}
|
||
|
||
if (iteration % 10000 == 0) {
|
||
std::cout << "[FuzzCcd] iteration=" << iteration
|
||
<< " last_hit=" << (hit.hit ? "yes" : "no")
|
||
<< " toi=" << hit.time_of_impact << std::endl;
|
||
}
|
||
}
|
||
}
|