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