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