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