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