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

Revision 752, 9.2 KB checked in by mattausch, 18 years ago (diff)

after rendering workshop submissioin
x3dparser can use def - use constructs
implemented improved evaluation (samples are only stored in leaves, only propagate pvs size)

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 
[610]61  /** Merges pvs of a into this pvs.
[466]62   */
[610]63  void Merge(const Pvs<T> &a);
[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);
[752]92
[485]93  /** Subtracts one pvs from another one.
[752]94  WARNING: could contains bugs
[485]95          @returns new pvs size
96  */
97  int SubtractPvs(const Pvs<T> &pvs);
[469]98  /** Returns PVS data, i.e., how often it was seen from the view cell,
99          and the object itsef.
100  */
[466]101  void GetData(const int index, T &entry, PvsData<T> &data);
[469]102
103  /** Collects the PVS entries and returns them in the vector.
104  */
105  void CollectEntries(std::vector<T> &entries);
106
[485]107  /** Removes sample from PVS if reference count is zero.
108          @param visibleSampels number of references to be removed
109  */
[556]110  bool RemoveSample(T sample, const float pdf);
[485]111
[677]112  /** Compute continuous PVS difference */
[704]113  void ComputeContinuousPvsDifference(Pvs<T> &pvs,
[677]114                                                                          float &pvsReduction,
115                                                                          float &pvsEnlargement);
116 
117
[752]118  /** Clears the pvs.
119  */
120  void Clear();
121
[695]122  /** Compute continuous PVS difference */
[704]123  float GetPvsHomogenity(Pvs<T> &pvs) {
[695]124        float
125          pvsReduction,
126          pvsEnlargement;
[713]127       
[695]128        ComputeContinuousPvsDifference(pvs,
129                                                                   pvsReduction,
130                                                                   pvsEnlargement);
[713]131       
[695]132        return pvsReduction + pvsEnlargement;
133  }
134
[677]135                                         
136                                         
[469]137  /// Map of PVS entries
[466]138  std::map<T, PvsData<T>, LtSample<T> > mEntries;
[310]139};
140
[581]141
[677]142
[695]143/**
144   Compute continuous PVS difference of 'b' with respect to the base PVS (*this).
145   Provides separatelly PVS reduction from PVS enlargement.
146
147*/
[341]148template <typename T>
[677]149void
[704]150Pvs<T>::ComputeContinuousPvsDifference(Pvs<T> &b,
[677]151                                                                           float &pvsReduction,
152                                                                           float &pvsEnlargement)
153{
[705]154        pvsReduction = 0.0f;
155        pvsEnlargement = 0.0f;
[695]156  // Uses sum of log differences, which corresponds to entropy
[704]157  std::map<T, PvsData<T>, LtSample<T> >::iterator it;
[677]158 
[704]159  for (it = b.mEntries.begin();
160          it != b.mEntries.end(); ++ it) {
[695]161        float bSumPdf = (*it).second.mSumPdf;
162        float aSumPdf = 0.0f;
163        PvsData<T> *data = Find((*it).first);           
164        if (data) {
[704]165          aSumPdf = data->mSumPdf;
[695]166          // mark this entry as processed to avoid double counting
[704]167          data->mSumPdf = -aSumPdf;
[695]168        }
[713]169
170#if 0
[695]171        float diff = bSumPdf - aSumPdf;
172       
173        if (diff > 0.0f) {
174          pvsEnlargement += diff;
175        } else {
176          pvsReduction += -diff;
177        }
[713]178#else
179        if (!data)
180          pvsEnlargement += 1.0f;
181#endif
[695]182  }
183 
184  for (it = mEntries.begin(); it != mEntries.end(); ++ it) {
185        float aSumPdf = (*it).second.mSumPdf;
186        float bSumPdf = 0.0f;
187        if (aSumPdf < 0.0f) {
188          // this entry was already accounted for!
189          // just revert it back
190          (*it).second.mSumPdf = -aSumPdf;
191        } else {
192          PvsData<T> *data = b.Find((*it).first);
193          if (data) {
[704]194                bSumPdf = data->mSumPdf;
[695]195          }
[713]196#if 0
[695]197          float diff = bSumPdf - aSumPdf;
198         
199          if (diff > 0.0f) {
200                pvsEnlargement += diff;
201          } else {
202                pvsReduction += -diff;
203          }
[713]204
205#else
206          if (!data)
207                pvsReduction += 1.0f;
208#endif
[695]209        }
210  }
[677]211}
212
213template <typename T>
[362]214int Pvs<T>::Diff(const Pvs<T> &b)
215{
216        int dif = 0;
217
218        std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
219
220        for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
221        {
222                PvsData<T> *data = Find((*it).first);           
223                if (!data) ++ dif;
224        }
225
226        return dif;
227}
228
229template <typename T>
[610]230void Pvs<T>::Merge(const Pvs<T> &a)
[341]231{
[362]232        std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
233
[610]234        for (it = a.mEntries.begin(); it != a.mEntries.end(); ++ it)
[362]235        {
236                PvsData<T> *data = Find((*it).first);
237               
238                if (data)
[556]239                        data->mSumPdf += (*it).second.mSumPdf;
[362]240                else
241                        mEntries.insert(*it);
242        }
[341]243}
244
[752]245template <typename T> void Pvs<T>::Clear()
246{
247        mEntries.clear();
248       
249
250}
251
252
[310]253template <typename T>
[311]254PvsData<T> *Pvs<T>::Find(T sample)
[310]255{
[311]256  std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.find(sample);
[310]257  if (i != mEntries.end()) {
258    return &(*i).second;
[311]259  } else
[310]260    return NULL;
261}
262
263template <typename T>
[311]264void Pvs<T>::GetData(const int index,
[485]265                                         T &entry,
266                                         PvsData<T> &data)
[310]267{
[311]268  std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.begin();
[310]269  for (int k = 0; k != index && i != mEntries.end(); i++, k++);
270
271  entry = (*i).first;
[311]272  data = (*i).second;
[310]273}
274
[311]275template <typename T>
[556]276float
277Pvs<T>::AddSample(T sample, const float pdf)
[177]278{
[492]279  PvsData<T> *data = Find(sample);
280 
281  if (data)  {
[556]282        data->mSumPdf+=pdf;
283        return data->mSumPdf;
[492]284  }
285  else {
[556]286        mEntries[sample] = PvsData<T>(pdf);
287        return pdf;
[492]288  }
[466]289}
[177]290
[466]291template <typename T>
[492]292bool
[556]293Pvs<T>::AddSample(T sample,
294                                  const float pdf,
295                                  float &contribution)
[466]296{
297  PvsData<T> *data = Find(sample);
298 
299  if (data)  {
[556]300        data->mSumPdf+=pdf;
301        contribution = pdf/data->mSumPdf;
[466]302        return false;
303  }
304  else {
[556]305        mEntries[sample] = PvsData<T>(pdf);
[466]306        contribution = 1.0f;
307        return true;
308  }
[311]309}
[308]310
[469]311template <typename T>
[492]312bool
[556]313Pvs<T>::GetSampleContribution(T sample,
314                                                          const float pdf,
315                                                          float &contribution)
[492]316{
317  PvsData<T> *data = Find(sample);
318 
319  if (data)  {
[556]320        contribution = pdf/(data->mSumPdf + pdf);
[492]321        return false;
322  }
323  else {
324        contribution = 1.0f;
325        return true;
326  }
327}
328
329template <typename T>
[556]330bool Pvs<T>::RemoveSample(T sample,
331                                                  const float pdf)
[485]332{
[556]333  std::map<T, PvsData<T>, LtSample<T> >::
334        iterator it = mEntries.find(sample);
[485]335 
[556]336  if (it == mEntries.end())
337        return false;
338 
339  PvsData<T> *data = &(*it).second;
340 
341  data->mSumPdf -= pdf;
342  if (data->mSumPdf <= 0.0f)
343        mEntries.erase(it);
344 
345  return true;
[485]346}
347
348template <typename T>
349int Pvs<T>::AddPvs(const Pvs<T> &pvs)
350{
[556]351  std::map<T, PvsData<T>, LtSample<T> >::
352        const_iterator it, it_end = pvs.mEntries.end();
353 
354  float contri;
355  // output PVS of view cell
356  for (it = pvs.mEntries.begin(); it != it_end; ++ it)
357        AddSample((*it).first, (*it).second.mSumPdf, contri);
358 
359  return GetSize();
[485]360}
361 
362template <typename T>
363int Pvs<T>::SubtractPvs(const Pvs<T> &pvs)
364{
[556]365  std::map<T, PvsData<T>, LtSample<T> >::
366        const_iterator it, it_end = pvs.mEntries.end();
367 
368  // output PVS of view cell
369  for (it = pvs.mEntries.begin(); it != it_end; ++ it)
370        RemoveSample((*it).first, (*it).second.mSumPdf);
371 
372  return GetSize();
[485]373}
374
375template <typename T>
[469]376void Pvs<T>::CollectEntries(std::vector<T> &entries)
377{
[485]378        std::map<T, PvsData<T>, LtSample<T> >::
379                const_iterator it, it_end = mEntries.end();
[469]380
381        // output PVS of view cell
382        for (it = mEntries.begin(); it != it_end; ++ it)
383                entries.push_back((*it)->first);
384}
385
[556]386template <typename T>
387void Pvs<T>::NormalizeMaximum()
388{
389  std::map<T, PvsData<T>, LtSample<T> >::
390        const_iterator it, it_end = mEntries.end();
391
392  float maxPdfSum = -1.0f;
393
394  // output PVS of view cell
395  for (it = mEntries.begin(); it != it_end; ++ it) {
396        float sum = (*it)->second.sumPdf;
397        if (sum > maxSum)
398          maxSum = sum;
399  }
400
401  maxSum = 1.0f/maxSum;
402
403  for (it = mEntries.begin(); it != it_end; ++ it) {
404        (*it)->second.sumPdf *= maxSum;
405  }
406 
407}
408
409
[311]410/** Class instantiating the Pvs template for kd tree nodes.
411*/
412class KdPvs: public Pvs<KdNode *>
[308]413{
[311]414        int Compress();
[308]415};
416
[311]417typedef std::map<KdNode *, PvsData<KdNode *>, LtSample<KdNode *> > KdPvsMap;
[469]418typedef std::map<Intersectable *, PvsData<Intersectable *>, LtSample<Intersectable *> > ObjectPvsMap;
419typedef PvsData<Intersectable *> ObjectPvsData;
[311]420typedef PvsData<KdNode *> KdPvsData;
[308]421
[469]422typedef Pvs<Intersectable *> ObjectPvs;
[366]423
[469]424
[177]425#endif
426
Note: See TracBrowser for help on using the repository browser.