[1237] | 1 | #include <stack>
|
---|
| 2 | #include <time.h>
|
---|
| 3 | #include <iomanip>
|
---|
| 4 |
|
---|
| 5 | #include "ViewCell.h"
|
---|
| 6 | #include "Plane3.h"
|
---|
| 7 | #include "HierarchyManager.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 "KdTree.h"
|
---|
[1315] | 19 | #include "IntersectableWrapper.h"
|
---|
[1237] | 20 | #include "VspTree.h"
|
---|
| 21 | #include "OspTree.h"
|
---|
[1259] | 22 | #include "BvHierarchy.h"
|
---|
[1237] | 23 |
|
---|
| 24 |
|
---|
| 25 | namespace GtpVisibilityPreprocessor {
|
---|
| 26 |
|
---|
| 27 |
|
---|
| 28 | #define USE_FIXEDPOINT_T 0
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | /*******************************************************************/
|
---|
| 32 | /* class HierarchyManager implementation */
|
---|
| 33 | /*******************************************************************/
|
---|
| 34 |
|
---|
| 35 |
|
---|
[1279] | 36 | HierarchyManager::HierarchyManager(VspTree *vspTree,
|
---|
| 37 | const int objectSpaceSubdivisionType):
|
---|
[1308] | 38 | mObjectSpaceSubdivisionType(objectSpaceSubdivisionType),
|
---|
[1279] | 39 | mVspTree(vspTree),
|
---|
| 40 | mOspTree(NULL),
|
---|
| 41 | mBvHierarchy(NULL)
|
---|
[1237] | 42 | {
|
---|
[1308] | 43 | switch(mObjectSpaceSubdivisionType)
|
---|
[1279] | 44 | {
|
---|
| 45 | case KD_BASED_OBJ_SUBDIV:
|
---|
| 46 | mOspTree = new OspTree();
|
---|
| 47 | mOspTree->mVspTree = mVspTree;
|
---|
[1379] | 48 | mOspTree->mHierarchyManager = this;
|
---|
[1279] | 49 | break;
|
---|
| 50 | case BV_BASED_OBJ_SUBDIV:
|
---|
| 51 | mBvHierarchy = new BvHierarchy();
|
---|
[1379] | 52 | mBvHierarchy->mHierarchyManager = this;
|
---|
[1279] | 53 | break;
|
---|
| 54 | default:
|
---|
| 55 | break;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[1379] | 58 | // hierarchy manager links view space partition and object space partition
|
---|
| 59 | mVspTree->mHierarchyManager = this;
|
---|
| 60 |
|
---|
[1288] | 61 | ParseEnvironment();
|
---|
[1237] | 62 | }
|
---|
| 63 |
|
---|
| 64 |
|
---|
[1279] | 65 | HierarchyManager::HierarchyManager(VspTree *vspTree, KdTree *kdTree):
|
---|
[1308] | 66 | mObjectSpaceSubdivisionType(KD_BASED_OBJ_SUBDIV),
|
---|
[1279] | 67 | mVspTree(vspTree),
|
---|
| 68 | mBvHierarchy(NULL)
|
---|
| 69 | {
|
---|
| 70 | mOspTree = new OspTree(*kdTree);
|
---|
| 71 | mOspTree->mVspTree = mVspTree;
|
---|
| 72 |
|
---|
[1379] | 73 | mVspTree->mHierarchyManager = this;
|
---|
[1279] | 74 |
|
---|
[1288] | 75 | ParseEnvironment();
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 |
|
---|
| 79 | void HierarchyManager::ParseEnvironment()
|
---|
| 80 | {
|
---|
[1279] | 81 | char subdivisionStatsLog[100];
|
---|
| 82 | Environment::GetSingleton()->GetStringValue("Hierarchy.subdivisionStats",
|
---|
| 83 | subdivisionStatsLog);
|
---|
| 84 | mSubdivisionStats.open(subdivisionStatsLog);
|
---|
[1288] | 85 |
|
---|
| 86 | Environment::GetSingleton()->GetFloatValue(
|
---|
| 87 | "Hierarchy.Termination.minGlobalCostRatio", mTermMinGlobalCostRatio);
|
---|
| 88 | Environment::GetSingleton()->GetIntValue(
|
---|
| 89 | "Hierarchy.Termination.globalCostMissTolerance", mTermGlobalCostMissTolerance);
|
---|
| 90 |
|
---|
[1370] | 91 | Environment::GetSingleton()->GetBoolValue(
|
---|
| 92 | "Hierarchy.Construction.startWithObjectSpace", mStartWithObjectSpace);
|
---|
| 93 |
|
---|
[1293] | 94 | Environment::GetSingleton()->GetIntValue(
|
---|
[1294] | 95 | "Hierarchy.Termination.maxLeaves", mTermMaxLeaves);
|
---|
| 96 |
|
---|
| 97 | Environment::GetSingleton()->GetIntValue(
|
---|
[1293] | 98 | "Hierarchy.Construction.type", mConstructionType);
|
---|
| 99 |
|
---|
[1311] | 100 | Environment::GetSingleton()->GetIntValue(
|
---|
| 101 | "Hierarchy.Construction.minDepthForOsp", mMinDepthForObjectSpaceSubdivion);
|
---|
[1370] | 102 |
|
---|
| 103 | Environment::GetSingleton()->GetIntValue(
|
---|
| 104 | "Hierarchy.Construction.minDepthForVsp", mMinDepthForViewSpaceSubdivion);
|
---|
[1311] | 105 |
|
---|
[1314] | 106 | Environment::GetSingleton()->GetBoolValue(
|
---|
| 107 | "Hierarchy.Construction.repairQueue", mRepairQueue);
|
---|
| 108 |
|
---|
[1294] | 109 | Debug << "******** Hierachy Manager Parameters ***********" << endl;
|
---|
| 110 | Debug << "max leaves: " << mTermMaxLeaves << endl;
|
---|
[1288] | 111 | Debug << "min global cost ratio: " << mTermMinGlobalCostRatio << endl;
|
---|
| 112 | Debug << "global cost miss tolerance: " << mTermGlobalCostMissTolerance << endl;
|
---|
[1314] | 113 | Debug << "construction type: " << mConstructionType << endl;
|
---|
| 114 | Debug << "min depth for object space subdivision: " << mMinDepthForObjectSpaceSubdivion << endl;
|
---|
| 115 | Debug << "repair queue: " << mRepairQueue << endl;
|
---|
[1279] | 116 | }
|
---|
| 117 |
|
---|
| 118 |
|
---|
| 119 | HierarchyManager::~HierarchyManager()
|
---|
| 120 | {
|
---|
| 121 | DEL_PTR(mOspTree);
|
---|
| 122 | //DEL_PTR(mVspTree);
|
---|
| 123 | DEL_PTR(mBvHierarchy);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 |
|
---|
[1370] | 127 | int HierarchyManager::GetObjectSpaceSubdivisionType() const
|
---|
| 128 | {
|
---|
| 129 | return mObjectSpaceSubdivisionType;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 |
|
---|
| 133 | int HierarchyManager::GetViewSpaceSubdivisionType() const
|
---|
| 134 | {
|
---|
| 135 | return mViewSpaceSubdivisionType;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 |
|
---|
[1279] | 139 | void HierarchyManager::SetViewCellsManager(ViewCellsManager *vcm)
|
---|
| 140 | {
|
---|
| 141 | mVspTree->SetViewCellsManager(vcm);
|
---|
| 142 |
|
---|
| 143 | if (mOspTree)
|
---|
| 144 | mOspTree->SetViewCellsManager(vcm);
|
---|
| 145 | if (mBvHierarchy)
|
---|
| 146 | mBvHierarchy->SetViewCellsManager(vcm);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 |
|
---|
| 150 | void HierarchyManager::SetViewCellsTree(ViewCellsTree *vcTree)
|
---|
| 151 | {
|
---|
| 152 | mVspTree->SetViewCellsTree(vcTree);
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 |
|
---|
[1379] | 156 | VspTree *HierarchyManager::GetVspTree()
|
---|
| 157 | {
|
---|
| 158 | return mVspTree;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 |
|
---|
| 162 | AxisAlignedBox3 HierarchyManager::GetViewSpaceBox() const
|
---|
| 163 | {
|
---|
| 164 | return mVspTree->mBoundingBox;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 |
|
---|
[1237] | 168 | SubdivisionCandidate *HierarchyManager::NextSubdivisionCandidate()
|
---|
| 169 | {
|
---|
| 170 | SubdivisionCandidate *splitCandidate = mTQueue.Top();
|
---|
| 171 | mTQueue.Pop();
|
---|
| 172 |
|
---|
| 173 | return splitCandidate;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 |
|
---|
| 177 | void HierarchyManager::EvalSubdivisionStats(const SubdivisionCandidate &tData)
|
---|
| 178 | {
|
---|
| 179 | const float costDecr = tData.GetRenderCostDecrease();
|
---|
| 180 |
|
---|
[1308] | 181 | switch (mObjectSpaceSubdivisionType)
|
---|
[1287] | 182 | {
|
---|
| 183 | case KD_BASED_OBJ_SUBDIV:
|
---|
| 184 | AddSubdivisionStats(mOspTree->mOspStats.Leaves() + mVspTree->mVspStats.Leaves(),
|
---|
| 185 | costDecr,
|
---|
| 186 | mTotalCost
|
---|
| 187 | );
|
---|
| 188 | break;
|
---|
| 189 | case BV_BASED_OBJ_SUBDIV:
|
---|
| 190 | AddSubdivisionStats(mBvHierarchy->mBvhStats.Leaves() + mVspTree->mVspStats.Leaves(),
|
---|
| 191 | costDecr,
|
---|
| 192 | mTotalCost
|
---|
| 193 | );
|
---|
| 194 | break;
|
---|
| 195 | default:
|
---|
| 196 | AddSubdivisionStats(mVspTree->mVspStats.Leaves(),
|
---|
| 197 | costDecr,
|
---|
| 198 | mTotalCost
|
---|
| 199 | );
|
---|
| 200 | break;
|
---|
| 201 | }
|
---|
[1237] | 202 | }
|
---|
| 203 |
|
---|
| 204 |
|
---|
| 205 | void HierarchyManager::AddSubdivisionStats(const int splits,
|
---|
| 206 | const float renderCostDecr,
|
---|
| 207 | const float totalRenderCost)
|
---|
| 208 | {
|
---|
[1308] | 209 | mSubdivisionStats
|
---|
[1237] | 210 | << "#Splits\n" << splits << endl
|
---|
| 211 | << "#RenderCostDecrease\n" << renderCostDecr << endl
|
---|
| 212 | << "#TotalRenderCost\n" << totalRenderCost << endl;
|
---|
| 213 | //<< "#AvgRenderCost\n" << avgRenderCost << endl;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 | bool HierarchyManager::GlobalTerminationCriteriaMet(SubdivisionCandidate *candidate) const
|
---|
| 218 | {
|
---|
[1288] | 219 | return (0
|
---|
[1294] | 220 | || (mHierarchyStats.Leaves() >= mTermMaxLeaves)
|
---|
| 221 | //|| (mGlobalCostMisses >= mTermGlobalCostMissTolerance)
|
---|
[1302] | 222 | || candidate->GlobalTerminationCriteriaMet()
|
---|
[1288] | 223 | );
|
---|
[1237] | 224 | }
|
---|
| 225 |
|
---|
| 226 |
|
---|
| 227 | void HierarchyManager::Construct(const VssRayContainer &sampleRays,
|
---|
[1311] | 228 | const ObjectContainer &objects,
|
---|
| 229 | AxisAlignedBox3 *forcedViewSpace)
|
---|
[1237] | 230 | {
|
---|
[1308] | 231 | mHierarchyStats.Reset();
|
---|
| 232 | mHierarchyStats.Start();
|
---|
[1311] | 233 |
|
---|
[1308] | 234 | mTotalCost = (float)objects.size();
|
---|
| 235 | Debug << "setting total cost to " << mTotalCost << endl;
|
---|
[1237] | 236 |
|
---|
[1311] | 237 | const long startTime = GetTime();
|
---|
[1308] | 238 | cout << "Constructing view space / object space tree ... \n";
|
---|
[1237] | 239 |
|
---|
[1379] | 240 | // compute view space bounding box
|
---|
| 241 | mVspTree->ComputeBoundingBox(sampleRays, forcedViewSpace);
|
---|
| 242 |
|
---|
[1323] | 243 | // use objects for evaluating vsp tree construction in the first levels
|
---|
| 244 | // of the subdivision
|
---|
| 245 | mSavedObjectSpaceSubdivisionType = mObjectSpaceSubdivisionType;
|
---|
| 246 | mObjectSpaceSubdivisionType = NO_OBJ_SUBDIV;
|
---|
| 247 |
|
---|
[1370] | 248 | mSavedViewSpaceSubdivisionType = mViewSpaceSubdivisionType;
|
---|
| 249 | mViewSpaceSubdivisionType = NO_VIEWSPACE_SUBDIV;
|
---|
| 250 |
|
---|
[1323] | 251 | // start with view space subdivison: prepare vsp tree for traversal
|
---|
[1329] | 252 | if (StartViewSpaceSubdivision())
|
---|
| 253 | {
|
---|
| 254 | mViewSpaceSubdivisionType = mSavedViewSpaceSubdivisionType;
|
---|
[1379] | 255 | PrepareViewSpaceSubdivision(sampleRays, objects);
|
---|
[1329] | 256 | }
|
---|
| 257 |
|
---|
[1323] | 258 | // start object space subdivision immediately?
|
---|
| 259 | if (StartObjectSpaceSubdivision())
|
---|
| 260 | {
|
---|
| 261 | mObjectSpaceSubdivisionType = mSavedObjectSpaceSubdivisionType;
|
---|
| 262 | PrepareObjectSpaceSubdivision(sampleRays, objects);
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[1294] | 265 | // process object space candidates
|
---|
[1311] | 266 | RunConstruction(sampleRays, objects, forcedViewSpace);
|
---|
[1308] | 267 |
|
---|
[1302] | 268 | cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
[1370] | 269 |
|
---|
[1313] | 270 | mHierarchyStats.Stop();
|
---|
[1259] | 271 | mVspTree->mVspStats.Stop();
|
---|
[1370] | 272 | FinishObjectSpaceSubdivision();
|
---|
| 273 |
|
---|
| 274 | mObjectSpaceSubdivisionType = mSavedObjectSpaceSubdivisionType;
|
---|
| 275 | mViewSpaceSubdivisionType = mSavedViewSpaceSubdivisionType;
|
---|
[1237] | 276 | }
|
---|
| 277 |
|
---|
| 278 |
|
---|
[1311] | 279 | void HierarchyManager::PrepareViewSpaceSubdivision(const VssRayContainer &sampleRays,
|
---|
[1379] | 280 | const ObjectContainer &objects)
|
---|
[1311] | 281 | {
|
---|
[1370] | 282 | cout << "starting view space hierarchy construction ... " << endl;
|
---|
| 283 |
|
---|
[1311] | 284 | RayInfoContainer *viewSpaceRays = new RayInfoContainer();
|
---|
| 285 | SubdivisionCandidate *vsc =
|
---|
[1379] | 286 | mVspTree->PrepareConstruction(sampleRays, *viewSpaceRays);
|
---|
[1311] | 287 |
|
---|
| 288 | mTQueue.Push(vsc);
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 |
|
---|
[1308] | 292 | void HierarchyManager::PrepareObjectSpaceSubdivision(const VssRayContainer &sampleRays,
|
---|
| 293 | const ObjectContainer &objects)
|
---|
| 294 | {
|
---|
| 295 | if (mObjectSpaceSubdivisionType == KD_BASED_OBJ_SUBDIV)
|
---|
| 296 | {
|
---|
| 297 | PrepareOspTree(sampleRays, objects);
|
---|
| 298 | }
|
---|
| 299 | else if (mObjectSpaceSubdivisionType == BV_BASED_OBJ_SUBDIV)
|
---|
| 300 | {
|
---|
| 301 | PrepareBvHierarchy(sampleRays, objects);
|
---|
| 302 | }
|
---|
| 303 | }
|
---|
[1294] | 304 |
|
---|
[1308] | 305 |
|
---|
| 306 | void HierarchyManager::PrepareBvHierarchy(const VssRayContainer &sampleRays,
|
---|
| 307 | const ObjectContainer &objects)
|
---|
| 308 |
|
---|
[1294] | 309 | {
|
---|
| 310 | cout << "starting bv hierarchy construction ... " << endl;
|
---|
| 311 |
|
---|
| 312 | mBvHierarchy->CreateRoot(objects);
|
---|
| 313 |
|
---|
| 314 | // compute first candidate
|
---|
| 315 | SubdivisionCandidate *sc =
|
---|
| 316 | mBvHierarchy->PrepareConstruction(sampleRays, objects);
|
---|
| 317 |
|
---|
| 318 | mTotalCost = mBvHierarchy->mTotalCost;
|
---|
| 319 | Debug << "reseting cost, new total cost: " << mTotalCost << endl;
|
---|
| 320 |
|
---|
[1308] | 321 | mTQueue.Push(sc);
|
---|
[1294] | 322 | }
|
---|
| 323 |
|
---|
| 324 |
|
---|
[1308] | 325 | void HierarchyManager::PrepareOspTree(const VssRayContainer &sampleRays,
|
---|
| 326 | const ObjectContainer &objects)
|
---|
[1294] | 327 | {
|
---|
[1370] | 328 | cout << "starting osp tree construction ... " << endl;
|
---|
| 329 |
|
---|
[1294] | 330 | RayInfoContainer *objectSpaceRays = new RayInfoContainer();
|
---|
| 331 |
|
---|
| 332 | // start with one big kd cell - all objects can be seen from everywhere
|
---|
| 333 | // note: only true for view space = object space
|
---|
| 334 |
|
---|
| 335 | // compute first candidate
|
---|
| 336 | SubdivisionCandidate *osc =
|
---|
| 337 | mOspTree->PrepareConstruction(sampleRays, objects, *objectSpaceRays);
|
---|
| 338 |
|
---|
| 339 | mTotalCost = mOspTree->mTotalCost;
|
---|
| 340 | Debug << "reseting cost, new total cost: " << mTotalCost << endl;
|
---|
| 341 |
|
---|
| 342 | mTQueue.Push(osc);
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 |
|
---|
[1287] | 346 | bool HierarchyManager::ApplySubdivisionCandidate(SubdivisionCandidate *sc)
|
---|
[1237] | 347 | {
|
---|
[1293] | 348 | const bool globalTerminationCriteriaMet = GlobalTerminationCriteriaMet(sc);
|
---|
[1237] | 349 | const bool vspSplit = (sc->Type() == SubdivisionCandidate::VIEW_SPACE);
|
---|
| 350 |
|
---|
| 351 | if (vspSplit)
|
---|
| 352 | {
|
---|
[1259] | 353 | VspNode *n = mVspTree->Subdivide(mTQueue, sc, globalTerminationCriteriaMet);
|
---|
[1288] | 354 |
|
---|
| 355 | if (n->IsLeaf()) // local or global termination criteria failed
|
---|
| 356 | return false;
|
---|
[1237] | 357 | }
|
---|
| 358 | else
|
---|
| 359 | {
|
---|
[1308] | 360 | if (mObjectSpaceSubdivisionType == KD_BASED_OBJ_SUBDIV)
|
---|
[1287] | 361 | {
|
---|
| 362 | KdNode *n = mOspTree->Subdivide(mTQueue, sc, globalTerminationCriteriaMet);
|
---|
[1288] | 363 |
|
---|
| 364 | if (n->IsLeaf()) // local or global termination criteria failed
|
---|
| 365 | return false;
|
---|
[1287] | 366 | }
|
---|
[1308] | 367 | else if (mObjectSpaceSubdivisionType == BV_BASED_OBJ_SUBDIV)
|
---|
[1287] | 368 | {
|
---|
| 369 | BvhNode *n = mBvHierarchy->Subdivide(mTQueue, sc, globalTerminationCriteriaMet);
|
---|
[1294] | 370 |
|
---|
[1288] | 371 | if (n->IsLeaf()) // local or global termination criteria failed
|
---|
| 372 | return false;
|
---|
[1287] | 373 | }
|
---|
[1237] | 374 | }
|
---|
[1288] | 375 | return true;//!globalTerminationCriteriaMet;
|
---|
[1237] | 376 | }
|
---|
| 377 |
|
---|
| 378 |
|
---|
[1370] | 379 | int HierarchyManager::GetObjectSpaceSubdivisionDepth() const
|
---|
| 380 | {
|
---|
| 381 | int maxDepth = 0;
|
---|
| 382 |
|
---|
| 383 | if (mObjectSpaceSubdivisionType == KD_BASED_OBJ_SUBDIV)
|
---|
| 384 | {
|
---|
| 385 | maxDepth = mOspTree->mOspStats.maxDepth;
|
---|
| 386 | }
|
---|
| 387 | else if (mObjectSpaceSubdivisionType == BV_BASED_OBJ_SUBDIV)
|
---|
| 388 | {
|
---|
| 389 | maxDepth = mBvHierarchy->mBvhStats.maxDepth;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | return maxDepth;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 |
|
---|
[1308] | 396 | bool HierarchyManager::StartObjectSpaceSubdivision() const
|
---|
[1237] | 397 | {
|
---|
[1370] | 398 | // view space construction already started
|
---|
| 399 | if (ObjectSpaceSubdivisionConstructed())
|
---|
| 400 | return false;
|
---|
| 401 |
|
---|
| 402 | // start immediately with object space subdivision?
|
---|
| 403 | if (mStartWithObjectSpace)
|
---|
| 404 | return true;
|
---|
| 405 |
|
---|
| 406 | // is the queue empty again?
|
---|
| 407 | if (ViewSpaceSubdivisionConstructed() && mTQueue.Empty())
|
---|
| 408 | return true;
|
---|
| 409 |
|
---|
| 410 | // has the depth for subdivision been reached?
|
---|
| 411 | return
|
---|
| 412 | ((mConstructionType == INTERLEAVED) &&
|
---|
| 413 | (mMinDepthForObjectSpaceSubdivion <= mVspTree->mVspStats.maxDepth));
|
---|
[1308] | 414 | }
|
---|
| 415 |
|
---|
| 416 |
|
---|
[1329] | 417 | bool HierarchyManager::StartViewSpaceSubdivision() const
|
---|
| 418 | {
|
---|
[1370] | 419 | // view space construction already started
|
---|
| 420 | if (ViewSpaceSubdivisionConstructed())
|
---|
| 421 | return false;
|
---|
| 422 |
|
---|
| 423 | // start immediately with view space subdivision?
|
---|
| 424 | if (!mStartWithObjectSpace)
|
---|
| 425 | return true;
|
---|
| 426 |
|
---|
| 427 | // is the queue empty again?
|
---|
| 428 | if (ObjectSpaceSubdivisionConstructed() && mTQueue.Empty())
|
---|
| 429 | return true;
|
---|
| 430 |
|
---|
| 431 | // has the depth for subdivision been reached?
|
---|
| 432 | return
|
---|
| 433 | ((mConstructionType == INTERLEAVED) &&
|
---|
| 434 | (mMinDepthForViewSpaceSubdivion <= GetObjectSpaceSubdivisionDepth()));
|
---|
[1329] | 435 | }
|
---|
| 436 |
|
---|
| 437 |
|
---|
[1311] | 438 | void HierarchyManager::RunConstruction(const VssRayContainer &sampleRays,
|
---|
| 439 | const ObjectContainer &objects,
|
---|
| 440 | AxisAlignedBox3 *forcedViewSpace)
|
---|
[1308] | 441 | {
|
---|
[1288] | 442 | mHierarchyStats.nodes = 0;
|
---|
| 443 | mGlobalCostMisses = 0;
|
---|
[1313] | 444 |
|
---|
| 445 | int i = 0;
|
---|
| 446 | while (!FinishedConstruction())
|
---|
| 447 | {
|
---|
[1305] | 448 | mCurrentCandidate = NextSubdivisionCandidate();
|
---|
[1294] | 449 | mTotalCost -= mCurrentCandidate->GetRenderCostDecrease();
|
---|
[1237] | 450 |
|
---|
| 451 | // cost ratio of cost decrease / totalCost
|
---|
[1294] | 452 | const float costRatio = mCurrentCandidate->GetRenderCostDecrease() / mTotalCost;
|
---|
[1237] | 453 |
|
---|
[1291] | 454 | //Debug << "ratio: " << costRatio << " min ratio: " << mTermMinGlobalCostRatio << endl;
|
---|
[1237] | 455 | if (costRatio < mTermMinGlobalCostRatio)
|
---|
[1305] | 456 | {
|
---|
[1237] | 457 | ++ mGlobalCostMisses;
|
---|
[1305] | 458 | }
|
---|
[1313] | 459 |
|
---|
[1370] | 460 | ///////////////////////////
|
---|
[1237] | 461 | //-- subdivide leaf node
|
---|
[1294] | 462 | if (ApplySubdivisionCandidate(mCurrentCandidate))
|
---|
[1237] | 463 | {
|
---|
[1313] | 464 | cout << "subdividing candidate " << ++ i << " of type " << mCurrentCandidate->Type() << endl;
|
---|
[1288] | 465 | mHierarchyStats.nodes += 2;
|
---|
| 466 |
|
---|
[1237] | 467 | // subdivision successful
|
---|
[1294] | 468 | EvalSubdivisionStats(*mCurrentCandidate);
|
---|
[1237] | 469 |
|
---|
[1305] | 470 | // reevaluate candidates affected by the split for view space splits,
|
---|
| 471 | // this would be object space splits and other way round
|
---|
[1314] | 472 | if (mRepairQueue) RepairQueue();
|
---|
[1311] | 473 | }
|
---|
| 474 |
|
---|
[1313] | 475 | // we use objects for evaluating vsp tree construction until
|
---|
| 476 | // a certain depth once a certain depth existiert ....
|
---|
| 477 | if (StartObjectSpaceSubdivision())
|
---|
| 478 | {
|
---|
[1323] | 479 | mObjectSpaceSubdivisionType = mSavedObjectSpaceSubdivisionType;
|
---|
[1314] | 480 |
|
---|
[1329] | 481 | cout << "starting object space subdivision at depth "
|
---|
| 482 | << mVspTree->mVspStats.maxDepth << " ("
|
---|
| 483 | << mMinDepthForObjectSpaceSubdivion << ") " << endl;
|
---|
| 484 |
|
---|
[1314] | 485 | PrepareObjectSpaceSubdivision(sampleRays, objects);
|
---|
| 486 |
|
---|
[1313] | 487 | cout << "reseting queue ... ";
|
---|
| 488 | ResetQueue();
|
---|
| 489 | cout << "finished" << endl;
|
---|
| 490 | }
|
---|
| 491 |
|
---|
[1329] | 492 | if (StartViewSpaceSubdivision())
|
---|
| 493 | {
|
---|
| 494 | mViewSpaceSubdivisionType = mSavedViewSpaceSubdivisionType;
|
---|
| 495 |
|
---|
| 496 | cout << "starting view space subdivision at depth "
|
---|
[1370] | 497 | << GetObjectSpaceSubdivisionDepth() << " ("
|
---|
| 498 | << mMinDepthForViewSpaceSubdivion << ") " << endl;
|
---|
[1329] | 499 |
|
---|
[1379] | 500 | PrepareViewSpaceSubdivision(sampleRays, objects);
|
---|
[1329] | 501 |
|
---|
| 502 | cout << "reseting queue ... ";
|
---|
| 503 | ResetQueue();
|
---|
| 504 | cout << "finished" << endl;
|
---|
| 505 | }
|
---|
| 506 |
|
---|
[1294] | 507 | DEL_PTR(mCurrentCandidate);
|
---|
[1237] | 508 | }
|
---|
| 509 | }
|
---|
| 510 |
|
---|
| 511 |
|
---|
| 512 | bool HierarchyManager::FinishedConstruction() const
|
---|
| 513 | {
|
---|
| 514 | return mTQueue.Empty();
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 |
|
---|
[1313] | 518 | bool HierarchyManager::ObjectSpaceSubdivisionConstructed() const
|
---|
[1311] | 519 | {
|
---|
| 520 | switch (mObjectSpaceSubdivisionType)
|
---|
| 521 | {
|
---|
| 522 | case KD_BASED_OBJ_SUBDIV:
|
---|
| 523 | return mOspTree && mOspTree->GetRoot();
|
---|
| 524 | case BV_BASED_OBJ_SUBDIV:
|
---|
[1344] | 525 | return mBvHierarchy && mBvHierarchy->GetRoot();
|
---|
[1311] | 526 | default:
|
---|
| 527 | return false;
|
---|
| 528 | }
|
---|
| 529 | }
|
---|
| 530 |
|
---|
| 531 |
|
---|
[1329] | 532 | bool HierarchyManager::ViewSpaceSubdivisionConstructed() const
|
---|
| 533 | {
|
---|
| 534 | return mVspTree && mVspTree->GetRoot();
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 |
|
---|
[1294] | 538 | void HierarchyManager::CollectObjectSpaceDirtyList(SubdivisionCandidateContainer &dirtyList)
|
---|
[1259] | 539 | {
|
---|
[1308] | 540 | switch (mObjectSpaceSubdivisionType)
|
---|
[1259] | 541 | {
|
---|
| 542 | case KD_BASED_OBJ_SUBDIV:
|
---|
| 543 | {
|
---|
| 544 | OspTree::OspSubdivisionCandidate *sc =
|
---|
| 545 | dynamic_cast<OspTree::OspSubdivisionCandidate *>(mCurrentCandidate);
|
---|
| 546 |
|
---|
| 547 | mOspTree->CollectDirtyCandidates(sc, dirtyList);
|
---|
| 548 | break;
|
---|
| 549 | }
|
---|
| 550 | case BV_BASED_OBJ_SUBDIV:
|
---|
| 551 | {
|
---|
| 552 | BvHierarchy::BvhSubdivisionCandidate *sc =
|
---|
| 553 | dynamic_cast<BvHierarchy::BvhSubdivisionCandidate *>(mCurrentCandidate);
|
---|
| 554 |
|
---|
| 555 | mBvHierarchy->CollectDirtyCandidates(sc, dirtyList);
|
---|
| 556 | break;
|
---|
| 557 | }
|
---|
| 558 | default:
|
---|
| 559 | break;
|
---|
| 560 | }
|
---|
| 561 | }
|
---|
| 562 |
|
---|
| 563 |
|
---|
| 564 | void HierarchyManager::CollectViewSpaceDirtyList(SubdivisionCandidateContainer &dirtyList)
|
---|
| 565 | {
|
---|
| 566 | VspTree::VspSubdivisionCandidate *sc =
|
---|
| 567 | dynamic_cast<VspTree::VspSubdivisionCandidate *>(mCurrentCandidate);
|
---|
| 568 |
|
---|
| 569 | mVspTree->CollectDirtyCandidates(sc, dirtyList);
|
---|
| 570 | }
|
---|
| 571 |
|
---|
| 572 |
|
---|
| 573 | void HierarchyManager::CollectDirtyCandidates(SubdivisionCandidateContainer &dirtyList)
|
---|
[1302] | 574 | {
|
---|
[1237] | 575 | // we have either a object space or view space split
|
---|
| 576 | if (mCurrentCandidate->Type() == SubdivisionCandidate::VIEW_SPACE)
|
---|
| 577 | {
|
---|
[1259] | 578 | CollectViewSpaceDirtyList(dirtyList);
|
---|
[1237] | 579 | }
|
---|
| 580 | else // object space split
|
---|
[1305] | 581 | {
|
---|
[1259] | 582 | CollectObjectSpaceDirtyList(dirtyList);
|
---|
[1237] | 583 | }
|
---|
| 584 | }
|
---|
| 585 |
|
---|
| 586 |
|
---|
| 587 | void HierarchyManager::RepairQueue()
|
---|
| 588 | {
|
---|
| 589 | // for each update of the view space partition:
|
---|
| 590 | // the candidates from object space partition which
|
---|
| 591 | // have been afected by the view space split (the kd split candidates
|
---|
| 592 | // which saw the view cell which was split) must be reevaluated
|
---|
| 593 | // (maybe not locally, just reinsert them into the queue)
|
---|
| 594 | //
|
---|
| 595 | // vice versa for the view cells
|
---|
| 596 | // for each update of the object space partition
|
---|
| 597 | // reevaluate split candidate for view cells which saw the split kd cell
|
---|
| 598 | //
|
---|
| 599 | // the priority queue update can be solved by implementing a binary heap
|
---|
| 600 | // (explicit data structure, binary tree)
|
---|
| 601 | // *) inserting and removal is efficient
|
---|
| 602 | // *) search is not efficient => store queue position with each
|
---|
| 603 | // split candidate
|
---|
| 604 |
|
---|
| 605 | // collect list of "dirty" candidates
|
---|
[1313] | 606 | long startTime = GetTime();
|
---|
| 607 |
|
---|
[1237] | 608 | vector<SubdivisionCandidate *> dirtyList;
|
---|
| 609 | CollectDirtyCandidates(dirtyList);
|
---|
[1313] | 610 | cout << "repairing " << (int)dirtyList.size() << " candidates ... ";
|
---|
| 611 |
|
---|
[1305] | 612 | //-- reevaluate the dirty list
|
---|
[1313] | 613 | SubdivisionCandidateContainer::const_iterator sit, sit_end = dirtyList.end();
|
---|
[1302] | 614 |
|
---|
[1237] | 615 | for (sit = dirtyList.begin(); sit != sit_end; ++ sit)
|
---|
| 616 | {
|
---|
| 617 | SubdivisionCandidate* sc = *sit;
|
---|
[1305] | 618 | const float rcd = sc->GetRenderCostDecrease();
|
---|
[1302] | 619 |
|
---|
| 620 | mTQueue.Erase(sc); // erase from queue
|
---|
| 621 | sc->EvalPriority(); // reevaluate
|
---|
| 622 |
|
---|
[1305] | 623 | /*
|
---|
| 624 | Debug << "candidate " << sc << " reevaluated\n"
|
---|
| 625 | << "render cost decrease diff " << rcd - sc->GetRenderCostDecrease()
|
---|
| 626 | << " old: " << rcd << " new " << sc->GetRenderCostDecrease() << endl;*/
|
---|
| 627 | if (0)
|
---|
| 628 | {
|
---|
| 629 | const float rcDiff = rcd - sc->GetRenderCostDecrease();
|
---|
| 630 | mTotalCost += rcDiff;
|
---|
| 631 | }
|
---|
[1302] | 632 | mTQueue.Push(sc); // reinsert
|
---|
[1237] | 633 | }
|
---|
[1313] | 634 |
|
---|
| 635 | long endTime = GetTime();
|
---|
| 636 | Real timeDiff = TimeDiff(startTime, endTime);
|
---|
| 637 |
|
---|
| 638 | mHierarchyStats.repairTime += timeDiff;
|
---|
| 639 |
|
---|
| 640 | cout << "finished in " << timeDiff * 1e-3f << " secs" << endl;
|
---|
[1237] | 641 | }
|
---|
| 642 |
|
---|
[1259] | 643 |
|
---|
[1313] | 644 | void HierarchyManager::ResetQueue()
|
---|
| 645 | {
|
---|
| 646 | SubdivisionCandidateContainer mCandidateBuffer;
|
---|
| 647 |
|
---|
| 648 | // remove from queue
|
---|
| 649 | while (!mTQueue.Empty())
|
---|
| 650 | {
|
---|
| 651 | SubdivisionCandidate *candidate = NextSubdivisionCandidate();
|
---|
| 652 | candidate->EvalPriority(); // reevaluate
|
---|
| 653 | cout << ".";
|
---|
| 654 | mCandidateBuffer.push_back(candidate);
|
---|
| 655 | }
|
---|
| 656 |
|
---|
| 657 | // put back into queue
|
---|
| 658 | SubdivisionCandidateContainer::const_iterator sit, sit_end = mCandidateBuffer.end();
|
---|
| 659 | for (sit = mCandidateBuffer.begin(); sit != sit_end; ++ sit)
|
---|
| 660 | {cout << ":";
|
---|
| 661 | mTQueue.Push(*sit);
|
---|
| 662 | }
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 |
|
---|
[1279] | 666 | void HierarchyManager::ExportObjectSpaceHierarchy(OUT_STREAM &stream)
|
---|
| 667 | {
|
---|
| 668 | // the type of the view cells hierarchy
|
---|
[1308] | 669 | switch (mObjectSpaceSubdivisionType)
|
---|
[1279] | 670 | {
|
---|
| 671 | case KD_BASED_OBJ_SUBDIV:
|
---|
| 672 | stream << "<ObjectSpaceHierarchy type=\"osp\">" << endl;
|
---|
| 673 | mOspTree->Export(stream);
|
---|
| 674 | stream << endl << "</ObjectSpaceHierarchy>" << endl;
|
---|
[1305] | 675 | break;
|
---|
[1279] | 676 | case BV_BASED_OBJ_SUBDIV:
|
---|
| 677 | stream << "<ObjectSpaceHierarchy type=\"bvh\">" << endl;
|
---|
| 678 | mBvHierarchy->Export(stream);
|
---|
| 679 | stream << endl << "</ObjectSpaceHierarchy>" << endl;
|
---|
| 680 | break;
|
---|
| 681 | }
|
---|
| 682 | }
|
---|
| 683 |
|
---|
| 684 |
|
---|
| 685 | bool HierarchyManager::AddSampleToPvs(Intersectable *obj,
|
---|
| 686 | const Vector3 &hitPoint,
|
---|
| 687 | ViewCell *vc,
|
---|
| 688 | const float pdf,
|
---|
| 689 | float &contribution) const
|
---|
| 690 | {
|
---|
| 691 | if (!obj) return false;
|
---|
| 692 |
|
---|
[1308] | 693 | switch (mObjectSpaceSubdivisionType)
|
---|
[1279] | 694 | {
|
---|
| 695 | case NO_OBJ_SUBDIV:
|
---|
| 696 | // potentially visible objects
|
---|
| 697 | return vc->AddPvsSample(obj, pdf, contribution);
|
---|
| 698 | case KD_BASED_OBJ_SUBDIV:
|
---|
| 699 | {
|
---|
| 700 | // potentially visible kd cells
|
---|
| 701 | KdLeaf *leaf = mOspTree->GetLeaf(hitPoint/*ray->mOriginNode*/);
|
---|
| 702 | return mOspTree->AddLeafToPvs(leaf, vc, pdf, contribution);
|
---|
| 703 | }
|
---|
| 704 | case BV_BASED_OBJ_SUBDIV:
|
---|
| 705 | {
|
---|
| 706 | BvhLeaf *leaf = mBvHierarchy->GetLeaf(obj);
|
---|
[1370] | 707 | BvhIntersectable *bvhObj = mBvHierarchy->GetOrCreateBvhIntersectable(leaf);
|
---|
| 708 |
|
---|
| 709 | return vc->AddPvsSample(bvhObj, pdf, contribution);
|
---|
[1279] | 710 | }
|
---|
| 711 | default:
|
---|
| 712 | return false;
|
---|
| 713 | }
|
---|
| 714 | }
|
---|
| 715 |
|
---|
| 716 |
|
---|
[1313] | 717 | void HierarchyManager::PrintHierarchyStatistics(ofstream &stream) const
|
---|
[1279] | 718 | {
|
---|
[1313] | 719 | stream << mHierarchyStats << endl;
|
---|
| 720 |
|
---|
| 721 | stream << "\nview space:" << endl << endl;
|
---|
| 722 | stream << mVspTree->GetStatistics() << endl;
|
---|
| 723 | stream << "\nobject space:" << endl << endl;
|
---|
[1370] | 724 |
|
---|
[1308] | 725 | switch (mObjectSpaceSubdivisionType)
|
---|
[1279] | 726 | {
|
---|
| 727 | case KD_BASED_OBJ_SUBDIV:
|
---|
| 728 | {
|
---|
[1313] | 729 | stream << mOspTree->GetStatistics() << endl;
|
---|
[1279] | 730 | break;
|
---|
| 731 | }
|
---|
| 732 | case BV_BASED_OBJ_SUBDIV:
|
---|
| 733 | {
|
---|
[1313] | 734 | stream << mBvHierarchy->GetStatistics() << endl;
|
---|
[1279] | 735 | break;
|
---|
| 736 | }
|
---|
| 737 | default:
|
---|
| 738 | break;
|
---|
| 739 | }
|
---|
| 740 | }
|
---|
| 741 |
|
---|
| 742 |
|
---|
[1287] | 743 | void HierarchyManager::ExportObjectSpaceHierarchy(Exporter *exporter,
|
---|
| 744 | const ObjectContainer &objects) const
|
---|
[1279] | 745 | {
|
---|
[1308] | 746 | switch (mObjectSpaceSubdivisionType)
|
---|
[1286] | 747 | {
|
---|
| 748 | case KD_BASED_OBJ_SUBDIV:
|
---|
[1279] | 749 | {
|
---|
[1287] | 750 | ExportOspTree(exporter, objects);
|
---|
[1286] | 751 | break;
|
---|
| 752 | }
|
---|
| 753 | case BV_BASED_OBJ_SUBDIV:
|
---|
| 754 | {
|
---|
[1287] | 755 | ExportBvHierarchy(exporter, objects);
|
---|
[1286] | 756 | break;
|
---|
| 757 | }
|
---|
| 758 | default:
|
---|
| 759 | break;
|
---|
| 760 | }
|
---|
| 761 | }
|
---|
[1279] | 762 |
|
---|
[1286] | 763 |
|
---|
[1287] | 764 | void HierarchyManager::ExportBvHierarchy(Exporter *exporter,
|
---|
| 765 | const ObjectContainer &objects) const
|
---|
[1286] | 766 | {
|
---|
[1287] | 767 | exporter->SetWireframe();
|
---|
| 768 | exporter->ExportBvHierarchy(*mBvHierarchy, 0);
|
---|
[1286] | 769 | }
|
---|
| 770 |
|
---|
| 771 |
|
---|
[1287] | 772 | void HierarchyManager::ExportOspTree(Exporter *exporter,
|
---|
| 773 | const ObjectContainer &objects) const
|
---|
[1286] | 774 | {
|
---|
[1287] | 775 | if (0) exporter->ExportGeometry(objects);
|
---|
[1279] | 776 |
|
---|
[1287] | 777 | exporter->SetWireframe();
|
---|
| 778 | exporter->ExportOspTree(*mOspTree, 0);
|
---|
[1286] | 779 | }
|
---|
| 780 |
|
---|
| 781 |
|
---|
[1313] | 782 | void HierarchyStatistics::Print(ostream &app) const
|
---|
| 783 | {
|
---|
| 784 | app << "=========== Hierarchy statistics ===============\n";
|
---|
| 785 |
|
---|
| 786 | app << setprecision(4);
|
---|
| 787 |
|
---|
| 788 | app << "#N_CTIME ( Construction time [s] )\n" << Time() << " \n";
|
---|
| 789 |
|
---|
| 790 | app << "#N_RTIME ( Repair time [s] )\n" << repairTime * 1e-3f << " \n";
|
---|
| 791 |
|
---|
| 792 | app << "#N_NODES ( Number of nodes )\n" << nodes << "\n";
|
---|
| 793 |
|
---|
| 794 | app << "#N_INTERIORS ( Number of interior nodes )\n" << Interior() << "\n";
|
---|
| 795 |
|
---|
| 796 | app << "#N_LEAVES ( Number of leaves )\n" << Leaves() << "\n";
|
---|
| 797 |
|
---|
| 798 | app << "#N_PMAXDEPTH ( Maximal reached depth )\n" << maxDepth << endl;
|
---|
| 799 |
|
---|
| 800 | app << "========== END OF Hierarchy statistics ==========\n";
|
---|
| 801 | }
|
---|
| 802 |
|
---|
| 803 |
|
---|
[1370] | 804 | void HierarchyManager::FinishObjectSpaceSubdivision() const
|
---|
| 805 | {
|
---|
| 806 | switch (mObjectSpaceSubdivisionType)
|
---|
| 807 | {
|
---|
| 808 | case KD_BASED_OBJ_SUBDIV:
|
---|
| 809 | {
|
---|
| 810 | mOspTree->mOspStats.Stop();
|
---|
| 811 | break;
|
---|
| 812 | }
|
---|
| 813 | case BV_BASED_OBJ_SUBDIV:
|
---|
| 814 | {
|
---|
| 815 | mBvHierarchy->mBvhStats.Stop();
|
---|
| 816 | break;
|
---|
| 817 | }
|
---|
| 818 | default:
|
---|
| 819 | break;
|
---|
| 820 | }
|
---|
| 821 | }
|
---|
| 822 |
|
---|
[1237] | 823 | } |
---|