[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 |
|
---|
| 135 | // todo: copy all elements instead of inserting
|
---|
| 136 | for (it = a.mEntries.begin(); it != a.mEntries.end(); ++ it)
|
---|
| 137 | mEntries.insert(*it);
|
---|
[485] | 138 |
|
---|
[362] | 139 | for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
|
---|
| 140 | {
|
---|
| 141 | PvsData<T> *data = Find((*it).first);
|
---|
| 142 |
|
---|
| 143 | if (data)
|
---|
[556] | 144 | data->mSumPdf += (*it).second.mSumPdf;
|
---|
[362] | 145 | else
|
---|
| 146 | mEntries.insert(*it);
|
---|
| 147 | }
|
---|
[341] | 148 | }
|
---|
| 149 |
|
---|
[310] | 150 | template <typename T>
|
---|
[311] | 151 | PvsData<T> *Pvs<T>::Find(T sample)
|
---|
[310] | 152 | {
|
---|
[311] | 153 | std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.find(sample);
|
---|
[310] | 154 | if (i != mEntries.end()) {
|
---|
| 155 | return &(*i).second;
|
---|
[311] | 156 | } else
|
---|
[310] | 157 | return NULL;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | template <typename T>
|
---|
[311] | 161 | void Pvs<T>::GetData(const int index,
|
---|
[485] | 162 | T &entry,
|
---|
| 163 | PvsData<T> &data)
|
---|
[310] | 164 | {
|
---|
[311] | 165 | std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.begin();
|
---|
[310] | 166 | for (int k = 0; k != index && i != mEntries.end(); i++, k++);
|
---|
| 167 |
|
---|
| 168 | entry = (*i).first;
|
---|
[311] | 169 | data = (*i).second;
|
---|
[310] | 170 | }
|
---|
| 171 |
|
---|
[311] | 172 | template <typename T>
|
---|
[556] | 173 | float
|
---|
| 174 | Pvs<T>::AddSample(T sample, const float pdf)
|
---|
[177] | 175 | {
|
---|
[492] | 176 | PvsData<T> *data = Find(sample);
|
---|
| 177 |
|
---|
| 178 | if (data) {
|
---|
[556] | 179 | data->mSumPdf+=pdf;
|
---|
| 180 | return data->mSumPdf;
|
---|
[492] | 181 | }
|
---|
| 182 | else {
|
---|
[556] | 183 | mEntries[sample] = PvsData<T>(pdf);
|
---|
| 184 | return pdf;
|
---|
[492] | 185 | }
|
---|
[466] | 186 | }
|
---|
[177] | 187 |
|
---|
[466] | 188 | template <typename T>
|
---|
[492] | 189 | bool
|
---|
[556] | 190 | Pvs<T>::AddSample(T sample,
|
---|
| 191 | const float pdf,
|
---|
| 192 | float &contribution)
|
---|
[466] | 193 | {
|
---|
| 194 | PvsData<T> *data = Find(sample);
|
---|
| 195 |
|
---|
| 196 | if (data) {
|
---|
[556] | 197 | data->mSumPdf+=pdf;
|
---|
| 198 | contribution = pdf/data->mSumPdf;
|
---|
[466] | 199 | return false;
|
---|
| 200 | }
|
---|
| 201 | else {
|
---|
[556] | 202 | mEntries[sample] = PvsData<T>(pdf);
|
---|
[466] | 203 | contribution = 1.0f;
|
---|
| 204 | return true;
|
---|
| 205 | }
|
---|
[311] | 206 | }
|
---|
[308] | 207 |
|
---|
[469] | 208 | template <typename T>
|
---|
[492] | 209 | bool
|
---|
[556] | 210 | Pvs<T>::GetSampleContribution(T sample,
|
---|
| 211 | const float pdf,
|
---|
| 212 | float &contribution)
|
---|
[492] | 213 | {
|
---|
| 214 | PvsData<T> *data = Find(sample);
|
---|
| 215 |
|
---|
| 216 | if (data) {
|
---|
[556] | 217 | contribution = pdf/(data->mSumPdf + pdf);
|
---|
[492] | 218 | return false;
|
---|
| 219 | }
|
---|
| 220 | else {
|
---|
| 221 | contribution = 1.0f;
|
---|
| 222 | return true;
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | template <typename T>
|
---|
[556] | 227 | bool Pvs<T>::RemoveSample(T sample,
|
---|
| 228 | const float pdf)
|
---|
[485] | 229 | {
|
---|
[556] | 230 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
| 231 | iterator it = mEntries.find(sample);
|
---|
[485] | 232 |
|
---|
[556] | 233 | if (it == mEntries.end())
|
---|
| 234 | return false;
|
---|
| 235 |
|
---|
| 236 | PvsData<T> *data = &(*it).second;
|
---|
| 237 |
|
---|
| 238 | data->mSumPdf -= pdf;
|
---|
| 239 | if (data->mSumPdf <= 0.0f)
|
---|
| 240 | mEntries.erase(it);
|
---|
| 241 |
|
---|
| 242 | return true;
|
---|
[485] | 243 | }
|
---|
| 244 |
|
---|
| 245 | template <typename T>
|
---|
| 246 | int Pvs<T>::AddPvs(const Pvs<T> &pvs)
|
---|
| 247 | {
|
---|
[556] | 248 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
| 249 | const_iterator it, it_end = pvs.mEntries.end();
|
---|
| 250 |
|
---|
| 251 | float contri;
|
---|
| 252 | // output PVS of view cell
|
---|
| 253 | for (it = pvs.mEntries.begin(); it != it_end; ++ it)
|
---|
| 254 | AddSample((*it).first, (*it).second.mSumPdf, contri);
|
---|
| 255 |
|
---|
| 256 | return GetSize();
|
---|
[485] | 257 | }
|
---|
| 258 |
|
---|
| 259 | template <typename T>
|
---|
| 260 | int Pvs<T>::SubtractPvs(const Pvs<T> &pvs)
|
---|
| 261 | {
|
---|
[556] | 262 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
| 263 | const_iterator it, it_end = pvs.mEntries.end();
|
---|
| 264 |
|
---|
| 265 | // output PVS of view cell
|
---|
| 266 | for (it = pvs.mEntries.begin(); it != it_end; ++ it)
|
---|
| 267 | RemoveSample((*it).first, (*it).second.mSumPdf);
|
---|
| 268 |
|
---|
| 269 | return GetSize();
|
---|
[485] | 270 | }
|
---|
| 271 |
|
---|
| 272 | template <typename T>
|
---|
[469] | 273 | void Pvs<T>::CollectEntries(std::vector<T> &entries)
|
---|
| 274 | {
|
---|
[485] | 275 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
| 276 | const_iterator it, it_end = mEntries.end();
|
---|
[469] | 277 |
|
---|
| 278 | // output PVS of view cell
|
---|
| 279 | for (it = mEntries.begin(); it != it_end; ++ it)
|
---|
| 280 | entries.push_back((*it)->first);
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[556] | 283 | template <typename T>
|
---|
| 284 | void Pvs<T>::NormalizeMaximum()
|
---|
| 285 | {
|
---|
| 286 | std::map<T, PvsData<T>, LtSample<T> >::
|
---|
| 287 | const_iterator it, it_end = mEntries.end();
|
---|
| 288 |
|
---|
| 289 | float maxPdfSum = -1.0f;
|
---|
| 290 |
|
---|
| 291 | // output PVS of view cell
|
---|
| 292 | for (it = mEntries.begin(); it != it_end; ++ it) {
|
---|
| 293 | float sum = (*it)->second.sumPdf;
|
---|
| 294 | if (sum > maxSum)
|
---|
| 295 | maxSum = sum;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | maxSum = 1.0f/maxSum;
|
---|
| 299 |
|
---|
| 300 | for (it = mEntries.begin(); it != it_end; ++ it) {
|
---|
| 301 | (*it)->second.sumPdf *= maxSum;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 |
|
---|
[311] | 307 | /** Class instantiating the Pvs template for kd tree nodes.
|
---|
| 308 | */
|
---|
| 309 | class KdPvs: public Pvs<KdNode *>
|
---|
[308] | 310 | {
|
---|
[311] | 311 | int Compress();
|
---|
[308] | 312 | };
|
---|
| 313 |
|
---|
[311] | 314 | typedef std::map<KdNode *, PvsData<KdNode *>, LtSample<KdNode *> > KdPvsMap;
|
---|
[469] | 315 | typedef std::map<Intersectable *, PvsData<Intersectable *>, LtSample<Intersectable *> > ObjectPvsMap;
|
---|
| 316 | typedef PvsData<Intersectable *> ObjectPvsData;
|
---|
[311] | 317 | typedef PvsData<KdNode *> KdPvsData;
|
---|
[308] | 318 |
|
---|
[469] | 319 | typedef Pvs<Intersectable *> ObjectPvs;
|
---|
[366] | 320 |
|
---|
[469] | 321 |
|
---|
[177] | 322 | #endif
|
---|
| 323 |
|
---|