1 | #ifndef _OspTree_H__
|
---|
2 | #define _OspTree_H__
|
---|
3 |
|
---|
4 | #include <stack>
|
---|
5 |
|
---|
6 | #include "Mesh.h"
|
---|
7 | #include "Containers.h"
|
---|
8 | #include "Statistics.h"
|
---|
9 | #include "VssRay.h"
|
---|
10 | #include "RayInfo.h"
|
---|
11 | #include "gzstream.h"
|
---|
12 | #include "SubdivisionCandidate.h"
|
---|
13 | #include "IntersectableWrapper.h"
|
---|
14 | #include "KdTree.h"
|
---|
15 | #include "HierarchyManager.h"
|
---|
16 |
|
---|
17 |
|
---|
18 |
|
---|
19 | namespace GtpVisibilityPreprocessor {
|
---|
20 |
|
---|
21 | class ViewCellLeaf;
|
---|
22 | class Plane3;
|
---|
23 | class AxisAlignedBox3;
|
---|
24 | class Ray;
|
---|
25 | class ViewCellsStatistics;
|
---|
26 | class ViewCellsManager;
|
---|
27 | class MergeCandidate;
|
---|
28 | class Beam;
|
---|
29 | class ViewCellsTree;
|
---|
30 | class Environment;
|
---|
31 | class VspInterior;
|
---|
32 | class VspLeaf;
|
---|
33 | class VspNode;
|
---|
34 | class KdNode;
|
---|
35 | class KdInterior;
|
---|
36 | class KdLeaf;
|
---|
37 | class OspTree;
|
---|
38 | class KdIntersectable;
|
---|
39 | class KdTree;
|
---|
40 | class VspTree;
|
---|
41 | class KdTreeStatistics;
|
---|
42 | class SubdivisionCandidate;
|
---|
43 | class HierarchyManager;
|
---|
44 | class KdNode;
|
---|
45 |
|
---|
46 |
|
---|
47 | /** View space partition statistics.
|
---|
48 | */
|
---|
49 | class OspTreeStatistics: public StatisticsBase
|
---|
50 | {
|
---|
51 | public:
|
---|
52 | // total number of nodes
|
---|
53 | int nodes;
|
---|
54 | // number of splits
|
---|
55 | int splits[3];
|
---|
56 |
|
---|
57 | // maximal reached depth
|
---|
58 | int maxDepth;
|
---|
59 | // minimal depth
|
---|
60 | int minDepth;
|
---|
61 |
|
---|
62 | // max depth nodes
|
---|
63 | int maxDepthNodes;
|
---|
64 | // minimum depth nodes
|
---|
65 | int minDepthNodes;
|
---|
66 | // max depth nodes
|
---|
67 | int minPvsNodes;
|
---|
68 | // minimum area nodes
|
---|
69 | int minProbabilityNodes;
|
---|
70 | /// nodes termination because of max cost ratio;
|
---|
71 | int maxCostNodes;
|
---|
72 | // max number of objects per node
|
---|
73 | int maxObjectRefs;
|
---|
74 | int objectRefs;
|
---|
75 | /// samples contributing to pvs
|
---|
76 | int contributingSamples;
|
---|
77 | /// sample contributions to pvs
|
---|
78 | int sampleContributions;
|
---|
79 | /// largest pvs
|
---|
80 | int maxPvs;
|
---|
81 | /// number of invalid leaves
|
---|
82 | int invalidLeaves;
|
---|
83 | /// accumulated number of rays refs
|
---|
84 | int accumRays;
|
---|
85 | /// overall potentially visible objects
|
---|
86 | int pvs;
|
---|
87 | // accumulated depth (used to compute average)
|
---|
88 | int accumDepth;
|
---|
89 | // global cost ratio violations
|
---|
90 | int mGlobalCostMisses;
|
---|
91 |
|
---|
92 |
|
---|
93 | // Constructor
|
---|
94 | OspTreeStatistics()
|
---|
95 | {
|
---|
96 | Reset();
|
---|
97 | }
|
---|
98 |
|
---|
99 | int Nodes() const {return nodes;}
|
---|
100 | int Interior() const { return nodes / 2; }
|
---|
101 | int Leaves() const { return (nodes / 2) + 1; }
|
---|
102 |
|
---|
103 | // TODO: computation wrong
|
---|
104 | double AvgDepth() const { return accumDepth / (double)Leaves();};
|
---|
105 |
|
---|
106 |
|
---|
107 | void Reset()
|
---|
108 | {
|
---|
109 | nodes = 0;
|
---|
110 | for (int i = 0; i < 3; ++ i)
|
---|
111 | splits[i] = 0;
|
---|
112 |
|
---|
113 | maxDepth = 0;
|
---|
114 | minDepth = 99999;
|
---|
115 | accumDepth = 0;
|
---|
116 | pvs = 0;
|
---|
117 | maxDepthNodes = 0;
|
---|
118 | minPvsNodes = 0;
|
---|
119 | minProbabilityNodes = 0;
|
---|
120 | maxCostNodes = 0;
|
---|
121 | contributingSamples = 0;
|
---|
122 | sampleContributions = 0;
|
---|
123 | maxPvs = 0;
|
---|
124 | invalidLeaves = 0;
|
---|
125 | objectRefs = 0;
|
---|
126 | maxObjectRefs = 0;
|
---|
127 | mGlobalCostMisses = 0;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | void Print(ostream &app) const;
|
---|
132 |
|
---|
133 | friend ostream &operator<<(ostream &s, const OspTreeStatistics &stat)
|
---|
134 | {
|
---|
135 | stat.Print(s);
|
---|
136 | return s;
|
---|
137 | }
|
---|
138 | };
|
---|
139 |
|
---|
140 |
|
---|
141 | /** Object Space Partitioning Kd tree.
|
---|
142 | */
|
---|
143 | class OspTree
|
---|
144 | {
|
---|
145 | friend class ViewCellsParseHandlers;
|
---|
146 | friend class HierarchyManager;
|
---|
147 |
|
---|
148 | public:
|
---|
149 |
|
---|
150 | /** Additional data which is passed down the BSP tree during traversal.
|
---|
151 | */
|
---|
152 | class OspTraversalData
|
---|
153 | {
|
---|
154 | public:
|
---|
155 | /// the current node
|
---|
156 | KdLeaf *mNode;
|
---|
157 | /// current depth
|
---|
158 | int mDepth;
|
---|
159 | /// rays piercing this node
|
---|
160 | RayInfoContainer *mRays;
|
---|
161 | /// the probability that this node contains view point
|
---|
162 | float mProbability;
|
---|
163 | /// the bounding box of the node
|
---|
164 | AxisAlignedBox3 mBoundingBox;
|
---|
165 | /// pvs size
|
---|
166 | float mRenderCost;
|
---|
167 | /// how often this branch has missed the max-cost ratio
|
---|
168 | int mMaxCostMisses;
|
---|
169 | // current axis
|
---|
170 | int mAxis;
|
---|
171 | // current priority
|
---|
172 | float mPriority;
|
---|
173 |
|
---|
174 |
|
---|
175 | OspTraversalData():
|
---|
176 | mNode(NULL),
|
---|
177 | mRays(NULL),
|
---|
178 | mDepth(0),
|
---|
179 | mRenderCost(0),
|
---|
180 | mProbability(0.0),
|
---|
181 | mMaxCostMisses(0),
|
---|
182 | mPriority(0),
|
---|
183 | mAxis(0)
|
---|
184 | {}
|
---|
185 |
|
---|
186 | OspTraversalData(KdLeaf *node,
|
---|
187 | const int depth,
|
---|
188 | RayInfoContainer *rays,
|
---|
189 | const float rc,
|
---|
190 | const float p,
|
---|
191 | const AxisAlignedBox3 &box):
|
---|
192 | mNode(node),
|
---|
193 | mDepth(depth),
|
---|
194 | mRays(rays),
|
---|
195 | mRenderCost(rc),
|
---|
196 | mProbability(p),
|
---|
197 | mBoundingBox(box),
|
---|
198 | mMaxCostMisses(0),
|
---|
199 | mPriority(0),
|
---|
200 | mAxis(0)
|
---|
201 | {}
|
---|
202 |
|
---|
203 | OspTraversalData(const int depth,
|
---|
204 | RayInfoContainer *rays,
|
---|
205 | const AxisAlignedBox3 &box):
|
---|
206 | mNode(NULL),
|
---|
207 | mDepth(depth),
|
---|
208 | mRays(rays),
|
---|
209 | mRenderCost(0),
|
---|
210 | mProbability(0),
|
---|
211 | mMaxCostMisses(0),
|
---|
212 | mAxis(0),
|
---|
213 | mBoundingBox(box)
|
---|
214 | {}
|
---|
215 |
|
---|
216 | /** Returns cost of the traversal data.
|
---|
217 | */
|
---|
218 | float GetCost() const
|
---|
219 | {
|
---|
220 | //cout << mPriority << endl;
|
---|
221 | return mPriority;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /// deletes contents and sets them to NULL
|
---|
225 | void Clear()
|
---|
226 | {
|
---|
227 | DEL_PTR(mRays);
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | friend bool operator<(const OspTraversalData &a, const OspTraversalData &b)
|
---|
232 | {
|
---|
233 | return a.GetCost() < b.GetCost();
|
---|
234 | }
|
---|
235 | };
|
---|
236 |
|
---|
237 | /** Candidate for a view space split.
|
---|
238 | */
|
---|
239 | class OspSubdivisionCandidate: public SubdivisionCandidate
|
---|
240 | {
|
---|
241 | public:
|
---|
242 | static OspTree* sOspTree;
|
---|
243 |
|
---|
244 | /// the current split plane
|
---|
245 | AxisAlignedPlane mSplitPlane;
|
---|
246 | /// parent data
|
---|
247 | OspTraversalData mParentData;
|
---|
248 |
|
---|
249 | OspSubdivisionCandidate(const OspTraversalData &tData): mParentData(tData)
|
---|
250 | {};
|
---|
251 |
|
---|
252 | int Type() const { return OBJECT_SPACE; }
|
---|
253 |
|
---|
254 | void EvalCandidate(bool computeSplitplane = true)
|
---|
255 | {
|
---|
256 | mDirty = false;
|
---|
257 | // todo: avoid computing split plane
|
---|
258 | sOspTree->EvalSubdivisionCandidate(*this);
|
---|
259 | }
|
---|
260 |
|
---|
261 | bool GlobalTerminationCriteriaMet() const
|
---|
262 | {
|
---|
263 | return sOspTree->GlobalTerminationCriteriaMet(mParentData);
|
---|
264 | }
|
---|
265 |
|
---|
266 | bool Apply(SplitQueue &splitQueue, bool terminationCriteriaMet)
|
---|
267 | {
|
---|
268 | KdNode *n = sOspTree->Subdivide(splitQueue ,this, terminationCriteriaMet);
|
---|
269 |
|
---|
270 | // local or global termination criteria failed
|
---|
271 | return !n->IsLeaf();
|
---|
272 | }
|
---|
273 |
|
---|
274 | void CollectDirtyCandidates(SubdivisionCandidateContainer &dirtyList,
|
---|
275 | const bool onlyUnmailed)
|
---|
276 | {
|
---|
277 | sOspTree->CollectDirtyCandidates(this, dirtyList, onlyUnmailed);
|
---|
278 | }
|
---|
279 |
|
---|
280 | float GetPriority() const
|
---|
281 | {
|
---|
282 | return mPriority;
|
---|
283 | }
|
---|
284 |
|
---|
285 | OspSubdivisionCandidate(const AxisAlignedPlane &plane, const OspTraversalData &tData):
|
---|
286 | mSplitPlane(plane), mParentData(tData)
|
---|
287 | {}
|
---|
288 | };
|
---|
289 |
|
---|
290 | /** Struct for traversing line segment.
|
---|
291 | */
|
---|
292 | struct LineTraversalData
|
---|
293 | {
|
---|
294 | KdNode *mNode;
|
---|
295 | Vector3 mExitPoint;
|
---|
296 |
|
---|
297 | float mMaxT;
|
---|
298 |
|
---|
299 | LineTraversalData () {}
|
---|
300 | LineTraversalData (KdNode *n, const Vector3 &p, const float maxt):
|
---|
301 | mNode(n), mExitPoint(p), mMaxT(maxt) {}
|
---|
302 | };
|
---|
303 |
|
---|
304 | /** Default constructor creating an empty tree.
|
---|
305 | */
|
---|
306 | OspTree();
|
---|
307 |
|
---|
308 | /** Copies tree from a kd tree.
|
---|
309 | */
|
---|
310 | OspTree(const KdTree &kdTree);
|
---|
311 |
|
---|
312 | /** Default destructor.
|
---|
313 | */
|
---|
314 | ~OspTree();
|
---|
315 |
|
---|
316 | /** Returns tree statistics.
|
---|
317 | */
|
---|
318 | const OspTreeStatistics &GetStatistics() const;
|
---|
319 |
|
---|
320 | /** Returns bounding box of the specified node.
|
---|
321 | */
|
---|
322 | AxisAlignedBox3 GetBoundingBox(KdNode *node) const;
|
---|
323 |
|
---|
324 | /** Returns list of leaves with pvs smaller than
|
---|
325 | a certain threshold.
|
---|
326 | @param onlyUnmailed if only the unmailed leaves should be considered
|
---|
327 | @param maxPvs the maximal pvs of a leaf to be added (-1 means unlimited)
|
---|
328 | */
|
---|
329 |
|
---|
330 | void CollectLeaves(vector<KdLeaf *> &leaves) const;
|
---|
331 |
|
---|
332 | /** Returns bounding box of the whole tree (= bbox of root node)
|
---|
333 | */
|
---|
334 | AxisAlignedBox3 GetBoundingBox()const;
|
---|
335 |
|
---|
336 | /** Returns root of the view space partitioning tree.
|
---|
337 | */
|
---|
338 | KdNode *GetRoot() const;
|
---|
339 |
|
---|
340 | /** Collects the leaf view cells of the tree
|
---|
341 | @param viewCells returns the view cells
|
---|
342 | */
|
---|
343 | void CollectViewCells(ViewCellContainer &viewCells, bool onlyValid) const;
|
---|
344 |
|
---|
345 | /** A ray is cast possible intersecting the tree.
|
---|
346 | @param the ray that is cast.
|
---|
347 | @returns the number of intersections with objects stored in the tree.
|
---|
348 | */
|
---|
349 | int CastRay(Ray &ray);
|
---|
350 |
|
---|
351 | /** finds neighbouring leaves of this tree node.
|
---|
352 | */
|
---|
353 | int FindNeighbors(KdLeaf *n,
|
---|
354 | vector<VspLeaf *> &neighbors,
|
---|
355 | const bool onlyUnmailed) const;
|
---|
356 |
|
---|
357 | /** Returns random leaf of BSP tree.
|
---|
358 | @param halfspace defines the halfspace from which the leaf is taken.
|
---|
359 | */
|
---|
360 | KdLeaf *GetRandomLeaf(const Plane3 &halfspace);
|
---|
361 |
|
---|
362 | /** Returns random leaf of BSP tree.
|
---|
363 | @param onlyUnmailed if only unmailed leaves should be returned.
|
---|
364 | */
|
---|
365 | KdLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
|
---|
366 |
|
---|
367 | /** Returns epsilon of this tree.
|
---|
368 | */
|
---|
369 | float GetEpsilon() const;
|
---|
370 |
|
---|
371 | /** Casts line segment into the tree.
|
---|
372 | @param origin the origin of the line segment
|
---|
373 | @param termination the end point of the line segment
|
---|
374 | @returns view cells intersecting the line segment.
|
---|
375 | */
|
---|
376 | int CastLineSegment(const Vector3 &origin,
|
---|
377 | const Vector3 &termination,
|
---|
378 | ViewCellContainer &viewcells);
|
---|
379 |
|
---|
380 | /** Sets pointer to view cells manager.
|
---|
381 | */
|
---|
382 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
383 |
|
---|
384 | /** Writes tree to output stream
|
---|
385 | */
|
---|
386 | bool Export(OUT_STREAM &stream);
|
---|
387 |
|
---|
388 | /** Returns or creates a new intersectable for use in a kd based pvs.
|
---|
389 | The OspTree is responsible for destruction of the intersectable.
|
---|
390 | */
|
---|
391 | KdIntersectable *GetOrCreateKdIntersectable(KdNode *node);
|
---|
392 |
|
---|
393 | /** Collects rays stored in the leaves.
|
---|
394 | */
|
---|
395 | void CollectRays(VssRayContainer &rays);
|
---|
396 |
|
---|
397 | /** Intersects box with the tree and returns the number of intersected boxes.
|
---|
398 | @returns number of view cells found
|
---|
399 | */
|
---|
400 | int ComputeBoxIntersections(const AxisAlignedBox3 &box,
|
---|
401 | ViewCellContainer &viewCells) const;
|
---|
402 |
|
---|
403 |
|
---|
404 | /** Returns kd leaf the point pt lies in, starting from root.
|
---|
405 | */
|
---|
406 | KdLeaf *GetLeaf(const Vector3 &pt, KdNode *root = NULL) const;
|
---|
407 |
|
---|
408 |
|
---|
409 | ViewCellsTree *GetViewCellsTree() const { return mViewCellsTree; }
|
---|
410 |
|
---|
411 | void SetViewCellsTree(ViewCellsTree *vt) { mViewCellsTree = vt; }
|
---|
412 |
|
---|
413 | float EvalRenderCost(const VssRayContainer &myrays);
|
---|
414 | float EvalLeafCost(const OspTraversalData &tData);
|
---|
415 |
|
---|
416 | /** Adds this objects to the kd leaf objects.
|
---|
417 | @warning: Can corrupt the tree
|
---|
418 | */
|
---|
419 | void InsertObjects(KdNode *node, const ObjectContainer &objects);
|
---|
420 |
|
---|
421 | /** Add the leaf to the pvs of the view cell.
|
---|
422 | */
|
---|
423 | bool AddLeafToPvs(
|
---|
424 | KdLeaf *leaf,
|
---|
425 | ViewCell *vc,
|
---|
426 | const float pdf,
|
---|
427 | float &contribution);
|
---|
428 |
|
---|
429 | protected:
|
---|
430 |
|
---|
431 | // --------------------------------------------------------------
|
---|
432 | // For sorting objects
|
---|
433 | // --------------------------------------------------------------
|
---|
434 | struct SortableEntry
|
---|
435 | {
|
---|
436 | /** There is a 3th "event" for rays which intersect a
|
---|
437 | box in the middle. These "events" don't induce a change in
|
---|
438 | pvs size, but may induce a change in view cell volume.
|
---|
439 | */
|
---|
440 | enum EType
|
---|
441 | {
|
---|
442 | BOX_MIN,
|
---|
443 | BOX_MAX,
|
---|
444 | BOX_INTERSECT
|
---|
445 | };
|
---|
446 |
|
---|
447 | int mType;
|
---|
448 | //int mPvs;
|
---|
449 | float mPos;
|
---|
450 | VssRay *mRay;
|
---|
451 |
|
---|
452 | Intersectable *mObject;
|
---|
453 |
|
---|
454 | SortableEntry() {}
|
---|
455 |
|
---|
456 | SortableEntry(const int type,
|
---|
457 | //const float pvs,
|
---|
458 | const float pos,
|
---|
459 | Intersectable *obj,
|
---|
460 | VssRay *ray):
|
---|
461 | mType(type),
|
---|
462 | //mPvs(pvs),
|
---|
463 | mPos(pos),
|
---|
464 | mObject(obj),
|
---|
465 | mRay(ray)
|
---|
466 | {}
|
---|
467 |
|
---|
468 | bool operator<(const SortableEntry &b) const
|
---|
469 | {
|
---|
470 | return mPos < b.mPos;
|
---|
471 | }
|
---|
472 | };
|
---|
473 |
|
---|
474 |
|
---|
475 | /** faster evaluation of split plane cost for kd axis aligned cells.
|
---|
476 | */
|
---|
477 | float EvalLocalSplitCost(const OspTraversalData &data,
|
---|
478 | const AxisAlignedBox3 &box,
|
---|
479 | const int axis,
|
---|
480 | const float &position,
|
---|
481 | float &pFront,
|
---|
482 | float &pBack) const;
|
---|
483 |
|
---|
484 | /** Evaluates candidate for splitting.
|
---|
485 | */
|
---|
486 | void EvalSubdivisionCandidate(OspSubdivisionCandidate &splitData);
|
---|
487 |
|
---|
488 | /** Computes priority of the traversal data and stores it in tData.
|
---|
489 | */
|
---|
490 | void EvalPriority(OspTraversalData &tData) const;
|
---|
491 |
|
---|
492 | /** Evaluates render cost decrease of next split.
|
---|
493 | */
|
---|
494 | float EvalRenderCostDecrease(const AxisAlignedPlane &candidatePlane,
|
---|
495 | const OspTraversalData &data,
|
---|
496 | float &normalizedOldRenderCost) const;
|
---|
497 |
|
---|
498 |
|
---|
499 | /** Collects view cells in the subtree under root.
|
---|
500 | */
|
---|
501 | void CollectViewCells(KdNode *root,
|
---|
502 | bool onlyValid,
|
---|
503 | ViewCellContainer &viewCells,
|
---|
504 | bool onlyUnmailed = false) const;
|
---|
505 |
|
---|
506 | /** Evaluates tree stats in the BSP tree leafs.
|
---|
507 | */
|
---|
508 | void EvaluateLeafStats(const OspTraversalData &data);
|
---|
509 |
|
---|
510 | /** Subdivides node using a best split priority queue.
|
---|
511 | @param tQueue the best split priority queue
|
---|
512 | @param splitCandidate the candidate for the next split
|
---|
513 | @param globalCriteriaMet if the global termination criteria were already met
|
---|
514 | @returns new root of the subtree
|
---|
515 | */
|
---|
516 | KdNode *Subdivide(SplitQueue &tQueue,
|
---|
517 | SubdivisionCandidate *splitCandidate,
|
---|
518 | const bool globalCriteriaMet);
|
---|
519 |
|
---|
520 | /** Subdivides leaf.
|
---|
521 | @param leaf the leaf to be subdivided
|
---|
522 |
|
---|
523 | @param polys the polygons to be split
|
---|
524 | @param frontPolys returns the polygons in front of the split plane
|
---|
525 | @param backPolys returns the polygons in the back of the split plane
|
---|
526 |
|
---|
527 | @param rays the polygons to be filtered
|
---|
528 | @param frontRays returns the polygons in front of the split plane
|
---|
529 | @param backRays returns the polygons in the back of the split plane
|
---|
530 |
|
---|
531 | @returns the root of the subdivision
|
---|
532 | */
|
---|
533 | KdInterior *SubdivideNode(
|
---|
534 | const AxisAlignedPlane &splitPlane,
|
---|
535 | const OspTraversalData &tData,
|
---|
536 | OspTraversalData &frontData,
|
---|
537 | OspTraversalData &backData);
|
---|
538 |
|
---|
539 | void SplitObjects(KdLeaf *leaf,
|
---|
540 | const AxisAlignedPlane & splitPlane,
|
---|
541 | const ObjectContainer &objects,
|
---|
542 | ObjectContainer &front,
|
---|
543 | ObjectContainer &back);
|
---|
544 |
|
---|
545 | /** does some post processing on the objects in the new child leaves.
|
---|
546 | */
|
---|
547 | void ProcessMultipleRefs(KdLeaf *leaf) const;
|
---|
548 |
|
---|
549 | /** Sorts split candidates for cost heuristics using axis aligned splits.
|
---|
550 | @param node the current node
|
---|
551 | @param axis the current split axis
|
---|
552 | */
|
---|
553 | void SortSubdivisionCandidates(const OspTraversalData &data,
|
---|
554 | const int axis,
|
---|
555 | float minBand,
|
---|
556 | float maxBand);
|
---|
557 |
|
---|
558 | /** Computes best cost for axis aligned planes.
|
---|
559 | */
|
---|
560 | float EvalLocalCostHeuristics(const OspTraversalData &tData,
|
---|
561 | const AxisAlignedBox3 &box,
|
---|
562 | const int axis,
|
---|
563 | float &position,
|
---|
564 | int &objectsFront,
|
---|
565 | int &objectsBack);
|
---|
566 |
|
---|
567 | /** Subdivides the rays into front and back rays according to the split plane.
|
---|
568 |
|
---|
569 | @param plane the split plane
|
---|
570 | @param rays contains the rays to be split. The rays are
|
---|
571 | distributed into front and back rays.
|
---|
572 | @param frontRays returns rays on the front side of the plane
|
---|
573 | @param backRays returns rays on the back side of the plane
|
---|
574 |
|
---|
575 | @returns the number of splits
|
---|
576 | */
|
---|
577 | int SplitRays(const AxisAlignedPlane &plane,
|
---|
578 | RayInfoContainer &rays,
|
---|
579 | RayInfoContainer &frontRays,
|
---|
580 | RayInfoContainer &backRays) const;
|
---|
581 |
|
---|
582 | int FilterRays(KdLeaf *leaf, const RayInfoContainer &rays, RayInfoContainer &filteredRays);
|
---|
583 |
|
---|
584 | /** Adds the object to the pvs of the front and back leaf with a given classification.
|
---|
585 |
|
---|
586 | @param obj the object to be added
|
---|
587 | @param cf the ray classification regarding the split plane
|
---|
588 | @param frontPvs returns the PVS of the front partition
|
---|
589 | @param backPvs returns the PVS of the back partition
|
---|
590 |
|
---|
591 | */
|
---|
592 | void UpdateObjPvsContri(Intersectable *obj,
|
---|
593 | const int cf,
|
---|
594 | float &frontPvs,
|
---|
595 | float &backPvs,
|
---|
596 | float &totalPvs) const;
|
---|
597 |
|
---|
598 | /** Returns true if tree can be terminated.
|
---|
599 | */
|
---|
600 | bool LocalTerminationCriteriaMet(const OspTraversalData &data) const;
|
---|
601 |
|
---|
602 | /** Returns true if global tree can be terminated.
|
---|
603 | */
|
---|
604 | bool GlobalTerminationCriteriaMet(const OspTraversalData &data) const;
|
---|
605 |
|
---|
606 | /** Selects an axis aligned for the next split.
|
---|
607 | @returns cost for this split
|
---|
608 | */
|
---|
609 | float SelectSplitPlane(
|
---|
610 | const OspTraversalData &tData,
|
---|
611 | AxisAlignedPlane &plane);
|
---|
612 |
|
---|
613 | /** Propagates valid flag up the tree.
|
---|
614 | */
|
---|
615 | void PropagateUpValidity(KdNode *node);
|
---|
616 |
|
---|
617 | /** Writes the node to disk
|
---|
618 | @note: should be implemented as visitor.
|
---|
619 | */
|
---|
620 | void ExportNode(KdNode *node, OUT_STREAM &stream);
|
---|
621 |
|
---|
622 | /** Returns estimated memory usage of tree.
|
---|
623 | */
|
---|
624 | float GetMemUsage() const;
|
---|
625 |
|
---|
626 | /** Evaluate the contributions of view cell volume of the left and the right view cell.
|
---|
627 | */
|
---|
628 | void EvalRayContribution(
|
---|
629 | KdLeaf *leaf,
|
---|
630 | const VssRay &ray,
|
---|
631 | float &renderCost);
|
---|
632 |
|
---|
633 | void EvalViewCellContribution(
|
---|
634 | KdLeaf *leaf,
|
---|
635 | ViewCell *viewCell,
|
---|
636 | float &renderCost);
|
---|
637 |
|
---|
638 | /** Evaluates the influence on the pvs of the event.
|
---|
639 | @param ve the visibility event
|
---|
640 | @param pvsLeft updates the left pvs
|
---|
641 | @param rightPvs updates the right pvs
|
---|
642 | */
|
---|
643 | void EvalHeuristicsContribution(KdLeaf *leaf,
|
---|
644 | const SortableEntry &ci,
|
---|
645 | float &renderCost,
|
---|
646 | ViewCellContainer &touchedViewCells);
|
---|
647 |
|
---|
648 | /** Prepares objects for the cost heuristics.
|
---|
649 | @returns pvs size of the node
|
---|
650 | */
|
---|
651 | float PrepareHeuristics(
|
---|
652 | const OspTraversalData &tData,
|
---|
653 | ViewCellContainer &touchedViewCells);
|
---|
654 |
|
---|
655 | /** Prepares heuristics for a particular ray.
|
---|
656 | */
|
---|
657 | void PrepareHeuristics(
|
---|
658 | const VssRay &ray,
|
---|
659 | ViewCellContainer &touchedViewCells);
|
---|
660 |
|
---|
661 | /** Prepares construction for vsp and osp trees.
|
---|
662 | */
|
---|
663 | void ComputeBoundingBox(const ObjectContainer &objects);
|
---|
664 |
|
---|
665 | void CollectDirtyCandidates(OspSubdivisionCandidate *sc,
|
---|
666 | vector<SubdivisionCandidate *> &dirtyList,
|
---|
667 | const bool onlyUnmailed);
|
---|
668 |
|
---|
669 | /** Collect view cells which see this kd leaf.
|
---|
670 | */
|
---|
671 | void CollectViewCells(KdLeaf *leaf,
|
---|
672 | ViewCellContainer &viewCells);
|
---|
673 |
|
---|
674 | /** Rays will be clipped to the bounding box.
|
---|
675 | */
|
---|
676 | void PreprocessRays(
|
---|
677 | KdLeaf *root,
|
---|
678 | const VssRayContainer &sampleRays,
|
---|
679 | RayInfoContainer &rays);
|
---|
680 |
|
---|
681 | /** Reads parameters from environment singleton.
|
---|
682 | */
|
---|
683 | void ReadEnvironment();
|
---|
684 |
|
---|
685 | /** Returns true if the specified ray end points is inside the kd leaf.
|
---|
686 | @param isTermination if origin or termination point should be checked
|
---|
687 | */
|
---|
688 | bool EndPointInsideNode(KdLeaf *leaf, VssRay &ray, bool isTermination) const;
|
---|
689 |
|
---|
690 | void AddViewCellVolumeContri(
|
---|
691 | ViewCell *vc,
|
---|
692 | float &frontVol,
|
---|
693 | float &backVol,
|
---|
694 | float &frontAndBackVol) const;
|
---|
695 |
|
---|
696 | /** Classifies and mail view cell with respect to the heuristics contribution.
|
---|
697 | Set view cell mail box according to it's influence on
|
---|
698 | front (0), back (1) and front / back node (2).
|
---|
699 | */
|
---|
700 | void MailViewCell(ViewCell *vc, const int cf) const;
|
---|
701 |
|
---|
702 | int ClassifyRay(VssRay *ray, KdLeaf *leaf, const AxisAlignedPlane &plane) const;
|
---|
703 |
|
---|
704 | void EvalSubdivisionStats(const SubdivisionCandidate &tData);
|
---|
705 |
|
---|
706 | void AddSubdivisionStats(const int viewCells,
|
---|
707 | const float renderCostDecr,
|
---|
708 | const float totalRenderCost);
|
---|
709 |
|
---|
710 | void EvalViewCellsForHeuristics(const VssRay &ray,
|
---|
711 | float &volLeft,
|
---|
712 | float &volRight);
|
---|
713 |
|
---|
714 | float EvalViewCellsVolume(KdLeaf *leaf, const RayInfoContainer &rays) const;
|
---|
715 |
|
---|
716 | int RemoveParentViewCellsPvs(KdLeaf *leaf, const RayInfoContainer &rays) const;
|
---|
717 |
|
---|
718 | int UpdateViewCellsPvs(KdLeaf *leaf, const RayInfoContainer &rays) const;
|
---|
719 |
|
---|
720 | int CheckViewCellsPvs(KdLeaf *leaf, const RayInfoContainer &rays) const;
|
---|
721 |
|
---|
722 | bool AddViewCellToObjectPvs(Intersectable *obj,
|
---|
723 | ViewCell *vc,
|
---|
724 | float &contribution,
|
---|
725 | bool onlyUnmailed) const;
|
---|
726 |
|
---|
727 | int ClassifyRays(const RayInfoContainer &rays,
|
---|
728 | KdLeaf *leaf,
|
---|
729 | const AxisAlignedPlane &plane,
|
---|
730 | RayInfoContainer &frontRays,
|
---|
731 | RayInfoContainer &backRays) const;
|
---|
732 |
|
---|
733 | void CollectTouchedViewCells(const RayInfoContainer &rays,
|
---|
734 | ViewCellContainer &touchedViewCells) const;
|
---|
735 |
|
---|
736 | void AddObjectContribution(KdLeaf *leaf,
|
---|
737 | Intersectable * obj,
|
---|
738 | ViewCellContainer &touchedViewCells,
|
---|
739 | float &renderCost);
|
---|
740 |
|
---|
741 | void SubtractObjectContribution(KdLeaf *leaf,
|
---|
742 | Intersectable *obj,
|
---|
743 | ViewCellContainer &touchedViewCells,
|
---|
744 | float &renderCost);
|
---|
745 |
|
---|
746 | void PrepareConstruction(SplitQueue &tQueue,
|
---|
747 | const VssRayContainer &sampleRays,
|
---|
748 | const ObjectContainer &objects,
|
---|
749 | RayInfoContainer &rays);
|
---|
750 |
|
---|
751 |
|
---|
752 | protected:
|
---|
753 |
|
---|
754 | /// pointer to the hierarchy of view cells
|
---|
755 | ViewCellsTree *mViewCellsTree;
|
---|
756 |
|
---|
757 | /// pointer to the view space partition
|
---|
758 | /// note: should be handled over the hierarchy manager
|
---|
759 | VspTree *mVspTree;
|
---|
760 |
|
---|
761 | /// pointer to the hierarchy manager
|
---|
762 | HierarchyManager *mHierarchyManager;
|
---|
763 |
|
---|
764 | /// The view cells manager
|
---|
765 | ViewCellsManager *mViewCellsManager;
|
---|
766 |
|
---|
767 | /// candidates for placing split planes during cost heuristics
|
---|
768 | vector<SortableEntry> *mSubdivisionCandidates;
|
---|
769 |
|
---|
770 | /// Pointer to the root of the tree
|
---|
771 | KdNode *mRoot;
|
---|
772 |
|
---|
773 | /// Statistics for the object space partition
|
---|
774 | OspTreeStatistics mOspStats;
|
---|
775 |
|
---|
776 | /// box around the whole view domain
|
---|
777 | AxisAlignedBox3 mBoundingBox;
|
---|
778 |
|
---|
779 |
|
---|
780 | //////////////////////////////
|
---|
781 | //-- local termination
|
---|
782 |
|
---|
783 | /// maximal possible depth
|
---|
784 | int mTermMaxDepth;
|
---|
785 | /// mininum probability
|
---|
786 | float mTermMinProbability;
|
---|
787 | /// minimal number of objects
|
---|
788 | int mTermMinObjects;
|
---|
789 | /// maximal contribution per ray
|
---|
790 | float mTermMaxRayContribution;
|
---|
791 | /// maximal acceptable cost ratio
|
---|
792 | float mTermMaxCostRatio;
|
---|
793 | /// tolerance value indicating how often the max cost ratio can be failed
|
---|
794 | int mTermMissTolerance;
|
---|
795 |
|
---|
796 |
|
---|
797 | ////////////////////////
|
---|
798 | //-- global criteria
|
---|
799 |
|
---|
800 | float mTermMinGlobalCostRatio;
|
---|
801 | int mTermGlobalCostMissTolerance;
|
---|
802 | int mGlobalCostMisses;
|
---|
803 |
|
---|
804 | /// maximal number of view cells
|
---|
805 | int mTermMaxLeaves;
|
---|
806 | /// maximal tree memory
|
---|
807 | float mMaxMemory;
|
---|
808 | /// the tree is out of memory
|
---|
809 | bool mOutOfMemory;
|
---|
810 |
|
---|
811 |
|
---|
812 | ///////////////////////////////////////////
|
---|
813 | //-- split heuristics based parameters
|
---|
814 |
|
---|
815 | bool mUseCostHeuristics;
|
---|
816 | /// balancing factor for PVS criterium
|
---|
817 | float mCtDivCi;
|
---|
818 | /// if only driving axis should be used for split
|
---|
819 | bool mOnlyDrivingAxis;
|
---|
820 | /// represents min and max band for sweep
|
---|
821 | float mSplitBorder;
|
---|
822 |
|
---|
823 | ////////////////////////////////////////////////
|
---|
824 |
|
---|
825 | /// current time stamp (used for keeping split history)
|
---|
826 | int mTimeStamp;
|
---|
827 | // if rays should be stored in leaves
|
---|
828 | bool mStoreRays;
|
---|
829 | /// epsilon for geometric comparisons
|
---|
830 | float mEpsilon;
|
---|
831 | /// subdivision stats output file
|
---|
832 | ofstream mSubdivisionStats;
|
---|
833 | /// keeps track of cost during subdivision
|
---|
834 | float mTotalCost;
|
---|
835 | /// keeps track of overall pvs size during subdivision
|
---|
836 | int mTotalPvsSize;
|
---|
837 | /// number of currenly generated view cells
|
---|
838 | int mCreatedLeaves;
|
---|
839 |
|
---|
840 | /// weight between render cost decrease and node render cost
|
---|
841 | float mRenderCostDecreaseWeight;
|
---|
842 |
|
---|
843 | /// stores the kd node intersectables used for pvs
|
---|
844 | KdIntersectableMap mKdIntersectables;
|
---|
845 |
|
---|
846 | private:
|
---|
847 |
|
---|
848 | bool mCopyFromKdTree;
|
---|
849 | };
|
---|
850 |
|
---|
851 |
|
---|
852 | }
|
---|
853 |
|
---|
854 | #endif
|
---|