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