1 | #ifndef _BvHierarchy_H__
|
---|
2 | #define _BvHierarchy_H__
|
---|
3 |
|
---|
4 | #include <stack>
|
---|
5 |
|
---|
6 | #include "Mesh.h"
|
---|
7 | #include "Containers.h"
|
---|
8 | #include "Statistics.h"
|
---|
9 | #include "VssRay.h"
|
---|
10 | #include "RayInfo.h"
|
---|
11 | #include "gzstream.h"
|
---|
12 | #include "SubdivisionCandidate.h"
|
---|
13 | #include "AxisAlignedBox3.h"
|
---|
14 | #include "IntersectableWrapper.h"
|
---|
15 | #include "HierarchyManager.h"
|
---|
16 |
|
---|
17 |
|
---|
18 | namespace GtpVisibilityPreprocessor {
|
---|
19 |
|
---|
20 |
|
---|
21 | class ViewCellLeaf;
|
---|
22 | class Plane3;
|
---|
23 | class AxisAlignedBox3;
|
---|
24 | class Ray;
|
---|
25 | class ViewCellsStatistics;
|
---|
26 | class ViewCellsManager;
|
---|
27 | class MergeCandidate;
|
---|
28 | class Beam;
|
---|
29 | class ViewCellsTree;
|
---|
30 | class Environment;
|
---|
31 | class BvhInterior;
|
---|
32 | class BvhLeaf;
|
---|
33 | class BvhNode;
|
---|
34 | class BvhIntersectable;
|
---|
35 | class BvhTree;
|
---|
36 | class VspTree;
|
---|
37 | class ViewCellsContainer;
|
---|
38 | class HierarchyManager;
|
---|
39 |
|
---|
40 |
|
---|
41 | /** View space partition statistics.
|
---|
42 | */
|
---|
43 | class BvhStatistics: public StatisticsBase
|
---|
44 | {
|
---|
45 | public:
|
---|
46 |
|
---|
47 | /// Constructor
|
---|
48 | BvhStatistics()
|
---|
49 | {
|
---|
50 | Reset();
|
---|
51 | }
|
---|
52 |
|
---|
53 | int Nodes() const {return nodes;}
|
---|
54 | int Interior() const { return nodes / 2; }
|
---|
55 | int Leaves() const { return (nodes / 2) + 1; }
|
---|
56 |
|
---|
57 | double AvgDepth() const
|
---|
58 | { return accumDepth / (double)Leaves(); }
|
---|
59 |
|
---|
60 | double AvgObjectRefs() const
|
---|
61 | { return objectRefs / (double)Leaves(); }
|
---|
62 |
|
---|
63 | double AvgRayRefs() const
|
---|
64 | { return rayRefs / (double)Leaves(); }
|
---|
65 |
|
---|
66 |
|
---|
67 | void Reset()
|
---|
68 | {
|
---|
69 | nodes = 0;
|
---|
70 | splits = 0;
|
---|
71 | maxDepth = 0;
|
---|
72 | minDepth = 99999;
|
---|
73 | accumDepth = 0;
|
---|
74 | maxDepthNodes = 0;
|
---|
75 | minProbabilityNodes = 0;
|
---|
76 | maxCostNodes = 0;
|
---|
77 |
|
---|
78 | ///////////////////
|
---|
79 | minObjectsNodes = 0;
|
---|
80 | maxObjectRefs = 0;
|
---|
81 | minObjectRefs = 999999999;
|
---|
82 | objectRefs = 0;
|
---|
83 | emptyNodes = 0;
|
---|
84 |
|
---|
85 | ///////////////////
|
---|
86 | minRaysNodes = 0;
|
---|
87 | maxRayRefs = 0;
|
---|
88 | minRayRefs = 999999999;
|
---|
89 | rayRefs = 0;
|
---|
90 | maxRayContriNodes = 0;
|
---|
91 | mGlobalCostMisses = 0;
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | public:
|
---|
96 |
|
---|
97 | // total number of nodes
|
---|
98 | int nodes;
|
---|
99 | // number of splits
|
---|
100 | int splits;
|
---|
101 | // maximal reached depth
|
---|
102 | int maxDepth;
|
---|
103 | // minimal depth
|
---|
104 | int minDepth;
|
---|
105 | // max depth nodes
|
---|
106 | int maxDepthNodes;
|
---|
107 | // accumulated depth (used to compute average)
|
---|
108 | int accumDepth;
|
---|
109 | // minimum area nodes
|
---|
110 | int minProbabilityNodes;
|
---|
111 | /// nodes termination because of max cost ratio;
|
---|
112 | int maxCostNodes;
|
---|
113 | // global cost ratio violations
|
---|
114 | int mGlobalCostMisses;
|
---|
115 |
|
---|
116 | //////////////////
|
---|
117 | // nodes with minimum objects
|
---|
118 | int minObjectsNodes;
|
---|
119 | // max number of rays per node
|
---|
120 | int maxObjectRefs;
|
---|
121 | // min number of rays per node
|
---|
122 | int minObjectRefs;
|
---|
123 | /// object references
|
---|
124 | int objectRefs;
|
---|
125 | // leaves with no objects
|
---|
126 | int emptyNodes;
|
---|
127 |
|
---|
128 | //////////////////////////
|
---|
129 | // nodes with minimum rays
|
---|
130 | int minRaysNodes;
|
---|
131 | // max number of rays per node
|
---|
132 | int maxRayRefs;
|
---|
133 | // min number of rays per node
|
---|
134 | int minRayRefs;
|
---|
135 | /// object references
|
---|
136 | int rayRefs;
|
---|
137 | /// nodes with max ray contribution
|
---|
138 | int maxRayContriNodes;
|
---|
139 |
|
---|
140 | void Print(ostream &app) const;
|
---|
141 |
|
---|
142 | friend ostream &operator<<(ostream &s, const BvhStatistics &stat)
|
---|
143 | {
|
---|
144 | stat.Print(s);
|
---|
145 | return s;
|
---|
146 | }
|
---|
147 | };
|
---|
148 |
|
---|
149 |
|
---|
150 | /**
|
---|
151 | VspNode abstract class serving for interior and leaf node implementation
|
---|
152 | */
|
---|
153 | class BvhNode
|
---|
154 | {
|
---|
155 | public:
|
---|
156 |
|
---|
157 | // types of vsp nodes
|
---|
158 | enum {Interior, Leaf};
|
---|
159 |
|
---|
160 | BvhNode();
|
---|
161 | BvhNode(const AxisAlignedBox3 &bbox);
|
---|
162 | BvhNode(const AxisAlignedBox3 &bbox, BvhInterior *parent);
|
---|
163 |
|
---|
164 | virtual ~BvhNode(){};
|
---|
165 |
|
---|
166 | /** Determines whether this node is a leaf or not
|
---|
167 | @return true if leaf
|
---|
168 | */
|
---|
169 | virtual bool IsLeaf() const = 0;
|
---|
170 |
|
---|
171 | /** Determines whether this node is a root
|
---|
172 | @return true if root
|
---|
173 | */
|
---|
174 | virtual bool IsRoot() const;
|
---|
175 |
|
---|
176 | /** Returns parent node.
|
---|
177 | */
|
---|
178 | BvhInterior *GetParent();
|
---|
179 |
|
---|
180 | /** Sets parent node.
|
---|
181 | */
|
---|
182 | void SetParent(BvhInterior *parent);
|
---|
183 |
|
---|
184 | /** collects all objects under this node.
|
---|
185 | */
|
---|
186 | virtual void CollectObjects(ObjectContainer &objects) = 0;
|
---|
187 |
|
---|
188 | /** The bounding box specifies the node extent.
|
---|
189 | */
|
---|
190 | inline
|
---|
191 | AxisAlignedBox3 GetBoundingBox() const
|
---|
192 | { return mBoundingBox; }
|
---|
193 |
|
---|
194 | /** Sets bouding box of this node.
|
---|
195 | */
|
---|
196 | inline
|
---|
197 | void SetBoundingBox(const AxisAlignedBox3 &boundingBox)
|
---|
198 | { mBoundingBox = boundingBox; }
|
---|
199 |
|
---|
200 | /** Cost of mergin this node.
|
---|
201 | */
|
---|
202 | float GetMergeCost() {return (float)-mTimeStamp; }
|
---|
203 |
|
---|
204 | int mTimeStamp;
|
---|
205 |
|
---|
206 | int mPvsEntriesIncr;
|
---|
207 | float mMemoryIncr;
|
---|
208 | float mRenderCostDecr;
|
---|
209 |
|
---|
210 | /////////////////////////////////////
|
---|
211 | //-- mailing options
|
---|
212 |
|
---|
213 | static void NewMail(const int reserve = 1) {
|
---|
214 | sMailId += sReservedMailboxes;
|
---|
215 | sReservedMailboxes = reserve;
|
---|
216 | }
|
---|
217 |
|
---|
218 | void Mail() { mMailbox = sMailId; }
|
---|
219 | bool Mailed() const { return mMailbox == sMailId; }
|
---|
220 |
|
---|
221 | void Mail(const int mailbox) { mMailbox = sMailId + mailbox; }
|
---|
222 | bool Mailed(const int mailbox) const { return mMailbox == sMailId + mailbox; }
|
---|
223 |
|
---|
224 | int IncMail() { return ++ mMailbox - sMailId; }
|
---|
225 |
|
---|
226 | static int sMailId;
|
---|
227 | int mMailbox;
|
---|
228 | static int sReservedMailboxes;
|
---|
229 |
|
---|
230 | ///////////////////////////////////
|
---|
231 |
|
---|
232 | protected:
|
---|
233 |
|
---|
234 | /// the bounding box of the node
|
---|
235 | AxisAlignedBox3 mBoundingBox;
|
---|
236 | /// parent of this node
|
---|
237 | BvhInterior *mParent;
|
---|
238 | };
|
---|
239 |
|
---|
240 |
|
---|
241 | /** BSP interior node implementation
|
---|
242 | */
|
---|
243 | class BvhInterior: public BvhNode
|
---|
244 | {
|
---|
245 | public:
|
---|
246 | /** Standard contructor taking a bounding box as argument.
|
---|
247 | */
|
---|
248 | BvhInterior(const AxisAlignedBox3 &bbox);
|
---|
249 | BvhInterior(const AxisAlignedBox3 &bbox, BvhInterior *parent);
|
---|
250 |
|
---|
251 | ~BvhInterior();
|
---|
252 | /** @return false since it is an interior node
|
---|
253 | */
|
---|
254 | bool IsLeaf() const;
|
---|
255 |
|
---|
256 | BvhNode *GetBack() { return mBack; }
|
---|
257 | BvhNode *GetFront() { return mFront; }
|
---|
258 |
|
---|
259 | /** Replace front or back child with new child.
|
---|
260 | */
|
---|
261 | void ReplaceChildLink(BvhNode *oldChild, BvhNode *newChild);
|
---|
262 |
|
---|
263 | /** Replace front and back child.
|
---|
264 | */
|
---|
265 | void SetupChildLinks(BvhNode *front, BvhNode *back);
|
---|
266 |
|
---|
267 | friend ostream &operator<<(ostream &s, const BvhInterior &A)
|
---|
268 | {
|
---|
269 | return s << A.mBoundingBox;
|
---|
270 | }
|
---|
271 |
|
---|
272 | virtual void CollectObjects(ObjectContainer &objects);
|
---|
273 |
|
---|
274 | protected:
|
---|
275 |
|
---|
276 | /// back node
|
---|
277 | BvhNode *mBack;
|
---|
278 | /// front node
|
---|
279 | BvhNode *mFront;
|
---|
280 | };
|
---|
281 |
|
---|
282 |
|
---|
283 | /** BSP leaf node implementation.
|
---|
284 | */
|
---|
285 | class BvhLeaf: public BvhNode
|
---|
286 | {
|
---|
287 | public:
|
---|
288 | /** Standard contructor taking a bounding box as argument.
|
---|
289 | */
|
---|
290 | BvhLeaf(const AxisAlignedBox3 &bbox);
|
---|
291 | BvhLeaf(const AxisAlignedBox3 &bbox, BvhInterior *parent);
|
---|
292 | BvhLeaf(const AxisAlignedBox3 &bbox, BvhInterior *parent, const int numObjects);
|
---|
293 |
|
---|
294 | ~BvhLeaf();
|
---|
295 |
|
---|
296 | /** @return true since it is an interior node
|
---|
297 | */
|
---|
298 | bool IsLeaf() const;
|
---|
299 |
|
---|
300 | SubdivisionCandidate *GetSubdivisionCandidate()// const
|
---|
301 | {
|
---|
302 | return mSubdivisionCandidate;
|
---|
303 | }
|
---|
304 |
|
---|
305 | void SetSubdivisionCandidate(SubdivisionCandidate *candidate)
|
---|
306 | {
|
---|
307 | mSubdivisionCandidate = candidate;
|
---|
308 | }
|
---|
309 |
|
---|
310 | virtual void CollectObjects(ObjectContainer &objects);
|
---|
311 |
|
---|
312 | public:
|
---|
313 |
|
---|
314 | /// Rays piercing this leaf.
|
---|
315 | VssRayContainer mVssRays;
|
---|
316 | /// objects
|
---|
317 | ObjectContainer mObjects;
|
---|
318 | /// universal counter
|
---|
319 | int mCounter;
|
---|
320 |
|
---|
321 | protected:
|
---|
322 |
|
---|
323 | /// pointer to a split plane candidate splitting this leaf
|
---|
324 | SubdivisionCandidate *mSubdivisionCandidate;
|
---|
325 | };
|
---|
326 |
|
---|
327 |
|
---|
328 | typedef map<BvhNode *, BvhIntersectable *> BvhIntersectableMap;
|
---|
329 |
|
---|
330 |
|
---|
331 | /** View Space Partitioning tree.
|
---|
332 | */
|
---|
333 | class BvHierarchy
|
---|
334 | {
|
---|
335 | friend class ViewCellsParseHandlers;
|
---|
336 | friend class HierarchyManager;
|
---|
337 |
|
---|
338 | protected:
|
---|
339 | struct SortableEntry;
|
---|
340 | typedef vector<SortableEntry> SortableEntryContainer;
|
---|
341 |
|
---|
342 | public:
|
---|
343 |
|
---|
344 | /** Additional data which is passed down the BSP tree during traversal.
|
---|
345 | */
|
---|
346 | class BvhTraversalData
|
---|
347 | {
|
---|
348 | public:
|
---|
349 |
|
---|
350 | BvhTraversalData():
|
---|
351 | mNode(NULL),
|
---|
352 | mDepth(0),
|
---|
353 | mProbability(0.0),
|
---|
354 | mMaxCostMisses(0),
|
---|
355 | mAxis(0),
|
---|
356 | mNumRays(0)
|
---|
357 | {
|
---|
358 | mSortedObjects[0] = mSortedObjects[1] = mSortedObjects[2] = NULL;
|
---|
359 | }
|
---|
360 |
|
---|
361 | BvhTraversalData(BvhLeaf *node,
|
---|
362 | const int depth,
|
---|
363 | const float v,
|
---|
364 | const int numRays):
|
---|
365 | mNode(node),
|
---|
366 | mDepth(depth),
|
---|
367 | //mBoundingBox(box),
|
---|
368 | mProbability(v),
|
---|
369 | mMaxCostMisses(0),
|
---|
370 | mAxis(0),
|
---|
371 | mNumRays(numRays)
|
---|
372 | {
|
---|
373 | mSortedObjects[0] = mSortedObjects[1] = mSortedObjects[2] = NULL;
|
---|
374 | }
|
---|
375 |
|
---|
376 | /** Deletes contents and sets them to NULL.
|
---|
377 | */
|
---|
378 | void Clear()
|
---|
379 | {
|
---|
380 | DEL_PTR(mNode);
|
---|
381 | DEL_PTR(mSortedObjects[0]);
|
---|
382 | DEL_PTR(mSortedObjects[1]);
|
---|
383 | DEL_PTR(mSortedObjects[2]);
|
---|
384 | }
|
---|
385 |
|
---|
386 | /// the current node
|
---|
387 | BvhLeaf *mNode;
|
---|
388 | /// current depth
|
---|
389 | int mDepth;
|
---|
390 | /// the probability that this node is seen
|
---|
391 | float mProbability;
|
---|
392 | /// the bounding box of the node
|
---|
393 | //AxisAlignedBox3 mBoundingBox;
|
---|
394 | /// how often this branch has missed the max-cost ratio
|
---|
395 | int mMaxCostMisses;
|
---|
396 | /// current axis
|
---|
397 | int mAxis;
|
---|
398 | /// number of rays
|
---|
399 | int mNumRays;
|
---|
400 | /// the sorted objects for the three dimensions
|
---|
401 | ObjectContainer *mSortedObjects[3];
|
---|
402 | };
|
---|
403 |
|
---|
404 |
|
---|
405 | /** Candidate for a object space split.
|
---|
406 | */
|
---|
407 | class BvhSubdivisionCandidate: public SubdivisionCandidate
|
---|
408 | {
|
---|
409 | public:
|
---|
410 |
|
---|
411 | BvhSubdivisionCandidate(const BvhTraversalData &tData): mParentData(tData)
|
---|
412 | {};
|
---|
413 |
|
---|
414 | ~BvhSubdivisionCandidate()
|
---|
415 | {
|
---|
416 | mParentData.Clear();
|
---|
417 | }
|
---|
418 |
|
---|
419 | int Type() const { return OBJECT_SPACE; }
|
---|
420 |
|
---|
421 | void EvalCandidate(bool computeSplitplane = true)
|
---|
422 | {
|
---|
423 | sBvHierarchy->EvalSubdivisionCandidate(*this, computeSplitplane);
|
---|
424 | }
|
---|
425 |
|
---|
426 | bool Apply(SplitQueue &splitQueue, bool terminationCriteriaMet)
|
---|
427 | {
|
---|
428 | BvhNode *n = sBvHierarchy->Subdivide(splitQueue, this, terminationCriteriaMet);
|
---|
429 |
|
---|
430 | // local or global termination criteria failed
|
---|
431 | return !n->IsLeaf();
|
---|
432 | }
|
---|
433 |
|
---|
434 | void CollectDirtyCandidates(SubdivisionCandidateContainer &dirtyList,
|
---|
435 | const bool onlyUnmailed)
|
---|
436 | {
|
---|
437 | sBvHierarchy->CollectDirtyCandidates(this, dirtyList, onlyUnmailed);
|
---|
438 | }
|
---|
439 |
|
---|
440 | bool GlobalTerminationCriteriaMet() const
|
---|
441 | {
|
---|
442 | return sBvHierarchy->GlobalTerminationCriteriaMet(mParentData);
|
---|
443 | }
|
---|
444 |
|
---|
445 | BvhSubdivisionCandidate(const ObjectContainer &frontObjects,
|
---|
446 | const ObjectContainer &backObjects,
|
---|
447 | const BvhTraversalData &tData):
|
---|
448 | mFrontObjects(frontObjects), mBackObjects(backObjects), mParentData(tData)
|
---|
449 | {}
|
---|
450 |
|
---|
451 | float GetPriority() const
|
---|
452 | {
|
---|
453 | HierarchyManager *hm = sBvHierarchy->mHierarchyManager;
|
---|
454 |
|
---|
455 | if (hm->ConsiderMemory())
|
---|
456 | {
|
---|
457 | const float rc = mRenderCostDecrease / (hm->mInitialRenderCost - hm->GetHierarchyStats().mTotalCost + 1.0f);
|
---|
458 | //const float mc = mMemoryIncr / / hm->GetHierarchyStats().mMemory;
|
---|
459 | //const float rc = mPriority / (hm->mInitialRenderCost - hm->GetHierarchyStats().mTotalCost + 1.0f);
|
---|
460 | const float mc = (float)mPvsEntriesIncr / (float)hm->GetHierarchyStats().mPvsEntries;
|
---|
461 |
|
---|
462 | //cout << "\np: " << mPriority << " i: " << hm->mInitialRenderCost << " t: " << hm->GetHierarchyStats().mTotalCost << endl;
|
---|
463 | //cout << "osp rc: " << rc << " mc: " << mc << endl;
|
---|
464 |
|
---|
465 | return hm->GetMemoryConst() * rc - (1.0f - hm->GetMemoryConst()) * mc;
|
---|
466 | }
|
---|
467 | else
|
---|
468 | {
|
---|
469 | return mPriority;
|
---|
470 | }
|
---|
471 | }
|
---|
472 |
|
---|
473 | /// pointer to parent tree.
|
---|
474 | static BvHierarchy *sBvHierarchy;
|
---|
475 |
|
---|
476 | /// parent data
|
---|
477 | BvhTraversalData mParentData;
|
---|
478 | /// the objects on the front of the potential split
|
---|
479 | ObjectContainer mFrontObjects;
|
---|
480 | /// the objects on the back of the potential split
|
---|
481 | ObjectContainer mBackObjects;
|
---|
482 | };
|
---|
483 |
|
---|
484 | /** Struct for traversing line segment.
|
---|
485 | */
|
---|
486 | struct LineTraversalData
|
---|
487 | {
|
---|
488 | BvhNode *mNode;
|
---|
489 | Vector3 mExitPoint;
|
---|
490 |
|
---|
491 | float mMaxT;
|
---|
492 |
|
---|
493 | LineTraversalData () {}
|
---|
494 | LineTraversalData (BvhNode *n, const Vector3 &p, const float maxt):
|
---|
495 | mNode(n), mExitPoint(p), mMaxT(maxt) {}
|
---|
496 | };
|
---|
497 |
|
---|
498 |
|
---|
499 | /** Default constructor creating an empty tree.
|
---|
500 | */
|
---|
501 | BvHierarchy();
|
---|
502 |
|
---|
503 | /** Default destructor.
|
---|
504 | */
|
---|
505 | ~BvHierarchy();
|
---|
506 |
|
---|
507 | /** Returns tree statistics.
|
---|
508 | */
|
---|
509 | const BvhStatistics &GetStatistics() const;
|
---|
510 |
|
---|
511 | /** Returns bounding box of the specified node.
|
---|
512 | */
|
---|
513 | AxisAlignedBox3 GetBoundingBox(BvhNode *node) const;
|
---|
514 |
|
---|
515 | /** Reads parameters from environment singleton.
|
---|
516 | */
|
---|
517 | void ReadEnvironment();
|
---|
518 |
|
---|
519 | /** Evaluates candidate for splitting.
|
---|
520 | */
|
---|
521 | void EvalSubdivisionCandidate(BvhSubdivisionCandidate &splitData,
|
---|
522 | bool computeSplitPlane = true);
|
---|
523 |
|
---|
524 | /** Returns list of leaves with pvs smaller than
|
---|
525 | a certain threshold.
|
---|
526 | @param onlyUnmailed if only the unmailed leaves should be considered
|
---|
527 | @param maxPvs the maximal pvs of a leaf to be added (-1 means unlimited)
|
---|
528 | */
|
---|
529 | void CollectLeaves(vector<BvhLeaf *> &leaves) const;
|
---|
530 |
|
---|
531 | /** Returns bounding box of the whole tree (= bbox of root node)
|
---|
532 | */
|
---|
533 | AxisAlignedBox3 GetBoundingBox()const;
|
---|
534 |
|
---|
535 | /** Returns root of the view space partitioning tree.
|
---|
536 | */
|
---|
537 | BvhNode *GetRoot() const;
|
---|
538 |
|
---|
539 | /** finds neighbouring leaves of this tree node.
|
---|
540 | */
|
---|
541 | int FindNeighbors(BvhLeaf *n,
|
---|
542 | vector<BvhLeaf *> &neighbors,
|
---|
543 | const bool onlyUnmailed) const;
|
---|
544 |
|
---|
545 | /** Returns random leaf of BSP tree.
|
---|
546 | @param halfspace defines the halfspace from which the leaf is taken.
|
---|
547 | */
|
---|
548 | BvhLeaf *GetRandomLeaf(const Plane3 &halfspace);
|
---|
549 |
|
---|
550 | /** Returns random leaf of BSP tree.
|
---|
551 | @param onlyUnmailed if only unmailed leaves should be returned.
|
---|
552 | */
|
---|
553 | BvhLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
|
---|
554 |
|
---|
555 | /** Casts line segment into the tree.
|
---|
556 | @param origin the origin of the line segment
|
---|
557 | @param termination the end point of the line segment
|
---|
558 | @returns view cells intersecting the line segment.
|
---|
559 | */
|
---|
560 | int CastLineSegment(const Vector3 &origin,
|
---|
561 | const Vector3 &termination,
|
---|
562 | ViewCellContainer &viewcells);
|
---|
563 |
|
---|
564 | /** Sets pointer to view cells manager.
|
---|
565 | */
|
---|
566 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
567 |
|
---|
568 | /** Writes tree to output stream
|
---|
569 | */
|
---|
570 | bool Export(OUT_STREAM &stream);
|
---|
571 |
|
---|
572 | /** Returns or creates a new intersectable for use in a kd based pvs.
|
---|
573 | The OspTree is responsible for destruction of the intersectable.
|
---|
574 | */
|
---|
575 | BvhIntersectable *GetOrCreateBvhIntersectable(BvhNode *node);
|
---|
576 |
|
---|
577 | /** Collects rays associated with the objects.
|
---|
578 | */
|
---|
579 | void CollectRays(const ObjectContainer &objects, VssRayContainer &rays) const;
|
---|
580 |
|
---|
581 | /** Intersects box with the tree and returns the number of intersected boxes.
|
---|
582 | @returns number of view cells found
|
---|
583 | */
|
---|
584 | int ComputeBoxIntersections(const AxisAlignedBox3 &box,
|
---|
585 | ViewCellContainer &viewCells) const;
|
---|
586 |
|
---|
587 | /** Returns leaf the point pt lies in, starting from root.
|
---|
588 | */
|
---|
589 | BvhLeaf *GetLeaf(Intersectable *obj, BvhNode *root = NULL) const;
|
---|
590 |
|
---|
591 | /** Sets a pointer to the view cells tree.
|
---|
592 | */
|
---|
593 | ViewCellsTree *GetViewCellsTree() const { return mViewCellsTree; }
|
---|
594 | /** See Get
|
---|
595 | */
|
---|
596 | void SetViewCellsTree(ViewCellsTree *vt) { mViewCellsTree = vt; }
|
---|
597 |
|
---|
598 | /** Returns estimated memory usage of tree.
|
---|
599 | */
|
---|
600 | float GetMemUsage() const;
|
---|
601 |
|
---|
602 |
|
---|
603 | ///////////////////////////
|
---|
604 | // hacks in order to provide interleaved heurisitcs
|
---|
605 |
|
---|
606 | BvhNode *SubdivideAndCopy(SplitQueue &tQueue, SubdivisionCandidate *splitCandidate);
|
---|
607 |
|
---|
608 | /////////////////////////////////
|
---|
609 |
|
---|
610 | protected:
|
---|
611 |
|
---|
612 | /** Returns true if tree can be terminated.
|
---|
613 | */
|
---|
614 | bool LocalTerminationCriteriaMet(const BvhTraversalData &data) const;
|
---|
615 |
|
---|
616 | /** Returns true if global tree can be terminated.
|
---|
617 | */
|
---|
618 | bool GlobalTerminationCriteriaMet(const BvhTraversalData &data) const;
|
---|
619 |
|
---|
620 | /** For sorting the objects during the heuristics
|
---|
621 | */
|
---|
622 | struct SortableEntry
|
---|
623 | {
|
---|
624 | Intersectable *mObject;
|
---|
625 | float mPos;
|
---|
626 |
|
---|
627 | SortableEntry() {}
|
---|
628 |
|
---|
629 | SortableEntry(Intersectable *obj, const float pos):
|
---|
630 | mObject(obj), mPos(pos)
|
---|
631 | {}
|
---|
632 |
|
---|
633 | bool operator<(const SortableEntry &b) const
|
---|
634 | {
|
---|
635 | return mPos < b.mPos;
|
---|
636 | }
|
---|
637 | };
|
---|
638 |
|
---|
639 | /** Evaluate balanced object partition.
|
---|
640 | */
|
---|
641 | float EvalLocalObjectPartition(const BvhTraversalData &tData,
|
---|
642 | const int axis,
|
---|
643 | ObjectContainer &objectsFront,
|
---|
644 | ObjectContainer &objectsBack);
|
---|
645 |
|
---|
646 | /** Evaluate surface area heuristic for the node.
|
---|
647 | */
|
---|
648 | float EvalSah(const BvhTraversalData &tData,
|
---|
649 | const int axis,
|
---|
650 | ObjectContainer &objectsFront,
|
---|
651 | ObjectContainer &objectsBack);
|
---|
652 |
|
---|
653 | /** Computes priority of the traversal data and stores it in tData.
|
---|
654 | */
|
---|
655 | void EvalPriority(BvhTraversalData &tData) const;
|
---|
656 |
|
---|
657 | /** Evaluates render cost of the bv induced by these objects
|
---|
658 | */
|
---|
659 | float EvalRenderCost(const ObjectContainer &objects) const;
|
---|
660 |
|
---|
661 | /** Evaluates tree stats in the BSP tree leafs.
|
---|
662 | */
|
---|
663 | void EvaluateLeafStats(const BvhTraversalData &data);
|
---|
664 |
|
---|
665 | /** Subdivides node using a best split priority queue.
|
---|
666 | @param tQueue the best split priority queue
|
---|
667 | @param splitCandidate the candidate for the next split
|
---|
668 | @param globalCriteriaMet if the global termination criteria were already met
|
---|
669 | @returns new root of the subtree
|
---|
670 | */
|
---|
671 | BvhNode *Subdivide(SplitQueue &tQueue,
|
---|
672 | SubdivisionCandidate *splitCandidate,
|
---|
673 | const bool globalCriteriaMet);
|
---|
674 |
|
---|
675 | /** Subdivides leaf.
|
---|
676 | @param sc the subdivisionCandidate holding all necessary data for subdivision
|
---|
677 |
|
---|
678 | @param frontData returns the traversal data for the front node
|
---|
679 | @param backData returns the traversal data for the back node
|
---|
680 |
|
---|
681 | @returns the new interior node = the of the subdivision
|
---|
682 | */
|
---|
683 | BvhInterior *SubdivideNode(const BvhSubdivisionCandidate &sc,
|
---|
684 | BvhTraversalData &frontData,
|
---|
685 | BvhTraversalData &backData);
|
---|
686 |
|
---|
687 | /** Splits the objects for the next subdivision.
|
---|
688 | @returns cost for this split
|
---|
689 | */
|
---|
690 | float SelectObjectPartition(const BvhTraversalData &tData,
|
---|
691 | ObjectContainer &frontObjects,
|
---|
692 | ObjectContainer &backObjects,
|
---|
693 | bool useVisibilityBasedHeuristics);
|
---|
694 |
|
---|
695 | /** Writes the node to disk
|
---|
696 | @note: should be implemented as visitor.
|
---|
697 | */
|
---|
698 | void ExportNode(BvhNode *node, OUT_STREAM &stream);
|
---|
699 |
|
---|
700 | /** Exports objects associated with this leaf.
|
---|
701 | */
|
---|
702 | void ExportObjects(BvhLeaf *leaf, OUT_STREAM &stream);
|
---|
703 |
|
---|
704 | /** Associates the objects with their bvh leaves.
|
---|
705 | */
|
---|
706 | static void AssociateObjectsWithLeaf(BvhLeaf *leaf);
|
---|
707 |
|
---|
708 |
|
---|
709 | /////////////////////////////
|
---|
710 | // Helper functions for local cost heuristics
|
---|
711 |
|
---|
712 | /** Prepare split candidates for cost heuristics using axis aligned splits.
|
---|
713 | @param node the current node
|
---|
714 | @param axis the current split axis
|
---|
715 | */
|
---|
716 | void PrepareLocalSubdivisionCandidates(const BvhTraversalData &tData,
|
---|
717 | const int axis);
|
---|
718 |
|
---|
719 | static void CreateLocalSubdivisionCandidates(const ObjectContainer &objects,
|
---|
720 | SortableEntryContainer **subdivisionCandidates,
|
---|
721 | const bool sort,
|
---|
722 | const int axis);
|
---|
723 |
|
---|
724 | /** Computes object partition with the best cost according to the heurisics.
|
---|
725 | @param tData the traversal data
|
---|
726 | @param axis the split axis
|
---|
727 | @param objectsFront the objects in the front child bv
|
---|
728 | @param objectsBack the objects in the back child bv
|
---|
729 | @param backObjectsStart the iterator marking the position where the back objects begin
|
---|
730 |
|
---|
731 | @returns relative cost (relative to parent cost)
|
---|
732 | */
|
---|
733 | float EvalLocalCostHeuristics(const BvhTraversalData &tData,
|
---|
734 | const int axis,
|
---|
735 | ObjectContainer &objectsFront,
|
---|
736 | ObjectContainer &objectsBack);
|
---|
737 |
|
---|
738 | /** Evaluates the contribution to the front and back volume
|
---|
739 | when this object is changing sides in the bvs.
|
---|
740 |
|
---|
741 | @param object the object
|
---|
742 | @param volLeft updates the left pvs
|
---|
743 | @param volPvs updates the right pvs
|
---|
744 | */
|
---|
745 | void EvalHeuristicsContribution(Intersectable *obj,
|
---|
746 | float &volLeft,
|
---|
747 | float &volRight);
|
---|
748 |
|
---|
749 | /** Prepares objects for the cost heuristics.
|
---|
750 | @returns sum of volume of associated view cells
|
---|
751 | */
|
---|
752 | float PrepareHeuristics(const BvhTraversalData &tData, const int axis);
|
---|
753 |
|
---|
754 |
|
---|
755 | ////////////////////////////////////////////////
|
---|
756 |
|
---|
757 |
|
---|
758 | /** Prepares construction for vsp and osp trees.
|
---|
759 | */
|
---|
760 | AxisAlignedBox3 EvalBoundingBox(const ObjectContainer &objects,
|
---|
761 | const AxisAlignedBox3 *parentBox = NULL) const;
|
---|
762 |
|
---|
763 | /** Collects list of invalid candidates. Candidates
|
---|
764 | are invalidated by a view space subdivision step
|
---|
765 | that affects this candidate.
|
---|
766 | */
|
---|
767 | void CollectDirtyCandidates(BvhSubdivisionCandidate *sc,
|
---|
768 | vector<SubdivisionCandidate *> &dirtyList,
|
---|
769 | const bool onlyUnmailed);
|
---|
770 |
|
---|
771 | /** Collect view cells which see this bvh leaf.
|
---|
772 | */
|
---|
773 | void CollectViewCells(const ObjectContainer &objects,
|
---|
774 | ViewCellContainer &viewCells,
|
---|
775 | const bool setCounter = false) const;
|
---|
776 |
|
---|
777 | /** Counts the view cells of this object. note: only
|
---|
778 | counts unmailed objects.
|
---|
779 | */
|
---|
780 | int CountViewCells(Intersectable *obj) const;
|
---|
781 |
|
---|
782 | /** Counts the view cells seen by this bvh leaf
|
---|
783 | */
|
---|
784 | int CountViewCells(const ObjectContainer &objects) const;
|
---|
785 |
|
---|
786 | /** Collects view cells which see an object.
|
---|
787 | */
|
---|
788 | void CollectViewCells(Intersectable *object,
|
---|
789 | ViewCellContainer &viewCells,
|
---|
790 | const bool useMailBoxing,
|
---|
791 | const bool setCounter = false) const;
|
---|
792 |
|
---|
793 | /** Evaluates increase in pvs size.
|
---|
794 | */
|
---|
795 | int EvalPvsEntriesIncr(BvhSubdivisionCandidate &splitCandidate) const;
|
---|
796 |
|
---|
797 | /** Rays will be clipped to the bounding box.
|
---|
798 | */
|
---|
799 | void PreprocessRays(BvhLeaf *root,
|
---|
800 | const VssRayContainer &sampleRays,
|
---|
801 | RayInfoContainer &rays);
|
---|
802 |
|
---|
803 | /** Print the subdivision stats in the subdivison log.
|
---|
804 | */
|
---|
805 | void PrintSubdivisionStats(const SubdivisionCandidate &tData);
|
---|
806 |
|
---|
807 | /** Prints out the stats for this subdivision.
|
---|
808 | */
|
---|
809 | void AddSubdivisionStats(const int viewCells,
|
---|
810 | const float renderCostDecr,
|
---|
811 | const float totalRenderCost);
|
---|
812 |
|
---|
813 | /** Stores rays with objects that see the rays.
|
---|
814 | */
|
---|
815 | int AssociateObjectsWithRays(const VssRayContainer &rays) const;
|
---|
816 |
|
---|
817 | /** Tests if object is in this leaf.
|
---|
818 | @note: assumes that objects are sorted by their id.
|
---|
819 | */
|
---|
820 | bool IsObjectInLeaf(BvhLeaf *, Intersectable *object) const;
|
---|
821 |
|
---|
822 | /** Prepares the construction of the bv hierarchy and returns
|
---|
823 | the first subdivision candidate.
|
---|
824 | */
|
---|
825 | SubdivisionCandidate *PrepareConstruction(const VssRayContainer &sampleRays,
|
---|
826 | const ObjectContainer &objects);
|
---|
827 |
|
---|
828 | /** Resets bv hierarchy. E.g. deletes root and resets stats.
|
---|
829 | */
|
---|
830 | SubdivisionCandidate *Reset(const VssRayContainer &rays,
|
---|
831 | const ObjectContainer &objects);
|
---|
832 |
|
---|
833 | /** Evaluates volume of view cells that see the objects.
|
---|
834 | */
|
---|
835 | float EvalViewCellsVolume(const ObjectContainer &objects) const;
|
---|
836 |
|
---|
837 | /** Assigns or newly creates initial list of sorted objects.
|
---|
838 | */
|
---|
839 | void AssignInitialSortedObjectList(BvhTraversalData &tData);
|
---|
840 |
|
---|
841 | /** Assigns sorted objects to front and back data.
|
---|
842 | */
|
---|
843 | void AssignSortedObjects(const BvhSubdivisionCandidate &sc,
|
---|
844 | BvhTraversalData &frontData,
|
---|
845 | BvhTraversalData &backData);
|
---|
846 |
|
---|
847 | /** Creates new root of hierarchy and computes bounding box.
|
---|
848 | Has to be called before the preparation of the subdivision.
|
---|
849 | */
|
---|
850 | void Initialise(const ObjectContainer &objects);
|
---|
851 |
|
---|
852 |
|
---|
853 | protected:
|
---|
854 |
|
---|
855 | /// pre-sorted subdivision candidtes for all three directions.
|
---|
856 | vector<SortableEntry> *mGlobalSubdivisionCandidates[3];
|
---|
857 | /// pointer to the hierarchy of view cells
|
---|
858 | ViewCellsTree *mViewCellsTree;
|
---|
859 | /// The view cells manager
|
---|
860 | ViewCellsManager *mViewCellsManager;
|
---|
861 | /// candidates for placing split planes during cost heuristics
|
---|
862 | vector<SortableEntry> *mSubdivisionCandidates;
|
---|
863 | /// Pointer to the root of the tree
|
---|
864 | BvhNode *mRoot;
|
---|
865 | /// Statistics for the object space partition
|
---|
866 | BvhStatistics mBvhStats;
|
---|
867 | /// box around the whole view domain
|
---|
868 | AxisAlignedBox3 mBoundingBox;
|
---|
869 | /// the hierarchy manager
|
---|
870 | HierarchyManager *mHierarchyManager;
|
---|
871 |
|
---|
872 |
|
---|
873 | ////////////////////
|
---|
874 | //-- local termination criteria
|
---|
875 |
|
---|
876 | /// maximal possible depth
|
---|
877 | int mTermMaxDepth;
|
---|
878 | /// mininum probability
|
---|
879 | float mTermMinProbability;
|
---|
880 | /// minimal number of objects
|
---|
881 | int mTermMinObjects;
|
---|
882 | /// maximal acceptable cost ratio
|
---|
883 | float mTermMaxCostRatio;
|
---|
884 | /// tolerance value indicating how often the max cost ratio can be failed
|
---|
885 | int mTermMissTolerance;
|
---|
886 | /// minimum number of rays
|
---|
887 | int mTermMinRays;
|
---|
888 |
|
---|
889 |
|
---|
890 | ////////////////////
|
---|
891 | //-- global termination criteria
|
---|
892 |
|
---|
893 | /// the minimal accepted global cost ratio
|
---|
894 | float mTermMinGlobalCostRatio;
|
---|
895 | //// number of accepted misses of the global cost ratio
|
---|
896 | int mTermGlobalCostMissTolerance;
|
---|
897 | /// maximal number of view cells
|
---|
898 | int mTermMaxLeaves;
|
---|
899 | /// maximal tree memory
|
---|
900 | float mMaxMemory;
|
---|
901 | /// the tree is out of memory
|
---|
902 | bool mOutOfMemory;
|
---|
903 |
|
---|
904 |
|
---|
905 | ////////////////////////////////////////
|
---|
906 | //-- split heuristics based parameters
|
---|
907 |
|
---|
908 | /// if a heuristics should be used for finding a split plane
|
---|
909 | bool mUseCostHeuristics;
|
---|
910 | /// if sah heuristcs should be used for finding a split plane
|
---|
911 | bool mUseSah;
|
---|
912 | /// balancing factor for PVS criterium
|
---|
913 | float mCtDivCi;
|
---|
914 | /// if only driving axis should be used for split
|
---|
915 | bool mOnlyDrivingAxis;
|
---|
916 | /// current time stamp (used for keeping split history)
|
---|
917 | int mTimeStamp;
|
---|
918 | // if rays should be stored in leaves
|
---|
919 | bool mStoreRays;
|
---|
920 | // subdivision stats output file
|
---|
921 | ofstream mSubdivisionStats;
|
---|
922 | /// keeps track of cost during subdivision
|
---|
923 | float mTotalCost;
|
---|
924 | int mPvsEntries;
|
---|
925 | /// keeps track of overall pvs size during subdivision
|
---|
926 | int mTotalPvsSize;
|
---|
927 | /// number of currenly generated view cells
|
---|
928 | int mCreatedLeaves;
|
---|
929 | /// represents min and max band for sweep
|
---|
930 | float mSplitBorder;
|
---|
931 | /// weight between render cost decrease and node render cost
|
---|
932 | float mRenderCostDecreaseWeight;
|
---|
933 | /// stores the kd node intersectables used for pvs
|
---|
934 | BvhIntersectableMap mBvhIntersectables;
|
---|
935 | /// if the objects should be sorted in one global step
|
---|
936 | bool mUseGlobalSorting;
|
---|
937 |
|
---|
938 | bool mUseBboxAreaForSah;
|
---|
939 |
|
---|
940 | SortableEntryContainer *mSortedObjects[3];
|
---|
941 |
|
---|
942 | int mMinRaysForVisibility;
|
---|
943 | };
|
---|
944 |
|
---|
945 | }
|
---|
946 |
|
---|
947 | #endif
|
---|