1 | #ifndef __PVS_H
|
---|
2 | #define __PVS_H
|
---|
3 |
|
---|
4 | #include <map>
|
---|
5 | #include <vector>
|
---|
6 |
|
---|
7 | class KdNode;
|
---|
8 | class BspNode;
|
---|
9 | class Ray;
|
---|
10 | class Intersectable;
|
---|
11 | class ViewCellKdNode;
|
---|
12 |
|
---|
13 | template<typename T>
|
---|
14 | struct 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 |
|
---|
25 | template<typename T>
|
---|
26 | struct 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 | */
|
---|
35 | template<typename T>
|
---|
36 | class Pvs
|
---|
37 | {
|
---|
38 | public:
|
---|
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 | bool GetSampleContribution(T sample, float &contribution);
|
---|
64 |
|
---|
65 | /** Adds sample to PVS.
|
---|
66 | @contribution contribution of sample (0 or 1)
|
---|
67 | @returns true if sample was not already in PVS.
|
---|
68 | */
|
---|
69 | bool AddSample(T sample, float &contribution);
|
---|
70 |
|
---|
71 | /** Adds sample to PVS.
|
---|
72 | @returns contribution of sample (0 or 1)
|
---|
73 | */
|
---|
74 | int AddSample(T sample);
|
---|
75 |
|
---|
76 | /** Adds one pvs to another one.
|
---|
77 | @returns new pvs size
|
---|
78 | */
|
---|
79 | int AddPvs(const Pvs<T> &pvs);
|
---|
80 | /** Subtracts one pvs from another one.
|
---|
81 | @returns new pvs size
|
---|
82 | */
|
---|
83 | int SubtractPvs(const Pvs<T> &pvs);
|
---|
84 | /** Returns PVS data, i.e., how often it was seen from the view cell,
|
---|
85 | and the object itsef.
|
---|
86 | */
|
---|
87 | void GetData(const int index, T &entry, PvsData<T> &data);
|
---|
88 |
|
---|
89 | /** Collects the PVS entries and returns them in the vector.
|
---|
90 | */
|
---|
91 | void CollectEntries(std::vector<T> &entries);
|
---|
92 |
|
---|
93 | /** Removes sample from PVS if reference count is zero.
|
---|
94 | @param visibleSampels number of references to be removed
|
---|
95 | */
|
---|
96 | bool RemoveSample(T sample, const int visibleSamples);
|
---|
97 |
|
---|
98 | /// Map of PVS entries
|
---|
99 | std::map<T, PvsData<T>, LtSample<T> > mEntries;
|
---|
100 | };
|
---|
101 |
|
---|
102 | template <typename T>
|
---|
103 | int Pvs<T>::Diff(const Pvs<T> &b)
|
---|
104 | {
|
---|
105 | int dif = 0;
|
---|
106 |
|
---|
107 | std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
|
---|
108 |
|
---|
109 | for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
|
---|
110 | {
|
---|
111 | PvsData<T> *data = Find((*it).first);
|
---|
112 | if (!data) ++ dif;
|
---|
113 | }
|
---|
114 |
|
---|
115 | return dif;
|
---|
116 | }
|
---|
117 |
|
---|
118 | template <typename T>
|
---|
119 | void Pvs<T>::Merge(const Pvs<T> &a, const Pvs<T> &b)
|
---|
120 | {
|
---|
121 | std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
|
---|
122 |
|
---|
123 | // todo: copy all elements instead of inserting
|
---|
124 | for (it = a.mEntries.begin(); it != a.mEntries.end(); ++ it)
|
---|
125 | mEntries.insert(*it);
|
---|
126 |
|
---|
127 | for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
|
---|
128 | {
|
---|
129 | PvsData<T> *data = Find((*it).first);
|
---|
130 |
|
---|
131 | if (data)
|
---|
132 | data->mVisibleSamples += (*it).second.mVisibleSamples;
|
---|
133 | else
|
---|
134 | mEntries.insert(*it);
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | template <typename T>
|
---|
139 | PvsData<T> *Pvs<T>::Find(T sample)
|
---|
140 | {
|
---|
141 | std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.find(sample);
|
---|
142 | if (i != mEntries.end()) {
|
---|
143 | return &(*i).second;
|
---|
144 | } else
|
---|
145 | return NULL;
|
---|
146 | }
|
---|
147 |
|
---|
148 | template <typename T>
|
---|
149 | void Pvs<T>::GetData(const int index,
|
---|
150 | T &entry,
|
---|
151 | PvsData<T> &data)
|
---|
152 | {
|
---|
153 | std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.begin();
|
---|
154 | for (int k = 0; k != index && i != mEntries.end(); i++, k++);
|
---|
155 |
|
---|
156 | entry = (*i).first;
|
---|
157 | data = (*i).second;
|
---|
158 | }
|
---|
159 |
|
---|
160 | template <typename T>
|
---|
161 | int
|
---|
162 | Pvs<T>::AddSample(T sample)
|
---|
163 | {
|
---|
164 | PvsData<T> *data = Find(sample);
|
---|
165 |
|
---|
166 | if (data) {
|
---|
167 | return ++data->mVisibleSamples;
|
---|
168 | }
|
---|
169 | else {
|
---|
170 | mEntries[sample] = PvsData<T>(1);
|
---|
171 | return 1;
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | template <typename T>
|
---|
176 | bool
|
---|
177 | Pvs<T>::AddSample(T sample, float &contribution)
|
---|
178 | {
|
---|
179 | PvsData<T> *data = Find(sample);
|
---|
180 |
|
---|
181 | if (data) {
|
---|
182 | data->mVisibleSamples++;
|
---|
183 | contribution = 1.0f/data->mVisibleSamples;
|
---|
184 | return false;
|
---|
185 | }
|
---|
186 | else {
|
---|
187 | mEntries[sample] = PvsData<T>(1);
|
---|
188 | contribution = 1.0f;
|
---|
189 | return true;
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | template <typename T>
|
---|
194 | bool
|
---|
195 | Pvs<T>::GetSampleContribution(T sample, float &contribution)
|
---|
196 | {
|
---|
197 | PvsData<T> *data = Find(sample);
|
---|
198 |
|
---|
199 | if (data) {
|
---|
200 | contribution = 1.0f/(data->mVisibleSamples + 1);
|
---|
201 | return false;
|
---|
202 | }
|
---|
203 | else {
|
---|
204 | contribution = 1.0f;
|
---|
205 | return true;
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | template <typename T>
|
---|
210 | bool Pvs<T>::RemoveSample(T sample, const int visibleSamples)
|
---|
211 | {
|
---|
212 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
213 | iterator it = mEntries.find(sample);
|
---|
214 |
|
---|
215 | if (it == mEntries.end())
|
---|
216 | return false;
|
---|
217 |
|
---|
218 | PvsData<T> *data = &(*it).second;
|
---|
219 |
|
---|
220 | data->mVisibleSamples -= visibleSamples;
|
---|
221 | if (data->mVisibleSamples <= 0)
|
---|
222 | mEntries.erase(it);
|
---|
223 |
|
---|
224 | return true;
|
---|
225 | }
|
---|
226 |
|
---|
227 | template <typename T>
|
---|
228 | int Pvs<T>::AddPvs(const Pvs<T> &pvs)
|
---|
229 | {
|
---|
230 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
231 | const_iterator it, it_end = pvs.mEntries.end();
|
---|
232 |
|
---|
233 | float contri;
|
---|
234 | // output PVS of view cell
|
---|
235 | for (it = pvs.mEntries.begin(); it != it_end; ++ it)
|
---|
236 | AddSample((*it).first, contri);
|
---|
237 |
|
---|
238 | return GetSize();
|
---|
239 | }
|
---|
240 |
|
---|
241 | template <typename T>
|
---|
242 | int Pvs<T>::SubtractPvs(const Pvs<T> &pvs)
|
---|
243 | {
|
---|
244 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
245 | const_iterator it, it_end = pvs.mEntries.end();
|
---|
246 |
|
---|
247 | // output PVS of view cell
|
---|
248 | for (it = pvs.mEntries.begin(); it != it_end; ++ it)
|
---|
249 | RemoveSample((*it).first, (*it).second.mVisibleSamples);
|
---|
250 |
|
---|
251 | return GetSize();
|
---|
252 | }
|
---|
253 |
|
---|
254 | template <typename T>
|
---|
255 | void Pvs<T>::CollectEntries(std::vector<T> &entries)
|
---|
256 | {
|
---|
257 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
258 | const_iterator it, it_end = mEntries.end();
|
---|
259 |
|
---|
260 | // output PVS of view cell
|
---|
261 | for (it = mEntries.begin(); it != it_end; ++ it)
|
---|
262 | entries.push_back((*it)->first);
|
---|
263 | }
|
---|
264 |
|
---|
265 | /** Class instantiating the Pvs template for kd tree nodes.
|
---|
266 | */
|
---|
267 | class KdPvs: public Pvs<KdNode *>
|
---|
268 | {
|
---|
269 | int Compress();
|
---|
270 | };
|
---|
271 |
|
---|
272 | typedef std::map<KdNode *, PvsData<KdNode *>, LtSample<KdNode *> > KdPvsMap;
|
---|
273 | typedef std::map<Intersectable *, PvsData<Intersectable *>, LtSample<Intersectable *> > ObjectPvsMap;
|
---|
274 | typedef PvsData<Intersectable *> ObjectPvsData;
|
---|
275 | typedef PvsData<KdNode *> KdPvsData;
|
---|
276 |
|
---|
277 | typedef Pvs<Intersectable *> ObjectPvs;
|
---|
278 |
|
---|
279 |
|
---|
280 | #endif
|
---|
281 |
|
---|