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