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