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