Debian Codegraph @

plugins/core/Standard/qRANSAC_SD/extern/RANSAC_SD/Grid.h

raw
1 #ifndef GRID_HEADER
2 #define GRID_HEADER
3
4 #include <unordered_map>
5
6 template< class CellT, unsigned int DimT >
7 class ArrayGridKernel
8 {
9 public:
10 ArrayGridKernel()
11 : m_array(NULL)
12 {}
13
14 ArrayGridKernel(ArrayGridKernel< CellT, DimT - 1 > *array)
15 : m_array(array)
16 {}
17
18 ~ArrayGridKernel()
19 {
20 delete[] m_array;
21 }
22
23 ArrayGridKernel< CellT, DimT - 1 > *&Data()
24 {
25 return m_array;
26 }
27
28 protected:
29 ArrayGridKernel< CellT, DimT - 1 > *m_array;
30 };
31
32
33 template< class CellT >
34 class ArrayGridKernel< CellT, 0 >
35 {
36 public:
37 operator CellT &()
38 {
39 return m_cell;
40 }
41
42 operator const CellT &() const
43 {
44 return m_cell;
45 }
46
47 CellT &Data()
48 {
49 return m_cell;
50 }
51
52 protected:
53 CellT m_cell;
54 };
55
56
57 template< class CellT, unsigned int DimT >
58 class ArrayGridAccessor
59 {
60 public:
61 ArrayGridAccessor(unsigned int *extent,
62 ArrayGridKernel< CellT, DimT - 1 > *&array)
63 : m_extent(extent)
64 , m_array(array)
65 {}
66
67 ArrayGridAccessor< CellT, DimT - 1 > operator[](unsigned int i)
68 {
69 if(!m_array)
70 m_array = new ArrayGridKernel< CellT, DimT - 1 >[*m_extent];
71 return ArrayGridAccessor< CellT, DimT - 1 >(m_extent + 1,
72 m_array[i].Data());
73 }
74
75 private:
76 unsigned int *m_extent;
77 ArrayGridKernel< CellT, DimT - 1 > *&m_array;
78 };
79
80
81 template< class CellT >
82 class ArrayGridAccessor< CellT, 0 >
83 {
84 public:
85 ArrayGridAccessor(unsigned int *, CellT &cell)
86 : m_cell(cell)
87 {}
88
89 CellT &operator=(const CellT &c)
90 {
91 return m_cell = c;
92 }
93
94 operator CellT &()
95 {
96 return m_cell;
97 }
98
99 private:
100 CellT &m_cell;
101 };
102
103 template< class CellT, unsigned int DimT >
104 class ConstArrayGridAccessor
105 {
106 public:
107 ConstArrayGridAccessor(const ArrayGridKernel< CellT, DimT - 1 > *array)
108 : m_array(array)
109 {}
110
111 const ConstArrayGridAccessor< CellT, DimT - 1 > operator[](
112 unsigned int i) const
113 {
114 return ConstArrayGridAccessor< CellT, DimT - 1 >(m_array[i].Data());
115 }
116
117 private:
118 const ArrayGridKernel< CellT, DimT - 1 > *m_array;
119 };
120
121 template< class CellT >
122 class ConstArrayGridAccessor< CellT, 0 >
123 {
124 public:
125 ConstArrayGridAccessor(unsigned int *, const CellT &cell)
126 : m_cell(cell)
127 {}
128
129 operator const CellT &() const
130 {
131 return m_cell;
132 }
133
134 private:
135 const CellT &m_cell;
136 };
137
138 template< class CellT, unsigned int DimT >
139 class ArrayGrid
140 : protected ArrayGridKernel< CellT, DimT >
141 {
142 public:
143 ArrayGrid()
144 {
145 for(unsigned int i = 0; i < DimT; ++i)
146 m_extent[i] = 0;
147 }
148
149 ArrayGrid(const MiscLib::Vector< unsigned int > &extent)
150 {
151 Extent(&extent[0]);
152 }
153
154 ArrayGrid(const unsigned int *extent)
155 {
156 Extent(extent);
157 }
158
159 void Clear()
160 {
161 delete this->m_array;
162 this->m_array = NULL;
163 }
164
165 void Extent(const unsigned int *extent)
166 {
167 Clear();
168 for(unsigned int i = 0; i < DimT; ++i)
169 m_extent[i] = extent[i];
170 }
171
172 ArrayGridAccessor< CellT, DimT - 1 > operator[](unsigned int i)
173 {
174 return ArrayGridAccessor< CellT, DimT >(m_extent, this->m_array)[i];
175 }
176
177 const ConstArrayGridAccessor< CellT, DimT - 1 > operator[](
178 unsigned int i) const
179 {
180 return ConstArrayGridAccessor< CellT, DimT >(this->m_array)[i];
181 }
182
183 private:
184 unsigned int m_extent[DimT];
185 };
186
187
188 template< class CellT, unsigned int DimT >
189 class HashGridAccessor
190 {
191 public:
192 HashGridAccessor(const size_t *factors, size_t hashKey,
193 std::unordered_map< size_t, CellT > &hash)
194 : m_factors(factors)
195 , m_hashKey(hashKey)
196 , m_hash(hash)
197 {}
198
199 HashGridAccessor< CellT, DimT - 1 > operator[](size_t i)
200 {
201 return HashGridAccessor< CellT, DimT - 1 >(m_factors + 1,
202 m_hashKey + (*m_factors) * i, m_hash);
203 }
204
205 private:
206 const size_t *m_factors;
207 size_t m_hashKey;
208 std::unordered_map< size_t, CellT > &m_hash;
209 };
210
211 template< class CellT >
212 class HashGridAccessor< CellT, 0 >
213 {
214 public:
215 HashGridAccessor(const size_t *, size_t hashKey,
216 std::unordered_map< size_t, CellT > &hash)
217 : m_hashKey(hashKey)
218 , m_hash(hash)
219 {}
220
221 CellT &operator=(const CellT &c)
222 {
223 return m_hash[m_hashKey] = c;
224 }
225
226 operator CellT &()
227 {
228 return m_hash[m_hashKey];
229 }
230
231 CellT &operator()()
232 {
233 return m_hash[m_hashKey];
234 }
235
236 CellT *operator->()
237 {
238 return &m_hash[m_hashKey];
239 }
240
241 private:
242 size_t m_hashKey;
243 std::unordered_map< size_t, CellT > &m_hash;
244 };
245
246 template< class CellT, unsigned int DimT >
247 class ConstHashGridAccessor
248 {
249 public:
250 ConstHashGridAccessor(const size_t *factors, size_t hashKey,
251 const std::unordered_map< size_t, CellT > &hash)
252 : m_factors(factors)
253 , m_hashKey(hashKey)
254 , m_hash(hash)
255 {}
256
257 ConstHashGridAccessor< CellT, DimT - 1 > operator[](size_t i)
258 {
259 return ConstHashGridAccessor< CellT, DimT - 1 >(m_factors + 1,
260 m_hashKey + (*m_factors) * i, m_hash);
261 }
262
263 private:
264 const size_t *m_factors;
265 size_t m_hashKey;
266 const std::unordered_map< size_t, CellT > &m_hash;
267 };
268
269 template< class CellT >
270 class ConstHashGridAccessor< CellT, 0 >
271 {
272 public:
273 ConstHashGridAccessor(const size_t *, size_t hashKey,
274 const std::unordered_map< size_t, CellT > &hash)
275 : m_hashKey(hashKey)
276 , m_hash(hash)
277 {}
278
279 operator const CellT *()
280 {
281 typename std::unordered_map< size_t, CellT >::const_iterator i =
282 m_hash.find(m_hashKey);
283 if(i != m_hash.end())
284 return &i->second;
285 return NULL;
286 }
287
288 private:
289 size_t m_hashKey;
290 const std::unordered_map< size_t, CellT > &m_hash;
291 };
292
293 template< class CellT, unsigned int DimT >
294 class HashGrid
295 {
296 public:
297 typedef typename std::unordered_map< size_t, CellT >::iterator iterator;
298 typedef typename std::unordered_map< size_t, CellT >::const_iterator
299 const_iterator;
300
301 HashGrid()
302 {
303 for(unsigned int i = 0; i < DimT; ++i)
304 m_factors[i] = 0;
305 }
306
307 void Clear()
308 {
309 m_hash.clear();
310 }
311
312 template< class ExtentT >
313 void Extent(const ExtentT &extent)
314 {
315 m_factors[DimT - 1] = 1;
316 for(unsigned int i = DimT - 1; i != 0; --i)
317 m_factors[i - 1] = m_factors[i] * extent[i];
318 }
319
320 HashGridAccessor< CellT, DimT - 1 > operator[](size_t i)
321 {
322 return HashGridAccessor< CellT, DimT >(m_factors, 0, m_hash)[i];
323 }
324
325 ConstHashGridAccessor< CellT, DimT - 1 > operator[](size_t i) const
326 {
327 return ConstHashGridAccessor< CellT, DimT >(m_factors, 0, m_hash)[i];
328 }
329
330 template< class IndexT >
331 CellT &operator[](const IndexT *index)
332 {
333 return m_hash[HashKey(index)];
334 }
335
336 CellT &at(size_t hashKey)
337 {
338 return m_hash[hashKey];
339 }
340
341 const CellT &at(size_t hashKey) const
342 {
343 return m_hash[hashKey];
344 }
345
346 template< class IndexT >
347 CellT *find(const IndexT &index)
348 {
349 iterator i = m_hash.find(HashKey(index));
350 if(i != m_hash.end())
351 return &i->second;
352 return NULL;
353 }
354
355 template< class IndexT >
356 const CellT *find(const IndexT &index) const
357 {
358 const_iterator i = m_hash.find(HashKey(index));
359 if(i != m_hash.end())
360 return &i->second;
361 return NULL;
362 }
363
364 iterator begin() { return m_hash.begin(); }
365 iterator end() { return m_hash.end(); }
366 const_iterator begin() const{ return m_hash.begin(); }
367 const_iterator end() const { return m_hash.end(); }
368 size_t size() const { return m_hash.size(); }
369
370 private:
371 template< class IndexT >
372 size_t HashKey(const IndexT &index) const
373 {
374 size_t hashKey = m_factors[0] * index[0];
375 for(unsigned int i = 1; i < DimT; ++i)
376 hashKey += m_factors[i] * index[i];
377 return hashKey;
378 }
379
380 private:
381 size_t m_factors[DimT];
382 std::unordered_map< size_t, CellT > m_hash;
383 };
384
385 #endif