Files
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

108 lines
3.5 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.
#pragma once
/**
* @file broadphase.h
* @brief 碰撞宽相位 Broadphase — BVH 范围查询 + Sweep-and-Prune + SIMD 批量 AABB (E3d)
*
* 为装配体干涉检查(interference_check)等场景提供批量 AABB 相交候选生成:
*
* - query(ref):BVH 加速的单盒范围查询(O(log n + k))。
* - overlap_pairs():Sweep-and-Prune 求两两相交对;复核阶段复用
* core::aabb_intersect_batch_simd 做 4-wide SIMD 批量测试。
*
* 宽相位只输出「可能相交」的候选(对),精确检测仍由窄相位
* (GJK/EPA)完成,从而将 O(n²) 全对检测降为 O(n log n + k)。
*
* @ingroup spatial
*/
#include "vde/core/aabb.h"
#include <cstdint>
#include <utility>
#include <vector>
namespace vde::spatial {
/**
* @brief 碰撞宽相位索引
*
* 输入一组 AABB(如装配体零件包围盒),构建:
* 1. 扁平 BVH(沿最长轴中值分裂)—— 供 query() 快速范围查询;
* 2. SoA 分量缓存 —— 供 SIMD 批量相交测试。
*
* @code{.cpp}
* Broadphase bp;
* bp.build(boxes);
* auto hits = bp.query(probe_box); // 单盒候选
* auto pairs = bp.overlap_pairs(); // 两两相交对
* @endcode
*
* @ingroup spatial
*/
class Broadphase {
public:
Broadphase() = default;
/// 叶节点最大盒子数
static constexpr int kLeafSize = 8;
/**
* @brief 构建索引
*
* 复制盒子列表,重建 BVH 与 SoA 分量缓存。
* 复杂度 O(n log n)。
*
* @param boxes 待索引的盒子列表(允许空)
*/
void build(const std::vector<core::AABB3D>& boxes);
/// 盒子总数
[[nodiscard]] size_t size() const { return boxes_.size(); }
/// 第 i 个盒子的引用
[[nodiscard]] const core::AABB3D& box(size_t i) const { return boxes_[i]; }
/**
* @brief 与参考盒相交的所有盒索引(BVH 范围查询)
*
* 叶节点内对盒子做 SIMD 批量 AABB 测试。
* 复杂度 O(log n + k),k 为结果数量。
*
* @param ref 查询包围盒
* @return 与 ref 相交的盒子索引(升序,去重)
*/
[[nodiscard]] std::vector<uint32_t> query(const core::AABB3D& ref) const;
/**
* @brief 两两相交对(Sweep-and-Prune + SIMD 批量复核)
*
* 沿 +x 扫描排序的 min_x,维护活动列表排除 x 轴分离的盒对;
* 复核阶段把活动列表整体打包,一次 SIMD 批量测试完成 yz 判别。
* 复杂度 O(n log n + k),k 为候选对数。
*
* @return 相交对 (i, j),满足 i < j
*/
[[nodiscard]] std::vector<std::pair<uint32_t, uint32_t>> overlap_pairs() const;
private:
struct Node {
core::AABB3D box;
int left = -1; ///< 内部节点:左子索引(-1 = 叶)
int right = -1; ///< 内部节点:右子索引
int begin = 0; ///< 叶节点:order_ 区间起点
int end = 0; ///< 叶节点:order_ 区间终点
};
std::vector<core::AABB3D> boxes_;
std::vector<Node> nodes_;
std::vector<uint32_t> order_; ///< 叶节点盒子索引(按构建顺序排列)
// SoA 分量缓存(SIMD 批量测试输入)
std::vector<double> min_x_, min_y_, min_z_, max_x_, max_y_, max_z_;
int build_rec(std::vector<uint32_t>& idx, size_t begin, size_t end);
void query_rec(int node, const core::AABB3D& ref, std::vector<uint32_t>& out) const;
void rebuild_soa();
};
} // namespace vde::spatial