1 // Created on: 2014-09-11
2 // Created by: Danila ULYANOV
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.
18 #include <Standard_Assert.hxx>
20 #include <NCollection_Array1.hxx>
23 // On Windows, function TryEnterCriticalSection has appeared in Windows NT
24 // and is surrounded by #ifdef in MS VC++ 7.1 headers.
25 // Thus to use it we need to define appropriate macro saying that we will
26 // run on Windows NT 4.0 at least
27 #if defined(_WIN32) && !defined(_WIN32_WINNT)
28 #define _WIN32_WINNT 0x0501
34 // =======================================================================
35 // function : BVH_LinearBuilder
37 // =======================================================================
38 template<class T, int N>
39 BVH_LinearBuilder<T, N>::BVH_LinearBuilder (const Standard_Integer theLeafNodeSize,
40 const Standard_Integer theMaxTreeDepth)
41 : BVH_Builder<T, N> (theLeafNodeSize,
47 // =======================================================================
48 // function : ~BVH_LinearBuilder
50 // =======================================================================
51 template<class T, int N>
52 BVH_LinearBuilder<T, N>::~BVH_LinearBuilder()
59 // Radix sort STL predicate for 32-bit integer.
62 Standard_Integer myBit;
66 //! Creates new radix sort predicate.
67 BitPredicate (const Standard_Integer theBit) : myBit (theBit)
72 //! Returns predicate value.
73 bool operator() (const BVH_EncodedLink theLink) const
75 const Standard_Integer aMask = 1 << myBit;
77 return !(theLink.first & aMask); // 0-bit to the left side
81 //! STL compare tool used in binary search algorithm.
84 Standard_Integer myBit;
88 //! Creates new STL comparator.
89 BitComparator (const Standard_Integer theBit) : myBit (theBit)
94 //! Checks left value for the given bit.
95 bool operator() (BVH_EncodedLink theLink1, BVH_EncodedLink /*theLink2*/)
97 return !(theLink1.first & (1 << myBit));
101 //! Tool object for sorting link array using radix sort algorithm.
104 typedef std::vector<BVH_EncodedLink>::iterator LinkIterator;
106 // Performs MSD (most significant digit) radix sort.
107 static void Perform (LinkIterator theStart, LinkIterator theFinal, Standard_Integer theBit = 29)
109 while (theStart != theFinal && theBit >= 0)
111 LinkIterator anOffset = std::partition (theStart, theFinal, BitPredicate (theBit--));
113 Perform (theStart, anOffset, theBit);
120 //! Calculates bounding boxes (AABBs) for the given BVH tree.
121 template<class T, int N>
122 Standard_Integer UpdateBounds (BVH_Set<T, N>* theSet, BVH_Tree<T, N>* theTree, const Standard_Integer theNode = 0)
124 const BVH_Vec4i aData = theTree->NodeInfoBuffer()[theNode];
128 const Standard_Integer aLftChild = theTree->NodeInfoBuffer()[theNode].y();
129 const Standard_Integer aRghChild = theTree->NodeInfoBuffer()[theNode].z();
131 const Standard_Integer aLftDepth = UpdateBounds (theSet, theTree, aLftChild);
132 const Standard_Integer aRghDepth = UpdateBounds (theSet, theTree, aRghChild);
134 typename BVH_Box<T, N>::BVH_VecNt aLftMinPoint = theTree->MinPointBuffer()[aLftChild];
135 typename BVH_Box<T, N>::BVH_VecNt aLftMaxPoint = theTree->MaxPointBuffer()[aLftChild];
136 typename BVH_Box<T, N>::BVH_VecNt aRghMinPoint = theTree->MinPointBuffer()[aRghChild];
137 typename BVH_Box<T, N>::BVH_VecNt aRghMaxPoint = theTree->MaxPointBuffer()[aRghChild];
139 BVH::BoxMinMax<T, N>::CwiseMin (aLftMinPoint, aRghMinPoint);
140 BVH::BoxMinMax<T, N>::CwiseMax (aLftMaxPoint, aRghMaxPoint);
142 theTree->MinPointBuffer()[theNode] = aLftMinPoint;
143 theTree->MaxPointBuffer()[theNode] = aLftMaxPoint;
145 return Max (aLftDepth, aRghDepth) + 1;
149 typename BVH_Box<T, N>::BVH_VecNt& aMinPoint = theTree->MinPointBuffer()[theNode];
150 typename BVH_Box<T, N>::BVH_VecNt& aMaxPoint = theTree->MaxPointBuffer()[theNode];
152 for (Standard_Integer aPrimIdx = aData.y(); aPrimIdx <= aData.z(); ++aPrimIdx)
154 const BVH_Box<T, N> aBox = theSet->Box (aPrimIdx);
156 if (aPrimIdx == aData.y())
158 aMinPoint = aBox.CornerMin();
159 aMaxPoint = aBox.CornerMax();
163 BVH::BoxMinMax<T, N>::CwiseMin (aMinPoint, aBox.CornerMin());
164 BVH::BoxMinMax<T, N>::CwiseMax (aMaxPoint, aBox.CornerMax());
173 // =======================================================================
174 // function : EmitHierachy
175 // purpose : Emits hierarchy from sorted Morton codes
176 // =======================================================================
177 template<class T, int N>
178 Standard_Integer BVH_LinearBuilder<T, N>::EmitHierachy (BVH_Tree<T, N>* theBVH,
179 const Standard_Integer theBit,
180 const Standard_Integer theShift,
181 std::vector<BVH_EncodedLink>::iterator theStart,
182 std::vector<BVH_EncodedLink>::iterator theFinal)
184 if (theFinal - theStart > BVH_Builder<T, N>::myLeafNodeSize && theBit >= 0)
186 std::vector<BVH_EncodedLink>::iterator aPosition = std::lower_bound (
187 theStart, theFinal, BVH_EncodedLink(), BVH::BitComparator (theBit));
189 if (aPosition == theStart || aPosition == theFinal)
191 return EmitHierachy (theBVH, theBit - 1, theShift, theStart, theFinal);
195 const Standard_Integer aNode = theBVH->AddInnerNode (0, 0);
197 const Standard_Integer aRghNode = theShift + static_cast<Standard_Integer> (aPosition - theStart);
199 const Standard_Integer aLftChild = EmitHierachy (theBVH, theBit - 1, theShift, theStart, aPosition);
200 const Standard_Integer aRghChild = EmitHierachy (theBVH, theBit - 1, aRghNode, aPosition, theFinal);
202 theBVH->NodeInfoBuffer()[aNode].y() = aLftChild;
203 theBVH->NodeInfoBuffer()[aNode].z() = aRghChild;
210 return theBVH->AddLeafNode (theShift, theShift + static_cast<Standard_Integer> (theFinal - theStart) - 1);
218 //! TBB task for parallel radix sort.
219 class RadixSortTask : public tbb::task
221 typedef std::vector<BVH_EncodedLink>::iterator LinkIterator;
225 //! Start range element.
226 LinkIterator myStart;
228 //! Final range element.
229 LinkIterator myFinal;
231 //! Bit position for range partition.
232 Standard_Integer myDigit;
236 //! Creates new TBB radix sort task.
237 RadixSortTask (LinkIterator theStart, LinkIterator theFinal, Standard_Integer theDigit)
238 : myStart (theStart),
245 //! Executes the task.
250 BVH::RadixSorter::Perform (myStart, myFinal, myDigit);
254 LinkIterator anOffset = std::partition (myStart, myFinal, BitPredicate (myDigit));
256 tbb::task_list aList;
258 aList.push_back (*new ( allocate_child() )
259 RadixSortTask (myStart, anOffset, myDigit - 1));
261 aList.push_back (*new ( allocate_child() )
262 RadixSortTask (anOffset, myFinal, myDigit - 1));
264 set_ref_count (3); // count + 1
265 spawn_and_wait_for_all (aList);
272 //! TBB task for parallel bounds updating.
273 template<class T, int N>
274 class UpdateBoundTask: public tbb::task
276 //! Set of geometric objects.
277 BVH_Set<T, N>* mySet;
279 //! BVH tree built over the set.
280 BVH_Tree<T, N>* myBVH;
282 //! BVH node to update bounding box.
283 Standard_Integer myNode;
285 //! Level of the processed BVH node.
286 Standard_Integer myLevel;
288 //! Height of the processed BVH node.
289 Standard_Integer* myHeight;
293 //! Creates new TBB parallel bound update task.
294 UpdateBoundTask (BVH_Set<T, N>* theSet,
295 BVH_Tree<T, N>* theBVH,
296 Standard_Integer theNode,
297 Standard_Integer theLevel,
298 Standard_Integer* theHeight)
308 //! Executes the task.
311 if (myBVH->IsOuter (myNode) || myLevel > 2)
313 *myHeight = BVH::UpdateBounds (mySet, myBVH, myNode);
317 Standard_Integer aLftHeight = 0;
318 Standard_Integer aRghHeight = 0;
320 tbb::task_list aList;
322 const Standard_Integer aLftChild = myBVH->NodeInfoBuffer()[myNode].y();
323 const Standard_Integer aRghChild = myBVH->NodeInfoBuffer()[myNode].z();
325 Standard_Integer aCount = 1;
327 if (!myBVH->IsOuter (aLftChild))
330 aList.push_back (*new ( allocate_child() )
331 UpdateBoundTask (mySet, myBVH, aLftChild, myLevel + 1, &aLftHeight));
335 aLftHeight = BVH::UpdateBounds (mySet, myBVH, aLftChild);
338 if (!myBVH->IsOuter (aRghChild))
341 aList.push_back (*new( allocate_child() )
342 UpdateBoundTask (mySet, myBVH, aRghChild, myLevel + 1, &aRghHeight));
346 aRghHeight = BVH::UpdateBounds (mySet, myBVH, aRghChild);
351 set_ref_count (aCount);
352 spawn_and_wait_for_all (aList);
355 typename BVH_Box<T, N>::BVH_VecNt aLftMinPoint = myBVH->MinPointBuffer()[aLftChild];
356 typename BVH_Box<T, N>::BVH_VecNt aLftMaxPoint = myBVH->MaxPointBuffer()[aLftChild];
357 typename BVH_Box<T, N>::BVH_VecNt aRghMinPoint = myBVH->MinPointBuffer()[aRghChild];
358 typename BVH_Box<T, N>::BVH_VecNt aRghMaxPoint = myBVH->MaxPointBuffer()[aRghChild];
360 BVH::BoxMinMax<T, N>::CwiseMin (aLftMinPoint, aRghMinPoint);
361 BVH::BoxMinMax<T, N>::CwiseMax (aLftMaxPoint, aRghMaxPoint);
363 myBVH->MinPointBuffer()[myNode] = aLftMinPoint;
364 myBVH->MaxPointBuffer()[myNode] = aLftMaxPoint;
366 *myHeight = Max (aLftHeight, aRghHeight) + 1;
376 // =======================================================================
379 // =======================================================================
380 template<class T, int N>
381 void BVH_LinearBuilder<T, N>::Build (BVH_Set<T, N>* theSet,
382 BVH_Tree<T, N>* theBVH,
383 const BVH_Box<T, N>& theBox)
385 Standard_STATIC_ASSERT (N == 3 || N == 4);
387 if (theBVH == NULL || theSet->Size() == 0)
394 const Standard_Integer aDimensionX = 1024;
395 const Standard_Integer aDimensionY = 1024;
396 const Standard_Integer aDimensionZ = 1024;
398 const BVH_VecNt aSceneMin = theBox.CornerMin();
399 const BVH_VecNt aSceneMax = theBox.CornerMax();
401 const T aMinSize = static_cast<T> (BVH::THE_NODE_MIN_SIZE);
403 const T aReverseSizeX = static_cast<T> (aDimensionX) / Max (aMinSize, aSceneMax.x() - aSceneMin.x());
404 const T aReverseSizeY = static_cast<T> (aDimensionY) / Max (aMinSize, aSceneMax.y() - aSceneMin.y());
405 const T aReverseSizeZ = static_cast<T> (aDimensionZ) / Max (aMinSize, aSceneMax.z() - aSceneMin.z());
407 std::vector<BVH_EncodedLink> anEncodedLinks (theSet->Size(), BVH_EncodedLink());
409 // Step 1 -- Assign Morton code to each primitive
410 for (Standard_Integer aPrimIdx = 0; aPrimIdx < theSet->Size(); ++aPrimIdx)
412 const BVH_VecNt aCenter = theSet->Box (aPrimIdx).Center();
414 Standard_Integer aVoxelX = BVH::IntFloor ((aCenter.x() - aSceneMin.x()) * aReverseSizeX);
415 Standard_Integer aVoxelY = BVH::IntFloor ((aCenter.y() - aSceneMin.y()) * aReverseSizeY);
416 Standard_Integer aVoxelZ = BVH::IntFloor ((aCenter.z() - aSceneMin.z()) * aReverseSizeZ);
418 aVoxelX = Max (0, Min (aVoxelX, aDimensionX - 1));
419 aVoxelY = Max (0, Min (aVoxelY, aDimensionY - 1));
420 aVoxelZ = Max (0, Min (aVoxelZ, aDimensionZ - 1));
422 aVoxelX = (aVoxelX | (aVoxelX << 16)) & 0x030000FF;
423 aVoxelX = (aVoxelX | (aVoxelX << 8)) & 0x0300F00F;
424 aVoxelX = (aVoxelX | (aVoxelX << 4)) & 0x030C30C3;
425 aVoxelX = (aVoxelX | (aVoxelX << 2)) & 0x09249249;
427 aVoxelY = (aVoxelY | (aVoxelY << 16)) & 0x030000FF;
428 aVoxelY = (aVoxelY | (aVoxelY << 8)) & 0x0300F00F;
429 aVoxelY = (aVoxelY | (aVoxelY << 4)) & 0x030C30C3;
430 aVoxelY = (aVoxelY | (aVoxelY << 2)) & 0x09249249;
432 aVoxelZ = (aVoxelZ | (aVoxelZ << 16)) & 0x030000FF;
433 aVoxelZ = (aVoxelZ | (aVoxelZ << 8)) & 0x0300F00F;
434 aVoxelZ = (aVoxelZ | (aVoxelZ << 4)) & 0x030C30C3;
435 aVoxelZ = (aVoxelZ | (aVoxelZ << 2)) & 0x09249249;
437 anEncodedLinks[aPrimIdx] = BVH_EncodedLink (
438 aVoxelX | (aVoxelY << 1) | (aVoxelZ << 2), aPrimIdx);
441 // Step 2 -- Sort primitives by their Morton codes using radix sort
444 BVH::RadixSortTask& aSortTask = *new ( tbb::task::allocate_root() )
445 BVH::RadixSortTask (anEncodedLinks.begin(), anEncodedLinks.end(), 29);
447 tbb::task::spawn_root_and_wait (aSortTask);
451 BVH::RadixSorter::Perform (anEncodedLinks.begin(), anEncodedLinks.end());
455 // Step 3 -- Emitting BVH hierarchy from sorted Morton codes
456 EmitHierachy (theBVH, 29, 0, anEncodedLinks.begin(), anEncodedLinks.end());
458 NCollection_Array1<Standard_Integer> aLinkMap (0, theSet->Size() - 1);
460 for (Standard_Integer aLinkIdx = 0; aLinkIdx < theSet->Size(); ++aLinkIdx)
462 aLinkMap (anEncodedLinks[aLinkIdx].second) = aLinkIdx;
465 // Step 4 -- Rearranging primitive list according to Morton codes (in place)
466 Standard_Integer aPrimIdx = 0;
468 while (aPrimIdx < theSet->Size())
470 const Standard_Integer aSortIdx = aLinkMap (aPrimIdx);
472 if (aPrimIdx != aSortIdx)
474 theSet->Swap (aPrimIdx, aSortIdx);
476 std::swap (aLinkMap (aPrimIdx),
477 aLinkMap (aSortIdx));
485 // Step 5 -- Compute bounding boxes of BVH nodes
486 theBVH->MinPointBuffer().resize (theBVH->NodeInfoBuffer().size());
487 theBVH->MaxPointBuffer().resize (theBVH->NodeInfoBuffer().size());
489 Standard_Integer aDepth = 0;
493 // Note: Although TBB tasks are allocated using placement
494 // new, we do not need to delete them explicitly
495 BVH::UpdateBoundTask<T, N>& aRootTask = *new ( tbb::task::allocate_root() )
496 BVH::UpdateBoundTask<T, N> (theSet, theBVH, 0, 0, &aDepth);
498 tbb::task::spawn_root_and_wait (aRootTask);
502 aDepth = BVH::UpdateBounds (theSet, theBVH, 0);
506 BVH_Builder<T, N>::UpdateDepth (theBVH, aDepth);