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