File indexing completed on 2026-11-26 10:21:21
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
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
0024
0025
0026
0027
0028
0029
0030
0031
0032
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
0041 BVH_LinearBuilder(const int theLeafNodeSize = BVH_Constants_LeafNodeSizeDefault,
0042 const int theMaxTreeDepth = BVH_Constants_MaxTreeDepth);
0043
0044
0045 ~BVH_LinearBuilder() override;
0046
0047
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
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
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
0086
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
0115
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
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
0151 return theBVH->AddLeafNode(theShift, theShift + theFinal - theStart - 1);
0152 }
0153 }
0154
0155 namespace BVH
0156 {
0157
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;
0208 BVH_Tree<T, N>* myBVH;
0209 int myNode;
0210 int myLevel;
0211 int* myHeight;
0212 };
0213
0214
0215 template <class T, int N>
0216 class UpdateBoundTask
0217 {
0218 public:
0219 UpdateBoundTask(const bool isParallel)
0220 : myIsParallel(isParallel)
0221 {
0222 }
0223
0224
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 }
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
0313 BVH_RadixSorter<T, N> aRadixSorter(theBox);
0314 aRadixSorter.SetParallel(this->IsParallel());
0315
0316
0317 aRadixSorter.Perform(theSet);
0318
0319
0320 emitHierachy(theBVH, aRadixSorter.EncodedLinks(), 29, 0, 0, theSet->Size());
0321
0322
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