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