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

Revision 1673, 11.3 KB checked in by mattausch, 18 years ago (diff)
Line 
1#ifndef __PVS_H
2#define __PVS_H
3
4#include <map>
5#include <vector>
6#include "common.h"
7
8namespace GtpVisibilityPreprocessor {
9
10class KdNode;
11class BspNode;
12class Ray;
13class Intersectable;
14class ViewCell;
15
16
17template<typename T>
18struct LtSample
19{
20    bool operator()(const T a, const T b) const
21    {
22                return a < b;
23        }
24};
25
26/** Information stored with a PVS entry. Consists of the number
27        the object was seen from the view cell.
28*/
29class PvsData {
30public:
31  // sum of probability density of visible sample rays
32  float mSumPdf;
33
34  PvsData() {}
35  PvsData(const float sumPdf):
36        mSumPdf(sumPdf) {}
37
38  // $$JB in order to return meaningfull values
39  // it assumes that the sum pdf has been normalized somehow!!!
40  float GetVisibility()
41  {
42          return mSumPdf;
43  }
44};
45
46
47class MailablePvsData
48{
49////////////////////////////
50//  Mailing stuff
51protected:
52        int mMailbox;
53
54public:
55  // last mail id -> warning not thread safe!
56  // both mailId and mailbox should be unique for each thread!!!
57  static int sMailId;
58  static int sReservedMailboxes;
59 
60 
61        static void NewMail(const int reserve = 1) {
62                sMailId += sReservedMailboxes;
63                sReservedMailboxes = reserve;
64        }
65       
66        void Mail() { mMailbox = sMailId; }
67        bool Mailed() const { return mMailbox == sMailId; }
68
69        void Mail(const int mailbox) { mMailbox = sMailId + mailbox; }
70        bool Mailed(const int mailbox) const { return mMailbox == sMailId + mailbox; }
71
72        int IncMail() { return ++ mMailbox - sMailId; }
73//////////////////////////////////////////
74
75  // sum of probability density of visible sample rays
76  float mSumPdf;
77  int mCounter;
78
79  MailablePvsData() {}
80  MailablePvsData(const float sumPdf):
81        mSumPdf(sumPdf) {}
82
83  // $$JB in order to return meaningfull values
84  // it assumes that the sum pdf has been normalized somehow!!!
85  float GetVisibility()
86  {
87          return mSumPdf;
88  }
89};
90
91
92/** Template class representing the Potentially Visible Set (PVS)
93        mainly from a view cell, but also e.g., from objects.
94*/
95template<typename T, typename S>
96class Pvs
97{
98public:
99  Pvs(): /*mSamples(0), */mEntries() {}
100 
101  //virtual ~Pvs();
102   
103  /** Compresses PVS lossless or lossy.
104  */
105  int Compress() {return 0;}
106  int GetSize() const {return (int)mEntries.size();}
107  bool Empty() const {return mEntries.empty();}
108
109  /** Normalize the visibility of entries in order to get comparable
110          results
111  */
112  void NormalizeMaximum();
113 
114  /** Merges pvs of a into this pvs.
115   */
116  void Merge(const Pvs<T, S> &a);
117 
118  /** Difference of pvs to pvs b.
119          @returns number of different entries.
120  */
121  int Diff(const Pvs<T, S> &b);
122 
123  /** Finds sample in PVS.
124          @returns sample if found, NULL otherwise.
125  */
126  S *Find(T sample);
127
128  bool GetSampleContribution(T sample, const float pdf, float &contribution);
129 
130  /** Adds sample to PVS.
131          @contribution contribution of sample (0 or 1)
132          @returns true if sample was not already in PVS.
133  */
134  bool AddSample(T sample, const float pdf, float &contribution);
135
136  /** Adds sample to PVS.
137          @returns contribution of sample (0 or 1)
138  */
139  float AddSample(T sample, const float pdf);
140 
141  /** Adds sample to PVS.
142          @returns PvsData
143  */
144  S *AddSample2(T sample, const float pdf);
145
146  /** Adds one pvs to another one.
147          @returns new pvs size
148  */
149  int AddPvs(const Pvs<T, S> &pvs);
150
151  /** Subtracts one pvs from another one.
152  WARNING: could contains bugs
153          @returns new pvs size
154  */
155  int SubtractPvs(const Pvs<T, S> &pvs);
156  /** Returns PVS data, i.e., how often it was seen from the view cell,
157          and the object itsef.
158  */
159  void GetData(const int index, T &entry, S &data);
160
161  /** Collects the PVS entries and returns them in the vector.
162  */
163  void CollectEntries(std::vector<T> &entries);
164
165  /** Removes sample from PVS if reference count is zero.
166          @param visibleSamples number of references to be removed
167  */
168  bool RemoveSample(T sample, const float pdf);
169
170  /** Compute continuous PVS difference */
171  void ComputeContinuousPvsDifference(Pvs<T, S> &pvs,
172                                                                          float &pvsReduction,
173                                                                          float &pvsEnlargement);
174 
175
176  /** Clears the pvs.
177  */
178  void Clear();
179
180  static int GetEntrySizeByte();
181  static float GetEntrySize();
182
183  /** Compute continuous PVS difference */
184  float GetPvsHomogenity(Pvs<T, S> &pvs) {
185        float
186          pvsReduction,
187          pvsEnlargement;
188       
189        ComputeContinuousPvsDifference(pvs,
190                                                                   pvsReduction,
191                                                                   pvsEnlargement);
192       
193        return pvsReduction + pvsEnlargement;
194  }               
195                                         
196  /// Map of PVS entries
197  std::map<T, S, LtSample<T> > mEntries;
198};
199
200
201
202/**
203   Compute continuous PVS difference of 'b' with respect to the base PVS (*this).
204   Provides separatelly PVS reduction from PVS enlargement.
205
206*/
207template <typename T, typename S>
208void
209Pvs<T, S>::ComputeContinuousPvsDifference(Pvs<T, S> &b,
210                                                                           float &pvsReduction,
211                                                                           float &pvsEnlargement)
212{
213        pvsReduction = 0.0f;
214        pvsEnlargement = 0.0f;
215  // Uses sum of log differences, which corresponds to entropy
216  std::map<T, S, LtSample<T> >::iterator it;
217 
218  for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
219  {
220        float bSumPdf = (*it).second.mSumPdf;
221        float aSumPdf = 0.0f;
222        S *data = Find((*it).first);           
223       
224        if (data)
225        {
226                aSumPdf = data->mSumPdf;
227                // mark this entry as processed to avoid double counting
228                data->mSumPdf = -aSumPdf;
229        }
230
231#if 0
232        float diff = bSumPdf - aSumPdf;
233       
234        if (diff > 0.0f) {
235          pvsEnlargement += diff;
236        } else {
237          pvsReduction += -diff;
238        }
239#else
240        if (!data)
241          pvsEnlargement += 1.0f;
242#endif
243  }
244 
245  for (it = mEntries.begin(); it != mEntries.end(); ++ it) {
246        float aSumPdf = (*it).second.mSumPdf;
247        float bSumPdf = 0.0f;
248        if (aSumPdf < 0.0f) {
249          // this entry was already accounted for!
250          // just revert it back
251          (*it).second.mSumPdf = -aSumPdf;
252        } else {
253          S *data = b.Find((*it).first);
254          if (data) {
255                bSumPdf = data->mSumPdf;
256          }
257#if 0
258          float diff = bSumPdf - aSumPdf;
259         
260          if (diff > 0.0f) {
261                pvsEnlargement += diff;
262          } else {
263                pvsReduction += -diff;
264          }
265
266#else
267          if (!data)
268                pvsReduction += 1.0f;
269#endif
270        }
271  }
272}
273
274template <typename T, typename S>
275int Pvs<T, S>::Diff(const Pvs<T, S> &b)
276{
277        int dif = 0;
278
279        std::map<T, S, LtSample<T> >::const_iterator it;
280
281        for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
282        {
283                S data = Find((*it).first);             
284                if (!data) ++ dif;
285        }
286
287        return dif;
288}
289
290template <typename T, typename S> void Pvs<T, S>::Merge(const Pvs<T, S> &a)
291{
292        std::map<T, S, LtSample<T> >::const_iterator it;
293
294        for (it = a.mEntries.begin(); it != a.mEntries.end(); ++ it)
295        {
296                AddSample((*it).first, (*it).second.mSumPdf);
297        }
298}
299
300
301template <typename T, typename S> void Pvs<T, S>::Clear()
302{
303        mEntries.clear();
304}
305
306
307template <typename T, typename S>
308S *Pvs<T, S>::Find(T sample)
309{
310  std::map<T, S, LtSample<T> >::iterator i = mEntries.find(sample);
311
312  if (i != mEntries.end())
313  {
314          return &(*i).second;
315  }
316  else
317  {
318          return NULL;
319  }
320}
321
322template <typename T, typename S>
323void Pvs<T, S>::GetData(const int index,
324                                         T &entry,
325                                         S &data)
326{
327  std::map<T, S, LtSample<T> >::iterator i = mEntries.begin();
328  for (int k = 0; k != index && i != mEntries.end(); i++, k++);
329
330  entry = (*i).first;
331  data = (*i).second;
332 
333}
334
335template <typename T, typename S>
336float
337Pvs<T, S>::AddSample(T sample, const float pdf)
338{
339        S *data = Find(sample);
340 
341        if (data) 
342        {
343                data->mSumPdf += pdf;
344                return data->mSumPdf;
345        }
346        else
347        {
348                mEntries[sample] = S(pdf);
349                return pdf;
350        }
351}
352
353
354template <typename T, typename S>
355S * Pvs<T, S>::AddSample2(T sample, const float pdf)
356{
357        S *data = Find(sample);
358 
359        if (data) 
360        {
361                data->mSumPdf += pdf;
362        }
363        else
364        {
365                mEntries[sample] = S(pdf);
366                data = Find(sample);
367        }
368
369        return data;
370}
371
372template <typename T, typename S>
373bool Pvs<T, S>::AddSample(T sample,
374                                  const float pdf,
375                                  float &contribution)
376{
377  S *data = Find(sample);
378 
379  if (data) 
380  {
381        data->mSumPdf += pdf;
382        contribution = pdf / data->mSumPdf;
383        return false;
384  }
385  else {
386        mEntries[sample] = S(pdf);
387        contribution = 1.0f;
388        return true;
389  }
390}
391
392template <typename T, typename S>
393bool
394Pvs<T, S>::GetSampleContribution(T sample,
395                                                                 const float pdf,
396                                                                 float &contribution)
397{
398  S *data = Find(sample);
399 
400  if (data)  {
401        contribution = pdf / (data->mSumPdf + pdf);
402        return false;
403  }
404  else {
405        contribution = 1.0f;
406        return true;
407  }
408}
409
410template <typename T, typename S>
411bool Pvs<T, S>::RemoveSample(T sample,
412                                                  const float pdf)
413{
414  std::map<T, S, LtSample<T> >::
415        iterator it = mEntries.find(sample);
416 
417  if (it == mEntries.end())
418        return false;
419 
420  S *data = &(*it).second;
421 
422  data->mSumPdf -= pdf;
423
424  if (data->mSumPdf <= 0.0f)
425  {
426          mEntries.erase(it);
427  }
428
429  return true;
430}
431
432template <typename T, typename S>
433int Pvs<T, S>::AddPvs(const Pvs<T, S> &pvs)
434{
435  std::map<T, S, LtSample<T> >::
436        const_iterator it, it_end = pvs.mEntries.end();
437 
438  float contri;
439  // output PVS of view cell
440  for (it = pvs.mEntries.begin(); it != it_end; ++ it)
441  {     
442          AddSample((*it).first, (*it).second.mSumPdf, contri);
443  }
444 
445  return GetSize();
446}
447 
448template <typename T, typename S>
449int Pvs<T, S>::SubtractPvs(const Pvs<T, S> &pvs)
450{
451  std::map<T, S, LtSample<T> >::
452        const_iterator it, it_end = pvs.mEntries.end();
453 
454  // output PVS of view cell
455  for (it = pvs.mEntries.begin(); it != it_end; ++ it)
456        RemoveSample((*it).first, (*it).second.mSumPdf);
457 
458  return GetSize();
459}
460
461template <typename T, typename S>
462void Pvs<T, S>::CollectEntries(std::vector<T> &entries)
463{
464        std::map<T, S, LtSample<T> >::
465                const_iterator it, it_end = mEntries.end();
466
467        // output PVS of view cell
468        for (it = mEntries.begin(); it != it_end; ++ it)
469                entries.push_back((*it)->first);
470}
471
472template <typename T, typename S>
473void Pvs<T, S>::NormalizeMaximum()
474{
475  std::map<T, S, LtSample<T> >::
476        const_iterator it, it_end = mEntries.end();
477
478  float maxPdfSum = -1.0f;
479
480  // output PVS of view cell
481  for (it = mEntries.begin(); it != it_end; ++ it) {
482        float sum = (*it)->second.sumPdf;
483        if (sum > maxSum)
484          maxSum = sum;
485  }
486
487  maxSum = 1.0f / maxSum;
488
489  for (it = mEntries.begin(); it != it_end; ++ it) {
490        (*it)->second.sumPdf *= maxSum;
491  }
492 
493}
494
495
496template <typename T, typename S>
497float Pvs<T, S>::GetEntrySize()
498{
499        return (float)(sizeof(T) + sizeof(S)) / float(1024 * 1024);
500}
501
502
503template <typename T, typename S>
504int Pvs<T, S>::GetEntrySizeByte()
505{
506        return sizeof(T) + sizeof(S);
507}
508
509
510///////////////////////////////////////
511
512/** Class instantiating the Pvs template for kd tree nodes.
513*/
514class KdPvs: public Pvs<KdNode *, PvsData>
515{
516public:
517        int Compress();
518};
519
520
521class ObjectPvs: public Pvs<Intersectable *, PvsData>
522{
523public:
524        /** Counts object int the pvs. Different to method "GetSize", not
525                only the raw container size is returned,
526                but the individual contributions of the entries are summed up.
527        */
528        int CountObjectsInPvs() const;
529};
530
531
532////////////
533//-- typedefs
534
535typedef std::map<KdNode *, PvsData, LtSample<KdNode *> > KdPvsMap;
536typedef std::map<Intersectable *, PvsData, LtSample<Intersectable *> > ObjectPvsMap;
537typedef std::map<ViewCell *, MailablePvsData, LtSample<ViewCell *> > ViewCellPvsMap;
538
539
540typedef Pvs<ViewCell *, MailablePvsData> ViewCellPvs;
541
542}
543
544#endif
545
Note: See TracBrowser for help on using the repository browser.