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