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