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