source: GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h @ 1998

Revision 1998, 20.2 KB checked in by mattausch, 17 years ago (diff)

changed to struct

RevLine 
[177]1#ifndef __PVS_H
2#define __PVS_H
3
4#include <map>
[469]5#include <vector>
[1189]6#include "common.h"
[177]7
[860]8namespace GtpVisibilityPreprocessor {
9
[177]10class KdNode;
[240]11class BspNode;
[191]12class Ray;
[308]13class Intersectable;
[1077]14class ViewCell;
[177]15
[1077]16
[1740]17/** Information stored with a PVS entry. Consists of the number
18        the object was seen from the view cell.
19*/
20template<typename T, typename S>
[1998]21struct PvsEntry
[1189]22{
[1740]23public:
24
[1741]25        PvsEntry() {}
26
[1740]27        PvsEntry(T sample, const S &data): mObject(sample), mData(data) {}
28
29        T mObject;
30        S mData;
31
32        template<typename T, typename S>
33        friend int operator< (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b);
[1789]34        template<typename T, typename S>
35        friend int operator== (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b);
[1740]36};
37
38
39template<typename T, typename S>
40int operator< (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b)
41{
42        return a.mObject < b.mObject;
43}
44
[1789]45template<typename T, typename S>
46int operator== (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b)
47{
48        return a.mObject == b.mObject;
49}
[1740]50
[1789]51
[1740]52template<typename T, typename S>
53struct LtSample
54{
55    bool operator()(const PvsEntry<T, S> &a, const PvsEntry<T, S> &b) const
[310]56    {
[1740]57                return a.mObject < b.mObject;
[310]58        }
59};
60
[1789]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}
[1740]66
[469]67/** Information stored with a PVS entry. Consists of the number
68        the object was seen from the view cell.
69*/
[1998]70struct PvsData {
[1189]71public:
[1706]72        PvsData() {}
73        PvsData(const float sumPdf):
[1189]74        mSumPdf(sumPdf) {}
[1740]75       
[1706]76        // $$JB in order to return meaningfull values
77        // it assumes that the sum pdf has been normalized somehow!!!
[1740]78        inline float GetVisibility()
[1706]79        {
80                return mSumPdf;
81        }
[1740]82
83        /// sum of probability density of visible sample rays
84        float mSumPdf;
[1189]85};
86
87
88class MailablePvsData
89{
[1740]90public:
91        // sum of probability density of visible sample rays
92        float mSumPdf;
93        int mCounter;
94
95        MailablePvsData() {}
96        MailablePvsData(const float sumPdf):
97        mSumPdf(sumPdf) {}
98
99        // $$JB in order to return meaningfull values
100        // it assumes that the sum pdf has been normalized somehow!!!
101        float GetVisibility()
102        {
103                return mSumPdf;
104        }
105
[1706]106        ////////////////////////////
107        //  Mailing stuff
[1184]108
[1706]109        // last mail id -> warning not thread safe!
110        // both mailId and mailbox should be unique for each thread!!!
111        static int sMailId;
112        static int sReservedMailboxes;
113
[1184]114        static void NewMail(const int reserve = 1) {
115                sMailId += sReservedMailboxes;
116                sReservedMailboxes = reserve;
117        }
[1706]118
[1184]119        void Mail() { mMailbox = sMailId; }
120        bool Mailed() const { return mMailbox == sMailId; }
121
122        void Mail(const int mailbox) { mMailbox = sMailId + mailbox; }
123        bool Mailed(const int mailbox) const { return mMailbox == sMailId + mailbox; }
124
125        int IncMail() { return ++ mMailbox - sMailId; }
[1740]126       
[1706]127        //////////////////////////////////////////
[1184]128
[1740]129protected:
[1184]130
[1740]131        int mMailbox;
[556]132
[310]133};
[1740]134
135
[1742]136template<typename T, typename S>
137class PvsIterator
138{
139public:
140PvsIterator<T, S>(){}
141        PvsIterator<T, S>(const typename vector<PvsEntry<T, S> >::const_iterator &itCurrent,
142                                          const typename vector<PvsEntry<T, S> >::const_iterator &itEnd):
143        mItCurrent(itCurrent), mItEnd(itEnd)
144        {
145        }
146
147        bool HasMoreEntries() const
148        {
149                return (mItCurrent != mItEnd);
150        }
151
[1744]152        const PvsEntry<T, S> &Next()
[1742]153        {
154                return *(mItCurrent ++);
155        }
156       
157private:
158        typename vector<PvsEntry<T, S> >::const_iterator mItCurrent;
159        typename vector<PvsEntry<T, S> >::const_iterator mItEnd;
[1740]160};
161
162
[469]163/** Template class representing the Potentially Visible Set (PVS)
164        mainly from a view cell, but also e.g., from objects.
165*/
[1189]166template<typename T, typename S>
[311]167class Pvs
[310]168{
[1740]169        template<typename T, typename S>
170        friend class PvsIterator;
171
[310]172public:
[1738]173
[1757]174        Pvs(): mSamples(0), mEntries(), mLastSorted(0) {}
[1738]175
[1742]176        /** creates pvs and initializes it with the given entries.
177                Assumes that entries are sorted-
178        */
[1740]179        Pvs(const vector<PvsEntry<T, S> > &samples);
[1742]180        virtual ~Pvs() {};
[492]181
[1706]182        /** Compresses PVS lossless or lossy.
183        */
184        int Compress() {return 0;}
185        int GetSize() const {return (int)mEntries.size();}
[1925]186        bool Empty() const {return mEntries.empty();}
[469]187
[1925]188        void Reserve(const int n) { mEntries.reserve(n); }
189
[1740]190        /** Normalize the visibility of entries in order to get
191                comparable results.
[1706]192        */
193        void NormalizeMaximum();
[1184]194
[1706]195        /** Merges pvs of a into this pvs.
[1740]196                Warning: very slow!
[1706]197        */
[1740]198        void MergeInPlace(const Pvs<T, S> &a);
[752]199
[1706]200        /** Difference of pvs to pvs b.
201                @returns number of different entries.
202        */
203        int Diff(const Pvs<T, S> &b);
[469]204
[1706]205        /** Finds sample in PVS.
[1789]206                @param checkDirty if dirty part of the pvs should be checked for entry
207                        (warning: linear runtime in dirty part)
[1740]208                @returns iterator on the sample.
[1706]209        */
[1790]210        bool Find(T sample,
211                          typename vector<PvsEntry<T, S> >::iterator &it,
212                          const bool checkDirty = true);
[469]213
[1706]214        bool GetSampleContribution(T sample, const float pdf, float &contribution);
[485]215
[1706]216        /** Adds sample to PVS.
217                @returns contribution of sample (0 or 1)
218        */
219        float AddSample(T sample, const float pdf);
[1757]220
[1789]221        /** Adds sample to PVS without checking for presence of the sample
222                pvs remains unsorted!
223        */
224        void AddSampleDirty(T sample, const float pdf);
[1757]225
[1789]226        /** Adds sample dirty (on the end of the vector) but
227                first checks if sample is already in clean part of the pvs.
228        */
229        bool AddSampleDirtyCheck(T sample, const float pdf);//, float &contribution);
230
231        /** Sort pvs entries - this should always be called after a
232                sequence of AddSampleDirty calls
233        */
234        void Sort();
235
[1877]236  /** Sort pvs entries assume that the pvs contains unique entries
237   */
238  void SimpleSort();
239
[1742]240        /** Adds sample to PVS. Assumes that the pvs is sorted
[1740]241                @returns contribution of sample (0 or 1)
242        */
[1844]243        //float AddSamples(const vector<PvsEntry<T, S> > &samples);
[752]244
[1706]245        /** Adds sample to PVS.
246                @returns PvsData
247        */
[1740]248        typename std::vector<PvsEntry<T, S> >::iterator AddSample2(T sample, const float pdf);
[1667]249
[1706]250        /** Subtracts one pvs from another one.
251                WARNING: could contains bugs
252                @returns new pvs size
253        */
254        int SubtractPvs(const Pvs<T, S> &pvs);
[1740]255
[1706]256        /** Returns PVS data, i.e., how often it was seen from the view cell,
257                and the object itsef.
258        */
259        void GetData(const int index, T &entry, S &data);
260
261        /** Collects the PVS entries and returns them in the vector.
262        */
263        void CollectEntries(std::vector<T> &entries);
264
265        /** Removes sample from PVS if reference count is zero.
266                @param visibleSamples number of references to be removed
267        */
268        bool RemoveSample(T sample, const float pdf);
269
270        /** Compute continuous PVS difference
271        */
[1740]272        void ComputeContinuousPvsDifference(Pvs<T, S> &pvs,
273                                                                                float &pvsReduction,
274                                                                                float &pvsEnlargement);
[1706]275
276        /** Clears the pvs.
277        */
[1750]278        void Clear(const bool trim = true);
[1706]279
[1750]280        void Trim();
281
[1706]282        static int GetEntrySizeByte();
283        static float GetEntrySize();
284
[1740]285        /** Compute continuous PVS difference
286        */
287        float GetPvsHomogenity(Pvs<T, S> &pvs);
[1706]288
[1740]289        static void Merge(Pvs<T, S> &mergedPvs, const Pvs<T, S> &a, const Pvs<T, S> &b);
[1706]290
[1789]291        static void Merge(Pvs<T, S> &mergedPvs,
292                                          const typename std::vector<PvsEntry<T, S> >::const_iterator &aBegin,
293                                          const typename std::vector<PvsEntry<T, S> >::const_iterator &aEnd,
294                                          const typename std::vector<PvsEntry<T, S> >::const_iterator &bBegin,
295                                          const typename std::vector<PvsEntry<T, S> >::const_iterator &bEnd,
296                                          const int aSamples,
297                                          const int bSamples);
298
[1742]299        int GetSamples() const
300        {
301                return mSamples;
302        }
[1738]303
[1789]304
305        bool IsDirty() const
306        {
307                return mLastSorted < mEntries.size();
308        }
309
310        bool RequiresResort() const
311        {
312                // the last part should not be more than log of the sorted part. this
313                // way we can achieve logarithmic behaviour for insertion and find
314                const int dirtySize = (int)mEntries.size() - mLastSorted;
[1877]315                return dirtySize > 4*(int)(log((double)mEntries.size()) / log(2.0));
[1789]316        }
317
318
319        int GetLastSorted() const
320        {
321                return mLastSorted;
322        }
323
[1742]324        typename PvsIterator<T, S> GetIterator() const;
325
326protected:
327
[1738]328        /// vector of PVS entries
[1740]329        vector<PvsEntry<T, S> > mEntries;
[1736]330       
331        /// Number of samples used to create the PVS
332        int mSamples;
[1757]333 
[1789]334        /// Last sorted entry in the pvs (important for find and merge)
335        int mLastSorted; 
[310]336};
337
[581]338
[1740]339template <typename T, typename S>
340Pvs<T, S>::Pvs(const vector<PvsEntry<T, S> > &samples)
341{
342        mEntries.reserve(samples.size());
343        mEntries = samples;
[1757]344        mLastSorted = 0;
[1789]345        mSamples = samples.size();
[1740]346}
[677]347
[1789]348
[1757]349template <typename T, typename S>
350void Pvs<T, S>::Sort()
351{
[1789]352        std::vector<PvsEntry<T, S> >::iterator it = mEntries.begin() + mLastSorted;
353        //std::vector<PvsEntry<T, S> >::const_iterator it = mEntries.begin() + mLastSorted;
354        std::vector<PvsEntry<T, S> >::iterator it_end = mEntries.end();
355
356        // throw out double entries
357        std::vector<PvsEntry<T, S> >::iterator newEnd = unique(it, it_end);
358        sort(it, newEnd);
359        //sort(mEntries.begin(), mEntries.end());
360
361        // now merge sorted ranges
362        ObjectPvs newPvs;
363        Merge(newPvs,
364                  mEntries.begin(), it,
365                  it, newEnd,
366                  mSamples, 0);
367       
368        mEntries = newPvs.mEntries;
369        mLastSorted = (int)mEntries.size();
[1757]370}
[1740]371
[1877]372template <typename T, typename S>
373void Pvs<T, S>::SimpleSort()
374{
375  sort(mEntries.begin(), mEntries.end());
376  mLastSorted = (int)mEntries.size();
377}
[1789]378
[1877]379
[695]380/**
381   Compute continuous PVS difference of 'b' with respect to the base PVS (*this).
382   Provides separatelly PVS reduction from PVS enlargement.
383
384*/
[1189]385template <typename T, typename S>
[677]386void
[1189]387Pvs<T, S>::ComputeContinuousPvsDifference(Pvs<T, S> &b,
[1740]388                                                                                  float &pvsReduction,
389                                                                                  float &pvsEnlargement)
[677]390{
[705]391        pvsReduction = 0.0f;
392        pvsEnlargement = 0.0f;
[1740]393
[1738]394        // Uses sum of log differences, which corresponds to entropy
[1740]395        std::vector<PvsEntry<T, S> >::iterator it;
[1738]396
397        for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
[1189]398        {
[1740]399                float bSumPdf = (*it).mData.mSumPdf;
[1738]400                float aSumPdf = 0.0f;
[713]401
[1790]402                vector<PvsEntry<T, S> >::iterator oit;
403                const bool entryFound = Find((*it).mObject, oit);               
[1740]404
405                if (entryFound)
[1738]406                {
[1740]407                        aSumPdf = (*it).mData.mSumPdf;
408
[1738]409                        // mark this entry as processed to avoid double counting
[1740]410                        (*it).mData.mSumPdf = -aSumPdf;
[1738]411                }
412
[713]413#if 0
[1740]414                const float diff = bSumPdf - aSumPdf;
[1738]415
416                if (diff > 0.0f) {
417                        pvsEnlargement += diff;
418                } else {
419                        pvsReduction += -diff;
420                }
[713]421#else
[1740]422                if (!entryFound)
[1738]423                        pvsEnlargement += 1.0f;
[713]424#endif
[1738]425        }
426
[1740]427        for (it = mEntries.begin(); it != mEntries.end(); ++ it)
428        {
429                float aSumPdf = (*it).mData.mSumPdf;
[1738]430                float bSumPdf = 0.0f;
431                if (aSumPdf < 0.0f) {
[1740]432               
[1738]433                        // this entry was already accounted for!
434                        // just revert it back
[1740]435                        (*it).mData.mSumPdf = -aSumPdf;
[1738]436                } else {
[1790]437                        vector<PvsEntry<T, S> >::iterator oit;
438               
439                        const bool entryFound = b.Find((*it).mObject, oit);
440                                               
[1740]441                        if (entryFound) {
442                                bSumPdf = (*oit).mData.mSumPdf;
[1738]443                        }
[713]444#if 0
[1740]445                        const float diff = bSumPdf - aSumPdf;
[713]446
[1738]447                        if (diff > 0.0f) {
448                                pvsEnlargement += diff;
449                        } else {
450                                pvsReduction += -diff;
451                        }
452
[713]453#else
[1740]454                        if (!entryFound)
[1738]455                                pvsReduction += 1.0f;
[713]456#endif
[1738]457                }
[695]458        }
[677]459}
460
[1738]461
[1189]462template <typename T, typename S>
463int Pvs<T, S>::Diff(const Pvs<T, S> &b)
[362]464{
465        int dif = 0;
466
[1740]467        std::vector<PvsEntry<T, S> >::const_iterator it;
[362]468
469        for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
470        {
[1790]471                vector<PvsEntry<T, S> >::iterator bit;
472                const bool entryFound = Find((*it).first, bit);
473
474                if (!entryFound) ++ dif;
[362]475        }
476
477        return dif;
478}
479
[1740]480
481template <typename T, typename S>
482void Pvs<T, S>::MergeInPlace(const Pvs<T, S> &a)
[341]483{
[1786]484        // early exit
485        if (a.Empty())
486        {
487                return;
488        }
489        else if (Empty())
490        {
491                mEntries.reserve(a.GetSize());
492                mEntries = a.mEntries;
493                mSamples = a.mSamples;
494                return;
495        }
496
[1740]497        ObjectPvs interPvs;
498       
499        Merge(interPvs, *this, a);
500       
501        mEntries.reserve(interPvs.GetSize());
502        mEntries = interPvs.mEntries;
[1751]503        mSamples = interPvs.mSamples;
[1740]504}
505
506
507template <typename T, typename S>
508void Pvs<T, S>::Merge(Pvs<T, S> &mergedPvs, const Pvs<T, S> &a, const Pvs<T, S> &b)
509{
510        std::vector<PvsEntry<T, S> >::const_iterator ait = a.mEntries.begin(), ait_end = a.mEntries.end();
511        std::vector<PvsEntry<T, S> >::const_iterator bit = b.mEntries.begin(), bit_end = b.mEntries.end();
[1741]512       
[1789]513        Merge(mergedPvs,
514                  ait, ait_end,
515                  bit, bit_end,
516                  a.mSamples,
517                  b.mSamples);
518}
519
520
521template <typename T, typename S>
522void Pvs<T, S>::Merge(Pvs<T, S> &mergedPvs,
523                                          const typename std::vector<PvsEntry<T, S> >::const_iterator &aBegin,
524                                          const typename std::vector<PvsEntry<T, S> >::const_iterator &aEnd,
525                                          const typename std::vector<PvsEntry<T, S> >::const_iterator &bBegin,
526                                          const typename std::vector<PvsEntry<T, S> >::const_iterator &bEnd,
527                                          const int aSamples,
528                                          const int bSamples)
529{
530        std::vector<PvsEntry<T, S> >::const_iterator ait = aBegin;
531        std::vector<PvsEntry<T, S> >::const_iterator bit = bBegin;
532       
533        for (; (ait != aEnd); ++ ait)
[1741]534        {
535                Intersectable *aObj = (*ait).mObject;
536                Intersectable *bObj = NULL;
[1789]537                //Intersectable *bObjOld = NULL;
538       
539                const PvsEntry<T, S> &aEntry = (*ait);
[1740]540
[1789]541                for (; (bit != bEnd) && ((*bit).mObject <= (*ait).mObject); ++ bit)
[1741]542                {
543                        bObj = (*bit).mObject;
544
545                        // object found => add up probabilities
[1742]546                        if (bObj == aEntry.mObject)
[1741]547                        {
548                                PvsData newData(aEntry.mData.mSumPdf + (*bit).mData.mSumPdf);
549                                PvsEntry<T, S> entry(bObj, newData);
550                                mergedPvs.mEntries.push_back(entry);
551                        }
552                        else
553                        {
554                                mergedPvs.mEntries.push_back(*bit);
555                        }
[1789]556                       
557                        //bObjOld = bObj;
[1741]558                }
559
560                // only push back if objects different
561                // (equal case is handled by second loop)
562                if (aObj != bObj)
563                {
564                        mergedPvs.mEntries.push_back(*ait);
565                }
566        }
567
568        // add the rest
[1789]569        for (; (bit != bEnd); ++ bit)
[1740]570        {
[1741]571                mergedPvs.mEntries.push_back(*bit);
572        }
[1789]573
574        mergedPvs.mSamples = aSamples + bSamples;
[1740]575}
576
577
[1750]578template <typename T, typename S> void Pvs<T, S>::Clear(const bool trim = true)
[752]579{
580        mEntries.clear();
[1750]581        mSamples = 0;
[1789]582        mLastSorted = 0;
[1786]583
584        if (trim)
585        {
586                vector<PvsEntry<T,S> >().swap(mEntries);
587        }
[752]588}
589
590
[1750]591template <typename T, typename S> void Pvs<T, S>::Trim()
592{
[1786]593        vector<PvsEntry<T,S> >(mEntries).swap(mEntries);
[1750]594}
595
596
[1189]597template <typename T, typename S>
[1790]598bool Pvs<T, S>::Find(T sample,
599                                         typename vector<PvsEntry<T, S> >::iterator &it,
600                                         const bool checkDirty)
[310]601{
[1877]602  bool found = false;
603 
604  PvsEntry<T, S> dummy(sample, PvsData());
605 
606  // only check clean part
607  vector<PvsEntry<T, S> >::iterator sorted_end = mEntries.begin() + mLastSorted;
608 
609  // binary search
610  it = lower_bound(mEntries.begin(), sorted_end, dummy);
611 
612  if ((it != mEntries.end()) && ((*it).mObject == sample))
613        found = true;
614 
615  // sample not found yet => search further in the unsorted part
616  if (!found && checkDirty) {
[1790]617       
[1942]618        for (it = sorted_end; (it != mEntries.end()) && ((*it).mObject != sample); ++ it) ;
[1877]619       
[1942]620        if (it != mEntries.end())
[1877]621          found = true;
622  }
623 
624  return found;
[310]625}
626
[1740]627
[1189]628template <typename T, typename S>
[1740]629void Pvs<T, S>::GetData(const int index, T &entry, S &data)
[310]630{
[1740]631        std::vector<PvsEntry<T, S> >::iterator i = mEntries.begin();
[1738]632        for (int k = 0; k != index && i != mEntries.end(); ++ i, ++ k);
[310]633
[1738]634        entry = (*i).first;
635        data = (*i).second;
[310]636}
637
[1738]638
[1189]639template <typename T, typename S>
[1740]640float Pvs<T, S>::AddSample(T sample, const float pdf)
[177]641{
[1738]642        ++ mSamples;
[1790]643       
644        vector<PvsEntry<T, S> >::iterator it;
645        const bool entryFound = Find(sample, it);               
[1738]646
[1790]647        if (entryFound)
648        {       
[1740]649                S &data = (*it).mData;
650                data.mSumPdf += pdf;
651                return data.mSumPdf;
[1189]652        }
[1738]653        else
[1942]654          {
[1740]655                PvsEntry<T, S> entry(sample, pdf);
656                mEntries.insert(it, entry);
[1789]657                ++ mLastSorted;
[1738]658                return pdf;
[1942]659          }
[466]660}
[177]661
[1189]662
663template <typename T, typename S>
[1757]664void Pvs<T, S>::AddSampleDirty(T sample, const float pdf)
665{
[1789]666        ++ mSamples;
667        mEntries.push_back(PvsEntry<T, S>(sample, pdf));
[1757]668}
669                                         
670
671template <typename T, typename S>
[1790]672typename vector< PvsEntry<T, S> >::iterator Pvs<T, S>::AddSample2(T sample,
673                                                                                                                                  const float pdf)
[1184]674{
[1740]675        ++ mSamples;
[1789]676       
[1790]677        vector<PvsEntry<T, S> >::iterator it;
678        const bool entryFound == Find(sample, it);
[1740]679
[1790]680        if (entryFound)
[1189]681        {
[1740]682                S &data = (*it).second;
683                data->mSumPdf += pdf;
[1189]684        }
[1740]685        else
[1189]686        {
[1740]687                PvsEntry<T, S> entry(sample, pdf);
688                mEntries.insert(it, entry);
[1789]689                ++ mLastSorted;
[1189]690        }
[1740]691
692        return it;
[1184]693}
694
[1740]695
[1789]696/** Adds sample dirty (on the end of the vector) but
697        first checks if sample is already in clean part of the pvs.
698*/
699template <typename T, typename S>
700bool Pvs<T, S>::AddSampleDirtyCheck(T sample,
701                                                                        const float pdf)
702                                                                        //,float &contribution)
703{
704        ++ mSamples;
705
[1790]706        vector<PvsEntry<T, S> >::iterator it;
[1942]707        bool entryFound = Find(sample, it);
[1789]708
[1877]709        if (entryFound) {
710          S &data = (*it).mData;
711         
712          data.mSumPdf += pdf;
713          //contribution = pdf / data.mSumPdf;
714         
715          return false;
[1789]716        }
[1877]717        else {
718          AddSampleDirty(sample, pdf);
719          //contribution = 1.0f;
720          return true;
[1789]721        }
[311]722}
[308]723
[492]724
[1189]725template <typename T, typename S>
[1740]726bool Pvs<T, S>::GetSampleContribution(T sample,
727                                                                          const float pdf,
728                                                                          float &contribution)
[485]729{
[1790]730        vector<PvsEntry<T, S> >::iterator it;
731        const bool entryFound = Find(sample, it);
[1189]732
[1790]733        if (entryFound) 
[1740]734        {
735                S &data = (*it).mData;
736                contribution = pdf / (data.mSumPdf + pdf);
737                return false;
738        }
739        else
740        {
741                contribution = 1.0f;
742                return true;
743        }
[485]744}
745
[1740]746
[1189]747template <typename T, typename S>
[1740]748bool Pvs<T, S>::RemoveSample(T sample, const float pdf)
[485]749{
[1740]750        -- mSamples;
[1789]751       
[1790]752        vector<PvsEntry<T, S> >::iterator it;
753        const bool entryFound = Find(sample, it);
[1737]754
[1790]755        if (!entryFound)
[1740]756                return false;
757
758        S &data = (*it).mData;
759
760        data.mSumPdf -= pdf;
761
762        if (data.mSumPdf <= 0.0f)
763        {
764                mEntries.erase(it);
[1789]765                -- mLastSorted; // wrong if sample was in tail!!
[1740]766        }
767
768        return true;
[485]769}
[1740]770
771
[1189]772template <typename T, typename S>
773int Pvs<T, S>::SubtractPvs(const Pvs<T, S> &pvs)
[485]774{
[1738]775        const int samples = mSamples - pvs.mSamples;
[1740]776
777        std::vector<PvsEntry<T, S> >::
[1738]778                const_iterator it, it_end = pvs.mEntries.end();
[1737]779
[1738]780        // output PVS of view cell
781        for (it = pvs.mEntries.begin(); it != it_end; ++ it)
[1740]782                RemoveSample((*it).mObject, (*it).mData.mSumPdf);
[1738]783
784        mSamples = samples;
[1740]785
[1738]786        return GetSize();
[485]787}
788
[1740]789
[1189]790template <typename T, typename S>
791void Pvs<T, S>::CollectEntries(std::vector<T> &entries)
[469]792{
[1740]793        std::vector<PvsEntry<T, S> >::
[485]794                const_iterator it, it_end = mEntries.end();
[469]795
796        // output PVS of view cell
797        for (it = mEntries.begin(); it != it_end; ++ it)
798                entries.push_back((*it)->first);
799}
800
[1740]801
[1189]802template <typename T, typename S>
803void Pvs<T, S>::NormalizeMaximum()
[556]804{
[1740]805        std::vector<PvsEntry<T, S> >::
806                const_iterator it, it_end = mEntries.end();
[556]807
[1740]808        float maxPdfSum = -1.0f;
[556]809
[1740]810        // output PVS of view cell
811        for (it = mEntries.begin(); it != it_end; ++ it) {
812                float sum = (*it)->second.sumPdf;
813                if (sum > maxSum)
814                        maxSum = sum;
815        }
[556]816
[1740]817        maxSum = 1.0f / maxSum;
[556]818
[1740]819        for (it = mEntries.begin(); it != it_end; ++ it) {
820                (*it)->second.sumPdf *= maxSum;
821        }
822
[556]823}
824
825
[1667]826template <typename T, typename S>
827float Pvs<T, S>::GetEntrySize()
828{
[1673]829        return (float)(sizeof(T) + sizeof(S)) / float(1024 * 1024);
[1667]830}
831
832
833template <typename T, typename S>
834int Pvs<T, S>::GetEntrySizeByte()
835{
836        return sizeof(T) + sizeof(S);
837}
838
839
[1740]840template <typename T, typename S>
841float Pvs<T, S>::GetPvsHomogenity(Pvs<T, S> &pvs)
842{
843        float pvsReduction, pvsEnlargement;
844
845        ComputeContinuousPvsDifference(pvs,     pvsReduction, pvsEnlargement);
846
847        return pvsReduction + pvsEnlargement;
848}
849
850
[1742]851template <typename T, typename S>
852typename PvsIterator<T, S> Pvs<T, S>::GetIterator() const
853{
854        PvsIterator<T, S> pit(mEntries.begin(), mEntries.end());
855
856        return pit;
857}
858
859
[1667]860///////////////////////////////////////
861
[311]862/** Class instantiating the Pvs template for kd tree nodes.
863*/
[1189]864class KdPvs: public Pvs<KdNode *, PvsData>
[308]865{
[1141]866public:
[311]867        int Compress();
[308]868};
869
[1077]870
[1842]871////////////
872//-- typedefs
873
874typedef PvsEntry<Intersectable *, PvsData> ObjectPvsEntry;
875typedef std::vector<ObjectPvsEntry> ObjectPvsEntries;
876typedef Pvs<ViewCell *, MailablePvsData> ViewCellPvs;
877typedef PvsIterator<Intersectable *, PvsData> ObjectPvsIterator;
878
879
[1189]880class ObjectPvs: public Pvs<Intersectable *, PvsData>
[1141]881{
882public:
[1586]883        /** Counts object int the pvs. Different to method "GetSize", not
[1141]884                only the raw container size is returned,
885                but the individual contributions of the entries are summed up.
886        */
[1707]887        float EvalPvsCost() const;
[1842]888
889        friend ostream &operator<<(ostream &s, const ObjectPvs &p)
890        {
891                ObjectPvsIterator pit = p.GetIterator();
892
893                while (pit.HasMoreEntries())
894                {               
895                        const ObjectPvsEntry &entry = pit.Next();
896                        Intersectable *obj = entry.mObject;
897
[1845]898                        cout << obj << " ";
[1842]899                }
900               
901                return s;
902        }
[1141]903};
904
[1586]905
[1077]906
[860]907}
[469]908
[177]909#endif
910
Note: See TracBrowser for help on using the repository browser.