- Timestamp:
- 01/21/06 18:32:30 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp
r556 r563 42 42 environment->GetBoolValue("RssPreprocessor.loadInitialSamples", mLoadInitialSamples); 43 43 environment->GetBoolValue("RssPreprocessor.storeInitialSamples", mStoreInitialSamples); 44 environment->GetBoolValue("RssPreprocessor.updateSubdivision", mUpdateSubdivision); 44 45 45 46 mStats.open("stats.log"); 46 47 } 47 mRssTree = NULL; 48 } 49 50 51 bool 52 RssPreprocessor::GenerateRays( 53 const int number, 54 const int sampleType, 55 SimpleRayContainer &rays 56 ) 57 { 58 bool result = false; 59 60 switch (sampleType) { 61 case RSS_BASED_DISTRIBUTION: 62 case RSS_SILHOUETTE_BASED_DISTRIBUTION: 63 if (mRssTree) { 64 GenerateImportanceRays(mRssTree, number, rays); 65 result = true; 66 rays.NormalizePdf(); 67 } 68 break; 69 default: 70 result = Preprocessor::GenerateRays(number, sampleType, rays); 71 } 72 73 return result; 74 } 75 76 void 77 RssPreprocessor::CastRays( 78 SimpleRayContainer &rays, 79 VssRayContainer &vssRays 80 ) 81 { 82 for (int i=0; i < rays.size(); i++) 83 CastRay(rays[i].mOrigin, rays[i].mDirection, rays[i].mPdf, vssRays); 84 } 85 48 86 49 87 RssPreprocessor::~RssPreprocessor() … … 190 228 } 191 229 192 Vector3193 RssPreprocessor::GetDirection(const Vector3 &viewpoint,194 AxisAlignedBox3 *viewSpaceBox195 )196 {197 Vector3 point;198 if (!use2dSampling) {199 if (mObjectBasedSampling) {200 Vector3 normal;201 int i = RandomValue(0, mObjects.size()-1);202 Intersectable *object = mObjects[i];203 object->GetRandomSurfacePoint(point, normal);204 } else {205 // select206 if (mDirectionalSampling) {207 point = viewpoint + UniformRandomVector();208 } else {209 // select the other point uniformly distruted in the whole viewspace210 AxisAlignedBox3 box;211 212 if (viewSpaceBox)213 box =*viewSpaceBox;214 else215 box = mKdTree->GetBox();216 217 point = box.GetRandomPoint();218 }219 }220 } else {221 AxisAlignedBox3 box;222 223 if (viewSpaceBox)224 box =*viewSpaceBox;225 else226 box = mKdTree->GetBox();227 228 point = box.GetRandomPoint();229 point.y = viewpoint.y;230 }231 232 return point - viewpoint;233 }234 235 Vector3236 RssPreprocessor::InitialGetDirection(const Vector3 &viewpoint,237 AxisAlignedBox3 *viewSpaceBox238 )239 {240 Vector3 point;241 if (!use2dSampling) {242 if (1 || mObjectBasedSampling) {243 Vector3 normal;244 int i = RandomValue(0, mObjects.size()-1);245 Intersectable *object = mObjects[i];246 object->GetRandomSurfacePoint(point, normal);247 } else {248 // select249 if (1 || mDirectionalSampling) {250 point = viewpoint + UniformRandomVector();251 } else {252 // select the other point uniformly distruted in the whole viewspace253 AxisAlignedBox3 box;254 255 if (viewSpaceBox)256 box =*viewSpaceBox;257 else258 box = mKdTree->GetBox();259 260 point = box.GetRandomPoint();261 }262 }263 } else {264 AxisAlignedBox3 box;265 266 if (viewSpaceBox)267 box =*viewSpaceBox;268 else269 box = mKdTree->GetBox();270 271 point = box.GetRandomPoint();272 point.y = viewpoint.y;273 }274 275 return point - viewpoint;276 }277 230 278 231 int … … 302 255 } 303 256 304 cout<<"Generated "<<num<<" rays."<<endl;305 257 306 258 return num; … … 322 274 exporter->SetFilled(); 323 275 // $$JB temporarily do not export the scene 324 if ( 1)276 if (0) 325 277 exporter->ExportScene(mSceneGraph->mRoot); 326 278 exporter->SetWireframe(); … … 474 426 } 475 427 428 429 476 430 bool 477 431 RssPreprocessor::ComputeVisibility() 478 432 { 479 433 480 if (renderer)481 renderer->InitGL();482 483 434 // connect(this, SIGNAL(EvalPvsStat()), renderer, SLOT(EvalPvsStat()) ); 484 435 … … 493 444 int totalSamples = 0; 494 445 495 AxisAlignedBox3 *box = new AxisAlignedBox3(mKdTree->GetBox()); 496 497 if (fromBoxVisibility) { 498 float m = box->Min(1); 499 float bsize = box->Size(1); 500 501 float size = 0.02f; 502 float s = 0.5f - size; 503 float olds = Magnitude(box->Size()); 504 box->Enlarge(box->Size()*Vector3(-s)); 505 // Vector3 translation = Vector3(-olds*0.2f, 0, 0); 506 Vector3 translation = Vector3(-0.05f*olds, 0, 0); 507 box->SetMin(box->Min() + translation); 508 box->SetMax(box->Max() + translation); 509 510 box->SetMin(1, m + bsize*0.1f); 511 box->SetMax(1, m + bsize*0.6f); 512 513 514 } else { 515 516 // sample city like heights 517 float m = box->Min(1); 518 float bsize = box->Size(1); 519 box->SetMin(1, m + bsize*0.2f); 520 box->SetMax(1, m + bsize*0.3f); 521 } 522 523 if (use2dSampling) 524 box->SetMax(1, box->Min(1)); 525 526 if (useViewSpaceBox) 527 { 528 mViewSpaceBox = box; 529 mViewCellsManager->SetViewSpaceBox(*box); 530 } 531 else 532 { 533 mViewSpaceBox = NULL; 534 mViewCellsManager->SetViewSpaceBox(mKdTree->GetBox()); 535 } 446 mViewSpaceBox = NULL; 447 mViewCellsManager->SetViewSpaceBox(mKdTree->GetBox()); 448 536 449 537 450 //-- load view cells from file if requested 538 if (mLoadViewCells) 539 { 540 // load now because otherwise bounding box not correct 541 mViewCellsManager->LoadViewCells(mViewCellsFilename, &mObjects); 542 } 543 544 RssTree *rssTree = NULL; 451 if (mLoadViewCells) { 452 // load now because otherwise bounding box not correct 453 mViewCellsManager->LoadViewCells(mViewCellsFilename, &mObjects); 454 } 455 545 456 int rssPass = 0; 546 457 int rssSamples = 0; 547 548 #if 0 458 549 459 if (mLoadInitialSamples) { 550 460 cout << "Loading samples from file ... "; … … 552 462 cout << "finished\n" << endl; 553 463 } else { 554 #endif555 while (totalSamples < mInitialSamples) {556 int passContributingSamples = 0;557 int passSampleContributions = 0;558 int passSamples = 0;559 int index = 0;560 561 int sampleContributions;562 563 //int s = Min(mSamplesPerPass, mInitialSamples);564 int s = mInitialSamples;565 566 float probability = 1.0f/mInitialSamples;567 for (int k=0; k < s; k++) {568 569 570 //Vector3 viewpoint = GetViewpoint(mViewSpaceBox);571 Vector3 viewpoint;572 mViewCellsManager->GetViewPoint(viewpoint);573 // viewpoint = GetViewpoint(mViewSpaceBox);574 Vector3 direction = InitialGetDirection(viewpoint, mViewSpaceBox);575 576 sampleContributions = CastRay(viewpoint, direction, probability, mVssRays);577 578 579 //-- CORR matt: put block inside loop580 if (sampleContributions) {581 passContributingSamples ++;582 passSampleContributions += sampleContributions;583 }584 passSamples++;585 totalSamples++;586 }587 588 589 float avgRayContrib = (passContributingSamples > 0) ?590 passSampleContributions/(float)passContributingSamples : 0;591 592 cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;593 cout << "#TotalSamples=" << totalSamples/1000594 << "k #SampleContributions=" << passSampleContributions << " ("595 << 100*passContributingSamples/(float)passSamples<<"%)"596 << "avgcontrib=" << avgRayContrib << endl;597 598 mPass++;599 }600 601 cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;602 cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush;603 604 rssSamples += (int)mVssRays.size();605 606 607 if (mExportRays) {608 char filename[64];609 sprintf(filename, "rss-rays-initial.x3d");610 ExportRays(filename, mVssRays, mExportNumRays);611 }612 613 if (mStoreInitialSamples)614 {615 cout << "Writing samples to file ... ";616 ExportSamples(mVssRays);617 cout << "finished\n" << endl;618 }619 620 if (mUseViewcells) {621 // construct view cells622 if (!mLoadViewCells)623 mViewCellsManager->Construct(mObjects, mVssRays);624 // evaluate contributions of the intitial rays625 mViewCellsManager->ComputeSampleContributions(mVssRays);626 627 628 mStats <<629 "#Pass\n" <<mPass<<endl<<630 "#RssPass\n" <<rssPass<<endl<<631 "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<632 "#TotalSamples\n" <<totalSamples<<endl<<633 "#RssSamples\n" <<rssSamples<<endl;634 635 636 mVssRays.PrintStatistics(mStats);637 mViewCellsManager->PrintPvsStatistics(mStats);638 639 VssRayContainer selectedRays;640 int desired = Max(641 mViewCellsManager->GetPostProcessSamples(),642 mViewCellsManager->GetVisualizationSamples());643 644 mVssRays.SelectRays(desired, selectedRays);645 646 //-- post process view cells647 mViewCellsManager->PostProcess(mObjects, selectedRays);648 649 //-- several visualizations and statistics650 Debug << "view cells after post processing: " << endl;651 mViewCellsManager->PrintStatistics(Debug);652 653 if (1)654 mViewCellsManager->Visualize(mObjects, selectedRays);655 656 ComputeRenderError();657 658 }659 660 661 662 rssPass++;663 664 rssTree = new RssTree;665 rssTree->SetPass(mPass);666 667 /// compute view cell contribution of rays if view cells manager already constructed668 mViewCellsManager->ComputeSampleContributions(mVssRays);669 670 if (mUseImportanceSampling) {671 672 // if (fromBoxVisibility)673 // rssTree->Construct(mObjects, mVssRays, mViewSpaceBox);674 // else675 rssTree->Construct(mObjects, mVssRays);676 677 rssTree->stat.Print(mStats);678 cout<<"RssTree root PVS size = "<<rssTree->GetRootPvsSize()<<endl;679 680 if (mExportRssTree) {681 ExportRssTree("rss-tree-100.x3d", rssTree, Vector3(1,0,0));682 ExportRssTree("rss-tree-001.x3d", rssTree, Vector3(0,0,1));683 ExportRssTree("rss-tree-101.x3d", rssTree, Vector3(1,0,1));684 ExportRssTree("rss-tree-101m.x3d", rssTree, Vector3(-1,0,-1));685 ExportRssTreeLeaves(rssTree, 10);686 }687 688 if (mExportPvs) {689 ExportPvs("rss-pvs-initial.x3d", rssTree);690 }691 }692 693 // viewcells->UpdatePVS(newVssRays);694 695 696 while (1) {697 int num = mRssSamplesPerPass;698 464 SimpleRayContainer rays; 699 VssRayContainer vssRays; 700 701 702 if (!mUseImportanceSampling) { 703 for (int j=0; j < num; j++) { 704 //changed by matt 705 //Vector3 viewpoint = GetViewpoint(mViewSpaceBox); 706 Vector3 viewpoint; 707 mViewCellsManager->GetViewPoint(viewpoint); 708 Vector3 direction = GetDirection(viewpoint, mViewSpaceBox); 709 rays.push_back(SimpleRay(viewpoint, direction, 1.0f)); 710 rays.mSumPdf += 1.0f; 711 } 712 } else { 713 num = GenerateImportanceRays(rssTree, num, rays); 714 } 715 716 rays.NormalizePdf(); 717 718 for (int i=0; i < rays.size(); i++) 719 CastRay(rays[i].mOrigin, rays[i].mDirection, rays[i].mPdf, vssRays); 720 721 722 totalSamples += (int)rays.size(); 723 rssSamples += (int)vssRays.size(); 724 465 466 GenerateRays(mInitialSamples, SPATIAL_BOX_BASED_DISTRIBUTION, rays); 467 468 CastRays(rays, mVssRays); 469 } 470 471 cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush; 472 473 rssSamples += (int)mVssRays.size(); 474 totalSamples += mInitialSamples; 475 mPass++; 476 477 if (mExportRays) { 478 char filename[64]; 479 sprintf(filename, "rss-rays-initial.x3d"); 480 ExportRays(filename, mVssRays, mExportNumRays); 481 } 482 483 if (mStoreInitialSamples) { 484 cout << "Writing samples to file ... "; 485 ExportSamples(mVssRays); 486 cout << "finished\n" << endl; 487 } 488 489 if (mUseViewcells) { 490 // construct view cells 491 if (!mLoadViewCells) 492 mViewCellsManager->Construct(mObjects, mVssRays); 493 494 495 496 // evaluate contributions of the intitial rays 497 mViewCellsManager->ComputeSampleContributions(mVssRays); 725 498 726 499 … … 731 504 "#TotalSamples\n" <<totalSamples<<endl<< 732 505 "#RssSamples\n" <<rssSamples<<endl; 506 507 508 mVssRays.PrintStatistics(mStats); 509 mViewCellsManager->PrintPvsStatistics(mStats); 510 511 VssRayContainer selectedRays; 512 int desired = Max( 513 mViewCellsManager->GetPostProcessSamples(), 514 mViewCellsManager->GetVisualizationSamples()); 515 516 mVssRays.SelectRays(desired, selectedRays); 517 518 //-- post process view cells 519 mViewCellsManager->PostProcess(mObjects, selectedRays); 520 521 //-- several visualizations and statistics 522 Debug << "view cells after post processing: " << endl; 523 mViewCellsManager->PrintStatistics(Debug); 524 525 if (1) 526 mViewCellsManager->Visualize(mObjects, selectedRays); 527 528 ComputeRenderError(); 529 } 530 531 rssPass++; 532 533 mRssTree = new RssTree; 534 mRssTree->SetPass(mPass); 535 536 /// compute view cell contribution of rays if view cells manager already constructed 537 mViewCellsManager->ComputeSampleContributions(mVssRays); 538 539 if (mUseImportanceSampling) { 540 541 mRssTree->Construct(mObjects, mVssRays); 542 543 mRssTree->stat.Print(mStats); 544 cout<<"RssTree root PVS size = "<<mRssTree->GetRootPvsSize()<<endl; 545 546 if (mExportRssTree) { 547 ExportRssTree("rss-tree-100.x3d", mRssTree, Vector3(1,0,0)); 548 ExportRssTree("rss-tree-001.x3d", mRssTree, Vector3(0,0,1)); 549 ExportRssTree("rss-tree-101.x3d", mRssTree, Vector3(1,0,1)); 550 ExportRssTree("rss-tree-101m.x3d", mRssTree, Vector3(-1,0,-1)); 551 ExportRssTreeLeaves(mRssTree, 10); 552 } 553 554 if (mExportPvs) { 555 ExportPvs("rss-pvs-initial.x3d", mRssTree); 556 } 557 } 558 559 // viewcells->UpdatePVS(newVssRays); 560 561 562 while (1) { 563 SimpleRayContainer rays; 564 VssRayContainer vssRays; 565 int castRays = 0; 566 if (mUseImportanceSampling) { 567 VssRayContainer tmpVssRays; 568 569 GenerateRays(mRssSamplesPerPass/2, RSS_BASED_DISTRIBUTION, rays); 570 CastRays(rays, tmpVssRays); 571 castRays += rays.size(); 572 float c1 = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false); 573 mStats<<"#RssRelContrib"<<endl<<c1/rays.size()<<endl; 574 575 vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() ); 576 rays.clear(); 577 tmpVssRays.clear(); 578 579 GenerateRays(mRssSamplesPerPass/4, SPATIAL_BOX_BASED_DISTRIBUTION, rays); 580 CastRays(rays, tmpVssRays); 581 castRays += rays.size(); 582 float c2 = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false); 583 mStats<<"#SpatialRelContrib"<<endl<<c2/rays.size()<<endl; 584 585 vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() ); 586 587 rays.clear(); 588 tmpVssRays.clear(); 589 590 591 GenerateRays(mRssSamplesPerPass/4, DIRECTION_BASED_DISTRIBUTION, rays); 592 CastRays(rays, tmpVssRays); 593 castRays += rays.size(); 594 float c3 = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false); 595 mStats<<"#DirectionalRelContrib"<<endl<<c3/rays.size()<<endl; 596 597 vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() ); 598 599 rays.clear(); 600 tmpVssRays.clear(); 601 602 // add contributions of all rays at once... 603 mViewCellsManager->AddSampleContributions(vssRays); 604 605 } 606 else { 607 int rayType = SPATIAL_BOX_BASED_DISTRIBUTION; 608 if (mObjectBasedSampling) 609 rayType = OBJECT_BASED_DISTRIBUTION; 610 else 611 if (mDirectionalSampling) 612 rayType = DIRECTION_BASED_DISTRIBUTION; 613 614 GenerateRays(mRssSamplesPerPass, rayType, rays); 615 CastRays(rays, vssRays); 616 castRays += rays.size(); 617 if (mUseViewcells) { 618 /// compute view cell contribution of rays 619 mViewCellsManager->ComputeSampleContributions(vssRays); 620 } 621 622 623 } 624 totalSamples += castRays; 625 rssSamples += (int)vssRays.size(); 626 627 cout<<"Generated "<<castRays<<" rays, progress "<<totalSamples/((float) mRssSamples + 628 mInitialSamples)<<"%\n"; 629 630 631 mStats << 632 "#Pass\n" <<mPass<<endl<< 633 "#RssPass\n" <<rssPass<<endl<< 634 "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<< 635 "#TotalSamples\n" <<totalSamples<<endl<< 636 "#RssSamples\n" <<rssSamples<<endl; 733 637 734 638 735 639 if (mUseViewcells) { 736 737 /// compute view cell contribution of rays738 mViewCellsManager->ComputeSampleContributions(vssRays);739 740 640 vssRays.PrintStatistics(mStats); 741 641 mViewCellsManager->PrintPvsStatistics(mStats); … … 770 670 771 671 if (mUseImportanceSampling) { 772 rssTree->AddRays(vssRays); 773 774 if (0) { 775 cout<<"############# Rss PVS STAT ##################\n"; 776 cout<<"#AVG_RSS_PVS\n"<<rssTree->GetAvgPvsSize()<<endl; 777 cout<<"#RSS_ROOT_PVS\n"<<rssTree->GetRootPvsSize()<<endl; 778 } 779 672 mRssTree->AddRays(vssRays); 780 673 781 674 if (mUpdateSubdivision) { 782 675 int updatePasses = 1; 783 676 if (mPass % updatePasses == 0) { 784 int subdivided = rssTree->UpdateSubdivision();677 int subdivided = mRssTree->UpdateSubdivision(); 785 678 cout<<"subdivided leafs = "<<subdivided<<endl; 786 cout<<"#total leaves = "<< rssTree->stat.Leaves()<<endl;679 cout<<"#total leaves = "<<mRssTree->stat.Leaves()<<endl; 787 680 } 788 681 } 789 682 } 790 791 792 683 793 684 if (mExportPvs) { 794 685 char filename[64]; 795 686 sprintf(filename, "rss-pvs-%04d.x3d", rssPass); 796 ExportPvs(filename, rssTree);797 } 798 687 ExportPvs(filename, mRssTree); 688 } 689 799 690 800 691 if (!mUseImportanceSampling) 801 692 CLEAR_CONTAINER(vssRays); 693 // otherwise the rays get deleted by the rss tree update according to RssTree.maxRays .... 802 694 803 695 if (totalSamples >= mRssSamples + mInitialSamples) … … 807 699 rssPass++; 808 700 mPass++; 809 rssTree->SetPass(mPass);701 mRssTree->SetPass(mPass); 810 702 } 811 703 … … 825 717 826 718 } 827 828 cerr<<"Deleting RSS tree...\n";829 delete rssTree;830 cerr<<"Done.\n";719 720 Debug<<"Deleting RSS tree...\n"; 721 delete mRssTree; 722 Debug<<"Done.\n"; 831 723 832 724
Note: See TracChangeset
for help on using the changeset viewer.