Ignore:
Timestamp:
01/20/07 00:14:25 (17 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp

    r1996 r1999  
    1616{ 
    1717   
    18 #define GVS_DEBUG 0 
     18#define GVS_DEBUG 1 
    1919 
    2020struct VizStruct 
     
    3131GvsPreprocessor::GvsPreprocessor():  
    3232Preprocessor(),  
    33 mSamplingType(SamplingStrategy::VIEWCELL_BASED_DISTRIBUTION), 
    34 //mSamplingType(SamplingStrategy::DIRECTION_BASED_DISTRIBUTION), 
     33//mSamplingType(SamplingStrategy::VIEWCELL_BASED_DISTRIBUTION), 
     34mSamplingType(SamplingStrategy::DIRECTION_BASED_DISTRIBUTION), 
    3535mProcessedViewCells(0), 
    3636mCurrentViewCell(NULL) 
     
    144144                        delete newRay; 
    145145                } 
    146                 else if (0 && GVS_DEBUG && (mVssRays.size() < 9)) 
     146                 
     147 
     148                return 1; 
     149        } 
     150 
     151        return 0; 
     152} 
     153 
     154// #id: 1270 
     155bool GvsPreprocessor::HasContribution(VssRay &ray) 
     156{ 
     157        if (!ray.mTerminationObject) 
     158                return false; 
     159 
     160        bool result; 
     161 
     162        if (!mPerViewCell) 
     163        { 
     164                // store the rays + the intersected view cells 
     165                const bool storeViewCells = false; //GVS_DEBUG; 
     166 
     167                mViewCellsManager->ComputeSampleContribution(ray,  
     168                                                                                                         true,  
     169                                                                                                         storeViewCells, 
     170                                                                                                         true); 
     171 
     172                result = ray.mPvsContribution > 0; 
     173        } 
     174        else 
     175        { 
     176                Intersectable *obj = ray.mTerminationObject; 
     177 
     178                if (!obj->mCounter) 
    147179                { 
    148                         mVssRays.push_back(new VssRay(oldRay)); 
    149                         mVssRays.push_back(new VssRay(currentRay)); 
    150                         mVssRays.push_back(new VssRay(*newRay)); 
    151                 } 
    152  
    153                 return 1; 
    154         } 
    155  
    156         return 0; 
    157 } 
    158  
    159  
    160 bool GvsPreprocessor::HandleRay(VssRay *vssRay) 
    161 { 
    162         // store the rays + the intersected view cells 
    163         const bool storeRaysForViz = false; //GVS_DEBUG; 
    164  
    165         if (!mPerViewCell) 
    166         { 
    167                 mViewCellsManager->ComputeSampleContribution(*vssRay,  
    168                                                                                                          true,  
    169                                                                                                          storeRaysForViz, 
    170                                                                                                          true); 
    171         } 
    172         else 
    173         { 
    174                 mViewCellsManager->ComputeSampleContribution(*vssRay,  
     180                        obj->mCounter = 1; 
     181                        mTrianglePvs.push_back(obj); 
     182 
     183                        /*mViewCellsManager->ComputeSampleContribution(ray,  
    175184                                                                                                         true,  
    176185                                                                                                         mCurrentViewCell, 
    177                                                                                                          true); 
    178         } 
    179  
    180         // some pvs contribution for this ray? 
    181         if (!vssRay->mPvsContribution) 
     186                                                                                                         true);*/ 
     187                         
     188                        result = true; 
     189                } 
     190                else 
     191                { 
     192                        result = false; 
     193                } 
     194        } 
     195 
     196        return result; 
     197} 
     198 
     199 
     200bool GvsPreprocessor::HandleRay(VssRay *vssRay) 
     201{ 
     202        if (!HasContribution(*vssRay)) 
    182203                return false; 
    183204 
    184         if (GVS_DEBUG) 
     205        if (0 && GVS_DEBUG) 
    185206                mVssRays.push_back(new VssRay(*vssRay)); 
    186207 
    187208        // add new ray to ray queue 
    188209        mRayQueue.push(vssRay); 
    189  
    190         if (storeRaysForViz) 
    191         { 
    192                 VssRay *nray = new VssRay(*vssRay); 
    193                 nray->mFlags = vssRay->mFlags; 
    194  
    195                 // store ray in contributing view cell 
    196                 ViewCellContainer::const_iterator vit, vit_end = vssRay->mViewCells.end(); 
    197                 for (vit = vssRay->mViewCells.begin(); vit != vit_end; ++ vit) 
    198                 {                        
    199                         (*vit)->GetOrCreateRays()->push_back(nray);                              
    200                 } 
    201         } 
    202210 
    203211        ++ mGvsStats.mTotalContribution; 
     
    399407        } 
    400408 
     409        int castRays = (int)simpleRays.size(); 
     410 
     411        VssRayContainer invalidSamples; 
     412 
    401413        // handle rays 
    402         EnqueueRays(vssRays); 
    403          
    404         int castRays = simpleRays.size(); 
    405          
     414        EnqueueRays(vssRays, invalidSamples); 
     415 
    406416    // recursivly subdivide each edge 
    407417        for (int i = 0; i < numBorderSamples; ++ i) 
     
    414424                                                                  currentRay); 
    415425        } 
    416  
     426         
    417427        mGvsStats.mBorderSamples += castRays; 
     428 
     429        CLEAR_CONTAINER(invalidSamples); 
    418430 
    419431        return castRays; 
     
    442454        if (!intersects) 
    443455        { 
    444                 cerr << "big error!! no intersection " << pt1 << " " << pt2 << endl; 
     456                //cerr << "big error!! no intersection " << pt1 << " " << pt2 << endl; 
    445457                return false; 
    446458        } 
     
    562574        CastRays(simpleRays, samples, castDoubleRays, pruneInvalidRays); 
    563575         
     576        VssRayContainer invalidSamples; 
     577 
    564578        // add to ray queue 
    565         EnqueueRays(samples); 
    566  
     579        EnqueueRays(samples, invalidSamples); 
     580 
     581        CLEAR_CONTAINER(invalidSamples); 
    567582        //Debug << "generated " <<  numSamples << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl; 
    568583        return (int)simpleRays.size(); 
     
    570585 
    571586 
    572 void GvsPreprocessor::EnqueueRays(VssRayContainer &samples) 
     587void GvsPreprocessor::EnqueueRays(VssRayContainer &samples, VssRayContainer &invalidSamples) 
    573588{ 
    574589        // add samples to ray queue 
     
    576591        for (vit = samples.begin(); vit != vit_end; ++ vit) 
    577592        { 
    578                 HandleRay(*vit); 
     593                VssRay *ray = *vit; 
     594 
     595                if (!HandleRay(ray)) 
     596                        invalidSamples.push_back(ray); 
    579597        } 
    580598} 
     
    597615                castSamples += newSamples; 
    598616 
    599                 //cout << newSamples << " ";     
     617                //cout << "new samples: " << newSamples << " " << "queue: "  << (int)mRayQueue.size() << endl; 
    600618                delete ray; 
    601619        } 
     
    639657 
    640658 
    641 void GvsPreprocessor::VisualizeViewCell(ViewCell *vc) 
     659//void GvsPreprocessor::VisualizeViewCell(ViewCell *vc) 
     660void GvsPreprocessor::VisualizeViewCell(const ObjectContainer &objects) 
    642661{ 
    643662    Intersectable::NewMail(); 
     
    651670                return; 
    652671 
    653         ObjectPvsIterator pit = vc->GetPvs().GetIterator(); 
     672        /*ObjectPvsIterator pit = vc->GetPvs().GetIterator(); 
    654673 
    655674        // output PVS of view cell 
     
    669688 
    670689                exporter->ExportIntersectable(intersect); 
     690        }*/ 
     691        ObjectContainer::const_iterator oit, oit_end = objects.end(); 
     692 
     693        for (oit = objects.begin(); oit != oit_end; ++ oit) 
     694        { 
     695                Intersectable *intersect = *oit; 
     696                 
     697                m = RandomMaterial(); 
     698                exporter->SetForcedMaterial(m); 
     699                exporter->ExportIntersectable(intersect); 
    671700        } 
    672701 
     
    682711        exporter->SetForcedMaterial(m); 
    683712 
    684         mViewCellsManager->ExportViewCellGeometry(exporter, vc, NULL, NULL); 
     713        //mViewCellsManager->ExportViewCellGeometry(exporter, vc, NULL, NULL); 
     714        //mViewCellsManager->ExportViewCellGeometry(exporter, mCurrentViewCell, NULL, NULL); 
     715 
     716        AxisAlignedBox3 bbox = mCurrentViewCell->GetMesh()->mBox; 
     717        exporter->ExportBox(bbox); 
    685718        //exporter->SetFilled(); 
    686719 
     
    720753void GvsPreprocessor::ProcessViewCell() 
    721754{ 
     755        //Intersectable::NewMail(); 
     756        // compute direct intersections with view cell 
     757        IntersectWithViewCell(); 
     758 
    722759        mGvsStats.mPerViewCellSamples = 0; 
    723760        int oldContribution = mGvsStats.mTotalContribution; 
     
    736773                passSamples += newSamples; 
    737774                mGvsStats.mPerViewCellSamples += newSamples; 
    738                 //cout << "here4 " << passSamples % (mSamplesPerPass + 1) << endl; 
     775                //cout << "here2 " << passSamples % (mSamplesPerPass + 1) << endl; 
    739776 
    740777                if (passSamples >= mSamplesPerPass) 
     
    774811                { 
    775812                        mViewCells.push_back(mViewCellsManager->GetViewCell((int)mViewCells.size())); 
     813                        continue; 
    776814                } 
    777                 else 
     815                 
     816                // HACK 
     817                const int tries = 10000; 
     818                int i = 0; 
     819 
     820                for (i = 0; i < tries; ++ i) 
    778821                { 
    779                         // HACK 
    780                         const int tries = 10000; 
    781                         int i = 0; 
    782  
    783                         for (i = 0; i < tries; ++ i) 
     822                        const int idx = (int)RandomValue(0.0f, (float)mViewCellsManager->GetNumViewCells() - 0.5f); 
     823         
     824                        ViewCell *viewCell = mViewCellsManager->GetViewCell(idx); 
     825 
     826                        if (!viewCell->Mailed()) 
    784827                        { 
    785                                 const int idx = (int)RandomValue(0.0f, (float)mViewCellsManager->GetNumViewCells() - 0.5f); 
    786          
    787                                 ViewCell *viewCell = mViewCellsManager->GetViewCell(idx); 
    788  
    789                                 if (!viewCell->Mailed()) 
     828                                viewCell->Mail(); 
     829                                break; 
     830                        } 
     831 
     832                        mViewCells.push_back(viewCell); 
     833                } 
     834 
     835                if (i == tries) 
     836                { 
     837                        cerr << "big error! no view cell found" << endl; 
     838                        return; 
     839                } 
     840        } 
     841} 
     842 
     843 
     844void GvsPreprocessor::IntersectWithViewCell() 
     845{ 
     846        mCurrentViewCell->GetMesh()->ComputeBoundingBox(); 
     847        AxisAlignedBox3 box = mCurrentViewCell->GetMesh()->mBox; 
     848        //cout << "box: " << box << endl; 
     849        vector<KdLeaf *> leaves; 
     850 
     851        mKdTree->GetBoxIntersections(box, leaves); 
     852 
     853        vector<KdLeaf *>::const_iterator lit, lit_end = leaves.end(); 
     854 
     855        for (lit = leaves.begin(); lit != leaves.end(); ++ lit) 
     856        { 
     857                KdLeaf *leaf = *lit; 
     858                ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end(); 
     859 
     860                for (oit = leaf->mObjects.begin(); oit != oit_end; ++ oit) 
     861                { 
     862                        TriangleIntersectable *triObj = dynamic_cast<TriangleIntersectable *>(*oit); 
     863 
     864                        if (box.Intersects(triObj->GetItem())) 
     865                        { 
     866                                if (!triObj->mCounter) 
    790867                                { 
    791                                         viewCell->Mail(); 
    792                                         break; 
     868                                        triObj->mCounter = 1; 
     869                                        mTrianglePvs.push_back(triObj); 
    793870                                } 
    794  
    795                                 mViewCells.push_back(viewCell); 
    796                         } 
    797  
    798                         if (i == tries) 
    799                         { 
    800                                 cerr << "big error! no view cell found" << endl; 
    801                                 return; 
    802871                        } 
    803872                } 
     
    808877void GvsPreprocessor::PerViewCellComputation() 
    809878{ 
     879        // hack: reset counter 
     880        ObjectContainer::const_iterator oit, oit_end = mObjects.end(); 
     881 
     882        for (oit = mObjects.begin(); oit != oit_end; ++ oit) 
     883        { 
     884                (*oit)->mCounter = 0; 
     885        } 
     886 
    810887        while (NextViewCell()) 
    811888        { 
    812                 cout << "\n***********************\nprocessing view cell " << mProcessedViewCells << endl; 
     889                cout << "\n***********************\n"  
     890                         << "processing view cell " << mProcessedViewCells  
     891                         << " (id: " << mCurrentViewCell->GetId() << ")" << endl; 
    813892 
    814893                // compute the pvs of the current view cell 
    815894                ProcessViewCell(); 
    816895 
    817                 mGvsStats.mTrianglePvs = mCurrentViewCell->GetPvs().GetSize(); 
     896                //mGvsStats.mTrianglePvs = mCurrentViewCell->GetPvs().GetSize(); 
     897                mGvsStats.mTrianglePvs = (int)mTrianglePvs.size(); 
     898 
     899                ObjectContainer objectPvs; 
    818900 
    819901                // exchange triangle pvs with objects 
    820                 UpdatePvs(mCurrentViewCell); 
    821  
    822                 if (GVS_DEBUG) 
    823                 { 
    824                         VisualizeViewCell(mCurrentViewCell); 
    825                         CLEAR_CONTAINER(mVssRays); 
    826                 } 
     902                //UpdatePvs(mCurrentViewCell); 
     903                GetObjectPvs(objectPvs); 
     904 
     905                cout << "triangle pvs of " << (int)mTrianglePvs.size() << " was converted to object pvs of " << (int)objectPvs.size() << endl; 
    827906 
    828907                //////// 
     
    830909                 
    831910                mGvsStats.mViewCells = mProcessedViewCells;//mPass; 
    832                 mGvsStats.mPerViewCellPvs = mCurrentViewCell->GetPvs().GetSize(); 
    833                 mGvsStats.mTotalPvs += mCurrentViewCell->GetPvs().GetSize(); 
     911                //mGvsStats.mPerViewCellPvs = mCurrentViewCell->GetPvs().GetSize(); 
     912                mGvsStats.mPerViewCellPvs = (int)mTrianglePvs.size(); 
     913 
     914                mGvsStats.mTotalPvs += mGvsStats.mPerViewCellPvs; 
    834915                mGvsStats.mTotalSamples += mGvsStats.mPerViewCellSamples; 
    835916 
    836917                mGvsStats.Stop(); 
    837918                mGvsStats.Print(mGvsStatsStream); 
     919 
     920                mTrianglePvs.clear(); 
     921 
     922                if (GVS_DEBUG) 
     923                { 
     924                        //VisualizeViewCell(mCurrentViewCell); 
     925                        VisualizeViewCell(objectPvs); 
     926                        CLEAR_CONTAINER(mVssRays); 
     927                } 
    838928 
    839929                // is this really necessary? 
     
    871961 
    872962        currentViewCell->SetPvs(newPvs); 
     963} 
     964 
     965  
     966void GvsPreprocessor::GetObjectPvs(ObjectContainer &objectPvs) const 
     967{ 
     968        BvhLeaf::NewMail(); 
     969 
     970        ObjectContainer::const_iterator oit, oit_end = mTrianglePvs.end(); 
     971 
     972        for (oit = mTrianglePvs.begin(); oit != oit_end; ++ oit) 
     973        { 
     974                Intersectable *intersect = *oit; 
     975         
     976                BvhLeaf *bv = intersect->mBvhLeaf; 
     977 
     978                if (!bv || bv->Mailed()) 
     979                        continue; 
     980                 
     981                bv->Mail(); 
     982 
     983                objectPvs.push_back(bv); 
     984                // hack: reset counter 
     985                (*oit)->mCounter = 0; 
     986        } 
    873987} 
    874988 
     
    9781092         
    9791093        vector<VizStruct>::const_iterator vit, vit_end = vizContainer.end(); 
     1094         
    9801095        for (vit = vizContainer.begin(); vit != vit_end; ++ vit) 
    9811096        { 
     
    9891104 
    9901105        VssRayContainer::const_iterator rit, rit_end = mVssRays.end(); 
     1106 
    9911107        for (rit = mVssRays.begin(); rit != rit_end; ++ rit) 
    9921108        { 
Note: See TracChangeset for help on using the changeset viewer.