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