-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAABB_Tri.cginc
394 lines (292 loc) · 7.81 KB
/
AABB_Tri.cginc
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/********************************************************/
/* AABB-triangle overlap test code */
/* by Tomas Akenine-Möller */
/* Function: int triBoxOverlap(float boxcenter[3], */
/* float boxhalfsize[3],float triverts[3][3]); */
/* History: */
/* 2001-03-05: released the code in its first version */
/* 2001-06-18: changed the order of the tests, faster */
/* */
/* Acknowledgement: Many thanks to Pierre Terdiman for */
/* suggestions and discussions on how to optimize code. */
/* Thanks to David Hunt for finding a ">="-bug! */
/********************************************************/
#define FINDMINMAX(x0,x1,x2,min,max) min = max = x0; if(x1<min) min=x1; if(x1>max) max=x1; if(x2<min) min=x2; if(x2>max) max=x2;
int planeBoxOverlap(float3 normal, float3 vert, float3 maxbox) // -NJMP-
{
int q;
float3 vmin, vmax;
float v;
v = vert.x;
if (normal.x > 0.0f)
{
vmin.x = -maxbox.x - v; // -NJMP-
vmax.x = maxbox.x - v; // -NJMP-
}
else
{
vmin.x = maxbox.x - v; // -NJMP-
vmax.x = -maxbox.x - v; // -NJMP-
}
v = vert.y;
if (normal.y > 0.0f)
{
vmin.y = -maxbox.y - v; // -NJMP-
vmax.y = maxbox.y - v; // -NJMP-
}
else
{
vmin.y = maxbox.y - v; // -NJMP-
vmax.y = -maxbox.y - v; // -NJMP-
}
v = vert.z;
if (normal.z > 0.0f)
{
vmin.z = -maxbox.z - v; // -NJMP-
vmax.z = maxbox.z - v; // -NJMP-
}
else
{
vmin.z = maxbox.z - v; // -NJMP-
vmax.z = -maxbox.z - v; // -NJMP-
}
if (dot(normal, vmin) > 0.0f)
return 0; // -NJMP-
if (dot(normal, vmax) >= 0.0f)
return 1; // -NJMP-
return 0;
}
int triBoxOverlap(float3 boxcenter, float3 boxhalfsize, float3 V0, float3 V1, float3 V2)
{
/* use separating axis theorem to test overlap between triangle and box */
/* need to test for overlap in these directions: */
/* 1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */
/* we do not even need to test these) */
/* 2) normal of the triangle */
/* 3) crossproduct(edge from tri, {x,y,z}-directin) */
/* this gives 3x3=9 more tests */
float3 v0, v1, v2;
// float axis[3];
float min, max, p0, p1, p2, rad; // -NJMP- "d" local variable removed
float3 normal, e0, e1, e2, fe;
float a, b, fa, fb;
/* This is the fastest branch on Sun */
/* move everything so that the boxcenter is in (0,0,0) */
v0 = V0 - boxcenter;
v1 = V1 - boxcenter;
v2 = V2 - boxcenter;
/* compute triangle edges */
e0 = v1 - v0; /* tri edge 0 */
e1 = v2 - v1; /* tri edge 1 */
e2 = v0 - v2; /* tri edge 2 */
/* Bullet 3: */
/* test the 9 tests first (this was faster) */
fe = abs(e0);
// AXISTEST_X01(e0.z, e0.y, fe.z, fe.y);
a = e0.z;
b = e0.y;
fa = fe.z;
fb = fe.y;
p0 = a * v0.y - b * v0.z;
p2 = a * v2.y - b * v2.z;
if (p0 < p2)
{
min = p0;
max = p2;
}
else
{
min = p2;
max = p0;
}
rad = fa * boxhalfsize.y + fb * boxhalfsize.z;
if (min > rad || max < -rad)
return 0;
// AXISTEST_Y02(e0.z, e0.x, fe.z, fe.x);
a = e0.z;
b = e0.x;
fa = fe.z;
fb = fe.x;
p0 = -a * v0.x + b * v0.z;
p2 = -a * v2.x + b * v2.z;
if (p0 < p2)
{
min = p0;
max = p2;
}
else
{
min = p2;
max = p0;
}
rad = fa * boxhalfsize.x + fb * boxhalfsize.z;
if (min > rad || max < -rad)
return 0;
// AXISTEST_Z12(e0.y, e0.x, fe.y, fe.x);
a = e0.y;
b = e0.x;
fa = fe.y;
fb = fe.x;
p1 = a * v1.x - b * v1.y;
p2 = a * v2.x - b * v2.y;
if (p2 < p1)
{
min = p2;
max = p1;
}
else
{
min = p1;
max = p2;
}
rad = fa * boxhalfsize.x + fb * boxhalfsize.y;
if (min > rad || max < -rad)
return 0;
fe = abs(e1);
// AXISTEST_X01(e1.z, e1.y, fe.z, fe.y);
a = e1.z;
b = e1.y;
fa = fe.z;
fb = fe.y;
p0 = a * v0.y - b * v0.z;
p2 = a * v2.y - b * v2.z;
if (p0 < p2)
{
min = p0;
max = p2;
}
else
{
min = p2;
max = p0;
}
rad = fa * boxhalfsize.y + fb * boxhalfsize.z;
if (min > rad || max < -rad)
return 0;
// AXISTEST_Y02(e1.z, e1.x, fe.z, fe.x);
a = e1.z;
b = e1.x;
fa = fe.z;
fb = fe.x;
p0 = -a * v0.x + b * v0.z;
p2 = -a * v2.x + b * v2.z;
if (p0 < p2)
{
min = p0;
max = p2;
}
else
{
min = p2;
max = p0;
}
rad = fa * boxhalfsize.x + fb * boxhalfsize.z;
if (min > rad || max < -rad)
return 0;
// AXISTEST_Z0(e1.y, e1.x, fe.y, fe.x);
a = e1.y;
b = e1.x;
fa = fe.y;
fb = fe.x;
p0 = a * v0.x - b * v0.y;
p1 = a * v1.x - b * v1.y;
if (p0 < p1)
{
min = p0;
max = p1;
}
else
{
min = p1;
max = p0;
}
rad = fa * boxhalfsize.x + fb * boxhalfsize.y;
if (min > rad || max < -rad)
return 0;
fe = abs(e2);
// AXISTEST_X2(e2.z, e2.y, fe.z, fe.y);
a = e2.z;
b = e2.y;
fa = fe.z;
fb = fe.y;
p0 = a * v0.y - b * v0.z;
p1 = a * v1.y - b * v1.z;
if (p0 < p1)
{
min = p0;
max = p1;
}
else
{
min = p1;
max = p0;
}
rad = fa * boxhalfsize.y + fb * boxhalfsize.z;
if (min > rad || max < -rad)
return 0;
// AXISTEST_Y1(e2.z, e2.x, fe.z, fe.x);
a = e2.z;
b = e2.x;
fa = fe.z;
fb = fe.x;
p0 = -a * v0.x + b * v0.z;
p1 = -a * v1.x + b * v1.z;
if (p0 < p1)
{
min = p0;
max = p1;
}
else
{
min = p1;
max = p0;
}
rad = fa * boxhalfsize.x + fb * boxhalfsize.z;
if (min > rad || max < -rad)
return 0;
// AXISTEST_Z12(e2.y, e2.x, fe.y, fe.x);
a = e2.y;
b = e2.x;
fa = fe.y;
fb = fe.x;
p1 = a * v1.x - b * v1.y;
p2 = a * v2.x - b * v2.y;
if (p2 < p1)
{
min = p2;
max = p1;
}
else
{
min = p1;
max = p2;
}
rad = fa * boxhalfsize.x + fb * boxhalfsize.y;
if (min > rad || max < -rad)
return 0;
/* Bullet 1: */
/* first test overlap in the {x,y,z}-directions */
/* find min, max of the triangle each direction, and test for overlap in */
/* that direction -- this is equivalent to testing a minimal AABB around */
/* the triangle against the AABB */
/* test in X-direction */
FINDMINMAX(v0.x,v1.x,v2.x,min,max);
if (min > boxhalfsize.x || max < -boxhalfsize.x)
return 0;
/* test in Y-direction */
FINDMINMAX(v0.y,v1.y,v2.y,min,max);
if (min > boxhalfsize.y || max < -boxhalfsize.y)
return 0;
/* test in Z-direction */
FINDMINMAX(v0.z,v1.z,v2.z,min,max);
if (min > boxhalfsize.z || max < -boxhalfsize.z)
return 0;
/* Bullet 2: */
/* test if the box intersects the plane of the triangle */
/* compute plane equation of triangle: normal*x+d=0 */
normal = cross(e0, e1);
// -NJMP- (line removed here)
if (!planeBoxOverlap(normal, v0, boxhalfsize))
return 0; // -NJMP-
return 1; /* box and triangle overlaps */
}