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