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 | HierarchyManager *hm = sOspTree->mHierarchyManager;
|
---|
283 | if (hm->ConsiderMemory())
|
---|
284 | {
|
---|
285 | const float rc = hm->GetHierarchyStats().mTotalCost - mRenderCostDecrease;
|
---|
286 | const float mc = hm->GetHierarchyStats().mMemory +
|
---|
287 | (float)mPvsEntriesIncr * ObjectPvs::GetEntrySizeByte();
|
---|
288 |
|
---|
289 | return - (rc * mc);
|
---|
290 | }
|
---|
291 | else
|
---|
292 | {
|
---|
293 | return mPriority;
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 | OspSubdivisionCandidate(const AxisAlignedPlane &plane, const OspTraversalData &tData):
|
---|
298 | mSplitPlane(plane), mParentData(tData)
|
---|
299 | {}
|
---|
300 | };
|
---|
301 |
|
---|
302 | /** Struct for traversing line segment.
|
---|
303 | */
|
---|
304 | struct LineTraversalData
|
---|
305 | {
|
---|
306 | KdNode *mNode;
|
---|
307 | Vector3 mExitPoint;
|
---|
308 |
|
---|
309 | float mMaxT;
|
---|
310 |
|
---|
311 | LineTraversalData () {}
|
---|
312 | LineTraversalData (KdNode *n, const Vector3 &p, const float maxt):
|
---|
313 | mNode(n), mExitPoint(p), mMaxT(maxt) {}
|
---|
314 | };
|
---|
315 |
|
---|
316 | /** Default constructor creating an empty tree.
|
---|
317 | */
|
---|
318 | OspTree();
|
---|
319 |
|
---|
320 | /** Copies tree from a kd tree.
|
---|
321 | */
|
---|
322 | OspTree(const KdTree &kdTree);
|
---|
323 |
|
---|
324 | /** Default destructor.
|
---|
325 | */
|
---|
326 | ~OspTree();
|
---|
327 |
|
---|
328 | /** Returns tree statistics.
|
---|
329 | */
|
---|
330 | const OspTreeStatistics &GetStatistics() const;
|
---|
331 |
|
---|
332 | /** Returns bounding box of the specified node.
|
---|
333 | */
|
---|
334 | AxisAlignedBox3 GetBoundingBox(KdNode *node) const;
|
---|
335 |
|
---|
336 | /** Returns list of leaves with pvs smaller than
|
---|
337 | a certain threshold.
|
---|
338 | @param onlyUnmailed if only the unmailed leaves should be considered
|
---|
339 | @param maxPvs the maximal pvs of a leaf to be added (-1 means unlimited)
|
---|
340 | */
|
---|
341 |
|
---|
342 | void CollectLeaves(vector<KdLeaf *> &leaves) const;
|
---|
343 |
|
---|
344 | /** Returns bounding box of the whole tree (= bbox of root node)
|
---|
345 | */
|
---|
346 | AxisAlignedBox3 GetBoundingBox()const;
|
---|
347 |
|
---|
348 | /** Returns root of the view space partitioning tree.
|
---|
349 | */
|
---|
350 | KdNode *GetRoot() const;
|
---|
351 |
|
---|
352 | /** Collects the leaf view cells of the tree
|
---|
353 | @param viewCells returns the view cells
|
---|
354 | */
|
---|
355 | void CollectViewCells(ViewCellContainer &viewCells, bool onlyValid) const;
|
---|
356 |
|
---|
357 | /** A ray is cast possible intersecting the tree.
|
---|
358 | @param the ray that is cast.
|
---|
359 | @returns the number of intersections with objects stored in the tree.
|
---|
360 | */
|
---|
361 | int CastRay(Ray &ray);
|
---|
362 |
|
---|
363 | /** finds neighbouring leaves of this tree node.
|
---|
364 | */
|
---|
365 | int FindNeighbors(KdLeaf *n,
|
---|
366 | vector<VspLeaf *> &neighbors,
|
---|
367 | const bool onlyUnmailed) const;
|
---|
368 |
|
---|
369 | /** Returns random leaf of BSP tree.
|
---|
370 | @param halfspace defines the halfspace from which the leaf is taken.
|
---|
371 | */
|
---|
372 | KdLeaf *GetRandomLeaf(const Plane3 &halfspace);
|
---|
373 |
|
---|
374 | /** Returns random leaf of BSP tree.
|
---|
375 | @param onlyUnmailed if only unmailed leaves should be returned.
|
---|
376 | */
|
---|
377 | KdLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
|
---|
378 |
|
---|
379 | /** Returns epsilon of this tree.
|
---|
380 | */
|
---|
381 | float GetEpsilon() const;
|
---|
382 |
|
---|
383 | /** Casts line segment into the tree.
|
---|
384 | @param origin the origin of the line segment
|
---|
385 | @param termination the end point of the line segment
|
---|
386 | @returns view cells intersecting the line segment.
|
---|
387 | */
|
---|
388 | int CastLineSegment(const Vector3 &origin,
|
---|
389 | const Vector3 &termination,
|
---|
390 | ViewCellContainer &viewcells);
|
---|
391 |
|
---|
392 | /** Sets pointer to view cells manager.
|
---|
393 | */
|
---|
394 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
395 |
|
---|
396 | /** Writes tree to output stream
|
---|
397 | */
|
---|
398 | bool Export(OUT_STREAM &stream);
|
---|
399 |
|
---|
400 | /** Returns or creates a new intersectable for use in a kd based pvs.
|
---|
401 | The OspTree is responsible for destruction of the intersectable.
|
---|
402 | */
|
---|
403 | KdIntersectable *GetOrCreateKdIntersectable(KdNode *node);
|
---|
404 |
|
---|
405 | /** Collects rays stored in the leaves.
|
---|
406 | */
|
---|
407 | void CollectRays(VssRayContainer &rays);
|
---|
408 |
|
---|
409 | /** Intersects box with the tree and returns the number of intersected boxes.
|
---|
410 | @returns number of view cells found
|
---|
411 | */
|
---|
412 | int ComputeBoxIntersections(const AxisAlignedBox3 &box,
|
---|
413 | ViewCellContainer &viewCells) const;
|
---|
414 |
|
---|
415 |
|
---|
416 | /** Returns kd leaf the point pt lies in, starting from root.
|
---|
417 | */
|
---|
418 | KdLeaf *GetLeaf(const Vector3 &pt, KdNode *root = NULL) const;
|
---|
419 |
|
---|
420 |
|
---|
421 | ViewCellsTree *GetViewCellsTree() const { return mViewCellsTree; }
|
---|
422 |
|
---|
423 | void SetViewCellsTree(ViewCellsTree *vt) { mViewCellsTree = vt; }
|
---|
424 |
|
---|
425 | float EvalRenderCost(const VssRayContainer &myrays);
|
---|
426 | float EvalLeafCost(const OspTraversalData &tData);
|
---|
427 |
|
---|
428 | /** Adds this objects to the kd leaf objects.
|
---|
429 | @warning: Can corrupt the tree
|
---|
430 | */
|
---|
431 | void InsertObjects(KdNode *node, const ObjectContainer &objects);
|
---|
432 |
|
---|
433 | /** Add the leaf to the pvs of the view cell.
|
---|
434 | */
|
---|
435 | bool AddLeafToPvs(
|
---|
436 | KdLeaf *leaf,
|
---|
437 | ViewCell *vc,
|
---|
438 | const float pdf,
|
---|
439 | float &contribution);
|
---|
440 |
|
---|
441 | protected:
|
---|
442 |
|
---|
443 | // --------------------------------------------------------------
|
---|
444 | // For sorting objects
|
---|
445 | // --------------------------------------------------------------
|
---|
446 | struct SortableEntry
|
---|
447 | {
|
---|
448 | /** There is a 3th "event" for rays which intersect a
|
---|
449 | box in the middle. These "events" don't induce a change in
|
---|
450 | pvs size, but may induce a change in view cell volume.
|
---|
451 | */
|
---|
452 | enum EType
|
---|
453 | {
|
---|
454 | BOX_MIN,
|
---|
455 | BOX_MAX,
|
---|
456 | BOX_INTERSECT
|
---|
457 | };
|
---|
458 |
|
---|
459 | int mType;
|
---|
460 | //int mPvs;
|
---|
461 | float mPos;
|
---|
462 | VssRay *mRay;
|
---|
463 |
|
---|
464 | Intersectable *mObject;
|
---|
465 |
|
---|
466 | SortableEntry() {}
|
---|
467 |
|
---|
468 | SortableEntry(const int type,
|
---|
469 | //const float pvs,
|
---|
470 | const float pos,
|
---|
471 | Intersectable *obj,
|
---|
472 | VssRay *ray):
|
---|
473 | mType(type),
|
---|
474 | //mPvs(pvs),
|
---|
475 | mPos(pos),
|
---|
476 | mObject(obj),
|
---|
477 | mRay(ray)
|
---|
478 | {}
|
---|
479 |
|
---|
480 | bool operator<(const SortableEntry &b) const
|
---|
481 | {
|
---|
482 | return mPos < b.mPos;
|
---|
483 | }
|
---|
484 | };
|
---|
485 |
|
---|
486 |
|
---|
487 | /** faster evaluation of split plane cost for kd axis aligned cells.
|
---|
488 | */
|
---|
489 | float EvalLocalSplitCost(const OspTraversalData &data,
|
---|
490 | const AxisAlignedBox3 &box,
|
---|
491 | const int axis,
|
---|
492 | const float &position,
|
---|
493 | float &pFront,
|
---|
494 | float &pBack) const;
|
---|
495 |
|
---|
496 | /** Evaluates candidate for splitting.
|
---|
497 | */
|
---|
498 | void EvalSubdivisionCandidate(OspSubdivisionCandidate &splitData);
|
---|
499 |
|
---|
500 | /** Computes priority of the traversal data and stores it in tData.
|
---|
501 | */
|
---|
502 | void EvalPriority(OspTraversalData &tData) const;
|
---|
503 |
|
---|
504 | /** Evaluates render cost decrease of next split.
|
---|
505 | */
|
---|
506 | float EvalRenderCostDecrease(const AxisAlignedPlane &candidatePlane,
|
---|
507 | const OspTraversalData &data,
|
---|
508 | float &normalizedOldRenderCost) const;
|
---|
509 |
|
---|
510 |
|
---|
511 | /** Collects view cells in the subtree under root.
|
---|
512 | */
|
---|
513 | void CollectViewCells(KdNode *root,
|
---|
514 | bool onlyValid,
|
---|
515 | ViewCellContainer &viewCells,
|
---|
516 | bool onlyUnmailed = false) const;
|
---|
517 |
|
---|
518 | /** Evaluates tree stats in the BSP tree leafs.
|
---|
519 | */
|
---|
520 | void EvaluateLeafStats(const OspTraversalData &data);
|
---|
521 |
|
---|
522 | /** Subdivides node using a best split priority queue.
|
---|
523 | @param tQueue the best split priority queue
|
---|
524 | @param splitCandidate the candidate for the next split
|
---|
525 | @param globalCriteriaMet if the global termination criteria were already met
|
---|
526 | @returns new root of the subtree
|
---|
527 | */
|
---|
528 | KdNode *Subdivide(SplitQueue &tQueue,
|
---|
529 | SubdivisionCandidate *splitCandidate,
|
---|
530 | const bool globalCriteriaMet);
|
---|
531 |
|
---|
532 | /** Subdivides leaf.
|
---|
533 | @param leaf the leaf to be subdivided
|
---|
534 |
|
---|
535 | @param polys the polygons to be split
|
---|
536 | @param frontPolys returns the polygons in front of the split plane
|
---|
537 | @param backPolys returns the polygons in the back of the split plane
|
---|
538 |
|
---|
539 | @param rays the polygons to be filtered
|
---|
540 | @param frontRays returns the polygons in front of the split plane
|
---|
541 | @param backRays returns the polygons in the back of the split plane
|
---|
542 |
|
---|
543 | @returns the root of the subdivision
|
---|
544 | */
|
---|
545 | KdInterior *SubdivideNode(
|
---|
546 | const AxisAlignedPlane &splitPlane,
|
---|
547 | const OspTraversalData &tData,
|
---|
548 | OspTraversalData &frontData,
|
---|
549 | OspTraversalData &backData);
|
---|
550 |
|
---|
551 | void SplitObjects(KdLeaf *leaf,
|
---|
552 | const AxisAlignedPlane & splitPlane,
|
---|
553 | const ObjectContainer &objects,
|
---|
554 | ObjectContainer &front,
|
---|
555 | ObjectContainer &back);
|
---|
556 |
|
---|
557 | /** does some post processing on the objects in the new child leaves.
|
---|
558 | */
|
---|
559 | void ProcessMultipleRefs(KdLeaf *leaf) const;
|
---|
560 |
|
---|
561 | /** Sorts split candidates for cost heuristics using axis aligned splits.
|
---|
562 | @param node the current node
|
---|
563 | @param axis the current split axis
|
---|
564 | */
|
---|
565 | void SortSubdivisionCandidates(const OspTraversalData &data,
|
---|
566 | const int axis,
|
---|
567 | float minBand,
|
---|
568 | float maxBand);
|
---|
569 |
|
---|
570 | /** Computes best cost for axis aligned planes.
|
---|
571 | */
|
---|
572 | float EvalLocalCostHeuristics(const OspTraversalData &tData,
|
---|
573 | const AxisAlignedBox3 &box,
|
---|
574 | const int axis,
|
---|
575 | float &position,
|
---|
576 | int &objectsFront,
|
---|
577 | int &objectsBack);
|
---|
578 |
|
---|
579 | /** Subdivides the rays into front and back rays according to the split plane.
|
---|
580 |
|
---|
581 | @param plane the split plane
|
---|
582 | @param rays contains the rays to be split. The rays are
|
---|
583 | distributed into front and back rays.
|
---|
584 | @param frontRays returns rays on the front side of the plane
|
---|
585 | @param backRays returns rays on the back side of the plane
|
---|
586 |
|
---|
587 | @returns the number of splits
|
---|
588 | */
|
---|
589 | int SplitRays(const AxisAlignedPlane &plane,
|
---|
590 | RayInfoContainer &rays,
|
---|
591 | RayInfoContainer &frontRays,
|
---|
592 | RayInfoContainer &backRays) const;
|
---|
593 |
|
---|
594 | int FilterRays(KdLeaf *leaf, const RayInfoContainer &rays, RayInfoContainer &filteredRays);
|
---|
595 |
|
---|
596 | /** Adds the object to the pvs of the front and back leaf with a given classification.
|
---|
597 |
|
---|
598 | @param obj the object to be added
|
---|
599 | @param cf the ray classification regarding the split plane
|
---|
600 | @param frontPvs returns the PVS of the front partition
|
---|
601 | @param backPvs returns the PVS of the back partition
|
---|
602 |
|
---|
603 | */
|
---|
604 | void UpdateObjPvsContri(Intersectable *obj,
|
---|
605 | const int cf,
|
---|
606 | float &frontPvs,
|
---|
607 | float &backPvs,
|
---|
608 | float &totalPvs) const;
|
---|
609 |
|
---|
610 | /** Returns true if tree can be terminated.
|
---|
611 | */
|
---|
612 | bool LocalTerminationCriteriaMet(const OspTraversalData &data) const;
|
---|
613 |
|
---|
614 | /** Returns true if global tree can be terminated.
|
---|
615 | */
|
---|
616 | bool GlobalTerminationCriteriaMet(const OspTraversalData &data) const;
|
---|
617 |
|
---|
618 | /** Selects an axis aligned for the next split.
|
---|
619 | @returns cost for this split
|
---|
620 | */
|
---|
621 | float SelectSplitPlane(
|
---|
622 | const OspTraversalData &tData,
|
---|
623 | AxisAlignedPlane &plane);
|
---|
624 |
|
---|
625 | /** Propagates valid flag up the tree.
|
---|
626 | */
|
---|
627 | void PropagateUpValidity(KdNode *node);
|
---|
628 |
|
---|
629 | /** Writes the node to disk
|
---|
630 | @note: should be implemented as visitor.
|
---|
631 | */
|
---|
632 | void ExportNode(KdNode *node, OUT_STREAM &stream);
|
---|
633 |
|
---|
634 | /** Returns estimated memory usage of tree.
|
---|
635 | */
|
---|
636 | float GetMemUsage() const;
|
---|
637 |
|
---|
638 | /** Evaluate the contributions of view cell volume of the left and the right view cell.
|
---|
639 | */
|
---|
640 | void EvalRayContribution(
|
---|
641 | KdLeaf *leaf,
|
---|
642 | const VssRay &ray,
|
---|
643 | float &renderCost);
|
---|
644 |
|
---|
645 | void EvalViewCellContribution(
|
---|
646 | KdLeaf *leaf,
|
---|
647 | ViewCell *viewCell,
|
---|
648 | float &renderCost);
|
---|
649 |
|
---|
650 | /** Evaluates the influence on the pvs of the event.
|
---|
651 | @param ve the visibility event
|
---|
652 | @param pvsLeft updates the left pvs
|
---|
653 | @param rightPvs updates the right pvs
|
---|
654 | */
|
---|
655 | void EvalHeuristicsContribution(KdLeaf *leaf,
|
---|
656 | const SortableEntry &ci,
|
---|
657 | float &renderCost,
|
---|
658 | ViewCellContainer &touchedViewCells);
|
---|
659 |
|
---|
660 | /** Prepares objects for the cost heuristics.
|
---|
661 | @returns pvs size of the node
|
---|
662 | */
|
---|
663 | float PrepareHeuristics(
|
---|
664 | const OspTraversalData &tData,
|
---|
665 | ViewCellContainer &touchedViewCells);
|
---|
666 |
|
---|
667 | /** Prepares heuristics for a particular ray.
|
---|
668 | */
|
---|
669 | void PrepareHeuristics(
|
---|
670 | const VssRay &ray,
|
---|
671 | ViewCellContainer &touchedViewCells);
|
---|
672 |
|
---|
673 | /** Prepares construction for vsp and osp trees.
|
---|
674 | */
|
---|
675 | void ComputeBoundingBox(const ObjectContainer &objects);
|
---|
676 |
|
---|
677 | void CollectDirtyCandidates(OspSubdivisionCandidate *sc,
|
---|
678 | vector<SubdivisionCandidate *> &dirtyList,
|
---|
679 | const bool onlyUnmailed);
|
---|
680 |
|
---|
681 | /** Collect view cells which see this kd leaf.
|
---|
682 | */
|
---|
683 | void CollectViewCells(KdLeaf *leaf,
|
---|
684 | ViewCellContainer &viewCells);
|
---|
685 |
|
---|
686 | /** Rays will be clipped to the bounding box.
|
---|
687 | */
|
---|
688 | void PreprocessRays(
|
---|
689 | KdLeaf *root,
|
---|
690 | const VssRayContainer &sampleRays,
|
---|
691 | RayInfoContainer &rays);
|
---|
692 |
|
---|
693 | /** Reads parameters from environment singleton.
|
---|
694 | */
|
---|
695 | void ReadEnvironment();
|
---|
696 |
|
---|
697 | /** Returns true if the specified ray end points is inside the kd leaf.
|
---|
698 | @param isTermination if origin or termination point should be checked
|
---|
699 | */
|
---|
700 | bool EndPointInsideNode(KdLeaf *leaf, VssRay &ray, bool isTermination) const;
|
---|
701 |
|
---|
702 | void AddViewCellVolumeContri(
|
---|
703 | ViewCell *vc,
|
---|
704 | float &frontVol,
|
---|
705 | float &backVol,
|
---|
706 | float &frontAndBackVol) const;
|
---|
707 |
|
---|
708 | /** Classifies and mail view cell with respect to the heuristics contribution.
|
---|
709 | Set view cell mail box according to it's influence on
|
---|
710 | front (0), back (1) and front / back node (2).
|
---|
711 | */
|
---|
712 | void MailViewCell(ViewCell *vc, const int cf) const;
|
---|
713 |
|
---|
714 | int ClassifyRay(VssRay *ray, KdLeaf *leaf, const AxisAlignedPlane &plane) const;
|
---|
715 |
|
---|
716 | void EvalSubdivisionStats(const SubdivisionCandidate &tData);
|
---|
717 |
|
---|
718 | void AddSubdivisionStats(const int viewCells,
|
---|
719 | const float renderCostDecr,
|
---|
720 | const float totalRenderCost);
|
---|
721 |
|
---|
722 | void EvalViewCellsForHeuristics(const VssRay &ray,
|
---|
723 | float &volLeft,
|
---|
724 | float &volRight);
|
---|
725 |
|
---|
726 | float EvalViewCellsVolume(KdLeaf *leaf, const RayInfoContainer &rays) const;
|
---|
727 |
|
---|
728 | int RemoveParentViewCellsPvs(KdLeaf *leaf, const RayInfoContainer &rays) const;
|
---|
729 |
|
---|
730 | int UpdateViewCellsPvs(KdLeaf *leaf, const RayInfoContainer &rays) const;
|
---|
731 |
|
---|
732 | int CheckViewCellsPvs(KdLeaf *leaf, const RayInfoContainer &rays) const;
|
---|
733 |
|
---|
734 | bool AddViewCellToObjectPvs(Intersectable *obj,
|
---|
735 | ViewCell *vc,
|
---|
736 | float &contribution,
|
---|
737 | bool onlyUnmailed) const;
|
---|
738 |
|
---|
739 | int ClassifyRays(const RayInfoContainer &rays,
|
---|
740 | KdLeaf *leaf,
|
---|
741 | const AxisAlignedPlane &plane,
|
---|
742 | RayInfoContainer &frontRays,
|
---|
743 | RayInfoContainer &backRays) const;
|
---|
744 |
|
---|
745 | void CollectTouchedViewCells(const RayInfoContainer &rays,
|
---|
746 | ViewCellContainer &touchedViewCells) const;
|
---|
747 |
|
---|
748 | void AddObjectContribution(KdLeaf *leaf,
|
---|
749 | Intersectable * obj,
|
---|
750 | ViewCellContainer &touchedViewCells,
|
---|
751 | float &renderCost);
|
---|
752 |
|
---|
753 | void SubtractObjectContribution(KdLeaf *leaf,
|
---|
754 | Intersectable * obj,
|
---|
755 | ViewCellContainer &touchedViewCells,
|
---|
756 | float &renderCost);
|
---|
757 |
|
---|
758 | SubdivisionCandidate *PrepareConstruction(const VssRayContainer &sampleRays,
|
---|
759 | const ObjectContainer &objects,
|
---|
760 | RayInfoContainer &rays);
|
---|
761 |
|
---|
762 |
|
---|
763 | protected:
|
---|
764 |
|
---|
765 | /// pointer to the hierarchy of view cells
|
---|
766 | ViewCellsTree *mViewCellsTree;
|
---|
767 |
|
---|
768 | /// pointer to the view space partition
|
---|
769 | /// note: should be handled over the hierarchy manager
|
---|
770 | VspTree *mVspTree;
|
---|
771 |
|
---|
772 | /// pointer to the hierarchy manager
|
---|
773 | HierarchyManager *mHierarchyManager;
|
---|
774 |
|
---|
775 | /// The view cells manager
|
---|
776 | ViewCellsManager *mViewCellsManager;
|
---|
777 |
|
---|
778 | /// candidates for placing split planes during cost heuristics
|
---|
779 | vector<SortableEntry> *mSubdivisionCandidates;
|
---|
780 |
|
---|
781 | /// Pointer to the root of the tree
|
---|
782 | KdNode *mRoot;
|
---|
783 |
|
---|
784 | /// Statistics for the object space partition
|
---|
785 | OspTreeStatistics mOspStats;
|
---|
786 |
|
---|
787 | /// box around the whole view domain
|
---|
788 | AxisAlignedBox3 mBoundingBox;
|
---|
789 |
|
---|
790 |
|
---|
791 | //////////////////////////////
|
---|
792 | //-- local termination
|
---|
793 |
|
---|
794 | /// maximal possible depth
|
---|
795 | int mTermMaxDepth;
|
---|
796 | /// mininum probability
|
---|
797 | float mTermMinProbability;
|
---|
798 | /// minimal number of objects
|
---|
799 | int mTermMinObjects;
|
---|
800 | /// maximal contribution per ray
|
---|
801 | float mTermMaxRayContribution;
|
---|
802 | /// maximal acceptable cost ratio
|
---|
803 | float mTermMaxCostRatio;
|
---|
804 | /// tolerance value indicating how often the max cost ratio can be failed
|
---|
805 | int mTermMissTolerance;
|
---|
806 |
|
---|
807 |
|
---|
808 | ////////////////////////
|
---|
809 | //-- global criteria
|
---|
810 |
|
---|
811 | float mTermMinGlobalCostRatio;
|
---|
812 | int mTermGlobalCostMissTolerance;
|
---|
813 | int mGlobalCostMisses;
|
---|
814 |
|
---|
815 | /// maximal number of view cells
|
---|
816 | int mTermMaxLeaves;
|
---|
817 | /// maximal tree memory
|
---|
818 | float mMaxMemory;
|
---|
819 | /// the tree is out of memory
|
---|
820 | bool mOutOfMemory;
|
---|
821 |
|
---|
822 |
|
---|
823 | ///////////////////////////////////////////
|
---|
824 | //-- split heuristics based parameters
|
---|
825 |
|
---|
826 | bool mUseCostHeuristics;
|
---|
827 | /// balancing factor for PVS criterium
|
---|
828 | float mCtDivCi;
|
---|
829 | /// if only driving axis should be used for split
|
---|
830 | bool mOnlyDrivingAxis;
|
---|
831 | /// represents min and max band for sweep
|
---|
832 | float mSplitBorder;
|
---|
833 |
|
---|
834 | ////////////////////////////////////////////////
|
---|
835 |
|
---|
836 | /// current time stamp (used for keeping split history)
|
---|
837 | int mTimeStamp;
|
---|
838 | // if rays should be stored in leaves
|
---|
839 | bool mStoreRays;
|
---|
840 | /// epsilon for geometric comparisons
|
---|
841 | float mEpsilon;
|
---|
842 | /// subdivision stats output file
|
---|
843 | ofstream mSubdivisionStats;
|
---|
844 | /// keeps track of cost during subdivision
|
---|
845 | float mTotalCost;
|
---|
846 | /// keeps track of overall pvs size during subdivision
|
---|
847 | int mTotalPvsSize;
|
---|
848 | /// number of currenly generated view cells
|
---|
849 | int mCreatedLeaves;
|
---|
850 |
|
---|
851 | /// weight between render cost decrease and node render cost
|
---|
852 | float mRenderCostDecreaseWeight;
|
---|
853 |
|
---|
854 | /// stores the kd node intersectables used for pvs
|
---|
855 | KdIntersectableMap mKdIntersectables;
|
---|
856 |
|
---|
857 | private:
|
---|
858 |
|
---|
859 | bool mCopyFromKdTree;
|
---|
860 | };
|
---|
861 |
|
---|
862 |
|
---|
863 | }
|
---|
864 |
|
---|
865 | #endif
|
---|