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