Files
ViewDesignEngine/tests/spatial/test_broadphase.cpp
T
hm 5f8d4578a6 feat(spatial): E3d 碰撞宽相位 Broadphase 接入 interference_check
- 新增 Broadphase:扁平 BVH(沿最长轴中值分裂)+ SoA 分量缓存
- query():BVH 剪枝 + 叶内 SIMD 批量 AABB 测试(O(log n + k))
- overlap_pairs():Sweep-and-Prune 沿 +x 扫描 + SIMD 批量复核,输出按索引规范化(i < j)
- check_interference 接入宽相位:仅候选对进入窄相位(GJK/EPA),total_pairs_checked 语义保持全对数
- 测试:test_broadphase 12 例(query/overlap_pairs 与暴力扫描一致性),test_interference_check 装配级 9 例全绿;LargeAssembly 4950 对 25ms
2026-08-09 17:39:53 +08:00

251 lines
8.9 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/spatial/broadphase.h"
#include <algorithm>
#include <cstdint>
#include <set>
#include <vector>
using namespace vde::spatial;
using namespace vde::core;
// ═══════════════════════════════════════════════════════════
// Broadphase 测试: build / query / overlap_pairs
// ═══════════════════════════════════════════════════════════
namespace {
/// 确定性伪随机数(LCG)— 测试可重复
uint32_t lcg_state = 0x12345678u;
double rand01() {
lcg_state = lcg_state * 1664525u + 1013904223u;
return static_cast<double>(lcg_state >> 8) / 16777216.0;
}
AABB3D make_box(double cx, double cy, double cz, double half) {
return AABB3D(Point3D(cx - half, cy - half, cz - half),
Point3D(cx + half, cy + half, cz + half));
}
/// 生成 n 个盒子:中心在 [-span, span]³ 内均匀随机,边长 ≤ 2*max_half
std::vector<AABB3D> make_random_boxes(size_t n, double span, double max_half) {
std::vector<AABB3D> boxes;
boxes.reserve(n);
for (size_t i = 0; i < n; ++i) {
const double cx = (rand01() * 2.0 - 1.0) * span;
const double cy = (rand01() * 2.0 - 1.0) * span;
const double cz = (rand01() * 2.0 - 1.0) * span;
const double half = rand01() * max_half;
boxes.push_back(make_box(cx, cy, cz, half));
}
return boxes;
}
/// 暴力扫描:与 ref 相交的所有盒子索引
std::vector<uint32_t> brute_force_query(const std::vector<AABB3D>& boxes,
const AABB3D& ref) {
std::vector<uint32_t> out;
for (size_t i = 0; i < boxes.size(); ++i)
if (boxes[i].intersects(ref)) out.push_back(static_cast<uint32_t>(i));
return out;
}
/// 暴力扫描:两两相交对 (i, j), i < j
std::vector<std::pair<uint32_t, uint32_t>> brute_force_pairs(
const std::vector<AABB3D>& boxes) {
std::vector<std::pair<uint32_t, uint32_t>> out;
for (size_t i = 0; i < boxes.size(); ++i)
for (size_t j = i + 1; j < boxes.size(); ++j)
if (boxes[i].intersects(boxes[j])) out.emplace_back(
static_cast<uint32_t>(i), static_cast<uint32_t>(j));
return out;
}
} // namespace
// ── build / 空输入 ────────────────────────────────
TEST(BroadphaseTest, BuildEmpty) {
Broadphase bp;
bp.build({});
EXPECT_EQ(bp.size(), 0u);
EXPECT_TRUE(bp.query(make_box(0, 0, 0, 10)).empty());
EXPECT_TRUE(bp.overlap_pairs().empty());
}
TEST(BroadphaseTest, BuildSingle) {
Broadphase bp;
bp.build({make_box(0, 0, 0, 1)});
EXPECT_EQ(bp.size(), 1u);
// 自身与自身相交
EXPECT_EQ(bp.query(make_box(0, 0, 0, 1)), std::vector<uint32_t>({0}));
// 远离的探针不相交
EXPECT_TRUE(bp.query(make_box(100, 100, 100, 1)).empty());
EXPECT_TRUE(bp.overlap_pairs().empty());
}
// ── query — 单盒范围查询 ──────────────────────────
TEST(BroadphaseTest, Query_HitsOverlappingBoxes) {
Broadphase bp;
bp.build({
make_box(0, 0, 0, 1), // 0: 原点
make_box(5, 0, 0, 1), // 1: +x
make_box(0, 5, 0, 1), // 2: +y
make_box(0, 0, 5, 1), // 3: +z
});
// 探针覆盖原点及其 +x 相邻盒 → 命中 0 与 1
auto hits = bp.query(make_box(2.5, 0, 0, 2));
ASSERT_EQ(hits.size(), 2u);
EXPECT_EQ(hits[0], 0u);
EXPECT_EQ(hits[1], 1u);
}
TEST(BroadphaseTest, Query_IncludesSelf) {
Broadphase bp;
bp.build({
make_box(0, 0, 0, 2),
make_box(10, 10, 10, 2),
});
// 与盒 0 完全相同的探针 → 至少命中自身
auto hits = bp.query(make_box(0, 0, 0, 2));
EXPECT_EQ(hits, std::vector<uint32_t>({0}));
}
TEST(BroadphaseTest, Query_MatchScalarBruteForce) {
const size_t n = 300;
const double span = 20.0, max_half = 1.5;
auto boxes = make_random_boxes(n, span, max_half);
Broadphase bp;
bp.build(boxes);
// 用 60 个随机探针与暴力扫描对比
for (int t = 0; t < 60; ++t) {
const double half = rand01() * 2.5;
const AABB3D ref = make_box((rand01() * 2.0 - 1.0) * span,
(rand01() * 2.0 - 1.0) * span,
(rand01() * 2.0 - 1.0) * span, half);
auto hits = bp.query(ref);
auto expected = brute_force_query(boxes, ref);
std::sort(expected.begin(), expected.end());
EXPECT_EQ(hits, expected) << "probe=" << t;
}
}
TEST(BroadphaseTest, Query_AllBoxesInLeaf) {
// 8 个盒子挤在叶节点容量边界内(kLeafSize = 8)
std::vector<AABB3D> boxes;
for (int k = 0; k < 8; ++k)
boxes.push_back(make_box(0.25 * k, 0, 0, 0.2));
Broadphase bp;
bp.build(boxes);
auto hits = bp.query(make_box(0.7, 0, 0, 0.3));
// 探针覆盖 x ∈ [0.4, 1.0] → 与盒 1..4 相交(中心 0.25..1.0,半宽 0.2)
std::vector<uint32_t> expected;
for (size_t i = 0; i < boxes.size(); ++i)
if (boxes[i].intersects(make_box(0.7, 0, 0, 0.3)))
expected.push_back(static_cast<uint32_t>(i));
EXPECT_EQ(hits, expected);
}
// ── overlap_pairs — 两两相交对 ─────────────────────
TEST(BroadphaseTest, OverlapPairs_AllBoxesOverlap) {
// 全部叠在一起 → 所有对都相交
std::vector<AABB3D> boxes;
for (int k = 0; k < 20; ++k)
boxes.push_back(make_box(0, 0, 0, 1));
Broadphase bp;
bp.build(boxes);
auto pairs = bp.overlap_pairs();
ASSERT_EQ(pairs.size(), 20u * 19u / 2u);
// 去重校验:每对 (i<j) 唯一
std::set<std::pair<uint32_t, uint32_t>> uniq(pairs.begin(), pairs.end());
EXPECT_EQ(uniq.size(), pairs.size());
}
TEST(BroadphaseTest, OverlapPairs_DisjointBoxes) {
// 严格分离 → 无相交对
Broadphase bp;
bp.build({
make_box(-10, 0, 0, 1),
make_box(0, 0, 0, 1),
make_box(10, 0, 0, 1),
});
EXPECT_TRUE(bp.overlap_pairs().empty());
}
TEST(BroadphaseTest, OverlapPairs_TouchingBoxes) {
// 贴面(x 方向相接)→ intersects 含边界 → 算相交
Broadphase bp;
bp.build({
make_box(0, 0, 0, 1), // [-1,1]
make_box(2, 0, 0, 1), // [1,3] 与上一盒在 x=1 相接
});
auto pairs = bp.overlap_pairs();
ASSERT_EQ(pairs.size(), 1u);
const std::pair<uint32_t, uint32_t> expected(0, 1);
EXPECT_EQ(pairs[0], expected);
}
TEST(BroadphaseTest, OverlapPairs_DenseGrid) {
// 3×3×3 密铺网格(间隙 0.1)→ 相邻盒相交、远盒不相交
std::vector<AABB3D> boxes;
const double half = 0.45, gap = 1.0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
for (int k = 0; k < 3; ++k)
boxes.push_back(make_box(gap * i, gap * j, gap * k, half));
Broadphase bp;
bp.build(boxes);
auto expected = brute_force_pairs(boxes);
auto pairs = bp.overlap_pairs();
EXPECT_EQ(pairs.size(), expected.size());
for (const auto& e : expected)
EXPECT_NE(std::find(pairs.begin(), pairs.end(), e), pairs.end())
<< "missing (" << e.first << "," << e.second << ")";
}
TEST(BroadphaseTest, OverlapPairs_MatchesBruteForce) {
const size_t n = 500;
const double span = 30.0, max_half = 3.0;
auto boxes = make_random_boxes(n, span, max_half);
Broadphase bp;
bp.build(boxes);
auto expected = brute_force_pairs(boxes);
auto pairs = bp.overlap_pairs();
EXPECT_EQ(pairs.size(), expected.size());
for (const auto& e : expected)
EXPECT_NE(std::find(pairs.begin(), pairs.end(), e), pairs.end())
<< "missing (" << e.first << "," << e.second << ")";
}
// ── query / overlap_pairs 一致性 ───────────────────
TEST(BroadphaseTest, QueryVsOverlapPairs_Consistent) {
const size_t n = 200;
const double span = 25.0, max_half = 2.0;
auto boxes = make_random_boxes(n, span, max_half);
Broadphase bp;
bp.build(boxes);
auto pairs = bp.overlap_pairs();
// (i,j) 相交 ⟺ i 的 query 命中 j(双向)
for (const auto& p : pairs) {
auto qi = bp.query(boxes[p.first]);
auto qj = bp.query(boxes[p.second]);
EXPECT_NE(std::find(qi.begin(), qi.end(), p.second), qi.end());
EXPECT_NE(std::find(qj.begin(), qj.end(), p.first), qj.end());
}
// 反向:query 命中的对也应出现在 overlap_pairs 中
for (uint32_t i = 0; i < n; ++i) {
auto qi = bp.query(boxes[i]);
for (uint32_t j : qi) {
if (j <= i) continue;
EXPECT_NE(std::find(pairs.begin(), pairs.end(),
std::make_pair(i, j)), pairs.end());
}
}
}