Pioneer
Loading...
Searching...
No Matches
BVHTree.h
Go to the documentation of this file.
1// Copyright © 2008-2024 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 "../vector3.h"
9#include <vector>
10
11struct BVHNode {
13
14 /* if triIndicesStart == 0 then not leaf,
15 * kids[] valid */
18
20
22 numTris(0),
23 triIndicesStart(nullptr)
24 {
25 kids[0] = nullptr;
26 kids[1] = nullptr;
27 }
28 bool IsLeaf() const
29 {
30 return triIndicesStart != nullptr;
31 }
32};
33
34class BVHTree {
35public:
36 typedef int objPtr_t;
37 BVHTree(const int numObjs, const objPtr_t *objPtrs, const Aabb *objAabbs);
39 {
40 delete[] m_objPtrAlloc;
41 delete[] m_bvhNodes;
42 }
43 BVHNode *GetRoot() { return m_root; }
44 size_t GetNumNodes() const { return m_nodeAllocPos; }
45 double CalculateSAH() const;
46
47private:
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 BVHNode *m_root;
55 objPtr_t *m_objPtrAlloc;
56 size_t m_objPtrAllocPos;
57 size_t m_objPtrAllocMax;
58
59 BVHNode *m_bvhNodes;
60 size_t m_nodeAllocPos;
61 size_t m_nodeAllocMax;
62};
63
64/*
65 * Single-node binary-tree Bounding Volume Hierarchy tree.
66 *
67 * Uses a top-down construction heuristic to produce a "full" binary tree (i.e.
68 * each node either is a leaf or is guaranteed to have two child nodes.)
69 */
71public:
72 struct Node {
74 uint32_t kids[2];
75 uint32_t leafIndex;
76 };
77
79
80 void Clear();
81
82 // Build the BVH structure from the given list of AABBs
83 // Individual nodes will have leaf indices into this array
84 void Build(const AABBd &bounds, AABBd *objAabbs, uint32_t numObjs);
85
86 // Return a pointer to the node at the given index
87 const Node *GetNode(uint32_t index) const { return m_nodes.data() + index; }
88
89 // Compute a list of { objId, leafIndex } intersections and add it to the passed array
90 void ComputeOverlap(uint32_t objId, const AABBd &objAabb, std::vector<std::pair<uint32_t, uint32_t>> &out_isect) const;
91 // Trace a ray through this AABB and add the list of intersected leaves to the passed array
92 void TraceRay(const vector3d &start, const vector3d &inv_dir, double len, std::vector<uint32_t> &out_isect) const;
93
94 size_t GetNumNodes() const { return m_nodes.size(); }
95 uint32_t GetHeight() const { return m_treeHeight; }
96 double CalculateSAH() const;
97
98private:
99 struct SortKey {
100 vector3f center;
101 uint32_t index;
102 };
103
104 void BuildNode(Node *node, SortKey *keys, uint32_t numKeys, AABBd *objAabbs, uint32_t height);
105 uint32_t Partition(SortKey *keys, uint32_t numKeys, const AABBd &aabb, AABBd *objAabbs);
106
107 std::vector<Node> m_nodes;
108 uint32_t m_treeHeight;
109 vector3d m_boundsCenter;
110 double m_inv_scale_factor;
111};
112
113#endif /* _BVHTREE_H */
Definition: BVHTree.h:34
int objPtr_t
Definition: BVHTree.h:36
size_t GetNumNodes() const
Definition: BVHTree.h:44
~BVHTree()
Definition: BVHTree.h:38
double CalculateSAH() const
Definition: BVHTree.cpp:165
BVHNode * GetRoot()
Definition: BVHTree.h:43
Definition: BVHTree.h:70
double CalculateSAH() const
Definition: BVHTree.cpp:378
void Clear()
Definition: BVHTree.cpp:213
size_t GetNumNodes() const
Definition: BVHTree.h:94
const Node * GetNode(uint32_t index) const
Definition: BVHTree.h:87
SingleBVHTree()
Definition: BVHTree.cpp:205
void TraceRay(const vector3d &start, const vector3d &inv_dir, double len, std::vector< uint32_t > &out_isect) const
Definition: BVHTree.cpp:351
uint32_t GetHeight() const
Definition: BVHTree.h:95
void ComputeOverlap(uint32_t objId, const AABBd &objAabb, std::vector< std::pair< uint32_t, uint32_t > > &out_isect) const
Definition: BVHTree.cpp:325
void Build(const AABBd &bounds, AABBd *objAabbs, uint32_t numObjs)
Definition: BVHTree.cpp:223
Definition: Aabb.h:11
Definition: BVHTree.h:11
int * triIndicesStart
Definition: BVHTree.h:17
Aabb aabb
Definition: BVHTree.h:12
BVHNode()
Definition: BVHTree.h:21
BVHNode * kids[2]
Definition: BVHTree.h:19
int numTris
Definition: BVHTree.h:16
bool IsLeaf() const
Definition: BVHTree.h:28
Definition: BVHTree.h:72
AABBd aabb
Definition: BVHTree.h:73
uint32_t kids[2]
Definition: BVHTree.h:74
uint32_t leafIndex
Definition: BVHTree.h:75