3c4e78f2 |
1 | // Created on: 2013-12-20 |
2 | // Created by: Denis BOGOLEPOV |
d5f74e42 |
3 | // Copyright (c) 2013-2014 OPEN CASCADE SAS |
3c4e78f2 |
4 | // |
5 | // This file is part of Open CASCADE Technology software library. |
6 | // |
d5f74e42 |
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 |
3c4e78f2 |
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 | // ======================================================================= |
17 | // function : BVH_BinnedBuilder |
18 | // purpose : |
19 | // ======================================================================= |
20 | template<class T, int N, int Bins> |
21 | BVH_BinnedBuilder<T, N, Bins>::BVH_BinnedBuilder (const Standard_Integer theLeafNodeSize, |
f751596e |
22 | const Standard_Integer theMaxTreeDepth, |
23 | const Standard_Boolean theToUseMainAxis) |
0ef61b50 |
24 | : BVH_QueueBuilder<T, N> (theLeafNodeSize, |
f751596e |
25 | theMaxTreeDepth), |
26 | myUseMainAxis (theToUseMainAxis) |
3c4e78f2 |
27 | { |
28 | // |
29 | } |
30 | |
31 | // ======================================================================= |
32 | // function : ~BVH_BinnedBuilder |
33 | // purpose : |
34 | // ======================================================================= |
35 | template<class T, int N, int Bins> |
36 | BVH_BinnedBuilder<T, N, Bins>::~BVH_BinnedBuilder() |
37 | { |
38 | // |
39 | } |
40 | |
41 | // ======================================================================= |
42 | // function : GetSubVolumes |
43 | // purpose : |
44 | // ======================================================================= |
45 | template<class T, int N, int Bins> |
46 | void BVH_BinnedBuilder<T, N, Bins>::GetSubVolumes (BVH_Set<T, N>* theSet, |
47 | BVH_Tree<T, N>* theBVH, |
48 | const Standard_Integer theNode, |
49 | BVH_BinVector& theBins, |
50 | const Standard_Integer theAxis) |
51 | { |
3a7a7013 |
52 | const T aMin = BVH::VecComp<T, N>::Get (theBVH->MinPoint (theNode), theAxis); |
53 | const T aMax = BVH::VecComp<T, N>::Get (theBVH->MaxPoint (theNode), theAxis); |
3c4e78f2 |
54 | |
3a7a7013 |
55 | const T anInverseStep = static_cast<T> (Bins) / (aMax - aMin); |
3c4e78f2 |
56 | |
57 | for (Standard_Integer anIdx = theBVH->BegPrimitive (theNode); anIdx <= theBVH->EndPrimitive (theNode); ++anIdx) |
58 | { |
59 | typename BVH_Set<T, N>::BVH_BoxNt aBox = theSet->Box (anIdx); |
60 | |
3a7a7013 |
61 | Standard_Integer aBinIndex = BVH::IntFloor<T> ( |
62 | (theSet->Center (anIdx, theAxis) - aMin) * anInverseStep); |
63 | |
3c4e78f2 |
64 | if (aBinIndex < 0) |
65 | { |
66 | aBinIndex = 0; |
67 | } |
68 | else if (aBinIndex >= Bins) |
69 | { |
70 | aBinIndex = Bins - 1; |
71 | } |
72 | |
73 | theBins[aBinIndex].Count++; |
74 | theBins[aBinIndex].Box.Combine (aBox); |
75 | } |
76 | } |
77 | |
3a7a7013 |
78 | namespace BVH |
3c4e78f2 |
79 | { |
80 | // ======================================================================= |
81 | // function : SplitPrimitives |
82 | // purpose : |
83 | // ======================================================================= |
84 | template<class T, int N> |
85 | Standard_Integer SplitPrimitives (BVH_Set<T, N>* theSet, |
86 | const BVH_Box<T, N>& theBox, |
87 | const Standard_Integer theBeg, |
88 | const Standard_Integer theEnd, |
89 | const Standard_Integer theBin, |
90 | const Standard_Integer theAxis, |
91 | const Standard_Integer theBins) |
92 | { |
3a7a7013 |
93 | const T aMin = BVH::VecComp<T, N>::Get (theBox.CornerMin(), theAxis); |
94 | const T aMax = BVH::VecComp<T, N>::Get (theBox.CornerMax(), theAxis); |
3c4e78f2 |
95 | |
3a7a7013 |
96 | const T anInverseStep = static_cast<T> (theBins) / (aMax - aMin); |
3c4e78f2 |
97 | |
98 | Standard_Integer aLftIdx (theBeg); |
99 | Standard_Integer aRghIdx (theEnd); |
100 | |
101 | do |
102 | { |
3a7a7013 |
103 | while (BVH::IntFloor<T> ((theSet->Center (aLftIdx, theAxis) - aMin) * anInverseStep) <= theBin && aLftIdx < theEnd) |
3c4e78f2 |
104 | { |
105 | ++aLftIdx; |
106 | } |
3a7a7013 |
107 | while (BVH::IntFloor<T> ((theSet->Center (aRghIdx, theAxis) - aMin) * anInverseStep) > theBin && aRghIdx > theBeg) |
3c4e78f2 |
108 | { |
109 | --aRghIdx; |
110 | } |
111 | |
112 | if (aLftIdx <= aRghIdx) |
113 | { |
114 | if (aLftIdx != aRghIdx) |
115 | { |
116 | theSet->Swap (aLftIdx, aRghIdx); |
117 | } |
118 | |
119 | ++aLftIdx; |
120 | --aRghIdx; |
121 | } |
122 | } while (aLftIdx <= aRghIdx); |
123 | |
124 | return aLftIdx; |
125 | } |
126 | } |
127 | |
128 | #if defined (_WIN32) && defined (max) |
129 | #undef max |
130 | #endif |
131 | |
132 | #include <limits> |
133 | |
f751596e |
134 | namespace BVH |
135 | { |
136 | template<class T, int N> |
137 | struct BVH_AxisSelector |
138 | { |
139 | typedef typename BVH::VectorType<T, N>::Type BVH_VecNt; |
140 | |
141 | // ======================================================================= |
142 | // function : MainAxis |
143 | // purpose : |
144 | // ======================================================================= |
145 | static Standard_Integer MainAxis (const BVH_VecNt& theSize) |
146 | { |
147 | if (theSize.y() > theSize.x()) |
148 | { |
149 | return theSize.y() > theSize.z() ? 1 : 2; |
150 | } |
151 | else |
152 | { |
153 | return theSize.z() > theSize.x() ? 2 : 0; |
154 | } |
155 | } |
156 | }; |
157 | |
158 | template<class T> |
159 | struct BVH_AxisSelector<T, 2> |
160 | { |
161 | typedef typename BVH::VectorType<T, 2>::Type BVH_VecNt; |
162 | |
163 | // ======================================================================= |
164 | // function : MainAxis |
165 | // purpose : |
166 | // ======================================================================= |
167 | static Standard_Integer MainAxis (const BVH_VecNt& theSize) |
168 | { |
169 | return theSize.x() > theSize.y() ? 0 : 1; |
170 | } |
171 | }; |
172 | } |
173 | |
3c4e78f2 |
174 | // ======================================================================= |
175 | // function : BuildNode |
176 | // purpose : |
177 | // ======================================================================= |
178 | template<class T, int N, int Bins> |
179 | void BVH_BinnedBuilder<T, N, Bins>::BuildNode (BVH_Set<T, N>* theSet, |
180 | BVH_Tree<T, N>* theBVH, |
181 | const Standard_Integer theNode) |
182 | { |
228de226 |
183 | const Standard_Integer aNodeBegPrimitive = theBVH->BegPrimitive (theNode); |
184 | const Standard_Integer aNodeEndPrimitive = theBVH->EndPrimitive (theNode); |
3c4e78f2 |
185 | |
228de226 |
186 | if (aNodeEndPrimitive - aNodeBegPrimitive < BVH_Builder<T, N>::myLeafNodeSize) |
187 | { |
188 | return; // node does not require partitioning |
189 | } |
190 | |
191 | const BVH_Box<T, N> anAABB (theBVH->MinPoint (theNode), |
192 | theBVH->MaxPoint (theNode)); |
193 | |
194 | const typename BVH_Box<T, N>::BVH_VecNt aSize = anAABB.Size(); |
3c4e78f2 |
195 | |
196 | // Parameters for storing best split |
197 | Standard_Integer aMinSplitAxis = -1; |
198 | Standard_Integer aMinSplitIndex = 0; |
199 | Standard_Integer aMinSplitNumLft = 0; |
200 | Standard_Integer aMinSplitNumRgh = 0; |
201 | |
202 | BVH_Box<T, N> aMinSplitBoxLft; |
203 | BVH_Box<T, N> aMinSplitBoxRgh; |
204 | |
205 | Standard_Real aMinSplitCost = std::numeric_limits<Standard_Real>::max(); |
206 | |
f751596e |
207 | Standard_Integer aMainAxis = BVH::BVH_AxisSelector<T, N>::MainAxis (aSize); |
208 | |
3c4e78f2 |
209 | // Find best split |
f751596e |
210 | for (Standard_Integer anAxis = myUseMainAxis ? aMainAxis : 0; anAxis <= (myUseMainAxis ? aMainAxis : Min (N - 1, 2)); ++anAxis) |
3c4e78f2 |
211 | { |
679d3878 |
212 | if (BVH::VecComp<T, N>::Get (aSize, anAxis) <= BVH::THE_NODE_MIN_SIZE) |
3c4e78f2 |
213 | continue; |
214 | |
215 | BVH_BinVector aBins; |
216 | GetSubVolumes (theSet, theBVH, theNode, aBins, anAxis); |
217 | |
218 | // Choose the best split (with minimum SAH cost) |
219 | for (Standard_Integer aSplit = 1; aSplit < Bins; ++aSplit) |
220 | { |
221 | Standard_Integer aLftCount = 0; |
222 | Standard_Integer aRghCount = 0; |
223 | |
224 | BVH_Box<T, N> aLftAABB; |
225 | BVH_Box<T, N> aRghAABB; |
226 | |
227 | for (Standard_Integer anIndex = 0; anIndex < aSplit; ++anIndex) |
228 | { |
229 | aLftCount += aBins[anIndex].Count; |
230 | aLftAABB.Combine (aBins[anIndex].Box); |
231 | } |
232 | |
233 | for (Standard_Integer anIndex = aSplit; anIndex < Bins; ++anIndex) |
234 | { |
235 | aRghCount += aBins[anIndex].Count; |
236 | aRghAABB.Combine (aBins[anIndex].Box); |
237 | } |
238 | |
239 | // Simple SAH evaluation |
240 | Standard_Real aCost = (static_cast<Standard_Real> (aLftAABB.Area()) /* / aNodeArea */) * aLftCount |
241 | + (static_cast<Standard_Real> (aRghAABB.Area()) /* / aNodeArea */) * aRghCount; |
242 | |
243 | if (aCost <= aMinSplitCost) |
244 | { |
245 | aMinSplitCost = aCost; |
246 | aMinSplitAxis = anAxis; |
247 | aMinSplitIndex = aSplit; |
248 | aMinSplitBoxLft = aLftAABB; |
249 | aMinSplitBoxRgh = aRghAABB; |
250 | aMinSplitNumLft = aLftCount; |
251 | aMinSplitNumRgh = aRghCount; |
252 | } |
253 | } |
254 | } |
255 | |
3c4e78f2 |
256 | theBVH->SetInner (theNode); |
257 | |
228de226 |
258 | Standard_Integer aMiddle = -1; |
259 | |
61aa7f3d |
260 | if (aMinSplitNumLft == 0 || aMinSplitNumRgh == 0 || aMinSplitAxis == -1) // case of objects with the same center |
228de226 |
261 | { |
262 | aMinSplitBoxLft.Clear(); |
263 | aMinSplitBoxRgh.Clear(); |
264 | |
265 | aMiddle = std::max (aNodeBegPrimitive + 1, |
266 | static_cast<Standard_Integer> ((aNodeBegPrimitive + aNodeEndPrimitive) / 2.f)); |
267 | |
268 | aMinSplitNumLft = aMiddle - aNodeBegPrimitive; |
269 | |
270 | for (Standard_Integer anIndex = aNodeBegPrimitive; anIndex < aMiddle; ++anIndex) |
271 | { |
272 | aMinSplitBoxLft.Combine (theSet->Box (anIndex)); |
273 | } |
274 | |
275 | aMinSplitNumRgh = aNodeEndPrimitive - aMiddle + 1; |
276 | |
277 | for (Standard_Integer anIndex = aNodeEndPrimitive; anIndex >= aMiddle; --anIndex) |
278 | { |
279 | aMinSplitBoxRgh.Combine (theSet->Box (anIndex)); |
280 | } |
281 | } |
282 | else |
283 | { |
3a7a7013 |
284 | aMiddle = BVH::SplitPrimitives<T, N> (theSet, anAABB, |
228de226 |
285 | aNodeBegPrimitive, aNodeEndPrimitive, aMinSplitIndex - 1, aMinSplitAxis, Bins); |
286 | } |
3c4e78f2 |
287 | |
288 | static const Standard_Integer aLftNode = 1; |
289 | static const Standard_Integer aRghNode = 2; |
290 | |
291 | // Setting up tasks for child nodes |
292 | for (Standard_Integer aSide = aLftNode; aSide <= aRghNode; ++aSide) |
293 | { |
294 | typename BVH_Box<T, N>::BVH_VecNt aMinPoint = (aSide == aLftNode) |
295 | ? aMinSplitBoxLft.CornerMin() |
296 | : aMinSplitBoxRgh.CornerMin(); |
297 | typename BVH_Box<T, N>::BVH_VecNt aMaxPoint = (aSide == aLftNode) |
298 | ? aMinSplitBoxLft.CornerMax() |
299 | : aMinSplitBoxRgh.CornerMax(); |
300 | |
301 | Standard_Integer aBegPrimitive = (aSide == aLftNode) |
228de226 |
302 | ? aNodeBegPrimitive |
3c4e78f2 |
303 | : aMiddle; |
304 | Standard_Integer aEndPrimitive = (aSide == aLftNode) |
305 | ? aMiddle - 1 |
228de226 |
306 | : aNodeEndPrimitive; |
3c4e78f2 |
307 | |
308 | Standard_Integer aChildIndex = theBVH->AddLeafNode (aMinPoint, aMaxPoint, aBegPrimitive, aEndPrimitive); |
309 | |
310 | theBVH->Level (aChildIndex) = theBVH->Level (theNode) + 1; |
311 | |
312 | // Check to see if child node must be split |
313 | const Standard_Integer aNbPimitives = (aSide == aLftNode) |
314 | ? aMinSplitNumLft |
315 | : aMinSplitNumRgh; |
316 | |
317 | if (aSide == aLftNode) |
318 | theBVH->LeftChild (theNode) = aChildIndex; |
319 | else |
320 | theBVH->RightChild (theNode) = aChildIndex; |
321 | |
322 | const Standard_Boolean isLeaf = aNbPimitives <= BVH_Builder<T, N>::myLeafNodeSize |
323 | || theBVH->Level (aChildIndex) >= BVH_Builder<T, N>::myMaxTreeDepth; |
324 | |
325 | if (!isLeaf) |
fc73a202 |
326 | { |
0ef61b50 |
327 | BVH_QueueBuilder<T, N>::myTasksQueue.Append (aChildIndex); |
fc73a202 |
328 | } |
329 | |
330 | BVH_Builder<T, N>::UpdateDepth (theBVH, theBVH->Level (aChildIndex)); |
3c4e78f2 |
331 | } |
332 | } |