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