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

Revision 469, 4.4 KB checked in by mattausch, 19 years ago (diff)

updated view cells, view cell manager. changed rendersimulator

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  PvsData<T>() {}
29  PvsData<T>(const int samples): mVisibleSamples(samples) {}
30};
31
32/** Template class representing the Potentially Visible Set (PVS)
33        mainly from a view cell, but also e.g., from objects.
34*/
35template<typename T>
36class Pvs
37{
38public:
39  Pvs(): /*mSamples(0), */mEntries() {}
40 
41  //int mSamples;
42 
43  /** Compresses PVS lossless or lossy.
44  */
45  int Compress() {return 0;}
46  int GetSize() const {return (int)mEntries.size();}
47  bool Empty() const {return mEntries.empty();}
48 
49  /** Merges pvs of a and pvs of b into this pvs.
50   */
51  void Merge(const Pvs<T> &a, const Pvs<T> &b);
52 
53  /** Difference of pvs to pvs b.
54          @returns number of different entries.
55  */
56  int Diff(const Pvs<T> &b);
57 
58  /** Finds sample in PVS.
59          @returns sample if found, NULL otherwise.
60  */
61  PvsData<T> *Find(T sample);
62 
63  /** Adds sample to PVS.
64          @contribution contribution of sample (0 or 1)
65          @returns true if sample was not already in PVS.
66  */
67  bool AddSample(T sample, float &contribution);
68
69  /** Adds sample to PVS.
70          @returns contribution of sample (0 or 1)
71  */
72  int AddSample(T sample);
73
74  /** Returns PVS data, i.e., how often it was seen from the view cell,
75          and the object itsef.
76  */
77  void GetData(const int index, T &entry, PvsData<T> &data);
78
79  /** Collects the PVS entries and returns them in the vector.
80  */
81  void CollectEntries(std::vector<T> &entries);
82
83  /// Map of PVS entries
84  std::map<T, PvsData<T>, LtSample<T> > mEntries;
85};
86
87template <typename T>
88int Pvs<T>::Diff(const Pvs<T> &b)
89{
90        int dif = 0;
91
92        std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
93
94        for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
95        {
96                PvsData<T> *data = Find((*it).first);           
97                if (!data) ++ dif;
98        }
99
100        return dif;
101}
102
103template <typename T>
104void Pvs<T>::Merge(const Pvs<T> &a, const Pvs<T> &b)
105{
106        std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
107
108        // todo: copy all elements instead of inserting
109        for (it = a.mEntries.begin(); it != a.mEntries.end(); ++ it)
110        {
111                mEntries.insert(*it);
112        }
113
114        for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
115        {
116                PvsData<T> *data = Find((*it).first);
117               
118                if (data)
119                        data->mVisibleSamples += (*it).second.mVisibleSamples;
120                else
121                        mEntries.insert(*it);
122        }
123}
124
125template <typename T>
126PvsData<T> *Pvs<T>::Find(T sample)
127{
128  std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.find(sample);
129  if (i != mEntries.end()) {
130    return &(*i).second;
131  } else
132    return NULL;
133}
134
135template <typename T>
136void Pvs<T>::GetData(const int index,
137               T &entry,
138               PvsData<T> &data)
139{
140  std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.begin();
141  for (int k = 0; k != index && i != mEntries.end(); i++, k++);
142
143  entry = (*i).first;
144  data = (*i).second;
145}
146
147template <typename T>
148int Pvs<T>::AddSample(T sample)
149{
150  float dummy;
151  return AddSample(sample, dummy) ? 1 : 0;
152}
153
154template <typename T>
155bool Pvs<T>::AddSample(T sample, float &contribution)
156{
157  PvsData<T> *data = Find(sample);
158 
159  if (data)  {
160        data->mVisibleSamples++;
161        contribution = 1.0f/data->mVisibleSamples;
162        return false;
163  }
164  else {
165        mEntries[sample] = PvsData<T>(1);
166        contribution = 1.0f;
167        return true;
168  }
169}
170
171template <typename T>
172void Pvs<T>::CollectEntries(std::vector<T> &entries)
173{
174        PvsMap<T>::const_iterator it, it_end = mEntries.end();
175
176        // output PVS of view cell
177        for (it = mEntries.begin(); it != it_end; ++ it)
178                entries.push_back((*it)->first);
179}
180
181/** Class instantiating the Pvs template for kd tree nodes.
182*/
183class KdPvs: public Pvs<KdNode *>
184{
185        int Compress();
186};
187
188typedef std::map<KdNode *, PvsData<KdNode *>, LtSample<KdNode *> > KdPvsMap;
189typedef std::map<Intersectable *, PvsData<Intersectable *>, LtSample<Intersectable *> > ObjectPvsMap;
190typedef PvsData<Intersectable *> ObjectPvsData;
191typedef PvsData<KdNode *> KdPvsData;
192
193typedef Pvs<Intersectable *> ObjectPvs;
194
195
196#endif
197
Note: See TracBrowser for help on using the repository browser.