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 | /** Normalize the visibility of entries in order to get
|
---|
189 | comparable results.
|
---|
190 | */
|
---|
191 | void NormalizeMaximum();
|
---|
192 |
|
---|
193 | /** Merges pvs of a into this pvs.
|
---|
194 | Warning: very slow!
|
---|
195 | */
|
---|
196 | void MergeInPlace(const Pvs<T, S> &a);
|
---|
197 |
|
---|
198 | /** Difference of pvs to pvs b.
|
---|
199 | @returns number of different entries.
|
---|
200 | */
|
---|
201 | int Diff(const Pvs<T, S> &b);
|
---|
202 |
|
---|
203 | /** Finds sample in PVS.
|
---|
204 | @param checkDirty if dirty part of the pvs should be checked for entry
|
---|
205 | (warning: linear runtime in dirty part)
|
---|
206 | @returns iterator on the sample.
|
---|
207 | */
|
---|
208 | bool Find(T sample,
|
---|
209 | typename vector<PvsEntry<T, S> >::iterator &it,
|
---|
210 | const bool checkDirty = true);
|
---|
211 |
|
---|
212 | bool GetSampleContribution(T sample, const float pdf, float &contribution);
|
---|
213 |
|
---|
214 | /** Adds sample to PVS.
|
---|
215 | @returns contribution of sample (0 or 1)
|
---|
216 | */
|
---|
217 | float AddSample(T sample, const float pdf);
|
---|
218 |
|
---|
219 | /** Adds sample to PVS without checking for presence of the sample
|
---|
220 | pvs remains unsorted!
|
---|
221 | */
|
---|
222 | void AddSampleDirty(T sample, const float pdf);
|
---|
223 |
|
---|
224 | /** Adds sample dirty (on the end of the vector) but
|
---|
225 | first checks if sample is already in clean part of the pvs.
|
---|
226 | */
|
---|
227 | bool AddSampleDirtyCheck(T sample, const float pdf);//, float &contribution);
|
---|
228 |
|
---|
229 | /** Sort pvs entries - this should always be called after a
|
---|
230 | sequence of AddSampleDirty calls
|
---|
231 | */
|
---|
232 | void Sort();
|
---|
233 |
|
---|
234 | /** Adds sample to PVS. Assumes that the pvs is sorted
|
---|
235 | @returns contribution of sample (0 or 1)
|
---|
236 | */
|
---|
237 | //float AddSamples(const vector<PvsEntry<T, S> > &samples);
|
---|
238 |
|
---|
239 | /** Adds sample to PVS.
|
---|
240 | @returns PvsData
|
---|
241 | */
|
---|
242 | typename std::vector<PvsEntry<T, S> >::iterator AddSample2(T sample, const float pdf);
|
---|
243 |
|
---|
244 | /** Subtracts one pvs from another one.
|
---|
245 | WARNING: could contains bugs
|
---|
246 | @returns new pvs size
|
---|
247 | */
|
---|
248 | int SubtractPvs(const Pvs<T, S> &pvs);
|
---|
249 |
|
---|
250 | /** Returns PVS data, i.e., how often it was seen from the view cell,
|
---|
251 | and the object itsef.
|
---|
252 | */
|
---|
253 | void GetData(const int index, T &entry, S &data);
|
---|
254 |
|
---|
255 | /** Collects the PVS entries and returns them in the vector.
|
---|
256 | */
|
---|
257 | void CollectEntries(std::vector<T> &entries);
|
---|
258 |
|
---|
259 | /** Removes sample from PVS if reference count is zero.
|
---|
260 | @param visibleSamples number of references to be removed
|
---|
261 | */
|
---|
262 | bool RemoveSample(T sample, const float pdf);
|
---|
263 |
|
---|
264 | /** Compute continuous PVS difference
|
---|
265 | */
|
---|
266 | void ComputeContinuousPvsDifference(Pvs<T, S> &pvs,
|
---|
267 | float &pvsReduction,
|
---|
268 | float &pvsEnlargement);
|
---|
269 |
|
---|
270 | /** Clears the pvs.
|
---|
271 | */
|
---|
272 | void Clear(const bool trim = true);
|
---|
273 |
|
---|
274 | void Trim();
|
---|
275 |
|
---|
276 | static int GetEntrySizeByte();
|
---|
277 | static float GetEntrySize();
|
---|
278 |
|
---|
279 | /** Compute continuous PVS difference
|
---|
280 | */
|
---|
281 | float GetPvsHomogenity(Pvs<T, S> &pvs);
|
---|
282 |
|
---|
283 | static void Merge(Pvs<T, S> &mergedPvs, const Pvs<T, S> &a, const Pvs<T, S> &b);
|
---|
284 |
|
---|
285 | static void Merge(Pvs<T, S> &mergedPvs,
|
---|
286 | const typename std::vector<PvsEntry<T, S> >::const_iterator &aBegin,
|
---|
287 | const typename std::vector<PvsEntry<T, S> >::const_iterator &aEnd,
|
---|
288 | const typename std::vector<PvsEntry<T, S> >::const_iterator &bBegin,
|
---|
289 | const typename std::vector<PvsEntry<T, S> >::const_iterator &bEnd,
|
---|
290 | const int aSamples,
|
---|
291 | const int bSamples);
|
---|
292 |
|
---|
293 | int GetSamples() const
|
---|
294 | {
|
---|
295 | return mSamples;
|
---|
296 | }
|
---|
297 |
|
---|
298 |
|
---|
299 | bool IsDirty() const
|
---|
300 | {
|
---|
301 | return mLastSorted < mEntries.size();
|
---|
302 | }
|
---|
303 |
|
---|
304 | bool RequiresResort() const
|
---|
305 | {
|
---|
306 | // the last part should not be more than log of the sorted part. this
|
---|
307 | // way we can achieve logarithmic behaviour for insertion and find
|
---|
308 | const int dirtySize = (int)mEntries.size() - mLastSorted;
|
---|
309 | return dirtySize > (int)(log((double)mEntries.size()) / log(2.0));
|
---|
310 | }
|
---|
311 |
|
---|
312 |
|
---|
313 | int GetLastSorted() const
|
---|
314 | {
|
---|
315 | return mLastSorted;
|
---|
316 | }
|
---|
317 |
|
---|
318 | typename PvsIterator<T, S> GetIterator() const;
|
---|
319 |
|
---|
320 | protected:
|
---|
321 |
|
---|
322 | /// vector of PVS entries
|
---|
323 | vector<PvsEntry<T, S> > mEntries;
|
---|
324 |
|
---|
325 | /// Number of samples used to create the PVS
|
---|
326 | int mSamples;
|
---|
327 |
|
---|
328 | /// Last sorted entry in the pvs (important for find and merge)
|
---|
329 | int mLastSorted;
|
---|
330 | };
|
---|
331 |
|
---|
332 |
|
---|
333 | template <typename T, typename S>
|
---|
334 | Pvs<T, S>::Pvs(const vector<PvsEntry<T, S> > &samples)
|
---|
335 | {
|
---|
336 | mEntries.reserve(samples.size());
|
---|
337 | mEntries = samples;
|
---|
338 | mLastSorted = 0;
|
---|
339 | mSamples = samples.size();
|
---|
340 | }
|
---|
341 |
|
---|
342 |
|
---|
343 | template <typename T, typename S>
|
---|
344 | void Pvs<T, S>::Sort()
|
---|
345 | {
|
---|
346 | std::vector<PvsEntry<T, S> >::iterator it = mEntries.begin() + mLastSorted;
|
---|
347 | //std::vector<PvsEntry<T, S> >::const_iterator it = mEntries.begin() + mLastSorted;
|
---|
348 | std::vector<PvsEntry<T, S> >::iterator it_end = mEntries.end();
|
---|
349 |
|
---|
350 | // throw out double entries
|
---|
351 | std::vector<PvsEntry<T, S> >::iterator newEnd = unique(it, it_end);
|
---|
352 | sort(it, newEnd);
|
---|
353 | //sort(mEntries.begin(), mEntries.end());
|
---|
354 |
|
---|
355 | // now merge sorted ranges
|
---|
356 | ObjectPvs newPvs;
|
---|
357 | Merge(newPvs,
|
---|
358 | mEntries.begin(), it,
|
---|
359 | it, newEnd,
|
---|
360 | mSamples, 0);
|
---|
361 |
|
---|
362 | mEntries = newPvs.mEntries;
|
---|
363 | mLastSorted = (int)mEntries.size();
|
---|
364 | }
|
---|
365 |
|
---|
366 |
|
---|
367 | /**
|
---|
368 | Compute continuous PVS difference of 'b' with respect to the base PVS (*this).
|
---|
369 | Provides separatelly PVS reduction from PVS enlargement.
|
---|
370 |
|
---|
371 | */
|
---|
372 | template <typename T, typename S>
|
---|
373 | void
|
---|
374 | Pvs<T, S>::ComputeContinuousPvsDifference(Pvs<T, S> &b,
|
---|
375 | float &pvsReduction,
|
---|
376 | float &pvsEnlargement)
|
---|
377 | {
|
---|
378 | pvsReduction = 0.0f;
|
---|
379 | pvsEnlargement = 0.0f;
|
---|
380 |
|
---|
381 | // Uses sum of log differences, which corresponds to entropy
|
---|
382 | std::vector<PvsEntry<T, S> >::iterator it;
|
---|
383 |
|
---|
384 | for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
|
---|
385 | {
|
---|
386 | float bSumPdf = (*it).mData.mSumPdf;
|
---|
387 | float aSumPdf = 0.0f;
|
---|
388 |
|
---|
389 | vector<PvsEntry<T, S> >::iterator oit;
|
---|
390 | const bool entryFound = Find((*it).mObject, oit);
|
---|
391 |
|
---|
392 | if (entryFound)
|
---|
393 | {
|
---|
394 | aSumPdf = (*it).mData.mSumPdf;
|
---|
395 |
|
---|
396 | // mark this entry as processed to avoid double counting
|
---|
397 | (*it).mData.mSumPdf = -aSumPdf;
|
---|
398 | }
|
---|
399 |
|
---|
400 | #if 0
|
---|
401 | const float diff = bSumPdf - aSumPdf;
|
---|
402 |
|
---|
403 | if (diff > 0.0f) {
|
---|
404 | pvsEnlargement += diff;
|
---|
405 | } else {
|
---|
406 | pvsReduction += -diff;
|
---|
407 | }
|
---|
408 | #else
|
---|
409 | if (!entryFound)
|
---|
410 | pvsEnlargement += 1.0f;
|
---|
411 | #endif
|
---|
412 | }
|
---|
413 |
|
---|
414 | for (it = mEntries.begin(); it != mEntries.end(); ++ it)
|
---|
415 | {
|
---|
416 | float aSumPdf = (*it).mData.mSumPdf;
|
---|
417 | float bSumPdf = 0.0f;
|
---|
418 | if (aSumPdf < 0.0f) {
|
---|
419 |
|
---|
420 | // this entry was already accounted for!
|
---|
421 | // just revert it back
|
---|
422 | (*it).mData.mSumPdf = -aSumPdf;
|
---|
423 | } else {
|
---|
424 | vector<PvsEntry<T, S> >::iterator oit;
|
---|
425 |
|
---|
426 | const bool entryFound = b.Find((*it).mObject, oit);
|
---|
427 |
|
---|
428 | if (entryFound) {
|
---|
429 | bSumPdf = (*oit).mData.mSumPdf;
|
---|
430 | }
|
---|
431 | #if 0
|
---|
432 | const float diff = bSumPdf - aSumPdf;
|
---|
433 |
|
---|
434 | if (diff > 0.0f) {
|
---|
435 | pvsEnlargement += diff;
|
---|
436 | } else {
|
---|
437 | pvsReduction += -diff;
|
---|
438 | }
|
---|
439 |
|
---|
440 | #else
|
---|
441 | if (!entryFound)
|
---|
442 | pvsReduction += 1.0f;
|
---|
443 | #endif
|
---|
444 | }
|
---|
445 | }
|
---|
446 | }
|
---|
447 |
|
---|
448 |
|
---|
449 | template <typename T, typename S>
|
---|
450 | int Pvs<T, S>::Diff(const Pvs<T, S> &b)
|
---|
451 | {
|
---|
452 | int dif = 0;
|
---|
453 |
|
---|
454 | std::vector<PvsEntry<T, S> >::const_iterator it;
|
---|
455 |
|
---|
456 | for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
|
---|
457 | {
|
---|
458 | vector<PvsEntry<T, S> >::iterator bit;
|
---|
459 | const bool entryFound = Find((*it).first, bit);
|
---|
460 |
|
---|
461 | if (!entryFound) ++ dif;
|
---|
462 | }
|
---|
463 |
|
---|
464 | return dif;
|
---|
465 | }
|
---|
466 |
|
---|
467 |
|
---|
468 | template <typename T, typename S>
|
---|
469 | void Pvs<T, S>::MergeInPlace(const Pvs<T, S> &a)
|
---|
470 | {
|
---|
471 | // early exit
|
---|
472 | if (a.Empty())
|
---|
473 | {
|
---|
474 | return;
|
---|
475 | }
|
---|
476 | else if (Empty())
|
---|
477 | {
|
---|
478 | mEntries.reserve(a.GetSize());
|
---|
479 | mEntries = a.mEntries;
|
---|
480 | mSamples = a.mSamples;
|
---|
481 | return;
|
---|
482 | }
|
---|
483 |
|
---|
484 | ObjectPvs interPvs;
|
---|
485 |
|
---|
486 | Merge(interPvs, *this, a);
|
---|
487 |
|
---|
488 | mEntries.reserve(interPvs.GetSize());
|
---|
489 | mEntries = interPvs.mEntries;
|
---|
490 | mSamples = interPvs.mSamples;
|
---|
491 | }
|
---|
492 |
|
---|
493 |
|
---|
494 | template <typename T, typename S>
|
---|
495 | void Pvs<T, S>::Merge(Pvs<T, S> &mergedPvs, const Pvs<T, S> &a, const Pvs<T, S> &b)
|
---|
496 | {
|
---|
497 | std::vector<PvsEntry<T, S> >::const_iterator ait = a.mEntries.begin(), ait_end = a.mEntries.end();
|
---|
498 | std::vector<PvsEntry<T, S> >::const_iterator bit = b.mEntries.begin(), bit_end = b.mEntries.end();
|
---|
499 |
|
---|
500 | Merge(mergedPvs,
|
---|
501 | ait, ait_end,
|
---|
502 | bit, bit_end,
|
---|
503 | a.mSamples,
|
---|
504 | b.mSamples);
|
---|
505 | }
|
---|
506 |
|
---|
507 |
|
---|
508 | template <typename T, typename S>
|
---|
509 | void Pvs<T, S>::Merge(Pvs<T, S> &mergedPvs,
|
---|
510 | const typename std::vector<PvsEntry<T, S> >::const_iterator &aBegin,
|
---|
511 | const typename std::vector<PvsEntry<T, S> >::const_iterator &aEnd,
|
---|
512 | const typename std::vector<PvsEntry<T, S> >::const_iterator &bBegin,
|
---|
513 | const typename std::vector<PvsEntry<T, S> >::const_iterator &bEnd,
|
---|
514 | const int aSamples,
|
---|
515 | const int bSamples)
|
---|
516 | {
|
---|
517 | std::vector<PvsEntry<T, S> >::const_iterator ait = aBegin;
|
---|
518 | std::vector<PvsEntry<T, S> >::const_iterator bit = bBegin;
|
---|
519 |
|
---|
520 | for (; (ait != aEnd); ++ ait)
|
---|
521 | {
|
---|
522 | Intersectable *aObj = (*ait).mObject;
|
---|
523 | Intersectable *bObj = NULL;
|
---|
524 | //Intersectable *bObjOld = NULL;
|
---|
525 |
|
---|
526 | const PvsEntry<T, S> &aEntry = (*ait);
|
---|
527 |
|
---|
528 | for (; (bit != bEnd) && ((*bit).mObject <= (*ait).mObject); ++ bit)
|
---|
529 | {
|
---|
530 | bObj = (*bit).mObject;
|
---|
531 |
|
---|
532 | // object found => add up probabilities
|
---|
533 | if (bObj == aEntry.mObject)
|
---|
534 | {
|
---|
535 | PvsData newData(aEntry.mData.mSumPdf + (*bit).mData.mSumPdf);
|
---|
536 | PvsEntry<T, S> entry(bObj, newData);
|
---|
537 | mergedPvs.mEntries.push_back(entry);
|
---|
538 | }
|
---|
539 | else
|
---|
540 | {
|
---|
541 | mergedPvs.mEntries.push_back(*bit);
|
---|
542 | }
|
---|
543 |
|
---|
544 | //bObjOld = bObj;
|
---|
545 | }
|
---|
546 |
|
---|
547 | // only push back if objects different
|
---|
548 | // (equal case is handled by second loop)
|
---|
549 | if (aObj != bObj)
|
---|
550 | {
|
---|
551 | mergedPvs.mEntries.push_back(*ait);
|
---|
552 | }
|
---|
553 | }
|
---|
554 |
|
---|
555 | // add the rest
|
---|
556 | for (; (bit != bEnd); ++ bit)
|
---|
557 | {
|
---|
558 | mergedPvs.mEntries.push_back(*bit);
|
---|
559 | }
|
---|
560 |
|
---|
561 | mergedPvs.mSamples = aSamples + bSamples;
|
---|
562 | }
|
---|
563 |
|
---|
564 |
|
---|
565 | template <typename T, typename S> void Pvs<T, S>::Clear(const bool trim = true)
|
---|
566 | {
|
---|
567 | mEntries.clear();
|
---|
568 | mSamples = 0;
|
---|
569 | mLastSorted = 0;
|
---|
570 |
|
---|
571 | if (trim)
|
---|
572 | {
|
---|
573 | vector<PvsEntry<T,S> >().swap(mEntries);
|
---|
574 | }
|
---|
575 | }
|
---|
576 |
|
---|
577 |
|
---|
578 | template <typename T, typename S> void Pvs<T, S>::Trim()
|
---|
579 | {
|
---|
580 | vector<PvsEntry<T,S> >(mEntries).swap(mEntries);
|
---|
581 | }
|
---|
582 |
|
---|
583 |
|
---|
584 | template <typename T, typename S>
|
---|
585 | bool Pvs<T, S>::Find(T sample,
|
---|
586 | typename vector<PvsEntry<T, S> >::iterator &it,
|
---|
587 | const bool checkDirty)
|
---|
588 | {
|
---|
589 | bool found = false;
|
---|
590 |
|
---|
591 | PvsEntry<T, S> dummy(sample, PvsData());
|
---|
592 |
|
---|
593 | // only check clean part
|
---|
594 | // $$ TMP JB
|
---|
595 | // mLastSorted = 0;
|
---|
596 | vector<PvsEntry<T, S> >::iterator sorted_end = mEntries.begin() + mLastSorted;
|
---|
597 |
|
---|
598 | // binary search
|
---|
599 | it = lower_bound(mEntries.begin(), sorted_end, dummy);
|
---|
600 |
|
---|
601 | if ((it != mEntries.end()) && ((*it).mObject == sample))
|
---|
602 | found = true;
|
---|
603 |
|
---|
604 | // sample not found yet => search further in the unsorted part
|
---|
605 | if (!found && checkDirty)
|
---|
606 | {
|
---|
607 | vector<PvsEntry<T, S> >::const_iterator dit, dit_end = mEntries.end();
|
---|
608 |
|
---|
609 | for (dit = sorted_end; (dit != dit_end) && ((*dit).mObject != sample); ++ dit) ;
|
---|
610 |
|
---|
611 | if ((dit != mEntries.end()) && ((*dit).mObject == sample))
|
---|
612 | found = true;
|
---|
613 | }
|
---|
614 |
|
---|
615 | return found;
|
---|
616 | }
|
---|
617 |
|
---|
618 |
|
---|
619 | template <typename T, typename S>
|
---|
620 | void Pvs<T, S>::GetData(const int index, T &entry, S &data)
|
---|
621 | {
|
---|
622 | std::vector<PvsEntry<T, S> >::iterator i = mEntries.begin();
|
---|
623 | for (int k = 0; k != index && i != mEntries.end(); ++ i, ++ k);
|
---|
624 |
|
---|
625 | entry = (*i).first;
|
---|
626 | data = (*i).second;
|
---|
627 | }
|
---|
628 |
|
---|
629 |
|
---|
630 | template <typename T, typename S>
|
---|
631 | float Pvs<T, S>::AddSample(T sample, const float pdf)
|
---|
632 | {
|
---|
633 | ++ mSamples;
|
---|
634 |
|
---|
635 | vector<PvsEntry<T, S> >::iterator it;
|
---|
636 | const bool entryFound = Find(sample, it);
|
---|
637 |
|
---|
638 | if (entryFound)
|
---|
639 | {
|
---|
640 | S &data = (*it).mData;
|
---|
641 | data.mSumPdf += pdf;
|
---|
642 | return data.mSumPdf;
|
---|
643 | }
|
---|
644 | else
|
---|
645 | {
|
---|
646 | PvsEntry<T, S> entry(sample, pdf);
|
---|
647 | mEntries.insert(it, entry);
|
---|
648 | ++ mLastSorted;
|
---|
649 | return pdf;
|
---|
650 | }
|
---|
651 | }
|
---|
652 |
|
---|
653 |
|
---|
654 | template <typename T, typename S>
|
---|
655 | void Pvs<T, S>::AddSampleDirty(T sample, const float pdf)
|
---|
656 | {
|
---|
657 | ++ mSamples;
|
---|
658 | mEntries.push_back(PvsEntry<T, S>(sample, pdf));
|
---|
659 | }
|
---|
660 |
|
---|
661 |
|
---|
662 | template <typename T, typename S>
|
---|
663 | typename vector< PvsEntry<T, S> >::iterator Pvs<T, S>::AddSample2(T sample,
|
---|
664 | const float pdf)
|
---|
665 | {
|
---|
666 | ++ mSamples;
|
---|
667 |
|
---|
668 | vector<PvsEntry<T, S> >::iterator it;
|
---|
669 | const bool entryFound == Find(sample, it);
|
---|
670 |
|
---|
671 | if (entryFound)
|
---|
672 | {
|
---|
673 | S &data = (*it).second;
|
---|
674 | data->mSumPdf += pdf;
|
---|
675 | }
|
---|
676 | else
|
---|
677 | {
|
---|
678 | PvsEntry<T, S> entry(sample, pdf);
|
---|
679 | mEntries.insert(it, entry);
|
---|
680 | ++ mLastSorted;
|
---|
681 | }
|
---|
682 |
|
---|
683 | return it;
|
---|
684 | }
|
---|
685 |
|
---|
686 |
|
---|
687 | /** Adds sample dirty (on the end of the vector) but
|
---|
688 | first checks if sample is already in clean part of the pvs.
|
---|
689 | */
|
---|
690 | template <typename T, typename S>
|
---|
691 | bool Pvs<T, S>::AddSampleDirtyCheck(T sample,
|
---|
692 | const float pdf)
|
---|
693 | //,float &contribution)
|
---|
694 | {
|
---|
695 | ++ mSamples;
|
---|
696 |
|
---|
697 | vector<PvsEntry<T, S> >::iterator it;
|
---|
698 | const bool entryFound = Find(sample, it);
|
---|
699 |
|
---|
700 | if (entryFound)
|
---|
701 | {
|
---|
702 | S &data = (*it).mData;
|
---|
703 |
|
---|
704 | data.mSumPdf += pdf;
|
---|
705 | //contribution = pdf / data.mSumPdf;
|
---|
706 |
|
---|
707 | return false;
|
---|
708 | }
|
---|
709 | else
|
---|
710 | {
|
---|
711 | AddSampleDirty(sample, pdf);
|
---|
712 | //contribution = 1.0f;
|
---|
713 |
|
---|
714 | return true;
|
---|
715 | }
|
---|
716 | }
|
---|
717 |
|
---|
718 |
|
---|
719 | template <typename T, typename S>
|
---|
720 | bool Pvs<T, S>::GetSampleContribution(T sample,
|
---|
721 | const float pdf,
|
---|
722 | float &contribution)
|
---|
723 | {
|
---|
724 | vector<PvsEntry<T, S> >::iterator it;
|
---|
725 | const bool entryFound = Find(sample, it);
|
---|
726 |
|
---|
727 | if (entryFound)
|
---|
728 | {
|
---|
729 | S &data = (*it).mData;
|
---|
730 | contribution = pdf / (data.mSumPdf + pdf);
|
---|
731 | return false;
|
---|
732 | }
|
---|
733 | else
|
---|
734 | {
|
---|
735 | contribution = 1.0f;
|
---|
736 | return true;
|
---|
737 | }
|
---|
738 | }
|
---|
739 |
|
---|
740 |
|
---|
741 | template <typename T, typename S>
|
---|
742 | bool Pvs<T, S>::RemoveSample(T sample, const float pdf)
|
---|
743 | {
|
---|
744 | -- mSamples;
|
---|
745 |
|
---|
746 | vector<PvsEntry<T, S> >::iterator it;
|
---|
747 | const bool entryFound = Find(sample, it);
|
---|
748 |
|
---|
749 | if (!entryFound)
|
---|
750 | return false;
|
---|
751 |
|
---|
752 | S &data = (*it).mData;
|
---|
753 |
|
---|
754 | data.mSumPdf -= pdf;
|
---|
755 |
|
---|
756 | if (data.mSumPdf <= 0.0f)
|
---|
757 | {
|
---|
758 | mEntries.erase(it);
|
---|
759 | -- mLastSorted; // wrong if sample was in tail!!
|
---|
760 | }
|
---|
761 |
|
---|
762 | return true;
|
---|
763 | }
|
---|
764 |
|
---|
765 |
|
---|
766 | template <typename T, typename S>
|
---|
767 | int Pvs<T, S>::SubtractPvs(const Pvs<T, S> &pvs)
|
---|
768 | {
|
---|
769 | const int samples = mSamples - pvs.mSamples;
|
---|
770 |
|
---|
771 | std::vector<PvsEntry<T, S> >::
|
---|
772 | const_iterator it, it_end = pvs.mEntries.end();
|
---|
773 |
|
---|
774 | // output PVS of view cell
|
---|
775 | for (it = pvs.mEntries.begin(); it != it_end; ++ it)
|
---|
776 | RemoveSample((*it).mObject, (*it).mData.mSumPdf);
|
---|
777 |
|
---|
778 | mSamples = samples;
|
---|
779 |
|
---|
780 | return GetSize();
|
---|
781 | }
|
---|
782 |
|
---|
783 |
|
---|
784 | template <typename T, typename S>
|
---|
785 | void Pvs<T, S>::CollectEntries(std::vector<T> &entries)
|
---|
786 | {
|
---|
787 | std::vector<PvsEntry<T, S> >::
|
---|
788 | const_iterator it, it_end = mEntries.end();
|
---|
789 |
|
---|
790 | // output PVS of view cell
|
---|
791 | for (it = mEntries.begin(); it != it_end; ++ it)
|
---|
792 | entries.push_back((*it)->first);
|
---|
793 | }
|
---|
794 |
|
---|
795 |
|
---|
796 | template <typename T, typename S>
|
---|
797 | void Pvs<T, S>::NormalizeMaximum()
|
---|
798 | {
|
---|
799 | std::vector<PvsEntry<T, S> >::
|
---|
800 | const_iterator it, it_end = mEntries.end();
|
---|
801 |
|
---|
802 | float maxPdfSum = -1.0f;
|
---|
803 |
|
---|
804 | // output PVS of view cell
|
---|
805 | for (it = mEntries.begin(); it != it_end; ++ it) {
|
---|
806 | float sum = (*it)->second.sumPdf;
|
---|
807 | if (sum > maxSum)
|
---|
808 | maxSum = sum;
|
---|
809 | }
|
---|
810 |
|
---|
811 | maxSum = 1.0f / maxSum;
|
---|
812 |
|
---|
813 | for (it = mEntries.begin(); it != it_end; ++ it) {
|
---|
814 | (*it)->second.sumPdf *= maxSum;
|
---|
815 | }
|
---|
816 |
|
---|
817 | }
|
---|
818 |
|
---|
819 |
|
---|
820 | template <typename T, typename S>
|
---|
821 | float Pvs<T, S>::GetEntrySize()
|
---|
822 | {
|
---|
823 | return (float)(sizeof(T) + sizeof(S)) / float(1024 * 1024);
|
---|
824 | }
|
---|
825 |
|
---|
826 |
|
---|
827 | template <typename T, typename S>
|
---|
828 | int Pvs<T, S>::GetEntrySizeByte()
|
---|
829 | {
|
---|
830 | return sizeof(T) + sizeof(S);
|
---|
831 | }
|
---|
832 |
|
---|
833 |
|
---|
834 | template <typename T, typename S>
|
---|
835 | float Pvs<T, S>::GetPvsHomogenity(Pvs<T, S> &pvs)
|
---|
836 | {
|
---|
837 | float pvsReduction, pvsEnlargement;
|
---|
838 |
|
---|
839 | ComputeContinuousPvsDifference(pvs, pvsReduction, pvsEnlargement);
|
---|
840 |
|
---|
841 | return pvsReduction + pvsEnlargement;
|
---|
842 | }
|
---|
843 |
|
---|
844 |
|
---|
845 | template <typename T, typename S>
|
---|
846 | typename PvsIterator<T, S> Pvs<T, S>::GetIterator() const
|
---|
847 | {
|
---|
848 | PvsIterator<T, S> pit(mEntries.begin(), mEntries.end());
|
---|
849 |
|
---|
850 | return pit;
|
---|
851 | }
|
---|
852 |
|
---|
853 |
|
---|
854 | ///////////////////////////////////////
|
---|
855 |
|
---|
856 | /** Class instantiating the Pvs template for kd tree nodes.
|
---|
857 | */
|
---|
858 | class KdPvs: public Pvs<KdNode *, PvsData>
|
---|
859 | {
|
---|
860 | public:
|
---|
861 | int Compress();
|
---|
862 | };
|
---|
863 |
|
---|
864 |
|
---|
865 | ////////////
|
---|
866 | //-- typedefs
|
---|
867 |
|
---|
868 | typedef PvsEntry<Intersectable *, PvsData> ObjectPvsEntry;
|
---|
869 | typedef std::vector<ObjectPvsEntry> ObjectPvsEntries;
|
---|
870 | typedef Pvs<ViewCell *, MailablePvsData> ViewCellPvs;
|
---|
871 | typedef PvsIterator<Intersectable *, PvsData> ObjectPvsIterator;
|
---|
872 |
|
---|
873 |
|
---|
874 | class ObjectPvs: public Pvs<Intersectable *, PvsData>
|
---|
875 | {
|
---|
876 | public:
|
---|
877 | /** Counts object int the pvs. Different to method "GetSize", not
|
---|
878 | only the raw container size is returned,
|
---|
879 | but the individual contributions of the entries are summed up.
|
---|
880 | */
|
---|
881 | float EvalPvsCost() const;
|
---|
882 |
|
---|
883 | friend ostream &operator<<(ostream &s, const ObjectPvs &p)
|
---|
884 | {
|
---|
885 | ObjectPvsIterator pit = p.GetIterator();
|
---|
886 |
|
---|
887 | while (pit.HasMoreEntries())
|
---|
888 | {
|
---|
889 | const ObjectPvsEntry &entry = pit.Next();
|
---|
890 | Intersectable *obj = entry.mObject;
|
---|
891 |
|
---|
892 | cout << obj << " ";
|
---|
893 | }
|
---|
894 |
|
---|
895 | return s;
|
---|
896 | }
|
---|
897 |
|
---|
898 | };
|
---|
899 |
|
---|
900 |
|
---|
901 |
|
---|
902 | }
|
---|
903 |
|
---|
904 | #endif
|
---|
905 |
|
---|