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 |
|
---|
117 |
|
---|
118 | /// Map of PVS entries
|
---|
119 | std::map<T, PvsData<T>, LtSample<T> > mEntries;
|
---|
120 | };
|
---|
121 |
|
---|
122 |
|
---|
123 |
|
---|
124 | /** Compute continuous PVS difference */
|
---|
125 | template <typename T>
|
---|
126 | void
|
---|
127 | Pvs<T>::ComputeContinuousPvsDifference(const Pvs<T> &pvs,
|
---|
128 | float &pvsReduction,
|
---|
129 | float &pvsEnlargement)
|
---|
130 | {
|
---|
131 |
|
---|
132 |
|
---|
133 | }
|
---|
134 |
|
---|
135 | template <typename T>
|
---|
136 | int Pvs<T>::Diff(const Pvs<T> &b)
|
---|
137 | {
|
---|
138 | int dif = 0;
|
---|
139 |
|
---|
140 | std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
|
---|
141 |
|
---|
142 | for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
|
---|
143 | {
|
---|
144 | PvsData<T> *data = Find((*it).first);
|
---|
145 | if (!data) ++ dif;
|
---|
146 | }
|
---|
147 |
|
---|
148 | return dif;
|
---|
149 | }
|
---|
150 |
|
---|
151 | template <typename T>
|
---|
152 | void Pvs<T>::Merge(const Pvs<T> &a)
|
---|
153 | {
|
---|
154 | std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
|
---|
155 |
|
---|
156 | for (it = a.mEntries.begin(); it != a.mEntries.end(); ++ it)
|
---|
157 | {
|
---|
158 | PvsData<T> *data = Find((*it).first);
|
---|
159 |
|
---|
160 | if (data)
|
---|
161 | data->mSumPdf += (*it).second.mSumPdf;
|
---|
162 | else
|
---|
163 | mEntries.insert(*it);
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | template <typename T>
|
---|
168 | PvsData<T> *Pvs<T>::Find(T sample)
|
---|
169 | {
|
---|
170 | std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.find(sample);
|
---|
171 | if (i != mEntries.end()) {
|
---|
172 | return &(*i).second;
|
---|
173 | } else
|
---|
174 | return NULL;
|
---|
175 | }
|
---|
176 |
|
---|
177 | template <typename T>
|
---|
178 | void Pvs<T>::GetData(const int index,
|
---|
179 | T &entry,
|
---|
180 | PvsData<T> &data)
|
---|
181 | {
|
---|
182 | std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.begin();
|
---|
183 | for (int k = 0; k != index && i != mEntries.end(); i++, k++);
|
---|
184 |
|
---|
185 | entry = (*i).first;
|
---|
186 | data = (*i).second;
|
---|
187 | }
|
---|
188 |
|
---|
189 | template <typename T>
|
---|
190 | float
|
---|
191 | Pvs<T>::AddSample(T sample, const float pdf)
|
---|
192 | {
|
---|
193 | PvsData<T> *data = Find(sample);
|
---|
194 |
|
---|
195 | if (data) {
|
---|
196 | data->mSumPdf+=pdf;
|
---|
197 | return data->mSumPdf;
|
---|
198 | }
|
---|
199 | else {
|
---|
200 | mEntries[sample] = PvsData<T>(pdf);
|
---|
201 | return pdf;
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | template <typename T>
|
---|
206 | bool
|
---|
207 | Pvs<T>::AddSample(T sample,
|
---|
208 | const float pdf,
|
---|
209 | float &contribution)
|
---|
210 | {
|
---|
211 | PvsData<T> *data = Find(sample);
|
---|
212 |
|
---|
213 | if (data) {
|
---|
214 | data->mSumPdf+=pdf;
|
---|
215 | contribution = pdf/data->mSumPdf;
|
---|
216 | return false;
|
---|
217 | }
|
---|
218 | else {
|
---|
219 | mEntries[sample] = PvsData<T>(pdf);
|
---|
220 | contribution = 1.0f;
|
---|
221 | return true;
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | template <typename T>
|
---|
226 | bool
|
---|
227 | Pvs<T>::GetSampleContribution(T sample,
|
---|
228 | const float pdf,
|
---|
229 | float &contribution)
|
---|
230 | {
|
---|
231 | PvsData<T> *data = Find(sample);
|
---|
232 |
|
---|
233 | if (data) {
|
---|
234 | contribution = pdf/(data->mSumPdf + pdf);
|
---|
235 | return false;
|
---|
236 | }
|
---|
237 | else {
|
---|
238 | contribution = 1.0f;
|
---|
239 | return true;
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | template <typename T>
|
---|
244 | bool Pvs<T>::RemoveSample(T sample,
|
---|
245 | const float pdf)
|
---|
246 | {
|
---|
247 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
248 | iterator it = mEntries.find(sample);
|
---|
249 |
|
---|
250 | if (it == mEntries.end())
|
---|
251 | return false;
|
---|
252 |
|
---|
253 | PvsData<T> *data = &(*it).second;
|
---|
254 |
|
---|
255 | data->mSumPdf -= pdf;
|
---|
256 | if (data->mSumPdf <= 0.0f)
|
---|
257 | mEntries.erase(it);
|
---|
258 |
|
---|
259 | return true;
|
---|
260 | }
|
---|
261 |
|
---|
262 | template <typename T>
|
---|
263 | int Pvs<T>::AddPvs(const Pvs<T> &pvs)
|
---|
264 | {
|
---|
265 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
266 | const_iterator it, it_end = pvs.mEntries.end();
|
---|
267 |
|
---|
268 | float contri;
|
---|
269 | // output PVS of view cell
|
---|
270 | for (it = pvs.mEntries.begin(); it != it_end; ++ it)
|
---|
271 | AddSample((*it).first, (*it).second.mSumPdf, contri);
|
---|
272 |
|
---|
273 | return GetSize();
|
---|
274 | }
|
---|
275 |
|
---|
276 | template <typename T>
|
---|
277 | int Pvs<T>::SubtractPvs(const Pvs<T> &pvs)
|
---|
278 | {
|
---|
279 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
280 | const_iterator it, it_end = pvs.mEntries.end();
|
---|
281 |
|
---|
282 | // output PVS of view cell
|
---|
283 | for (it = pvs.mEntries.begin(); it != it_end; ++ it)
|
---|
284 | RemoveSample((*it).first, (*it).second.mSumPdf);
|
---|
285 |
|
---|
286 | return GetSize();
|
---|
287 | }
|
---|
288 |
|
---|
289 | template <typename T>
|
---|
290 | void Pvs<T>::CollectEntries(std::vector<T> &entries)
|
---|
291 | {
|
---|
292 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
293 | const_iterator it, it_end = mEntries.end();
|
---|
294 |
|
---|
295 | // output PVS of view cell
|
---|
296 | for (it = mEntries.begin(); it != it_end; ++ it)
|
---|
297 | entries.push_back((*it)->first);
|
---|
298 | }
|
---|
299 |
|
---|
300 | template <typename T>
|
---|
301 | void Pvs<T>::NormalizeMaximum()
|
---|
302 | {
|
---|
303 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
304 | const_iterator it, it_end = mEntries.end();
|
---|
305 |
|
---|
306 | float maxPdfSum = -1.0f;
|
---|
307 |
|
---|
308 | // output PVS of view cell
|
---|
309 | for (it = mEntries.begin(); it != it_end; ++ it) {
|
---|
310 | float sum = (*it)->second.sumPdf;
|
---|
311 | if (sum > maxSum)
|
---|
312 | maxSum = sum;
|
---|
313 | }
|
---|
314 |
|
---|
315 | maxSum = 1.0f/maxSum;
|
---|
316 |
|
---|
317 | for (it = mEntries.begin(); it != it_end; ++ it) {
|
---|
318 | (*it)->second.sumPdf *= maxSum;
|
---|
319 | }
|
---|
320 |
|
---|
321 | }
|
---|
322 |
|
---|
323 |
|
---|
324 | /** Class instantiating the Pvs template for kd tree nodes.
|
---|
325 | */
|
---|
326 | class KdPvs: public Pvs<KdNode *>
|
---|
327 | {
|
---|
328 | int Compress();
|
---|
329 | };
|
---|
330 |
|
---|
331 | typedef std::map<KdNode *, PvsData<KdNode *>, LtSample<KdNode *> > KdPvsMap;
|
---|
332 | typedef std::map<Intersectable *, PvsData<Intersectable *>, LtSample<Intersectable *> > ObjectPvsMap;
|
---|
333 | typedef PvsData<Intersectable *> ObjectPvsData;
|
---|
334 | typedef PvsData<KdNode *> KdPvsData;
|
---|
335 |
|
---|
336 | typedef Pvs<Intersectable *> ObjectPvs;
|
---|
337 |
|
---|
338 |
|
---|
339 | #endif
|
---|
340 |
|
---|