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 OspTree;
|
---|
34 | class KdIntersectable;
|
---|
35 | class KdTree;
|
---|
36 | class VspTree;
|
---|
37 | class KdTreeStatistics;
|
---|
38 | class SubdivisionCandidate;
|
---|
39 |
|
---|
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 |
|
---|
164 | /** Returns parent node.
|
---|
165 | */
|
---|
166 | VspInterior *GetParent();
|
---|
167 |
|
---|
168 | /** Sets parent node.
|
---|
169 | */
|
---|
170 | void SetParent(VspInterior *parent);
|
---|
171 |
|
---|
172 | /** Returns true if this node is a sibling of node n.
|
---|
173 | */
|
---|
174 | bool IsSibling(VspNode *n) const;
|
---|
175 |
|
---|
176 | /** returns depth of the node.
|
---|
177 | */
|
---|
178 | int GetDepth() const;
|
---|
179 |
|
---|
180 | /** returns true if the whole subtree is valid
|
---|
181 | */
|
---|
182 | bool TreeValid() const;
|
---|
183 |
|
---|
184 | void SetTreeValid(const bool v);
|
---|
185 |
|
---|
186 | //-- mailing options
|
---|
187 |
|
---|
188 | void Mail() { mMailbox = sMailId; }
|
---|
189 | static void NewMail() { ++ sMailId; }
|
---|
190 | bool Mailed() const { return mMailbox == sMailId; }
|
---|
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 |
|
---|
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 |
|
---|
299 | /** Sets pointer to view cell.
|
---|
300 | */
|
---|
301 | void SetViewCell(ViewCellLeaf *viewCell);
|
---|
302 |
|
---|
303 | SubdivisionCandidate *GetSubdivisionCandidate() const
|
---|
304 | {
|
---|
305 | return mSubdivisionCandidate;
|
---|
306 | }
|
---|
307 |
|
---|
308 | public:
|
---|
309 |
|
---|
310 | /// Rays piercing this leaf.
|
---|
311 | VssRayContainer mVssRays;
|
---|
312 |
|
---|
313 | /// leaf pvs
|
---|
314 | ObjectPvs *mPvs;
|
---|
315 |
|
---|
316 | /// Probability that the view point lies in this leaf
|
---|
317 | float mProbability;
|
---|
318 |
|
---|
319 | protected:
|
---|
320 |
|
---|
321 | /// pointer to a split plane candidate splitting this leaf
|
---|
322 | SubdivisionCandidate *mSubdivisionCandidate;
|
---|
323 |
|
---|
324 | /// if NULL this does not correspond to feasible viewcell
|
---|
325 | ViewCellLeaf *mViewCell;
|
---|
326 | };
|
---|
327 |
|
---|
328 |
|
---|
329 | /** View Space Partitioning tree.
|
---|
330 | */
|
---|
331 | class VspTree
|
---|
332 | {
|
---|
333 | friend class ViewCellsParseHandlers;
|
---|
334 | friend class HierarchyManager;
|
---|
335 |
|
---|
336 | public:
|
---|
337 |
|
---|
338 | /** Additional data which is passed down the BSP tree during traversal.
|
---|
339 | */
|
---|
340 | class VspTraversalData
|
---|
341 | {
|
---|
342 | public:
|
---|
343 | /// the current node
|
---|
344 | VspLeaf *mNode;
|
---|
345 | /// current depth
|
---|
346 | int mDepth;
|
---|
347 | /// rays piercing this node
|
---|
348 | RayInfoContainer *mRays;
|
---|
349 | /// the probability that this node contains view point
|
---|
350 | float mProbability;
|
---|
351 | /// the bounding box of the node
|
---|
352 | AxisAlignedBox3 mBoundingBox;
|
---|
353 | /// pvs size
|
---|
354 | int mPvs;
|
---|
355 | /// how often this branch has missed the max-cost ratio
|
---|
356 | int mMaxCostMisses;
|
---|
357 | // current priority
|
---|
358 | float mPriority;
|
---|
359 |
|
---|
360 |
|
---|
361 | /** Returns average ray contribution.
|
---|
362 | */
|
---|
363 | float GetAvgRayContribution() const
|
---|
364 | {
|
---|
365 | return (float)mPvs / ((float)mRays->size() + Limits::Small);
|
---|
366 | }
|
---|
367 |
|
---|
368 |
|
---|
369 | VspTraversalData():
|
---|
370 | mNode(NULL),
|
---|
371 | mDepth(0),
|
---|
372 | mRays(NULL),
|
---|
373 | mPvs(0),
|
---|
374 | mProbability(0.0),
|
---|
375 | mMaxCostMisses(0),
|
---|
376 | mPriority(0)
|
---|
377 | {}
|
---|
378 |
|
---|
379 | VspTraversalData(VspLeaf *node,
|
---|
380 | const int depth,
|
---|
381 | RayInfoContainer *rays,
|
---|
382 | const int pvs,
|
---|
383 | const float p,
|
---|
384 | const AxisAlignedBox3 &box):
|
---|
385 | mNode(node),
|
---|
386 | mDepth(depth),
|
---|
387 | mRays(rays),
|
---|
388 | mPvs(pvs),
|
---|
389 | mProbability(p),
|
---|
390 | mBoundingBox(box),
|
---|
391 | mMaxCostMisses(0),
|
---|
392 | mPriority(0)
|
---|
393 | {}
|
---|
394 |
|
---|
395 | VspTraversalData(const int depth,
|
---|
396 | RayInfoContainer *rays,
|
---|
397 | const AxisAlignedBox3 &box):
|
---|
398 | mNode(NULL),
|
---|
399 | mDepth(depth),
|
---|
400 | mRays(rays),
|
---|
401 | mPvs(0),
|
---|
402 | mProbability(0),
|
---|
403 | mMaxCostMisses(0),
|
---|
404 | mBoundingBox(box)
|
---|
405 | {}
|
---|
406 |
|
---|
407 | /** Returns cost of the traversal data.
|
---|
408 | */
|
---|
409 | float GetCost() const
|
---|
410 | {
|
---|
411 | //cout << mPriority << endl;
|
---|
412 | return mPriority;
|
---|
413 | }
|
---|
414 |
|
---|
415 | /// deletes contents and sets them to NULL
|
---|
416 | void Clear()
|
---|
417 | {
|
---|
418 | DEL_PTR(mRays);
|
---|
419 | }
|
---|
420 |
|
---|
421 | friend bool operator<(const VspTraversalData &a, const VspTraversalData &b)
|
---|
422 | {
|
---|
423 | return a.GetCost() < b.GetCost();
|
---|
424 | }
|
---|
425 | };
|
---|
426 |
|
---|
427 | /** Candidate for a view space split.
|
---|
428 | */
|
---|
429 | class VspSubdivisionCandidate: public SubdivisionCandidate
|
---|
430 | {
|
---|
431 | public:
|
---|
432 |
|
---|
433 | static VspTree* sVspTree;
|
---|
434 |
|
---|
435 | /// the current split plane
|
---|
436 | AxisAlignedPlane mSplitPlane;
|
---|
437 | /// parent node traversal data
|
---|
438 | VspTraversalData mParentData;
|
---|
439 |
|
---|
440 | VspSubdivisionCandidate(const VspTraversalData &tData): mParentData(tData)
|
---|
441 | {};
|
---|
442 |
|
---|
443 | int Type() const { return VIEW_SPACE; }
|
---|
444 |
|
---|
445 | void EvalPriority()
|
---|
446 | {
|
---|
447 | sVspTree->EvalSubdivisionCandidate(*this);
|
---|
448 | }
|
---|
449 |
|
---|
450 | bool GlobalTerminationCriteriaMet() const
|
---|
451 | {
|
---|
452 | return sVspTree->GlobalTerminationCriteriaMet(mParentData);
|
---|
453 | }
|
---|
454 |
|
---|
455 | VspSubdivisionCandidate(
|
---|
456 | const AxisAlignedPlane &plane,
|
---|
457 | const VspTraversalData &tData):
|
---|
458 | mSplitPlane(plane), mParentData(tData)
|
---|
459 | {}
|
---|
460 | };
|
---|
461 |
|
---|
462 | /** Struct for traversing line segment.
|
---|
463 | */
|
---|
464 | struct LineTraversalData
|
---|
465 | {
|
---|
466 | VspNode *mNode;
|
---|
467 | Vector3 mExitPoint;
|
---|
468 |
|
---|
469 | float mMaxT;
|
---|
470 |
|
---|
471 | LineTraversalData () {}
|
---|
472 | LineTraversalData (VspNode *n, const Vector3 &p, const float maxt):
|
---|
473 | mNode(n), mExitPoint(p), mMaxT(maxt) {}
|
---|
474 | };
|
---|
475 |
|
---|
476 | //typedef std::priority_queue<VspTraversalData> VspOspTraversalQueue;
|
---|
477 |
|
---|
478 | /** Default constructor creating an empty tree.
|
---|
479 | */
|
---|
480 | VspTree();
|
---|
481 |
|
---|
482 | /** Default destructor.
|
---|
483 | */
|
---|
484 | ~VspTree();
|
---|
485 |
|
---|
486 | /** Returns BSP Tree statistics.
|
---|
487 | */
|
---|
488 | const VspTreeStatistics &GetStatistics() const;
|
---|
489 |
|
---|
490 | /** Returns bounding box of the specified node.
|
---|
491 | */
|
---|
492 | AxisAlignedBox3 GetBoundingBox(VspNode *node) const;
|
---|
493 |
|
---|
494 | /** Returns list of BSP leaves with pvs smaller than
|
---|
495 | a certain threshold.
|
---|
496 | @param onlyUnmailed if only the unmailed leaves should be considered
|
---|
497 | @param maxPvs the maximal pvs of a leaf to be added (-1 means unlimited)
|
---|
498 | */
|
---|
499 | void CollectLeaves(vector<VspLeaf *> &leaves,
|
---|
500 | const bool onlyUnmailed = false,
|
---|
501 | const int maxPvs = -1) const;
|
---|
502 |
|
---|
503 | /** Returns box which bounds the whole tree.
|
---|
504 | */
|
---|
505 | AxisAlignedBox3 GetBoundingBox() const;
|
---|
506 |
|
---|
507 | /** Returns root of the view space partitioning tree.
|
---|
508 | */
|
---|
509 | VspNode *GetRoot() const;
|
---|
510 |
|
---|
511 | /** Collects the leaf view cells of the tree
|
---|
512 | @param viewCells returns the view cells
|
---|
513 | */
|
---|
514 | void CollectViewCells(ViewCellContainer &viewCells, bool onlyValid) const;
|
---|
515 |
|
---|
516 | /** A ray is cast possible intersecting the tree.
|
---|
517 | @param the ray that is cast.
|
---|
518 | @returns the number of intersections with objects stored in the tree.
|
---|
519 | */
|
---|
520 | int CastRay(Ray &ray);
|
---|
521 |
|
---|
522 |
|
---|
523 | /** finds neighbouring leaves of this tree node.
|
---|
524 | */
|
---|
525 | int FindNeighbors(VspLeaf *n,
|
---|
526 | vector<VspLeaf *> &neighbors,
|
---|
527 | const bool onlyUnmailed) const;
|
---|
528 |
|
---|
529 | /** Returns random leaf of BSP tree.
|
---|
530 | @param halfspace defines the halfspace from which the leaf is taken.
|
---|
531 | */
|
---|
532 | VspLeaf *GetRandomLeaf(const Plane3 &halfspace);
|
---|
533 |
|
---|
534 | /** Returns random leaf of BSP tree.
|
---|
535 | @param onlyUnmailed if only unmailed leaves should be returned.
|
---|
536 | */
|
---|
537 | VspLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
|
---|
538 |
|
---|
539 | /** Returns epsilon of this tree.
|
---|
540 | */
|
---|
541 | float GetEpsilon() const;
|
---|
542 |
|
---|
543 | /** Casts line segment into the tree.
|
---|
544 | @param origin the origin of the line segment
|
---|
545 | @param termination the end point of the line segment
|
---|
546 | @returns view cells intersecting the line segment.
|
---|
547 | */
|
---|
548 | int CastLineSegment(const Vector3 &origin,
|
---|
549 | const Vector3 &termination,
|
---|
550 | ViewCellContainer &viewcells);
|
---|
551 |
|
---|
552 |
|
---|
553 | /** Sets pointer to view cells manager.
|
---|
554 | */
|
---|
555 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
556 |
|
---|
557 | /** Returns view cell the current point is located in.
|
---|
558 | @param point the current view point
|
---|
559 | @param active if currently active view cells should be returned or
|
---|
560 | elementary view cell
|
---|
561 | */
|
---|
562 | ViewCell *GetViewCell(const Vector3 &point, const bool active = false);
|
---|
563 |
|
---|
564 |
|
---|
565 | /** Returns true if this view point is in a valid view space,
|
---|
566 | false otherwise.
|
---|
567 | */
|
---|
568 | bool ViewPointValid(const Vector3 &viewPoint) const;
|
---|
569 |
|
---|
570 | /** Returns view cell corresponding to
|
---|
571 | the invalid view space.
|
---|
572 | */
|
---|
573 | VspViewCell *GetOutOfBoundsCell();
|
---|
574 |
|
---|
575 | /** Writes tree to output stream
|
---|
576 | */
|
---|
577 | bool Export(OUT_STREAM &stream);
|
---|
578 |
|
---|
579 | /** Casts beam, i.e. a 5D frustum of rays, into tree.
|
---|
580 | Tests conservative using the bounding box of the nodes.
|
---|
581 | @returns number of view cells it intersected
|
---|
582 | */
|
---|
583 | int CastBeam(Beam &beam);
|
---|
584 |
|
---|
585 |
|
---|
586 | /** Checks if tree validity-flags are right
|
---|
587 | with respect to view cell valitiy.
|
---|
588 | If not, marks subtree as invalid.
|
---|
589 | */
|
---|
590 | void ValidateTree();
|
---|
591 |
|
---|
592 | /** Invalid view cells are added to the unbounded space
|
---|
593 | */
|
---|
594 | void CollapseViewCells();
|
---|
595 |
|
---|
596 | /** Collects rays stored in the leaves.
|
---|
597 | */
|
---|
598 | void CollectRays(VssRayContainer &rays);
|
---|
599 |
|
---|
600 | /** Intersects box with the tree and returns the number of intersected boxes.
|
---|
601 | @returns number of view cells found
|
---|
602 | */
|
---|
603 | int ComputeBoxIntersections(const AxisAlignedBox3 &box,
|
---|
604 | ViewCellContainer &viewCells) const;
|
---|
605 |
|
---|
606 | /** Remove the references of the parent view cell from the kd nodes associated with
|
---|
607 | the objects.
|
---|
608 | */
|
---|
609 | void RemoveParentViewCellReferences(ViewCell *parent) const;
|
---|
610 |
|
---|
611 | /** Adds references to the view cell to the kd nodes associated with the objects.
|
---|
612 | */
|
---|
613 | void AddViewCellReferences(ViewCell *vc) const;
|
---|
614 |
|
---|
615 | /** Returns view cells of this ray, either taking precomputed cells
|
---|
616 | or by recomputation.
|
---|
617 | */
|
---|
618 | void GetViewCells(const VssRay &ray, ViewCellContainer &viewCells);
|
---|
619 |
|
---|
620 |
|
---|
621 | ViewCellsTree *GetViewCellsTree() const { return mViewCellsTree; }
|
---|
622 |
|
---|
623 | void SetViewCellsTree(ViewCellsTree *vt) { mViewCellsTree = vt; }
|
---|
624 |
|
---|
625 |
|
---|
626 | protected:
|
---|
627 |
|
---|
628 | // --------------------------------------------------------------
|
---|
629 | // For sorting objects
|
---|
630 | // --------------------------------------------------------------
|
---|
631 | struct SortableEntry
|
---|
632 | {
|
---|
633 | enum EType
|
---|
634 | {
|
---|
635 | ERayMin,
|
---|
636 | ERayMax
|
---|
637 | };
|
---|
638 |
|
---|
639 | int type;
|
---|
640 | float value;
|
---|
641 | VssRay *ray;
|
---|
642 |
|
---|
643 | SortableEntry() {}
|
---|
644 | SortableEntry(const int t, const float v, VssRay *r):
|
---|
645 | type(t), value(v), ray(r)
|
---|
646 | {
|
---|
647 | }
|
---|
648 |
|
---|
649 | friend bool operator<(const SortableEntry &a, const SortableEntry &b)
|
---|
650 | {
|
---|
651 | return a.value < b.value;
|
---|
652 | }
|
---|
653 | };
|
---|
654 |
|
---|
655 | /** faster evaluation of split plane cost for kd axis aligned cells.
|
---|
656 | */
|
---|
657 | float EvalLocalSplitCost(const VspTraversalData &data,
|
---|
658 | const AxisAlignedBox3 &box,
|
---|
659 | const int axis,
|
---|
660 | const float &position,
|
---|
661 | float &pFront,
|
---|
662 | float &pBack) const;
|
---|
663 |
|
---|
664 | void ComputeBoundingBox(const VssRayContainer &rays,
|
---|
665 | AxisAlignedBox3 *forcedBoundingBox);
|
---|
666 |
|
---|
667 | /** Evaluates candidate for splitting.
|
---|
668 | */
|
---|
669 | void EvalSubdivisionCandidate(VspSubdivisionCandidate &splitData);
|
---|
670 |
|
---|
671 | /** Evaluates render cost decrease of next split.
|
---|
672 | */
|
---|
673 | float EvalRenderCostDecrease(const AxisAlignedPlane &candidatePlane,
|
---|
674 | const VspTraversalData &data,
|
---|
675 | float &normalizedOldRenderCost) const;
|
---|
676 |
|
---|
677 | /** Collects view cells in the subtree under root.
|
---|
678 | */
|
---|
679 | void CollectViewCells(VspNode *root,
|
---|
680 | bool onlyValid,
|
---|
681 | ViewCellContainer &viewCells,
|
---|
682 | bool onlyUnmailed = false) const;
|
---|
683 |
|
---|
684 | /** Returns view cell corresponding to
|
---|
685 | the invalid view space. If it does not exist, it is created.
|
---|
686 | */
|
---|
687 | VspViewCell *GetOrCreateOutOfBoundsCell();
|
---|
688 |
|
---|
689 | /** Collapses the tree with respect to the view cell partition,
|
---|
690 | i.e. leaves having the same view cell are collapsed.
|
---|
691 | @param node the root of the subtree to be collapsed
|
---|
692 | @param collapsed returns the number of collapsed nodes
|
---|
693 | @returns node of type leaf if the node could be collapsed,
|
---|
694 | this node otherwise
|
---|
695 | */
|
---|
696 | VspNode *CollapseTree(VspNode *node, int &collapsed);
|
---|
697 |
|
---|
698 | /** Helper function revalidating the view cell leaf list after merge.
|
---|
699 | */
|
---|
700 | void RepairViewCellsLeafLists();
|
---|
701 |
|
---|
702 | /** Evaluates tree stats in the BSP tree leafs.
|
---|
703 | */
|
---|
704 | void EvaluateLeafStats(const VspTraversalData &data);
|
---|
705 |
|
---|
706 | /** Subdivides node using a best split priority queue.
|
---|
707 | @param tQueue the best split priority queue
|
---|
708 | @param splitCandidate the candidate for the next split
|
---|
709 | @param globalCriteriaMet if the global termination criteria were already met
|
---|
710 | @returns new root of the subtree
|
---|
711 | */
|
---|
712 | VspNode *Subdivide(SplitQueue &tQueue,
|
---|
713 | SubdivisionCandidate *splitCandidate,
|
---|
714 | const bool globalCriteriaMet);
|
---|
715 |
|
---|
716 | /** Adds stats to subdivision log file.
|
---|
717 | */
|
---|
718 | void AddSubdivisionStats(const int viewCells,
|
---|
719 | const float renderCostDecr,
|
---|
720 | const float totalRenderCost,
|
---|
721 | const float avgRenderCost);
|
---|
722 |
|
---|
723 | /** Subdivides leaf.
|
---|
724 |
|
---|
725 | @param tData data object holding, e.g., a pointer to the leaf
|
---|
726 | @param frontData returns the data (e.g., pointer to the leaf) in front of the split plane
|
---|
727 | @param backData returns the data (e.g., pointer to the leaf) in the back of the split plane
|
---|
728 |
|
---|
729 | @param rays the polygons to be filtered
|
---|
730 | @param frontRays returns the polygons in front of the split plane
|
---|
731 |
|
---|
732 | @returns the root of the subdivision
|
---|
733 | */
|
---|
734 | VspInterior *SubdivideNode(const AxisAlignedPlane &splitPlane,
|
---|
735 | VspTraversalData &tData,
|
---|
736 | VspTraversalData &frontData,
|
---|
737 | VspTraversalData &backData);
|
---|
738 |
|
---|
739 | /** Selects an axis aligned for the next split.
|
---|
740 | @returns cost for this split
|
---|
741 | */
|
---|
742 | float SelectSplitPlane(const VspTraversalData &tData,
|
---|
743 | AxisAlignedPlane &plane,
|
---|
744 | float &pFront,
|
---|
745 | float &pBack);
|
---|
746 |
|
---|
747 | /** Sorts split candidates along the specified axis.
|
---|
748 | The split candidates are generated on possible visibility
|
---|
749 | events (i.e., where ray segments intersect the ray boundaries).
|
---|
750 | The sorted candidates are needed to compute the heuristics.
|
---|
751 |
|
---|
752 | @param polys the input for choosing split candidates
|
---|
753 | @param axis the current split axis
|
---|
754 | @param splitCandidates returns sorted list of split candidates
|
---|
755 | */
|
---|
756 | void SortSubdivisionCandidates(const RayInfoContainer &rays,
|
---|
757 | const int axis,
|
---|
758 | float minBand,
|
---|
759 | float maxBand);
|
---|
760 |
|
---|
761 | /** Evaluate pvs size associated with the rays.
|
---|
762 | */
|
---|
763 | int EvalPvsSize(const RayInfoContainer &rays) const;
|
---|
764 |
|
---|
765 | /** Computes pvs increase with respect to the previous pvs for heuristics.
|
---|
766 | */
|
---|
767 | int GetPvsIncr(Intersectable *object, const KdPvsMap &activeNodes);
|
---|
768 |
|
---|
769 | /** Returns absolute pvs contribution of this object.
|
---|
770 | */
|
---|
771 | int GetPvsContribution(Intersectable *object) const;
|
---|
772 |
|
---|
773 | /** Computes best cost for axis aligned planes.
|
---|
774 | */
|
---|
775 | float EvalLocalCostHeuristics(const VspTraversalData &tData,
|
---|
776 | const AxisAlignedBox3 &box,
|
---|
777 | const int axis,
|
---|
778 | float &position);
|
---|
779 |
|
---|
780 | /** Evaluates the influence on the pvs of the visibility event ve.
|
---|
781 | @param ve the visibility event
|
---|
782 | @param pvsLeft updates the left pvs
|
---|
783 | @param rightPvs updates the right pvs
|
---|
784 | */
|
---|
785 | void EvalPvsIncr(const SortableEntry &ve,
|
---|
786 | int &pvsLeft,
|
---|
787 | int &pvsRight) const;
|
---|
788 |
|
---|
789 | void RemoveContriFromPvs(KdLeaf *leaf, int &pvs) const;
|
---|
790 | void AddContriToPvs(KdLeaf *leaf, int &pvs) const;
|
---|
791 |
|
---|
792 | /** Prepares objects for the heuristics.
|
---|
793 | @returns pvs size of the ray container
|
---|
794 | */
|
---|
795 | int PrepareHeuristics(const RayInfoContainer &rays);
|
---|
796 |
|
---|
797 | int PrepareHeuristics(KdLeaf *leaf);
|
---|
798 |
|
---|
799 | /** Subdivides the rays into front and back rays according to the split plane.
|
---|
800 |
|
---|
801 | @param plane the split plane
|
---|
802 | @param rays contains the rays to be split. The rays are
|
---|
803 | distributed into front and back rays.
|
---|
804 | @param frontRays returns rays on the front side of the plane
|
---|
805 | @param backRays returns rays on the back side of the plane
|
---|
806 |
|
---|
807 | @returns the number of splits
|
---|
808 | */
|
---|
809 | int SplitRays(const AxisAlignedPlane &plane,
|
---|
810 | RayInfoContainer &rays,
|
---|
811 | RayInfoContainer &frontRays,
|
---|
812 | RayInfoContainer &backRays) const;
|
---|
813 |
|
---|
814 | /** Classfifies the object with respect to the
|
---|
815 | pvs of the front and back leaf and updates pvs size
|
---|
816 | accordingly.
|
---|
817 |
|
---|
818 | @param obj the object to be added
|
---|
819 | @param cf the ray classification regarding the split plane
|
---|
820 | @param frontPvs returns the PVS of the front partition
|
---|
821 | @param backPvs returns the PVS of the back partition
|
---|
822 |
|
---|
823 | */
|
---|
824 | void UpdateObjPvsContri(Intersectable *obj,
|
---|
825 | const int cf,
|
---|
826 | float &frontPvs,
|
---|
827 | float &backPvs,
|
---|
828 | float &totalPvs) const;
|
---|
829 |
|
---|
830 | /** See UpdateObjPvsContri.
|
---|
831 | */
|
---|
832 | void UpdateKdLeafPvsContri(KdLeaf *leaf,
|
---|
833 | const int cf,
|
---|
834 | float &frontPvs,
|
---|
835 | float &backPvs,
|
---|
836 | float &totalPvs) const;
|
---|
837 |
|
---|
838 | /** Collects pvs from rays.
|
---|
839 | */
|
---|
840 | void CollectPvs(const RayInfoContainer &rays,
|
---|
841 | ObjectContainer &objects) const;
|
---|
842 |
|
---|
843 | /** Returns true if tree can be terminated.
|
---|
844 | */
|
---|
845 | bool LocalTerminationCriteriaMet(const VspTraversalData &data) const;
|
---|
846 |
|
---|
847 | /** Returns true if global tree can be terminated.
|
---|
848 | */
|
---|
849 | bool GlobalTerminationCriteriaMet(const VspTraversalData &data) const;
|
---|
850 |
|
---|
851 | /** Adds ray sample contributions to the PVS.
|
---|
852 | @param sampleContributions the number contributions of the samples
|
---|
853 | @param contributingSampels the number of contributing rays
|
---|
854 |
|
---|
855 | */
|
---|
856 | void AddSamplesToPvs(VspLeaf *leaf,
|
---|
857 | const RayInfoContainer &rays,
|
---|
858 | float &sampleContributions,
|
---|
859 | int &contributingSamples);
|
---|
860 |
|
---|
861 | bool AddKdLeafToPvs(KdLeaf *leaf,
|
---|
862 | ViewCell *vc,
|
---|
863 | const float pvs,
|
---|
864 | float &contribution);
|
---|
865 |
|
---|
866 | /** Propagates valid flag up the tree.
|
---|
867 | */
|
---|
868 | void PropagateUpValidity(VspNode *node);
|
---|
869 |
|
---|
870 | /** Writes the node to disk
|
---|
871 | @note: should be implemented as visitor.
|
---|
872 | */
|
---|
873 | void ExportNode(VspNode *node, OUT_STREAM &stream);
|
---|
874 |
|
---|
875 | /** Returns estimated memory usage of tree.
|
---|
876 | */
|
---|
877 | float GetMemUsage() const;
|
---|
878 |
|
---|
879 | /** Updates view cell pvs of objects.
|
---|
880 | */
|
---|
881 | void ProcessViewCellObjects(ViewCell *parent,
|
---|
882 | ViewCell *front,
|
---|
883 | ViewCell *back) const;
|
---|
884 |
|
---|
885 | void CreateViewCell(VspTraversalData &tData, const bool updatePvs);
|
---|
886 |
|
---|
887 | /** Collect split candidates which are affected by the last split
|
---|
888 | and must be reevaluated.
|
---|
889 | */
|
---|
890 | void CollectDirtyCandidates(VspSubdivisionCandidate *sc, vector<SubdivisionCandidate *> &dirtyList);
|
---|
891 |
|
---|
892 | /** Rays will be clipped to the bounding box.
|
---|
893 | */
|
---|
894 | void PreprocessRays(const VssRayContainer &sampleRays, RayInfoContainer &rays);
|
---|
895 |
|
---|
896 | /** Evaluate subdivision statistics.
|
---|
897 | */
|
---|
898 | void EvalSubdivisionStats(const SubdivisionCandidate &tData);
|
---|
899 |
|
---|
900 | SubdivisionCandidate *PrepareConstruction(const VssRayContainer &sampleRays,
|
---|
901 | AxisAlignedBox3 *forcedViewSpace,
|
---|
902 | RayInfoContainer &rays);
|
---|
903 |
|
---|
904 | protected:
|
---|
905 |
|
---|
906 |
|
---|
907 | /// pointer to the hierarchy of view cells
|
---|
908 | ViewCellsTree *mViewCellsTree;
|
---|
909 |
|
---|
910 | OspTree *mOspTree;
|
---|
911 |
|
---|
912 | bool mUseKdPvsForHeuristics;
|
---|
913 | bool mStoreKdPvs;
|
---|
914 |
|
---|
915 | ViewCellsManager *mViewCellsManager;
|
---|
916 |
|
---|
917 | vector<SortableEntry> *mLocalSubdivisionCandidates;
|
---|
918 |
|
---|
919 | /// Pointer to the root of the tree
|
---|
920 | VspNode *mRoot;
|
---|
921 |
|
---|
922 | VspTreeStatistics mVspStats;
|
---|
923 |
|
---|
924 | /// View cell corresponding to the space outside the valid view space
|
---|
925 | VspViewCell *mOutOfBoundsCell;
|
---|
926 |
|
---|
927 | /// box around the whole view domain
|
---|
928 | AxisAlignedBox3 mBoundingBox;
|
---|
929 |
|
---|
930 |
|
---|
931 | //-- local termination
|
---|
932 |
|
---|
933 | /// minimal number of rays before subdivision termination
|
---|
934 | int mTermMinRays;
|
---|
935 | /// maximal possible depth
|
---|
936 | int mTermMaxDepth;
|
---|
937 | /// mininum probability
|
---|
938 | float mTermMinProbability;
|
---|
939 | /// mininum PVS
|
---|
940 | int mTermMinPvs;
|
---|
941 | /// maximal contribution per ray
|
---|
942 | float mTermMaxRayContribution;
|
---|
943 | /// maximal acceptable cost ratio
|
---|
944 | float mTermMaxCostRatio;
|
---|
945 | /// tolerance value indicating how often the max cost ratio can be failed
|
---|
946 | int mTermMissTolerance;
|
---|
947 |
|
---|
948 |
|
---|
949 | //-- global criteria
|
---|
950 | float mTermMinGlobalCostRatio;
|
---|
951 | int mTermGlobalCostMissTolerance;
|
---|
952 | int mGlobalCostMisses;
|
---|
953 |
|
---|
954 | /// maximal number of view cells
|
---|
955 | int mMaxViewCells;
|
---|
956 | /// maximal tree memory
|
---|
957 | float mMaxMemory;
|
---|
958 | /// the tree is out of memory
|
---|
959 | bool mOutOfMemory;
|
---|
960 |
|
---|
961 |
|
---|
962 | //-- split heuristics based parameters
|
---|
963 |
|
---|
964 | bool mUseCostHeuristics;
|
---|
965 | /// balancing factor for PVS criterium
|
---|
966 | float mCtDivCi;
|
---|
967 | /// if only driving axis should be used for split
|
---|
968 | bool mOnlyDrivingAxis;
|
---|
969 | /// if random split axis should be used
|
---|
970 | bool mUseRandomAxis;
|
---|
971 | /// if vsp bsp tree should simulate octree
|
---|
972 | bool mCirculatingAxis;
|
---|
973 | /// minimal relative position where the split axis can be placed
|
---|
974 | float mMinBand;
|
---|
975 | /// maximal relative position where the split axis can be placed
|
---|
976 | float mMaxBand;
|
---|
977 |
|
---|
978 |
|
---|
979 | /// current time stamp (used for keeping split history)
|
---|
980 | int mTimeStamp;
|
---|
981 | // if rays should be stored in leaves
|
---|
982 | bool mStoreRays;
|
---|
983 |
|
---|
984 | /// epsilon for geometric comparisons
|
---|
985 | float mEpsilon;
|
---|
986 |
|
---|
987 |
|
---|
988 | /// subdivision stats output file
|
---|
989 | ofstream mSubdivisionStats;
|
---|
990 | /// keeps track of cost during subdivision
|
---|
991 | float mTotalCost;
|
---|
992 | /// keeps track of overall pvs size during subdivision
|
---|
993 | int mTotalPvsSize;
|
---|
994 | /// number of currenly generated view cells
|
---|
995 | int mCreatedViewCells;
|
---|
996 |
|
---|
997 | /// weight between render cost decrease and node render cost
|
---|
998 | float mRenderCostDecreaseWeight;
|
---|
999 |
|
---|
1000 | int mMaxTests;
|
---|
1001 | };
|
---|
1002 |
|
---|
1003 |
|
---|
1004 | }
|
---|
1005 |
|
---|
1006 | #endif
|
---|