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)
186 std::vector<BVH_EncodedLink>::iterator aPosition =
187 (theBit >= 0) ? std::lower_bound (theStart, theFinal, BVH_EncodedLink(), BVH::BitComparator (theBit))
188 : theStart + ((theFinal - theStart) / 2);
190 if (aPosition == theStart || aPosition == theFinal)
192 return EmitHierachy (theBVH, theBit - 1, theShift, theStart, theFinal);
196 const Standard_Integer aNode = theBVH->AddInnerNode (0, 0);
198 const Standard_Integer aRghNode = theShift + static_cast<Standard_Integer> (aPosition - theStart);
200 const Standard_Integer aLftChild = EmitHierachy (theBVH, theBit - 1, theShift, theStart, aPosition);
201 const Standard_Integer aRghChild = EmitHierachy (theBVH, theBit - 1, aRghNode, aPosition, theFinal);
203 theBVH->NodeInfoBuffer()[aNode].y() = aLftChild;
204 theBVH->NodeInfoBuffer()[aNode].z() = aRghChild;
211 return theBVH->AddLeafNode (theShift, theShift + static_cast<Standard_Integer> (theFinal - theStart) - 1);
219 //! TBB task for parallel radix sort.
220 class RadixSortTask : public tbb::task
222 typedef std::vector<BVH_EncodedLink>::iterator LinkIterator;
226 //! Start range element.
227 LinkIterator myStart;
229 //! Final range element.
230 LinkIterator myFinal;
232 //! Bit position for range partition.
233 Standard_Integer myDigit;
237 //! Creates new TBB radix sort task.
238 RadixSortTask (LinkIterator theStart, LinkIterator theFinal, Standard_Integer theDigit)
239 : myStart (theStart),
246 //! Executes the task.
251 BVH::RadixSorter::Perform (myStart, myFinal, myDigit);
255 LinkIterator anOffset = std::partition (myStart, myFinal, BitPredicate (myDigit));
257 tbb::task_list aList;
259 aList.push_back (*new ( allocate_child() )
260 RadixSortTask (myStart, anOffset, myDigit - 1));
262 aList.push_back (*new ( allocate_child() )
263 RadixSortTask (anOffset, myFinal, myDigit - 1));
265 set_ref_count (3); // count + 1
266 spawn_and_wait_for_all (aList);
273 //! TBB task for parallel bounds updating.
274 template<class T, int N>
275 class UpdateBoundTask: public tbb::task
277 //! Set of geometric objects.
278 BVH_Set<T, N>* mySet;
280 //! BVH tree built over the set.
281 BVH_Tree<T, N>* myBVH;
283 //! BVH node to update bounding box.
284 Standard_Integer myNode;
286 //! Level of the processed BVH node.
287 Standard_Integer myLevel;
289 //! Height of the processed BVH node.
290 Standard_Integer* myHeight;
294 //! Creates new TBB parallel bound update task.
295 UpdateBoundTask (BVH_Set<T, N>* theSet,
296 BVH_Tree<T, N>* theBVH,
297 Standard_Integer theNode,
298 Standard_Integer theLevel,
299 Standard_Integer* theHeight)
309 //! Executes the task.
312 if (myBVH->IsOuter (myNode) || myLevel > 2)
314 *myHeight = BVH::UpdateBounds (mySet, myBVH, myNode);
318 Standard_Integer aLftHeight = 0;
319 Standard_Integer aRghHeight = 0;
321 tbb::task_list aList;
323 const Standard_Integer aLftChild = myBVH->NodeInfoBuffer()[myNode].y();
324 const Standard_Integer aRghChild = myBVH->NodeInfoBuffer()[myNode].z();
326 Standard_Integer aCount = 1;
328 if (!myBVH->IsOuter (aLftChild))
331 aList.push_back (*new ( allocate_child() )
332 UpdateBoundTask (mySet, myBVH, aLftChild, myLevel + 1, &aLftHeight));
336 aLftHeight = BVH::UpdateBounds (mySet, myBVH, aLftChild);
339 if (!myBVH->IsOuter (aRghChild))
342 aList.push_back (*new( allocate_child() )
343 UpdateBoundTask (mySet, myBVH, aRghChild, myLevel + 1, &aRghHeight));
347 aRghHeight = BVH::UpdateBounds (mySet, myBVH, aRghChild);
352 set_ref_count (aCount);
353 spawn_and_wait_for_all (aList);
356 typename BVH_Box<T, N>::BVH_VecNt aLftMinPoint = myBVH->MinPointBuffer()[aLftChild];
357 typename BVH_Box<T, N>::BVH_VecNt aLftMaxPoint = myBVH->MaxPointBuffer()[aLftChild];
358 typename BVH_Box<T, N>::BVH_VecNt aRghMinPoint = myBVH->MinPointBuffer()[aRghChild];
359 typename BVH_Box<T, N>::BVH_VecNt aRghMaxPoint = myBVH->MaxPointBuffer()[aRghChild];
361 BVH::BoxMinMax<T, N>::CwiseMin (aLftMinPoint, aRghMinPoint);
362 BVH::BoxMinMax<T, N>::CwiseMax (aLftMaxPoint, aRghMaxPoint);
364 myBVH->MinPointBuffer()[myNode] = aLftMinPoint;
365 myBVH->MaxPointBuffer()[myNode] = aLftMaxPoint;
367 *myHeight = Max (aLftHeight, aRghHeight) + 1;
377 // =======================================================================
380 // =======================================================================
381 template<class T, int N>
382 void BVH_LinearBuilder<T, N>::Build (BVH_Set<T, N>* theSet,
383 BVH_Tree<T, N>* theBVH,
384 const BVH_Box<T, N>& theBox)
386 Standard_STATIC_ASSERT (N == 3 || N == 4);
387 const Standard_Integer aSetSize = theSet->Size();
388 if (theBVH == NULL || aSetSize == 0)
395 const Standard_Integer aDimensionX = 1024;
396 const Standard_Integer aDimensionY = 1024;
397 const Standard_Integer aDimensionZ = 1024;
399 const BVH_VecNt aSceneMin = theBox.CornerMin();
400 const BVH_VecNt aSceneMax = theBox.CornerMax();
402 const T aMinSize = static_cast<T> (BVH::THE_NODE_MIN_SIZE);
404 const T aReverseSizeX = static_cast<T> (aDimensionX) / Max (aMinSize, aSceneMax.x() - aSceneMin.x());
405 const T aReverseSizeY = static_cast<T> (aDimensionY) / Max (aMinSize, aSceneMax.y() - aSceneMin.y());
406 const T aReverseSizeZ = static_cast<T> (aDimensionZ) / Max (aMinSize, aSceneMax.z() - aSceneMin.z());
408 std::vector<BVH_EncodedLink> anEncodedLinks (aSetSize, BVH_EncodedLink());
410 // Step 1 -- Assign Morton code to each primitive
411 for (Standard_Integer aPrimIdx = 0; aPrimIdx < aSetSize; ++aPrimIdx)
413 const BVH_VecNt aCenter = theSet->Box (aPrimIdx).Center();
415 Standard_Integer aVoxelX = BVH::IntFloor ((aCenter.x() - aSceneMin.x()) * aReverseSizeX);
416 Standard_Integer aVoxelY = BVH::IntFloor ((aCenter.y() - aSceneMin.y()) * aReverseSizeY);
417 Standard_Integer aVoxelZ = BVH::IntFloor ((aCenter.z() - aSceneMin.z()) * aReverseSizeZ);
419 aVoxelX = Max (0, Min (aVoxelX, aDimensionX - 1));
420 aVoxelY = Max (0, Min (aVoxelY, aDimensionY - 1));
421 aVoxelZ = Max (0, Min (aVoxelZ, aDimensionZ - 1));
423 aVoxelX = (aVoxelX | (aVoxelX << 16)) & 0x030000FF;
424 aVoxelX = (aVoxelX | (aVoxelX << 8)) & 0x0300F00F;
425 aVoxelX = (aVoxelX | (aVoxelX << 4)) & 0x030C30C3;
426 aVoxelX = (aVoxelX | (aVoxelX << 2)) & 0x09249249;
428 aVoxelY = (aVoxelY | (aVoxelY << 16)) & 0x030000FF;
429 aVoxelY = (aVoxelY | (aVoxelY << 8)) & 0x0300F00F;
430 aVoxelY = (aVoxelY | (aVoxelY << 4)) & 0x030C30C3;
431 aVoxelY = (aVoxelY | (aVoxelY << 2)) & 0x09249249;
433 aVoxelZ = (aVoxelZ | (aVoxelZ << 16)) & 0x030000FF;
434 aVoxelZ = (aVoxelZ | (aVoxelZ << 8)) & 0x0300F00F;
435 aVoxelZ = (aVoxelZ | (aVoxelZ << 4)) & 0x030C30C3;
436 aVoxelZ = (aVoxelZ | (aVoxelZ << 2)) & 0x09249249;
438 anEncodedLinks[aPrimIdx] = BVH_EncodedLink (
439 aVoxelX | (aVoxelY << 1) | (aVoxelZ << 2), aPrimIdx);
442 // Step 2 -- Sort primitives by their Morton codes using radix sort
445 BVH::RadixSortTask& aSortTask = *new ( tbb::task::allocate_root() )
446 BVH::RadixSortTask (anEncodedLinks.begin(), anEncodedLinks.end(), 29);
448 tbb::task::spawn_root_and_wait (aSortTask);
452 BVH::RadixSorter::Perform (anEncodedLinks.begin(), anEncodedLinks.end());
456 // Step 3 -- Emitting BVH hierarchy from sorted Morton codes
457 EmitHierachy (theBVH, 29, 0, anEncodedLinks.begin(), anEncodedLinks.end());
459 NCollection_Array1<Standard_Integer> aLinkMap (0, aSetSize - 1);
460 for (Standard_Integer aLinkIdx = 0; aLinkIdx < aSetSize; ++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 < aSetSize)
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);