1 | #ifndef _VspTree_H__
|
---|
2 | #define _VspTree_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 |
|
---|
14 |
|
---|
15 | namespace GtpVisibilityPreprocessor {
|
---|
16 |
|
---|
17 | class ViewCellLeaf;
|
---|
18 | class Plane3;
|
---|
19 | class AxisAlignedBox3;
|
---|
20 | class Ray;
|
---|
21 | class ViewCellsStatistics;
|
---|
22 | class ViewCellsManager;
|
---|
23 | class MergeCandidate;
|
---|
24 | class Beam;
|
---|
25 | class ViewCellsTree;
|
---|
26 | class Environment;
|
---|
27 | class VspInterior;
|
---|
28 | class VspLeaf;
|
---|
29 | class VspNode;
|
---|
30 | class KdNode;
|
---|
31 | class KdInterior;
|
---|
32 | class KdLeaf;
|
---|
33 | class HierarchyManager;
|
---|
34 | class KdIntersectable;
|
---|
35 | class KdTree;
|
---|
36 | class VspTree;
|
---|
37 | class KdTreeStatistics;
|
---|
38 | //class SubdivisionCandidate;
|
---|
39 | //class VspSubdivisionCandidate;
|
---|
40 |
|
---|
41 | /** View space partition statistics.
|
---|
42 | */
|
---|
43 | class VspTreeStatistics: public StatisticsBase
|
---|
44 | {
|
---|
45 | public:
|
---|
46 | // total number of nodes
|
---|
47 | int nodes;
|
---|
48 | // number of splits
|
---|
49 | int splits[3];
|
---|
50 |
|
---|
51 | // totals number of rays
|
---|
52 | int rays;
|
---|
53 | // maximal reached depth
|
---|
54 | int maxDepth;
|
---|
55 | // minimal depth
|
---|
56 | int minDepth;
|
---|
57 |
|
---|
58 | // max depth nodes
|
---|
59 | int maxDepthNodes;
|
---|
60 | // minimum depth nodes
|
---|
61 | int minDepthNodes;
|
---|
62 | // max depth nodes
|
---|
63 | int minPvsNodes;
|
---|
64 | // nodes with minimum PVS
|
---|
65 | int minRaysNodes;
|
---|
66 | // max ray contribution nodes
|
---|
67 | int maxRayContribNodes;
|
---|
68 | // minimum area nodes
|
---|
69 | int minProbabilityNodes;
|
---|
70 | /// nodes termination because of max cost ratio;
|
---|
71 | int maxCostNodes;
|
---|
72 | // max number of rays per node
|
---|
73 | int maxObjectRefs;
|
---|
74 | /// samples contributing to pvs
|
---|
75 | int contributingSamples;
|
---|
76 | /// sample contributions to pvs
|
---|
77 | int sampleContributions;
|
---|
78 | /// largest pvs
|
---|
79 | int maxPvs;
|
---|
80 | /// number of invalid leaves
|
---|
81 | int invalidLeaves;
|
---|
82 | /// accumulated number of rays refs
|
---|
83 | int accumRays;
|
---|
84 | /// overall pvs size
|
---|
85 | int pvs;
|
---|
86 | // accumulated depth (used to compute average)
|
---|
87 | int accumDepth;
|
---|
88 | // global cost ratio violations
|
---|
89 | int mGlobalCostMisses;
|
---|
90 |
|
---|
91 | // Constructor
|
---|
92 | VspTreeStatistics()
|
---|
93 | {
|
---|
94 | Reset();
|
---|
95 | }
|
---|
96 |
|
---|
97 | int Nodes() const {return nodes;}
|
---|
98 | int Interior() const { return nodes / 2; }
|
---|
99 | int Leaves() const { return (nodes / 2) + 1; }
|
---|
100 |
|
---|
101 | // TODO: computation wrong
|
---|
102 | double AvgDepth() const { return accumDepth / (double)Leaves();};
|
---|
103 | double AvgRays() const { return accumRays / (double)Leaves();};
|
---|
104 |
|
---|
105 | void Reset()
|
---|
106 | {
|
---|
107 | nodes = 0;
|
---|
108 | for (int i = 0; i < 3; ++ i)
|
---|
109 | splits[i] = 0;
|
---|
110 |
|
---|
111 | maxDepth = 0;
|
---|
112 | minDepth = 99999;
|
---|
113 | accumDepth = 0;
|
---|
114 | pvs = 0;
|
---|
115 | maxDepthNodes = 0;
|
---|
116 | minPvsNodes = 0;
|
---|
117 | minRaysNodes = 0;
|
---|
118 | maxRayContribNodes = 0;
|
---|
119 | minProbabilityNodes = 0;
|
---|
120 | maxCostNodes = 0;
|
---|
121 |
|
---|
122 | contributingSamples = 0;
|
---|
123 | sampleContributions = 0;
|
---|
124 |
|
---|
125 | maxPvs = 0;
|
---|
126 | invalidLeaves = 0;
|
---|
127 | accumRays = 0;
|
---|
128 | maxObjectRefs = 0;
|
---|
129 | mGlobalCostMisses = 0;
|
---|
130 | }
|
---|
131 |
|
---|
132 | void Print(ostream &app) const;
|
---|
133 |
|
---|
134 | friend ostream &operator<<(ostream &s, const VspTreeStatistics &stat)
|
---|
135 | {
|
---|
136 | stat.Print(s);
|
---|
137 | return s;
|
---|
138 | }
|
---|
139 | };
|
---|
140 |
|
---|
141 |
|
---|
142 | /**
|
---|
143 | VspNode abstract class serving for interior and leaf node implementation
|
---|
144 | */
|
---|
145 | class VspNode
|
---|
146 | {
|
---|
147 | public:
|
---|
148 |
|
---|
149 | // types of vsp nodes
|
---|
150 | enum {Interior, Leaf};
|
---|
151 |
|
---|
152 | VspNode();
|
---|
153 | virtual ~VspNode(){};
|
---|
154 | VspNode(VspInterior *parent);
|
---|
155 |
|
---|
156 | /** Determines whether this node is a leaf or not
|
---|
157 | @return true if leaf
|
---|
158 | */
|
---|
159 | virtual bool IsLeaf() const = 0;
|
---|
160 |
|
---|
161 | virtual int Type() const = 0;
|
---|
162 |
|
---|
163 | /** Determines whether this node is a root
|
---|
164 | @return true if root
|
---|
165 | */
|
---|
166 | virtual bool IsRoot() const;
|
---|
167 | /** Returns parent node.
|
---|
168 | */
|
---|
169 | VspInterior *GetParent();
|
---|
170 | /** Sets parent node.
|
---|
171 | */
|
---|
172 | void SetParent(VspInterior *parent);
|
---|
173 | /** Returns true if this node is a sibling of node n.
|
---|
174 | */
|
---|
175 | bool IsSibling(VspNode *n) const;
|
---|
176 | /** returns depth of the node.
|
---|
177 | */
|
---|
178 | int GetDepth() const;
|
---|
179 | /** returns true if the whole subtree is valid
|
---|
180 | */
|
---|
181 | bool TreeValid() const;
|
---|
182 |
|
---|
183 | void SetTreeValid(const bool v);
|
---|
184 |
|
---|
185 | //-- mailing options
|
---|
186 |
|
---|
187 | void Mail() { mMailbox = sMailId; }
|
---|
188 | static void NewMail() { ++ sMailId; }
|
---|
189 | bool Mailed() const { return mMailbox == sMailId; }
|
---|
190 |
|
---|
191 |
|
---|
192 | static int sMailId;
|
---|
193 | int mMailbox;
|
---|
194 |
|
---|
195 | int mTimeStamp;
|
---|
196 |
|
---|
197 | protected:
|
---|
198 |
|
---|
199 | /// if this sub tree is a completely valid view space region
|
---|
200 | bool mTreeValid;
|
---|
201 | /// parent of this node
|
---|
202 | VspInterior *mParent;
|
---|
203 | };
|
---|
204 |
|
---|
205 |
|
---|
206 | /** BSP interior node implementation
|
---|
207 | */
|
---|
208 | class VspInterior: public VspNode
|
---|
209 | {
|
---|
210 | public:
|
---|
211 | /** Standard contructor taking split plane as argument.
|
---|
212 | */
|
---|
213 | VspInterior(const AxisAlignedPlane &plane);
|
---|
214 |
|
---|
215 | ~VspInterior();
|
---|
216 | /** @return false since it is an interior node
|
---|
217 | */
|
---|
218 | bool IsLeaf() const;
|
---|
219 |
|
---|
220 | int Type() const;
|
---|
221 |
|
---|
222 | VspNode *GetBack();
|
---|
223 | VspNode *GetFront();
|
---|
224 |
|
---|
225 | /** Returns split plane.
|
---|
226 | */
|
---|
227 | AxisAlignedPlane GetPlane() const;
|
---|
228 |
|
---|
229 | /** Returns position of split plane.
|
---|
230 | */
|
---|
231 | float GetPosition() const;
|
---|
232 |
|
---|
233 | /** Returns split axis.
|
---|
234 | */
|
---|
235 | int GetAxis() const;
|
---|
236 |
|
---|
237 | /** Replace front or back child with new child.
|
---|
238 | */
|
---|
239 | void ReplaceChildLink(VspNode *oldChild, VspNode *newChild);
|
---|
240 |
|
---|
241 | /** Replace front and back child.
|
---|
242 | */
|
---|
243 | void SetupChildLinks(VspNode *front, VspNode *back);
|
---|
244 |
|
---|
245 | friend ostream &operator<<(ostream &s, const VspInterior &A)
|
---|
246 | {
|
---|
247 | return s << A.mPlane.mAxis << " " << A.mPlane.mPosition;
|
---|
248 | }
|
---|
249 |
|
---|
250 | AxisAlignedBox3 GetBoundingBox() const;
|
---|
251 | void SetBoundingBox(const AxisAlignedBox3 &box);
|
---|
252 |
|
---|
253 | /** Computes intersection of this plane with the ray segment.
|
---|
254 | */
|
---|
255 | int ComputeRayIntersection(const RayInfo &rayData, float &t) const
|
---|
256 | {
|
---|
257 | return rayData.ComputeRayIntersection(mPlane.mAxis, mPlane.mPosition, t);
|
---|
258 | }
|
---|
259 |
|
---|
260 |
|
---|
261 | protected:
|
---|
262 | /// bounding box for this interior node: should we really store this?
|
---|
263 | AxisAlignedBox3 mBoundingBox;
|
---|
264 |
|
---|
265 | /// Splitting plane corresponding to this node
|
---|
266 | AxisAlignedPlane mPlane;
|
---|
267 |
|
---|
268 | /// back node
|
---|
269 | VspNode *mBack;
|
---|
270 | /// front node
|
---|
271 | VspNode *mFront;
|
---|
272 | };
|
---|
273 |
|
---|
274 |
|
---|
275 | /** BSP leaf node implementation.
|
---|
276 | */
|
---|
277 | class VspLeaf: public VspNode
|
---|
278 | {
|
---|
279 | friend VspTree;
|
---|
280 |
|
---|
281 | public:
|
---|
282 | VspLeaf();
|
---|
283 | VspLeaf(ViewCellLeaf *viewCell);
|
---|
284 | VspLeaf(VspInterior *parent);
|
---|
285 | VspLeaf(VspInterior *parent, ViewCellLeaf *viewCell);
|
---|
286 |
|
---|
287 | ~VspLeaf();
|
---|
288 |
|
---|
289 | /** @return true since it is an interior node
|
---|
290 | */
|
---|
291 | bool IsLeaf() const;
|
---|
292 |
|
---|
293 | int Type() const;
|
---|
294 |
|
---|
295 | /** Returns pointer of view cell.
|
---|
296 | */
|
---|
297 | ViewCellLeaf *GetViewCell() const;
|
---|
298 | /** Sets pointer to view cell.
|
---|
299 | */
|
---|
300 | void SetViewCell(ViewCellLeaf *viewCell);
|
---|
301 |
|
---|
302 | SubdivisionCandidate *GetSubdivisionCandidate()
|
---|
303 | {
|
---|
304 | return mSubdivisionCandidate;
|
---|
305 | }
|
---|
306 |
|
---|
307 | void SetSubdivisionCandidate(SubdivisionCandidate *candidate)
|
---|
308 | {
|
---|
309 | mSubdivisionCandidate = candidate;
|
---|
310 | }
|
---|
311 |
|
---|
312 |
|
---|
313 | public:
|
---|
314 |
|
---|
315 | /// Rays piercing this leaf.
|
---|
316 | VssRayContainer mVssRays;
|
---|
317 | /// leaf pvs
|
---|
318 | ObjectPvs *mPvs;
|
---|
319 | /// Probability that the view point lies in this leaf
|
---|
320 | float mProbability;
|
---|
321 |
|
---|
322 | protected:
|
---|
323 |
|
---|
324 | /// pointer to a split plane candidate splitting this leaf
|
---|
325 | SubdivisionCandidate *mSubdivisionCandidate;
|
---|
326 | /// if NULL this does not correspond to feasible viewcell
|
---|
327 | ViewCellLeaf *mViewCell;
|
---|
328 | };
|
---|
329 |
|
---|
330 |
|
---|
331 | /** View Space Partitioning tree.
|
---|
332 | */
|
---|
333 | class VspTree
|
---|
334 | {
|
---|
335 | friend class ViewCellsParseHandlers;
|
---|
336 | friend class HierarchyManager;
|
---|
337 |
|
---|
338 | public:
|
---|
339 |
|
---|
340 | /** Additional data which is passed down the BSP tree during traversal.
|
---|
341 | */
|
---|
342 | class VspTraversalData
|
---|
343 | {
|
---|
344 | public:
|
---|
345 |
|
---|
346 | /** Returns average ray contribution.
|
---|
347 | */
|
---|
348 | float GetAvgRayContribution() const
|
---|
349 | {
|
---|
350 | return (float)mPvs / ((float)mRays->size() + Limits::Small);
|
---|
351 | }
|
---|
352 |
|
---|
353 |
|
---|
354 | VspTraversalData():
|
---|
355 | mNode(NULL),
|
---|
356 | mDepth(0),
|
---|
357 | mRays(NULL),
|
---|
358 | mPvs(0),
|
---|
359 | mProbability(0.0),
|
---|
360 | mMaxCostMisses(0),
|
---|
361 | mPriority(0)
|
---|
362 | {}
|
---|
363 |
|
---|
364 | VspTraversalData(VspLeaf *node,
|
---|
365 | const int depth,
|
---|
366 | RayInfoContainer *rays,
|
---|
367 | const int pvs,
|
---|
368 | const float p,
|
---|
369 | const AxisAlignedBox3 &box):
|
---|
370 | mNode(node),
|
---|
371 | mDepth(depth),
|
---|
372 | mRays(rays),
|
---|
373 | mPvs(pvs),
|
---|
374 | mProbability(p),
|
---|
375 | mBoundingBox(box),
|
---|
376 | mMaxCostMisses(0),
|
---|
377 | mPriority(0)
|
---|
378 | {}
|
---|
379 |
|
---|
380 | VspTraversalData(const int depth,
|
---|
381 | RayInfoContainer *rays,
|
---|
382 | const AxisAlignedBox3 &box):
|
---|
383 | mNode(NULL),
|
---|
384 | mDepth(depth),
|
---|
385 | mRays(rays),
|
---|
386 | mPvs(0),
|
---|
387 | mProbability(0),
|
---|
388 | mMaxCostMisses(0),
|
---|
389 | mBoundingBox(box)
|
---|
390 | {}
|
---|
391 |
|
---|
392 | /** Returns cost of the traversal data.
|
---|
393 | */
|
---|
394 | float GetCost() const
|
---|
395 | {
|
---|
396 | return mPriority;
|
---|
397 | }
|
---|
398 |
|
---|
399 | /// deletes contents and sets them to NULL
|
---|
400 | void Clear()
|
---|
401 | {
|
---|
402 | DEL_PTR(mRays);
|
---|
403 |
|
---|
404 | if (mNode)
|
---|
405 | {
|
---|
406 | // delete old view cell
|
---|
407 | delete mNode->GetViewCell();
|
---|
408 | delete mNode;
|
---|
409 | mNode = NULL;
|
---|
410 | }
|
---|
411 | }
|
---|
412 |
|
---|
413 | /// the current node
|
---|
414 | VspLeaf *mNode;
|
---|
415 | /// current depth
|
---|
416 | int mDepth;
|
---|
417 | /// rays piercing this node
|
---|
418 | RayInfoContainer *mRays;
|
---|
419 | /// the probability that this node contains view point
|
---|
420 | float mProbability;
|
---|
421 | /// the bounding box of the node
|
---|
422 | AxisAlignedBox3 mBoundingBox;
|
---|
423 | /// pvs size
|
---|
424 | int mPvs;
|
---|
425 | /// how often this branch has missed the max-cost ratio
|
---|
426 | int mMaxCostMisses;
|
---|
427 | // current priority
|
---|
428 | float mPriority;
|
---|
429 |
|
---|
430 |
|
---|
431 | friend bool operator<(const VspTraversalData &a, const VspTraversalData &b)
|
---|
432 | {
|
---|
433 | return a.GetCost() < b.GetCost();
|
---|
434 | }
|
---|
435 | };
|
---|
436 |
|
---|
437 | /** Candidate for a view space split.
|
---|
438 | */
|
---|
439 | class VspSubdivisionCandidate: public SubdivisionCandidate
|
---|
440 | {
|
---|
441 | public:
|
---|
442 |
|
---|
443 | static VspTree* sVspTree;
|
---|
444 |
|
---|
445 | /// the current split plane
|
---|
446 | AxisAlignedPlane mSplitPlane;
|
---|
447 | /// parent node traversal data
|
---|
448 | VspTraversalData mParentData;
|
---|
449 |
|
---|
450 | VspSubdivisionCandidate(const VspTraversalData &tData): mParentData(tData)
|
---|
451 | {};
|
---|
452 |
|
---|
453 | ~VspSubdivisionCandidate()
|
---|
454 | {
|
---|
455 | mParentData.Clear();
|
---|
456 | }
|
---|
457 |
|
---|
458 | int Type() const { return VIEW_SPACE; }
|
---|
459 |
|
---|
460 | void EvalPriority()
|
---|
461 | {
|
---|
462 | sVspTree->EvalSubdivisionCandidate(*this);
|
---|
463 | }
|
---|
464 |
|
---|
465 | bool GlobalTerminationCriteriaMet() const
|
---|
466 | {
|
---|
467 | return sVspTree->GlobalTerminationCriteriaMet(mParentData);
|
---|
468 | }
|
---|
469 |
|
---|
470 | VspSubdivisionCandidate(
|
---|
471 | const AxisAlignedPlane &plane,
|
---|
472 | const VspTraversalData &tData):
|
---|
473 | mSplitPlane(plane), mParentData(tData)
|
---|
474 | {}
|
---|
475 | };
|
---|
476 |
|
---|
477 | /** Struct for traversing line segment.
|
---|
478 | */
|
---|
479 | struct LineTraversalData
|
---|
480 | {
|
---|
481 | VspNode *mNode;
|
---|
482 | Vector3 mExitPoint;
|
---|
483 | float mMaxT;
|
---|
484 |
|
---|
485 | LineTraversalData () {}
|
---|
486 | LineTraversalData (VspNode *n, const Vector3 &p, const float maxt):
|
---|
487 | mNode(n), mExitPoint(p), mMaxT(maxt) {}
|
---|
488 | };
|
---|
489 |
|
---|
490 | /** Default constructor creating an empty tree.
|
---|
491 | */
|
---|
492 | VspTree();
|
---|
493 | /** Default destructor.
|
---|
494 | */
|
---|
495 | ~VspTree();
|
---|
496 |
|
---|
497 | /** Returns BSP Tree statistics.
|
---|
498 | */
|
---|
499 | const VspTreeStatistics &GetStatistics() const;
|
---|
500 |
|
---|
501 | /** Returns bounding box of the specified node.
|
---|
502 | */
|
---|
503 | AxisAlignedBox3 GetBoundingBox(VspNode *node) const;
|
---|
504 |
|
---|
505 | /** Returns list of BSP leaves with pvs smaller than
|
---|
506 | a certain threshold.
|
---|
507 | @param onlyUnmailed if only the unmailed leaves should be considered
|
---|
508 | @param maxPvs the maximal pvs of a leaf to be added (-1 means unlimited)
|
---|
509 | */
|
---|
510 | void CollectLeaves(vector<VspLeaf *> &leaves,
|
---|
511 | const bool onlyUnmailed = false,
|
---|
512 | const int maxPvs = -1) const;
|
---|
513 |
|
---|
514 | /** Returns box which bounds the whole tree.
|
---|
515 | */
|
---|
516 | AxisAlignedBox3 GetBoundingBox() const;
|
---|
517 |
|
---|
518 | /** Returns root of the view space partitioning tree.
|
---|
519 | */
|
---|
520 | VspNode *GetRoot() const;
|
---|
521 |
|
---|
522 | /** Collects the leaf view cells of the tree
|
---|
523 | @param viewCells returns the view cells
|
---|
524 | */
|
---|
525 | void CollectViewCells(ViewCellContainer &viewCells, bool onlyValid) const;
|
---|
526 |
|
---|
527 | /** A ray is cast possible intersecting the tree.
|
---|
528 | @param the ray that is cast.
|
---|
529 | @returns the number of intersections with objects stored in the tree.
|
---|
530 | */
|
---|
531 | int CastRay(Ray &ray);
|
---|
532 |
|
---|
533 |
|
---|
534 | /** finds neighbouring leaves of this tree node.
|
---|
535 | */
|
---|
536 | int FindNeighbors(VspLeaf *n,
|
---|
537 | vector<VspLeaf *> &neighbors,
|
---|
538 | const bool onlyUnmailed) const;
|
---|
539 |
|
---|
540 | /** Returns random leaf of BSP tree.
|
---|
541 | @param halfspace defines the halfspace from which the leaf is taken.
|
---|
542 | */
|
---|
543 | VspLeaf *GetRandomLeaf(const Plane3 &halfspace);
|
---|
544 |
|
---|
545 | /** Returns random leaf of BSP tree.
|
---|
546 | @param onlyUnmailed if only unmailed leaves should be returned.
|
---|
547 | */
|
---|
548 | VspLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
|
---|
549 |
|
---|
550 | /** Returns epsilon of this tree.
|
---|
551 | */
|
---|
552 | float GetEpsilon() const;
|
---|
553 |
|
---|
554 | /** Casts line segment into the tree.
|
---|
555 | @param origin the origin of the line segment
|
---|
556 | @param termination the end point of the line segment
|
---|
557 | @returns view cells intersecting the line segment.
|
---|
558 | */
|
---|
559 | int CastLineSegment(const Vector3 &origin,
|
---|
560 | const Vector3 &termination,
|
---|
561 | ViewCellContainer &viewcells,
|
---|
562 | const bool useMailboxing = true);
|
---|
563 |
|
---|
564 |
|
---|
565 | /** Sets pointer to view cells manager.
|
---|
566 | */
|
---|
567 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
568 |
|
---|
569 | /** Returns view cell the current point is located in.
|
---|
570 | @param point the current view point
|
---|
571 | @param active if currently active view cells should be returned or
|
---|
572 | elementary view cell
|
---|
573 | */
|
---|
574 | ViewCell *GetViewCell(const Vector3 &point, const bool active = false);
|
---|
575 |
|
---|
576 |
|
---|
577 | /** Returns true if this view point is in a valid view space,
|
---|
578 | false otherwise.
|
---|
579 | */
|
---|
580 | bool ViewPointValid(const Vector3 &viewPoint) const;
|
---|
581 |
|
---|
582 | /** Returns view cell corresponding to
|
---|
583 | the invalid view space.
|
---|
584 | */
|
---|
585 | VspViewCell *GetOutOfBoundsCell();
|
---|
586 |
|
---|
587 | /** Writes tree to output stream
|
---|
588 | */
|
---|
589 | bool Export(OUT_STREAM &stream);
|
---|
590 |
|
---|
591 | /** Casts beam, i.e. a 5D frustum of rays, into tree.
|
---|
592 | Tests conservative using the bounding box of the nodes.
|
---|
593 | @returns number of view cells it intersected
|
---|
594 | */
|
---|
595 | int CastBeam(Beam &beam);
|
---|
596 |
|
---|
597 | /** Checks if tree validity-flags are right
|
---|
598 | with respect to view cell valitiy.
|
---|
599 | If not, marks subtree as invalid.
|
---|
600 | */
|
---|
601 | void ValidateTree();
|
---|
602 |
|
---|
603 | /** Invalid view cells are added to the unbounded space
|
---|
604 | */
|
---|
605 | void CollapseViewCells();
|
---|
606 |
|
---|
607 | /** Collects rays stored in the leaves.
|
---|
608 | */
|
---|
609 | void CollectRays(VssRayContainer &rays);
|
---|
610 |
|
---|
611 | /** Intersects box with the tree and returns the number of intersected boxes.
|
---|
612 | @returns number of view cells found
|
---|
613 | */
|
---|
614 | int ComputeBoxIntersections(const AxisAlignedBox3 &box,
|
---|
615 | ViewCellContainer &viewCells) const;
|
---|
616 |
|
---|
617 | /** Remove the references of the parent view cell from the kd nodes associated with
|
---|
618 | the objects.
|
---|
619 | */
|
---|
620 | void RemoveParentViewCellReferences(ViewCell *parent) const;
|
---|
621 |
|
---|
622 | /** Adds references to the view cell to the kd nodes associated with the objects.
|
---|
623 | */
|
---|
624 | void AddViewCellReferences(ViewCell *vc) const;
|
---|
625 |
|
---|
626 | /** Returns view cells of this ray, either taking precomputed cells
|
---|
627 | or by recomputation.
|
---|
628 | */
|
---|
629 | void GetViewCells(const VssRay &ray, ViewCellContainer &viewCells);
|
---|
630 |
|
---|
631 | /** Returns view cells tree.
|
---|
632 | */
|
---|
633 | ViewCellsTree *GetViewCellsTree() const { return mViewCellsTree; }
|
---|
634 |
|
---|
635 | /** Sets the view cells tree.
|
---|
636 | */
|
---|
637 | void SetViewCellsTree(ViewCellsTree *vt) { mViewCellsTree = vt; }
|
---|
638 |
|
---|
639 |
|
---|
640 | protected:
|
---|
641 |
|
---|
642 | // --------------------------------------------------------------
|
---|
643 | // For sorting objects
|
---|
644 | // --------------------------------------------------------------
|
---|
645 | struct SortableEntry
|
---|
646 | {
|
---|
647 | enum EType
|
---|
648 | {
|
---|
649 | ERayMin,
|
---|
650 | ERayMax
|
---|
651 | };
|
---|
652 |
|
---|
653 | int type;
|
---|
654 | float value;
|
---|
655 | VssRay *ray;
|
---|
656 |
|
---|
657 | SortableEntry() {}
|
---|
658 | SortableEntry(const int t, const float v, VssRay *r):
|
---|
659 | type(t), value(v), ray(r)
|
---|
660 | {
|
---|
661 | }
|
---|
662 |
|
---|
663 | friend inline bool operator<(const SortableEntry &a, const SortableEntry &b)
|
---|
664 | { // prefer max event
|
---|
665 | //if (EpsilonEqual(a.value, b.value, 0.0001f))
|
---|
666 | // return (a.type == ERayMax);
|
---|
667 |
|
---|
668 | return (a.value < b.value);
|
---|
669 | }
|
---|
670 | };
|
---|
671 |
|
---|
672 | /** faster evaluation of split plane cost for kd axis aligned cells.
|
---|
673 | */
|
---|
674 | float EvalLocalSplitCost(const VspTraversalData &data,
|
---|
675 | const AxisAlignedBox3 &box,
|
---|
676 | const int axis,
|
---|
677 | const float &position,
|
---|
678 | float &pFront,
|
---|
679 | float &pBack) const;
|
---|
680 |
|
---|
681 | void ComputeBoundingBox(const VssRayContainer &rays,
|
---|
682 | AxisAlignedBox3 *forcedBoundingBox);
|
---|
683 |
|
---|
684 | /** Evaluates candidate for splitting.
|
---|
685 | */
|
---|
686 | void EvalSubdivisionCandidate(VspSubdivisionCandidate &splitData);
|
---|
687 |
|
---|
688 | /** Evaluates render cost decrease of next split.
|
---|
689 | */
|
---|
690 | float EvalRenderCostDecrease(const AxisAlignedPlane &candidatePlane,
|
---|
691 | const VspTraversalData &data,
|
---|
692 | float &normalizedOldRenderCost) const;
|
---|
693 |
|
---|
694 | /** Collects view cells in the subtree under root.
|
---|
695 | */
|
---|
696 | void CollectViewCells(VspNode *root,
|
---|
697 | bool onlyValid,
|
---|
698 | ViewCellContainer &viewCells,
|
---|
699 | bool onlyUnmailed = false) const;
|
---|
700 |
|
---|
701 | /** Returns view cell corresponding to
|
---|
702 | the invalid view space. If it does not exist, it is created.
|
---|
703 | */
|
---|
704 | VspViewCell *GetOrCreateOutOfBoundsCell();
|
---|
705 |
|
---|
706 | /** Collapses the tree with respect to the view cell partition,
|
---|
707 | i.e. leaves having the same view cell are collapsed.
|
---|
708 | @param node the root of the subtree to be collapsed
|
---|
709 | @param collapsed returns the number of collapsed nodes
|
---|
710 | @returns node of type leaf if the node could be collapsed,
|
---|
711 | this node otherwise
|
---|
712 | */
|
---|
713 | VspNode *CollapseTree(VspNode *node, int &collapsed);
|
---|
714 |
|
---|
715 | /** Helper function revalidating the view cell leaf list after merge.
|
---|
716 | */
|
---|
717 | void RepairViewCellsLeafLists();
|
---|
718 |
|
---|
719 | /** Evaluates tree stats in the BSP tree leafs.
|
---|
720 | */
|
---|
721 | void EvaluateLeafStats(const VspTraversalData &data);
|
---|
722 |
|
---|
723 | /** Subdivides node using a best split priority queue.
|
---|
724 | @param tQueue the best split priority queue
|
---|
725 | @param splitCandidate the candidate for the next split
|
---|
726 | @param globalCriteriaMet if the global termination criteria were already met
|
---|
727 | @returns new root of the subtree
|
---|
728 | */
|
---|
729 | VspNode *Subdivide(SplitQueue &tQueue,
|
---|
730 | SubdivisionCandidate *splitCandidate,
|
---|
731 | const bool globalCriteriaMet);
|
---|
732 |
|
---|
733 | /** Adds stats to subdivision log file.
|
---|
734 | */
|
---|
735 | void AddSubdivisionStats(const int viewCells,
|
---|
736 | const float renderCostDecr,
|
---|
737 | const float totalRenderCost,
|
---|
738 | const float avgRenderCost);
|
---|
739 |
|
---|
740 | /** Subdivides leaf.
|
---|
741 |
|
---|
742 | @param tData data object holding, e.g., a pointer to the leaf
|
---|
743 | @param frontData returns the data (e.g., pointer to the leaf) in front of the split plane
|
---|
744 | @param backData returns the data (e.g., pointer to the leaf) in the back of the split plane
|
---|
745 |
|
---|
746 | @param rays the polygons to be filtered
|
---|
747 | @param frontRays returns the polygons in front of the split plane
|
---|
748 |
|
---|
749 | @returns the root of the subdivision
|
---|
750 | */
|
---|
751 | VspInterior *SubdivideNode(const AxisAlignedPlane &splitPlane,
|
---|
752 | VspTraversalData &tData,
|
---|
753 | VspTraversalData &frontData,
|
---|
754 | VspTraversalData &backData);
|
---|
755 |
|
---|
756 | /** Selects an axis aligned for the next split.
|
---|
757 | @returns cost for this split
|
---|
758 | */
|
---|
759 | float SelectSplitPlane(const VspTraversalData &tData,
|
---|
760 | AxisAlignedPlane &plane,
|
---|
761 | float &pFront,
|
---|
762 | float &pBack);
|
---|
763 |
|
---|
764 | /** Sorts split candidates along the specified axis.
|
---|
765 | The split candidates are generated on possible visibility
|
---|
766 | events (i.e., where ray segments intersect the ray boundaries).
|
---|
767 | The sorted candidates are needed to compute the heuristics.
|
---|
768 |
|
---|
769 | @param polys the input for choosing split candidates
|
---|
770 | @param axis the current split axis
|
---|
771 | @param splitCandidates returns sorted list of split candidates
|
---|
772 | */
|
---|
773 | void SortSubdivisionCandidates(const RayInfoContainer &rays,
|
---|
774 | const int axis,
|
---|
775 | float minBand,
|
---|
776 | float maxBand);
|
---|
777 |
|
---|
778 | /** Evaluate pvs size as induced by the samples.
|
---|
779 | */
|
---|
780 | int EvalPvsSize(const RayInfoContainer &rays) const;
|
---|
781 |
|
---|
782 | /** Computes best cost for axis aligned planes.
|
---|
783 | */
|
---|
784 | float EvalLocalCostHeuristics(const VspTraversalData &tData,
|
---|
785 | const AxisAlignedBox3 &box,
|
---|
786 | const int axis,
|
---|
787 | float &position);
|
---|
788 |
|
---|
789 |
|
---|
790 | //////////////////////////////////////////
|
---|
791 | // Helper function for computing heuristics
|
---|
792 |
|
---|
793 | /** Evaluates the contribution to left and right pvs at a visibility event ve.
|
---|
794 | @param ve the visibility event
|
---|
795 | @param pvsLeft updates the left pvs
|
---|
796 | @param rightPvs updates the right pvs
|
---|
797 | */
|
---|
798 | void EvalHeuristics(const SortableEntry &ve, int &pvsLeft, int &pvsRight) const;
|
---|
799 |
|
---|
800 | /** Evaluates contribution of min event to pvs
|
---|
801 | */
|
---|
802 | int EvalMinEventContribution(
|
---|
803 | const VssRay &ray, const bool isTermination) const;
|
---|
804 |
|
---|
805 | /** Evaluates contribution of max event to pvs
|
---|
806 | */
|
---|
807 | int EvalMaxEventContribution(
|
---|
808 | const VssRay &ray, const bool isTermination) const;
|
---|
809 |
|
---|
810 | /** Evaluates contribution of kd leaf when encountering a min event
|
---|
811 | */
|
---|
812 | int EvalMinEventContribution(KdLeaf *leaf) const;
|
---|
813 | /** Evaluates contribution of kd leaf when encountering a max event
|
---|
814 | */
|
---|
815 | int EvalMaxEventContribution(KdLeaf *leaf) const;
|
---|
816 |
|
---|
817 | /** Prepares objects for the heuristics.
|
---|
818 | @returns pvs size as seen by the rays.
|
---|
819 | */
|
---|
820 | int PrepareHeuristics(const RayInfoContainer &rays);
|
---|
821 |
|
---|
822 | /** Prepare a single ray for heuristics.
|
---|
823 | */
|
---|
824 | int PrepareHeuristics(const VssRay &ray, const bool isTermination);
|
---|
825 | /** Prepare a single kd leaf for heuristics.
|
---|
826 | */
|
---|
827 | int PrepareHeuristics(KdLeaf *leaf);
|
---|
828 |
|
---|
829 | /////////////////////////////////////////////////////////////////////////////
|
---|
830 |
|
---|
831 |
|
---|
832 | /** Subdivides the rays into front and back rays according to the split plane.
|
---|
833 |
|
---|
834 | @param plane the split plane
|
---|
835 | @param rays contains the rays to be split. The rays are
|
---|
836 | distributed into front and back rays.
|
---|
837 | @param frontRays returns rays on the front side of the plane
|
---|
838 | @param backRays returns rays on the back side of the plane
|
---|
839 |
|
---|
840 | @returns the number of splits
|
---|
841 | */
|
---|
842 | int SplitRays(const AxisAlignedPlane &plane,
|
---|
843 | RayInfoContainer &rays,
|
---|
844 | RayInfoContainer &frontRays,
|
---|
845 | RayInfoContainer &backRays) const;
|
---|
846 |
|
---|
847 | /** Classfifies the object with respect to the
|
---|
848 | pvs of the front and back leaf and updates pvs size
|
---|
849 | accordingly.
|
---|
850 |
|
---|
851 | @param obj the object to be added
|
---|
852 | @param cf the ray classification regarding the split plane
|
---|
853 | @param frontPvs returns the PVS of the front partition
|
---|
854 | @param backPvs returns the PVS of the back partition
|
---|
855 |
|
---|
856 | */
|
---|
857 | void UpdateContributionsToPvs(
|
---|
858 | const VssRay &ray,
|
---|
859 | const bool isTermination,
|
---|
860 | const int cf,
|
---|
861 | float &frontPvs,
|
---|
862 | float &backPvs,
|
---|
863 | float &totalPvs) const;
|
---|
864 |
|
---|
865 | /** Evaluates the contribution for objects.
|
---|
866 | */
|
---|
867 | void UpdateContributionsToPvs(
|
---|
868 | Intersectable *obj,
|
---|
869 | const int cf,
|
---|
870 | float &frontPvs,
|
---|
871 | float &backPvs,
|
---|
872 | float &totalPvs) const;
|
---|
873 |
|
---|
874 | /** Evaluates the contribution for bounding volume leaves.
|
---|
875 | */
|
---|
876 | void UpdateContributionsToPvs(
|
---|
877 | BvhLeaf *leaf,
|
---|
878 | const int cf,
|
---|
879 | float &frontPvs,
|
---|
880 | float &backPvs,
|
---|
881 | float &totalPvs) const;
|
---|
882 |
|
---|
883 | /** Evaluates the contribution for kd leaves.
|
---|
884 | */
|
---|
885 | void UpdateContributionsToPvs(
|
---|
886 | KdLeaf *leaf,
|
---|
887 | const int cf,
|
---|
888 | float &frontPvs,
|
---|
889 | float &backPvs,
|
---|
890 | float &totalPvs) const;
|
---|
891 |
|
---|
892 | /** Returns true if tree can be terminated.
|
---|
893 | */
|
---|
894 | bool LocalTerminationCriteriaMet(const VspTraversalData &data) const;
|
---|
895 |
|
---|
896 | /** Returns true if global tree can be terminated.
|
---|
897 | */
|
---|
898 | bool GlobalTerminationCriteriaMet(const VspTraversalData &data) const;
|
---|
899 |
|
---|
900 | /** Adds ray sample contributions to the PVS.
|
---|
901 | @param sampleContributions the number contributions of the samples
|
---|
902 | @param contributingSampels the number of contributing rays
|
---|
903 |
|
---|
904 | */
|
---|
905 | void AddSamplesToPvs(VspLeaf *leaf,
|
---|
906 | const RayInfoContainer &rays,
|
---|
907 | float &sampleContributions,
|
---|
908 | int &contributingSamples);
|
---|
909 |
|
---|
910 | /** Propagates valid flag up the tree.
|
---|
911 | */
|
---|
912 | void PropagateUpValidity(VspNode *node);
|
---|
913 |
|
---|
914 | /** Writes the node to disk
|
---|
915 | @note: should be implemented as visitor.
|
---|
916 | */
|
---|
917 | void ExportNode(VspNode *node, OUT_STREAM &stream);
|
---|
918 |
|
---|
919 | /** Returns estimated memory usage of tree.
|
---|
920 | */
|
---|
921 | float GetMemUsage() const;
|
---|
922 |
|
---|
923 | /** Updates view cell pvs of objects.
|
---|
924 | */
|
---|
925 | void ProcessViewCellObjects(ViewCell *parent,
|
---|
926 | ViewCell *front,
|
---|
927 | ViewCell *back) const;
|
---|
928 |
|
---|
929 | void CreateViewCell(VspTraversalData &tData, const bool updatePvs);
|
---|
930 |
|
---|
931 | /** Collect split candidates which are affected by the last split
|
---|
932 | and must be reevaluated.
|
---|
933 | */
|
---|
934 | void CollectDirtyCandidates(VspSubdivisionCandidate *sc, vector<SubdivisionCandidate *> &dirtyList);
|
---|
935 |
|
---|
936 | void CollectDirtyCandidate(
|
---|
937 | const VssRay &ray,
|
---|
938 | const bool isTermination,
|
---|
939 | vector<SubdivisionCandidate *> &dirtyList) const;
|
---|
940 |
|
---|
941 | /** Rays will be clipped to the bounding box.
|
---|
942 | */
|
---|
943 | void PreprocessRays(const VssRayContainer &sampleRays, RayInfoContainer &rays);
|
---|
944 |
|
---|
945 | /** Evaluate subdivision statistics.
|
---|
946 | */
|
---|
947 | void EvalSubdivisionStats(const SubdivisionCandidate &tData);
|
---|
948 |
|
---|
949 | SubdivisionCandidate *PrepareConstruction(
|
---|
950 | const VssRayContainer &sampleRays,
|
---|
951 | RayInfoContainer &rays);
|
---|
952 |
|
---|
953 | /** Evaluates pvs contribution of this ray.
|
---|
954 | */
|
---|
955 | int EvalContributionToPvs(const VssRay &ray, const bool isTermination) const;
|
---|
956 |
|
---|
957 | /** Evaluates pvs contribution of a kd node.
|
---|
958 | */
|
---|
959 | int EvalContributionToPvs(KdLeaf *leaf) const;
|
---|
960 |
|
---|
961 |
|
---|
962 | protected:
|
---|
963 |
|
---|
964 | /// pointer to the hierarchy of view cells
|
---|
965 | ViewCellsTree *mViewCellsTree;
|
---|
966 |
|
---|
967 | HierarchyManager *mHierarchyManager;
|
---|
968 | //OspTree *mOspTree;
|
---|
969 | //bool mUseKdPvsForHeuristics;
|
---|
970 |
|
---|
971 | ViewCellsManager *mViewCellsManager;
|
---|
972 |
|
---|
973 | vector<SortableEntry> *mLocalSubdivisionCandidates;
|
---|
974 |
|
---|
975 | /// Pointer to the root of the tree
|
---|
976 | VspNode *mRoot;
|
---|
977 |
|
---|
978 | VspTreeStatistics mVspStats;
|
---|
979 |
|
---|
980 | /// View cell corresponding to the space outside the valid view space
|
---|
981 | VspViewCell *mOutOfBoundsCell;
|
---|
982 |
|
---|
983 | /// box around the whole view domain
|
---|
984 | AxisAlignedBox3 mBoundingBox;
|
---|
985 |
|
---|
986 |
|
---|
987 | //-- local termination
|
---|
988 |
|
---|
989 | /// minimal number of rays before subdivision termination
|
---|
990 | int mTermMinRays;
|
---|
991 | /// maximal possible depth
|
---|
992 | int mTermMaxDepth;
|
---|
993 | /// mininum probability
|
---|
994 | float mTermMinProbability;
|
---|
995 | /// mininum PVS
|
---|
996 | int mTermMinPvs;
|
---|
997 | /// maximal contribution per ray
|
---|
998 | float mTermMaxRayContribution;
|
---|
999 | /// maximal acceptable cost ratio
|
---|
1000 | float mTermMaxCostRatio;
|
---|
1001 | /// tolerance value indicating how often the max cost ratio can be failed
|
---|
1002 | int mTermMissTolerance;
|
---|
1003 |
|
---|
1004 |
|
---|
1005 | //-- global criteria
|
---|
1006 | float mTermMinGlobalCostRatio;
|
---|
1007 | int mTermGlobalCostMissTolerance;
|
---|
1008 |
|
---|
1009 |
|
---|
1010 | /// maximal number of view cells
|
---|
1011 | int mMaxViewCells;
|
---|
1012 | /// maximal tree memory
|
---|
1013 | float mMaxMemory;
|
---|
1014 | /// the tree is out of memory
|
---|
1015 | bool mOutOfMemory;
|
---|
1016 |
|
---|
1017 |
|
---|
1018 | //-- split heuristics based parameters
|
---|
1019 |
|
---|
1020 | bool mUseCostHeuristics;
|
---|
1021 | /// balancing factor for PVS criterium
|
---|
1022 | float mCtDivCi;
|
---|
1023 | /// if only driving axis should be used for split
|
---|
1024 | bool mOnlyDrivingAxis;
|
---|
1025 | /// if random split axis should be used
|
---|
1026 | bool mUseRandomAxis;
|
---|
1027 | /// if vsp bsp tree should simulate octree
|
---|
1028 | bool mCirculatingAxis;
|
---|
1029 | /// minimal relative position where the split axis can be placed
|
---|
1030 | float mMinBand;
|
---|
1031 | /// maximal relative position where the split axis can be placed
|
---|
1032 | float mMaxBand;
|
---|
1033 |
|
---|
1034 |
|
---|
1035 | /// current time stamp (used for keeping split history)
|
---|
1036 | int mTimeStamp;
|
---|
1037 | // if rays should be stored in leaves
|
---|
1038 | bool mStoreRays;
|
---|
1039 |
|
---|
1040 | /// epsilon for geometric comparisons
|
---|
1041 | float mEpsilon;
|
---|
1042 |
|
---|
1043 |
|
---|
1044 | /// subdivision stats output file
|
---|
1045 | ofstream mSubdivisionStats;
|
---|
1046 | /// keeps track of cost during subdivision
|
---|
1047 | float mTotalCost;
|
---|
1048 | /// keeps track of overall pvs size during subdivision
|
---|
1049 | int mTotalPvsSize;
|
---|
1050 | /// number of currenly generated view cells
|
---|
1051 | int mCreatedViewCells;
|
---|
1052 |
|
---|
1053 | /// weight between render cost decrease and node render cost
|
---|
1054 | float mRenderCostDecreaseWeight;
|
---|
1055 |
|
---|
1056 | int mMaxTests;
|
---|
1057 | };
|
---|
1058 |
|
---|
1059 |
|
---|
1060 | }
|
---|
1061 |
|
---|
1062 | #endif
|
---|