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