Page:
api reference spatial
Pages
01 quickstart
02 sdf modeling
03 brep modeling
27 竞品分析
30 严谨差距分析
API REFERENCE
API使用手册
Home
README
VERSIONING
api overview
api reference brep
api reference capi
api reference core
api reference curves
api reference foundation
api reference mesh
api reference spatial
api usage
architecture
benchmark report
building
changelog
contributing
format support
module manual
testing
严谨差距分析
几何引擎全景调研
向ACIS学习
向Parasolid学习
布尔算法调研
快速入门
改进路线图
更新日志
构建指南
架构概览
模块说明书
测试指南
版本管理规范
竞品分析
贡献指南
项目概述
Clone
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.
Spatial 模块 API 参考
空间索引模块 — BVH 包围盒层次结构 + B-Rep 级空间查询
模块概述
spatial 模块提供高效的空间索引数据结构,核心是基于 SAH(Surface Area Heuristic)的 BVH(包围盒层次结构),支持快速的射线求交、K 近邻查询和范围查询。同时提供 B-Rep 级别的空间查询工具,包括多击穿射线投射、面邻接查询和均匀网格空间索引。
头文件列表
| 头文件 | 说明 |
|---|---|
bvh.h |
包围盒层次结构(SAH 构建、射线/范围/KNN 查询、B-Rep 查询) |
spatial_index.h |
空间索引抽象基类 |
octree.h |
八叉树 |
kd_tree.h |
K-D 树 |
r_tree.h |
R 树 |
incremental_bvh.h |
增量 BVH |
详细 API
bvh.h — 包围盒层次结构
namespace vde::spatial {
// === 分裂策略 ===
enum class BVHSplitStrategy {
Middle, // 按最长轴中点分裂 — 构建最快
Equal, // 按数量均等分裂 — 适合均匀分布
SAH // Surface Area Heuristic — 查询最快(默认)
};
// === 构建选项 ===
struct BVHBuildOptions {
BVHSplitStrategy strategy = BVHSplitStrategy::SAH;
int leaf_size = 4; // 叶节点最大图元数
int max_depth = 64; // 最大深度
};
// === 射线命中结果 ===
struct HitResult {
double t; // 射线参数
Triangle3D tri; // 命中三角形
Point3D point; // 命中点 = origin + t * dir
int tri_index; // 三角形索引
};
// === BVH 主类 ===
class BVH : public SpatialIndex<Triangle3D> {
public:
explicit BVH(const BVHBuildOptions& opts = {});
void build(const std::vector<Triangle3D>& tris); // 构建(O(n log n))
void insert(const Triangle3D& tri); // 插入(全量重建)
bool remove(const Triangle3D& tri); // 删除
void clear(); // 清空
size_t size() const; // 图元数量
// 查询(基类接口)
std::vector<Triangle3D> query_range(const AABB3D& range) const; // AABB 范围查询
std::vector<Triangle3D> query_knn(const Point3D& point, size_t k) const; // K 近邻
std::vector<Triangle3D> query_ray(const Ray3Dd& ray) const; // 射线-全部命中
// 新增索引查询(VDE 新增)
int index_of(const Triangle3D& tri) const; // 三角形→索引
std::vector<std::pair<int, Triangle3D>> query_knn_indices( // KNN + 索引
const Point3D& point, size_t k) const;
std::vector<std::pair<int, Triangle3D>> query_range_indices( // 范围 + 索引
const AABB3D& range) const;
std::vector<std::pair<int, Triangle3D>> query_ray_indices( // 射线 + 索引
const Ray3Dd& ray) const;
// 射线最近命中(O(log n))
std::optional<HitResult> query_ray_nearest(const Ray3Dd& ray) const;
};
} // namespace vde::spatial
B-Rep 级空间查询
namespace vde::spatial {
// === 多击穿射线投射(VDE-177) ===
struct BrepHitResult {
double t; // 射线参数
int face_index; // 命中的面索引
Point3D point; // 命中点
};
[[nodiscard]] std::vector<BrepHitResult> ray_cast_multi(
const brep::BrepModel& body,
const Ray3Dd& ray); // 按距离排序的所有面命中
// === 面邻接查询(VDE-178) ===
[[nodiscard]] std::vector<int> face_adjacency(
const brep::BrepModel& body,
int face_id); // 相邻面 ID 列表
// === 空间索引(VDE-186) ===
struct SpatialLookup {
int face_id; // 面索引
int edge_id; // 边索引
AABB3D cell_bounds; // 网格单元包围盒
};
[[nodiscard]] std::vector<SpatialLookup> spatial_index(
const brep::BrepModel& body); // 均匀网格空间索引
} // namespace vde::spatial
spatial_index.h — 抽象基类
namespace vde::spatial {
template <typename Primitive>
class SpatialIndex {
public:
virtual ~SpatialIndex() = default;
virtual void build(const std::vector<Primitive>& primitives) = 0;
virtual void insert(const Primitive& p) = 0;
virtual bool remove(const Primitive& p) = 0;
virtual void clear() = 0;
virtual size_t size() const = 0;
virtual std::vector<Primitive> query_range(const AABB3D& range) const = 0;
virtual std::vector<Primitive> query_knn(const Point3D& point, size_t k) const = 0;
virtual std::vector<Primitive> query_ray(const Ray3Dd& ray) const = 0;
};
} // namespace vde::spatial
代码示例
示例 1: BVH 射线最近命中
#include <vde/spatial/bvh.h>
#include <vde/core/triangle.h>
#include <vde/core/line.h>
using namespace vde;
std::vector<core::Triangle3D> tris = { /* ... 三角形列表 ... */ };
// 构建 BVH(SAH 策略)
spatial::BVH bvh(spatial::BVHBuildOptions{
.strategy = spatial::BVHSplitStrategy::SAH,
.leaf_size = 8
});
bvh.build(tris);
// 射线查询
core::Ray3Dd ray(Point3D(0, 0, 10), Vector3D(0, 0, -1));
if (auto hit = bvh.query_ray_nearest(ray)) {
std::cout << "命中 t=" << hit->t
<< ", 三角形索引=" << hit->tri_index
<< ", 点=" << hit->point.transpose() << "\n";
}
// KNN 查询(含索引)
auto neighbors = bvh.query_knn_indices(Point3D(1, 2, 3), 5);
for (auto& [idx, tri] : neighbors) {
std::cout << "三角形 #" << idx << ": " << tri.centroid().transpose() << "\n";
}
示例 2: B-Rep 级射线投射
#include <vde/spatial/bvh.h>
#include <vde/brep/modeling.h>
using namespace vde;
// 创建 B-Rep 模型
auto body = brep::make_sphere(10.0, 32, 32);
// 射线从上方射入
core::Ray3Dd ray(Point3D(0, 0, 20), Vector3D(0, 0, -1));
// 多击穿射线投射
auto hits = spatial::ray_cast_multi(body, ray);
std::cout << "命中面数: " << hits.size() << "\n";
for (auto& hit : hits) {
std::cout << " 面" << hit.face_index
<< " 在 t=" << hit.t << "\n";
// 查询相邻面
auto neighbors = spatial::face_adjacency(body, hit.face_index);
std::cout << " 相邻面: " << neighbors.size() << "\n";
}