Ignore:
Timestamp:
02/15/07 00:35:41 (17 years ago)
Author:
mattausch
Message:

implemented hashpvs

File:
1 edited

Legend:

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

    r2100 r2116  
    1 #ifndef __PVS_H 
    2 #define __PVS_H 
    3  
    4 #include <map> 
     1#ifndef __VERBOSEPVS_H 
     2#define __VERBOSEPVS_H 
     3 
    54#include <vector> 
    65#include "common.h" 
    76#include <math.h> 
     7#include "PvsBase.h" 
     8 
     9 
    810using namespace std; 
    911 
    1012namespace GtpVisibilityPreprocessor { 
    11  
    12 class KdNode; 
    13 class BspNode; 
    14 class Ray; 
    15 class Intersectable; 
    16 class ViewCell; 
    17  
    18  
    19 /** Information stored with a PVS entry. Consists of the number 
    20         the object was seen from the view cell. 
    21 */ 
    22 template<typename T, typename S> 
    23 struct PvsEntry  
    24 { 
    25 public: 
    26  
    27         PvsEntry() {} 
    28  
    29         PvsEntry(T sample, const S &data): mObject(sample), mData(data) {} 
    30  
    31         T mObject; 
    32         S mData; 
    33  
    34         template<typename T, typename S> 
    35         friend int operator< (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b); 
    36         template<typename T, typename S> 
    37         friend int operator== (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b); 
    38 }; 
    39  
    40  
    41 template<typename T, typename S> 
    42 int operator< (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b) 
    43 { 
    44         return a.mObject < b.mObject; 
    45 }  
    46  
    47 template<typename T, typename S> 
    48 int operator== (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b) 
    49 { 
    50         return a.mObject == b.mObject; 
    51 }  
    52  
    53  
    54 template<typename T, typename S> 
    55 struct LtSample 
    56 { 
    57     bool operator()(const PvsEntry<T, S> &a, const PvsEntry<T, S> &b) const 
    58     { 
    59                 return a.mObject < b.mObject; 
    60         }  
    61 }; 
    62  
    63 template<typename T, typename S> 
    64 int equalSample (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b) 
    65 { 
    66         return a.mObject == b.mObject; 
    67 }  
    68  
    69 /** Information stored with a PVS entry. Consists of the number 
    70         the object was seen from the view cell. 
    71 */ 
    72 struct PvsData { 
    73 public: 
    74         PvsData() {} 
    75         PvsData(const float sumPdf): 
    76         mSumPdf(sumPdf) {} 
    77          
    78         // $$JB in order to return meaningfull values 
    79         // it assumes that the sum pdf has been normalized somehow!!! 
    80         inline float GetVisibility()  
    81         {  
    82                 return mSumPdf;  
    83         } 
    84  
    85         /// sum of probability density of visible sample rays 
    86         float mSumPdf; 
    87 }; 
    88  
    89  
    90 class MailablePvsData  
    91 { 
    92 public: 
    93         // sum of probability density of visible sample rays 
    94         float mSumPdf; 
    95         int mCounter; 
    96  
    97         MailablePvsData() {} 
    98         MailablePvsData(const float sumPdf): 
    99         mSumPdf(sumPdf) {} 
    100  
    101         // $$JB in order to return meaningfull values 
    102         // it assumes that the sum pdf has been normalized somehow!!! 
    103         float GetVisibility()  
    104         {  
    105                 return mSumPdf;  
    106         } 
    107  
    108         /////////////// 
    109         //  Mailing stuff 
    110  
    111         // last mail id -> warning not thread safe! 
    112         // both mailId and mailbox should be unique for each thread!!! 
    113         static int sMailId; 
    114         static int sReservedMailboxes; 
    115  
    116         static void NewMail(const int reserve = 1) { 
    117                 sMailId += sReservedMailboxes; 
    118                 sReservedMailboxes = reserve; 
    119         } 
    120  
    121         void Mail() { mMailbox = sMailId; } 
    122         bool Mailed() const { return mMailbox == sMailId; } 
    123  
    124         void Mail(const int mailbox) { mMailbox = sMailId + mailbox; } 
    125         bool Mailed(const int mailbox) const { return mMailbox == sMailId + mailbox; } 
    126  
    127         int IncMail() { return ++ mMailbox - sMailId; } 
    128          
    129         ////////////////////////////////////////// 
    130  
    131 protected: 
    132  
    133         int mMailbox; 
    134  
    135 }; 
    13613 
    13714 
     
    14017{ 
    14118public: 
    142          
    143         PvsIterator<T, S>() {} 
    144  
     19        PvsIterator<T, S>(){} 
    14520        PvsIterator<T, S>(const typename vector<PvsEntry<T, S> >::const_iterator &itCurrent, 
    14621                                          const typename vector<PvsEntry<T, S> >::const_iterator &itEnd): 
     
    15833                return *(mItCurrent ++); 
    15934        } 
    160          
     35 
    16136private: 
    16237        typename vector<PvsEntry<T, S> >::const_iterator mItCurrent; 
     
    16944*/ 
    17045template<typename T, typename S> 
    171 class Pvs 
    172 { 
    173         template<typename T, typename S> 
    174                 friend class PvsIterator; 
     46class VerbosePvs 
     47{ 
     48        template<typename T, typename S> friend class PvsIterator; 
    17549 
    17650public: 
    17751 
    178         Pvs(): mSamples(0), mEntries(), mLastSorted(0), mQueriesSinceSort(0) {} 
     52        VerbosePvs(): mSamples(0), mEntries(), mLastSorted(0), mQueriesSinceSort(0) {} 
    17953 
    18054        /** creates pvs and initializes it with the given entries.  
    18155        Assumes that entries are sorted- 
    18256        */ 
    183         Pvs(const vector<PvsEntry<T, S> > &samples); 
    184         virtual ~Pvs() {}; 
     57        VerbosePvs(const vector<PvsEntry<T, S> > &samples); 
     58        virtual ~VerbosePvs() {}; 
    18559 
    18660        /** Compresses PVS lossless or lossy. 
     
    21488        Warning: very slow! 
    21589        */ 
    216         void MergeInPlace(const Pvs<T, S> &a); 
     90        void MergeInPlace(const VerbosePvs<T, S> &a); 
    21791 
    21892        /** Difference of pvs to pvs b. 
    21993        @returns number of different entries. 
    22094        */ 
    221         int Diff(const Pvs<T, S> &b); 
     95        int Diff(const VerbosePvs<T, S> &b); 
    22296 
    22397        /** Finds sample in PVS. 
     
    246120                first checks if sample is already in clean part of the pvs. 
    247121        */ 
    248         bool AddSampleDirtyCheck(T sample, const float pdf);//, float &contribution); 
     122        bool AddSampleDirtyCheck(T sample, const float pdf); 
    249123 
    250124        /** Sort pvs entries - this should always be called after a 
     
    260134                @returns PvsData 
    261135        */ 
    262         typename std::vector<PvsEntry<T, S> >::iterator AddSample2(T sample, const float pdf); 
     136        typename std::vector<PvsEntry<T, S> >::iterator  
     137                AddSample2(T sample, const float pdf); 
    263138 
    264139        /** Subtracts one pvs from another one. 
     
    266141                @returns new pvs size 
    267142        */ 
    268         int SubtractPvs(const Pvs<T, S> &pvs); 
     143        int SubtractPvs(const VerbosePvs<T, S> &pvs); 
    269144 
    270145        /** Returns PVS data, i.e., how often it was seen from the view cell,  
     
    284159        /** Compute continuous PVS difference  
    285160        */ 
    286         void ComputeContinuousPvsDifference(Pvs<T, S> &pvs,  
     161        void ComputeContinuousPvsDifference(VerbosePvs<T, S> &pvs,  
    287162                                                                                float &pvsReduction, 
    288163                                                                                float &pvsEnlargement); 
     
    299174        /** Compute continuous PVS difference  
    300175        */ 
    301         float GetPvsHomogenity(Pvs<T, S> &pvs); 
    302  
    303         static void Merge(Pvs<T, S> &mergedPvs, const Pvs<T, S> &a, const Pvs<T, S> &b); 
     176        float GetPvsHomogenity(VerbosePvs<T, S> &pvs); 
     177 
     178        static void Merge(VerbosePvs<T, S> &mergedPvs,  
     179                                          const VerbosePvs<T, S> &a,  
     180                                          const VerbosePvs<T, S> &b); 
    304181 
    305182        int GetSamples() const 
     
    320197                const int dirtySize = n - mLastSorted; 
    321198 
    322 #define LOG2E 1.442695040f 
     199                const double LOG2E = 1.442695040f; 
    323200 
    324201                const float logN = log((float)max(1, n))/LOG2E; 
     
    344221        } 
    345222 
    346  
    347223        int GetLastSorted() const 
    348224        { 
     
    354230protected: 
    355231 
    356         static void Merge(Pvs<T, S> &mergedPvs,  
     232        static void Merge(VerbosePvs<T, S> &mergedPvs,  
    357233                                          const typename std::vector<PvsEntry<T, S> >::const_iterator &aBegin, 
    358234                                          const typename std::vector<PvsEntry<T, S> >::const_iterator &aEnd, 
     
    362238                                          const int bSamples); 
    363239 
     240        ////////////////////////////// 
     241 
    364242        /// vector of PVS entries 
    365243        vector<PvsEntry<T, S> > mEntries;  
     
    376254 
    377255template <typename T, typename S> 
    378 Pvs<T, S>::Pvs(const vector<PvsEntry<T, S> > &samples) 
     256VerbosePvs<T, S>::VerbosePvs(const vector<PvsEntry<T, S> > &samples) 
    379257{ 
    380258        mEntries.reserve(samples.size()); 
     
    386264 
    387265template <typename T, typename S> 
    388 void Pvs<T, S>::Sort() 
    389 { 
    390         std::vector<PvsEntry<T, S> >::iterator it = mEntries.begin() + mLastSorted; 
    391         std::vector<PvsEntry<T, S> >::iterator it_end = mEntries.end(); 
     266void VerbosePvs<T, S>::Sort() 
     267{ 
     268        vector<PvsEntry<T, S> >::iterator it = mEntries.begin() + mLastSorted; 
     269        vector<PvsEntry<T, S> >::iterator it_end = mEntries.end(); 
    392270 
    393271        // throw out double entries 
     
    411289 
    412290template <typename T, typename S> 
    413 void Pvs<T, S>::SimpleSort() 
     291void VerbosePvs<T, S>::SimpleSort() 
    414292{ 
    415293        //  sort(mEntries.begin(), mEntries.end()); 
    416         std::vector<PvsEntry<T, S> >::iterator it = mEntries.begin() + mLastSorted; 
     294        vector<PvsEntry<T, S> >::iterator it = mEntries.begin() + mLastSorted; 
    417295 
    418296        sort(it, mEntries.end()); 
     
    431309template <typename T, typename S> 
    432310void 
    433 Pvs<T, S>::ComputeContinuousPvsDifference(Pvs<T, S> &b, 
     311VerbosePvs<T, S>::ComputeContinuousPvsDifference(VerbosePvs<T, S> &b, 
    434312                                                                                  float &pvsReduction, 
    435313                                                                                  float &pvsEnlargement) 
     
    439317 
    440318        // Uses sum of log differences, which corresponds to entropy 
    441         std::vector<PvsEntry<T, S> >::iterator it; 
     319        vector<PvsEntry<T, S> >::iterator it; 
    442320 
    443321        for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)  
     
    514392 
    515393template <typename T, typename S> 
    516 int Pvs<T, S>::Diff(const Pvs<T, S> &b) 
     394int VerbosePvs<T, S>::Diff(const VerbosePvs<T, S> &b) 
    517395{ 
    518396        int dif = 0; 
     
    533411 
    534412template <typename T, typename S>  
    535 void Pvs<T, S>::MergeInPlace(const Pvs<T, S> &a) 
     413void VerbosePvs<T, S>::MergeInPlace(const VerbosePvs<T, S> &a) 
    536414{ 
    537415        // early exit 
     
    559437 
    560438template <typename T, typename S> 
    561 void Pvs<T, S>::Merge(Pvs<T, S> &mergedPvs, const Pvs<T, S> &a, const Pvs<T, S> &b) 
    562 { 
    563         std::vector<PvsEntry<T, S> >::const_iterator ait = a.mEntries.begin(), ait_end = a.mEntries.end(); 
    564         std::vector<PvsEntry<T, S> >::const_iterator bit = b.mEntries.begin(), bit_end = b.mEntries.end(); 
     439void VerbosePvs<T, S>::Merge(VerbosePvs<T, S> &mergedPvs,  
     440                                                         const VerbosePvs<T, S> &a,  
     441                                                         const VerbosePvs<T, S> &b) 
     442{ 
     443        std::vector<PvsEntry<T, S> >::const_iterator ait =  
     444                a.mEntries.begin(), ait_end = a.mEntries.end(); 
     445        std::vector<PvsEntry<T, S> >::const_iterator bit =  
     446                b.mEntries.begin(), bit_end = b.mEntries.end(); 
    565447         
    566448        Merge(mergedPvs,  
     
    573455 
    574456template <typename T, typename S> 
    575 void Pvs<T, S>::Merge(Pvs<T, S> &mergedPvs,  
     457void VerbosePvs<T, S>::Merge(VerbosePvs<T, S> &mergedPvs,  
    576458                                          const typename std::vector<PvsEntry<T, S> >::const_iterator &aBegin, 
    577459                                          const typename std::vector<PvsEntry<T, S> >::const_iterator &aEnd, 
     
    627509 
    628510 
    629 template <typename T, typename S> void Pvs<T, S>::Clear(const bool trim = true) 
     511template <typename T, typename S> void VerbosePvs<T, S>::Clear(const bool trim = true) 
    630512{ 
    631513        mEntries.clear(); 
     
    640522 
    641523 
    642 template <typename T, typename S> void Pvs<T, S>::Trim() 
     524template <typename T, typename S> void VerbosePvs<T, S>::Trim() 
    643525{ 
    644526        vector<PvsEntry<T,S> >(mEntries).swap(mEntries); 
     
    647529 
    648530template <typename T, typename S>  
    649 bool Pvs<T, S>::Find(T sample, 
     531bool VerbosePvs<T, S>::Find(T sample, 
    650532                                         typename vector<PvsEntry<T, S> >::iterator &it, 
    651533                                         const bool checkDirty) 
     
    684566 
    685567template <typename T, typename S> 
    686 void Pvs<T, S>::GetData(const int index, T &entry, S &data) 
     568void VerbosePvs<T, S>::GetData(const int index, T &entry, S &data) 
    687569{ 
    688570        std::vector<PvsEntry<T, S> >::iterator i = mEntries.begin(); 
     
    695577 
    696578template <typename T, typename S> 
    697 float Pvs<T, S>::AddSample(T sample, const float pdf) 
     579float VerbosePvs<T, S>::AddSample(T sample, const float pdf) 
    698580{ 
    699581        ++ mSamples; 
     
    719601 
    720602template <typename T, typename S> 
    721 void Pvs<T, S>::AddSampleDirty(T sample, const float pdf) 
     603void VerbosePvs<T, S>::AddSampleDirty(T sample, const float pdf) 
    722604{ 
    723605        ++ mSamples; 
     
    727609 
    728610template <typename T, typename S> 
    729 typename vector< PvsEntry<T, S> >::iterator Pvs<T, S>::AddSample2(T sample,  
     611typename vector< PvsEntry<T, S> >::iterator VerbosePvs<T, S>::AddSample2(T sample,  
    730612                                                                                                                                  const float pdf) 
    731613{ 
     
    751633 
    752634 
    753 /** Adds sample dirty (on the end of the vector) but 
    754         first checks if sample is already in clean part of the pvs. 
    755 */ 
    756635template <typename T, typename S>  
    757 bool Pvs<T, S>::AddSampleDirtyCheck(T sample, 
     636bool VerbosePvs<T, S>::AddSampleDirtyCheck(T sample, 
    758637                                                                        const float pdf) 
    759638                                                                        //,float &contribution) 
     
    784663 
    785664template <typename T, typename S> 
    786 bool Pvs<T, S>::GetSampleContribution(T sample, 
     665bool VerbosePvs<T, S>::GetSampleContribution(T sample, 
    787666                                                                          const float pdf, 
    788667                                                                          float &contribution)  
     
    806685 
    807686template <typename T, typename S> 
    808 bool Pvs<T, S>::RemoveSample(T sample, const float pdf) 
     687bool VerbosePvs<T, S>::RemoveSample(T sample, const float pdf) 
    809688{ 
    810689        -- mSamples; 
     
    831710 
    832711template <typename T, typename S> 
    833 int Pvs<T, S>::SubtractPvs(const Pvs<T, S> &pvs) 
     712int VerbosePvs<T, S>::SubtractPvs(const VerbosePvs<T, S> &pvs) 
    834713{ 
    835714        const int samples = mSamples - pvs.mSamples; 
     
    849728 
    850729template <typename T, typename S> 
    851 void Pvs<T, S>::CollectEntries(std::vector<T> &entries) 
     730void VerbosePvs<T, S>::CollectEntries(std::vector<T> &entries) 
    852731{ 
    853732        std::vector<PvsEntry<T, S> >:: 
     
    861740 
    862741template <typename T, typename S> 
    863 void Pvs<T, S>::NormalizeMaximum() 
     742void VerbosePvs<T, S>::NormalizeMaximum() 
    864743{ 
    865744        std::vector<PvsEntry<T, S> >:: 
     
    880759                (*it)->second.sumPdf *= maxSum; 
    881760        } 
    882  
    883 } 
    884  
    885  
    886 template <typename T, typename S> 
    887 float Pvs<T, S>::GetEntrySize() 
     761} 
     762 
     763 
     764template <typename T, typename S> 
     765float VerbosePvs<T, S>::GetEntrySize() 
    888766{ 
    889767        return (float)(sizeof(T) + sizeof(S)) / float(1024 * 1024); 
     
    892770 
    893771template <typename T, typename S> 
    894 int Pvs<T, S>::GetEntrySizeByte() 
     772int VerbosePvs<T, S>::GetEntrySizeByte() 
    895773{ 
    896774        return sizeof(T) + sizeof(S); 
     
    899777 
    900778template <typename T, typename S> 
    901 float Pvs<T, S>::GetPvsHomogenity(Pvs<T, S> &pvs)  
     779float VerbosePvs<T, S>::GetPvsHomogenity(VerbosePvs<T, S> &pvs)  
    902780{ 
    903781        float pvsReduction, pvsEnlargement; 
    904782 
    905783        ComputeContinuousPvsDifference(pvs,     pvsReduction, pvsEnlargement); 
    906  
    907784        return pvsReduction + pvsEnlargement; 
    908785} 
     
    910787 
    911788template <typename T, typename S> 
    912 typename PvsIterator<T, S> Pvs<T, S>::GetIterator() const 
     789typename PvsIterator<T, S> VerbosePvs<T, S>::GetIterator() const 
    913790{ 
    914791        PvsIterator<T, S> pit(mEntries.begin(), mEntries.end()); 
    915  
    916792        return pit; 
    917793} 
    918794 
    919  
    920 /////////////////////////////////////// 
    921  
    922 /** Class instantiating the Pvs template for kd tree nodes. 
    923 */ 
    924 class KdPvs: public Pvs<KdNode *, PvsData> 
    925 { 
    926 public: 
    927         int Compress(); 
    928 }; 
    929  
    930  
    931 //////////// 
    932 //-- typedefs 
    933  
    934 typedef PvsEntry<Intersectable *, PvsData> ObjectPvsEntry; 
    935 typedef std::vector<ObjectPvsEntry> ObjectPvsEntries; 
    936 typedef Pvs<ViewCell *, MailablePvsData> ViewCellPvs; 
    937 typedef PvsIterator<Intersectable *, PvsData> ObjectPvsIterator; 
    938  
    939  
    940 class ObjectPvs: public Pvs<Intersectable *, PvsData> 
    941 { 
    942 public: 
    943         /** Counts object int the pvs. Different to method "GetSize", not 
    944                 only the raw container size is returned, 
    945                 but the individual contributions of the entries are summed up. 
    946         */ 
    947         float EvalPvsCost() const; 
    948  
    949         friend ostream &operator<<(ostream &s, const ObjectPvs &p)  
    950         { 
    951                 ObjectPvsIterator pit = p.GetIterator(); 
    952  
    953                 while (pit.HasMoreEntries()) 
    954                 {                
    955                         const ObjectPvsEntry &entry = pit.Next(); 
    956                         Intersectable *obj = entry.mObject; 
    957  
    958                         cout << obj << " "; 
    959                 } 
    960                  
    961                 return s; 
    962         } 
    963 }; 
    964  
    965  
    966  
    967795} 
    968796 
Note: See TracChangeset for help on using the changeset viewer.