Changeset 2116 for GTP/trunk/Lib/Vis/Preprocessing/src/HashPvs.h
- Timestamp:
- 02/15/07 00:35:41 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/Lib/Vis/Preprocessing/src/HashPvs.h
r2102 r2116 2 2 #define __HASHPVS_H 3 3 4 #include <hash_map>5 #include < vector>4 //#include <hash_map> 5 #include <hash_set> 6 6 #include "common.h" 7 7 #include <math.h> … … 13 13 namespace GtpVisibilityPreprocessor { 14 14 15 class KdNode; 16 class BspNode; 17 class Ray; 18 class Intersectable; 19 class ViewCell; 15 16 template<typename T> 17 struct my_hash_compare 18 { 19 enum 20 { 21 bucket_size = 4, 22 min_buckets = 80 23 }; 24 25 size_t operator()(T a) const 26 { 27 size_t h = (size_t)(a->GetId());// % 2000; 28 //for (; *s; ++s) 29 // h = 5 * h + *s; 30 return h; 31 } 32 33 bool operator()(T a, T b) const 34 { 35 return a < b; 36 } 37 }; 38 39 /* 40 template<typename T> 41 struct gt 42 { 43 bool operator() (T s1, T s2) const 44 { 45 return s1 > s2; 46 } 47 }; 48 */ 49 //#define HASH_SET stdext::hash_set<T, stdext::hash_compare<T, gt<T> > > 50 #define HASH_SET stdext::hash_set<T, my_hash_compare<T> > 51 52 /** Iterator over a hash pvs. 53 */ 54 template<typename T, typename S> 55 class HashPvsIterator 56 { 57 public: 58 59 HashPvsIterator<T, S>() {} 60 61 HashPvsIterator<T, S>(const typename HASH_SET::const_iterator &itCurrent, 62 const typename HASH_SET::const_iterator &itEnd): 63 mItCurrent(itCurrent), mItEnd(itEnd) 64 { 65 } 66 67 bool HasMoreEntries() const 68 { 69 return (mItCurrent != mItEnd); 70 } 71 72 PvsEntry<T, S> Next() 73 { 74 // hack: create new pvs entry 75 return PvsEntry<T, S>(*(mItCurrent) ++, S(0.0)); 76 } 77 78 private: 79 typename HASH_SET::const_iterator mItCurrent; 80 typename HASH_SET::const_iterator mItEnd; 81 }; 20 82 21 83 … … 23 85 mainly from a view cell, but also e.g., from objects. 24 86 */ 25 template<typename T> 26 class HashPvs: public PvsBase<T> 27 { 28 template<typename T> 29 friend class PvsIterator2; 87 template<typename T, typename S> 88 class HashPvs//: public PvsBase<T> 89 { 90 template<typename T, typename S> friend class HashPvsIterator; 30 91 31 92 public: 32 93 94 //HashPvs(): mEntries((Intersectable *)100) {}; 33 95 HashPvs() {}; 34 96 //virtual ~HashPvs() {}; … … 40 102 @returns contribution of sample (0 or 1) 41 103 */ 42 float AddSample(T sample );104 float AddSample(T sample, const float pdf); 43 105 44 106 /** Adds sample to PVS without checking for presence of the sample 45 107 warning: pvs remains unsorted! 46 108 */ 47 void AddSampleDirty(T sample );109 void AddSampleDirty(T sample, const float pdf); 48 110 49 111 /** Adds sample dirty (on the end of the vector) but 50 112 first checks if sample is already in clean part of the pvs. 51 113 */ 52 bool AddSampleDirtyCheck(T sample );114 bool AddSampleDirtyCheck(T sample, const float pdf); 53 115 54 116 /** Sort pvs entries - this should always be called after a … … 61 123 void Clear(const bool trim = true); 62 124 63 //static void Merge(PvsBase &mergedPvs, const PvsBase &a, const PvsBase &b);64 65 125 bool IsDirty() const; 66 126 67 127 bool RequiresResort() const; 68 128 69 //typename PvsIterator<T> GetIterator() const; 70 129 /** Finds sample in PVS. 130 @param checkDirty if dirty part of the pvs should be checked for entry 131 (warning: linear runtime in dirty part) 132 @returns iterator on the sample if found, else the place where 133 it would be added in the sorted vector. 134 */ 135 bool Find(T sample, typename HASH_SET::iterator &it); 136 137 typename HashPvsIterator<T, S> GetIterator() const; 138 139 /** Compute continuous PVS difference 140 */ 141 float GetPvsHomogenity(HashPvs<T, S> &pvs); 142 143 static void Merge(HashPvs<T, S> &mergedPvs, 144 const HashPvs<T, S> &a, 145 const HashPvs<T, S> &b); 146 147 static int GetEntrySizeByte(); 148 static float GetEntrySize(); 149 150 bool GetSampleContribution(T sample, 151 const float pdf, 152 float &contribution); 153 154 int GetSamples() const 155 { 156 return mSamples; 157 } 158 159 void MergeInPlace(const HashPvs<T, S> &a) 160 { 161 cerr << "not implemented yet" << endl; 162 } 163 164 bool RequiresResortLog() const 165 { 166 return false; 167 } 168 169 void Reserve(const int n) 170 { 171 // not necessary 172 } 173 174 /** Sort pvs entries assume that the pvs contains unique entries 175 */ 176 void SimpleSort() 177 { 178 // not necessary 179 } 180 181 int SubtractPvs(const HashPvs<T, S> &pvs) 182 { 183 cerr << "not yet implemented" << endl; 184 return 0; 185 } 186 187 /** Compute continuous PVS difference 188 */ 189 void ComputeContinuousPvsDifference(HashPvs<T, S> &pvs, 190 float &pvsReduction, 191 float &pvsEnlargement) 192 { 193 cerr << "not yet implemented" << endl; 194 } 71 195 protected: 72 196 73 /// vectorof PVS entries74 vector<T>mEntries;197 /// hash table of PVS entries 198 HASH_SET mEntries; 75 199 76 200 /// Number of samples used to create the PVS … … 79 203 80 204 81 template <typename T> 82 int HashPvs<T>::GetSize() const 83 { 84 return 0; 85 } 86 87 88 template <typename T> 89 bool HashPvs<T>::Empty() const 205 template <typename T, typename S> 206 bool HashPvs<T, S>::Find(T sample, typename HASH_SET::iterator &it) 207 { 208 it = mEntries.find(sample); 209 210 // already in map 211 return (it != mEntries.end()); 212 } 213 214 215 template <typename T, typename S> 216 int HashPvs<T, S>::GetSize() const 217 { 218 return (int)mEntries.size(); 219 } 220 221 222 template <typename T, typename S> 223 bool HashPvs<T, S>::Empty() const 224 { 225 return mEntries.empty(); 226 } 227 228 229 template <typename T, typename S> 230 float HashPvs<T, S>::AddSample(T sample, const float pdf) 231 { 232 HASH_SET::iterator it; 233 234 if (Find(sample, it)) 235 return 0.0f; 236 237 mEntries.insert(sample); 238 return 1.0f; 239 } 240 241 242 template <typename T, typename S> 243 void HashPvs<T, S>::AddSampleDirty(T sample, const float pdf) 244 { 245 HASH_SET::iterator it; 246 247 // not yet in map 248 if (!Find(sample, it)) 249 { 250 mEntries.insert(sample); 251 } 252 } 253 254 255 template <typename T, typename S> 256 bool HashPvs<T, S>::AddSampleDirtyCheck(T sample, 257 const float pdf) 258 { 259 HASH_SET::iterator it; 260 261 // already in map 262 if (Find(sample, it)) 263 return false; 264 265 mEntries.insert(sample); 266 return true; 267 } 268 269 270 template <typename T, typename S> 271 void HashPvs<T, S>::Sort() 272 { 273 } 274 275 276 template <typename T, typename S> 277 void HashPvs<T, S>::Clear(const bool trim = true) 278 { 279 mEntries.clear(); 280 } 281 282 283 template <typename T, typename S> 284 bool HashPvs<T, S>::IsDirty() const 90 285 { 91 286 return false; … … 93 288 94 289 95 template <typename T> 96 float HashPvs<T>::AddSample(T sample) 97 { 98 return 0; 99 } 100 101 102 template <typename T> 103 void HashPvs<T>::AddSampleDirty(T sample) 104 { 105 } 106 107 108 template <typename T> 109 bool HashPvs<T>::AddSampleDirtyCheck(T sample) 290 template <typename T, typename S> 291 bool HashPvs<T, S>::RequiresResort() const 110 292 { 111 293 return false; … … 113 295 114 296 115 template <typename T> 116 void HashPvs<T>::Sort() 117 { 118 } 119 120 121 template <typename T> 122 void HashPvs<T>::Clear(const bool trim = true) 123 { 124 } 125 126 127 template <typename T> 128 bool HashPvs<T>::IsDirty() const 129 { 130 return false; 131 } 132 133 134 template <typename T> 135 bool HashPvs<T>::RequiresResort() const 136 { 137 return false; 138 } 139 140 //typename PvsIterator<T> GetIterator() const; 297 template <typename T, typename S> 298 typename HashPvsIterator<T, S> HashPvs<T, S>::GetIterator() const 299 { 300 HashPvsIterator<T, S> pit(mEntries.begin(), mEntries.end()); 301 302 return pit; 303 } 304 305 306 template <typename T, typename S> 307 float HashPvs<T, S>::GetEntrySize() 308 { 309 return (float)(sizeof(T)) / float(1024 * 1024); 310 } 311 312 313 template <typename T, typename S> 314 int HashPvs<T, S>::GetEntrySizeByte() 315 { 316 return sizeof(T); 317 } 318 319 320 template <typename T, typename S> 321 float HashPvs<T, S>::GetPvsHomogenity(HashPvs<T, S> &pvs) 322 { 323 float pvsReduction, pvsEnlargement; 324 325 ComputeContinuousPvsDifference(pvs, pvsReduction, pvsEnlargement); 326 327 return pvsReduction + pvsEnlargement; 328 } 329 330 331 template <typename T, typename S> 332 bool HashPvs<T, S>::GetSampleContribution(T sample, 333 const float pdf, 334 float &contribution) 335 { 336 HASH_SET::iterator it; 337 const bool entryFound = Find(sample, it); 338 339 if (entryFound) 340 { 341 contribution = 0.0f; 342 return false; 343 } 344 else 345 { 346 contribution = 1.0f; 347 return true; 348 } 349 } 350 351 352 template <typename T, typename S> 353 void HashPvs<T, S>::Merge(HashPvs<T, S> &mergedPvs, 354 const HashPvs<T, S> &a, 355 const HashPvs<T, S> &b) 356 { 357 HASH_SET::const_iterator ait, ait_end = a.mEntries.end(); 358 359 for (ait = a.mEntries.begin(); ait != ait_end; ++ ait) 360 { 361 mergedPvs.AddSample(*ait, 1.0f); 362 } 363 364 HASH_SET::const_iterator bit, bit_end = b.mEntries.end(); 365 366 for (bit = b.mEntries.begin(); bit != bit_end; ++ bit) 367 { 368 mergedPvs.AddSample(*bit, 1.0f); 369 } 370 371 //cerr << "not implemented" << endl; 372 } 141 373 142 374 }
Note: See TracChangeset
for help on using the changeset viewer.