[1237] | 1 | #include <stack>
|
---|
| 2 | #include <time.h>
|
---|
| 3 | #include <iomanip>
|
---|
| 4 |
|
---|
| 5 | #include "BvHierarchy.h"
|
---|
| 6 | #include "ViewCell.h"
|
---|
| 7 | #include "Plane3.h"
|
---|
| 8 | #include "Mesh.h"
|
---|
| 9 | #include "common.h"
|
---|
| 10 | #include "Environment.h"
|
---|
| 11 | #include "Polygon3.h"
|
---|
| 12 | #include "Ray.h"
|
---|
| 13 | #include "AxisAlignedBox3.h"
|
---|
| 14 | #include "Exporter.h"
|
---|
| 15 | #include "Plane3.h"
|
---|
| 16 | #include "ViewCellsManager.h"
|
---|
| 17 | #include "Beam.h"
|
---|
| 18 | #include "VspTree.h"
|
---|
[1370] | 19 | #include "HierarchyManager.h"
|
---|
[1237] | 20 |
|
---|
| 21 |
|
---|
| 22 | namespace GtpVisibilityPreprocessor {
|
---|
| 23 |
|
---|
| 24 |
|
---|
[1370] | 25 | #define PROBABILIY_IS_BV_VOLUME 1
|
---|
[1237] | 26 | #define USE_FIXEDPOINT_T 0
|
---|
| 27 |
|
---|
[1357] | 28 | int BvhNode::sMailId = 10000; //2147483647;
|
---|
[1291] | 29 | int BvhNode::sReservedMailboxes = 1;
|
---|
| 30 |
|
---|
[1237] | 31 | BvHierarchy *BvHierarchy::BvhSubdivisionCandidate::sBvHierarchy = NULL;
|
---|
| 32 |
|
---|
| 33 |
|
---|
[1357] | 34 | /// sorting operator
|
---|
[1237] | 35 | inline static bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
| 36 | {
|
---|
| 37 | return obj1->mId < obj2->mId;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | /***************************************************************/
|
---|
| 42 | /* class BvhNode implementation */
|
---|
| 43 | /***************************************************************/
|
---|
| 44 |
|
---|
[1297] | 45 | BvhNode::BvhNode(): mParent(NULL), mMailbox(0)
|
---|
[1237] | 46 | {
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | BvhNode::BvhNode(const AxisAlignedBox3 &bbox):
|
---|
[1297] | 50 | mParent(NULL), mBoundingBox(bbox), mMailbox(0)
|
---|
[1237] | 51 | {
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | BvhNode::BvhNode(const AxisAlignedBox3 &bbox, BvhInterior *parent):
|
---|
[1297] | 56 | mBoundingBox(bbox), mParent(parent), mMailbox(0)
|
---|
[1237] | 57 | {
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | bool BvhNode::IsRoot() const
|
---|
| 62 | {
|
---|
| 63 | return mParent == NULL;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 | BvhInterior *BvhNode::GetParent()
|
---|
| 68 | {
|
---|
| 69 | return mParent;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 |
|
---|
| 73 | void BvhNode::SetParent(BvhInterior *parent)
|
---|
| 74 | {
|
---|
| 75 | mParent = parent;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | /******************************************************************/
|
---|
| 81 | /* class BvhInterior implementation */
|
---|
| 82 | /******************************************************************/
|
---|
| 83 |
|
---|
| 84 |
|
---|
| 85 | BvhLeaf::BvhLeaf(const AxisAlignedBox3 &bbox):
|
---|
[1302] | 86 | BvhNode(bbox), mSubdivisionCandidate(NULL)
|
---|
[1237] | 87 | {
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 |
|
---|
| 91 | BvhLeaf::BvhLeaf(const AxisAlignedBox3 &bbox, BvhInterior *parent):
|
---|
| 92 | BvhNode(bbox, parent)
|
---|
| 93 | {
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 |
|
---|
[1287] | 97 | BvhLeaf::BvhLeaf(const AxisAlignedBox3 &bbox,
|
---|
| 98 | BvhInterior *parent,
|
---|
| 99 | const int numObjects):
|
---|
[1237] | 100 | BvhNode(bbox, parent)
|
---|
| 101 | {
|
---|
| 102 | mObjects.reserve(numObjects);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 |
|
---|
| 106 | bool BvhLeaf::IsLeaf() const
|
---|
| 107 | {
|
---|
| 108 | return true;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 |
|
---|
| 112 | BvhLeaf::~BvhLeaf()
|
---|
| 113 | {
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 |
|
---|
| 117 | /******************************************************************/
|
---|
| 118 | /* class BvhInterior implementation */
|
---|
| 119 | /******************************************************************/
|
---|
| 120 |
|
---|
| 121 |
|
---|
| 122 | BvhInterior::BvhInterior(const AxisAlignedBox3 &bbox):
|
---|
[1287] | 123 | BvhNode(bbox), mFront(NULL), mBack(NULL)
|
---|
[1237] | 124 | {
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 | BvhInterior::BvhInterior(const AxisAlignedBox3 &bbox, BvhInterior *parent):
|
---|
[1287] | 129 | BvhNode(bbox, parent), mFront(NULL), mBack(NULL)
|
---|
[1237] | 130 | {
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 | void BvhInterior::ReplaceChildLink(BvhNode *oldChild, BvhNode *newChild)
|
---|
| 135 | {
|
---|
| 136 | if (mBack == oldChild)
|
---|
| 137 | mBack = newChild;
|
---|
| 138 | else
|
---|
| 139 | mFront = newChild;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 |
|
---|
| 143 | bool BvhInterior::IsLeaf() const
|
---|
| 144 | {
|
---|
| 145 | return false;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 |
|
---|
| 149 | BvhInterior::~BvhInterior()
|
---|
| 150 | {
|
---|
| 151 | DEL_PTR(mFront);
|
---|
| 152 | DEL_PTR(mBack);
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 |
|
---|
| 156 | void BvhInterior::SetupChildLinks(BvhNode *front, BvhNode *back)
|
---|
| 157 | {
|
---|
| 158 | mBack = back;
|
---|
| 159 | mFront = front;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 |
|
---|
| 163 |
|
---|
| 164 | /*******************************************************************/
|
---|
| 165 | /* class BvHierarchy implementation */
|
---|
| 166 | /*******************************************************************/
|
---|
| 167 |
|
---|
| 168 |
|
---|
| 169 | BvHierarchy::BvHierarchy():
|
---|
| 170 | mRoot(NULL),
|
---|
| 171 | mTimeStamp(1)
|
---|
| 172 | {
|
---|
| 173 | ReadEnvironment();
|
---|
[1357] | 174 | mSubdivisionCandidates = new SortableEntryContainer;
|
---|
[1237] | 175 | }
|
---|
| 176 |
|
---|
| 177 |
|
---|
| 178 | BvHierarchy::~BvHierarchy()
|
---|
| 179 | {
|
---|
| 180 | // delete kd intersectables
|
---|
| 181 | BvhIntersectableMap::iterator it, it_end = mBvhIntersectables.end();
|
---|
| 182 |
|
---|
| 183 | for (it = mBvhIntersectables.begin(); it != mBvhIntersectables.end(); ++ it)
|
---|
| 184 | {
|
---|
| 185 | DEL_PTR((*it).second);
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | DEL_PTR(mSubdivisionCandidates);
|
---|
| 189 |
|
---|
| 190 | mSubdivisionStats.close();
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 |
|
---|
| 194 | void BvHierarchy::ReadEnvironment()
|
---|
| 195 | {
|
---|
| 196 | bool randomize = false;
|
---|
| 197 | Environment::GetSingleton()->GetBoolValue("VspTree.Construction.randomize", randomize);
|
---|
| 198 | if (randomize)
|
---|
| 199 | Randomize(); // initialise random generator for heuristics
|
---|
| 200 |
|
---|
[1370] | 201 |
|
---|
| 202 | /////////////////////////////////////////////////////////////
|
---|
[1237] | 203 | //-- termination criteria for autopartition
|
---|
[1288] | 204 | Environment::GetSingleton()->GetIntValue("BvHierarchy.Termination.maxDepth", mTermMaxDepth);
|
---|
| 205 | Environment::GetSingleton()->GetIntValue("BvHierarchy.Termination.maxLeaves", mTermMaxLeaves);
|
---|
| 206 | Environment::GetSingleton()->GetIntValue("BvHierarchy.Termination.minObjects", mTermMinObjects);
|
---|
[1370] | 207 | Environment::GetSingleton()->GetIntValue("BvHierarchy.Termination.minRays", mTermMinRays);
|
---|
[1288] | 208 | Environment::GetSingleton()->GetFloatValue("BvHierarchy.Termination.minProbability", mTermMinProbability);
|
---|
[1237] | 209 |
|
---|
[1288] | 210 | Environment::GetSingleton()->GetIntValue("BvHierarchy.Termination.missTolerance", mTermMissTolerance);
|
---|
[1370] | 211 |
|
---|
| 212 |
|
---|
[1421] | 213 | //////////////////////////////
|
---|
[1237] | 214 | //-- max cost ratio for early tree termination
|
---|
[1370] | 215 |
|
---|
[1288] | 216 | Environment::GetSingleton()->GetFloatValue("BvHierarchy.Termination.maxCostRatio", mTermMaxCostRatio);
|
---|
| 217 | Environment::GetSingleton()->GetFloatValue("BvHierarchy.Termination.minGlobalCostRatio",
|
---|
[1237] | 218 | mTermMinGlobalCostRatio);
|
---|
[1294] | 219 | Environment::GetSingleton()->GetIntValue("BvHierarchy.Termination.globalCostMissTolerance",
|
---|
| 220 | mTermGlobalCostMissTolerance);
|
---|
[1237] | 221 |
|
---|
| 222 |
|
---|
[1421] | 223 | //////////////////////////////
|
---|
[1370] | 224 | //-- factors for subdivision heuristics
|
---|
| 225 |
|
---|
| 226 | // if only the driving axis is used for splits
|
---|
[1288] | 227 | Environment::GetSingleton()->GetBoolValue("BvHierarchy.splitUseOnlyDrivingAxis", mOnlyDrivingAxis);
|
---|
| 228 | Environment::GetSingleton()->GetFloatValue("BvHierarchy.maxStaticMemory", mMaxMemory);
|
---|
| 229 | Environment::GetSingleton()->GetBoolValue("BvHierarchy.useCostHeuristics", mUseCostHeuristics);
|
---|
[1237] | 230 |
|
---|
| 231 | char subdivisionStatsLog[100];
|
---|
[1288] | 232 | Environment::GetSingleton()->GetStringValue("BvHierarchy.subdivisionStats", subdivisionStatsLog);
|
---|
[1237] | 233 | mSubdivisionStats.open(subdivisionStatsLog);
|
---|
| 234 |
|
---|
[1288] | 235 | Environment::GetSingleton()->GetFloatValue(
|
---|
| 236 | "BvHierarchy.Construction.renderCostDecreaseWeight", mRenderCostDecreaseWeight);
|
---|
[1237] | 237 |
|
---|
[1359] | 238 | Environment::GetSingleton()->GetBoolValue("BvHierarchy.Construction.useGlobalSorting", mUseGlobalSorting);
|
---|
[1237] | 239 |
|
---|
[1357] | 240 |
|
---|
[1421] | 241 | /////////////
|
---|
[1237] | 242 | //-- debug output
|
---|
[1359] | 243 |
|
---|
[1237] | 244 | Debug << "******* Bvh hierarchy options ******** " << endl;
|
---|
| 245 | Debug << "max depth: " << mTermMaxDepth << endl;
|
---|
[1287] | 246 | Debug << "min probabiliy: " << mTermMinProbability<< endl;
|
---|
[1237] | 247 | Debug << "min objects: " << mTermMinObjects << endl;
|
---|
| 248 | Debug << "max cost ratio: " << mTermMaxCostRatio << endl;
|
---|
| 249 | Debug << "miss tolerance: " << mTermMissTolerance << endl;
|
---|
| 250 | Debug << "max leaves: " << mTermMaxLeaves << endl;
|
---|
| 251 | Debug << "randomize: " << randomize << endl;
|
---|
| 252 | Debug << "min global cost ratio: " << mTermMinGlobalCostRatio << endl;
|
---|
| 253 | Debug << "global cost miss tolerance: " << mTermGlobalCostMissTolerance << endl;
|
---|
| 254 | Debug << "only driving axis: " << mOnlyDrivingAxis << endl;
|
---|
| 255 | Debug << "max memory: " << mMaxMemory << endl;
|
---|
| 256 | Debug << "use cost heuristics: " << mUseCostHeuristics << endl;
|
---|
| 257 | Debug << "subdivision stats log: " << subdivisionStatsLog << endl;
|
---|
| 258 | Debug << "split borders: " << mSplitBorder << endl;
|
---|
| 259 | Debug << "render cost decrease weight: " << mRenderCostDecreaseWeight << endl;
|
---|
[1357] | 260 | Debug << "use global sort: " << mUseGlobalSorting << endl;
|
---|
[1237] | 261 | Debug << endl;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 |
|
---|
[1486] | 265 | void BvHierarchy::AssociateObjectsWithLeaf(BvhLeaf *leaf)
|
---|
[1237] | 266 | {
|
---|
| 267 | ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
|
---|
| 268 | for (oit = leaf->mObjects.begin(); oit != oit_end; ++ oit)
|
---|
| 269 | {
|
---|
| 270 | (*oit)->mBvhLeaf = leaf;
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 |
|
---|
[1486] | 274 |
|
---|
[1370] | 275 | static int CountRays(const ObjectContainer &objects)
|
---|
| 276 | {
|
---|
| 277 | int nRays = 0;
|
---|
[1237] | 278 |
|
---|
[1370] | 279 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
| 280 |
|
---|
| 281 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
| 282 | {
|
---|
| 283 | nRays += (int)(*oit)->mVssRays.size();
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | return nRays;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[1486] | 289 |
|
---|
[1345] | 290 | BvhInterior *BvHierarchy::SubdivideNode(const BvhSubdivisionCandidate &sc,
|
---|
[1237] | 291 | BvhTraversalData &frontData,
|
---|
| 292 | BvhTraversalData &backData)
|
---|
| 293 | {
|
---|
[1345] | 294 | const BvhTraversalData &tData = sc.mParentData;
|
---|
[1237] | 295 | BvhLeaf *leaf = tData.mNode;
|
---|
[1370] | 296 | AxisAlignedBox3 parentBox = leaf->GetBoundingBox();
|
---|
[1237] | 297 |
|
---|
[1421] | 298 | // update stats: we have two new leaves
|
---|
| 299 | mBvhStats.nodes += 2;
|
---|
[1379] | 300 |
|
---|
| 301 | if (tData.mDepth > mBvhStats.maxDepth)
|
---|
| 302 | {
|
---|
| 303 | mBvhStats.maxDepth = tData.mDepth;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
[1237] | 306 | // add the new nodes to the tree
|
---|
[1370] | 307 | BvhInterior *node = new BvhInterior(parentBox, leaf->GetParent());
|
---|
[1294] | 308 |
|
---|
[1237] | 309 |
|
---|
[1421] | 310 | //////////////////
|
---|
[1237] | 311 | //-- create front and back leaf
|
---|
| 312 |
|
---|
[1405] | 313 | AxisAlignedBox3 fbox = EvalBoundingBox(sc.mFrontObjects, &parentBox);
|
---|
| 314 | AxisAlignedBox3 bbox = EvalBoundingBox(sc.mBackObjects, &parentBox);
|
---|
[1370] | 315 |
|
---|
[1287] | 316 | BvhLeaf *back =
|
---|
[1370] | 317 | new BvhLeaf(bbox, node, (int)sc.mBackObjects.size());
|
---|
[1287] | 318 | BvhLeaf *front =
|
---|
[1370] | 319 | new BvhLeaf(fbox, node, (int)sc.mFrontObjects.size());
|
---|
[1237] | 320 |
|
---|
| 321 | BvhInterior *parent = leaf->GetParent();
|
---|
| 322 |
|
---|
| 323 | // replace a link from node's parent
|
---|
| 324 | if (parent)
|
---|
| 325 | {
|
---|
| 326 | parent->ReplaceChildLink(leaf, node);
|
---|
| 327 | node->SetParent(parent);
|
---|
| 328 | }
|
---|
[1345] | 329 | else // no parent => this node is the root
|
---|
[1237] | 330 | {
|
---|
| 331 | mRoot = node;
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | // and setup child links
|
---|
| 335 | node->SetupChildLinks(front, back);
|
---|
| 336 |
|
---|
| 337 | ++ mBvhStats.splits;
|
---|
| 338 |
|
---|
| 339 |
|
---|
[1421] | 340 | ////////////////////////////////////////
|
---|
[1370] | 341 | //-- fill front and back traversal data with the new values
|
---|
| 342 |
|
---|
| 343 | frontData.mDepth = backData.mDepth = tData.mDepth + 1;
|
---|
| 344 |
|
---|
[1237] | 345 | frontData.mNode = front;
|
---|
| 346 | backData.mNode = back;
|
---|
| 347 |
|
---|
[1345] | 348 | back->mObjects = sc.mBackObjects;
|
---|
| 349 | front->mObjects = sc.mFrontObjects;
|
---|
[1237] | 350 |
|
---|
[1370] | 351 | // if the number of rays is too low, no assumptions can be made
|
---|
| 352 | // (=> switch to surface area heuristics?)
|
---|
| 353 | frontData.mNumRays = CountRays(sc.mFrontObjects);
|
---|
| 354 | backData.mNumRays = CountRays(sc.mBackObjects);
|
---|
| 355 |
|
---|
[1237] | 356 | AssociateObjectsWithLeaf(back);
|
---|
| 357 | AssociateObjectsWithLeaf(front);
|
---|
| 358 |
|
---|
[1370] | 359 | #if PROBABILIY_IS_BV_VOLUME
|
---|
| 360 | // volume of bvh (= probability that this bvh can be seen)
|
---|
| 361 | frontData.mProbability = fbox.GetVolume();
|
---|
| 362 | backData.mProbability = bbox.GetVolume();
|
---|
| 363 | #else
|
---|
[1345] | 364 | // compute probability of this node being visible,
|
---|
| 365 | // i.e., volume of the view cells that can see this node
|
---|
| 366 | frontData.mProbability = EvalViewCellsVolume(sc.mFrontObjects);
|
---|
| 367 | backData.mProbability = EvalViewCellsVolume(sc.mBackObjects);
|
---|
[1370] | 368 | #endif
|
---|
[1237] | 369 |
|
---|
[1345] | 370 | // how often was max cost ratio missed in this branch?
|
---|
| 371 | frontData.mMaxCostMisses = sc.mMaxCostMisses;
|
---|
| 372 | backData.mMaxCostMisses = sc.mMaxCostMisses;
|
---|
| 373 |
|
---|
[1357] | 374 | // assign the objects in sorted order
|
---|
| 375 | if (mUseGlobalSorting)
|
---|
| 376 | {
|
---|
| 377 | AssignSortedObjects(sc, frontData, backData);
|
---|
| 378 | }
|
---|
| 379 |
|
---|
[1345] | 380 | // return the new interior node
|
---|
[1237] | 381 | return node;
|
---|
| 382 | }
|
---|
| 383 |
|
---|
| 384 |
|
---|
| 385 | BvhNode *BvHierarchy::Subdivide(SplitQueue &tQueue,
|
---|
| 386 | SubdivisionCandidate *splitCandidate,
|
---|
| 387 | const bool globalCriteriaMet)
|
---|
| 388 | {
|
---|
[1288] | 389 | BvhSubdivisionCandidate *sc =
|
---|
| 390 | dynamic_cast<BvhSubdivisionCandidate *>(splitCandidate);
|
---|
[1237] | 391 | BvhTraversalData &tData = sc->mParentData;
|
---|
| 392 |
|
---|
[1345] | 393 | BvhNode *currentNode = tData.mNode;
|
---|
[1237] | 394 |
|
---|
| 395 | if (!LocalTerminationCriteriaMet(tData) && !globalCriteriaMet)
|
---|
| 396 | {
|
---|
[1421] | 397 | //////////////
|
---|
[1294] | 398 | //-- continue subdivision
|
---|
| 399 |
|
---|
[1237] | 400 | BvhTraversalData tFrontData;
|
---|
| 401 | BvhTraversalData tBackData;
|
---|
[1294] | 402 |
|
---|
[1237] | 403 | // create new interior node and two leaf node
|
---|
[1345] | 404 | currentNode = SubdivideNode(
|
---|
| 405 | *sc,
|
---|
| 406 | tFrontData,
|
---|
| 407 | tBackData);
|
---|
[1237] | 408 |
|
---|
[1287] | 409 | // decrease the weighted average cost of the subdivisoin
|
---|
[1237] | 410 | mTotalCost -= sc->GetRenderCostDecrease();
|
---|
| 411 |
|
---|
| 412 | // subdivision statistics
|
---|
[1287] | 413 | if (1) PrintSubdivisionStats(*sc);
|
---|
[1237] | 414 |
|
---|
[1345] | 415 |
|
---|
[1421] | 416 | ///////////////////////////
|
---|
[1237] | 417 | //-- push the new split candidates on the queue
|
---|
| 418 |
|
---|
[1287] | 419 | BvhSubdivisionCandidate *frontCandidate =
|
---|
| 420 | new BvhSubdivisionCandidate(tFrontData);
|
---|
| 421 | BvhSubdivisionCandidate *backCandidate =
|
---|
| 422 | new BvhSubdivisionCandidate(tBackData);
|
---|
[1237] | 423 |
|
---|
| 424 | EvalSubdivisionCandidate(*frontCandidate);
|
---|
| 425 | EvalSubdivisionCandidate(*backCandidate);
|
---|
[1297] | 426 |
|
---|
| 427 | // cross reference
|
---|
| 428 | tFrontData.mNode->SetSubdivisionCandidate(frontCandidate);
|
---|
| 429 | tBackData.mNode->SetSubdivisionCandidate(backCandidate);
|
---|
[1305] | 430 |
|
---|
[1237] | 431 | tQueue.Push(frontCandidate);
|
---|
| 432 | tQueue.Push(backCandidate);
|
---|
| 433 | }
|
---|
| 434 |
|
---|
[1345] | 435 | /////////////////////////////////
|
---|
| 436 | //-- node is a leaf => terminate traversal
|
---|
[1237] | 437 |
|
---|
[1345] | 438 | if (currentNode->IsLeaf())
|
---|
[1237] | 439 | {
|
---|
[1345] | 440 | //////////////////////////////////////
|
---|
[1297] | 441 | //-- store additional info
|
---|
[1237] | 442 | EvaluateLeafStats(tData);
|
---|
| 443 |
|
---|
[1297] | 444 | const bool mStoreRays = true;
|
---|
[1237] | 445 | if (mStoreRays)
|
---|
| 446 | {
|
---|
[1345] | 447 | BvhLeaf *leaf = dynamic_cast<BvhLeaf *>(currentNode);
|
---|
[1237] | 448 | CollectRays(leaf->mObjects, leaf->mVssRays);
|
---|
| 449 | }
|
---|
[1305] | 450 |
|
---|
[1345] | 451 | //////////////////////////////////////
|
---|
| 452 |
|
---|
| 453 | // this leaf is no candidate for splitting anymore
|
---|
| 454 | // => detach subdivision candidate
|
---|
[1305] | 455 | tData.mNode->SetSubdivisionCandidate(NULL);
|
---|
[1345] | 456 | // detach node so we don't delete it with the traversal data
|
---|
[1294] | 457 | tData.mNode = NULL;
|
---|
[1237] | 458 | }
|
---|
| 459 |
|
---|
[1345] | 460 | return currentNode;
|
---|
[1237] | 461 | }
|
---|
| 462 |
|
---|
| 463 |
|
---|
| 464 | void BvHierarchy::EvalSubdivisionCandidate(BvhSubdivisionCandidate &splitCandidate)
|
---|
| 465 | {
|
---|
| 466 | // compute best object partition
|
---|
[1287] | 467 | const float ratio = SelectObjectPartition(
|
---|
| 468 | splitCandidate.mParentData,
|
---|
| 469 | splitCandidate.mFrontObjects,
|
---|
| 470 | splitCandidate.mBackObjects);
|
---|
| 471 |
|
---|
| 472 | BvhLeaf *leaf = splitCandidate.mParentData.mNode;
|
---|
[1237] | 473 |
|
---|
[1288] | 474 | // cost ratio violated?
|
---|
[1297] | 475 | const bool maxCostRatioViolated = mTermMaxCostRatio < ratio;
|
---|
[1287] | 476 |
|
---|
[1288] | 477 | splitCandidate.mMaxCostMisses = maxCostRatioViolated ?
|
---|
[1297] | 478 | splitCandidate.mParentData.mMaxCostMisses + 1 :
|
---|
| 479 | splitCandidate.mParentData.mMaxCostMisses;
|
---|
[1288] | 480 |
|
---|
[1302] | 481 | const float oldProp = EvalViewCellsVolume(leaf->mObjects);
|
---|
[1379] | 482 | const float oldRenderCost = EvalRenderCost(leaf->mObjects);
|
---|
| 483 |
|
---|
[1237] | 484 | // compute global decrease in render cost
|
---|
[1379] | 485 | const float newRenderCost =
|
---|
| 486 | EvalRenderCost(splitCandidate.mFrontObjects) +
|
---|
| 487 | EvalRenderCost(splitCandidate.mBackObjects);
|
---|
[1237] | 488 |
|
---|
[1287] | 489 | const float renderCostDecr = oldRenderCost - newRenderCost;
|
---|
| 490 |
|
---|
[1522] | 491 | #ifdef _DEBUG
|
---|
[1379] | 492 | Debug << "old render cost: " << oldRenderCost << endl;
|
---|
| 493 | Debug << "new render cost: " << newRenderCost << endl;
|
---|
| 494 | Debug << "render cost decrease: " << renderCostDecr << endl;
|
---|
[1522] | 495 | #endif
|
---|
[1237] | 496 | splitCandidate.SetRenderCostDecrease(renderCostDecr);
|
---|
| 497 |
|
---|
[1379] | 498 | #if 1
|
---|
[1237] | 499 | // take render cost of node into account
|
---|
| 500 | // otherwise danger of being stuck in a local minimum!!
|
---|
| 501 | const float factor = mRenderCostDecreaseWeight;
|
---|
| 502 | const float priority = factor * renderCostDecr + (1.0f - factor) * oldRenderCost;
|
---|
[1379] | 503 | #else
|
---|
| 504 | const float priority = (float)-splitCandidate.mParentData.mDepth;
|
---|
[1237] | 505 | #endif
|
---|
| 506 |
|
---|
| 507 | // compute global decrease in render cost
|
---|
| 508 | splitCandidate.SetPriority(priority);
|
---|
| 509 | }
|
---|
| 510 |
|
---|
| 511 |
|
---|
[1251] | 512 | inline bool BvHierarchy::LocalTerminationCriteriaMet(const BvhTraversalData &data) const
|
---|
[1237] | 513 | {
|
---|
| 514 | // matt: TODO
|
---|
[1288] | 515 | return ( 0
|
---|
[1408] | 516 | || ((int)data.mNode->mObjects.size() <= mTermMinObjects)
|
---|
[1370] | 517 | || (data.mProbability <= mTermMinProbability)
|
---|
| 518 | || (data.mDepth >= mTermMaxDepth)
|
---|
| 519 | || (data.mNumRays <= mTermMinRays)
|
---|
[1237] | 520 | );
|
---|
| 521 | }
|
---|
| 522 |
|
---|
| 523 |
|
---|
[1251] | 524 | inline bool BvHierarchy::GlobalTerminationCriteriaMet(const BvhTraversalData &data) const
|
---|
[1237] | 525 | {
|
---|
[1421] | 526 | const bool terminationCriteriaMet =
|
---|
| 527 | (0
|
---|
[1288] | 528 | || (mBvhStats.Leaves() >= mTermMaxLeaves)
|
---|
[1522] | 529 | //|| (mBvhStats.mGlobalCostMisses >= mTermGlobalCostMissTolerance)
|
---|
[1288] | 530 | //|| mOutOfMemory
|
---|
[1237] | 531 | );
|
---|
[1421] | 532 |
|
---|
[1444] | 533 | if (1 && terminationCriteriaMet)
|
---|
[1421] | 534 | {
|
---|
| 535 | Debug << "bvh global termination criteria met:" << endl;
|
---|
[1449] | 536 | Debug << "cost misses: " << mBvhStats.mGlobalCostMisses << " " << mTermGlobalCostMissTolerance << endl;
|
---|
[1421] | 537 | Debug << "leaves: " << mBvhStats.Leaves() << " " << mTermMaxLeaves << endl;
|
---|
| 538 | }
|
---|
| 539 |
|
---|
| 540 | return terminationCriteriaMet;
|
---|
[1237] | 541 | }
|
---|
| 542 |
|
---|
| 543 |
|
---|
| 544 | void BvHierarchy::EvaluateLeafStats(const BvhTraversalData &data)
|
---|
| 545 | {
|
---|
| 546 | // the node became a leaf -> evaluate stats for leafs
|
---|
| 547 | BvhLeaf *leaf = data.mNode;
|
---|
| 548 |
|
---|
| 549 | ++ mCreatedLeaves;
|
---|
| 550 |
|
---|
[1370] | 551 |
|
---|
| 552 | if (data.mProbability <= mTermMinProbability)
|
---|
[1237] | 553 | {
|
---|
[1370] | 554 | ++ mBvhStats.minProbabilityNodes;
|
---|
[1237] | 555 | }
|
---|
| 556 |
|
---|
[1370] | 557 | ////////////////////////////////////////////
|
---|
| 558 | // depth related stuff
|
---|
| 559 |
|
---|
| 560 | if (data.mDepth < mBvhStats.minDepth)
|
---|
| 561 | {
|
---|
| 562 | mBvhStats.minDepth = data.mDepth;
|
---|
| 563 | }
|
---|
| 564 |
|
---|
| 565 | if (data.mDepth >= mTermMaxDepth)
|
---|
| 566 | {
|
---|
| 567 | ++ mBvhStats.maxDepthNodes;
|
---|
| 568 | }
|
---|
| 569 |
|
---|
[1237] | 570 | // accumulate depth to compute average depth
|
---|
| 571 | mBvhStats.accumDepth += data.mDepth;
|
---|
[1370] | 572 |
|
---|
| 573 |
|
---|
| 574 | ////////////////////////////////////////////
|
---|
| 575 | // objects related stuff
|
---|
| 576 |
|
---|
| 577 | // note: this number should always accumulate to the total number of objects
|
---|
| 578 | mBvhStats.objectRefs += (int)leaf->mObjects.size();
|
---|
| 579 |
|
---|
| 580 | if ((int)leaf->mObjects.size() <= mTermMinObjects)
|
---|
| 581 | {
|
---|
[1288] | 582 | ++ mBvhStats.minObjectsNodes;
|
---|
[1370] | 583 | }
|
---|
| 584 |
|
---|
[1408] | 585 | if (leaf->mObjects.empty())
|
---|
| 586 | {
|
---|
| 587 | ++ mBvhStats.emptyNodes;
|
---|
| 588 | }
|
---|
| 589 |
|
---|
[1370] | 590 | if ((int)leaf->mObjects.size() > mBvhStats.maxObjectRefs)
|
---|
| 591 | {
|
---|
[1237] | 592 | mBvhStats.maxObjectRefs = (int)leaf->mObjects.size();
|
---|
[1370] | 593 | }
|
---|
| 594 |
|
---|
| 595 | if ((int)leaf->mObjects.size() < mBvhStats.minObjectRefs)
|
---|
| 596 | {
|
---|
| 597 | mBvhStats.minObjectRefs = (int)leaf->mObjects.size();
|
---|
| 598 | }
|
---|
| 599 |
|
---|
| 600 | ////////////////////////////////////////////
|
---|
| 601 | // ray related stuff
|
---|
| 602 |
|
---|
| 603 | // note: this number should always accumulate to the total number of rays
|
---|
| 604 | mBvhStats.rayRefs += data.mNumRays;
|
---|
| 605 |
|
---|
| 606 | if (data.mNumRays <= mTermMinRays)
|
---|
| 607 | {
|
---|
| 608 | ++ mBvhStats.minRaysNodes;
|
---|
| 609 | }
|
---|
| 610 |
|
---|
| 611 | if (data.mNumRays > mBvhStats.maxRayRefs)
|
---|
| 612 | {
|
---|
| 613 | mBvhStats.maxRayRefs = data.mNumRays;
|
---|
| 614 | }
|
---|
| 615 |
|
---|
| 616 | if (data.mNumRays < mBvhStats.minRayRefs)
|
---|
| 617 | {
|
---|
| 618 | mBvhStats.minRayRefs = data.mNumRays;
|
---|
| 619 | }
|
---|
| 620 |
|
---|
[1415] | 621 | #if 0
|
---|
[1370] | 622 | cout << "depth: " << data.mDepth << " objects: " << (int)leaf->mObjects.size()
|
---|
| 623 | << " rays: " << data.mNumRays << " rays / objects "
|
---|
| 624 | << (float)data.mNumRays / (float)leaf->mObjects.size() << endl;
|
---|
[1415] | 625 | #endif
|
---|
[1237] | 626 | }
|
---|
| 627 |
|
---|
| 628 |
|
---|
[1297] | 629 | #if 0
|
---|
[1370] | 630 |
|
---|
| 631 | /// compute object boundaries using spatial mid split
|
---|
[1287] | 632 | float BvHierarchy::EvalLocalObjectPartition(const BvhTraversalData &tData,
|
---|
| 633 | const int axis,
|
---|
| 634 | ObjectContainer &objectsFront,
|
---|
| 635 | ObjectContainer &objectsBack)
|
---|
[1237] | 636 | {
|
---|
[1287] | 637 | const float maxBox = tData.mBoundingBox.Max(axis);
|
---|
| 638 | const float minBox = tData.mBoundingBox.Min(axis);
|
---|
[1237] | 639 |
|
---|
[1287] | 640 | float midPoint = (maxBox + minBox) * 0.5f;
|
---|
| 641 |
|
---|
| 642 | ObjectContainer::const_iterator oit, oit_end = tData.mNode->mObjects.end();
|
---|
[1237] | 643 |
|
---|
[1287] | 644 | for (oit = tData.mNode->mObjects.begin(); oit != oit_end; ++ oit)
|
---|
| 645 | {
|
---|
| 646 | Intersectable *obj = *oit;
|
---|
[1297] | 647 | const AxisAlignedBox3 box = obj->GetBox();
|
---|
[1291] | 648 |
|
---|
[1294] | 649 | const float objMid = (box.Max(axis) + box.Min(axis)) * 0.5f;
|
---|
[1291] | 650 |
|
---|
[1287] | 651 | // object mailed => belongs to back objects
|
---|
| 652 | if (objMid < midPoint)
|
---|
[1370] | 653 | {
|
---|
[1287] | 654 | objectsBack.push_back(obj);
|
---|
[1370] | 655 | }
|
---|
[1287] | 656 | else
|
---|
[1370] | 657 | {
|
---|
[1287] | 658 | objectsFront.push_back(obj);
|
---|
[1370] | 659 | }
|
---|
[1287] | 660 | }
|
---|
[1237] | 661 |
|
---|
[1379] | 662 | const float oldRenderCost = EvalRenderCost(tData.mNode->mObjects);
|
---|
[1287] | 663 | const float newRenderCost =
|
---|
[1379] | 664 | EvalRenderCost(objectsFront) * EvalRenderCost(objectsBack);
|
---|
[1237] | 665 |
|
---|
[1287] | 666 | const float ratio = newRenderCost / oldRenderCost;
|
---|
| 667 | return ratio;
|
---|
| 668 | }
|
---|
[1237] | 669 |
|
---|
[1297] | 670 | #else
|
---|
[1237] | 671 |
|
---|
[1370] | 672 | /// compute object partition by getting balanced objects on the left and right side
|
---|
[1297] | 673 | float BvHierarchy::EvalLocalObjectPartition(const BvhTraversalData &tData,
|
---|
| 674 | const int axis,
|
---|
| 675 | ObjectContainer &objectsFront,
|
---|
| 676 | ObjectContainer &objectsBack)
|
---|
| 677 | {
|
---|
[1357] | 678 | PrepareLocalSubdivisionCandidates(tData, axis);
|
---|
[1297] | 679 |
|
---|
[1357] | 680 | SortableEntryContainer::const_iterator cit, cit_end = mSubdivisionCandidates->end();
|
---|
[1297] | 681 |
|
---|
| 682 | int i = 0;
|
---|
| 683 | const int border = (int)tData.mNode->mObjects.size() / 2;
|
---|
| 684 |
|
---|
| 685 | for (cit = mSubdivisionCandidates->begin(); cit != cit_end; ++ cit, ++ i)
|
---|
| 686 | {
|
---|
| 687 | Intersectable *obj = (*cit).mObject;
|
---|
| 688 |
|
---|
| 689 | // object mailed => belongs to back objects
|
---|
| 690 | if (i < border)
|
---|
[1379] | 691 | {
|
---|
[1297] | 692 | objectsBack.push_back(obj);
|
---|
[1379] | 693 | }
|
---|
[1297] | 694 | else
|
---|
[1379] | 695 | {
|
---|
[1297] | 696 | objectsFront.push_back(obj);
|
---|
[1379] | 697 | }
|
---|
[1297] | 698 | }
|
---|
| 699 |
|
---|
[1379] | 700 | const float oldRenderCost = EvalRenderCost(tData.mNode->mObjects);
|
---|
| 701 | const float newRenderCost = EvalRenderCost(objectsFront) + EvalRenderCost(objectsBack);
|
---|
[1297] | 702 |
|
---|
| 703 | const float ratio = newRenderCost / oldRenderCost;
|
---|
| 704 | return ratio;
|
---|
| 705 | }
|
---|
| 706 | #endif
|
---|
| 707 |
|
---|
| 708 |
|
---|
[1323] | 709 | float BvHierarchy::EvalSah(const BvhTraversalData &tData,
|
---|
| 710 | const int axis,
|
---|
| 711 | ObjectContainer &objectsFront,
|
---|
| 712 | ObjectContainer &objectsBack)
|
---|
| 713 | {
|
---|
| 714 | // go through the lists, count the number of objects left and right
|
---|
| 715 | // and evaluate the following cost funcion:
|
---|
| 716 | // C = ct_div_ci + (ol + or)/queries
|
---|
[1379] | 717 | PrepareLocalSubdivisionCandidates(tData, axis);
|
---|
| 718 |
|
---|
[1323] | 719 | int objectsLeft = 0, objectsRight = (int)tData.mNode->mObjects.size();
|
---|
| 720 |
|
---|
| 721 | AxisAlignedBox3 box = tData.mNode->GetBoundingBox();
|
---|
| 722 |
|
---|
| 723 | float minBox = box.Min(axis);
|
---|
| 724 | float maxBox = box.Max(axis);
|
---|
| 725 | float boxArea = box.SurfaceArea();
|
---|
| 726 |
|
---|
| 727 | float minSum = 1e20f;
|
---|
| 728 |
|
---|
| 729 | float minBorder = maxBox;
|
---|
| 730 | float maxBorder = minBox;
|
---|
[1379] | 731 | float areaLeft = 0, areaRight = 0;
|
---|
[1323] | 732 |
|
---|
[1357] | 733 | SortableEntryContainer::const_iterator currentPos =
|
---|
[1323] | 734 | mSubdivisionCandidates->begin();
|
---|
| 735 |
|
---|
[1379] | 736 |
|
---|
| 737 | // we keep track of both borders of the bounding boxes =>
|
---|
| 738 | // store the events in descending order
|
---|
| 739 | vector<float> bordersRight;
|
---|
| 740 | bordersRight.resize(mSubdivisionCandidates->size());
|
---|
| 741 |
|
---|
[1357] | 742 | SortableEntryContainer::reverse_iterator rcit =
|
---|
[1323] | 743 | mSubdivisionCandidates->rbegin(), rcit_end = mSubdivisionCandidates->rend();
|
---|
[1379] | 744 |
|
---|
[1323] | 745 | vector<float>::reverse_iterator rbit = bordersRight.rbegin();
|
---|
| 746 |
|
---|
| 747 | for (; rcit != rcit_end; ++ rcit, ++ rbit)
|
---|
| 748 | {
|
---|
| 749 | Intersectable *obj = (*rcit).mObject;
|
---|
| 750 | const AxisAlignedBox3 box = obj->GetBox();
|
---|
| 751 |
|
---|
[1379] | 752 | if (box.Min(axis) < minBorder)
|
---|
| 753 | {
|
---|
[1323] | 754 | minBorder = box.Min(axis);
|
---|
[1379] | 755 | }
|
---|
| 756 |
|
---|
[1323] | 757 | (*rbit) = minBorder;
|
---|
| 758 | }
|
---|
| 759 |
|
---|
| 760 | vector<float>::const_iterator bit = bordersRight.begin();
|
---|
[1357] | 761 | SortableEntryContainer::const_iterator cit, cit_end = mSubdivisionCandidates->end();
|
---|
[1379] | 762 |
|
---|
[1323] | 763 | for (cit = mSubdivisionCandidates->begin(); cit != cit_end; ++ cit, ++ bit)
|
---|
| 764 | {
|
---|
| 765 | Intersectable *obj = (*cit).mObject;
|
---|
| 766 |
|
---|
| 767 | ++ objectsLeft;
|
---|
| 768 | -- objectsRight;
|
---|
| 769 |
|
---|
| 770 | AxisAlignedBox3 lbox = box;
|
---|
| 771 | AxisAlignedBox3 rbox = box;
|
---|
| 772 |
|
---|
| 773 | const AxisAlignedBox3 obox = obj->GetBox();
|
---|
| 774 |
|
---|
[1379] | 775 | // the borders of the bounding boxes have changed
|
---|
[1323] | 776 | if (obox.Max(axis) > maxBorder)
|
---|
[1379] | 777 | {
|
---|
[1323] | 778 | maxBorder = obox.Max(axis);
|
---|
[1379] | 779 | }
|
---|
[1323] | 780 |
|
---|
[1379] | 781 | minBorder = (*bit);
|
---|
| 782 |
|
---|
[1323] | 783 | lbox.SetMax(axis, maxBorder);
|
---|
[1379] | 784 | rbox.SetMin(axis, minBorder);
|
---|
[1323] | 785 |
|
---|
[1379] | 786 | const float al = lbox.SurfaceArea();
|
---|
| 787 | const float ar = rbox.SurfaceArea();
|
---|
| 788 |
|
---|
| 789 | const float sum = objectsLeft * al + objectsRight * ar;
|
---|
[1323] | 790 |
|
---|
[1379] | 791 | /*cout << "pos=" << (*cit).mPos << "\t q=(" << objectsLeft << "," << objectsRight <<")\t r=("
|
---|
[1370] | 792 | << lbox.SurfaceArea() << "," << rbox.SurfaceArea() << ")" << endl;
|
---|
[1379] | 793 | cout << "minborder: " << minBorder << " maxborder: " << maxBorder << endl;
|
---|
[1370] | 794 | cout << "cost= " << sum << endl;
|
---|
[1379] | 795 | */
|
---|
[1323] | 796 | if (sum < minSum)
|
---|
| 797 | {
|
---|
[1379] | 798 | minSum = sum;
|
---|
| 799 | areaLeft = al;
|
---|
| 800 | areaRight = ar;
|
---|
[1370] | 801 | // objects belong to left side now
|
---|
[1323] | 802 | for (; currentPos != (cit + 1); ++ currentPos);
|
---|
| 803 | }
|
---|
| 804 | }
|
---|
| 805 |
|
---|
[1379] | 806 |
|
---|
[1370] | 807 | ////////////////////////////////////////////
|
---|
[1323] | 808 | //-- assign object to front and back volume
|
---|
| 809 |
|
---|
| 810 | // belongs to back bv
|
---|
| 811 | for (cit = mSubdivisionCandidates->begin(); cit != currentPos; ++ cit)
|
---|
| 812 | objectsBack.push_back((*cit).mObject);
|
---|
| 813 |
|
---|
| 814 | // belongs to front bv
|
---|
| 815 | for (cit = currentPos; cit != cit_end; ++ cit)
|
---|
| 816 | objectsFront.push_back((*cit).mObject);
|
---|
| 817 |
|
---|
| 818 | float oldCost = (float)tData.mNode->mObjects.size();
|
---|
| 819 | float newCost = minSum / boxArea;
|
---|
| 820 | float ratio = newCost / oldCost;
|
---|
| 821 |
|
---|
[1379] | 822 | #ifdef _DEBUG
|
---|
[1414] | 823 | cout << "\n\nobjects=(" << (int)objectsBack.size() << "," << (int)objectsFront.size() << " of "
|
---|
| 824 | << (int)tData.mNode->mObjects.size() << ")\t area=("
|
---|
[1379] | 825 | << areaLeft << "," << areaRight << ")" << endl;
|
---|
| 826 | cout << "cost= " << minSum << endl;
|
---|
[1323] | 827 | #endif
|
---|
| 828 | return ratio;
|
---|
| 829 | }
|
---|
| 830 |
|
---|
| 831 |
|
---|
| 832 | static bool PrepareOutput(const int axis,
|
---|
| 833 | const int leaves,
|
---|
| 834 | ofstream &sumStats,
|
---|
| 835 | ofstream &vollStats,
|
---|
| 836 | ofstream &volrStats)
|
---|
| 837 | {
|
---|
| 838 | if ((axis == 0) && (leaves > 0) && (leaves < 90))
|
---|
| 839 | {
|
---|
| 840 | char str[64];
|
---|
| 841 | sprintf(str, "tmp/bvh_heur_sum-%04d.log", leaves);
|
---|
| 842 | sumStats.open(str);
|
---|
| 843 | sprintf(str, "tmp/bvh_heur_voll-%04d.log", leaves);
|
---|
| 844 | vollStats.open(str);
|
---|
| 845 | sprintf(str, "tmp/bvh_heur_volr-%04d.log", leaves);
|
---|
| 846 | volrStats.open(str);
|
---|
| 847 | }
|
---|
| 848 |
|
---|
| 849 | return sumStats.is_open() && vollStats.is_open() && volrStats.is_open();
|
---|
| 850 | }
|
---|
| 851 |
|
---|
| 852 |
|
---|
| 853 | static void PrintHeuristics(const int objectsRight,
|
---|
| 854 | const float sum,
|
---|
| 855 | const float volLeft,
|
---|
| 856 | const float volRight,
|
---|
| 857 | const float viewSpaceVol,
|
---|
| 858 | ofstream &sumStats,
|
---|
| 859 | ofstream &vollStats,
|
---|
| 860 | ofstream &volrStats)
|
---|
| 861 | {
|
---|
| 862 | sumStats
|
---|
| 863 | << "#Position\n" << objectsRight << endl
|
---|
| 864 | << "#Sum\n" << sum / viewSpaceVol << endl
|
---|
| 865 | << "#Vol\n" << (volLeft + volRight) / viewSpaceVol << endl;
|
---|
| 866 |
|
---|
| 867 | vollStats
|
---|
| 868 | << "#Position\n" << objectsRight << endl
|
---|
| 869 | << "#Vol\n" << volLeft / viewSpaceVol << endl;
|
---|
| 870 |
|
---|
| 871 | volrStats
|
---|
| 872 | << "#Position\n" << objectsRight << endl
|
---|
| 873 | << "#Vol\n" << volRight / viewSpaceVol << endl;
|
---|
| 874 | }
|
---|
| 875 |
|
---|
| 876 |
|
---|
[1287] | 877 | float BvHierarchy::EvalLocalCostHeuristics(const BvhTraversalData &tData,
|
---|
| 878 | const int axis,
|
---|
| 879 | ObjectContainer &objectsFront,
|
---|
| 880 | ObjectContainer &objectsBack)
|
---|
| 881 | {
|
---|
[1357] | 882 | ////////////////////////////////////////////////////////////////
|
---|
[1287] | 883 | // go through the lists, count the number of objects left and right
|
---|
| 884 | // and evaluate the cost funcion
|
---|
[1237] | 885 |
|
---|
[1357] | 886 | // prepare the heuristics by setting mailboxes and counters.
|
---|
| 887 | const float totalVol = PrepareHeuristics(tData, axis);
|
---|
| 888 |
|
---|
[1287] | 889 | // local helper variables
|
---|
| 890 | float volLeft = 0;
|
---|
| 891 | float volRight = totalVol;
|
---|
| 892 | int nObjectsLeft = 0;
|
---|
| 893 | const int nTotalObjects = (int)tData.mNode->mObjects.size();
|
---|
[1379] | 894 | const float viewSpaceVol = mHierarchyManager->GetViewSpaceBox().GetVolume();
|
---|
[1237] | 895 |
|
---|
[1357] | 896 | SortableEntryContainer::const_iterator backObjectsStart = mSubdivisionCandidates->begin();
|
---|
[1287] | 897 |
|
---|
[1237] | 898 | /////////////////////////////////
|
---|
[1357] | 899 | //-- the parameters for the current optimum
|
---|
[1237] | 900 |
|
---|
[1287] | 901 | float volBack = volLeft;
|
---|
| 902 | float volFront = volRight;
|
---|
| 903 | float newRenderCost = nTotalObjects * totalVol;
|
---|
[1237] | 904 |
|
---|
[1314] | 905 | #ifdef _DEBUG
|
---|
| 906 | ofstream sumStats;
|
---|
| 907 | ofstream vollStats;
|
---|
| 908 | ofstream volrStats;
|
---|
[1237] | 909 |
|
---|
[1323] | 910 | const bool printStats =
|
---|
| 911 | PrepareOutput(axis, mBvhStats.Leaves(), sumStats, vollStats, volrStats);
|
---|
[1314] | 912 | #endif
|
---|
| 913 |
|
---|
[1357] | 914 | ///////////////////////////////////////////////////
|
---|
| 915 | //-- the sweep heuristics
|
---|
[1237] | 916 | //-- traverse through events and find best split plane
|
---|
| 917 |
|
---|
[1357] | 918 | SortableEntryContainer::const_iterator cit, cit_end = cit_end = mSubdivisionCandidates->end();
|
---|
[1287] | 919 |
|
---|
| 920 | for (cit = mSubdivisionCandidates->begin(); cit != cit_end; ++ cit)
|
---|
[1237] | 921 | {
|
---|
[1287] | 922 | Intersectable *object = (*cit).mObject;
|
---|
[1370] | 923 |
|
---|
[1287] | 924 | // evaluate change in l and r volume
|
---|
| 925 | // voll = view cells that see only left node (i.e., left pvs)
|
---|
| 926 | // volr = view cells that see only right node (i.e., right pvs)
|
---|
| 927 | EvalHeuristicsContribution(object, volLeft, volRight);
|
---|
[1237] | 928 |
|
---|
[1287] | 929 | ++ nObjectsLeft;
|
---|
| 930 | const int nObjectsRight = nTotalObjects - nObjectsLeft;
|
---|
[1237] | 931 |
|
---|
[1287] | 932 | // the heuristics
|
---|
| 933 | const float sum = volLeft * (float)nObjectsLeft +
|
---|
| 934 | volRight * (float)nObjectsRight;
|
---|
| 935 |
|
---|
[1314] | 936 | #ifdef _DEBUG
|
---|
| 937 | if (printStats)
|
---|
[1357] | 938 | {
|
---|
[1323] | 939 | PrintHeuristics(nObjectsRight, sum, volLeft, volRight, viewSpaceVol,
|
---|
| 940 | sumStats, vollStats, volrStats);
|
---|
[1357] | 941 | }
|
---|
[1314] | 942 | #endif
|
---|
| 943 |
|
---|
[1287] | 944 | if (sum < newRenderCost)
|
---|
[1237] | 945 | {
|
---|
[1287] | 946 | newRenderCost = sum;
|
---|
[1237] | 947 |
|
---|
[1287] | 948 | volBack = volLeft;
|
---|
| 949 | volFront = volRight;
|
---|
[1237] | 950 |
|
---|
[1287] | 951 | // objects belongs to left side now
|
---|
[1357] | 952 | for (; backObjectsStart != (cit + 1); ++ backObjectsStart);
|
---|
[1237] | 953 | }
|
---|
| 954 | }
|
---|
| 955 |
|
---|
[1357] | 956 | ////////////////////////////////////////////
|
---|
[1287] | 957 | //-- assign object to front and back volume
|
---|
[1237] | 958 |
|
---|
[1287] | 959 | // belongs to back bv
|
---|
[1357] | 960 | for (cit = mSubdivisionCandidates->begin(); cit != backObjectsStart; ++ cit)
|
---|
| 961 | {
|
---|
[1287] | 962 | objectsBack.push_back((*cit).mObject);
|
---|
[1357] | 963 | }
|
---|
[1287] | 964 | // belongs to front bv
|
---|
[1357] | 965 | for (cit = backObjectsStart; cit != cit_end; ++ cit)
|
---|
| 966 | {
|
---|
[1287] | 967 | objectsFront.push_back((*cit).mObject);
|
---|
[1357] | 968 | }
|
---|
[1237] | 969 |
|
---|
[1357] | 970 | // render cost of the old parent
|
---|
[1287] | 971 | const float oldRenderCost = (float)nTotalObjects * totalVol + Limits::Small;
|
---|
| 972 | // the relative cost ratio
|
---|
| 973 | const float ratio = newRenderCost / oldRenderCost;
|
---|
| 974 |
|
---|
[1314] | 975 | #ifdef _DEBUG
|
---|
[1522] | 976 | Debug << "\n§§§§ bvh eval const decrease §§§§" << endl
|
---|
[1293] | 977 | << "back pvs: " << (int)objectsBack.size() << " front pvs: " << (int)objectsFront.size() << " total pvs: " << nTotalObjects << endl
|
---|
[1237] | 978 | << "back p: " << volBack / viewSpaceVol << " front p " << volFront / viewSpaceVol << " p: " << totalVol / viewSpaceVol << endl
|
---|
| 979 | << "old rc: " << oldRenderCost / viewSpaceVol << " new rc: " << newRenderCost / viewSpaceVol << endl
|
---|
| 980 | << "render cost decrease: " << oldRenderCost / viewSpaceVol - newRenderCost / viewSpaceVol << endl;
|
---|
[1314] | 981 | #endif
|
---|
[1237] | 982 |
|
---|
| 983 | return ratio;
|
---|
| 984 | }
|
---|
| 985 |
|
---|
| 986 |
|
---|
[1357] | 987 | void BvHierarchy::PrepareLocalSubdivisionCandidates(const BvhTraversalData &tData,
|
---|
| 988 | const int axis)
|
---|
[1237] | 989 | {
|
---|
[1357] | 990 | //-- insert object queries
|
---|
[1370] | 991 | ObjectContainer *objects = mUseGlobalSorting ? tData.mSortedObjects[axis] : &tData.mNode->mObjects;
|
---|
[1357] | 992 |
|
---|
[1370] | 993 | CreateLocalSubdivisionCandidates(*objects, &mSubdivisionCandidates, !mUseGlobalSorting, axis);
|
---|
[1357] | 994 | }
|
---|
| 995 |
|
---|
| 996 |
|
---|
| 997 | void BvHierarchy::CreateLocalSubdivisionCandidates(const ObjectContainer &objects,
|
---|
| 998 | SortableEntryContainer **subdivisionCandidates,
|
---|
| 999 | const bool sort,
|
---|
| 1000 | const int axis)
|
---|
| 1001 | {
|
---|
[1345] | 1002 | (*subdivisionCandidates)->clear();
|
---|
[1237] | 1003 |
|
---|
[1357] | 1004 | // compute requested size and look if subdivision candidate has to be recomputed
|
---|
[1345] | 1005 | const int requestedSize = (int)objects.size() * 2;
|
---|
[1237] | 1006 |
|
---|
| 1007 | // creates a sorted split candidates array
|
---|
[1345] | 1008 | if ((*subdivisionCandidates)->capacity() > 500000 &&
|
---|
| 1009 | requestedSize < (int)((*subdivisionCandidates)->capacity() / 10) )
|
---|
[1237] | 1010 | {
|
---|
[1357] | 1011 | delete (*subdivisionCandidates);
|
---|
| 1012 | (*subdivisionCandidates) = new SortableEntryContainer;
|
---|
[1237] | 1013 | }
|
---|
| 1014 |
|
---|
[1345] | 1015 | (*subdivisionCandidates)->reserve(requestedSize);
|
---|
[1237] | 1016 |
|
---|
[1345] | 1017 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
[1287] | 1018 |
|
---|
[1345] | 1019 | for (oit = objects.begin(); oit < oit_end; ++ oit)
|
---|
[1237] | 1020 | {
|
---|
| 1021 | Intersectable *object = *oit;
|
---|
[1287] | 1022 | const AxisAlignedBox3 &box = object->GetBox();
|
---|
| 1023 | const float midPt = (box.Min(axis) + box.Max(axis)) * 0.5f;
|
---|
[1237] | 1024 |
|
---|
[1345] | 1025 | (*subdivisionCandidates)->push_back(SortableEntry(object, midPt));
|
---|
[1237] | 1026 | }
|
---|
| 1027 |
|
---|
[1357] | 1028 | if (sort)
|
---|
| 1029 | {
|
---|
| 1030 | stable_sort((*subdivisionCandidates)->begin(), (*subdivisionCandidates)->end());
|
---|
| 1031 | }
|
---|
[1237] | 1032 | }
|
---|
| 1033 |
|
---|
| 1034 |
|
---|
| 1035 | const BvhStatistics &BvHierarchy::GetStatistics() const
|
---|
| 1036 | {
|
---|
| 1037 | return mBvhStats;
|
---|
| 1038 | }
|
---|
| 1039 |
|
---|
| 1040 |
|
---|
[1287] | 1041 | float BvHierarchy::PrepareHeuristics(const BvhTraversalData &tData, const int axis)
|
---|
| 1042 | {
|
---|
[1323] | 1043 | BvhLeaf *leaf = tData.mNode;
|
---|
| 1044 | float vol = 0;
|
---|
| 1045 |
|
---|
[1357] | 1046 | // sort so we can use a sweep from right to left
|
---|
| 1047 | PrepareLocalSubdivisionCandidates(tData, axis);
|
---|
| 1048 |
|
---|
[1287] | 1049 | // collect and mark the view cells as belonging to front pvs
|
---|
| 1050 | ViewCellContainer viewCells;
|
---|
| 1051 | CollectViewCells(tData.mNode->mObjects, viewCells, true);
|
---|
[1323] | 1052 |
|
---|
| 1053 | ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
|
---|
[1287] | 1054 | for (vit = viewCells.begin(); vit != vit_end; ++ vit)
|
---|
| 1055 | {
|
---|
| 1056 | vol += (*vit)->GetVolume();
|
---|
| 1057 | }
|
---|
| 1058 |
|
---|
[1370] | 1059 | // we will mail view cells switching to the back side
|
---|
[1287] | 1060 | ViewCell::NewMail();
|
---|
[1323] | 1061 |
|
---|
[1287] | 1062 | return vol;
|
---|
| 1063 | }
|
---|
| 1064 | ///////////////////////////////////////////////////////////
|
---|
| 1065 |
|
---|
| 1066 |
|
---|
| 1067 | void BvHierarchy::EvalHeuristicsContribution(Intersectable *obj,
|
---|
| 1068 | float &volLeft,
|
---|
| 1069 | float &volRight)
|
---|
[1237] | 1070 | {
|
---|
[1287] | 1071 | // collect all view cells associated with this objects
|
---|
| 1072 | // (also multiple times, if they are pierced by several rays)
|
---|
[1237] | 1073 | ViewCellContainer viewCells;
|
---|
[1287] | 1074 | const bool useMailboxing = false;
|
---|
[1323] | 1075 |
|
---|
[1287] | 1076 | CollectViewCells(obj, viewCells, useMailboxing);
|
---|
[1237] | 1077 |
|
---|
[1357] | 1078 | // classify view cells and compute volume contri accordingly
|
---|
| 1079 | // possible view cell classifications:
|
---|
| 1080 | // view cell mailed => view cell can be seen from left child node
|
---|
| 1081 | // view cell counter > 0 view cell can be seen from right child node
|
---|
| 1082 | // combined: view cell volume belongs to both nodes
|
---|
[1237] | 1083 | ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
|
---|
| 1084 |
|
---|
| 1085 | for (vit = viewCells.begin(); vit != vit_end; ++ vit)
|
---|
| 1086 | {
|
---|
| 1087 | // view cells can also be seen from left child node
|
---|
| 1088 | ViewCell *viewCell = *vit;
|
---|
| 1089 |
|
---|
| 1090 | const float vol = viewCell->GetVolume();
|
---|
| 1091 |
|
---|
| 1092 | if (!viewCell->Mailed())
|
---|
| 1093 | {
|
---|
| 1094 | viewCell->Mail();
|
---|
| 1095 | // we now see view cell from both nodes
|
---|
[1287] | 1096 | // => add volume to left node
|
---|
| 1097 | volLeft += vol;
|
---|
[1237] | 1098 | }
|
---|
| 1099 |
|
---|
| 1100 | // last reference into the right node
|
---|
| 1101 | if (-- viewCell->mCounter == 0)
|
---|
[1357] | 1102 | {
|
---|
[1237] | 1103 | // view cell was previously seen from both nodes =>
|
---|
[1287] | 1104 | // remove volume from right node
|
---|
| 1105 | volRight -= vol;
|
---|
[1237] | 1106 | }
|
---|
| 1107 | }
|
---|
| 1108 | }
|
---|
| 1109 |
|
---|
| 1110 |
|
---|
| 1111 | void BvHierarchy::SetViewCellsManager(ViewCellsManager *vcm)
|
---|
| 1112 | {
|
---|
| 1113 | mViewCellsManager = vcm;
|
---|
| 1114 | }
|
---|
| 1115 |
|
---|
| 1116 |
|
---|
| 1117 | AxisAlignedBox3 BvHierarchy::GetBoundingBox() const
|
---|
| 1118 | {
|
---|
| 1119 | return mBoundingBox;
|
---|
| 1120 | }
|
---|
| 1121 |
|
---|
| 1122 |
|
---|
| 1123 | float BvHierarchy::SelectObjectPartition(const BvhTraversalData &tData,
|
---|
| 1124 | ObjectContainer &frontObjects,
|
---|
| 1125 | ObjectContainer &backObjects)
|
---|
| 1126 | {
|
---|
| 1127 | ObjectContainer nFrontObjects[3];
|
---|
| 1128 | ObjectContainer nBackObjects[3];
|
---|
| 1129 | float nCostRatio[3];
|
---|
| 1130 |
|
---|
| 1131 | int sAxis = 0;
|
---|
| 1132 | int bestAxis = -1;
|
---|
| 1133 |
|
---|
| 1134 | if (mOnlyDrivingAxis)
|
---|
| 1135 | {
|
---|
[1370] | 1136 | const AxisAlignedBox3 box = tData.mNode->GetBoundingBox();
|
---|
[1237] | 1137 | sAxis = box.Size().DrivingAxis();
|
---|
| 1138 | }
|
---|
| 1139 |
|
---|
[1357] | 1140 | ////////////////////////////////////////////
|
---|
| 1141 | //-- evaluate split cost for all three axis
|
---|
[1237] | 1142 |
|
---|
| 1143 | for (int axis = 0; axis < 3; ++ axis)
|
---|
| 1144 | {
|
---|
| 1145 | if (!mOnlyDrivingAxis || (axis == sAxis))
|
---|
| 1146 | {
|
---|
[1287] | 1147 | if (mUseCostHeuristics)
|
---|
[1298] | 1148 | {
|
---|
[1370] | 1149 | //////////////////////////////////
|
---|
| 1150 | //-- split objects using heuristics
|
---|
| 1151 |
|
---|
| 1152 | if (mHierarchyManager->GetViewSpaceSubdivisionType() ==
|
---|
| 1153 | HierarchyManager::KD_BASED_VIEWSPACE_SUBDIV)
|
---|
| 1154 | {
|
---|
| 1155 | //-- heuristics using objects weighted by view cells volume
|
---|
| 1156 | nCostRatio[axis] =
|
---|
| 1157 | EvalLocalCostHeuristics(
|
---|
| 1158 | tData,
|
---|
| 1159 | axis,
|
---|
| 1160 | nFrontObjects[axis],
|
---|
| 1161 | nBackObjects[axis]);
|
---|
| 1162 | }
|
---|
| 1163 | else
|
---|
| 1164 | {
|
---|
| 1165 | //-- use surface area heuristic because view cells not constructed yet
|
---|
| 1166 | nCostRatio[axis] =
|
---|
[1379] | 1167 | EvalSah(
|
---|
| 1168 | tData,
|
---|
| 1169 | axis,
|
---|
| 1170 | nFrontObjects[axis],
|
---|
| 1171 | nBackObjects[axis]);
|
---|
[1370] | 1172 | }
|
---|
[1237] | 1173 | }
|
---|
[1287] | 1174 | else
|
---|
[1298] | 1175 | {
|
---|
[1370] | 1176 | //-- split objects using some simple criteria
|
---|
[1287] | 1177 | nCostRatio[axis] =
|
---|
| 1178 | EvalLocalObjectPartition(
|
---|
| 1179 | tData,
|
---|
| 1180 | axis,
|
---|
| 1181 | nFrontObjects[axis],
|
---|
| 1182 | nBackObjects[axis]);
|
---|
| 1183 | }
|
---|
| 1184 |
|
---|
[1237] | 1185 | if (bestAxis == -1)
|
---|
| 1186 | {
|
---|
| 1187 | bestAxis = axis;
|
---|
| 1188 | }
|
---|
| 1189 | else if (nCostRatio[axis] < nCostRatio[bestAxis])
|
---|
| 1190 | {
|
---|
| 1191 | bestAxis = axis;
|
---|
| 1192 | }
|
---|
| 1193 | }
|
---|
| 1194 | }
|
---|
| 1195 |
|
---|
[1357] | 1196 | /////////////////////////////////////
|
---|
[1237] | 1197 | //-- assign values
|
---|
[1287] | 1198 |
|
---|
[1237] | 1199 | frontObjects = nFrontObjects[bestAxis];
|
---|
[1287] | 1200 | backObjects = nBackObjects[bestAxis];
|
---|
[1237] | 1201 |
|
---|
[1293] | 1202 | //Debug << "val: " << nCostRatio[bestAxis] << " axis: " << bestAxis << endl;
|
---|
[1237] | 1203 | return nCostRatio[bestAxis];
|
---|
| 1204 | }
|
---|
| 1205 |
|
---|
| 1206 |
|
---|
[1370] | 1207 | int BvHierarchy::AssociateObjectsWithRays(const VssRayContainer &rays) const
|
---|
[1237] | 1208 | {
|
---|
[1370] | 1209 | int nRays = 0;
|
---|
[1237] | 1210 | VssRayContainer::const_iterator rit, rit_end = rays.end();
|
---|
| 1211 |
|
---|
[1370] | 1212 | VssRay::NewMail();
|
---|
| 1213 |
|
---|
[1237] | 1214 | for (rit = rays.begin(); rit != rays.end(); ++ rit)
|
---|
| 1215 | {
|
---|
| 1216 | VssRay *ray = (*rit);
|
---|
| 1217 |
|
---|
| 1218 | if (ray->mTerminationObject)
|
---|
| 1219 | {
|
---|
| 1220 | ray->mTerminationObject->mVssRays.push_back(ray);
|
---|
[1370] | 1221 | if (!ray->Mailed())
|
---|
| 1222 | {
|
---|
| 1223 | ray->Mail();
|
---|
| 1224 | ++ nRays;
|
---|
| 1225 | }
|
---|
[1237] | 1226 | }
|
---|
| 1227 |
|
---|
[1370] | 1228 | if (1 && ray->mOriginObject)
|
---|
[1237] | 1229 | {
|
---|
| 1230 | ray->mOriginObject->mVssRays.push_back(ray);
|
---|
[1370] | 1231 |
|
---|
| 1232 | if (!ray->Mailed())
|
---|
| 1233 | {
|
---|
| 1234 | ray->Mail();
|
---|
| 1235 | ++ nRays;
|
---|
| 1236 | }
|
---|
[1237] | 1237 | }
|
---|
| 1238 | }
|
---|
[1370] | 1239 |
|
---|
| 1240 | return nRays;
|
---|
[1237] | 1241 | }
|
---|
| 1242 |
|
---|
| 1243 |
|
---|
[1287] | 1244 | void BvHierarchy::PrintSubdivisionStats(const SubdivisionCandidate &sc)
|
---|
[1237] | 1245 | {
|
---|
[1421] | 1246 | const float costDecr =
|
---|
| 1247 | sc.GetRenderCostDecrease();// / mHierarchyManager->GetViewSpaceBox().GetVolume();
|
---|
[1237] | 1248 |
|
---|
| 1249 | mSubdivisionStats
|
---|
[1421] | 1250 | << "#Leaves\n" << mBvhStats.Leaves() << endl
|
---|
[1287] | 1251 | << "#RenderCostDecrease\n" << costDecr << endl
|
---|
| 1252 | << "#TotalRenderCost\n" << mTotalCost << endl;
|
---|
[1237] | 1253 | }
|
---|
| 1254 |
|
---|
| 1255 |
|
---|
| 1256 | void BvHierarchy::CollectRays(const ObjectContainer &objects,
|
---|
| 1257 | VssRayContainer &rays) const
|
---|
| 1258 | {
|
---|
| 1259 | VssRay::NewMail();
|
---|
| 1260 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
| 1261 |
|
---|
| 1262 | // evaluate reverse pvs and view cell volume on left and right cell
|
---|
| 1263 | // note: should I take all leaf objects or rather the objects hit by rays?
|
---|
| 1264 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
| 1265 | {
|
---|
| 1266 | Intersectable *obj = *oit;
|
---|
| 1267 | VssRayContainer::const_iterator rit, rit_end = obj->mVssRays.end();
|
---|
| 1268 |
|
---|
| 1269 | for (rit = obj->mVssRays.begin(); rit < rit_end; ++ rit)
|
---|
| 1270 | {
|
---|
| 1271 | VssRay *ray = (*rit);
|
---|
| 1272 |
|
---|
| 1273 | if (!ray->Mailed())
|
---|
| 1274 | {
|
---|
| 1275 | ray->Mail();
|
---|
| 1276 | rays.push_back(ray);
|
---|
| 1277 | }
|
---|
| 1278 | }
|
---|
| 1279 | }
|
---|
| 1280 | }
|
---|
| 1281 |
|
---|
| 1282 |
|
---|
[1379] | 1283 | float BvHierarchy::EvalRenderCost(const ObjectContainer &objects) const
|
---|
[1287] | 1284 | {
|
---|
[1379] | 1285 | if (mHierarchyManager->GetViewSpaceSubdivisionType() == HierarchyManager::NO_VIEWSPACE_SUBDIV)
|
---|
| 1286 | {
|
---|
| 1287 | if (objects.empty())
|
---|
| 1288 | return 0.0f;
|
---|
[1357] | 1289 |
|
---|
[1421] | 1290 | ////////////////////
|
---|
[1379] | 1291 | //-- surface area heuristics
|
---|
| 1292 |
|
---|
[1405] | 1293 | const AxisAlignedBox3 box = EvalBoundingBox(objects);
|
---|
[1379] | 1294 | const float area = box.SurfaceArea();
|
---|
| 1295 |
|
---|
[1421] | 1296 | return (float)objects.size() * area / mHierarchyManager->GetViewSpaceBox().SurfaceArea();
|
---|
[1379] | 1297 | }
|
---|
| 1298 | else
|
---|
[1421] | 1299 | { ////////////////////
|
---|
[1379] | 1300 | //-- render cost heuristics
|
---|
| 1301 |
|
---|
| 1302 | const float viewSpaceVol = mHierarchyManager->GetViewSpaceBox().GetVolume();
|
---|
| 1303 | // probability that view point lies in a view cell which sees this node
|
---|
| 1304 | const float p = EvalViewCellsVolume(objects) / viewSpaceVol;
|
---|
| 1305 |
|
---|
| 1306 | return (float)objects.size() * p;
|
---|
| 1307 | }
|
---|
[1287] | 1308 | }
|
---|
| 1309 |
|
---|
| 1310 |
|
---|
[1405] | 1311 | AxisAlignedBox3 BvHierarchy::EvalBoundingBox(const ObjectContainer &objects,
|
---|
| 1312 | const AxisAlignedBox3 *parentBox) const
|
---|
[1237] | 1313 | {
|
---|
[1405] | 1314 | // if there are no objects in this box, box size is set to parent box size.
|
---|
| 1315 | // Question: Invalidate box instead?
|
---|
[1287] | 1316 | if (parentBox && objects.empty())
|
---|
| 1317 | return *parentBox;
|
---|
| 1318 |
|
---|
[1237] | 1319 | AxisAlignedBox3 box;
|
---|
| 1320 | box.Initialize();
|
---|
| 1321 |
|
---|
| 1322 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
| 1323 |
|
---|
| 1324 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
| 1325 | {
|
---|
| 1326 | Intersectable *obj = *oit;
|
---|
[1370] | 1327 | // grow bounding box to include all objects
|
---|
[1287] | 1328 | box.Include(obj->GetBox());
|
---|
[1237] | 1329 | }
|
---|
[1287] | 1330 |
|
---|
[1237] | 1331 | return box;
|
---|
| 1332 | }
|
---|
| 1333 |
|
---|
| 1334 |
|
---|
| 1335 | void BvHierarchy::CollectLeaves(vector<BvhLeaf *> &leaves) const
|
---|
| 1336 | {
|
---|
| 1337 | stack<BvhNode *> nodeStack;
|
---|
| 1338 | nodeStack.push(mRoot);
|
---|
| 1339 |
|
---|
| 1340 | while (!nodeStack.empty())
|
---|
| 1341 | {
|
---|
| 1342 | BvhNode *node = nodeStack.top();
|
---|
| 1343 | nodeStack.pop();
|
---|
[1287] | 1344 |
|
---|
[1237] | 1345 | if (node->IsLeaf())
|
---|
| 1346 | {
|
---|
| 1347 | BvhLeaf *leaf = (BvhLeaf *)node;
|
---|
| 1348 | leaves.push_back(leaf);
|
---|
| 1349 | }
|
---|
| 1350 | else
|
---|
| 1351 | {
|
---|
| 1352 | BvhInterior *interior = (BvhInterior *)node;
|
---|
| 1353 |
|
---|
| 1354 | nodeStack.push(interior->GetBack());
|
---|
| 1355 | nodeStack.push(interior->GetFront());
|
---|
| 1356 | }
|
---|
| 1357 | }
|
---|
| 1358 | }
|
---|
| 1359 |
|
---|
| 1360 |
|
---|
| 1361 | AxisAlignedBox3 BvHierarchy::GetBoundingBox(BvhNode *node) const
|
---|
| 1362 | {
|
---|
| 1363 | return node->GetBoundingBox();
|
---|
| 1364 | }
|
---|
| 1365 |
|
---|
| 1366 |
|
---|
[1287] | 1367 | void BvHierarchy::CollectViewCells(const ObjectContainer &objects,
|
---|
| 1368 | ViewCellContainer &viewCells,
|
---|
| 1369 | const bool setCounter) const
|
---|
[1237] | 1370 | {
|
---|
[1379] | 1371 | // no view cells yet
|
---|
| 1372 | if (mHierarchyManager->GetViewSpaceSubdivisionType() ==
|
---|
| 1373 | HierarchyManager::NO_VIEWSPACE_SUBDIV)
|
---|
| 1374 | return;
|
---|
| 1375 |
|
---|
[1237] | 1376 | ViewCell::NewMail();
|
---|
[1287] | 1377 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
[1237] | 1378 |
|
---|
| 1379 | // loop through all object and collect view cell pvs of this node
|
---|
[1287] | 1380 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
[1237] | 1381 | {
|
---|
[1287] | 1382 | CollectViewCells(*oit, viewCells, true, setCounter);
|
---|
[1237] | 1383 | }
|
---|
| 1384 | }
|
---|
| 1385 |
|
---|
| 1386 |
|
---|
[1287] | 1387 | void BvHierarchy::CollectViewCells(Intersectable *obj,
|
---|
| 1388 | ViewCellContainer &viewCells,
|
---|
| 1389 | const bool useMailBoxing,
|
---|
| 1390 | const bool setCounter) const
|
---|
[1237] | 1391 | {
|
---|
[1287] | 1392 | VssRayContainer::const_iterator rit, rit_end = obj->mVssRays.end();
|
---|
[1237] | 1393 |
|
---|
[1287] | 1394 | for (rit = obj->mVssRays.begin(); rit < rit_end; ++ rit)
|
---|
[1237] | 1395 | {
|
---|
| 1396 | VssRay *ray = (*rit);
|
---|
[1287] | 1397 | ViewCellContainer tmpViewCells;
|
---|
| 1398 |
|
---|
[1379] | 1399 | mHierarchyManager->mVspTree->GetViewCells(*ray, tmpViewCells);
|
---|
[1237] | 1400 |
|
---|
| 1401 | ViewCellContainer::const_iterator vit, vit_end = tmpViewCells.end();
|
---|
| 1402 |
|
---|
| 1403 | for (vit = tmpViewCells.begin(); vit != vit_end; ++ vit)
|
---|
| 1404 | {
|
---|
| 1405 | VspViewCell *vc = dynamic_cast<VspViewCell *>(*vit);
|
---|
| 1406 |
|
---|
[1287] | 1407 | // store view cells
|
---|
| 1408 | if (!useMailBoxing || !vc->Mailed())
|
---|
[1237] | 1409 | {
|
---|
[1287] | 1410 | if (useMailBoxing)
|
---|
| 1411 | {
|
---|
| 1412 | vc->Mail();
|
---|
| 1413 | if (setCounter)
|
---|
[1305] | 1414 | {
|
---|
[1287] | 1415 | vc->mCounter = 0;
|
---|
[1305] | 1416 | }
|
---|
[1287] | 1417 | }
|
---|
[1237] | 1418 | viewCells.push_back(vc);
|
---|
| 1419 | }
|
---|
[1287] | 1420 |
|
---|
| 1421 | if (setCounter)
|
---|
| 1422 | {
|
---|
| 1423 | ++ vc->mCounter;
|
---|
| 1424 | }
|
---|
[1237] | 1425 | }
|
---|
| 1426 | }
|
---|
[1287] | 1427 | }
|
---|
[1237] | 1428 |
|
---|
| 1429 |
|
---|
[1287] | 1430 | void BvHierarchy::CollectDirtyCandidates(BvhSubdivisionCandidate *sc,
|
---|
| 1431 | vector<SubdivisionCandidate *> &dirtyList)
|
---|
| 1432 | {
|
---|
| 1433 | BvhTraversalData &tData = sc->mParentData;
|
---|
| 1434 | BvhLeaf *node = tData.mNode;
|
---|
| 1435 |
|
---|
| 1436 | ViewCellContainer viewCells;
|
---|
| 1437 | CollectViewCells(node->mObjects, viewCells);
|
---|
[1415] | 1438 | if (0) cout << "collected " << (int)viewCells.size() << " dirty candidates" << endl;
|
---|
[1287] | 1439 |
|
---|
| 1440 | // split candidates handling
|
---|
| 1441 | // these view cells are thrown into dirty list
|
---|
[1237] | 1442 | ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
|
---|
| 1443 |
|
---|
| 1444 | for (vit = viewCells.begin(); vit != vit_end; ++ vit)
|
---|
| 1445 | {
|
---|
| 1446 | VspViewCell *vc = dynamic_cast<VspViewCell *>(*vit);
|
---|
| 1447 | VspLeaf *leaf = vc->mLeaf;
|
---|
[1297] | 1448 | SubdivisionCandidate *candidate = leaf->GetSubdivisionCandidate();
|
---|
| 1449 |
|
---|
[1305] | 1450 | if (candidate) // is this leaf still a split candidate?
|
---|
| 1451 | {
|
---|
| 1452 | dirtyList.push_back(candidate);
|
---|
| 1453 | }
|
---|
[1237] | 1454 | }
|
---|
| 1455 | }
|
---|
| 1456 |
|
---|
| 1457 |
|
---|
| 1458 | BvhNode *BvHierarchy::GetRoot() const
|
---|
| 1459 | {
|
---|
| 1460 | return mRoot;
|
---|
| 1461 | }
|
---|
| 1462 |
|
---|
| 1463 |
|
---|
| 1464 | bool BvHierarchy::IsObjectInLeaf(BvhLeaf *leaf, Intersectable *object) const
|
---|
| 1465 | {
|
---|
| 1466 | ObjectContainer::const_iterator oit =
|
---|
| 1467 | lower_bound(leaf->mObjects.begin(), leaf->mObjects.end(), object, ilt);
|
---|
| 1468 |
|
---|
| 1469 | // objects sorted by id
|
---|
| 1470 | if ((oit != leaf->mObjects.end()) && ((*oit)->GetId() == object->GetId()))
|
---|
| 1471 | {
|
---|
| 1472 | return true;
|
---|
| 1473 | }
|
---|
| 1474 | else
|
---|
| 1475 | {
|
---|
| 1476 | return false;
|
---|
| 1477 | }
|
---|
| 1478 | }
|
---|
| 1479 |
|
---|
| 1480 |
|
---|
| 1481 | BvhLeaf *BvHierarchy::GetLeaf(Intersectable *object, BvhNode *node) const
|
---|
| 1482 | {
|
---|
| 1483 | // rather use the simple version
|
---|
[1297] | 1484 | if (!object) return NULL;
|
---|
| 1485 | return object->mBvhLeaf;
|
---|
| 1486 |
|
---|
[1237] | 1487 | ///////////////////////////////////////
|
---|
| 1488 | // start from root of tree
|
---|
[1297] | 1489 |
|
---|
[1237] | 1490 | if (node == NULL)
|
---|
| 1491 | node = mRoot;
|
---|
[1297] | 1492 |
|
---|
[1237] | 1493 | vector<BvhLeaf *> leaves;
|
---|
| 1494 |
|
---|
| 1495 | stack<BvhNode *> nodeStack;
|
---|
| 1496 | nodeStack.push(node);
|
---|
| 1497 |
|
---|
| 1498 | BvhLeaf *leaf = NULL;
|
---|
| 1499 |
|
---|
| 1500 | while (!nodeStack.empty())
|
---|
| 1501 | {
|
---|
| 1502 | BvhNode *node = nodeStack.top();
|
---|
| 1503 | nodeStack.pop();
|
---|
| 1504 |
|
---|
| 1505 | if (node->IsLeaf())
|
---|
| 1506 | {
|
---|
| 1507 | leaf = dynamic_cast<BvhLeaf *>(node);
|
---|
| 1508 |
|
---|
| 1509 | if (IsObjectInLeaf(leaf, object))
|
---|
[1293] | 1510 | {
|
---|
[1237] | 1511 | return leaf;
|
---|
[1293] | 1512 | }
|
---|
[1237] | 1513 | }
|
---|
| 1514 | else
|
---|
| 1515 | {
|
---|
| 1516 | // find point
|
---|
| 1517 | BvhInterior *interior = dynamic_cast<BvhInterior *>(node);
|
---|
| 1518 |
|
---|
| 1519 | if (interior->GetBack()->GetBoundingBox().Includes(object->GetBox()))
|
---|
| 1520 | {
|
---|
| 1521 | nodeStack.push(interior->GetBack());
|
---|
| 1522 | }
|
---|
| 1523 |
|
---|
| 1524 | // search both sides as we are using bounding volumes
|
---|
| 1525 | if (interior->GetFront()->GetBoundingBox().Includes(object->GetBox()))
|
---|
| 1526 | {
|
---|
| 1527 | nodeStack.push(interior->GetFront());
|
---|
| 1528 | }
|
---|
| 1529 | }
|
---|
| 1530 | }
|
---|
| 1531 |
|
---|
| 1532 | return leaf;
|
---|
| 1533 | }
|
---|
| 1534 |
|
---|
| 1535 |
|
---|
| 1536 | BvhIntersectable *BvHierarchy::GetOrCreateBvhIntersectable(BvhNode *node)
|
---|
| 1537 | {
|
---|
| 1538 | // search nodes
|
---|
[1486] | 1539 | std::map<BvhNode *, BvhIntersectable *>::const_iterator it
|
---|
| 1540 | = mBvhIntersectables.find(node);
|
---|
[1237] | 1541 |
|
---|
| 1542 | if (it != mBvhIntersectables.end())
|
---|
| 1543 | {
|
---|
| 1544 | return (*it).second;
|
---|
| 1545 | }
|
---|
| 1546 |
|
---|
| 1547 | // not in map => create new entry
|
---|
| 1548 | BvhIntersectable *bvhObj = new BvhIntersectable(node);
|
---|
| 1549 | mBvhIntersectables[node] = bvhObj;
|
---|
| 1550 |
|
---|
| 1551 | return bvhObj;
|
---|
| 1552 | }
|
---|
| 1553 |
|
---|
| 1554 |
|
---|
| 1555 | bool BvHierarchy::Export(OUT_STREAM &stream)
|
---|
| 1556 | {
|
---|
| 1557 | ExportNode(mRoot, stream);
|
---|
| 1558 |
|
---|
| 1559 | return true;
|
---|
| 1560 | }
|
---|
| 1561 |
|
---|
| 1562 |
|
---|
[1286] | 1563 | void BvHierarchy::ExportObjects(BvhLeaf *leaf, OUT_STREAM &stream)
|
---|
| 1564 | {
|
---|
| 1565 | ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
|
---|
| 1566 | for (oit = leaf->mObjects.begin(); oit != oit_end; ++ oit)
|
---|
| 1567 | {
|
---|
| 1568 | stream << (*oit)->GetId() << " ";
|
---|
| 1569 | }
|
---|
| 1570 | }
|
---|
| 1571 |
|
---|
| 1572 |
|
---|
[1237] | 1573 | void BvHierarchy::ExportNode(BvhNode *node, OUT_STREAM &stream)
|
---|
| 1574 | {
|
---|
| 1575 | if (node->IsLeaf())
|
---|
| 1576 | {
|
---|
| 1577 | BvhLeaf *leaf = dynamic_cast<BvhLeaf *>(node);
|
---|
[1287] | 1578 | const AxisAlignedBox3 box = leaf->GetBoundingBox();
|
---|
[1286] | 1579 | stream << "<Leaf"
|
---|
[1287] | 1580 | << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
|
---|
| 1581 | << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\""
|
---|
[1286] | 1582 | << " objects=\"";
|
---|
[1237] | 1583 |
|
---|
[1286] | 1584 | //-- export objects
|
---|
| 1585 | ExportObjects(leaf, stream);
|
---|
[1237] | 1586 |
|
---|
| 1587 | stream << "\" />" << endl;
|
---|
| 1588 | }
|
---|
| 1589 | else
|
---|
| 1590 | {
|
---|
| 1591 | BvhInterior *interior = dynamic_cast<BvhInterior *>(node);
|
---|
[1287] | 1592 | const AxisAlignedBox3 box = interior->GetBoundingBox();
|
---|
| 1593 |
|
---|
[1286] | 1594 | stream << "<Interior"
|
---|
[1287] | 1595 | << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
|
---|
| 1596 | << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z
|
---|
[1286] | 1597 | << "\">" << endl;
|
---|
[1237] | 1598 |
|
---|
| 1599 | ExportNode(interior->GetBack(), stream);
|
---|
| 1600 | ExportNode(interior->GetFront(), stream);
|
---|
| 1601 |
|
---|
| 1602 | stream << "</Interior>" << endl;
|
---|
| 1603 | }
|
---|
| 1604 | }
|
---|
| 1605 |
|
---|
| 1606 |
|
---|
[1287] | 1607 | float BvHierarchy::EvalViewCellsVolume(const ObjectContainer &objects) const
|
---|
[1237] | 1608 | {
|
---|
| 1609 | float vol = 0;
|
---|
| 1610 |
|
---|
[1287] | 1611 | ViewCellContainer viewCells;
|
---|
| 1612 | CollectViewCells(objects, viewCells);
|
---|
[1237] | 1613 |
|
---|
[1287] | 1614 | ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
|
---|
[1237] | 1615 |
|
---|
[1287] | 1616 | for (vit = viewCells.begin(); vit != vit_end; ++ vit)
|
---|
[1237] | 1617 | {
|
---|
[1287] | 1618 | vol += (*vit)->GetVolume();
|
---|
[1237] | 1619 | }
|
---|
| 1620 |
|
---|
| 1621 | return vol;
|
---|
| 1622 | }
|
---|
| 1623 |
|
---|
[1357] | 1624 |
|
---|
[1294] | 1625 | void BvHierarchy::CreateRoot(const ObjectContainer &objects)
|
---|
| 1626 | {
|
---|
[1449] | 1627 | ///////
|
---|
[1294] | 1628 | //-- create new root
|
---|
[1449] | 1629 |
|
---|
[1405] | 1630 | AxisAlignedBox3 box = EvalBoundingBox(objects);
|
---|
[1294] | 1631 | BvhLeaf *bvhleaf = new BvhLeaf(box, NULL, (int)objects.size());
|
---|
| 1632 | bvhleaf->mObjects = objects;
|
---|
| 1633 | mRoot = bvhleaf;
|
---|
| 1634 |
|
---|
| 1635 | // associate root with current objects
|
---|
| 1636 | AssociateObjectsWithLeaf(bvhleaf);
|
---|
| 1637 | }
|
---|
| 1638 |
|
---|
[1404] | 1639 | /*
|
---|
| 1640 | Mesh *BvHierarchy::MergeLeafToMesh()
|
---|
| 1641 | void BvHierarchy::MergeLeavesToMeshes()
|
---|
| 1642 | {
|
---|
| 1643 | vector<BvhLeaf *> leaves;
|
---|
| 1644 | CollectLeaves(leaves);
|
---|
[1294] | 1645 |
|
---|
[1404] | 1646 | vector<BvhLeaf *>::const_iterator lit, lit_end = leaves.end();
|
---|
| 1647 |
|
---|
| 1648 | for (lit = leaves.begin(); lit != lit_end; ++ lit)
|
---|
| 1649 | {
|
---|
| 1650 | Mesh *mesh = MergeLeafToMesh(*lit);
|
---|
| 1651 | }
|
---|
| 1652 | }*/
|
---|
| 1653 |
|
---|
| 1654 |
|
---|
[1237] | 1655 | SubdivisionCandidate *BvHierarchy::PrepareConstruction(const VssRayContainer &sampleRays,
|
---|
[1294] | 1656 | const ObjectContainer &objects)
|
---|
[1237] | 1657 | {
|
---|
[1522] | 1658 | ///////////////////////////////////////
|
---|
| 1659 | //-- we assume that we have objects sorted by their id =>
|
---|
[1404] | 1660 | //-- we don't have to sort them here and an binary search
|
---|
| 1661 | //-- for identifying if a object is in a leaf.
|
---|
[1421] | 1662 |
|
---|
[1308] | 1663 | mBvhStats.Reset();
|
---|
| 1664 | mBvhStats.Start();
|
---|
[1421] | 1665 |
|
---|
[1308] | 1666 | mBvhStats.nodes = 1;
|
---|
[1522] | 1667 |
|
---|
[1237] | 1668 | // store pointer to this tree
|
---|
| 1669 | BvhSubdivisionCandidate::sBvHierarchy = this;
|
---|
[1421] | 1670 |
|
---|
[1237] | 1671 | // compute bounding box from objects
|
---|
[1302] | 1672 | // note: we assume that root was already created
|
---|
[1294] | 1673 | mBoundingBox = mRoot->GetBoundingBox();
|
---|
| 1674 | BvhLeaf *bvhleaf = dynamic_cast<BvhLeaf *>(mRoot);
|
---|
[1237] | 1675 |
|
---|
[1370] | 1676 | // multiply termination criterium for comparison,
|
---|
| 1677 | // so it can be set between zero and one and
|
---|
| 1678 | // no division is necessary during traversal
|
---|
| 1679 |
|
---|
| 1680 | #if PROBABILIY_IS_BV_VOLUME
|
---|
[1287] | 1681 | mTermMinProbability *= mBoundingBox.GetVolume();
|
---|
[1370] | 1682 | // probability that bounding volume is seen
|
---|
| 1683 | const float prop = GetBoundingBox().GetVolume();
|
---|
| 1684 | #else
|
---|
| 1685 | mTermMinProbability *= mVspTree->GetBoundingBox().GetVolume();
|
---|
| 1686 | // probability that volume is "seen" from the view cells
|
---|
[1294] | 1687 | const float prop = EvalViewCellsVolume(objects);
|
---|
[1287] | 1688 | #endif
|
---|
[1237] | 1689 |
|
---|
[1370] | 1690 | // only rays intersecting objects in node are interesting
|
---|
| 1691 | const int nRays = AssociateObjectsWithRays(sampleRays);
|
---|
| 1692 | //Debug << "using " << nRays << " of " << (int)sampleRays.size() << " rays" << endl;
|
---|
| 1693 |
|
---|
[1288] | 1694 | // create bvh traversal data
|
---|
[1370] | 1695 | BvhTraversalData oData(bvhleaf, 0, prop, nRays);
|
---|
[1237] | 1696 |
|
---|
[1357] | 1697 | // create sorted object lists for the first data
|
---|
| 1698 | if (mUseGlobalSorting)
|
---|
| 1699 | {
|
---|
| 1700 | CreateInitialSortedObjectList(oData);
|
---|
| 1701 | }
|
---|
| 1702 |
|
---|
| 1703 |
|
---|
[1449] | 1704 | ///////////////////
|
---|
[1294] | 1705 | //-- add first candidate for object space partition
|
---|
[1357] | 1706 |
|
---|
[1237] | 1707 | BvhSubdivisionCandidate *oSubdivisionCandidate =
|
---|
| 1708 | new BvhSubdivisionCandidate(oData);
|
---|
| 1709 |
|
---|
| 1710 | EvalSubdivisionCandidate(*oSubdivisionCandidate);
|
---|
[1302] | 1711 | bvhleaf->SetSubdivisionCandidate(oSubdivisionCandidate);
|
---|
[1237] | 1712 |
|
---|
[1379] | 1713 | const float viewSpaceVol = mHierarchyManager->GetViewSpaceBox().GetVolume();
|
---|
[1287] | 1714 | mTotalCost = (float)objects.size() * prop / viewSpaceVol;
|
---|
[1237] | 1715 |
|
---|
[1287] | 1716 | PrintSubdivisionStats(*oSubdivisionCandidate);
|
---|
[1237] | 1717 |
|
---|
| 1718 | return oSubdivisionCandidate;
|
---|
| 1719 | }
|
---|
| 1720 |
|
---|
| 1721 |
|
---|
[1357] | 1722 | void BvHierarchy::CreateInitialSortedObjectList(BvhTraversalData &tData)
|
---|
| 1723 | {
|
---|
| 1724 | SortableEntryContainer *sortedObjects = new SortableEntryContainer();
|
---|
| 1725 |
|
---|
| 1726 | // we sort the objects as a preprocess so they don't have
|
---|
| 1727 | // to be sorted for each split
|
---|
| 1728 | for (int i = 0; i < 3; ++ i)
|
---|
| 1729 | {
|
---|
| 1730 | CreateLocalSubdivisionCandidates(tData.mNode->mObjects, &sortedObjects, true, i);
|
---|
| 1731 |
|
---|
| 1732 | tData.mSortedObjects[i] = new ObjectContainer();
|
---|
| 1733 | tData.mSortedObjects[i]->reserve((int)sortedObjects->size());
|
---|
| 1734 |
|
---|
| 1735 | SortableEntryContainer::const_iterator oit, oit_end = sortedObjects->end();
|
---|
| 1736 | for (oit = sortedObjects->begin(); oit != oit_end; ++ oit)
|
---|
| 1737 | {
|
---|
| 1738 | tData.mSortedObjects[i]->push_back((*oit).mObject);
|
---|
| 1739 | }
|
---|
| 1740 | }
|
---|
| 1741 |
|
---|
| 1742 | delete sortedObjects;
|
---|
| 1743 | }
|
---|
| 1744 |
|
---|
| 1745 |
|
---|
| 1746 | void BvHierarchy::AssignSortedObjects(const BvhSubdivisionCandidate &sc,
|
---|
| 1747 | BvhTraversalData &frontData,
|
---|
| 1748 | BvhTraversalData &backData)
|
---|
| 1749 | {
|
---|
| 1750 | Intersectable::NewMail();
|
---|
| 1751 |
|
---|
| 1752 | // we sorted the objects as a preprocess so they don't have
|
---|
| 1753 | // to be sorted for each split
|
---|
| 1754 | ObjectContainer::const_iterator fit, fit_end = sc.mFrontObjects.end();
|
---|
| 1755 |
|
---|
| 1756 | for (fit = sc.mFrontObjects.begin(); fit != fit_end; ++ fit)
|
---|
| 1757 | {
|
---|
| 1758 | (*fit)->Mail();
|
---|
| 1759 | }
|
---|
| 1760 |
|
---|
| 1761 | for (int i = 0; i < 3; ++ i)
|
---|
| 1762 | {
|
---|
[1359] | 1763 | frontData.mSortedObjects[i] = new ObjectContainer();
|
---|
| 1764 | backData.mSortedObjects[i] = new ObjectContainer();
|
---|
| 1765 |
|
---|
[1357] | 1766 | frontData.mSortedObjects[i]->reserve((int)sc.mFrontObjects.size());
|
---|
| 1767 | backData.mSortedObjects[i]->reserve((int)sc.mFrontObjects.size());
|
---|
| 1768 |
|
---|
[1370] | 1769 | ObjectContainer::const_iterator oit, oit_end = sc.mParentData.mSortedObjects[i]->end();
|
---|
[1357] | 1770 |
|
---|
[1370] | 1771 | for (oit = sc.mParentData.mSortedObjects[i]->begin(); oit != oit_end; ++ oit)
|
---|
[1357] | 1772 | {
|
---|
| 1773 | if ((*oit)->Mailed())
|
---|
| 1774 | {
|
---|
| 1775 | frontData.mSortedObjects[i]->push_back(*oit);
|
---|
| 1776 | }
|
---|
| 1777 | else
|
---|
| 1778 | {
|
---|
| 1779 | backData.mSortedObjects[i]->push_back(*oit);
|
---|
| 1780 | }
|
---|
| 1781 | }
|
---|
| 1782 | }
|
---|
| 1783 | }
|
---|
| 1784 |
|
---|
| 1785 |
|
---|
[1279] | 1786 | void BvhStatistics::Print(ostream &app) const
|
---|
| 1787 | {
|
---|
[1288] | 1788 | app << "=========== BvHierarchy statistics ===============\n";
|
---|
[1279] | 1789 |
|
---|
| 1790 | app << setprecision(4);
|
---|
| 1791 |
|
---|
| 1792 | app << "#N_CTIME ( Construction time [s] )\n" << Time() << " \n";
|
---|
| 1793 |
|
---|
| 1794 | app << "#N_NODES ( Number of nodes )\n" << nodes << "\n";
|
---|
| 1795 |
|
---|
| 1796 | app << "#N_INTERIORS ( Number of interior nodes )\n" << Interior() << "\n";
|
---|
| 1797 |
|
---|
| 1798 | app << "#N_LEAVES ( Number of leaves )\n" << Leaves() << "\n";
|
---|
| 1799 |
|
---|
| 1800 | app << "#AXIS_ALIGNED_SPLITS (number of axis aligned splits)\n" << splits << endl;
|
---|
| 1801 |
|
---|
| 1802 | app << "#N_MAXCOSTNODES ( Percentage of leaves with terminated because of max cost ratio )\n"
|
---|
| 1803 | << maxCostNodes * 100 / (double)Leaves() << endl;
|
---|
| 1804 |
|
---|
| 1805 | app << "#N_PMINPROBABILITYLEAVES ( Percentage of leaves with mininum probability )\n"
|
---|
| 1806 | << minProbabilityNodes * 100 / (double)Leaves() << endl;
|
---|
| 1807 |
|
---|
[1288] | 1808 |
|
---|
[1370] | 1809 | //////////////////////////////////////////////////
|
---|
| 1810 |
|
---|
| 1811 | app << "#N_PMAXDEPTHLEAVES ( Percentage of leaves at maximum depth )\n"
|
---|
| 1812 | << maxDepthNodes * 100 / (double)Leaves() << endl;
|
---|
| 1813 |
|
---|
[1279] | 1814 | app << "#N_PMAXDEPTH ( Maximal reached depth )\n" << maxDepth << endl;
|
---|
| 1815 |
|
---|
| 1816 | app << "#N_PMINDEPTH ( Minimal reached depth )\n" << minDepth << endl;
|
---|
| 1817 |
|
---|
| 1818 | app << "#AVGDEPTH ( average depth )\n" << AvgDepth() << endl;
|
---|
| 1819 |
|
---|
[1370] | 1820 |
|
---|
| 1821 | ////////////////////////////////////////////////////////
|
---|
| 1822 |
|
---|
| 1823 | app << "#N_PMINOBJECTSLEAVES ( Percentage of leaves with mininum objects )\n"
|
---|
| 1824 | << minObjectsNodes * 100 / (double)Leaves() << endl;
|
---|
[1279] | 1825 |
|
---|
| 1826 | app << "#N_MAXOBJECTREFS ( Max number of object refs / leaf )\n" << maxObjectRefs << "\n";
|
---|
| 1827 |
|
---|
[1370] | 1828 | app << "#N_MINOBJECTREFS ( Min number of object refs / leaf )\n" << minObjectRefs << "\n";
|
---|
[1408] | 1829 |
|
---|
| 1830 | app << "#N_EMPTYLEAFS ( Empty leafs )\n" << emptyNodes << "\n";
|
---|
[1279] | 1831 |
|
---|
[1370] | 1832 | app << "#N_PAVGOBJECTSLEAVES ( average object refs / leaf)\n" << AvgObjectRefs() << endl;
|
---|
| 1833 |
|
---|
| 1834 |
|
---|
| 1835 | ////////////////////////////////////////////////////////
|
---|
| 1836 |
|
---|
| 1837 | app << "#N_PMINRAYSLEAVES ( Percentage of leaves with mininum rays )\n"
|
---|
| 1838 | << minRaysNodes * 100 / (double)Leaves() << endl;
|
---|
| 1839 |
|
---|
| 1840 | app << "#N_MAXRAYREFS ( Max number of ray refs / leaf )\n" << maxRayRefs << "\n";
|
---|
| 1841 |
|
---|
| 1842 | app << "#N_MINRAYREFS ( Min number of ray refs / leaf )\n" << minRayRefs << "\n";
|
---|
| 1843 |
|
---|
| 1844 | app << "#N_PAVGRAYLEAVES ( average ray refs / leaf )\n" << AvgRayRefs() << endl;
|
---|
| 1845 |
|
---|
| 1846 | app << "#N_PAVGRAYCONTRIBLEAVES ( Average ray contribution)\n" <<
|
---|
| 1847 | rayRefs / (double)objectRefs << endl;
|
---|
| 1848 |
|
---|
| 1849 | app << "#N_PMAXRAYCONTRIBLEAVES ( Percentage of leaves with maximal ray contribution )\n"<<
|
---|
| 1850 | maxRayContriNodes * 100 / (double)Leaves() << endl;
|
---|
| 1851 |
|
---|
[1449] | 1852 | app << "#N_PGLOBALCOSTMISSES ( Global cost misses )\n" << mGlobalCostMisses << endl;
|
---|
| 1853 |
|
---|
[1370] | 1854 | app << "========== END OF BvHierarchy statistics ==========\n";
|
---|
[1272] | 1855 | }
|
---|
[1259] | 1856 |
|
---|
[1279] | 1857 |
|
---|
| 1858 | } |
---|