source: trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h @ 581

Revision 581, 7.1 KB checked in by mattausch, 18 years ago (diff)

added function for pvs compression

RevLine 
[177]1#ifndef __PVS_H
2#define __PVS_H
3
4#include <map>
[469]5#include <vector>
[177]6
7class KdNode;
[240]8class BspNode;
[191]9class Ray;
[308]10class Intersectable;
[406]11class ViewCellKdNode;
[177]12
[310]13template<typename T>
14struct LtSample {
[311]15    bool operator()(const T a, const T b) const
[310]16    {
17                return a < b;
18        }
19};
20
[469]21/** Information stored with a PVS entry. Consists of the number
22        the object was seen from the view cell.
23*/
[310]24
25template<typename T>
26struct PvsData {
[556]27  //  int mVisibleSamples;
28  // sum of probability density of visible sample rays
29  float mSumPdf;
[310]30  PvsData<T>() {}
[556]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; }
[310]37};
38
[469]39/** Template class representing the Potentially Visible Set (PVS)
40        mainly from a view cell, but also e.g., from objects.
41*/
[310]42template<typename T>
[311]43class Pvs
[310]44{
45public:
[469]46  Pvs(): /*mSamples(0), */mEntries() {}
[466]47 
[469]48  //int mSamples;
[466]49 
[469]50  /** Compresses PVS lossless or lossy.
51  */
[466]52  int Compress() {return 0;}
53  int GetSize() const {return (int)mEntries.size();}
[469]54  bool Empty() const {return mEntries.empty();}
[556]55
56  /** Normalize the visibility of entries in order to get comparable
57          results */
[469]58 
[556]59  void NormalizeMaximum();
60 
[466]61  /** Merges pvs of a and pvs of b into this pvs.
62   */
63  void Merge(const Pvs<T> &a, const Pvs<T> &b);
[469]64 
[466]65  /** Difference of pvs to pvs b.
66          @returns number of different entries.
67  */
68  int Diff(const Pvs<T> &b);
69 
[469]70  /** Finds sample in PVS.
71          @returns sample if found, NULL otherwise.
72  */
[466]73  PvsData<T> *Find(T sample);
[492]74
[556]75  bool GetSampleContribution(T sample, const float pdf, float &contribution);
[469]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  */
[556]81  bool AddSample(T sample, const float pdf, float &contribution);
[469]82
83  /** Adds sample to PVS.
84          @returns contribution of sample (0 or 1)
85  */
[556]86  float AddSample(T sample, const float pdf);
[492]87 
[485]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);
[469]96  /** Returns PVS data, i.e., how often it was seen from the view cell,
97          and the object itsef.
98  */
[466]99  void GetData(const int index, T &entry, PvsData<T> &data);
[469]100
101  /** Collects the PVS entries and returns them in the vector.
102  */
103  void CollectEntries(std::vector<T> &entries);
104
[485]105  /** Removes sample from PVS if reference count is zero.
106          @param visibleSampels number of references to be removed
107  */
[556]108  bool RemoveSample(T sample, const float pdf);
[485]109
[469]110  /// Map of PVS entries
[466]111  std::map<T, PvsData<T>, LtSample<T> > mEntries;
[310]112};
113
[581]114
[341]115template <typename T>
[362]116int Pvs<T>::Diff(const Pvs<T> &b)
117{
118        int dif = 0;
119
120        std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
121
122        for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
123        {
124                PvsData<T> *data = Find((*it).first);           
125                if (!data) ++ dif;
126        }
127
128        return dif;
129}
130
131template <typename T>
[350]132void Pvs<T>::Merge(const Pvs<T> &a, const Pvs<T> &b)
[341]133{
[362]134        std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
135
[573]136        mEntries = a.mEntries;
[485]137       
[362]138        for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
139        {
140                PvsData<T> *data = Find((*it).first);
141               
142                if (data)
[556]143                        data->mSumPdf += (*it).second.mSumPdf;
[362]144                else
145                        mEntries.insert(*it);
146        }
[341]147}
148
[310]149template <typename T>
[311]150PvsData<T> *Pvs<T>::Find(T sample)
[310]151{
[311]152  std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.find(sample);
[310]153  if (i != mEntries.end()) {
154    return &(*i).second;
[311]155  } else
[310]156    return NULL;
157}
158
159template <typename T>
[311]160void Pvs<T>::GetData(const int index,
[485]161                                         T &entry,
162                                         PvsData<T> &data)
[310]163{
[311]164  std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.begin();
[310]165  for (int k = 0; k != index && i != mEntries.end(); i++, k++);
166
167  entry = (*i).first;
[311]168  data = (*i).second;
[310]169}
170
[311]171template <typename T>
[556]172float
173Pvs<T>::AddSample(T sample, const float pdf)
[177]174{
[492]175  PvsData<T> *data = Find(sample);
176 
177  if (data)  {
[556]178        data->mSumPdf+=pdf;
179        return data->mSumPdf;
[492]180  }
181  else {
[556]182        mEntries[sample] = PvsData<T>(pdf);
183        return pdf;
[492]184  }
[466]185}
[177]186
[466]187template <typename T>
[492]188bool
[556]189Pvs<T>::AddSample(T sample,
190                                  const float pdf,
191                                  float &contribution)
[466]192{
193  PvsData<T> *data = Find(sample);
194 
195  if (data)  {
[556]196        data->mSumPdf+=pdf;
197        contribution = pdf/data->mSumPdf;
[466]198        return false;
199  }
200  else {
[556]201        mEntries[sample] = PvsData<T>(pdf);
[466]202        contribution = 1.0f;
203        return true;
204  }
[311]205}
[308]206
[469]207template <typename T>
[492]208bool
[556]209Pvs<T>::GetSampleContribution(T sample,
210                                                          const float pdf,
211                                                          float &contribution)
[492]212{
213  PvsData<T> *data = Find(sample);
214 
215  if (data)  {
[556]216        contribution = pdf/(data->mSumPdf + pdf);
[492]217        return false;
218  }
219  else {
220        contribution = 1.0f;
221        return true;
222  }
223}
224
225template <typename T>
[556]226bool Pvs<T>::RemoveSample(T sample,
227                                                  const float pdf)
[485]228{
[556]229  std::map<T, PvsData<T>, LtSample<T> >::
230        iterator it = mEntries.find(sample);
[485]231 
[556]232  if (it == mEntries.end())
233        return false;
234 
235  PvsData<T> *data = &(*it).second;
236 
237  data->mSumPdf -= pdf;
238  if (data->mSumPdf <= 0.0f)
239        mEntries.erase(it);
240 
241  return true;
[485]242}
243
244template <typename T>
245int Pvs<T>::AddPvs(const Pvs<T> &pvs)
246{
[556]247  std::map<T, PvsData<T>, LtSample<T> >::
248        const_iterator it, it_end = pvs.mEntries.end();
249 
250  float contri;
251  // output PVS of view cell
252  for (it = pvs.mEntries.begin(); it != it_end; ++ it)
253        AddSample((*it).first, (*it).second.mSumPdf, contri);
254 
255  return GetSize();
[485]256}
257 
258template <typename T>
259int Pvs<T>::SubtractPvs(const Pvs<T> &pvs)
260{
[556]261  std::map<T, PvsData<T>, LtSample<T> >::
262        const_iterator it, it_end = pvs.mEntries.end();
263 
264  // output PVS of view cell
265  for (it = pvs.mEntries.begin(); it != it_end; ++ it)
266        RemoveSample((*it).first, (*it).second.mSumPdf);
267 
268  return GetSize();
[485]269}
270
271template <typename T>
[469]272void Pvs<T>::CollectEntries(std::vector<T> &entries)
273{
[485]274        std::map<T, PvsData<T>, LtSample<T> >::
275                const_iterator it, it_end = mEntries.end();
[469]276
277        // output PVS of view cell
278        for (it = mEntries.begin(); it != it_end; ++ it)
279                entries.push_back((*it)->first);
280}
281
[556]282template <typename T>
283void Pvs<T>::NormalizeMaximum()
284{
285  std::map<T, PvsData<T>, LtSample<T> >::
286        const_iterator it, it_end = mEntries.end();
287
288  float maxPdfSum = -1.0f;
289
290  // output PVS of view cell
291  for (it = mEntries.begin(); it != it_end; ++ it) {
292        float sum = (*it)->second.sumPdf;
293        if (sum > maxSum)
294          maxSum = sum;
295  }
296
297  maxSum = 1.0f/maxSum;
298
299  for (it = mEntries.begin(); it != it_end; ++ it) {
300        (*it)->second.sumPdf *= maxSum;
301  }
302 
303}
304
305
[311]306/** Class instantiating the Pvs template for kd tree nodes.
307*/
308class KdPvs: public Pvs<KdNode *>
[308]309{
[311]310        int Compress();
[308]311};
312
[311]313typedef std::map<KdNode *, PvsData<KdNode *>, LtSample<KdNode *> > KdPvsMap;
[469]314typedef std::map<Intersectable *, PvsData<Intersectable *>, LtSample<Intersectable *> > ObjectPvsMap;
315typedef PvsData<Intersectable *> ObjectPvsData;
[311]316typedef PvsData<KdNode *> KdPvsData;
[308]317
[469]318typedef Pvs<Intersectable *> ObjectPvs;
[366]319
[469]320
[177]321#endif
322
Note: See TracBrowser for help on using the repository browser.