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