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