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

Revision 704, 8.8 KB checked in by mattausch, 18 years ago (diff)

implemented first version of the visibility filter

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(Pvs<T> &pvs,
112                                                                          float &pvsReduction,
113                                                                          float &pvsEnlargement);
114 
115
116  /** Compute continuous PVS difference */
117  float GetPvsHomogenity(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(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> >::iterator it;
150 
151  for (it = b.mEntries.begin();
152          it != b.mEntries.end(); ++ it) {
153        float bSumPdf = (*it).second.mSumPdf;
154        float aSumPdf = 0.0f;
155        PvsData<T> *data = Find((*it).first);           
156        if (data) {
157          aSumPdf = data->mSumPdf;
158          // mark this entry as processed to avoid double counting
159          data->mSumPdf = -aSumPdf;
160        }
161        float diff = bSumPdf - aSumPdf;
162       
163        if (diff > 0.0f) {
164          pvsEnlargement += diff;
165        } else {
166          pvsReduction += -diff;
167        }
168  }
169 
170  for (it = mEntries.begin(); it != mEntries.end(); ++ it) {
171        float aSumPdf = (*it).second.mSumPdf;
172        float bSumPdf = 0.0f;
173        if (aSumPdf < 0.0f) {
174          // this entry was already accounted for!
175          // just revert it back
176          (*it).second.mSumPdf = -aSumPdf;
177        } else {
178          PvsData<T> *data = b.Find((*it).first);
179          if (data) {
180                bSumPdf = data->mSumPdf;
181          }
182          float diff = bSumPdf - aSumPdf;
183         
184          if (diff > 0.0f) {
185                pvsEnlargement += diff;
186          } else {
187                pvsReduction += -diff;
188          }
189        }
190  }
191}
192
193template <typename T>
194int Pvs<T>::Diff(const Pvs<T> &b)
195{
196        int dif = 0;
197
198        std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
199
200        for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
201        {
202                PvsData<T> *data = Find((*it).first);           
203                if (!data) ++ dif;
204        }
205
206        return dif;
207}
208
209template <typename T>
210void Pvs<T>::Merge(const Pvs<T> &a)
211{
212        std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
213
214        for (it = a.mEntries.begin(); it != a.mEntries.end(); ++ it)
215        {
216                PvsData<T> *data = Find((*it).first);
217               
218                if (data)
219                        data->mSumPdf += (*it).second.mSumPdf;
220                else
221                        mEntries.insert(*it);
222        }
223}
224
225template <typename T>
226PvsData<T> *Pvs<T>::Find(T sample)
227{
228  std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.find(sample);
229  if (i != mEntries.end()) {
230    return &(*i).second;
231  } else
232    return NULL;
233}
234
235template <typename T>
236void Pvs<T>::GetData(const int index,
237                                         T &entry,
238                                         PvsData<T> &data)
239{
240  std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.begin();
241  for (int k = 0; k != index && i != mEntries.end(); i++, k++);
242
243  entry = (*i).first;
244  data = (*i).second;
245}
246
247template <typename T>
248float
249Pvs<T>::AddSample(T sample, const float pdf)
250{
251  PvsData<T> *data = Find(sample);
252 
253  if (data)  {
254        data->mSumPdf+=pdf;
255        return data->mSumPdf;
256  }
257  else {
258        mEntries[sample] = PvsData<T>(pdf);
259        return pdf;
260  }
261}
262
263template <typename T>
264bool
265Pvs<T>::AddSample(T sample,
266                                  const float pdf,
267                                  float &contribution)
268{
269  PvsData<T> *data = Find(sample);
270 
271  if (data)  {
272        data->mSumPdf+=pdf;
273        contribution = pdf/data->mSumPdf;
274        return false;
275  }
276  else {
277        mEntries[sample] = PvsData<T>(pdf);
278        contribution = 1.0f;
279        return true;
280  }
281}
282
283template <typename T>
284bool
285Pvs<T>::GetSampleContribution(T sample,
286                                                          const float pdf,
287                                                          float &contribution)
288{
289  PvsData<T> *data = Find(sample);
290 
291  if (data)  {
292        contribution = pdf/(data->mSumPdf + pdf);
293        return false;
294  }
295  else {
296        contribution = 1.0f;
297        return true;
298  }
299}
300
301template <typename T>
302bool Pvs<T>::RemoveSample(T sample,
303                                                  const float pdf)
304{
305  std::map<T, PvsData<T>, LtSample<T> >::
306        iterator it = mEntries.find(sample);
307 
308  if (it == mEntries.end())
309        return false;
310 
311  PvsData<T> *data = &(*it).second;
312 
313  data->mSumPdf -= pdf;
314  if (data->mSumPdf <= 0.0f)
315        mEntries.erase(it);
316 
317  return true;
318}
319
320template <typename T>
321int Pvs<T>::AddPvs(const Pvs<T> &pvs)
322{
323  std::map<T, PvsData<T>, LtSample<T> >::
324        const_iterator it, it_end = pvs.mEntries.end();
325 
326  float contri;
327  // output PVS of view cell
328  for (it = pvs.mEntries.begin(); it != it_end; ++ it)
329        AddSample((*it).first, (*it).second.mSumPdf, contri);
330 
331  return GetSize();
332}
333 
334template <typename T>
335int Pvs<T>::SubtractPvs(const Pvs<T> &pvs)
336{
337  std::map<T, PvsData<T>, LtSample<T> >::
338        const_iterator it, it_end = pvs.mEntries.end();
339 
340  // output PVS of view cell
341  for (it = pvs.mEntries.begin(); it != it_end; ++ it)
342        RemoveSample((*it).first, (*it).second.mSumPdf);
343 
344  return GetSize();
345}
346
347template <typename T>
348void Pvs<T>::CollectEntries(std::vector<T> &entries)
349{
350        std::map<T, PvsData<T>, LtSample<T> >::
351                const_iterator it, it_end = mEntries.end();
352
353        // output PVS of view cell
354        for (it = mEntries.begin(); it != it_end; ++ it)
355                entries.push_back((*it)->first);
356}
357
358template <typename T>
359void Pvs<T>::NormalizeMaximum()
360{
361  std::map<T, PvsData<T>, LtSample<T> >::
362        const_iterator it, it_end = mEntries.end();
363
364  float maxPdfSum = -1.0f;
365
366  // output PVS of view cell
367  for (it = mEntries.begin(); it != it_end; ++ it) {
368        float sum = (*it)->second.sumPdf;
369        if (sum > maxSum)
370          maxSum = sum;
371  }
372
373  maxSum = 1.0f/maxSum;
374
375  for (it = mEntries.begin(); it != it_end; ++ it) {
376        (*it)->second.sumPdf *= maxSum;
377  }
378 
379}
380
381
382/** Class instantiating the Pvs template for kd tree nodes.
383*/
384class KdPvs: public Pvs<KdNode *>
385{
386        int Compress();
387};
388
389typedef std::map<KdNode *, PvsData<KdNode *>, LtSample<KdNode *> > KdPvsMap;
390typedef std::map<Intersectable *, PvsData<Intersectable *>, LtSample<Intersectable *> > ObjectPvsMap;
391typedef PvsData<Intersectable *> ObjectPvsData;
392typedef PvsData<KdNode *> KdPvsData;
393
394typedef Pvs<Intersectable *> ObjectPvs;
395
396
397#endif
398
Note: See TracBrowser for help on using the repository browser.