[373] | 1 | #ifndef _ViewCellBsp_H__ |
---|
| 2 | #define _ViewCellBsp_H__ |
---|
| 3 | |
---|
| 4 | #include "Mesh.h" |
---|
| 5 | #include "Containers.h" |
---|
[390] | 6 | #include "Polygon3.h" |
---|
[373] | 7 | #include <stack> |
---|
[414] | 8 | #include "Statistics.h" |
---|
[448] | 9 | #include "VssRay.h" |
---|
[373] | 10 | |
---|
[448] | 11 | |
---|
[373] | 12 | class ViewCell; |
---|
| 13 | class BspViewCell; |
---|
| 14 | class Plane3; |
---|
| 15 | class BspTree; |
---|
| 16 | class BspInterior; |
---|
[390] | 17 | //class Polygon3; |
---|
[373] | 18 | class AxisAlignedBox3; |
---|
| 19 | class Ray; |
---|
| 20 | |
---|
[390] | 21 | class BspNodeGeometry |
---|
| 22 | { |
---|
| 23 | public: |
---|
| 24 | BspNodeGeometry() |
---|
[396] | 25 | {}; |
---|
[390] | 26 | |
---|
| 27 | ~BspNodeGeometry(); |
---|
| 28 | |
---|
| 29 | float GetArea() const; |
---|
| 30 | |
---|
[448] | 31 | /** Computes new front and back geometry based on the old cell |
---|
| 32 | geometry and a new split plane |
---|
[390] | 33 | */ |
---|
[448] | 34 | void SplitGeometry(BspNodeGeometry &front, |
---|
[396] | 35 | BspNodeGeometry &back, |
---|
[448] | 36 | const Plane3 &splitPlane, |
---|
| 37 | const AxisAlignedBox3 &box, |
---|
| 38 | const float epsilon) const; |
---|
[390] | 39 | |
---|
[448] | 40 | Polygon3 *SplitPolygon(Polygon3 *poly, const float epsilon) const; |
---|
[396] | 41 | |
---|
[390] | 42 | PolygonContainer mPolys; |
---|
| 43 | }; |
---|
| 44 | |
---|
[373] | 45 | /** Data structure used for optimized ray casting. |
---|
| 46 | */ |
---|
| 47 | struct BspRayTraversalData |
---|
| 48 | { |
---|
| 49 | BspNode *mNode; |
---|
| 50 | Vector3 mExitPoint; |
---|
| 51 | float mMaxT; |
---|
| 52 | |
---|
| 53 | BspRayTraversalData() {} |
---|
| 54 | |
---|
| 55 | BspRayTraversalData(BspNode *n, const Vector3 &extp, const float maxt): |
---|
| 56 | mNode(n), mExitPoint(extp), mMaxT(maxt) |
---|
| 57 | {} |
---|
| 58 | }; |
---|
| 59 | |
---|
| 60 | /** Data used for passing ray data down the tree. |
---|
| 61 | */ |
---|
| 62 | struct BoundedRay |
---|
| 63 | { |
---|
| 64 | Ray *mRay; |
---|
| 65 | float mMinT; |
---|
| 66 | float mMaxT; |
---|
| 67 | |
---|
| 68 | BoundedRay(): mMinT(0), mMaxT(1e6), mRay(NULL) |
---|
| 69 | {} |
---|
| 70 | BoundedRay(Ray *r, float minT, float maxT): |
---|
| 71 | mRay(r), mMinT(minT), mMaxT(maxT) |
---|
| 72 | {} |
---|
| 73 | }; |
---|
| 74 | |
---|
| 75 | typedef vector<BoundedRay *> BoundedRayContainer; |
---|
| 76 | |
---|
[414] | 77 | class BspTreeStatistics: public StatisticsBase |
---|
[373] | 78 | { |
---|
| 79 | public: |
---|
[414] | 80 | // total number of nodes |
---|
| 81 | int nodes; |
---|
| 82 | // number of splits |
---|
| 83 | int splits; |
---|
| 84 | // totals number of rays |
---|
| 85 | int rays; |
---|
| 86 | // maximal reached depth |
---|
| 87 | int maxDepth; |
---|
| 88 | // minimal depth |
---|
| 89 | int minDepth; |
---|
[426] | 90 | |
---|
[414] | 91 | // max depth nodes |
---|
| 92 | int maxDepthNodes; |
---|
[426] | 93 | // minimum depth nodes
|
---|
| 94 | int minDepthNodes;
|
---|
| 95 | // max depth nodes
|
---|
| 96 | int minPvsNodes;
|
---|
| 97 | // nodes with minimum PVS
|
---|
| 98 | int minRaysNodes;
|
---|
| 99 | // max ray contribution nodes
|
---|
| 100 | int maxRayContribNodes;
|
---|
| 101 | // minimum area nodes
|
---|
| 102 | int minAreaNodes; |
---|
| 103 | |
---|
[414] | 104 | // max number of rays per node |
---|
| 105 | int maxObjectRefs; |
---|
| 106 | // accumulated depth (used to compute average) |
---|
| 107 | int accumDepth; |
---|
| 108 | // number of initial polygons |
---|
| 109 | int polys; |
---|
| 110 | /// samples contributing to pvs |
---|
| 111 | int contributingSamples; |
---|
| 112 | /// sample contributions to pvs |
---|
| 113 | int sampleContributions; |
---|
| 114 | /// largest pvs |
---|
| 115 | int largestPvs; |
---|
[373] | 116 | |
---|
[414] | 117 | // Constructor |
---|
| 118 | BspTreeStatistics() |
---|
| 119 | { |
---|
| 120 | Reset(); |
---|
| 121 | } |
---|
[373] | 122 | |
---|
[414] | 123 | int Nodes() const {return nodes;} |
---|
| 124 | int Interior() const { return nodes / 2; } |
---|
| 125 | int Leaves() const { return (nodes / 2) + 1; } |
---|
| 126 | |
---|
| 127 | // TODO: computation wrong |
---|
| 128 | double AvgDepth() const { return accumDepth / (double)Leaves();}; |
---|
[373] | 129 | |
---|
[414] | 130 | void Reset() |
---|
| 131 | { |
---|
| 132 | nodes = 0; |
---|
| 133 | splits = 0; |
---|
[426] | 134 | |
---|
[414] | 135 | maxDepth = 0; |
---|
| 136 | minDepth = 99999; |
---|
| 137 | polys = 0; |
---|
| 138 | accumDepth = 0; |
---|
| 139 | |
---|
[426] | 140 | maxDepthNodes = 0;
|
---|
| 141 | minPvsNodes = 0;
|
---|
| 142 | minRaysNodes = 0;
|
---|
| 143 | maxRayContribNodes = 0; |
---|
| 144 | minAreaNodes = 0; |
---|
| 145 | |
---|
[414] | 146 | contributingSamples = 0; |
---|
| 147 | sampleContributions = 0; |
---|
| 148 | } |
---|
[373] | 149 | |
---|
[414] | 150 | void Print(ostream &app) const; |
---|
[373] | 151 | |
---|
[414] | 152 | friend ostream &operator<<(ostream &s, const BspTreeStatistics &stat) |
---|
| 153 | { |
---|
| 154 | stat.Print(s); |
---|
| 155 | return s; |
---|
| 156 | } |
---|
[373] | 157 | }; |
---|
| 158 | |
---|
[414] | 159 | class BspViewCellsStatistics: public StatisticsBase |
---|
[375] | 160 | { |
---|
| 161 | public: |
---|
| 162 | |
---|
[414] | 163 | /// number of view cells |
---|
| 164 | int viewCells; |
---|
| 165 | |
---|
| 166 | /// size of the PVS |
---|
| 167 | int pvs; |
---|
[375] | 168 | |
---|
[414] | 169 | /// largest PVS of all view cells |
---|
| 170 | int maxPvs; |
---|
[375] | 171 | |
---|
[414] | 172 | /// smallest PVS of all view cells |
---|
| 173 | int minPvs; |
---|
[375] | 174 | |
---|
[414] | 175 | /// view cells with empty PVS |
---|
| 176 | int emptyPvs; |
---|
[375] | 177 | |
---|
[414] | 178 | /// number of bsp leaves covering the view space |
---|
| 179 | int bspLeaves; |
---|
[375] | 180 | |
---|
[414] | 181 | /// largest number of leaves covered by one view cell |
---|
| 182 | int maxBspLeaves; |
---|
[375] | 183 | |
---|
[414] | 184 | // Constructor |
---|
| 185 | BspViewCellsStatistics() |
---|
| 186 | { |
---|
| 187 | Reset(); |
---|
| 188 | } |
---|
[375] | 189 | |
---|
[414] | 190 | double AvgBspLeaves() const {return (double)bspLeaves / (double)viewCells;}; |
---|
| 191 | double AvgPvs() const {return (double)pvs / (double)viewCells;}; |
---|
[375] | 192 | |
---|
[414] | 193 | void Reset() |
---|
| 194 | { |
---|
| 195 | viewCells = 0; |
---|
| 196 | pvs = 0; |
---|
| 197 | maxPvs = 0; |
---|
[375] | 198 | |
---|
[414] | 199 | minPvs = 999999; |
---|
| 200 | emptyPvs = 0; |
---|
| 201 | bspLeaves = 0; |
---|
| 202 | maxBspLeaves = 0; |
---|
| 203 | } |
---|
[375] | 204 | |
---|
[414] | 205 | void Print(ostream &app) const; |
---|
| 206 | |
---|
| 207 | friend ostream &operator<<(ostream &s, const BspViewCellsStatistics &stat) |
---|
| 208 | { |
---|
| 209 | stat.Print(s); |
---|
| 210 | return s; |
---|
| 211 | } |
---|
[375] | 212 | }; |
---|
| 213 | |
---|
[373] | 214 | /** |
---|
| 215 | BspNode abstract class serving for interior and leaf node implementation |
---|
| 216 | */ |
---|
| 217 | class BspNode |
---|
| 218 | { |
---|
| 219 | friend class BspTree; |
---|
| 220 | |
---|
| 221 | public: |
---|
| 222 | BspNode(); |
---|
[390] | 223 | virtual ~BspNode(){}; |
---|
[373] | 224 | BspNode(BspInterior *parent); |
---|
| 225 | |
---|
| 226 | /** Determines whether this node is a leaf or not |
---|
| 227 | @return true if leaf |
---|
| 228 | */ |
---|
| 229 | virtual bool IsLeaf() const = 0; |
---|
| 230 | |
---|
| 231 | /** Determines whether this node is a root |
---|
| 232 | @return true if root |
---|
| 233 | */ |
---|
| 234 | virtual bool IsRoot() const; |
---|
| 235 | |
---|
| 236 | /** Returns parent node. |
---|
| 237 | */ |
---|
| 238 | BspInterior *GetParent(); |
---|
[390] | 239 | |
---|
[373] | 240 | /** Sets parent node. |
---|
| 241 | */ |
---|
| 242 | void SetParent(BspInterior *parent); |
---|
| 243 | |
---|
[390] | 244 | |
---|
[421] | 245 | static int sMailId; |
---|
| 246 | int mMailbox; |
---|
[373] | 247 | |
---|
[421] | 248 | void Mail() { mMailbox = sMailId; } |
---|
| 249 | static void NewMail() { ++ sMailId; } |
---|
| 250 | bool Mailed() const { return mMailbox == sMailId; } |
---|
[373] | 251 | |
---|
| 252 | protected: |
---|
| 253 | |
---|
| 254 | /// parent of this node |
---|
| 255 | BspInterior *mParent; |
---|
| 256 | }; |
---|
| 257 | |
---|
| 258 | /** BSP interior node implementation |
---|
| 259 | */ |
---|
| 260 | class BspInterior : public BspNode |
---|
| 261 | { |
---|
| 262 | friend class BspTree; |
---|
| 263 | public: |
---|
| 264 | /** Standard contructor taking split plane as argument. |
---|
| 265 | */ |
---|
| 266 | BspInterior(const Plane3 &plane); |
---|
[411] | 267 | ~BspInterior(); |
---|
[373] | 268 | /** @return false since it is an interior node |
---|
| 269 | */ |
---|
| 270 | bool IsLeaf() const; |
---|
| 271 | |
---|
| 272 | BspNode *GetBack(); |
---|
| 273 | BspNode *GetFront(); |
---|
| 274 | |
---|
[448] | 275 | /** Returns split plane. |
---|
| 276 | */ |
---|
| 277 | Plane3 GetPlane() const; |
---|
[373] | 278 | |
---|
[448] | 279 | /** Replace front or back child with new child. |
---|
| 280 | */ |
---|
[373] | 281 | void ReplaceChildLink(BspNode *oldChild, BspNode *newChild); |
---|
[448] | 282 | /** Replace front and back child. |
---|
| 283 | */ |
---|
[373] | 284 | void SetupChildLinks(BspNode *b, BspNode *f); |
---|
| 285 | |
---|
| 286 | friend ostream &operator<<(ostream &s, const BspInterior &A) |
---|
| 287 | { |
---|
| 288 | return s << A.mPlane; |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | protected: |
---|
| 292 | |
---|
| 293 | /// Splitting plane corresponding to this node |
---|
| 294 | Plane3 mPlane; |
---|
[448] | 295 | |
---|
[373] | 296 | /// back node |
---|
| 297 | BspNode *mBack; |
---|
| 298 | /// front node |
---|
| 299 | BspNode *mFront; |
---|
| 300 | }; |
---|
| 301 | |
---|
| 302 | /** BSP leaf node implementation. |
---|
| 303 | */ |
---|
| 304 | class BspLeaf : public BspNode |
---|
| 305 | { |
---|
| 306 | friend class BspTree; |
---|
| 307 | |
---|
| 308 | public: |
---|
| 309 | BspLeaf(); |
---|
| 310 | BspLeaf(BspViewCell *viewCell); |
---|
| 311 | BspLeaf(BspInterior *parent); |
---|
| 312 | BspLeaf(BspInterior *parent, BspViewCell *viewCell); |
---|
| 313 | |
---|
| 314 | /** @return true since it is an interior node |
---|
| 315 | */ |
---|
| 316 | bool IsLeaf() const; |
---|
| 317 | |
---|
| 318 | /** Returns pointer of view cell. |
---|
| 319 | */ |
---|
| 320 | BspViewCell *GetViewCell() const; |
---|
| 321 | |
---|
| 322 | /** Sets pointer to view cell. |
---|
| 323 | */ |
---|
| 324 | void SetViewCell(BspViewCell *viewCell); |
---|
| 325 | |
---|
[448] | 326 | VssRayContainer mVssRays; |
---|
[373] | 327 | |
---|
| 328 | protected: |
---|
| 329 | |
---|
| 330 | /// if NULL this does not correspond to feasible viewcell |
---|
| 331 | BspViewCell *mViewCell; |
---|
| 332 | }; |
---|
| 333 | |
---|
| 334 | /** Implementation of the view cell BSP tree. |
---|
| 335 | */ |
---|
| 336 | class BspTree |
---|
| 337 | { |
---|
| 338 | public: |
---|
| 339 | |
---|
| 340 | /** Additional data which is passed down the BSP tree during traversal. |
---|
| 341 | */ |
---|
| 342 | struct BspTraversalData |
---|
| 343 | { |
---|
| 344 | /// the current node |
---|
| 345 | BspNode *mNode; |
---|
| 346 | /// polygonal data for splitting |
---|
| 347 | PolygonContainer *mPolygons; |
---|
| 348 | /// current depth |
---|
| 349 | int mDepth; |
---|
| 350 | /// the view cell associated with this subdivsion |
---|
| 351 | ViewCell *mViewCell; |
---|
| 352 | /// rays piercing this node |
---|
| 353 | BoundedRayContainer *mRays; |
---|
[442] | 354 | /// area of current node |
---|
| 355 | float mArea; |
---|
| 356 | /// geometry of node as induced by planes |
---|
| 357 | BspNodeGeometry *mGeometry; |
---|
[441] | 358 | |
---|
[390] | 359 | /// pvs size |
---|
| 360 | int mPvs; |
---|
| 361 | |
---|
[423] | 362 | /** Returns average ray contribution. |
---|
| 363 | */ |
---|
| 364 | float GetAvgRayContribution() const |
---|
| 365 | { |
---|
| 366 | return (float)mPvs / ((float)mRays->size() + Limits::Small); |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | |
---|
[373] | 370 | BspTraversalData(): |
---|
| 371 | mNode(NULL), |
---|
| 372 | mPolygons(NULL), |
---|
| 373 | mDepth(0), |
---|
| 374 | mViewCell(NULL), |
---|
[390] | 375 | mRays(NULL), |
---|
[442] | 376 | mPvs(0), |
---|
| 377 | mArea(0.0), |
---|
| 378 | mGeometry(NULL) |
---|
[373] | 379 | {} |
---|
| 380 | |
---|
| 381 | BspTraversalData(BspNode *node, |
---|
| 382 | PolygonContainer *polys, |
---|
| 383 | const int depth, |
---|
| 384 | ViewCell *viewCell, |
---|
[390] | 385 | BoundedRayContainer *rays, |
---|
[442] | 386 | int pvs, |
---|
| 387 | float area, |
---|
| 388 | BspNodeGeometry *cell): |
---|
[373] | 389 | mNode(node), |
---|
| 390 | mPolygons(polys), |
---|
| 391 | mDepth(depth), |
---|
| 392 | mViewCell(viewCell), |
---|
[390] | 393 | mRays(rays), |
---|
[442] | 394 | mPvs(pvs), |
---|
| 395 | mArea(area), |
---|
| 396 | mGeometry(cell) |
---|
[373] | 397 | {} |
---|
| 398 | }; |
---|
| 399 | |
---|
| 400 | typedef std::stack<BspTraversalData> BspTraversalStack; |
---|
| 401 | |
---|
| 402 | /** Default constructor creating an empty tree. |
---|
| 403 | */ |
---|
[441] | 404 | BspTree(); |
---|
| 405 | |
---|
[373] | 406 | ~BspTree(); |
---|
| 407 | |
---|
[441] | 408 | |
---|
[373] | 409 | const BspTreeStatistics &GetStatistics() const; |
---|
| 410 | |
---|
| 411 | /** Constructs tree using the given list of view cells. |
---|
| 412 | For this type of construction we filter all view cells down the |
---|
| 413 | tree. If there is no polygon left, the last split plane |
---|
| 414 | decides inside or outside of the viewcell. A pointer to the |
---|
| 415 | appropriate view cell is stored within each leaf. |
---|
| 416 | Many leafs can point to the same viewcell. |
---|
| 417 | */ |
---|
| 418 | void Construct(const ViewCellContainer &viewCells); |
---|
| 419 | |
---|
| 420 | /** Constructs tree using the given list of objects. |
---|
| 421 | @note the objects are not taken as view cells, but the view cells are |
---|
| 422 | constructed from the subdivision: Each leaf is taken as one viewcell. |
---|
| 423 | @param objects list of objects |
---|
| 424 | */ |
---|
| 425 | void Construct(const ObjectContainer &objects); |
---|
| 426 | |
---|
[436] | 427 | void Construct(const ObjectContainer &objects, |
---|
| 428 | const RayContainer &sampleRays); |
---|
| 429 | |
---|
[373] | 430 | /** Constructs the tree from a given set of rays. |
---|
| 431 | @param sampleRays the set of sample rays the construction is based on |
---|
| 432 | @param viewCells if not NULL, new view cells are |
---|
| 433 | created in the leafs and stored in the conatainer |
---|
| 434 | */ |
---|
| 435 | void Construct(const RayContainer &sampleRays); |
---|
| 436 | |
---|
| 437 | /** Returns list of BSP leaves. |
---|
| 438 | */ |
---|
| 439 | void CollectLeaves(vector<BspLeaf *> &leaves) const; |
---|
| 440 | |
---|
| 441 | /** Returns box which bounds the whole tree. |
---|
| 442 | */ |
---|
| 443 | AxisAlignedBox3 GetBoundingBox()const; |
---|
| 444 | |
---|
| 445 | /** Returns root of BSP tree. |
---|
| 446 | */ |
---|
| 447 | BspNode *GetRoot() const; |
---|
| 448 | |
---|
| 449 | /** Exports Bsp tree to file. |
---|
| 450 | */ |
---|
| 451 | bool Export(const string filename); |
---|
| 452 | |
---|
| 453 | /** Collects the leaf view cells of the tree |
---|
| 454 | @param viewCells returns the view cells |
---|
| 455 | */ |
---|
| 456 | void CollectViewCells(ViewCellContainer &viewCells) const; |
---|
| 457 | |
---|
| 458 | /** A ray is cast possible intersecting the tree. |
---|
| 459 | @param the ray that is cast. |
---|
| 460 | @returns the number of intersections with objects stored in the tree. |
---|
| 461 | */ |
---|
| 462 | int CastRay(Ray &ray); |
---|
| 463 | |
---|
| 464 | /// bsp tree construction types |
---|
[422] | 465 | enum {FROM_INPUT_VIEW_CELLS, FROM_SCENE_GEOMETRY, FROM_SAMPLES}; |
---|
[373] | 466 | |
---|
| 467 | /** Returns statistics. |
---|
| 468 | */ |
---|
| 469 | BspTreeStatistics &GetStat(); |
---|
| 470 | |
---|
| 471 | /** finds neighbouring leaves of this tree node. |
---|
| 472 | */ |
---|
| 473 | int FindNeighbors(BspNode *n, vector<BspLeaf *> &neighbors, |
---|
| 474 | const bool onlyUnmailed) const; |
---|
| 475 | |
---|
| 476 | /** Constructs geometry associated with the half space intersections |
---|
| 477 | leading to this node. |
---|
| 478 | */ |
---|
| 479 | void ConstructGeometry(BspNode *n, PolygonContainer &cell) const; |
---|
| 480 | |
---|
| 481 | /** Construct geometry of view cell. |
---|
| 482 | */ |
---|
| 483 | void ConstructGeometry(BspViewCell *vc, PolygonContainer &cell) const; |
---|
| 484 | |
---|
[443] | 485 | /** Constructs geometry of view cell returning a BSP node geometry type. |
---|
| 486 | */ |
---|
[390] | 487 | void ConstructGeometry(BspNode *n, BspNodeGeometry &cell) const; |
---|
| 488 | |
---|
[373] | 489 | /** Returns random leaf of BSP tree. |
---|
| 490 | @param halfspace defines the halfspace from which the leaf is taken. |
---|
| 491 | */ |
---|
| 492 | BspLeaf *GetRandomLeaf(const Plane3 &halfspace); |
---|
| 493 | |
---|
| 494 | /** Returns random leaf of BSP tree. |
---|
| 495 | @param onlyUnmailed if only unmailed leaves should be returned. |
---|
| 496 | */ |
---|
| 497 | BspLeaf *GetRandomLeaf(const bool onlyUnmailed = false); |
---|
| 498 | |
---|
[375] | 499 | /** Traverses tree and counts all view cells as well as their PVS size. |
---|
[373] | 500 | */ |
---|
[375] | 501 | void EvaluateViewCellsStats(BspViewCellsStatistics &stat) const; |
---|
[373] | 502 | |
---|
[441] | 503 | /** Returns view cell corresponding to unbounded space. |
---|
| 504 | */ |
---|
| 505 | BspViewCell *GetRootCell() const; |
---|
| 506 | |
---|
[448] | 507 | /** Returns epsilon of this tree. |
---|
[421] | 508 | */ |
---|
[448] | 509 | float GetEpsilon() const; |
---|
[420] | 510 | |
---|
[373] | 511 | protected: |
---|
| 512 | |
---|
| 513 | // -------------------------------------------------------------- |
---|
| 514 | // For sorting objects |
---|
| 515 | // -------------------------------------------------------------- |
---|
| 516 | struct SortableEntry |
---|
| 517 | { |
---|
| 518 | enum {POLY_MIN, POLY_MAX}; |
---|
| 519 | |
---|
| 520 | int type; |
---|
| 521 | float value; |
---|
| 522 | Polygon3 *poly; |
---|
| 523 | SortableEntry() {} |
---|
| 524 | SortableEntry(const int t, const float v, Polygon3 *poly): |
---|
| 525 | type(t), value(v), poly(poly) {} |
---|
| 526 | |
---|
| 527 | bool operator<(const SortableEntry &b) const |
---|
| 528 | { |
---|
| 529 | return value < b.value; |
---|
| 530 | } |
---|
| 531 | }; |
---|
| 532 | |
---|
| 533 | /** Evaluates tree stats in the BSP tree leafs. |
---|
| 534 | */ |
---|
| 535 | void EvaluateLeafStats(const BspTraversalData &data); |
---|
| 536 | |
---|
| 537 | /** Subdivides node with respect to the traversal data. |
---|
| 538 | @param tStack current traversal stack |
---|
| 539 | @param tData traversal data also holding node to be subdivided |
---|
| 540 | @returns new root of the subtree |
---|
| 541 | */ |
---|
| 542 | BspNode *Subdivide(BspTraversalStack &tStack, BspTraversalData &tData); |
---|
| 543 | |
---|
| 544 | /** Constructs the tree from the given list of polygons and rays. |
---|
| 545 | @param polys stores set of polygons on which subdivision may be based |
---|
| 546 | @param rays storesset of rays on which subdivision may be based |
---|
| 547 | */ |
---|
| 548 | void Construct(PolygonContainer *polys, BoundedRayContainer *rays); |
---|
| 549 | |
---|
| 550 | /** Selects the best possible splitting plane. |
---|
| 551 | @param leaf the leaf to be split |
---|
| 552 | @param polys the polygon list on which the split decition is based |
---|
| 553 | @param rays ray container on which selection may be based |
---|
| 554 | @note the polygons can be reordered in the process |
---|
| 555 | @returns the split plane |
---|
| 556 | */ |
---|
| 557 | Plane3 SelectPlane(BspLeaf *leaf, |
---|
[400] | 558 | BspTraversalData &data); |
---|
[373] | 559 | |
---|
| 560 | /** Evaluates the contribution of the candidate split plane. |
---|
| 561 | |
---|
[384] | 562 | @param candidatePlane the candidate split plane |
---|
[373] | 563 | @param polys the polygons the split can be based on |
---|
| 564 | @param rays the rays the split can be based on |
---|
[384] | 565 | |
---|
[373] | 566 | @returns the cost of the candidate split plane |
---|
| 567 | */ |
---|
[384] | 568 | float SplitPlaneCost(const Plane3 &candidatePlane, |
---|
[400] | 569 | BspTraversalData &data) const; |
---|
[373] | 570 | |
---|
| 571 | /** Strategies where the effect of the split plane is tested |
---|
| 572 | on all input rays. |
---|
| 573 | @returns the cost of the candidate split plane |
---|
| 574 | */ |
---|
[384] | 575 | float SplitPlaneCost(const Plane3 &candidatePlane, |
---|
[373] | 576 | const PolygonContainer &polys) const; |
---|
| 577 | |
---|
| 578 | /** Strategies where the effect of the split plane is tested |
---|
[390] | 579 | on all input rays. |
---|
[384] | 580 | |
---|
[373] | 581 | @returns the cost of the candidate split plane |
---|
| 582 | */ |
---|
[390] | 583 | float SplitPlaneCost(const Plane3 &candidatePlane, |
---|
[384] | 584 | const BoundedRayContainer &rays, |
---|
[442] | 585 | const int pvs, |
---|
| 586 | const float area, |
---|
| 587 | const BspNodeGeometry &cell) const; |
---|
[373] | 588 | |
---|
| 589 | /** Filters next view cell down the tree and inserts it into the appropriate leaves |
---|
| 590 | (i.e., possibly more than one leaf). |
---|
| 591 | */ |
---|
| 592 | void InsertViewCell(ViewCell *viewCell); |
---|
| 593 | /** Inserts polygons down the tree. The polygons are filtered until a leaf is reached, |
---|
| 594 | then further subdivided. |
---|
| 595 | */ |
---|
| 596 | void InsertPolygons(PolygonContainer *polys); |
---|
| 597 | |
---|
| 598 | /** Subdivide leaf. |
---|
| 599 | @param leaf the leaf to be subdivided |
---|
| 600 | |
---|
| 601 | @param polys the polygons to be split |
---|
| 602 | @param frontPolys returns the polygons in front of the split plane |
---|
| 603 | @param backPolys returns the polygons in the back of the split plane |
---|
| 604 | |
---|
| 605 | @param rays the polygons to be filtered |
---|
| 606 | @param frontRays returns the polygons in front of the split plane |
---|
| 607 | @param backRays returns the polygons in the back of the split plane |
---|
| 608 | |
---|
| 609 | @returns the root of the subdivision |
---|
| 610 | */ |
---|
| 611 | |
---|
[390] | 612 | BspInterior *SubdivideNode(BspTraversalData &tData, |
---|
| 613 | BspTraversalData &frontData, |
---|
| 614 | BspTraversalData &backData, |
---|
| 615 | PolygonContainer &coincident); |
---|
| 616 | |
---|
[373] | 617 | /** Filters polygons down the tree. |
---|
| 618 | @param node the current BSP node |
---|
| 619 | @param polys the polygons to be filtered |
---|
| 620 | @param frontPolys returns the polygons in front of the split plane |
---|
| 621 | @param backPolys returns the polygons in the back of the split plane |
---|
| 622 | */ |
---|
| 623 | void FilterPolygons(BspInterior *node, |
---|
| 624 | PolygonContainer *polys, |
---|
| 625 | PolygonContainer *frontPolys, |
---|
| 626 | PolygonContainer *backPolys); |
---|
| 627 | |
---|
| 628 | /** Selects the split plane in order to construct a tree with |
---|
| 629 | certain characteristics (e.g., balanced tree, least splits, |
---|
| 630 | 2.5d aligned) |
---|
| 631 | @param polygons container of polygons |
---|
| 632 | @param rays bundle of rays on which the split can be based |
---|
| 633 | */ |
---|
[383] | 634 | Plane3 SelectPlaneHeuristics(BspLeaf *leaf, |
---|
[400] | 635 | BspTraversalData &data); |
---|
[373] | 636 | |
---|
| 637 | /** Extracts the meshes of the objects and adds them to polygons. |
---|
| 638 | Adds object aabb to the aabb of the tree. |
---|
| 639 | @param maxPolys the maximal number of objects to be stored as polygons |
---|
| 640 | @returns the number of polygons |
---|
| 641 | */ |
---|
| 642 | int AddToPolygonSoup(const ObjectContainer &objects, |
---|
| 643 | PolygonContainer &polys, |
---|
| 644 | int maxObjects = 0); |
---|
| 645 | |
---|
| 646 | /** Extracts the meshes of the view cells and and adds them to polygons. |
---|
| 647 | Adds view cell aabb to the aabb of the tree. |
---|
| 648 | @param maxPolys the maximal number of objects to be stored as polygons |
---|
| 649 | @returns the number of polygons |
---|
| 650 | */ |
---|
| 651 | int AddToPolygonSoup(const ViewCellContainer &viewCells, |
---|
| 652 | PolygonContainer &polys, |
---|
| 653 | int maxObjects = 0); |
---|
| 654 | |
---|
| 655 | /** Extract polygons of this mesh and add to polygon container. |
---|
| 656 | @param mesh the mesh that drives the polygon construction |
---|
| 657 | @param parent the parent intersectable this polygon is constructed from |
---|
| 658 | @returns number of polygons |
---|
| 659 | */ |
---|
| 660 | int AddMeshToPolygons(Mesh *mesh, PolygonContainer &polys, MeshInstance *parent); |
---|
| 661 | |
---|
| 662 | /** returns next candidate index and reorders polygons so no candidate is chosen two times |
---|
| 663 | @param the current candidate index |
---|
| 664 | @param max the range of candidates |
---|
| 665 | */ |
---|
| 666 | int GetNextCandidateIdx(int currentIdx, PolygonContainer &polys); |
---|
| 667 | |
---|
| 668 | /** Helper function which extracts a view cell on the front and the back |
---|
| 669 | of the split plane. |
---|
| 670 | @param backViewCell returns view cell on the back of the split plane |
---|
| 671 | @param frontViewCell returns a view cell on the front of the split plane |
---|
| 672 | @param coincident container of polygons coincident to the split plane |
---|
| 673 | @param splitPlane the split plane which decides about back and front |
---|
| 674 | @param extractBack if a back view cell is extracted |
---|
| 675 | @param extractFront if a front view cell is extracted |
---|
| 676 | */ |
---|
[390] | 677 | void ExtractViewCells(BspTraversalData &frontData, |
---|
| 678 | BspTraversalData &backData, |
---|
[373] | 679 | const PolygonContainer &coincident, |
---|
[437] | 680 | const Plane3 &splitPlane) const; |
---|
[373] | 681 | |
---|
| 682 | /** Computes best cost ratio for the suface area heuristics for axis aligned |
---|
| 683 | splits. This heuristics minimizes the cost for ray traversal. |
---|
| 684 | @param polys the polygons guiding the ratio computation |
---|
| 685 | @param box the bounding box of the leaf |
---|
| 686 | @param axis the current split axis |
---|
| 687 | @param position returns the split position |
---|
| 688 | @param objectsBack the number of objects in the back of the split plane |
---|
| 689 | @param objectsFront the number of objects in the front of the split plane |
---|
| 690 | */ |
---|
| 691 | float BestCostRatio(const PolygonContainer &polys, |
---|
| 692 | const AxisAlignedBox3 &box, |
---|
| 693 | const int axis, |
---|
| 694 | float &position, |
---|
| 695 | int &objectsBack, |
---|
| 696 | int &objectsFront) const; |
---|
| 697 | |
---|
| 698 | /** Sorts split candidates for surface area heuristics for axis aligned splits. |
---|
| 699 | @param polys the input for choosing split candidates |
---|
| 700 | @param axis the current split axis |
---|
| 701 | @param splitCandidates returns sorted list of split candidates |
---|
| 702 | */ |
---|
| 703 | void SortSplitCandidates(const PolygonContainer &polys, |
---|
| 704 | const int axis, |
---|
| 705 | vector<SortableEntry> &splitCandidates) const; |
---|
| 706 | |
---|
| 707 | /** Selects an axis aligned split plane. |
---|
| 708 | Returns true if split is valied |
---|
| 709 | */ |
---|
| 710 | bool SelectAxisAlignedPlane(Plane3 &plane, const PolygonContainer &polys) const; |
---|
| 711 | |
---|
| 712 | /** Subdivides the rays into front and back rays according to the split plane. |
---|
| 713 | |
---|
| 714 | @param plane the split plane |
---|
| 715 | @param rays contains the rays to be split. The rays are |
---|
| 716 | distributed into front and back rays. |
---|
| 717 | @param frontRays returns rays on the front side of the plane |
---|
| 718 | @param backRays returns rays on the back side of the plane |
---|
| 719 | |
---|
| 720 | @returns the number of splits |
---|
| 721 | */ |
---|
| 722 | int SplitRays(const Plane3 &plane, |
---|
| 723 | BoundedRayContainer &rays, |
---|
| 724 | BoundedRayContainer &frontRays, |
---|
| 725 | BoundedRayContainer &backRays); |
---|
| 726 | |
---|
| 727 | |
---|
| 728 | /** Extracts the split planes representing the space bounded by node n. |
---|
| 729 | */ |
---|
[396] | 730 | void ExtractHalfSpaces(BspNode *n, vector<Plane3> &halfSpaces) const; |
---|
[373] | 731 | |
---|
[428] | 732 | /** Adds the object to the pvs of the front and back leaf with a given classification. |
---|
| 733 | |
---|
| 734 | @param obj the object to be added |
---|
| 735 | @param cf the ray classification regarding the split plane |
---|
| 736 | @param frontPvs returns the PVS of the front partition |
---|
| 737 | @param backPvs returns the PVS of the back partition |
---|
| 738 | |
---|
[379] | 739 | */ |
---|
[428] | 740 | void AddObjToPvs(Intersectable *obj, const int cf, int &frontPvs, int &backPvs) const; |
---|
[437] | 741 | |
---|
| 742 | /** Computes PVS size induced by the rays. |
---|
| 743 | */ |
---|
[384] | 744 | int ComputePvsSize(const BoundedRayContainer &rays) const; |
---|
| 745 | |
---|
[437] | 746 | /** Returns true if tree can be terminated. |
---|
| 747 | */ |
---|
[420] | 748 | inline bool TerminationCriteriaMet(const BspTraversalData &data) const; |
---|
[397] | 749 | |
---|
[437] | 750 | /** Computes accumulated ray lenght of this rays. |
---|
| 751 | */ |
---|
[409] | 752 | float AccumulatedRayLength(BoundedRayContainer &rays) const; |
---|
| 753 | |
---|
[448] | 754 | /** Splits polygons with respect to the split plane. |
---|
| 755 | @param polys the polygons to be split. the polygons are consumed and |
---|
| 756 | distributed to the containers frontPolys, backPolys, coincident. |
---|
| 757 | @param frontPolys returns the polygons in the front of the split plane |
---|
| 758 | @param backPolys returns the polygons in the back of the split plane |
---|
| 759 | @param coincident returns the polygons coincident to the split plane |
---|
| 760 | |
---|
| 761 | @returns the number of splits |
---|
| 762 | */ |
---|
| 763 | int SplitPolygons(const Plane3 &plane, |
---|
| 764 | PolygonContainer &polys, |
---|
| 765 | PolygonContainer &frontPolys, |
---|
| 766 | PolygonContainer &backPolys, |
---|
| 767 | PolygonContainer &coincident) const; |
---|
| 768 | |
---|
| 769 | /** Adds ray sample contributions to the PVS. |
---|
| 770 | @param sampleContributions the number contributions of the samples |
---|
| 771 | @param contributingSampels the number of contributing rays |
---|
| 772 | |
---|
| 773 | */ |
---|
| 774 | void AddToPvs(BspLeaf *leaf, |
---|
| 775 | const BoundedRayContainer &rays, |
---|
| 776 | int &sampleContributions, |
---|
| 777 | int &contributingSamples); |
---|
| 778 | |
---|
[437] | 779 | /// Pointer to the root of the tree. |
---|
[373] | 780 | BspNode *mRoot; |
---|
[437] | 781 | |
---|
| 782 | /// Stores statistics during traversal. |
---|
[373] | 783 | BspTreeStatistics mStat; |
---|
| 784 | |
---|
| 785 | /// Strategies for choosing next split plane. |
---|
| 786 | enum {NO_STRATEGY = 0, |
---|
| 787 | RANDOM_POLYGON = 1, |
---|
| 788 | AXIS_ALIGNED = 2, |
---|
| 789 | LEAST_SPLITS = 4, |
---|
| 790 | BALANCED_POLYS = 8, |
---|
| 791 | BALANCED_VIEW_CELLS = 16, |
---|
| 792 | LARGEST_POLY_AREA = 32, |
---|
| 793 | VERTICAL_AXIS = 64, |
---|
| 794 | BLOCKED_RAYS = 128, |
---|
| 795 | LEAST_RAY_SPLITS = 256, |
---|
| 796 | BALANCED_RAYS = 512, |
---|
| 797 | PVS = 1024 |
---|
| 798 | }; |
---|
| 799 | |
---|
| 800 | /// box around the whole view domain |
---|
| 801 | AxisAlignedBox3 mBox; |
---|
| 802 | |
---|
| 803 | /// view cell corresponding to unbounded space |
---|
[392] | 804 | BspViewCell *mRootCell; |
---|
[373] | 805 | |
---|
[437] | 806 | /// if view cells should be generated or the given view cells should be used. |
---|
[373] | 807 | bool mGenerateViewCells; |
---|
| 808 | |
---|
| 809 | /// maximal number of polygons before subdivision termination |
---|
[426] | 810 | int mTermMinPolys; |
---|
[373] | 811 | /// maximal number of rays before subdivision termination |
---|
[426] | 812 | int mTermMinRays; |
---|
[373] | 813 | /// maximal possible depth |
---|
[420] | 814 | int mTermMaxDepth; |
---|
[397] | 815 | /// mininum area |
---|
[420] | 816 | float mTermMinArea; |
---|
[397] | 817 | /// mininum PVS |
---|
[420] | 818 | int mTermMinPvs; |
---|
[426] | 819 | |
---|
| 820 | /// minimal number of polygons for axis aligned split |
---|
| 821 | int mTermMinPolysForAxisAligned; |
---|
| 822 | /// minimal number of rays for axis aligned split |
---|
| 823 | int mTermMinRaysForAxisAligned; |
---|
| 824 | /// minimal number of objects for axis aligned split |
---|
| 825 | int mTermMinObjectsForAxisAligned; |
---|
| 826 | /// maximal contribution per ray |
---|
| 827 | float mTermMaxRayContribution; |
---|
| 828 | /// minimal accumulated ray length |
---|
| 829 | float mTermMinAccRayLength; |
---|
| 830 | |
---|
| 831 | |
---|
[373] | 832 | /// strategy to get the best split plane |
---|
[420] | 833 | int mSplitPlaneStrategy; |
---|
[373] | 834 | /// number of candidates evaluated for the next split plane |
---|
[420] | 835 | int mMaxPolyCandidates; |
---|
| 836 | /// number of candidates for split planes evaluated using the rays |
---|
| 837 | int mMaxRayCandidates; |
---|
[441] | 838 | /// maximum tests for split plane evaluation with a single candidate |
---|
| 839 | int mMaxTests; |
---|
| 840 | |
---|
[424] | 841 | float mCtDivCi; |
---|
| 842 | |
---|
[373] | 843 | /// axis aligned split criteria |
---|
[424] | 844 | float mAaCtDivCi; |
---|
[420] | 845 | float mSplitBorder; |
---|
| 846 | float mMaxCostRatio; |
---|
[373] | 847 | |
---|
| 848 | // factors guiding the split plane heuristics |
---|
[421] | 849 | float mVerticalSplitsFactor; |
---|
| 850 | float mLargestPolyAreaFactor; |
---|
| 851 | float mBlockedRaysFactor; |
---|
| 852 | float mLeastRaySplitsFactor; |
---|
| 853 | float mBalancedRaysFactor; |
---|
| 854 | float mPvsFactor; |
---|
[424] | 855 | float mLeastSplitsFactor; |
---|
| 856 | float mBalancedPolysFactor; |
---|
| 857 | float mBalancedViewCellsFactor; |
---|
[373] | 858 | |
---|
[420] | 859 | /// if area or accumulated ray lenght should be used for PVS heuristics |
---|
[442] | 860 | bool mPvsUseArea; |
---|
[373] | 861 | |
---|
[448] | 862 | /// epsilon where two points are still considered equal |
---|
| 863 | float mEpsilon; |
---|
| 864 | |
---|
[373] | 865 | private: |
---|
[428] | 866 | |
---|
[373] | 867 | /** Evaluates split plane classification with respect to the plane's |
---|
| 868 | contribution for a balanced tree. |
---|
| 869 | */ |
---|
[421] | 870 | static const float sLeastPolySplitsTable[4]; |
---|
[373] | 871 | /** Evaluates split plane classification with respect to the plane's |
---|
| 872 | contribution for a minimum number splits in the tree. |
---|
| 873 | */ |
---|
[421] | 874 | static const float sBalancedPolysTable[4]; |
---|
[373] | 875 | /** Evaluates split plane classification with respect to the plane's |
---|
| 876 | contribution for a minimum number of ray splits. |
---|
| 877 | */ |
---|
[421] | 878 | static const float sLeastRaySplitsTable[5]; |
---|
[373] | 879 | /** Evaluates split plane classification with respect to the plane's |
---|
| 880 | contribution for balanced rays. |
---|
| 881 | */ |
---|
[421] | 882 | static const float sBalancedRaysTable[5]; |
---|
[373] | 883 | |
---|
[428] | 884 | /// Generates unique ids for PVS criterium |
---|
| 885 | static void GenerateUniqueIdsForPvs(); |
---|
| 886 | |
---|
| 887 | //-- unique ids for PVS criterium |
---|
| 888 | static int sFrontId; |
---|
| 889 | static int sBackId; |
---|
| 890 | static int sFrontAndBackId; |
---|
[373] | 891 | }; |
---|
| 892 | |
---|
| 893 | #endif |
---|