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