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