Ignore:
Timestamp:
11/22/05 20:51:37 (19 years ago)
Author:
bittner
Message:

vss updates

File:
1 edited

Legend:

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

    r403 r427  
    1010#include "VssTree.h" 
    1111 
     12 
     13bool useViewSpaceBox = true; 
     14bool use2dSampling = false; 
     15bool useViewspacePlane = false; 
     16 
    1217VssPreprocessor::VssPreprocessor(): 
    1318        mPass(0), 
     
    1823  environment->GetIntValue("VssPreprocessor.initialSamples", mInitialSamples); 
    1924  environment->GetIntValue("VssPreprocessor.vssSamples", mVssSamples); 
     25  environment->GetIntValue("VssPreprocessor.vssSamplesPerPass", mVssSamplesPerPass); 
    2026        environment->GetBoolValue("VssPreprocessor.useImportanceSampling", mUseImportanceSampling); 
    2127         
     
    4955        static Ray ray; 
    5056        AxisAlignedBox3 box = mKdTree->GetBox(); 
     57 
     58        AxisAlignedBox3 sbox = box; 
     59        sbox.Enlarge(Vector3(-Limits::Small)); 
     60        if (!sbox.IsInside(viewPoint)) 
     61                return 0; 
    5162         
    5263        SetupRay(ray, viewPoint, direction); 
     
    145156 
    146157Vector3 
    147 VssPreprocessor::GetDirection(const Vector3 &viewpoint) 
    148 { 
    149         int i = RandomValue(0, mObjects.size()-1); 
    150         Intersectable *object = mObjects[i]; 
    151         Vector3 point, normal; 
    152         object->GetRandomSurfacePoint(point, normal); 
     158VssPreprocessor::GetDirection(const Vector3 &viewpoint, 
     159                                                                                                                        AxisAlignedBox3 *viewSpaceBox 
     160                                                                                                                        ) 
     161{ 
     162        Vector3 point; 
     163        if (!use2dSampling) { 
     164                Vector3 normal; 
     165                int i = RandomValue(0, mObjects.size()-1); 
     166                Intersectable *object = mObjects[i]; 
     167                object->GetRandomSurfacePoint(point, normal); 
     168        } else { 
     169                AxisAlignedBox3 box; 
     170                 
     171                if (viewSpaceBox) 
     172                        box =*viewSpaceBox; 
     173                else  
     174                        box = mKdTree->GetBox(); 
     175                 
     176                point = box.GetRandomPoint(); 
     177                point.y = viewpoint.y; 
     178        } 
     179         
    153180        return point - viewpoint; 
    154181} 
    155182 
    156183int 
    157 VssPreprocessor::RandomizedImportanceSampling(VssTree *vssTree, 
    158                                                                                                                                                                                         const int desiredSamples) 
    159 { 
    160         float minRayContribution; 
    161         float maxRayContribution; 
    162         float avgRayContribution; 
    163  
    164         vssTree->GetRayContributionStatistics(minRayContribution, 
    165                                                                                                                                                                 maxRayContribution, 
    166                                                                                                                                                                 avgRayContribution); 
    167  
    168         cout<< 
    169                 "#MIN_RAY_CONTRIB\n"<<minRayContribution<<endl<< 
    170                 "#MAX_RAY_CONTRIB\n"<<maxRayContribution<<endl<< 
    171                 "#AVG_RAY_CONTRIB\n"<<avgRayContribution<<endl; 
    172          
    173         float p = desiredSamples/(float)(avgRayContribution*vssTree->stat.Leaves()); 
    174         SimpleRayContainer rays; 
    175         int num = vssTree->GenerateRays(p, rays); 
     184VssPreprocessor::GenerateImportanceRays(VssTree *vssTree, 
     185                                                                                                                                                                const int desiredSamples, 
     186                                                                                                                                                                SimpleRayContainer &rays 
     187                                                                                                                                                                ) 
     188{ 
     189        int num; 
     190        if (0) { 
     191                float minRayContribution; 
     192                float maxRayContribution; 
     193                float avgRayContribution; 
     194                 
     195                vssTree->GetRayContributionStatistics(minRayContribution, 
     196                                                                                                                                                                        maxRayContribution, 
     197                                                                                                                                                                        avgRayContribution); 
     198                 
     199                cout<< 
     200                        "#MIN_RAY_CONTRIB\n"<<minRayContribution<<endl<< 
     201                        "#MAX_RAY_CONTRIB\n"<<maxRayContribution<<endl<< 
     202                        "#AVG_RAY_CONTRIB\n"<<avgRayContribution<<endl; 
     203                 
     204                float p = desiredSamples/(float)(avgRayContribution*vssTree->stat.Leaves()); 
     205                num = vssTree->GenerateRays(p, rays); 
     206        } else { 
     207                int leaves = vssTree->stat.Leaves()/2; 
     208                num = vssTree->GenerateRays(desiredSamples, leaves, rays); 
     209        } 
     210         
    176211        cout<<"Generated "<<num<<" rays."<<endl; 
    177212         
    178         VssRayContainer  vssRays; 
    179          
    180         for (int i=0; i < rays.size(); i++) 
    181                 CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays); 
    182  
    183         vssTree->AddRays(vssRays); 
    184213        return num; 
    185214} 
    186215 
     216 
     217bool 
     218VssPreprocessor::ExportRays(const char *filename, 
     219                                                                                                                const VssRayContainer &vssRays, 
     220                                                                                                                const int number 
     221                                                                                                                ) 
     222{ 
     223        cout<<"Exporting vss rays..."<<endl<<flush; 
     224         
     225        float prob = number/(float)vssRays.size(); 
     226 
     227 
     228        Exporter *exporter = NULL; 
     229        exporter = Exporter::GetExporter(filename); 
     230        //      exporter->SetWireframe(); 
     231        //      exporter->ExportKdTree(*mKdTree); 
     232        exporter->SetFilled(); 
     233        exporter->ExportScene(mSceneGraph->mRoot); 
     234        exporter->SetWireframe(); 
     235 
     236        if (mViewSpaceBox) { 
     237                exporter->SetForcedMaterial(RgbColor(1,0,0)); 
     238                exporter->ExportBox(*mViewSpaceBox); 
     239                exporter->ResetForcedMaterial(); 
     240        } 
     241         
     242        VssRayContainer rays;   for (int i=0; i < vssRays.size(); i++) 
     243                if (RandomValue(0,1) < prob) 
     244                        rays.push_back(vssRays[i]); 
     245 
     246        exporter->ExportRays(rays, RgbColor(1, 0, 0)); 
     247         
     248        delete exporter; 
     249 
     250        cout<<"done."<<endl<<flush; 
     251 
     252        return true; 
     253} 
     254 
     255 
     256bool 
     257VssPreprocessor::ExportVssTreeLeaf(char *filename, 
     258                                                                                                                                         VssTree *tree, 
     259                                                                                                                                         VssTreeLeaf *leaf) 
     260{ 
     261        Exporter *exporter = NULL; 
     262        exporter = Exporter::GetExporter(filename); 
     263        exporter->SetWireframe(); 
     264        exporter->ExportKdTree(*mKdTree); 
     265         
     266        if (mViewSpaceBox) { 
     267                exporter->SetForcedMaterial(RgbColor(1,0,0)); 
     268                exporter->ExportBox(*mViewSpaceBox); 
     269                exporter->ResetForcedMaterial(); 
     270        } 
     271         
     272        exporter->SetForcedMaterial(RgbColor(0,0,1)); 
     273        exporter->ExportBox(tree->GetBBox(leaf)); 
     274        exporter->ResetForcedMaterial(); 
     275         
     276        VssRayContainer rays[4]; 
     277        for (int i=0; i < leaf->rays.size(); i++) { 
     278                int k = leaf->rays[i].GetRayClass(); 
     279                rays[k].push_back(leaf->rays[i].mRay); 
     280        } 
     281         
     282        // SOURCE RAY 
     283        exporter->ExportRays(rays[0], RgbColor(1, 0, 0)); 
     284        // TERMINATION RAY 
     285        exporter->ExportRays(rays[1], RgbColor(1, 1, 1)); 
     286        // PASSING_RAY 
     287        exporter->ExportRays(rays[2], RgbColor(1, 1, 0)); 
     288        // CONTAINED_RAY 
     289        exporter->ExportRays(rays[3], RgbColor(0, 0, 1)); 
     290 
     291        delete exporter; 
     292        return true; 
     293} 
     294 
     295void 
     296VssPreprocessor::ExportVssTreeLeaves(VssTree *tree, const int number) 
     297{ 
     298        vector<VssTreeLeaf *> leaves; 
     299        tree->CollectLeaves(leaves); 
     300 
     301        int num = 0; 
     302        int i; 
     303        float p = number / (float)leaves.size(); 
     304        for (i=0; i < leaves.size(); i++) { 
     305                if (RandomValue(0,1) < p) { 
     306                        char filename[64]; 
     307                        sprintf(filename, "vss-leaf-%04d.x3d", num); 
     308                        ExportVssTreeLeaf(filename, tree, leaves[i]); 
     309                        num++; 
     310                } 
     311                if (num >= number) 
     312                        break; 
     313        } 
     314} 
    187315 
    188316bool 
     
    196324  int totalSamples = 0; 
    197325 
    198         AxisAlignedBox3 *viewSpaceBox = NULL; 
    199  
    200         AxisAlignedBox3 box = mKdTree->GetBox(); 
    201  
    202          
    203         if (1) 
    204                 box.Enlarge(box.Size()*-Vector3(0.45, 0.45, 0.45)); 
    205         else { 
     326 
     327        AxisAlignedBox3 *box = new AxisAlignedBox3(mKdTree->GetBox()); 
     328 
     329        if (!useViewspacePlane) { 
     330                float size = 0.01f; 
     331                float s = 0.5f - size; 
     332                float olds = Magnitude(box->Size()); 
     333                box->Enlarge(box->Size()*Vector3(-s)); 
     334                Vector3 translation = Vector3(-olds*0.1f, 0, 0); 
     335                box->SetMin(box->Min() + translation); 
     336                box->SetMax(box->Max() + translation); 
     337        } else { 
     338                 
    206339                // sample city like heights 
    207                 box.SetMin(1, box.Min(1) + box.Size(1)*0.1); 
    208                 box.SetMax(1, box.Min(1) + box.Size(1)*0.2); 
    209         } 
    210          
    211         bool useViewSpaceBox = false; 
     340                box->SetMin(1, box->Min(1) + box->Size(1)*0.1); 
     341                box->SetMax(1, box->Min(1) + box->Size(1)*0.2); 
     342        } 
     343 
     344        if (use2dSampling) 
     345                box->SetMax(1, box->Min(1)); 
     346         
    212347        if (useViewSpaceBox) 
    213                 viewSpaceBox = &box; 
     348                mViewSpaceBox = box; 
     349        else 
     350                mViewSpaceBox = NULL; 
     351                 
    214352 
    215353        VssTree *vssTree = NULL; 
     
    223361                int sampleContributions; 
    224362                 
    225                  
    226                 for (int k=0; k < mSamplesPerPass; k++) { 
     363                int s = Min(mSamplesPerPass, mInitialSamples); 
     364                for (int k=0; k < s; k++) { 
    227365                         
    228                         Vector3 viewpoint = GetViewpoint(viewSpaceBox); 
    229                         Vector3 direction = GetDirection(viewpoint); 
     366                        Vector3 viewpoint = GetViewpoint(mViewSpaceBox); 
     367                        Vector3 direction = GetDirection(viewpoint, mViewSpaceBox); 
    230368                         
    231369                        sampleContributions = CastRay(viewpoint, direction, mVssRays); 
     
    271409        cout << "#totalRayStackSize=" << mVssRays.size() << endl <<flush; 
    272410 
    273         cout<<"Exporting vss rays..."<<endl<<flush; 
    274          
    275         int exportRays = 10000; 
    276  
    277         if (exportRays) { 
    278                 float prob = exportRays/(float)mVssRays.size(); 
    279                  
    280                 Exporter *exporter = NULL; 
    281                 exporter = Exporter::GetExporter("vss-rays.x3d"); 
    282                 exporter->SetWireframe(); 
    283                 exporter->ExportKdTree(*mKdTree); 
    284                 if (viewSpaceBox) { 
    285                         exporter->SetForcedMaterial(RgbColor(1,0,0)); 
    286                         exporter->ExportBox(*viewSpaceBox); 
    287                         exporter->ResetForcedMaterial(); 
    288                 } 
    289                 VssRayContainer rays; 
    290                 for (int i=0; i < mVssRays.size(); i++) 
    291                         if (RandomValue(0,1) < prob) 
    292                                 rays.push_back(mVssRays[i]); 
    293                 exporter->ExportRays(rays, RgbColor(1, 0, 0)); 
    294  
    295                 delete exporter; 
    296         } 
    297         cout<<"done."<<endl<<flush; 
     411         
     412        //      int numExportRays = 10000; 
     413 
     414        int numExportRays = 0; 
     415 
     416        if (numExportRays) { 
     417                char filename[64]; 
     418                sprintf(filename, "vss-rays-initial.x3d"); 
     419                ExportRays(filename, mVssRays, numExportRays); 
     420        } 
    298421 
    299422 
     
    302425        vssTree = new VssTree; 
    303426         
    304         vssTree->Construct(mVssRays, viewSpaceBox); 
     427        vssTree->Construct(mVssRays, mViewSpaceBox); 
    305428 
    306429        cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl; 
    307430 
     431        ExportVssTreeLeaves(vssTree, 10); 
    308432         
    309433        int samples = 0; 
     434        int pass = 0; 
    310435        while (1) { 
    311                 int num = mSamplesPerPass; 
     436                int num = mVssSamplesPerPass; 
     437                SimpleRayContainer rays; 
     438                VssRayContainer vssRays; 
     439                 
    312440                if (!mUseImportanceSampling) { 
    313                         VssRayContainer vssRays; 
    314441                        for (int j=0; j < num; j++) { 
    315                                 Vector3 viewpoint = GetViewpoint(viewSpaceBox); 
    316                                 Vector3 direction = GetDirection(viewpoint); 
    317                                 CastRay(viewpoint, direction, vssRays); 
     442                                Vector3 viewpoint = GetViewpoint(mViewSpaceBox); 
     443                                Vector3 direction = GetDirection(viewpoint, mViewSpaceBox); 
     444                                rays.push_back(SimpleRay(viewpoint, direction)); 
    318445                        } 
    319                         vssTree->AddRays(vssRays); 
    320446                } else { 
    321                         num = RandomizedImportanceSampling(vssTree, num); 
    322                 } 
     447                        num = GenerateImportanceRays(vssTree, num, rays); 
     448                } 
     449                 
     450                 
     451                for (int i=0; i < rays.size(); i++) 
     452                        CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays); 
     453                 
     454                vssTree->AddRays(vssRays); 
     455                 
     456                if (1) { 
     457                        int subdivided = vssTree->UpdateSubdivision(); 
     458                        cout<<"subdivided leafs = "<<subdivided<<endl; 
     459                } 
     460                 
     461                if (numExportRays) { 
     462                        char filename[64]; 
     463                        if (mUseImportanceSampling) 
     464                                sprintf(filename, "vss-rays-i%04d.x3d", pass); 
     465                        else 
     466                                sprintf(filename, "vss-rays-%04d.x3d", pass); 
     467                         
     468                        ExportRays(filename, vssRays, numExportRays); 
     469                } 
     470 
     471                 
    323472                samples+=num; 
    324473                float pvs = vssTree->GetAvgPvsSize(); 
     474                cout<<"*****************************\n"; 
    325475                cout<<samples<<" avgPVS ="<<pvs<<endl; 
     476                cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl; 
     477                cout<<"*****************************\n"; 
    326478                if (samples >= mVssSamples) 
    327479                        break; 
     480                pass++; 
    328481        } 
    329482 
Note: See TracChangeset for help on using the changeset viewer.