-
Notifications
You must be signed in to change notification settings - Fork 6
/
SpacialHash.h
366 lines (305 loc) · 10.9 KB
/
SpacialHash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//
// SpacialHash.h - fast spacial hash for nearest-neighbor type queries
//
// Copyright (c) 2013-2016 Arthur Danskin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#ifndef SPACIALHASH_H
#define SPACIALHASH_H
template <typename T>
class spatial_hash {
public:
struct key_type {
float2 pos;
float radius;
mutable uint query;
key_type(float2 p, float r) : pos(p), radius(r), query(0) {}
};
typedef T mapped_type;
typedef std::pair<key_type, mapped_type> value_type;
private:
//typedef std::vector< uint > bucket_type;
typedef std::basic_string<uint> bucket_type;
std::vector<value_type> m_elements; // store actual contents
std::vector<bucket_type> m_cells; // array of grid cells
float m_cell_size; // size of each grid cell (width == height)
float m_icell_size; // inverse of m_cell_size
uint m_width; // sqrt(m_cells.size())
mutable uint m_currentQuery;
// return x, y grid cell for position
int2 scale(float2 p) const
{
return int2(floor_int(p.x * m_icell_size), floor_int(p.y * m_icell_size));
}
// return grid index for x, y
uint hash(int2 p) const
{
return (p.y * m_width + p.x) % m_cells.size();
}
bool acceptElement() const
{
return (m_cell_size > 0);
}
public:
size_t getSizeof() const
{
size_t sz = sizeof(*this);
sz += SIZEOF_VEC(m_elements);
sz += SIZEOF_VEC(m_cells);
return sz;
}
const std::vector<value_type> &getElements() const { return m_elements; }
// change size of hash
void reset(float cell_size, uint cells)
{
clear();
m_cells.resize(cells);
m_width = std::floor(std::sqrt((float)m_cells.size()));
m_cell_size = cell_size;
m_icell_size = 1.f / cell_size;
}
// remove all elements from hash
void clear()
{
if (m_elements.size())
{
foreach (bucket_type& el, m_cells)
el.clear();
m_elements.clear();
}
m_currentQuery = 0;
}
void shrink_to_fit()
{
m_elements.shrink_to_fit();
m_cells.shrink_to_fit();
for_ (el, m_cells)
el.shrink_to_fit();
}
int width() const { return m_width; }
float cell_size() const { return m_cell_size; }
int cell_count() const { return m_cells.size(); }
int elements() const { return m_elements.size(); }
spatial_hash(float cell_size, uint cells) { reset(cell_size, cells); }
spatial_hash() : m_cell_size(0) { }
void insertPoint(float2 p, const T& v)
{
ASSERT(acceptElement());
if (!acceptElement())
return;
const int cell = hash(scale(p));
m_elements.push_back(make_pair(key_type(p, 0.f), v));
m_cells[cell].push_back(m_elements.size()-1);
}
void insertCircle(float2 p, float r, const T& v)
{
ASSERT(acceptElement());
if (!acceptElement())
return;
const int2 s = scale(p - float2(r));
const int2 e = scale(p + float2(r));
m_elements.push_back(make_pair(key_type(p, r), v));
for (int x=s.x; x<=e.x; x++)
{
for (int y=s.y; y<=e.y; y++)
{
// FIXME this is really inserting a square
const uint bi = hash(int2(x, y));
bucket_type &bu = m_cells[bi];
bu.push_back(m_elements.size()-1);
}
}
}
template <typename Fun>
bool intersectPointEach(float2 p, const Fun& fun) const
{
ASSERT(m_cell_size > 0.f);
if (m_elements.empty())
return 0;
const int2 coord = scale(p);
const uint cell = hash(coord);
const bucket_type &bucket = m_cells[cell];
bool foundAny = false;
m_currentQuery++;
foreach (const uint idx, bucket)
{
const value_type &el = m_elements[idx];
if (el.first.query != m_currentQuery &&
intersectPointCircle(p, el.first.pos, el.first.radius))
{
el.first.query = m_currentQuery;
foundAny = true;
if (fun(el))
return true;
}
}
return foundAny;
}
template <typename Fun>
bool intersectCircleEach(float2 p, float r, const Fun& fun) const
{
ASSERT(m_cell_size > 0.f);
if (m_elements.empty())
return 0;
const int2 s = scale(p - float2(r));
const int2 e = scale(p + float2(r));
bool foundAny = false;
// if we are going to search the whole table, might as well do it efficiently...
const size_t cellsToSearch = (e.x - s.x) * (e.y - s.y);
if (cellsToSearch >= m_cells.size())
{
foreach (const value_type &el, m_elements) {
if (intersectCircleCircle(el.first.pos, el.first.radius, p, r))
{
foundAny = true;
if (fun(el))
return true;
}
}
return foundAny;
}
const uint query = ++m_currentQuery;
for (int x=s.x; x<=e.x; x++) {
for (int y=s.y; y<=e.y; y++)
{
const uint cell = hash(int2(x, y));
const bucket_type &bucket = m_cells[cell];
foreach (const uint idx, bucket)
{
const value_type& el = m_elements[idx];
if (el.first.query != query &&
intersectCircleCircle(el.first.pos, el.first.radius, p, r))
{
foundAny = true;
el.first.query = query;
if (fun(el))
return true;
}
}
}
}
return foundAny;
}
template <typename Fun>
bool intersectRectangleEach(float2 p, float2 r, const Fun& fun) const
{
ASSERT(m_cell_size > 0.f);
if (m_elements.empty())
return 0;
const int2 s = scale(p - r);
const int2 e = scale(p + r);
bool foundAny = false;
// if we are going to search the whole table, might as well do it efficiently...
const size_t cellsToSearch = (e.x - s.x) * (e.y - s.y);
if (cellsToSearch >= m_cells.size())
{
foreach (const value_type &el, m_elements) {
if (intersectCircleRectangle(el.first.pos, el.first.radius, p, r))
{
foundAny = true;
if (fun(el))
return true;
}
}
return foundAny;
}
const uint query = ++m_currentQuery;
for (int x=s.x; x<=e.x; x++) {
for (int y=s.y; y<=e.y; y++)
{
const uint cell = hash(int2(x, y));
const bucket_type &bucket = m_cells[cell];
foreach (const uint idx, bucket)
{
const value_type &el = m_elements[idx];
if (el.first.query != query &&
intersectCircleRectangle(el.first.pos, el.first.radius, p, r))
{
el.first.query = query;
foundAny = true;
if (fun(el))
return true;
}
}
}
}
return foundAny;
}
template <typename Fun>
bool each(const Fun& fun) const
{
ASSERT(m_cell_size > 0.f);
if (m_elements.size() == 0)
return false;
foreach (const value_type &el, m_elements) {
if (fun(el))
return true;
}
return true;
}
// add all elements within the input circle to the input vector
// return count of items found
int intersectCircle(vector<T>* output, float2 p, float r) const
{
int count = 0;
intersectCircleEach(p, r, [&](const value_type &val)
{
output->push_back(const_cast<T&>(val.second));
count++;
return false;
});
return count;
}
struct QueryNearest {
const float2 center;
mutable float nearestDist = std::numeric_limits<float>::max();
mutable const value_type *nearestElt;
QueryNearest(const value_type *def) : center(def->first.pos), nearestElt(def) {}
bool operator()(const value_type& el) const
{
const float distSqr = distanceSqr(el.first.pos, center);
if (distSqr < squared(nearestDist + el.first.radius))
{
nearestDist = std::sqrt(distSqr) - el.first.radius;
nearestElt = ⪙
}
return true;
}
};
// return item nearest to p within radius r
value_type intersectCircleNearest(float2 p, float r, const T& def=T()) const
{
value_type qval = make_pair(key_type(p, r), def);
QueryNearest query(&qval);
intersectCircleEach(p, r, query);
return *query.nearestElt;
}
value_type intersectPointNearest(float2 p, const T& def=T()) const
{
value_type qval = make_pair(key_type(p, 0.f), def);
QueryNearest query(&qval);
intersectPointEach(p, query);
return *query.nearestElt;
}
bool intersectCircle(float2 p, float r) const
{
return intersectCircleEach(p, r, [&](const value_type& el) { return false; });
}
};
#endif // SPACIALHASH_H