[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"
|
---|
[1667] | 15 | #include "HierarchyManager.h"
|
---|
[2227] | 16 | #include "Timer/PerfTimer.h"
|
---|
[1237] | 17 |
|
---|
| 18 |
|
---|
| 19 | namespace GtpVisibilityPreprocessor {
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 | class ViewCellLeaf;
|
---|
| 23 | class Plane3;
|
---|
| 24 | class AxisAlignedBox3;
|
---|
| 25 | class Ray;
|
---|
| 26 | class ViewCellsStatistics;
|
---|
| 27 | class ViewCellsManager;
|
---|
| 28 | class MergeCandidate;
|
---|
| 29 | class Beam;
|
---|
| 30 | class ViewCellsTree;
|
---|
| 31 | class Environment;
|
---|
| 32 | class BvhInterior;
|
---|
| 33 | class BvhLeaf;
|
---|
| 34 | class BvhNode;
|
---|
| 35 | class BvhTree;
|
---|
| 36 | class VspTree;
|
---|
[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 |
|
---|
[1763] | 65 |
|
---|
[1237] | 66 | void Reset()
|
---|
| 67 | {
|
---|
| 68 | nodes = 0;
|
---|
| 69 | splits = 0;
|
---|
| 70 | maxDepth = 0;
|
---|
[1705] | 71 |
|
---|
[1237] | 72 | minDepth = 99999;
|
---|
| 73 | accumDepth = 0;
|
---|
| 74 | maxDepthNodes = 0;
|
---|
| 75 | minProbabilityNodes = 0;
|
---|
| 76 | maxCostNodes = 0;
|
---|
[1370] | 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 |
|
---|
[2176] | 139 | void Print(std::ostream &app) const;
|
---|
[1237] | 140 |
|
---|
[2176] | 141 | friend std::ostream &operator<<(std::ostream &s, const BvhStatistics &stat)
|
---|
[1237] | 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 | */
|
---|
[1758] | 152 | class BvhNode: public Intersectable
|
---|
[1237] | 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 |
|
---|
[1666] | 183 | /** collects all objects under this node.
|
---|
| 184 | */
|
---|
[1614] | 185 | virtual void CollectObjects(ObjectContainer &objects) = 0;
|
---|
[1666] | 186 |
|
---|
[1237] | 187 | /** The bounding box specifies the node extent.
|
---|
| 188 | */
|
---|
[1357] | 189 | inline
|
---|
[1237] | 190 | AxisAlignedBox3 GetBoundingBox() const
|
---|
| 191 | { return mBoundingBox; }
|
---|
| 192 |
|
---|
[1666] | 193 | /** Sets bouding box of this node.
|
---|
| 194 | */
|
---|
[1357] | 195 | inline
|
---|
[1237] | 196 | void SetBoundingBox(const AxisAlignedBox3 &boundingBox)
|
---|
| 197 | { mBoundingBox = boundingBox; }
|
---|
| 198 |
|
---|
[1679] | 199 | /** Cost of mergin this node.
|
---|
| 200 | */
|
---|
| 201 | float GetMergeCost() {return (float)-mTimeStamp; }
|
---|
[1237] | 202 |
|
---|
[2115] | 203 | virtual int GetRandomEdgePoint(Vector3 &point, Vector3 &normal);
|
---|
[1666] | 204 |
|
---|
[1763] | 205 | inline int GetTimeStamp() const { return mTimeStamp; }
|
---|
| 206 | inline void SetTimeStamp(const int timeStamp) { mTimeStamp = timeStamp; };
|
---|
| 207 |
|
---|
[1237] | 208 |
|
---|
[1786] | 209 | ////////////////////////
|
---|
[1758] | 210 | //-- inherited functions from Intersectable
|
---|
| 211 |
|
---|
| 212 | AxisAlignedBox3 GetBox() const { return mBoundingBox; }
|
---|
| 213 |
|
---|
| 214 | int CastRay(Ray &ray) { return 0; }
|
---|
| 215 |
|
---|
| 216 | bool IsConvex() const { return true; }
|
---|
| 217 | bool IsWatertight() const { return true; }
|
---|
| 218 | float IntersectionComplexity() { return 1; }
|
---|
| 219 |
|
---|
| 220 | int NumberOfFaces() const { return 6; };
|
---|
| 221 |
|
---|
[2115] | 222 | int GetRandomSurfacePoint(GtpVisibilityPreprocessor::Vector3 &point,
|
---|
[1758] | 223 | GtpVisibilityPreprocessor::Vector3 &normal)
|
---|
| 224 | {
|
---|
| 225 | // TODO
|
---|
| 226 | return 0;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | int GetRandomVisibleSurfacePoint(GtpVisibilityPreprocessor::Vector3 &point,
|
---|
| 230 | GtpVisibilityPreprocessor::Vector3 &normal,
|
---|
| 231 | const GtpVisibilityPreprocessor::Vector3 &viewpoint,
|
---|
| 232 | const int maxTries)
|
---|
| 233 | {
|
---|
| 234 | // TODO
|
---|
| 235 | return 0;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | int Type() const
|
---|
| 239 | {
|
---|
| 240 | return Intersectable::BVH_INTERSECTABLE;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[2176] | 243 | std::ostream &Describe(std::ostream &s) { return s; }
|
---|
[1758] | 244 |
|
---|
[1237] | 245 | ///////////////////////////////////
|
---|
| 246 |
|
---|
[1786] | 247 | float mRenderCost;
|
---|
| 248 |
|
---|
[1237] | 249 | protected:
|
---|
| 250 |
|
---|
| 251 | /// the bounding box of the node
|
---|
| 252 | AxisAlignedBox3 mBoundingBox;
|
---|
| 253 | /// parent of this node
|
---|
| 254 | BvhInterior *mParent;
|
---|
[1763] | 255 | int mTimeStamp;
|
---|
[1237] | 256 | };
|
---|
| 257 |
|
---|
| 258 |
|
---|
| 259 | /** BSP interior node implementation
|
---|
| 260 | */
|
---|
| 261 | class BvhInterior: public BvhNode
|
---|
| 262 | {
|
---|
| 263 | public:
|
---|
| 264 | /** Standard contructor taking a bounding box as argument.
|
---|
| 265 | */
|
---|
| 266 | BvhInterior(const AxisAlignedBox3 &bbox);
|
---|
| 267 | BvhInterior(const AxisAlignedBox3 &bbox, BvhInterior *parent);
|
---|
| 268 |
|
---|
| 269 | ~BvhInterior();
|
---|
| 270 | /** @return false since it is an interior node
|
---|
| 271 | */
|
---|
| 272 | bool IsLeaf() const;
|
---|
[1294] | 273 |
|
---|
[1237] | 274 | BvhNode *GetBack() { return mBack; }
|
---|
| 275 | BvhNode *GetFront() { return mFront; }
|
---|
| 276 |
|
---|
| 277 | /** Replace front or back child with new child.
|
---|
| 278 | */
|
---|
| 279 | void ReplaceChildLink(BvhNode *oldChild, BvhNode *newChild);
|
---|
| 280 |
|
---|
| 281 | /** Replace front and back child.
|
---|
| 282 | */
|
---|
| 283 | void SetupChildLinks(BvhNode *front, BvhNode *back);
|
---|
| 284 |
|
---|
[2176] | 285 | friend std::ostream &operator<<(std::ostream &s, const BvhInterior &A)
|
---|
[1237] | 286 | {
|
---|
| 287 | return s << A.mBoundingBox;
|
---|
| 288 | }
|
---|
[1679] | 289 |
|
---|
| 290 | virtual void CollectObjects(ObjectContainer &objects);
|
---|
[1684] | 291 |
|
---|
[1237] | 292 | protected:
|
---|
| 293 |
|
---|
| 294 | /// back node
|
---|
| 295 | BvhNode *mBack;
|
---|
| 296 | /// front node
|
---|
| 297 | BvhNode *mFront;
|
---|
| 298 | };
|
---|
| 299 |
|
---|
| 300 |
|
---|
| 301 | /** BSP leaf node implementation.
|
---|
| 302 | */
|
---|
| 303 | class BvhLeaf: public BvhNode
|
---|
| 304 | {
|
---|
| 305 | public:
|
---|
| 306 | /** Standard contructor taking a bounding box as argument.
|
---|
| 307 | */
|
---|
| 308 | BvhLeaf(const AxisAlignedBox3 &bbox);
|
---|
[1920] | 309 | BvhLeaf(const AxisAlignedBox3 &bbox, BvhInterior *parent);
|
---|
[1237] | 310 | BvhLeaf(const AxisAlignedBox3 &bbox, BvhInterior *parent, const int numObjects);
|
---|
| 311 |
|
---|
| 312 | ~BvhLeaf();
|
---|
| 313 |
|
---|
| 314 | /** @return true since it is an interior node
|
---|
| 315 | */
|
---|
| 316 | bool IsLeaf() const;
|
---|
| 317 |
|
---|
[1297] | 318 | SubdivisionCandidate *GetSubdivisionCandidate()// const
|
---|
[1237] | 319 | {
|
---|
| 320 | return mSubdivisionCandidate;
|
---|
| 321 | }
|
---|
| 322 |
|
---|
[1297] | 323 | void SetSubdivisionCandidate(SubdivisionCandidate *candidate)
|
---|
| 324 | {
|
---|
| 325 | mSubdivisionCandidate = candidate;
|
---|
| 326 | }
|
---|
[1684] | 327 |
|
---|
| 328 | virtual void CollectObjects(ObjectContainer &objects);
|
---|
| 329 |
|
---|
[1707] | 330 | /** Returns level of the hierarchy that is "active" right now.
|
---|
[1706] | 331 | */
|
---|
| 332 | BvhNode *GetActiveNode()
|
---|
| 333 | {
|
---|
| 334 | return mActiveNode;
|
---|
| 335 | }
|
---|
| 336 |
|
---|
[1707] | 337 | /** Returns level of the hierarchy that is "active" right now.
|
---|
| 338 | */
|
---|
| 339 | void SetActiveNode(BvhNode *node)
|
---|
| 340 | {
|
---|
| 341 | mActiveNode = node;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
[1237] | 344 | public:
|
---|
[1785] | 345 | // gl list use to store the geometry on the gl server
|
---|
| 346 | int mGlList;
|
---|
| 347 |
|
---|
| 348 | /// objects
|
---|
| 349 | ObjectContainer mObjects;
|
---|
| 350 |
|
---|
[1920] | 351 |
|
---|
[1237] | 352 | protected:
|
---|
[1785] | 353 |
|
---|
| 354 | /// pointer to a split plane candidate splitting this leaf
|
---|
| 355 | SubdivisionCandidate *mSubdivisionCandidate;
|
---|
| 356 |
|
---|
| 357 | /// the active node which will be accounted for in the pvs
|
---|
| 358 | BvhNode *mActiveNode;
|
---|
[1237] | 359 | };
|
---|
| 360 |
|
---|
| 361 |
|
---|
| 362 | /** View Space Partitioning tree.
|
---|
| 363 | */
|
---|
| 364 | class BvHierarchy
|
---|
| 365 | {
|
---|
| 366 | friend class ViewCellsParseHandlers;
|
---|
[2119] | 367 | friend class ObjectsParseHandlers;
|
---|
[1237] | 368 | friend class HierarchyManager;
|
---|
| 369 |
|
---|
[1379] | 370 | protected:
|
---|
[1345] | 371 | struct SortableEntry;
|
---|
| 372 | typedef vector<SortableEntry> SortableEntryContainer;
|
---|
| 373 |
|
---|
[1379] | 374 | public:
|
---|
| 375 |
|
---|
[1237] | 376 | /** Additional data which is passed down the BSP tree during traversal.
|
---|
| 377 | */
|
---|
| 378 | class BvhTraversalData
|
---|
| 379 | {
|
---|
| 380 | public:
|
---|
[1294] | 381 |
|
---|
[1237] | 382 | BvhTraversalData():
|
---|
| 383 | mNode(NULL),
|
---|
| 384 | mDepth(0),
|
---|
| 385 | mMaxCostMisses(0),
|
---|
[1370] | 386 | mAxis(0),
|
---|
[1912] | 387 | mNumRays(0),
|
---|
| 388 | mCorrectedPvs(0),
|
---|
| 389 | mPvs(0),
|
---|
[1913] | 390 | mCorrectedVolume(0),
|
---|
| 391 | mVolume(0)
|
---|
[1357] | 392 | {
|
---|
[1778] | 393 | for (int i = 0; i < 4; ++ i)
|
---|
| 394 | mSortedObjects[i] = NULL;
|
---|
[1357] | 395 | }
|
---|
[1237] | 396 |
|
---|
| 397 | BvhTraversalData(BvhLeaf *node,
|
---|
| 398 | const int depth,
|
---|
[1370] | 399 | const float v,
|
---|
| 400 | const int numRays):
|
---|
[1237] | 401 | mNode(node),
|
---|
| 402 | mDepth(depth),
|
---|
| 403 | mMaxCostMisses(0),
|
---|
[1370] | 404 | mAxis(0),
|
---|
[1912] | 405 | mNumRays(numRays),
|
---|
| 406 | mCorrectedPvs(0),
|
---|
| 407 | mPvs(0),
|
---|
[1913] | 408 | mCorrectedVolume(0),
|
---|
[2210] | 409 | mVolume(v),
|
---|
| 410 | mSampledObjects(NULL)
|
---|
[1357] | 411 | {
|
---|
[1778] | 412 | for (int i = 0; i < 4; ++ i)
|
---|
| 413 | mSortedObjects[i] = NULL;
|
---|
[1357] | 414 | }
|
---|
[1237] | 415 |
|
---|
[1357] | 416 | /** Deletes contents and sets them to NULL.
|
---|
| 417 | */
|
---|
[1237] | 418 | void Clear()
|
---|
| 419 | {
|
---|
[1294] | 420 | DEL_PTR(mNode);
|
---|
[1778] | 421 | for (int i = 0; i < 4; ++ i)
|
---|
| 422 | DEL_PTR(mSortedObjects[i]);
|
---|
[2210] | 423 |
|
---|
| 424 | DEL_PTR(mSampledObjects);
|
---|
[1237] | 425 | }
|
---|
| 426 |
|
---|
[1294] | 427 | /// the current node
|
---|
| 428 | BvhLeaf *mNode;
|
---|
| 429 | /// current depth
|
---|
| 430 | int mDepth;
|
---|
[1913] | 431 | /// the volume of the node
|
---|
| 432 | float mVolume;
|
---|
| 433 | /// the corrected volume
|
---|
| 434 | float mCorrectedVolume;
|
---|
[1294] | 435 | /// how often this branch has missed the max-cost ratio
|
---|
| 436 | int mMaxCostMisses;
|
---|
| 437 | /// current axis
|
---|
| 438 | int mAxis;
|
---|
[1370] | 439 | /// number of rays
|
---|
| 440 | int mNumRays;
|
---|
[1912] | 441 | /// parent Pvs;
|
---|
| 442 | float mPvs;
|
---|
| 443 | /// parent pvs correction factor
|
---|
| 444 | float mCorrectedPvs;
|
---|
| 445 |
|
---|
[2210] | 446 | /** the sorted objects for the three dimensions + one for the original
|
---|
| 447 | order + one for the objects which have been sampled at least once
|
---|
| 448 | */
|
---|
[1778] | 449 | ObjectContainer *mSortedObjects[4];
|
---|
[2210] | 450 | ObjectContainer *mSampledObjects;
|
---|
[1237] | 451 | };
|
---|
| 452 |
|
---|
[1357] | 453 |
|
---|
| 454 | /** Candidate for a object space split.
|
---|
[1237] | 455 | */
|
---|
| 456 | class BvhSubdivisionCandidate: public SubdivisionCandidate
|
---|
| 457 | {
|
---|
| 458 | public:
|
---|
| 459 |
|
---|
[1294] | 460 | BvhSubdivisionCandidate(const BvhTraversalData &tData): mParentData(tData)
|
---|
[1237] | 461 | {};
|
---|
| 462 |
|
---|
[1305] | 463 | ~BvhSubdivisionCandidate()
|
---|
| 464 | {
|
---|
| 465 | mParentData.Clear();
|
---|
| 466 | }
|
---|
[1294] | 467 |
|
---|
[1237] | 468 | int Type() const { return OBJECT_SPACE; }
|
---|
| 469 |
|
---|
[1667] | 470 | void EvalCandidate(bool computeSplitplane = true)
|
---|
[1237] | 471 | {
|
---|
[1705] | 472 | mDirty = false;
|
---|
[2224] | 473 | sBvHierarchy->EvalSubdivisionCandidate(*this, computeSplitplane, true);
|
---|
[1237] | 474 | }
|
---|
| 475 |
|
---|
[2224] | 476 | bool Apply(SplitQueue &splitQueue, bool terminationCriteriaMet, SubdivisionCandidateContainer &dirtyList)
|
---|
[1632] | 477 | {
|
---|
[2224] | 478 | BvhNode *n = sBvHierarchy->Subdivide(splitQueue, this, terminationCriteriaMet, dirtyList);
|
---|
[1667] | 479 |
|
---|
[1632] | 480 | // local or global termination criteria failed
|
---|
| 481 | return !n->IsLeaf();
|
---|
[1633] | 482 | }
|
---|
[1632] | 483 |
|
---|
[1633] | 484 | void CollectDirtyCandidates(SubdivisionCandidateContainer &dirtyList,
|
---|
| 485 | const bool onlyUnmailed)
|
---|
| 486 | {
|
---|
| 487 | sBvHierarchy->CollectDirtyCandidates(this, dirtyList, onlyUnmailed);
|
---|
| 488 | }
|
---|
| 489 |
|
---|
[1237] | 490 | bool GlobalTerminationCriteriaMet() const
|
---|
| 491 | {
|
---|
| 492 | return sBvHierarchy->GlobalTerminationCriteriaMet(mParentData);
|
---|
| 493 | }
|
---|
| 494 |
|
---|
[1667] | 495 | BvhSubdivisionCandidate(const ObjectContainer &frontObjects,
|
---|
| 496 | const ObjectContainer &backObjects,
|
---|
| 497 | const BvhTraversalData &tData):
|
---|
[1237] | 498 | mFrontObjects(frontObjects), mBackObjects(backObjects), mParentData(tData)
|
---|
| 499 | {}
|
---|
[1294] | 500 |
|
---|
[1667] | 501 | float GetPriority() const
|
---|
| 502 | {
|
---|
[2187] | 503 | //return (float)-mParentData.mDepth;
|
---|
| 504 | return mPriority;
|
---|
[1667] | 505 | }
|
---|
| 506 |
|
---|
[1912] | 507 | /////////////////////////////7
|
---|
| 508 |
|
---|
[1305] | 509 | /// pointer to parent tree.
|
---|
[1294] | 510 | static BvHierarchy *sBvHierarchy;
|
---|
[1680] | 511 |
|
---|
[1294] | 512 | /// parent data
|
---|
| 513 | BvhTraversalData mParentData;
|
---|
[1305] | 514 | /// the objects on the front of the potential split
|
---|
[1294] | 515 | ObjectContainer mFrontObjects;
|
---|
[1305] | 516 | /// the objects on the back of the potential split
|
---|
[1294] | 517 | ObjectContainer mBackObjects;
|
---|
[1912] | 518 |
|
---|
[2210] | 519 | /// the sampled objects on the front of the potential split
|
---|
| 520 | ObjectContainer mSampledFrontObjects;
|
---|
| 521 | /// the sampled objects on the back of the potential split
|
---|
| 522 | ObjectContainer mSampledBackObjects;
|
---|
| 523 |
|
---|
[1912] | 524 | float mCorrectedFrontPvs;
|
---|
| 525 | float mCorrectedBackPvs;
|
---|
| 526 |
|
---|
[1913] | 527 | float mCorrectedFrontVolume;
|
---|
| 528 | float mCorrectedBackVolume;
|
---|
[2210] | 529 |
|
---|
| 530 | //int mNumFrontRays;
|
---|
| 531 | //int mNumBackRays;
|
---|
| 532 |
|
---|
| 533 | int mNumFrontViewCells;
|
---|
| 534 | int mNumBackViewCells;
|
---|
| 535 |
|
---|
| 536 | float mVolumeFrontViewCells;
|
---|
| 537 | float mVolumeBackViewCells;
|
---|
[1237] | 538 | };
|
---|
| 539 |
|
---|
| 540 | /** Struct for traversing line segment.
|
---|
| 541 | */
|
---|
| 542 | struct LineTraversalData
|
---|
| 543 | {
|
---|
| 544 | BvhNode *mNode;
|
---|
| 545 | Vector3 mExitPoint;
|
---|
| 546 |
|
---|
| 547 | float mMaxT;
|
---|
| 548 |
|
---|
| 549 | LineTraversalData () {}
|
---|
| 550 | LineTraversalData (BvhNode *n, const Vector3 &p, const float maxt):
|
---|
| 551 | mNode(n), mExitPoint(p), mMaxT(maxt) {}
|
---|
| 552 | };
|
---|
| 553 |
|
---|
| 554 |
|
---|
| 555 | /** Default constructor creating an empty tree.
|
---|
| 556 | */
|
---|
| 557 | BvHierarchy();
|
---|
| 558 |
|
---|
| 559 | /** Default destructor.
|
---|
| 560 | */
|
---|
| 561 | ~BvHierarchy();
|
---|
| 562 |
|
---|
| 563 | /** Returns tree statistics.
|
---|
| 564 | */
|
---|
| 565 | const BvhStatistics &GetStatistics() const;
|
---|
| 566 |
|
---|
| 567 | /** Returns bounding box of the specified node.
|
---|
| 568 | */
|
---|
| 569 | AxisAlignedBox3 GetBoundingBox(BvhNode *node) const;
|
---|
| 570 |
|
---|
| 571 | /** Reads parameters from environment singleton.
|
---|
| 572 | */
|
---|
| 573 | void ReadEnvironment();
|
---|
| 574 |
|
---|
| 575 | /** Evaluates candidate for splitting.
|
---|
| 576 | */
|
---|
[2224] | 577 | void EvalSubdivisionCandidate(BvhSubdivisionCandidate &splitData,
|
---|
| 578 | const bool computeSplitPlane,
|
---|
| 579 | const bool preprocessViewCells);
|
---|
[1237] | 580 |
|
---|
[1707] | 581 | /** Returns vector of leaves.
|
---|
[1237] | 582 | */
|
---|
[1707] | 583 | void CollectLeaves(BvhNode *root, vector<BvhLeaf *> &leaves) const;
|
---|
[1237] | 584 |
|
---|
[2093] | 585 | /** Returns vector of leaves.
|
---|
| 586 | */
|
---|
| 587 | void CollectNodes(BvhNode *root, vector<BvhNode *> &nodes) const;
|
---|
| 588 |
|
---|
[1237] | 589 | /** Returns bounding box of the whole tree (= bbox of root node)
|
---|
| 590 | */
|
---|
| 591 | AxisAlignedBox3 GetBoundingBox()const;
|
---|
| 592 |
|
---|
| 593 | /** Returns root of the view space partitioning tree.
|
---|
| 594 | */
|
---|
| 595 | BvhNode *GetRoot() const;
|
---|
| 596 |
|
---|
| 597 | /** finds neighbouring leaves of this tree node.
|
---|
| 598 | */
|
---|
| 599 | int FindNeighbors(BvhLeaf *n,
|
---|
| 600 | vector<BvhLeaf *> &neighbors,
|
---|
| 601 | const bool onlyUnmailed) const;
|
---|
| 602 |
|
---|
| 603 | /** Returns random leaf of BSP tree.
|
---|
| 604 | @param halfspace defines the halfspace from which the leaf is taken.
|
---|
| 605 | */
|
---|
| 606 | BvhLeaf *GetRandomLeaf(const Plane3 &halfspace);
|
---|
| 607 |
|
---|
| 608 | /** Returns random leaf of BSP tree.
|
---|
| 609 | @param onlyUnmailed if only unmailed leaves should be returned.
|
---|
| 610 | */
|
---|
| 611 | BvhLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
|
---|
| 612 |
|
---|
| 613 | /** Casts line segment into the tree.
|
---|
| 614 | @param origin the origin of the line segment
|
---|
| 615 | @param termination the end point of the line segment
|
---|
| 616 | @returns view cells intersecting the line segment.
|
---|
| 617 | */
|
---|
| 618 | int CastLineSegment(const Vector3 &origin,
|
---|
| 619 | const Vector3 &termination,
|
---|
| 620 | ViewCellContainer &viewcells);
|
---|
| 621 |
|
---|
| 622 | /** Sets pointer to view cells manager.
|
---|
| 623 | */
|
---|
| 624 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
| 625 |
|
---|
[1913] | 626 | float GetViewSpaceVolume() const;
|
---|
[1237] | 627 | /** Writes tree to output stream
|
---|
| 628 | */
|
---|
| 629 | bool Export(OUT_STREAM &stream);
|
---|
| 630 |
|
---|
[1640] | 631 | /** Collects rays associated with the objects.
|
---|
[1237] | 632 | */
|
---|
| 633 | void CollectRays(const ObjectContainer &objects, VssRayContainer &rays) const;
|
---|
| 634 |
|
---|
| 635 | /** Intersects box with the tree and returns the number of intersected boxes.
|
---|
| 636 | @returns number of view cells found
|
---|
| 637 | */
|
---|
[1640] | 638 | int ComputeBoxIntersections(const AxisAlignedBox3 &box,
|
---|
| 639 | ViewCellContainer &viewCells) const;
|
---|
[1237] | 640 |
|
---|
| 641 | /** Returns leaf the point pt lies in, starting from root.
|
---|
| 642 | */
|
---|
[2210] | 643 | inline BvhLeaf *GetLeaf(Intersectable *object, BvhNode *root = NULL) const
|
---|
| 644 | {
|
---|
| 645 | // hack: we use the simpler but faster version
|
---|
[2233] | 646 | //if (!object) return NULL;
|
---|
[1237] | 647 |
|
---|
[2210] | 648 | return object->mBvhLeaf;
|
---|
| 649 | }
|
---|
| 650 |
|
---|
[1370] | 651 | /** Sets a pointer to the view cells tree.
|
---|
| 652 | */
|
---|
[1237] | 653 | ViewCellsTree *GetViewCellsTree() const { return mViewCellsTree; }
|
---|
[1707] | 654 |
|
---|
[1370] | 655 | /** See Get
|
---|
| 656 | */
|
---|
[1237] | 657 | void SetViewCellsTree(ViewCellsTree *vt) { mViewCellsTree = vt; }
|
---|
| 658 |
|
---|
[1640] | 659 | /** Returns estimated memory usage of tree.
|
---|
| 660 | */
|
---|
| 661 | float GetMemUsage() const;
|
---|
[1237] | 662 |
|
---|
[1707] | 663 | /** Sets this node to be an active node.
|
---|
| 664 | */
|
---|
| 665 | void SetActive(BvhNode *node) const;
|
---|
[1680] | 666 |
|
---|
[1707] | 667 |
|
---|
[2210] | 668 | void StoreSampledObjects(ObjectContainer &sampledObjects, const ObjectContainer &objects);
|
---|
| 669 |
|
---|
[1684] | 670 | ///////////////////////////
|
---|
| 671 | // hacks in order to provide interleaved heurisitcs
|
---|
| 672 |
|
---|
[1686] | 673 | BvhNode *SubdivideAndCopy(SplitQueue &tQueue, SubdivisionCandidate *splitCandidate);
|
---|
[1684] | 674 |
|
---|
| 675 | /////////////////////////////////
|
---|
| 676 |
|
---|
[2233] | 677 | static inline float EvalAbsCost(const ObjectContainer &objects);
|
---|
[1698] | 678 |
|
---|
[1718] | 679 | void CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects);
|
---|
[1707] | 680 |
|
---|
[1786] | 681 | float GetRenderCostIncrementially(BvhNode *node) const;
|
---|
| 682 |
|
---|
[1844] | 683 | void Compress();
|
---|
[1843] | 684 | void CreateUniqueObjectIds();
|
---|
[1786] | 685 |
|
---|
[2187] | 686 | PerfTimer mNodeTimer;
|
---|
| 687 | PerfTimer mSubdivTimer;
|
---|
| 688 | PerfTimer mEvalTimer;
|
---|
| 689 | PerfTimer mSplitTimer;
|
---|
[2198] | 690 | PerfTimer mPlaneTimer;
|
---|
[2199] | 691 | PerfTimer mSortTimer;
|
---|
[2198] | 692 | PerfTimer mCollectTimer;
|
---|
[2187] | 693 |
|
---|
[1237] | 694 | protected:
|
---|
| 695 |
|
---|
| 696 | /** Returns true if tree can be terminated.
|
---|
| 697 | */
|
---|
[1251] | 698 | bool LocalTerminationCriteriaMet(const BvhTraversalData &data) const;
|
---|
[1237] | 699 |
|
---|
| 700 | /** Returns true if global tree can be terminated.
|
---|
| 701 | */
|
---|
[1251] | 702 | bool GlobalTerminationCriteriaMet(const BvhTraversalData &data) const;
|
---|
[1237] | 703 |
|
---|
[1287] | 704 | /** For sorting the objects during the heuristics
|
---|
| 705 | */
|
---|
[1237] | 706 | struct SortableEntry
|
---|
| 707 | {
|
---|
[1287] | 708 | Intersectable *mObject;
|
---|
[1237] | 709 | float mPos;
|
---|
| 710 |
|
---|
| 711 | SortableEntry() {}
|
---|
| 712 |
|
---|
[1287] | 713 | SortableEntry(Intersectable *obj, const float pos):
|
---|
| 714 | mObject(obj), mPos(pos)
|
---|
[1237] | 715 | {}
|
---|
| 716 |
|
---|
| 717 | bool operator<(const SortableEntry &b) const
|
---|
| 718 | {
|
---|
| 719 | return mPos < b.mPos;
|
---|
| 720 | }
|
---|
| 721 | };
|
---|
[1345] | 722 |
|
---|
[1287] | 723 | /** Evaluate balanced object partition.
|
---|
[1237] | 724 | */
|
---|
[1640] | 725 | float EvalLocalObjectPartition(const BvhTraversalData &tData,
|
---|
| 726 | const int axis,
|
---|
| 727 | ObjectContainer &objectsFront,
|
---|
| 728 | ObjectContainer &objectsBack);
|
---|
[1237] | 729 |
|
---|
[1640] | 730 | /** Evaluate surface area heuristic for the node.
|
---|
| 731 | */
|
---|
| 732 | float EvalSah(const BvhTraversalData &tData,
|
---|
| 733 | const int axis,
|
---|
| 734 | ObjectContainer &objectsFront,
|
---|
| 735 | ObjectContainer &objectsBack);
|
---|
[1323] | 736 |
|
---|
[2227] | 737 | float EvalSahWithTigherBbox(const BvhTraversalData &tData,
|
---|
| 738 | const int axis,
|
---|
| 739 | ObjectContainer &objectsFront,
|
---|
| 740 | ObjectContainer &objectsBack);
|
---|
[1237] | 741 |
|
---|
[1379] | 742 | /** Evaluates render cost of the bv induced by these objects
|
---|
[1237] | 743 | */
|
---|
[2198] | 744 | float EvalRenderCost(const ObjectContainer &objects);// const;
|
---|
[1237] | 745 |
|
---|
[2210] | 746 | float EvalProbability(const ObjectContainer &objects);
|
---|
| 747 |
|
---|
[1237] | 748 | /** Evaluates tree stats in the BSP tree leafs.
|
---|
| 749 | */
|
---|
| 750 | void EvaluateLeafStats(const BvhTraversalData &data);
|
---|
| 751 |
|
---|
| 752 | /** Subdivides node using a best split priority queue.
|
---|
| 753 | @param tQueue the best split priority queue
|
---|
| 754 | @param splitCandidate the candidate for the next split
|
---|
| 755 | @param globalCriteriaMet if the global termination criteria were already met
|
---|
| 756 | @returns new root of the subtree
|
---|
| 757 | */
|
---|
[1640] | 758 | BvhNode *Subdivide(SplitQueue &tQueue,
|
---|
| 759 | SubdivisionCandidate *splitCandidate,
|
---|
[2224] | 760 | const bool globalCriteriaMet
|
---|
| 761 | ,vector<SubdivisionCandidate *> &dirtyList
|
---|
| 762 | );
|
---|
[1237] | 763 |
|
---|
| 764 | /** Subdivides leaf.
|
---|
[1345] | 765 | @param sc the subdivisionCandidate holding all necessary data for subdivision
|
---|
[1237] | 766 |
|
---|
[1345] | 767 | @param frontData returns the traversal data for the front node
|
---|
| 768 | @param backData returns the traversal data for the back node
|
---|
[1237] | 769 |
|
---|
[1345] | 770 | @returns the new interior node = the of the subdivision
|
---|
[1237] | 771 | */
|
---|
[1640] | 772 | BvhInterior *SubdivideNode(const BvhSubdivisionCandidate &sc,
|
---|
| 773 | BvhTraversalData &frontData,
|
---|
| 774 | BvhTraversalData &backData);
|
---|
[1237] | 775 |
|
---|
| 776 | /** Splits the objects for the next subdivision.
|
---|
| 777 | @returns cost for this split
|
---|
| 778 | */
|
---|
[1640] | 779 | float SelectObjectPartition(const BvhTraversalData &tData,
|
---|
| 780 | ObjectContainer &frontObjects,
|
---|
[1676] | 781 | ObjectContainer &backObjects,
|
---|
| 782 | bool useVisibilityBasedHeuristics);
|
---|
[1237] | 783 |
|
---|
| 784 | /** Writes the node to disk
|
---|
| 785 | @note: should be implemented as visitor.
|
---|
| 786 | */
|
---|
| 787 | void ExportNode(BvhNode *node, OUT_STREAM &stream);
|
---|
| 788 |
|
---|
[1640] | 789 | /** Exports objects associated with this leaf.
|
---|
| 790 | */
|
---|
[1286] | 791 | void ExportObjects(BvhLeaf *leaf, OUT_STREAM &stream);
|
---|
| 792 |
|
---|
[1548] | 793 | /** Associates the objects with their bvh leaves.
|
---|
[1294] | 794 | */
|
---|
[1486] | 795 | static void AssociateObjectsWithLeaf(BvhLeaf *leaf);
|
---|
| 796 |
|
---|
[2198] | 797 |
|
---|
[1237] | 798 | /////////////////////////////
|
---|
| 799 | // Helper functions for local cost heuristics
|
---|
| 800 |
|
---|
[1357] | 801 | /** Prepare split candidates for cost heuristics using axis aligned splits.
|
---|
[1237] | 802 | @param node the current node
|
---|
| 803 | @param axis the current split axis
|
---|
| 804 | */
|
---|
[1640] | 805 | void PrepareLocalSubdivisionCandidates(const BvhTraversalData &tData,
|
---|
| 806 | const int axis);
|
---|
[1237] | 807 |
|
---|
[1640] | 808 | static void CreateLocalSubdivisionCandidates(const ObjectContainer &objects,
|
---|
| 809 | SortableEntryContainer **subdivisionCandidates,
|
---|
| 810 | const bool sort,
|
---|
| 811 | const int axis);
|
---|
[1357] | 812 |
|
---|
[1779] | 813 | float EvalPriority(const BvhSubdivisionCandidate &splitCandidate,
|
---|
| 814 | const float renderCostDecr,
|
---|
| 815 | const float oldRenderCost) const;
|
---|
| 816 |
|
---|
[1357] | 817 | /** Computes object partition with the best cost according to the heurisics.
|
---|
| 818 | @param tData the traversal data
|
---|
| 819 | @param axis the split axis
|
---|
| 820 | @param objectsFront the objects in the front child bv
|
---|
| 821 | @param objectsBack the objects in the back child bv
|
---|
| 822 | @param backObjectsStart the iterator marking the position where the back objects begin
|
---|
| 823 |
|
---|
| 824 | @returns relative cost (relative to parent cost)
|
---|
[1237] | 825 | */
|
---|
[1640] | 826 | float EvalLocalCostHeuristics(const BvhTraversalData &tData,
|
---|
| 827 | const int axis,
|
---|
| 828 | ObjectContainer &objectsFront,
|
---|
| 829 | ObjectContainer &objectsBack);
|
---|
[1237] | 830 |
|
---|
[1287] | 831 | /** Evaluates the contribution to the front and back volume
|
---|
| 832 | when this object is changing sides in the bvs.
|
---|
[1237] | 833 |
|
---|
[1287] | 834 | @param object the object
|
---|
| 835 | @param volLeft updates the left pvs
|
---|
| 836 | @param volPvs updates the right pvs
|
---|
[1237] | 837 | */
|
---|
[1640] | 838 | void EvalHeuristicsContribution(Intersectable *obj,
|
---|
| 839 | float &volLeft,
|
---|
| 840 | float &volRight);
|
---|
[1237] | 841 |
|
---|
| 842 | /** Prepares objects for the cost heuristics.
|
---|
| 843 | @returns sum of volume of associated view cells
|
---|
| 844 | */
|
---|
[1287] | 845 | float PrepareHeuristics(const BvhTraversalData &tData, const int axis);
|
---|
[1237] | 846 |
|
---|
[1705] | 847 | /** Evaluates cost for a leaf given the surface area heuristics.
|
---|
| 848 | */
|
---|
[1779] | 849 | float EvalSahCost(BvhLeaf *leaf) const;
|
---|
[1633] | 850 |
|
---|
[1237] | 851 | ////////////////////////////////////////////////
|
---|
| 852 |
|
---|
| 853 |
|
---|
| 854 | /** Prepares construction for vsp and osp trees.
|
---|
| 855 | */
|
---|
[1640] | 856 | AxisAlignedBox3 EvalBoundingBox(const ObjectContainer &objects,
|
---|
| 857 | const AxisAlignedBox3 *parentBox = NULL) const;
|
---|
[1237] | 858 |
|
---|
[1370] | 859 | /** Collects list of invalid candidates. Candidates
|
---|
| 860 | are invalidated by a view space subdivision step
|
---|
| 861 | that affects this candidate.
|
---|
| 862 | */
|
---|
[1640] | 863 | void CollectDirtyCandidates(BvhSubdivisionCandidate *sc,
|
---|
| 864 | vector<SubdivisionCandidate *> &dirtyList,
|
---|
| 865 | const bool onlyUnmailed);
|
---|
[1237] | 866 |
|
---|
[1287] | 867 | /** Collect view cells which see this bvh leaf.
|
---|
[1237] | 868 | */
|
---|
[1744] | 869 | int CollectViewCells(const ObjectContainer &objects,
|
---|
| 870 | ViewCellContainer &viewCells,
|
---|
| 871 | const bool setCounter,
|
---|
[2198] | 872 | const bool onlyUnmailedRays);// const;
|
---|
[1237] | 873 |
|
---|
[1933] | 874 | /** Collects view cells which see an object.
|
---|
| 875 | @param useMailBoxing if mailing should be used and
|
---|
| 876 | only unmailed object should pass
|
---|
| 877 | @param setCounter counter for the sweep algorithm
|
---|
[1941] | 878 | @param onlyUnmailedRays if only unmailed rays should be considered
|
---|
[1933] | 879 | */
|
---|
| 880 | int CollectViewCells(Intersectable *object,
|
---|
| 881 | ViewCellContainer &viewCells,
|
---|
| 882 | const bool useMailBoxing,
|
---|
| 883 | const bool setCounter,
|
---|
[2198] | 884 | const bool onlyUnmailedRays);// const;
|
---|
[1933] | 885 |
|
---|
[1576] | 886 | /** Counts the view cells of this object. note: only
|
---|
| 887 | counts unmailed objects.
|
---|
| 888 | */
|
---|
[2198] | 889 | int CountViewCells(Intersectable *obj);// const;
|
---|
[1576] | 890 |
|
---|
| 891 | /** Counts the view cells seen by this bvh leaf
|
---|
| 892 | */
|
---|
[2198] | 893 | int CountViewCells(const ObjectContainer &objects);// const;
|
---|
[1576] | 894 |
|
---|
[2198] | 895 | #if STORE_VIEWCELLS_WITH_BVH
|
---|
| 896 |
|
---|
| 897 | int AssociateViewCellsWithObject(Intersectable *obj, const bool useMailBoxing) const;
|
---|
| 898 |
|
---|
[2210] | 899 | void AssociateViewCellsWithObjects(const ObjectContainer &objects) const;
|
---|
[2198] | 900 |
|
---|
| 901 | void ReleaseViewCells(const ObjectContainer &objects);
|
---|
| 902 |
|
---|
| 903 | int CollectViewCellsFromRays(Intersectable *obj,
|
---|
| 904 | ViewCellContainer &viewCells,
|
---|
| 905 | const bool useMailBoxing,
|
---|
| 906 | const bool setCounter,
|
---|
| 907 | const bool onlyUnmailedRays);
|
---|
| 908 |
|
---|
| 909 | int CountViewCellsFromRays(Intersectable *obj);
|
---|
| 910 | #endif
|
---|
| 911 |
|
---|
[1576] | 912 | /** Evaluates increase in pvs size.
|
---|
| 913 | */
|
---|
[2198] | 914 | int EvalPvsEntriesIncr(BvhSubdivisionCandidate &splitCandidate,
|
---|
[2227] | 915 | const float raysPerObjects,
|
---|
[2210] | 916 | const int numParentViewCells,
|
---|
| 917 | const int numFrontViewCells,
|
---|
| 918 | const int numBackViewCells);// const;
|
---|
[1576] | 919 |
|
---|
[1237] | 920 | /** Rays will be clipped to the bounding box.
|
---|
| 921 | */
|
---|
[1640] | 922 | void PreprocessRays(BvhLeaf *root,
|
---|
| 923 | const VssRayContainer &sampleRays,
|
---|
| 924 | RayInfoContainer &rays);
|
---|
[1237] | 925 |
|
---|
[1287] | 926 | /** Print the subdivision stats in the subdivison log.
|
---|
| 927 | */
|
---|
| 928 | void PrintSubdivisionStats(const SubdivisionCandidate &tData);
|
---|
[1237] | 929 |
|
---|
[1370] | 930 | /** Prints out the stats for this subdivision.
|
---|
| 931 | */
|
---|
[1640] | 932 | void AddSubdivisionStats(const int viewCells,
|
---|
| 933 | const float renderCostDecr,
|
---|
| 934 | const float totalRenderCost);
|
---|
[1237] | 935 |
|
---|
[1370] | 936 | /** Stores rays with objects that see the rays.
|
---|
| 937 | */
|
---|
| 938 | int AssociateObjectsWithRays(const VssRayContainer &rays) const;
|
---|
[1237] | 939 |
|
---|
[1370] | 940 | /** Tests if object is in this leaf.
|
---|
| 941 | @note: assumes that objects are sorted by their id.
|
---|
| 942 | */
|
---|
[1237] | 943 | bool IsObjectInLeaf(BvhLeaf *, Intersectable *object) const;
|
---|
| 944 |
|
---|
[1370] | 945 | /** Prepares the construction of the bv hierarchy and returns
|
---|
| 946 | the first subdivision candidate.
|
---|
| 947 | */
|
---|
[1779] | 948 | void PrepareConstruction(SplitQueue &tQueue,
|
---|
| 949 | const VssRayContainer &sampleRays,
|
---|
| 950 | const ObjectContainer &objects);
|
---|
[1237] | 951 |
|
---|
[1548] | 952 | /** Resets bv hierarchy. E.g. deletes root and resets stats.
|
---|
| 953 | */
|
---|
[1779] | 954 | void Reset(SplitQueue &tQueue,
|
---|
| 955 | const VssRayContainer &rays,
|
---|
| 956 | const ObjectContainer &objects);
|
---|
[1548] | 957 |
|
---|
[1370] | 958 | /** Evaluates volume of view cells that see the objects.
|
---|
| 959 | */
|
---|
[2198] | 960 | float EvalViewCellsVolume(const ObjectContainer &objects);// const;
|
---|
[1237] | 961 |
|
---|
[1580] | 962 | /** Assigns or newly creates initial list of sorted objects.
|
---|
[1370] | 963 | */
|
---|
[1779] | 964 | void AssignInitialSortedObjectList(BvhTraversalData &tData,
|
---|
| 965 | const ObjectContainer &objects);
|
---|
[1259] | 966 |
|
---|
[1370] | 967 | /** Assigns sorted objects to front and back data.
|
---|
| 968 | */
|
---|
[1640] | 969 | void AssignSortedObjects(const BvhSubdivisionCandidate &sc,
|
---|
| 970 | BvhTraversalData &frontData,
|
---|
| 971 | BvhTraversalData &backData);
|
---|
[1548] | 972 |
|
---|
[1640] | 973 | /** Creates new root of hierarchy and computes bounding box.
|
---|
| 974 | Has to be called before the preparation of the subdivision.
|
---|
[1548] | 975 | */
|
---|
[1640] | 976 | void Initialise(const ObjectContainer &objects);
|
---|
| 977 |
|
---|
| 978 |
|
---|
[1779] | 979 | ////////////////////
|
---|
[1774] | 980 | // initial subdivision
|
---|
| 981 |
|
---|
| 982 | /** Makes an initial parititon of the object space based on
|
---|
| 983 | some criteria (size, shader)
|
---|
| 984 | */
|
---|
[1779] | 985 | void ApplyInitialSubdivision(SubdivisionCandidate *firstCandidate,
|
---|
[1789] | 986 | vector<SubdivisionCandidate *> &candidateContainer);
|
---|
[1774] | 987 |
|
---|
[1784] | 988 | void ApplyInitialSplit(const BvhTraversalData &tData,
|
---|
| 989 | ObjectContainer &frontObjects,
|
---|
| 990 | ObjectContainer &backObjects);
|
---|
[1774] | 991 |
|
---|
[1779] | 992 | bool InitialTerminationCriteriaMet(const BvhTraversalData &tData) const;
|
---|
| 993 |
|
---|
[2093] | 994 | /** Sets the bvh node ids.
|
---|
| 995 | */
|
---|
| 996 | void SetUniqueNodeIds();
|
---|
[1779] | 997 |
|
---|
[1237] | 998 | protected:
|
---|
| 999 |
|
---|
[1345] | 1000 | /// pre-sorted subdivision candidtes for all three directions.
|
---|
| 1001 | vector<SortableEntry> *mGlobalSubdivisionCandidates[3];
|
---|
[1237] | 1002 | /// pointer to the hierarchy of view cells
|
---|
| 1003 | ViewCellsTree *mViewCellsTree;
|
---|
| 1004 | /// The view cells manager
|
---|
| 1005 | ViewCellsManager *mViewCellsManager;
|
---|
| 1006 | /// candidates for placing split planes during cost heuristics
|
---|
| 1007 | vector<SortableEntry> *mSubdivisionCandidates;
|
---|
| 1008 | /// Pointer to the root of the tree
|
---|
| 1009 | BvhNode *mRoot;
|
---|
| 1010 | /// Statistics for the object space partition
|
---|
[1370] | 1011 | BvhStatistics mBvhStats;
|
---|
[1237] | 1012 | /// box around the whole view domain
|
---|
| 1013 | AxisAlignedBox3 mBoundingBox;
|
---|
[1370] | 1014 | /// the hierarchy manager
|
---|
| 1015 | HierarchyManager *mHierarchyManager;
|
---|
[1237] | 1016 |
|
---|
| 1017 |
|
---|
[1449] | 1018 | ////////////////////
|
---|
[1357] | 1019 | //-- local termination criteria
|
---|
[1237] | 1020 |
|
---|
| 1021 | /// maximal possible depth
|
---|
| 1022 | int mTermMaxDepth;
|
---|
| 1023 | /// mininum probability
|
---|
[1287] | 1024 | float mTermMinProbability;
|
---|
[1237] | 1025 | /// minimal number of objects
|
---|
| 1026 | int mTermMinObjects;
|
---|
| 1027 | /// maximal acceptable cost ratio
|
---|
| 1028 | float mTermMaxCostRatio;
|
---|
| 1029 | /// tolerance value indicating how often the max cost ratio can be failed
|
---|
| 1030 | int mTermMissTolerance;
|
---|
[1370] | 1031 | /// minimum number of rays
|
---|
| 1032 | int mTermMinRays;
|
---|
[1237] | 1033 |
|
---|
| 1034 |
|
---|
[1449] | 1035 | ////////////////////
|
---|
[1357] | 1036 | //-- global termination criteria
|
---|
[1237] | 1037 |
|
---|
[1580] | 1038 | /// the minimal accepted global cost ratio
|
---|
[1237] | 1039 | float mTermMinGlobalCostRatio;
|
---|
[1580] | 1040 | //// number of accepted misses of the global cost ratio
|
---|
[1237] | 1041 | int mTermGlobalCostMissTolerance;
|
---|
| 1042 | /// maximal number of view cells
|
---|
| 1043 | int mTermMaxLeaves;
|
---|
| 1044 | /// maximal tree memory
|
---|
| 1045 | float mMaxMemory;
|
---|
| 1046 | /// the tree is out of memory
|
---|
| 1047 | bool mOutOfMemory;
|
---|
| 1048 |
|
---|
| 1049 |
|
---|
[1357] | 1050 | ////////////////////////////////////////
|
---|
[1237] | 1051 | //-- split heuristics based parameters
|
---|
| 1052 |
|
---|
[1643] | 1053 | /// if a heuristics should be used for finding a split plane
|
---|
| 1054 | bool mUseCostHeuristics;
|
---|
| 1055 | /// if sah heuristcs should be used for finding a split plane
|
---|
| 1056 | bool mUseSah;
|
---|
| 1057 | /// balancing factor for PVS criterium
|
---|
[1237] | 1058 | float mCtDivCi;
|
---|
| 1059 | /// if only driving axis should be used for split
|
---|
| 1060 | bool mOnlyDrivingAxis;
|
---|
| 1061 | /// current time stamp (used for keeping split history)
|
---|
| 1062 | int mTimeStamp;
|
---|
| 1063 | // if rays should be stored in leaves
|
---|
| 1064 | bool mStoreRays;
|
---|
[1357] | 1065 | // subdivision stats output file
|
---|
[2176] | 1066 | std::ofstream mSubdivisionStats;
|
---|
[1237] | 1067 | /// keeps track of cost during subdivision
|
---|
| 1068 | float mTotalCost;
|
---|
[1662] | 1069 | int mPvsEntries;
|
---|
[1237] | 1070 | /// keeps track of overall pvs size during subdivision
|
---|
| 1071 | int mTotalPvsSize;
|
---|
| 1072 | /// number of currenly generated view cells
|
---|
| 1073 | int mCreatedLeaves;
|
---|
| 1074 | /// represents min and max band for sweep
|
---|
[2124] | 1075 | //float mSplitBorder;
|
---|
[1237] | 1076 | /// weight between render cost decrease and node render cost
|
---|
| 1077 | float mRenderCostDecreaseWeight;
|
---|
[1758] | 1078 |
|
---|
[1580] | 1079 | /// if the objects should be sorted in one global step
|
---|
| 1080 | bool mUseGlobalSorting;
|
---|
[1237] | 1081 |
|
---|
[1662] | 1082 | bool mUseBboxAreaForSah;
|
---|
| 1083 |
|
---|
[1779] | 1084 | //SortableEntryContainer *mSortedObjects[4];
|
---|
[1676] | 1085 |
|
---|
| 1086 | int mMinRaysForVisibility;
|
---|
[1727] | 1087 |
|
---|
[1732] | 1088 | /// constant value for driving the heuristics
|
---|
| 1089 | float mMemoryConst;
|
---|
| 1090 |
|
---|
[1786] | 1091 | int mMaxTests;
|
---|
| 1092 |
|
---|
[1779] | 1093 | bool mIsInitialSubdivision;
|
---|
| 1094 |
|
---|
| 1095 | bool mApplyInitialPartition;
|
---|
[1786] | 1096 |
|
---|
| 1097 | int mInitialMinObjects;
|
---|
| 1098 | float mInitialMaxAreaRatio;
|
---|
| 1099 | float mInitialMinArea;
|
---|
[1237] | 1100 | };
|
---|
| 1101 |
|
---|
[2233] | 1102 |
|
---|
| 1103 | float BvHierarchy::EvalAbsCost(const ObjectContainer &objects)
|
---|
| 1104 | {
|
---|
| 1105 | float result;
|
---|
| 1106 |
|
---|
| 1107 | #if USE_BETTER_RENDERCOST_EST
|
---|
| 1108 |
|
---|
| 1109 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
| 1110 |
|
---|
| 1111 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
| 1112 | {
|
---|
| 1113 | result += ViewCellsManager::GetRendercost(*oit);
|
---|
| 1114 | }
|
---|
| 1115 |
|
---|
| 1116 | #else
|
---|
| 1117 |
|
---|
| 1118 | result = (float)objects.size();
|
---|
| 1119 |
|
---|
| 1120 | #endif
|
---|
| 1121 |
|
---|
| 1122 | #if BOUND_RENDERCOST
|
---|
| 1123 | result = max(result, MIN_RENDERCOST);
|
---|
| 1124 | #endif
|
---|
| 1125 |
|
---|
| 1126 | return result;
|
---|
[1237] | 1127 | }
|
---|
| 1128 |
|
---|
[2233] | 1129 | }
|
---|
| 1130 |
|
---|
[1237] | 1131 | #endif
|
---|