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

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

added mesh instance support
improved support for occlusion queries + other extensions

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