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