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 | // 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 | */
|
---|
42 | template<typename T>
|
---|
43 | class Pvs
|
---|
44 | {
|
---|
45 | public:
|
---|
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 into this pvs.
|
---|
62 | */
|
---|
63 | void Merge(const Pvs<T> &a);
|
---|
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 | /** Compute continuous PVS difference */
|
---|
111 | void ComputeContinuousPvsDifference(const Pvs<T> &pvs,
|
---|
112 | float &pvsReduction,
|
---|
113 | float &pvsEnlargement);
|
---|
114 |
|
---|
115 |
|
---|
116 | /** Compute continuous PVS difference */
|
---|
117 | float GetPvsHomogenity(const Pvs<T> &pvs) {
|
---|
118 | float
|
---|
119 | pvsReduction,
|
---|
120 | pvsEnlargement;
|
---|
121 |
|
---|
122 | ComputeContinuousPvsDifference(pvs,
|
---|
123 | pvsReduction,
|
---|
124 | pvsEnlargement);
|
---|
125 |
|
---|
126 | return pvsReduction + pvsEnlargement;
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 |
|
---|
131 | /// Map of PVS entries
|
---|
132 | std::map<T, PvsData<T>, LtSample<T> > mEntries;
|
---|
133 | };
|
---|
134 |
|
---|
135 |
|
---|
136 |
|
---|
137 | /**
|
---|
138 | Compute continuous PVS difference of 'b' with respect to the base PVS (*this).
|
---|
139 | Provides separatelly PVS reduction from PVS enlargement.
|
---|
140 |
|
---|
141 | */
|
---|
142 | template <typename T>
|
---|
143 | void
|
---|
144 | Pvs<T>::ComputeContinuousPvsDifference(const Pvs<T> &b,
|
---|
145 | float &pvsReduction,
|
---|
146 | float &pvsEnlargement)
|
---|
147 | {
|
---|
148 | // Uses sum of log differences, which corresponds to entropy
|
---|
149 | std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
|
---|
150 |
|
---|
151 | for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it) {
|
---|
152 | float bSumPdf = (*it).second.mSumPdf;
|
---|
153 | float aSumPdf = 0.0f;
|
---|
154 | PvsData<T> *data = Find((*it).first);
|
---|
155 | if (data) {
|
---|
156 | aSumPdf = data->second.mSumPdf;
|
---|
157 | // mark this entry as processed to avoid double counting
|
---|
158 | data->second.mSumPdf = -aSumPdf;
|
---|
159 | }
|
---|
160 | float diff = bSumPdf - aSumPdf;
|
---|
161 |
|
---|
162 | if (diff > 0.0f) {
|
---|
163 | pvsEnlargement += diff;
|
---|
164 | } else {
|
---|
165 | pvsReduction += -diff;
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | for (it = mEntries.begin(); it != mEntries.end(); ++ it) {
|
---|
170 | float aSumPdf = (*it).second.mSumPdf;
|
---|
171 | float bSumPdf = 0.0f;
|
---|
172 | if (aSumPdf < 0.0f) {
|
---|
173 | // this entry was already accounted for!
|
---|
174 | // just revert it back
|
---|
175 | (*it).second.mSumPdf = -aSumPdf;
|
---|
176 | } else {
|
---|
177 | PvsData<T> *data = b.Find((*it).first);
|
---|
178 | if (data) {
|
---|
179 | bSumPdf = data->second.mSumPdf;
|
---|
180 | }
|
---|
181 | float diff = bSumPdf - aSumPdf;
|
---|
182 |
|
---|
183 | if (diff > 0.0f) {
|
---|
184 | pvsEnlargement += diff;
|
---|
185 | } else {
|
---|
186 | pvsReduction += -diff;
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | template <typename T>
|
---|
193 | int Pvs<T>::Diff(const Pvs<T> &b)
|
---|
194 | {
|
---|
195 | int dif = 0;
|
---|
196 |
|
---|
197 | std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
|
---|
198 |
|
---|
199 | for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
|
---|
200 | {
|
---|
201 | PvsData<T> *data = Find((*it).first);
|
---|
202 | if (!data) ++ dif;
|
---|
203 | }
|
---|
204 |
|
---|
205 | return dif;
|
---|
206 | }
|
---|
207 |
|
---|
208 | template <typename T>
|
---|
209 | void Pvs<T>::Merge(const Pvs<T> &a)
|
---|
210 | {
|
---|
211 | std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
|
---|
212 |
|
---|
213 | for (it = a.mEntries.begin(); it != a.mEntries.end(); ++ it)
|
---|
214 | {
|
---|
215 | PvsData<T> *data = Find((*it).first);
|
---|
216 |
|
---|
217 | if (data)
|
---|
218 | data->mSumPdf += (*it).second.mSumPdf;
|
---|
219 | else
|
---|
220 | mEntries.insert(*it);
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | template <typename T>
|
---|
225 | PvsData<T> *Pvs<T>::Find(T sample)
|
---|
226 | {
|
---|
227 | std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.find(sample);
|
---|
228 | if (i != mEntries.end()) {
|
---|
229 | return &(*i).second;
|
---|
230 | } else
|
---|
231 | return NULL;
|
---|
232 | }
|
---|
233 |
|
---|
234 | template <typename T>
|
---|
235 | void Pvs<T>::GetData(const int index,
|
---|
236 | T &entry,
|
---|
237 | PvsData<T> &data)
|
---|
238 | {
|
---|
239 | std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.begin();
|
---|
240 | for (int k = 0; k != index && i != mEntries.end(); i++, k++);
|
---|
241 |
|
---|
242 | entry = (*i).first;
|
---|
243 | data = (*i).second;
|
---|
244 | }
|
---|
245 |
|
---|
246 | template <typename T>
|
---|
247 | float
|
---|
248 | Pvs<T>::AddSample(T sample, const float pdf)
|
---|
249 | {
|
---|
250 | PvsData<T> *data = Find(sample);
|
---|
251 |
|
---|
252 | if (data) {
|
---|
253 | data->mSumPdf+=pdf;
|
---|
254 | return data->mSumPdf;
|
---|
255 | }
|
---|
256 | else {
|
---|
257 | mEntries[sample] = PvsData<T>(pdf);
|
---|
258 | return pdf;
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | template <typename T>
|
---|
263 | bool
|
---|
264 | Pvs<T>::AddSample(T sample,
|
---|
265 | const float pdf,
|
---|
266 | float &contribution)
|
---|
267 | {
|
---|
268 | PvsData<T> *data = Find(sample);
|
---|
269 |
|
---|
270 | if (data) {
|
---|
271 | data->mSumPdf+=pdf;
|
---|
272 | contribution = pdf/data->mSumPdf;
|
---|
273 | return false;
|
---|
274 | }
|
---|
275 | else {
|
---|
276 | mEntries[sample] = PvsData<T>(pdf);
|
---|
277 | contribution = 1.0f;
|
---|
278 | return true;
|
---|
279 | }
|
---|
280 | }
|
---|
281 |
|
---|
282 | template <typename T>
|
---|
283 | bool
|
---|
284 | Pvs<T>::GetSampleContribution(T sample,
|
---|
285 | const float pdf,
|
---|
286 | float &contribution)
|
---|
287 | {
|
---|
288 | PvsData<T> *data = Find(sample);
|
---|
289 |
|
---|
290 | if (data) {
|
---|
291 | contribution = pdf/(data->mSumPdf + pdf);
|
---|
292 | return false;
|
---|
293 | }
|
---|
294 | else {
|
---|
295 | contribution = 1.0f;
|
---|
296 | return true;
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | template <typename T>
|
---|
301 | bool Pvs<T>::RemoveSample(T sample,
|
---|
302 | const float pdf)
|
---|
303 | {
|
---|
304 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
305 | iterator it = mEntries.find(sample);
|
---|
306 |
|
---|
307 | if (it == mEntries.end())
|
---|
308 | return false;
|
---|
309 |
|
---|
310 | PvsData<T> *data = &(*it).second;
|
---|
311 |
|
---|
312 | data->mSumPdf -= pdf;
|
---|
313 | if (data->mSumPdf <= 0.0f)
|
---|
314 | mEntries.erase(it);
|
---|
315 |
|
---|
316 | return true;
|
---|
317 | }
|
---|
318 |
|
---|
319 | template <typename T>
|
---|
320 | int Pvs<T>::AddPvs(const Pvs<T> &pvs)
|
---|
321 | {
|
---|
322 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
323 | const_iterator it, it_end = pvs.mEntries.end();
|
---|
324 |
|
---|
325 | float contri;
|
---|
326 | // output PVS of view cell
|
---|
327 | for (it = pvs.mEntries.begin(); it != it_end; ++ it)
|
---|
328 | AddSample((*it).first, (*it).second.mSumPdf, contri);
|
---|
329 |
|
---|
330 | return GetSize();
|
---|
331 | }
|
---|
332 |
|
---|
333 | template <typename T>
|
---|
334 | int Pvs<T>::SubtractPvs(const Pvs<T> &pvs)
|
---|
335 | {
|
---|
336 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
337 | const_iterator it, it_end = pvs.mEntries.end();
|
---|
338 |
|
---|
339 | // output PVS of view cell
|
---|
340 | for (it = pvs.mEntries.begin(); it != it_end; ++ it)
|
---|
341 | RemoveSample((*it).first, (*it).second.mSumPdf);
|
---|
342 |
|
---|
343 | return GetSize();
|
---|
344 | }
|
---|
345 |
|
---|
346 | template <typename T>
|
---|
347 | void Pvs<T>::CollectEntries(std::vector<T> &entries)
|
---|
348 | {
|
---|
349 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
350 | const_iterator it, it_end = mEntries.end();
|
---|
351 |
|
---|
352 | // output PVS of view cell
|
---|
353 | for (it = mEntries.begin(); it != it_end; ++ it)
|
---|
354 | entries.push_back((*it)->first);
|
---|
355 | }
|
---|
356 |
|
---|
357 | template <typename T>
|
---|
358 | void Pvs<T>::NormalizeMaximum()
|
---|
359 | {
|
---|
360 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
361 | const_iterator it, it_end = mEntries.end();
|
---|
362 |
|
---|
363 | float maxPdfSum = -1.0f;
|
---|
364 |
|
---|
365 | // output PVS of view cell
|
---|
366 | for (it = mEntries.begin(); it != it_end; ++ it) {
|
---|
367 | float sum = (*it)->second.sumPdf;
|
---|
368 | if (sum > maxSum)
|
---|
369 | maxSum = sum;
|
---|
370 | }
|
---|
371 |
|
---|
372 | maxSum = 1.0f/maxSum;
|
---|
373 |
|
---|
374 | for (it = mEntries.begin(); it != it_end; ++ it) {
|
---|
375 | (*it)->second.sumPdf *= maxSum;
|
---|
376 | }
|
---|
377 |
|
---|
378 | }
|
---|
379 |
|
---|
380 |
|
---|
381 | /** Class instantiating the Pvs template for kd tree nodes.
|
---|
382 | */
|
---|
383 | class KdPvs: public Pvs<KdNode *>
|
---|
384 | {
|
---|
385 | int Compress();
|
---|
386 | };
|
---|
387 |
|
---|
388 | typedef std::map<KdNode *, PvsData<KdNode *>, LtSample<KdNode *> > KdPvsMap;
|
---|
389 | typedef std::map<Intersectable *, PvsData<Intersectable *>, LtSample<Intersectable *> > ObjectPvsMap;
|
---|
390 | typedef PvsData<Intersectable *> ObjectPvsData;
|
---|
391 | typedef PvsData<KdNode *> KdPvsData;
|
---|
392 |
|
---|
393 | typedef Pvs<Intersectable *> ObjectPvs;
|
---|
394 |
|
---|
395 |
|
---|
396 | #endif
|
---|
397 |
|
---|