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