1 | #ifndef _VspOspTree_H__
|
---|
2 | #define _VspOspTree_H__
|
---|
3 |
|
---|
4 | #include "Mesh.h"
|
---|
5 | #include "Containers.h"
|
---|
6 | #include "Polygon3.h"
|
---|
7 | #include <stack>
|
---|
8 | #include "Statistics.h"
|
---|
9 | #include "VssRay.h"
|
---|
10 | #include "RayInfo.h"
|
---|
11 | #include "gzstream.h"
|
---|
12 |
|
---|
13 |
|
---|
14 | namespace GtpVisibilityPreprocessor {
|
---|
15 |
|
---|
16 | class ViewCellLeaf;
|
---|
17 | class VspViewCell;
|
---|
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 |
|
---|
31 |
|
---|
32 | /** A definition for an axis aligned plane.
|
---|
33 | */
|
---|
34 | struct AxisAlignedPlane
|
---|
35 | {
|
---|
36 | public:
|
---|
37 | int mAxis;
|
---|
38 | float mPosition;
|
---|
39 | };
|
---|
40 |
|
---|
41 |
|
---|
42 | class VspTreeStatistics: public StatisticsBase
|
---|
43 | {
|
---|
44 | public:
|
---|
45 | // total number of nodes
|
---|
46 | int nodes;
|
---|
47 | // number of splits
|
---|
48 | int splits[3];
|
---|
49 |
|
---|
50 | // totals number of rays
|
---|
51 | int rays;
|
---|
52 | // maximal reached depth
|
---|
53 | int maxDepth;
|
---|
54 | // minimal depth
|
---|
55 | int minDepth;
|
---|
56 |
|
---|
57 | // max depth nodes
|
---|
58 | int maxDepthNodes;
|
---|
59 | // minimum depth nodes
|
---|
60 | int minDepthNodes;
|
---|
61 | // max depth nodes
|
---|
62 | int minPvsNodes;
|
---|
63 | // nodes with minimum PVS
|
---|
64 | int minRaysNodes;
|
---|
65 | // max ray contribution nodes
|
---|
66 | int maxRayContribNodes;
|
---|
67 | // minimum area nodes
|
---|
68 | int minProbabilityNodes;
|
---|
69 | /// nodes termination because of max cost ratio;
|
---|
70 | int maxCostNodes;
|
---|
71 | // max number of rays per node
|
---|
72 | int maxObjectRefs;
|
---|
73 | /// samples contributing to pvs
|
---|
74 | int contributingSamples;
|
---|
75 | /// sample contributions to pvs
|
---|
76 | int sampleContributions;
|
---|
77 | /// largest pvs
|
---|
78 | int maxPvs;
|
---|
79 | /// number of invalid leaves
|
---|
80 | int invalidLeaves;
|
---|
81 | /// accumulated number of rays refs
|
---|
82 | int accumRays;
|
---|
83 | int pvs;
|
---|
84 | // accumulated depth (used to compute average)
|
---|
85 | int accumDepth;
|
---|
86 |
|
---|
87 | // Constructor
|
---|
88 | VspTreeStatistics()
|
---|
89 | {
|
---|
90 | Reset();
|
---|
91 | }
|
---|
92 |
|
---|
93 | int Nodes() const {return nodes;}
|
---|
94 | int Interior() const { return nodes / 2; }
|
---|
95 | int Leaves() const { return (nodes / 2) + 1; }
|
---|
96 |
|
---|
97 | // TODO: computation wrong
|
---|
98 | double AvgDepth() const { return accumDepth / (double)Leaves();};
|
---|
99 | double AvgRays() const { return accumRays / (double)Leaves();};
|
---|
100 |
|
---|
101 | void Reset()
|
---|
102 | {
|
---|
103 | nodes = 0;
|
---|
104 | for (int i = 0; i < 3; ++ i)
|
---|
105 | splits[i] = 0;
|
---|
106 |
|
---|
107 | maxDepth = 0;
|
---|
108 | minDepth = 99999;
|
---|
109 | accumDepth = 0;
|
---|
110 | pvs = 0;
|
---|
111 | maxDepthNodes = 0;
|
---|
112 | minPvsNodes = 0;
|
---|
113 | minRaysNodes = 0;
|
---|
114 | maxRayContribNodes = 0;
|
---|
115 | minProbabilityNodes = 0;
|
---|
116 | maxCostNodes = 0;
|
---|
117 |
|
---|
118 | contributingSamples = 0;
|
---|
119 | sampleContributions = 0;
|
---|
120 |
|
---|
121 | maxPvs = 0;
|
---|
122 | invalidLeaves = 0;
|
---|
123 | accumRays = 0;
|
---|
124 | }
|
---|
125 |
|
---|
126 | void Print(ostream &app) const;
|
---|
127 |
|
---|
128 | friend ostream &operator<<(ostream &s, const VspTreeStatistics &stat)
|
---|
129 | {
|
---|
130 | stat.Print(s);
|
---|
131 | return s;
|
---|
132 | }
|
---|
133 | };
|
---|
134 |
|
---|
135 | /**
|
---|
136 | VspNode abstract class serving for interior and leaf node implementation
|
---|
137 | */
|
---|
138 | class VspNode
|
---|
139 | {
|
---|
140 |
|
---|
141 | public:
|
---|
142 | VspNode();
|
---|
143 | virtual ~VspNode(){};
|
---|
144 | VspNode(VspInterior *parent);
|
---|
145 |
|
---|
146 | /** Determines whether this node is a leaf or not
|
---|
147 | @return true if leaf
|
---|
148 | */
|
---|
149 | virtual bool IsLeaf() const = 0;
|
---|
150 |
|
---|
151 | /** Determines whether this node is a root
|
---|
152 | @return true if root
|
---|
153 | */
|
---|
154 | virtual bool IsRoot() const;
|
---|
155 |
|
---|
156 | /** Returns parent node.
|
---|
157 | */
|
---|
158 | VspInterior *GetParent();
|
---|
159 |
|
---|
160 | /** Sets parent node.
|
---|
161 | */
|
---|
162 | void SetParent(VspInterior *parent);
|
---|
163 |
|
---|
164 | /** Returns true if this node is a sibling of node n.
|
---|
165 | */
|
---|
166 | bool IsSibling(VspNode *n) const;
|
---|
167 |
|
---|
168 | /** returns depth of the node.
|
---|
169 | */
|
---|
170 | int GetDepth() const;
|
---|
171 |
|
---|
172 | /** returns true if the whole subtree is valid
|
---|
173 | */
|
---|
174 | bool TreeValid() const;
|
---|
175 |
|
---|
176 | void SetTreeValid(const bool v);
|
---|
177 |
|
---|
178 | //-- mailing options
|
---|
179 |
|
---|
180 | void Mail() { mMailbox = sMailId; }
|
---|
181 | static void NewMail() { ++ sMailId; }
|
---|
182 | bool Mailed() const { return mMailbox == sMailId; }
|
---|
183 |
|
---|
184 | static int sMailId;
|
---|
185 | int mMailbox;
|
---|
186 |
|
---|
187 | int mTimeStamp;
|
---|
188 |
|
---|
189 | protected:
|
---|
190 |
|
---|
191 | /// if this sub tree is a completely valid view space region
|
---|
192 | bool mTreeValid;
|
---|
193 | /// parent of this node
|
---|
194 | VspInterior *mParent;
|
---|
195 | };
|
---|
196 |
|
---|
197 |
|
---|
198 | /** BSP interior node implementation
|
---|
199 | */
|
---|
200 | class VspInterior: public VspNode
|
---|
201 | {
|
---|
202 | public:
|
---|
203 | /** Standard contructor taking split plane as argument.
|
---|
204 | */
|
---|
205 | VspInterior(const AxisAlignedPlane &plane);
|
---|
206 | ~VspInterior();
|
---|
207 | /** @return false since it is an interior node
|
---|
208 | */
|
---|
209 | bool IsLeaf() const;
|
---|
210 |
|
---|
211 | VspNode *GetBack();
|
---|
212 | VspNode *GetFront();
|
---|
213 |
|
---|
214 | /** Returns split plane.
|
---|
215 | */
|
---|
216 | AxisAlignedPlane GetPlane() const;
|
---|
217 |
|
---|
218 | /** Replace front or back child with new child.
|
---|
219 | */
|
---|
220 | void ReplaceChildLink(VspNode *oldChild, VspNode *newChild);
|
---|
221 | /** Replace front and back child.
|
---|
222 | */
|
---|
223 | void SetupChildLinks(VspNode *b, VspNode *f);
|
---|
224 |
|
---|
225 | friend ostream &operator<<(ostream &s, const VspInterior &A)
|
---|
226 | {
|
---|
227 | return s << A.mPlane.mAxis << " " << A.mPlane.mPosition;
|
---|
228 | }
|
---|
229 |
|
---|
230 | protected:
|
---|
231 |
|
---|
232 | /// Splitting plane corresponding to this node
|
---|
233 | AxisAlignedPlane mPlane;
|
---|
234 |
|
---|
235 | /// back node
|
---|
236 | VspNode *mBack;
|
---|
237 | /// front node
|
---|
238 | VspNode *mFront;
|
---|
239 | };
|
---|
240 |
|
---|
241 |
|
---|
242 | /** BSP leaf node implementation.
|
---|
243 | */
|
---|
244 | class VspLeaf: public VspNode
|
---|
245 | {
|
---|
246 |
|
---|
247 | public:
|
---|
248 | VspLeaf();
|
---|
249 | VspLeaf(ViewCellLeaf *viewCell);
|
---|
250 | VspLeaf(VspInterior *parent);
|
---|
251 | VspLeaf(VspInterior *parent, ViewCellLeaf *viewCell);
|
---|
252 |
|
---|
253 | ~VspLeaf();
|
---|
254 |
|
---|
255 | /** @return true since it is an interior node
|
---|
256 | */
|
---|
257 | bool IsLeaf() const;
|
---|
258 |
|
---|
259 | /** Returns pointer of view cell.
|
---|
260 | */
|
---|
261 | ViewCellLeaf *GetViewCell() const;
|
---|
262 |
|
---|
263 | /** Sets pointer to view cell.
|
---|
264 | */
|
---|
265 | void SetViewCell(ViewCellLeaf *viewCell);
|
---|
266 |
|
---|
267 | /// Rays piercing this leaf.
|
---|
268 | VssRayContainer mVssRays;
|
---|
269 |
|
---|
270 | /// leaf pvs
|
---|
271 | ObjectPvs *mPvs;
|
---|
272 |
|
---|
273 | /// Probability that the view point lies in this leaf
|
---|
274 | float mProbability;
|
---|
275 |
|
---|
276 | protected:
|
---|
277 |
|
---|
278 | /// if NULL this does not correspond to feasible viewcell
|
---|
279 | ViewCellLeaf *mViewCell;
|
---|
280 | };
|
---|
281 |
|
---|
282 |
|
---|
283 | /**
|
---|
284 | This class implements a structure holding two different hierarchies,
|
---|
285 | one for object space partitioning and one for view space partitioning.
|
---|
286 |
|
---|
287 | The object space and the view space are subdivided using a cost heuristics.
|
---|
288 | If an object space split or a view space split is chosen is also evaluated
|
---|
289 | based on the heuristics.
|
---|
290 |
|
---|
291 | The view space heuristics is evaluated by weighting and adding the pvss of the back and
|
---|
292 | front node of each specific split. unlike for the standalone method vspbsp tree,
|
---|
293 | the pvs of an object would not be the pvs of single object but that of all objects
|
---|
294 | which are contained in the same leaf of the object subdivision. This could be done
|
---|
295 | by storing the pointer to the object space partition parent, which would allow access to all children.
|
---|
296 | Another possibility is to include traced kd-cells in the ray casing process.
|
---|
297 |
|
---|
298 | Accordingly, the object space heuristics is evaluated by storing a pvs of view cells with each object.
|
---|
299 | the contribution to an object to the pvs is the number of view cells it can be seen from.
|
---|
300 |
|
---|
301 |
|
---|
302 | There is a potential efficiency problem involved in a sense that once a certain type
|
---|
303 | of split is chosen for view space / object space, the candidates for the next split of
|
---|
304 | object space / view space must be reevaluated.
|
---|
305 |
|
---|
306 | */
|
---|
307 | class VspOspTree
|
---|
308 | {
|
---|
309 | friend class ViewCellsParseHandlers;
|
---|
310 | friend class VspVspViewCellsManager;
|
---|
311 |
|
---|
312 | public:
|
---|
313 |
|
---|
314 | /** Additional data which is passed down the BSP tree during traversal.
|
---|
315 | */
|
---|
316 | class VspOspTraversalData
|
---|
317 | {
|
---|
318 | public:
|
---|
319 | /// the current node
|
---|
320 | VspNode *mNode;
|
---|
321 | /// current depth
|
---|
322 | int mDepth;
|
---|
323 | /// rays piercing this node
|
---|
324 | RayInfoContainer *mRays;
|
---|
325 | /// the probability that this node contains view point
|
---|
326 | float mProbability;
|
---|
327 | /// the bounding box of the node
|
---|
328 | AxisAlignedBox3 mBoundingBox;
|
---|
329 | /// pvs size
|
---|
330 | int mPvs;
|
---|
331 | /// how often this branch has missed the max-cost ratio
|
---|
332 | int mMaxCostMisses;
|
---|
333 | // current axis
|
---|
334 | int mAxis;
|
---|
335 | // current priority
|
---|
336 | float mPriority;
|
---|
337 |
|
---|
338 |
|
---|
339 | /** Returns average ray contribution.
|
---|
340 | */
|
---|
341 | float GetAvgRayContribution() const
|
---|
342 | {
|
---|
343 | return (float)mPvs / ((float)mRays->size() + Limits::Small);
|
---|
344 | }
|
---|
345 |
|
---|
346 |
|
---|
347 | VspOspTraversalData():
|
---|
348 | mNode(NULL),
|
---|
349 | mDepth(0),
|
---|
350 | mRays(NULL),
|
---|
351 | mPvs(0),
|
---|
352 | mProbability(0.0),
|
---|
353 | mMaxCostMisses(0),
|
---|
354 | mPriority(0),
|
---|
355 | mAxis(0)
|
---|
356 | {}
|
---|
357 |
|
---|
358 | VspOspTraversalData(VspNode *node,
|
---|
359 | const int depth,
|
---|
360 | RayInfoContainer *rays,
|
---|
361 | const int pvs,
|
---|
362 | const float p,
|
---|
363 | const AxisAlignedBox3 &box):
|
---|
364 | mNode(node),
|
---|
365 | mDepth(depth),
|
---|
366 | mRays(rays),
|
---|
367 | mPvs(pvs),
|
---|
368 | mProbability(p),
|
---|
369 | mBoundingBox(box),
|
---|
370 | mMaxCostMisses(0),
|
---|
371 | mPriority(0),
|
---|
372 | mAxis(0)
|
---|
373 | {}
|
---|
374 |
|
---|
375 | VspOspTraversalData(PolygonContainer *polys,
|
---|
376 | 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 | mAxis(0),
|
---|
386 | mBoundingBox(box)
|
---|
387 | {}
|
---|
388 |
|
---|
389 | /** Returns cost of the traversal data.
|
---|
390 | */
|
---|
391 | float GetCost() const
|
---|
392 | {
|
---|
393 | //cout << mPriority << endl;
|
---|
394 | return mPriority;
|
---|
395 | }
|
---|
396 |
|
---|
397 | // deletes contents and sets them to NULL
|
---|
398 | void Clear()
|
---|
399 | {
|
---|
400 | DEL_PTR(mRays);
|
---|
401 | }
|
---|
402 |
|
---|
403 | friend bool operator<(const VspOspTraversalData &a, const VspOspTraversalData &b)
|
---|
404 | {
|
---|
405 | return a.GetCost() < b.GetCost();
|
---|
406 | }
|
---|
407 | };
|
---|
408 |
|
---|
409 |
|
---|
410 | typedef std::priority_queue<VspOspTraversalData> VspOspTraversalQueue;
|
---|
411 |
|
---|
412 |
|
---|
413 | struct VspOspSplitCandidate
|
---|
414 | {
|
---|
415 | /// the current plane
|
---|
416 | AxisAlignedPlane mSplitPlane;
|
---|
417 | /// the number of misses of max cost ratio until this split
|
---|
418 | int mMaxCostMisses;
|
---|
419 | /// parent data
|
---|
420 | VspOspTraversalData mParentData;
|
---|
421 | /// cost of applying this split
|
---|
422 | float mRenderCost;
|
---|
423 |
|
---|
424 | VspOspSplitCandidate(): mRenderCost(0)
|
---|
425 | {};
|
---|
426 |
|
---|
427 | VspOspSplitCandidate(const AxisAlignedPlane &plane, const VspOspTraversalData &tData):
|
---|
428 | mSplitPlane(plane), mParentData(tData), mRenderCost(0)
|
---|
429 | {}
|
---|
430 |
|
---|
431 | /** Returns cost of the traversal data.
|
---|
432 | */
|
---|
433 | float GetCost() const
|
---|
434 | {
|
---|
435 | #if 1
|
---|
436 | return mRenderCost;
|
---|
437 | #else
|
---|
438 | return (float) (-mDepth); // for kd tree
|
---|
439 | #endif
|
---|
440 | }
|
---|
441 |
|
---|
442 | friend bool operator<(const VspOspSplitCandidate &a, const VspOspSplitCandidate &b)
|
---|
443 | {
|
---|
444 | return a.GetCost() < b.GetCost();
|
---|
445 | }
|
---|
446 | };
|
---|
447 |
|
---|
448 | typedef std::priority_queue<VspOspSplitCandidate> VspOspSplitQueue;
|
---|
449 |
|
---|
450 | /** Default constructor creating an empty tree.
|
---|
451 | */
|
---|
452 | VspOspTree();
|
---|
453 |
|
---|
454 | /** Default destructor.
|
---|
455 | */
|
---|
456 | ~VspOspTree();
|
---|
457 |
|
---|
458 | /** Returns BSP Tree statistics.
|
---|
459 | */
|
---|
460 | const VspTreeStatistics &GetStatistics() const;
|
---|
461 |
|
---|
462 |
|
---|
463 | /** Constructs the tree from a given set of rays.
|
---|
464 | @param sampleRays the set of sample rays the construction is based on
|
---|
465 | @param viewCells if not NULL, new view cells are
|
---|
466 | created in the leafs and stored in the container
|
---|
467 | */
|
---|
468 | void Construct(const VssRayContainer &sampleRays,
|
---|
469 | AxisAlignedBox3 *forcedBoundingBox);
|
---|
470 |
|
---|
471 | /** Returns list of BSP leaves with pvs smaller than
|
---|
472 | a certain threshold.
|
---|
473 | @param onlyUnmailed if only the unmailed leaves should be considered
|
---|
474 | @param maxPvs the maximal pvs (-1 means unlimited)
|
---|
475 | */
|
---|
476 | void CollectLeaves(vector<VspLeaf *> &leaves,
|
---|
477 | const bool onlyUnmailed = false,
|
---|
478 | const int maxPvs = -1) const;
|
---|
479 |
|
---|
480 | /** Returns box which bounds the whole tree.
|
---|
481 | */
|
---|
482 | AxisAlignedBox3 GetBoundingBox()const;
|
---|
483 |
|
---|
484 | /** Returns root of the view space partitioning tree.
|
---|
485 | */
|
---|
486 | VspNode *GetRoot() const;
|
---|
487 |
|
---|
488 | /** Collects the leaf view cells of the tree
|
---|
489 | @param viewCells returns the view cells
|
---|
490 | */
|
---|
491 | void CollectViewCells(ViewCellContainer &viewCells, bool onlyValid) const;
|
---|
492 |
|
---|
493 | /** A ray is cast possible intersecting the tree.
|
---|
494 | @param the ray that is cast.
|
---|
495 | @returns the number of intersections with objects stored in the tree.
|
---|
496 | */
|
---|
497 | int CastRay(Ray &ray);
|
---|
498 |
|
---|
499 |
|
---|
500 | /** finds neighbouring leaves of this tree node.
|
---|
501 | */
|
---|
502 | int FindNeighbors(VspNode *n,
|
---|
503 | vector<VspLeaf *> &neighbors,
|
---|
504 | const bool onlyUnmailed) const;
|
---|
505 |
|
---|
506 | /** Returns random leaf of BSP tree.
|
---|
507 | @param halfspace defines the halfspace from which the leaf is taken.
|
---|
508 | */
|
---|
509 | VspLeaf *GetRandomLeaf(const Plane3 &halfspace);
|
---|
510 |
|
---|
511 | /** Returns random leaf of BSP tree.
|
---|
512 | @param onlyUnmailed if only unmailed leaves should be returned.
|
---|
513 | */
|
---|
514 | VspLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
|
---|
515 |
|
---|
516 | /** Returns epsilon of this tree.
|
---|
517 | */
|
---|
518 | float GetEpsilon() const;
|
---|
519 |
|
---|
520 | /** Casts line segment into the tree.
|
---|
521 | @param origin the origin of the line segment
|
---|
522 | @param termination the end point of the line segment
|
---|
523 | @returns view cells intersecting the line segment.
|
---|
524 | */
|
---|
525 | int CastLineSegment(const Vector3 &origin,
|
---|
526 | const Vector3 &termination,
|
---|
527 | ViewCellContainer &viewcells);
|
---|
528 |
|
---|
529 |
|
---|
530 | /** Sets pointer to view cells manager.
|
---|
531 | */
|
---|
532 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
533 |
|
---|
534 |
|
---|
535 | /** Collapses the tree with respect to the view cell partition.
|
---|
536 | @returns number of collapsed nodes
|
---|
537 | */
|
---|
538 | int CollapseTree();
|
---|
539 |
|
---|
540 | /** Returns view cell the current point is located in.
|
---|
541 | @param point the current view point
|
---|
542 | @param active if currently active view cells should be returned or
|
---|
543 | elementary view cell
|
---|
544 | */
|
---|
545 | ViewCell *GetViewCell(const Vector3 &point, const bool active = false);
|
---|
546 |
|
---|
547 |
|
---|
548 | /** Returns true if this view point is in a valid view space,
|
---|
549 | false otherwise.
|
---|
550 | */
|
---|
551 | bool ViewPointValid(const Vector3 &viewPoint) const;
|
---|
552 |
|
---|
553 | /** Returns view cell corresponding to
|
---|
554 | the invalid view space.
|
---|
555 | */
|
---|
556 | VspViewCell *GetOutOfBoundsCell();
|
---|
557 |
|
---|
558 | /** Writes tree to output stream
|
---|
559 | */
|
---|
560 | #if ZIPPED_VIEWCELLS
|
---|
561 | bool Export(ogzstream &stream);
|
---|
562 | #else
|
---|
563 | bool Export(ofstream &stream);
|
---|
564 | #endif
|
---|
565 |
|
---|
566 | /** Casts beam, i.e. a 5D frustum of rays, into tree.
|
---|
567 | Tests conservative using the bounding box of the nodes.
|
---|
568 | @returns number of view cells it intersected
|
---|
569 | */
|
---|
570 | int CastBeam(Beam &beam);
|
---|
571 |
|
---|
572 |
|
---|
573 | /** Checks if tree validity-flags are right
|
---|
574 | with respect to view cell valitiy.
|
---|
575 | If not, marks subtree as invalid.
|
---|
576 | */
|
---|
577 | void ValidateTree();
|
---|
578 |
|
---|
579 | /** Invalid view cells are added to the unbounded space
|
---|
580 | */
|
---|
581 | void CollapseViewCells();
|
---|
582 |
|
---|
583 | /** Collects rays stored in the leaves.
|
---|
584 | */
|
---|
585 | void CollectRays(VssRayContainer &rays);
|
---|
586 |
|
---|
587 | /** Intersects box with the tree and returns the number of intersected boxes.
|
---|
588 | @returns number of view cells found
|
---|
589 | */
|
---|
590 | int ComputeBoxIntersections(const AxisAlignedBox3 &box, ViewCellContainer &viewCells) const;
|
---|
591 |
|
---|
592 | // pointer to the hierarchy of view cells
|
---|
593 | ViewCellsTree *mViewCellsTree;
|
---|
594 |
|
---|
595 |
|
---|
596 | protected:
|
---|
597 |
|
---|
598 | // --------------------------------------------------------------
|
---|
599 | // For sorting objects
|
---|
600 | // --------------------------------------------------------------
|
---|
601 | struct SortableEntry
|
---|
602 | {
|
---|
603 | enum EType
|
---|
604 | {
|
---|
605 | ERayMin,
|
---|
606 | ERayMax
|
---|
607 | };
|
---|
608 |
|
---|
609 | int type;
|
---|
610 | float value;
|
---|
611 | VssRay *ray;
|
---|
612 |
|
---|
613 | SortableEntry() {}
|
---|
614 | SortableEntry(const int t, const float v, VssRay *r):type(t),
|
---|
615 | value(v), ray(r)
|
---|
616 | {
|
---|
617 | }
|
---|
618 |
|
---|
619 | friend bool operator<(const SortableEntry &a, const SortableEntry &b)
|
---|
620 | {
|
---|
621 | return a.value < b.value;
|
---|
622 | }
|
---|
623 | };
|
---|
624 |
|
---|
625 | /** faster evaluation of split plane cost for kd axis aligned cells.
|
---|
626 | */
|
---|
627 | float EvalSplitCost(const VspOspTraversalData &data,
|
---|
628 | const AxisAlignedBox3 &box,
|
---|
629 | const int axis,
|
---|
630 | const float &position,
|
---|
631 | float &pFront,
|
---|
632 | float &pBack) const;
|
---|
633 |
|
---|
634 | /** Evaluates candidate for splitting.
|
---|
635 | */
|
---|
636 | void EvalSplitCandidate(VspOspTraversalData &tData, VspOspSplitCandidate &splitData);
|
---|
637 |
|
---|
638 | /** Computes priority of the traversal data and stores it in tData.
|
---|
639 | */
|
---|
640 | void EvalPriority(VspOspTraversalData &tData) const;
|
---|
641 |
|
---|
642 | /** Evaluates render cost decrease of next split.
|
---|
643 | */
|
---|
644 | float EvalRenderCostDecrease(const AxisAlignedPlane &candidatePlane,
|
---|
645 | const VspOspTraversalData &data) const;
|
---|
646 |
|
---|
647 | /** Constructs tree using the split priority queue.
|
---|
648 | */
|
---|
649 | void Construct(RayInfoContainer *rays);
|
---|
650 |
|
---|
651 | /** Collects view cells in the subtree under root.
|
---|
652 | */
|
---|
653 | void CollectViewCells(VspNode *root,
|
---|
654 | bool onlyValid,
|
---|
655 | ViewCellContainer &viewCells,
|
---|
656 | bool onlyUnmailed = false) const;
|
---|
657 |
|
---|
658 | /** Returns view cell corresponding to
|
---|
659 | the invalid view space. If it does not exist, it is created.
|
---|
660 | */
|
---|
661 | VspViewCell *GetOrCreateOutOfBoundsCell();
|
---|
662 |
|
---|
663 | /** Collapses the tree with respect to the view cell partition,
|
---|
664 | i.e. leaves having the same view cell are collapsed.
|
---|
665 | @param node the root of the subtree to be collapsed
|
---|
666 | @param collapsed returns the number of collapsed nodes
|
---|
667 | @returns node of type leaf if the node could be collapsed,
|
---|
668 | this node otherwise
|
---|
669 | */
|
---|
670 | VspNode *CollapseTree(VspNode *node, int &collapsed);
|
---|
671 |
|
---|
672 | /** Helper function revalidating the view cell leaf list after merge.
|
---|
673 | */
|
---|
674 | void RepairViewCellsLeafLists();
|
---|
675 |
|
---|
676 | /** Evaluates tree stats in the BSP tree leafs.
|
---|
677 | */
|
---|
678 | void EvaluateLeafStats(const VspOspTraversalData &data);
|
---|
679 |
|
---|
680 | /** Subdivides node with respect to the traversal data.
|
---|
681 | @param tStack current traversal stack
|
---|
682 | @param tData traversal data also holding node to be subdivided
|
---|
683 | @returns new root of the subtree
|
---|
684 | */
|
---|
685 | VspNode *Subdivide(VspOspSplitQueue &tQueue,
|
---|
686 | VspOspSplitCandidate &splitCandidate);
|
---|
687 |
|
---|
688 |
|
---|
689 | /** Subdivides leaf.
|
---|
690 | @param leaf the leaf to be subdivided
|
---|
691 |
|
---|
692 | @param polys the polygons to be split
|
---|
693 | @param frontPolys returns the polygons in front of the split plane
|
---|
694 | @param backPolys returns the polygons in the back of the split plane
|
---|
695 |
|
---|
696 | @param rays the polygons to be filtered
|
---|
697 | @param frontRays returns the polygons in front of the split plane
|
---|
698 | @param backRays returns the polygons in the back of the split plane
|
---|
699 |
|
---|
700 | @returns the root of the subdivision
|
---|
701 | */
|
---|
702 |
|
---|
703 | VspInterior *SubdivideNode(const AxisAlignedPlane &splitPlane,
|
---|
704 | VspOspTraversalData &tData,
|
---|
705 | VspOspTraversalData &frontData,
|
---|
706 | VspOspTraversalData &backData);
|
---|
707 |
|
---|
708 | /** Selects an axis aligned for the next split.
|
---|
709 | @returns cost for this split
|
---|
710 | */
|
---|
711 | float SelectPlane(const VspOspTraversalData &tData,
|
---|
712 | AxisAlignedPlane &plane,
|
---|
713 | float &pFront,
|
---|
714 | float &pBack);
|
---|
715 |
|
---|
716 | /** Sorts split candidates for surface area heuristics for axis aligned splits.
|
---|
717 | @param polys the input for choosing split candidates
|
---|
718 | @param axis the current split axis
|
---|
719 | @param splitCandidates returns sorted list of split candidates
|
---|
720 | */
|
---|
721 | void SortSplitCandidates(const RayInfoContainer &rays,
|
---|
722 | const int axis,
|
---|
723 | float minBand,
|
---|
724 | float maxBand);
|
---|
725 |
|
---|
726 | /** Computes best cost for axis aligned planes.
|
---|
727 | */
|
---|
728 | float BestCostRatioHeuristics(const RayInfoContainer &rays,
|
---|
729 | const AxisAlignedBox3 &box,
|
---|
730 | const int pvsSize,
|
---|
731 | const int axis,
|
---|
732 | float &position);
|
---|
733 |
|
---|
734 | /** Subdivides the rays into front and back rays according to the split plane.
|
---|
735 |
|
---|
736 | @param plane the split plane
|
---|
737 | @param rays contains the rays to be split. The rays are
|
---|
738 | distributed into front and back rays.
|
---|
739 | @param frontRays returns rays on the front side of the plane
|
---|
740 | @param backRays returns rays on the back side of the plane
|
---|
741 |
|
---|
742 | @returns the number of splits
|
---|
743 | */
|
---|
744 | int SplitRays(const Plane3 &plane,
|
---|
745 | RayInfoContainer &rays,
|
---|
746 | RayInfoContainer &frontRays,
|
---|
747 | RayInfoContainer &backRays) const;
|
---|
748 |
|
---|
749 | /** Adds the object to the pvs of the front and back leaf with a given classification.
|
---|
750 |
|
---|
751 | @param obj the object to be added
|
---|
752 | @param cf the ray classification regarding the split plane
|
---|
753 | @param frontPvs returns the PVS of the front partition
|
---|
754 | @param backPvs returns the PVS of the back partition
|
---|
755 |
|
---|
756 | */
|
---|
757 | void AddObjToPvs(Intersectable *obj,
|
---|
758 | const int cf,
|
---|
759 | float &frontPvs,
|
---|
760 | float &backPvs,
|
---|
761 | float &totalPvs) const;
|
---|
762 |
|
---|
763 | /** Computes PVS size induced by the rays.
|
---|
764 | */
|
---|
765 | int ComputePvsSize(const RayInfoContainer &rays) const;
|
---|
766 |
|
---|
767 | /** Returns true if tree can be terminated.
|
---|
768 | */
|
---|
769 | inline bool LocalTerminationCriteriaMet(const VspOspTraversalData &data) const;
|
---|
770 |
|
---|
771 | /** Returns true if global tree can be terminated.
|
---|
772 | */
|
---|
773 | inline bool GlobalTerminationCriteriaMet(const VspOspTraversalData &data) const;
|
---|
774 |
|
---|
775 | /** Adds ray sample contributions to the PVS.
|
---|
776 | @param sampleContributions the number contributions of the samples
|
---|
777 | @param contributingSampels the number of contributing rays
|
---|
778 |
|
---|
779 | */
|
---|
780 | void AddToPvs(VspLeaf *leaf,
|
---|
781 | const RayInfoContainer &rays,
|
---|
782 | float &sampleContributions,
|
---|
783 | int &contributingSamples);
|
---|
784 |
|
---|
785 | /** Propagates valid flag up the tree.
|
---|
786 | */
|
---|
787 | void PropagateUpValidity(VspNode *node);
|
---|
788 |
|
---|
789 | /** Writes the node to disk
|
---|
790 | @note: should be implemented as visitor.
|
---|
791 | */
|
---|
792 | #if ZIPPED_VIEWCELLS
|
---|
793 | void ExportNode(VspNode *node, ogzstream &stream);
|
---|
794 | #else
|
---|
795 | void ExportNode(VspNode *node, ofstream &stream);
|
---|
796 | #endif
|
---|
797 |
|
---|
798 | /** Returns estimated memory usage of tree.
|
---|
799 | */
|
---|
800 | float GetMemUsage() const;
|
---|
801 |
|
---|
802 |
|
---|
803 | protected:
|
---|
804 |
|
---|
805 | ViewCellsManager *mViewCellsManager;
|
---|
806 | vector<SortableEntry> *mSplitCandidates;
|
---|
807 |
|
---|
808 | /// Pointer to the root of the tree
|
---|
809 | VspNode *mRoot;
|
---|
810 |
|
---|
811 | VspTreeStatistics mVspStats;
|
---|
812 |
|
---|
813 | /// View cell corresponding to the space outside the valid view space
|
---|
814 | VspViewCell *mOutOfBoundsCell;
|
---|
815 |
|
---|
816 | /// box around the whole view domain
|
---|
817 | AxisAlignedBox3 mBox;
|
---|
818 |
|
---|
819 |
|
---|
820 |
|
---|
821 | //-- local termination
|
---|
822 |
|
---|
823 | /// minimal number of rays before subdivision termination
|
---|
824 | int mTermMinRays;
|
---|
825 | /// maximal possible depth
|
---|
826 | int mTermMaxDepth;
|
---|
827 | /// mininum probability
|
---|
828 | float mTermMinProbability;
|
---|
829 | /// mininum PVS
|
---|
830 | int mTermMinPvs;
|
---|
831 | /// maximal contribution per ray
|
---|
832 | float mTermMaxRayContribution;
|
---|
833 | /// maximal acceptable cost ratio
|
---|
834 | float mTermMaxCostRatio;
|
---|
835 | /// tolerance value indicating how often the max cost ratio can be failed
|
---|
836 | int mTermMissTolerance;
|
---|
837 |
|
---|
838 |
|
---|
839 | //-- global criteria
|
---|
840 | float mTermMinGlobalCostRatio;
|
---|
841 | int mTermGlobalCostMissTolerance;
|
---|
842 | int mGlobalCostMisses;
|
---|
843 |
|
---|
844 | /// maximal number of view cells
|
---|
845 | int mMaxViewCells;
|
---|
846 | /// maximal tree memory
|
---|
847 | float mMaxMemory;
|
---|
848 | /// the tree is out of memory
|
---|
849 | bool mOutOfMemory;
|
---|
850 |
|
---|
851 |
|
---|
852 |
|
---|
853 | //-- split heuristics based parameters
|
---|
854 |
|
---|
855 | bool mUseCostHeuristics;
|
---|
856 | /// balancing factor for PVS criterium
|
---|
857 | float mCtDivCi;
|
---|
858 | /// if only driving axis should be used for split
|
---|
859 | bool mOnlyDrivingAxis;
|
---|
860 | /// if random split axis should be used
|
---|
861 | bool mUseRandomAxis;
|
---|
862 | /// if vsp bsp tree should simulate octree
|
---|
863 | bool mCirculatingAxis;
|
---|
864 | /// minimal relative position where the split axis can be placed
|
---|
865 | float mMinBand;
|
---|
866 | /// maximal relative position where the split axis can be placed
|
---|
867 | float mMaxBand;
|
---|
868 |
|
---|
869 |
|
---|
870 | /// current time stamp (used for keeping split history)
|
---|
871 | int mTimeStamp;
|
---|
872 | // if rays should be stored in leaves
|
---|
873 | bool mStoreRays;
|
---|
874 |
|
---|
875 | /// epsilon for geometric comparisons
|
---|
876 | float mEpsilon;
|
---|
877 |
|
---|
878 | /// if we should use breath first priority for the splits
|
---|
879 | int mNodePriorityQueueType;
|
---|
880 |
|
---|
881 | // priority queue strategy
|
---|
882 | enum {BREATH_FIRST, DEPTH_FIRST, COST_BASED};
|
---|
883 |
|
---|
884 |
|
---|
885 | /// subdivision stats output file
|
---|
886 | ofstream mSubdivisionStats;
|
---|
887 | /// keeps track of cost during subdivision
|
---|
888 | float mTotalCost;
|
---|
889 | /// keeps track of overall pvs size during subdivision
|
---|
890 | int mTotalPvsSize;
|
---|
891 | /// number of currenly generated view cells
|
---|
892 | int mCreatedViewCells;
|
---|
893 |
|
---|
894 | private:
|
---|
895 |
|
---|
896 | /// Generates unique ids for PVS criterium
|
---|
897 | static void GenerateUniqueIdsForPvs();
|
---|
898 |
|
---|
899 | //-- unique ids for PVS criterium
|
---|
900 | static int sFrontId;
|
---|
901 | static int sBackId;
|
---|
902 | static int sFrontAndBackId;
|
---|
903 | };
|
---|
904 |
|
---|
905 | }
|
---|
906 |
|
---|
907 |
|
---|
908 | #endif
|
---|