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