Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-26 10:21:21

0001 // Created on: 2014-09-11
0002 // Created by: Danila ULYANOV
0003 // Copyright (c) 2013-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef _BVH_LinearBuilder_Header
0017 #define _BVH_LinearBuilder_Header
0018 
0019 #include <BVH_RadixSorter.hxx>
0020 #include <NCollection_DynamicArray.hxx>
0021 #include <Standard_Assert.hxx>
0022 
0023 //! Performs fast BVH construction using LBVH building approach.
0024 //! Algorithm uses spatial Morton codes to reduce the BVH construction
0025 //! problem to a sorting problem (radix sort -- O(N) complexity). This
0026 //! Linear Bounding Volume Hierarchy (LBVH) builder produces BVH trees
0027 //! of lower quality compared to SAH-based BVH builders but it is over
0028 //! an order of magnitude faster (up to 3M triangles per second).
0029 //!
0030 //! For more details see:
0031 //! C. Lauterbach, M. Garland, S. Sengupta, D. Luebke, and D. Manocha.
0032 //! Fast BVH construction on GPUs. Eurographics, 2009.
0033 template <class T, int N>
0034 class BVH_LinearBuilder : public BVH_Builder<T, N>
0035 {
0036 public:
0037   typedef typename BVH::VectorType<T, N>::Type BVH_VecNt;
0038 
0039 public:
0040   //! Creates binned LBVH builder.
0041   BVH_LinearBuilder(const int theLeafNodeSize = BVH_Constants_LeafNodeSizeDefault,
0042                     const int theMaxTreeDepth = BVH_Constants_MaxTreeDepth);
0043 
0044   //! Releases resources of LBVH builder.
0045   ~BVH_LinearBuilder() override;
0046 
0047   //! Builds BVH.
0048   void Build(BVH_Set<T, N>*       theSet,
0049              BVH_Tree<T, N>*      theBVH,
0050              const BVH_Box<T, N>& theBox) const override;
0051 
0052 protected:
0053   typedef NCollection_Array1<BVH_EncodedLink>::iterator LinkIterator;
0054 
0055 protected:
0056   //! Emits hierarchy from sorted Morton codes.
0057   int emitHierachy(BVH_Tree<T, N>*                            theBVH,
0058                    const NCollection_Array1<BVH_EncodedLink>& theEncodedLinks,
0059                    const int                                  theBit,
0060                    const int                                  theShift,
0061                    const int                                  theStart,
0062                    const int                                  theFinal) const;
0063 
0064   //! Returns index of the first element which does not compare less than the given one.
0065   int lowerBound(const NCollection_Array1<BVH_EncodedLink>& theEncodedLinks,
0066                  int                                        theStart,
0067                  int                                        theFinal,
0068                  int                                        theDigit) const;
0069 };
0070 
0071 //=================================================================================================
0072 
0073 template <class T, int N>
0074 BVH_LinearBuilder<T, N>::BVH_LinearBuilder(const int theLeafNodeSize, const int theMaxTreeDepth)
0075     : BVH_Builder<T, N>(theLeafNodeSize, theMaxTreeDepth)
0076 {
0077 }
0078 
0079 //=================================================================================================
0080 
0081 template <class T, int N>
0082 BVH_LinearBuilder<T, N>::~BVH_LinearBuilder() = default;
0083 
0084 // =======================================================================
0085 // function : lowerBound
0086 // purpose  : Returns index of first element greater than the given one
0087 // =======================================================================
0088 template <class T, int N>
0089 int BVH_LinearBuilder<T, N>::lowerBound(const NCollection_Array1<BVH_EncodedLink>& theEncodedLinks,
0090                                         int                                        theStart,
0091                                         int                                        theFinal,
0092                                         int                                        theDigit) const
0093 {
0094   int          aNbPrims = theFinal - theStart;
0095   unsigned int aBit     = 1U << theDigit;
0096   while (aNbPrims > 0)
0097   {
0098     const int aStep = aNbPrims / 2;
0099     if (theEncodedLinks.Value(theStart + aStep).first & aBit)
0100     {
0101       aNbPrims = aStep;
0102     }
0103     else
0104     {
0105       theStart += aStep + 1;
0106       aNbPrims -= aStep + 1;
0107     }
0108   }
0109 
0110   return theStart;
0111 }
0112 
0113 // =======================================================================
0114 // function : emitHierachy
0115 // purpose  : Emits hierarchy from sorted Morton codes
0116 // =======================================================================
0117 template <class T, int N>
0118 int BVH_LinearBuilder<T, N>::emitHierachy(
0119   BVH_Tree<T, N>*                            theBVH,
0120   const NCollection_Array1<BVH_EncodedLink>& theEncodedLinks,
0121   const int                                  theDigit,
0122   const int                                  theShift,
0123   const int                                  theStart,
0124   const int                                  theFinal) const
0125 {
0126   if (theFinal - theStart > BVH_Builder<T, N>::myLeafNodeSize)
0127   {
0128     const int aPosition = theDigit < 0 ? (theStart + theFinal) / 2
0129                                        : lowerBound(theEncodedLinks, theStart, theFinal, theDigit);
0130     if (aPosition == theStart || aPosition == theFinal)
0131     {
0132       return emitHierachy(theBVH, theEncodedLinks, theDigit - 1, theShift, theStart, theFinal);
0133     }
0134 
0135     // Build inner node
0136     const int aNode    = theBVH->AddInnerNode(0, 0);
0137     const int aRghNode = theShift + aPosition - theStart;
0138 
0139     const int aLftChild =
0140       emitHierachy(theBVH, theEncodedLinks, theDigit - 1, theShift, theStart, aPosition);
0141     const int aRghChild =
0142       emitHierachy(theBVH, theEncodedLinks, theDigit - 1, aRghNode, aPosition, theFinal);
0143 
0144     theBVH->NodeInfoBuffer()[aNode].y() = aLftChild;
0145     theBVH->NodeInfoBuffer()[aNode].z() = aRghChild;
0146     return aNode;
0147   }
0148   else
0149   {
0150     // Build leaf node
0151     return theBVH->AddLeafNode(theShift, theShift + theFinal - theStart - 1);
0152   }
0153 }
0154 
0155 namespace BVH
0156 {
0157 //! Calculates bounding boxes (AABBs) for the given BVH tree.
0158 template <class T, int N>
0159 int UpdateBounds(BVH_Set<T, N>* theSet, BVH_Tree<T, N>* theTree, const int theNode = 0)
0160 {
0161   const BVH_Vec4i aData = theTree->NodeInfoBuffer()[theNode];
0162   if (aData.x() == 0)
0163   {
0164     const int aLftChild = theTree->NodeInfoBuffer()[theNode].y();
0165     const int aRghChild = theTree->NodeInfoBuffer()[theNode].z();
0166 
0167     const int aLftDepth = UpdateBounds(theSet, theTree, aLftChild);
0168     const int aRghDepth = UpdateBounds(theSet, theTree, aRghChild);
0169 
0170     typename BVH_Box<T, N>::BVH_VecNt aLftMinPoint = theTree->MinPointBuffer()[aLftChild];
0171     typename BVH_Box<T, N>::BVH_VecNt aLftMaxPoint = theTree->MaxPointBuffer()[aLftChild];
0172     typename BVH_Box<T, N>::BVH_VecNt aRghMinPoint = theTree->MinPointBuffer()[aRghChild];
0173     typename BVH_Box<T, N>::BVH_VecNt aRghMaxPoint = theTree->MaxPointBuffer()[aRghChild];
0174 
0175     BVH::BoxMinMax<T, N>::CwiseMin(aLftMinPoint, aRghMinPoint);
0176     BVH::BoxMinMax<T, N>::CwiseMax(aLftMaxPoint, aRghMaxPoint);
0177 
0178     theTree->MinPointBuffer()[theNode] = aLftMinPoint;
0179     theTree->MaxPointBuffer()[theNode] = aLftMaxPoint;
0180     return (std::max)(aLftDepth, aRghDepth) + 1;
0181   }
0182   else
0183   {
0184     typename BVH_Box<T, N>::BVH_VecNt& aMinPoint = theTree->MinPointBuffer()[theNode];
0185     typename BVH_Box<T, N>::BVH_VecNt& aMaxPoint = theTree->MaxPointBuffer()[theNode];
0186     for (int aPrimIdx = aData.y(); aPrimIdx <= aData.z(); ++aPrimIdx)
0187     {
0188       const BVH_Box<T, N> aBox = theSet->Box(aPrimIdx);
0189       if (aPrimIdx == aData.y())
0190       {
0191         aMinPoint = aBox.CornerMin();
0192         aMaxPoint = aBox.CornerMax();
0193       }
0194       else
0195       {
0196         BVH::BoxMinMax<T, N>::CwiseMin(aMinPoint, aBox.CornerMin());
0197         BVH::BoxMinMax<T, N>::CwiseMax(aMaxPoint, aBox.CornerMax());
0198       }
0199     }
0200   }
0201   return 0;
0202 }
0203 
0204 template <class T, int N>
0205 struct BoundData
0206 {
0207   BVH_Set<T, N>*  mySet;    //!< Set of geometric objects
0208   BVH_Tree<T, N>* myBVH;    //!< BVH tree built over the set
0209   int             myNode;   //!< BVH node to update bounding box
0210   int             myLevel;  //!< Level of the processed BVH node
0211   int*            myHeight; //!< Height of the processed BVH node
0212 };
0213 
0214 //! Task for parallel bounds updating.
0215 template <class T, int N>
0216 class UpdateBoundTask
0217 {
0218 public:
0219   UpdateBoundTask(const bool isParallel)
0220       : myIsParallel(isParallel)
0221   {
0222   }
0223 
0224   //! Executes the task.
0225   void operator()(const BoundData<T, N>& theData) const
0226   {
0227     if (theData.myBVH->IsOuter(theData.myNode) || theData.myLevel > 2)
0228     {
0229       *theData.myHeight = BVH::UpdateBounds(theData.mySet, theData.myBVH, theData.myNode);
0230     }
0231     else
0232     {
0233       int aLftHeight = 0;
0234       int aRghHeight = 0;
0235 
0236       const int aLftChild = theData.myBVH->NodeInfoBuffer()[theData.myNode].y();
0237       const int aRghChild = theData.myBVH->NodeInfoBuffer()[theData.myNode].z();
0238 
0239       NCollection_DynamicArray<BoundData<T, N>> aList(2);
0240       if (!theData.myBVH->IsOuter(aLftChild))
0241       {
0242         BoundData<T, N> aBoundData = {theData.mySet,
0243                                       theData.myBVH,
0244                                       aLftChild,
0245                                       theData.myLevel + 1,
0246                                       &aLftHeight};
0247         aList.Append(aBoundData);
0248       }
0249       else
0250       {
0251         aLftHeight = BVH::UpdateBounds(theData.mySet, theData.myBVH, aLftChild);
0252       }
0253 
0254       if (!theData.myBVH->IsOuter(aRghChild))
0255       {
0256         BoundData<T, N> aBoundData = {theData.mySet,
0257                                       theData.myBVH,
0258                                       aRghChild,
0259                                       theData.myLevel + 1,
0260                                       &aRghHeight};
0261         aList.Append(aBoundData);
0262       }
0263       else
0264       {
0265         aRghHeight = BVH::UpdateBounds(theData.mySet, theData.myBVH, aRghChild);
0266       }
0267 
0268       if (aList.Size() > 0)
0269       {
0270         OSD_Parallel::ForEach(aList.begin(),
0271                               aList.end(),
0272                               UpdateBoundTask<T, N>(myIsParallel),
0273                               !myIsParallel);
0274       }
0275 
0276       typename BVH_Box<T, N>::BVH_VecNt aLftMinPoint = theData.myBVH->MinPointBuffer()[aLftChild];
0277       typename BVH_Box<T, N>::BVH_VecNt aLftMaxPoint = theData.myBVH->MaxPointBuffer()[aLftChild];
0278       typename BVH_Box<T, N>::BVH_VecNt aRghMinPoint = theData.myBVH->MinPointBuffer()[aRghChild];
0279       typename BVH_Box<T, N>::BVH_VecNt aRghMaxPoint = theData.myBVH->MaxPointBuffer()[aRghChild];
0280 
0281       BVH::BoxMinMax<T, N>::CwiseMin(aLftMinPoint, aRghMinPoint);
0282       BVH::BoxMinMax<T, N>::CwiseMax(aLftMaxPoint, aRghMaxPoint);
0283 
0284       theData.myBVH->MinPointBuffer()[theData.myNode] = aLftMinPoint;
0285       theData.myBVH->MaxPointBuffer()[theData.myNode] = aLftMaxPoint;
0286 
0287       *theData.myHeight = (std::max)(aLftHeight, aRghHeight) + 1;
0288     }
0289   }
0290 
0291 private:
0292   bool myIsParallel;
0293 };
0294 } // namespace BVH
0295 
0296 //=================================================================================================
0297 
0298 template <class T, int N>
0299 void BVH_LinearBuilder<T, N>::Build(BVH_Set<T, N>*       theSet,
0300                                     BVH_Tree<T, N>*      theBVH,
0301                                     const BVH_Box<T, N>& theBox) const
0302 {
0303   Standard_STATIC_ASSERT(N == 2 || N == 3 || N == 4);
0304   const int aSetSize = theSet->Size();
0305   if (theBVH == nullptr || aSetSize == 0)
0306   {
0307     return;
0308   }
0309 
0310   theBVH->Clear();
0311 
0312   // Step 0 -- Initialize parameter of virtual grid
0313   BVH_RadixSorter<T, N> aRadixSorter(theBox);
0314   aRadixSorter.SetParallel(this->IsParallel());
0315 
0316   // Step 1 - Perform radix sorting of primitive set
0317   aRadixSorter.Perform(theSet);
0318 
0319   // Step 2 -- Emitting BVH hierarchy from sorted Morton codes
0320   emitHierachy(theBVH, aRadixSorter.EncodedLinks(), 29, 0, 0, theSet->Size());
0321 
0322   // Step 3 -- Compute bounding boxes of BVH nodes
0323   theBVH->MinPointBuffer().Resize(theBVH->NodeInfoBuffer().Size());
0324   theBVH->MaxPointBuffer().Resize(theBVH->NodeInfoBuffer().Size());
0325 
0326   int                        aHeight    = 0;
0327   BVH::BoundData<T, N>       aBoundData = {theSet, theBVH, 0, 0, &aHeight};
0328   BVH::UpdateBoundTask<T, N> aBoundTask(this->IsParallel());
0329   aBoundTask(aBoundData);
0330 
0331   BVH_Builder<T, N>::updateDepth(theBVH, aHeight);
0332 }
0333 
0334 #endif // _BVH_LinearBuilder_Header