Ignore:
Timestamp:
11/25/06 00:19:18 (18 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h

    r1786 r1789  
    3232        template<typename T, typename S> 
    3333        friend int operator< (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b); 
     34        template<typename T, typename S> 
     35        friend int operator== (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b); 
    3436}; 
    3537 
     
    3941{ 
    4042        return a.mObject < b.mObject; 
     43}  
     44 
     45template<typename T, typename S> 
     46int operator== (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b) 
     47{ 
     48        return a.mObject == b.mObject; 
    4149}  
    4250 
     
    5159}; 
    5260 
     61template<typename T, typename S> 
     62int equalSample (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b) 
     63{ 
     64        return a.mObject == b.mObject; 
     65}  
    5366 
    5467/** Information stored with a PVS entry. Consists of the number 
     
    189202 
    190203        /** Finds sample in PVS. 
     204                @param checkDirty if dirty part of the pvs should be checked for entry  
     205                        (warning: linear runtime in dirty part) 
    191206                @returns iterator on the sample. 
    192207        */ 
    193         typename vector<PvsEntry<T, S> >::iterator Find(T sample); 
     208        typename vector<PvsEntry<T, S> >::iterator Find(T sample, const bool checkDirty = true); 
    194209 
    195210        bool GetSampleContribution(T sample, const float pdf, float &contribution); 
     
    199214                @returns true if sample was not already in PVS. 
    200215        */ 
    201         bool AddSample(T sample, const float pdf, float &contribution); 
     216        //bool AddSample(T sample, const float pdf, float &contribution); 
    202217 
    203218        /** Adds sample to PVS. 
     
    206221        float AddSample(T sample, const float pdf); 
    207222 
    208   /** Adds sample to PVS without checking for presence of the sample 
    209           pvs remians unsorted! 
    210   */ 
    211   void AddSampleDirty(T sample, const float pdf); 
    212  
    213   /** Sort pvs entries - this should always be called after a 
    214           sequence of AddSampleDirty calls */ 
    215   void Sort(); 
    216    
     223        /** Adds sample to PVS without checking for presence of the sample 
     224                pvs remains unsorted! 
     225        */ 
     226        void AddSampleDirty(T sample, const float pdf); 
     227 
     228        /** Adds sample dirty (on the end of the vector) but 
     229                first checks if sample is already in clean part of the pvs. 
     230        */ 
     231        bool AddSampleDirtyCheck(T sample, const float pdf);//, float &contribution); 
     232 
     233        /** Sort pvs entries - this should always be called after a 
     234                sequence of AddSampleDirty calls  
     235        */ 
     236        void Sort(); 
     237 
    217238        /** Adds sample to PVS. Assumes that the pvs is sorted 
    218239                @returns contribution of sample (0 or 1) 
     
    266287        static void Merge(Pvs<T, S> &mergedPvs, const Pvs<T, S> &a, const Pvs<T, S> &b); 
    267288 
     289        static void Merge(Pvs<T, S> &mergedPvs,  
     290                                          const typename std::vector<PvsEntry<T, S> >::const_iterator &aBegin, 
     291                                          const typename std::vector<PvsEntry<T, S> >::const_iterator &aEnd, 
     292                                          const typename std::vector<PvsEntry<T, S> >::const_iterator &bBegin, 
     293                                          const typename std::vector<PvsEntry<T, S> >::const_iterator &bEnd, 
     294                                          const int aSamples,  
     295                                          const int bSamples); 
     296 
    268297        int GetSamples() const 
    269298        { 
    270299                return mSamples; 
     300        } 
     301 
     302 
     303        bool IsDirty() const 
     304        { 
     305                return mLastSorted < mEntries.size(); 
     306        } 
     307 
     308        bool RequiresResort() const 
     309        { 
     310                // the last part should not be more than log of the sorted part. this 
     311                // way we can achieve logarithmic behaviour for insertion and find 
     312                const int dirtySize = (int)mEntries.size() - mLastSorted; 
     313                return dirtySize > (int)(log((double)mEntries.size()) / log(2.0)); 
     314        } 
     315 
     316 
     317        int GetLastSorted() const 
     318        { 
     319                return mLastSorted; 
    271320        } 
    272321 
     
    281330        int mSamples; 
    282331   
    283   /// Last sorted entry in the pvs (important for find and merge 
    284   int mLastSorted; 
    285    
     332        /// Last sorted entry in the pvs (important for find and merge) 
     333        int mLastSorted;   
    286334}; 
    287335 
     
    293341        mEntries = samples; 
    294342        mLastSorted = 0; 
    295 } 
     343        mSamples = samples.size(); 
     344} 
     345 
    296346 
    297347template <typename T, typename S> 
    298348void Pvs<T, S>::Sort() 
    299349{ 
    300   std::vector<PvsEntry<T, S> >::iterator it = mEntries.begin(); 
    301   it.inc(mLastSorted); 
    302   sort(it, mEntries.end()); 
    303   mLastSorted = mEntries.size() - 1; 
    304 } 
     350        std::vector<PvsEntry<T, S> >::iterator it = mEntries.begin() + mLastSorted; 
     351        //std::vector<PvsEntry<T, S> >::const_iterator it = mEntries.begin() + mLastSorted; 
     352        std::vector<PvsEntry<T, S> >::iterator it_end = mEntries.end(); 
     353 
     354        // throw out double entries 
     355        std::vector<PvsEntry<T, S> >::iterator newEnd = unique(it, it_end); 
     356        sort(it, newEnd); 
     357        //sort(mEntries.begin(), mEntries.end()); 
     358 
     359        // now merge sorted ranges 
     360        ObjectPvs newPvs; 
     361        Merge(newPvs,  
     362                  mEntries.begin(), it,  
     363                  it, newEnd,  
     364                  mSamples, 0); 
     365         
     366        mEntries = newPvs.mEntries; 
     367        mLastSorted = (int)mEntries.size(); 
     368} 
     369 
    305370 
    306371/** 
     
    436501        std::vector<PvsEntry<T, S> >::const_iterator bit = b.mEntries.begin(), bit_end = b.mEntries.end(); 
    437502         
    438         for (; (ait != ait_end); ++ ait) 
     503        Merge(mergedPvs,  
     504                  ait, ait_end, 
     505                  bit, bit_end, 
     506                  a.mSamples, 
     507                  b.mSamples); 
     508} 
     509 
     510 
     511template <typename T, typename S> 
     512void Pvs<T, S>::Merge(Pvs<T, S> &mergedPvs,  
     513                                          const typename std::vector<PvsEntry<T, S> >::const_iterator &aBegin, 
     514                                          const typename std::vector<PvsEntry<T, S> >::const_iterator &aEnd, 
     515                                          const typename std::vector<PvsEntry<T, S> >::const_iterator &bBegin, 
     516                                          const typename std::vector<PvsEntry<T, S> >::const_iterator &bEnd, 
     517                                          const int aSamples,  
     518                                          const int bSamples) 
     519{ 
     520        std::vector<PvsEntry<T, S> >::const_iterator ait = aBegin; 
     521        std::vector<PvsEntry<T, S> >::const_iterator bit = bBegin; 
     522         
     523        for (; (ait != aEnd); ++ ait) 
    439524        { 
    440525                Intersectable *aObj = (*ait).mObject; 
    441526                Intersectable *bObj = NULL; 
    442                 PvsEntry<T, S> aEntry = (*ait); 
    443  
    444                 for (; (bit != bit_end) && ((*bit).mObject <= (*ait).mObject); ++ bit) 
     527                //Intersectable *bObjOld = NULL; 
     528         
     529                const PvsEntry<T, S> &aEntry = (*ait); 
     530 
     531                for (; (bit != bEnd) && ((*bit).mObject <= (*ait).mObject); ++ bit) 
    445532                { 
    446533                        bObj = (*bit).mObject; 
     
    457544                                mergedPvs.mEntries.push_back(*bit); 
    458545                        } 
     546                         
     547                        //bObjOld = bObj; 
    459548                } 
    460549 
     
    468557 
    469558        // add the rest 
    470         for (; (bit != bit_end); ++ bit) 
     559        for (; (bit != bEnd); ++ bit) 
    471560        { 
    472561                mergedPvs.mEntries.push_back(*bit); 
    473562        } 
    474         mergedPvs.mSamples = a.mSamples + b.mSamples; 
     563 
     564        mergedPvs.mSamples = aSamples + bSamples; 
    475565} 
    476566 
     
    480570        mEntries.clear(); 
    481571        mSamples = 0; 
     572        mLastSorted = 0; 
    482573 
    483574        if (trim) 
     
    495586 
    496587template <typename T, typename S>  
    497 typename std::vector<PvsEntry<T, S> >::iterator Pvs<T, S>::Find(T sample) 
     588typename std::vector<PvsEntry<T, S> >::iterator Pvs<T, S>::Find(T sample, const bool checkDirty) 
    498589{ 
    499590        PvsEntry<T, S> dummy(sample, PvsData()); 
    500         vector<PvsEntry<T, S> >::iterator it = lower_bound(mEntries.begin(), mEntries.end(), dummy); 
    501                                  
     591 
     592        // only check clean part 
     593        //mLastSorted = 0; 
     594        vector<PvsEntry<T, S> >::iterator sorted_end = mEntries.begin() + mLastSorted; 
     595if (sorted_end != mEntries.end()) 
     596cout << "not entries end!! " << endl; 
     597        // binary search 
     598        vector<PvsEntry<T, S> >::iterator it = lower_bound(mEntries.begin(), sorted_end, dummy); 
     599 
     600        // sample not found yet => search further in the unsorted part 
     601        if (checkDirty && 
     602                ((it == mEntries.end()) || ((*it).mObject != sample))) 
     603        { 
     604                vector<PvsEntry<T, S> >::const_iterator it_end = mEntries.end(); 
     605                for (it = sorted_end; (it != it_end) && ((*it).mObject != sample); ++ it); 
     606        } 
     607        else cout << "f "; 
    502608        return it; 
    503609} 
     
    519625{ 
    520626        ++ mSamples; 
     627        cout << "! "; 
    521628        std::vector<PvsEntry<T, S> >::iterator it = Find(sample); 
    522629 
    523630        if ((it != mEntries.end()) && ((*it).mObject == sample)) 
    524         { 
     631        {       cout << "g"; 
    525632                S &data = (*it).mData; 
    526633                data.mSumPdf += pdf; 
     
    528635        } 
    529636        else  
    530         { 
     637        {cout << "t"; 
    531638                PvsEntry<T, S> entry(sample, pdf); 
    532639                mEntries.insert(it, entry); 
     640                ++ mLastSorted; 
    533641                return pdf; 
    534642        } 
     643        cout << " $"; 
    535644} 
    536645 
     
    539648void Pvs<T, S>::AddSampleDirty(T sample, const float pdf) 
    540649{ 
    541   ++ mSamples; 
    542   mEntries.push_back(PvsEntry<T, S>(sample, pdf)); 
     650        ++ mSamples; 
     651        mEntries.push_back(PvsEntry<T, S>(sample, pdf)); 
    543652} 
    544653                                          
     
    548657{ 
    549658        ++ mSamples; 
     659         
    550660        std::vector<PvsEntry<T, S> >::iterator it = Find(sample); 
    551661 
     
    559669                PvsEntry<T, S> entry(sample, pdf); 
    560670                mEntries.insert(it, entry); 
     671                ++ mLastSorted; 
    561672        } 
    562673 
     
    565676 
    566677 
    567 template <typename T, typename S>  
     678/*template <typename T, typename S>  
    568679bool Pvs<T, S>::AddSample(T sample, 
    569680                                                  const float pdf, 
     
    589700                mEntries.insert(it, entry); 
    590701                contribution = 1.0f; 
     702                ++ mLastSorted; 
     703 
     704                return true; 
     705        } 
     706}*/ 
     707 
     708 
     709/** Adds sample dirty (on the end of the vector) but 
     710        first checks if sample is already in clean part of the pvs. 
     711*/ 
     712template <typename T, typename S>  
     713bool Pvs<T, S>::AddSampleDirtyCheck(T sample, 
     714                                                                        const float pdf) 
     715                                                                        //,float &contribution) 
     716{ 
     717        ++ mSamples; 
     718 
     719        std::vector<PvsEntry<T, S> >::iterator it = Find(sample); 
     720 
     721        if ((it != mEntries.end()) && ((*it).mObject == sample)) 
     722        { 
     723                S &data = (*it).mData; 
     724 
     725                data.mSumPdf += pdf; 
     726                //contribution = pdf / data.mSumPdf; 
     727 
     728                return false; 
     729        } 
     730        else  
     731        { 
     732                AddSampleDirty(sample, pdf); 
     733                //contribution = 1.0f; 
    591734 
    592735                return true; 
     
    620763{ 
    621764        -- mSamples; 
    622  
     765         
    623766        std::vector<PvsEntry<T, S> >::iterator it = Find(sample); 
    624767 
     
    633776        { 
    634777                mEntries.erase(it); 
     778                -- mLastSorted; // wrong if sample was in tail!! 
    635779        } 
    636780 
Note: See TracChangeset for help on using the changeset viewer.