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