Changeset 2116 for GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h
- Timestamp:
- 02/15/07 00:35:41 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h
r2100 r2116 1 #ifndef __PVS_H 2 #define __PVS_H 3 4 #include <map> 1 #ifndef __VERBOSEPVS_H 2 #define __VERBOSEPVS_H 3 5 4 #include <vector> 6 5 #include "common.h" 7 6 #include <math.h> 7 #include "PvsBase.h" 8 9 8 10 using namespace std; 9 11 10 12 namespace GtpVisibilityPreprocessor { 11 12 class KdNode;13 class BspNode;14 class Ray;15 class Intersectable;16 class ViewCell;17 18 19 /** Information stored with a PVS entry. Consists of the number20 the object was seen from the view cell.21 */22 template<typename T, typename S>23 struct PvsEntry24 {25 public:26 27 PvsEntry() {}28 29 PvsEntry(T sample, const S &data): mObject(sample), mData(data) {}30 31 T mObject;32 S mData;33 34 template<typename T, typename S>35 friend int operator< (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b);36 template<typename T, typename S>37 friend int operator== (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b);38 };39 40 41 template<typename T, typename S>42 int operator< (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b)43 {44 return a.mObject < b.mObject;45 }46 47 template<typename T, typename S>48 int operator== (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b)49 {50 return a.mObject == b.mObject;51 }52 53 54 template<typename T, typename S>55 struct LtSample56 {57 bool operator()(const PvsEntry<T, S> &a, const PvsEntry<T, S> &b) const58 {59 return a.mObject < b.mObject;60 }61 };62 63 template<typename T, typename S>64 int equalSample (const PvsEntry<T, S> &a, const PvsEntry<T, S> &b)65 {66 return a.mObject == b.mObject;67 }68 69 /** Information stored with a PVS entry. Consists of the number70 the object was seen from the view cell.71 */72 struct PvsData {73 public:74 PvsData() {}75 PvsData(const float sumPdf):76 mSumPdf(sumPdf) {}77 78 // $$JB in order to return meaningfull values79 // it assumes that the sum pdf has been normalized somehow!!!80 inline float GetVisibility()81 {82 return mSumPdf;83 }84 85 /// sum of probability density of visible sample rays86 float mSumPdf;87 };88 89 90 class MailablePvsData91 {92 public:93 // sum of probability density of visible sample rays94 float mSumPdf;95 int mCounter;96 97 MailablePvsData() {}98 MailablePvsData(const float sumPdf):99 mSumPdf(sumPdf) {}100 101 // $$JB in order to return meaningfull values102 // it assumes that the sum pdf has been normalized somehow!!!103 float GetVisibility()104 {105 return mSumPdf;106 }107 108 ///////////////109 // Mailing stuff110 111 // last mail id -> warning not thread safe!112 // both mailId and mailbox should be unique for each thread!!!113 static int sMailId;114 static int sReservedMailboxes;115 116 static void NewMail(const int reserve = 1) {117 sMailId += sReservedMailboxes;118 sReservedMailboxes = reserve;119 }120 121 void Mail() { mMailbox = sMailId; }122 bool Mailed() const { return mMailbox == sMailId; }123 124 void Mail(const int mailbox) { mMailbox = sMailId + mailbox; }125 bool Mailed(const int mailbox) const { return mMailbox == sMailId + mailbox; }126 127 int IncMail() { return ++ mMailbox - sMailId; }128 129 //////////////////////////////////////////130 131 protected:132 133 int mMailbox;134 135 };136 13 137 14 … … 140 17 { 141 18 public: 142 143 PvsIterator<T, S>() {} 144 19 PvsIterator<T, S>(){} 145 20 PvsIterator<T, S>(const typename vector<PvsEntry<T, S> >::const_iterator &itCurrent, 146 21 const typename vector<PvsEntry<T, S> >::const_iterator &itEnd): … … 158 33 return *(mItCurrent ++); 159 34 } 160 35 161 36 private: 162 37 typename vector<PvsEntry<T, S> >::const_iterator mItCurrent; … … 169 44 */ 170 45 template<typename T, typename S> 171 class Pvs 172 { 173 template<typename T, typename S> 174 friend class PvsIterator; 46 class VerbosePvs 47 { 48 template<typename T, typename S> friend class PvsIterator; 175 49 176 50 public: 177 51 178 Pvs(): mSamples(0), mEntries(), mLastSorted(0), mQueriesSinceSort(0) {}52 VerbosePvs(): mSamples(0), mEntries(), mLastSorted(0), mQueriesSinceSort(0) {} 179 53 180 54 /** creates pvs and initializes it with the given entries. 181 55 Assumes that entries are sorted- 182 56 */ 183 Pvs(const vector<PvsEntry<T, S> > &samples);184 virtual ~ Pvs() {};57 VerbosePvs(const vector<PvsEntry<T, S> > &samples); 58 virtual ~VerbosePvs() {}; 185 59 186 60 /** Compresses PVS lossless or lossy. … … 214 88 Warning: very slow! 215 89 */ 216 void MergeInPlace(const Pvs<T, S> &a);90 void MergeInPlace(const VerbosePvs<T, S> &a); 217 91 218 92 /** Difference of pvs to pvs b. 219 93 @returns number of different entries. 220 94 */ 221 int Diff(const Pvs<T, S> &b);95 int Diff(const VerbosePvs<T, S> &b); 222 96 223 97 /** Finds sample in PVS. … … 246 120 first checks if sample is already in clean part of the pvs. 247 121 */ 248 bool AddSampleDirtyCheck(T sample, const float pdf); //, float &contribution);122 bool AddSampleDirtyCheck(T sample, const float pdf); 249 123 250 124 /** Sort pvs entries - this should always be called after a … … 260 134 @returns PvsData 261 135 */ 262 typename std::vector<PvsEntry<T, S> >::iterator AddSample2(T sample, const float pdf); 136 typename std::vector<PvsEntry<T, S> >::iterator 137 AddSample2(T sample, const float pdf); 263 138 264 139 /** Subtracts one pvs from another one. … … 266 141 @returns new pvs size 267 142 */ 268 int SubtractPvs(const Pvs<T, S> &pvs);143 int SubtractPvs(const VerbosePvs<T, S> &pvs); 269 144 270 145 /** Returns PVS data, i.e., how often it was seen from the view cell, … … 284 159 /** Compute continuous PVS difference 285 160 */ 286 void ComputeContinuousPvsDifference( Pvs<T, S> &pvs,161 void ComputeContinuousPvsDifference(VerbosePvs<T, S> &pvs, 287 162 float &pvsReduction, 288 163 float &pvsEnlargement); … … 299 174 /** Compute continuous PVS difference 300 175 */ 301 float GetPvsHomogenity(Pvs<T, S> &pvs); 302 303 static void Merge(Pvs<T, S> &mergedPvs, const Pvs<T, S> &a, const Pvs<T, S> &b); 176 float GetPvsHomogenity(VerbosePvs<T, S> &pvs); 177 178 static void Merge(VerbosePvs<T, S> &mergedPvs, 179 const VerbosePvs<T, S> &a, 180 const VerbosePvs<T, S> &b); 304 181 305 182 int GetSamples() const … … 320 197 const int dirtySize = n - mLastSorted; 321 198 322 #define LOG2E 1.442695040f 199 const double LOG2E = 1.442695040f; 323 200 324 201 const float logN = log((float)max(1, n))/LOG2E; … … 344 221 } 345 222 346 347 223 int GetLastSorted() const 348 224 { … … 354 230 protected: 355 231 356 static void Merge( Pvs<T, S> &mergedPvs,232 static void Merge(VerbosePvs<T, S> &mergedPvs, 357 233 const typename std::vector<PvsEntry<T, S> >::const_iterator &aBegin, 358 234 const typename std::vector<PvsEntry<T, S> >::const_iterator &aEnd, … … 362 238 const int bSamples); 363 239 240 ////////////////////////////// 241 364 242 /// vector of PVS entries 365 243 vector<PvsEntry<T, S> > mEntries; … … 376 254 377 255 template <typename T, typename S> 378 Pvs<T, S>::Pvs(const vector<PvsEntry<T, S> > &samples)256 VerbosePvs<T, S>::VerbosePvs(const vector<PvsEntry<T, S> > &samples) 379 257 { 380 258 mEntries.reserve(samples.size()); … … 386 264 387 265 template <typename T, typename S> 388 void Pvs<T, S>::Sort()389 { 390 std::vector<PvsEntry<T, S> >::iterator it = mEntries.begin() + mLastSorted;391 std::vector<PvsEntry<T, S> >::iterator it_end = mEntries.end();266 void VerbosePvs<T, S>::Sort() 267 { 268 vector<PvsEntry<T, S> >::iterator it = mEntries.begin() + mLastSorted; 269 vector<PvsEntry<T, S> >::iterator it_end = mEntries.end(); 392 270 393 271 // throw out double entries … … 411 289 412 290 template <typename T, typename S> 413 void Pvs<T, S>::SimpleSort()291 void VerbosePvs<T, S>::SimpleSort() 414 292 { 415 293 // sort(mEntries.begin(), mEntries.end()); 416 std::vector<PvsEntry<T, S> >::iterator it = mEntries.begin() + mLastSorted;294 vector<PvsEntry<T, S> >::iterator it = mEntries.begin() + mLastSorted; 417 295 418 296 sort(it, mEntries.end()); … … 431 309 template <typename T, typename S> 432 310 void 433 Pvs<T, S>::ComputeContinuousPvsDifference(Pvs<T, S> &b,311 VerbosePvs<T, S>::ComputeContinuousPvsDifference(VerbosePvs<T, S> &b, 434 312 float &pvsReduction, 435 313 float &pvsEnlargement) … … 439 317 440 318 // Uses sum of log differences, which corresponds to entropy 441 std::vector<PvsEntry<T, S> >::iterator it;319 vector<PvsEntry<T, S> >::iterator it; 442 320 443 321 for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it) … … 514 392 515 393 template <typename T, typename S> 516 int Pvs<T, S>::Diff(constPvs<T, S> &b)394 int VerbosePvs<T, S>::Diff(const VerbosePvs<T, S> &b) 517 395 { 518 396 int dif = 0; … … 533 411 534 412 template <typename T, typename S> 535 void Pvs<T, S>::MergeInPlace(constPvs<T, S> &a)413 void VerbosePvs<T, S>::MergeInPlace(const VerbosePvs<T, S> &a) 536 414 { 537 415 // early exit … … 559 437 560 438 template <typename T, typename S> 561 void Pvs<T, S>::Merge(Pvs<T, S> &mergedPvs, const Pvs<T, S> &a, const Pvs<T, S> &b) 562 { 563 std::vector<PvsEntry<T, S> >::const_iterator ait = a.mEntries.begin(), ait_end = a.mEntries.end(); 564 std::vector<PvsEntry<T, S> >::const_iterator bit = b.mEntries.begin(), bit_end = b.mEntries.end(); 439 void VerbosePvs<T, S>::Merge(VerbosePvs<T, S> &mergedPvs, 440 const VerbosePvs<T, S> &a, 441 const VerbosePvs<T, S> &b) 442 { 443 std::vector<PvsEntry<T, S> >::const_iterator ait = 444 a.mEntries.begin(), ait_end = a.mEntries.end(); 445 std::vector<PvsEntry<T, S> >::const_iterator bit = 446 b.mEntries.begin(), bit_end = b.mEntries.end(); 565 447 566 448 Merge(mergedPvs, … … 573 455 574 456 template <typename T, typename S> 575 void Pvs<T, S>::Merge(Pvs<T, S> &mergedPvs,457 void VerbosePvs<T, S>::Merge(VerbosePvs<T, S> &mergedPvs, 576 458 const typename std::vector<PvsEntry<T, S> >::const_iterator &aBegin, 577 459 const typename std::vector<PvsEntry<T, S> >::const_iterator &aEnd, … … 627 509 628 510 629 template <typename T, typename S> void Pvs<T, S>::Clear(const bool trim = true)511 template <typename T, typename S> void VerbosePvs<T, S>::Clear(const bool trim = true) 630 512 { 631 513 mEntries.clear(); … … 640 522 641 523 642 template <typename T, typename S> void Pvs<T, S>::Trim()524 template <typename T, typename S> void VerbosePvs<T, S>::Trim() 643 525 { 644 526 vector<PvsEntry<T,S> >(mEntries).swap(mEntries); … … 647 529 648 530 template <typename T, typename S> 649 bool Pvs<T, S>::Find(T sample,531 bool VerbosePvs<T, S>::Find(T sample, 650 532 typename vector<PvsEntry<T, S> >::iterator &it, 651 533 const bool checkDirty) … … 684 566 685 567 template <typename T, typename S> 686 void Pvs<T, S>::GetData(const int index, T &entry, S &data)568 void VerbosePvs<T, S>::GetData(const int index, T &entry, S &data) 687 569 { 688 570 std::vector<PvsEntry<T, S> >::iterator i = mEntries.begin(); … … 695 577 696 578 template <typename T, typename S> 697 float Pvs<T, S>::AddSample(T sample, const float pdf)579 float VerbosePvs<T, S>::AddSample(T sample, const float pdf) 698 580 { 699 581 ++ mSamples; … … 719 601 720 602 template <typename T, typename S> 721 void Pvs<T, S>::AddSampleDirty(T sample, const float pdf)603 void VerbosePvs<T, S>::AddSampleDirty(T sample, const float pdf) 722 604 { 723 605 ++ mSamples; … … 727 609 728 610 template <typename T, typename S> 729 typename vector< PvsEntry<T, S> >::iterator Pvs<T, S>::AddSample2(T sample,611 typename vector< PvsEntry<T, S> >::iterator VerbosePvs<T, S>::AddSample2(T sample, 730 612 const float pdf) 731 613 { … … 751 633 752 634 753 /** Adds sample dirty (on the end of the vector) but754 first checks if sample is already in clean part of the pvs.755 */756 635 template <typename T, typename S> 757 bool Pvs<T, S>::AddSampleDirtyCheck(T sample,636 bool VerbosePvs<T, S>::AddSampleDirtyCheck(T sample, 758 637 const float pdf) 759 638 //,float &contribution) … … 784 663 785 664 template <typename T, typename S> 786 bool Pvs<T, S>::GetSampleContribution(T sample,665 bool VerbosePvs<T, S>::GetSampleContribution(T sample, 787 666 const float pdf, 788 667 float &contribution) … … 806 685 807 686 template <typename T, typename S> 808 bool Pvs<T, S>::RemoveSample(T sample, const float pdf)687 bool VerbosePvs<T, S>::RemoveSample(T sample, const float pdf) 809 688 { 810 689 -- mSamples; … … 831 710 832 711 template <typename T, typename S> 833 int Pvs<T, S>::SubtractPvs(constPvs<T, S> &pvs)712 int VerbosePvs<T, S>::SubtractPvs(const VerbosePvs<T, S> &pvs) 834 713 { 835 714 const int samples = mSamples - pvs.mSamples; … … 849 728 850 729 template <typename T, typename S> 851 void Pvs<T, S>::CollectEntries(std::vector<T> &entries)730 void VerbosePvs<T, S>::CollectEntries(std::vector<T> &entries) 852 731 { 853 732 std::vector<PvsEntry<T, S> >:: … … 861 740 862 741 template <typename T, typename S> 863 void Pvs<T, S>::NormalizeMaximum()742 void VerbosePvs<T, S>::NormalizeMaximum() 864 743 { 865 744 std::vector<PvsEntry<T, S> >:: … … 880 759 (*it)->second.sumPdf *= maxSum; 881 760 } 882 883 } 884 885 886 template <typename T, typename S> 887 float Pvs<T, S>::GetEntrySize() 761 } 762 763 764 template <typename T, typename S> 765 float VerbosePvs<T, S>::GetEntrySize() 888 766 { 889 767 return (float)(sizeof(T) + sizeof(S)) / float(1024 * 1024); … … 892 770 893 771 template <typename T, typename S> 894 int Pvs<T, S>::GetEntrySizeByte()772 int VerbosePvs<T, S>::GetEntrySizeByte() 895 773 { 896 774 return sizeof(T) + sizeof(S); … … 899 777 900 778 template <typename T, typename S> 901 float Pvs<T, S>::GetPvsHomogenity(Pvs<T, S> &pvs)779 float VerbosePvs<T, S>::GetPvsHomogenity(VerbosePvs<T, S> &pvs) 902 780 { 903 781 float pvsReduction, pvsEnlargement; 904 782 905 783 ComputeContinuousPvsDifference(pvs, pvsReduction, pvsEnlargement); 906 907 784 return pvsReduction + pvsEnlargement; 908 785 } … … 910 787 911 788 template <typename T, typename S> 912 typename PvsIterator<T, S> Pvs<T, S>::GetIterator() const789 typename PvsIterator<T, S> VerbosePvs<T, S>::GetIterator() const 913 790 { 914 791 PvsIterator<T, S> pit(mEntries.begin(), mEntries.end()); 915 916 792 return pit; 917 793 } 918 794 919 920 ///////////////////////////////////////921 922 /** Class instantiating the Pvs template for kd tree nodes.923 */924 class KdPvs: public Pvs<KdNode *, PvsData>925 {926 public:927 int Compress();928 };929 930 931 ////////////932 //-- typedefs933 934 typedef PvsEntry<Intersectable *, PvsData> ObjectPvsEntry;935 typedef std::vector<ObjectPvsEntry> ObjectPvsEntries;936 typedef Pvs<ViewCell *, MailablePvsData> ViewCellPvs;937 typedef PvsIterator<Intersectable *, PvsData> ObjectPvsIterator;938 939 940 class ObjectPvs: public Pvs<Intersectable *, PvsData>941 {942 public:943 /** Counts object int the pvs. Different to method "GetSize", not944 only the raw container size is returned,945 but the individual contributions of the entries are summed up.946 */947 float EvalPvsCost() const;948 949 friend ostream &operator<<(ostream &s, const ObjectPvs &p)950 {951 ObjectPvsIterator pit = p.GetIterator();952 953 while (pit.HasMoreEntries())954 {955 const ObjectPvsEntry &entry = pit.Next();956 Intersectable *obj = entry.mObject;957 958 cout << obj << " ";959 }960 961 return s;962 }963 };964 965 966 967 795 } 968 796
Note: See TracChangeset
for help on using the changeset viewer.