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