1 // Created on: 2013-12-20
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _BVH_TreeBase_Header
17 #define _BVH_TreeBase_Header
19 #include <BVH_Box.hxx>
21 template<class T, int N> class BVH_Builder;
23 //! A non-template class for using as base for BVH_TreeBase
24 //! (just to have a named base class).
25 class BVH_TreeBaseTransient : public Standard_Transient
27 DEFINE_STANDARD_RTTIEXT(BVH_TreeBaseTransient, Standard_Transient)
29 BVH_TreeBaseTransient() {}
32 //! Stores parameters of bounding volume hierarchy (BVH).
33 //! Bounding volume hierarchy (BVH) organizes geometric objects in
34 //! the tree based on spatial relationships. Each node in the tree
35 //! contains an axis-aligned bounding box of all the objects below
36 //! it. Bounding volume hierarchies are used in many algorithms to
37 //! support efficient operations on the sets of geometric objects,
38 //! such as collision detection, ray-tracing, searching of nearest
39 //! objects, and view frustum culling.
40 template<class T, int N>
41 class BVH_TreeBase : public BVH_TreeBaseTransient
43 friend class BVH_Builder<T, N>;
45 public: //! @name custom data types
47 typedef typename BVH_Box<T, N>::BVH_VecNt BVH_VecNt;
49 public: //! @name general methods
51 //! Creates new empty BVH tree.
52 BVH_TreeBase() : myDepth (0) { }
54 //! Releases resources of BVH tree.
55 virtual ~BVH_TreeBase() {}
57 //! Returns depth (height) of BVH tree.
63 //! Returns total number of BVH tree nodes.
66 return BVH::Array<int, 4>::Size (myNodeInfoBuffer);
69 public: //! @name methods for accessing individual nodes
71 //! Returns minimum point of the given node.
72 BVH_VecNt& MinPoint (const int theNodeIndex)
74 return BVH::Array<T, N>::ChangeValue (myMinPointBuffer, theNodeIndex);
77 //! Returns maximum point of the given node.
78 BVH_VecNt& MaxPoint (const int theNodeIndex)
80 return BVH::Array<T, N>::ChangeValue (myMaxPointBuffer, theNodeIndex);
83 //! Returns minimum point of the given node.
84 const BVH_VecNt& MinPoint (const int theNodeIndex) const
86 return BVH::Array<T, N>::Value (myMinPointBuffer, theNodeIndex);
89 //! Returns maximum point of the given node.
90 const BVH_VecNt& MaxPoint (const int theNodeIndex) const
92 return BVH::Array<T, N>::Value (myMaxPointBuffer, theNodeIndex);
95 //! Returns index of first primitive of the given leaf node.
96 int& BegPrimitive (const int theNodeIndex)
98 return BVH::Array<int, 4>::ChangeValue (myNodeInfoBuffer, theNodeIndex).y();
101 //! Returns index of last primitive of the given leaf node.
102 int& EndPrimitive (const int theNodeIndex)
104 return BVH::Array<int, 4>::ChangeValue (myNodeInfoBuffer, theNodeIndex).z();
107 //! Returns index of first primitive of the given leaf node.
108 int BegPrimitive (const int theNodeIndex) const
110 return BVH::Array<int, 4>::Value (myNodeInfoBuffer, theNodeIndex).y();
113 //! Returns index of last primitive of the given leaf node.
114 int EndPrimitive (const int theNodeIndex) const
116 return BVH::Array<int, 4>::Value (myNodeInfoBuffer, theNodeIndex).z();
119 //! Returns number of primitives in the given leaf node.
120 int NbPrimitives (const int theNodeIndex) const
122 return EndPrimitive (theNodeIndex) - BegPrimitive (theNodeIndex) + 1;
125 //! Returns level (depth) of the given node.
126 int& Level (const int theNodeIndex)
128 return BVH::Array<int, 4>::ChangeValue (myNodeInfoBuffer, theNodeIndex).w();
131 //! Returns level (depth) of the given node.
132 int Level (const int theNodeIndex) const
134 return BVH::Array<int, 4>::Value (myNodeInfoBuffer, theNodeIndex).w();
137 //! Checks whether the given node is outer.
138 bool IsOuter (const int theNodeIndex) const
140 return BVH::Array<int, 4>::Value (myNodeInfoBuffer, theNodeIndex).x() != 0;
143 public: //! @name methods for accessing serialized tree data
145 //! Returns array of node data records.
146 BVH_Array4i& NodeInfoBuffer()
148 return myNodeInfoBuffer;
151 //! Returns array of node data records.
152 const BVH_Array4i& NodeInfoBuffer() const
154 return myNodeInfoBuffer;
157 //! Returns array of node minimum points.
158 typename BVH::ArrayType<T, N>::Type& MinPointBuffer()
160 return myMinPointBuffer;
163 //! Returns array of node maximum points.
164 typename BVH::ArrayType<T, N>::Type& MaxPointBuffer()
166 return myMaxPointBuffer;
169 //! Returns array of node minimum points.
170 const typename BVH::ArrayType<T, N>::Type& MinPointBuffer() const
172 return myMinPointBuffer;
175 //! Returns array of node maximum points.
176 const typename BVH::ArrayType<T, N>::Type& MaxPointBuffer() const
178 return myMaxPointBuffer;
181 public: //! @name protected fields
183 //! Array of node data records.
184 BVH_Array4i myNodeInfoBuffer;
186 //! Array of node minimum points.
187 typename BVH::ArrayType<T, N>::Type myMinPointBuffer;
189 //! Array of node maximum points.
190 typename BVH::ArrayType<T, N>::Type myMaxPointBuffer;
192 //! Current depth of BVH tree (set by builder).
197 //! Type corresponding to quad BVH.
198 struct BVH_QuadTree { };
200 //! Type corresponding to binary BVH.
201 struct BVH_BinaryTree { };
203 //! BVH tree with given arity (2 or 4).
204 template<class T, int N, class Arity = BVH_BinaryTree>
210 #endif // _BVH_TreeBase_Header