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