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