1 | #include <stack>
|
---|
2 | #include <time.h>
|
---|
3 | #include <iomanip>
|
---|
4 |
|
---|
5 | #include "BvHierarchy.h"
|
---|
6 | #include "ViewCell.h"
|
---|
7 | #include "Plane3.h"
|
---|
8 | #include "Mesh.h"
|
---|
9 | #include "common.h"
|
---|
10 | #include "Environment.h"
|
---|
11 | #include "Polygon3.h"
|
---|
12 | #include "Ray.h"
|
---|
13 | #include "AxisAlignedBox3.h"
|
---|
14 | #include "Exporter.h"
|
---|
15 | #include "Plane3.h"
|
---|
16 | #include "ViewCellsManager.h"
|
---|
17 | #include "Beam.h"
|
---|
18 | #include "VspTree.h"
|
---|
19 |
|
---|
20 |
|
---|
21 | namespace GtpVisibilityPreprocessor {
|
---|
22 |
|
---|
23 |
|
---|
24 | #define USE_FIXEDPOINT_T 0
|
---|
25 |
|
---|
26 | static float debugVol = 0;
|
---|
27 |
|
---|
28 | int BvhNode::sMailId = 10000;//2147483647;
|
---|
29 | int BvhNode::sReservedMailboxes = 1;
|
---|
30 |
|
---|
31 | BvHierarchy *BvHierarchy::BvhSubdivisionCandidate::sBvHierarchy = NULL;
|
---|
32 |
|
---|
33 |
|
---|
34 | inline static bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
35 | {
|
---|
36 | return obj1->mId < obj2->mId;
|
---|
37 | }
|
---|
38 |
|
---|
39 |
|
---|
40 | /***************************************************************/
|
---|
41 | /* class BvhNode implementation */
|
---|
42 | /***************************************************************/
|
---|
43 |
|
---|
44 | BvhNode::BvhNode(): mParent(NULL), mMailbox(0)
|
---|
45 | {
|
---|
46 | }
|
---|
47 |
|
---|
48 | BvhNode::BvhNode(const AxisAlignedBox3 &bbox):
|
---|
49 | mParent(NULL), mBoundingBox(bbox), mMailbox(0)
|
---|
50 | {
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 | BvhNode::BvhNode(const AxisAlignedBox3 &bbox, BvhInterior *parent):
|
---|
55 | mBoundingBox(bbox), mParent(parent), mMailbox(0)
|
---|
56 | {
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | bool BvhNode::IsRoot() const
|
---|
61 | {
|
---|
62 | return mParent == NULL;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | BvhInterior *BvhNode::GetParent()
|
---|
67 | {
|
---|
68 | return mParent;
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | void BvhNode::SetParent(BvhInterior *parent)
|
---|
73 | {
|
---|
74 | mParent = parent;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 |
|
---|
79 | /******************************************************************/
|
---|
80 | /* class BvhInterior implementation */
|
---|
81 | /******************************************************************/
|
---|
82 |
|
---|
83 |
|
---|
84 | BvhLeaf::BvhLeaf(const AxisAlignedBox3 &bbox):
|
---|
85 | BvhNode(bbox), mSubdivisionCandidate(NULL)
|
---|
86 | {
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | BvhLeaf::BvhLeaf(const AxisAlignedBox3 &bbox, BvhInterior *parent):
|
---|
91 | BvhNode(bbox, parent)
|
---|
92 | {
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | BvhLeaf::BvhLeaf(const AxisAlignedBox3 &bbox,
|
---|
97 | BvhInterior *parent,
|
---|
98 | const int numObjects):
|
---|
99 | BvhNode(bbox, parent)
|
---|
100 | {
|
---|
101 | mObjects.reserve(numObjects);
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | bool BvhLeaf::IsLeaf() const
|
---|
106 | {
|
---|
107 | return true;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | BvhLeaf::~BvhLeaf()
|
---|
112 | {
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | /******************************************************************/
|
---|
117 | /* class BvhInterior implementation */
|
---|
118 | /******************************************************************/
|
---|
119 |
|
---|
120 |
|
---|
121 | BvhInterior::BvhInterior(const AxisAlignedBox3 &bbox):
|
---|
122 | BvhNode(bbox), mFront(NULL), mBack(NULL)
|
---|
123 | {
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | BvhInterior::BvhInterior(const AxisAlignedBox3 &bbox, BvhInterior *parent):
|
---|
128 | BvhNode(bbox, parent), mFront(NULL), mBack(NULL)
|
---|
129 | {
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | void BvhInterior::ReplaceChildLink(BvhNode *oldChild, BvhNode *newChild)
|
---|
134 | {
|
---|
135 | if (mBack == oldChild)
|
---|
136 | mBack = newChild;
|
---|
137 | else
|
---|
138 | mFront = newChild;
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | bool BvhInterior::IsLeaf() const
|
---|
143 | {
|
---|
144 | return false;
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | BvhInterior::~BvhInterior()
|
---|
149 | {
|
---|
150 | DEL_PTR(mFront);
|
---|
151 | DEL_PTR(mBack);
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | void BvhInterior::SetupChildLinks(BvhNode *front, BvhNode *back)
|
---|
156 | {
|
---|
157 | mBack = back;
|
---|
158 | mFront = front;
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 |
|
---|
163 | /*******************************************************************/
|
---|
164 | /* class BvHierarchy implementation */
|
---|
165 | /*******************************************************************/
|
---|
166 |
|
---|
167 |
|
---|
168 | BvHierarchy::BvHierarchy():
|
---|
169 | mRoot(NULL),
|
---|
170 | mTimeStamp(1)
|
---|
171 | {
|
---|
172 | ReadEnvironment();
|
---|
173 | mSubdivisionCandidates = new vector<SortableEntry>;
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | BvHierarchy::~BvHierarchy()
|
---|
178 | {
|
---|
179 | // delete kd intersectables
|
---|
180 | BvhIntersectableMap::iterator it, it_end = mBvhIntersectables.end();
|
---|
181 |
|
---|
182 | for (it = mBvhIntersectables.begin(); it != mBvhIntersectables.end(); ++ it)
|
---|
183 | {
|
---|
184 | DEL_PTR((*it).second);
|
---|
185 | }
|
---|
186 |
|
---|
187 | DEL_PTR(mSubdivisionCandidates);
|
---|
188 |
|
---|
189 | mSubdivisionStats.close();
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | void BvHierarchy::ReadEnvironment()
|
---|
194 | {
|
---|
195 | bool randomize = false;
|
---|
196 | Environment::GetSingleton()->GetBoolValue("VspTree.Construction.randomize", randomize);
|
---|
197 | if (randomize)
|
---|
198 | Randomize(); // initialise random generator for heuristics
|
---|
199 |
|
---|
200 | //-- termination criteria for autopartition
|
---|
201 | Environment::GetSingleton()->GetIntValue("BvHierarchy.Termination.maxDepth", mTermMaxDepth);
|
---|
202 | Environment::GetSingleton()->GetIntValue("BvHierarchy.Termination.maxLeaves", mTermMaxLeaves);
|
---|
203 | Environment::GetSingleton()->GetIntValue("BvHierarchy.Termination.minObjects", mTermMinObjects);
|
---|
204 | Environment::GetSingleton()->GetFloatValue("BvHierarchy.Termination.minProbability", mTermMinProbability);
|
---|
205 |
|
---|
206 | Environment::GetSingleton()->GetIntValue("BvHierarchy.Termination.missTolerance", mTermMissTolerance);
|
---|
207 |
|
---|
208 | //-- max cost ratio for early tree termination
|
---|
209 | Environment::GetSingleton()->GetFloatValue("BvHierarchy.Termination.maxCostRatio", mTermMaxCostRatio);
|
---|
210 |
|
---|
211 | Environment::GetSingleton()->GetFloatValue("BvHierarchy.Termination.minGlobalCostRatio",
|
---|
212 | mTermMinGlobalCostRatio);
|
---|
213 | Environment::GetSingleton()->GetIntValue("BvHierarchy.Termination.globalCostMissTolerance",
|
---|
214 | mTermGlobalCostMissTolerance);
|
---|
215 |
|
---|
216 | //-- factors for bsp tree split plane heuristics
|
---|
217 |
|
---|
218 | // if only the driving axis is used for axis aligned split
|
---|
219 | Environment::GetSingleton()->GetBoolValue("BvHierarchy.splitUseOnlyDrivingAxis", mOnlyDrivingAxis);
|
---|
220 | Environment::GetSingleton()->GetFloatValue("BvHierarchy.maxStaticMemory", mMaxMemory);
|
---|
221 | Environment::GetSingleton()->GetBoolValue("BvHierarchy.useCostHeuristics", mUseCostHeuristics);
|
---|
222 |
|
---|
223 | char subdivisionStatsLog[100];
|
---|
224 | Environment::GetSingleton()->GetStringValue("BvHierarchy.subdivisionStats", subdivisionStatsLog);
|
---|
225 | mSubdivisionStats.open(subdivisionStatsLog);
|
---|
226 |
|
---|
227 | Environment::GetSingleton()->GetFloatValue(
|
---|
228 | "BvHierarchy.Construction.renderCostDecreaseWeight", mRenderCostDecreaseWeight);
|
---|
229 |
|
---|
230 |
|
---|
231 | //-- debug output
|
---|
232 |
|
---|
233 | Debug << "******* Bvh hierarchy options ******** " << endl;
|
---|
234 | Debug << "max depth: " << mTermMaxDepth << endl;
|
---|
235 | Debug << "min probabiliy: " << mTermMinProbability<< endl;
|
---|
236 | Debug << "min objects: " << mTermMinObjects << endl;
|
---|
237 | Debug << "max cost ratio: " << mTermMaxCostRatio << endl;
|
---|
238 | Debug << "miss tolerance: " << mTermMissTolerance << endl;
|
---|
239 | Debug << "max leaves: " << mTermMaxLeaves << endl;
|
---|
240 | Debug << "randomize: " << randomize << endl;
|
---|
241 | Debug << "min global cost ratio: " << mTermMinGlobalCostRatio << endl;
|
---|
242 | Debug << "global cost miss tolerance: " << mTermGlobalCostMissTolerance << endl;
|
---|
243 | Debug << "only driving axis: " << mOnlyDrivingAxis << endl;
|
---|
244 | Debug << "max memory: " << mMaxMemory << endl;
|
---|
245 | Debug << "use cost heuristics: " << mUseCostHeuristics << endl;
|
---|
246 | Debug << "subdivision stats log: " << subdivisionStatsLog << endl;
|
---|
247 |
|
---|
248 | Debug << "split borders: " << mSplitBorder << endl;
|
---|
249 | Debug << "render cost decrease weight: " << mRenderCostDecreaseWeight << endl;
|
---|
250 | Debug << endl;
|
---|
251 | }
|
---|
252 |
|
---|
253 |
|
---|
254 | void AssociateObjectsWithLeaf(BvhLeaf *leaf)
|
---|
255 | {
|
---|
256 | ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
|
---|
257 | for (oit = leaf->mObjects.begin(); oit != oit_end; ++ oit)
|
---|
258 | {
|
---|
259 | (*oit)->mBvhLeaf = leaf;
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 |
|
---|
264 | BvhInterior *BvHierarchy::SubdivideNode(const ObjectContainer &frontObjects,
|
---|
265 | const ObjectContainer &backObjects,
|
---|
266 | const BvhTraversalData &tData,
|
---|
267 | BvhTraversalData &frontData,
|
---|
268 | BvhTraversalData &backData)
|
---|
269 | {
|
---|
270 | BvhLeaf *leaf = tData.mNode;
|
---|
271 |
|
---|
272 | // two new leaves
|
---|
273 | mBvhStats.nodes += 2;
|
---|
274 |
|
---|
275 | // add the new nodes to the tree
|
---|
276 | BvhInterior *node = new BvhInterior(tData.mBoundingBox, leaf->GetParent());
|
---|
277 |
|
---|
278 |
|
---|
279 | //-- the front and back traversal data is filled with the new values
|
---|
280 |
|
---|
281 | frontData.mDepth = backData.mDepth = tData.mDepth + 1;
|
---|
282 |
|
---|
283 | frontData.mBoundingBox = ComputeBoundingBox(frontObjects, &tData.mBoundingBox);
|
---|
284 | backData.mBoundingBox = ComputeBoundingBox(backObjects, &tData.mBoundingBox);
|
---|
285 |
|
---|
286 | /////////////
|
---|
287 | //-- create front and back leaf
|
---|
288 |
|
---|
289 | BvhLeaf *back =
|
---|
290 | new BvhLeaf(backData.mBoundingBox, node, (int)backObjects.size());
|
---|
291 | BvhLeaf *front =
|
---|
292 | new BvhLeaf(frontData.mBoundingBox, node, (int)frontObjects.size());
|
---|
293 |
|
---|
294 | BvhInterior *parent = leaf->GetParent();
|
---|
295 |
|
---|
296 |
|
---|
297 | // replace a link from node's parent
|
---|
298 | if (parent)
|
---|
299 | {
|
---|
300 | parent->ReplaceChildLink(leaf, node);
|
---|
301 | node->SetParent(parent);
|
---|
302 | }
|
---|
303 | else // new root
|
---|
304 | {
|
---|
305 | mRoot = node;
|
---|
306 | }
|
---|
307 |
|
---|
308 | // and setup child links
|
---|
309 | node->SetupChildLinks(front, back);
|
---|
310 |
|
---|
311 | ++ mBvhStats.splits;
|
---|
312 |
|
---|
313 |
|
---|
314 | ////////////////////////////////////
|
---|
315 |
|
---|
316 | frontData.mNode = front;
|
---|
317 | backData.mNode = back;
|
---|
318 |
|
---|
319 | back->mObjects = backObjects;
|
---|
320 | front->mObjects = frontObjects;
|
---|
321 |
|
---|
322 | AssociateObjectsWithLeaf(back);
|
---|
323 | AssociateObjectsWithLeaf(front);
|
---|
324 |
|
---|
325 | // compute probability, i.e., volume of seen view cells
|
---|
326 | frontData.mProbability = EvalViewCellsVolume(frontObjects);
|
---|
327 | backData.mProbability = EvalViewCellsVolume(backObjects);
|
---|
328 |
|
---|
329 | return node;
|
---|
330 | }
|
---|
331 |
|
---|
332 |
|
---|
333 | BvhNode *BvHierarchy::Subdivide(SplitQueue &tQueue,
|
---|
334 | SubdivisionCandidate *splitCandidate,
|
---|
335 | const bool globalCriteriaMet)
|
---|
336 | {
|
---|
337 | BvhSubdivisionCandidate *sc =
|
---|
338 | dynamic_cast<BvhSubdivisionCandidate *>(splitCandidate);
|
---|
339 | BvhTraversalData &tData = sc->mParentData;
|
---|
340 |
|
---|
341 | BvhNode *newNode = tData.mNode;
|
---|
342 |
|
---|
343 | if (!LocalTerminationCriteriaMet(tData) && !globalCriteriaMet)
|
---|
344 | {
|
---|
345 | //-- continue subdivision
|
---|
346 |
|
---|
347 | BvhTraversalData tFrontData;
|
---|
348 | BvhTraversalData tBackData;
|
---|
349 |
|
---|
350 | // create new interior node and two leaf node
|
---|
351 | newNode = SubdivideNode(sc->mFrontObjects,
|
---|
352 | sc->mBackObjects,
|
---|
353 | tData,
|
---|
354 | tFrontData,
|
---|
355 | tBackData);
|
---|
356 |
|
---|
357 | const int maxCostMisses = sc->mMaxCostMisses;
|
---|
358 |
|
---|
359 | // how often was max cost ratio missed in this branch?
|
---|
360 | tFrontData.mMaxCostMisses = maxCostMisses;
|
---|
361 | tBackData.mMaxCostMisses = maxCostMisses;
|
---|
362 |
|
---|
363 | // decrease the weighted average cost of the subdivisoin
|
---|
364 | mTotalCost -= sc->GetRenderCostDecrease();
|
---|
365 |
|
---|
366 | // subdivision statistics
|
---|
367 | if (1) PrintSubdivisionStats(*sc);
|
---|
368 |
|
---|
369 | //-- push the new split candidates on the queue
|
---|
370 |
|
---|
371 | BvhSubdivisionCandidate *frontCandidate =
|
---|
372 | new BvhSubdivisionCandidate(tFrontData);
|
---|
373 | BvhSubdivisionCandidate *backCandidate =
|
---|
374 | new BvhSubdivisionCandidate(tBackData);
|
---|
375 |
|
---|
376 | EvalSubdivisionCandidate(*frontCandidate);
|
---|
377 | EvalSubdivisionCandidate(*backCandidate);
|
---|
378 |
|
---|
379 | // cross reference
|
---|
380 | tFrontData.mNode->SetSubdivisionCandidate(frontCandidate);
|
---|
381 | tBackData.mNode->SetSubdivisionCandidate(backCandidate);
|
---|
382 |
|
---|
383 | Debug << "leaf: " << tFrontData.mNode << " setting f candidate: " << tFrontData.mNode->GetSubdivisionCandidate() << " type: " << tFrontData.mNode->GetSubdivisionCandidate()->Type() << endl;
|
---|
384 | Debug << "leaf: " << tBackData.mNode << " setting b candidate: " << tBackData.mNode->GetSubdivisionCandidate() << " type: " << tBackData.mNode->GetSubdivisionCandidate()->Type() << endl;
|
---|
385 |
|
---|
386 | tQueue.Push(frontCandidate);
|
---|
387 | tQueue.Push(backCandidate);
|
---|
388 |
|
---|
389 | // delete old leaf node
|
---|
390 | //DEL_PTR(tData.mNode);
|
---|
391 | }
|
---|
392 |
|
---|
393 | //-- terminate traversal
|
---|
394 |
|
---|
395 | if (newNode->IsLeaf())
|
---|
396 | {
|
---|
397 | //-- store additional info
|
---|
398 |
|
---|
399 | EvaluateLeafStats(tData);
|
---|
400 |
|
---|
401 | const bool mStoreRays = true;
|
---|
402 | if (mStoreRays)
|
---|
403 | {
|
---|
404 | BvhLeaf *leaf = dynamic_cast<BvhLeaf *>(newNode);
|
---|
405 | CollectRays(leaf->mObjects, leaf->mVssRays);
|
---|
406 | }
|
---|
407 |
|
---|
408 | // detach subdivision candidate: this leaf is no candidate for
|
---|
409 | // splitting anymore
|
---|
410 | tData.mNode->SetSubdivisionCandidate(NULL);
|
---|
411 | // detach node so it won't get deleted
|
---|
412 | tData.mNode = NULL;
|
---|
413 | }
|
---|
414 |
|
---|
415 | return newNode;
|
---|
416 | }
|
---|
417 |
|
---|
418 |
|
---|
419 | void BvHierarchy::EvalSubdivisionCandidate(BvhSubdivisionCandidate &splitCandidate)
|
---|
420 | {
|
---|
421 | // compute best object partition
|
---|
422 | const float ratio = SelectObjectPartition(
|
---|
423 | splitCandidate.mParentData,
|
---|
424 | splitCandidate.mFrontObjects,
|
---|
425 | splitCandidate.mBackObjects);
|
---|
426 |
|
---|
427 | BvhLeaf *leaf = splitCandidate.mParentData.mNode;
|
---|
428 |
|
---|
429 | // cost ratio violated?
|
---|
430 | const bool maxCostRatioViolated = mTermMaxCostRatio < ratio;
|
---|
431 |
|
---|
432 | splitCandidate.mMaxCostMisses = maxCostRatioViolated ?
|
---|
433 | splitCandidate.mParentData.mMaxCostMisses + 1 :
|
---|
434 | splitCandidate.mParentData.mMaxCostMisses;
|
---|
435 |
|
---|
436 | const float viewSpaceVol = mVspTree->GetBoundingBox().GetVolume();
|
---|
437 | const float oldProp = EvalViewCellsVolume(leaf->mObjects);
|
---|
438 | //const float oldProp2 = splitCandidate.mParentData.mProbability; Debug << "here8 " << (oldProp - oldProp2) / viewSpaceVol << " " << oldProp / viewSpaceVol << " " << oldProp2 / viewSpaceVol << endl;
|
---|
439 |
|
---|
440 | const float oldRenderCost = oldProp * (float)leaf->mObjects.size() / viewSpaceVol;
|
---|
441 |
|
---|
442 | // compute global decrease in render cost
|
---|
443 | float newRenderCost = EvalRenderCost(splitCandidate.mParentData,
|
---|
444 | splitCandidate.mFrontObjects,
|
---|
445 | splitCandidate.mBackObjects);
|
---|
446 |
|
---|
447 | newRenderCost /= viewSpaceVol;
|
---|
448 | const float renderCostDecr = oldRenderCost - newRenderCost;
|
---|
449 |
|
---|
450 | Debug << "\nbvh render cost decr: " << renderCostDecr << endl;
|
---|
451 | splitCandidate.SetRenderCostDecrease(renderCostDecr);
|
---|
452 |
|
---|
453 | #if 0
|
---|
454 | const float priority = (float)-splitCandidate.mParentData.mDepth;
|
---|
455 | #else
|
---|
456 | // take render cost of node into account
|
---|
457 | // otherwise danger of being stuck in a local minimum!!
|
---|
458 | const float factor = mRenderCostDecreaseWeight;
|
---|
459 | const float priority = factor * renderCostDecr + (1.0f - factor) * oldRenderCost;
|
---|
460 | #endif
|
---|
461 |
|
---|
462 | // compute global decrease in render cost
|
---|
463 | splitCandidate.SetPriority(priority);
|
---|
464 | }
|
---|
465 |
|
---|
466 |
|
---|
467 | inline bool BvHierarchy::LocalTerminationCriteriaMet(const BvhTraversalData &data) const
|
---|
468 | {
|
---|
469 | // matt: TODO
|
---|
470 | return ( 0
|
---|
471 | //|| ((int)data.mNode->mObjects.size() < mTermMinObjects)
|
---|
472 | //|| (data.mProbability <= mTermMinProbability)
|
---|
473 | //|| (data.mDepth >= mTermMaxDepth)
|
---|
474 | );
|
---|
475 | }
|
---|
476 |
|
---|
477 |
|
---|
478 | inline bool BvHierarchy::GlobalTerminationCriteriaMet(const BvhTraversalData &data) const
|
---|
479 | {
|
---|
480 | // matt: TODO
|
---|
481 | return (0
|
---|
482 | || (mBvhStats.Leaves() >= mTermMaxLeaves)
|
---|
483 | //|| (mGlobalCostMisses >= mTermGlobalCostMissTolerance)
|
---|
484 | //|| mOutOfMemory
|
---|
485 | );
|
---|
486 | }
|
---|
487 |
|
---|
488 |
|
---|
489 | void BvHierarchy::EvaluateLeafStats(const BvhTraversalData &data)
|
---|
490 | {
|
---|
491 | // the node became a leaf -> evaluate stats for leafs
|
---|
492 | BvhLeaf *leaf = data.mNode;
|
---|
493 |
|
---|
494 | ++ mCreatedLeaves;
|
---|
495 |
|
---|
496 | if (data.mDepth >= mTermMaxDepth)
|
---|
497 | {
|
---|
498 | ++ mBvhStats.maxDepthNodes;
|
---|
499 | //Debug << "new max depth: " << mVspStats.maxDepthNodes << endl;
|
---|
500 | }
|
---|
501 |
|
---|
502 | if (data.mDepth < mTermMaxDepth)
|
---|
503 | {
|
---|
504 | ++ mBvhStats.minDepthNodes;
|
---|
505 | }
|
---|
506 |
|
---|
507 | if (data.mProbability <= mTermMinProbability)
|
---|
508 | ++ mBvhStats.minProbabilityNodes;
|
---|
509 |
|
---|
510 | // accumulate depth to compute average depth
|
---|
511 | mBvhStats.accumDepth += data.mDepth;
|
---|
512 |
|
---|
513 | if ((int)(leaf->mObjects.size()) < mTermMinObjects)
|
---|
514 | ++ mBvhStats.minObjectsNodes;
|
---|
515 |
|
---|
516 | if ((int)(leaf->mObjects.size()) > mBvhStats.maxObjectRefs)
|
---|
517 | mBvhStats.maxObjectRefs = (int)leaf->mObjects.size();
|
---|
518 | }
|
---|
519 |
|
---|
520 |
|
---|
521 | #if 0
|
---|
522 | float BvHierarchy::EvalLocalObjectPartition(const BvhTraversalData &tData,
|
---|
523 | const int axis,
|
---|
524 | ObjectContainer &objectsFront,
|
---|
525 | ObjectContainer &objectsBack)
|
---|
526 | {
|
---|
527 | const float maxBox = tData.mBoundingBox.Max(axis);
|
---|
528 | const float minBox = tData.mBoundingBox.Min(axis);
|
---|
529 |
|
---|
530 | float midPoint = (maxBox + minBox) * 0.5f;
|
---|
531 |
|
---|
532 | ObjectContainer::const_iterator oit, oit_end = tData.mNode->mObjects.end();
|
---|
533 |
|
---|
534 | for (oit = tData.mNode->mObjects.begin(); oit != oit_end; ++ oit)
|
---|
535 | {
|
---|
536 | Intersectable *obj = *oit;
|
---|
537 | const AxisAlignedBox3 box = obj->GetBox();
|
---|
538 |
|
---|
539 | const float objMid = (box.Max(axis) + box.Min(axis)) * 0.5f;
|
---|
540 |
|
---|
541 | // object mailed => belongs to back objects
|
---|
542 | if (objMid < midPoint)
|
---|
543 | objectsBack.push_back(obj);
|
---|
544 | else
|
---|
545 | objectsFront.push_back(obj);
|
---|
546 | }
|
---|
547 |
|
---|
548 | const float oldRenderCost = tData.mProbability * (float)tData.mNode->mObjects.size();
|
---|
549 | const float newRenderCost =
|
---|
550 | EvalRenderCost(tData, objectsFront, objectsBack);
|
---|
551 |
|
---|
552 | const float ratio = newRenderCost / oldRenderCost;
|
---|
553 | return ratio;
|
---|
554 | }
|
---|
555 |
|
---|
556 | #else
|
---|
557 |
|
---|
558 | float BvHierarchy::EvalLocalObjectPartition(const BvhTraversalData &tData,
|
---|
559 | const int axis,
|
---|
560 | ObjectContainer &objectsFront,
|
---|
561 | ObjectContainer &objectsBack)
|
---|
562 | {
|
---|
563 | SortSubdivisionCandidates(tData, axis);
|
---|
564 |
|
---|
565 | vector<SortableEntry>::const_iterator cit, cit_end = mSubdivisionCandidates->end();
|
---|
566 |
|
---|
567 | int i = 0;
|
---|
568 | const int border = (int)tData.mNode->mObjects.size() / 2;
|
---|
569 |
|
---|
570 | for (cit = mSubdivisionCandidates->begin(); cit != cit_end; ++ cit, ++ i)
|
---|
571 | {
|
---|
572 | Intersectable *obj = (*cit).mObject;
|
---|
573 |
|
---|
574 | // object mailed => belongs to back objects
|
---|
575 | if (i < border)
|
---|
576 | objectsBack.push_back(obj);
|
---|
577 | else
|
---|
578 | objectsFront.push_back(obj);
|
---|
579 | }
|
---|
580 |
|
---|
581 | const float oldProp = EvalViewCellsVolume(tData.mNode->mObjects);
|
---|
582 | //const float oldProp2 = tData.mProbability;
|
---|
583 |
|
---|
584 | const float oldRenderCost = oldProp * (float)tData.mNode->mObjects.size();
|
---|
585 | const float newRenderCost = EvalRenderCost(tData, objectsFront, objectsBack);
|
---|
586 |
|
---|
587 | const float ratio = newRenderCost / oldRenderCost;
|
---|
588 | return ratio;
|
---|
589 | }
|
---|
590 | #endif
|
---|
591 |
|
---|
592 |
|
---|
593 | float BvHierarchy::EvalSah(const BvhTraversalData &tData,
|
---|
594 | const int axis,
|
---|
595 | ObjectContainer &objectsFront,
|
---|
596 | ObjectContainer &objectsBack)
|
---|
597 | {
|
---|
598 | SortSubdivisionCandidates(tData, axis);
|
---|
599 |
|
---|
600 | // go through the lists, count the number of objects left and right
|
---|
601 | // and evaluate the following cost funcion:
|
---|
602 | // C = ct_div_ci + (ol + or)/queries
|
---|
603 | int objectsLeft = 0, objectsRight = (int)tData.mNode->mObjects.size();
|
---|
604 |
|
---|
605 | AxisAlignedBox3 box = tData.mNode->GetBoundingBox();
|
---|
606 |
|
---|
607 | float minBox = box.Min(axis);
|
---|
608 | float maxBox = box.Max(axis);
|
---|
609 | float boxArea = box.SurfaceArea();
|
---|
610 |
|
---|
611 | float minSum = 1e20f;
|
---|
612 |
|
---|
613 | vector<float> bordersRight;
|
---|
614 | bordersRight.resize(mSubdivisionCandidates->size());
|
---|
615 |
|
---|
616 | float minBorder = maxBox;
|
---|
617 | float maxBorder = minBox;
|
---|
618 |
|
---|
619 | vector<SortableEntry>::const_iterator currentPos =
|
---|
620 | mSubdivisionCandidates->begin();
|
---|
621 |
|
---|
622 | vector<SortableEntry>::reverse_iterator rcit =
|
---|
623 | mSubdivisionCandidates->rbegin(), rcit_end = mSubdivisionCandidates->rend();
|
---|
624 |
|
---|
625 | vector<float>::reverse_iterator rbit = bordersRight.rbegin();
|
---|
626 |
|
---|
627 | for (; rcit != rcit_end; ++ rcit, ++ rbit)
|
---|
628 | {
|
---|
629 | Intersectable *obj = (*rcit).mObject;
|
---|
630 | const AxisAlignedBox3 box = obj->GetBox();
|
---|
631 |
|
---|
632 | if (box.Min() < minBorder)
|
---|
633 | minBorder = box.Min(axis);
|
---|
634 |
|
---|
635 | (*rbit) = minBorder;
|
---|
636 | }
|
---|
637 |
|
---|
638 | vector<float>::const_iterator bit = bordersRight.begin();
|
---|
639 |
|
---|
640 | vector<SortableEntry>::const_iterator cit, cit_end = mSubdivisionCandidates->end();
|
---|
641 | for (cit = mSubdivisionCandidates->begin(); cit != cit_end; ++ cit, ++ bit)
|
---|
642 | {
|
---|
643 | Intersectable *obj = (*cit).mObject;
|
---|
644 |
|
---|
645 | ++ objectsLeft;
|
---|
646 | -- objectsRight;
|
---|
647 |
|
---|
648 | AxisAlignedBox3 lbox = box;
|
---|
649 | AxisAlignedBox3 rbox = box;
|
---|
650 |
|
---|
651 | const AxisAlignedBox3 obox = obj->GetBox();
|
---|
652 |
|
---|
653 | if (obox.Max(axis) > maxBorder)
|
---|
654 | maxBorder = obox.Max(axis);
|
---|
655 |
|
---|
656 | lbox.SetMax(axis, maxBorder);
|
---|
657 | rbox.SetMin(axis, *bit);
|
---|
658 |
|
---|
659 | const float sum = objectsLeft * lbox.SurfaceArea() + objectsRight * rbox.SurfaceArea();
|
---|
660 |
|
---|
661 | // cout<<"pos="<<(*ci).value<<"\t q=("<<ql<<","<<qr<<")\t r=("<<rl<<","<<rr<<")"<<endl;
|
---|
662 | // cout<<"cost= "<<sum<<endl;
|
---|
663 |
|
---|
664 | if (sum < minSum)
|
---|
665 | {
|
---|
666 | minSum = sum;
|
---|
667 | // objects belongs to left side now
|
---|
668 | for (; currentPos != (cit + 1); ++ currentPos);
|
---|
669 | }
|
---|
670 | }
|
---|
671 |
|
---|
672 | //-- assign object to front and back volume
|
---|
673 |
|
---|
674 | // belongs to back bv
|
---|
675 | for (cit = mSubdivisionCandidates->begin(); cit != currentPos; ++ cit)
|
---|
676 | objectsBack.push_back((*cit).mObject);
|
---|
677 |
|
---|
678 | // belongs to front bv
|
---|
679 | for (cit = currentPos; cit != cit_end; ++ cit)
|
---|
680 | objectsFront.push_back((*cit).mObject);
|
---|
681 |
|
---|
682 | float oldCost = (float)tData.mNode->mObjects.size();
|
---|
683 | float newCost = minSum / boxArea;
|
---|
684 | float ratio = newCost / oldCost;
|
---|
685 |
|
---|
686 | #if 0
|
---|
687 | cout<<"===================="<<endl;
|
---|
688 | cout<<"costRatio="<<ratio<<" pos="<<position<<" t="<<(position - minBox)/(maxBox - minBox)
|
---|
689 | <<"\t o=("<<objectsBack<<","<<objectsFront<<")"<<endl;
|
---|
690 | #endif
|
---|
691 | return ratio;
|
---|
692 | }
|
---|
693 |
|
---|
694 |
|
---|
695 | static bool PrepareOutput(const int axis,
|
---|
696 | const int leaves,
|
---|
697 | ofstream &sumStats,
|
---|
698 | ofstream &vollStats,
|
---|
699 | ofstream &volrStats)
|
---|
700 | {
|
---|
701 | if ((axis == 0) && (leaves > 0) && (leaves < 90))
|
---|
702 | {
|
---|
703 | char str[64];
|
---|
704 | sprintf(str, "tmp/bvh_heur_sum-%04d.log", leaves);
|
---|
705 | sumStats.open(str);
|
---|
706 | sprintf(str, "tmp/bvh_heur_voll-%04d.log", leaves);
|
---|
707 | vollStats.open(str);
|
---|
708 | sprintf(str, "tmp/bvh_heur_volr-%04d.log", leaves);
|
---|
709 | volrStats.open(str);
|
---|
710 | }
|
---|
711 |
|
---|
712 | return sumStats.is_open() && vollStats.is_open() && volrStats.is_open();
|
---|
713 | }
|
---|
714 |
|
---|
715 |
|
---|
716 | static void PrintHeuristics(const int objectsRight,
|
---|
717 | const float sum,
|
---|
718 | const float volLeft,
|
---|
719 | const float volRight,
|
---|
720 | const float viewSpaceVol,
|
---|
721 | ofstream &sumStats,
|
---|
722 | ofstream &vollStats,
|
---|
723 | ofstream &volrStats)
|
---|
724 | {
|
---|
725 | sumStats
|
---|
726 | << "#Position\n" << objectsRight << endl
|
---|
727 | << "#Sum\n" << sum / viewSpaceVol << endl
|
---|
728 | << "#Vol\n" << (volLeft + volRight) / viewSpaceVol << endl;
|
---|
729 |
|
---|
730 | vollStats
|
---|
731 | << "#Position\n" << objectsRight << endl
|
---|
732 | << "#Vol\n" << volLeft / viewSpaceVol << endl;
|
---|
733 |
|
---|
734 | volrStats
|
---|
735 | << "#Position\n" << objectsRight << endl
|
---|
736 | << "#Vol\n" << volRight / viewSpaceVol << endl;
|
---|
737 | }
|
---|
738 |
|
---|
739 |
|
---|
740 | float BvHierarchy::EvalLocalCostHeuristics(const BvhTraversalData &tData,
|
---|
741 | const int axis,
|
---|
742 | ObjectContainer &objectsFront,
|
---|
743 | ObjectContainer &objectsBack)
|
---|
744 | {
|
---|
745 | // prepare the heuristics by setting mailboxes and counters.
|
---|
746 | const float totalVol = PrepareHeuristics(tData, axis);
|
---|
747 |
|
---|
748 | // go through the lists, count the number of objects left and right
|
---|
749 | // and evaluate the cost funcion
|
---|
750 |
|
---|
751 | // local helper variables
|
---|
752 | float volLeft = 0;
|
---|
753 | float volRight = totalVol;
|
---|
754 |
|
---|
755 | int nObjectsLeft = 0;
|
---|
756 |
|
---|
757 | const int nTotalObjects = (int)tData.mNode->mObjects.size();
|
---|
758 | const float viewSpaceVol = mVspTree->GetBoundingBox().GetVolume();
|
---|
759 |
|
---|
760 | vector<SortableEntry>::const_iterator currentPos =
|
---|
761 | mSubdivisionCandidates->begin();
|
---|
762 |
|
---|
763 | /////////////////////////////////
|
---|
764 | // the parameters for the current optimum
|
---|
765 |
|
---|
766 | float volBack = volLeft;
|
---|
767 | float volFront = volRight;
|
---|
768 | float newRenderCost = nTotalObjects * totalVol;
|
---|
769 |
|
---|
770 | #ifdef _DEBUG
|
---|
771 | ofstream sumStats;
|
---|
772 | ofstream vollStats;
|
---|
773 | ofstream volrStats;
|
---|
774 |
|
---|
775 | const bool printStats =
|
---|
776 | PrepareOutput(axis, mBvhStats.Leaves(), sumStats, vollStats, volrStats);
|
---|
777 |
|
---|
778 | #endif
|
---|
779 |
|
---|
780 | /////////////////////////////
|
---|
781 | // the sweep heuristics
|
---|
782 |
|
---|
783 | //-- traverse through events and find best split plane
|
---|
784 |
|
---|
785 | vector<SortableEntry>::const_iterator cit, cit_end = mSubdivisionCandidates->end();
|
---|
786 |
|
---|
787 | for (cit = mSubdivisionCandidates->begin(); cit != cit_end; ++ cit)
|
---|
788 | {
|
---|
789 | Intersectable *object = (*cit).mObject;
|
---|
790 |
|
---|
791 | // evaluate change in l and r volume
|
---|
792 | // voll = view cells that see only left node (i.e., left pvs)
|
---|
793 | // volr = view cells that see only right node (i.e., right pvs)
|
---|
794 | EvalHeuristicsContribution(object, volLeft, volRight);
|
---|
795 |
|
---|
796 | ++ nObjectsLeft;
|
---|
797 |
|
---|
798 | const int nObjectsRight = nTotalObjects - nObjectsLeft;
|
---|
799 |
|
---|
800 | // the heuristics
|
---|
801 | const float sum = volLeft * (float)nObjectsLeft +
|
---|
802 | volRight * (float)nObjectsRight;
|
---|
803 |
|
---|
804 | #ifdef _DEBUG
|
---|
805 | if (printStats)
|
---|
806 | PrintHeuristics(nObjectsRight, sum, volLeft, volRight, viewSpaceVol,
|
---|
807 | sumStats, vollStats, volrStats);
|
---|
808 | #endif
|
---|
809 |
|
---|
810 | if (sum < newRenderCost)
|
---|
811 | {
|
---|
812 | newRenderCost = sum;
|
---|
813 |
|
---|
814 | volBack = volLeft;
|
---|
815 | volFront = volRight;
|
---|
816 |
|
---|
817 | // objects belongs to left side now
|
---|
818 | for (; currentPos != (cit + 1); ++ currentPos);
|
---|
819 | }
|
---|
820 | }
|
---|
821 |
|
---|
822 | //-- assign object to front and back volume
|
---|
823 |
|
---|
824 | // belongs to back bv
|
---|
825 | for (cit = mSubdivisionCandidates->begin(); cit != currentPos; ++ cit)
|
---|
826 | objectsBack.push_back((*cit).mObject);
|
---|
827 |
|
---|
828 | // belongs to front bv
|
---|
829 | for (cit = currentPos; cit != cit_end; ++ cit)
|
---|
830 | objectsFront.push_back((*cit).mObject);
|
---|
831 |
|
---|
832 | const float oldRenderCost = (float)nTotalObjects * totalVol + Limits::Small;
|
---|
833 | // the relative cost ratio
|
---|
834 | const float ratio = newRenderCost / oldRenderCost;
|
---|
835 |
|
---|
836 | #ifdef _DEBUG
|
---|
837 | Debug << "\n§§§§ eval local cost §§§§" << endl
|
---|
838 | << "back pvs: " << (int)objectsBack.size() << " front pvs: " << (int)objectsFront.size() << " total pvs: " << nTotalObjects << endl
|
---|
839 | << "back p: " << volBack / viewSpaceVol << " front p " << volFront / viewSpaceVol << " p: " << totalVol / viewSpaceVol << endl
|
---|
840 | << "old rc: " << oldRenderCost / viewSpaceVol << " new rc: " << newRenderCost / viewSpaceVol << endl
|
---|
841 | << "render cost decrease: " << oldRenderCost / viewSpaceVol - newRenderCost / viewSpaceVol << endl;
|
---|
842 | #endif
|
---|
843 |
|
---|
844 | return ratio;
|
---|
845 | }
|
---|
846 |
|
---|
847 |
|
---|
848 | void BvHierarchy::SortSubdivisionCandidates(const BvhTraversalData &tData,
|
---|
849 | const int axis)
|
---|
850 | {
|
---|
851 | mSubdivisionCandidates->clear();
|
---|
852 |
|
---|
853 | //RayInfoContainer *rays = tData.mRays;
|
---|
854 | BvhLeaf *leaf = tData.mNode;
|
---|
855 |
|
---|
856 | // compute requested size
|
---|
857 | const int requestedSize = (int)leaf->mObjects.size() * 2;
|
---|
858 |
|
---|
859 | // creates a sorted split candidates array
|
---|
860 | if (mSubdivisionCandidates->capacity() > 500000 &&
|
---|
861 | requestedSize < (int)(mSubdivisionCandidates->capacity() / 10) )
|
---|
862 | {
|
---|
863 | delete mSubdivisionCandidates;
|
---|
864 | mSubdivisionCandidates = new vector<SortableEntry>;
|
---|
865 | }
|
---|
866 |
|
---|
867 | mSubdivisionCandidates->reserve(requestedSize);
|
---|
868 |
|
---|
869 | //-- insert object queries
|
---|
870 | //-- These queries can induce a change in pvs size.
|
---|
871 | //-- Note that they cannot induce a change in view cell volume, as
|
---|
872 | //-- there is no ray associated with these events!!
|
---|
873 |
|
---|
874 | ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
|
---|
875 |
|
---|
876 | for (oit = leaf->mObjects.begin(); oit < oit_end; ++ oit)
|
---|
877 | {
|
---|
878 | Intersectable *obj = *oit;
|
---|
879 |
|
---|
880 | Intersectable *object = *oit;
|
---|
881 | const AxisAlignedBox3 &box = object->GetBox();
|
---|
882 | const float midPt = (box.Min(axis) + box.Max(axis)) * 0.5f;
|
---|
883 |
|
---|
884 | mSubdivisionCandidates->push_back(SortableEntry(object, midPt));
|
---|
885 | }
|
---|
886 |
|
---|
887 | stable_sort(mSubdivisionCandidates->begin(), mSubdivisionCandidates->end());
|
---|
888 | }
|
---|
889 |
|
---|
890 |
|
---|
891 | const BvhStatistics &BvHierarchy::GetStatistics() const
|
---|
892 | {
|
---|
893 | return mBvhStats;
|
---|
894 | }
|
---|
895 |
|
---|
896 |
|
---|
897 | float BvHierarchy::PrepareHeuristics(const BvhTraversalData &tData, const int axis)
|
---|
898 | {
|
---|
899 | BvhLeaf *leaf = tData.mNode;
|
---|
900 | float vol = 0;
|
---|
901 |
|
---|
902 | // sort so we can use a sweep from right to left
|
---|
903 | SortSubdivisionCandidates(tData, axis);
|
---|
904 |
|
---|
905 | // collect and mark the view cells as belonging to front pvs
|
---|
906 | ViewCellContainer viewCells;
|
---|
907 | CollectViewCells(tData.mNode->mObjects, viewCells, true);
|
---|
908 |
|
---|
909 | ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
|
---|
910 | for (vit = viewCells.begin(); vit != vit_end; ++ vit)
|
---|
911 | {
|
---|
912 | vol += (*vit)->GetVolume();
|
---|
913 | }
|
---|
914 |
|
---|
915 | // mail the objects on the left side
|
---|
916 | Intersectable::NewMail();
|
---|
917 | // mail view cells on the left side
|
---|
918 | ViewCell::NewMail();
|
---|
919 |
|
---|
920 | return vol;
|
---|
921 | }
|
---|
922 | ///////////////////////////////////////////////////////////
|
---|
923 |
|
---|
924 |
|
---|
925 | void BvHierarchy::EvalHeuristicsContribution(Intersectable *obj,
|
---|
926 | float &volLeft,
|
---|
927 | float &volRight)
|
---|
928 | {
|
---|
929 | // collect all view cells associated with this objects
|
---|
930 | // (also multiple times, if they are pierced by several rays)
|
---|
931 | ViewCellContainer viewCells;
|
---|
932 | const bool useMailboxing = false;
|
---|
933 |
|
---|
934 | CollectViewCells(obj, viewCells, useMailboxing);
|
---|
935 |
|
---|
936 | /// classify view cells and compute volume contri accordingly
|
---|
937 | /// possible view cell classifications:
|
---|
938 | /// view cell mailed => view cell can be seen from left child node
|
---|
939 | /// view cell counter > 0 view cell can be seen from right child node
|
---|
940 | /// combined: view cell volume belongs to both nodes
|
---|
941 | ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
|
---|
942 |
|
---|
943 | for (vit = viewCells.begin(); vit != vit_end; ++ vit)
|
---|
944 | {
|
---|
945 | // view cells can also be seen from left child node
|
---|
946 | ViewCell *viewCell = *vit;
|
---|
947 |
|
---|
948 | const float vol = viewCell->GetVolume();
|
---|
949 |
|
---|
950 | if (!viewCell->Mailed())
|
---|
951 | {
|
---|
952 | viewCell->Mail();
|
---|
953 |
|
---|
954 | // we now see view cell from both nodes
|
---|
955 | // => add volume to left node
|
---|
956 | volLeft += vol;
|
---|
957 | }
|
---|
958 |
|
---|
959 | // last reference into the right node
|
---|
960 | if (-- viewCell->mCounter == 0)
|
---|
961 | {
|
---|
962 | // view cell was previously seen from both nodes =>
|
---|
963 | // remove volume from right node
|
---|
964 | volRight -= vol;
|
---|
965 | }
|
---|
966 | }
|
---|
967 | }
|
---|
968 |
|
---|
969 |
|
---|
970 | void BvHierarchy::SetViewCellsManager(ViewCellsManager *vcm)
|
---|
971 | {
|
---|
972 | mViewCellsManager = vcm;
|
---|
973 | }
|
---|
974 |
|
---|
975 |
|
---|
976 | AxisAlignedBox3 BvHierarchy::GetBoundingBox() const
|
---|
977 | {
|
---|
978 | return mBoundingBox;
|
---|
979 | }
|
---|
980 |
|
---|
981 |
|
---|
982 | float BvHierarchy::SelectObjectPartition(const BvhTraversalData &tData,
|
---|
983 | ObjectContainer &frontObjects,
|
---|
984 | ObjectContainer &backObjects)
|
---|
985 | {
|
---|
986 | ObjectContainer nFrontObjects[3];
|
---|
987 | ObjectContainer nBackObjects[3];
|
---|
988 |
|
---|
989 | float nCostRatio[3];
|
---|
990 |
|
---|
991 | // create bounding box of node geometry
|
---|
992 | AxisAlignedBox3 box = tData.mBoundingBox;
|
---|
993 |
|
---|
994 | int sAxis = 0;
|
---|
995 | int bestAxis = -1;
|
---|
996 |
|
---|
997 | if (mOnlyDrivingAxis)
|
---|
998 | {
|
---|
999 | sAxis = box.Size().DrivingAxis();
|
---|
1000 | }
|
---|
1001 |
|
---|
1002 | // -- evaluate split cost for all three axis
|
---|
1003 |
|
---|
1004 | for (int axis = 0; axis < 3; ++ axis)
|
---|
1005 | {
|
---|
1006 | if (!mOnlyDrivingAxis || (axis == sAxis))
|
---|
1007 | {
|
---|
1008 | if (mUseCostHeuristics)
|
---|
1009 | {
|
---|
1010 | //-- partition objects using heuristics
|
---|
1011 | nCostRatio[axis] =
|
---|
1012 | EvalLocalCostHeuristics(
|
---|
1013 | tData,
|
---|
1014 | axis,
|
---|
1015 | nFrontObjects[axis],
|
---|
1016 | nBackObjects[axis]);
|
---|
1017 | }
|
---|
1018 | else
|
---|
1019 | {
|
---|
1020 | nCostRatio[axis] =
|
---|
1021 | EvalLocalObjectPartition(
|
---|
1022 | tData,
|
---|
1023 | axis,
|
---|
1024 | nFrontObjects[axis],
|
---|
1025 | nBackObjects[axis]);
|
---|
1026 | }
|
---|
1027 |
|
---|
1028 | if (bestAxis == -1)
|
---|
1029 | {
|
---|
1030 | bestAxis = axis;
|
---|
1031 | }
|
---|
1032 | else if (nCostRatio[axis] < nCostRatio[bestAxis])
|
---|
1033 | {
|
---|
1034 | bestAxis = axis;
|
---|
1035 | }
|
---|
1036 | }
|
---|
1037 | }
|
---|
1038 |
|
---|
1039 | //-- assign values
|
---|
1040 |
|
---|
1041 | frontObjects = nFrontObjects[bestAxis];
|
---|
1042 | backObjects = nBackObjects[bestAxis];
|
---|
1043 |
|
---|
1044 | //Debug << "val: " << nCostRatio[bestAxis] << " axis: " << bestAxis << endl;
|
---|
1045 | return nCostRatio[bestAxis];
|
---|
1046 | }
|
---|
1047 |
|
---|
1048 |
|
---|
1049 | void BvHierarchy::AssociateObjectsWithRays(const VssRayContainer &rays)
|
---|
1050 | {
|
---|
1051 |
|
---|
1052 | VssRayContainer::const_iterator rit, rit_end = rays.end();
|
---|
1053 |
|
---|
1054 | for (rit = rays.begin(); rit != rays.end(); ++ rit)
|
---|
1055 | {
|
---|
1056 | VssRay *ray = (*rit);
|
---|
1057 |
|
---|
1058 | if (ray->mTerminationObject)
|
---|
1059 | {
|
---|
1060 | ray->mTerminationObject->mVssRays.push_back(ray);
|
---|
1061 | }
|
---|
1062 |
|
---|
1063 | if (0 && ray->mOriginObject)
|
---|
1064 | {
|
---|
1065 | ray->mOriginObject->mVssRays.push_back(ray);
|
---|
1066 | }
|
---|
1067 | }
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 |
|
---|
1071 | void BvHierarchy::PrintSubdivisionStats(const SubdivisionCandidate &sc)
|
---|
1072 | {
|
---|
1073 | const float costDecr = sc.GetRenderCostDecrease();
|
---|
1074 |
|
---|
1075 | mSubdivisionStats
|
---|
1076 | << "#Leaves\n" << mBvhStats.Leaves()
|
---|
1077 | << "#RenderCostDecrease\n" << costDecr << endl
|
---|
1078 | << "#TotalRenderCost\n" << mTotalCost << endl;
|
---|
1079 | //<< "#AvgRenderCost\n" << avgRenderCost << endl;
|
---|
1080 | }
|
---|
1081 |
|
---|
1082 |
|
---|
1083 | void BvHierarchy::CollectRays(const ObjectContainer &objects,
|
---|
1084 | VssRayContainer &rays) const
|
---|
1085 | {
|
---|
1086 | VssRay::NewMail();
|
---|
1087 |
|
---|
1088 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
1089 |
|
---|
1090 | // evaluate reverse pvs and view cell volume on left and right cell
|
---|
1091 | // note: should I take all leaf objects or rather the objects hit by rays?
|
---|
1092 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
1093 | {
|
---|
1094 | Intersectable *obj = *oit;
|
---|
1095 |
|
---|
1096 | VssRayContainer::const_iterator rit, rit_end = obj->mVssRays.end();
|
---|
1097 |
|
---|
1098 | for (rit = obj->mVssRays.begin(); rit < rit_end; ++ rit)
|
---|
1099 | {
|
---|
1100 | VssRay *ray = (*rit);
|
---|
1101 |
|
---|
1102 | if (!ray->Mailed())
|
---|
1103 | {
|
---|
1104 | ray->Mail();
|
---|
1105 | rays.push_back(ray);
|
---|
1106 | }
|
---|
1107 | }
|
---|
1108 | }
|
---|
1109 | }
|
---|
1110 |
|
---|
1111 |
|
---|
1112 | float BvHierarchy::EvalRenderCost(const BvhTraversalData &tData,
|
---|
1113 | const ObjectContainer &objectsFront,
|
---|
1114 | const ObjectContainer &objectsBack) const
|
---|
1115 | {
|
---|
1116 | BvhLeaf *leaf = tData.mNode;
|
---|
1117 |
|
---|
1118 | // probability that view point lies in a view cell which sees this node
|
---|
1119 | const float pFront = EvalViewCellsVolume(objectsFront);
|
---|
1120 | const float pBack = EvalViewCellsVolume(objectsBack);
|
---|
1121 |
|
---|
1122 | const int totalObjects = (int)leaf->mObjects.size();
|
---|
1123 | const int nObjectsFront = (int)objectsFront.size();
|
---|
1124 | const int nObjectsBack = (int)objectsBack.size();
|
---|
1125 |
|
---|
1126 | //-- pvs rendering heuristics
|
---|
1127 | const float newRenderCost = nObjectsFront * pFront + nObjectsBack * pBack;
|
---|
1128 | /*
|
---|
1129 | const float viewSpaceVol = mVspTree->GetBoundingBox().GetVolume();
|
---|
1130 | Debug << "\nbvh render cost\n"
|
---|
1131 | << "back p: " << pBack / viewSpaceVol << " front p " << pFront / viewSpaceVol << endl
|
---|
1132 | << "new rc: " << newRenderCost / viewSpaceVol << endl;*/
|
---|
1133 |
|
---|
1134 | return newRenderCost;
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 |
|
---|
1138 | AxisAlignedBox3 BvHierarchy::ComputeBoundingBox(const ObjectContainer &objects,
|
---|
1139 | const AxisAlignedBox3 *parentBox)
|
---|
1140 | {
|
---|
1141 | if (parentBox && objects.empty())
|
---|
1142 | return *parentBox;
|
---|
1143 |
|
---|
1144 | AxisAlignedBox3 box;
|
---|
1145 | box.Initialize();
|
---|
1146 |
|
---|
1147 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
1148 |
|
---|
1149 | //-- compute bounding box
|
---|
1150 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
1151 | {
|
---|
1152 | Intersectable *obj = *oit;
|
---|
1153 |
|
---|
1154 | // compute bounding box of view space
|
---|
1155 | box.Include(obj->GetBox());
|
---|
1156 | }
|
---|
1157 |
|
---|
1158 | return box;
|
---|
1159 | }
|
---|
1160 |
|
---|
1161 |
|
---|
1162 | void BvHierarchy::CollectLeaves(vector<BvhLeaf *> &leaves) const
|
---|
1163 | {
|
---|
1164 | stack<BvhNode *> nodeStack;
|
---|
1165 | nodeStack.push(mRoot);
|
---|
1166 |
|
---|
1167 | while (!nodeStack.empty())
|
---|
1168 | {
|
---|
1169 | BvhNode *node = nodeStack.top();
|
---|
1170 | nodeStack.pop();
|
---|
1171 |
|
---|
1172 | if (node->IsLeaf())
|
---|
1173 | {
|
---|
1174 | BvhLeaf *leaf = (BvhLeaf *)node;
|
---|
1175 | leaves.push_back(leaf);
|
---|
1176 | }
|
---|
1177 | else
|
---|
1178 | {
|
---|
1179 | BvhInterior *interior = (BvhInterior *)node;
|
---|
1180 |
|
---|
1181 | nodeStack.push(interior->GetBack());
|
---|
1182 | nodeStack.push(interior->GetFront());
|
---|
1183 | }
|
---|
1184 | }
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 |
|
---|
1188 | AxisAlignedBox3 BvHierarchy::GetBoundingBox(BvhNode *node) const
|
---|
1189 | {
|
---|
1190 | return node->GetBoundingBox();
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 |
|
---|
1194 | void BvHierarchy::CollectViewCells(const ObjectContainer &objects,
|
---|
1195 | ViewCellContainer &viewCells,
|
---|
1196 | const bool setCounter) const
|
---|
1197 | {
|
---|
1198 | ViewCell::NewMail();
|
---|
1199 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
1200 |
|
---|
1201 | // loop through all object and collect view cell pvs of this node
|
---|
1202 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
1203 | {
|
---|
1204 | CollectViewCells(*oit, viewCells, true, setCounter);
|
---|
1205 | }
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 |
|
---|
1209 | void BvHierarchy::CollectViewCells(Intersectable *obj,
|
---|
1210 | ViewCellContainer &viewCells,
|
---|
1211 | const bool useMailBoxing,
|
---|
1212 | const bool setCounter) const
|
---|
1213 | {
|
---|
1214 | VssRayContainer::const_iterator rit, rit_end = obj->mVssRays.end();
|
---|
1215 |
|
---|
1216 | for (rit = obj->mVssRays.begin(); rit < rit_end; ++ rit)
|
---|
1217 | {
|
---|
1218 | VssRay *ray = (*rit);
|
---|
1219 | ViewCellContainer tmpViewCells;
|
---|
1220 |
|
---|
1221 | mVspTree->GetViewCells(*ray, tmpViewCells);
|
---|
1222 |
|
---|
1223 | ViewCellContainer::const_iterator vit, vit_end = tmpViewCells.end();
|
---|
1224 |
|
---|
1225 | for (vit = tmpViewCells.begin(); vit != vit_end; ++ vit)
|
---|
1226 | {
|
---|
1227 | VspViewCell *vc = dynamic_cast<VspViewCell *>(*vit);
|
---|
1228 |
|
---|
1229 | // store view cells
|
---|
1230 | if (!useMailBoxing || !vc->Mailed())
|
---|
1231 | {
|
---|
1232 | if (useMailBoxing)
|
---|
1233 | {
|
---|
1234 | vc->Mail();
|
---|
1235 | if (setCounter)
|
---|
1236 | {
|
---|
1237 | vc->mCounter = 0;
|
---|
1238 | }
|
---|
1239 | }
|
---|
1240 | viewCells.push_back(vc);
|
---|
1241 | }
|
---|
1242 |
|
---|
1243 | if (setCounter)
|
---|
1244 | {
|
---|
1245 | ++ vc->mCounter;
|
---|
1246 | }
|
---|
1247 | }
|
---|
1248 | }
|
---|
1249 | }
|
---|
1250 |
|
---|
1251 |
|
---|
1252 | void BvHierarchy::CollectDirtyCandidates(BvhSubdivisionCandidate *sc,
|
---|
1253 | vector<SubdivisionCandidate *> &dirtyList)
|
---|
1254 | {
|
---|
1255 | BvhTraversalData &tData = sc->mParentData;
|
---|
1256 | BvhLeaf *node = tData.mNode;
|
---|
1257 |
|
---|
1258 | ViewCellContainer viewCells;
|
---|
1259 | CollectViewCells(node->mObjects, viewCells);
|
---|
1260 |
|
---|
1261 | // split candidates handling
|
---|
1262 | // these view cells are thrown into dirty list
|
---|
1263 | ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
|
---|
1264 |
|
---|
1265 | Debug << "collecting " << (int)viewCells.size() << " dirty candidates" << endl;
|
---|
1266 |
|
---|
1267 | for (vit = viewCells.begin(); vit != vit_end; ++ vit)
|
---|
1268 | {
|
---|
1269 | VspViewCell *vc = dynamic_cast<VspViewCell *>(*vit);
|
---|
1270 | VspLeaf *leaf = vc->mLeaf;
|
---|
1271 | SubdivisionCandidate *candidate = leaf->GetSubdivisionCandidate();
|
---|
1272 |
|
---|
1273 | if (candidate) // is this leaf still a split candidate?
|
---|
1274 | {
|
---|
1275 | dirtyList.push_back(candidate);
|
---|
1276 | }
|
---|
1277 | }
|
---|
1278 | }
|
---|
1279 |
|
---|
1280 |
|
---|
1281 | BvhNode *BvHierarchy::GetRoot() const
|
---|
1282 | {
|
---|
1283 | return mRoot;
|
---|
1284 | }
|
---|
1285 |
|
---|
1286 |
|
---|
1287 | bool BvHierarchy::IsObjectInLeaf(BvhLeaf *leaf, Intersectable *object) const
|
---|
1288 | {
|
---|
1289 | ObjectContainer::const_iterator oit =
|
---|
1290 | lower_bound(leaf->mObjects.begin(), leaf->mObjects.end(), object, ilt);
|
---|
1291 |
|
---|
1292 | // objects sorted by id
|
---|
1293 | if ((oit != leaf->mObjects.end()) && ((*oit)->GetId() == object->GetId()))
|
---|
1294 | {
|
---|
1295 | return true;
|
---|
1296 | }
|
---|
1297 | else
|
---|
1298 | {
|
---|
1299 | return false;
|
---|
1300 | }
|
---|
1301 | }
|
---|
1302 |
|
---|
1303 |
|
---|
1304 | BvhLeaf *BvHierarchy::GetLeaf(Intersectable *object, BvhNode *node) const
|
---|
1305 | {
|
---|
1306 | // rather use the simple version
|
---|
1307 | if (!object) return NULL;
|
---|
1308 | return object->mBvhLeaf;
|
---|
1309 |
|
---|
1310 | ///////////////////////////////////////
|
---|
1311 | // start from root of tree
|
---|
1312 |
|
---|
1313 | if (node == NULL)
|
---|
1314 | node = mRoot;
|
---|
1315 |
|
---|
1316 | vector<BvhLeaf *> leaves;
|
---|
1317 |
|
---|
1318 | stack<BvhNode *> nodeStack;
|
---|
1319 | nodeStack.push(node);
|
---|
1320 |
|
---|
1321 | BvhLeaf *leaf = NULL;
|
---|
1322 |
|
---|
1323 | while (!nodeStack.empty())
|
---|
1324 | {
|
---|
1325 | BvhNode *node = nodeStack.top();
|
---|
1326 | nodeStack.pop();
|
---|
1327 |
|
---|
1328 | if (node->IsLeaf())
|
---|
1329 | {
|
---|
1330 | leaf = dynamic_cast<BvhLeaf *>(node);
|
---|
1331 |
|
---|
1332 | if (IsObjectInLeaf(leaf, object))
|
---|
1333 | {
|
---|
1334 | return leaf;
|
---|
1335 | }
|
---|
1336 | }
|
---|
1337 | else
|
---|
1338 | {
|
---|
1339 | // find point
|
---|
1340 | BvhInterior *interior = dynamic_cast<BvhInterior *>(node);
|
---|
1341 |
|
---|
1342 | if (interior->GetBack()->GetBoundingBox().Includes(object->GetBox()))
|
---|
1343 | {
|
---|
1344 | nodeStack.push(interior->GetBack());
|
---|
1345 | }
|
---|
1346 |
|
---|
1347 | // search both sides as we are using bounding volumes
|
---|
1348 | if (interior->GetFront()->GetBoundingBox().Includes(object->GetBox()))
|
---|
1349 | {
|
---|
1350 | nodeStack.push(interior->GetFront());
|
---|
1351 | }
|
---|
1352 | }
|
---|
1353 | }
|
---|
1354 |
|
---|
1355 | return leaf;
|
---|
1356 | }
|
---|
1357 |
|
---|
1358 |
|
---|
1359 | BvhIntersectable *BvHierarchy::GetOrCreateBvhIntersectable(BvhNode *node)
|
---|
1360 | {
|
---|
1361 | // search nodes
|
---|
1362 | std::map<BvhNode *, BvhIntersectable *>::
|
---|
1363 | const_iterator it = mBvhIntersectables.find(node);
|
---|
1364 |
|
---|
1365 | if (it != mBvhIntersectables.end())
|
---|
1366 | {
|
---|
1367 | return (*it).second;
|
---|
1368 | }
|
---|
1369 |
|
---|
1370 | // not in map => create new entry
|
---|
1371 | BvhIntersectable *bvhObj = new BvhIntersectable(node);
|
---|
1372 | mBvhIntersectables[node] = bvhObj;
|
---|
1373 |
|
---|
1374 | return bvhObj;
|
---|
1375 | }
|
---|
1376 |
|
---|
1377 |
|
---|
1378 | bool BvHierarchy::Export(OUT_STREAM &stream)
|
---|
1379 | {
|
---|
1380 | ExportNode(mRoot, stream);
|
---|
1381 |
|
---|
1382 | return true;
|
---|
1383 | }
|
---|
1384 |
|
---|
1385 |
|
---|
1386 | void BvHierarchy::ExportObjects(BvhLeaf *leaf, OUT_STREAM &stream)
|
---|
1387 | {
|
---|
1388 | ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
|
---|
1389 | for (oit = leaf->mObjects.begin(); oit != oit_end; ++ oit)
|
---|
1390 | {
|
---|
1391 | stream << (*oit)->GetId() << " ";
|
---|
1392 | }
|
---|
1393 | }
|
---|
1394 |
|
---|
1395 |
|
---|
1396 | void BvHierarchy::ExportNode(BvhNode *node, OUT_STREAM &stream)
|
---|
1397 | {
|
---|
1398 | if (node->IsLeaf())
|
---|
1399 | {
|
---|
1400 | BvhLeaf *leaf = dynamic_cast<BvhLeaf *>(node);
|
---|
1401 | const AxisAlignedBox3 box = leaf->GetBoundingBox();
|
---|
1402 | stream << "<Leaf"
|
---|
1403 | << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
|
---|
1404 | << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\""
|
---|
1405 | << " objects=\"";
|
---|
1406 |
|
---|
1407 | //-- export objects
|
---|
1408 | ExportObjects(leaf, stream);
|
---|
1409 |
|
---|
1410 | stream << "\" />" << endl;
|
---|
1411 | }
|
---|
1412 | else
|
---|
1413 | {
|
---|
1414 | BvhInterior *interior = dynamic_cast<BvhInterior *>(node);
|
---|
1415 | const AxisAlignedBox3 box = interior->GetBoundingBox();
|
---|
1416 |
|
---|
1417 | stream << "<Interior"
|
---|
1418 | << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
|
---|
1419 | << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z
|
---|
1420 | << "\">" << endl;
|
---|
1421 |
|
---|
1422 | ExportNode(interior->GetBack(), stream);
|
---|
1423 | ExportNode(interior->GetFront(), stream);
|
---|
1424 |
|
---|
1425 | stream << "</Interior>" << endl;
|
---|
1426 | }
|
---|
1427 | }
|
---|
1428 |
|
---|
1429 |
|
---|
1430 | float BvHierarchy::EvalViewCellsVolume(const ObjectContainer &objects) const
|
---|
1431 | {
|
---|
1432 | float vol = 0;
|
---|
1433 |
|
---|
1434 | ViewCellContainer viewCells;
|
---|
1435 | CollectViewCells(objects, viewCells);
|
---|
1436 |
|
---|
1437 | ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
|
---|
1438 |
|
---|
1439 | for (vit = viewCells.begin(); vit != vit_end; ++ vit)
|
---|
1440 | {
|
---|
1441 | vol += (*vit)->GetVolume();
|
---|
1442 | }
|
---|
1443 |
|
---|
1444 | return vol;
|
---|
1445 | }
|
---|
1446 |
|
---|
1447 | void BvHierarchy::CreateRoot(const ObjectContainer &objects)
|
---|
1448 | {
|
---|
1449 | //-- create new root
|
---|
1450 | AxisAlignedBox3 box = ComputeBoundingBox(objects);
|
---|
1451 | BvhLeaf *bvhleaf = new BvhLeaf(box, NULL, (int)objects.size());
|
---|
1452 | bvhleaf->mObjects = objects;
|
---|
1453 |
|
---|
1454 | mRoot = bvhleaf;
|
---|
1455 |
|
---|
1456 | // associate root with current objects
|
---|
1457 | AssociateObjectsWithLeaf(bvhleaf);
|
---|
1458 | }
|
---|
1459 |
|
---|
1460 |
|
---|
1461 | SubdivisionCandidate *BvHierarchy::PrepareConstruction(const VssRayContainer &sampleRays,
|
---|
1462 | const ObjectContainer &objects)
|
---|
1463 | {
|
---|
1464 | mBvhStats.Reset();
|
---|
1465 | mBvhStats.Start();
|
---|
1466 | mBvhStats.nodes = 1;
|
---|
1467 |
|
---|
1468 | // note matt: we assume that we have objects sorted by their id
|
---|
1469 |
|
---|
1470 | // store pointer to this tree
|
---|
1471 | BvhSubdivisionCandidate::sBvHierarchy = this;
|
---|
1472 | mBvhStats.nodes = 1;
|
---|
1473 |
|
---|
1474 | // compute bounding box from objects
|
---|
1475 | // note: we assume that root was already created
|
---|
1476 | mBoundingBox = mRoot->GetBoundingBox();
|
---|
1477 | BvhLeaf *bvhleaf = dynamic_cast<BvhLeaf *>(mRoot);
|
---|
1478 |
|
---|
1479 | mTermMinProbability *= mBoundingBox.GetVolume();
|
---|
1480 | mGlobalCostMisses = 0;
|
---|
1481 |
|
---|
1482 | // only rays intersecting objects in node are interesting
|
---|
1483 | AssociateObjectsWithRays(sampleRays);
|
---|
1484 |
|
---|
1485 | // probabilty is voume of all "seen" view cells
|
---|
1486 | #if 1
|
---|
1487 | const float prop = EvalViewCellsVolume(objects);
|
---|
1488 | #else
|
---|
1489 | const float prop = GetBoundingBox().GetVolume();
|
---|
1490 | #endif
|
---|
1491 |
|
---|
1492 | // create bvh traversal data
|
---|
1493 | BvhTraversalData oData(bvhleaf, 0, mBoundingBox, prop);
|
---|
1494 |
|
---|
1495 | //-- add first candidate for object space partition
|
---|
1496 | BvhSubdivisionCandidate *oSubdivisionCandidate =
|
---|
1497 | new BvhSubdivisionCandidate(oData);
|
---|
1498 |
|
---|
1499 | EvalSubdivisionCandidate(*oSubdivisionCandidate);
|
---|
1500 | bvhleaf->SetSubdivisionCandidate(oSubdivisionCandidate);
|
---|
1501 |
|
---|
1502 | const float viewSpaceVol = mVspTree->GetBoundingBox().GetVolume();
|
---|
1503 | mTotalCost = (float)objects.size() * prop / viewSpaceVol;
|
---|
1504 |
|
---|
1505 | PrintSubdivisionStats(*oSubdivisionCandidate);
|
---|
1506 |
|
---|
1507 | return oSubdivisionCandidate;
|
---|
1508 | }
|
---|
1509 |
|
---|
1510 |
|
---|
1511 | bool BvHierarchy::AddLeafToPvs(BvhLeaf *leaf,
|
---|
1512 | ViewCell *vc,
|
---|
1513 | const float pdf,
|
---|
1514 | float &contribution)
|
---|
1515 | {
|
---|
1516 | // add kd intersecable to pvs
|
---|
1517 | BvhIntersectable *bvhObj = GetOrCreateBvhIntersectable(leaf);
|
---|
1518 |
|
---|
1519 | return vc->AddPvsSample(bvhObj, pdf, contribution);
|
---|
1520 | }
|
---|
1521 |
|
---|
1522 |
|
---|
1523 | void BvhStatistics::Print(ostream &app) const
|
---|
1524 | {
|
---|
1525 | app << "=========== BvHierarchy statistics ===============\n";
|
---|
1526 |
|
---|
1527 | app << setprecision(4);
|
---|
1528 |
|
---|
1529 | app << "#N_CTIME ( Construction time [s] )\n" << Time() << " \n";
|
---|
1530 |
|
---|
1531 | app << "#N_NODES ( Number of nodes )\n" << nodes << "\n";
|
---|
1532 |
|
---|
1533 | app << "#N_INTERIORS ( Number of interior nodes )\n" << Interior() << "\n";
|
---|
1534 |
|
---|
1535 | app << "#N_LEAVES ( Number of leaves )\n" << Leaves() << "\n";
|
---|
1536 |
|
---|
1537 | app << "#AXIS_ALIGNED_SPLITS (number of axis aligned splits)\n" << splits << endl;
|
---|
1538 |
|
---|
1539 | app << "#N_PMINDEPTHLEAVES ( Percentage of leaves at minimum depth )\n"
|
---|
1540 | << minDepthNodes * 100 / (double)Leaves() << endl;
|
---|
1541 |
|
---|
1542 | app << "#N_PMAXDEPTHLEAVES ( Percentage of leaves at maximum depth )\n"
|
---|
1543 | << maxDepthNodes * 100 / (double)Leaves() << endl;
|
---|
1544 |
|
---|
1545 | app << "#N_MAXCOSTNODES ( Percentage of leaves with terminated because of max cost ratio )\n"
|
---|
1546 | << maxCostNodes * 100 / (double)Leaves() << endl;
|
---|
1547 |
|
---|
1548 | app << "#N_PMINPROBABILITYLEAVES ( Percentage of leaves with mininum probability )\n"
|
---|
1549 | << minProbabilityNodes * 100 / (double)Leaves() << endl;
|
---|
1550 |
|
---|
1551 | app << "#N_PMINOBJECTSLEAVES ( Percentage of leaves with mininum objects )\n"
|
---|
1552 | << minObjectsNodes * 100 / (double)Leaves() << endl;
|
---|
1553 |
|
---|
1554 | app << "#N_PMAXDEPTH ( Maximal reached depth )\n" << maxDepth << endl;
|
---|
1555 |
|
---|
1556 | app << "#N_PMINDEPTH ( Minimal reached depth )\n" << minDepth << endl;
|
---|
1557 |
|
---|
1558 | app << "#AVGDEPTH ( average depth )\n" << AvgDepth() << endl;
|
---|
1559 |
|
---|
1560 | app << "#N_INVALIDLEAVES (number of invalid leaves )\n" << invalidLeaves << endl;
|
---|
1561 |
|
---|
1562 | app << "#N_MAXOBJECTREFS ( Max number of object refs / leaf )\n" << maxObjectRefs << "\n";
|
---|
1563 |
|
---|
1564 | //app << "#N_RAYS (number of rays / leaf)\n" << AvgRays() << endl;
|
---|
1565 |
|
---|
1566 | app << "========== END OF VspTree statistics ==========\n";
|
---|
1567 | }
|
---|
1568 |
|
---|
1569 |
|
---|
1570 | } |
---|