1 // Created on: 2016-04-13
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013-2016 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15 
16 #ifndef _BVH_QuickSorter_Header
17 #define _BVH_QuickSorter_Header
18 
19 #include <BVH_Sorter.hxx>
20 
21 //! Performs centroid-based sorting of abstract set along
22 //! the given axis (X - 0, Y - 1, Z - 2) using quick sort.
23 template<class T, int N>
24 class BVH_QuickSorter : public BVH_Sorter<T, N>
25 {
26 public:
27 
28   //! Creates new BVH quick sorter for the given axis.
BVH_QuickSorter(const Standard_Integer theAxis=0)29   BVH_QuickSorter (const Standard_Integer theAxis = 0) : myAxis (theAxis) { }
30 
31   //! Sorts the set.
Perform(BVH_Set<T,N> * theSet)32   virtual void Perform (BVH_Set<T, N>* theSet) Standard_OVERRIDE
33   {
34     Perform (theSet, 0, theSet->Size() - 1);
35   }
36 
37   //! Sorts the given (inclusive) range in the set.
Perform(BVH_Set<T,N> * theSet,const Standard_Integer theStart,const Standard_Integer theFinal)38   virtual void Perform (BVH_Set<T, N>* theSet, const Standard_Integer theStart, const Standard_Integer theFinal) Standard_OVERRIDE
39   {
40     Standard_Integer aLft = theStart;
41     Standard_Integer aRgh = theFinal;
42 
43     T aPivot = theSet->Center ((aRgh + aLft) / 2, myAxis);
44     while (aLft < aRgh)
45     {
46       while (theSet->Center (aLft, myAxis) < aPivot && aLft < theFinal)
47       {
48         ++aLft;
49       }
50 
51       while (theSet->Center (aRgh, myAxis) > aPivot && aRgh > theStart)
52       {
53         --aRgh;
54       }
55 
56       if (aLft <= aRgh)
57       {
58         if (aLft != aRgh)
59         {
60           theSet->Swap (aLft, aRgh);
61         }
62         ++aLft;
63         --aRgh;
64       }
65     }
66 
67     if (aRgh > theStart)
68     {
69       Perform (theSet, theStart, aRgh);
70     }
71 
72     if (aLft < theFinal)
73     {
74       Perform (theSet, aLft, theFinal);
75     }
76   }
77 
78 protected:
79 
80   //! Axis used to arrange the primitives (X - 0, Y - 1, Z - 2).
81   Standard_Integer myAxis;
82 
83 };
84 
85 #endif // _BVH_QuickSorter_Header
86