[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> |
---|
| 8 | |
---|
| 9 | class ViewCell; |
---|
| 10 | class BspViewCell; |
---|
| 11 | class Plane3; |
---|
| 12 | class BspTree; |
---|
| 13 | class BspInterior; |
---|
[390] | 14 | //class Polygon3; |
---|
[373] | 15 | class AxisAlignedBox3; |
---|
| 16 | class Ray; |
---|
| 17 | |
---|
[390] | 18 | class BspNodeGeometry |
---|
| 19 | { |
---|
| 20 | public: |
---|
| 21 | BspNodeGeometry() |
---|
| 22 | {}; |
---|
| 23 | |
---|
| 24 | BspNodeGeometry(const PolygonContainer &polys, |
---|
| 25 | const vector<Plane3> &planes, |
---|
| 26 | const vector<bool> &sides); |
---|
| 27 | BspNodeGeometry(const vector<Plane3> &planes, |
---|
| 28 | const vector<bool> &sides); |
---|
| 29 | |
---|
| 30 | ~BspNodeGeometry(); |
---|
| 31 | |
---|
| 32 | float GetArea() const; |
---|
| 33 | |
---|
| 34 | /** Computes new cell based on the old cell definition and a new split plane |
---|
| 35 | @param side indicates which side of the halfspace |
---|
| 36 | @returns true if plane contributes to the cell. |
---|
| 37 | */ |
---|
| 38 | BspNodeGeometry *ConstructChild(const BspTree &tree, |
---|
| 39 | const Plane3 &splitPlane, |
---|
| 40 | const bool side) const; |
---|
| 41 | |
---|
| 42 | vector<Plane3> mPlanes; |
---|
| 43 | vector<bool> mSides; |
---|
| 44 | PolygonContainer mPolys; |
---|
| 45 | }; |
---|
| 46 | |
---|
[373] | 47 | /** Data structure used for optimized ray casting. |
---|
| 48 | */ |
---|
| 49 | struct BspRayTraversalData |
---|
| 50 | { |
---|
| 51 | BspNode *mNode; |
---|
| 52 | Vector3 mExitPoint; |
---|
| 53 | float mMaxT; |
---|
| 54 | |
---|
| 55 | BspRayTraversalData() {} |
---|
| 56 | |
---|
| 57 | BspRayTraversalData(BspNode *n, const Vector3 &extp, const float maxt): |
---|
| 58 | mNode(n), mExitPoint(extp), mMaxT(maxt) |
---|
| 59 | {} |
---|
| 60 | }; |
---|
| 61 | |
---|
| 62 | /** Data used for passing ray data down the tree. |
---|
| 63 | */ |
---|
| 64 | struct BoundedRay |
---|
| 65 | { |
---|
| 66 | Ray *mRay; |
---|
| 67 | float mMinT; |
---|
| 68 | float mMaxT; |
---|
| 69 | |
---|
| 70 | BoundedRay(): mMinT(0), mMaxT(1e6), mRay(NULL) |
---|
| 71 | {} |
---|
| 72 | BoundedRay(Ray *r, float minT, float maxT): |
---|
| 73 | mRay(r), mMinT(minT), mMaxT(maxT) |
---|
| 74 | {} |
---|
| 75 | }; |
---|
| 76 | |
---|
| 77 | typedef vector<BoundedRay *> BoundedRayContainer; |
---|
| 78 | |
---|
| 79 | class BspTreeStatistics |
---|
| 80 | { |
---|
| 81 | public: |
---|
| 82 | // total number of nodes |
---|
| 83 | int nodes; |
---|
| 84 | // number of splits |
---|
| 85 | int splits; |
---|
| 86 | // totals number of rays |
---|
| 87 | int rays; |
---|
| 88 | // maximal reached depth |
---|
| 89 | int maxDepth; |
---|
| 90 | // minimal depth |
---|
| 91 | int minDepth; |
---|
| 92 | // max depth nodes |
---|
| 93 | int maxDepthNodes; |
---|
| 94 | // max number of rays per node |
---|
| 95 | int maxObjectRefs; |
---|
| 96 | // accumulated depth (used to compute average) |
---|
| 97 | int accumDepth; |
---|
| 98 | // number of initial polygons |
---|
| 99 | int polys; |
---|
| 100 | /// samples contributing to pvs |
---|
| 101 | int contributingSamples; |
---|
| 102 | /// sample contributions to pvs |
---|
| 103 | int sampleContributions; |
---|
[375] | 104 | /// largest pvs |
---|
| 105 | int largestPvs; |
---|
[373] | 106 | |
---|
| 107 | // Constructor |
---|
| 108 | BspTreeStatistics() |
---|
| 109 | { |
---|
| 110 | Reset(); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | int Nodes() const {return nodes;} |
---|
| 114 | int Interior() const { return nodes / 2; } |
---|
| 115 | int Leaves() const { return (nodes / 2) + 1; } |
---|
| 116 | double AvgDepth() const { return accumDepth / (double)Leaves();}; // TODO: computation wrong |
---|
| 117 | |
---|
| 118 | void Reset() |
---|
| 119 | { |
---|
| 120 | nodes = 0; |
---|
| 121 | splits = 0; |
---|
| 122 | maxDepthNodes = 0; |
---|
| 123 | maxDepth = 0; |
---|
| 124 | minDepth = 99999; |
---|
| 125 | polys = 0; |
---|
| 126 | accumDepth = 0; |
---|
[375] | 127 | |
---|
[373] | 128 | contributingSamples = 0; |
---|
| 129 | sampleContributions = 0; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | void |
---|
| 133 | Print(ostream &app) const; |
---|
| 134 | |
---|
| 135 | friend ostream &operator<<(ostream &s, const BspTreeStatistics &stat) { |
---|
| 136 | stat.Print(s); |
---|
| 137 | return s; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | }; |
---|
| 141 | |
---|
[375] | 142 | class BspViewCellsStatistics |
---|
| 143 | { |
---|
| 144 | public: |
---|
| 145 | |
---|
| 146 | /// number of view cells |
---|
| 147 | int viewCells; |
---|
| 148 | |
---|
| 149 | /// size of the PVS |
---|
| 150 | int pvs; |
---|
| 151 | |
---|
| 152 | /// largest PVS of all view cells |
---|
| 153 | int maxPvs; |
---|
| 154 | |
---|
| 155 | /// smallest PVS of all view cells |
---|
| 156 | int minPvs; |
---|
| 157 | |
---|
| 158 | /// view cells with empty PVS |
---|
| 159 | int emptyPvs; |
---|
| 160 | |
---|
| 161 | /// number of bsp leaves covering the view space |
---|
| 162 | int bspLeaves; |
---|
| 163 | |
---|
| 164 | /// largest number of leaves covered by one view cell |
---|
| 165 | int maxBspLeaves; |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | // Constructor |
---|
| 169 | BspViewCellsStatistics() |
---|
| 170 | { |
---|
| 171 | Reset(); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | double AvgBspLeaves() const {return (double)bspLeaves / (double)viewCells;}; |
---|
| 175 | double AvgPvs() const {return (double)pvs / (double)viewCells;}; |
---|
| 176 | |
---|
| 177 | |
---|
| 178 | void Reset() |
---|
| 179 | { |
---|
| 180 | viewCells = 0; |
---|
| 181 | pvs = 0; |
---|
| 182 | maxPvs = 0; |
---|
| 183 | minPvs = 999999; |
---|
| 184 | emptyPvs = 0; |
---|
| 185 | bspLeaves = 0; |
---|
| 186 | maxBspLeaves = 0; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | void |
---|
| 190 | Print(ostream &app) const; |
---|
| 191 | |
---|
| 192 | friend ostream &operator<<(ostream &s, const BspViewCellsStatistics &stat) { |
---|
| 193 | stat.Print(s); |
---|
| 194 | return s; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | }; |
---|
| 198 | |
---|
[373] | 199 | /** |
---|
| 200 | BspNode abstract class serving for interior and leaf node implementation |
---|
| 201 | */ |
---|
| 202 | class BspNode |
---|
| 203 | { |
---|
| 204 | friend class BspTree; |
---|
| 205 | |
---|
| 206 | public: |
---|
| 207 | BspNode(); |
---|
[390] | 208 | virtual ~BspNode(){}; |
---|
[373] | 209 | BspNode(BspInterior *parent); |
---|
| 210 | |
---|
| 211 | /** Determines whether this node is a leaf or not |
---|
| 212 | @return true if leaf |
---|
| 213 | */ |
---|
| 214 | virtual bool IsLeaf() const = 0; |
---|
| 215 | |
---|
| 216 | /** Determines whether this node is a root |
---|
| 217 | @return true if root |
---|
| 218 | */ |
---|
| 219 | virtual bool IsRoot() const; |
---|
| 220 | |
---|
| 221 | /** Returns parent node. |
---|
| 222 | */ |
---|
| 223 | BspInterior *GetParent(); |
---|
[390] | 224 | |
---|
[373] | 225 | /** Sets parent node. |
---|
| 226 | */ |
---|
| 227 | void SetParent(BspInterior *parent); |
---|
| 228 | |
---|
[390] | 229 | |
---|
[373] | 230 | static int mailID; |
---|
| 231 | int mailbox; |
---|
| 232 | |
---|
| 233 | void Mail() { mailbox = mailID; } |
---|
| 234 | static void NewMail() { mailID++; } |
---|
| 235 | bool Mailed() const { return mailbox == mailID; } |
---|
| 236 | |
---|
| 237 | protected: |
---|
| 238 | |
---|
| 239 | /// parent of this node |
---|
| 240 | BspInterior *mParent; |
---|
| 241 | }; |
---|
| 242 | |
---|
| 243 | /** BSP interior node implementation |
---|
| 244 | */ |
---|
| 245 | class BspInterior : public BspNode |
---|
| 246 | { |
---|
| 247 | friend class BspTree; |
---|
| 248 | public: |
---|
| 249 | /** Standard contructor taking split plane as argument. |
---|
| 250 | */ |
---|
| 251 | BspInterior(const Plane3 &plane); |
---|
| 252 | /** @return false since it is an interior node |
---|
| 253 | */ |
---|
| 254 | bool IsLeaf() const; |
---|
| 255 | |
---|
| 256 | BspNode *GetBack(); |
---|
| 257 | BspNode *GetFront(); |
---|
| 258 | |
---|
| 259 | Plane3 *GetPlane(); |
---|
| 260 | |
---|
| 261 | void ReplaceChildLink(BspNode *oldChild, BspNode *newChild); |
---|
| 262 | void SetupChildLinks(BspNode *b, BspNode *f); |
---|
| 263 | |
---|
| 264 | /** Splits polygons with respect to the split plane. |
---|
| 265 | @param polys the polygons to be split. the polygons are consumed and |
---|
| 266 | distributed to the containers frontPolys, backPolys, coincident. |
---|
| 267 | @param frontPolys returns the polygons in the front of the split plane |
---|
| 268 | @param backPolys returns the polygons in the back of the split plane |
---|
| 269 | @param coincident returns the polygons coincident to the split plane |
---|
[390] | 270 | |
---|
[373] | 271 | @returns the number of splits |
---|
| 272 | */ |
---|
| 273 | int SplitPolygons(PolygonContainer &polys, |
---|
| 274 | PolygonContainer &frontPolys, |
---|
| 275 | PolygonContainer &backPolys, |
---|
[390] | 276 | PolygonContainer &coincident); |
---|
[373] | 277 | |
---|
| 278 | friend ostream &operator<<(ostream &s, const BspInterior &A) |
---|
| 279 | { |
---|
| 280 | return s << A.mPlane; |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | protected: |
---|
| 284 | |
---|
| 285 | /// Splitting plane corresponding to this node |
---|
| 286 | Plane3 mPlane; |
---|
| 287 | /// back node |
---|
| 288 | BspNode *mBack; |
---|
| 289 | /// front node |
---|
| 290 | BspNode *mFront; |
---|
| 291 | }; |
---|
| 292 | |
---|
| 293 | /** BSP leaf node implementation. |
---|
| 294 | */ |
---|
| 295 | class BspLeaf : public BspNode |
---|
| 296 | { |
---|
| 297 | friend class BspTree; |
---|
| 298 | |
---|
| 299 | public: |
---|
| 300 | BspLeaf(); |
---|
| 301 | BspLeaf(BspViewCell *viewCell); |
---|
| 302 | BspLeaf(BspInterior *parent); |
---|
| 303 | BspLeaf(BspInterior *parent, BspViewCell *viewCell); |
---|
| 304 | |
---|
| 305 | /** @return true since it is an interior node |
---|
| 306 | */ |
---|
| 307 | bool IsLeaf() const; |
---|
| 308 | |
---|
| 309 | /** Returns pointer of view cell. |
---|
| 310 | */ |
---|
| 311 | BspViewCell *GetViewCell() const; |
---|
| 312 | |
---|
| 313 | /** Sets pointer to view cell. |
---|
| 314 | */ |
---|
| 315 | void SetViewCell(BspViewCell *viewCell); |
---|
| 316 | |
---|
| 317 | /** Generates new view cell and adds rays to the PVS. |
---|
| 318 | @param sampleContributions the number contributions of the sampels |
---|
| 319 | @param contributingSampels the number of contributing rays |
---|
| 320 | |
---|
| 321 | */ |
---|
| 322 | void GenerateViewCell(const BoundedRayContainer &rays, |
---|
| 323 | int &sampleContributions, |
---|
| 324 | int &contributingSamples); |
---|
| 325 | |
---|
| 326 | protected: |
---|
| 327 | |
---|
| 328 | /// if NULL this does not correspond to feasible viewcell |
---|
| 329 | BspViewCell *mViewCell; |
---|
| 330 | }; |
---|
| 331 | |
---|
| 332 | /** Implementation of the view cell BSP tree. |
---|
| 333 | */ |
---|
| 334 | class BspTree |
---|
| 335 | { |
---|
| 336 | public: |
---|
| 337 | |
---|
| 338 | /** Additional data which is passed down the BSP tree during traversal. |
---|
| 339 | */ |
---|
| 340 | struct BspTraversalData |
---|
| 341 | { |
---|
| 342 | /// the current node |
---|
| 343 | BspNode *mNode; |
---|
| 344 | /// polygonal data for splitting |
---|
| 345 | PolygonContainer *mPolygons; |
---|
| 346 | /// current depth |
---|
| 347 | int mDepth; |
---|
| 348 | /// the view cell associated with this subdivsion |
---|
| 349 | ViewCell *mViewCell; |
---|
| 350 | /// rays piercing this node |
---|
| 351 | BoundedRayContainer *mRays; |
---|
[390] | 352 | /// area of current node |
---|
| 353 | float mArea; |
---|
| 354 | BspNodeGeometry *mCell; |
---|
[373] | 355 | |
---|
[390] | 356 | /// pvs size |
---|
| 357 | int mPvs; |
---|
| 358 | |
---|
[373] | 359 | BspTraversalData(): |
---|
| 360 | mNode(NULL), |
---|
| 361 | mPolygons(NULL), |
---|
| 362 | mDepth(0), |
---|
| 363 | mViewCell(NULL), |
---|
[390] | 364 | mRays(NULL), |
---|
| 365 | mPvs(0), |
---|
| 366 | mArea(0.0), |
---|
| 367 | mCell(NULL) |
---|
[373] | 368 | {} |
---|
| 369 | |
---|
| 370 | BspTraversalData(BspNode *node, |
---|
| 371 | PolygonContainer *polys, |
---|
| 372 | const int depth, |
---|
| 373 | ViewCell *viewCell, |
---|
[390] | 374 | BoundedRayContainer *rays, |
---|
| 375 | int pvs, |
---|
| 376 | float area, |
---|
| 377 | BspNodeGeometry *cell): |
---|
[373] | 378 | mNode(node), |
---|
| 379 | mPolygons(polys), |
---|
| 380 | mDepth(depth), |
---|
| 381 | mViewCell(viewCell), |
---|
[390] | 382 | mRays(rays), |
---|
| 383 | mPvs(pvs), |
---|
| 384 | mArea(area), |
---|
| 385 | mCell(cell) |
---|
[373] | 386 | {} |
---|
| 387 | }; |
---|
| 388 | |
---|
| 389 | typedef std::stack<BspTraversalData> BspTraversalStack; |
---|
| 390 | |
---|
| 391 | /** Default constructor creating an empty tree. |
---|
| 392 | @param viewCell view cell corresponding to unbounded space |
---|
| 393 | */ |
---|
| 394 | BspTree(ViewCell *viewCell); |
---|
| 395 | |
---|
| 396 | ~BspTree(); |
---|
| 397 | |
---|
| 398 | const BspTreeStatistics &GetStatistics() const; |
---|
| 399 | |
---|
| 400 | /** Constructs tree using the given list of view cells. |
---|
| 401 | For this type of construction we filter all view cells down the |
---|
| 402 | tree. If there is no polygon left, the last split plane |
---|
| 403 | decides inside or outside of the viewcell. A pointer to the |
---|
| 404 | appropriate view cell is stored within each leaf. |
---|
| 405 | Many leafs can point to the same viewcell. |
---|
| 406 | */ |
---|
| 407 | void Construct(const ViewCellContainer &viewCells); |
---|
| 408 | |
---|
| 409 | /** Constructs tree using the given list of objects. |
---|
| 410 | @note the objects are not taken as view cells, but the view cells are |
---|
| 411 | constructed from the subdivision: Each leaf is taken as one viewcell. |
---|
| 412 | @param objects list of objects |
---|
| 413 | */ |
---|
| 414 | void Construct(const ObjectContainer &objects); |
---|
| 415 | |
---|
| 416 | /** Constructs the tree from a given set of rays. |
---|
| 417 | @param sampleRays the set of sample rays the construction is based on |
---|
| 418 | @param viewCells if not NULL, new view cells are |
---|
| 419 | created in the leafs and stored in the conatainer |
---|
| 420 | */ |
---|
| 421 | void Construct(const RayContainer &sampleRays); |
---|
| 422 | |
---|
| 423 | /** Returns list of BSP leaves. |
---|
| 424 | */ |
---|
| 425 | void CollectLeaves(vector<BspLeaf *> &leaves) const; |
---|
| 426 | |
---|
| 427 | /** Returns box which bounds the whole tree. |
---|
| 428 | */ |
---|
| 429 | AxisAlignedBox3 GetBoundingBox()const; |
---|
| 430 | |
---|
| 431 | /** Returns root of BSP tree. |
---|
| 432 | */ |
---|
| 433 | BspNode *GetRoot() const; |
---|
| 434 | |
---|
| 435 | /** Exports Bsp tree to file. |
---|
| 436 | */ |
---|
| 437 | bool Export(const string filename); |
---|
| 438 | |
---|
| 439 | /** Collects the leaf view cells of the tree |
---|
| 440 | @param viewCells returns the view cells |
---|
| 441 | */ |
---|
| 442 | void CollectViewCells(ViewCellContainer &viewCells) const; |
---|
| 443 | |
---|
| 444 | /** A ray is cast possible intersecting the tree. |
---|
| 445 | @param the ray that is cast. |
---|
| 446 | @returns the number of intersections with objects stored in the tree. |
---|
| 447 | */ |
---|
| 448 | int CastRay(Ray &ray); |
---|
| 449 | |
---|
| 450 | /** Set to true if new view cells shall be generated in each leaf. |
---|
| 451 | */ |
---|
| 452 | void SetGenerateViewCells(int generateViewCells); |
---|
| 453 | |
---|
| 454 | /// bsp tree construction types |
---|
| 455 | enum {FROM_INPUT_VIEW_CELLS, FROM_SCENE_GEOMETRY, FROM_RAYS}; |
---|
| 456 | |
---|
| 457 | /** Returns statistics. |
---|
| 458 | */ |
---|
| 459 | BspTreeStatistics &GetStat(); |
---|
| 460 | |
---|
| 461 | /** finds neighbouring leaves of this tree node. |
---|
| 462 | */ |
---|
| 463 | int FindNeighbors(BspNode *n, vector<BspLeaf *> &neighbors, |
---|
| 464 | const bool onlyUnmailed) const; |
---|
| 465 | |
---|
| 466 | /** Constructs geometry associated with the half space intersections |
---|
| 467 | leading to this node. |
---|
| 468 | */ |
---|
| 469 | void ConstructGeometry(BspNode *n, PolygonContainer &cell) const; |
---|
| 470 | |
---|
| 471 | /** Construct geometry of view cell. |
---|
| 472 | */ |
---|
| 473 | void ConstructGeometry(BspViewCell *vc, PolygonContainer &cell) const; |
---|
| 474 | |
---|
[390] | 475 | void ConstructGeometry(BspNode *n, BspNodeGeometry &cell) const; |
---|
| 476 | |
---|
[373] | 477 | /** Returns random leaf of BSP tree. |
---|
| 478 | @param halfspace defines the halfspace from which the leaf is taken. |
---|
| 479 | */ |
---|
| 480 | BspLeaf *GetRandomLeaf(const Plane3 &halfspace); |
---|
| 481 | |
---|
| 482 | /** Returns random leaf of BSP tree. |
---|
| 483 | @param onlyUnmailed if only unmailed leaves should be returned. |
---|
| 484 | */ |
---|
| 485 | BspLeaf *GetRandomLeaf(const bool onlyUnmailed = false); |
---|
| 486 | |
---|
| 487 | /** Returns true if merge criteria are reached. |
---|
| 488 | */ |
---|
| 489 | bool ShouldMerge(BspLeaf *front, BspLeaf *back) const; |
---|
| 490 | |
---|
| 491 | /** Merges view cells based on some criteria |
---|
| 492 | E.g., empty view cells can pe purged, view cells which have |
---|
| 493 | a very similar PVS can be merged to one larger view cell. |
---|
| 494 | |
---|
| 495 | @returns true if merge was successful. |
---|
| 496 | */ |
---|
| 497 | bool MergeViewCells(BspLeaf *front, BspLeaf *back) const; |
---|
| 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 | |
---|
| 503 | protected: |
---|
| 504 | |
---|
| 505 | // -------------------------------------------------------------- |
---|
| 506 | // For sorting objects |
---|
| 507 | // -------------------------------------------------------------- |
---|
| 508 | struct SortableEntry |
---|
| 509 | { |
---|
| 510 | enum {POLY_MIN, POLY_MAX}; |
---|
| 511 | |
---|
| 512 | int type; |
---|
| 513 | float value; |
---|
| 514 | Polygon3 *poly; |
---|
| 515 | SortableEntry() {} |
---|
| 516 | SortableEntry(const int t, const float v, Polygon3 *poly): |
---|
| 517 | type(t), value(v), poly(poly) {} |
---|
| 518 | |
---|
| 519 | bool operator<(const SortableEntry &b) const |
---|
| 520 | { |
---|
| 521 | return value < b.value; |
---|
| 522 | } |
---|
| 523 | }; |
---|
| 524 | |
---|
| 525 | /** Evaluates tree stats in the BSP tree leafs. |
---|
| 526 | */ |
---|
| 527 | void EvaluateLeafStats(const BspTraversalData &data); |
---|
| 528 | |
---|
| 529 | /** Subdivides node with respect to the traversal data. |
---|
| 530 | @param tStack current traversal stack |
---|
| 531 | @param tData traversal data also holding node to be subdivided |
---|
| 532 | @returns new root of the subtree |
---|
| 533 | */ |
---|
| 534 | BspNode *Subdivide(BspTraversalStack &tStack, BspTraversalData &tData); |
---|
| 535 | |
---|
| 536 | /** Constructs the tree from the given list of polygons and rays. |
---|
| 537 | @param polys stores set of polygons on which subdivision may be based |
---|
| 538 | @param rays storesset of rays on which subdivision may be based |
---|
| 539 | */ |
---|
| 540 | void Construct(PolygonContainer *polys, BoundedRayContainer *rays); |
---|
| 541 | |
---|
| 542 | /** Selects the best possible splitting plane. |
---|
| 543 | @param leaf the leaf to be split |
---|
| 544 | @param polys the polygon list on which the split decition is based |
---|
| 545 | @param rays ray container on which selection may be based |
---|
| 546 | @note the polygons can be reordered in the process |
---|
| 547 | @returns the split plane |
---|
| 548 | */ |
---|
| 549 | Plane3 SelectPlane(BspLeaf *leaf, |
---|
[390] | 550 | BspTraversalData &data, |
---|
| 551 | BspTraversalData &frontData, |
---|
| 552 | BspTraversalData &backData); |
---|
[373] | 553 | |
---|
| 554 | /** Evaluates the contribution of the candidate split plane. |
---|
| 555 | |
---|
[384] | 556 | @param candidatePlane the candidate split plane |
---|
[373] | 557 | @param polys the polygons the split can be based on |
---|
| 558 | @param rays the rays the split can be based on |
---|
[384] | 559 | |
---|
[373] | 560 | @returns the cost of the candidate split plane |
---|
| 561 | */ |
---|
[384] | 562 | float SplitPlaneCost(const Plane3 &candidatePlane, |
---|
[390] | 563 | BspTraversalData &data, |
---|
| 564 | BspTraversalData &frontData, |
---|
| 565 | BspTraversalData &backData) const; |
---|
[373] | 566 | |
---|
| 567 | /** Strategies where the effect of the split plane is tested |
---|
| 568 | on all input rays. |
---|
| 569 | @returns the cost of the candidate split plane |
---|
| 570 | */ |
---|
[384] | 571 | float SplitPlaneCost(const Plane3 &candidatePlane, |
---|
[373] | 572 | const PolygonContainer &polys) const; |
---|
| 573 | |
---|
| 574 | /** Strategies where the effect of the split plane is tested |
---|
[390] | 575 | on all input rays. |
---|
[384] | 576 | |
---|
[373] | 577 | @returns the cost of the candidate split plane |
---|
| 578 | */ |
---|
[390] | 579 | float SplitPlaneCost(const Plane3 &candidatePlane, |
---|
[384] | 580 | const BoundedRayContainer &rays, |
---|
[390] | 581 | const int pvs, |
---|
| 582 | const float area, |
---|
| 583 | const BspNodeGeometry &cell, |
---|
| 584 | BspTraversalData &frontData, |
---|
| 585 | BspTraversalData &backData) const; |
---|
[373] | 586 | |
---|
| 587 | /** Filters next view cell down the tree and inserts it into the appropriate leaves |
---|
| 588 | (i.e., possibly more than one leaf). |
---|
| 589 | */ |
---|
| 590 | void InsertViewCell(ViewCell *viewCell); |
---|
| 591 | /** Inserts polygons down the tree. The polygons are filtered until a leaf is reached, |
---|
| 592 | then further subdivided. |
---|
| 593 | */ |
---|
| 594 | void InsertPolygons(PolygonContainer *polys); |
---|
| 595 | |
---|
| 596 | /** Subdivide leaf. |
---|
| 597 | @param leaf the leaf to be subdivided |
---|
| 598 | |
---|
| 599 | @param polys the polygons to be split |
---|
| 600 | @param frontPolys returns the polygons in front of the split plane |
---|
| 601 | @param backPolys returns the polygons in the back of the split plane |
---|
| 602 | |
---|
| 603 | @param rays the polygons to be filtered |
---|
| 604 | @param frontRays returns the polygons in front of the split plane |
---|
| 605 | @param backRays returns the polygons in the back of the split plane |
---|
| 606 | |
---|
| 607 | @returns the root of the subdivision |
---|
| 608 | */ |
---|
| 609 | |
---|
[390] | 610 | BspInterior *SubdivideNode(BspTraversalData &tData, |
---|
| 611 | BspTraversalData &frontData, |
---|
| 612 | BspTraversalData &backData, |
---|
| 613 | PolygonContainer &coincident); |
---|
| 614 | |
---|
[373] | 615 | /** Filters polygons down the tree. |
---|
| 616 | @param node the current BSP node |
---|
| 617 | @param polys the polygons to be filtered |
---|
| 618 | @param frontPolys returns the polygons in front of the split plane |
---|
| 619 | @param backPolys returns the polygons in the back of the split plane |
---|
| 620 | */ |
---|
| 621 | void FilterPolygons(BspInterior *node, |
---|
| 622 | PolygonContainer *polys, |
---|
| 623 | PolygonContainer *frontPolys, |
---|
| 624 | PolygonContainer *backPolys); |
---|
| 625 | |
---|
| 626 | /** Selects the split plane in order to construct a tree with |
---|
| 627 | certain characteristics (e.g., balanced tree, least splits, |
---|
| 628 | 2.5d aligned) |
---|
| 629 | @param polygons container of polygons |
---|
| 630 | @param rays bundle of rays on which the split can be based |
---|
| 631 | */ |
---|
[383] | 632 | Plane3 SelectPlaneHeuristics(BspLeaf *leaf, |
---|
[390] | 633 | BspTraversalData &data, |
---|
| 634 | BspTraversalData &frontData, |
---|
| 635 | BspTraversalData &backData); |
---|
[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, |
---|
[390] | 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 | /** Bounds ray and returns minT and maxT. |
---|
| 713 | @returns true if ray hits BSP tree bounding box |
---|
| 714 | */ |
---|
| 715 | bool BoundRay(const Ray &ray, float &minT, float &maxT) const; |
---|
| 716 | |
---|
| 717 | /** Subdivides the rays into front and back rays according to the split plane. |
---|
| 718 | |
---|
| 719 | @param plane the split plane |
---|
| 720 | @param rays contains the rays to be split. The rays are |
---|
| 721 | distributed into front and back rays. |
---|
| 722 | @param frontRays returns rays on the front side of the plane |
---|
| 723 | @param backRays returns rays on the back side of the plane |
---|
| 724 | |
---|
| 725 | @returns the number of splits |
---|
| 726 | */ |
---|
| 727 | int SplitRays(const Plane3 &plane, |
---|
| 728 | BoundedRayContainer &rays, |
---|
| 729 | BoundedRayContainer &frontRays, |
---|
| 730 | BoundedRayContainer &backRays); |
---|
| 731 | |
---|
| 732 | |
---|
| 733 | /** Extracts the split planes representing the space bounded by node n. |
---|
| 734 | */ |
---|
[390] | 735 | void ExtractSplitPlanes(BspNode *n, vector<Plane3> &planes, vector<bool> &sides) const; |
---|
[373] | 736 | |
---|
[379] | 737 | /** Computes the pvs of the front and back leaf with a given classification. |
---|
| 738 | */ |
---|
[384] | 739 | void AddToPvs(Intersectable &obj, |
---|
[390] | 740 | int &frontPvs, |
---|
| 741 | int &backPvs, |
---|
| 742 | const int cf, |
---|
| 743 | const int frontId, |
---|
| 744 | const int backId, |
---|
| 745 | const int frontAndBackId) const; |
---|
[373] | 746 | |
---|
[384] | 747 | int ComputePvsSize(const BoundedRayContainer &rays) const; |
---|
| 748 | |
---|
[373] | 749 | /// Pointer to the root of the tree |
---|
| 750 | BspNode *mRoot; |
---|
| 751 | |
---|
| 752 | BspTreeStatistics mStat; |
---|
| 753 | |
---|
| 754 | /// Strategies for choosing next split plane. |
---|
| 755 | enum {NO_STRATEGY = 0, |
---|
| 756 | RANDOM_POLYGON = 1, |
---|
| 757 | AXIS_ALIGNED = 2, |
---|
| 758 | LEAST_SPLITS = 4, |
---|
| 759 | BALANCED_POLYS = 8, |
---|
| 760 | BALANCED_VIEW_CELLS = 16, |
---|
| 761 | LARGEST_POLY_AREA = 32, |
---|
| 762 | VERTICAL_AXIS = 64, |
---|
| 763 | BLOCKED_RAYS = 128, |
---|
| 764 | LEAST_RAY_SPLITS = 256, |
---|
| 765 | BALANCED_RAYS = 512, |
---|
| 766 | PVS = 1024 |
---|
| 767 | }; |
---|
| 768 | |
---|
| 769 | /// box around the whole view domain |
---|
| 770 | AxisAlignedBox3 mBox; |
---|
| 771 | |
---|
| 772 | /// view cell corresponding to unbounded space |
---|
| 773 | ViewCell *mRootCell; |
---|
| 774 | |
---|
| 775 | /// should view cells be stored or generated in the leaves? |
---|
| 776 | bool mGenerateViewCells; |
---|
| 777 | |
---|
| 778 | /// if rays should be stored that are piercing this view cell |
---|
| 779 | bool mStorePiercingRays; |
---|
| 780 | |
---|
| 781 | public: |
---|
| 782 | /// Parses the environment and stores the global BSP tree parameters |
---|
| 783 | static void ParseEnvironment(); |
---|
| 784 | |
---|
| 785 | /// maximal number of polygons before subdivision termination |
---|
| 786 | static int sTermMaxPolygons; |
---|
| 787 | /// maximal number of rays before subdivision termination |
---|
| 788 | static int sTermMaxRays; |
---|
| 789 | /// maximal possible depth |
---|
| 790 | static int sTermMaxDepth; |
---|
[390] | 791 | /// mininam pvs |
---|
| 792 | static int sTermMinPvs; |
---|
[373] | 793 | /// strategy to get the best split plane |
---|
| 794 | static int sSplitPlaneStrategy; |
---|
| 795 | /// number of candidates evaluated for the next split plane |
---|
[379] | 796 | static int sMaxPolyCandidates; |
---|
| 797 | static int sMaxRayCandidates; |
---|
[373] | 798 | /// BSP tree construction method |
---|
| 799 | static int sConstructionMethod; |
---|
| 800 | /// maximal number of polygons for axis aligned split |
---|
| 801 | static int sTermMaxPolysForAxisAligned; |
---|
| 802 | /// maximal number of rays for axis aligned split |
---|
| 803 | static int sTermMaxRaysForAxisAligned; |
---|
| 804 | /// maximal number of objects for axis aligned split |
---|
| 805 | static int sTermMaxObjectsForAxisAligned; |
---|
| 806 | |
---|
| 807 | /// axis aligned split criteria |
---|
| 808 | static float sCt_div_ci; |
---|
| 809 | static float sSplitBorder; |
---|
| 810 | static float sMaxCostRatio; |
---|
| 811 | |
---|
| 812 | // factors guiding the split plane heuristics |
---|
| 813 | static float sLeastSplitsFactor; |
---|
| 814 | static float sBalancedPolysFactor; |
---|
| 815 | static float sBalancedViewCellsFactor; |
---|
| 816 | static float sVerticalSplitsFactor; |
---|
| 817 | static float sLargestPolyAreaFactor; |
---|
| 818 | static float sBlockedRaysFactor; |
---|
| 819 | static float sLeastRaySplitsFactor; |
---|
| 820 | static float sBalancedRaysFactor; |
---|
| 821 | static float sPvsFactor; |
---|
| 822 | |
---|
[378] | 823 | //-- thresholds used for view cells are merging |
---|
[373] | 824 | static int sMinPvsDif; |
---|
[378] | 825 | static int sMinPvs; |
---|
| 826 | static int sMaxPvs; |
---|
[373] | 827 | |
---|
| 828 | private: |
---|
| 829 | /** Evaluates split plane classification with respect to the plane's |
---|
| 830 | contribution for a balanced tree. |
---|
| 831 | */ |
---|
| 832 | static float sLeastPolySplitsTable[4]; |
---|
| 833 | /** Evaluates split plane classification with respect to the plane's |
---|
| 834 | contribution for a minimum number splits in the tree. |
---|
| 835 | */ |
---|
| 836 | static float sBalancedPolysTable[4]; |
---|
| 837 | /** Evaluates split plane classification with respect to the plane's |
---|
| 838 | contribution for a minimum number of ray splits. |
---|
| 839 | */ |
---|
| 840 | static float sLeastRaySplitsTable[5]; |
---|
| 841 | /** Evaluates split plane classification with respect to the plane's |
---|
| 842 | contribution for balanced rays. |
---|
| 843 | */ |
---|
| 844 | static float sBalancedRaysTable[5]; |
---|
| 845 | |
---|
| 846 | }; |
---|
| 847 | |
---|
| 848 | #endif |
---|