[1237] | 1 | #ifndef _BvHierarchy_H__
|
---|
| 2 | #define _BvHierarchy_H__
|
---|
| 3 |
|
---|
| 4 | #include <stack>
|
---|
| 5 |
|
---|
| 6 | #include "Mesh.h"
|
---|
| 7 | #include "Containers.h"
|
---|
| 8 | #include "Statistics.h"
|
---|
| 9 | #include "VssRay.h"
|
---|
| 10 | #include "RayInfo.h"
|
---|
| 11 | #include "gzstream.h"
|
---|
[1239] | 12 | #include "SubdivisionCandidate.h"
|
---|
[1237] | 13 | #include "AxisAlignedBox3.h"
|
---|
[1315] | 14 | #include "IntersectableWrapper.h"
|
---|
[1237] | 15 |
|
---|
| 16 |
|
---|
| 17 | namespace GtpVisibilityPreprocessor {
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 | class ViewCellLeaf;
|
---|
| 21 | class Plane3;
|
---|
| 22 | class AxisAlignedBox3;
|
---|
| 23 | class Ray;
|
---|
| 24 | class ViewCellsStatistics;
|
---|
| 25 | class ViewCellsManager;
|
---|
| 26 | class MergeCandidate;
|
---|
| 27 | class Beam;
|
---|
| 28 | class ViewCellsTree;
|
---|
| 29 | class Environment;
|
---|
| 30 | class BvhInterior;
|
---|
| 31 | class BvhLeaf;
|
---|
| 32 | class BvhNode;
|
---|
| 33 | class BvhIntersectable;
|
---|
| 34 | class BvhTree;
|
---|
| 35 | class VspTree;
|
---|
| 36 | class ViewCellsContainer;
|
---|
[1370] | 37 | class HierarchyManager;
|
---|
[1237] | 38 |
|
---|
[1297] | 39 |
|
---|
[1237] | 40 | /** View space partition statistics.
|
---|
| 41 | */
|
---|
| 42 | class BvhStatistics: public StatisticsBase
|
---|
| 43 | {
|
---|
| 44 | public:
|
---|
[1370] | 45 |
|
---|
| 46 | /// Constructor
|
---|
[1237] | 47 | BvhStatistics()
|
---|
| 48 | {
|
---|
| 49 | Reset();
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | int Nodes() const {return nodes;}
|
---|
| 53 | int Interior() const { return nodes / 2; }
|
---|
| 54 | int Leaves() const { return (nodes / 2) + 1; }
|
---|
| 55 |
|
---|
| 56 | double AvgDepth() const
|
---|
| 57 | { return accumDepth / (double)Leaves(); }
|
---|
| 58 |
|
---|
[1370] | 59 | double AvgObjectRefs() const
|
---|
| 60 | { return objectRefs / (double)Leaves(); }
|
---|
| 61 |
|
---|
| 62 | double AvgRayRefs() const
|
---|
| 63 | { return rayRefs / (double)Leaves(); }
|
---|
| 64 |
|
---|
| 65 |
|
---|
[1237] | 66 | void Reset()
|
---|
| 67 | {
|
---|
| 68 | nodes = 0;
|
---|
| 69 | splits = 0;
|
---|
| 70 | maxDepth = 0;
|
---|
| 71 | minDepth = 99999;
|
---|
| 72 | accumDepth = 0;
|
---|
| 73 | maxDepthNodes = 0;
|
---|
| 74 | minProbabilityNodes = 0;
|
---|
| 75 | maxCostNodes = 0;
|
---|
[1370] | 76 |
|
---|
| 77 | ///////////////////
|
---|
| 78 | minObjectsNodes = 0;
|
---|
[1237] | 79 | maxObjectRefs = 0;
|
---|
[1370] | 80 | minObjectRefs = 999999999;
|
---|
[1237] | 81 | objectRefs = 0;
|
---|
[1370] | 82 |
|
---|
| 83 | ///////////////////
|
---|
| 84 | minRaysNodes = 0;
|
---|
| 85 | maxRayRefs = 0;
|
---|
| 86 | minRayRefs = 999999999;
|
---|
| 87 | rayRefs = 0;
|
---|
| 88 | maxRayContriNodes = 0;
|
---|
[1237] | 89 | }
|
---|
| 90 |
|
---|
[1370] | 91 |
|
---|
| 92 | public:
|
---|
| 93 |
|
---|
| 94 | // total number of nodes
|
---|
| 95 | int nodes;
|
---|
| 96 | // number of splits
|
---|
| 97 | int splits;
|
---|
| 98 | // maximal reached depth
|
---|
| 99 | int maxDepth;
|
---|
| 100 | // minimal depth
|
---|
| 101 | int minDepth;
|
---|
| 102 | // max depth nodes
|
---|
| 103 | int maxDepthNodes;
|
---|
| 104 | // accumulated depth (used to compute average)
|
---|
| 105 | int accumDepth;
|
---|
| 106 | // minimum area nodes
|
---|
| 107 | int minProbabilityNodes;
|
---|
| 108 | /// nodes termination because of max cost ratio;
|
---|
| 109 | int maxCostNodes;
|
---|
| 110 |
|
---|
| 111 | ///////////////////////////
|
---|
| 112 | // nodes with minimum objects
|
---|
| 113 | int minObjectsNodes;
|
---|
| 114 | // max number of rays per node
|
---|
| 115 | int maxObjectRefs;
|
---|
| 116 | // min number of rays per node
|
---|
| 117 | int minObjectRefs;
|
---|
| 118 | /// object references
|
---|
| 119 | int objectRefs;
|
---|
| 120 |
|
---|
| 121 |
|
---|
| 122 | //////////////////////////
|
---|
| 123 | // nodes with minimum rays
|
---|
| 124 | int minRaysNodes;
|
---|
| 125 | // max number of rays per node
|
---|
| 126 | int maxRayRefs;
|
---|
| 127 | // min number of rays per node
|
---|
| 128 | int minRayRefs;
|
---|
| 129 | /// object references
|
---|
| 130 | int rayRefs;
|
---|
| 131 | /// nodes with max ray contribution
|
---|
| 132 | int maxRayContriNodes;
|
---|
| 133 |
|
---|
[1237] | 134 | void Print(ostream &app) const;
|
---|
| 135 |
|
---|
| 136 | friend ostream &operator<<(ostream &s, const BvhStatistics &stat)
|
---|
| 137 | {
|
---|
| 138 | stat.Print(s);
|
---|
| 139 | return s;
|
---|
| 140 | }
|
---|
| 141 | };
|
---|
| 142 |
|
---|
| 143 |
|
---|
| 144 | /**
|
---|
| 145 | VspNode abstract class serving for interior and leaf node implementation
|
---|
| 146 | */
|
---|
| 147 | class BvhNode
|
---|
| 148 | {
|
---|
| 149 | public:
|
---|
| 150 |
|
---|
| 151 | // types of vsp nodes
|
---|
| 152 | enum {Interior, Leaf};
|
---|
| 153 |
|
---|
[1297] | 154 | BvhNode();
|
---|
[1237] | 155 | BvhNode(const AxisAlignedBox3 &bbox);
|
---|
| 156 | BvhNode(const AxisAlignedBox3 &bbox, BvhInterior *parent);
|
---|
| 157 |
|
---|
| 158 | virtual ~BvhNode(){};
|
---|
| 159 |
|
---|
| 160 | /** Determines whether this node is a leaf or not
|
---|
| 161 | @return true if leaf
|
---|
| 162 | */
|
---|
| 163 | virtual bool IsLeaf() const = 0;
|
---|
| 164 |
|
---|
| 165 | /** Determines whether this node is a root
|
---|
| 166 | @return true if root
|
---|
| 167 | */
|
---|
| 168 | virtual bool IsRoot() const;
|
---|
| 169 |
|
---|
| 170 | /** Returns parent node.
|
---|
| 171 | */
|
---|
| 172 | BvhInterior *GetParent();
|
---|
| 173 |
|
---|
| 174 | /** Sets parent node.
|
---|
| 175 | */
|
---|
| 176 | void SetParent(BvhInterior *parent);
|
---|
| 177 |
|
---|
| 178 | /** The bounding box specifies the node extent.
|
---|
| 179 | */
|
---|
[1357] | 180 | inline
|
---|
[1237] | 181 | AxisAlignedBox3 GetBoundingBox() const
|
---|
| 182 | { return mBoundingBox; }
|
---|
| 183 |
|
---|
| 184 |
|
---|
[1357] | 185 | inline
|
---|
[1237] | 186 | void SetBoundingBox(const AxisAlignedBox3 &boundingBox)
|
---|
| 187 | { mBoundingBox = boundingBox; }
|
---|
| 188 |
|
---|
| 189 |
|
---|
[1291] | 190 | /////////////////////////////////////
|
---|
[1237] | 191 | //-- mailing options
|
---|
[1291] | 192 |
|
---|
| 193 | static void NewMail(const int reserve = 1) {
|
---|
| 194 | sMailId += sReservedMailboxes;
|
---|
| 195 | sReservedMailboxes = reserve;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[1237] | 198 | void Mail() { mMailbox = sMailId; }
|
---|
| 199 | bool Mailed() const { return mMailbox == sMailId; }
|
---|
| 200 |
|
---|
[1291] | 201 | void Mail(const int mailbox) { mMailbox = sMailId + mailbox; }
|
---|
| 202 | bool Mailed(const int mailbox) const { return mMailbox == sMailId + mailbox; }
|
---|
| 203 |
|
---|
| 204 | int IncMail() { return ++ mMailbox - sMailId; }
|
---|
| 205 |
|
---|
[1237] | 206 | static int sMailId;
|
---|
| 207 | int mMailbox;
|
---|
[1291] | 208 | static int sReservedMailboxes;
|
---|
[1237] | 209 |
|
---|
| 210 | ///////////////////////////////////
|
---|
| 211 |
|
---|
| 212 | protected:
|
---|
| 213 |
|
---|
| 214 | /// the bounding box of the node
|
---|
| 215 | AxisAlignedBox3 mBoundingBox;
|
---|
| 216 | /// parent of this node
|
---|
| 217 | BvhInterior *mParent;
|
---|
| 218 | };
|
---|
| 219 |
|
---|
| 220 |
|
---|
| 221 | /** BSP interior node implementation
|
---|
| 222 | */
|
---|
| 223 | class BvhInterior: public BvhNode
|
---|
| 224 | {
|
---|
| 225 | public:
|
---|
| 226 | /** Standard contructor taking a bounding box as argument.
|
---|
| 227 | */
|
---|
| 228 | BvhInterior(const AxisAlignedBox3 &bbox);
|
---|
| 229 | BvhInterior(const AxisAlignedBox3 &bbox, BvhInterior *parent);
|
---|
| 230 |
|
---|
| 231 | ~BvhInterior();
|
---|
| 232 | /** @return false since it is an interior node
|
---|
| 233 | */
|
---|
| 234 | bool IsLeaf() const;
|
---|
[1294] | 235 |
|
---|
[1237] | 236 | BvhNode *GetBack() { return mBack; }
|
---|
| 237 | BvhNode *GetFront() { return mFront; }
|
---|
| 238 |
|
---|
| 239 | /** Replace front or back child with new child.
|
---|
| 240 | */
|
---|
| 241 | void ReplaceChildLink(BvhNode *oldChild, BvhNode *newChild);
|
---|
| 242 |
|
---|
| 243 | /** Replace front and back child.
|
---|
| 244 | */
|
---|
| 245 | void SetupChildLinks(BvhNode *front, BvhNode *back);
|
---|
| 246 |
|
---|
| 247 | friend ostream &operator<<(ostream &s, const BvhInterior &A)
|
---|
| 248 | {
|
---|
| 249 | return s << A.mBoundingBox;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | protected:
|
---|
| 253 |
|
---|
| 254 | /// back node
|
---|
| 255 | BvhNode *mBack;
|
---|
| 256 | /// front node
|
---|
| 257 | BvhNode *mFront;
|
---|
| 258 | };
|
---|
| 259 |
|
---|
| 260 |
|
---|
| 261 | /** BSP leaf node implementation.
|
---|
| 262 | */
|
---|
| 263 | class BvhLeaf: public BvhNode
|
---|
| 264 | {
|
---|
| 265 | public:
|
---|
| 266 | /** Standard contructor taking a bounding box as argument.
|
---|
| 267 | */
|
---|
| 268 | BvhLeaf(const AxisAlignedBox3 &bbox);
|
---|
| 269 | BvhLeaf(const AxisAlignedBox3 &bbox, BvhInterior *parent);
|
---|
| 270 | BvhLeaf(const AxisAlignedBox3 &bbox, BvhInterior *parent, const int numObjects);
|
---|
| 271 |
|
---|
| 272 | ~BvhLeaf();
|
---|
| 273 |
|
---|
| 274 | /** @return true since it is an interior node
|
---|
| 275 | */
|
---|
| 276 | bool IsLeaf() const;
|
---|
| 277 |
|
---|
[1297] | 278 | SubdivisionCandidate *GetSubdivisionCandidate()// const
|
---|
[1237] | 279 | {
|
---|
| 280 | return mSubdivisionCandidate;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[1297] | 283 | void SetSubdivisionCandidate(SubdivisionCandidate *candidate)
|
---|
| 284 | {
|
---|
| 285 | mSubdivisionCandidate = candidate;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
[1237] | 288 | public:
|
---|
| 289 |
|
---|
| 290 | /// Rays piercing this leaf.
|
---|
| 291 | VssRayContainer mVssRays;
|
---|
| 292 | /// objects
|
---|
| 293 | ObjectContainer mObjects;
|
---|
[1259] | 294 | /// universal counter
|
---|
| 295 | int mCounter;
|
---|
[1287] | 296 |
|
---|
[1237] | 297 | protected:
|
---|
| 298 |
|
---|
| 299 | /// pointer to a split plane candidate splitting this leaf
|
---|
| 300 | SubdivisionCandidate *mSubdivisionCandidate;
|
---|
| 301 | };
|
---|
| 302 |
|
---|
| 303 |
|
---|
| 304 | typedef map<BvhNode *, BvhIntersectable *> BvhIntersectableMap;
|
---|
| 305 |
|
---|
| 306 |
|
---|
| 307 | /** View Space Partitioning tree.
|
---|
| 308 | */
|
---|
| 309 | class BvHierarchy
|
---|
| 310 | {
|
---|
| 311 | friend class ViewCellsParseHandlers;
|
---|
| 312 | friend class HierarchyManager;
|
---|
| 313 |
|
---|
[1379] | 314 | protected:
|
---|
[1345] | 315 | struct SortableEntry;
|
---|
| 316 | typedef vector<SortableEntry> SortableEntryContainer;
|
---|
| 317 |
|
---|
[1379] | 318 | public:
|
---|
| 319 |
|
---|
[1237] | 320 | /** Additional data which is passed down the BSP tree during traversal.
|
---|
| 321 | */
|
---|
| 322 | class BvhTraversalData
|
---|
| 323 | {
|
---|
| 324 | public:
|
---|
[1294] | 325 |
|
---|
[1237] | 326 | BvhTraversalData():
|
---|
| 327 | mNode(NULL),
|
---|
| 328 | mDepth(0),
|
---|
[1287] | 329 | mProbability(0.0),
|
---|
[1237] | 330 | mMaxCostMisses(0),
|
---|
[1370] | 331 | mAxis(0),
|
---|
| 332 | mNumRays(0)
|
---|
[1357] | 333 | {
|
---|
| 334 | mSortedObjects[0] = mSortedObjects[1] = mSortedObjects[2] = NULL;
|
---|
| 335 | }
|
---|
[1237] | 336 |
|
---|
| 337 | BvhTraversalData(BvhLeaf *node,
|
---|
| 338 | const int depth,
|
---|
[1370] | 339 | const float v,
|
---|
| 340 | const int numRays):
|
---|
[1237] | 341 | mNode(node),
|
---|
| 342 | mDepth(depth),
|
---|
[1370] | 343 | //mBoundingBox(box),
|
---|
[1287] | 344 | mProbability(v),
|
---|
[1237] | 345 | mMaxCostMisses(0),
|
---|
[1370] | 346 | mAxis(0),
|
---|
| 347 | mNumRays(numRays)
|
---|
[1357] | 348 | {
|
---|
| 349 | mSortedObjects[0] = mSortedObjects[1] = mSortedObjects[2] = NULL;
|
---|
| 350 | }
|
---|
[1237] | 351 |
|
---|
[1357] | 352 | /** Deletes contents and sets them to NULL.
|
---|
| 353 | */
|
---|
[1237] | 354 | void Clear()
|
---|
| 355 | {
|
---|
[1294] | 356 | DEL_PTR(mNode);
|
---|
[1370] | 357 | DEL_PTR(mSortedObjects[0]);
|
---|
[1357] | 358 | DEL_PTR(mSortedObjects[1]);
|
---|
[1370] | 359 | DEL_PTR(mSortedObjects[2]);
|
---|
[1237] | 360 | }
|
---|
| 361 |
|
---|
[1294] | 362 | /// the current node
|
---|
| 363 | BvhLeaf *mNode;
|
---|
| 364 | /// current depth
|
---|
| 365 | int mDepth;
|
---|
| 366 | /// the probability that this node is seen
|
---|
| 367 | float mProbability;
|
---|
| 368 | /// the bounding box of the node
|
---|
[1370] | 369 | //AxisAlignedBox3 mBoundingBox;
|
---|
[1294] | 370 | /// how often this branch has missed the max-cost ratio
|
---|
| 371 | int mMaxCostMisses;
|
---|
| 372 | /// current axis
|
---|
| 373 | int mAxis;
|
---|
[1370] | 374 | /// number of rays
|
---|
| 375 | int mNumRays;
|
---|
[1357] | 376 | /// the sorted objects for the three dimensions
|
---|
| 377 | ObjectContainer *mSortedObjects[3];
|
---|
[1237] | 378 | };
|
---|
| 379 |
|
---|
[1357] | 380 |
|
---|
| 381 | /** Candidate for a object space split.
|
---|
[1237] | 382 | */
|
---|
| 383 | class BvhSubdivisionCandidate: public SubdivisionCandidate
|
---|
| 384 | {
|
---|
| 385 | public:
|
---|
| 386 |
|
---|
[1294] | 387 | BvhSubdivisionCandidate(const BvhTraversalData &tData): mParentData(tData)
|
---|
[1237] | 388 | {};
|
---|
| 389 |
|
---|
[1305] | 390 | ~BvhSubdivisionCandidate()
|
---|
| 391 | {
|
---|
| 392 | mParentData.Clear();
|
---|
| 393 | }
|
---|
[1294] | 394 |
|
---|
[1237] | 395 | int Type() const { return OBJECT_SPACE; }
|
---|
| 396 |
|
---|
| 397 | void EvalPriority()
|
---|
| 398 | {
|
---|
| 399 | sBvHierarchy->EvalSubdivisionCandidate(*this);
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | bool GlobalTerminationCriteriaMet() const
|
---|
| 403 | {
|
---|
| 404 | return sBvHierarchy->GlobalTerminationCriteriaMet(mParentData);
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | BvhSubdivisionCandidate(
|
---|
| 408 | const ObjectContainer &frontObjects,
|
---|
| 409 | const ObjectContainer &backObjects,
|
---|
| 410 | const BvhTraversalData &tData):
|
---|
| 411 | mFrontObjects(frontObjects), mBackObjects(backObjects), mParentData(tData)
|
---|
| 412 | {}
|
---|
[1294] | 413 |
|
---|
[1305] | 414 | /// pointer to parent tree.
|
---|
[1294] | 415 | static BvHierarchy *sBvHierarchy;
|
---|
| 416 | /// parent data
|
---|
| 417 | BvhTraversalData mParentData;
|
---|
[1305] | 418 | /// the objects on the front of the potential split
|
---|
[1294] | 419 | ObjectContainer mFrontObjects;
|
---|
[1305] | 420 | /// the objects on the back of the potential split
|
---|
[1294] | 421 | ObjectContainer mBackObjects;
|
---|
[1237] | 422 | };
|
---|
| 423 |
|
---|
| 424 | /** Struct for traversing line segment.
|
---|
| 425 | */
|
---|
| 426 | struct LineTraversalData
|
---|
| 427 | {
|
---|
| 428 | BvhNode *mNode;
|
---|
| 429 | Vector3 mExitPoint;
|
---|
| 430 |
|
---|
| 431 | float mMaxT;
|
---|
| 432 |
|
---|
| 433 | LineTraversalData () {}
|
---|
| 434 | LineTraversalData (BvhNode *n, const Vector3 &p, const float maxt):
|
---|
| 435 | mNode(n), mExitPoint(p), mMaxT(maxt) {}
|
---|
| 436 | };
|
---|
| 437 |
|
---|
| 438 |
|
---|
| 439 | /** Default constructor creating an empty tree.
|
---|
| 440 | */
|
---|
| 441 | BvHierarchy();
|
---|
| 442 |
|
---|
| 443 | /** Default destructor.
|
---|
| 444 | */
|
---|
| 445 | ~BvHierarchy();
|
---|
| 446 |
|
---|
| 447 | /** Returns tree statistics.
|
---|
| 448 | */
|
---|
| 449 | const BvhStatistics &GetStatistics() const;
|
---|
| 450 |
|
---|
| 451 | /** Returns bounding box of the specified node.
|
---|
| 452 | */
|
---|
| 453 | AxisAlignedBox3 GetBoundingBox(BvhNode *node) const;
|
---|
| 454 |
|
---|
| 455 | /** Reads parameters from environment singleton.
|
---|
| 456 | */
|
---|
| 457 | void ReadEnvironment();
|
---|
| 458 |
|
---|
| 459 | /** Evaluates candidate for splitting.
|
---|
| 460 | */
|
---|
| 461 | void EvalSubdivisionCandidate(BvhSubdivisionCandidate &splitData);
|
---|
| 462 |
|
---|
| 463 | /** Returns list of leaves with pvs smaller than
|
---|
| 464 | a certain threshold.
|
---|
| 465 | @param onlyUnmailed if only the unmailed leaves should be considered
|
---|
| 466 | @param maxPvs the maximal pvs of a leaf to be added (-1 means unlimited)
|
---|
| 467 | */
|
---|
| 468 | void CollectLeaves(vector<BvhLeaf *> &leaves) const;
|
---|
| 469 |
|
---|
| 470 | /** Returns bounding box of the whole tree (= bbox of root node)
|
---|
| 471 | */
|
---|
| 472 | AxisAlignedBox3 GetBoundingBox()const;
|
---|
| 473 |
|
---|
| 474 | /** Returns root of the view space partitioning tree.
|
---|
| 475 | */
|
---|
| 476 | BvhNode *GetRoot() const;
|
---|
| 477 |
|
---|
| 478 | /** A ray is cast possible intersecting the tree.
|
---|
| 479 | @param the ray that is cast.
|
---|
| 480 | @returns the number of intersections with objects stored in the tree.
|
---|
| 481 | */
|
---|
| 482 | //int CastRay(Ray &ray);
|
---|
| 483 |
|
---|
| 484 | /** finds neighbouring leaves of this tree node.
|
---|
| 485 | */
|
---|
| 486 | int FindNeighbors(BvhLeaf *n,
|
---|
| 487 | vector<BvhLeaf *> &neighbors,
|
---|
| 488 | const bool onlyUnmailed) const;
|
---|
| 489 |
|
---|
| 490 | /** Returns random leaf of BSP tree.
|
---|
| 491 | @param halfspace defines the halfspace from which the leaf is taken.
|
---|
| 492 | */
|
---|
| 493 | BvhLeaf *GetRandomLeaf(const Plane3 &halfspace);
|
---|
| 494 |
|
---|
| 495 | /** Returns random leaf of BSP tree.
|
---|
| 496 | @param onlyUnmailed if only unmailed leaves should be returned.
|
---|
| 497 | */
|
---|
| 498 | BvhLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
|
---|
| 499 |
|
---|
| 500 | /** Casts line segment into the tree.
|
---|
| 501 | @param origin the origin of the line segment
|
---|
| 502 | @param termination the end point of the line segment
|
---|
| 503 | @returns view cells intersecting the line segment.
|
---|
| 504 | */
|
---|
| 505 | int CastLineSegment(const Vector3 &origin,
|
---|
| 506 | const Vector3 &termination,
|
---|
| 507 | ViewCellContainer &viewcells);
|
---|
| 508 |
|
---|
| 509 | /** Sets pointer to view cells manager.
|
---|
| 510 | */
|
---|
| 511 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
| 512 |
|
---|
| 513 | /** Writes tree to output stream
|
---|
| 514 | */
|
---|
| 515 | bool Export(OUT_STREAM &stream);
|
---|
| 516 |
|
---|
| 517 | /** Returns or creates a new intersectable for use in a kd based pvs.
|
---|
| 518 | The OspTree is responsible for destruction of the intersectable.
|
---|
| 519 | */
|
---|
| 520 | BvhIntersectable *GetOrCreateBvhIntersectable(BvhNode *node);
|
---|
| 521 |
|
---|
| 522 | /** Collects rays.
|
---|
| 523 | */
|
---|
| 524 | void CollectRays(const ObjectContainer &objects, VssRayContainer &rays) const;
|
---|
| 525 |
|
---|
| 526 | /** Intersects box with the tree and returns the number of intersected boxes.
|
---|
| 527 | @returns number of view cells found
|
---|
| 528 | */
|
---|
[1259] | 529 | int ComputeBoxIntersections(
|
---|
| 530 | const AxisAlignedBox3 &box,
|
---|
| 531 | ViewCellContainer &viewCells) const;
|
---|
[1237] | 532 |
|
---|
| 533 | /** Returns leaf the point pt lies in, starting from root.
|
---|
| 534 | */
|
---|
| 535 | BvhLeaf *GetLeaf(Intersectable *obj, BvhNode *root = NULL) const;
|
---|
| 536 |
|
---|
[1370] | 537 | /** Sets a pointer to the view cells tree.
|
---|
| 538 | */
|
---|
[1237] | 539 | ViewCellsTree *GetViewCellsTree() const { return mViewCellsTree; }
|
---|
[1370] | 540 | /** See Get
|
---|
| 541 | */
|
---|
[1237] | 542 | void SetViewCellsTree(ViewCellsTree *vt) { mViewCellsTree = vt; }
|
---|
| 543 |
|
---|
| 544 |
|
---|
| 545 | protected:
|
---|
| 546 |
|
---|
| 547 | /** Returns true if tree can be terminated.
|
---|
| 548 | */
|
---|
[1251] | 549 | bool LocalTerminationCriteriaMet(const BvhTraversalData &data) const;
|
---|
[1237] | 550 |
|
---|
| 551 | /** Returns true if global tree can be terminated.
|
---|
| 552 | */
|
---|
[1251] | 553 | bool GlobalTerminationCriteriaMet(const BvhTraversalData &data) const;
|
---|
[1237] | 554 |
|
---|
[1287] | 555 | /** For sorting the objects during the heuristics
|
---|
| 556 | */
|
---|
[1237] | 557 | struct SortableEntry
|
---|
| 558 | {
|
---|
[1287] | 559 | Intersectable *mObject;
|
---|
[1237] | 560 | float mPos;
|
---|
| 561 |
|
---|
| 562 | SortableEntry() {}
|
---|
| 563 |
|
---|
[1287] | 564 | SortableEntry(Intersectable *obj, const float pos):
|
---|
| 565 | mObject(obj), mPos(pos)
|
---|
[1237] | 566 | {}
|
---|
| 567 |
|
---|
| 568 | bool operator<(const SortableEntry &b) const
|
---|
| 569 | {
|
---|
| 570 | return mPos < b.mPos;
|
---|
| 571 | }
|
---|
| 572 | };
|
---|
[1345] | 573 |
|
---|
[1287] | 574 | /** Evaluate balanced object partition.
|
---|
[1237] | 575 | */
|
---|
[1287] | 576 | float EvalLocalObjectPartition(
|
---|
| 577 | const BvhTraversalData &tData,
|
---|
| 578 | const int axis,
|
---|
| 579 | ObjectContainer &objectsFront,
|
---|
| 580 | ObjectContainer &objectsBack);
|
---|
[1237] | 581 |
|
---|
[1323] | 582 | float EvalSah(
|
---|
| 583 | const BvhTraversalData &tData,
|
---|
| 584 | const int axis,
|
---|
| 585 | ObjectContainer &objectsFront,
|
---|
| 586 | ObjectContainer &objectsBack);
|
---|
| 587 |
|
---|
[1237] | 588 | /** Computes priority of the traversal data and stores it in tData.
|
---|
| 589 | */
|
---|
| 590 | void EvalPriority(BvhTraversalData &tData) const;
|
---|
| 591 |
|
---|
[1379] | 592 | /** Evaluates render cost of the bv induced by these objects
|
---|
[1237] | 593 | */
|
---|
[1379] | 594 | float EvalRenderCost(const ObjectContainer &objects) const;
|
---|
[1237] | 595 |
|
---|
| 596 | /** Evaluates tree stats in the BSP tree leafs.
|
---|
| 597 | */
|
---|
| 598 | void EvaluateLeafStats(const BvhTraversalData &data);
|
---|
| 599 |
|
---|
| 600 | /** Subdivides node using a best split priority queue.
|
---|
| 601 | @param tQueue the best split priority queue
|
---|
| 602 | @param splitCandidate the candidate for the next split
|
---|
| 603 | @param globalCriteriaMet if the global termination criteria were already met
|
---|
| 604 | @returns new root of the subtree
|
---|
| 605 | */
|
---|
| 606 | BvhNode *Subdivide(
|
---|
| 607 | SplitQueue &tQueue,
|
---|
| 608 | SubdivisionCandidate *splitCandidate,
|
---|
| 609 | const bool globalCriteriaMet);
|
---|
| 610 |
|
---|
| 611 | /** Subdivides leaf.
|
---|
[1345] | 612 | @param sc the subdivisionCandidate holding all necessary data for subdivision
|
---|
[1237] | 613 |
|
---|
[1345] | 614 | @param frontData returns the traversal data for the front node
|
---|
| 615 | @param backData returns the traversal data for the back node
|
---|
[1237] | 616 |
|
---|
[1345] | 617 | @returns the new interior node = the of the subdivision
|
---|
[1237] | 618 | */
|
---|
| 619 | BvhInterior *SubdivideNode(
|
---|
[1345] | 620 | const BvhSubdivisionCandidate &sc,
|
---|
[1237] | 621 | BvhTraversalData &frontData,
|
---|
| 622 | BvhTraversalData &backData);
|
---|
| 623 |
|
---|
| 624 | /** Splits the objects for the next subdivision.
|
---|
| 625 | @returns cost for this split
|
---|
| 626 | */
|
---|
| 627 | float SelectObjectPartition(
|
---|
| 628 | const BvhTraversalData &tData,
|
---|
| 629 | ObjectContainer &frontObjects,
|
---|
| 630 | ObjectContainer &backObjects);
|
---|
| 631 |
|
---|
| 632 | /** Writes the node to disk
|
---|
| 633 | @note: should be implemented as visitor.
|
---|
| 634 | */
|
---|
| 635 | void ExportNode(BvhNode *node, OUT_STREAM &stream);
|
---|
| 636 |
|
---|
[1286] | 637 | void ExportObjects(BvhLeaf *leaf, OUT_STREAM &stream);
|
---|
| 638 |
|
---|
[1237] | 639 | /** Returns estimated memory usage of tree.
|
---|
| 640 | */
|
---|
| 641 | float GetMemUsage() const;
|
---|
| 642 |
|
---|
[1294] | 643 | /** Creates new root of hierarchy.
|
---|
| 644 | */
|
---|
| 645 | void CreateRoot(const ObjectContainer &objects);
|
---|
[1237] | 646 |
|
---|
| 647 | /////////////////////////////
|
---|
| 648 | // Helper functions for local cost heuristics
|
---|
| 649 |
|
---|
| 650 |
|
---|
[1357] | 651 | /** Prepare split candidates for cost heuristics using axis aligned splits.
|
---|
[1237] | 652 | @param node the current node
|
---|
| 653 | @param axis the current split axis
|
---|
| 654 | */
|
---|
[1357] | 655 | void PrepareLocalSubdivisionCandidates(
|
---|
| 656 | const BvhTraversalData &tData,
|
---|
[1287] | 657 | const int axis);
|
---|
[1237] | 658 |
|
---|
[1357] | 659 | static void CreateLocalSubdivisionCandidates(
|
---|
| 660 | const ObjectContainer &objects,
|
---|
| 661 | SortableEntryContainer **subdivisionCandidates,
|
---|
| 662 | const bool sort,
|
---|
| 663 | const int axis);
|
---|
| 664 |
|
---|
| 665 | /** Computes object partition with the best cost according to the heurisics.
|
---|
| 666 | @param tData the traversal data
|
---|
| 667 | @param axis the split axis
|
---|
| 668 | @param objectsFront the objects in the front child bv
|
---|
| 669 | @param objectsBack the objects in the back child bv
|
---|
| 670 | @param backObjectsStart the iterator marking the position where the back objects begin
|
---|
| 671 |
|
---|
| 672 | @returns relative cost (relative to parent cost)
|
---|
[1237] | 673 | */
|
---|
| 674 | float EvalLocalCostHeuristics(
|
---|
| 675 | const BvhTraversalData &tData,
|
---|
| 676 | const int axis,
|
---|
| 677 | ObjectContainer &objectsFront,
|
---|
[1357] | 678 | ObjectContainer &objectsBack);
|
---|
[1237] | 679 |
|
---|
[1287] | 680 | /** Evaluates the contribution to the front and back volume
|
---|
| 681 | when this object is changing sides in the bvs.
|
---|
[1237] | 682 |
|
---|
[1287] | 683 | @param object the object
|
---|
| 684 | @param volLeft updates the left pvs
|
---|
| 685 | @param volPvs updates the right pvs
|
---|
[1237] | 686 | */
|
---|
| 687 | void EvalHeuristicsContribution(
|
---|
[1287] | 688 | Intersectable *obj,
|
---|
[1237] | 689 | float &volLeft,
|
---|
[1287] | 690 | float &volRight);
|
---|
[1237] | 691 |
|
---|
| 692 | /** Prepares objects for the cost heuristics.
|
---|
| 693 | @returns sum of volume of associated view cells
|
---|
| 694 | */
|
---|
[1287] | 695 | float PrepareHeuristics(const BvhTraversalData &tData, const int axis);
|
---|
[1237] | 696 |
|
---|
| 697 | ////////////////////////////////////////////////
|
---|
| 698 |
|
---|
| 699 |
|
---|
| 700 | /** Prepares construction for vsp and osp trees.
|
---|
| 701 | */
|
---|
[1287] | 702 | AxisAlignedBox3 ComputeBoundingBox(
|
---|
| 703 | const ObjectContainer &objects,
|
---|
[1379] | 704 | const AxisAlignedBox3 *parentBox = NULL) const;
|
---|
[1237] | 705 |
|
---|
[1370] | 706 | /** Collects list of invalid candidates. Candidates
|
---|
| 707 | are invalidated by a view space subdivision step
|
---|
| 708 | that affects this candidate.
|
---|
| 709 | */
|
---|
[1287] | 710 | void CollectDirtyCandidates(
|
---|
| 711 | BvhSubdivisionCandidate *sc,
|
---|
[1237] | 712 | vector<SubdivisionCandidate *> &dirtyList);
|
---|
| 713 |
|
---|
[1287] | 714 | /** Collect view cells which see this bvh leaf.
|
---|
[1237] | 715 | */
|
---|
[1287] | 716 | void CollectViewCells(
|
---|
| 717 | const ObjectContainer &objects,
|
---|
| 718 | ViewCellContainer &viewCells,
|
---|
| 719 | const bool setCounter = false) const;
|
---|
[1237] | 720 |
|
---|
[1287] | 721 | /** Collects view cells which see an object.
|
---|
| 722 | */
|
---|
| 723 | void CollectViewCells(
|
---|
| 724 | Intersectable *object,
|
---|
| 725 | ViewCellContainer &viewCells,
|
---|
| 726 | const bool useMailBoxing,
|
---|
| 727 | const bool setCounter = false) const;
|
---|
| 728 |
|
---|
[1237] | 729 | /** Rays will be clipped to the bounding box.
|
---|
| 730 | */
|
---|
| 731 | void PreprocessRays(
|
---|
| 732 | BvhLeaf *root,
|
---|
| 733 | const VssRayContainer &sampleRays,
|
---|
| 734 | RayInfoContainer &rays);
|
---|
| 735 |
|
---|
[1287] | 736 | /** Print the subdivision stats in the subdivison log.
|
---|
| 737 | */
|
---|
| 738 | void PrintSubdivisionStats(const SubdivisionCandidate &tData);
|
---|
[1237] | 739 |
|
---|
[1370] | 740 | /** Prints out the stats for this subdivision.
|
---|
| 741 | */
|
---|
[1237] | 742 | void AddSubdivisionStats(
|
---|
| 743 | const int viewCells,
|
---|
| 744 | const float renderCostDecr,
|
---|
| 745 | const float totalRenderCost);
|
---|
| 746 |
|
---|
[1370] | 747 | /** Stores rays with objects that see the rays.
|
---|
| 748 | */
|
---|
| 749 | int AssociateObjectsWithRays(const VssRayContainer &rays) const;
|
---|
[1237] | 750 |
|
---|
[1370] | 751 | /** Tests if object is in this leaf.
|
---|
| 752 | @note: assumes that objects are sorted by their id.
|
---|
| 753 | */
|
---|
[1237] | 754 | bool IsObjectInLeaf(BvhLeaf *, Intersectable *object) const;
|
---|
| 755 |
|
---|
[1370] | 756 | /** Prepares the construction of the bv hierarchy and returns
|
---|
| 757 | the first subdivision candidate.
|
---|
| 758 | */
|
---|
[1287] | 759 | SubdivisionCandidate *PrepareConstruction(
|
---|
| 760 | const VssRayContainer &sampleRays,
|
---|
[1370] | 761 | const ObjectContainer &objects);
|
---|
[1237] | 762 |
|
---|
[1370] | 763 | /** Evaluates volume of view cells that see the objects.
|
---|
| 764 | */
|
---|
[1287] | 765 | float EvalViewCellsVolume(const ObjectContainer &objects) const;
|
---|
[1237] | 766 |
|
---|
[1370] | 767 | /** Creates initial list of sorted objects.
|
---|
| 768 | */
|
---|
[1357] | 769 | void CreateInitialSortedObjectList(BvhTraversalData &tData);
|
---|
[1259] | 770 |
|
---|
[1370] | 771 | /** Assigns sorted objects to front and back data.
|
---|
| 772 | */
|
---|
[1357] | 773 | void AssignSortedObjects(
|
---|
| 774 | const BvhSubdivisionCandidate &sc,
|
---|
| 775 | BvhTraversalData &frontData,
|
---|
| 776 | BvhTraversalData &backData);
|
---|
| 777 |
|
---|
| 778 |
|
---|
[1237] | 779 | protected:
|
---|
| 780 |
|
---|
[1345] | 781 | /// pre-sorted subdivision candidtes for all three directions.
|
---|
| 782 | vector<SortableEntry> *mGlobalSubdivisionCandidates[3];
|
---|
[1237] | 783 | /// pointer to the hierarchy of view cells
|
---|
| 784 | ViewCellsTree *mViewCellsTree;
|
---|
| 785 | /// The view cells manager
|
---|
| 786 | ViewCellsManager *mViewCellsManager;
|
---|
| 787 | /// candidates for placing split planes during cost heuristics
|
---|
| 788 | vector<SortableEntry> *mSubdivisionCandidates;
|
---|
| 789 | /// Pointer to the root of the tree
|
---|
| 790 | BvhNode *mRoot;
|
---|
| 791 | /// Statistics for the object space partition
|
---|
[1370] | 792 | BvhStatistics mBvhStats;
|
---|
[1237] | 793 | /// box around the whole view domain
|
---|
| 794 | AxisAlignedBox3 mBoundingBox;
|
---|
[1370] | 795 | /// the hierarchy manager
|
---|
| 796 | HierarchyManager *mHierarchyManager;
|
---|
[1237] | 797 |
|
---|
| 798 |
|
---|
[1357] | 799 | ////////////////////////////////
|
---|
| 800 | //-- local termination criteria
|
---|
[1237] | 801 |
|
---|
| 802 | /// maximal possible depth
|
---|
| 803 | int mTermMaxDepth;
|
---|
| 804 | /// mininum probability
|
---|
[1287] | 805 | float mTermMinProbability;
|
---|
[1237] | 806 | /// minimal number of objects
|
---|
| 807 | int mTermMinObjects;
|
---|
| 808 | /// maximal acceptable cost ratio
|
---|
| 809 | float mTermMaxCostRatio;
|
---|
| 810 | /// tolerance value indicating how often the max cost ratio can be failed
|
---|
| 811 | int mTermMissTolerance;
|
---|
[1370] | 812 | /// minimum number of rays
|
---|
| 813 | int mTermMinRays;
|
---|
[1237] | 814 |
|
---|
| 815 |
|
---|
[1357] | 816 | ////////////////////////////////////
|
---|
| 817 | //-- global termination criteria
|
---|
[1237] | 818 |
|
---|
| 819 | float mTermMinGlobalCostRatio;
|
---|
| 820 | int mTermGlobalCostMissTolerance;
|
---|
| 821 | int mGlobalCostMisses;
|
---|
| 822 |
|
---|
| 823 | /// maximal number of view cells
|
---|
| 824 | int mTermMaxLeaves;
|
---|
| 825 | /// maximal tree memory
|
---|
| 826 | float mMaxMemory;
|
---|
| 827 | /// the tree is out of memory
|
---|
| 828 | bool mOutOfMemory;
|
---|
| 829 |
|
---|
| 830 |
|
---|
[1357] | 831 | ////////////////////////////////////////
|
---|
[1237] | 832 | //-- split heuristics based parameters
|
---|
| 833 |
|
---|
| 834 | bool mUseCostHeuristics;
|
---|
| 835 | /// balancing factor for PVS criterium
|
---|
| 836 | float mCtDivCi;
|
---|
| 837 | /// if only driving axis should be used for split
|
---|
| 838 | bool mOnlyDrivingAxis;
|
---|
| 839 | /// current time stamp (used for keeping split history)
|
---|
| 840 | int mTimeStamp;
|
---|
| 841 | // if rays should be stored in leaves
|
---|
| 842 | bool mStoreRays;
|
---|
[1357] | 843 | // subdivision stats output file
|
---|
[1237] | 844 | ofstream mSubdivisionStats;
|
---|
| 845 | /// keeps track of cost during subdivision
|
---|
| 846 | float mTotalCost;
|
---|
| 847 | /// keeps track of overall pvs size during subdivision
|
---|
| 848 | int mTotalPvsSize;
|
---|
| 849 | /// number of currenly generated view cells
|
---|
| 850 | int mCreatedLeaves;
|
---|
| 851 | /// represents min and max band for sweep
|
---|
| 852 | float mSplitBorder;
|
---|
| 853 | /// weight between render cost decrease and node render cost
|
---|
| 854 | float mRenderCostDecreaseWeight;
|
---|
| 855 | /// stores the kd node intersectables used for pvs
|
---|
| 856 | BvhIntersectableMap mBvhIntersectables;
|
---|
| 857 |
|
---|
[1357] | 858 | bool mUseGlobalSorting;
|
---|
[1237] | 859 | };
|
---|
| 860 |
|
---|
| 861 | }
|
---|
| 862 |
|
---|
| 863 | #endif
|
---|