[235] | 1 | #include "ViewCell.h"
|
---|
| 2 | #include "Mesh.h"
|
---|
[308] | 3 | #include "Intersectable.h"
|
---|
[235] | 4 | #include "MeshKdTree.h"
|
---|
| 5 | #include "Triangle3.h"
|
---|
[580] | 6 | #include "common.h"
|
---|
| 7 | #include "Environment.h"
|
---|
| 8 | #include "ViewCellsManager.h"
|
---|
| 9 | #include "Exporter.h"
|
---|
| 10 |
|
---|
[463] | 11 | #include <time.h>
|
---|
[462] | 12 | #include <iomanip>
|
---|
[580] | 13 | #include <stack>
|
---|
[235] | 14 |
|
---|
[580] | 15 |
|
---|
| 16 |
|
---|
| 17 | template <typename T> class myless
|
---|
| 18 | {
|
---|
| 19 | public:
|
---|
| 20 | //bool operator() (HierarchyNode *v1, HierarchyNode *v2) const
|
---|
| 21 | bool operator() (T v1, T v2) const
|
---|
| 22 | {
|
---|
[600] | 23 | return (v1->GetMergeCost() < v2->GetMergeCost());
|
---|
[580] | 24 | }
|
---|
| 25 | };
|
---|
| 26 |
|
---|
| 27 |
|
---|
| 28 | typedef priority_queue<ViewCell *, vector<ViewCell *>, myless<vector<ViewCell *>::value_type> > TraversalQueue;
|
---|
| 29 |
|
---|
| 30 | int ViewCell::sMailId = 21843194198;
|
---|
| 31 | int ViewCell::sReservedMailboxes = 1;
|
---|
[660] | 32 | int ViewCell::sLastUpdated = 0;
|
---|
[580] | 33 |
|
---|
| 34 | //int upperPvsLimit = 120;
|
---|
| 35 | //int lowerPvsLimit = 5;
|
---|
| 36 |
|
---|
| 37 | float MergeCandidate::sRenderCostWeight = 0;
|
---|
| 38 |
|
---|
| 39 |
|
---|
| 40 | // pvs penalty can be different from pvs size
|
---|
| 41 | inline float EvalPvsPenalty(const int pvs,
|
---|
| 42 | const int lower,
|
---|
| 43 | const int upper)
|
---|
| 44 | {
|
---|
| 45 | // clamp to minmax values
|
---|
[601] | 46 | /*if (pvs < lower)
|
---|
[580] | 47 | return (float)lower;
|
---|
| 48 | if (pvs > upper)
|
---|
| 49 | return (float)upper;
|
---|
[601] | 50 | */
|
---|
[580] | 51 | return (float)pvs;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 |
|
---|
[585] | 55 | inline int CountDiffPvs(ViewCell *vc)
|
---|
| 56 | {
|
---|
| 57 | int count = 0;
|
---|
| 58 |
|
---|
| 59 | ObjectPvsMap::const_iterator it, it_end = vc->GetPvs().mEntries.end();
|
---|
| 60 | for (it = vc->GetPvs().mEntries.begin(); it != it_end; ++ it)
|
---|
| 61 | {
|
---|
| 62 | if (!(*it).first->Mailed())
|
---|
| 63 | {
|
---|
| 64 | (*it).first->Mail();
|
---|
| 65 | ++ count;
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | return count;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 |
|
---|
[580] | 73 | int ComputeMergedPvsSize(const ObjectPvs &pvs1, const ObjectPvs &pvs2)
|
---|
| 74 | {
|
---|
| 75 | int pvs = pvs1.GetSize();
|
---|
| 76 |
|
---|
| 77 | // compute new pvs size
|
---|
| 78 | ObjectPvsMap::const_iterator it, it_end = pvs1.mEntries.end();
|
---|
| 79 |
|
---|
| 80 | Intersectable::NewMail();
|
---|
| 81 |
|
---|
| 82 | for (it = pvs1.mEntries.begin(); it != it_end; ++ it)
|
---|
| 83 | {
|
---|
| 84 | (*it).first->Mail();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | it_end = pvs2.mEntries.end();
|
---|
| 88 |
|
---|
| 89 | for (it = pvs2.mEntries.begin(); it != it_end; ++ it)
|
---|
| 90 | {
|
---|
| 91 | Intersectable *obj = (*it).first;
|
---|
| 92 | if (!obj->Mailed())
|
---|
| 93 | ++ pvs;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | return pvs;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 |
|
---|
[478] | 100 | ViewCell::ViewCell():
|
---|
| 101 | MeshInstance(NULL),
|
---|
| 102 | mPiercingRays(0),
|
---|
[551] | 103 | mArea(-1),
|
---|
| 104 | mVolume(-1),
|
---|
[580] | 105 | mValid(true),
|
---|
| 106 | mParent(NULL),
|
---|
[600] | 107 | mMergeCost(0),
|
---|
[590] | 108 | mIsActive(false)
|
---|
[265] | 109 | {
|
---|
| 110 | }
|
---|
[235] | 111 |
|
---|
[478] | 112 | ViewCell::ViewCell(Mesh *mesh):
|
---|
| 113 | MeshInstance(mesh),
|
---|
| 114 | mPiercingRays(0),
|
---|
[551] | 115 | mArea(-1),
|
---|
| 116 | mVolume(-1),
|
---|
[580] | 117 | mValid(true),
|
---|
| 118 | mParent(NULL),
|
---|
[600] | 119 | mMergeCost(0),
|
---|
[660] | 120 | mIsActive(false),
|
---|
| 121 | mLastUpdated(sLastUpdated)
|
---|
[372] | 122 | {
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[503] | 125 |
|
---|
[469] | 126 | const ObjectPvs &ViewCell::GetPvs() const
|
---|
[419] | 127 | {
|
---|
| 128 | return mPvs;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[469] | 131 | ObjectPvs &ViewCell::GetPvs()
|
---|
[372] | 132 | {
|
---|
| 133 | return mPvs;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[503] | 136 |
|
---|
[235] | 137 | int ViewCell::Type() const
|
---|
| 138 | {
|
---|
[308] | 139 | return VIEW_CELL;
|
---|
[235] | 140 | }
|
---|
| 141 |
|
---|
[503] | 142 |
|
---|
[469] | 143 | float ViewCell::GetVolume() const
|
---|
| 144 | {
|
---|
| 145 | return mVolume;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[478] | 148 |
|
---|
[469] | 149 | void ViewCell::SetVolume(float volume)
|
---|
| 150 | {
|
---|
| 151 | mVolume = volume;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[503] | 154 |
|
---|
| 155 | void ViewCell::SetMesh(Mesh *mesh)
|
---|
| 156 | {
|
---|
| 157 | mMesh = mesh;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 |
|
---|
[478] | 161 | float ViewCell::GetArea() const
|
---|
| 162 | {
|
---|
| 163 | return mArea;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 |
|
---|
| 167 | void ViewCell::SetArea(float area)
|
---|
| 168 | {
|
---|
| 169 | mArea = area;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 |
|
---|
[547] | 173 | void ViewCell::SetValid(const bool valid)
|
---|
| 174 | {
|
---|
[564] | 175 | mValid = valid;
|
---|
[547] | 176 | }
|
---|
| 177 |
|
---|
| 178 |
|
---|
| 179 | bool ViewCell::GetValid() const
|
---|
| 180 | {
|
---|
| 181 | return mValid;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 |
|
---|
[580] | 185 | /*bool ViewCell::IsLeaf() const
|
---|
| 186 | {
|
---|
| 187 | return true;
|
---|
| 188 | }*/
|
---|
| 189 |
|
---|
| 190 |
|
---|
| 191 | void ViewCell::SetParent(ViewCellInterior *parent)
|
---|
| 192 | {
|
---|
| 193 | mParent = parent;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 |
|
---|
| 197 | bool ViewCell::IsRoot() const
|
---|
| 198 | {
|
---|
| 199 | return !mParent;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 |
|
---|
| 203 | ViewCellInterior *ViewCell::GetParent() const
|
---|
| 204 | {
|
---|
| 205 | return mParent;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 |
|
---|
[600] | 209 | void ViewCell::SetMergeCost(const float mergeCost)
|
---|
[580] | 210 | {
|
---|
[600] | 211 | mMergeCost = mergeCost;
|
---|
[580] | 212 | }
|
---|
| 213 |
|
---|
| 214 |
|
---|
[600] | 215 | float ViewCell::GetMergeCost() const
|
---|
[580] | 216 | {
|
---|
[600] | 217 | return mMergeCost;
|
---|
[580] | 218 | }
|
---|
| 219 |
|
---|
| 220 |
|
---|
[660] | 221 | void ViewCell::SetActive()
|
---|
| 222 | {
|
---|
| 223 | mIsActive = true;
|
---|
| 224 | mLastUpdated = sLastUpdated;
|
---|
| 225 | }
|
---|
[581] | 226 |
|
---|
[660] | 227 |
|
---|
| 228 | bool ViewCell::IsActive() const
|
---|
| 229 | {
|
---|
| 230 | return mIsActive && (mLastUpdated == sLastUpdated);
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 |
|
---|
[462] | 234 | /************************************************************************/
|
---|
[580] | 235 | /* class ViewCellInterior implementation */
|
---|
| 236 | /************************************************************************/
|
---|
| 237 |
|
---|
| 238 |
|
---|
| 239 | ViewCellInterior::ViewCellInterior()
|
---|
| 240 | {
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 |
|
---|
| 244 | ViewCellInterior::~ViewCellInterior()
|
---|
| 245 | {
|
---|
| 246 | ViewCellContainer::const_iterator it, it_end = mChildren.end();
|
---|
| 247 |
|
---|
| 248 | for (it = mChildren.begin(); it != it_end; ++ it)
|
---|
| 249 | delete (*it);
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 |
|
---|
| 253 | ViewCellInterior::ViewCellInterior(Mesh *mesh):
|
---|
| 254 | ViewCell(mesh)
|
---|
| 255 | {
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 |
|
---|
| 259 | bool ViewCellInterior::IsLeaf() const
|
---|
| 260 | {
|
---|
| 261 | return false;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 |
|
---|
[650] | 265 | void ViewCellInterior::SetupChildLink(ViewCell *vc)
|
---|
[580] | 266 | {
|
---|
[650] | 267 | mChildren.push_back(vc);
|
---|
| 268 | vc->mParent = this;
|
---|
[580] | 269 | }
|
---|
| 270 |
|
---|
| 271 |
|
---|
[586] | 272 | void ViewCellInterior::RemoveChildLink(ViewCell *l)
|
---|
| 273 | {
|
---|
| 274 | // erase leaf from old view cell
|
---|
| 275 | ViewCellContainer::iterator it = mChildren.begin();
|
---|
[580] | 276 |
|
---|
[586] | 277 | for (; (*it) != l; ++ it);
|
---|
| 278 | if (it == mChildren.end())
|
---|
| 279 | Debug << "error" << endl;
|
---|
| 280 | else
|
---|
| 281 | mChildren.erase(it);
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[580] | 284 | /************************************************************************/
|
---|
[462] | 285 | /* class ViewCellsStatistics implementation */
|
---|
| 286 | /************************************************************************/
|
---|
| 287 |
|
---|
[580] | 288 |
|
---|
| 289 |
|
---|
| 290 |
|
---|
[463] | 291 | void ViewCellsStatistics::Print(ostream &app) const
|
---|
| 292 | {
|
---|
| 293 | app << "=========== View Cells Statistics ===============\n";
|
---|
| 294 |
|
---|
| 295 | app << setprecision(4);
|
---|
| 296 |
|
---|
| 297 | //app << "#N_CTIME ( Construction time [s] )\n" << Time() << " \n";
|
---|
| 298 |
|
---|
[613] | 299 | app << "#N_OVERALLPVS ( objects in PVS )\n" << pvsSize << endl;
|
---|
[463] | 300 |
|
---|
| 301 | app << "#N_PMAXPVS ( largest PVS )\n" << maxPvs << endl;
|
---|
| 302 |
|
---|
| 303 | app << "#N_PMINPVS ( smallest PVS )\n" << minPvs << endl;
|
---|
| 304 |
|
---|
| 305 | app << "#N_PAVGPVS ( average PVS )\n" << AvgPvs() << endl;
|
---|
| 306 |
|
---|
[485] | 307 | app << "#N_PEMPTYPVS ( view cells with empty PVS )\n" << emptyPvs << endl;
|
---|
[463] | 308 |
|
---|
| 309 | app << "#N_VIEWCELLS ( number of view cells)\n" << viewCells << endl;
|
---|
| 310 |
|
---|
| 311 | app << "#N_AVGLEAVES (average number of leaves per view cell )\n" << AvgLeaves() << endl;
|
---|
| 312 |
|
---|
| 313 | app << "#N_MAXLEAVES ( maximal number of leaves per view cell )\n" << maxLeaves << endl;
|
---|
| 314 |
|
---|
[564] | 315 | app << "#N_INVALID ( number of invalid view cells )\n" << invalid << endl;
|
---|
| 316 |
|
---|
[463] | 317 | app << "========== End of View Cells Statistics ==========\n";
|
---|
[580] | 318 | }
|
---|
| 319 |
|
---|
| 320 |
|
---|
| 321 | /*************************************************************************/
|
---|
| 322 | /* class ViewCellsTree implementation */
|
---|
| 323 | /*************************************************************************/
|
---|
| 324 |
|
---|
| 325 |
|
---|
| 326 | ViewCellsTree::ViewCellsTree(ViewCellsManager *vcm):
|
---|
| 327 | mRoot(NULL),
|
---|
| 328 | mUseAreaForPvs(false),
|
---|
[584] | 329 | mViewCellsManager(vcm),
|
---|
| 330 | mIsCompressed(false)
|
---|
[580] | 331 | {
|
---|
| 332 | environment->GetBoolValue("ViewCells.Visualization.exportMergedViewCells", mExportMergedViewCells);
|
---|
[582] | 333 | environment->GetFloatValue("ViewCells.maxStaticMemory", mMaxMemory);
|
---|
[580] | 334 |
|
---|
| 335 | //-- merge options
|
---|
| 336 | environment->GetFloatValue("ViewCells.PostProcess.renderCostWeight", mRenderCostWeight);
|
---|
| 337 | environment->GetIntValue("ViewCells.PostProcess.minViewCells", mMergeMinViewCells);
|
---|
| 338 | environment->GetFloatValue("ViewCells.PostProcess.maxCostRatio", mMergeMaxCostRatio);
|
---|
[586] | 339 | environment->GetBoolValue("ViewCells.PostProcess.refine", mRefineViewCells);
|
---|
[582] | 340 |
|
---|
[586] | 341 |
|
---|
[582] | 342 | Debug << "========= view cell tree options ================\n";
|
---|
| 343 | Debug << "minimum view cells: " << mMergeMinViewCells << endl;
|
---|
| 344 | Debug << "max cost ratio: " << mMergeMaxCostRatio << endl;
|
---|
| 345 | Debug << "max memory: " << mMaxMemory << endl;
|
---|
[586] | 346 | Debug << "refining view cells: " << mRefineViewCells << endl;
|
---|
[582] | 347 |
|
---|
[580] | 348 | MergeCandidate::sRenderCostWeight = mRenderCostWeight;
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 |
|
---|
[582] | 352 | // return memory usage in MB
|
---|
| 353 | float ViewCellsTree::GetMemUsage() const
|
---|
| 354 | {
|
---|
| 355 | return 0;
|
---|
| 356 | /*(sizeof(ViewCellsTree) +
|
---|
| 357 | mBspStats.Leaves() * sizeof(BspLeaf) +
|
---|
| 358 | mBspStats.Interior() * sizeof(BspInterior) +
|
---|
| 359 | mBspStats.accumRays * sizeof(RayInfo)) / (1024.0f * 1024.0f);*/
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 |
|
---|
[580] | 363 | int ViewCellsTree::GetSize(ViewCell *vc) const
|
---|
| 364 | {
|
---|
| 365 | int vcSize = 0;
|
---|
| 366 |
|
---|
| 367 | stack<ViewCell *> tstack;
|
---|
| 368 |
|
---|
| 369 | tstack.push(vc);
|
---|
| 370 |
|
---|
| 371 | while (!tstack.empty())
|
---|
| 372 | {
|
---|
| 373 | ViewCell *vc = tstack.top();
|
---|
| 374 | tstack.pop();
|
---|
| 375 |
|
---|
| 376 | if (vc->IsLeaf())
|
---|
| 377 | {
|
---|
| 378 | ++ vcSize;
|
---|
| 379 | }
|
---|
| 380 | else
|
---|
| 381 | {
|
---|
| 382 | ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(vc);
|
---|
| 383 | ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
|
---|
| 384 | for (it = interior->mChildren.begin(); it != it_end; ++ it)
|
---|
| 385 | tstack.push(*it);
|
---|
| 386 |
|
---|
| 387 | }
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | return vcSize;
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 |
|
---|
| 394 | void ViewCellsTree::CollectLeaves(ViewCell *vc, ViewCellContainer &leaves) const
|
---|
| 395 | {
|
---|
| 396 | stack<ViewCell *> tstack;
|
---|
| 397 |
|
---|
| 398 | tstack.push(vc);
|
---|
| 399 |
|
---|
| 400 | while (!tstack.empty())
|
---|
| 401 | {
|
---|
| 402 | ViewCell *vc = tstack.top();
|
---|
| 403 | tstack.pop();
|
---|
| 404 |
|
---|
| 405 | if (vc->IsLeaf())
|
---|
| 406 | {
|
---|
| 407 | leaves.push_back(vc);
|
---|
| 408 | }
|
---|
| 409 | else
|
---|
| 410 | {
|
---|
| 411 | ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(vc);
|
---|
| 412 | ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
|
---|
[664] | 413 |
|
---|
[580] | 414 | for (it = interior->mChildren.begin(); it != it_end; ++ it)
|
---|
[664] | 415 | {
|
---|
[580] | 416 | tstack.push(*it);
|
---|
[664] | 417 | }
|
---|
[580] | 418 |
|
---|
| 419 | }
|
---|
| 420 | }
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 |
|
---|
| 424 | ViewCellsTree::~ViewCellsTree()
|
---|
| 425 | {
|
---|
| 426 | DEL_PTR(mRoot);
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 |
|
---|
| 430 | int ViewCellsTree::ConstructMergeTree(const VssRayContainer &rays,
|
---|
| 431 | const ObjectContainer &objects)
|
---|
| 432 | {
|
---|
[582] | 433 | mNumActiveViewCells = (int)mViewCellsManager->GetViewCells().size();
|
---|
[580] | 434 |
|
---|
| 435 | float variance = 0;
|
---|
| 436 | int totalPvs = 0;
|
---|
[600] | 437 | float totalRenderCost = 0;
|
---|
[580] | 438 |
|
---|
| 439 | //-- compute statistics values of initial view cells
|
---|
[600] | 440 | mViewCellsManager->EvaluateRenderStatistics(totalRenderCost,
|
---|
[580] | 441 | mExpectedCost,
|
---|
| 442 | mDeviation,
|
---|
| 443 | variance,
|
---|
| 444 | totalPvs,
|
---|
| 445 | mAvgRenderCost);
|
---|
| 446 |
|
---|
| 447 |
|
---|
| 448 | //-- fill merge queue
|
---|
| 449 | vector<MergeCandidate> candidates;
|
---|
| 450 |
|
---|
| 451 | mViewCellsManager->CollectMergeCandidates(rays, candidates);
|
---|
| 452 | while(!candidates.empty())
|
---|
| 453 | {
|
---|
| 454 | MergeCandidate mc = candidates.back();
|
---|
| 455 | candidates.pop_back();
|
---|
| 456 | EvalMergeCost(mc);
|
---|
| 457 | mMergeQueue.push(mc);
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | Debug << "************************* merge ***********************************" << endl;
|
---|
| 461 | Debug << "deviation: " << mDeviation << endl;
|
---|
| 462 | Debug << "avg render cost: " << mAvgRenderCost << endl;
|
---|
[600] | 463 | Debug << "expected cost: " << mExpectedCost << endl;
|
---|
[580] | 464 |
|
---|
| 465 |
|
---|
| 466 | ViewCellsManager::PvsStatistics pvsStats;
|
---|
| 467 | mViewCellsManager->GetPvsStatistics(pvsStats);
|
---|
| 468 |
|
---|
[605] | 469 | //static float expectedValue = pvsStats.avgPvs;
|
---|
[580] | 470 |
|
---|
| 471 | // the current view cells are kept in this container
|
---|
| 472 | // we start with the current view cells from the
|
---|
| 473 | // view cell manager. They will change with
|
---|
| 474 | // subsequent merges
|
---|
[582] | 475 | ViewCellContainer &activeViewCells = mViewCellsManager->GetViewCells();
|
---|
[580] | 476 |
|
---|
| 477 |
|
---|
| 478 | ViewCell::NewMail();
|
---|
| 479 |
|
---|
| 480 | MergeStatistics mergeStats;
|
---|
| 481 | mergeStats.Start();
|
---|
| 482 |
|
---|
| 483 | long startTime = GetTime();
|
---|
| 484 |
|
---|
| 485 | mergeStats.collectTime = TimeDiff(startTime, GetTime());
|
---|
| 486 | mergeStats.candidates = (int)mMergeQueue.size();
|
---|
| 487 | startTime = GetTime();
|
---|
| 488 |
|
---|
| 489 | // frequency stats are updated
|
---|
[650] | 490 | const int statsOut = 500;
|
---|
[580] | 491 |
|
---|
| 492 | // passes are needed for statistics, because we don't want to record
|
---|
| 493 | // every merge
|
---|
| 494 | int pass = 0;
|
---|
| 495 | int mergedPerPass = 0;
|
---|
| 496 | float realExpectedCost = mExpectedCost;
|
---|
| 497 | float realAvgRenderCost = mAvgRenderCost;
|
---|
[582] | 498 | int realNumActiveViewCells = mNumActiveViewCells;
|
---|
[580] | 499 |
|
---|
| 500 | // maximal ratio of old expected render cost to expected render
|
---|
| 501 | // when the the render queue has to be reset.
|
---|
| 502 | float avgCostMaxDeviation;
|
---|
| 503 | int maxMergesPerPass;
|
---|
[582] | 504 | int numMergedViewCells = 0;
|
---|
[580] | 505 |
|
---|
| 506 | environment->GetIntValue("ViewCells.PostProcess.maxMergesPerPass", maxMergesPerPass);
|
---|
| 507 | environment->GetFloatValue("ViewCells.PostProcess.avgCostMaxDeviation", avgCostMaxDeviation);
|
---|
| 508 |
|
---|
| 509 | cout << "actual merge starts now ... " << endl;
|
---|
[604] | 510 |
|
---|
[580] | 511 | //-- use priority queue to merge leaf pairs
|
---|
[612] | 512 |
|
---|
[600] | 513 | //const float maxAvgCost = 350;
|
---|
| 514 | while (!mMergeQueue.empty())//NumActiveViewCells > mMergeMinViewCells))
|
---|
[580] | 515 | {
|
---|
| 516 | //-- reset merge queue if the ratio of current expected cost / real expected cost
|
---|
| 517 | // too small or after a given number of merges
|
---|
| 518 | if ((mergedPerPass > maxMergesPerPass) ||
|
---|
| 519 | (avgCostMaxDeviation > mAvgRenderCost / realAvgRenderCost))
|
---|
| 520 | {
|
---|
| 521 | Debug << "************ reset queue *****************\n"
|
---|
| 522 | << "ratios: " << avgCostMaxDeviation
|
---|
| 523 | << " real avg render cost " << realAvgRenderCost << " average render cost " << mAvgRenderCost
|
---|
| 524 | << " merged per pass : " << mergedPerPass << " of maximal " << maxMergesPerPass << endl;
|
---|
| 525 |
|
---|
| 526 | Debug << "Values before reset: "
|
---|
| 527 | << " erc: " << mExpectedCost
|
---|
| 528 | << " avgrc: " << mAvgRenderCost
|
---|
| 529 | << " dev: " << mDeviation << endl;
|
---|
| 530 |
|
---|
| 531 | // adjust render cost
|
---|
| 532 | ++ pass;
|
---|
| 533 |
|
---|
| 534 | mergedPerPass = 0;
|
---|
| 535 | mExpectedCost = realExpectedCost;
|
---|
| 536 | mAvgRenderCost = realAvgRenderCost;
|
---|
[582] | 537 | mNumActiveViewCells = realNumActiveViewCells;
|
---|
[580] | 538 |
|
---|
[582] | 539 | const int numMergedViewCells = UpdateActiveViewCells(activeViewCells);
|
---|
| 540 |
|
---|
[586] | 541 | // refines the view cells
|
---|
| 542 | // then priorities are recomputed
|
---|
| 543 | // and the candidates are put back into merge queue
|
---|
| 544 | if (mRefineViewCells)
|
---|
| 545 | RefineViewCells(rays, objects);
|
---|
| 546 | else
|
---|
| 547 | ResetMergeQueue();
|
---|
[580] | 548 |
|
---|
| 549 | Debug << "Values after reset: "
|
---|
| 550 | << " erc: " << mExpectedCost
|
---|
| 551 | << " avg: " << mAvgRenderCost
|
---|
| 552 | << " dev: " << mDeviation << endl;
|
---|
| 553 |
|
---|
| 554 | if (mExportMergedViewCells)
|
---|
| 555 | {
|
---|
[582] | 556 | ExportMergedViewCells(activeViewCells, objects, numMergedViewCells);
|
---|
[580] | 557 | }
|
---|
| 558 | }
|
---|
| 559 |
|
---|
| 560 |
|
---|
| 561 | MergeCandidate mc = mMergeQueue.top();
|
---|
| 562 | mMergeQueue.pop();
|
---|
| 563 |
|
---|
[582] | 564 | // both view cells equal
|
---|
| 565 | // NOTE: do I really still need this? probably cannot happen!!
|
---|
[580] | 566 | if (mc.mLeftViewCell == mc.mRightViewCell)
|
---|
| 567 | continue;
|
---|
| 568 |
|
---|
| 569 | if (mc.IsValid())
|
---|
| 570 | {
|
---|
| 571 | ViewCell::NewMail();
|
---|
[600] | 572 |
|
---|
| 573 | //-- update statistical values
|
---|
[582] | 574 | -- realNumActiveViewCells;
|
---|
[580] | 575 | ++ mergeStats.merged;
|
---|
| 576 | ++ mergedPerPass;
|
---|
| 577 |
|
---|
[600] | 578 | const float renderCostIncr = mc.GetRenderCost();
|
---|
| 579 | const float mergeCostIncr = mc.GetMergeCost();
|
---|
[580] | 580 |
|
---|
[600] | 581 | totalRenderCost += renderCostIncr;
|
---|
[580] | 582 | mDeviation += mc.GetDeviationIncr();
|
---|
| 583 |
|
---|
[600] | 584 |
|
---|
[580] | 585 | // merge the view cells of leaf1 and leaf2
|
---|
| 586 | int pvsDiff;
|
---|
| 587 | ViewCellInterior *mergedVc =
|
---|
| 588 | MergeViewCells(mc.mLeftViewCell, mc.mRightViewCell, pvsDiff);
|
---|
| 589 |
|
---|
[600] | 590 |
|
---|
| 591 | // total render cost and deviation has changed
|
---|
| 592 | // real expected cost will be larger than expected cost used for the
|
---|
| 593 | // cost heuristics, but cannot recompute costs on each increase of the
|
---|
| 594 | // expected cost
|
---|
[580] | 595 | totalPvs += pvsDiff;
|
---|
[600] | 596 | realExpectedCost = totalRenderCost / (float)realNumActiveViewCells;
|
---|
| 597 | realAvgRenderCost = (float)totalPvs / (float)realNumActiveViewCells;
|
---|
| 598 |
|
---|
| 599 | // set merge cost to this node
|
---|
| 600 | mergedVc->SetMergeCost(totalRenderCost);
|
---|
[582] | 601 |
|
---|
[612] | 602 | //if (mViewCellsManager->EqualToSpatialNode(mergedVc))
|
---|
| 603 | // ++ mergeStats.siblings;
|
---|
[608] | 604 | mergedVc->SetCost(realExpectedCost);
|
---|
[607] | 605 |
|
---|
[650] | 606 | if ((mergeStats.merged % statsOut) == 0)
|
---|
[580] | 607 | cout << "merged " << mergeStats.merged << " view cells" << endl;
|
---|
| 608 |
|
---|
| 609 | }
|
---|
| 610 | else
|
---|
| 611 | {
|
---|
| 612 | // merge candidate not valid, because one of the leaves was already
|
---|
| 613 | // merged with another one => validate and reinsert into queue
|
---|
[582] | 614 | if (ValidateMergeCandidate(mc))
|
---|
| 615 | {
|
---|
| 616 | EvalMergeCost(mc);
|
---|
| 617 | mMergeQueue.push(mc);
|
---|
| 618 | }
|
---|
[580] | 619 | }
|
---|
[612] | 620 |
|
---|
[580] | 621 | }
|
---|
| 622 |
|
---|
| 623 | // adjust stats and reset queue one final time
|
---|
| 624 | mExpectedCost = realExpectedCost;
|
---|
| 625 | mAvgRenderCost = realAvgRenderCost;
|
---|
[582] | 626 | mNumActiveViewCells = realNumActiveViewCells;
|
---|
[580] | 627 |
|
---|
[582] | 628 | UpdateActiveViewCells(activeViewCells);
|
---|
[580] | 629 |
|
---|
[586] | 630 | // refine view cells and reset costs
|
---|
| 631 | if (mRefineViewCells)
|
---|
| 632 | RefineViewCells(rays, objects);
|
---|
| 633 | else
|
---|
| 634 | ResetMergeQueue();
|
---|
| 635 |
|
---|
[580] | 636 | // create a root node if the merge was not done till root level,
|
---|
| 637 | // else take the single node as new root
|
---|
[582] | 638 | if ((int)activeViewCells.size() > 1)
|
---|
[580] | 639 | {
|
---|
[587] | 640 | Debug << "creating root of view cell hierarchy for "
|
---|
| 641 | << (int)activeViewCells.size() << " view cells" << endl;
|
---|
[612] | 642 |
|
---|
[582] | 643 | ViewCellInterior *root = mViewCellsManager->MergeViewCells(activeViewCells);
|
---|
[600] | 644 | root->SetMergeCost(totalRenderCost);
|
---|
[608] | 645 | // $$JB keep this 0 temporarilly
|
---|
| 646 | root->SetCost(0.0f);
|
---|
[612] | 647 |
|
---|
[580] | 648 | mRoot = root;
|
---|
| 649 | }
|
---|
[582] | 650 | else if ((int)activeViewCells.size() == 1)
|
---|
[580] | 651 | {
|
---|
[582] | 652 | Debug << "setting root of the merge history" << endl;
|
---|
| 653 | mRoot = activeViewCells[0];
|
---|
[580] | 654 | }
|
---|
| 655 |
|
---|
[644] | 656 | //-- empty merge queue just in case
|
---|
[600] | 657 | while (!mMergeQueue.empty())
|
---|
| 658 | {
|
---|
| 659 | mMergeQueue.pop();
|
---|
| 660 | }
|
---|
| 661 |
|
---|
| 662 |
|
---|
[581] | 663 | // TODO delete because makes no sense here
|
---|
[580] | 664 | mergeStats.expectedRenderCost = realExpectedCost;
|
---|
| 665 | mergeStats.deviation = mDeviation;
|
---|
| 666 |
|
---|
| 667 | // we want to optimize this heuristics
|
---|
| 668 | mergeStats.heuristics =
|
---|
| 669 | mDeviation * (1.0f - mRenderCostWeight) +
|
---|
| 670 | mExpectedCost * mRenderCostWeight;
|
---|
| 671 |
|
---|
| 672 | mergeStats.mergeTime = TimeDiff(startTime, GetTime());
|
---|
| 673 | mergeStats.Stop();
|
---|
| 674 | Debug << mergeStats << endl << endl;
|
---|
| 675 |
|
---|
[608] | 676 | // assign colors for the view cells so that at least one is always consistent
|
---|
| 677 | AssignRandomColors();
|
---|
[580] | 678 | //TODO: should return sample contributions?
|
---|
| 679 | return mergeStats.merged;
|
---|
| 680 | }
|
---|
| 681 |
|
---|
| 682 |
|
---|
[584] | 683 | ViewCell *ViewCellsTree::GetRoot() const
|
---|
[581] | 684 | {
|
---|
| 685 | return mRoot;
|
---|
| 686 | }
|
---|
| 687 |
|
---|
| 688 |
|
---|
[580] | 689 | void ViewCellsTree::ResetMergeQueue()
|
---|
| 690 | {
|
---|
| 691 | cout << "reset merge queue ... ";
|
---|
| 692 |
|
---|
| 693 | vector<MergeCandidate> buf;
|
---|
| 694 | buf.reserve(mMergeQueue.size());
|
---|
| 695 |
|
---|
| 696 |
|
---|
| 697 | // store merge candidates in intermediate buffer
|
---|
| 698 | while (!mMergeQueue.empty())
|
---|
| 699 | {
|
---|
| 700 | MergeCandidate mc = mMergeQueue.top();
|
---|
| 701 | mMergeQueue.pop();
|
---|
| 702 |
|
---|
| 703 | // recalculate cost
|
---|
[582] | 704 | if (ValidateMergeCandidate(mc))
|
---|
| 705 | {
|
---|
| 706 | EvalMergeCost(mc);
|
---|
| 707 | buf.push_back(mc);
|
---|
| 708 | }
|
---|
[580] | 709 | }
|
---|
| 710 |
|
---|
| 711 | vector<MergeCandidate>::const_iterator bit, bit_end = buf.end();
|
---|
| 712 |
|
---|
| 713 | // reinsert back into queue
|
---|
| 714 | for (bit = buf.begin(); bit != bit_end; ++ bit)
|
---|
| 715 | {
|
---|
| 716 | mMergeQueue.push(*bit);
|
---|
| 717 | }
|
---|
| 718 |
|
---|
| 719 | cout << "finished" << endl;
|
---|
| 720 | }
|
---|
| 721 |
|
---|
| 722 |
|
---|
[582] | 723 | int ViewCellsTree::UpdateActiveViewCells(ViewCellContainer &viewCells)
|
---|
[580] | 724 | {
|
---|
[582] | 725 | int numMergedViewCells = 0;
|
---|
[580] | 726 |
|
---|
[584] | 727 | Debug << "updating active vc: " << (int)viewCells.size() << endl;
|
---|
[580] | 728 | // find all already merged view cells and remove them from view cells
|
---|
[582] | 729 |
|
---|
| 730 | // sort out all view cells which are not active anymore, i.e., they
|
---|
| 731 | // were already part of a merge
|
---|
[580] | 732 | int i = 0;
|
---|
| 733 |
|
---|
[582] | 734 | ViewCell::NewMail();
|
---|
| 735 |
|
---|
[580] | 736 | while (1)
|
---|
| 737 | {
|
---|
[582] | 738 | // remove all merged view cells from end of the vector
|
---|
| 739 | while (!viewCells.empty() && (viewCells.back()->GetParent()))
|
---|
[580] | 740 | {
|
---|
| 741 | viewCells.pop_back();
|
---|
| 742 | }
|
---|
| 743 |
|
---|
| 744 | // all merged view cells have been found
|
---|
| 745 | if (i >= viewCells.size())
|
---|
| 746 | break;
|
---|
| 747 |
|
---|
| 748 | // already merged view cell, put it to end of vector
|
---|
[582] | 749 | if (viewCells[i]->GetParent())
|
---|
[580] | 750 | swap(viewCells[i], viewCells.back());
|
---|
| 751 |
|
---|
[582] | 752 | viewCells[i ++]->Mail();
|
---|
[580] | 753 | }
|
---|
| 754 |
|
---|
[582] | 755 |
|
---|
[580] | 756 | // add new view cells to container only if they don't have been
|
---|
| 757 | // merged in the mean time
|
---|
[582] | 758 | ViewCellContainer::const_iterator ait, ait_end = mMergedViewCells.end();
|
---|
| 759 | for (ait = mMergedViewCells.begin(); ait != ait_end; ++ ait)
|
---|
[580] | 760 | {
|
---|
[582] | 761 | ViewCell *vc = mMergedViewCells.back();
|
---|
| 762 | if (!vc->GetParent() && !vc->Mailed())
|
---|
[580] | 763 | {
|
---|
[582] | 764 | vc->Mail();
|
---|
| 765 | viewCells.push_back(vc);
|
---|
| 766 | ++ numMergedViewCells;
|
---|
[580] | 767 | }
|
---|
| 768 | }
|
---|
| 769 |
|
---|
[582] | 770 | mMergedViewCells.clear();
|
---|
| 771 |
|
---|
[580] | 772 | // update standard deviation
|
---|
| 773 | ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
|
---|
| 774 |
|
---|
| 775 | mDeviation = 0;
|
---|
| 776 |
|
---|
| 777 | for (vit = viewCells.begin(); vit != vit_end; ++ vit)
|
---|
| 778 | {
|
---|
| 779 | int lower = mViewCellsManager->GetMinPvsSize();
|
---|
| 780 | int upper = mViewCellsManager->GetMaxPvsSize();
|
---|
| 781 | float penalty = EvalPvsPenalty((*vit)->GetPvs().GetSize(), lower, upper);
|
---|
| 782 |
|
---|
| 783 | mDeviation += fabs(mAvgRenderCost - penalty);
|
---|
| 784 | }
|
---|
| 785 |
|
---|
| 786 | mDeviation /= (float)viewCells.size();
|
---|
[582] | 787 |
|
---|
| 788 | return numMergedViewCells;
|
---|
[580] | 789 | }
|
---|
| 790 |
|
---|
| 791 |
|
---|
| 792 | void ViewCellsTree::ExportMergedViewCells(ViewCellContainer &viewCells,
|
---|
| 793 | const ObjectContainer &objects,
|
---|
[582] | 794 | const int numMergedViewCells)
|
---|
[580] | 795 | {
|
---|
| 796 |
|
---|
| 797 |
|
---|
| 798 | char s[64];
|
---|
| 799 |
|
---|
| 800 | sprintf(s, "merged_viewcells%07d.x3d", (int)viewCells.size());
|
---|
| 801 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
| 802 |
|
---|
| 803 | if (exporter)
|
---|
| 804 | {
|
---|
| 805 | cout << "exporting " << (int)viewCells.size() << " merged view cells ... ";
|
---|
| 806 | exporter->ExportGeometry(objects);
|
---|
| 807 | //Debug << "vc size " << (int)viewCells.size() << " merge queue size: " << (int)mMergeQueue.size() << endl;
|
---|
| 808 | ViewCellContainer::const_iterator it, it_end = viewCells.end();
|
---|
| 809 |
|
---|
| 810 | int i = 0;
|
---|
| 811 | for (it = viewCells.begin(); it != it_end; ++ it)
|
---|
| 812 | {
|
---|
| 813 | Material m;
|
---|
| 814 | // assign special material to new view cells
|
---|
| 815 | // new view cells are on the back of container
|
---|
[582] | 816 | if (i ++ >= (viewCells.size() - numMergedViewCells))
|
---|
[580] | 817 | {
|
---|
| 818 | //m = RandomMaterial();
|
---|
| 819 | m.mDiffuseColor.r = RandomValue(0.5f, 1.0f);
|
---|
| 820 | m.mDiffuseColor.g = RandomValue(0.5f, 1.0f);
|
---|
| 821 | m.mDiffuseColor.b = RandomValue(0.5f, 1.0f);
|
---|
| 822 | }
|
---|
| 823 | else
|
---|
| 824 | {
|
---|
| 825 | float col = RandomValue(0.1f, 0.4f);
|
---|
| 826 | m.mDiffuseColor.r = col;
|
---|
| 827 | m.mDiffuseColor.g = col;
|
---|
| 828 | m.mDiffuseColor.b = col;
|
---|
| 829 | }
|
---|
| 830 |
|
---|
| 831 | exporter->SetForcedMaterial(m);
|
---|
| 832 | mViewCellsManager->ExportViewCellGeometry(exporter, *it);
|
---|
| 833 | }
|
---|
| 834 | delete exporter;
|
---|
| 835 | cout << "finished" << endl;
|
---|
| 836 | }
|
---|
| 837 | }
|
---|
| 838 |
|
---|
| 839 |
|
---|
| 840 | // TODO: should be done in view cells manager
|
---|
[586] | 841 | ViewCellInterior *ViewCellsTree::MergeViewCells(ViewCell *l,
|
---|
| 842 | ViewCell *r,
|
---|
| 843 | int &pvsDiff) //const
|
---|
[580] | 844 | {
|
---|
[582] | 845 | ViewCellInterior *vc = mViewCellsManager->MergeViewCells(l, r);
|
---|
[580] | 846 |
|
---|
| 847 | // if merge was unsuccessful
|
---|
| 848 | if (!vc) return NULL;
|
---|
| 849 |
|
---|
| 850 | // set new size of view cell
|
---|
| 851 | if (mUseAreaForPvs)
|
---|
| 852 | vc->SetArea(l->GetArea() + l->GetArea());
|
---|
| 853 | else
|
---|
[602] | 854 | {
|
---|
| 855 | vc->SetVolume(r->GetVolume() + l->GetVolume());
|
---|
| 856 | }
|
---|
[580] | 857 | // important so other merge candidates sharing this view cell
|
---|
| 858 | // are notified that the merge cost must be updated!!
|
---|
| 859 | vc->Mail();
|
---|
| 860 |
|
---|
| 861 | const int pvs1 = l->GetPvs().GetSize();
|
---|
| 862 | const int pvs2 = r->GetPvs().GetSize();
|
---|
| 863 |
|
---|
| 864 |
|
---|
[582] | 865 | // new view cells are stored in this vector
|
---|
| 866 | mMergedViewCells.push_back(vc);
|
---|
[580] | 867 |
|
---|
| 868 | pvsDiff = vc->GetPvs().GetSize() - pvs1 - pvs2;
|
---|
| 869 |
|
---|
| 870 | return vc;
|
---|
| 871 | }
|
---|
| 872 |
|
---|
| 873 |
|
---|
| 874 |
|
---|
[586] | 875 | int ViewCellsTree::RefineViewCells(const VssRayContainer &rays,
|
---|
| 876 | const ObjectContainer &objects)
|
---|
[580] | 877 | {
|
---|
| 878 | Debug << "refining " << (int)mMergeQueue.size() << " candidates " << endl;
|
---|
[586] | 879 |
|
---|
| 880 | // intermediate buffer for shuffled view cells
|
---|
| 881 | vector<MergeCandidate> buf;
|
---|
| 882 | buf.reserve(mMergeQueue.size());
|
---|
| 883 |
|
---|
[580] | 884 | // Use priority queue of remaining leaf pairs
|
---|
| 885 | // The candidates either share the same view cells or
|
---|
| 886 | // are border leaves which share a boundary.
|
---|
| 887 | // We test if they can be shuffled, i.e.,
|
---|
| 888 | // either one leaf is made part of one view cell or the other
|
---|
| 889 | // leaf is made part of the other view cell. It is tested if the
|
---|
| 890 | // remaining view cells are "better" than the old ones.
|
---|
| 891 |
|
---|
[586] | 892 | const int numPasses = 3;
|
---|
[580] | 893 | int pass = 0;
|
---|
| 894 | int passShuffled = 0;
|
---|
| 895 | int shuffled = 0;
|
---|
| 896 | int shuffledViewCells = 0;
|
---|
| 897 |
|
---|
| 898 | ViewCell::NewMail();
|
---|
| 899 |
|
---|
[586] | 900 | while (!mMergeQueue.empty())
|
---|
[580] | 901 | {
|
---|
[586] | 902 | MergeCandidate mc = mMergeQueue.top();
|
---|
| 903 | mMergeQueue.pop();
|
---|
[580] | 904 |
|
---|
[586] | 905 | // both view cells equal or already shuffled
|
---|
| 906 | if ((mc.GetLeftViewCell() == mc.GetRightViewCell()) ||
|
---|
| 907 | mc.GetLeftViewCell()->IsLeaf() || mc.GetRightViewCell()->IsLeaf())
|
---|
| 908 | {
|
---|
| 909 | continue;
|
---|
| 910 | }
|
---|
[580] | 911 |
|
---|
[586] | 912 | // candidate for shuffling
|
---|
| 913 | const bool wasShuffled = ShuffleLeaves(mc);
|
---|
[580] | 914 |
|
---|
[586] | 915 | // shuffled or put into other queue for further refine
|
---|
| 916 | if (wasShuffled)
|
---|
| 917 | {
|
---|
| 918 | ++ passShuffled;
|
---|
| 919 |
|
---|
| 920 | if (!mc.GetLeftViewCell()->Mailed())
|
---|
[580] | 921 | {
|
---|
[586] | 922 | mc.GetLeftViewCell()->Mail();
|
---|
| 923 | ++ shuffledViewCells;
|
---|
[580] | 924 | }
|
---|
[586] | 925 | if (!mc.GetRightViewCell()->Mailed())
|
---|
[580] | 926 | {
|
---|
[586] | 927 | mc.GetRightViewCell()->Mail();
|
---|
| 928 | ++ shuffledViewCells;
|
---|
[580] | 929 | }
|
---|
| 930 | }
|
---|
| 931 |
|
---|
[586] | 932 | // put back into intermediate vector
|
---|
| 933 | buf.push_back(mc);
|
---|
[580] | 934 | }
|
---|
| 935 |
|
---|
[586] | 936 |
|
---|
| 937 | //-- in the end, the candidates must be in the mergequeue again
|
---|
| 938 | // with the correct cost
|
---|
| 939 |
|
---|
| 940 | cout << "reset merge queue ... ";
|
---|
| 941 |
|
---|
| 942 |
|
---|
| 943 | vector<MergeCandidate>::const_iterator bit, bit_end = buf.end();
|
---|
| 944 |
|
---|
| 945 | for (bit = buf.begin(); bit != bit_end; ++ bit)
|
---|
| 946 | {
|
---|
| 947 | MergeCandidate mc = *bit;
|
---|
| 948 | // recalculate cost
|
---|
| 949 | if (ValidateMergeCandidate(mc))
|
---|
| 950 | {
|
---|
| 951 | EvalMergeCost(mc);
|
---|
| 952 | mMergeQueue.push(mc);
|
---|
| 953 | }
|
---|
[580] | 954 | }
|
---|
| 955 |
|
---|
[586] | 956 | cout << "finished" << endl;
|
---|
| 957 |
|
---|
[580] | 958 | return shuffledViewCells;
|
---|
| 959 | }
|
---|
| 960 |
|
---|
| 961 |
|
---|
| 962 |
|
---|
| 963 |
|
---|
| 964 | inline int AddedPvsSize(ObjectPvs pvs1, const ObjectPvs &pvs2)
|
---|
| 965 | {
|
---|
| 966 | return pvs1.AddPvs(pvs2);
|
---|
| 967 | }
|
---|
| 968 |
|
---|
| 969 |
|
---|
| 970 | // recomputes pvs size minus pvs of leaf l
|
---|
| 971 | #if 0
|
---|
| 972 | inline int SubtractedPvsSize(BspViewCell *vc, BspLeaf *l, const ObjectPvs &pvs2)
|
---|
| 973 | {
|
---|
| 974 | ObjectPvs pvs;
|
---|
| 975 | vector<BspLeaf *>::const_iterator it, it_end = vc->mLeaves.end();
|
---|
| 976 | for (it = vc->mLeaves.begin(); it != vc->mLeaves.end(); ++ it)
|
---|
| 977 | if (*it != l)
|
---|
| 978 | pvs.AddPvs(*(*it)->mPvs);
|
---|
| 979 | return pvs.GetSize();
|
---|
| 980 | }
|
---|
| 981 | #endif
|
---|
| 982 |
|
---|
| 983 |
|
---|
| 984 | // computes pvs1 minus pvs2
|
---|
| 985 | inline int SubtractedPvsSize(ObjectPvs pvs1, const ObjectPvs &pvs2)
|
---|
| 986 | {
|
---|
| 987 | return pvs1.SubtractPvs(pvs2);
|
---|
| 988 | }
|
---|
| 989 |
|
---|
| 990 |
|
---|
| 991 | float ViewCellsTree::EvalShuffleCost(ViewCell *leaf,
|
---|
[586] | 992 | ViewCellInterior *vc1,
|
---|
| 993 | ViewCellInterior *vc2) const
|
---|
[580] | 994 | {
|
---|
| 995 | //const int pvs1 = SubtractedPvsSize(vc1, leaf, *leaf->mPvs);
|
---|
| 996 | const int pvs1 = SubtractedPvsSize(vc1->GetPvs(), leaf->GetPvs());
|
---|
| 997 | const int pvs2 = AddedPvsSize(vc2->GetPvs(), leaf->GetPvs());
|
---|
| 998 |
|
---|
| 999 | const int lowerPvsLimit = mViewCellsManager->GetMinPvsSize();
|
---|
| 1000 | const int upperPvsLimit = mViewCellsManager->GetMaxPvsSize();
|
---|
| 1001 |
|
---|
| 1002 | const float pvsPenalty1 =
|
---|
| 1003 | EvalPvsPenalty(pvs1, lowerPvsLimit, upperPvsLimit);
|
---|
| 1004 |
|
---|
| 1005 | const float pvsPenalty2 =
|
---|
| 1006 | EvalPvsPenalty(pvs2, lowerPvsLimit, upperPvsLimit);
|
---|
| 1007 |
|
---|
| 1008 |
|
---|
| 1009 | // don't shuffle leaves with pvs > max
|
---|
| 1010 | if (0 && (pvs1 + pvs2 > mViewCellsManager->GetMaxPvsSize()))
|
---|
| 1011 | {
|
---|
| 1012 | return 1e20f;
|
---|
| 1013 | }
|
---|
| 1014 |
|
---|
| 1015 | float p1, p2;
|
---|
| 1016 |
|
---|
| 1017 | if (mUseAreaForPvs)
|
---|
| 1018 | {
|
---|
| 1019 | p1 = vc1->GetArea() - leaf->GetArea();
|
---|
| 1020 | p2 = vc2->GetArea() + leaf->GetArea();
|
---|
| 1021 | }
|
---|
| 1022 | else
|
---|
| 1023 | {
|
---|
| 1024 | p1 = vc1->GetVolume() - leaf->GetVolume();
|
---|
| 1025 | p2 = vc2->GetVolume() + leaf->GetVolume();
|
---|
| 1026 | }
|
---|
| 1027 |
|
---|
| 1028 | const float renderCost1 = pvsPenalty1 * p1;
|
---|
| 1029 | const float renderCost2 = pvsPenalty2 * p2;
|
---|
| 1030 |
|
---|
| 1031 | float dev1, dev2;
|
---|
| 1032 |
|
---|
| 1033 | if (1)
|
---|
| 1034 | {
|
---|
| 1035 | dev1 = fabs(mAvgRenderCost - pvsPenalty1);
|
---|
| 1036 | dev2 = fabs(mAvgRenderCost - pvsPenalty2);
|
---|
| 1037 | }
|
---|
| 1038 | else
|
---|
| 1039 | {
|
---|
| 1040 | dev1 = fabs(mExpectedCost - renderCost1);
|
---|
| 1041 | dev2 = fabs(mExpectedCost - renderCost2);
|
---|
| 1042 | }
|
---|
| 1043 |
|
---|
| 1044 | return mRenderCostWeight * (renderCost1 + renderCost2) +
|
---|
[582] | 1045 | (1.0f - mRenderCostWeight) * (dev1 + dev2) / (float)mNumActiveViewCells;
|
---|
[580] | 1046 | }
|
---|
| 1047 |
|
---|
| 1048 |
|
---|
| 1049 | void ViewCellsTree::ShuffleLeaf(ViewCell *leaf,
|
---|
[586] | 1050 | ViewCellInterior *vc1,
|
---|
| 1051 | ViewCellInterior *vc2) const
|
---|
[580] | 1052 | {
|
---|
| 1053 | // compute new pvs and area
|
---|
[586] | 1054 | // TODO change
|
---|
[580] | 1055 | vc1->GetPvs().SubtractPvs(leaf->GetPvs());
|
---|
| 1056 | vc2->GetPvs().AddPvs(leaf->GetPvs());
|
---|
| 1057 |
|
---|
| 1058 | if (mUseAreaForPvs)
|
---|
| 1059 | {
|
---|
| 1060 | vc1->SetArea(vc1->GetArea() - leaf->GetArea());
|
---|
| 1061 | vc2->SetArea(vc2->GetArea() + leaf->GetArea());
|
---|
| 1062 | }
|
---|
| 1063 | else
|
---|
| 1064 | {
|
---|
| 1065 | vc1->SetVolume(vc1->GetVolume() - leaf->GetVolume());
|
---|
| 1066 | vc2->SetVolume(vc2->GetVolume() + leaf->GetVolume());
|
---|
| 1067 | }
|
---|
| 1068 |
|
---|
[586] | 1069 |
|
---|
| 1070 | ViewCellInterior *p = dynamic_cast<ViewCellInterior *>(leaf->GetParent());
|
---|
[580] | 1071 |
|
---|
[586] | 1072 | p->RemoveChildLink(leaf);
|
---|
| 1073 | vc2->SetupChildLink(leaf);
|
---|
[580] | 1074 | }
|
---|
| 1075 |
|
---|
| 1076 |
|
---|
[586] | 1077 | bool ViewCellsTree::ShuffleLeaves(MergeCandidate &mc) const
|
---|
[580] | 1078 | {
|
---|
| 1079 | float cost1, cost2;
|
---|
| 1080 |
|
---|
[586] | 1081 | ViewCellInterior *vc1 = dynamic_cast<ViewCellInterior *>(mc.GetLeftViewCell());
|
---|
| 1082 | ViewCellInterior *vc2 = dynamic_cast<ViewCellInterior *>(mc.GetRightViewCell());
|
---|
| 1083 |
|
---|
| 1084 | ViewCell *leaf1 = mc.GetInitialLeftViewCell();
|
---|
| 1085 | ViewCell *leaf2 = mc.GetInitialRightViewCell();
|
---|
| 1086 |
|
---|
[580] | 1087 | //-- first test if shuffling would decrease cost
|
---|
[586] | 1088 | cost1 = GetCostHeuristics(vc1);
|
---|
| 1089 | cost2 = GetCostHeuristics(vc2);
|
---|
[580] | 1090 |
|
---|
| 1091 | const float oldCost = cost1 + cost2;
|
---|
| 1092 |
|
---|
| 1093 | float shuffledCost1 = Limits::Infinity;
|
---|
| 1094 | float shuffledCost2 = Limits::Infinity;
|
---|
| 1095 |
|
---|
[586] | 1096 | if (leaf1)
|
---|
| 1097 | shuffledCost1 = EvalShuffleCost(leaf1, vc1, vc2);
|
---|
| 1098 | if (leaf2)
|
---|
| 1099 | shuffledCost2 = EvalShuffleCost(leaf2, vc2, vc1);
|
---|
[580] | 1100 |
|
---|
| 1101 | // if cost of shuffle is less than old cost => shuffle
|
---|
| 1102 | if ((oldCost <= shuffledCost1) && (oldCost <= shuffledCost2))
|
---|
| 1103 | return false;
|
---|
| 1104 |
|
---|
| 1105 | if (shuffledCost1 < shuffledCost2)
|
---|
| 1106 | {
|
---|
[586] | 1107 | if (leaf1)
|
---|
| 1108 | ShuffleLeaf(leaf1, vc1, vc2);
|
---|
| 1109 | mc.mInitialLeftViewCell = NULL;
|
---|
[580] | 1110 | }
|
---|
| 1111 | else
|
---|
| 1112 | {
|
---|
[586] | 1113 | if (leaf2)
|
---|
| 1114 | ShuffleLeaf(leaf2, vc2, vc1);
|
---|
| 1115 | mc.mInitialRightViewCell = NULL;
|
---|
[580] | 1116 | }
|
---|
[586] | 1117 |
|
---|
[580] | 1118 | return true;
|
---|
| 1119 | }
|
---|
| 1120 |
|
---|
| 1121 |
|
---|
| 1122 | float ViewCellsTree::GetVariance(ViewCell *vc) const
|
---|
| 1123 | {
|
---|
| 1124 | const int upper = mViewCellsManager->GetMaxPvsSize();
|
---|
| 1125 | const int lower = mViewCellsManager->GetMinPvsSize();
|
---|
| 1126 |
|
---|
| 1127 | if (1)
|
---|
| 1128 | {
|
---|
| 1129 | const float penalty = EvalPvsPenalty(vc->GetPvs().GetSize(), lower, upper);
|
---|
[582] | 1130 | return (mAvgRenderCost - penalty) * (mAvgRenderCost - penalty) / (float)mNumActiveViewCells;
|
---|
[580] | 1131 | }
|
---|
| 1132 |
|
---|
| 1133 | const float leafCost = GetRenderCost(vc);
|
---|
| 1134 | return (mExpectedCost - leafCost) * (mExpectedCost - leafCost);
|
---|
| 1135 | }
|
---|
| 1136 |
|
---|
| 1137 |
|
---|
| 1138 | float ViewCellsTree::GetDeviation(ViewCell *vc) const
|
---|
| 1139 | {
|
---|
| 1140 | const int upper = mViewCellsManager->GetMaxPvsSize();
|
---|
| 1141 | const int lower = mViewCellsManager->GetMinPvsSize();
|
---|
| 1142 |
|
---|
| 1143 | if (1)
|
---|
| 1144 | {
|
---|
| 1145 | const float penalty = EvalPvsPenalty(vc->GetPvs().GetSize(), lower, upper);
|
---|
[582] | 1146 | return fabs(mAvgRenderCost - penalty) / (float)mNumActiveViewCells;
|
---|
[580] | 1147 | }
|
---|
| 1148 |
|
---|
| 1149 | const float renderCost = GetRenderCost(vc);
|
---|
| 1150 | return fabs(mExpectedCost - renderCost);
|
---|
| 1151 | }
|
---|
| 1152 |
|
---|
| 1153 |
|
---|
| 1154 |
|
---|
| 1155 | float ViewCellsTree::GetRenderCost(ViewCell *vc) const
|
---|
| 1156 | {
|
---|
| 1157 | if (mUseAreaForPvs)
|
---|
| 1158 | return vc->GetPvs().GetSize() * vc->GetArea();
|
---|
| 1159 |
|
---|
| 1160 | return vc->GetPvs().GetSize() * vc->GetVolume();
|
---|
| 1161 | }
|
---|
| 1162 |
|
---|
| 1163 |
|
---|
| 1164 | float ViewCellsTree::GetCostHeuristics(ViewCell *vc) const
|
---|
| 1165 | {
|
---|
| 1166 | return GetRenderCost(vc) * mRenderCostWeight +
|
---|
| 1167 | GetDeviation(vc) * (1.0f - mRenderCostWeight);
|
---|
| 1168 | }
|
---|
| 1169 |
|
---|
| 1170 |
|
---|
[582] | 1171 | bool ViewCellsTree::ValidateMergeCandidate(MergeCandidate &mc) const
|
---|
[580] | 1172 | {
|
---|
| 1173 | while (mc.mLeftViewCell->mParent)
|
---|
| 1174 | {
|
---|
| 1175 | mc.mLeftViewCell = mc.mLeftViewCell->mParent;
|
---|
| 1176 | }
|
---|
| 1177 |
|
---|
| 1178 | while (mc.mRightViewCell->mParent)
|
---|
| 1179 | {
|
---|
| 1180 | mc.mRightViewCell = mc.mRightViewCell->mParent;
|
---|
| 1181 | }
|
---|
| 1182 |
|
---|
[582] | 1183 | return mc.mLeftViewCell != mc.mRightViewCell;
|
---|
[580] | 1184 | }
|
---|
| 1185 |
|
---|
| 1186 |
|
---|
| 1187 | void ViewCellsTree::EvalMergeCost(MergeCandidate &mc) const
|
---|
| 1188 | {
|
---|
| 1189 | //-- compute pvs difference
|
---|
| 1190 | const int newPvs =
|
---|
| 1191 | ComputeMergedPvsSize(mc.mLeftViewCell->GetPvs(),
|
---|
| 1192 | mc.mRightViewCell->GetPvs());
|
---|
| 1193 |
|
---|
| 1194 | const float newPenalty =
|
---|
| 1195 | EvalPvsPenalty(newPvs,
|
---|
| 1196 | mViewCellsManager->GetMinPvsSize(),
|
---|
| 1197 | mViewCellsManager->GetMaxPvsSize());
|
---|
| 1198 |
|
---|
| 1199 | ViewCell *vc1 = mc.mLeftViewCell;
|
---|
| 1200 | ViewCell *vc2 = mc.mRightViewCell;
|
---|
| 1201 |
|
---|
| 1202 | //-- compute ratio of old cost
|
---|
| 1203 | // (i.e., added size of left and right view cell times pvs size)
|
---|
| 1204 | // to new rendering cost (i.e, size of merged view cell times pvs size)
|
---|
| 1205 | const float oldCost = GetRenderCost(vc1) + GetRenderCost(vc2);
|
---|
| 1206 |
|
---|
| 1207 | const float newCost = mUseAreaForPvs ?
|
---|
| 1208 | (float)newPenalty * (vc1->GetArea() + vc2->GetArea()) :
|
---|
| 1209 | (float)newPenalty * (vc1->GetVolume() + vc2->GetVolume());
|
---|
| 1210 |
|
---|
| 1211 |
|
---|
| 1212 | // strong penalty if pvs size too large
|
---|
| 1213 | if (0 && (newPvs > mViewCellsManager->GetMaxPvsSize()))
|
---|
| 1214 | {
|
---|
| 1215 | mc.mRenderCost = 1e20f;
|
---|
| 1216 | }
|
---|
| 1217 | else
|
---|
| 1218 | {
|
---|
| 1219 | mc.mRenderCost = (newCost - oldCost) /
|
---|
| 1220 | mViewCellsManager->GetViewSpaceBox().GetVolume();
|
---|
| 1221 | }
|
---|
| 1222 |
|
---|
| 1223 |
|
---|
| 1224 | //-- merge cost also takes deviation into account
|
---|
| 1225 | float newDev, oldDev;
|
---|
| 1226 |
|
---|
| 1227 | if (1)
|
---|
[582] | 1228 | newDev = fabs(mAvgRenderCost - newPenalty) / (float)mNumActiveViewCells;
|
---|
[580] | 1229 | else
|
---|
[582] | 1230 | newDev = fabs(mExpectedCost - newCost) / (float)mNumActiveViewCells;
|
---|
[580] | 1231 |
|
---|
| 1232 | oldDev = GetDeviation(vc1) + GetDeviation(vc2);
|
---|
| 1233 |
|
---|
| 1234 | // compute deviation increase
|
---|
| 1235 | mc.mDeviationIncr = newDev - oldDev;
|
---|
| 1236 |
|
---|
| 1237 | //Debug << "render cost: " << mc.mRenderCost * mRenderCostWeight << endl;
|
---|
| 1238 | //Debug << "standard deviation: " << mc.mDeviationIncr * mRenderCostWeight << endl;
|
---|
| 1239 | }
|
---|
| 1240 |
|
---|
| 1241 | void ViewCellsTree::CompressViewCellsPvs()
|
---|
| 1242 | {
|
---|
[584] | 1243 | if (!mIsCompressed)
|
---|
[581] | 1244 | {
|
---|
[584] | 1245 | mIsCompressed = true;
|
---|
| 1246 | CompressViewCellsPvs(mRoot);
|
---|
| 1247 | }
|
---|
| 1248 | }
|
---|
[580] | 1249 |
|
---|
[584] | 1250 | void ViewCellsTree::CompressViewCellsPvs(ViewCell *root)
|
---|
| 1251 | {
|
---|
| 1252 | if (!root->IsLeaf())
|
---|
| 1253 | {
|
---|
| 1254 | ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(root);
|
---|
| 1255 |
|
---|
| 1256 | ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
|
---|
[585] | 1257 |
|
---|
[584] | 1258 | // compress child sets first
|
---|
| 1259 | for (it = interior->mChildren.begin(); it != it_end; ++ it)
|
---|
[581] | 1260 | {
|
---|
[584] | 1261 | CompressViewCellsPvs(*it);
|
---|
[581] | 1262 | }
|
---|
[584] | 1263 |
|
---|
[585] | 1264 | // compress root node
|
---|
[610] | 1265 | PullUpVisibility(interior);
|
---|
[581] | 1266 | }
|
---|
[580] | 1267 | }
|
---|
| 1268 |
|
---|
| 1269 |
|
---|
[660] | 1270 | void ViewCellsTree::ExportStats(const string &mergeStats)
|
---|
[650] | 1271 | {
|
---|
| 1272 | TraversalQueue tqueue;
|
---|
| 1273 |
|
---|
| 1274 | tqueue.push(mRoot);
|
---|
| 1275 | int numViewCells = 1;
|
---|
| 1276 |
|
---|
| 1277 | const AxisAlignedBox3 box = mViewCellsManager->GetViewSpaceBox();
|
---|
| 1278 | const float vol = box.GetVolume();
|
---|
| 1279 |
|
---|
| 1280 | int totalPvs;
|
---|
[651] | 1281 | float totalRenderCost, avgRenderCost, expectedCost;
|
---|
[650] | 1282 |
|
---|
| 1283 | float deviation = 0;
|
---|
| 1284 | totalPvs = (int)mRoot->GetPvs().GetSize();
|
---|
| 1285 | totalRenderCost = avgRenderCost = expectedCost = (float)mRoot->GetPvs().GetSize();
|
---|
| 1286 |
|
---|
| 1287 | ofstream stats;
|
---|
[660] | 1288 | stats.open(mergeStats.c_str());
|
---|
[650] | 1289 |
|
---|
| 1290 | stats
|
---|
| 1291 | << "#Pass\n" << 0 << endl
|
---|
| 1292 | //<< "#Merged\n" << mergeStats.merged << endl
|
---|
| 1293 | << "#ViewCells\n" << numViewCells << endl
|
---|
| 1294 | << "#RenderCostDecrease\n" << 0 << endl // TODO
|
---|
| 1295 | << "#TotalRenderCost\n" << totalRenderCost << endl
|
---|
| 1296 | << "#CurrentPvs\n" << mRoot->GetPvs().GetSize() << endl
|
---|
| 1297 | << "#ExpectedCost\n" << expectedCost << endl
|
---|
| 1298 | << "#AvgRenderCost\n" << avgRenderCost << endl
|
---|
| 1299 | << "#Deviation\n" << deviation << endl
|
---|
| 1300 | << "#TotalPvs\n" << totalPvs << endl
|
---|
| 1301 | << "#PvsSizeDecrease\n" << 0 << endl // TODO
|
---|
| 1302 | << "#Volume\n" << mRoot->GetVolume() << endl
|
---|
| 1303 | << endl;
|
---|
| 1304 |
|
---|
| 1305 |
|
---|
| 1306 | while (!tqueue.empty())
|
---|
| 1307 | {
|
---|
| 1308 | ViewCell *vc = tqueue.top();
|
---|
| 1309 | tqueue.pop();
|
---|
| 1310 |
|
---|
| 1311 | if (!vc->IsLeaf())
|
---|
| 1312 | {
|
---|
| 1313 | ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(vc);
|
---|
| 1314 |
|
---|
| 1315 | const int parentPvs = interior->GetPvs().GetSize();
|
---|
| 1316 | const float parentCost = (float)parentPvs * interior->GetVolume();
|
---|
| 1317 | float childCost = 0;
|
---|
| 1318 | int childPvs = 0;
|
---|
| 1319 |
|
---|
| 1320 | -- numViewCells;
|
---|
| 1321 |
|
---|
| 1322 | ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
|
---|
| 1323 |
|
---|
| 1324 | for (it = interior->mChildren.begin(); it != it_end; ++ it)
|
---|
| 1325 | {
|
---|
| 1326 | childCost += (float)(*it)->GetPvs().GetSize() * (*it)->GetVolume();
|
---|
| 1327 | childPvs += (*it)->GetPvs().GetSize();
|
---|
| 1328 |
|
---|
| 1329 | tqueue.push(*it);
|
---|
| 1330 | ++ numViewCells;
|
---|
| 1331 | }
|
---|
| 1332 |
|
---|
| 1333 |
|
---|
| 1334 | const float costDecr = (parentCost - childCost) / vol;
|
---|
| 1335 |
|
---|
| 1336 | totalRenderCost -= costDecr;
|
---|
| 1337 | totalPvs += childPvs - parentPvs;
|
---|
| 1338 |
|
---|
| 1339 | expectedCost = totalRenderCost / (float)numViewCells;
|
---|
| 1340 | avgRenderCost = (float)totalPvs / (float)numViewCells;
|
---|
| 1341 |
|
---|
| 1342 | stats
|
---|
| 1343 | << "#Pass\n" << 0 << endl
|
---|
| 1344 | //<< "#Merged\n" << mergeStats.merged << endl
|
---|
| 1345 | << "#ViewCells\n" << numViewCells << endl
|
---|
| 1346 | << "#RenderCostDecrease\n" << costDecr << endl // TODO
|
---|
| 1347 | << "#TotalRenderCost\n" << totalRenderCost << endl
|
---|
| 1348 | << "#CurrentPvs\n" << vc->GetPvs().GetSize() << endl
|
---|
| 1349 | << "#ExpectedCost\n" << expectedCost << endl
|
---|
| 1350 | << "#AvgRenderCost\n" << avgRenderCost << endl
|
---|
| 1351 | << "#Deviation\n" << deviation << endl
|
---|
| 1352 | << "#TotalPvs\n" << totalPvs << endl
|
---|
| 1353 | << "#PvsSizeDecrease\n" << childPvs - parentPvs << endl // TODO
|
---|
| 1354 | << "#Volume\n" << vc->GetVolume() << endl
|
---|
| 1355 | << endl;
|
---|
| 1356 |
|
---|
| 1357 | }
|
---|
| 1358 | }
|
---|
| 1359 |
|
---|
| 1360 | stats.close();
|
---|
| 1361 | }
|
---|
| 1362 |
|
---|
| 1363 |
|
---|
[651] | 1364 | #if 0
|
---|
| 1365 | float ViewCellsTree::ComputeVolume(ViewCell *vc)
|
---|
[650] | 1366 | {
|
---|
| 1367 | if (vc->IsLeaf())
|
---|
| 1368 | {
|
---|
| 1369 | return vc->GetVolume();
|
---|
| 1370 | }
|
---|
| 1371 | else
|
---|
| 1372 | {
|
---|
| 1373 | ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(vc);
|
---|
| 1374 |
|
---|
| 1375 | ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
|
---|
| 1376 |
|
---|
| 1377 | float volume = 0;
|
---|
| 1378 |
|
---|
| 1379 | for (it = interior->mChildren.begin(); it != it_end; ++ it)
|
---|
| 1380 | {
|
---|
| 1381 | volume += ComputeVolume(*it);
|
---|
| 1382 | }
|
---|
| 1383 |
|
---|
| 1384 | interior->SetVolume(volume);
|
---|
| 1385 | return volume;
|
---|
| 1386 | }
|
---|
[651] | 1387 | }
|
---|
| 1388 | #endif
|
---|
[650] | 1389 |
|
---|
| 1390 | void ViewCellsTree::SetRoot(ViewCell *root)
|
---|
| 1391 | {
|
---|
| 1392 | mRoot = root;
|
---|
| 1393 | }
|
---|
| 1394 |
|
---|
[660] | 1395 |
|
---|
| 1396 |
|
---|
[581] | 1397 | void ViewCellsTree::CollectBestViewCellSet(ViewCellContainer &viewCells,
|
---|
| 1398 | const int numViewCells)
|
---|
[580] | 1399 | {
|
---|
| 1400 | TraversalQueue tqueue;
|
---|
[581] | 1401 | tqueue.push(mRoot);
|
---|
[582] | 1402 |
|
---|
[580] | 1403 | while (!tqueue.empty())
|
---|
| 1404 | {
|
---|
| 1405 | ViewCell *vc = tqueue.top();
|
---|
[650] | 1406 | tqueue.pop();
|
---|
| 1407 |
|
---|
[582] | 1408 | // save the view cells if it is a leaf or if enough view cells have already been traversed
|
---|
| 1409 | // because of the priority queue, this will be the optimal set of v
|
---|
[650] | 1410 | if (vc->IsLeaf() || ((viewCells.size() + tqueue.size() + 1) >= numViewCells))
|
---|
[581] | 1411 | {
|
---|
| 1412 | viewCells.push_back(vc);
|
---|
| 1413 | }
|
---|
[582] | 1414 | else
|
---|
| 1415 | {
|
---|
[581] | 1416 | ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(vc);
|
---|
[580] | 1417 |
|
---|
[581] | 1418 | ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
|
---|
| 1419 |
|
---|
| 1420 | for (it = interior->mChildren.begin(); it != it_end; ++ it)
|
---|
| 1421 | {
|
---|
| 1422 | tqueue.push(*it);
|
---|
| 1423 | }
|
---|
| 1424 | }
|
---|
[580] | 1425 | }
|
---|
| 1426 | }
|
---|
[581] | 1427 |
|
---|
[580] | 1428 |
|
---|
[610] | 1429 | void ViewCellsTree::PullUpVisibility(ViewCellInterior *interior)
|
---|
[581] | 1430 | {
|
---|
[584] | 1431 | Intersectable::NewMail((int)interior->mChildren.size());
|
---|
[580] | 1432 |
|
---|
[581] | 1433 | ViewCellContainer::const_iterator cit, cit_end = interior->mChildren.end();
|
---|
| 1434 |
|
---|
| 1435 | ObjectPvsMap::const_iterator oit;
|
---|
| 1436 |
|
---|
| 1437 | // mail all objects in the leaf sets
|
---|
[584] | 1438 | // we are interested in the objects which are present in all leaves
|
---|
| 1439 | // => count how often an object is part of a child set
|
---|
[581] | 1440 | for (cit = interior->mChildren.begin(); cit != cit_end; ++ cit)
|
---|
| 1441 | {
|
---|
| 1442 | ViewCell *vc = *cit;
|
---|
| 1443 |
|
---|
| 1444 | ObjectPvsMap::const_iterator oit_end = vc->GetPvs().mEntries.end();
|
---|
| 1445 |
|
---|
| 1446 | for (oit = vc->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
|
---|
| 1447 | {
|
---|
[584] | 1448 | Intersectable *obj = (*oit).first;
|
---|
| 1449 | if ((cit == interior->mChildren.begin()) && !obj->Mailed())
|
---|
| 1450 | obj->Mail();
|
---|
[581] | 1451 |
|
---|
[584] | 1452 | int incm = obj->IncMail();
|
---|
[581] | 1453 | }
|
---|
| 1454 | }
|
---|
| 1455 |
|
---|
| 1456 | interior->GetPvs().mEntries.clear();
|
---|
| 1457 |
|
---|
[584] | 1458 |
|
---|
[581] | 1459 | // only the objects which are present in all leaf pvs
|
---|
| 1460 | // should remain in the parent pvs
|
---|
[584] | 1461 | // these are the objects which have been mailed in all children
|
---|
[581] | 1462 | for (cit = interior->mChildren.begin(); cit != cit_end; ++ cit)
|
---|
| 1463 | {
|
---|
| 1464 | ViewCell *vc = *cit;
|
---|
| 1465 |
|
---|
| 1466 | ObjectPvsMap::const_iterator oit_end = vc->GetPvs().mEntries.end();
|
---|
| 1467 |
|
---|
| 1468 | for (oit = vc->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
|
---|
[585] | 1469 | {
|
---|
[581] | 1470 | if ((*oit).first->Mailed((int)interior->mChildren.size()))
|
---|
| 1471 | {
|
---|
| 1472 | interior->GetPvs().AddSample((*oit).first, (*oit).second.mSumPdf);
|
---|
| 1473 | }
|
---|
| 1474 | }
|
---|
| 1475 | }
|
---|
| 1476 |
|
---|
[584] | 1477 |
|
---|
| 1478 |
|
---|
| 1479 | // delete all the objects from the leaf sets which were moved to parent pvs
|
---|
[581] | 1480 | ObjectPvsMap::const_iterator oit_end = interior->GetPvs().mEntries.end();
|
---|
| 1481 |
|
---|
| 1482 | for (oit = interior->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
|
---|
| 1483 | {
|
---|
| 1484 | for (cit = interior->mChildren.begin(); cit != cit_end; ++ cit)
|
---|
| 1485 | {
|
---|
[584] | 1486 | if (!(*cit)->GetPvs().RemoveSample((*oit).first, Limits::Infinity))
|
---|
| 1487 | Debug << "should not come here!" << endl;
|
---|
[581] | 1488 | }
|
---|
| 1489 | }
|
---|
[585] | 1490 |
|
---|
| 1491 | int dummy = interior->GetPvs().GetSize();
|
---|
| 1492 |
|
---|
| 1493 | for (cit = interior->mChildren.begin(); cit != cit_end; ++ cit)
|
---|
| 1494 | {
|
---|
| 1495 | dummy += (*cit)->GetPvs().GetSize();
|
---|
| 1496 | }
|
---|
| 1497 |
|
---|
[581] | 1498 | }
|
---|
| 1499 |
|
---|
| 1500 |
|
---|
[584] | 1501 | void ViewCellsTree::GetPvs(ViewCell *vc, ObjectPvs &pvs) const
|
---|
| 1502 | {
|
---|
[585] | 1503 | Intersectable::NewMail();
|
---|
| 1504 |
|
---|
| 1505 | if (!mIsCompressed)
|
---|
| 1506 | pvs = vc->GetPvs();
|
---|
| 1507 |
|
---|
| 1508 | int pvsSize = 0;
|
---|
| 1509 | ViewCell *root = vc;
|
---|
| 1510 |
|
---|
| 1511 | // also add pvs from this view cell to root
|
---|
| 1512 | while (root->GetParent())
|
---|
[584] | 1513 | {
|
---|
[585] | 1514 | root = root->GetParent();
|
---|
| 1515 | pvs.AddPvs(root->GetPvs());
|
---|
| 1516 | }
|
---|
[584] | 1517 |
|
---|
[585] | 1518 | stack<ViewCell *> tstack;
|
---|
| 1519 | tstack.push(vc);
|
---|
[584] | 1520 |
|
---|
[585] | 1521 | while (!tstack.empty())
|
---|
| 1522 | {
|
---|
| 1523 | vc = tstack.top();
|
---|
| 1524 | tstack.pop();
|
---|
| 1525 |
|
---|
| 1526 | pvs.AddPvs(vc->GetPvs());
|
---|
| 1527 |
|
---|
[584] | 1528 | if (!vc->IsLeaf())
|
---|
| 1529 | {
|
---|
| 1530 | ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(vc);
|
---|
[585] | 1531 |
|
---|
[584] | 1532 | ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
|
---|
| 1533 |
|
---|
| 1534 | for (it = interior->mChildren.begin(); it != it_end; ++ it)
|
---|
| 1535 | {
|
---|
[585] | 1536 | tstack.push(*it);
|
---|
| 1537 | }
|
---|
[584] | 1538 | }
|
---|
| 1539 | }
|
---|
| 1540 | }
|
---|
| 1541 |
|
---|
| 1542 |
|
---|
| 1543 | int ViewCellsTree::GetPvsSize(ViewCell *vc) const
|
---|
| 1544 | {
|
---|
[585] | 1545 | Intersectable::NewMail();
|
---|
[584] | 1546 |
|
---|
[585] | 1547 | if (!mIsCompressed)
|
---|
| 1548 | return vc->GetPvs().GetSize();
|
---|
| 1549 |
|
---|
| 1550 | int pvsSize = 0;
|
---|
| 1551 | ViewCell *root = vc;
|
---|
| 1552 |
|
---|
| 1553 | // also add pvs from this view cell to root
|
---|
| 1554 | while (root->GetParent())
|
---|
[584] | 1555 | {
|
---|
[585] | 1556 | root = root->GetParent();
|
---|
| 1557 | pvsSize += CountDiffPvs(root);
|
---|
| 1558 | }
|
---|
[584] | 1559 |
|
---|
[585] | 1560 | stack<ViewCell *> tstack;
|
---|
| 1561 | tstack.push(vc);
|
---|
[584] | 1562 |
|
---|
[585] | 1563 | while (!tstack.empty())
|
---|
| 1564 | {
|
---|
| 1565 | vc = tstack.top();
|
---|
| 1566 | tstack.pop();
|
---|
| 1567 |
|
---|
| 1568 | pvsSize += CountDiffPvs(vc);
|
---|
| 1569 |
|
---|
| 1570 | if (!vc->IsLeaf())
|
---|
[584] | 1571 | {
|
---|
[585] | 1572 | ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(vc);
|
---|
| 1573 |
|
---|
| 1574 | ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
|
---|
| 1575 |
|
---|
| 1576 | for (it = interior->mChildren.begin(); it != it_end; ++ it)
|
---|
| 1577 | {
|
---|
| 1578 | tstack.push(*it);
|
---|
| 1579 | }
|
---|
[584] | 1580 | }
|
---|
| 1581 | }
|
---|
| 1582 |
|
---|
| 1583 | return pvsSize;
|
---|
| 1584 |
|
---|
| 1585 | }
|
---|
| 1586 |
|
---|
| 1587 |
|
---|
| 1588 | float ViewCellsTree::GetMemoryCost(ViewCell *vc) const
|
---|
| 1589 | {
|
---|
| 1590 | const float entrySize =
|
---|
| 1591 | sizeof(PvsData<Intersectable *>) + sizeof(Intersectable *);
|
---|
| 1592 |
|
---|
| 1593 | return (float)GetNumPvsEntries(vc) * entrySize;
|
---|
| 1594 | }
|
---|
| 1595 |
|
---|
| 1596 |
|
---|
| 1597 | int ViewCellsTree::GetNumPvsEntries(ViewCell *vc) const
|
---|
| 1598 | {
|
---|
[590] | 1599 | int pvsSize = 0;
|
---|
| 1600 | // only count leaves for uncompressed method for fairness
|
---|
| 1601 | if (mIsCompressed || vc->IsLeaf())
|
---|
| 1602 | pvsSize = vc->GetPvs().GetSize();
|
---|
[584] | 1603 |
|
---|
| 1604 | if (!vc->IsLeaf())
|
---|
| 1605 | {
|
---|
| 1606 | ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(vc);
|
---|
| 1607 |
|
---|
| 1608 | ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
|
---|
| 1609 |
|
---|
| 1610 | for (it = interior->mChildren.begin(); it != it_end; ++ it)
|
---|
| 1611 | {
|
---|
| 1612 | pvsSize += GetNumPvsEntries(*it);
|
---|
| 1613 | }
|
---|
| 1614 | }
|
---|
| 1615 |
|
---|
| 1616 | return pvsSize;
|
---|
| 1617 | }
|
---|
| 1618 |
|
---|
| 1619 |
|
---|
| 1620 | bool ViewCellsTree::IsCompressed() const
|
---|
| 1621 | {
|
---|
| 1622 | return mIsCompressed;
|
---|
| 1623 | }
|
---|
| 1624 |
|
---|
| 1625 |
|
---|
[590] | 1626 | ViewCell *ViewCellsTree::GetActiveViewCell(ViewCell *vc) const
|
---|
| 1627 | {
|
---|
[660] | 1628 | while (vc->GetParent() && !vc->IsActive())
|
---|
[590] | 1629 | {
|
---|
| 1630 | vc = vc->GetParent();
|
---|
| 1631 | }
|
---|
| 1632 |
|
---|
| 1633 | return vc;
|
---|
| 1634 | }
|
---|
| 1635 |
|
---|
| 1636 |
|
---|
[651] | 1637 | void ViewCellsTree::PropagatePvs(ViewCell *vc)
|
---|
[664] | 1638 | {
|
---|
| 1639 | ViewCell *viewCell = vc;
|
---|
| 1640 |
|
---|
[660] | 1641 | // propagate pvs up
|
---|
[664] | 1642 | while (viewCell->GetParent())
|
---|
[610] | 1643 | {
|
---|
[664] | 1644 | viewCell->GetParent()->GetPvs().Merge(vc->GetPvs());
|
---|
| 1645 | viewCell = viewCell->GetParent();
|
---|
[610] | 1646 | }
|
---|
[651] | 1647 |
|
---|
| 1648 | if (vc->IsLeaf())
|
---|
| 1649 | return;
|
---|
| 1650 |
|
---|
[660] | 1651 | // propagate pvs to the leaves
|
---|
[651] | 1652 | stack<ViewCell *> tstack;
|
---|
| 1653 | tstack.push(vc);
|
---|
| 1654 |
|
---|
| 1655 | while (!tstack.empty())
|
---|
| 1656 | {
|
---|
| 1657 | ViewCell *viewCell = tstack.top();
|
---|
| 1658 | tstack.pop();
|
---|
| 1659 |
|
---|
| 1660 | if (viewCell != vc)
|
---|
| 1661 | {
|
---|
| 1662 | viewCell->GetPvs().Merge(vc->GetPvs());
|
---|
| 1663 | }
|
---|
| 1664 |
|
---|
| 1665 | if (!viewCell->IsLeaf())
|
---|
| 1666 | {
|
---|
| 1667 | ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(viewCell);
|
---|
[664] | 1668 |
|
---|
[651] | 1669 | ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
|
---|
| 1670 |
|
---|
| 1671 | for (it = interior->mChildren.begin(); it != it_end; ++ it)
|
---|
| 1672 | {
|
---|
| 1673 | tstack.push(*it);
|
---|
| 1674 | }
|
---|
| 1675 | }
|
---|
| 1676 | }
|
---|
[610] | 1677 | }
|
---|
[605] | 1678 |
|
---|
[610] | 1679 |
|
---|
[650] | 1680 | void ViewCellsTree::AssignRandomColors()
|
---|
[608] | 1681 | {
|
---|
| 1682 | TraversalQueue tqueue;
|
---|
| 1683 | tqueue.push(mRoot);
|
---|
| 1684 | mRoot->SetColor(RandomColor(0.3f, 1.0f));
|
---|
[664] | 1685 |
|
---|
[608] | 1686 | while (!tqueue.empty())
|
---|
| 1687 | {
|
---|
| 1688 | ViewCell *vc = tqueue.top();
|
---|
[650] | 1689 | tqueue.pop();
|
---|
| 1690 |
|
---|
[608] | 1691 | // save the view cells if it is a leaf or if enough view cells have already been traversed
|
---|
| 1692 | // because of the priority queue, this will be the optimal set of v
|
---|
| 1693 | if (!vc->IsLeaf()) {
|
---|
| 1694 | ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(vc);
|
---|
| 1695 |
|
---|
| 1696 | ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
|
---|
| 1697 | float maxProbability = -1.0f;
|
---|
| 1698 | ViewCell *maxViewCell = NULL;
|
---|
| 1699 | for (it = interior->mChildren.begin(); it != it_end; ++ it) {
|
---|
| 1700 | ViewCell *v = *it;
|
---|
| 1701 | // set random color
|
---|
| 1702 | v->SetColor(RandomColor(0.3f, 1.0f));
|
---|
| 1703 | if (v->GetVolume() > maxProbability) {
|
---|
| 1704 | maxProbability = v->GetVolume();
|
---|
| 1705 | maxViewCell = v;
|
---|
| 1706 | }
|
---|
| 1707 | maxViewCell->SetColor(vc->GetColor());
|
---|
| 1708 | tqueue.push(v);
|
---|
| 1709 | }
|
---|
| 1710 |
|
---|
| 1711 | }
|
---|
[650] | 1712 |
|
---|
[608] | 1713 | }
|
---|
| 1714 | }
|
---|
| 1715 |
|
---|
| 1716 | /** Get costs resulting from each merge step. */
|
---|
| 1717 | void
|
---|
| 1718 | ViewCellsTree::GetCostFunction(vector<float> &costFunction)
|
---|
| 1719 | {
|
---|
| 1720 | TraversalQueue tqueue;
|
---|
| 1721 | tqueue.push(mRoot);
|
---|
| 1722 | while (!tqueue.empty()) {
|
---|
| 1723 | ViewCell *vc = tqueue.top();
|
---|
[650] | 1724 | tqueue.pop();
|
---|
[608] | 1725 | // save the view cells if it is a leaf or if enough view cells have already been traversed
|
---|
| 1726 | // because of the priority queue, this will be the optimal set of v
|
---|
| 1727 | if (!vc->IsLeaf()) {
|
---|
| 1728 | ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(vc);
|
---|
| 1729 | costFunction.push_back(interior->GetCost());
|
---|
| 1730 |
|
---|
| 1731 | ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
|
---|
| 1732 |
|
---|
| 1733 | for (it = interior->mChildren.begin(); it != it_end; ++ it) {
|
---|
| 1734 | tqueue.push(*it);
|
---|
| 1735 | }
|
---|
| 1736 |
|
---|
| 1737 | }
|
---|
| 1738 | }
|
---|
| 1739 | }
|
---|
| 1740 |
|
---|
| 1741 |
|
---|
[605] | 1742 | void ViewCellsTree::UpdateViewCellsStats(ViewCell *vc, ViewCellsStatistics &vcStat)
|
---|
| 1743 | {
|
---|
| 1744 | ++ vcStat.viewCells;
|
---|
| 1745 |
|
---|
| 1746 | const int pvsSize = GetPvsSize(vc);
|
---|
| 1747 |
|
---|
[613] | 1748 | vcStat.pvsSize += pvsSize;
|
---|
[605] | 1749 |
|
---|
| 1750 | if (pvsSize == 0)
|
---|
| 1751 | ++ vcStat.emptyPvs;
|
---|
| 1752 |
|
---|
| 1753 | if (pvsSize > vcStat.maxPvs)
|
---|
| 1754 | vcStat.maxPvs = pvsSize;
|
---|
| 1755 |
|
---|
| 1756 | if (pvsSize < vcStat.minPvs)
|
---|
| 1757 | vcStat.minPvs = pvsSize;
|
---|
| 1758 |
|
---|
| 1759 | if (!vc->GetValid())
|
---|
| 1760 | ++ vcStat.invalid;
|
---|
[613] | 1761 |
|
---|
| 1762 | /*ViewCellsContainer leaves;
|
---|
| 1763 | CollectLeaves(vc, leaves);
|
---|
| 1764 |
|
---|
| 1765 | vcStat.leaves = (int)leaves.size();*/
|
---|
[605] | 1766 | }
|
---|
| 1767 |
|
---|
| 1768 |
|
---|
[610] | 1769 | bool ViewCellsTree::Export(ofstream &stream)
|
---|
| 1770 | {
|
---|
| 1771 | ExportViewCell(mRoot, stream);
|
---|
[605] | 1772 |
|
---|
[610] | 1773 | return true;
|
---|
| 1774 | }
|
---|
| 1775 |
|
---|
| 1776 |
|
---|
[651] | 1777 | void ViewCellsTree::CreateUniqueViewCellsIds()
|
---|
[610] | 1778 | {
|
---|
[651] | 1779 | stack<ViewCell *> tstack;
|
---|
[610] | 1780 |
|
---|
[651] | 1781 | int currentId = 0;
|
---|
| 1782 |
|
---|
| 1783 | tstack.push(mRoot);
|
---|
| 1784 |
|
---|
| 1785 | while (!tstack.empty())
|
---|
[610] | 1786 | {
|
---|
[651] | 1787 | ViewCell *vc = tstack.top();
|
---|
| 1788 | tstack.pop();
|
---|
| 1789 |
|
---|
| 1790 | vc->SetId(currentId ++);
|
---|
| 1791 |
|
---|
| 1792 | if (!vc->IsLeaf())
|
---|
| 1793 | {
|
---|
| 1794 | ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(vc);
|
---|
| 1795 | ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
|
---|
| 1796 | for (it = interior->mChildren.begin(); it != it_end; ++ it)
|
---|
| 1797 | {
|
---|
| 1798 | tstack.push(*it);
|
---|
| 1799 | }
|
---|
| 1800 | }
|
---|
[610] | 1801 | }
|
---|
[651] | 1802 | }
|
---|
[610] | 1803 |
|
---|
| 1804 |
|
---|
[651] | 1805 | void ViewCellsTree::ExportViewCell(ViewCell *viewCell, ofstream &stream)
|
---|
| 1806 | {
|
---|
| 1807 | ObjectPvsMap::iterator it, it_end = viewCell->GetPvs().mEntries.end();
|
---|
| 1808 |
|
---|
[610] | 1809 | if (viewCell->IsLeaf())
|
---|
| 1810 | {
|
---|
| 1811 | stream << "<Leaf ";
|
---|
| 1812 | stream << "id=\"" << viewCell->GetId() << "\" ";
|
---|
[660] | 1813 | stream << "active=\"" << viewCell->IsActive() << "\" ";
|
---|
[610] | 1814 | stream << "mergecost=\"" << viewCell->GetMergeCost() << "\" ";
|
---|
| 1815 | stream << "pvs=\"";
|
---|
[651] | 1816 | if (0)// test with empty pvs
|
---|
| 1817 | for (it = viewCell->GetPvs().mEntries.begin(); it != it_end; ++ it)
|
---|
| 1818 | {
|
---|
| 1819 | stream << (*it).first->GetId() << " ";
|
---|
| 1820 | }
|
---|
| 1821 |
|
---|
| 1822 |
|
---|
| 1823 | stream << "\" />" << endl;
|
---|
| 1824 | //stream << " </Leaf>" << endl;
|
---|
[610] | 1825 | }
|
---|
| 1826 | else
|
---|
| 1827 | {
|
---|
| 1828 | ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(viewCell);
|
---|
| 1829 |
|
---|
| 1830 | stream << "<Interior ";
|
---|
| 1831 | stream << "id=\"" << viewCell->GetId() << "\" ";
|
---|
[660] | 1832 | stream << "active=\"" << viewCell->IsActive() << "\" ";
|
---|
[610] | 1833 | stream << "mergecost=\"" << viewCell->GetMergeCost() << "\" ";
|
---|
| 1834 | stream << "pvs=\"";
|
---|
| 1835 |
|
---|
[651] | 1836 | if (0)// test with empty pvs
|
---|
| 1837 | for (it = viewCell->GetPvs().mEntries.begin(); it != it_end; ++ it)
|
---|
| 1838 | {
|
---|
| 1839 | stream << (*it).first->GetId() << " ";
|
---|
| 1840 | }
|
---|
| 1841 |
|
---|
| 1842 | stream << "\" >" << endl;
|
---|
| 1843 |
|
---|
[610] | 1844 | ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
|
---|
| 1845 |
|
---|
| 1846 | for (it = interior->mChildren.begin(); it != it_end; ++ it)
|
---|
| 1847 | {
|
---|
| 1848 | ExportViewCell(*it, stream);
|
---|
| 1849 | }
|
---|
| 1850 |
|
---|
| 1851 | stream << "</Interior>" << endl;
|
---|
| 1852 | }
|
---|
| 1853 | }
|
---|
| 1854 |
|
---|
| 1855 |
|
---|
[660] | 1856 | void ViewCellsTree::ResetPvs()
|
---|
| 1857 | {
|
---|
| 1858 | stack<ViewCell *> tstack;
|
---|
[610] | 1859 |
|
---|
[660] | 1860 | tstack.push(mRoot);
|
---|
| 1861 |
|
---|
| 1862 | while (!tstack.empty())
|
---|
| 1863 | {
|
---|
| 1864 | ViewCell *vc = tstack.top();
|
---|
| 1865 | tstack.pop();
|
---|
| 1866 |
|
---|
| 1867 | vc->GetPvs().mEntries.clear();
|
---|
| 1868 |
|
---|
| 1869 | if (!vc->IsLeaf())
|
---|
| 1870 | {
|
---|
| 1871 | ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(vc);
|
---|
| 1872 | ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
|
---|
| 1873 | for (it = interior->mChildren.begin(); it != it_end; ++ it)
|
---|
| 1874 | {
|
---|
| 1875 | tstack.push(*it);
|
---|
| 1876 | }
|
---|
| 1877 | }
|
---|
| 1878 | }
|
---|
| 1879 | }
|
---|
| 1880 |
|
---|
| 1881 |
|
---|
| 1882 | void ViewCellsTree::SetActiveSetToLeaves()
|
---|
| 1883 | {
|
---|
| 1884 | }
|
---|
| 1885 |
|
---|
[580] | 1886 | /**************************************************************************/
|
---|
| 1887 | /* MergeCandidate implementation */
|
---|
| 1888 | /**************************************************************************/
|
---|
| 1889 |
|
---|
| 1890 |
|
---|
| 1891 | MergeCandidate::MergeCandidate(ViewCell *l, ViewCell *r):
|
---|
| 1892 | mRenderCost(0),
|
---|
| 1893 | mDeviationIncr(0),
|
---|
| 1894 | mLeftViewCell(l),
|
---|
[586] | 1895 | mRightViewCell(r),
|
---|
| 1896 | mInitialLeftViewCell(l),
|
---|
| 1897 | mInitialRightViewCell(r)
|
---|
[580] | 1898 | {
|
---|
| 1899 | //EvalMergeCost();
|
---|
| 1900 | }
|
---|
| 1901 |
|
---|
| 1902 |
|
---|
| 1903 | void MergeCandidate::SetRightViewCell(ViewCell *v)
|
---|
| 1904 | {
|
---|
| 1905 | mRightViewCell = v;
|
---|
| 1906 | }
|
---|
| 1907 |
|
---|
| 1908 |
|
---|
| 1909 | void MergeCandidate::SetLeftViewCell(ViewCell *v)
|
---|
| 1910 | {
|
---|
| 1911 | mLeftViewCell = v;
|
---|
| 1912 | }
|
---|
| 1913 |
|
---|
| 1914 |
|
---|
| 1915 | ViewCell *MergeCandidate::GetRightViewCell() const
|
---|
| 1916 | {
|
---|
| 1917 | return mRightViewCell;
|
---|
| 1918 | }
|
---|
| 1919 |
|
---|
| 1920 |
|
---|
| 1921 | ViewCell *MergeCandidate::GetLeftViewCell() const
|
---|
| 1922 | {
|
---|
| 1923 | return mLeftViewCell;
|
---|
| 1924 | }
|
---|
| 1925 |
|
---|
| 1926 |
|
---|
| 1927 | ViewCell *MergeCandidate::GetInitialRightViewCell() const
|
---|
| 1928 | {
|
---|
| 1929 | return mInitialRightViewCell;
|
---|
| 1930 | }
|
---|
| 1931 |
|
---|
| 1932 |
|
---|
| 1933 | ViewCell *MergeCandidate::GetInitialLeftViewCell() const
|
---|
| 1934 | {
|
---|
| 1935 | return mInitialLeftViewCell;
|
---|
| 1936 | }
|
---|
| 1937 |
|
---|
| 1938 |
|
---|
| 1939 | bool MergeCandidate::IsValid() const
|
---|
| 1940 | {
|
---|
[582] | 1941 | return !(mLeftViewCell->mParent || mRightViewCell->mParent);
|
---|
[580] | 1942 | }
|
---|
| 1943 |
|
---|
| 1944 |
|
---|
| 1945 | float MergeCandidate::GetRenderCost() const
|
---|
| 1946 | {
|
---|
| 1947 | return mRenderCost;
|
---|
| 1948 | }
|
---|
| 1949 |
|
---|
| 1950 |
|
---|
| 1951 | float MergeCandidate::GetDeviationIncr() const
|
---|
| 1952 | {
|
---|
| 1953 | return mDeviationIncr;
|
---|
| 1954 | }
|
---|
| 1955 |
|
---|
| 1956 |
|
---|
| 1957 | float MergeCandidate::GetMergeCost() const
|
---|
| 1958 | {
|
---|
| 1959 | return mRenderCost * sRenderCostWeight +
|
---|
| 1960 | mDeviationIncr * (1.0f - sRenderCostWeight);
|
---|
| 1961 | }
|
---|
| 1962 |
|
---|
| 1963 |
|
---|
[605] | 1964 |
|
---|
[610] | 1965 |
|
---|
| 1966 |
|
---|
| 1967 |
|
---|
[580] | 1968 | /************************************************************************/
|
---|
| 1969 | /* MergeStatistics implementation */
|
---|
| 1970 | /************************************************************************/
|
---|
| 1971 |
|
---|
| 1972 |
|
---|
| 1973 | void MergeStatistics::Print(ostream &app) const
|
---|
| 1974 | {
|
---|
| 1975 | app << "===== Merge statistics ===============\n";
|
---|
| 1976 |
|
---|
| 1977 | app << setprecision(4);
|
---|
| 1978 |
|
---|
| 1979 | app << "#N_CTIME ( Overall time [s] )\n" << Time() << " \n";
|
---|
| 1980 |
|
---|
| 1981 | app << "#N_CCTIME ( Collect candidates time [s] )\n" << collectTime * 1e-3f << " \n";
|
---|
| 1982 |
|
---|
| 1983 | app << "#N_MTIME ( Merge time [s] )\n" << mergeTime * 1e-3f << " \n";
|
---|
| 1984 |
|
---|
| 1985 | app << "#N_NODES ( Number of nodes before merge )\n" << nodes << "\n";
|
---|
| 1986 |
|
---|
| 1987 | app << "#N_CANDIDATES ( Number of merge candidates )\n" << candidates << "\n";
|
---|
| 1988 |
|
---|
| 1989 | app << "#N_MERGEDSIBLINGS ( Number of merged siblings )\n" << siblings << "\n";
|
---|
| 1990 |
|
---|
| 1991 | app << "#OVERALLCOST ( overall merge cost )\n" << overallCost << "\n";
|
---|
| 1992 |
|
---|
| 1993 | app << "#N_MERGEDNODES ( Number of merged nodes )\n" << merged << "\n";
|
---|
| 1994 |
|
---|
| 1995 | app << "#MAX_TREEDIST ( Maximal distance in tree of merged leaves )\n" << maxTreeDist << "\n";
|
---|
| 1996 |
|
---|
| 1997 | app << "#AVG_TREEDIST ( Average distance in tree of merged leaves )\n" << AvgTreeDist() << "\n";
|
---|
| 1998 |
|
---|
| 1999 | app << "#EXPECTEDCOST ( expected render cost )\n" << expectedRenderCost << "\n";
|
---|
| 2000 |
|
---|
| 2001 | app << "#DEVIATION ( deviation )\n" << deviation << "\n";
|
---|
| 2002 |
|
---|
| 2003 | app << "#HEURISTICS ( heuristics )\n" << heuristics << "\n";
|
---|
| 2004 |
|
---|
| 2005 |
|
---|
| 2006 | app << "===== END OF BspTree statistics ==========\n";
|
---|
[608] | 2007 | }
|
---|