- Timestamp:
- 01/03/06 23:33:45 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp
r490 r492 11 11 #include "ViewCellsManager.h" 12 12 #include "RenderSimulator.h" 13 #include "GlRenderer.h" 13 14 14 15 static bool useViewSpaceBox = false; … … 33 34 environment->GetIntValue("RssPreprocessor.Export.numRays", mExportNumRays); 34 35 environment->GetBoolValue("RssPreprocessor.useViewcells", mUseViewcells); 35 environment->GetBoolValue("RssPreprocessor.useViewcells", mUseViewcells); 36 environment->GetBoolValue("RssPreprocessor.objectBasedSampling", mObjectBasedSampling); 37 environment->GetBoolValue("RssPreprocessor.directionalSampling", mDirectionalSampling); 36 38 37 39 environment->GetBoolValue("RssPreprocessor.loadInitialSamples", mLoadInitialSamples); … … 39 41 40 42 mStats.open("stats.log"); 43 41 44 } 42 45 43 46 RssPreprocessor::~RssPreprocessor() 44 47 { 45 CLEAR_CONTAINER(mVssRays); 48 // mVssRays get deleted in the tree 49 // CLEAR_CONTAINER(mVssRays); 46 50 } 47 51 … … 78 82 Vector3 pointA, pointB; 79 83 float bsize = Magnitude(box.Size()); 84 80 85 81 86 if (mKdTree->CastRay(ray)) { … … 86 91 // compute intersection with the scene bounding box 87 92 float tmin, tmax; 88 box.ComputeMinMaxT(ray, &tmin, &tmax); 89 if (tmax > bsize) { 90 //cerr<<"Warning: tmax > box size tmax="<<tmax<<" tmin="<<tmin<<" size="<<bsize<<endl; 91 //cerr<<"ray"<<ray<<endl; 92 } 93 pointA = ray.Extrap(tmax); 93 if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax) 94 pointA = ray.Extrap(tmax); 95 else 96 return 0; 94 97 } 95 98 … … 108 111 objectB = NULL; 109 112 float tmin, tmax; 110 box.ComputeMinMaxT(ray, &tmin, &tmax); 111 if (tmax > bsize) { 112 //cerr<<"Warning: tmax > box size tmax="<<tmax<<" tmin="<<tmin<<" size="<<bsize<<endl; 113 //cerr<<"ray"<<ray<<endl; 114 } 115 pointB = ray.Extrap(tmax); 116 } 117 113 if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax) 114 pointB = ray.Extrap(tmax); 115 else 116 return 0; 117 } 118 119 // if (objectA == NULL && objectB != NULL) { 120 if (1) { 121 // cast again to ensure that there is no objectA 122 SetupRay(ray, pointB, direction); 123 if (mKdTree->CastRay(ray)) { 124 objectA = ray.intersections[0].mObject; 125 pointA = ray.Extrap(ray.intersections[0].mT); 126 } 127 } 128 129 118 130 VssRay *vssRay = NULL; 119 131 120 bool validSample = true;121 if ( detectEmptyViewSpace) {132 bool validSample = (objectA != objectB); 133 if (0 && detectEmptyViewSpace) { // consider all samples valid 122 134 // check if the viewpoint lies on the line segment AB 123 135 if (Distance(pointA, pointB) < … … 176 188 Vector3 point; 177 189 if (!use2dSampling) { 178 Vector3 normal; 179 int i = (int)RandomValue(0, (Real)((int)mObjects.size()-1)); 180 Intersectable *object = mObjects[i]; 181 object->GetRandomSurfacePoint(point, normal); 190 if (mObjectBasedSampling) { 191 Vector3 normal; 192 int i = RandomValue(0, mObjects.size()-1); 193 Intersectable *object = mObjects[i]; 194 object->GetRandomSurfacePoint(point, normal); 195 } else { 196 // select 197 if (mDirectionalSampling) { 198 point = viewpoint + UniformRandomVector(); 199 } else { 200 // select the other point uniformly distruted in the whole viewspace 201 AxisAlignedBox3 box; 202 203 if (viewSpaceBox) 204 box =*viewSpaceBox; 205 else 206 box = mKdTree->GetBox(); 207 208 point = box.GetRandomPoint(); 209 } 210 } 182 211 } else { 183 212 AxisAlignedBox3 box; … … 187 216 else 188 217 box = mKdTree->GetBox(); 189 218 190 219 point = box.GetRandomPoint(); 191 220 point.y = viewpoint.y; 192 221 } 193 222 223 return point - viewpoint; 224 } 225 226 Vector3 227 RssPreprocessor::InitialGetDirection(const Vector3 &viewpoint, 228 AxisAlignedBox3 *viewSpaceBox 229 ) 230 { 231 Vector3 point; 232 if (!use2dSampling) { 233 if (1 || mObjectBasedSampling) { 234 Vector3 normal; 235 int i = RandomValue(0, mObjects.size()-1); 236 Intersectable *object = mObjects[i]; 237 object->GetRandomSurfacePoint(point, normal); 238 } else { 239 // select 240 if (1 || mDirectionalSampling) { 241 point = viewpoint + UniformRandomVector(); 242 } else { 243 // select the other point uniformly distruted in the whole viewspace 244 AxisAlignedBox3 box; 245 246 if (viewSpaceBox) 247 box =*viewSpaceBox; 248 else 249 box = mKdTree->GetBox(); 250 251 point = box.GetRandomPoint(); 252 } 253 } 254 } else { 255 AxisAlignedBox3 box; 256 257 if (viewSpaceBox) 258 box =*viewSpaceBox; 259 else 260 box = mKdTree->GetBox(); 261 262 point = box.GetRandomPoint(); 263 point.y = viewpoint.y; 264 } 265 194 266 return point - viewpoint; 195 267 } … … 203 275 int num; 204 276 205 206 207 float avgPvsSize; 208 float avgRayContribution; 209 float avgPvsEntropy; 210 float avgRayLengthEntropy; 211 float avgImportance; 212 float avgRays; 213 rssTree->GetTreeStatistics( 214 avgPvsSize, 215 avgRays, 216 avgRayContribution, 217 avgPvsEntropy, 218 avgRayLengthEntropy, 219 avgImportance); 220 277 rssTree->UpdateTreeStatistics(); 278 221 279 cout<< 222 "#RSS_AVG_PVS_SIZE\n"<< avgPvsSize<<endl<<223 "#RSS_AVG_RAYS\n"<< avgRays<<endl<<224 "#RSS_AVG_RAY_CONTRIB\n"<< avgRayContribution<<endl<<225 "#RSS_AVG_PVS_ENTROPY\n"<< avgPvsEntropy<<endl<<226 "#RSS_AVG_RAY_LENGTH_ENTROPY\n"<< avgRayLengthEntropy<<endl<<227 "#RSS_AVG_IMPORTANCE\n"<< avgImportance<<endl;280 "#RSS_AVG_PVS_SIZE\n"<<rssTree->stat.avgPvsSize<<endl<< 281 "#RSS_AVG_RAYS\n"<<rssTree->stat.avgRays<<endl<< 282 "#RSS_AVG_RAY_CONTRIB\n"<<rssTree->stat.avgRayContribution<<endl<< 283 "#RSS_AVG_PVS_ENTROPY\n"<<rssTree->stat.avgPvsEntropy<<endl<< 284 "#RSS_AVG_RAY_LENGTH_ENTROPY\n"<<rssTree->stat.avgRayLengthEntropy<<endl<< 285 "#RSS_AVG_IMPORTANCE\n"<<rssTree->stat.avgImportance<<endl; 228 286 229 287 if (0) { 230 float p = desiredSamples/(float)( avgRayContribution*rssTree->stat.Leaves());288 float p = desiredSamples/(float)(rssTree->stat.avgRayContribution*rssTree->stat.Leaves()); 231 289 num = rssTree->GenerateRays(p, rays); 232 290 } else { … … 249 307 cout<<"Exporting vss rays..."<<endl<<flush; 250 308 251 float prob = number/(float)vssRays.size();252 253 254 309 Exporter *exporter = NULL; 255 310 exporter = Exporter::GetExporter(filename); … … 266 321 } 267 322 268 VssRayContainer rays; for (int i=0; i < vssRays.size(); i++)269 if (RandomValue(0,1) < prob) 270 rays.push_back(vssRays[i]);323 VssRayContainer rays; 324 325 vssRays.SelectRays( number, rays); 271 326 272 327 exporter->ExportRays(rays, RgbColor(1, 0, 0)); … … 388 443 } 389 444 445 446 void 447 RssPreprocessor::ComputeRenderError() 448 { 449 // compute rendering error 450 if (renderer) { 451 emit EvalPvsStat(); 452 QMutex mutex; 453 mutex.lock(); 454 renderer->mRenderingFinished.wait(&mutex); 455 mutex.unlock(); 456 mStats << 457 "#AvgPvsRenderError\n" <<renderer->mPvsStat.GetAvgError()<<endl<< 458 "#MaxPvsRenderError\n" <<renderer->mPvsStat.GetMaxError()<<endl<< 459 "#ErrorFreeFrames\n" <<renderer->mPvsStat.GetErrorFreeFrames()<<endl; 460 } 461 } 462 390 463 bool 391 464 RssPreprocessor::ComputeVisibility() 392 465 { 466 467 connect(this, SIGNAL(EvalPvsStat()), renderer, SLOT(EvalPvsStat()) ); 468 393 469 cout<<"Rss Preprocessor started\n"<<flush; 394 470 cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl; 395 mSceneGraph->CollectObjects(&mObjects); 471 472 Randomize(0); 473 396 474 397 475 long startTime = GetTime(); … … 443 521 444 522 RssTree *rssTree = NULL; 445 446 if (mLoadInitialSamples) 447 { 448 cout << "Loading samples from file ... "; 449 LoadSamples(mVssRays, mObjects); 450 cout << "finished\n" << endl; 451 } 452 else 453 { 523 int rssPass = 0; 524 int rssSamples = 0; 525 526 #if 0 527 if (mLoadInitialSamples) { 528 cout << "Loading samples from file ... "; 529 LoadSamples(mVssRays, mObjects); 530 cout << "finished\n" << endl; 531 } else { 532 #endif 454 533 while (totalSamples < mInitialSamples) { 455 int passContributingSamples = 0; 456 int passSampleContributions = 0; 457 int passSamples = 0; 458 int index = 0; 459 460 int sampleContributions; 461 462 int s = Min(mSamplesPerPass, mInitialSamples); 463 for (int k=0; k < s; k++) { 464 534 int passContributingSamples = 0; 535 int passSampleContributions = 0; 536 int passSamples = 0; 537 int index = 0; 538 539 int sampleContributions; 540 541 //int s = Min(mSamplesPerPass, mInitialSamples); 542 int s = mInitialSamples; 543 544 for (int k=0; k < s; k++) { 545 546 465 547 //Vector3 viewpoint = GetViewpoint(mViewSpaceBox); 466 548 Vector3 viewpoint; 467 mViewCellsManager->GetViewPoint(viewpoint); 468 Vector3 direction = GetDirection(viewpoint, mViewSpaceBox); 469 549 // mViewCellsManager->GetViewPoint(viewpoint); 550 viewpoint = GetViewpoint(mViewSpaceBox); 551 Vector3 direction = InitialGetDirection(viewpoint, mViewSpaceBox); 552 470 553 sampleContributions = CastRay(viewpoint, direction, mVssRays); 471 472 554 555 473 556 //-- CORR matt: put block inside loop 474 557 if (sampleContributions) { 475 476 558 passContributingSamples ++; 559 passSampleContributions += sampleContributions; 477 560 } 478 561 passSamples++; 479 562 totalSamples++; 480 481 482 483 563 } 564 565 566 float avgRayContrib = (passContributingSamples > 0) ? 484 567 passSampleContributions/(float)passContributingSamples : 0; 485 486 cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; 487 cout << "#TotalSamples=" << totalSamples/1000 488 << "k #SampleContributions=" << passSampleContributions << " (" 489 << 100*passContributingSamples/(float)passSamples<<"%)" 490 << "avgcontrib=" << avgRayContrib << endl; 491 492 mStats << 568 569 cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; 570 cout << "#TotalSamples=" << totalSamples/1000 571 << "k #SampleContributions=" << passSampleContributions << " (" 572 << 100*passContributingSamples/(float)passSamples<<"%)" 573 << "avgcontrib=" << avgRayContrib << endl; 574 575 mPass++; 576 } 577 578 cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl; 579 cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush; 580 581 rssSamples += mVssRays.size(); 582 583 584 if (mExportRays) { 585 char filename[64]; 586 sprintf(filename, "rss-rays-initial.x3d"); 587 ExportRays(filename, mVssRays, mExportNumRays); 588 } 589 590 if (mStoreInitialSamples) 591 { 592 cout << "Writing samples to file ... "; 593 WriteSamples(mVssRays); 594 cout << "finished\n" << endl; 595 } 596 597 if (mUseViewcells) { 598 // construct view cells 599 mViewCellsManager->Construct(mObjects, mVssRays); 600 // evaluate contributions of the intitial rays 601 mViewCellsManager->ComputeSampleContributions(mVssRays); 602 603 604 mStats << 493 605 "#Pass\n" <<mPass<<endl<< 494 "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<< 495 "#TotalSamples\n" << totalSamples<< endl<< 496 "#SampleContributions\n" << passSampleContributions << endl << 497 "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl << 498 "#AvgRayContrib\n" << avgRayContrib << endl; 499 500 mPass++; 501 502 } 503 504 cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl; 505 } 506 507 cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush; 508 509 510 if (mExportRays) { 511 char filename[64]; 512 sprintf(filename, "rss-rays-initial.x3d"); 513 ExportRays(filename, mVssRays, mExportNumRays); 514 } 515 516 if (mStoreInitialSamples) 517 { 518 cout << "Writing samples to file ... "; 519 WriteSamples(mVssRays); 520 cout << "finished\n" << endl; 521 } 522 523 if (mUseViewcells) { 524 // construct view cells 525 mViewCellsManager->Construct(mObjects, mVssRays); 526 606 "#RssPass\n" <<rssPass<<endl<< 607 "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<< 608 "#TotalSamples\n" <<totalSamples<<endl<< 609 "#RssSamples\n" <<rssSamples<<endl; 610 611 ComputeRenderError(); 612 613 mVssRays.PrintStatistics(mStats); 614 mViewCellsManager->PrintPvsStatistics(mStats); 615 527 616 VssRayContainer selectedRays; 528 617 int desired = Max( 529 618 mViewCellsManager->GetPostProcessSamples(), 530 619 mViewCellsManager->GetVisualizationSamples()); 531 float p = desired/(float)mVssRays.size(); 532 533 for (int i=0; i < mVssRays.size(); i++) { 534 if (Random(1.0f) < p) 535 selectedRays.push_back(mVssRays[i]); 536 } 620 621 mVssRays.SelectRays(desired, selectedRays); 622 537 623 //-- post process view cells 538 624 mViewCellsManager->PostProcess(mObjects, selectedRays); … … 547 633 548 634 635 636 rssPass++; 637 549 638 rssTree = new RssTree; 550 639 rssTree->SetPass(mPass); 640 551 641 if (mUseImportanceSampling) { 552 642 … … 555 645 else 556 646 rssTree->Construct(mVssRays, NULL); 647 648 rssTree->stat.Print(mStats); 557 649 cout<<"RssTree root PVS size = "<<rssTree->GetRootPvsSize()<<endl; 558 650 … … 572 664 // viewcells->UpdatePVS(newVssRays); 573 665 574 int rssPass = 0; 575 int rssSamples = 0; 666 576 667 while (1) { 577 668 int num = mRssSamplesPerPass; 578 669 SimpleRayContainer rays; 579 670 VssRayContainer vssRays; 580 671 672 581 673 if (!mUseImportanceSampling) { 582 674 for (int j=0; j < num; j++) { … … 597 689 598 690 599 totalSamples+=num; 600 rssSamples+=num; 601 691 totalSamples += rays.size(); 692 rssSamples += vssRays.size(); 693 694 695 602 696 mStats << 603 697 "#Pass\n" <<mPass<<endl<< … … 607 701 "#RssSamples\n" <<rssSamples<<endl; 608 702 703 609 704 if (mUseViewcells) { 610 705 … … 616 711 } 617 712 618 // add rays to the tree after the viewcells have been cast to have their contributions 619 // already when adding into the tree 620 // do not add those rays which have too low or no contribution.... 621 622 if (mUseImportanceSampling) { 623 rssTree->AddRays(vssRays); 624 625 if (0) { 626 cout<<"############# Rss PVS STAT ##################\n"; 627 cout<<"#AVG_RSS_PVS\n"<<rssTree->GetAvgPvsSize()<<endl; 628 cout<<"#RSS_ROOT_PVS\n"<<rssTree->GetRootPvsSize()<<endl; 629 } 630 631 if (mUpdateSubdivision) { 632 int subdivided = rssTree->UpdateSubdivision(); 633 cout<<"subdivided leafs = "<<subdivided<<endl; 634 cout<<"#total leaves = "<<rssTree->stat.Leaves()<<endl; 635 } 636 } 637 713 714 ComputeRenderError(); 715 716 // epxort rays before adding them to the tree -> some of them can be deleted 717 638 718 if (mExportRays) { 639 719 char filename[64]; … … 644 724 645 725 ExportRays(filename, vssRays, mExportNumRays); 646 } 726 727 // now export all contributing rays 728 VssRayContainer contributingRays; 729 vssRays.GetContributingRays(contributingRays, mPass); 730 mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<contributingRays.size()<<endl; 731 sprintf(filename, "rss-crays-%04d.x3d", rssPass); 732 ExportRays(filename, contributingRays, mExportNumRays); 733 } 734 735 736 // add rays to the tree after the viewcells have been cast to have their contributions 737 // already when adding into the tree 738 // do not add those rays which have too low or no contribution.... 739 740 if (mUseImportanceSampling) { 741 rssTree->AddRays(vssRays); 742 743 if (0) { 744 cout<<"############# Rss PVS STAT ##################\n"; 745 cout<<"#AVG_RSS_PVS\n"<<rssTree->GetAvgPvsSize()<<endl; 746 cout<<"#RSS_ROOT_PVS\n"<<rssTree->GetRootPvsSize()<<endl; 747 } 748 749 750 if (mUpdateSubdivision) { 751 int updatePasses = 1; 752 if (mPass % updatePasses == 0) { 753 int subdivided = rssTree->UpdateSubdivision(); 754 cout<<"subdivided leafs = "<<subdivided<<endl; 755 cout<<"#total leaves = "<<rssTree->stat.Leaves()<<endl; 756 } 757 } 758 } 759 647 760 648 761 … … 653 766 } 654 767 768 769 if (!mUseImportanceSampling) 770 CLEAR_CONTAINER(vssRays); 771 655 772 if (totalSamples >= mRssSamples + mInitialSamples) 656 773 break; … … 659 776 rssPass++; 660 777 mPass++; 778 rssTree->SetPass(mPass); 661 779 } 662 780
Note: See TracChangeset
for help on using the changeset viewer.