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

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 and pvs of b into this pvs.
62   */
63  void Merge(const Pvs<T> &a, const Pvs<T> &b);
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  /// Map of PVS entries
111  std::map<T, PvsData<T>, LtSample<T> > mEntries;
112};
113
114
115template <typename T>
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>
132void Pvs<T>::Merge(const Pvs<T> &a, const Pvs<T> &b)
133{
134        std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
135
136        mEntries = a.mEntries;
137       
138        for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
139        {
140                PvsData<T> *data = Find((*it).first);
141               
142                if (data)
143                        data->mSumPdf += (*it).second.mSumPdf;
144                else
145                        mEntries.insert(*it);
146        }
147}
148
149template <typename T>
150PvsData<T> *Pvs<T>::Find(T sample)
151{
152  std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.find(sample);
153  if (i != mEntries.end()) {
154    return &(*i).second;
155  } else
156    return NULL;
157}
158
159template <typename T>
160void Pvs<T>::GetData(const int index,
161                                         T &entry,
162                                         PvsData<T> &data)
163{
164  std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.begin();
165  for (int k = 0; k != index && i != mEntries.end(); i++, k++);
166
167  entry = (*i).first;
168  data = (*i).second;
169}
170
171template <typename T>
172float
173Pvs<T>::AddSample(T sample, const float pdf)
174{
175  PvsData<T> *data = Find(sample);
176 
177  if (data)  {
178        data->mSumPdf+=pdf;
179        return data->mSumPdf;
180  }
181  else {
182        mEntries[sample] = PvsData<T>(pdf);
183        return pdf;
184  }
185}
186
187template <typename T>
188bool
189Pvs<T>::AddSample(T sample,
190                                  const float pdf,
191                                  float &contribution)
192{
193  PvsData<T> *data = Find(sample);
194 
195  if (data)  {
196        data->mSumPdf+=pdf;
197        contribution = pdf/data->mSumPdf;
198        return false;
199  }
200  else {
201        mEntries[sample] = PvsData<T>(pdf);
202        contribution = 1.0f;
203        return true;
204  }
205}
206
207template <typename T>
208bool
209Pvs<T>::GetSampleContribution(T sample,
210                                                          const float pdf,
211                                                          float &contribution)
212{
213  PvsData<T> *data = Find(sample);
214 
215  if (data)  {
216        contribution = pdf/(data->mSumPdf + pdf);
217        return false;
218  }
219  else {
220        contribution = 1.0f;
221        return true;
222  }
223}
224
225template <typename T>
226bool Pvs<T>::RemoveSample(T sample,
227                                                  const float pdf)
228{
229  std::map<T, PvsData<T>, LtSample<T> >::
230        iterator it = mEntries.find(sample);
231 
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;
242}
243
244template <typename T>
245int Pvs<T>::AddPvs(const Pvs<T> &pvs)
246{
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();
256}
257 
258template <typename T>
259int Pvs<T>::SubtractPvs(const Pvs<T> &pvs)
260{
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();
269}
270
271template <typename T>
272void Pvs<T>::CollectEntries(std::vector<T> &entries)
273{
274        std::map<T, PvsData<T>, LtSample<T> >::
275                const_iterator it, it_end = mEntries.end();
276
277        // output PVS of view cell
278        for (it = mEntries.begin(); it != it_end; ++ it)
279                entries.push_back((*it)->first);
280}
281
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
306/** Class instantiating the Pvs template for kd tree nodes.
307*/
308class KdPvs: public Pvs<KdNode *>
309{
310        int Compress();
311};
312
313typedef std::map<KdNode *, PvsData<KdNode *>, LtSample<KdNode *> > KdPvsMap;
314typedef std::map<Intersectable *, PvsData<Intersectable *>, LtSample<Intersectable *> > ObjectPvsMap;
315typedef PvsData<Intersectable *> ObjectPvsData;
316typedef PvsData<KdNode *> KdPvsData;
317
318typedef Pvs<Intersectable *> ObjectPvs;
319
320
321#endif
322
Note: See TracBrowser for help on using the repository browser.