1 | #ifndef __PVS_H
|
---|
2 | #define __PVS_H
|
---|
3 |
|
---|
4 | #include <map>
|
---|
5 | #include <vector>
|
---|
6 | #include "common.h"
|
---|
7 |
|
---|
8 | namespace GtpVisibilityPreprocessor {
|
---|
9 |
|
---|
10 | class KdNode;
|
---|
11 | class BspNode;
|
---|
12 | class Ray;
|
---|
13 | class Intersectable;
|
---|
14 | class ViewCell;
|
---|
15 |
|
---|
16 |
|
---|
17 | /** Information stored with a PVS entry. Consists of the number
|
---|
18 | the object was seen from the view cell.
|
---|
19 | */
|
---|
20 | template<typename T, typename S>
|
---|
21 | class PvsEntry
|
---|
22 | {
|
---|
23 | public:
|
---|
24 |
|
---|
25 | PvsEntry() {}
|
---|
26 |
|
---|
27 | PvsEntry(T sample, const S &data): mObject(sample), mData(data) {}
|
---|
28 |
|
---|
29 | T mObject;
|
---|
30 | S mData;
|
---|
31 |
|
---|
32 | template<typename T, typename S>
|
---|
33 | friend int operator< (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b);
|
---|
34 | template<typename T, typename S>
|
---|
35 | friend int operator== (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b);
|
---|
36 | };
|
---|
37 |
|
---|
38 |
|
---|
39 | template<typename T, typename S>
|
---|
40 | int operator< (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b)
|
---|
41 | {
|
---|
42 | return a.mObject < b.mObject;
|
---|
43 | }
|
---|
44 |
|
---|
45 | template<typename T, typename S>
|
---|
46 | int operator== (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b)
|
---|
47 | {
|
---|
48 | return a.mObject == b.mObject;
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | template<typename T, typename S>
|
---|
53 | struct LtSample
|
---|
54 | {
|
---|
55 | bool operator()(const PvsEntry<T, S> &a, const PvsEntry<T, S> &b) const
|
---|
56 | {
|
---|
57 | return a.mObject < b.mObject;
|
---|
58 | }
|
---|
59 | };
|
---|
60 |
|
---|
61 | template<typename T, typename S>
|
---|
62 | int equalSample (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b)
|
---|
63 | {
|
---|
64 | return a.mObject == b.mObject;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /** Information stored with a PVS entry. Consists of the number
|
---|
68 | the object was seen from the view cell.
|
---|
69 | */
|
---|
70 | class PvsData {
|
---|
71 | public:
|
---|
72 | PvsData() {}
|
---|
73 | PvsData(const float sumPdf):
|
---|
74 | mSumPdf(sumPdf) {}
|
---|
75 |
|
---|
76 | // $$JB in order to return meaningfull values
|
---|
77 | // it assumes that the sum pdf has been normalized somehow!!!
|
---|
78 | inline float GetVisibility()
|
---|
79 | {
|
---|
80 | return mSumPdf;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /// sum of probability density of visible sample rays
|
---|
84 | float mSumPdf;
|
---|
85 | };
|
---|
86 |
|
---|
87 |
|
---|
88 | class MailablePvsData
|
---|
89 | {
|
---|
90 | public:
|
---|
91 | // sum of probability density of visible sample rays
|
---|
92 | float mSumPdf;
|
---|
93 | int mCounter;
|
---|
94 |
|
---|
95 | MailablePvsData() {}
|
---|
96 | MailablePvsData(const float sumPdf):
|
---|
97 | mSumPdf(sumPdf) {}
|
---|
98 |
|
---|
99 | // $$JB in order to return meaningfull values
|
---|
100 | // it assumes that the sum pdf has been normalized somehow!!!
|
---|
101 | float GetVisibility()
|
---|
102 | {
|
---|
103 | return mSumPdf;
|
---|
104 | }
|
---|
105 |
|
---|
106 | ////////////////////////////
|
---|
107 | // Mailing stuff
|
---|
108 |
|
---|
109 | // last mail id -> warning not thread safe!
|
---|
110 | // both mailId and mailbox should be unique for each thread!!!
|
---|
111 | static int sMailId;
|
---|
112 | static int sReservedMailboxes;
|
---|
113 |
|
---|
114 | static void NewMail(const int reserve = 1) {
|
---|
115 | sMailId += sReservedMailboxes;
|
---|
116 | sReservedMailboxes = reserve;
|
---|
117 | }
|
---|
118 |
|
---|
119 | void Mail() { mMailbox = sMailId; }
|
---|
120 | bool Mailed() const { return mMailbox == sMailId; }
|
---|
121 |
|
---|
122 | void Mail(const int mailbox) { mMailbox = sMailId + mailbox; }
|
---|
123 | bool Mailed(const int mailbox) const { return mMailbox == sMailId + mailbox; }
|
---|
124 |
|
---|
125 | int IncMail() { return ++ mMailbox - sMailId; }
|
---|
126 |
|
---|
127 | //////////////////////////////////////////
|
---|
128 |
|
---|
129 | protected:
|
---|
130 |
|
---|
131 | int mMailbox;
|
---|
132 |
|
---|
133 | };
|
---|
134 |
|
---|
135 |
|
---|
136 | template<typename T, typename S>
|
---|
137 | class PvsIterator
|
---|
138 | {
|
---|
139 | public:
|
---|
140 | PvsIterator<T, S>(){}
|
---|
141 | PvsIterator<T, S>(const typename vector<PvsEntry<T, S> >::const_iterator &itCurrent,
|
---|
142 | const typename vector<PvsEntry<T, S> >::const_iterator &itEnd):
|
---|
143 | mItCurrent(itCurrent), mItEnd(itEnd)
|
---|
144 | {
|
---|
145 | }
|
---|
146 |
|
---|
147 | bool HasMoreEntries() const
|
---|
148 | {
|
---|
149 | return (mItCurrent != mItEnd);
|
---|
150 | }
|
---|
151 |
|
---|
152 | const PvsEntry<T, S> &Next()
|
---|
153 | {
|
---|
154 | return *(mItCurrent ++);
|
---|
155 | }
|
---|
156 |
|
---|
157 | private:
|
---|
158 | typename vector<PvsEntry<T, S> >::const_iterator mItCurrent;
|
---|
159 | typename vector<PvsEntry<T, S> >::const_iterator mItEnd;
|
---|
160 | };
|
---|
161 |
|
---|
162 |
|
---|
163 | /** Template class representing the Potentially Visible Set (PVS)
|
---|
164 | mainly from a view cell, but also e.g., from objects.
|
---|
165 | */
|
---|
166 | template<typename T, typename S>
|
---|
167 | class Pvs
|
---|
168 | {
|
---|
169 | template<typename T, typename S>
|
---|
170 | friend class PvsIterator;
|
---|
171 |
|
---|
172 | public:
|
---|
173 |
|
---|
174 | Pvs(): mSamples(0), mEntries(), mLastSorted(0) {}
|
---|
175 |
|
---|
176 | /** creates pvs and initializes it with the given entries.
|
---|
177 | Assumes that entries are sorted-
|
---|
178 | */
|
---|
179 | Pvs(const vector<PvsEntry<T, S> > &samples);
|
---|
180 | virtual ~Pvs() {};
|
---|
181 |
|
---|
182 | /** Compresses PVS lossless or lossy.
|
---|
183 | */
|
---|
184 | int Compress() {return 0;}
|
---|
185 | int GetSize() const {return (int)mEntries.size();}
|
---|
186 | bool Empty() const {return mEntries.empty();}
|
---|
187 |
|
---|
188 | void Reserve(const int n) { mEntries.reserve(n); }
|
---|
189 |
|
---|
190 | /** Normalize the visibility of entries in order to get
|
---|
191 | comparable results.
|
---|
192 | */
|
---|
193 | void NormalizeMaximum();
|
---|
194 |
|
---|
195 | /** Merges pvs of a into this pvs.
|
---|
196 | Warning: very slow!
|
---|
197 | */
|
---|
198 | void MergeInPlace(const Pvs<T, S> &a);
|
---|
199 |
|
---|
200 | /** Difference of pvs to pvs b.
|
---|
201 | @returns number of different entries.
|
---|
202 | */
|
---|
203 | int Diff(const Pvs<T, S> &b);
|
---|
204 |
|
---|
205 | /** Finds sample in PVS.
|
---|
206 | @param checkDirty if dirty part of the pvs should be checked for entry
|
---|
207 | (warning: linear runtime in dirty part)
|
---|
208 | @returns iterator on the sample.
|
---|
209 | */
|
---|
210 | bool Find(T sample,
|
---|
211 | typename vector<PvsEntry<T, S> >::iterator &it,
|
---|
212 | const bool checkDirty = true);
|
---|
213 |
|
---|
214 | bool GetSampleContribution(T sample, const float pdf, float &contribution);
|
---|
215 |
|
---|
216 | /** Adds sample to PVS.
|
---|
217 | @returns contribution of sample (0 or 1)
|
---|
218 | */
|
---|
219 | float AddSample(T sample, const float pdf);
|
---|
220 |
|
---|
221 | /** Adds sample to PVS without checking for presence of the sample
|
---|
222 | pvs remains unsorted!
|
---|
223 | */
|
---|
224 | void AddSampleDirty(T sample, const float pdf);
|
---|
225 |
|
---|
226 | /** Adds sample dirty (on the end of the vector) but
|
---|
227 | first checks if sample is already in clean part of the pvs.
|
---|
228 | */
|
---|
229 | bool AddSampleDirtyCheck(T sample, const float pdf);//, float &contribution);
|
---|
230 |
|
---|
231 | /** Sort pvs entries - this should always be called after a
|
---|
232 | sequence of AddSampleDirty calls
|
---|
233 | */
|
---|
234 | void Sort();
|
---|
235 |
|
---|
236 | /** Sort pvs entries assume that the pvs contains unique entries
|
---|
237 | */
|
---|
238 | void SimpleSort();
|
---|
239 |
|
---|
240 | /** Adds sample to PVS. Assumes that the pvs is sorted
|
---|
241 | @returns contribution of sample (0 or 1)
|
---|
242 | */
|
---|
243 | //float AddSamples(const vector<PvsEntry<T, S> > &samples);
|
---|
244 |
|
---|
245 | /** Adds sample to PVS.
|
---|
246 | @returns PvsData
|
---|
247 | */
|
---|
248 | typename std::vector<PvsEntry<T, S> >::iterator AddSample2(T sample, const float pdf);
|
---|
249 |
|
---|
250 | /** Subtracts one pvs from another one.
|
---|
251 | WARNING: could contains bugs
|
---|
252 | @returns new pvs size
|
---|
253 | */
|
---|
254 | int SubtractPvs(const Pvs<T, S> &pvs);
|
---|
255 |
|
---|
256 | /** Returns PVS data, i.e., how often it was seen from the view cell,
|
---|
257 | and the object itsef.
|
---|
258 | */
|
---|
259 | void GetData(const int index, T &entry, S &data);
|
---|
260 |
|
---|
261 | /** Collects the PVS entries and returns them in the vector.
|
---|
262 | */
|
---|
263 | void CollectEntries(std::vector<T> &entries);
|
---|
264 |
|
---|
265 | /** Removes sample from PVS if reference count is zero.
|
---|
266 | @param visibleSamples number of references to be removed
|
---|
267 | */
|
---|
268 | bool RemoveSample(T sample, const float pdf);
|
---|
269 |
|
---|
270 | /** Compute continuous PVS difference
|
---|
271 | */
|
---|
272 | void ComputeContinuousPvsDifference(Pvs<T, S> &pvs,
|
---|
273 | float &pvsReduction,
|
---|
274 | float &pvsEnlargement);
|
---|
275 |
|
---|
276 | /** Clears the pvs.
|
---|
277 | */
|
---|
278 | void Clear(const bool trim = true);
|
---|
279 |
|
---|
280 | void Trim();
|
---|
281 |
|
---|
282 | static int GetEntrySizeByte();
|
---|
283 | static float GetEntrySize();
|
---|
284 |
|
---|
285 | /** Compute continuous PVS difference
|
---|
286 | */
|
---|
287 | float GetPvsHomogenity(Pvs<T, S> &pvs);
|
---|
288 |
|
---|
289 | static void Merge(Pvs<T, S> &mergedPvs, const Pvs<T, S> &a, const Pvs<T, S> &b);
|
---|
290 |
|
---|
291 | static void Merge(Pvs<T, S> &mergedPvs,
|
---|
292 | const typename std::vector<PvsEntry<T, S> >::const_iterator &aBegin,
|
---|
293 | const typename std::vector<PvsEntry<T, S> >::const_iterator &aEnd,
|
---|
294 | const typename std::vector<PvsEntry<T, S> >::const_iterator &bBegin,
|
---|
295 | const typename std::vector<PvsEntry<T, S> >::const_iterator &bEnd,
|
---|
296 | const int aSamples,
|
---|
297 | const int bSamples);
|
---|
298 |
|
---|
299 | int GetSamples() const
|
---|
300 | {
|
---|
301 | return mSamples;
|
---|
302 | }
|
---|
303 |
|
---|
304 |
|
---|
305 | bool IsDirty() const
|
---|
306 | {
|
---|
307 | return mLastSorted < mEntries.size();
|
---|
308 | }
|
---|
309 |
|
---|
310 | bool RequiresResort() const
|
---|
311 | {
|
---|
312 | // the last part should not be more than log of the sorted part. this
|
---|
313 | // way we can achieve logarithmic behaviour for insertion and find
|
---|
314 | const int dirtySize = (int)mEntries.size() - mLastSorted;
|
---|
315 | return dirtySize > 4*(int)(log((double)mEntries.size()) / log(2.0));
|
---|
316 | }
|
---|
317 |
|
---|
318 |
|
---|
319 | int GetLastSorted() const
|
---|
320 | {
|
---|
321 | return mLastSorted;
|
---|
322 | }
|
---|
323 |
|
---|
324 | typename PvsIterator<T, S> GetIterator() const;
|
---|
325 |
|
---|
326 | protected:
|
---|
327 |
|
---|
328 | /// vector of PVS entries
|
---|
329 | vector<PvsEntry<T, S> > mEntries;
|
---|
330 |
|
---|
331 | /// Number of samples used to create the PVS
|
---|
332 | int mSamples;
|
---|
333 |
|
---|
334 | /// Last sorted entry in the pvs (important for find and merge)
|
---|
335 | int mLastSorted;
|
---|
336 | };
|
---|
337 |
|
---|
338 |
|
---|
339 | template <typename T, typename S>
|
---|
340 | Pvs<T, S>::Pvs(const vector<PvsEntry<T, S> > &samples)
|
---|
341 | {
|
---|
342 | mEntries.reserve(samples.size());
|
---|
343 | mEntries = samples;
|
---|
344 | mLastSorted = 0;
|
---|
345 | mSamples = samples.size();
|
---|
346 | }
|
---|
347 |
|
---|
348 |
|
---|
349 | template <typename T, typename S>
|
---|
350 | void Pvs<T, S>::Sort()
|
---|
351 | {
|
---|
352 | std::vector<PvsEntry<T, S> >::iterator it = mEntries.begin() + mLastSorted;
|
---|
353 | //std::vector<PvsEntry<T, S> >::const_iterator it = mEntries.begin() + mLastSorted;
|
---|
354 | std::vector<PvsEntry<T, S> >::iterator it_end = mEntries.end();
|
---|
355 |
|
---|
356 | // throw out double entries
|
---|
357 | std::vector<PvsEntry<T, S> >::iterator newEnd = unique(it, it_end);
|
---|
358 | sort(it, newEnd);
|
---|
359 | //sort(mEntries.begin(), mEntries.end());
|
---|
360 |
|
---|
361 | // now merge sorted ranges
|
---|
362 | ObjectPvs newPvs;
|
---|
363 | Merge(newPvs,
|
---|
364 | mEntries.begin(), it,
|
---|
365 | it, newEnd,
|
---|
366 | mSamples, 0);
|
---|
367 |
|
---|
368 | mEntries = newPvs.mEntries;
|
---|
369 | mLastSorted = (int)mEntries.size();
|
---|
370 | }
|
---|
371 |
|
---|
372 | template <typename T, typename S>
|
---|
373 | void Pvs<T, S>::SimpleSort()
|
---|
374 | {
|
---|
375 | sort(mEntries.begin(), mEntries.end());
|
---|
376 | mLastSorted = (int)mEntries.size();
|
---|
377 | }
|
---|
378 |
|
---|
379 |
|
---|
380 | /**
|
---|
381 | Compute continuous PVS difference of 'b' with respect to the base PVS (*this).
|
---|
382 | Provides separatelly PVS reduction from PVS enlargement.
|
---|
383 |
|
---|
384 | */
|
---|
385 | template <typename T, typename S>
|
---|
386 | void
|
---|
387 | Pvs<T, S>::ComputeContinuousPvsDifference(Pvs<T, S> &b,
|
---|
388 | float &pvsReduction,
|
---|
389 | float &pvsEnlargement)
|
---|
390 | {
|
---|
391 | pvsReduction = 0.0f;
|
---|
392 | pvsEnlargement = 0.0f;
|
---|
393 |
|
---|
394 | // Uses sum of log differences, which corresponds to entropy
|
---|
395 | std::vector<PvsEntry<T, S> >::iterator it;
|
---|
396 |
|
---|
397 | for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
|
---|
398 | {
|
---|
399 | float bSumPdf = (*it).mData.mSumPdf;
|
---|
400 | float aSumPdf = 0.0f;
|
---|
401 |
|
---|
402 | vector<PvsEntry<T, S> >::iterator oit;
|
---|
403 | const bool entryFound = Find((*it).mObject, oit);
|
---|
404 |
|
---|
405 | if (entryFound)
|
---|
406 | {
|
---|
407 | aSumPdf = (*it).mData.mSumPdf;
|
---|
408 |
|
---|
409 | // mark this entry as processed to avoid double counting
|
---|
410 | (*it).mData.mSumPdf = -aSumPdf;
|
---|
411 | }
|
---|
412 |
|
---|
413 | #if 0
|
---|
414 | const float diff = bSumPdf - aSumPdf;
|
---|
415 |
|
---|
416 | if (diff > 0.0f) {
|
---|
417 | pvsEnlargement += diff;
|
---|
418 | } else {
|
---|
419 | pvsReduction += -diff;
|
---|
420 | }
|
---|
421 | #else
|
---|
422 | if (!entryFound)
|
---|
423 | pvsEnlargement += 1.0f;
|
---|
424 | #endif
|
---|
425 | }
|
---|
426 |
|
---|
427 | for (it = mEntries.begin(); it != mEntries.end(); ++ it)
|
---|
428 | {
|
---|
429 | float aSumPdf = (*it).mData.mSumPdf;
|
---|
430 | float bSumPdf = 0.0f;
|
---|
431 | if (aSumPdf < 0.0f) {
|
---|
432 |
|
---|
433 | // this entry was already accounted for!
|
---|
434 | // just revert it back
|
---|
435 | (*it).mData.mSumPdf = -aSumPdf;
|
---|
436 | } else {
|
---|
437 | vector<PvsEntry<T, S> >::iterator oit;
|
---|
438 |
|
---|
439 | const bool entryFound = b.Find((*it).mObject, oit);
|
---|
440 |
|
---|
441 | if (entryFound) {
|
---|
442 | bSumPdf = (*oit).mData.mSumPdf;
|
---|
443 | }
|
---|
444 | #if 0
|
---|
445 | const float diff = bSumPdf - aSumPdf;
|
---|
446 |
|
---|
447 | if (diff > 0.0f) {
|
---|
448 | pvsEnlargement += diff;
|
---|
449 | } else {
|
---|
450 | pvsReduction += -diff;
|
---|
451 | }
|
---|
452 |
|
---|
453 | #else
|
---|
454 | if (!entryFound)
|
---|
455 | pvsReduction += 1.0f;
|
---|
456 | #endif
|
---|
457 | }
|
---|
458 | }
|
---|
459 | }
|
---|
460 |
|
---|
461 |
|
---|
462 | template <typename T, typename S>
|
---|
463 | int Pvs<T, S>::Diff(const Pvs<T, S> &b)
|
---|
464 | {
|
---|
465 | int dif = 0;
|
---|
466 |
|
---|
467 | std::vector<PvsEntry<T, S> >::const_iterator it;
|
---|
468 |
|
---|
469 | for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
|
---|
470 | {
|
---|
471 | vector<PvsEntry<T, S> >::iterator bit;
|
---|
472 | const bool entryFound = Find((*it).first, bit);
|
---|
473 |
|
---|
474 | if (!entryFound) ++ dif;
|
---|
475 | }
|
---|
476 |
|
---|
477 | return dif;
|
---|
478 | }
|
---|
479 |
|
---|
480 |
|
---|
481 | template <typename T, typename S>
|
---|
482 | void Pvs<T, S>::MergeInPlace(const Pvs<T, S> &a)
|
---|
483 | {
|
---|
484 | // early exit
|
---|
485 | if (a.Empty())
|
---|
486 | {
|
---|
487 | return;
|
---|
488 | }
|
---|
489 | else if (Empty())
|
---|
490 | {
|
---|
491 | mEntries.reserve(a.GetSize());
|
---|
492 | mEntries = a.mEntries;
|
---|
493 | mSamples = a.mSamples;
|
---|
494 | return;
|
---|
495 | }
|
---|
496 |
|
---|
497 | ObjectPvs interPvs;
|
---|
498 |
|
---|
499 | Merge(interPvs, *this, a);
|
---|
500 |
|
---|
501 | mEntries.reserve(interPvs.GetSize());
|
---|
502 | mEntries = interPvs.mEntries;
|
---|
503 | mSamples = interPvs.mSamples;
|
---|
504 | }
|
---|
505 |
|
---|
506 |
|
---|
507 | template <typename T, typename S>
|
---|
508 | void Pvs<T, S>::Merge(Pvs<T, S> &mergedPvs, const Pvs<T, S> &a, const Pvs<T, S> &b)
|
---|
509 | {
|
---|
510 | std::vector<PvsEntry<T, S> >::const_iterator ait = a.mEntries.begin(), ait_end = a.mEntries.end();
|
---|
511 | std::vector<PvsEntry<T, S> >::const_iterator bit = b.mEntries.begin(), bit_end = b.mEntries.end();
|
---|
512 |
|
---|
513 | Merge(mergedPvs,
|
---|
514 | ait, ait_end,
|
---|
515 | bit, bit_end,
|
---|
516 | a.mSamples,
|
---|
517 | b.mSamples);
|
---|
518 | }
|
---|
519 |
|
---|
520 |
|
---|
521 | template <typename T, typename S>
|
---|
522 | void Pvs<T, S>::Merge(Pvs<T, S> &mergedPvs,
|
---|
523 | const typename std::vector<PvsEntry<T, S> >::const_iterator &aBegin,
|
---|
524 | const typename std::vector<PvsEntry<T, S> >::const_iterator &aEnd,
|
---|
525 | const typename std::vector<PvsEntry<T, S> >::const_iterator &bBegin,
|
---|
526 | const typename std::vector<PvsEntry<T, S> >::const_iterator &bEnd,
|
---|
527 | const int aSamples,
|
---|
528 | const int bSamples)
|
---|
529 | {
|
---|
530 | std::vector<PvsEntry<T, S> >::const_iterator ait = aBegin;
|
---|
531 | std::vector<PvsEntry<T, S> >::const_iterator bit = bBegin;
|
---|
532 |
|
---|
533 | for (; (ait != aEnd); ++ ait)
|
---|
534 | {
|
---|
535 | Intersectable *aObj = (*ait).mObject;
|
---|
536 | Intersectable *bObj = NULL;
|
---|
537 | //Intersectable *bObjOld = NULL;
|
---|
538 |
|
---|
539 | const PvsEntry<T, S> &aEntry = (*ait);
|
---|
540 |
|
---|
541 | for (; (bit != bEnd) && ((*bit).mObject <= (*ait).mObject); ++ bit)
|
---|
542 | {
|
---|
543 | bObj = (*bit).mObject;
|
---|
544 |
|
---|
545 | // object found => add up probabilities
|
---|
546 | if (bObj == aEntry.mObject)
|
---|
547 | {
|
---|
548 | PvsData newData(aEntry.mData.mSumPdf + (*bit).mData.mSumPdf);
|
---|
549 | PvsEntry<T, S> entry(bObj, newData);
|
---|
550 | mergedPvs.mEntries.push_back(entry);
|
---|
551 | }
|
---|
552 | else
|
---|
553 | {
|
---|
554 | mergedPvs.mEntries.push_back(*bit);
|
---|
555 | }
|
---|
556 |
|
---|
557 | //bObjOld = bObj;
|
---|
558 | }
|
---|
559 |
|
---|
560 | // only push back if objects different
|
---|
561 | // (equal case is handled by second loop)
|
---|
562 | if (aObj != bObj)
|
---|
563 | {
|
---|
564 | mergedPvs.mEntries.push_back(*ait);
|
---|
565 | }
|
---|
566 | }
|
---|
567 |
|
---|
568 | // add the rest
|
---|
569 | for (; (bit != bEnd); ++ bit)
|
---|
570 | {
|
---|
571 | mergedPvs.mEntries.push_back(*bit);
|
---|
572 | }
|
---|
573 |
|
---|
574 | mergedPvs.mSamples = aSamples + bSamples;
|
---|
575 | }
|
---|
576 |
|
---|
577 |
|
---|
578 | template <typename T, typename S> void Pvs<T, S>::Clear(const bool trim = true)
|
---|
579 | {
|
---|
580 | mEntries.clear();
|
---|
581 | mSamples = 0;
|
---|
582 | mLastSorted = 0;
|
---|
583 |
|
---|
584 | if (trim)
|
---|
585 | {
|
---|
586 | vector<PvsEntry<T,S> >().swap(mEntries);
|
---|
587 | }
|
---|
588 | }
|
---|
589 |
|
---|
590 |
|
---|
591 | template <typename T, typename S> void Pvs<T, S>::Trim()
|
---|
592 | {
|
---|
593 | vector<PvsEntry<T,S> >(mEntries).swap(mEntries);
|
---|
594 | }
|
---|
595 |
|
---|
596 |
|
---|
597 | template <typename T, typename S>
|
---|
598 | bool Pvs<T, S>::Find(T sample,
|
---|
599 | typename vector<PvsEntry<T, S> >::iterator &it,
|
---|
600 | const bool checkDirty)
|
---|
601 | {
|
---|
602 | bool found = false;
|
---|
603 |
|
---|
604 | PvsEntry<T, S> dummy(sample, PvsData());
|
---|
605 |
|
---|
606 | // only check clean part
|
---|
607 | vector<PvsEntry<T, S> >::iterator sorted_end = mEntries.begin() + mLastSorted;
|
---|
608 |
|
---|
609 | // binary search
|
---|
610 | it = lower_bound(mEntries.begin(), sorted_end, dummy);
|
---|
611 |
|
---|
612 | if ((it != mEntries.end()) && ((*it).mObject == sample))
|
---|
613 | found = true;
|
---|
614 |
|
---|
615 | // sample not found yet => search further in the unsorted part
|
---|
616 | if (!found && checkDirty) {
|
---|
617 |
|
---|
618 | for (it = sorted_end; (it != mEntries.end()) && ((*it).mObject != sample); ++ it) ;
|
---|
619 |
|
---|
620 | if (it != mEntries.end())
|
---|
621 | found = true;
|
---|
622 | }
|
---|
623 |
|
---|
624 | return found;
|
---|
625 | }
|
---|
626 |
|
---|
627 |
|
---|
628 | template <typename T, typename S>
|
---|
629 | void Pvs<T, S>::GetData(const int index, T &entry, S &data)
|
---|
630 | {
|
---|
631 | std::vector<PvsEntry<T, S> >::iterator i = mEntries.begin();
|
---|
632 | for (int k = 0; k != index && i != mEntries.end(); ++ i, ++ k);
|
---|
633 |
|
---|
634 | entry = (*i).first;
|
---|
635 | data = (*i).second;
|
---|
636 | }
|
---|
637 |
|
---|
638 |
|
---|
639 | template <typename T, typename S>
|
---|
640 | float Pvs<T, S>::AddSample(T sample, const float pdf)
|
---|
641 | {
|
---|
642 | ++ mSamples;
|
---|
643 |
|
---|
644 | vector<PvsEntry<T, S> >::iterator it;
|
---|
645 | const bool entryFound = Find(sample, it);
|
---|
646 |
|
---|
647 | if (entryFound)
|
---|
648 | {
|
---|
649 | S &data = (*it).mData;
|
---|
650 | data.mSumPdf += pdf;
|
---|
651 | return data.mSumPdf;
|
---|
652 | }
|
---|
653 | else
|
---|
654 | {
|
---|
655 | PvsEntry<T, S> entry(sample, pdf);
|
---|
656 | mEntries.insert(it, entry);
|
---|
657 | ++ mLastSorted;
|
---|
658 | return pdf;
|
---|
659 | }
|
---|
660 | }
|
---|
661 |
|
---|
662 |
|
---|
663 | template <typename T, typename S>
|
---|
664 | void Pvs<T, S>::AddSampleDirty(T sample, const float pdf)
|
---|
665 | {
|
---|
666 | ++ mSamples;
|
---|
667 | mEntries.push_back(PvsEntry<T, S>(sample, pdf));
|
---|
668 | }
|
---|
669 |
|
---|
670 |
|
---|
671 | template <typename T, typename S>
|
---|
672 | typename vector< PvsEntry<T, S> >::iterator Pvs<T, S>::AddSample2(T sample,
|
---|
673 | const float pdf)
|
---|
674 | {
|
---|
675 | ++ mSamples;
|
---|
676 |
|
---|
677 | vector<PvsEntry<T, S> >::iterator it;
|
---|
678 | const bool entryFound == Find(sample, it);
|
---|
679 |
|
---|
680 | if (entryFound)
|
---|
681 | {
|
---|
682 | S &data = (*it).second;
|
---|
683 | data->mSumPdf += pdf;
|
---|
684 | }
|
---|
685 | else
|
---|
686 | {
|
---|
687 | PvsEntry<T, S> entry(sample, pdf);
|
---|
688 | mEntries.insert(it, entry);
|
---|
689 | ++ mLastSorted;
|
---|
690 | }
|
---|
691 |
|
---|
692 | return it;
|
---|
693 | }
|
---|
694 |
|
---|
695 |
|
---|
696 | /** Adds sample dirty (on the end of the vector) but
|
---|
697 | first checks if sample is already in clean part of the pvs.
|
---|
698 | */
|
---|
699 | template <typename T, typename S>
|
---|
700 | bool Pvs<T, S>::AddSampleDirtyCheck(T sample,
|
---|
701 | const float pdf)
|
---|
702 | //,float &contribution)
|
---|
703 | {
|
---|
704 | ++ mSamples;
|
---|
705 |
|
---|
706 | vector<PvsEntry<T, S> >::iterator it;
|
---|
707 | bool entryFound = Find(sample, it);
|
---|
708 |
|
---|
709 | if (entryFound) {
|
---|
710 | S &data = (*it).mData;
|
---|
711 |
|
---|
712 | data.mSumPdf += pdf;
|
---|
713 | //contribution = pdf / data.mSumPdf;
|
---|
714 |
|
---|
715 | return false;
|
---|
716 | }
|
---|
717 | else {
|
---|
718 | AddSampleDirty(sample, pdf);
|
---|
719 | //contribution = 1.0f;
|
---|
720 | return true;
|
---|
721 | }
|
---|
722 | }
|
---|
723 |
|
---|
724 |
|
---|
725 | template <typename T, typename S>
|
---|
726 | bool Pvs<T, S>::GetSampleContribution(T sample,
|
---|
727 | const float pdf,
|
---|
728 | float &contribution)
|
---|
729 | {
|
---|
730 | vector<PvsEntry<T, S> >::iterator it;
|
---|
731 | const bool entryFound = Find(sample, it);
|
---|
732 |
|
---|
733 | if (entryFound)
|
---|
734 | {
|
---|
735 | S &data = (*it).mData;
|
---|
736 | contribution = pdf / (data.mSumPdf + pdf);
|
---|
737 | return false;
|
---|
738 | }
|
---|
739 | else
|
---|
740 | {
|
---|
741 | contribution = 1.0f;
|
---|
742 | return true;
|
---|
743 | }
|
---|
744 | }
|
---|
745 |
|
---|
746 |
|
---|
747 | template <typename T, typename S>
|
---|
748 | bool Pvs<T, S>::RemoveSample(T sample, const float pdf)
|
---|
749 | {
|
---|
750 | -- mSamples;
|
---|
751 |
|
---|
752 | vector<PvsEntry<T, S> >::iterator it;
|
---|
753 | const bool entryFound = Find(sample, it);
|
---|
754 |
|
---|
755 | if (!entryFound)
|
---|
756 | return false;
|
---|
757 |
|
---|
758 | S &data = (*it).mData;
|
---|
759 |
|
---|
760 | data.mSumPdf -= pdf;
|
---|
761 |
|
---|
762 | if (data.mSumPdf <= 0.0f)
|
---|
763 | {
|
---|
764 | mEntries.erase(it);
|
---|
765 | -- mLastSorted; // wrong if sample was in tail!!
|
---|
766 | }
|
---|
767 |
|
---|
768 | return true;
|
---|
769 | }
|
---|
770 |
|
---|
771 |
|
---|
772 | template <typename T, typename S>
|
---|
773 | int Pvs<T, S>::SubtractPvs(const Pvs<T, S> &pvs)
|
---|
774 | {
|
---|
775 | const int samples = mSamples - pvs.mSamples;
|
---|
776 |
|
---|
777 | std::vector<PvsEntry<T, S> >::
|
---|
778 | const_iterator it, it_end = pvs.mEntries.end();
|
---|
779 |
|
---|
780 | // output PVS of view cell
|
---|
781 | for (it = pvs.mEntries.begin(); it != it_end; ++ it)
|
---|
782 | RemoveSample((*it).mObject, (*it).mData.mSumPdf);
|
---|
783 |
|
---|
784 | mSamples = samples;
|
---|
785 |
|
---|
786 | return GetSize();
|
---|
787 | }
|
---|
788 |
|
---|
789 |
|
---|
790 | template <typename T, typename S>
|
---|
791 | void Pvs<T, S>::CollectEntries(std::vector<T> &entries)
|
---|
792 | {
|
---|
793 | std::vector<PvsEntry<T, S> >::
|
---|
794 | const_iterator it, it_end = mEntries.end();
|
---|
795 |
|
---|
796 | // output PVS of view cell
|
---|
797 | for (it = mEntries.begin(); it != it_end; ++ it)
|
---|
798 | entries.push_back((*it)->first);
|
---|
799 | }
|
---|
800 |
|
---|
801 |
|
---|
802 | template <typename T, typename S>
|
---|
803 | void Pvs<T, S>::NormalizeMaximum()
|
---|
804 | {
|
---|
805 | std::vector<PvsEntry<T, S> >::
|
---|
806 | const_iterator it, it_end = mEntries.end();
|
---|
807 |
|
---|
808 | float maxPdfSum = -1.0f;
|
---|
809 |
|
---|
810 | // output PVS of view cell
|
---|
811 | for (it = mEntries.begin(); it != it_end; ++ it) {
|
---|
812 | float sum = (*it)->second.sumPdf;
|
---|
813 | if (sum > maxSum)
|
---|
814 | maxSum = sum;
|
---|
815 | }
|
---|
816 |
|
---|
817 | maxSum = 1.0f / maxSum;
|
---|
818 |
|
---|
819 | for (it = mEntries.begin(); it != it_end; ++ it) {
|
---|
820 | (*it)->second.sumPdf *= maxSum;
|
---|
821 | }
|
---|
822 |
|
---|
823 | }
|
---|
824 |
|
---|
825 |
|
---|
826 | template <typename T, typename S>
|
---|
827 | float Pvs<T, S>::GetEntrySize()
|
---|
828 | {
|
---|
829 | return (float)(sizeof(T) + sizeof(S)) / float(1024 * 1024);
|
---|
830 | }
|
---|
831 |
|
---|
832 |
|
---|
833 | template <typename T, typename S>
|
---|
834 | int Pvs<T, S>::GetEntrySizeByte()
|
---|
835 | {
|
---|
836 | return sizeof(T) + sizeof(S);
|
---|
837 | }
|
---|
838 |
|
---|
839 |
|
---|
840 | template <typename T, typename S>
|
---|
841 | float Pvs<T, S>::GetPvsHomogenity(Pvs<T, S> &pvs)
|
---|
842 | {
|
---|
843 | float pvsReduction, pvsEnlargement;
|
---|
844 |
|
---|
845 | ComputeContinuousPvsDifference(pvs, pvsReduction, pvsEnlargement);
|
---|
846 |
|
---|
847 | return pvsReduction + pvsEnlargement;
|
---|
848 | }
|
---|
849 |
|
---|
850 |
|
---|
851 | template <typename T, typename S>
|
---|
852 | typename PvsIterator<T, S> Pvs<T, S>::GetIterator() const
|
---|
853 | {
|
---|
854 | PvsIterator<T, S> pit(mEntries.begin(), mEntries.end());
|
---|
855 |
|
---|
856 | return pit;
|
---|
857 | }
|
---|
858 |
|
---|
859 |
|
---|
860 | ///////////////////////////////////////
|
---|
861 |
|
---|
862 | /** Class instantiating the Pvs template for kd tree nodes.
|
---|
863 | */
|
---|
864 | class KdPvs: public Pvs<KdNode *, PvsData>
|
---|
865 | {
|
---|
866 | public:
|
---|
867 | int Compress();
|
---|
868 | };
|
---|
869 |
|
---|
870 |
|
---|
871 | ////////////
|
---|
872 | //-- typedefs
|
---|
873 |
|
---|
874 | typedef PvsEntry<Intersectable *, PvsData> ObjectPvsEntry;
|
---|
875 | typedef std::vector<ObjectPvsEntry> ObjectPvsEntries;
|
---|
876 | typedef Pvs<ViewCell *, MailablePvsData> ViewCellPvs;
|
---|
877 | typedef PvsIterator<Intersectable *, PvsData> ObjectPvsIterator;
|
---|
878 |
|
---|
879 |
|
---|
880 | class ObjectPvs: public Pvs<Intersectable *, PvsData>
|
---|
881 | {
|
---|
882 | public:
|
---|
883 | /** Counts object int the pvs. Different to method "GetSize", not
|
---|
884 | only the raw container size is returned,
|
---|
885 | but the individual contributions of the entries are summed up.
|
---|
886 | */
|
---|
887 | float EvalPvsCost() const;
|
---|
888 |
|
---|
889 | friend ostream &operator<<(ostream &s, const ObjectPvs &p)
|
---|
890 | {
|
---|
891 | ObjectPvsIterator pit = p.GetIterator();
|
---|
892 |
|
---|
893 | while (pit.HasMoreEntries())
|
---|
894 | {
|
---|
895 | const ObjectPvsEntry &entry = pit.Next();
|
---|
896 | Intersectable *obj = entry.mObject;
|
---|
897 |
|
---|
898 | cout << obj << " ";
|
---|
899 | }
|
---|
900 |
|
---|
901 | return s;
|
---|
902 | }
|
---|
903 | };
|
---|
904 |
|
---|
905 |
|
---|
906 |
|
---|
907 | }
|
---|
908 |
|
---|
909 | #endif
|
---|
910 |
|
---|