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