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 | vector<PvsEntry<T, S> >::const_iterator dit, dit_end = mEntries.end();
|
---|
618 |
|
---|
619 | for (dit = sorted_end; (dit != dit_end) && ((*dit).mObject != sample); ++ dit) ;
|
---|
620 |
|
---|
621 | if (dit != dit_end)
|
---|
622 | found = true;
|
---|
623 | }
|
---|
624 |
|
---|
625 | return found;
|
---|
626 | }
|
---|
627 |
|
---|
628 |
|
---|
629 | template <typename T, typename S>
|
---|
630 | void Pvs<T, S>::GetData(const int index, T &entry, S &data)
|
---|
631 | {
|
---|
632 | std::vector<PvsEntry<T, S> >::iterator i = mEntries.begin();
|
---|
633 | for (int k = 0; k != index && i != mEntries.end(); ++ i, ++ k);
|
---|
634 |
|
---|
635 | entry = (*i).first;
|
---|
636 | data = (*i).second;
|
---|
637 | }
|
---|
638 |
|
---|
639 |
|
---|
640 | template <typename T, typename S>
|
---|
641 | float Pvs<T, S>::AddSample(T sample, const float pdf)
|
---|
642 | {
|
---|
643 | ++ mSamples;
|
---|
644 |
|
---|
645 | vector<PvsEntry<T, S> >::iterator it;
|
---|
646 | const bool entryFound = Find(sample, it);
|
---|
647 |
|
---|
648 | if (entryFound)
|
---|
649 | {
|
---|
650 | S &data = (*it).mData;
|
---|
651 | data.mSumPdf += pdf;
|
---|
652 | return data.mSumPdf;
|
---|
653 | }
|
---|
654 | else
|
---|
655 | {
|
---|
656 | PvsEntry<T, S> entry(sample, pdf);
|
---|
657 | mEntries.insert(it, entry);
|
---|
658 | ++ mLastSorted;
|
---|
659 | return pdf;
|
---|
660 | }
|
---|
661 | }
|
---|
662 |
|
---|
663 |
|
---|
664 | template <typename T, typename S>
|
---|
665 | void Pvs<T, S>::AddSampleDirty(T sample, const float pdf)
|
---|
666 | {
|
---|
667 | ++ mSamples;
|
---|
668 | mEntries.push_back(PvsEntry<T, S>(sample, pdf));
|
---|
669 | }
|
---|
670 |
|
---|
671 |
|
---|
672 | template <typename T, typename S>
|
---|
673 | typename vector< PvsEntry<T, S> >::iterator Pvs<T, S>::AddSample2(T sample,
|
---|
674 | const float pdf)
|
---|
675 | {
|
---|
676 | ++ mSamples;
|
---|
677 |
|
---|
678 | vector<PvsEntry<T, S> >::iterator it;
|
---|
679 | const bool entryFound == Find(sample, it);
|
---|
680 |
|
---|
681 | if (entryFound)
|
---|
682 | {
|
---|
683 | S &data = (*it).second;
|
---|
684 | data->mSumPdf += pdf;
|
---|
685 | }
|
---|
686 | else
|
---|
687 | {
|
---|
688 | PvsEntry<T, S> entry(sample, pdf);
|
---|
689 | mEntries.insert(it, entry);
|
---|
690 | ++ mLastSorted;
|
---|
691 | }
|
---|
692 |
|
---|
693 | return it;
|
---|
694 | }
|
---|
695 |
|
---|
696 |
|
---|
697 | /** Adds sample dirty (on the end of the vector) but
|
---|
698 | first checks if sample is already in clean part of the pvs.
|
---|
699 | */
|
---|
700 | template <typename T, typename S>
|
---|
701 | bool Pvs<T, S>::AddSampleDirtyCheck(T sample,
|
---|
702 | const float pdf)
|
---|
703 | //,float &contribution)
|
---|
704 | {
|
---|
705 | ++ mSamples;
|
---|
706 |
|
---|
707 | vector<PvsEntry<T, S> >::iterator it;
|
---|
708 | const bool entryFound = Find(sample, it);
|
---|
709 |
|
---|
710 | if (entryFound) {
|
---|
711 | S &data = (*it).mData;
|
---|
712 |
|
---|
713 | data.mSumPdf += pdf;
|
---|
714 | //contribution = pdf / data.mSumPdf;
|
---|
715 |
|
---|
716 | return false;
|
---|
717 | }
|
---|
718 | else {
|
---|
719 | AddSampleDirty(sample, pdf);
|
---|
720 | //contribution = 1.0f;
|
---|
721 | return true;
|
---|
722 | }
|
---|
723 | }
|
---|
724 |
|
---|
725 |
|
---|
726 | template <typename T, typename S>
|
---|
727 | bool Pvs<T, S>::GetSampleContribution(T sample,
|
---|
728 | const float pdf,
|
---|
729 | float &contribution)
|
---|
730 | {
|
---|
731 | vector<PvsEntry<T, S> >::iterator it;
|
---|
732 | const bool entryFound = Find(sample, it);
|
---|
733 |
|
---|
734 | if (entryFound)
|
---|
735 | {
|
---|
736 | S &data = (*it).mData;
|
---|
737 | contribution = pdf / (data.mSumPdf + pdf);
|
---|
738 | return false;
|
---|
739 | }
|
---|
740 | else
|
---|
741 | {
|
---|
742 | contribution = 1.0f;
|
---|
743 | return true;
|
---|
744 | }
|
---|
745 | }
|
---|
746 |
|
---|
747 |
|
---|
748 | template <typename T, typename S>
|
---|
749 | bool Pvs<T, S>::RemoveSample(T sample, const float pdf)
|
---|
750 | {
|
---|
751 | -- mSamples;
|
---|
752 |
|
---|
753 | vector<PvsEntry<T, S> >::iterator it;
|
---|
754 | const bool entryFound = Find(sample, it);
|
---|
755 |
|
---|
756 | if (!entryFound)
|
---|
757 | return false;
|
---|
758 |
|
---|
759 | S &data = (*it).mData;
|
---|
760 |
|
---|
761 | data.mSumPdf -= pdf;
|
---|
762 |
|
---|
763 | if (data.mSumPdf <= 0.0f)
|
---|
764 | {
|
---|
765 | mEntries.erase(it);
|
---|
766 | -- mLastSorted; // wrong if sample was in tail!!
|
---|
767 | }
|
---|
768 |
|
---|
769 | return true;
|
---|
770 | }
|
---|
771 |
|
---|
772 |
|
---|
773 | template <typename T, typename S>
|
---|
774 | int Pvs<T, S>::SubtractPvs(const Pvs<T, S> &pvs)
|
---|
775 | {
|
---|
776 | const int samples = mSamples - pvs.mSamples;
|
---|
777 |
|
---|
778 | std::vector<PvsEntry<T, S> >::
|
---|
779 | const_iterator it, it_end = pvs.mEntries.end();
|
---|
780 |
|
---|
781 | // output PVS of view cell
|
---|
782 | for (it = pvs.mEntries.begin(); it != it_end; ++ it)
|
---|
783 | RemoveSample((*it).mObject, (*it).mData.mSumPdf);
|
---|
784 |
|
---|
785 | mSamples = samples;
|
---|
786 |
|
---|
787 | return GetSize();
|
---|
788 | }
|
---|
789 |
|
---|
790 |
|
---|
791 | template <typename T, typename S>
|
---|
792 | void Pvs<T, S>::CollectEntries(std::vector<T> &entries)
|
---|
793 | {
|
---|
794 | std::vector<PvsEntry<T, S> >::
|
---|
795 | const_iterator it, it_end = mEntries.end();
|
---|
796 |
|
---|
797 | // output PVS of view cell
|
---|
798 | for (it = mEntries.begin(); it != it_end; ++ it)
|
---|
799 | entries.push_back((*it)->first);
|
---|
800 | }
|
---|
801 |
|
---|
802 |
|
---|
803 | template <typename T, typename S>
|
---|
804 | void Pvs<T, S>::NormalizeMaximum()
|
---|
805 | {
|
---|
806 | std::vector<PvsEntry<T, S> >::
|
---|
807 | const_iterator it, it_end = mEntries.end();
|
---|
808 |
|
---|
809 | float maxPdfSum = -1.0f;
|
---|
810 |
|
---|
811 | // output PVS of view cell
|
---|
812 | for (it = mEntries.begin(); it != it_end; ++ it) {
|
---|
813 | float sum = (*it)->second.sumPdf;
|
---|
814 | if (sum > maxSum)
|
---|
815 | maxSum = sum;
|
---|
816 | }
|
---|
817 |
|
---|
818 | maxSum = 1.0f / maxSum;
|
---|
819 |
|
---|
820 | for (it = mEntries.begin(); it != it_end; ++ it) {
|
---|
821 | (*it)->second.sumPdf *= maxSum;
|
---|
822 | }
|
---|
823 |
|
---|
824 | }
|
---|
825 |
|
---|
826 |
|
---|
827 | template <typename T, typename S>
|
---|
828 | float Pvs<T, S>::GetEntrySize()
|
---|
829 | {
|
---|
830 | return (float)(sizeof(T) + sizeof(S)) / float(1024 * 1024);
|
---|
831 | }
|
---|
832 |
|
---|
833 |
|
---|
834 | template <typename T, typename S>
|
---|
835 | int Pvs<T, S>::GetEntrySizeByte()
|
---|
836 | {
|
---|
837 | return sizeof(T) + sizeof(S);
|
---|
838 | }
|
---|
839 |
|
---|
840 |
|
---|
841 | template <typename T, typename S>
|
---|
842 | float Pvs<T, S>::GetPvsHomogenity(Pvs<T, S> &pvs)
|
---|
843 | {
|
---|
844 | float pvsReduction, pvsEnlargement;
|
---|
845 |
|
---|
846 | ComputeContinuousPvsDifference(pvs, pvsReduction, pvsEnlargement);
|
---|
847 |
|
---|
848 | return pvsReduction + pvsEnlargement;
|
---|
849 | }
|
---|
850 |
|
---|
851 |
|
---|
852 | template <typename T, typename S>
|
---|
853 | typename PvsIterator<T, S> Pvs<T, S>::GetIterator() const
|
---|
854 | {
|
---|
855 | PvsIterator<T, S> pit(mEntries.begin(), mEntries.end());
|
---|
856 |
|
---|
857 | return pit;
|
---|
858 | }
|
---|
859 |
|
---|
860 |
|
---|
861 | ///////////////////////////////////////
|
---|
862 |
|
---|
863 | /** Class instantiating the Pvs template for kd tree nodes.
|
---|
864 | */
|
---|
865 | class KdPvs: public Pvs<KdNode *, PvsData>
|
---|
866 | {
|
---|
867 | public:
|
---|
868 | int Compress();
|
---|
869 | };
|
---|
870 |
|
---|
871 |
|
---|
872 | ////////////
|
---|
873 | //-- typedefs
|
---|
874 |
|
---|
875 | typedef PvsEntry<Intersectable *, PvsData> ObjectPvsEntry;
|
---|
876 | typedef std::vector<ObjectPvsEntry> ObjectPvsEntries;
|
---|
877 | typedef Pvs<ViewCell *, MailablePvsData> ViewCellPvs;
|
---|
878 | typedef PvsIterator<Intersectable *, PvsData> ObjectPvsIterator;
|
---|
879 |
|
---|
880 |
|
---|
881 | class ObjectPvs: public Pvs<Intersectable *, PvsData>
|
---|
882 | {
|
---|
883 | public:
|
---|
884 | /** Counts object int the pvs. Different to method "GetSize", not
|
---|
885 | only the raw container size is returned,
|
---|
886 | but the individual contributions of the entries are summed up.
|
---|
887 | */
|
---|
888 | float EvalPvsCost() const;
|
---|
889 |
|
---|
890 | friend ostream &operator<<(ostream &s, const ObjectPvs &p)
|
---|
891 | {
|
---|
892 | ObjectPvsIterator pit = p.GetIterator();
|
---|
893 |
|
---|
894 | while (pit.HasMoreEntries())
|
---|
895 | {
|
---|
896 | const ObjectPvsEntry &entry = pit.Next();
|
---|
897 | Intersectable *obj = entry.mObject;
|
---|
898 |
|
---|
899 | cout << obj << " ";
|
---|
900 | }
|
---|
901 |
|
---|
902 | return s;
|
---|
903 | }
|
---|
904 | };
|
---|
905 |
|
---|
906 |
|
---|
907 |
|
---|
908 | }
|
---|
909 |
|
---|
910 | #endif
|
---|
911 |
|
---|