[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; |
---|
| 9 | class Plane3; |
---|
| 10 | //class Mesh; |
---|
[224] | 11 | class BspTree; |
---|
[221] | 12 | class BspInterior; |
---|
[235] | 13 | class Polygon3; |
---|
[242] | 14 | class AxisAlignedBox3; |
---|
[260] | 15 | class Ray; |
---|
[221] | 16 | |
---|
[242] | 17 | //namespace GtpVisibilityPreprocessor { |
---|
| 18 | |
---|
[238] | 19 | /** Container storing a soup of polygons used during BSP tree construction |
---|
[235] | 20 | */ |
---|
[260] | 21 | typedef vector<Polygon3 *> PolygonContainer; |
---|
| 22 | typedef vector<Ray *> RayContainer; |
---|
[224] | 23 | |
---|
[241] | 24 | struct BspRayTraversalData |
---|
| 25 | { |
---|
| 26 | BspNode *mNode; |
---|
| 27 | Vector3 mExitPoint; |
---|
| 28 | float mMaxT; |
---|
| 29 | |
---|
| 30 | BspRayTraversalData() {} |
---|
| 31 | |
---|
| 32 | BspRayTraversalData(BspNode *n, const Vector3 &p, const float maxt): |
---|
| 33 | mNode(n), mExitPoint(p), mMaxT(maxt) |
---|
| 34 | {} |
---|
| 35 | }; |
---|
| 36 | |
---|
[234] | 37 | class BspTreeStatistics // TODO: should have common superclass with KdTreeStatistics |
---|
| 38 | { |
---|
| 39 | public: |
---|
| 40 | // total number of nodes |
---|
| 41 | int nodes; |
---|
[235] | 42 | // number of splits |
---|
| 43 | int splits; |
---|
[234] | 44 | // totals number of rays |
---|
| 45 | int rays; |
---|
[235] | 46 | // maximal reached depth |
---|
| 47 | int maxDepth; |
---|
[234] | 48 | // total number of query domains |
---|
| 49 | int queryDomains; |
---|
| 50 | // total number of ray references |
---|
| 51 | int rayRefs; |
---|
| 52 | // refs in non empty leafs |
---|
| 53 | int rayRefsNonZeroQuery; |
---|
| 54 | // total number of query references |
---|
| 55 | int objectRefs; |
---|
| 56 | // nodes with zero queries |
---|
| 57 | int zeroQueryNodes; |
---|
| 58 | // max depth nodes |
---|
| 59 | int maxDepthNodes; |
---|
| 60 | // max number of rays per node |
---|
| 61 | int maxObjectRefs; |
---|
| 62 | // number of dynamically added ray refs |
---|
| 63 | int addedRayRefs; |
---|
| 64 | // number of dynamically removed ray refs |
---|
| 65 | int removedRayRefs; |
---|
| 66 | |
---|
| 67 | // Constructor |
---|
| 68 | BspTreeStatistics() |
---|
| 69 | { |
---|
[235] | 70 | Reset(); |
---|
[234] | 71 | } |
---|
| 72 | |
---|
| 73 | int Nodes() const {return nodes;} |
---|
| 74 | int Interior() const { return nodes/2; } |
---|
| 75 | int Leaves() const { return (nodes/2) + 1; } |
---|
| 76 | |
---|
| 77 | void Reset() |
---|
| 78 | { |
---|
| 79 | nodes = 0; |
---|
[235] | 80 | splits = 0; |
---|
[234] | 81 | rays = queryDomains = 0; |
---|
| 82 | rayRefs = rayRefsNonZeroQuery = objectRefs = 0; |
---|
| 83 | zeroQueryNodes = 0; |
---|
| 84 | maxDepthNodes = 0; |
---|
| 85 | //minCostNodes = 0; |
---|
| 86 | maxObjectRefs = 0; |
---|
| 87 | addedRayRefs = removedRayRefs = 0; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | void |
---|
| 91 | Print(ostream &app) const; |
---|
| 92 | |
---|
| 93 | friend ostream &operator<<(ostream &s, const BspTreeStatistics &stat) { |
---|
| 94 | stat.Print(s); |
---|
| 95 | return s; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | }; |
---|
| 99 | |
---|
[233] | 100 | /** |
---|
| 101 | BspNode abstract class serving for interior and leaf node implementation |
---|
| 102 | */ |
---|
| 103 | class BspNode |
---|
| 104 | { |
---|
| 105 | friend BspTree; |
---|
[221] | 106 | |
---|
[233] | 107 | public: |
---|
[235] | 108 | BspNode(); |
---|
| 109 | BspNode(BspInterior *parent); |
---|
| 110 | |
---|
[233] | 111 | /** Determines whether this node is a leaf or not |
---|
| 112 | @return true if leaf |
---|
| 113 | */ |
---|
| 114 | virtual bool IsLeaf() const = 0; |
---|
[221] | 115 | |
---|
[233] | 116 | /** Determines whether this node is a root |
---|
| 117 | @return true if root |
---|
| 118 | */ |
---|
| 119 | virtual bool IsRoot() const; |
---|
[221] | 120 | |
---|
[233] | 121 | /** Returns parent node. |
---|
| 122 | */ |
---|
| 123 | BspInterior *GetParent(); |
---|
[235] | 124 | /** Sets parent node. |
---|
| 125 | */ |
---|
| 126 | void SetParent(BspInterior *parent); |
---|
| 127 | |
---|
[260] | 128 | PolygonContainer *GetPolygons(); |
---|
| 129 | |
---|
[233] | 130 | protected: |
---|
[221] | 131 | |
---|
[233] | 132 | /// parent of this node |
---|
| 133 | BspInterior *mParent; |
---|
[260] | 134 | |
---|
| 135 | PolygonContainer mPolygons; |
---|
[233] | 136 | }; |
---|
[221] | 137 | |
---|
[233] | 138 | /** BSP interior node implementation |
---|
| 139 | */ |
---|
| 140 | class BspInterior : public BspNode |
---|
| 141 | { |
---|
| 142 | public: |
---|
| 143 | /** Standard contructor taking split plane as argument. |
---|
| 144 | */ |
---|
| 145 | BspInterior(Plane3 plane); |
---|
| 146 | /** @return false since it is an interior node |
---|
| 147 | */ |
---|
| 148 | bool IsLeaf() const; |
---|
[222] | 149 | |
---|
[233] | 150 | BspNode *GetBack(); |
---|
| 151 | BspNode *GetFront(); |
---|
[221] | 152 | |
---|
[233] | 153 | Plane3 *GetPlane(); |
---|
[221] | 154 | |
---|
[233] | 155 | void ReplaceChildLink(BspNode *oldChild, BspNode *newChild); |
---|
| 156 | void SetupChildLinks(BspNode *b, BspNode *f); |
---|
[225] | 157 | |
---|
[233] | 158 | /** Splits polygons. |
---|
| 159 | @param polys the polygons to be split |
---|
| 160 | @param frontPolys returns the polygons in the front of the split plane |
---|
| 161 | @param backPolys returns the polygons in the back of the split plane |
---|
[235] | 162 | @param splits number of splits |
---|
[233] | 163 | */ |
---|
[238] | 164 | void SplitPolygons(PolygonContainer *polys, PolygonContainer *frontPolys, |
---|
[260] | 165 | PolygonContainer *backPolys, int &splits, bool storePolys = false); |
---|
[225] | 166 | |
---|
[237] | 167 | friend ostream &operator<<(ostream &s, const BspInterior &A) |
---|
| 168 | { |
---|
| 169 | return s << A.mPlane; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | |
---|
[233] | 173 | protected: |
---|
| 174 | |
---|
| 175 | /// Splitting plane corresponding to this node |
---|
| 176 | Plane3 mPlane; |
---|
| 177 | /// back node |
---|
| 178 | BspNode *mBack; |
---|
| 179 | /// front node |
---|
| 180 | BspNode *mFront; |
---|
| 181 | }; |
---|
[225] | 182 | |
---|
[237] | 183 | /** BSP leaf node implementation. |
---|
| 184 | */ |
---|
[233] | 185 | class BspLeaf : public BspNode |
---|
| 186 | { |
---|
[260] | 187 | friend BspTree; |
---|
| 188 | |
---|
[233] | 189 | public: |
---|
| 190 | BspLeaf(ViewCell *viewCell = NULL); |
---|
[225] | 191 | |
---|
[260] | 192 | /** @return true since it is an interior node |
---|
| 193 | */ |
---|
[233] | 194 | bool IsLeaf() const; |
---|
[260] | 195 | /** Returns pointer from view cell. |
---|
| 196 | */ |
---|
[233] | 197 | ViewCell *GetViewCell(); |
---|
[260] | 198 | /** Sets pointer to view cell. |
---|
| 199 | */ |
---|
| 200 | void SetViewCell(ViewCell *viewCell); |
---|
[225] | 201 | |
---|
[233] | 202 | protected: |
---|
[225] | 203 | |
---|
[260] | 204 | /** Helper function used to add remaining polygons to leaf. |
---|
| 205 | */ |
---|
| 206 | void AddPolygons(PolygonContainer *polys); |
---|
| 207 | |
---|
[233] | 208 | /// polygonal representation of this viewcell |
---|
| 209 | /// if NULL this note does not correspond to feasible viewcell |
---|
| 210 | ViewCell *mViewCell; |
---|
| 211 | }; |
---|
[225] | 212 | |
---|
| 213 | |
---|
[233] | 214 | /** Implementation of the ViewCell BSP tree. */ |
---|
| 215 | class BspTree |
---|
| 216 | { |
---|
| 217 | public: |
---|
| 218 | |
---|
| 219 | /** Additional data which is passed down the BSP tree during traversal. |
---|
| 220 | */ |
---|
| 221 | struct BspTraversalData |
---|
| 222 | { |
---|
| 223 | /// the current node |
---|
| 224 | BspNode *mNode; |
---|
| 225 | /// parent of current node |
---|
| 226 | BspInterior *mParent; |
---|
| 227 | /// polygonal data for splitting |
---|
[238] | 228 | PolygonContainer *mPolygons; |
---|
[233] | 229 | /// current depth |
---|
| 230 | int mDepth; |
---|
| 231 | |
---|
| 232 | BspTraversalData() {} |
---|
| 233 | |
---|
[238] | 234 | BspTraversalData(BspNode *node, BspInterior *parent, PolygonContainer *polys, const int depth): |
---|
[235] | 235 | mNode(node), mParent(parent), mPolygons(polys), mDepth(depth) {} |
---|
[233] | 236 | }; |
---|
| 237 | |
---|
| 238 | typedef std::stack<BspTraversalData> BspTraversalStack; |
---|
| 239 | |
---|
[235] | 240 | /// BSP tree construction type |
---|
[260] | 241 | enum {VIEWCELLS, SCENE_GEOMETRY, RAYS}; |
---|
[235] | 242 | |
---|
| 243 | /** Default constructor creating an empty tree. |
---|
[233] | 244 | */ |
---|
[235] | 245 | BspTree(); |
---|
[233] | 246 | |
---|
| 247 | ~BspTree(); |
---|
| 248 | |
---|
[235] | 249 | const BspTreeStatistics &GetStatistics() const; |
---|
| 250 | |
---|
[260] | 251 | /** Constructs tree using the given list of view cells.
|
---|
| 252 | A pointer to the appropriate view cell is stored within each leaf.
|
---|
[235] | 253 | Many leafs can point to the same viewcell.
|
---|
| 254 | */
|
---|
| 255 | void Construct(const ViewCellContainer &viewCells);
|
---|
| 256 |
|
---|
[260] | 257 | /** Constructs tree using the given list of objects.
|
---|
| 258 | Note that the objects are not taken as view cells, but the view cells are
|
---|
| 259 | constructed from the subdivision: Each leaf is taken as one viewcell;
|
---|
| 260 |
|
---|
[235] | 261 | @param objects list of objects
|
---|
[260] | 262 | @returns list of view cells.
|
---|
[235] | 263 | */
|
---|
[260] | 264 | ViewCellContainer *Construct(const ObjectContainer &objects); |
---|
[235] | 265 | |
---|
[260] | 266 | /** Constructs tree using the given number of rays
|
---|
| 267 | @param objects list of objects
|
---|
| 268 | @returns list of view cells.
|
---|
| 269 | */ |
---|
| 270 | ViewCellContainer *Construct(const RayContainer &rays); |
---|
| 271 | |
---|
[242] | 272 | int CollectLeafPvs(); |
---|
[235] | 273 | |
---|
[240] | 274 | void CollectLeaves(vector<BspLeaf *> &leaves); |
---|
| 275 | |
---|
[242] | 276 | /** Returns box which bounds the whole tree. |
---|
| 277 | */ |
---|
| 278 | AxisAlignedBox3 GetBoundingBox()const; |
---|
| 279 | |
---|
| 280 | /** Returns root of BSP tree. |
---|
| 281 | */ |
---|
| 282 | BspNode *GetRoot() const; |
---|
| 283 | |
---|
[260] | 284 | /** If the view cell polygons are stored in the nodes. |
---|
| 285 | */ |
---|
| 286 | bool StorePolys() const; |
---|
| 287 | |
---|
[233] | 288 | protected: |
---|
[238] | 289 | |
---|
[260] | 290 | /** Initialises BSP tree. |
---|
| 291 | @param maxPolygons the maximal polygon count before termination of |
---|
| 292 | subdivision |
---|
| 293 | @param maxDepth the maximal depth before termination of |
---|
| 294 | subdivision |
---|
| 295 | */ |
---|
| 296 | void InitTree(int maxPolygons, int maxDepth); |
---|
| 297 | |
---|
| 298 | /** Constructs the tree from the given list of polygons. |
---|
| 299 | */ |
---|
| 300 | void Construct(PolygonContainer *polys);
|
---|
| 301 | |
---|
[238] | 302 | /** Evaluates plane classification with respect to the plane's |
---|
| 303 | contribution for a balanced tree. |
---|
| 304 | */ |
---|
| 305 | static inline int EvalForBalancedTree(const int classficiation); |
---|
| 306 | /** Evaluates plane classification with respect to the plane's |
---|
| 307 | contribution for a minimum number splits in the tree. |
---|
| 308 | */ |
---|
| 309 | static inline int EvalForLeastSplits(const int classification); |
---|
| 310 | |
---|
| 311 | /** Evaluates the contribution of the candidate split plane. |
---|
| 312 | */ |
---|
| 313 | int EvalSplitPlane(PolygonContainer *polygons, const Plane3 &candidatePlane) const; |
---|
| 314 | |
---|
| 315 | /** Evaluates tree stats in the BSP tree leafs. |
---|
| 316 | */ |
---|
[235] | 317 | void EvaluateLeafStats(const BspTraversalData &data); |
---|
| 318 | |
---|
| 319 | /** Subdivides node with respect to the traversal data. |
---|
| 320 | @param tStack current traversal stack |
---|
| 321 | @param tData traversal data also holding node to be subdivided |
---|
| 322 | @param viewCell the view cell that will be represented with this part of the Bsp tree. |
---|
[239] | 323 | @returns new root of the subtree |
---|
[233] | 324 | */ |
---|
[239] | 325 | BspNode *Subdivide(BspTraversalStack &tStack, BspTraversalData &tData, ViewCell *viewCell = NULL); |
---|
[233] | 326 | |
---|
[235] | 327 | /** Selects a splitting plane. |
---|
[238] | 328 | @param polygons the polygon data on which the split decition is based |
---|
[233] | 329 | */ |
---|
[238] | 330 | Plane3 SelectPlane(PolygonContainer *polygons) const; |
---|
[224] | 331 |
|
---|
[233] | 332 | /** Filters next viewcell down the tree and inserts it into the appropriate leaves
|
---|
| 333 | (i.e., possibly more than one leaf).
|
---|
| 334 | */
|
---|
[235] | 335 | void InsertViewCell(ViewCell *viewCell); |
---|
[233] | 336 |
|
---|
[235] | 337 | /** Subdivide leaf.
|
---|
[233] | 338 | @param leaf the leaf to be subdivided
|
---|
| 339 | @param parent the parent node of this leaf
|
---|
| 340 | @param polys the input polygons
|
---|
| 341 | @param depth the current tree depth
|
---|
| 342 | @param frontPolys the polygons of the front child node as a result from splitting
|
---|
| 343 | @param backPolys the polygons of the back child node as a result from splitting
|
---|
| 344 | */
|
---|
[260] | 345 | BspNode *SubdivideNode(BspLeaf *leaf,
|
---|
| 346 | BspInterior *parent,
|
---|
| 347 | PolygonContainer *polys,
|
---|
| 348 | const int depth,
|
---|
| 349 | PolygonContainer *frontPolys, |
---|
| 350 | PolygonContainer *backPolys); |
---|
[221] | 351 | |
---|
[233] | 352 | /** Filters polygons down the tree. |
---|
| 353 | @param node the current BSP node |
---|
| 354 | @param polys the polygons to be filtered |
---|
| 355 | @param frontPolys returns the polygons in front of the split plane |
---|
| 356 | @param backPolys returns the polygons in the back of the split plane |
---|
| 357 | */ |
---|
[238] | 358 | void FilterPolygons(BspInterior *node, PolygonContainer *polys, |
---|
| 359 | PolygonContainer *frontPolys, PolygonContainer *backPolys); |
---|
[224] | 360 | |
---|
[238] | 361 | /** Selects the split plane in order to get a balanced tree. |
---|
| 362 | @param polygons container of polygons |
---|
| 363 | @param maxTests the maximal number of candidate tests |
---|
| 364 | */ |
---|
| 365 | Plane3 SelectPlaneHeuristics(PolygonContainer *polygons, int maxTests) const; |
---|
| 366 | |
---|
[260] | 367 | /** Extracts the meshes of the objects and copies them into the mesh. |
---|
| 368 | Adds object aabb to the aabb of the tree. |
---|
[241] | 369 | @param maxPolys the maximal number of objects to be stored as polygons |
---|
[233] | 370 | */ |
---|
[241] | 371 | void Copy2PolygonSoup(const ObjectContainer &objects, PolygonContainer &polys, int maxObjects); |
---|
[260] | 372 | /** Extracts the meshes of the view cells and copies them into the mesh. |
---|
| 373 | Adds view cell aabb to the aabb of the tree. |
---|
| 374 | @param maxPolys the maximal number of objects to be stored as polygons |
---|
| 375 | */ |
---|
| 376 | void Copy2PolygonSoup(const ViewCellContainer &viewCells, PolygonContainer &polys, int maxObjects); |
---|
[224] | 377 | |
---|
[241] | 378 | /** Add this mesh as polygons to polygon container. |
---|
[235] | 379 | */ |
---|
[241] | 380 | void AddMesh2Polygons(Mesh *mesh, PolygonContainer &polys); |
---|
[233] | 381 | |
---|
[241] | 382 | /** A ray is cast possible intersecting the tree. |
---|
| 383 | @param the ray that is cast. |
---|
| 384 | @returns the number of intersections with objects stored in the tree. |
---|
| 385 | */ |
---|
| 386 | int CastRay(Ray &ray); |
---|
| 387 | |
---|
[233] | 388 | /// Pointer to the root of the tree |
---|
| 389 | BspNode *mRoot; |
---|
[237] | 390 | |
---|
[233] | 391 | /// Pointer to the root cell of the viewspace |
---|
| 392 | // ViewCell *mRootCell; |
---|
[237] | 393 | |
---|
[235] | 394 | BspTreeStatistics mStat; |
---|
[234] | 395 | |
---|
[235] | 396 | /// local maximal polygons (changes depending on method) |
---|
| 397 | int mTermMaxPolygons; |
---|
| 398 | int mTermMaxDepth; |
---|
| 399 | |
---|
[237] | 400 | /// Strategies for choosing next split plane. |
---|
[238] | 401 | enum {NEXT_POLYGON, LEAST_SPLITS, BALANCED_TREE, COMBINED}; |
---|
[236] | 402 | |
---|
[241] | 403 | /// box around the whole view domain |
---|
| 404 | AxisAlignedBox3 mBox; |
---|
| 405 | |
---|
[260] | 406 | /// if view cell calculation is incremential |
---|
| 407 | bool mIsIncremential; |
---|
| 408 | |
---|
| 409 | /// if polygons should be stored in the tree |
---|
| 410 | bool mStorePolys; |
---|
| 411 | |
---|
[235] | 412 | public: |
---|
| 413 | /// Parses the environment and stores the global BSP tree parameters |
---|
| 414 | static void ParseEnvironment(); |
---|
| 415 | |
---|
| 416 | /// maximal number of polygons where tree construction is terminated |
---|
| 417 | static int sTermMaxPolygons; |
---|
| 418 | /// maximal possible depth |
---|
| 419 | static int sTermMaxDepth; |
---|
[238] | 420 | /// strategy to get the best split plane |
---|
[236] | 421 | static int sSplitPlaneStrategy; |
---|
[238] | 422 | /// number of candidates evaluated for the next split plane |
---|
| 423 | static int sMaxCandidates; |
---|
[233] | 424 | }; |
---|
| 425 | |
---|
[221] | 426 | //}; // GtpVisibilityPreprocessor |
---|
| 427 | |
---|
| 428 | #endif |
---|