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