[177] | 1 | #ifndef __PVS_H
|
---|
| 2 | #define __PVS_H
|
---|
| 3 |
|
---|
| 4 | #include <map>
|
---|
[469] | 5 | #include <vector>
|
---|
[177] | 6 |
|
---|
| 7 | class KdNode;
|
---|
[240] | 8 | class BspNode;
|
---|
[191] | 9 | class Ray;
|
---|
[308] | 10 | class Intersectable;
|
---|
[406] | 11 | class ViewCellKdNode;
|
---|
[177] | 12 |
|
---|
[310] | 13 | template<typename T>
|
---|
| 14 | struct LtSample {
|
---|
[311] | 15 | bool operator()(const T a, const T b) const
|
---|
[310] | 16 | {
|
---|
| 17 | return a < b;
|
---|
| 18 | }
|
---|
| 19 | };
|
---|
| 20 |
|
---|
[469] | 21 | /** Information stored with a PVS entry. Consists of the number
|
---|
| 22 | the object was seen from the view cell.
|
---|
| 23 | */
|
---|
[310] | 24 |
|
---|
| 25 | template<typename T>
|
---|
| 26 | struct PvsData {
|
---|
[556] | 27 | // int mVisibleSamples;
|
---|
| 28 | // sum of probability density of visible sample rays
|
---|
| 29 | float mSumPdf;
|
---|
[310] | 30 | PvsData<T>() {}
|
---|
[556] | 31 | PvsData<T>(const float sumPdf):
|
---|
| 32 | mSumPdf(sumPdf) {}
|
---|
| 33 |
|
---|
| 34 | // $$JB in order to return meaningfull values
|
---|
| 35 | // it assumes that the sum pdf has been normalized somehow!!!
|
---|
| 36 | float GetVisibility() { return mSumPdf; }
|
---|
[310] | 37 | };
|
---|
| 38 |
|
---|
[469] | 39 | /** Template class representing the Potentially Visible Set (PVS)
|
---|
| 40 | mainly from a view cell, but also e.g., from objects.
|
---|
| 41 | */
|
---|
[310] | 42 | template<typename T>
|
---|
[311] | 43 | class Pvs
|
---|
[310] | 44 | {
|
---|
| 45 | public:
|
---|
[469] | 46 | Pvs(): /*mSamples(0), */mEntries() {}
|
---|
[466] | 47 |
|
---|
[469] | 48 | //int mSamples;
|
---|
[466] | 49 |
|
---|
[469] | 50 | /** Compresses PVS lossless or lossy.
|
---|
| 51 | */
|
---|
[466] | 52 | int Compress() {return 0;}
|
---|
| 53 | int GetSize() const {return (int)mEntries.size();}
|
---|
[469] | 54 | bool Empty() const {return mEntries.empty();}
|
---|
[556] | 55 |
|
---|
| 56 | /** Normalize the visibility of entries in order to get comparable
|
---|
| 57 | results */
|
---|
[469] | 58 |
|
---|
[556] | 59 | void NormalizeMaximum();
|
---|
| 60 |
|
---|
[466] | 61 | /** Merges pvs of a and pvs of b into this pvs.
|
---|
| 62 | */
|
---|
| 63 | void Merge(const Pvs<T> &a, const Pvs<T> &b);
|
---|
[469] | 64 |
|
---|
[466] | 65 | /** Difference of pvs to pvs b.
|
---|
| 66 | @returns number of different entries.
|
---|
| 67 | */
|
---|
| 68 | int Diff(const Pvs<T> &b);
|
---|
| 69 |
|
---|
[469] | 70 | /** Finds sample in PVS.
|
---|
| 71 | @returns sample if found, NULL otherwise.
|
---|
| 72 | */
|
---|
[466] | 73 | PvsData<T> *Find(T sample);
|
---|
[492] | 74 |
|
---|
[556] | 75 | bool GetSampleContribution(T sample, const float pdf, float &contribution);
|
---|
[469] | 76 |
|
---|
| 77 | /** Adds sample to PVS.
|
---|
| 78 | @contribution contribution of sample (0 or 1)
|
---|
| 79 | @returns true if sample was not already in PVS.
|
---|
| 80 | */
|
---|
[556] | 81 | bool AddSample(T sample, const float pdf, float &contribution);
|
---|
[469] | 82 |
|
---|
| 83 | /** Adds sample to PVS.
|
---|
| 84 | @returns contribution of sample (0 or 1)
|
---|
| 85 | */
|
---|
[556] | 86 | float AddSample(T sample, const float pdf);
|
---|
[492] | 87 |
|
---|
[485] | 88 | /** Adds one pvs to another one.
|
---|
| 89 | @returns new pvs size
|
---|
| 90 | */
|
---|
| 91 | int AddPvs(const Pvs<T> &pvs);
|
---|
| 92 | /** Subtracts one pvs from another one.
|
---|
| 93 | @returns new pvs size
|
---|
| 94 | */
|
---|
| 95 | int SubtractPvs(const Pvs<T> &pvs);
|
---|
[469] | 96 | /** Returns PVS data, i.e., how often it was seen from the view cell,
|
---|
| 97 | and the object itsef.
|
---|
| 98 | */
|
---|
[466] | 99 | void GetData(const int index, T &entry, PvsData<T> &data);
|
---|
[469] | 100 |
|
---|
| 101 | /** Collects the PVS entries and returns them in the vector.
|
---|
| 102 | */
|
---|
| 103 | void CollectEntries(std::vector<T> &entries);
|
---|
| 104 |
|
---|
[485] | 105 | /** Removes sample from PVS if reference count is zero.
|
---|
| 106 | @param visibleSampels number of references to be removed
|
---|
| 107 | */
|
---|
[556] | 108 | bool RemoveSample(T sample, const float pdf);
|
---|
[485] | 109 |
|
---|
[469] | 110 | /// Map of PVS entries
|
---|
[466] | 111 | std::map<T, PvsData<T>, LtSample<T> > mEntries;
|
---|
[310] | 112 | };
|
---|
| 113 |
|
---|
[341] | 114 | template <typename T>
|
---|
[362] | 115 | int Pvs<T>::Diff(const Pvs<T> &b)
|
---|
| 116 | {
|
---|
| 117 | int dif = 0;
|
---|
| 118 |
|
---|
| 119 | std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
|
---|
| 120 |
|
---|
| 121 | for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
|
---|
| 122 | {
|
---|
| 123 | PvsData<T> *data = Find((*it).first);
|
---|
| 124 | if (!data) ++ dif;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | return dif;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | template <typename T>
|
---|
[350] | 131 | void Pvs<T>::Merge(const Pvs<T> &a, const Pvs<T> &b)
|
---|
[341] | 132 | {
|
---|
[362] | 133 | std::map<T, PvsData<T>, LtSample<T> >::const_iterator it;
|
---|
| 134 |
|
---|
[573] | 135 | mEntries = a.mEntries;
|
---|
[362] | 136 | // todo: copy all elements instead of inserting
|
---|
[573] | 137 | //for (it = a.mEntries.begin(); it != a.mEntries.end(); ++ it)
|
---|
| 138 | // mEntries.insert(*it);
|
---|
[485] | 139 |
|
---|
[362] | 140 | for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
|
---|
| 141 | {
|
---|
| 142 | PvsData<T> *data = Find((*it).first);
|
---|
| 143 |
|
---|
| 144 | if (data)
|
---|
[556] | 145 | data->mSumPdf += (*it).second.mSumPdf;
|
---|
[362] | 146 | else
|
---|
| 147 | mEntries.insert(*it);
|
---|
| 148 | }
|
---|
[341] | 149 | }
|
---|
| 150 |
|
---|
[310] | 151 | template <typename T>
|
---|
[311] | 152 | PvsData<T> *Pvs<T>::Find(T sample)
|
---|
[310] | 153 | {
|
---|
[311] | 154 | std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.find(sample);
|
---|
[310] | 155 | if (i != mEntries.end()) {
|
---|
| 156 | return &(*i).second;
|
---|
[311] | 157 | } else
|
---|
[310] | 158 | return NULL;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | template <typename T>
|
---|
[311] | 162 | void Pvs<T>::GetData(const int index,
|
---|
[485] | 163 | T &entry,
|
---|
| 164 | PvsData<T> &data)
|
---|
[310] | 165 | {
|
---|
[311] | 166 | std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.begin();
|
---|
[310] | 167 | for (int k = 0; k != index && i != mEntries.end(); i++, k++);
|
---|
| 168 |
|
---|
| 169 | entry = (*i).first;
|
---|
[311] | 170 | data = (*i).second;
|
---|
[310] | 171 | }
|
---|
| 172 |
|
---|
[311] | 173 | template <typename T>
|
---|
[556] | 174 | float
|
---|
| 175 | Pvs<T>::AddSample(T sample, const float pdf)
|
---|
[177] | 176 | {
|
---|
[492] | 177 | PvsData<T> *data = Find(sample);
|
---|
| 178 |
|
---|
| 179 | if (data) {
|
---|
[556] | 180 | data->mSumPdf+=pdf;
|
---|
| 181 | return data->mSumPdf;
|
---|
[492] | 182 | }
|
---|
| 183 | else {
|
---|
[556] | 184 | mEntries[sample] = PvsData<T>(pdf);
|
---|
| 185 | return pdf;
|
---|
[492] | 186 | }
|
---|
[466] | 187 | }
|
---|
[177] | 188 |
|
---|
[466] | 189 | template <typename T>
|
---|
[492] | 190 | bool
|
---|
[556] | 191 | Pvs<T>::AddSample(T sample,
|
---|
| 192 | const float pdf,
|
---|
| 193 | float &contribution)
|
---|
[466] | 194 | {
|
---|
| 195 | PvsData<T> *data = Find(sample);
|
---|
| 196 |
|
---|
| 197 | if (data) {
|
---|
[556] | 198 | data->mSumPdf+=pdf;
|
---|
| 199 | contribution = pdf/data->mSumPdf;
|
---|
[466] | 200 | return false;
|
---|
| 201 | }
|
---|
| 202 | else {
|
---|
[556] | 203 | mEntries[sample] = PvsData<T>(pdf);
|
---|
[466] | 204 | contribution = 1.0f;
|
---|
| 205 | return true;
|
---|
| 206 | }
|
---|
[311] | 207 | }
|
---|
[308] | 208 |
|
---|
[469] | 209 | template <typename T>
|
---|
[492] | 210 | bool
|
---|
[556] | 211 | Pvs<T>::GetSampleContribution(T sample,
|
---|
| 212 | const float pdf,
|
---|
| 213 | float &contribution)
|
---|
[492] | 214 | {
|
---|
| 215 | PvsData<T> *data = Find(sample);
|
---|
| 216 |
|
---|
| 217 | if (data) {
|
---|
[556] | 218 | contribution = pdf/(data->mSumPdf + pdf);
|
---|
[492] | 219 | return false;
|
---|
| 220 | }
|
---|
| 221 | else {
|
---|
| 222 | contribution = 1.0f;
|
---|
| 223 | return true;
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | template <typename T>
|
---|
[556] | 228 | bool Pvs<T>::RemoveSample(T sample,
|
---|
| 229 | const float pdf)
|
---|
[485] | 230 | {
|
---|
[556] | 231 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
| 232 | iterator it = mEntries.find(sample);
|
---|
[485] | 233 |
|
---|
[556] | 234 | if (it == mEntries.end())
|
---|
| 235 | return false;
|
---|
| 236 |
|
---|
| 237 | PvsData<T> *data = &(*it).second;
|
---|
| 238 |
|
---|
| 239 | data->mSumPdf -= pdf;
|
---|
| 240 | if (data->mSumPdf <= 0.0f)
|
---|
| 241 | mEntries.erase(it);
|
---|
| 242 |
|
---|
| 243 | return true;
|
---|
[485] | 244 | }
|
---|
| 245 |
|
---|
| 246 | template <typename T>
|
---|
| 247 | int Pvs<T>::AddPvs(const Pvs<T> &pvs)
|
---|
| 248 | {
|
---|
[556] | 249 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
| 250 | const_iterator it, it_end = pvs.mEntries.end();
|
---|
| 251 |
|
---|
| 252 | float contri;
|
---|
| 253 | // output PVS of view cell
|
---|
| 254 | for (it = pvs.mEntries.begin(); it != it_end; ++ it)
|
---|
| 255 | AddSample((*it).first, (*it).second.mSumPdf, contri);
|
---|
| 256 |
|
---|
| 257 | return GetSize();
|
---|
[485] | 258 | }
|
---|
| 259 |
|
---|
| 260 | template <typename T>
|
---|
| 261 | int Pvs<T>::SubtractPvs(const Pvs<T> &pvs)
|
---|
| 262 | {
|
---|
[556] | 263 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
| 264 | const_iterator it, it_end = pvs.mEntries.end();
|
---|
| 265 |
|
---|
| 266 | // output PVS of view cell
|
---|
| 267 | for (it = pvs.mEntries.begin(); it != it_end; ++ it)
|
---|
| 268 | RemoveSample((*it).first, (*it).second.mSumPdf);
|
---|
| 269 |
|
---|
| 270 | return GetSize();
|
---|
[485] | 271 | }
|
---|
| 272 |
|
---|
| 273 | template <typename T>
|
---|
[469] | 274 | void Pvs<T>::CollectEntries(std::vector<T> &entries)
|
---|
| 275 | {
|
---|
[485] | 276 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
| 277 | const_iterator it, it_end = mEntries.end();
|
---|
[469] | 278 |
|
---|
| 279 | // output PVS of view cell
|
---|
| 280 | for (it = mEntries.begin(); it != it_end; ++ it)
|
---|
| 281 | entries.push_back((*it)->first);
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[556] | 284 | template <typename T>
|
---|
| 285 | void Pvs<T>::NormalizeMaximum()
|
---|
| 286 | {
|
---|
| 287 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
| 288 | const_iterator it, it_end = mEntries.end();
|
---|
| 289 |
|
---|
| 290 | float maxPdfSum = -1.0f;
|
---|
| 291 |
|
---|
| 292 | // output PVS of view cell
|
---|
| 293 | for (it = mEntries.begin(); it != it_end; ++ it) {
|
---|
| 294 | float sum = (*it)->second.sumPdf;
|
---|
| 295 | if (sum > maxSum)
|
---|
| 296 | maxSum = sum;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | maxSum = 1.0f/maxSum;
|
---|
| 300 |
|
---|
| 301 | for (it = mEntries.begin(); it != it_end; ++ it) {
|
---|
| 302 | (*it)->second.sumPdf *= maxSum;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 |
|
---|
[311] | 308 | /** Class instantiating the Pvs template for kd tree nodes.
|
---|
| 309 | */
|
---|
| 310 | class KdPvs: public Pvs<KdNode *>
|
---|
[308] | 311 | {
|
---|
[311] | 312 | int Compress();
|
---|
[308] | 313 | };
|
---|
| 314 |
|
---|
[311] | 315 | typedef std::map<KdNode *, PvsData<KdNode *>, LtSample<KdNode *> > KdPvsMap;
|
---|
[469] | 316 | typedef std::map<Intersectable *, PvsData<Intersectable *>, LtSample<Intersectable *> > ObjectPvsMap;
|
---|
| 317 | typedef PvsData<Intersectable *> ObjectPvsData;
|
---|
[311] | 318 | typedef PvsData<KdNode *> KdPvsData;
|
---|
[308] | 319 |
|
---|
[469] | 320 | typedef Pvs<Intersectable *> ObjectPvs;
|
---|
[366] | 321 |
|
---|
[469] | 322 |
|
---|
[177] | 323 | #endif
|
---|
| 324 |
|
---|