ViewDesignEngine  3.1.0
高性能 CAD 计算几何引擎
bvh.h
浏览该文件的文档.
1 
14 #pragma once
15 #include <optional>
17 #include "vde/core/aabb.h"
18 #include "vde/core/triangle.h"
19 
20 namespace vde::spatial {
21 using core::Point3D;
22 using core::Vector3D;
23 using core::Ray3Dd;
24 using core::Triangle3D;
25 
29 enum class BVHSplitStrategy {
30  Middle,
31  Equal,
32  SAH
33 };
34 
42  int leaf_size = 4;
43  int max_depth = 64;
44 };
45 
70 class BVH : public SpatialIndex<Triangle3D> {
71 public:
77  explicit BVH(const BVHBuildOptions& opts = {}) : opts_(opts) {}
78 
87  void build(const std::vector<Triangle3D>& tris) override;
88 
97  void insert(const Triangle3D& tri) override;
98 
105  bool remove(const Triangle3D& tri) override;
106 
116  std::vector<Triangle3D> query_range(const AABB3D& range) const override;
117 
127  std::vector<Triangle3D> query_knn(const Point3D& point, size_t k) const override;
128 
138  std::vector<Triangle3D> query_ray(const Ray3Dd& ray) const override;
139 
143  void clear() override;
144 
150  size_t size() const override { return primitives_.size(); }
151 
155  struct HitResult {
156  double t;
159  };
160 
179  std::optional<HitResult> query_ray_nearest(const Ray3Dd& ray) const;
180 
181 private:
188  struct Node {
189  AABB3D bounds;
190  int left = -1;
191  int right = -1;
192  int first_prim = 0;
193  int prim_count = 0;
196  bool leaf() const { return left < 0; }
197  };
198 
199  std::vector<Node> nodes_;
200  std::vector<Triangle3D> primitives_;
201  BVHBuildOptions opts_;
211  void build_recursive(int node, int first, int count, int depth);
212 
222  double sah_cost(int n_left, int n_right, const AABB3D& left, const AABB3D& right) const;
223 };
224 
225 } // namespace vde::spatial
包围盒层次结构 — 三角形网格加速结构
Definition: bvh.h:70
std::vector< Triangle3D > query_range(const AABB3D &range) const override
AABB 范围查询
void build(const std::vector< Triangle3D > &tris) override
清除已有数据并重建 BVH
bool remove(const Triangle3D &tri) override
删除三角形
BVH(const BVHBuildOptions &opts={})
构造 BVH 并指定构建选项
Definition: bvh.h:77
std::vector< Triangle3D > query_knn(const Point3D &point, size_t k) const override
K 近邻查询
void clear() override
清空 BVH 树和所有三角形
std::vector< Triangle3D > query_ray(const Ray3Dd &ray) const override
射线查询:返回所有命中的三角形
std::optional< HitResult > query_ray_nearest(const Ray3Dd &ray) const
射线最近命中查询
size_t size() const override
三角形数量
Definition: bvh.h:150
void insert(const Triangle3D &tri) override
插入单个三角形
空间索引抽象基类
Definition: spatial_index.h:44
Ray3D< double > Ray3Dd
双精度三维射线
Definition: line.h:141
Triangle< double > Triangle3D
双精度三维三角形
Definition: triangle.h:105
AABB< double > AABB3D
双精度三维包围盒
Definition: aabb.h:131
foundation::Vector3D Vector3D
双精度三维向量(重新导出)
Definition: point.h:49
foundation::Point3D Point3D
双精度三维点(重新导出)
Definition: point.h:31
BVHSplitStrategy
BVH 构建分裂策略
Definition: bvh.h:29
空间索引抽象基类
BVH 构建选项
Definition: bvh.h:40
BVHSplitStrategy strategy
Definition: bvh.h:41
射线最近命中查询结果
Definition: bvh.h:155