Ignore:
Timestamp:
11/01/05 02:37:51 (19 years ago)
Author:
mattausch
Message:

added post merging bsp view cells
fixed bug at per ray subdivision

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp

    r361 r362  
    3232{ 
    3333  ray.intersections.clear(); 
    34   ray.leaves.clear(); 
     34  ray.kdLeaves.clear(); 
    3535  ray.meshes.clear(); 
    36   ray.viewCells.clear(); 
     36  ray.bspLeaves.clear(); 
    3737 
    3838  //  cout<<point<<" "<<direction<<endl; 
     
    6868                mSceneGraph->CollectObjects(&objects); 
    6969                mBspTree->Construct(objects); 
    70                  
    71                 mBspTree->CollectViewCells(mViewCells); 
    7270                break; 
    7371        case BspTree::FROM_RAYS: 
     
    7573                mBspTree->SetGenerateViewCells(true); 
    7674                mBspTree->Construct(mSampleRays); 
    77                  
    78                 mBspTree->CollectViewCells(mViewCells); 
    7975                break; 
    8076        default: 
     
    9490  int contributingSamples = 0; 
    9591  int j; 
    96   for (j=0; j < ray.leaves.size(); j++) { 
    97     KdNode *node = GetNodeForPvs( ray.leaves[j] ); 
     92  for (j=0; j < ray.kdLeaves.size(); j++) { 
     93    KdNode *node = GetNodeForPvs( ray.kdLeaves[j] ); 
    9894    contributingSamples += object->mKdPvs.AddSample(node); 
    9995  } 
    10096    
    10197  if (mPass > 10) 
    102     for (j=1; j < ((int)ray.leaves.size() - 1); j++) { 
    103       ray.leaves[j]->AddPassingRay(ray, contributingSamples ? 1 : 0); 
     98    for (j=1; j < ((int)ray.kdLeaves.size() - 1); j++) { 
     99      ray.kdLeaves[j]->AddPassingRay(ray, contributingSamples ? 1 : 0); 
    104100  } 
    105101   
     
    114110  
    115111        // object can be seen from the view cell => add to view cell pvs 
    116         for (j=0; j < ray.viewCells.size(); ++ j)  
     112        for (j=0; j < ray.bspLeaves.size(); ++ j)  
    117113        {       // if ray not in unbounded space 
    118                 if (ray.viewCells[j] != &mUnbounded) 
    119                         contributingSamples += ray.viewCells[j]->GetPvs().AddSample(obj); 
     114                if (ray.bspLeaves[j]->GetViewCell() != &mUnbounded) 
     115                        contributingSamples +=  
     116                                ray.bspLeaves[j]->GetViewCell()->GetPvs().AddSample(obj); 
    120117        } 
    121118  
    122119        // rays passing through this viewcell 
    123120        if (mPass > 1) 
    124                 for (j=1; j < ((int)ray.viewCells.size() - 1); ++ j)  
    125                 { 
    126                         if (ray.viewCells[j] != &mUnbounded) 
    127                                 ray.viewCells[j]->AddPassingRay(ray, contributingSamples ? 1 : 0); 
     121                for (j=1; j < ((int)ray.bspLeaves.size() - 1); ++ j)  
     122                { 
     123                        if (ray.bspLeaves[j]->GetViewCell() != &mUnbounded) 
     124                                ray.bspLeaves[j]->GetViewCell()-> 
     125                                        AddPassingRay(ray, contributingSamples ? 1 : 0); 
    128126                } 
    129127  
     
    183181        else 
    184182                { 
    185                         if (ray.leaves.size()) { 
     183                        if (ray.kdLeaves.size()) { 
    186184                                if (!reverseRay) 
    187185                                        sampleContributions += AddNodeSamples(object, ray); 
     
    372370 
    373371  vector<Ray> rays[10]; 
    374  
    375   vector<Ray> vcRays[5]; 
    376   ViewCellContainer pvsViewCells; 
    377   vector<Ray> viewCellRays; // used for BSP tree construction 
    378372 
    379373  while (totalSamples < mTotalSamples) { 
     
    462456                                        } 
    463457                                        //------------------- 
    464  
    465458                                        if (mViewCellsType == BSP_VIEW_CELLS)  
    466459                                        { 
    467                                                 // save rays for bsp tree construction 
    468                                                 if (collectSamplesForBsp) 
    469                                                 { 
    470                                                         // also add origin to sample in order to extract it as input polygons 
    471                                                         MeshInstance *mi = dynamic_cast<MeshInstance *>(object); 
    472                                                         ray.sourceObject = Ray::Intersection(0.0, mi, faceIndex); 
    473                                                          
    474                             mSampleRays.push_back(new Ray(ray)); 
    475                                                 } 
    476                                                 else 
    477                                                 { 
    478                                                         // construct BSP tree using the samples 
    479                                                         if (!mBspTree) 
    480                                                         { 
    481                                                                 BuildBspTree(); 
    482  
    483                                                                 cout << "generated " << (int)mViewCells.size() << " view cells" << endl; 
    484  
    485                                                                 passContributingSamples += mBspTree->GetStat().contributingSamples; 
    486                                                                 passSampleContributions += mBspTree->GetStat().sampleContributions; 
    487  
    488                                                                 BspTreeStatistics(Debug);        
    489                                                                 Export("vc_bsptree.x3d", false, false, true); 
    490                                                         } 
    491                                                                  
    492                                                         // some random view cells for output 
    493                                                         if (pvsViewCells.empty()) 
    494                                                         { 
    495                                                                 int vcPvsOut = Min((int)mViewCells.size(), 5); 
    496                                                          
    497                                                                 for (int j = 0; j < vcPvsOut; ++ j) 
    498                                                                 { 
    499                                                                         int idx = Random((int)mViewCells.size()); 
    500                                                                         Debug << "output view cell=" << idx << endl; 
    501                                                                         pvsViewCells.push_back(mViewCells[Random((int)mViewCells.size())]); 
    502                                                                 } 
    503                                                         } 
    504                                                         else 
    505                                                         {        
    506                                                                 // check whether we can add the current ray to the rays 
    507                                                                 for (int k = 0; k < (int)ray.viewCells.size(); ++ k) 
    508                                                                         for (int j = 0; j < (int)pvsViewCells.size(); ++ j)  
    509                                                                                 if (pvsViewCells[j] == ray.viewCells[k])  
    510                                                                                         vcRays[j].push_back(ray);                                                        
    511                                                         } 
    512                                                 } 
     460                                                ProcessBspViewCells(collectSamplesForBsp, 
     461                                                                                        ray, 
     462                                                                                        object, 
     463                                                                                        faceIndex, 
     464                                                                                        passContributingSamples, 
     465                                                                                        passSampleContributions); 
    513466                                        } 
    514                  
    515467                                } 
    516468                        } else { 
     
    580532                cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl; 
    581533   
     534        // merge or subdivide view cells 
     535        if (mBspTree) 
     536        { 
     537                int merged = PostprocessViewCells(mSampleRays); 
     538                Debug << "merged " << merged << " view cells" << endl; 
     539 
     540                ViewCellContainer vc; 
     541                mBspTree->CollectViewCells(vc); 
     542 
     543                ViewCellContainer::const_iterator it, it_end = vc.end(); 
     544 
     545                int mergedPvs = 0; 
     546                for (it = vc.begin(); it != it_end; ++ it) 
     547                        mergedPvs += (*it)->GetPvs().GetSize(); 
     548 
     549                Debug << "merged pvs size: " << mergedPvs << endl; 
     550 
     551        } 
     552         
    582553  //  HoleSamplingPass(); 
    583   if (0) { 
    584     Exporter *exporter = Exporter::GetExporter("ray-density.x3d"); 
    585     exporter->SetExportRayDensity(true); 
    586     exporter->ExportKdTree(*mKdTree); 
    587  
     554        if (0) { 
     555                Exporter *exporter = Exporter::GetExporter("ray-density.x3d");  
     556                exporter->SetExportRayDensity(true); 
     557                exporter->ExportKdTree(*mKdTree); 
     558 
     559                if (mBspTree && (mViewCellsType == BSP_VIEW_CELLS)) 
     560                        exporter->ExportBspTree(*mBspTree); 
     561 
     562                delete exporter; 
     563        } 
     564   
     565        bool exportRays = false; 
     566        if (exportRays) { 
     567                Exporter *exporter = NULL; 
     568                exporter = Exporter::GetExporter("sample-rays.x3d"); 
     569                exporter->SetWireframe(); 
     570                exporter->ExportKdTree(*mKdTree); 
     571                exporter->ExportBspTree(*mBspTree); 
     572 
     573                for (i=0; i < pvsOut; i++)  
     574                exporter->ExportRays(rays[i], 1000, RgbColor(1, 0, 0)); 
     575                exporter->SetFilled(); 
     576                   
     577                delete exporter; 
     578        } 
     579 
     580        //-- several visualizations and statistics 
     581        if (1) { 
    588582        if (mBspTree && (mViewCellsType == BSP_VIEW_CELLS)) 
    589                 exporter->ExportBspTree(*mBspTree); 
    590  
    591     delete exporter; 
    592   } 
    593    
    594   bool exportRays = false; 
    595   if (exportRays) { 
    596     Exporter *exporter = NULL; 
    597     exporter = Exporter::GetExporter("sample-rays.x3d"); 
    598     exporter->SetWireframe(); 
    599     exporter->ExportKdTree(*mKdTree); 
    600         exporter->ExportBspTree(*mBspTree); 
    601  
    602     for (i=0; i < pvsOut; i++)  
    603       exporter->ExportRays(rays[i], 1000, RgbColor(1, 0, 0)); 
    604     exporter->SetFilled(); 
    605            
    606     delete exporter; 
    607   } 
    608  
    609   //-- several visualizations and statistics 
    610   if (1) { 
    611   if (mBspTree && (mViewCellsType == BSP_VIEW_CELLS)) 
    612   { 
    613           bool exportSplits = false; 
    614           environment->GetBoolValue("BspTree.exportSplits", exportSplits); 
    615  
    616           // export the bsp splits 
    617           if (exportSplits) 
    618                   ExportSplits(objects); 
    619          
    620           Exporter *exporter = Exporter::GetExporter("viewCells.x3d"); 
    621  
    622           // export leaves 
    623           if (0 && exporter) 
    624           { 
    625                   vector<BspLeaf *> leaves; 
    626                   mBspTree->CollectLeaves(leaves); 
    627                   vector<BspLeaf *> randLeaves; 
    628                   vector<BspLeaf *> neighbors; 
    629  
    630                   for (int k = 0; k < 1; ++ k)  
    631                   { 
    632                           BspLeaf *leaf = leaves[Random((int)leaves.size())]; 
    633                           randLeaves.push_back(leaf); 
    634                           mBspTree->FindNeighbors(leaf, neighbors, false); 
    635                   }      
    636  
    637                   exporter->ExportLeavesGeometry(*mBspTree, randLeaves); 
    638                   exporter->SetWireframe(); 
    639           exporter->ExportLeavesGeometry(*mBspTree, neighbors);  
    640                          
    641           delete exporter; 
    642           } 
    643  
    644            vector<BspLeaf *> leaves; 
    645            mBspTree->CollectLeaves(leaves); 
    646             
    647            for (int k = 0; k < 100; ++ k)  
    648            { 
    649                    vector<BspLeaf *> randLeaves; 
    650                    vector<BspLeaf *> neighbors; 
    651                     
    652                    char s[64]; sprintf(s, "bsp-vc%04d.x3d", k); 
    653                    Exporter *exporter = Exporter::GetExporter(s); 
    654  
    655                    BspLeaf *leaf = leaves[Random((int)leaves.size())]; 
    656                    Debug << "\noutput leaf " << k << endl; 
    657                    randLeaves.push_back(leaf); 
    658                    mBspTree->FindNeighbors(leaf, neighbors, false); 
    659  
    660                    exporter->ExportLeavesGeometry(*mBspTree, randLeaves); 
    661                    exporter->SetWireframe(); 
    662                    exporter->ExportLeavesGeometry(*mBspTree, neighbors);         
    663                          
    664                    delete exporter; 
    665            }     
    666  
    667           for (int j = 0; j < pvsViewCells.size(); ++ j) 
    668           { 
    669                   ViewCell *vc = pvsViewCells[j]; 
    670  
    671                   Intersectable::NewMail(); 
    672                   char s[64]; sprintf(s, "bsp-pvs%04d.x3d", j); 
    673  
    674                   Exporter *exporter = Exporter::GetExporter(s); 
    675                   exporter->SetFilled(); 
    676  
    677                   ViewCellPvsMap::iterator it = vc->GetPvs().mEntries.begin(); 
    678  
    679                   Material m;//= RandomMaterial(); 
    680                   m.mDiffuseColor = RgbColor(0, 1, 0); 
    681                   exporter->SetForcedMaterial(m); 
    682  
    683                   exporter->ExportViewCell(vc); 
    684  
    685                   Debug << j << ": pvs size=" << (int)vc->GetPvs().GetSize()  
    686                             << ", piercing rays=" << (int)vcRays[j].size() << endl; 
    687  
    688                   exporter->SetWireframe(); 
    689  
    690                   // export view cells 
    691                   m.mDiffuseColor = RgbColor(1, 0, 1); 
    692                   exporter->SetForcedMaterial(m); 
    693                   exporter->ExportViewCells(mViewCells); 
    694                          
    695                   // export rays piercing this view cell 
    696                   exporter->ExportRays(vcRays[j], 1000, RgbColor(0, 1, 0)); 
    697  
    698                   m.mDiffuseColor = RgbColor(1, 0, 0); 
    699                   exporter->SetForcedMaterial(m); 
    700  
    701                   // output PVS of view cell 
    702                   for (; it != vc->GetPvs().mEntries.end(); ++ it)  
    703                   { 
    704                           Intersectable *intersect = (*it).first; 
    705                           if (!intersect->Mailed()) 
    706                           { 
    707                                   exporter->ExportIntersectable(intersect); 
    708                                   intersect->Mail(); 
    709                           }                      
    710                   } 
    711                  
    712                   // output rest of the objects 
    713                   if (0) 
    714                   { 
    715                           Material m;//= RandomMaterial(); 
    716                           m.mDiffuseColor = RgbColor(0, 0, 1); 
    717                           exporter->SetForcedMaterial(m); 
    718  
    719                           for (int j = 0; j < objects.size(); ++ j) 
    720                                   if (!objects[j]->Mailed()) 
    721                                   { 
    722                                           //if (j == 2198)m.mDiffuseColor = RgbColor(1, 0, 1); 
    723                                           //else m.mDiffuseColor = RgbColor(1, 1, 0); 
    724                                           exporter->SetForcedMaterial(m); 
    725                                           exporter->ExportIntersectable(objects[j]); 
    726                                           objects[j]->Mail(); 
    727                                   } 
    728                   } 
    729                   DEL_PTR(exporter); 
    730                 } 
    731   }   
    732  
     583        { 
     584                bool exportSplits = false; 
     585                environment->GetBoolValue("BspTree.exportSplits", exportSplits); 
     586 
     587                // export the bsp splits 
     588                if (exportSplits) 
     589                        ExportSplits(objects); 
     590                 
     591                ExportBspPvs(objects); 
     592        } 
    733593   for (int k=0; k < pvsOut; k++) { 
    734594      Intersectable *object = objects[k]; 
     
    772632} 
    773633 
     634bool SamplingPreprocessor::ProcessBspViewCells(bool collectSamplesForBsp, 
     635                                                                                           Ray &ray, 
     636                                                                                           Intersectable *object, 
     637                                                                                           int faceIndex, 
     638                                                                                           int &contributingSamples, 
     639                                                                                           int &sampleContributions) 
     640{ 
     641        // save rays for bsp tree construction 
     642        if (collectSamplesForBsp) 
     643        { 
     644                // also add origin to sample in order to extract it as input polygons 
     645                MeshInstance *mi = dynamic_cast<MeshInstance *>(object); 
     646                ray.sourceObject = Ray::Intersection(0.0, mi, faceIndex); 
     647                                                         
     648        mSampleRays.push_back(new Ray(ray)); 
     649                 
     650                return false; 
     651        } 
     652         
     653        // construct BSP tree using the samples 
     654        if (!mBspTree) 
     655        { 
     656                BuildBspTree(); 
     657                 
     658                contributingSamples += mBspTree->GetStat().contributingSamples; 
     659                sampleContributions += mBspTree->GetStat().sampleContributions; 
     660 
     661                BspTreeStatistics(Debug);        
     662                if (0) Export("vc_bsptree.x3d", false, false, true); 
     663        } 
     664 
     665        return true; 
     666} 
     667 
     668// merge or subdivide view cells 
     669int SamplingPreprocessor::PostprocessViewCells(const RayContainer &rays) 
     670{ 
     671        int merged = 0; 
     672        RayContainer::const_iterator rit, rit_end = rays.end(); 
     673        vector<BspLeaf *>::const_iterator lit; 
     674if (0) 
     675        for (rit = rays.begin(); rit != rays.end(); ++ rit) 
     676        {   
     677                // traverse leaves stored in the rays and compare and merge consecutive 
     678                // leaves (i.e., the neighbors in the tree) 
     679                lit = (*rit)->bspLeaves.begin(); 
     680 
     681                BspLeaf *previousLeaf = *lit; 
     682                ++ lit; 
     683                 
     684                for (; lit != (*rit)->bspLeaves.end(); ++ lit) 
     685                { 
     686                        BspLeaf *leaf = *lit; 
     687                        if (mBspTree->ShouldMerge(leaf, previousLeaf)) 
     688                        {                        
     689                                mBspTree->MergeViewCells(leaf, previousLeaf); 
     690                                ++ merged; 
     691                        } 
     692                        previousLeaf = leaf; 
     693                } 
     694        } 
     695        return merged; 
     696} 
    774697 
    775698void SamplingPreprocessor::ExportSplits(const ObjectContainer &objects) 
     
    829752        } 
    830753} 
     754 
     755void SamplingPreprocessor::ExportBspPvs(const ObjectContainer &objects) 
     756{ 
     757        //-- some random view cells and rays for output 
     758        const int leafOut = 10; 
     759 
     760        vector<Ray> vcRays[leafOut]; 
     761        vector<BspLeaf *> bspLeaves; 
     762                         
     763        for (int i = 0; i < leafOut; ++ i) 
     764                bspLeaves.push_back(mBspTree->GetRandomLeaf()); 
     765         
     766        const int raysOut = min((int)mSampleRays.size(), 20000); 
     767 
     768        // check whether we can add the current ray to the output rays 
     769        for (int k = 0; k < raysOut; ++ k)  
     770        { 
     771                Ray *ray = mSampleRays[k]; 
     772 
     773                for (int j = 0; j < (int)ray->bspLeaves.size(); ++ j) 
     774                { 
     775                        for (int i = 0; i < (int)bspLeaves.size(); ++ i) 
     776                        { 
     777                                if (bspLeaves[i] == ray->bspLeaves[j])  
     778                                { 
     779                                        vcRays[i].push_back(*ray); 
     780                                } 
     781                        } 
     782                } 
     783        } 
     784 
     785        ViewCell::NewMail(); 
     786 
     787        for (int i = 0; i < bspLeaves.size(); ++ i) 
     788        { 
     789                Intersectable::NewMail(); 
     790 
     791                ViewCell *vc = bspLeaves[i]->GetViewCell(); 
     792 
     793                //bspLeaves[j]->Mail(); 
     794                char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i); 
     795 
     796                Exporter *exporter = Exporter::GetExporter(s); 
     797                exporter->SetFilled(); 
     798 
     799                ViewCellPvsMap::iterator it = vc->GetPvs().mEntries.begin(); 
     800 
     801                Material m;//= RandomMaterial(); 
     802                m.mDiffuseColor = RgbColor(0, 1, 0); 
     803                exporter->SetForcedMaterial(m); 
     804 
     805                exporter->SetWireframe(); 
     806 
     807                if (vc->GetMesh()) 
     808                        exporter->ExportViewCell(vc); 
     809                else 
     810                { 
     811            PolygonContainer cell; 
     812 
     813                        mBspTree->ConstructGeometry(bspLeaves[i], cell); 
     814                        exporter->ExportPolygons(cell); 
     815                        CLEAR_CONTAINER(cell); 
     816 
     817                        /*vector<BspLeaf *> neighbors; 
     818                        mBspTree->FindNeighbors(bspLeaves[j], neighbors); 
     819                        for (int j = 0; j < (int)neighbors.size(); ++ j) 
     820                        {       if (neighbors[j]->mViewCell == bspLeaves[j]->mViewCell) 
     821                                {}}*/ 
     822                } 
     823 
     824                Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize()  
     825                                << ", piercing rays=" << (int)vcRays[i].size() << endl; 
     826 
     827                // export view cells 
     828                if (0) 
     829                { 
     830                        m.mDiffuseColor = RgbColor(1, 0, 1); 
     831                        exporter->SetForcedMaterial(m); 
     832                        exporter->ExportViewCells(mViewCells); 
     833                } 
     834 
     835                // export rays piercing this view cell 
     836                exporter->ExportRays(vcRays[i], 1000, RgbColor(0, 1, 0)); 
     837 
     838                m.mDiffuseColor = RgbColor(1, 0, 0); 
     839                exporter->SetForcedMaterial(m); 
     840 
     841                // output PVS of view cell 
     842                for (; it != vc->GetPvs().mEntries.end(); ++ it)  
     843                { 
     844                        Intersectable *intersect = (*it).first; 
     845                        if (!intersect->Mailed()) 
     846                        { 
     847                                exporter->ExportIntersectable(intersect); 
     848                                intersect->Mail(); 
     849                        }                        
     850                } 
     851                         
     852                // output rest of the objects 
     853                if (0) 
     854                { 
     855                        Material m;//= RandomMaterial(); 
     856                        m.mDiffuseColor = RgbColor(0, 0, 1); 
     857                        exporter->SetForcedMaterial(m); 
     858 
     859                        for (int j = 0; j < objects.size(); ++ j) 
     860                                if (!objects[j]->Mailed()) 
     861                                { 
     862                                        //if (j == 2198)m.mDiffuseColor = RgbColor(1, 0, 1); 
     863                                        //else m.mDiffuseColor = RgbColor(1, 1, 0); 
     864                                        exporter->SetForcedMaterial(m); 
     865                                        exporter->ExportIntersectable(objects[j]); 
     866                                        objects[j]->Mail(); 
     867                                } 
     868                } 
     869                DEL_PTR(exporter); 
     870        } 
     871} 
Note: See TracChangeset for help on using the changeset viewer.