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