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