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