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

Revision 695, 8.9 KB checked in by bittner, 19 years ago (diff)

pvs homegenity measure added

Line 
1#ifndef __PVS_H
2#define __PVS_H
3
4#include <map>
5#include <vector>
6
7class KdNode;
8class BspNode;
9class Ray;
10class Intersectable;
11class ViewCellKdNode;
12
13template<typename T>
14struct LtSample {
15    bool operator()(const T a, const T b) const
16    {
17                return a < b;
18        }
19};
20
21/** Information stored with a PVS entry. Consists of the number
22        the object was seen from the view cell.
23*/
24
25template<typename T>
26struct PvsData {
27  //  int mVisibleSamples;
28  // sum of probability density of visible sample rays
29  float mSumPdf;
30  PvsData<T>() {}
31  PvsData<T>(const float sumPdf):
32        mSumPdf(sumPdf) {}
33
34  // $$JB in order to return meaningfull values
35  // it assumes that the sum pdf has been normalized somehow!!!
36  float GetVisibility() { return mSumPdf; }
37};
38
39/** Template class representing the Potentially Visible Set (PVS)
40        mainly from a view cell, but also e.g., from objects.
41*/
42template<typename T>
43class Pvs
44{
45public:
46  Pvs(): /*mSamples(0), */mEntries() {}
47 
48  //int mSamples;
49 
50  /** Compresses PVS lossless or lossy.
51  */
52  int Compress() {return 0;}
53  int GetSize() const {return (int)mEntries.size();}
54  bool Empty() const {return mEntries.empty();}
55
56  /** Normalize the visibility of entries in order to get comparable
57          results */
58 
59  void NormalizeMaximum();
60 
61  /** Merges pvs of a into this pvs.
62   */
63  void Merge(const Pvs<T> &a);
64 
65  /** Difference of pvs to pvs b.
66          @returns number of different entries.
67  */
68  int Diff(const Pvs<T> &b);
69 
70  /** Finds sample in PVS.
71          @returns sample if found, NULL otherwise.
72  */
73  PvsData<T> *Find(T sample);
74
75  bool GetSampleContribution(T sample, const float pdf, float &contribution);
76 
77  /** Adds sample to PVS.
78          @contribution contribution of sample (0 or 1)
79          @returns true if sample was not already in PVS.
80  */
81  bool AddSample(T sample, const float pdf, float &contribution);
82
83  /** Adds sample to PVS.
84          @returns contribution of sample (0 or 1)
85  */
86  float AddSample(T sample, const float pdf);
87 
88  /** Adds one pvs to another one.
89          @returns new pvs size
90  */
91  int AddPvs(const Pvs<T> &pvs);
92  /** Subtracts one pvs from another one.
93          @returns new pvs size
94  */
95  int SubtractPvs(const Pvs<T> &pvs);
96  /** Returns PVS data, i.e., how often it was seen from the view cell,
97          and the object itsef.
98  */
99  void GetData(const int index, T &entry, PvsData<T> &data);
100
101  /** Collects the PVS entries and returns them in the vector.
102  */
103  void CollectEntries(std::vector<T> &entries);
104
105  /** Removes sample from PVS if reference count is zero.
106          @param visibleSampels number of references to be removed
107  */
108  bool RemoveSample(T sample, const float pdf);
109
110  /** Compute continuous PVS difference */
111  void ComputeContinuousPvsDifference(const Pvs<T> &pvs,
112                                                                          float &pvsReduction,
113                                                                          float &pvsEnlargement);
114 
115
116  /** Compute continuous PVS difference */
117  float GetPvsHomogenity(const Pvs<T> &pvs) {
118        float
119          pvsReduction,
120          pvsEnlargement;
121
122        ComputeContinuousPvsDifference(pvs,
123                                                                   pvsReduction,
124                                                                   pvsEnlargement);
125
126        return pvsReduction + pvsEnlargement;
127  }
128
129                                         
130                                         
131  /// Map of PVS entries
132  std::map<T, PvsData<T>, LtSample<T> > mEntries;
133};
134
135
136
137/**
138   Compute continuous PVS difference of 'b' with respect to the base PVS (*this).
139   Provides separatelly PVS reduction from PVS enlargement.
140
141*/
142template <typename T>
143void
144Pvs<T>::ComputeContinuousPvsDifference(const Pvs<T> &b,
145                                                                           float &pvsReduction,
146                                                                           float &pvsEnlargement)
147{
148  // Uses sum of log differences, which corresponds to entropy
149  std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
150 
151  for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it) {
152        float bSumPdf = (*it).second.mSumPdf;
153        float aSumPdf = 0.0f;
154        PvsData<T> *data = Find((*it).first);           
155        if (data) {
156          aSumPdf = data->second.mSumPdf;
157          // mark this entry as processed to avoid double counting
158          data->second.mSumPdf = -aSumPdf;
159        }
160        float diff = bSumPdf - aSumPdf;
161       
162        if (diff > 0.0f) {
163          pvsEnlargement += diff;
164        } else {
165          pvsReduction += -diff;
166        }
167  }
168 
169  for (it = mEntries.begin(); it != mEntries.end(); ++ it) {
170        float aSumPdf = (*it).second.mSumPdf;
171        float bSumPdf = 0.0f;
172        if (aSumPdf < 0.0f) {
173          // this entry was already accounted for!
174          // just revert it back
175          (*it).second.mSumPdf = -aSumPdf;
176        } else {
177          PvsData<T> *data = b.Find((*it).first);
178          if (data) {
179                bSumPdf = data->second.mSumPdf;
180          }
181          float diff = bSumPdf - aSumPdf;
182         
183          if (diff > 0.0f) {
184                pvsEnlargement += diff;
185          } else {
186                pvsReduction += -diff;
187          }
188        }
189  }
190}
191
192template <typename T>
193int Pvs<T>::Diff(const Pvs<T> &b)
194{
195        int dif = 0;
196
197        std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
198
199        for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
200        {
201                PvsData<T> *data = Find((*it).first);           
202                if (!data) ++ dif;
203        }
204
205        return dif;
206}
207
208template <typename T>
209void Pvs<T>::Merge(const Pvs<T> &a)
210{
211        std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
212
213        for (it = a.mEntries.begin(); it != a.mEntries.end(); ++ it)
214        {
215                PvsData<T> *data = Find((*it).first);
216               
217                if (data)
218                        data->mSumPdf += (*it).second.mSumPdf;
219                else
220                        mEntries.insert(*it);
221        }
222}
223
224template <typename T>
225PvsData<T> *Pvs<T>::Find(T sample)
226{
227  std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.find(sample);
228  if (i != mEntries.end()) {
229    return &(*i).second;
230  } else
231    return NULL;
232}
233
234template <typename T>
235void Pvs<T>::GetData(const int index,
236                                         T &entry,
237                                         PvsData<T> &data)
238{
239  std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.begin();
240  for (int k = 0; k != index && i != mEntries.end(); i++, k++);
241
242  entry = (*i).first;
243  data = (*i).second;
244}
245
246template <typename T>
247float
248Pvs<T>::AddSample(T sample, const float pdf)
249{
250  PvsData<T> *data = Find(sample);
251 
252  if (data)  {
253        data->mSumPdf+=pdf;
254        return data->mSumPdf;
255  }
256  else {
257        mEntries[sample] = PvsData<T>(pdf);
258        return pdf;
259  }
260}
261
262template <typename T>
263bool
264Pvs<T>::AddSample(T sample,
265                                  const float pdf,
266                                  float &contribution)
267{
268  PvsData<T> *data = Find(sample);
269 
270  if (data)  {
271        data->mSumPdf+=pdf;
272        contribution = pdf/data->mSumPdf;
273        return false;
274  }
275  else {
276        mEntries[sample] = PvsData<T>(pdf);
277        contribution = 1.0f;
278        return true;
279  }
280}
281
282template <typename T>
283bool
284Pvs<T>::GetSampleContribution(T sample,
285                                                          const float pdf,
286                                                          float &contribution)
287{
288  PvsData<T> *data = Find(sample);
289 
290  if (data)  {
291        contribution = pdf/(data->mSumPdf + pdf);
292        return false;
293  }
294  else {
295        contribution = 1.0f;
296        return true;
297  }
298}
299
300template <typename T>
301bool Pvs<T>::RemoveSample(T sample,
302                                                  const float pdf)
303{
304  std::map<T, PvsData<T>, LtSample<T> >::
305        iterator it = mEntries.find(sample);
306 
307  if (it == mEntries.end())
308        return false;
309 
310  PvsData<T> *data = &(*it).second;
311 
312  data->mSumPdf -= pdf;
313  if (data->mSumPdf <= 0.0f)
314        mEntries.erase(it);
315 
316  return true;
317}
318
319template <typename T>
320int Pvs<T>::AddPvs(const Pvs<T> &pvs)
321{
322  std::map<T, PvsData<T>, LtSample<T> >::
323        const_iterator it, it_end = pvs.mEntries.end();
324 
325  float contri;
326  // output PVS of view cell
327  for (it = pvs.mEntries.begin(); it != it_end; ++ it)
328        AddSample((*it).first, (*it).second.mSumPdf, contri);
329 
330  return GetSize();
331}
332 
333template <typename T>
334int Pvs<T>::SubtractPvs(const Pvs<T> &pvs)
335{
336  std::map<T, PvsData<T>, LtSample<T> >::
337        const_iterator it, it_end = pvs.mEntries.end();
338 
339  // output PVS of view cell
340  for (it = pvs.mEntries.begin(); it != it_end; ++ it)
341        RemoveSample((*it).first, (*it).second.mSumPdf);
342 
343  return GetSize();
344}
345
346template <typename T>
347void Pvs<T>::CollectEntries(std::vector<T> &entries)
348{
349        std::map<T, PvsData<T>, LtSample<T> >::
350                const_iterator it, it_end = mEntries.end();
351
352        // output PVS of view cell
353        for (it = mEntries.begin(); it != it_end; ++ it)
354                entries.push_back((*it)->first);
355}
356
357template <typename T>
358void Pvs<T>::NormalizeMaximum()
359{
360  std::map<T, PvsData<T>, LtSample<T> >::
361        const_iterator it, it_end = mEntries.end();
362
363  float maxPdfSum = -1.0f;
364
365  // output PVS of view cell
366  for (it = mEntries.begin(); it != it_end; ++ it) {
367        float sum = (*it)->second.sumPdf;
368        if (sum > maxSum)
369          maxSum = sum;
370  }
371
372  maxSum = 1.0f/maxSum;
373
374  for (it = mEntries.begin(); it != it_end; ++ it) {
375        (*it)->second.sumPdf *= maxSum;
376  }
377 
378}
379
380
381/** Class instantiating the Pvs template for kd tree nodes.
382*/
383class KdPvs: public Pvs<KdNode *>
384{
385        int Compress();
386};
387
388typedef std::map<KdNode *, PvsData<KdNode *>, LtSample<KdNode *> > KdPvsMap;
389typedef std::map<Intersectable *, PvsData<Intersectable *>, LtSample<Intersectable *> > ObjectPvsMap;
390typedef PvsData<Intersectable *> ObjectPvsData;
391typedef PvsData<KdNode *> KdPvsData;
392
393typedef Pvs<Intersectable *> ObjectPvs;
394
395
396#endif
397
Note: See TracBrowser for help on using the repository browser.