Pioneer
BVHTree.h
Go to the documentation of this file.
1 // Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef _BVHTREE_H
5 #define _BVHTREE_H
6 
7 #include "../Aabb.h"
8 #include "../utils.h"
9 #include "../vector3.h"
10 #include <assert.h>
11 #include <vector>
12 
13 struct BVHNode {
15 
16  /* if triIndicesStart == 0 then not leaf,
17  * kids[] valid */
18  int numTris;
20 
22 
23  BVHNode() :
24  numTris(0),
25  triIndicesStart(nullptr)
26  {
27  kids[0] = nullptr;
28  kids[1] = nullptr;
29  }
30  bool IsLeaf() const
31  {
32  return triIndicesStart != nullptr;
33  }
34 };
35 
36 class BVHTree {
37 public:
38  typedef int objPtr_t;
39  BVHTree(const int numObjs, const objPtr_t *objPtrs, const Aabb *objAabbs);
41  {
42  delete[] m_objPtrAlloc;
43  delete[] m_bvhNodes;
44  }
45  BVHNode *GetRoot() { return m_root; }
46 
47 private:
48  void BuildNode(BVHNode *node,
49  const objPtr_t *objPtrs,
50  const Aabb *objAabbs,
51  std::vector<objPtr_t> &activeObjIdxs);
52  void MakeLeaf(BVHNode *node, const objPtr_t *objPtrs, std::vector<objPtr_t> &objs);
53  BVHNode *AllocNode()
54  {
55  if (m_nodeAllocPos >= m_nodeAllocMax) Error("Out of space in m_bvhNodes.");
56  return &m_bvhNodes[m_nodeAllocPos++];
57  }
58  BVHNode *m_root;
59  objPtr_t *m_objPtrAlloc;
60  size_t m_objPtrAllocPos;
61  size_t m_objPtrAllocMax;
62 
63  BVHNode *m_bvhNodes;
64  size_t m_nodeAllocPos;
65  size_t m_nodeAllocMax;
66 };
67 
68 #endif /* _BVHTREE_H */
Definition: BVHTree.h:36
int objPtr_t
Definition: BVHTree.h:38
~BVHTree()
Definition: BVHTree.h:40
BVHTree(const int numObjs, const objPtr_t *objPtrs, const Aabb *objAabbs)
Definition: BVHTree.cpp:11
BVHNode * GetRoot()
Definition: BVHTree.h:45
Definition: Aabb.h:9
Definition: BVHTree.h:13
int * triIndicesStart
Definition: BVHTree.h:19
Aabb aabb
Definition: BVHTree.h:14
BVHNode()
Definition: BVHTree.h:23
BVHNode * kids[2]
Definition: BVHTree.h:21
int numTris
Definition: BVHTree.h:18
bool IsLeaf() const
Definition: BVHTree.h:30
void Error(const char *message, Args... args)
Definition: utils.h:53