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 |
|
---|
73 | minDepth = 99999;
|
---|
74 | accumDepth = 0;
|
---|
75 | maxDepthNodes = 0;
|
---|
76 | minProbabilityNodes = 0;
|
---|
77 | maxCostNodes = 0;
|
---|
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 | /** Returns level of the hierarchy that is "active" right now.
|
---|
313 | */
|
---|
314 | BvhNode *GetActiveNode()
|
---|
315 | {
|
---|
316 | return mActiveNode;
|
---|
317 | }
|
---|
318 |
|
---|
319 | /** Returns level of the hierarchy that is "active" right now.
|
---|
320 | */
|
---|
321 | void SetActiveNode(BvhNode *node)
|
---|
322 | {
|
---|
323 | mActiveNode = node;
|
---|
324 | }
|
---|
325 |
|
---|
326 | public:
|
---|
327 |
|
---|
328 | /// Rays piercing this leaf.
|
---|
329 | //VssRayContainer mVssRays;
|
---|
330 | /// objects
|
---|
331 | ObjectContainer mObjects;
|
---|
332 | /// universal counter
|
---|
333 | int mCounter;
|
---|
334 |
|
---|
335 | protected:
|
---|
336 |
|
---|
337 | /// pointer to a split plane candidate splitting this leaf
|
---|
338 | SubdivisionCandidate *mSubdivisionCandidate;
|
---|
339 |
|
---|
340 | /// the active node which will be accounted for in the pvs
|
---|
341 | BvhNode *mActiveNode;
|
---|
342 | };
|
---|
343 |
|
---|
344 |
|
---|
345 | typedef map<BvhLeaf *, BvhIntersectable *> BvhIntersectableMap;
|
---|
346 |
|
---|
347 |
|
---|
348 | /** View Space Partitioning tree.
|
---|
349 | */
|
---|
350 | class BvHierarchy
|
---|
351 | {
|
---|
352 | friend class ViewCellsParseHandlers;
|
---|
353 | friend class HierarchyManager;
|
---|
354 |
|
---|
355 | protected:
|
---|
356 | struct SortableEntry;
|
---|
357 | typedef vector<SortableEntry> SortableEntryContainer;
|
---|
358 |
|
---|
359 | public:
|
---|
360 |
|
---|
361 | /** Additional data which is passed down the BSP tree during traversal.
|
---|
362 | */
|
---|
363 | class BvhTraversalData
|
---|
364 | {
|
---|
365 | public:
|
---|
366 |
|
---|
367 | BvhTraversalData():
|
---|
368 | mNode(NULL),
|
---|
369 | mDepth(0),
|
---|
370 | mProbability(0.0),
|
---|
371 | mMaxCostMisses(0),
|
---|
372 | mAxis(0),
|
---|
373 | mNumRays(0)
|
---|
374 | {
|
---|
375 | mSortedObjects[0] = mSortedObjects[1] = mSortedObjects[2] = NULL;
|
---|
376 | }
|
---|
377 |
|
---|
378 | BvhTraversalData(BvhLeaf *node,
|
---|
379 | const int depth,
|
---|
380 | const float v,
|
---|
381 | const int numRays):
|
---|
382 | mNode(node),
|
---|
383 | mDepth(depth),
|
---|
384 | //mBoundingBox(box),
|
---|
385 | mProbability(v),
|
---|
386 | mMaxCostMisses(0),
|
---|
387 | mAxis(0),
|
---|
388 | mNumRays(numRays)
|
---|
389 | {
|
---|
390 | mSortedObjects[0] = mSortedObjects[1] = mSortedObjects[2] = NULL;
|
---|
391 | }
|
---|
392 |
|
---|
393 | /** Deletes contents and sets them to NULL.
|
---|
394 | */
|
---|
395 | void Clear()
|
---|
396 | {
|
---|
397 | DEL_PTR(mNode);
|
---|
398 | DEL_PTR(mSortedObjects[0]);
|
---|
399 | DEL_PTR(mSortedObjects[1]);
|
---|
400 | DEL_PTR(mSortedObjects[2]);
|
---|
401 | }
|
---|
402 |
|
---|
403 | /// the current node
|
---|
404 | BvhLeaf *mNode;
|
---|
405 | /// current depth
|
---|
406 | int mDepth;
|
---|
407 | /// the probability that this node is seen
|
---|
408 | float mProbability;
|
---|
409 | /// the bounding box of the node
|
---|
410 | //AxisAlignedBox3 mBoundingBox;
|
---|
411 | /// how often this branch has missed the max-cost ratio
|
---|
412 | int mMaxCostMisses;
|
---|
413 | /// current axis
|
---|
414 | int mAxis;
|
---|
415 | /// number of rays
|
---|
416 | int mNumRays;
|
---|
417 | /// the sorted objects for the three dimensions
|
---|
418 | ObjectContainer *mSortedObjects[3];
|
---|
419 | };
|
---|
420 |
|
---|
421 |
|
---|
422 | /** Candidate for a object space split.
|
---|
423 | */
|
---|
424 | class BvhSubdivisionCandidate: public SubdivisionCandidate
|
---|
425 | {
|
---|
426 | public:
|
---|
427 |
|
---|
428 | BvhSubdivisionCandidate(const BvhTraversalData &tData): mParentData(tData)
|
---|
429 | {};
|
---|
430 |
|
---|
431 | ~BvhSubdivisionCandidate()
|
---|
432 | {
|
---|
433 | mParentData.Clear();
|
---|
434 | }
|
---|
435 |
|
---|
436 | int Type() const { return OBJECT_SPACE; }
|
---|
437 |
|
---|
438 | void EvalCandidate(bool computeSplitplane = true)
|
---|
439 | {
|
---|
440 | mDirty = false;
|
---|
441 | sBvHierarchy->EvalSubdivisionCandidate(*this, computeSplitplane);
|
---|
442 | }
|
---|
443 |
|
---|
444 | bool Apply(SplitQueue &splitQueue, bool terminationCriteriaMet)
|
---|
445 | {
|
---|
446 | BvhNode *n = sBvHierarchy->Subdivide(splitQueue, this, terminationCriteriaMet);
|
---|
447 |
|
---|
448 | // local or global termination criteria failed
|
---|
449 | return !n->IsLeaf();
|
---|
450 | }
|
---|
451 |
|
---|
452 | void CollectDirtyCandidates(SubdivisionCandidateContainer &dirtyList,
|
---|
453 | const bool onlyUnmailed)
|
---|
454 | {
|
---|
455 | sBvHierarchy->CollectDirtyCandidates(this, dirtyList, onlyUnmailed);
|
---|
456 | }
|
---|
457 |
|
---|
458 | bool GlobalTerminationCriteriaMet() const
|
---|
459 | {
|
---|
460 | return sBvHierarchy->GlobalTerminationCriteriaMet(mParentData);
|
---|
461 | }
|
---|
462 |
|
---|
463 | BvhSubdivisionCandidate(const ObjectContainer &frontObjects,
|
---|
464 | const ObjectContainer &backObjects,
|
---|
465 | const BvhTraversalData &tData):
|
---|
466 | mFrontObjects(frontObjects), mBackObjects(backObjects), mParentData(tData)
|
---|
467 | {}
|
---|
468 |
|
---|
469 | float GetPriority() const
|
---|
470 | {
|
---|
471 | return mPriority;
|
---|
472 | }
|
---|
473 |
|
---|
474 | /// pointer to parent tree.
|
---|
475 | static BvHierarchy *sBvHierarchy;
|
---|
476 |
|
---|
477 | /// parent data
|
---|
478 | BvhTraversalData mParentData;
|
---|
479 | /// the objects on the front of the potential split
|
---|
480 | ObjectContainer mFrontObjects;
|
---|
481 | /// the objects on the back of the potential split
|
---|
482 | ObjectContainer mBackObjects;
|
---|
483 | };
|
---|
484 |
|
---|
485 | /** Struct for traversing line segment.
|
---|
486 | */
|
---|
487 | struct LineTraversalData
|
---|
488 | {
|
---|
489 | BvhNode *mNode;
|
---|
490 | Vector3 mExitPoint;
|
---|
491 |
|
---|
492 | float mMaxT;
|
---|
493 |
|
---|
494 | LineTraversalData () {}
|
---|
495 | LineTraversalData (BvhNode *n, const Vector3 &p, const float maxt):
|
---|
496 | mNode(n), mExitPoint(p), mMaxT(maxt) {}
|
---|
497 | };
|
---|
498 |
|
---|
499 |
|
---|
500 | /** Default constructor creating an empty tree.
|
---|
501 | */
|
---|
502 | BvHierarchy();
|
---|
503 |
|
---|
504 | /** Default destructor.
|
---|
505 | */
|
---|
506 | ~BvHierarchy();
|
---|
507 |
|
---|
508 | /** Returns tree statistics.
|
---|
509 | */
|
---|
510 | const BvhStatistics &GetStatistics() const;
|
---|
511 |
|
---|
512 | /** Returns bounding box of the specified node.
|
---|
513 | */
|
---|
514 | AxisAlignedBox3 GetBoundingBox(BvhNode *node) const;
|
---|
515 |
|
---|
516 | /** Reads parameters from environment singleton.
|
---|
517 | */
|
---|
518 | void ReadEnvironment();
|
---|
519 |
|
---|
520 | /** Evaluates candidate for splitting.
|
---|
521 | */
|
---|
522 | void EvalSubdivisionCandidate(BvhSubdivisionCandidate &splitData,
|
---|
523 | bool computeSplitPlane = true);
|
---|
524 |
|
---|
525 | /** Returns vector of leaves.
|
---|
526 | */
|
---|
527 | void CollectLeaves(BvhNode *root, vector<BvhLeaf *> &leaves) const;
|
---|
528 |
|
---|
529 | /** Returns bounding box of the whole tree (= bbox of root node)
|
---|
530 | */
|
---|
531 | AxisAlignedBox3 GetBoundingBox()const;
|
---|
532 |
|
---|
533 | /** Returns root of the view space partitioning tree.
|
---|
534 | */
|
---|
535 | BvhNode *GetRoot() const;
|
---|
536 |
|
---|
537 | /** finds neighbouring leaves of this tree node.
|
---|
538 | */
|
---|
539 | int FindNeighbors(BvhLeaf *n,
|
---|
540 | vector<BvhLeaf *> &neighbors,
|
---|
541 | const bool onlyUnmailed) const;
|
---|
542 |
|
---|
543 | /** Returns random leaf of BSP tree.
|
---|
544 | @param halfspace defines the halfspace from which the leaf is taken.
|
---|
545 | */
|
---|
546 | BvhLeaf *GetRandomLeaf(const Plane3 &halfspace);
|
---|
547 |
|
---|
548 | /** Returns random leaf of BSP tree.
|
---|
549 | @param onlyUnmailed if only unmailed leaves should be returned.
|
---|
550 | */
|
---|
551 | BvhLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
|
---|
552 |
|
---|
553 | /** Casts line segment into the tree.
|
---|
554 | @param origin the origin of the line segment
|
---|
555 | @param termination the end point of the line segment
|
---|
556 | @returns view cells intersecting the line segment.
|
---|
557 | */
|
---|
558 | int CastLineSegment(const Vector3 &origin,
|
---|
559 | const Vector3 &termination,
|
---|
560 | ViewCellContainer &viewcells);
|
---|
561 |
|
---|
562 | /** Sets pointer to view cells manager.
|
---|
563 | */
|
---|
564 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
565 |
|
---|
566 | /** Writes tree to output stream
|
---|
567 | */
|
---|
568 | bool Export(OUT_STREAM &stream);
|
---|
569 |
|
---|
570 | /** Returns or creates a new intersectable for use in a bv based pvs.
|
---|
571 | The hierarchy is responsible for destruction of the intersectable.
|
---|
572 | */
|
---|
573 | BvhIntersectable *GetOrCreateBvhIntersectable(BvhLeaf *node);
|
---|
574 |
|
---|
575 | /** Collects rays associated with the objects.
|
---|
576 | */
|
---|
577 | void CollectRays(const ObjectContainer &objects, VssRayContainer &rays) const;
|
---|
578 |
|
---|
579 | /** Intersects box with the tree and returns the number of intersected boxes.
|
---|
580 | @returns number of view cells found
|
---|
581 | */
|
---|
582 | int ComputeBoxIntersections(const AxisAlignedBox3 &box,
|
---|
583 | ViewCellContainer &viewCells) const;
|
---|
584 |
|
---|
585 | /** Returns leaf the point pt lies in, starting from root.
|
---|
586 | */
|
---|
587 | BvhLeaf *GetLeaf(Intersectable *obj, BvhNode *root = NULL) const;
|
---|
588 |
|
---|
589 | /** Sets a pointer to the view cells tree.
|
---|
590 | */
|
---|
591 | ViewCellsTree *GetViewCellsTree() const { return mViewCellsTree; }
|
---|
592 |
|
---|
593 | /** See Get
|
---|
594 | */
|
---|
595 | void SetViewCellsTree(ViewCellsTree *vt) { mViewCellsTree = vt; }
|
---|
596 |
|
---|
597 | /** Returns estimated memory usage of tree.
|
---|
598 | */
|
---|
599 | float GetMemUsage() const;
|
---|
600 |
|
---|
601 | /** Sets this node to be an active node.
|
---|
602 | */
|
---|
603 | void SetActive(BvhNode *node) const;
|
---|
604 |
|
---|
605 |
|
---|
606 | ///////////////////////////
|
---|
607 | // hacks in order to provide interleaved heurisitcs
|
---|
608 |
|
---|
609 | BvhNode *SubdivideAndCopy(SplitQueue &tQueue, SubdivisionCandidate *splitCandidate);
|
---|
610 |
|
---|
611 | /////////////////////////////////
|
---|
612 |
|
---|
613 | static float EvalAbsCost(const ObjectContainer &objects);
|
---|
614 |
|
---|
615 | void CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects);
|
---|
616 |
|
---|
617 | protected:
|
---|
618 |
|
---|
619 | /** Returns true if tree can be terminated.
|
---|
620 | */
|
---|
621 | bool LocalTerminationCriteriaMet(const BvhTraversalData &data) const;
|
---|
622 |
|
---|
623 | /** Returns true if global tree can be terminated.
|
---|
624 | */
|
---|
625 | bool GlobalTerminationCriteriaMet(const BvhTraversalData &data) const;
|
---|
626 |
|
---|
627 | /** For sorting the objects during the heuristics
|
---|
628 | */
|
---|
629 | struct SortableEntry
|
---|
630 | {
|
---|
631 | Intersectable *mObject;
|
---|
632 | float mPos;
|
---|
633 |
|
---|
634 | SortableEntry() {}
|
---|
635 |
|
---|
636 | SortableEntry(Intersectable *obj, const float pos):
|
---|
637 | mObject(obj), mPos(pos)
|
---|
638 | {}
|
---|
639 |
|
---|
640 | bool operator<(const SortableEntry &b) const
|
---|
641 | {
|
---|
642 | return mPos < b.mPos;
|
---|
643 | }
|
---|
644 | };
|
---|
645 |
|
---|
646 | /** Evaluate balanced object partition.
|
---|
647 | */
|
---|
648 | float EvalLocalObjectPartition(const BvhTraversalData &tData,
|
---|
649 | const int axis,
|
---|
650 | ObjectContainer &objectsFront,
|
---|
651 | ObjectContainer &objectsBack);
|
---|
652 |
|
---|
653 | /** Evaluate surface area heuristic for the node.
|
---|
654 | */
|
---|
655 | float EvalSah(const BvhTraversalData &tData,
|
---|
656 | const int axis,
|
---|
657 | ObjectContainer &objectsFront,
|
---|
658 | ObjectContainer &objectsBack);
|
---|
659 |
|
---|
660 | /** Computes priority of the traversal data and stores it in tData.
|
---|
661 | */
|
---|
662 | void EvalPriority(BvhTraversalData &tData) const;
|
---|
663 |
|
---|
664 | /** Evaluates render cost of the bv induced by these objects
|
---|
665 | */
|
---|
666 | float EvalRenderCost(const ObjectContainer &objects) const;
|
---|
667 |
|
---|
668 | /** Evaluates tree stats in the BSP tree leafs.
|
---|
669 | */
|
---|
670 | void EvaluateLeafStats(const BvhTraversalData &data);
|
---|
671 |
|
---|
672 | /** Subdivides node using a best split priority queue.
|
---|
673 | @param tQueue the best split priority queue
|
---|
674 | @param splitCandidate the candidate for the next split
|
---|
675 | @param globalCriteriaMet if the global termination criteria were already met
|
---|
676 | @returns new root of the subtree
|
---|
677 | */
|
---|
678 | BvhNode *Subdivide(SplitQueue &tQueue,
|
---|
679 | SubdivisionCandidate *splitCandidate,
|
---|
680 | const bool globalCriteriaMet);
|
---|
681 |
|
---|
682 | /** Subdivides leaf.
|
---|
683 | @param sc the subdivisionCandidate holding all necessary data for subdivision
|
---|
684 |
|
---|
685 | @param frontData returns the traversal data for the front node
|
---|
686 | @param backData returns the traversal data for the back node
|
---|
687 |
|
---|
688 | @returns the new interior node = the of the subdivision
|
---|
689 | */
|
---|
690 | BvhInterior *SubdivideNode(const BvhSubdivisionCandidate &sc,
|
---|
691 | BvhTraversalData &frontData,
|
---|
692 | BvhTraversalData &backData);
|
---|
693 |
|
---|
694 | /** Splits the objects for the next subdivision.
|
---|
695 | @returns cost for this split
|
---|
696 | */
|
---|
697 | float SelectObjectPartition(const BvhTraversalData &tData,
|
---|
698 | ObjectContainer &frontObjects,
|
---|
699 | ObjectContainer &backObjects,
|
---|
700 | bool useVisibilityBasedHeuristics);
|
---|
701 |
|
---|
702 | /** Writes the node to disk
|
---|
703 | @note: should be implemented as visitor.
|
---|
704 | */
|
---|
705 | void ExportNode(BvhNode *node, OUT_STREAM &stream);
|
---|
706 |
|
---|
707 | /** Exports objects associated with this leaf.
|
---|
708 | */
|
---|
709 | void ExportObjects(BvhLeaf *leaf, OUT_STREAM &stream);
|
---|
710 |
|
---|
711 | /** Associates the objects with their bvh leaves.
|
---|
712 | */
|
---|
713 | static void AssociateObjectsWithLeaf(BvhLeaf *leaf);
|
---|
714 |
|
---|
715 |
|
---|
716 | /////////////////////////////
|
---|
717 | // Helper functions for local cost heuristics
|
---|
718 |
|
---|
719 | /** Prepare split candidates for cost heuristics using axis aligned splits.
|
---|
720 | @param node the current node
|
---|
721 | @param axis the current split axis
|
---|
722 | */
|
---|
723 | void PrepareLocalSubdivisionCandidates(const BvhTraversalData &tData,
|
---|
724 | const int axis);
|
---|
725 |
|
---|
726 | static void CreateLocalSubdivisionCandidates(const ObjectContainer &objects,
|
---|
727 | SortableEntryContainer **subdivisionCandidates,
|
---|
728 | const bool sort,
|
---|
729 | const int axis);
|
---|
730 |
|
---|
731 | /** Computes object partition with the best cost according to the heurisics.
|
---|
732 | @param tData the traversal data
|
---|
733 | @param axis the split axis
|
---|
734 | @param objectsFront the objects in the front child bv
|
---|
735 | @param objectsBack the objects in the back child bv
|
---|
736 | @param backObjectsStart the iterator marking the position where the back objects begin
|
---|
737 |
|
---|
738 | @returns relative cost (relative to parent cost)
|
---|
739 | */
|
---|
740 | float EvalLocalCostHeuristics(const BvhTraversalData &tData,
|
---|
741 | const int axis,
|
---|
742 | ObjectContainer &objectsFront,
|
---|
743 | ObjectContainer &objectsBack);
|
---|
744 |
|
---|
745 | /** Evaluates the contribution to the front and back volume
|
---|
746 | when this object is changing sides in the bvs.
|
---|
747 |
|
---|
748 | @param object the object
|
---|
749 | @param volLeft updates the left pvs
|
---|
750 | @param volPvs updates the right pvs
|
---|
751 | */
|
---|
752 | void EvalHeuristicsContribution(Intersectable *obj,
|
---|
753 | float &volLeft,
|
---|
754 | float &volRight);
|
---|
755 |
|
---|
756 | /** Prepares objects for the cost heuristics.
|
---|
757 | @returns sum of volume of associated view cells
|
---|
758 | */
|
---|
759 | float PrepareHeuristics(const BvhTraversalData &tData, const int axis);
|
---|
760 |
|
---|
761 | /** Evaluates cost for a leaf given the surface area heuristics.
|
---|
762 | */
|
---|
763 | float EvalSahCost(BvhLeaf *leaf);
|
---|
764 |
|
---|
765 | ////////////////////////////////////////////////
|
---|
766 |
|
---|
767 |
|
---|
768 | /** Prepares construction for vsp and osp trees.
|
---|
769 | */
|
---|
770 | AxisAlignedBox3 EvalBoundingBox(const ObjectContainer &objects,
|
---|
771 | const AxisAlignedBox3 *parentBox = NULL) const;
|
---|
772 |
|
---|
773 | /** Collects list of invalid candidates. Candidates
|
---|
774 | are invalidated by a view space subdivision step
|
---|
775 | that affects this candidate.
|
---|
776 | */
|
---|
777 | void CollectDirtyCandidates(BvhSubdivisionCandidate *sc,
|
---|
778 | vector<SubdivisionCandidate *> &dirtyList,
|
---|
779 | const bool onlyUnmailed);
|
---|
780 |
|
---|
781 | /** Collect view cells which see this bvh leaf.
|
---|
782 | */
|
---|
783 | int CollectViewCells(const ObjectContainer &objects,
|
---|
784 | ViewCellContainer &viewCells,
|
---|
785 | const bool setCounter,
|
---|
786 | const bool onlyUnmailedRays) const;
|
---|
787 |
|
---|
788 | /** Counts the view cells of this object. note: only
|
---|
789 | counts unmailed objects.
|
---|
790 | */
|
---|
791 | int CountViewCells(Intersectable *obj) const;
|
---|
792 |
|
---|
793 | /** Counts the view cells seen by this bvh leaf
|
---|
794 | */
|
---|
795 | int CountViewCells(const ObjectContainer &objects) const;
|
---|
796 |
|
---|
797 | /** Collects view cells which see an object.
|
---|
798 | */
|
---|
799 | int CollectViewCells(Intersectable *object,
|
---|
800 | ViewCellContainer &viewCells,
|
---|
801 | const bool useMailBoxing,
|
---|
802 | const bool setCounter,
|
---|
803 | const bool onlyUnmailedRays) const;
|
---|
804 |
|
---|
805 | /** Evaluates increase in pvs size.
|
---|
806 | */
|
---|
807 | int EvalPvsEntriesIncr(BvhSubdivisionCandidate &splitCandidate) const;
|
---|
808 |
|
---|
809 | /** Rays will be clipped to the bounding box.
|
---|
810 | */
|
---|
811 | void PreprocessRays(BvhLeaf *root,
|
---|
812 | const VssRayContainer &sampleRays,
|
---|
813 | RayInfoContainer &rays);
|
---|
814 |
|
---|
815 | /** Print the subdivision stats in the subdivison log.
|
---|
816 | */
|
---|
817 | void PrintSubdivisionStats(const SubdivisionCandidate &tData);
|
---|
818 |
|
---|
819 | /** Prints out the stats for this subdivision.
|
---|
820 | */
|
---|
821 | void AddSubdivisionStats(const int viewCells,
|
---|
822 | const float renderCostDecr,
|
---|
823 | const float totalRenderCost);
|
---|
824 |
|
---|
825 | /** Stores rays with objects that see the rays.
|
---|
826 | */
|
---|
827 | int AssociateObjectsWithRays(const VssRayContainer &rays) const;
|
---|
828 |
|
---|
829 | /** Tests if object is in this leaf.
|
---|
830 | @note: assumes that objects are sorted by their id.
|
---|
831 | */
|
---|
832 | bool IsObjectInLeaf(BvhLeaf *, Intersectable *object) const;
|
---|
833 |
|
---|
834 | /** Prepares the construction of the bv hierarchy and returns
|
---|
835 | the first subdivision candidate.
|
---|
836 | */
|
---|
837 | SubdivisionCandidate *PrepareConstruction(const VssRayContainer &sampleRays,
|
---|
838 | const ObjectContainer &objects);
|
---|
839 |
|
---|
840 | /** Resets bv hierarchy. E.g. deletes root and resets stats.
|
---|
841 | */
|
---|
842 | SubdivisionCandidate *Reset(const VssRayContainer &rays,
|
---|
843 | const ObjectContainer &objects);
|
---|
844 |
|
---|
845 | /** Evaluates volume of view cells that see the objects.
|
---|
846 | */
|
---|
847 | float EvalViewCellsVolume(const ObjectContainer &objects) const;
|
---|
848 |
|
---|
849 | /** Assigns or newly creates initial list of sorted objects.
|
---|
850 | */
|
---|
851 | void AssignInitialSortedObjectList(BvhTraversalData &tData);
|
---|
852 |
|
---|
853 | /** Assigns sorted objects to front and back data.
|
---|
854 | */
|
---|
855 | void AssignSortedObjects(const BvhSubdivisionCandidate &sc,
|
---|
856 | BvhTraversalData &frontData,
|
---|
857 | BvhTraversalData &backData);
|
---|
858 |
|
---|
859 | /** Creates new root of hierarchy and computes bounding box.
|
---|
860 | Has to be called before the preparation of the subdivision.
|
---|
861 | */
|
---|
862 | void Initialise(const ObjectContainer &objects);
|
---|
863 |
|
---|
864 |
|
---|
865 | protected:
|
---|
866 |
|
---|
867 | /// pre-sorted subdivision candidtes for all three directions.
|
---|
868 | vector<SortableEntry> *mGlobalSubdivisionCandidates[3];
|
---|
869 | /// pointer to the hierarchy of view cells
|
---|
870 | ViewCellsTree *mViewCellsTree;
|
---|
871 | /// The view cells manager
|
---|
872 | ViewCellsManager *mViewCellsManager;
|
---|
873 | /// candidates for placing split planes during cost heuristics
|
---|
874 | vector<SortableEntry> *mSubdivisionCandidates;
|
---|
875 | /// Pointer to the root of the tree
|
---|
876 | BvhNode *mRoot;
|
---|
877 | /// Statistics for the object space partition
|
---|
878 | BvhStatistics mBvhStats;
|
---|
879 | /// box around the whole view domain
|
---|
880 | AxisAlignedBox3 mBoundingBox;
|
---|
881 | /// the hierarchy manager
|
---|
882 | HierarchyManager *mHierarchyManager;
|
---|
883 |
|
---|
884 |
|
---|
885 | ////////////////////
|
---|
886 | //-- local termination criteria
|
---|
887 |
|
---|
888 | /// maximal possible depth
|
---|
889 | int mTermMaxDepth;
|
---|
890 | /// mininum probability
|
---|
891 | float mTermMinProbability;
|
---|
892 | /// minimal number of objects
|
---|
893 | int mTermMinObjects;
|
---|
894 | /// maximal acceptable cost ratio
|
---|
895 | float mTermMaxCostRatio;
|
---|
896 | /// tolerance value indicating how often the max cost ratio can be failed
|
---|
897 | int mTermMissTolerance;
|
---|
898 | /// minimum number of rays
|
---|
899 | int mTermMinRays;
|
---|
900 |
|
---|
901 |
|
---|
902 | ////////////////////
|
---|
903 | //-- global termination criteria
|
---|
904 |
|
---|
905 | /// the minimal accepted global cost ratio
|
---|
906 | float mTermMinGlobalCostRatio;
|
---|
907 | //// number of accepted misses of the global cost ratio
|
---|
908 | int mTermGlobalCostMissTolerance;
|
---|
909 | /// maximal number of view cells
|
---|
910 | int mTermMaxLeaves;
|
---|
911 | /// maximal tree memory
|
---|
912 | float mMaxMemory;
|
---|
913 | /// the tree is out of memory
|
---|
914 | bool mOutOfMemory;
|
---|
915 |
|
---|
916 |
|
---|
917 | ////////////////////////////////////////
|
---|
918 | //-- split heuristics based parameters
|
---|
919 |
|
---|
920 | /// if a heuristics should be used for finding a split plane
|
---|
921 | bool mUseCostHeuristics;
|
---|
922 | /// if sah heuristcs should be used for finding a split plane
|
---|
923 | bool mUseSah;
|
---|
924 | /// balancing factor for PVS criterium
|
---|
925 | float mCtDivCi;
|
---|
926 | /// if only driving axis should be used for split
|
---|
927 | bool mOnlyDrivingAxis;
|
---|
928 | /// current time stamp (used for keeping split history)
|
---|
929 | int mTimeStamp;
|
---|
930 | // if rays should be stored in leaves
|
---|
931 | bool mStoreRays;
|
---|
932 | // subdivision stats output file
|
---|
933 | ofstream mSubdivisionStats;
|
---|
934 | /// keeps track of cost during subdivision
|
---|
935 | float mTotalCost;
|
---|
936 | int mPvsEntries;
|
---|
937 | /// keeps track of overall pvs size during subdivision
|
---|
938 | int mTotalPvsSize;
|
---|
939 | /// number of currenly generated view cells
|
---|
940 | int mCreatedLeaves;
|
---|
941 | /// represents min and max band for sweep
|
---|
942 | float mSplitBorder;
|
---|
943 | /// weight between render cost decrease and node render cost
|
---|
944 | float mRenderCostDecreaseWeight;
|
---|
945 | /// stores the kd node intersectables used for pvs
|
---|
946 | BvhIntersectableMap mBvhIntersectables;
|
---|
947 | /// if the objects should be sorted in one global step
|
---|
948 | bool mUseGlobalSorting;
|
---|
949 |
|
---|
950 | bool mUseBboxAreaForSah;
|
---|
951 |
|
---|
952 | SortableEntryContainer *mSortedObjects[3];
|
---|
953 |
|
---|
954 | int mMinRaysForVisibility;
|
---|
955 |
|
---|
956 | /// constant value for driving the heuristics
|
---|
957 | float mMemoryConst;
|
---|
958 |
|
---|
959 | int mMaxTests;
|
---|
960 | };
|
---|
961 |
|
---|
962 | }
|
---|
963 |
|
---|
964 | #endif
|
---|