1 // Created on: 2016-06-20
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2016 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_BinaryTree_Header
17 #define _BVH_BinaryTree_Header
19 #include <BVH_QuadTree.hxx>
24 //! Specialization of binary BVH tree.
25 template<class T, int N>
26 class BVH_Tree<T, N, BVH_BinaryTree> : public BVH_TreeBase<T, N>
28 public: //! @name custom data types
30 typedef typename BVH_TreeBase<T, N>::BVH_VecNt BVH_VecNt;
32 public: //! @name methods for accessing individual nodes
34 //! Creates new empty BVH tree.
35 BVH_Tree() : BVH_TreeBase<T, N>() { }
37 //! Sets node type to 'outer'.
38 void SetOuter (const int theNodeIndex) { BVH::Array<int, 4>::ChangeValue (this->myNodeInfoBuffer, theNodeIndex).x() = 1; }
40 //! Sets node type to 'inner'.
41 void SetInner (const int theNodeIndex) { BVH::Array<int, 4>::ChangeValue (this->myNodeInfoBuffer, theNodeIndex).x() = 0; }
43 //! Returns index of the K-th child of the given inner node.
44 //! \tparam K the index of node child (0 or 1)
46 int Child (const int theNodeIndex) const { return BVH::Array<int, 4>::Value (this->myNodeInfoBuffer, theNodeIndex)[K + 1]; }
48 //! Returns index of the K-th child of the given inner node.
49 //! \tparam K the index of node child (0 or 1)
51 int& ChangeChild (const int theNodeIndex) { return BVH::Array<int, 4>::ChangeValue (this->myNodeInfoBuffer, theNodeIndex)[K + 1]; }
53 //! Returns index of the K-th child of the given inner node.
54 //! \tparam K the index of node child (0 or 1)
56 int& Child (const int theNodeIndex) { return BVH::Array<int, 4>::ChangeValue (this->myNodeInfoBuffer, theNodeIndex)[K + 1]; }
58 public: //! @name methods for adding/removing tree nodes
60 //! Removes all nodes from the tree.
64 BVH::Array<T, N>::Clear (this->myMinPointBuffer);
65 BVH::Array<T, N>::Clear (this->myMaxPointBuffer);
66 BVH::Array<int, 4>::Clear(this->myNodeInfoBuffer);
69 //! Reserves internal BVH storage, so that it
70 //! can contain the given number of BVH nodes.
71 void Reserve (const int theNbNodes)
73 BVH::Array<T, N>::Reserve (this->myMinPointBuffer, theNbNodes);
74 BVH::Array<T, N>::Reserve (this->myMaxPointBuffer, theNbNodes);
75 BVH::Array<int, 4>::Reserve (this->myNodeInfoBuffer, theNbNodes);
78 //! Adds new leaf node to the BVH.
79 int AddLeafNode (const BVH_VecNt& theMinPoint,
80 const BVH_VecNt& theMaxPoint,
84 BVH::Array<T, N>::Append (this->myMinPointBuffer, theMinPoint);
85 BVH::Array<T, N>::Append (this->myMaxPointBuffer, theMaxPoint);
86 BVH::Array<int, 4>::Append (this->myNodeInfoBuffer, BVH_Vec4i (1, theBegElem, theEndElem, 0));
87 return BVH::Array<int, 4>::Size (this->myNodeInfoBuffer) - 1;
90 //! Adds new inner node to the BVH.
91 int AddInnerNode (const BVH_VecNt& theMinPoint,
92 const BVH_VecNt& theMaxPoint,
93 const int theLftChild,
94 const int theRghChild)
96 BVH::Array<T, N>::Append (this->myMinPointBuffer, theMinPoint);
97 BVH::Array<T, N>::Append (this->myMaxPointBuffer, theMaxPoint);
98 BVH::Array<int, 4>::Append (this->myNodeInfoBuffer, BVH_Vec4i (0, theLftChild, theRghChild, 0));
99 return BVH::Array<int, 4>::Size (this->myNodeInfoBuffer) - 1;
102 //! Adds new leaf node to the BVH.
103 int AddLeafNode (const BVH_Box<T, N>& theAABB,
104 const int theBegElem,
105 const int theEndElem)
107 return AddLeafNode (theAABB.CornerMin(), theAABB.CornerMax(), theBegElem, theEndElem);
110 //! Adds new inner node to the BVH.
111 int AddInnerNode (const BVH_Box<T, N>& theAABB,
112 const int theLftChild,
113 const int theRghChild)
115 return AddInnerNode (theAABB.CornerMin(), theAABB.CornerMax(), theLftChild, theRghChild);
118 //! Adds new leaf node to the BVH with UNINITIALIZED bounds.
119 int AddLeafNode (const int theBegElem,
120 const int theEndElem)
122 BVH::Array<int, 4>::Append (this->myNodeInfoBuffer, BVH_Vec4i (1, theBegElem, theEndElem, 0));
123 return BVH::Array<int, 4>::Size (this->myNodeInfoBuffer) - 1;
126 //! Adds new inner node to the BVH with UNINITIALIZED bounds.
127 int AddInnerNode (const int theLftChild,
128 const int theRghChild)
130 BVH::Array<int, 4>::Append (this->myNodeInfoBuffer, BVH_Vec4i (0, theLftChild, theRghChild, 0));
131 return BVH::Array<int, 4>::Size (this->myNodeInfoBuffer) - 1;
134 public: //! @name methods specific to binary BVH
136 //! Returns value of SAH (surface area heuristic).
137 //! Allows to compare the quality of BVH trees constructed for
138 //! the same sets of geometric objects with different methods.
139 T EstimateSAH() const;
141 //! Collapses the tree into QBVH an returns it. As a result, each
142 //! 2-nd level of current tree is kept and the rest are discarded.
143 BVH_Tree<T, N, BVH_QuadTree>* CollapseToQuadTree() const;
149 //! Internal function for recursive calculation of
150 //! surface area heuristic (SAH) of the given tree.
151 template<class T, int N>
152 void EstimateSAH (const BVH_Tree<T, N, BVH_BinaryTree>* theTree, const int theNode, T theProb, T& theSAH)
154 BVH_Box<T, N> aBox (theTree->MinPoint (theNode),
155 theTree->MaxPoint (theNode));
157 if (theTree->IsOuter (theNode))
159 theSAH += theProb * (theTree->EndPrimitive (theNode) - theTree->BegPrimitive (theNode) + 1);
163 theSAH += theProb * static_cast<T> (2.0);
165 BVH_Box<T, N> aLftBox (theTree->MinPoint (theTree->template Child<0> (theNode)),
166 theTree->MaxPoint (theTree->template Child<0> (theNode)));
170 EstimateSAH (theTree, theTree->template Child<0> (theNode),
171 theProb * aLftBox.Area() / aBox.Area(), theSAH);
174 BVH_Box<T, N> aRghBox (theTree->MinPoint (theTree->template Child<1> (theNode)),
175 theTree->MaxPoint (theTree->template Child<1> (theNode)));
179 EstimateSAH (theTree, theTree->template Child<1> (theNode),
180 theProb * aRghBox.Area() / aBox.Area(), theSAH);
186 // =======================================================================
187 // function : EstimateSAH
189 // =======================================================================
190 template<class T, int N>
191 T BVH_Tree<T, N, BVH_BinaryTree>::EstimateSAH() const
193 T aSAH = static_cast<T> (0.0);
194 BVH::EstimateSAH<T, N> (this, 0, static_cast<T> (1.0), aSAH);
198 // =======================================================================
199 // function : CollapseToQuadTree
201 // =======================================================================
202 template<class T, int N>
203 BVH_Tree<T, N, BVH_QuadTree>* BVH_Tree<T, N, BVH_BinaryTree>::CollapseToQuadTree() const
205 BVH_Tree<T, N, BVH_QuadTree>* aQBVH = new BVH_Tree<T, N, BVH_QuadTree>;
207 if (this->Length() == 0)
212 std::deque<std::pair<int, int> > aQueue (1, std::make_pair (0, 0));
214 for (int aNbNodes = 1; !aQueue.empty();)
216 const std::pair<int, int> aNode = aQueue.front();
218 BVH::Array<T, N>::Append (aQBVH->myMinPointBuffer, BVH::Array<T, N>::Value (this->myMinPointBuffer, opencascade::std::get<0> (aNode)));
219 BVH::Array<T, N>::Append (aQBVH->myMaxPointBuffer, BVH::Array<T, N>::Value (this->myMaxPointBuffer, opencascade::std::get<0> (aNode)));
222 if (this->IsOuter (opencascade::std::get<0> (aNode))) // is leaf node
224 aNodeInfo = BVH_Vec4i (1 /* leaf flag */,
225 this->BegPrimitive (opencascade::std::get<0> (aNode)), this->EndPrimitive (opencascade::std::get<0> (aNode)), opencascade::std::get<1> (aNode) /* level */);
229 NCollection_Vector<int> aGrandChildNodes;
231 const int aLftChild = Child<0> (opencascade::std::get<0> (aNode));
232 const int aRghChild = Child<1> (opencascade::std::get<0> (aNode));
233 if (this->IsOuter (aLftChild)) // is leaf node
235 aGrandChildNodes.Append (aLftChild);
239 aGrandChildNodes.Append (Child<0> (aLftChild));
240 aGrandChildNodes.Append (Child<1> (aLftChild));
243 if (this->IsOuter (aRghChild)) // is leaf node
245 aGrandChildNodes.Append (aRghChild);
249 aGrandChildNodes.Append (Child<0> (aRghChild));
250 aGrandChildNodes.Append (Child<1> (aRghChild));
253 for (int aNodeIdx = 0; aNodeIdx < aGrandChildNodes.Size(); ++aNodeIdx)
255 aQueue.push_back (std::make_pair (aGrandChildNodes (aNodeIdx), opencascade::std::get<1> (aNode) + 1));
258 aNodeInfo = BVH_Vec4i (0 /* inner flag */,
259 aNbNodes, aGrandChildNodes.Size() - 1, opencascade::std::get<1> (aNode) /* level */);
261 aQBVH->myDepth = Max (aQBVH->myDepth, opencascade::std::get<1> (aNode) + 1);
263 aNbNodes += aGrandChildNodes.Size();
266 BVH::Array<int, 4>::Append (aQBVH->myNodeInfoBuffer, aNodeInfo);
267 aQueue.pop_front(); // node processing completed
273 #endif // _BVH_BinaryTree_Header