BVH.hpp
Go to the documentation of this file.
1 
28 /*
29  * BVH.hpp
30  *
31  * @date 21.01.2018
32  * @author Johan M. von Behren <johan@vonbehren.eu>
33  */
34 
35 #pragma once
36 
37 #include <vector>
38 #include <memory>
39 
41 #include "lvr2/geometry/Normal.hpp"
42 
43 using std::unique_ptr;
44 using std::vector;
45 using std::pair;
46 
47 namespace lvr2
48 {
49 
60 template<typename BaseVecT>
61 class BVHTree
62 {
63 public:
64 
71  BVHTree(const vector<float>& vertices, const vector<uint32_t>& faces);
72 
76  BVHTree(
77  const floatArr vertices, size_t n_vertices,
78  const indexArray faces, size_t n_faces
79  );
80 
84  BVHTree(const MeshBufferPtr mesh);
85 
89  const vector<uint32_t>& getTriIndexList() const;
90 
94  const vector<float>& getLimits() const;
95 
106  const vector<uint32_t>& getIndexesOrTrilists() const;
107 
122  const vector<float>& getTrianglesIntersectionData() const;
123 
124 private:
125 
126  // Internal triangle representation
127  struct Triangle {
128 
129  Triangle();
130 
131  // indices in vertex array
132  // todo: not used?
133  uint32_t idx1;
134  uint32_t idx2;
135  uint32_t idx3;
136 
137  BaseVecT center;
139 
140  // intersection pre-computed cache
141  float d, d1, d2, d3;
143 
144  // todo: not used?
146  };
147 
148  // Internal AABB representation
149  struct AABB {
151 
152  // Triangles corresponding to this AABB
153  vector<size_t> triangles;
154  };
155 
156  // Abstract tree node
157  struct BVHNode {
159  virtual bool isLeaf() = 0;
160  };
161  using BVHNodePtr = unique_ptr<BVHNode>;
162 
163  // Inner tree node
164  struct BVHInner: BVHNode {
165 
166  // Left sub tree
168 
169  // Right sub tree
171  virtual bool isLeaf() { return false; }
172  };
173  using BVHInnerPtr = unique_ptr<BVHInner>;
174 
175  // Leaf tree node
176  struct BVHLeaf: BVHNode {
177 
178  // Triangles corresponding to this leaf node
179  vector<size_t> triangles;
180  virtual bool isLeaf() { return true; }
181  };
182  using BVHLeafPtr = unique_ptr<BVHLeaf>;
183 
184  // working variables for tree construction
186  vector<Triangle> m_triangles;
187 
188  // cache friendly data for the SIMD device
189  vector<uint32_t> m_triIndexList;
190  vector<float> m_limits;
191  vector<uint32_t> m_indexesOrTrilists;
193 
202  BVHNodePtr buildTree(const vector<float>& vertices, const vector<uint32_t>& faces);
203 
213  const floatArr vertices, size_t n_vertices,
214  const indexArray faces, size_t n_faces
215  );
216 
225  BVHNodePtr buildTreeRecursive(vector<AABB>& work, uint32_t depth = 0);
226 
230  void createCFTree();
231 
239  void createCFTreeRecursive(BVHNodePtr currentNode, uint32_t& idxBoxes);
240 
245 };
246 
247 } /* namespace lvr2 */
248 
249 #include "lvr2/geometry/BVH.tcc"
BoundingBox< BaseVecT > bb
Definition: BVH.hpp:145
virtual bool isLeaf()
Definition: BVH.hpp:180
vector< uint32_t > m_indexesOrTrilists
Definition: BVH.hpp:191
Normal< float > e1
Definition: BVH.hpp:142
const vector< float > & getLimits() const
HalfEdgeMesh< Vec > mesh
BVHNodePtr buildTree(const vector< float > &vertices, const vector< uint32_t > &faces)
Builds the tree without it&#39;s cache friendly representation. Utilizes the buildTreeRecursive method...
std::shared_ptr< MeshBuffer > MeshBufferPtr
Definition: MeshBuffer.hpp:217
void createCFTree()
Creates the cache friendly representation of the tree. Needs the tree itself!
const vector< uint32_t > & getTriIndexList() const
unique_ptr< BVHNode > BVHNodePtr
Definition: BVH.hpp:161
BVHNodePtr m_root
Definition: BVH.hpp:185
vector< float > m_trianglesIntersectionData
Definition: BVH.hpp:192
unique_ptr< BVHLeaf > BVHLeafPtr
Definition: BVH.hpp:182
Normal< float > e2
Definition: BVH.hpp:142
unique_ptr< BVHInner > BVHInnerPtr
Definition: BVH.hpp:173
Normal< float > normal
Definition: BVH.hpp:138
Implementation of an Bounding Volume Hierarchy Tree used for ray casting.
Definition: BVH.hpp:61
BVHNodePtr left
Definition: BVH.hpp:167
BVHNodePtr right
Definition: BVH.hpp:170
A dynamic bounding box class.
Definition: BoundingBox.hpp:49
const vector< float > & getTrianglesIntersectionData() const
Returns precalculated values for the triangle intersection tests.
const vector< uint32_t > & getIndexesOrTrilists() const
BVHTree(const vector< float > &vertices, const vector< uint32_t > &faces)
Constructs the tree itself and it&#39;s cache friendly representation.
vector< size_t > triangles
Definition: BVH.hpp:153
BoundingBox< BaseVecT > bb
Definition: BVH.hpp:158
vector< float > m_limits
Definition: BVH.hpp:190
boost::shared_array< unsigned int > indexArray
Definition: DataStruct.hpp:128
void convertTrianglesIntersectionData()
Converts the precalculated triangle intersection data to a SIMD friendly structure.
boost::shared_array< float > floatArr
Definition: DataStruct.hpp:133
Normal< float > e3
Definition: BVH.hpp:142
BoundingBox< BaseVecT > bb
Definition: BVH.hpp:150
virtual bool isLeaf()
Definition: BVH.hpp:171
vector< size_t > triangles
Definition: BVH.hpp:179
vector< Triangle > m_triangles
Definition: BVH.hpp:186
void createCFTreeRecursive(BVHNodePtr currentNode, uint32_t &idxBoxes)
Recursive method for cache friendly tree creation.
BVHNodePtr buildTreeRecursive(vector< AABB > &work, uint32_t depth=0)
Recursive method to build the tree.
vector< uint32_t > m_triIndexList
Definition: BVH.hpp:189


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Mon Feb 28 2022 22:46:06