-
Notifications
You must be signed in to change notification settings - Fork 74
/
overlapSphereBox.h
369 lines (311 loc) · 10.8 KB
/
overlapSphereBox.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
367
368
369
/*****************************************************************/
/* "On Faster Sphere-Box Overlap Testing" by */
/* Thomas Larsson, Tomas Akenine-Moller and Eric Lengyel. */
/* */
/* Accompanying source code */
/* */
/* Sphere-AABB and Sphere-OBB overlap tests code */
/* */
/* */
/* Functions: */
/* */
/* int overlapSphereAABB_Arvo(Sphere3D & sphere, Box3D & box); */
/* int overlapSphereAABB_QRI(Sphere3D & sphere, Box3D & box); */
/* int overlapSphereAABB_QRF(Sphere3D & sphere, Box3D & box); */
/* int overlapSphereAABB_Cons(Sphere3D & sphere, Box3D & box); */
/* int overlapSphereAABB_SSE(Sphere3D & sphere, Box3D & box); */
/* */
/* int overlapSphereOBB_G_Arvo(Sphere3D & sphere, OBox3D & obox);*/
/* int overlapSphereOBB_QRI(Sphere3D & sphere, OBox3D & obox); */
/* int overlapSphereOBB_QRF(Sphere3D & sphere, OBox3D & obox); */
/* int overlapSphereOBB_Cons(Sphere3D & sphere, OBox3D & obox); */
/* int overlapSphereOBB_SSE(Sphere3D & sphere, OBox3D & obox); */
/* */
/* History: */
/* 2005-12-05: First version of source code created */
/* 2006-05-08: Added SSE versions of the overlap tests */
/* */
/*****************************************************************/
#include <xmmintrin.h>
typedef struct Point3D {
float x, y, z;
} Point3D;
typedef struct Sphere3D {
__declspec(align(16)) Point3D c;
float r;
} Sphere3D;
typedef struct Box3D {
__declspec(align(16)) Point3D min;
__declspec(align(16)) Point3D max;
} Box3D;
typedef struct OBox3D {
__declspec(align(16)) Point3D mid;
__declspec(align(16)) Point3D ext;
__declspec(align(16)) Point3D xaxis;
__declspec(align(16)) Point3D yaxis;
__declspec(align(16)) Point3D zaxis;
} OBox3D;
inline int overlapSphereAABB_Arvo(const Sphere3D & sphere, const Box3D & box) {
float dmin = 0;
float e;
if(sphere.c.x < box.min.x) {
e = sphere.c.x - box.min.x;
dmin += e * e;
} else if( sphere.c.x > box.max.x) {
e = sphere.c.x - box.max.x;
dmin += e * e;
}
if(sphere.c.y < box.min.y) {
e = sphere.c.y - box.min.y;
dmin += e * e;
} else if( sphere.c.y > box.max.y) {
e = sphere.c.y - box.max.y;
dmin += e * e;
}
if(sphere.c.z < box.min.z) {
e = sphere.c.z - box.min.z;
dmin += e * e;
} else if( sphere.c.z > box.max.z) {
e = sphere.c.z - box.max.z;
dmin += e * e;
}
if( dmin <= sphere.r * sphere.r ) return 1;
return 0;
}
inline int overlapSphereAABB_QRI(const Sphere3D & sphere, const Box3D & box) {
float dmin = 0;
float e;
if((e = sphere.c.x - box.min.x) < 0) {
if (e < -sphere.r) return 0;
dmin += e * e;
} else if((e = sphere.c.x - box.max.x) > 0) {
if (e > sphere.r) return 0;
dmin += e * e;
}
if((e = sphere.c.y - box.min.y) < 0) {
if (e < -sphere.r) return 0;
dmin += e * e;
} else if((e = sphere.c.y - box.max.y) > 0) {
if (e > sphere.r) return 0;
dmin += e * e;
}
if((e = sphere.c.z - box.min.z) < 0) {
if (e < -sphere.r) return 0;
dmin += e * e;
} else if((e = sphere.c.z - box.max.z) > 0) {
if (e > sphere.r) return 0;
dmin += e * e;
}
if( dmin <= sphere.r * sphere.r ) return 1;
return 0;
}
inline int overlapSphereAABB_QRF(const Sphere3D & sphere, const Box3D & box) {
float dmin = 0;
float e;
if (sphere.c.x < box.min.x - sphere.r || sphere.c.x > box.max.x + sphere.r) return 0;
if (sphere.c.y < box.min.y - sphere.r || sphere.c.y > box.max.y + sphere.r) return 0;
if (sphere.c.z < box.min.z - sphere.r || sphere.c.z > box.max.z + sphere.r) return 0;
if(sphere.c.x < box.min.x) {
e = sphere.c.x - box.min.x;
dmin += e * e;
} else if( sphere.c.x > box.max.x) {
e = sphere.c.x - box.max.x;
dmin += e * e;
}
if(sphere.c.y < box.min.y) {
e = sphere.c.y - box.min.y;
dmin += e * e;
} else if( sphere.c.y > box.max.y) {
e = sphere.c.y - box.max.y;
dmin += e * e;
}
if(sphere.c.z < box.min.z) {
e = sphere.c.z - box.min.z;
dmin += e * e;
} else if( sphere.c.z > box.max.z) {
e = sphere.c.z - box.max.z;
dmin += e * e;
}
if( dmin <= sphere.r * sphere.r ) return 1;
return 0;
}
inline int overlapSphereAABB_Cons(const Sphere3D & sphere, const Box3D & box) {
if (sphere.c.x < box.min.x - sphere.r || sphere.c.x > box.max.x + sphere.r) return 0;
if (sphere.c.y < box.min.y - sphere.r || sphere.c.y > box.max.y + sphere.r) return 0;
if (sphere.c.z < box.min.z - sphere.r || sphere.c.z > box.max.z + sphere.r) return 0;
return 1;
}
static bool overlapSphereAABB_SSE(const Sphere3D& sphere, const Box3D& box)
{
/*float e = MaxZero(box.min.x - sphere.c.x) + MaxZero(sphere.c.x - box.max.x);
float dmin = e * e;
e = MaxZero(box.min.y - sphere.c.y) + MaxZero(sphere.c.y - box.max.y);
dmin += e * e;
e = MaxZero(box.min.z - sphere.c.z) + MaxZero(sphere.c.z - box.max.z);
dmin += e * e;
float r = sphere.r;
return (dmin <= r * r);*/
__m128 zero = _mm_setzero_ps();
__m128 center = *(__m128 *) &sphere.c;
__m128 boxmin = *(__m128 *) &box.min;
__m128 boxmax = *(__m128 *) &box.max;
__m128 e = _mm_add_ps(_mm_max_ps(_mm_sub_ps(boxmin, center), zero), _mm_max_ps(_mm_sub_ps(center, boxmax), zero));
e = _mm_mul_ps(e, e);
const Point3D *p = (Point3D *) &e;
float r = sphere.r;
return (p->x + p->y + p->z <= r * r);
}
inline int overlapSphereOBB_G_Arvo(const Sphere3D & sphere, const OBox3D & obox) {
float d, e, dmin = 0;
Point3D v;
v.x = sphere.c.x - obox.mid.x;
v.y = sphere.c.y - obox.mid.y;
v.z = sphere.c.z - obox.mid.z;
d = v.x * obox.xaxis.x + v.y * obox.xaxis.y + v.z * obox.xaxis.z;
if(d < -obox.ext.x) {
e = d + obox.ext.x;
dmin += e * e;
} else if(d > obox.ext.x) {
e = d - obox.ext.x;
dmin += e * e;
}
d = v.x * obox.yaxis.x + v.y * obox.yaxis.y + v.z * obox.yaxis.z;
if(d < -obox.ext.y) {
e = d + obox.ext.y;
dmin += e * e;
} else if(d > obox.ext.y) {
e = d - obox.ext.y;
dmin += e * e;
}
d = v.x * obox.zaxis.x + v.y * obox.zaxis.y + v.z * obox.zaxis.z;
if(d < -obox.ext.z) {
e = d + obox.ext.z;
dmin += e * e;
} else if(d > obox.ext.z) {
e = d - obox.ext.z;
dmin += e * e;
}
if( dmin <= sphere.r * sphere.r ) return 1;
return 0;
}
inline int overlapSphereOBB_QRI(const Sphere3D & sphere, const OBox3D & obox) {
float d, e, dmin = 0;
Point3D v;
v.x = sphere.c.x - obox.mid.x;
v.y = sphere.c.y - obox.mid.y;
v.z = sphere.c.z - obox.mid.z;
d = v.x * obox.xaxis.x + v.y * obox.xaxis.y + v.z * obox.xaxis.z;
if((e = d + obox.ext.x) < 0) {
if (-e > sphere.r) return 0;
dmin += e * e;
} else if((e = d - obox.ext.x) > 0) {
if (e > sphere.r) return 0;
dmin += e * e;
}
d = v.x * obox.yaxis.x + v.y * obox.yaxis.y + v.z * obox.yaxis.z;
if((e = d + obox.ext.y) < 0) {
if (-e > sphere.r) return 0;
dmin += e * e;
} else if((e = d - obox.ext.y) > 0) {
if (e > sphere.r) return 0;
dmin += e * e;
}
d = v.x * obox.zaxis.x + v.y * obox.zaxis.y + v.z * obox.zaxis.z;
if((e = d + obox.ext.z) < 0) {
if (-e > sphere.r) return 0;
dmin += e * e;
} else if((e = d - obox.ext.z) > 0) {
if (e > sphere.r) return 0;
dmin += e * e;
}
if( dmin <= sphere.r * sphere.r ) return 1;
return 0;
}
inline int overlapSphereOBB_QRF(const Sphere3D & sphere, const OBox3D & obox) {
float dx, dy, dz, e, dmin = 0;
Point3D v;
v.x = sphere.c.x - obox.mid.x;
v.y = sphere.c.y - obox.mid.y;
v.z = sphere.c.z - obox.mid.z;
dx = v.x * obox.xaxis.x + v.y * obox.xaxis.y + v.z * obox.xaxis.z;
if (dx < - obox.ext.x - sphere.r || dx > obox.ext.x + sphere.r) return 0;
dy = v.x * obox.yaxis.x + v.y * obox.yaxis.y + v.z * obox.yaxis.z;
if (dy < - obox.ext.y - sphere.r || dy > obox.ext.y + sphere.r) return 0;
dz = v.x * obox.zaxis.x + v.y * obox.zaxis.y + v.z * obox.zaxis.z;
if (dz < - obox.ext.z - sphere.r || dz > obox.ext.z + sphere.r) return 0;
if(dx < -obox.ext.x) {
e = dx + obox.ext.x;
dmin += e * e;
} else if(dx > obox.ext.x) {
e = dx - obox.ext.x;
dmin += e * e;
}
if(dy < -obox.ext.y) {
e = dy + obox.ext.y;
dmin += e * e;
} else if(dy > obox.ext.y) {
e = dy - obox.ext.y;
dmin += e * e;
}
if(dz < -obox.ext.z) {
e = dz + obox.ext.z;
dmin += e * e;
} else if(dz > obox.ext.z) {
e = dz - obox.ext.z;
dmin += e * e;
}
if( dmin <= sphere.r * sphere.r ) return 1;
return 0;
}
inline int overlapSphereOBB_Cons(const Sphere3D & sphere, const OBox3D & obox) {
float dx, dy, dz;
Point3D v;
v.x = sphere.c.x - obox.mid.x;
v.y = sphere.c.y - obox.mid.y;
v.z = sphere.c.z - obox.mid.z;
dx = v.x * obox.xaxis.x + v.y * obox.xaxis.y + v.z * obox.xaxis.z;
if (dx < - obox.ext.x - sphere.r || dx > obox.ext.x + sphere.r) return 0;
dy = v.x * obox.yaxis.x + v.y * obox.yaxis.y + v.z * obox.yaxis.z;
if (dy < - obox.ext.y - sphere.r || dy > obox.ext.y + sphere.r) return 0;
dz = v.x * obox.zaxis.x + v.y * obox.zaxis.y + v.z * obox.zaxis.z;
if (dz < - obox.ext.z - sphere.r || dz > obox.ext.z + sphere.r) return 0;
return 1;
}
static bool overlapSphereOBB_SSE(const Sphere3D& sphere, const OBox3D& box)
{
__m128 zero = _mm_setzero_ps();
__m128 center = *(__m128 *) &sphere.c;
__m128 ext = *(__m128 *) &box.ext;
__m128 mid = *(__m128 *) &box.mid;
__m128 xaxis = *(__m128 *) &box.xaxis;
__m128 yaxis = *(__m128 *) &box.yaxis;
__m128 zaxis = *(__m128 *) &box.zaxis;
__m128 v = _mm_sub_ps(center, mid);
__m128 xmul = _mm_mul_ps(v, xaxis);
__m128 ymul = _mm_mul_ps(v, yaxis);
__m128 zmul = _mm_mul_ps(v, zaxis);
// Sequential adds
/*
__m128 d;
d.m128_f32[0] = xmul.m128_f32[0] + xmul.m128_f32[1] + xmul.m128_f32[2];
d.m128_f32[1] = ymul.m128_f32[0] + ymul.m128_f32[1] + ymul.m128_f32[2];
d.m128_f32[2] = zmul.m128_f32[0] + zmul.m128_f32[1] + zmul.m128_f32[2];
*/
// Re-arrange elements so we can use parallell adds
__m128 d, d1, d2;
__m128 tmp0, tmp2, tmp1, tmp3;
tmp0 = _mm_shuffle_ps(xmul, ymul, 0x44);
tmp2 = _mm_shuffle_ps(xmul, ymul, 0xEE);
tmp1 = _mm_shuffle_ps(zmul, zero, 0x44);
tmp3 = _mm_shuffle_ps(zmul, zero, 0xEE);
d = _mm_shuffle_ps(tmp0, tmp1, 0x88);
d1 = _mm_shuffle_ps(tmp0, tmp1, 0xDD);
d2 = _mm_shuffle_ps(tmp2, tmp3, 0x88);
d = _mm_add_ps(d, d1);
d = _mm_add_ps(d, d2);
__m128 e = _mm_add_ps(_mm_min_ps(_mm_add_ps(d, ext), zero), _mm_max_ps(_mm_sub_ps(d, ext), zero));
e = _mm_mul_ps(e, e);
const Point3D *p = (Point3D *) &e;
float r = sphere.r;
return (p->x + p->y + p->z <= r * r);
}