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