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