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