Changeset 1789 for GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h
- Timestamp:
- 11/25/06 00:19:18 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h
r1786 r1789 32 32 template<typename T, typename S> 33 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); 34 36 }; 35 37 … … 39 41 { 40 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; 41 49 } 42 50 … … 51 59 }; 52 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 } 53 66 54 67 /** Information stored with a PVS entry. Consists of the number … … 189 202 190 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) 191 206 @returns iterator on the sample. 192 207 */ 193 typename vector<PvsEntry<T, S> >::iterator Find(T sample );208 typename vector<PvsEntry<T, S> >::iterator Find(T sample, const bool checkDirty = true); 194 209 195 210 bool GetSampleContribution(T sample, const float pdf, float &contribution); … … 199 214 @returns true if sample was not already in PVS. 200 215 */ 201 bool AddSample(T sample, const float pdf, float &contribution);216 //bool AddSample(T sample, const float pdf, float &contribution); 202 217 203 218 /** Adds sample to PVS. … … 206 221 float AddSample(T sample, const float pdf); 207 222 208 /** Adds sample to PVS without checking for presence of the sample 209 pvs remians unsorted! 210 */ 211 void AddSampleDirty(T sample, const float pdf); 212 213 /** Sort pvs entries - this should always be called after a 214 sequence of AddSampleDirty calls */ 215 void Sort(); 216 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 217 238 /** Adds sample to PVS. Assumes that the pvs is sorted 218 239 @returns contribution of sample (0 or 1) … … 266 287 static void Merge(Pvs<T, S> &mergedPvs, const Pvs<T, S> &a, const Pvs<T, S> &b); 267 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 268 297 int GetSamples() const 269 298 { 270 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; 271 320 } 272 321 … … 281 330 int mSamples; 282 331 283 /// Last sorted entry in the pvs (important for find and merge 284 int mLastSorted; 285 332 /// Last sorted entry in the pvs (important for find and merge) 333 int mLastSorted; 286 334 }; 287 335 … … 293 341 mEntries = samples; 294 342 mLastSorted = 0; 295 } 343 mSamples = samples.size(); 344 } 345 296 346 297 347 template <typename T, typename S> 298 348 void Pvs<T, S>::Sort() 299 349 { 300 std::vector<PvsEntry<T, S> >::iterator it = mEntries.begin(); 301 it.inc(mLastSorted); 302 sort(it, mEntries.end()); 303 mLastSorted = mEntries.size() - 1; 304 } 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 305 370 306 371 /** … … 436 501 std::vector<PvsEntry<T, S> >::const_iterator bit = b.mEntries.begin(), bit_end = b.mEntries.end(); 437 502 438 for (; (ait != ait_end); ++ ait) 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) 439 524 { 440 525 Intersectable *aObj = (*ait).mObject; 441 526 Intersectable *bObj = NULL; 442 PvsEntry<T, S> aEntry = (*ait); 443 444 for (; (bit != bit_end) && ((*bit).mObject <= (*ait).mObject); ++ bit) 527 //Intersectable *bObjOld = NULL; 528 529 const PvsEntry<T, S> &aEntry = (*ait); 530 531 for (; (bit != bEnd) && ((*bit).mObject <= (*ait).mObject); ++ bit) 445 532 { 446 533 bObj = (*bit).mObject; … … 457 544 mergedPvs.mEntries.push_back(*bit); 458 545 } 546 547 //bObjOld = bObj; 459 548 } 460 549 … … 468 557 469 558 // add the rest 470 for (; (bit != b it_end); ++ bit)559 for (; (bit != bEnd); ++ bit) 471 560 { 472 561 mergedPvs.mEntries.push_back(*bit); 473 562 } 474 mergedPvs.mSamples = a.mSamples + b.mSamples; 563 564 mergedPvs.mSamples = aSamples + bSamples; 475 565 } 476 566 … … 480 570 mEntries.clear(); 481 571 mSamples = 0; 572 mLastSorted = 0; 482 573 483 574 if (trim) … … 495 586 496 587 template <typename T, typename S> 497 typename std::vector<PvsEntry<T, S> >::iterator Pvs<T, S>::Find(T sample )588 typename std::vector<PvsEntry<T, S> >::iterator Pvs<T, S>::Find(T sample, const bool checkDirty) 498 589 { 499 590 PvsEntry<T, S> dummy(sample, PvsData()); 500 vector<PvsEntry<T, S> >::iterator it = lower_bound(mEntries.begin(), mEntries.end(), dummy); 501 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 "; 502 608 return it; 503 609 } … … 519 625 { 520 626 ++ mSamples; 627 cout << "! "; 521 628 std::vector<PvsEntry<T, S> >::iterator it = Find(sample); 522 629 523 630 if ((it != mEntries.end()) && ((*it).mObject == sample)) 524 { 631 { cout << "g"; 525 632 S &data = (*it).mData; 526 633 data.mSumPdf += pdf; … … 528 635 } 529 636 else 530 { 637 {cout << "t"; 531 638 PvsEntry<T, S> entry(sample, pdf); 532 639 mEntries.insert(it, entry); 640 ++ mLastSorted; 533 641 return pdf; 534 642 } 643 cout << " $"; 535 644 } 536 645 … … 539 648 void Pvs<T, S>::AddSampleDirty(T sample, const float pdf) 540 649 { 541 542 650 ++ mSamples; 651 mEntries.push_back(PvsEntry<T, S>(sample, pdf)); 543 652 } 544 653 … … 548 657 { 549 658 ++ mSamples; 659 550 660 std::vector<PvsEntry<T, S> >::iterator it = Find(sample); 551 661 … … 559 669 PvsEntry<T, S> entry(sample, pdf); 560 670 mEntries.insert(it, entry); 671 ++ mLastSorted; 561 672 } 562 673 … … 565 676 566 677 567 template <typename T, typename S>678 /*template <typename T, typename S> 568 679 bool Pvs<T, S>::AddSample(T sample, 569 680 const float pdf, … … 589 700 mEntries.insert(it, entry); 590 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; 591 734 592 735 return true; … … 620 763 { 621 764 -- mSamples; 622 765 623 766 std::vector<PvsEntry<T, S> >::iterator it = Find(sample); 624 767 … … 633 776 { 634 777 mEntries.erase(it); 778 -- mLastSorted; // wrong if sample was in tail!! 635 779 } 636 780
Note: See TracChangeset
for help on using the changeset viewer.