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 | /** Normalize the visibility of entries in order to get
|
---|
190 | comparable results.
|
---|
191 | */
|
---|
192 | void NormalizeMaximum();
|
---|
193 |
|
---|
194 | /** Merges pvs of a into this pvs.
|
---|
195 | Warning: very slow!
|
---|
196 | */
|
---|
197 | void MergeInPlace(const Pvs<T, S> &a);
|
---|
198 |
|
---|
199 | /** Difference of pvs to pvs b.
|
---|
200 | @returns number of different entries.
|
---|
201 | */
|
---|
202 | int Diff(const Pvs<T, S> &b);
|
---|
203 |
|
---|
204 | /** Finds sample in PVS.
|
---|
205 | @param checkDirty if dirty part of the pvs should be checked for entry
|
---|
206 | (warning: linear runtime in dirty part)
|
---|
207 | @returns iterator on the sample.
|
---|
208 | */
|
---|
209 | bool Find(T sample,
|
---|
210 | typename vector<PvsEntry<T, S> >::iterator &it,
|
---|
211 | const bool checkDirty = true);
|
---|
212 |
|
---|
213 | bool GetSampleContribution(T sample, const float pdf, float &contribution);
|
---|
214 |
|
---|
215 | /** Adds sample to PVS.
|
---|
216 | @returns contribution of sample (0 or 1)
|
---|
217 | */
|
---|
218 | float AddSample(T sample, const float pdf);
|
---|
219 |
|
---|
220 | /** Adds sample to PVS without checking for presence of the sample
|
---|
221 | pvs remains unsorted!
|
---|
222 | */
|
---|
223 | void AddSampleDirty(T sample, const float pdf);
|
---|
224 |
|
---|
225 | /** Adds sample dirty (on the end of the vector) but
|
---|
226 | first checks if sample is already in clean part of the pvs.
|
---|
227 | */
|
---|
228 | bool AddSampleDirtyCheck(T sample, const float pdf);//, float &contribution);
|
---|
229 |
|
---|
230 | /** Sort pvs entries - this should always be called after a
|
---|
231 | sequence of AddSampleDirty calls
|
---|
232 | */
|
---|
233 | void Sort();
|
---|
234 |
|
---|
235 | /** Sort pvs entries assume that the pvs contains unique entries
|
---|
236 | */
|
---|
237 | void SimpleSort();
|
---|
238 |
|
---|
239 | /** Adds sample to PVS. Assumes that the pvs is sorted
|
---|
240 | @returns contribution of sample (0 or 1)
|
---|
241 | */
|
---|
242 | //float AddSamples(const vector<PvsEntry<T, S> > &samples);
|
---|
243 |
|
---|
244 | /** Adds sample to PVS.
|
---|
245 | @returns PvsData
|
---|
246 | */
|
---|
247 | typename std::vector<PvsEntry<T, S> >::iterator AddSample2(T sample, const float pdf);
|
---|
248 |
|
---|
249 | /** Subtracts one pvs from another one.
|
---|
250 | WARNING: could contains bugs
|
---|
251 | @returns new pvs size
|
---|
252 | */
|
---|
253 | int SubtractPvs(const Pvs<T, S> &pvs);
|
---|
254 |
|
---|
255 | /** Returns PVS data, i.e., how often it was seen from the view cell,
|
---|
256 | and the object itsef.
|
---|
257 | */
|
---|
258 | void GetData(const int index, T &entry, S &data);
|
---|
259 |
|
---|
260 | /** Collects the PVS entries and returns them in the vector.
|
---|
261 | */
|
---|
262 | void CollectEntries(std::vector<T> &entries);
|
---|
263 |
|
---|
264 | /** Removes sample from PVS if reference count is zero.
|
---|
265 | @param visibleSamples number of references to be removed
|
---|
266 | */
|
---|
267 | bool RemoveSample(T sample, const float pdf);
|
---|
268 |
|
---|
269 | /** Compute continuous PVS difference
|
---|
270 | */
|
---|
271 | void ComputeContinuousPvsDifference(Pvs<T, S> &pvs,
|
---|
272 | float &pvsReduction,
|
---|
273 | float &pvsEnlargement);
|
---|
274 |
|
---|
275 | /** Clears the pvs.
|
---|
276 | */
|
---|
277 | void Clear(const bool trim = true);
|
---|
278 |
|
---|
279 | void Trim();
|
---|
280 |
|
---|
281 | static int GetEntrySizeByte();
|
---|
282 | static float GetEntrySize();
|
---|
283 |
|
---|
284 | /** Compute continuous PVS difference
|
---|
285 | */
|
---|
286 | float GetPvsHomogenity(Pvs<T, S> &pvs);
|
---|
287 |
|
---|
288 | static void Merge(Pvs<T, S> &mergedPvs, const Pvs<T, S> &a, const Pvs<T, S> &b);
|
---|
289 |
|
---|
290 | static void Merge(Pvs<T, S> &mergedPvs,
|
---|
291 | const typename std::vector<PvsEntry<T, S> >::const_iterator &aBegin,
|
---|
292 | const typename std::vector<PvsEntry<T, S> >::const_iterator &aEnd,
|
---|
293 | const typename std::vector<PvsEntry<T, S> >::const_iterator &bBegin,
|
---|
294 | const typename std::vector<PvsEntry<T, S> >::const_iterator &bEnd,
|
---|
295 | const int aSamples,
|
---|
296 | const int bSamples);
|
---|
297 |
|
---|
298 | int GetSamples() const
|
---|
299 | {
|
---|
300 | return mSamples;
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | bool IsDirty() const
|
---|
305 | {
|
---|
306 | return mLastSorted < mEntries.size();
|
---|
307 | }
|
---|
308 |
|
---|
309 | bool RequiresResort() const
|
---|
310 | {
|
---|
311 | // the last part should not be more than log of the sorted part. this
|
---|
312 | // way we can achieve logarithmic behaviour for insertion and find
|
---|
313 | const int dirtySize = (int)mEntries.size() - mLastSorted;
|
---|
314 | return dirtySize > 4*(int)(log((double)mEntries.size()) / log(2.0));
|
---|
315 | }
|
---|
316 |
|
---|
317 |
|
---|
318 | int GetLastSorted() const
|
---|
319 | {
|
---|
320 | return mLastSorted;
|
---|
321 | }
|
---|
322 |
|
---|
323 | typename PvsIterator<T, S> GetIterator() const;
|
---|
324 |
|
---|
325 | protected:
|
---|
326 |
|
---|
327 | /// vector of PVS entries
|
---|
328 | vector<PvsEntry<T, S> > mEntries;
|
---|
329 |
|
---|
330 | /// Number of samples used to create the PVS
|
---|
331 | int mSamples;
|
---|
332 |
|
---|
333 | /// Last sorted entry in the pvs (important for find and merge)
|
---|
334 | int mLastSorted;
|
---|
335 | };
|
---|
336 |
|
---|
337 |
|
---|
338 | template <typename T, typename S>
|
---|
339 | Pvs<T, S>::Pvs(const vector<PvsEntry<T, S> > &samples)
|
---|
340 | {
|
---|
341 | mEntries.reserve(samples.size());
|
---|
342 | mEntries = samples;
|
---|
343 | mLastSorted = 0;
|
---|
344 | mSamples = samples.size();
|
---|
345 | }
|
---|
346 |
|
---|
347 |
|
---|
348 | template <typename T, typename S>
|
---|
349 | void Pvs<T, S>::Sort()
|
---|
350 | {
|
---|
351 | std::vector<PvsEntry<T, S> >::iterator it = mEntries.begin() + mLastSorted;
|
---|
352 | //std::vector<PvsEntry<T, S> >::const_iterator it = mEntries.begin() + mLastSorted;
|
---|
353 | std::vector<PvsEntry<T, S> >::iterator it_end = mEntries.end();
|
---|
354 |
|
---|
355 | // throw out double entries
|
---|
356 | std::vector<PvsEntry<T, S> >::iterator newEnd = unique(it, it_end);
|
---|
357 | sort(it, newEnd);
|
---|
358 | //sort(mEntries.begin(), mEntries.end());
|
---|
359 |
|
---|
360 | // now merge sorted ranges
|
---|
361 | ObjectPvs newPvs;
|
---|
362 | Merge(newPvs,
|
---|
363 | mEntries.begin(), it,
|
---|
364 | it, newEnd,
|
---|
365 | mSamples, 0);
|
---|
366 |
|
---|
367 | mEntries = newPvs.mEntries;
|
---|
368 | mLastSorted = (int)mEntries.size();
|
---|
369 | }
|
---|
370 |
|
---|
371 | template <typename T, typename S>
|
---|
372 | void Pvs<T, S>::SimpleSort()
|
---|
373 | {
|
---|
374 | sort(mEntries.begin(), mEntries.end());
|
---|
375 | mLastSorted = (int)mEntries.size();
|
---|
376 | }
|
---|
377 |
|
---|
378 |
|
---|
379 | /**
|
---|
380 | Compute continuous PVS difference of 'b' with respect to the base PVS (*this).
|
---|
381 | Provides separatelly PVS reduction from PVS enlargement.
|
---|
382 |
|
---|
383 | */
|
---|
384 | template <typename T, typename S>
|
---|
385 | void
|
---|
386 | Pvs<T, S>::ComputeContinuousPvsDifference(Pvs<T, S> &b,
|
---|
387 | float &pvsReduction,
|
---|
388 | float &pvsEnlargement)
|
---|
389 | {
|
---|
390 | pvsReduction = 0.0f;
|
---|
391 | pvsEnlargement = 0.0f;
|
---|
392 |
|
---|
393 | // Uses sum of log differences, which corresponds to entropy
|
---|
394 | std::vector<PvsEntry<T, S> >::iterator it;
|
---|
395 |
|
---|
396 | for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
|
---|
397 | {
|
---|
398 | float bSumPdf = (*it).mData.mSumPdf;
|
---|
399 | float aSumPdf = 0.0f;
|
---|
400 |
|
---|
401 | vector<PvsEntry<T, S> >::iterator oit;
|
---|
402 | const bool entryFound = Find((*it).mObject, oit);
|
---|
403 |
|
---|
404 | if (entryFound)
|
---|
405 | {
|
---|
406 | aSumPdf = (*it).mData.mSumPdf;
|
---|
407 |
|
---|
408 | // mark this entry as processed to avoid double counting
|
---|
409 | (*it).mData.mSumPdf = -aSumPdf;
|
---|
410 | }
|
---|
411 |
|
---|
412 | #if 0
|
---|
413 | const float diff = bSumPdf - aSumPdf;
|
---|
414 |
|
---|
415 | if (diff > 0.0f) {
|
---|
416 | pvsEnlargement += diff;
|
---|
417 | } else {
|
---|
418 | pvsReduction += -diff;
|
---|
419 | }
|
---|
420 | #else
|
---|
421 | if (!entryFound)
|
---|
422 | pvsEnlargement += 1.0f;
|
---|
423 | #endif
|
---|
424 | }
|
---|
425 |
|
---|
426 | for (it = mEntries.begin(); it != mEntries.end(); ++ it)
|
---|
427 | {
|
---|
428 | float aSumPdf = (*it).mData.mSumPdf;
|
---|
429 | float bSumPdf = 0.0f;
|
---|
430 | if (aSumPdf < 0.0f) {
|
---|
431 |
|
---|
432 | // this entry was already accounted for!
|
---|
433 | // just revert it back
|
---|
434 | (*it).mData.mSumPdf = -aSumPdf;
|
---|
435 | } else {
|
---|
436 | vector<PvsEntry<T, S> >::iterator oit;
|
---|
437 |
|
---|
438 | const bool entryFound = b.Find((*it).mObject, oit);
|
---|
439 |
|
---|
440 | if (entryFound) {
|
---|
441 | bSumPdf = (*oit).mData.mSumPdf;
|
---|
442 | }
|
---|
443 | #if 0
|
---|
444 | const float diff = bSumPdf - aSumPdf;
|
---|
445 |
|
---|
446 | if (diff > 0.0f) {
|
---|
447 | pvsEnlargement += diff;
|
---|
448 | } else {
|
---|
449 | pvsReduction += -diff;
|
---|
450 | }
|
---|
451 |
|
---|
452 | #else
|
---|
453 | if (!entryFound)
|
---|
454 | pvsReduction += 1.0f;
|
---|
455 | #endif
|
---|
456 | }
|
---|
457 | }
|
---|
458 | }
|
---|
459 |
|
---|
460 |
|
---|
461 | template <typename T, typename S>
|
---|
462 | int Pvs<T, S>::Diff(const Pvs<T, S> &b)
|
---|
463 | {
|
---|
464 | int dif = 0;
|
---|
465 |
|
---|
466 | std::vector<PvsEntry<T, S> >::const_iterator it;
|
---|
467 |
|
---|
468 | for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
|
---|
469 | {
|
---|
470 | vector<PvsEntry<T, S> >::iterator bit;
|
---|
471 | const bool entryFound = Find((*it).first, bit);
|
---|
472 |
|
---|
473 | if (!entryFound) ++ dif;
|
---|
474 | }
|
---|
475 |
|
---|
476 | return dif;
|
---|
477 | }
|
---|
478 |
|
---|
479 |
|
---|
480 | template <typename T, typename S>
|
---|
481 | void Pvs<T, S>::MergeInPlace(const Pvs<T, S> &a)
|
---|
482 | {
|
---|
483 | // early exit
|
---|
484 | if (a.Empty())
|
---|
485 | {
|
---|
486 | return;
|
---|
487 | }
|
---|
488 | else if (Empty())
|
---|
489 | {
|
---|
490 | mEntries.reserve(a.GetSize());
|
---|
491 | mEntries = a.mEntries;
|
---|
492 | mSamples = a.mSamples;
|
---|
493 | return;
|
---|
494 | }
|
---|
495 |
|
---|
496 | ObjectPvs interPvs;
|
---|
497 |
|
---|
498 | Merge(interPvs, *this, a);
|
---|
499 |
|
---|
500 | mEntries.reserve(interPvs.GetSize());
|
---|
501 | mEntries = interPvs.mEntries;
|
---|
502 | mSamples = interPvs.mSamples;
|
---|
503 | }
|
---|
504 |
|
---|
505 |
|
---|
506 | template <typename T, typename S>
|
---|
507 | void Pvs<T, S>::Merge(Pvs<T, S> &mergedPvs, const Pvs<T, S> &a, const Pvs<T, S> &b)
|
---|
508 | {
|
---|
509 | std::vector<PvsEntry<T, S> >::const_iterator ait = a.mEntries.begin(), ait_end = a.mEntries.end();
|
---|
510 | std::vector<PvsEntry<T, S> >::const_iterator bit = b.mEntries.begin(), bit_end = b.mEntries.end();
|
---|
511 |
|
---|
512 | Merge(mergedPvs,
|
---|
513 | ait, ait_end,
|
---|
514 | bit, bit_end,
|
---|
515 | a.mSamples,
|
---|
516 | b.mSamples);
|
---|
517 | }
|
---|
518 |
|
---|
519 |
|
---|
520 | template <typename T, typename S>
|
---|
521 | void Pvs<T, S>::Merge(Pvs<T, S> &mergedPvs,
|
---|
522 | const typename std::vector<PvsEntry<T, S> >::const_iterator &aBegin,
|
---|
523 | const typename std::vector<PvsEntry<T, S> >::const_iterator &aEnd,
|
---|
524 | const typename std::vector<PvsEntry<T, S> >::const_iterator &bBegin,
|
---|
525 | const typename std::vector<PvsEntry<T, S> >::const_iterator &bEnd,
|
---|
526 | const int aSamples,
|
---|
527 | const int bSamples)
|
---|
528 | {
|
---|
529 | std::vector<PvsEntry<T, S> >::const_iterator ait = aBegin;
|
---|
530 | std::vector<PvsEntry<T, S> >::const_iterator bit = bBegin;
|
---|
531 |
|
---|
532 | for (; (ait != aEnd); ++ ait)
|
---|
533 | {
|
---|
534 | Intersectable *aObj = (*ait).mObject;
|
---|
535 | Intersectable *bObj = NULL;
|
---|
536 | //Intersectable *bObjOld = NULL;
|
---|
537 |
|
---|
538 | const PvsEntry<T, S> &aEntry = (*ait);
|
---|
539 |
|
---|
540 | for (; (bit != bEnd) && ((*bit).mObject <= (*ait).mObject); ++ bit)
|
---|
541 | {
|
---|
542 | bObj = (*bit).mObject;
|
---|
543 |
|
---|
544 | // object found => add up probabilities
|
---|
545 | if (bObj == aEntry.mObject)
|
---|
546 | {
|
---|
547 | PvsData newData(aEntry.mData.mSumPdf + (*bit).mData.mSumPdf);
|
---|
548 | PvsEntry<T, S> entry(bObj, newData);
|
---|
549 | mergedPvs.mEntries.push_back(entry);
|
---|
550 | }
|
---|
551 | else
|
---|
552 | {
|
---|
553 | mergedPvs.mEntries.push_back(*bit);
|
---|
554 | }
|
---|
555 |
|
---|
556 | //bObjOld = bObj;
|
---|
557 | }
|
---|
558 |
|
---|
559 | // only push back if objects different
|
---|
560 | // (equal case is handled by second loop)
|
---|
561 | if (aObj != bObj)
|
---|
562 | {
|
---|
563 | mergedPvs.mEntries.push_back(*ait);
|
---|
564 | }
|
---|
565 | }
|
---|
566 |
|
---|
567 | // add the rest
|
---|
568 | for (; (bit != bEnd); ++ bit)
|
---|
569 | {
|
---|
570 | mergedPvs.mEntries.push_back(*bit);
|
---|
571 | }
|
---|
572 |
|
---|
573 | mergedPvs.mSamples = aSamples + bSamples;
|
---|
574 | }
|
---|
575 |
|
---|
576 |
|
---|
577 | template <typename T, typename S> void Pvs<T, S>::Clear(const bool trim = true)
|
---|
578 | {
|
---|
579 | mEntries.clear();
|
---|
580 | mSamples = 0;
|
---|
581 | mLastSorted = 0;
|
---|
582 |
|
---|
583 | if (trim)
|
---|
584 | {
|
---|
585 | vector<PvsEntry<T,S> >().swap(mEntries);
|
---|
586 | }
|
---|
587 | }
|
---|
588 |
|
---|
589 |
|
---|
590 | template <typename T, typename S> void Pvs<T, S>::Trim()
|
---|
591 | {
|
---|
592 | vector<PvsEntry<T,S> >(mEntries).swap(mEntries);
|
---|
593 | }
|
---|
594 |
|
---|
595 |
|
---|
596 | template <typename T, typename S>
|
---|
597 | bool Pvs<T, S>::Find(T sample,
|
---|
598 | typename vector<PvsEntry<T, S> >::iterator &it,
|
---|
599 | const bool checkDirty)
|
---|
600 | {
|
---|
601 | bool found = false;
|
---|
602 |
|
---|
603 | PvsEntry<T, S> dummy(sample, PvsData());
|
---|
604 |
|
---|
605 | // only check clean part
|
---|
606 | vector<PvsEntry<T, S> >::iterator sorted_end = mEntries.begin() + mLastSorted;
|
---|
607 |
|
---|
608 | // binary search
|
---|
609 | it = lower_bound(mEntries.begin(), sorted_end, dummy);
|
---|
610 |
|
---|
611 | if ((it != mEntries.end()) && ((*it).mObject == sample))
|
---|
612 | found = true;
|
---|
613 |
|
---|
614 | // sample not found yet => search further in the unsorted part
|
---|
615 | if (!found && checkDirty) {
|
---|
616 | vector<PvsEntry<T, S> >::const_iterator dit, dit_end = mEntries.end();
|
---|
617 |
|
---|
618 | for (dit = sorted_end; (dit != dit_end) && ((*dit).mObject != sample); ++ dit) ;
|
---|
619 |
|
---|
620 | if (dit != dit_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 | const 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 |
|
---|
910 | #endif
|
---|
911 |
|
---|