Changeset 1740 for GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h
- Timestamp:
- 11/10/06 22:15:56 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h
r1739 r1740 15 15 16 16 17 template<typename T> 18 struct LtSample 19 { 20 bool operator()(const T a, const T b) const 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(T sample, const S &data): mObject(sample), mData(data) {} 26 27 T mObject; 28 S mData; 29 30 template<typename T, typename S> 31 friend int operator< (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b); 32 }; 33 34 35 template<typename T, typename S> 36 int operator< (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b) 37 { 38 return a.mObject < b.mObject; 39 } 40 41 42 template<typename T, typename S> 43 struct LtSample 44 { 45 bool operator()(const PvsEntry<T, S> &a, const PvsEntry<T, S> &b) const 21 46 { 22 return a < b;47 return a.mObject < b.mObject; 23 48 } 24 49 }; 50 25 51 26 52 /** Information stored with a PVS entry. Consists of the number … … 29 55 class PvsData { 30 56 public: 57 PvsData() {} 58 PvsData(const float sumPdf): 59 mSumPdf(sumPdf) {} 60 61 // $$JB in order to return meaningfull values 62 // it assumes that the sum pdf has been normalized somehow!!! 63 inline float GetVisibility() 64 { 65 return mSumPdf; 66 } 67 31 68 /// sum of probability density of visible sample rays 32 69 float mSumPdf; 33 34 PvsData() {} 35 PvsData(const float sumPdf): 70 }; 71 72 73 class MailablePvsData 74 { 75 public: 76 // sum of probability density of visible sample rays 77 float mSumPdf; 78 int mCounter; 79 80 MailablePvsData() {} 81 MailablePvsData(const float sumPdf): 36 82 mSumPdf(sumPdf) {} 37 83 … … 42 88 return mSumPdf; 43 89 } 44 }; 45 46 47 class MailablePvsData 48 { 90 49 91 //////////////////////////// 50 92 // Mailing stuff 51 protected: 52 int mMailbox; 53 54 public: 93 55 94 // last mail id -> warning not thread safe! 56 95 // both mailId and mailbox should be unique for each thread!!! … … 58 97 static int sReservedMailboxes; 59 98 60 61 99 static void NewMail(const int reserve = 1) { 62 100 sMailId += sReservedMailboxes; … … 71 109 72 110 int IncMail() { return ++ mMailbox - sMailId; } 111 73 112 ////////////////////////////////////////// 74 113 75 // sum of probability density of visible sample rays 76 float mSumPdf; 77 int mCounter; 78 79 MailablePvsData() {} 80 MailablePvsData(const float sumPdf): 81 mSumPdf(sumPdf) {} 82 83 // $$JB in order to return meaningfull values 84 // it assumes that the sum pdf has been normalized somehow!!! 85 float GetVisibility() 86 { 87 return mSumPdf; 88 } 89 }; 114 protected: 115 116 int mMailbox; 117 118 }; 119 120 90 121 template<typename T, typename S> 91 class Iterator 92 { 93 //template<typename T, typename S> friend Pvs<T, S>; 94 private: 95 std::vector<std::pair<T, S> >::iterator mIt; 96 //vector<pair<T, S> >::iterator mItEnd; 97 }; 122 class PvsIterator 123 { 124 private: 125 typename vector<PvsEntry<T, S> >::iterator mItCurrent; 126 typename vector<PvsEntry<T, S> >::iterator mItEnd; 127 }; 128 129 98 130 /** Template class representing the Potentially Visible Set (PVS) 99 131 mainly from a view cell, but also e.g., from objects. … … 102 134 class Pvs 103 135 { 136 template<typename T, typename S> 137 friend class PvsIterator; 138 104 139 public: 105 //vector<pair<T, S> >::iterator mItEnd; 106 107 108 template<typename T, typename S> 109 class ConstIterator 110 { 111 // template<typename T, typename S> friend Pvs<T, S>; 112 private: 113 //vector<pair<T, S> >::const_iterator mIt; 114 //vector<pair<T, S> >::const_iteator mItEnd; 115 }; 116 117 Pvs(): mSamples(0), mEntries() {} 118 119 140 141 Pvs(): mSamples(0), mEntries() {} 142 143 Pvs(const vector<PvsEntry<T, S> > &samples); 120 144 //virtual ~Pvs(); 121 145 … … 126 150 bool Empty() const {return mEntries.empty();} 127 151 128 /** Normalize the visibility of entries in order to get comparable129 results152 /** Normalize the visibility of entries in order to get 153 comparable results. 130 154 */ 131 155 void NormalizeMaximum(); 132 156 133 157 /** Merges pvs of a into this pvs. 134 */ 135 void Merge(const Pvs<T, S> &a); 158 Warning: very slow! 159 */ 160 void MergeInPlace(const Pvs<T, S> &a); 136 161 137 162 /** Difference of pvs to pvs b. … … 141 166 142 167 /** Finds sample in PVS. 143 @returns sample if found, NULL otherwise.144 */ 145 S *Find(T sample);168 @returns iterator on the sample. 169 */ 170 typename vector<PvsEntry<T, S> >::iterator Find(T sample); 146 171 147 172 bool GetSampleContribution(T sample, const float pdf, float &contribution); … … 157 182 */ 158 183 float AddSample(T sample, const float pdf); 184 185 /** Adds sample to PVS. 186 @returns contribution of sample (0 or 1) 187 */ 188 float AddSamples(const vector<PvsEntry<T, S> > &samples); 159 189 160 190 /** Adds sample to PVS. 161 191 @returns PvsData 162 192 */ 163 S *AddSample2(T sample, const float pdf); 164 165 /** Adds one pvs to another one. 166 @returns new pvs size 167 */ 168 int AddPvs(const Pvs<T, S> &pvs); 193 typename std::vector<PvsEntry<T, S> >::iterator AddSample2(T sample, const float pdf); 169 194 170 195 /** Subtracts one pvs from another one. … … 173 198 */ 174 199 int SubtractPvs(const Pvs<T, S> &pvs); 200 175 201 /** Returns PVS data, i.e., how often it was seen from the view cell, 176 202 and the object itsef. … … 189 215 /** Compute continuous PVS difference 190 216 */ 191 void ComputeContinuousPvsDifference(Pvs<T, S> &pvs, 192 float &pvsReduction, 193 float &pvsEnlargement); 194 217 void ComputeContinuousPvsDifference(Pvs<T, S> &pvs, 218 float &pvsReduction, 219 float &pvsEnlargement); 195 220 196 221 /** Clears the pvs. … … 201 226 static float GetEntrySize(); 202 227 203 /** Compute continuous PVS difference */ 204 float GetPvsHomogenity(Pvs<T, S> &pvs) { 205 float pvsReduction, pvsEnlargement; 206 207 ComputeContinuousPvsDifference(pvs, 208 pvsReduction, 209 pvsEnlargement); 210 211 return pvsReduction + pvsEnlargement; 212 } 213 214 int Size() { return mEntries.size(); } 215 216 //vector<pair<T, S> >::const_iterator &GetConstIterator() const { return mEntries.begin(); } 217 //vector<pair<T, S> >::iterator &GetIterator() { return mEntries.begin(); } 228 /** Compute continuous PVS difference 229 */ 230 float GetPvsHomogenity(Pvs<T, S> &pvs); 231 232 static void Merge(Pvs<T, S> &mergedPvs, const Pvs<T, S> &a, const Pvs<T, S> &b); 218 233 219 234 //protected: 220 235 221 236 /// vector of PVS entries 222 vector< pair<T, S> > mEntries;237 vector<PvsEntry<T, S> > mEntries; 223 238 224 239 /// Number of samples used to create the PVS … … 227 242 228 243 244 template <typename T, typename S> 245 Pvs<T, S>::Pvs(const vector<PvsEntry<T, S> > &samples) 246 { 247 mEntries.reserve(samples.size()); 248 mEntries = samples; 249 } 250 229 251 230 252 /** … … 236 258 void 237 259 Pvs<T, S>::ComputeContinuousPvsDifference(Pvs<T, S> &b, 238 239 260 float &pvsReduction, 261 float &pvsEnlargement) 240 262 { 241 263 pvsReduction = 0.0f; 242 264 pvsEnlargement = 0.0f; 265 243 266 // Uses sum of log differences, which corresponds to entropy 244 std::vector< pair<T, S> >::iterator it;267 std::vector<PvsEntry<T, S> >::iterator it; 245 268 246 269 for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it) 247 270 { 248 float bSumPdf = (*it). second.mSumPdf;271 float bSumPdf = (*it).mData.mSumPdf; 249 272 float aSumPdf = 0.0f; 250 S *data = Find((*it).first); 251 252 if (data) 273 274 vector<PvsEntry<T, S> >::iterator oit = Find((*it).mObject); 275 276 const bool entryFound = (it != mEntries.end()) && ((*it).mObject == (*oit).mObject); 277 278 if (entryFound) 253 279 { 254 aSumPdf = data->mSumPdf; 280 aSumPdf = (*it).mData.mSumPdf; 281 255 282 // mark this entry as processed to avoid double counting 256 data->mSumPdf = -aSumPdf;283 (*it).mData.mSumPdf = -aSumPdf; 257 284 } 258 285 259 286 #if 0 260 float diff = bSumPdf - aSumPdf;287 const float diff = bSumPdf - aSumPdf; 261 288 262 289 if (diff > 0.0f) { … … 266 293 } 267 294 #else 268 if (! data)295 if (!entryFound) 269 296 pvsEnlargement += 1.0f; 270 297 #endif 271 298 } 272 299 273 for (it = mEntries.begin(); it != mEntries.end(); ++ it) { 274 float aSumPdf = (*it).second.mSumPdf; 300 for (it = mEntries.begin(); it != mEntries.end(); ++ it) 301 { 302 float aSumPdf = (*it).mData.mSumPdf; 275 303 float bSumPdf = 0.0f; 276 304 if (aSumPdf < 0.0f) { 305 277 306 // this entry was already accounted for! 278 307 // just revert it back 279 (*it). second.mSumPdf = -aSumPdf;308 (*it).mData.mSumPdf = -aSumPdf; 280 309 } else { 281 S *data = b.Find((*it).first); 282 if (data) { 283 bSumPdf = data->mSumPdf; 310 vector<PvsEntry<T, S> >::iterator oit = b.Find((*it).mObject); 311 312 const bool entryFound = (it != mEntries.end()) && ((*it).mObject == (*oit).mObject); 313 314 if (entryFound) { 315 bSumPdf = (*oit).mData.mSumPdf; 284 316 } 285 317 #if 0 286 float diff = bSumPdf - aSumPdf;318 const float diff = bSumPdf - aSumPdf; 287 319 288 320 if (diff > 0.0f) { … … 293 325 294 326 #else 295 if (! data)327 if (!entryFound) 296 328 pvsReduction += 1.0f; 297 329 #endif … … 306 338 int dif = 0; 307 339 308 std::vector< pair<T, S> >::const_iterator it;340 std::vector<PvsEntry<T, S> >::const_iterator it; 309 341 310 342 for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it) 311 343 { 312 S data= Find((*it).first);313 if ( !data) ++ dif;344 std::vector<PvsEntry<T, S> >::const_iterator bit = Find((*it).first); 345 if (bit == mEntries.end()) ++ dif; 314 346 } 315 347 … … 317 349 } 318 350 319 template <typename T, typename S> void Pvs<T, S>::Merge(const Pvs<T, S> &a) 320 { 321 #if TODO 351 352 template <typename T, typename S> 353 void Pvs<T, S>::MergeInPlace(const Pvs<T, S> &a) 354 { 355 const int samples = mSamples + a.mSamples; 356 357 ObjectPvs interPvs; 358 359 Merge(interPvs, *this, a); 360 361 mEntries.reserve(interPvs.GetSize()); 362 mEntries = interPvs.mEntries; 363 364 mSamples = samples; 365 } 366 367 368 template <typename T, typename S> 369 void Pvs<T, S>::Merge(Pvs<T, S> &mergedPvs, const Pvs<T, S> &a, const Pvs<T, S> &b) 370 { 371 std::vector<PvsEntry<T, S> >::const_iterator ait = a.mEntries.begin(), ait_end = a.mEntries.end(); 372 std::vector<PvsEntry<T, S> >::const_iterator bit = b.mEntries.begin(), bit_end = b.mEntries.end(); 373 374 while (1) 375 { 376 for (; (ait != ait_end) && ((*ait).mObject < (*bit).mObject); ++ ait) 377 { 378 if ((*ait).mObject != (*bit).mObject) 379 mergedPvs.mEntries.push_back(*ait); 380 } 381 382 for (; (bit != bit_end) && ((ait == ait_end) || ((*bit).mObject <= (*bit).mObject)); ++ bit) 383 { 384 if ((*ait).mObject == (*bit).mObject) 385 { 386 PvsData newData((*ait).mData.mSumPdf + (*bit).mData.mSumPdf); 387 PvsEntry<T, S> entry((*ait).mObject, newData); 388 mergedPvs.mEntries.push_back(entry); 389 } 390 else 391 { 392 mergedPvs.mEntries.push_back(*bit); 393 } 394 } 395 } 396 } 397 398 399 /*template <typename T, typename S> void Pvs<T, S>::QuickMerge(const Pvs<T, S> &a) 400 { 322 401 const int samples = mSamples + a.mSamples; 323 402 324 std::vector<pair<T, S>> >::const_iterator it; 325 326 for (it = a.mEntries.begin(); it != a.mEntries.end(); ++ it) 327 { 328 AddSample((*it).first, (*it).second.mSumPdf); 403 std::vector<PvsEntry<T, S>> >::const_iterator it; 404 405 for (it = mEntries.begin(); it != mEntries.end(); ++ it) 406 { 407 it->first->Mail(); 408 } 409 410 for (it = a.mEntries.begin(); it != a.mEntries.end(); ++ it) 411 { 412 if (!it->first->Mailed()) 413 { 414 mEntries.push_back(*it); 415 } 329 416 } 330 417 331 418 mSamples = samples; 332 #endif 333 } 419 }*/ 334 420 335 421 … … 341 427 342 428 template <typename T, typename S> 343 S *Pvs<T, S>::Find(T sample) 344 { 345 #if TODO 346 std::vector<pair<T, S>, LtSample<T> >::iterator i = mEntries.find(sample); 347 348 if (i != mEntries.end()) 349 { 350 return &(*i).second; 351 } 352 else 353 { 354 return NULL; 355 } 356 #else 357 return NULL; 358 #endif 359 } 360 361 template <typename T, typename S> 362 void Pvs<T, S>::GetData(const int index, 363 T &entry, 364 S &data) 365 { 366 std::vector<pair<T, S> >::iterator i = mEntries.begin(); 429 typename std::vector<PvsEntry<T, S> >::iterator Pvs<T, S>::Find(T sample) 430 { 431 PvsEntry<T, S> dummy(sample, PvsData()); 432 vector<PvsEntry<T, S> >::iterator it = lower_bound(mEntries.begin(), mEntries.end(), dummy); 433 434 return it; 435 } 436 437 438 template <typename T, typename S> 439 void Pvs<T, S>::GetData(const int index, T &entry, S &data) 440 { 441 std::vector<PvsEntry<T, S> >::iterator i = mEntries.begin(); 367 442 for (int k = 0; k != index && i != mEntries.end(); ++ i, ++ k); 368 443 … … 373 448 374 449 template <typename T, typename S> 375 float 376 Pvs<T, S>::AddSample(T sample, const float pdf) 377 { 378 #if TODO 450 float Pvs<T, S>::AddSample(T sample, const float pdf) 451 { 379 452 ++ mSamples; 380 S *data = Find(sample); 381 382 if (data) 383 { 453 std::vector<PvsEntry<T, S> >::iterator it = Find(sample); 454 455 if ((it != mEntries.end()) && ((*it).mObject == sample)) 456 { 457 S &data = (*it).mData; 458 data.mSumPdf += pdf; 459 460 return data.mSumPdf; 461 } 462 else 463 { 464 PvsEntry<T, S> entry(sample, pdf); 465 466 mEntries.insert(it, entry); 467 return pdf; 468 } 469 } 470 471 472 template <typename T, typename S> 473 typename vector< PvsEntry<T, S> >::iterator Pvs<T, S>::AddSample2(T sample, const float pdf) 474 { 475 ++ mSamples; 476 std::vector<PvsEntry<T, S> >::iterator it = Find(sample); 477 478 if ((it != mEntries.end()) && ((*it).first == T)) 479 { 480 S &data = (*it).second; 384 481 data->mSumPdf += pdf; 385 return data->mSumPdf;386 482 } 387 483 else 388 484 { 389 mEntries[sample] = S(pdf); 390 return pdf; 391 } 392 #else 393 return 0; 394 #endif 395 } 396 397 398 template <typename T, typename S> 399 S * Pvs<T, S>::AddSample2(T sample, const float pdf) 400 { 401 mSamples++; 402 S *data = Find(sample); 403 404 if (data) 405 { 406 data->mSumPdf += pdf; 407 } 408 else 409 { 410 mEntries[sample] = S(pdf); 411 data = Find(sample); 412 } 413 414 return data; 415 } 485 PvsEntry<T, S> entry(sample, pdf); 486 mEntries.insert(it, entry); 487 } 488 489 return it; 490 } 491 416 492 417 493 template <typename T, typename S> 418 494 bool Pvs<T, S>::AddSample(T sample, 419 const float pdf,420 float &contribution)495 const float pdf, 496 float &contribution) 421 497 { 422 498 ++ mSamples; 423 S *data = Find(sample); 424 #if TODO 425 if (data) 426 { 427 data->mSumPdf += pdf; 428 contribution = pdf / data->mSumPdf; 499 500 std::vector<PvsEntry<T, S> >::iterator it = Find(sample); 501 502 if ((it != mEntries.end()) && ((*it).mObject == sample)) 503 { 504 S &data = (*it).mData; 505 506 data.mSumPdf += pdf; 507 contribution = pdf / data.mSumPdf; 508 429 509 return false; 430 510 } 431 else { 432 mEntries[sample] = S(pdf); 511 else 512 { 513 PvsEntry<T, S> entry(sample, pdf); 514 515 mEntries.insert(it, entry); 516 contribution = 1.0f; 517 518 return true; 519 } 520 } 521 522 523 template <typename T, typename S> 524 bool Pvs<T, S>::GetSampleContribution(T sample, 525 const float pdf, 526 float &contribution) 527 { 528 std::vector<PvsEntry<T, S> >::iterator it = Find(sample); 529 530 if (it != mEntries.end() && ((*it).mObject == sample)) 531 { 532 S &data = (*it).mData; 533 contribution = pdf / (data.mSumPdf + pdf); 534 return false; 535 } 536 else 537 { 433 538 contribution = 1.0f; 434 539 return true; 435 540 } 436 #else 541 } 542 543 544 template <typename T, typename S> 545 bool Pvs<T, S>::RemoveSample(T sample, const float pdf) 546 { 547 -- mSamples; 548 549 std::vector<PvsEntry<T, S> >::iterator it = Find(sample); 550 551 if (it == mEntries.end()) 552 return false; 553 554 S &data = (*it).mData; 555 556 data.mSumPdf -= pdf; 557 558 if (data.mSumPdf <= 0.0f) 559 { 560 mEntries.erase(it); 561 } 562 437 563 return true; 438 #endif 439 } 440 441 template <typename T, typename S> 442 bool 443 Pvs<T, S>::GetSampleContribution(T sample, 444 const float pdf, 445 float &contribution) 446 { 447 S *data = Find(sample); 448 449 if (data) { 450 contribution = pdf / (data->mSumPdf + pdf); 451 return false; 452 } 453 else { 454 contribution = 1.0f; 455 return true; 456 } 457 } 458 459 template <typename T, typename S> 460 bool Pvs<T, S>::RemoveSample(T sample, 461 const float pdf) 462 { 463 -- mSamples; 464 #if TODO 465 std::vector<pair<T, S> >::iterator it = mEntries.find(sample); 466 467 if (it == mEntries.end()) 468 return false; 469 470 S *data = &(*it).second; 471 472 data->mSumPdf -= pdf; 473 474 if (data->mSumPdf <= 0.0f) 475 { 476 mEntries.erase(it); 477 } 478 #endif 479 return true; 480 } 481 482 template <typename T, typename S> 483 int Pvs<T, S>::AddPvs(const Pvs<T, S> &pvs) 484 { 485 int samples = mSamples + pvs.mSamples; 486 std::vector<pair<T, S> >:: 487 const_iterator it, it_end = pvs.mEntries.end(); 488 489 float contri; 490 // output PVS of view cell 491 for (it = pvs.mEntries.begin(); it != it_end; ++ it) 492 { 493 AddSample((*it).first, (*it).second.mSumPdf, contri); 494 } 495 496 mSamples = samples; 497 498 return GetSize(); 499 } 500 564 } 565 566 501 567 template <typename T, typename S> 502 568 int Pvs<T, S>::SubtractPvs(const Pvs<T, S> &pvs) 503 569 { 504 570 const int samples = mSamples - pvs.mSamples; 505 #if TODO 506 std::vector< pair<T, S> >::571 572 std::vector<PvsEntry<T, S> >:: 507 573 const_iterator it, it_end = pvs.mEntries.end(); 508 574 509 575 // output PVS of view cell 510 576 for (it = pvs.mEntries.begin(); it != it_end; ++ it) 511 RemoveSample((*it). first, (*it).second.mSumPdf);577 RemoveSample((*it).mObject, (*it).mData.mSumPdf); 512 578 513 579 mSamples = samples; 514 #endif 580 515 581 return GetSize(); 516 582 } 517 583 584 518 585 template <typename T, typename S> 519 586 void Pvs<T, S>::CollectEntries(std::vector<T> &entries) 520 587 { 521 std::vector< pair<T, S> >::588 std::vector<PvsEntry<T, S> >:: 522 589 const_iterator it, it_end = mEntries.end(); 523 590 … … 527 594 } 528 595 596 529 597 template <typename T, typename S> 530 598 void Pvs<T, S>::NormalizeMaximum() 531 599 { 532 std::vector<pair<T, S> >::533 const_iterator it, it_end = mEntries.end();534 535 536 537 538 539 float sum = (*it)->second.sumPdf;540 if (sum > maxSum)541 542 543 544 545 546 547 (*it)->second.sumPdf *= maxSum;548 549 600 std::vector<PvsEntry<T, S> >:: 601 const_iterator it, it_end = mEntries.end(); 602 603 float maxPdfSum = -1.0f; 604 605 // output PVS of view cell 606 for (it = mEntries.begin(); it != it_end; ++ it) { 607 float sum = (*it)->second.sumPdf; 608 if (sum > maxSum) 609 maxSum = sum; 610 } 611 612 maxSum = 1.0f / maxSum; 613 614 for (it = mEntries.begin(); it != it_end; ++ it) { 615 (*it)->second.sumPdf *= maxSum; 616 } 617 550 618 } 551 619 … … 562 630 { 563 631 return sizeof(T) + sizeof(S); 632 } 633 634 635 template <typename T, typename S> 636 float Pvs<T, S>::GetPvsHomogenity(Pvs<T, S> &pvs) 637 { 638 float pvsReduction, pvsEnlargement; 639 640 ComputeContinuousPvsDifference(pvs, pvsReduction, pvsEnlargement); 641 642 return pvsReduction + pvsEnlargement; 564 643 } 565 644 … … 590 669 //-- typedefs 591 670 592 //typedef std::map<KdNode *, PvsData, LtSample<KdNode *> > KdPvsMap; 593 //typedef std::map<Intersectable *, PvsData, LtSample<Intersectable *> > ObjectPvsEntries; 594 //typedef std::map<ViewCell *, MailablePvsData, LtSample<ViewCell *> > ViewCellPvsEntries; 595 596 597 typedef std::vector<pair<Intersectable *, PvsData> > ObjectPvsEntries; 598 671 typedef std::vector<PvsEntry<Intersectable *, PvsData> > ObjectPvsEntries; 599 672 typedef Pvs<ViewCell *, MailablePvsData> ViewCellPvs; 600 673
Note: See TracChangeset
for help on using the changeset viewer.