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