1 | #ifndef _VspBspTree_H__
|
---|
2 | #define _VspBspTree_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 "ViewCellBsp.h"
|
---|
12 |
|
---|
13 |
|
---|
14 |
|
---|
15 | namespace GtpVisibilityPreprocessor {
|
---|
16 |
|
---|
17 | class ViewCellLeaf;
|
---|
18 | //class BspViewCell;
|
---|
19 | class Plane3;
|
---|
20 | class VspBspTree;
|
---|
21 | class BspInterior;
|
---|
22 | class BspNode;
|
---|
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 |
|
---|
32 | /**
|
---|
33 | This is a view space partitioning specialised BSPtree.
|
---|
34 | There are no polygon splits, but we split the sample rays.
|
---|
35 | The candidates for the next split plane are evaluated only
|
---|
36 | by checking the sampled visibility information.
|
---|
37 | The polygons are employed merely as candidates for the next split planes.
|
---|
38 | */
|
---|
39 | class VspBspTree
|
---|
40 | {
|
---|
41 | friend class ViewCellsParseHandlers;
|
---|
42 | friend class VspBspViewCellsManager;
|
---|
43 | public:
|
---|
44 |
|
---|
45 | /** Additional data which is passed down the BSP tree during traversal.
|
---|
46 | */
|
---|
47 | class VspBspTraversalData
|
---|
48 | {
|
---|
49 | public:
|
---|
50 | /// the current node
|
---|
51 | BspNode *mNode;
|
---|
52 | /// polygonal data for splitting
|
---|
53 | PolygonContainer *mPolygons;
|
---|
54 | /// current depth
|
---|
55 | int mDepth;
|
---|
56 | /// rays piercing this node
|
---|
57 | RayInfoContainer *mRays;
|
---|
58 | /// the probability that this node contains view point
|
---|
59 | float mProbability;
|
---|
60 | /// geometry of node as induced by planes
|
---|
61 | BspNodeGeometry *mGeometry;
|
---|
62 | /// pvs size
|
---|
63 | int mPvs;
|
---|
64 | /// how often this branch has missed the max-cost ratio
|
---|
65 | int mMaxCostMisses;
|
---|
66 | /// if this node is a kd-node (i.e., boundaries are axis aligned
|
---|
67 | bool mIsKdNode;
|
---|
68 | // current axis
|
---|
69 | int mAxis;
|
---|
70 | // current priority
|
---|
71 | float mPriority;
|
---|
72 |
|
---|
73 |
|
---|
74 | /** Returns average ray contribution.
|
---|
75 | */
|
---|
76 | float GetAvgRayContribution() const
|
---|
77 | {
|
---|
78 | return (float)mPvs / ((float)mRays->size() + Limits::Small);
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | VspBspTraversalData():
|
---|
83 | mNode(NULL),
|
---|
84 | mPolygons(NULL),
|
---|
85 | mDepth(0),
|
---|
86 | mRays(NULL),
|
---|
87 | mPvs(0),
|
---|
88 | mProbability(0.0),
|
---|
89 | mGeometry(NULL),
|
---|
90 | mMaxCostMisses(0),
|
---|
91 | mIsKdNode(false),
|
---|
92 | mPriority(0),
|
---|
93 | mAxis(0)
|
---|
94 | {}
|
---|
95 |
|
---|
96 | VspBspTraversalData(BspNode *node,
|
---|
97 | PolygonContainer *polys,
|
---|
98 | const int depth,
|
---|
99 | RayInfoContainer *rays,
|
---|
100 | const int pvs,
|
---|
101 | const float p,
|
---|
102 | BspNodeGeometry *geom):
|
---|
103 | mNode(node),
|
---|
104 | mPolygons(polys),
|
---|
105 | mDepth(depth),
|
---|
106 | mRays(rays),
|
---|
107 | mPvs(pvs),
|
---|
108 | mProbability(p),
|
---|
109 | mGeometry(geom),
|
---|
110 | mMaxCostMisses(0),
|
---|
111 | mIsKdNode(false),
|
---|
112 | mPriority(0),
|
---|
113 | mAxis(0)
|
---|
114 | {}
|
---|
115 |
|
---|
116 | VspBspTraversalData(PolygonContainer *polys,
|
---|
117 | const int depth,
|
---|
118 | RayInfoContainer *rays,
|
---|
119 | BspNodeGeometry *geom):
|
---|
120 | mNode(NULL),
|
---|
121 | mPolygons(polys),
|
---|
122 | mDepth(depth),
|
---|
123 | mRays(rays),
|
---|
124 | mPvs(0),
|
---|
125 | mProbability(0),
|
---|
126 | mGeometry(geom),
|
---|
127 | mMaxCostMisses(0),
|
---|
128 | mIsKdNode(false),
|
---|
129 | mAxis(0)
|
---|
130 | {}
|
---|
131 |
|
---|
132 | /** Returns priority of the traversal data.
|
---|
133 | */
|
---|
134 | float GetCost() const
|
---|
135 | {
|
---|
136 | //cout << mPriority << endl;
|
---|
137 | return mPriority;
|
---|
138 | }
|
---|
139 |
|
---|
140 | // deletes contents and sets them to NULL
|
---|
141 | void Clear()
|
---|
142 | {
|
---|
143 | DEL_PTR(mPolygons);
|
---|
144 | DEL_PTR(mRays);
|
---|
145 | DEL_PTR(mGeometry);
|
---|
146 | }
|
---|
147 |
|
---|
148 | friend bool operator<(const VspBspTraversalData &a, const VspBspTraversalData &b)
|
---|
149 | {
|
---|
150 | return a.GetCost() < b.GetCost();
|
---|
151 | }
|
---|
152 | };
|
---|
153 |
|
---|
154 |
|
---|
155 | typedef std::priority_queue<VspBspTraversalData> VspBspTraversalQueue;
|
---|
156 |
|
---|
157 |
|
---|
158 | class VspBspSubdivisionCandidate
|
---|
159 | {
|
---|
160 | public:
|
---|
161 |
|
---|
162 | VspBspSubdivisionCandidate(): mPriority(0), mRenderCostDecr(0)
|
---|
163 | {};
|
---|
164 |
|
---|
165 | VspBspSubdivisionCandidate(const Plane3 &plane, const VspBspTraversalData &tData):
|
---|
166 | mSplitPlane(plane), mParentData(tData), mPriority(0), mRenderCostDecr(0)
|
---|
167 | {}
|
---|
168 |
|
---|
169 | /** Returns cost of the traversal data.
|
---|
170 | */
|
---|
171 | float GetPriority() const
|
---|
172 | {
|
---|
173 | #if 1
|
---|
174 | return mPriority;
|
---|
175 | #else
|
---|
176 | return (float) (-mDepth); // for kd tree
|
---|
177 | #endif
|
---|
178 | }
|
---|
179 |
|
---|
180 | /// the current split plane
|
---|
181 | Plane3 mSplitPlane;
|
---|
182 | /// split axis of this plane (0, 1, 2, or 3 if non-axis-aligned)
|
---|
183 | int mSplitAxis;
|
---|
184 | /// the number of misses of max cost ratio until this split
|
---|
185 | int mMaxCostMisses;
|
---|
186 |
|
---|
187 | /// parent data
|
---|
188 | VspBspTraversalData mParentData;
|
---|
189 | /// prioriry of this split
|
---|
190 | float mPriority;
|
---|
191 |
|
---|
192 | float mRenderCostDecr;
|
---|
193 |
|
---|
194 |
|
---|
195 | friend bool operator<(const VspBspSubdivisionCandidate &a, const VspBspSubdivisionCandidate &b)
|
---|
196 | {
|
---|
197 | return a.GetPriority() < b.GetPriority();
|
---|
198 | }
|
---|
199 | };
|
---|
200 |
|
---|
201 | typedef std::priority_queue<VspBspSubdivisionCandidate> VspBspSplitQueue;
|
---|
202 |
|
---|
203 | /** Default constructor creating an empty tree.
|
---|
204 | */
|
---|
205 | VspBspTree();
|
---|
206 |
|
---|
207 | /** Default destructor.
|
---|
208 | */
|
---|
209 | ~VspBspTree();
|
---|
210 |
|
---|
211 | /** Returns BSP Tree statistics.
|
---|
212 | */
|
---|
213 | const BspTreeStatistics &GetStatistics() const;
|
---|
214 |
|
---|
215 |
|
---|
216 | /** Constructs the tree from a given set of rays.
|
---|
217 | @param sampleRays the set of sample rays the construction is based on
|
---|
218 | @param forcedBoundingBox overwrites the view space box
|
---|
219 | */
|
---|
220 | void Construct(const VssRayContainer &sampleRays,
|
---|
221 | AxisAlignedBox3 *forcedBoundingBox);
|
---|
222 |
|
---|
223 | /** Returns list of BSP leaves with pvs smaller than
|
---|
224 | a certain threshold.
|
---|
225 | @param onlyUnmailed if only the unmailed leaves should be considered
|
---|
226 | @param maxPvs the maximal pvs of a leaf to be added (-1 means unlimited)
|
---|
227 | */
|
---|
228 | void CollectLeaves(vector<BspLeaf *> &leaves,
|
---|
229 | const bool onlyUnmailed = false,
|
---|
230 | const int maxPvs = -1) const;
|
---|
231 |
|
---|
232 | /** Returns box which bounds the whole tree.
|
---|
233 | */
|
---|
234 | AxisAlignedBox3 GetBoundingBox() const;
|
---|
235 |
|
---|
236 | /** Returns root of BSP tree.
|
---|
237 | */
|
---|
238 | BspNode *GetRoot() const;
|
---|
239 |
|
---|
240 | /** Collects the leaf view cells of the tree
|
---|
241 | @param viewCells returns the view cells
|
---|
242 | */
|
---|
243 | void CollectViewCells(ViewCellContainer &viewCells, bool onlyValid) const;
|
---|
244 |
|
---|
245 | /** A ray is cast possible intersecting the tree.
|
---|
246 | @param the ray that is cast.
|
---|
247 | @returns the number of intersections with objects stored in the tree.
|
---|
248 | */
|
---|
249 | int CastRay(Ray &ray);
|
---|
250 |
|
---|
251 | /// bsp tree construction types
|
---|
252 | enum {FROM_INPUT_VIEW_CELLS, FROM_SCENE_GEOMETRY, FROM_SAMPLES};
|
---|
253 |
|
---|
254 | /** finds neighbouring leaves of this tree node.
|
---|
255 | */
|
---|
256 | int FindNeighbors(BspNode *n,
|
---|
257 | vector<BspLeaf *> &neighbors,
|
---|
258 | const bool onlyUnmailed) const;
|
---|
259 |
|
---|
260 | /** Constructs geometry associated with the half space intersections
|
---|
261 | leading to this node.
|
---|
262 | */
|
---|
263 | void ConstructGeometry(BspNode *n, BspNodeGeometry &geom) const;
|
---|
264 |
|
---|
265 | /** Construct geometry of view cell.
|
---|
266 | */
|
---|
267 | void ConstructGeometry(ViewCell *vc, BspNodeGeometry &geom) const;
|
---|
268 |
|
---|
269 | /** Returns random leaf of BSP tree.
|
---|
270 | @param halfspace defines the halfspace from which the leaf is taken.
|
---|
271 | */
|
---|
272 | BspLeaf *GetRandomLeaf(const Plane3 &halfspace);
|
---|
273 |
|
---|
274 | /** Returns random leaf of BSP tree.
|
---|
275 | @param onlyUnmailed if only unmailed leaves should be returned.
|
---|
276 | */
|
---|
277 | BspLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
|
---|
278 |
|
---|
279 | /** Returns epsilon of this tree.
|
---|
280 | */
|
---|
281 | float GetEpsilon() const;
|
---|
282 |
|
---|
283 | /** Casts line segment into the tree.
|
---|
284 | @param origin the origin of the line segment
|
---|
285 | @param termination the end point of the line segment
|
---|
286 | @returns view cells intersecting the line segment.
|
---|
287 | */
|
---|
288 | int CastLineSegment(const Vector3 &origin,
|
---|
289 | const Vector3 &termination,
|
---|
290 | ViewCellContainer &viewcells);
|
---|
291 |
|
---|
292 |
|
---|
293 | /** Sets pointer to view cells manager.
|
---|
294 | */
|
---|
295 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
296 |
|
---|
297 | /** Returns distance from node 1 to node 2.
|
---|
298 | */
|
---|
299 | int TreeDistance(BspNode *n1, BspNode *n2) const;
|
---|
300 |
|
---|
301 | /** Collapses the tree with respect to the view cell partition.
|
---|
302 | @returns number of collapsed nodes
|
---|
303 | */
|
---|
304 | int CollapseTree();
|
---|
305 |
|
---|
306 | /** Returns view cell the current point is located in.
|
---|
307 | @param point the current view point
|
---|
308 | @param active if currently active view cells should be returned or
|
---|
309 | elementary view cell
|
---|
310 | */
|
---|
311 | ViewCell *GetViewCell(const Vector3 &point, const bool active = false);
|
---|
312 |
|
---|
313 |
|
---|
314 | /** Returns true if this view point is in a valid view space,
|
---|
315 | false otherwise.
|
---|
316 | */
|
---|
317 | bool ViewPointValid(const Vector3 &viewPoint) const;
|
---|
318 |
|
---|
319 | /** Returns view cell corresponding to
|
---|
320 | the invalid view space.
|
---|
321 | */
|
---|
322 | BspViewCell *GetOutOfBoundsCell();
|
---|
323 |
|
---|
324 | /** Writes tree to output stream
|
---|
325 | */
|
---|
326 | bool Export(OUT_STREAM &stream);
|
---|
327 |
|
---|
328 | /** Casts beam, i.e. a 5D frustum of rays, into tree.
|
---|
329 | Tests conservative using the bounding box of the nodes.
|
---|
330 | @returns number of view cells it intersected
|
---|
331 | */
|
---|
332 | int CastBeam(Beam &beam);
|
---|
333 |
|
---|
334 | /** Finds approximate neighbours, i.e., finds correct neighbors
|
---|
335 | in most cases but sometimes more.
|
---|
336 | */
|
---|
337 | int FindApproximateNeighbors(BspNode *n,
|
---|
338 | vector<BspLeaf *> &neighbors,
|
---|
339 | const bool onlyUnmailed) const;
|
---|
340 |
|
---|
341 | /** Checks if tree validity-flags are right
|
---|
342 | with respect to view cell valitiy.
|
---|
343 | If not, marks subtree as invalid.
|
---|
344 | */
|
---|
345 | void ValidateTree();
|
---|
346 |
|
---|
347 | /** Invalid view cells are added to the unbounded space
|
---|
348 | */
|
---|
349 | void CollapseViewCells();
|
---|
350 |
|
---|
351 | /** Collects rays stored in the leaves.
|
---|
352 | */
|
---|
353 | void CollectRays(VssRayContainer &rays);
|
---|
354 |
|
---|
355 | /** Intersects box with the tree and returns the number of intersected boxes.
|
---|
356 | @returns number of view cells found
|
---|
357 | */
|
---|
358 | int ComputeBoxIntersections(const AxisAlignedBox3 &box, ViewCellContainer &viewCells) const;
|
---|
359 |
|
---|
360 | /// pointer to the hierarchy of view cells
|
---|
361 | ViewCellsTree *mViewCellsTree;
|
---|
362 |
|
---|
363 |
|
---|
364 | protected:
|
---|
365 |
|
---|
366 | // --------------------------------------------------------------
|
---|
367 | // For sorting objects
|
---|
368 | // --------------------------------------------------------------
|
---|
369 | struct SortableEntry
|
---|
370 | {
|
---|
371 | enum EType
|
---|
372 | {
|
---|
373 | ERayMin,
|
---|
374 | ERayMax
|
---|
375 | };
|
---|
376 |
|
---|
377 | int type;
|
---|
378 | float value;
|
---|
379 | VssRay *ray;
|
---|
380 |
|
---|
381 | SortableEntry() {}
|
---|
382 | SortableEntry(const int t, const float v, VssRay *r):type(t),
|
---|
383 | value(v), ray(r)
|
---|
384 | {
|
---|
385 | }
|
---|
386 |
|
---|
387 | friend bool operator<(const SortableEntry &a, const SortableEntry &b)
|
---|
388 | {
|
---|
389 | return a.value < b.value;
|
---|
390 | }
|
---|
391 | };
|
---|
392 |
|
---|
393 | void ComputeBoundingBox(const VssRayContainer &sampleRays,
|
---|
394 | AxisAlignedBox3 *forcedBoundingBox);
|
---|
395 |
|
---|
396 | /** faster evaluation of split plane cost for kd axis aligned cells.
|
---|
397 | */
|
---|
398 | float EvalAxisAlignedSplitCost(const VspBspTraversalData &data,
|
---|
399 | const AxisAlignedBox3 &box,
|
---|
400 | const int axis,
|
---|
401 | const float &position,
|
---|
402 | float &pFront,
|
---|
403 | float &pBack) const;
|
---|
404 |
|
---|
405 | /** Evaluates candidate for splitting.
|
---|
406 | */
|
---|
407 | void EvalSubdivisionCandidate(VspBspSubdivisionCandidate &splitData);
|
---|
408 |
|
---|
409 | /** Computes priority of the traversal data and stores it in tData.
|
---|
410 | */
|
---|
411 | void EvalPriority(VspBspTraversalData &tData) const;
|
---|
412 |
|
---|
413 | /** Evaluates render cost decrease of next split.
|
---|
414 | */
|
---|
415 | float EvalRenderCostDecrease(const Plane3 &candidatePlane,
|
---|
416 | const VspBspTraversalData &data,
|
---|
417 | float &normalizedOldRenderCost) const;
|
---|
418 |
|
---|
419 | /** Constructs tree using the split priority queue.
|
---|
420 | */
|
---|
421 | void ConstructWithSplitQueue(const PolygonContainer &polys, RayInfoContainer *rays);
|
---|
422 |
|
---|
423 | /** Collects view cells in the subtree under root.
|
---|
424 | */
|
---|
425 | void CollectViewCells(BspNode *root,
|
---|
426 | bool onlyValid,
|
---|
427 | ViewCellContainer &viewCells,
|
---|
428 | bool onlyUnmailed = false) const;
|
---|
429 |
|
---|
430 | /** Returns view cell corresponding to
|
---|
431 | the invalid view space. If it does not exist, it is created.
|
---|
432 | */
|
---|
433 | BspViewCell *GetOrCreateOutOfBoundsCell();
|
---|
434 |
|
---|
435 | /** Collapses the tree with respect to the view cell partition,
|
---|
436 | i.e. leaves having the same view cell are collapsed.
|
---|
437 | @param node the root of the subtree to be collapsed
|
---|
438 | @param collapsed returns the number of collapsed nodes
|
---|
439 | @returns node of type leaf if the node could be collapsed,
|
---|
440 | this node otherwise
|
---|
441 | */
|
---|
442 | BspNode *CollapseTree(BspNode *node, int &collapsed);
|
---|
443 |
|
---|
444 | /** Helper function revalidating the view cell leaf list after merge.
|
---|
445 | */
|
---|
446 | void RepairViewCellsLeafLists();
|
---|
447 |
|
---|
448 | /** Evaluates tree stats in the BSP tree leafs.
|
---|
449 | */
|
---|
450 | void EvaluateLeafStats(const VspBspTraversalData &data);
|
---|
451 |
|
---|
452 | /** Subdivides node with respect to the traversal data.
|
---|
453 | @param tStack current traversal stack
|
---|
454 | @param tData traversal data also holding node to be subdivided
|
---|
455 | @returns new root of the subtree
|
---|
456 | */
|
---|
457 | BspNode *Subdivide(VspBspTraversalQueue &tStack,
|
---|
458 | VspBspTraversalData &tData);
|
---|
459 |
|
---|
460 | /** Subdivides node using a best split priority queue.
|
---|
461 | @param tQueue the best split priority queue
|
---|
462 | @param splitCandidate the candidate for the next split
|
---|
463 | @returns new root of the subtree
|
---|
464 | */
|
---|
465 | BspNode *Subdivide(VspBspSplitQueue &tQueue,
|
---|
466 | VspBspSubdivisionCandidate &splitCandidate);
|
---|
467 |
|
---|
468 | /** Constructs the tree from the given traversal data.
|
---|
469 | @param polys stores set of polygons on which subdivision may be based
|
---|
470 | @param rays stores set of rays on which subdivision may be based
|
---|
471 | */
|
---|
472 | void Construct(const PolygonContainer &polys, RayInfoContainer *rays);
|
---|
473 |
|
---|
474 | /** Selects the best possible splitting plane.
|
---|
475 | @param plane returns the split plane
|
---|
476 | @param leaf the leaf to be split
|
---|
477 | @param data the traversal data holding the polygons and rays which the split decision is based
|
---|
478 | @param frontData the front node traversal data (which may be updated to avoid repcomputations
|
---|
479 | @param backData the front node traversal data (which may be updated to avoid repcomputations
|
---|
480 | @param splitAxis 0 - 2 if axis aligned split, 3 if polygon-aligned split
|
---|
481 |
|
---|
482 | @note the polygons can be reordered in the process
|
---|
483 |
|
---|
484 | @returns true if the cost of the split is under maxCostRatio
|
---|
485 |
|
---|
486 | */
|
---|
487 | bool SelectPlane(Plane3 &plane,
|
---|
488 | BspLeaf *leaf,
|
---|
489 | VspBspTraversalData &data,
|
---|
490 | VspBspTraversalData &frontData,
|
---|
491 | VspBspTraversalData &backData,
|
---|
492 | int &splitAxis);
|
---|
493 |
|
---|
494 | /** Strategies where the effect of the split plane is tested
|
---|
495 | on all input rays.
|
---|
496 |
|
---|
497 | @returns the cost of the candidate split plane
|
---|
498 | */
|
---|
499 | float EvalSplitPlaneCost(const Plane3 &candidatePlane,
|
---|
500 | const VspBspTraversalData &data,
|
---|
501 | BspNodeGeometry &geomFront,
|
---|
502 | BspNodeGeometry &geomBack,
|
---|
503 | float &pFront,
|
---|
504 | float &pBack) const;
|
---|
505 |
|
---|
506 | /** Subdivides leaf.
|
---|
507 |
|
---|
508 | @param tData data object holding, e.g., a pointer to the leaf
|
---|
509 | @param frontData returns the data (e.g., pointer to the leaf) in front of the split plane
|
---|
510 | @param backData returns the data (e.g., pointer to the leaf) in the back of the split plane
|
---|
511 |
|
---|
512 | @param rays the polygons to be filtered
|
---|
513 | @param frontRays returns the polygons in front of the split plane
|
---|
514 | @param coincident returns the polygons which are coincident to the plane and thus discarded
|
---|
515 | for traversal
|
---|
516 |
|
---|
517 | @returns the root of the subdivision
|
---|
518 | */
|
---|
519 |
|
---|
520 | BspInterior *SubdivideNode(const Plane3 &splitPlane,
|
---|
521 | VspBspTraversalData &tData,
|
---|
522 | VspBspTraversalData &frontData,
|
---|
523 | VspBspTraversalData &backData,
|
---|
524 | PolygonContainer &coincident);
|
---|
525 |
|
---|
526 | /** Extracts the meshes of the objects and adds them to polygons.
|
---|
527 | Adds object aabb to the aabb of the tree.
|
---|
528 | @param maxPolys the maximal number of objects to be stored as polygons
|
---|
529 | @returns the number of polygons
|
---|
530 | */
|
---|
531 | int AddToPolygonSoup(const ObjectContainer &objects,
|
---|
532 | PolygonContainer &polys,
|
---|
533 | int maxObjects = 0);
|
---|
534 |
|
---|
535 | /** Extracts the meshes of the view cells and and adds them to polygons.
|
---|
536 | Adds view cell aabb to the aabb of the tree.
|
---|
537 | @param maxPolys the maximal number of objects to be stored as polygons
|
---|
538 | @returns the number of polygons
|
---|
539 | */
|
---|
540 | int AddToPolygonSoup(const ViewCellContainer &viewCells,
|
---|
541 | PolygonContainer &polys,
|
---|
542 | int maxObjects = 0);
|
---|
543 |
|
---|
544 | /** Extract polygons of this mesh and add to polygon container.
|
---|
545 | @param mesh the mesh that drives the polygon construction
|
---|
546 | @param parent the parent intersectable this polygon is constructed from
|
---|
547 | @returns number of polygons
|
---|
548 | */
|
---|
549 | int AddMeshToPolygons(Mesh *mesh, PolygonContainer &polys, MeshInstance *parent);
|
---|
550 |
|
---|
551 | /** Selects an axis aligned for the next split.
|
---|
552 | @returns cost for this split
|
---|
553 | */
|
---|
554 | float SelectAxisAlignedPlane(Plane3 &plane,
|
---|
555 | const VspBspTraversalData &tData,
|
---|
556 | int &axis,
|
---|
557 | BspNodeGeometry **frontGeom,
|
---|
558 | BspNodeGeometry **backGeom,
|
---|
559 | float &pFront,
|
---|
560 | float &pBack,
|
---|
561 | const bool useKdSplit);
|
---|
562 |
|
---|
563 | /** Sorts split candidates for cost heuristics using axis aligned splits.
|
---|
564 | @param polys the input for choosing split candidates
|
---|
565 | @param axis the current split axis
|
---|
566 | @param splitCandidates returns sorted list of split candidates
|
---|
567 | */
|
---|
568 | void SortSubdivisionCandidates(const RayInfoContainer &rays,
|
---|
569 | const int axis,
|
---|
570 | float minBand,
|
---|
571 | float maxBand);
|
---|
572 |
|
---|
573 | /** Computes best cost for axis aligned planes.
|
---|
574 | */
|
---|
575 | float BestCostRatioHeuristics(const RayInfoContainer &rays,
|
---|
576 | const AxisAlignedBox3 &box,
|
---|
577 | const int pvsSize,
|
---|
578 | const int axis,
|
---|
579 | float &position);
|
---|
580 |
|
---|
581 | /** Subdivides the rays into front and back rays according to the split plane.
|
---|
582 |
|
---|
583 | @param plane the split plane
|
---|
584 | @param rays contains the rays to be split. The rays are
|
---|
585 | distributed into front and back rays.
|
---|
586 | @param frontRays returns rays on the front side of the plane
|
---|
587 | @param backRays returns rays on the back side of the plane
|
---|
588 |
|
---|
589 | @returns the number of splits
|
---|
590 | */
|
---|
591 | int SplitRays(const Plane3 &plane,
|
---|
592 | RayInfoContainer &rays,
|
---|
593 | RayInfoContainer &frontRays,
|
---|
594 | RayInfoContainer &backRays) const;
|
---|
595 |
|
---|
596 |
|
---|
597 | /** Extracts the split planes representing the space bounded by node n.
|
---|
598 | */
|
---|
599 | void ExtractHalfSpaces(BspNode *n, vector<Plane3> &halfSpaces) const;
|
---|
600 |
|
---|
601 | /** Adds the object to the pvs of the front and back leaf with a given classification.
|
---|
602 |
|
---|
603 | @param obj the object to be added
|
---|
604 | @param cf the ray classification regarding the split plane
|
---|
605 | @param frontPvs returns the PVS of the front partition
|
---|
606 | @param backPvs returns the PVS of the back partition
|
---|
607 |
|
---|
608 | */
|
---|
609 | void AddObjToPvs(Intersectable *obj,
|
---|
610 | const int cf,
|
---|
611 | float &frontPvs,
|
---|
612 | float &backPvs,
|
---|
613 | float &totalPvs) const;
|
---|
614 |
|
---|
615 | /** Computes PVS size induced by the rays.
|
---|
616 | */
|
---|
617 | int ComputePvsSize(const RayInfoContainer &rays) const;
|
---|
618 |
|
---|
619 | /** Returns true if tree can be terminated.
|
---|
620 | */
|
---|
621 | bool LocalTerminationCriteriaMet(const VspBspTraversalData &data) const;
|
---|
622 |
|
---|
623 | /** Returns true if global tree can be terminated.
|
---|
624 | */
|
---|
625 | bool GlobalTerminationCriteriaMet(const VspBspTraversalData &data) const;
|
---|
626 |
|
---|
627 | /** Computes accumulated ray lenght of this rays.
|
---|
628 | */
|
---|
629 | float AccumulatedRayLength(const RayInfoContainer &rays) const;
|
---|
630 |
|
---|
631 | /** Splits polygons with respect to the split plane.
|
---|
632 |
|
---|
633 | @param plane the split plane
|
---|
634 | @param polys the polygons to be split. the polygons are consumed and
|
---|
635 | distributed to the containers frontPolys, backPolys, coincident.
|
---|
636 | @param frontPolys returns the polygons in the front of the split plane
|
---|
637 | @param backPolys returns the polygons in the back of the split plane
|
---|
638 | @param coincident returns the polygons coincident to the split plane
|
---|
639 |
|
---|
640 | @returns the number of splits
|
---|
641 | */
|
---|
642 | int SplitPolygons(const Plane3 &plane,
|
---|
643 | PolygonContainer &polys,
|
---|
644 | PolygonContainer &frontPolys,
|
---|
645 | PolygonContainer &backPolys,
|
---|
646 | PolygonContainer &coincident) const;
|
---|
647 |
|
---|
648 | /** Adds ray sample contributions to the PVS.
|
---|
649 | @param sampleContributions the number contributions of the samples
|
---|
650 | @param contributingSampels the number of contributing rays
|
---|
651 |
|
---|
652 | */
|
---|
653 | void AddToPvs(BspLeaf *leaf,
|
---|
654 | const RayInfoContainer &rays,
|
---|
655 | float &sampleContributions,
|
---|
656 | int &contributingSamples);
|
---|
657 |
|
---|
658 |
|
---|
659 | /** Take 3 ray endpoints, where two are minimum and one a maximum
|
---|
660 | point or the other way round.
|
---|
661 | */
|
---|
662 | Plane3 ChooseCandidatePlane(const RayInfoContainer &rays) const;
|
---|
663 |
|
---|
664 | /** Take plane normal as plane normal and the midpoint of the ray.
|
---|
665 | PROBLEM: does not resemble any point where visibility is
|
---|
666 | likely to change
|
---|
667 | */
|
---|
668 | Plane3 ChooseCandidatePlane2(const RayInfoContainer &rays) const;
|
---|
669 |
|
---|
670 | /** Fit the plane between the two lines so that the plane
|
---|
671 | has equal shortest distance to both lines.
|
---|
672 | */
|
---|
673 | Plane3 ChooseCandidatePlane3(const RayInfoContainer &rays) const;
|
---|
674 |
|
---|
675 | /** Collects candidates for merging.
|
---|
676 | @param leaves the leaves to be merged
|
---|
677 | @returns number of leaves in queue
|
---|
678 | */
|
---|
679 | int CollectMergeCandidates(const vector<BspLeaf *> leaves, vector<MergeCandidate> &candidates);
|
---|
680 |
|
---|
681 | /** Collects candidates for the merge in the merge queue.
|
---|
682 | @returns number of leaves in queue
|
---|
683 | */
|
---|
684 | int CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
|
---|
685 |
|
---|
686 | /** Preprocesses polygons and throws out all polygons which are coincident to
|
---|
687 | the view space box faces (they can be problematic).
|
---|
688 | */
|
---|
689 | void PreprocessPolygons(PolygonContainer &polys);
|
---|
690 |
|
---|
691 | /** Propagates valid flag up the tree.
|
---|
692 | */
|
---|
693 | void PropagateUpValidity(BspNode *node);
|
---|
694 |
|
---|
695 | /** Writes the node to disk
|
---|
696 | @note: should be implemented as visitor.
|
---|
697 | */
|
---|
698 | void ExportNode(BspNode *node, OUT_STREAM &stream);
|
---|
699 |
|
---|
700 | /** Returns estimated memory usage of tree.
|
---|
701 | */
|
---|
702 | float GetMemUsage() const;
|
---|
703 | //float GetMemUsage(const VspBspTraversalQueue &tstack) const;
|
---|
704 |
|
---|
705 |
|
---|
706 | void EvalSubdivisionStats(const VspBspTraversalData &tData,
|
---|
707 | const VspBspTraversalData &tFrontData,
|
---|
708 | const VspBspTraversalData &tBackData
|
---|
709 | );
|
---|
710 |
|
---|
711 | /** Adds stats to subdivision log file.
|
---|
712 | */
|
---|
713 | void AddSubdivisionStats(const int viewCells,
|
---|
714 | const float renderCostDecr,
|
---|
715 | const float splitCandidateCost,
|
---|
716 | const float totalRenderCost,
|
---|
717 | const float avgRenderCost);
|
---|
718 |
|
---|
719 | ///////////////////////////////////////////////////////////
|
---|
720 |
|
---|
721 |
|
---|
722 | protected:
|
---|
723 |
|
---|
724 | /// Pointer to the root of the tree
|
---|
725 | BspNode *mRoot;
|
---|
726 |
|
---|
727 | /// the pointer to the view cells manager
|
---|
728 | ViewCellsManager *mViewCellsManager;
|
---|
729 |
|
---|
730 | /// View cell corresponding to the space outside the valid view space
|
---|
731 | BspViewCell *mOutOfBoundsCell;
|
---|
732 |
|
---|
733 | /// the bsp tree statistics
|
---|
734 | BspTreeStatistics mBspStats;
|
---|
735 |
|
---|
736 | /// sorted split candidates used for sweep-heuristics
|
---|
737 | vector<SortableEntry> *mLocalSubdivisionCandidates;
|
---|
738 |
|
---|
739 | /// box around the whole view domain
|
---|
740 | AxisAlignedBox3 mBox;
|
---|
741 |
|
---|
742 |
|
---|
743 | //-- termination critera
|
---|
744 |
|
---|
745 | /// minimal number of rays before subdivision termination
|
---|
746 | int mTermMinRays;
|
---|
747 | /// maximal possible depth
|
---|
748 | int mTermMaxDepth;
|
---|
749 | /// mininum probability
|
---|
750 | float mTermMinProbability;
|
---|
751 | /// mininum PVS
|
---|
752 | int mTermMinPvs;
|
---|
753 | /// maximal contribution per ray
|
---|
754 | float mTermMaxRayContribution;
|
---|
755 | /// minimal accumulated ray length
|
---|
756 | float mTermMinAccRayLength;
|
---|
757 | /// maximal acceptable cost ratio
|
---|
758 | float mTermMaxCostRatio;
|
---|
759 | /// tolerance value indicating how often the max cost ratio can be failed
|
---|
760 | int mTermMissTolerance;
|
---|
761 |
|
---|
762 |
|
---|
763 | //-- termination criteria for
|
---|
764 | //-- hybrid stategy where only axis aligned split are used until
|
---|
765 | //-- a certain point and then also polygon aligned split are taken
|
---|
766 |
|
---|
767 | /// minimal number of rays where axis aligned split is taken
|
---|
768 | int mTermMinRaysForAxisAligned;
|
---|
769 | /// max ray contribution
|
---|
770 | float mTermMaxRayContriForAxisAligned;
|
---|
771 | /// weight for heuristics evaluation
|
---|
772 | float mAxisAlignedCtDivCi;
|
---|
773 | /// spezifies the split border of the axis aligned split
|
---|
774 | float mAxisAlignedSplitBorder;
|
---|
775 |
|
---|
776 |
|
---|
777 | //-- global terminatino criteria
|
---|
778 |
|
---|
779 | float mTermMinGlobalCostRatio;
|
---|
780 | int mTermGlobalCostMissTolerance;
|
---|
781 | int mGlobalCostMisses;
|
---|
782 |
|
---|
783 | /// maximal number of view cells
|
---|
784 | int mMaxViewCells;
|
---|
785 | /// maximal tree memory
|
---|
786 | float mMaxMemory;
|
---|
787 | /// the tree is out of memory
|
---|
788 | bool mOutOfMemory;
|
---|
789 |
|
---|
790 |
|
---|
791 | /// number of candidates evaluated for the next split plane
|
---|
792 | int mMaxPolyCandidates;
|
---|
793 | /// number of candidates for split planes evaluated using the rays
|
---|
794 | int mMaxRayCandidates;
|
---|
795 |
|
---|
796 |
|
---|
797 |
|
---|
798 | //-- axis aligned split criteria
|
---|
799 |
|
---|
800 | /// if only driving axis should be used for choosing the axis-aligned split
|
---|
801 | bool mOnlyDrivingAxis;
|
---|
802 | /// if heuristics should be used to place the split plane of an axis-aligned split
|
---|
803 | bool mUseCostHeuristics;
|
---|
804 | /// if driving axis should taken if max cost is exceeded for
|
---|
805 | /// all evaluated axis aligned split plane candidates
|
---|
806 | bool mUseDrivingAxisIfMaxCostViolated;
|
---|
807 | /// minimal relative position where the split axis can be placed
|
---|
808 | float mMinBand;
|
---|
809 | /// maximal relative position where the split axis can be placed
|
---|
810 | float mMaxBand;
|
---|
811 | /// balancing factor for PVS criterium
|
---|
812 | float mCtDivCi;
|
---|
813 | /// if random split axis should be used
|
---|
814 | bool mUseRandomAxis;
|
---|
815 | /// if vsp bsp tree should simulate octree
|
---|
816 | bool mCirculatingAxis;
|
---|
817 |
|
---|
818 |
|
---|
819 |
|
---|
820 | /// priority queue strategy
|
---|
821 | enum {BREATH_FIRST, DEPTH_FIRST, COST_BASED};
|
---|
822 | /// if we should use breath first priority for the splits
|
---|
823 | int mNodePriorityQueueType;
|
---|
824 | /// if split cost queue should be used to compute next best split
|
---|
825 | bool mUseSplitCostQueue;
|
---|
826 |
|
---|
827 |
|
---|
828 |
|
---|
829 | /// Strategies for choosing next split plane.
|
---|
830 | enum {NO_STRATEGY = 0,
|
---|
831 | RANDOM_POLYGON = 1,
|
---|
832 | AXIS_ALIGNED = 2,
|
---|
833 | LEAST_RAY_SPLITS = 256,
|
---|
834 | BALANCED_RAYS = 512,
|
---|
835 | PVS = 1024
|
---|
836 | };
|
---|
837 |
|
---|
838 | /// strategy to get the best split plane
|
---|
839 | int mSplitPlaneStrategy;
|
---|
840 |
|
---|
841 | //-- factors guiding the split plane heuristics
|
---|
842 |
|
---|
843 | float mLeastRaySplitsFactor;
|
---|
844 | float mBalancedRaysFactor;
|
---|
845 | float mPvsFactor;
|
---|
846 |
|
---|
847 |
|
---|
848 | /// if area or volume should be used for PVS heuristics
|
---|
849 | bool mUseAreaForPvs;
|
---|
850 | /// tolerance for polygon split
|
---|
851 | float mEpsilon;
|
---|
852 | /// maximal number of test rays used to evaluate candidate split plane
|
---|
853 | int mMaxTests;
|
---|
854 | /// normalizes different bsp split plane criteria
|
---|
855 | float mCostNormalizer;
|
---|
856 | // if rays should be stored in leaves
|
---|
857 | bool mStoreRays;
|
---|
858 | /// weight between render cost (expected value) and variance
|
---|
859 | float mRenderCostWeight;
|
---|
860 | /// weight between render cost decrease and node render cost
|
---|
861 | float mRenderCostDecreaseWeight;
|
---|
862 |
|
---|
863 | //-- subdivision statistics
|
---|
864 |
|
---|
865 | /// subdivision stats output file
|
---|
866 | ofstream mSubdivisionStats;
|
---|
867 | float mTotalCost;
|
---|
868 | int mTotalPvsSize;
|
---|
869 |
|
---|
870 |
|
---|
871 | /// use polygon split whenever there are polys left
|
---|
872 | bool mUsePolygonSplitIfAvailable;
|
---|
873 | /// current time stamp (used for keeping split history)
|
---|
874 | int mTimeStamp;
|
---|
875 | /// number of currenly generated view cells
|
---|
876 | int mCreatedViewCells;
|
---|
877 |
|
---|
878 |
|
---|
879 | private:
|
---|
880 |
|
---|
881 | /// Generates unique ids for PVS criterium
|
---|
882 | static void GenerateUniqueIdsForPvs();
|
---|
883 |
|
---|
884 | //-- unique ids for PVS criterium
|
---|
885 | static int sFrontId;
|
---|
886 | static int sBackId;
|
---|
887 | static int sFrontAndBackId;
|
---|
888 | };
|
---|
889 |
|
---|
890 | }
|
---|
891 |
|
---|
892 |
|
---|
893 | #endif
|
---|