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