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