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