source: GTP/trunk/Lib/Vis/Preprocessing/src/BitVectorPvs.h @ 2117

Revision 2117, 7.6 KB checked in by mattausch, 17 years ago (diff)

implemented bit pvs (warnin: only worjs for preprocessing)

Line 
1#ifndef __BITVECTORPVS_H
2#define __BITVECTORPVS_H
3
4#include <vector>
5#include "common.h"
6#include "PvsBase.h"
7
8using namespace std;
9
10namespace GtpVisibilityPreprocessor {
11
12// specialisation of vector<bool>
13typedef vector<bool> bit_vector;
14
15
16/** Iterator over a bitvector pvs.
17*/
18template<typename T, typename S>
19class BitVectorPvsIterator
20{
21public:
22       
23        BitVectorPvsIterator<T, S>(const typename bit_vector::const_iterator &itBegin,
24                                                           const typename bit_vector::const_iterator &itEnd
25                                                           //,const int lastElement
26                                                           ):
27        mItCurrent(itBegin), mItEnd(itEnd), mDistance(0)//, mLastElement
28        {
29        }
30
31        bool HasMoreEntries() const
32        {
33                // find next element in bit vector
34                // warning: has to do traversal each time
35                typename bit_vector::const_iterator itNext = mItCurrent;
36
37                for (;(itNext != mItEnd) && !(*itNext); ++ itNext);
38
39                return (itNext != mItEnd);
40        }
41
42        T Next(S &pdf)
43        {
44                return Next();
45        }
46       
47        T Next()
48        {
49                // hack: create new pvs entry
50                for (; !(*mItCurrent); ++ mItCurrent, ++ mDistance);
51
52                T sample = sObjects[mDistance];
53
54                ++ mDistance;
55                ++ mItCurrent;
56
57                return sample;
58        }
59
60       
61        // vector of objects corresponding to pvs entries
62        static vector<T> sObjects;
63
64private:
65
66        typename bit_vector::const_iterator mItCurrent;
67        //typename bit_vector::const_iterator mItNext;
68        typename bit_vector::const_iterator mItEnd;
69       
70        // note: store distance explicitly because I
71        // don't know how efficient
72        // std::distance is on specialisation of vector<bool>
73        int mDistance;
74};
75
76
77/** Pvs implemented as bitvector
78*/
79template<typename T, typename S>
80class BitVectorPvs//: public PvsBase<T>
81{
82        template<typename T, typename S> friend class BitVectorPvsIterator;
83
84public:
85
86        BitVectorPvs();
87        //virtual ~HashPvs() {};
88
89        int GetSize() const;
90        bool Empty() const;
91
92        /** Adds sample to PVS.
93                @returns contribution of sample (0 or 1)
94        */
95        float AddSample(T sample, const float pdf);
96
97        /** Adds sample to PVS without checking for presence of the sample
98                warning: pvs remains unsorted!
99        */
100        void AddSampleDirty(T sample, const float pdf);
101
102        /** Adds sample dirty (on the end of the vector) but
103                first checks if sample is already in clean part of the pvs.
104        */
105        bool AddSampleDirtyCheck(T sample, const float pdf);
106
107        /** Sort pvs entries - this should always be called after a
108                sequence of AddSampleDirty calls
109        */
110        void Sort();
111
112        /** Clears the pvs.
113        */
114        void Clear(const bool trim = true);
115
116        bool IsDirty() const;
117
118        bool RequiresResort() const;
119
120        /** Finds sample in PVS.
121        */
122        bool Find(T sample);
123
124        typename BitVectorPvsIterator<T, S> GetIterator() const;
125
126        /** Compute continuous PVS difference
127        */
128        float GetPvsHomogenity(BitVectorPvs<T, S> &pvs);
129
130        static void Merge(BitVectorPvs<T, S> &mergedPvs,
131                                          const BitVectorPvs<T, S> &a,
132                                          const BitVectorPvs<T, S> &b);
133
134        static int GetEntrySizeByte();
135        static float GetEntrySize();
136
137        bool GetSampleContribution(T sample,
138                                                   const float pdf,
139                                                           float &contribution);
140
141        int GetSamples() const
142        {
143                return mSamples;
144        }
145
146        void MergeInPlace(const BitVectorPvs<T, S> &a)
147        {
148                cerr << "not implemented yet" << endl;
149        }
150
151        bool RequiresResortLog() const
152        {
153                return false;
154        }
155
156        void Reserve(const int n)
157        {
158                mEntries.reserve(n);
159        }
160
161        /** Sort pvs entries assume that the pvs contains unique entries
162        */
163        void SimpleSort()
164        {
165                // not necessary
166        }
167
168        int SubtractPvs(const BitVectorPvs<T, S> &pvs)
169        {
170                cerr << "not yet implemented" << endl;
171                return 0;
172        }
173
174        /** Compute continuous PVS difference
175        */
176        void ComputeContinuousPvsDifference(BitVectorPvs<T, S> &pvs,
177                                                                                float &pvsReduction,
178                                                                                float &pvsEnlargement)
179        {
180                cerr << "not yet implemented" << endl;
181        }
182
183
184        static void SetPvsSize(const int pvsSize) { sPvsSize = pvsSize; };
185       
186protected:
187
188        /// hash table of PVS entries
189        bit_vector mEntries;
190
191        /// Number of samples used to create the PVS
192        int mSamples;
193
194public:
195        static int sPvsSize;
196};
197
198
199template <typename T, typename S>
200BitVectorPvs<T, S>::BitVectorPvs()
201{
202        // initialize bit vector
203        mEntries.reserve(sPvsSize);
204        mEntries.resize(sPvsSize);
205
206        // set pvs entries to false
207        Clear();
208}
209
210
211template <typename T, typename S>
212bool BitVectorPvs<T, S>::Find(T sample)
213{
214        return mEntries[sample->GetId()];
215}
216
217
218template <typename T, typename S>
219int BitVectorPvs<T, S>::GetSize() const
220{
221        int size = 0;
222        bit_vector::const_iterator bit, bit_end = mEntries.end();
223
224        for (bit = mEntries.begin(); bit != bit_end; ++ bit)
225        {
226                if (*bit) ++ size;
227        }
228
229        return size;
230}
231
232
233template <typename T, typename S>
234bool BitVectorPvs<T, S>::Empty() const
235{
236        bit_vector::const_iterator bit, bit_end = mEntries.end();
237
238        for (bit = mEntries.begin(); bit != bit_end; ++ bit)
239        {
240                if (*bit) return false;
241        }
242
243        return true;
244}
245
246
247template <typename T, typename S>
248float BitVectorPvs<T, S>::AddSample(T sample, const float pdf)
249{
250        if (Find(sample))
251                return 0.0f;
252       
253        mEntries[sample->GetId()] = true;
254
255        return 1.0f;
256}
257       
258
259template <typename T, typename S>
260void BitVectorPvs<T, S>::AddSampleDirty(T sample, const float pdf)
261{
262        if (!Find(sample))
263        {
264                mEntries[sample->GetId()] = true;
265        }
266}
267
268
269template <typename T, typename S>
270bool BitVectorPvs<T, S>::AddSampleDirtyCheck(T sample,
271                                                                                const float pdf)
272{
273        if (Find(sample))
274                return false;
275       
276        mEntries[sample->GetId()] = true;
277        return true;
278}
279
280
281template <typename T, typename S>
282void BitVectorPvs<T, S>::Sort()
283{
284}
285
286
287template <typename T, typename S>
288void BitVectorPvs<T, S>::Clear(const bool trim = true)
289{
290        bit_vector::iterator bit, bit_end = mEntries.end();
291        for (bit = mEntries.begin(); bit != bit_end; ++ bit)
292        {
293                (*bit) = false;
294        }
295}
296
297
298template <typename T, typename S>
299bool BitVectorPvs<T, S>::IsDirty() const
300{
301        return false;
302}
303
304
305template <typename T, typename S>
306bool BitVectorPvs<T, S>::RequiresResort() const
307{
308        return false;
309}
310
311
312template <typename T, typename S>
313typename BitVectorPvsIterator<T, S> BitVectorPvs<T, S>::GetIterator() const
314{
315        BitVectorPvsIterator<T, S> pit(mEntries.begin(), mEntries.end());
316
317        return pit;
318}
319
320
321template <typename T, typename S>
322float BitVectorPvs<T, S>::GetEntrySize()
323{
324        return (float)(sizeof(bool)) / float(1024 * 1024);
325}
326
327
328template <typename T, typename S>
329int BitVectorPvs<T, S>::GetEntrySizeByte()
330{
331        return sizeof(bool);
332}
333
334
335template <typename T, typename S>
336float BitVectorPvs<T, S>::GetPvsHomogenity(BitVectorPvs<T, S> &pvs)
337{
338        float pvsReduction, pvsEnlargement;
339
340        ComputeContinuousPvsDifference(pvs,     pvsReduction, pvsEnlargement);
341
342        return pvsReduction + pvsEnlargement;
343}
344
345
346template <typename T, typename S>
347bool BitVectorPvs<T, S>::GetSampleContribution(T sample,
348                                                                          const float pdf,
349                                                                          float &contribution)
350{
351        const bool entryFound = Find(sample);
352
353        if (entryFound) 
354        {
355                contribution = 0.0f;
356                return false;
357        }
358        else
359        {
360                contribution = 1.0f;
361                return true;
362        }
363}
364
365
366template <typename T, typename S>
367void BitVectorPvs<T, S>::Merge(BitVectorPvs<T, S> &mergedPvs,
368                                                           const BitVectorPvs<T, S> &a,
369                                                           const BitVectorPvs<T, S> &b)
370{
371        bit_vector::iterator bit, bit_end = mergedPvs.mEntries.end();
372        bit_vector::const_iterator bitA =  a.mEntries.begin(), bitB = b.mEntries.begin();
373
374        for (bit = mergedPvs.mEntries.begin(); bit != bit_end; ++ bit, ++ bitA, ++ bitB)
375        {
376        (*bit) = (*bitA) | (*bitB);
377        }
378}
379
380
381template<typename T, typename S>
382int BitVectorPvs<T, S>::sPvsSize = 0;
383
384template<typename T, typename S>
385vector<T> BitVectorPvsIterator<T, S>::sObjects;
386
387}
388
389#endif
390
Note: See TracBrowser for help on using the repository browser.