[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 |
|
---|
[697] | 347 | /** Intersects box with the tree and returns the number of intersected boxes.
|
---|
| 348 | @returns number of view cells found
|
---|
| 349 | */
|
---|
| 350 | int ComputeBoxIntersections(const AxisAlignedBox3 &box, ViewCellContainer &viewCells) const;
|
---|
[639] | 351 |
|
---|
[697] | 352 | // pointer to the hierarchy of view cells
|
---|
[590] | 353 | ViewCellsTree *mViewCellsTree;
|
---|
[639] | 354 |
|
---|
| 355 |
|
---|
[463] | 356 | protected:
|
---|
| 357 |
|
---|
| 358 | // --------------------------------------------------------------
|
---|
| 359 | // For sorting objects
|
---|
| 360 | // --------------------------------------------------------------
|
---|
| 361 | struct SortableEntry
|
---|
| 362 | {
|
---|
[480] | 363 | enum EType
|
---|
| 364 | {
|
---|
| 365 | ERayMin,
|
---|
| 366 | ERayMax
|
---|
| 367 | };
|
---|
| 368 |
|
---|
[463] | 369 | int type;
|
---|
| 370 | float value;
|
---|
[482] | 371 | VssRay *ray;
|
---|
[480] | 372 |
|
---|
[463] | 373 | SortableEntry() {}
|
---|
[482] | 374 | SortableEntry(const int t, const float v, VssRay *r):type(t),
|
---|
| 375 | value(v), ray(r)
|
---|
[480] | 376 | {
|
---|
| 377 | }
|
---|
[463] | 378 |
|
---|
[480] | 379 | friend bool operator<(const SortableEntry &a, const SortableEntry &b)
|
---|
[463] | 380 | {
|
---|
[480] | 381 | return a.value < b.value;
|
---|
| 382 | }
|
---|
[463] | 383 | };
|
---|
| 384 |
|
---|
[547] | 385 | /** faster evaluation of split plane cost for kd axis aligned cells.
|
---|
| 386 | */
|
---|
[542] | 387 | float EvalAxisAlignedSplitCost(const VspBspTraversalData &data,
|
---|
| 388 | const AxisAlignedBox3 &box,
|
---|
| 389 | const int axis,
|
---|
[547] | 390 | const float &position,
|
---|
| 391 | float &pFront,
|
---|
| 392 | float &pBack) const;
|
---|
[542] | 393 |
|
---|
[664] | 394 | /** Evaluates candidate for splitting.
|
---|
| 395 | */
|
---|
[652] | 396 | void EvalSplitCandidate(VspBspTraversalData &tData, VspBspSplitCandidate &splitData);
|
---|
| 397 |
|
---|
[664] | 398 | /** Computes priority of the traversal data and stores it in tData.
|
---|
| 399 | */
|
---|
| 400 | void EvalPriority(VspBspTraversalData &tData) const;
|
---|
| 401 |
|
---|
[652] | 402 | float EvalRenderCostDecrease(const Plane3 &candidatePlane,
|
---|
| 403 | const VspBspTraversalData &data) const;
|
---|
| 404 |
|
---|
[654] | 405 | void ConstructWithSplitQueue(const PolygonContainer &polys, RayInfoContainer *rays);
|
---|
[653] | 406 |
|
---|
| 407 |
|
---|
[508] | 408 | /** Returns view cell corresponding to
|
---|
| 409 | the invalid view space. If it does not exist, it is created.
|
---|
| 410 | */
|
---|
| 411 | BspViewCell *GetOrCreateOutOfBoundsCell();
|
---|
| 412 |
|
---|
[495] | 413 | /** Collapses the tree with respect to the view cell partition,
|
---|
| 414 | i.e. leaves having the same view cell are collapsed.
|
---|
[501] | 415 | @param node the root of the subtree to be collapsed
|
---|
| 416 | @param collapsed returns the number of collapsed nodes
|
---|
[495] | 417 | @returns node of type leaf if the node could be collapsed,
|
---|
| 418 | this node otherwise
|
---|
| 419 | */
|
---|
[501] | 420 | BspNode *CollapseTree(BspNode *node, int &collapsed);
|
---|
[508] | 421 |
|
---|
[485] | 422 | /** Helper function revalidating the view cell leaf list after merge.
|
---|
| 423 | */
|
---|
[517] | 424 | void RepairViewCellsLeafLists();
|
---|
[485] | 425 |
|
---|
[463] | 426 | /** Evaluates tree stats in the BSP tree leafs.
|
---|
| 427 | */
|
---|
| 428 | void EvaluateLeafStats(const VspBspTraversalData &data);
|
---|
| 429 |
|
---|
| 430 | /** Subdivides node with respect to the traversal data.
|
---|
| 431 | @param tStack current traversal stack
|
---|
| 432 | @param tData traversal data also holding node to be subdivided
|
---|
| 433 | @returns new root of the subtree
|
---|
| 434 | */
|
---|
[600] | 435 | BspNode *Subdivide(VspBspTraversalQueue &tStack,
|
---|
[463] | 436 | VspBspTraversalData &tData);
|
---|
| 437 |
|
---|
[653] | 438 | BspNode *Subdivide(VspBspSplitQueue &tQueue,
|
---|
| 439 | VspBspSplitCandidate &splitCandidate);
|
---|
| 440 |
|
---|
[463] | 441 | /** Constructs the tree from the given traversal data.
|
---|
| 442 | @param polys stores set of polygons on which subdivision may be based
|
---|
| 443 | @param rays storesset of rays on which subdivision may be based
|
---|
| 444 | */
|
---|
| 445 | void Construct(const PolygonContainer &polys, RayInfoContainer *rays);
|
---|
| 446 |
|
---|
| 447 | /** Selects the best possible splitting plane.
|
---|
[472] | 448 | @param plane returns the split plane
|
---|
[463] | 449 | @param leaf the leaf to be split
|
---|
| 450 | @param polys the polygon list on which the split decition is based
|
---|
| 451 | @param rays ray container on which selection may be based
|
---|
| 452 | @note the polygons can be reordered in the process
|
---|
[472] | 453 | @returns true if the cost of the split is under maxCostRatio
|
---|
| 454 |
|
---|
[463] | 455 | */
|
---|
[472] | 456 | bool SelectPlane(Plane3 &plane,
|
---|
| 457 | BspLeaf *leaf,
|
---|
[508] | 458 | VspBspTraversalData &data,
|
---|
| 459 | VspBspTraversalData &frontData,
|
---|
[612] | 460 | VspBspTraversalData &backData,
|
---|
| 461 | int &splitAxis);
|
---|
[463] | 462 |
|
---|
| 463 | /** Strategies where the effect of the split plane is tested
|
---|
| 464 | on all input rays.
|
---|
| 465 |
|
---|
| 466 | @returns the cost of the candidate split plane
|
---|
| 467 | */
|
---|
[573] | 468 | float EvalSplitPlaneCost(const Plane3 &candidatePlane,
|
---|
| 469 | const VspBspTraversalData &data,
|
---|
| 470 | BspNodeGeometry &geomFront,
|
---|
| 471 | BspNodeGeometry &geomBack,
|
---|
| 472 | float &pFront,
|
---|
| 473 | float &pBack) const;
|
---|
[463] | 474 |
|
---|
[653] | 475 | /** Subdivides leaf.
|
---|
[463] | 476 | @param leaf the leaf to be subdivided
|
---|
| 477 |
|
---|
| 478 | @param polys the polygons to be split
|
---|
| 479 | @param frontPolys returns the polygons in front of the split plane
|
---|
| 480 | @param backPolys returns the polygons in the back of the split plane
|
---|
| 481 |
|
---|
| 482 | @param rays the polygons to be filtered
|
---|
| 483 | @param frontRays returns the polygons in front of the split plane
|
---|
| 484 | @param backRays returns the polygons in the back of the split plane
|
---|
| 485 |
|
---|
| 486 | @returns the root of the subdivision
|
---|
| 487 | */
|
---|
| 488 |
|
---|
[653] | 489 | BspInterior *SubdivideNode(const Plane3 &splitPlane,
|
---|
| 490 | VspBspTraversalData &tData,
|
---|
| 491 | VspBspTraversalData &frontData,
|
---|
| 492 | VspBspTraversalData &backData,
|
---|
| 493 | PolygonContainer &coincident);
|
---|
[463] | 494 |
|
---|
| 495 | /** Extracts the meshes of the objects and adds them to polygons.
|
---|
| 496 | Adds object aabb to the aabb of the tree.
|
---|
| 497 | @param maxPolys the maximal number of objects to be stored as polygons
|
---|
| 498 | @returns the number of polygons
|
---|
| 499 | */
|
---|
| 500 | int AddToPolygonSoup(const ObjectContainer &objects,
|
---|
| 501 | PolygonContainer &polys,
|
---|
| 502 | int maxObjects = 0);
|
---|
| 503 |
|
---|
| 504 | /** Extracts the meshes of the view cells and and adds them to polygons.
|
---|
| 505 | Adds view cell aabb to the aabb of the tree.
|
---|
| 506 | @param maxPolys the maximal number of objects to be stored as polygons
|
---|
| 507 | @returns the number of polygons
|
---|
| 508 | */
|
---|
| 509 | int AddToPolygonSoup(const ViewCellContainer &viewCells,
|
---|
| 510 | PolygonContainer &polys,
|
---|
| 511 | int maxObjects = 0);
|
---|
| 512 |
|
---|
| 513 | /** Extract polygons of this mesh and add to polygon container.
|
---|
| 514 | @param mesh the mesh that drives the polygon construction
|
---|
| 515 | @param parent the parent intersectable this polygon is constructed from
|
---|
| 516 | @returns number of polygons
|
---|
| 517 | */
|
---|
| 518 | int AddMeshToPolygons(Mesh *mesh, PolygonContainer &polys, MeshInstance *parent);
|
---|
| 519 |
|
---|
[495] | 520 | /** Selects an axis aligned for the next split.
|
---|
| 521 | @returns cost for this split
|
---|
[463] | 522 | */
|
---|
[480] | 523 | float SelectAxisAlignedPlane(Plane3 &plane,
|
---|
[491] | 524 | const VspBspTraversalData &tData,
|
---|
[495] | 525 | int &axis,
|
---|
[508] | 526 | BspNodeGeometry **frontGeom,
|
---|
| 527 | BspNodeGeometry **backGeom,
|
---|
[547] | 528 | float &pFront,
|
---|
| 529 | float &pBack,
|
---|
| 530 | const bool useKdSplit);
|
---|
[480] | 531 |
|
---|
[463] | 532 | /** Sorts split candidates for surface area heuristics for axis aligned splits.
|
---|
| 533 | @param polys the input for choosing split candidates
|
---|
| 534 | @param axis the current split axis
|
---|
| 535 | @param splitCandidates returns sorted list of split candidates
|
---|
| 536 | */
|
---|
[710] | 537 | void SortSplitCandidates(const RayInfoContainer &rays,
|
---|
| 538 | const int axis,
|
---|
| 539 | float minBand,
|
---|
| 540 | float maxBand);
|
---|
[463] | 541 |
|
---|
[480] | 542 | /** Computes best cost for axis aligned planes.
|
---|
| 543 | */
|
---|
| 544 | float BestCostRatioHeuristics(const RayInfoContainer &rays,
|
---|
| 545 | const AxisAlignedBox3 &box,
|
---|
| 546 | const int pvsSize,
|
---|
[710] | 547 | const int axis,
|
---|
[480] | 548 | float &position);
|
---|
| 549 |
|
---|
[463] | 550 | /** Selects an axis aligned split plane.
|
---|
[491] | 551 | @Returns true if split is valied
|
---|
[463] | 552 | */
|
---|
| 553 | bool SelectAxisAlignedPlane(Plane3 &plane, const PolygonContainer &polys) const;
|
---|
| 554 |
|
---|
| 555 | /** Subdivides the rays into front and back rays according to the split plane.
|
---|
| 556 |
|
---|
| 557 | @param plane the split plane
|
---|
| 558 | @param rays contains the rays to be split. The rays are
|
---|
| 559 | distributed into front and back rays.
|
---|
| 560 | @param frontRays returns rays on the front side of the plane
|
---|
| 561 | @param backRays returns rays on the back side of the plane
|
---|
| 562 |
|
---|
| 563 | @returns the number of splits
|
---|
| 564 | */
|
---|
| 565 | int SplitRays(const Plane3 &plane,
|
---|
| 566 | RayInfoContainer &rays,
|
---|
| 567 | RayInfoContainer &frontRays,
|
---|
[639] | 568 | RayInfoContainer &backRays) const;
|
---|
[463] | 569 |
|
---|
| 570 |
|
---|
| 571 | /** Extracts the split planes representing the space bounded by node n.
|
---|
| 572 | */
|
---|
| 573 | void ExtractHalfSpaces(BspNode *n, vector<Plane3> &halfSpaces) const;
|
---|
| 574 |
|
---|
| 575 | /** Adds the object to the pvs of the front and back leaf with a given classification.
|
---|
| 576 |
|
---|
| 577 | @param obj the object to be added
|
---|
| 578 | @param cf the ray classification regarding the split plane
|
---|
| 579 | @param frontPvs returns the PVS of the front partition
|
---|
| 580 | @param backPvs returns the PVS of the back partition
|
---|
| 581 |
|
---|
| 582 | */
|
---|
[508] | 583 | void AddObjToPvs(Intersectable *obj,
|
---|
| 584 | const int cf,
|
---|
| 585 | int &frontPvs,
|
---|
| 586 | int &backPvs,
|
---|
| 587 | int &totalPvs) const;
|
---|
[463] | 588 |
|
---|
| 589 | /** Computes PVS size induced by the rays.
|
---|
| 590 | */
|
---|
| 591 | int ComputePvsSize(const RayInfoContainer &rays) const;
|
---|
| 592 |
|
---|
| 593 | /** Returns true if tree can be terminated.
|
---|
| 594 | */
|
---|
[654] | 595 | inline bool LocalTerminationCriteriaMet(const VspBspTraversalData &data) const;
|
---|
[463] | 596 |
|
---|
[694] | 597 | /** Returns true if global tree can be terminated.
|
---|
| 598 | */
|
---|
[654] | 599 | inline bool GlobalTerminationCriteriaMet(const VspBspTraversalData &data) const;
|
---|
| 600 |
|
---|
[463] | 601 | /** Computes accumulated ray lenght of this rays.
|
---|
| 602 | */
|
---|
| 603 | float AccumulatedRayLength(const RayInfoContainer &rays) const;
|
---|
| 604 |
|
---|
| 605 | /** Splits polygons with respect to the split plane.
|
---|
| 606 |
|
---|
| 607 | @param plane the split plane
|
---|
| 608 | @param polys the polygons to be split. the polygons are consumed and
|
---|
| 609 | distributed to the containers frontPolys, backPolys, coincident.
|
---|
| 610 | @param frontPolys returns the polygons in the front of the split plane
|
---|
| 611 | @param backPolys returns the polygons in the back of the split plane
|
---|
| 612 | @param coincident returns the polygons coincident to the split plane
|
---|
| 613 |
|
---|
| 614 | @returns the number of splits
|
---|
| 615 | */
|
---|
| 616 | int SplitPolygons(const Plane3 &plane,
|
---|
| 617 | PolygonContainer &polys,
|
---|
| 618 | PolygonContainer &frontPolys,
|
---|
| 619 | PolygonContainer &backPolys,
|
---|
| 620 | PolygonContainer &coincident) const;
|
---|
| 621 |
|
---|
| 622 | /** Adds ray sample contributions to the PVS.
|
---|
| 623 | @param sampleContributions the number contributions of the samples
|
---|
| 624 | @param contributingSampels the number of contributing rays
|
---|
| 625 |
|
---|
| 626 | */
|
---|
| 627 | void AddToPvs(BspLeaf *leaf,
|
---|
| 628 | const RayInfoContainer &rays,
|
---|
[556] | 629 | float &sampleContributions,
|
---|
[463] | 630 | int &contributingSamples);
|
---|
| 631 |
|
---|
[466] | 632 |
|
---|
[485] | 633 |
|
---|
[580] | 634 |
|
---|
| 635 |
|
---|
| 636 |
|
---|
[473] | 637 | /** Take 3 ray endpoints, where two are minimum and one a maximum
|
---|
| 638 | point or the other way round.
|
---|
| 639 | */
|
---|
| 640 | Plane3 ChooseCandidatePlane(const RayInfoContainer &rays) const;
|
---|
[466] | 641 |
|
---|
[473] | 642 | /** Take plane normal as plane normal and the midpoint of the ray.
|
---|
[485] | 643 | PROBLEM: does not resemble any point where visibility is
|
---|
| 644 | likely to change
|
---|
[473] | 645 | */
|
---|
| 646 | Plane3 ChooseCandidatePlane2(const RayInfoContainer &rays) const;
|
---|
| 647 |
|
---|
[485] | 648 | /** Fit the plane between the two lines so that the plane
|
---|
| 649 | has equal shortest distance to both lines.
|
---|
[473] | 650 | */
|
---|
| 651 | Plane3 ChooseCandidatePlane3(const RayInfoContainer &rays) const;
|
---|
[466] | 652 |
|
---|
[580] | 653 | /** Collects candidates for merging.
|
---|
| 654 | @param leaves the leaves to be merged
|
---|
| 655 | @returns number of leaves in queue
|
---|
[486] | 656 | */
|
---|
[580] | 657 | int CollectMergeCandidates(const vector<BspLeaf *> leaves, vector<MergeCandidate> &candidates);
|
---|
[478] | 658 |
|
---|
[580] | 659 | /** Collects candidates for the merge in the merge queue.
|
---|
| 660 | @returns number of leaves in queue
|
---|
| 661 | */
|
---|
| 662 | int CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
|
---|
[547] | 663 |
|
---|
[648] | 664 | /** Preprocesses polygons and throws out all polygons which are coincident to
|
---|
| 665 | the view space box faces (they can be problematic).
|
---|
| 666 | */
|
---|
| 667 | void PreprocessPolygons(PolygonContainer &polys);
|
---|
[580] | 668 |
|
---|
[487] | 669 | /** Propagates valid flag up the tree.
|
---|
| 670 | */
|
---|
| 671 | void PropagateUpValidity(BspNode *node);
|
---|
| 672 |
|
---|
[508] | 673 | /** Writes the node to disk
|
---|
| 674 | @note: should be implemented as visitor
|
---|
[489] | 675 | */
|
---|
[508] | 676 | void ExportNode(BspNode *node, ofstream &stream);
|
---|
[489] | 677 |
|
---|
[587] | 678 | /** Returns estimated memory usage of tree.
|
---|
[508] | 679 | */
|
---|
[600] | 680 | //float GetMemUsage(const VspBspTraversalQueue &tstack) const;
|
---|
[508] | 681 | float GetMemUsage() const;
|
---|
| 682 |
|
---|
[551] | 683 |
|
---|
[564] | 684 |
|
---|
[463] | 685 | /// Pointer to the root of the tree
|
---|
| 686 | BspNode *mRoot;
|
---|
| 687 |
|
---|
[574] | 688 | BspTreeStatistics mBspStats;
|
---|
[463] | 689 |
|
---|
| 690 | /// Strategies for choosing next split plane.
|
---|
| 691 | enum {NO_STRATEGY = 0,
|
---|
| 692 | RANDOM_POLYGON = 1,
|
---|
| 693 | AXIS_ALIGNED = 2,
|
---|
| 694 | LEAST_RAY_SPLITS = 256,
|
---|
| 695 | BALANCED_RAYS = 512,
|
---|
| 696 | PVS = 1024
|
---|
| 697 | };
|
---|
| 698 |
|
---|
| 699 | /// box around the whole view domain
|
---|
| 700 | AxisAlignedBox3 mBox;
|
---|
| 701 |
|
---|
[612] | 702 | bool mUseCostHeuristics;
|
---|
| 703 |
|
---|
[463] | 704 | /// minimal number of rays before subdivision termination
|
---|
| 705 | int mTermMinRays;
|
---|
| 706 | /// maximal possible depth
|
---|
| 707 | int mTermMaxDepth;
|
---|
[547] | 708 | /// mininum probability
|
---|
| 709 | float mTermMinProbability;
|
---|
[463] | 710 | /// mininum PVS
|
---|
| 711 | int mTermMinPvs;
|
---|
| 712 | /// maximal contribution per ray
|
---|
| 713 | float mTermMaxRayContribution;
|
---|
| 714 | /// minimal accumulated ray length
|
---|
| 715 | float mTermMinAccRayLength;
|
---|
| 716 |
|
---|
[600] | 717 | //HACK
|
---|
| 718 | int mTermMinPolygons;
|
---|
[547] | 719 |
|
---|
[654] | 720 | float mTermMinGlobalCostRatio;
|
---|
| 721 | int mTermGlobalCostMissTolerance;
|
---|
| 722 |
|
---|
| 723 | int mGlobalCostMisses;
|
---|
| 724 |
|
---|
[655] | 725 | bool mUseSplitCostQueue;
|
---|
[508] | 726 | //-- termination criteria for axis aligned split
|
---|
| 727 |
|
---|
| 728 | /// minimal number of rays for axis aligned split
|
---|
| 729 | int mTermMinRaysForAxisAligned;
|
---|
| 730 | // max ray contribution
|
---|
| 731 | float mTermMaxRayContriForAxisAligned;
|
---|
| 732 |
|
---|
[463] | 733 | /// strategy to get the best split plane
|
---|
| 734 | int mSplitPlaneStrategy;
|
---|
| 735 | /// number of candidates evaluated for the next split plane
|
---|
| 736 | int mMaxPolyCandidates;
|
---|
| 737 | /// number of candidates for split planes evaluated using the rays
|
---|
| 738 | int mMaxRayCandidates;
|
---|
| 739 | /// balancing factor for PVS criterium
|
---|
| 740 | float mCtDivCi;
|
---|
| 741 |
|
---|
| 742 | //-- axis aligned split criteria
|
---|
[472] | 743 | float mAxisAlignedCtDivCi;
|
---|
| 744 | /// spezifies the split border of the axis aligned split
|
---|
| 745 | float mAxisAlignedSplitBorder;
|
---|
[463] | 746 |
|
---|
[472] | 747 | /// maximal acceptable cost ratio
|
---|
| 748 | float mTermMaxCostRatio;
|
---|
| 749 | /// tolerance value indicating how often the max cost ratio can be failed
|
---|
| 750 | int mTermMissTolerance;
|
---|
| 751 |
|
---|
[463] | 752 | //-- factors guiding the split plane heuristics
|
---|
| 753 | float mLeastRaySplitsFactor;
|
---|
| 754 | float mBalancedRaysFactor;
|
---|
| 755 | float mPvsFactor;
|
---|
| 756 |
|
---|
[547] | 757 | /// if area or volume should be used for PVS heuristics
|
---|
| 758 | bool mUseAreaForPvs;
|
---|
[472] | 759 | /// tolerance for polygon split
|
---|
[463] | 760 | float mEpsilon;
|
---|
[472] | 761 | /// maximal number of test rays used to evaluate candidate split plane
|
---|
[463] | 762 | int mMaxTests;
|
---|
[474] | 763 | /// normalizes different bsp split plane criteria
|
---|
| 764 | float mCostNormalizer;
|
---|
[473] | 765 | /// maximal number of view cells
|
---|
| 766 | int mMaxViewCells;
|
---|
[580] | 767 |
|
---|
[600] | 768 | ofstream mSubdivisionStats;
|
---|
[463] | 769 |
|
---|
[478] | 770 | // if rays should be stored in leaves
|
---|
| 771 | bool mStoreRays;
|
---|
[485] | 772 |
|
---|
| 773 | /// if only driving axis should be used for split
|
---|
| 774 | bool mOnlyDrivingAxis;
|
---|
[478] | 775 |
|
---|
[485] | 776 | ViewCellsManager *mViewCellsManager;
|
---|
| 777 |
|
---|
[480] | 778 | vector<SortableEntry> *mSplitCandidates;
|
---|
| 779 |
|
---|
[508] | 780 |
|
---|
[580] | 781 | float mRenderCostWeight;
|
---|
[489] | 782 | /// View cell corresponding to the space outside the valid view space
|
---|
| 783 | BspViewCell *mOutOfBoundsCell;
|
---|
[487] | 784 |
|
---|
[508] | 785 | /// maximal tree memory
|
---|
| 786 | float mMaxMemory;
|
---|
[517] | 787 | /// the tree is out of memory
|
---|
| 788 | bool mOutOfMemory;
|
---|
[463] | 789 |
|
---|
[600] | 790 | float mTotalCost;
|
---|
[607] | 791 | int mTotalPvsSize;
|
---|
| 792 |
|
---|
[611] | 793 | //int mSplits;
|
---|
[663] | 794 | /// subdivision stats output file
|
---|
[600] | 795 | ofstream mSubdivsionStats;
|
---|
[663] | 796 | /// if random split axis should be used
|
---|
[605] | 797 | bool mUseRandomAxis;
|
---|
[663] | 798 | /// use polygon split whenever there are polys left
|
---|
[607] | 799 | bool mUsePolygonSplitIfAvailable;
|
---|
[663] | 800 | /// current time stamp (used for keeping split history)
|
---|
[610] | 801 | int mTimeStamp;
|
---|
[663] | 802 | /// number of currenly generated view cells
|
---|
[612] | 803 | int mCreatedViewCells;
|
---|
[663] | 804 | /// if vsp bsp tree should simulate octree
|
---|
| 805 | bool mSimulateOctree;
|
---|
[612] | 806 |
|
---|
[664] | 807 | /// if we should use breath first priority for the splits
|
---|
| 808 | bool mBreathFirstSplits;
|
---|
[663] | 809 |
|
---|
[676] | 810 | bool mEmptyViewCellsMergeAllowed;
|
---|
| 811 |
|
---|
[580] | 812 | private:
|
---|
[557] | 813 |
|
---|
[463] | 814 | /// Generates unique ids for PVS criterium
|
---|
| 815 | static void GenerateUniqueIdsForPvs();
|
---|
| 816 |
|
---|
| 817 | //-- unique ids for PVS criterium
|
---|
| 818 | static int sFrontId;
|
---|
| 819 | static int sBackId;
|
---|
| 820 | static int sFrontAndBackId;
|
---|
| 821 | };
|
---|
| 822 |
|
---|
[578] | 823 |
|
---|
[478] | 824 |
|
---|
| 825 |
|
---|
[463] | 826 | #endif
|
---|