/
isect2d.h
326 lines (264 loc) · 8.68 KB
/
isect2d.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
#pragma once
#include <cmath>
#include <set>
#include <unordered_set>
#include <vector>
#include <array>
#include <functional> // for hash function
#include <algorithm> // for std::max
#include "aabb.h"
#include "obb.h"
namespace std {
template <>
struct hash<pair<int32_t, int32_t>> {
size_t operator()(const pair<int32_t,int32_t>& k) const {
return hash<int64_t>()(int64_t(k.first) << 32 | k.second);
}
};
}
namespace isect2d {
template<typename T>
static constexpr const T clamp(T val, T min, T max) {
return val < min ? min
: val > max ? max : val;
}
template<typename V>
struct ISect2D {
using i32 = int_fast32_t;
struct Pair {
Pair(int _a, int _b, int _next) : first(_a), second(_b), next(_next) {}
int first;
int second;
int next;
};
i32 split_x = 0;
i32 split_y = 0;
i32 res_x = 0;
i32 res_y = 0;
i32 xpad = 0;
i32 ypad = 0;
std::vector<std::vector<int32_t>> gridAABBs;
std::vector<Pair> pairs;
std::vector<int> pairMap;
std::vector<AABB<V>> aabbs;
ISect2D(size_t collisionHashSize = 2048) {
pairMap.assign(collisionHashSize, -1);
}
void resize(const V _split, const V _resolution) {
split_x = _split.x;
split_y = _split.y;
res_x = _resolution.x;
res_y = _resolution.y;
// Ensure no division by zero
split_x = std::max(split_x, static_cast<i32>(1));
split_y = std::max(split_y, static_cast<i32>(1));
res_x = std::max(res_x, split_x);
res_y = std::max(res_y, split_y);
xpad = res_x / split_x;
ypad = res_y / split_y;
gridAABBs.resize(split_x * split_y);
}
void clear() {
pairs.clear();
pairMap.assign(pairMap.size(), -1);
aabbs.clear();
for (auto& grid : gridAABBs){
grid.clear();
}
}
void intersect(const AABB<V>& _aabb,
std::function<bool(const AABB<V>& _aabb, const AABB<V>& _other)> _cb,
bool _insert = true) {
i32 x1 = _aabb.min.x / xpad;
i32 y1 = _aabb.min.y / ypad;
i32 x2 = _aabb.max.x / xpad + 1;
i32 y2 = _aabb.max.y / ypad + 1;
x1 = clamp(x1, i32(0), split_x-1);
y1 = clamp(y1, i32(0), split_y-1);
x2 = clamp(x2, i32(1), split_x);
y2 = clamp(y2, i32(1), split_y);
for (i32 y = y1; y < y2; y++) {
for (i32 x = x1; x < x2; x++) {
auto& v = gridAABBs[x + y * split_x];
for (int32_t i : v) {
const auto& other = aabbs[i];
if (_aabb.intersect(other)) {
if (!_cb(_aabb, other)) {
return;
}
}
}
}
}
if (_insert) {
aabbs.push_back(_aabb);
int index = aabbs.size() - 1;
for (i32 y = y1; y < y2; y++) {
for (i32 x = x1; x < x2; x++) {
gridAABBs[x + y * split_x].push_back(index);
}
}
}
}
void insert(const AABB<V>& _aabb) {
i32 x1 = _aabb.min.x / xpad;
i32 y1 = _aabb.min.y / ypad;
i32 x2 = _aabb.max.x / xpad + 1;
i32 y2 = _aabb.max.y / ypad + 1;
x1 = clamp(x1, i32(0), split_x-1);
y1 = clamp(y1, i32(0), split_y-1);
x2 = clamp(x2, i32(1), split_x);
y2 = clamp(y2, i32(1), split_y);
aabbs.push_back(_aabb);
int index = aabbs.size() - 1;
for (i32 y = y1; y < y2; y++) {
for (i32 x = x1; x < x2; x++) {
gridAABBs[x + y * split_x].push_back(index);
}
}
}
/*
* Performs broadphase collision detection on _aabbs dividing the
* screen size _resolution by _split on X and Y dimension Returns the
* set of colliding pairs in the _aabbs container
*/
void intersect(const std::vector<AABB<V>>& _aabbs) {
clear();
size_t index = 0;
for (const auto& aabb : _aabbs) {
i32 x1 = aabb.min.x / xpad;
i32 y1 = aabb.min.y / ypad;
i32 x2 = aabb.max.x / xpad + 1;
i32 y2 = aabb.max.y / ypad + 1;
x1 = clamp(x1, i32(0), split_x-1);
y1 = clamp(y1, i32(0), split_y-1);
x2 = clamp(x2, i32(1), split_x);
y2 = clamp(y2, i32(1), split_y);
for (i32 y = y1; y < y2; y++) {
for (i32 x = x1; x < x2; x++) {
gridAABBs[x + y * split_x].push_back(index);
}
}
index++;
}
for (auto& v : gridAABBs) {
if (v.empty()) { continue; }
// check all items against each other
for (size_t j = 0; j < v.size()-1; ++j) {
const auto& a(_aabbs[v[j]]);
for (size_t k = j + 1; k < v.size(); ++k) {
const auto& b(_aabbs[v[k]]);
if (a.intersect(b)) {
size_t key = hash_key(hash_int(v[j])<<32 | hash_int(v[k]));
key &= (pairMap.size()-1);
int i = pairMap[key];
while (i != -1) {
if (pairs[i].first == v[j] && pairs[i].second == v[k]) {
// found
break;
}
i = pairs[i].next;
}
if (i == -1) {
pairs.push_back(Pair{v[j], v[k], pairMap[key]});
pairMap[key] = pairs.size()-1;
}
}
}
}
v.clear();
}
}
private:
// from fontstash
static uint64_t hash_int(uint32_t a) {
a += ~(a<<15);
a ^= (a>>10);
a += (a<<3);
a ^= (a>>6);
a += ~(a<<11);
a ^= (a>>16);
return a;
}
// from https://gist.github.com/badboy/6267743
// - 64 bit to 32 bit Hash Functions
static uint32_t hash_key(uint64_t key) {
key = (~key) + (key << 18);
key = key ^ (key >> 31);
key = key * 21;
key = key ^ (key >> 11);
key = key + (key << 6);
key = key ^ (key >> 22);
return key;
}
};
/*
* Performs broadphase collision detection on _aabbs dividing the
* screen size _resolution by _split on X and Y dimension Returns the
* set of colliding pairs in the _aabbs container
*
* NB: Likely to be slower than ISect2D::intersect() !
*/
template<typename V>
static std::unordered_set<std::pair<int, int>> intersect(const std::vector<AABB<V>>& _aabbs,
V _split, V _resolution) {
struct AABBPair {
const AABB<V>* aabb;
unsigned int index;
};
std::unordered_set<std::pair<int, int>> pairs;
int n = int(_split.x * _split.y);
std::vector<AABBPair>* gridAABBs = new std::vector<AABBPair>[n];
const short xpad = short(ceilf(_resolution.x / _split.x));
const short ypad = short(ceilf(_resolution.y / _split.y));
short x = 0, y = 0;
for (int j = 0; j < _split.y; ++j) {
for (int i = 0; i < _split.x; ++i) {
AABB<V> cell(x, y, x + xpad, y + ypad);
for (unsigned int index = 0; index < _aabbs.size(); ++index) {
const AABB<V>* aabb = &_aabbs[index];
// test the aabb against the current grid cell
if (cell.intersect(*aabb)) {
gridAABBs[int(i + j * _split.x)].push_back({aabb, index});
}
}
x += xpad;
if (x >= _resolution.x) {
x = 0;
y += ypad;
}
}
}
for (int i = 0; i < n; ++i) {
auto& v = gridAABBs[i];
for (size_t j = 0; j < v.size(); ++j) {
for (size_t k = j + 1; k < v.size(); ++k) {
if (v[j].index != v[k].index && v[j].aabb->intersect(*v[k].aabb)) {
pairs.insert({ v[j].index, v[k].index });
}
}
}
}
delete[] gridAABBs;
return pairs;
}
/*
* Performs bruteforce broadphase collision detection on _aabbs
* Returns the set of colliding pairs in the _aabbs container
*/
template<typename V>
static std::unordered_set<std::pair<int, int>> intersect(const std::vector<AABB<V>>& _aabbs) {
std::unordered_set<std::pair<int, int>> pairs;
if (_aabbs.size() == 0) {
return pairs;
}
for (size_t i = 0; i < _aabbs.size(); ++i) {
for (size_t j = i + 1; j < _aabbs.size(); ++j) {
if (_aabbs[i].intersect(_aabbs[j])) {
pairs.insert({ i, j });
}
}
}
return pairs;
}
}