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