-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeom_bvh.c
479 lines (444 loc) · 14 KB
/
geom_bvh.c
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include <Cgeom/geom_bvh.h>
#include <stdlib.h>
//#define BVH_DEBUG
#ifdef BVH_DEBUG
# include <stdio.h>
# define BVHDBG(...) fprintf(stderr, __VA_ARGS__)
#else
# define BVHDBG(...)
#endif
// The internal representation of boxes is min/max instead of center/halfwidth
struct geom_bvh2d_struct{
geom_bvh2d child[4];
double b[4]; // x-min, x-max, y-min, y-max
int tag; // for internal nodes, set to max of all subnodes
};
struct geom_bvh3d_struct{
geom_bvh3d child[8];
double b[6]; // x-min, x-max, y-min, y-max, z-min, z-max
int tag;
};
static int isqrt_ceil(int u){
if(u <= 0){ return 0; }
int a = 2;
int b = u/a;
while(((a-b) > 1) || ((b-a) > 1)){
a = (a+b)/2;
b = u/a;
}
a = (a<b)?a:b;
return (a*a == u) ? a : a+1;
}
static int icbrt_ceil(int x){
const int xsave = x;
int s = 30;
int y = 0, b;
// the 3 add's of 1 can be replaced by or's of 1 since the arguments are always even
while(s >= 0){ // Do 11 times.
y *= 2;
//b = (3*y*(y + 1) + 1) << s;
b = ((3*y*(y | 1)) | 1) << s;
s -= 3;
if(x >= b){
x -= b;
y |= 1; //y++;
}
}
return (y*y*y == xsave) ? y : y+1;
}
static void sort_bvh2(geom_bvh2d *B, int beg, int end, int d){
if(end > beg+1){
geom_bvh2d t, piv = B[beg];
int l = beg+1, r = end;
while(l < r){
if(B[l]->b[2*d+0]+B[l]->b[2*d+1] <= piv->b[2*d+0]+piv->b[2*d+1]){
++l;
}else{
--r;
t = B[l];
B[l] = B[r];
B[r] = t;
}
}
--l;
t = B[l];
B[l] = B[beg];
B[beg] = t;
sort_bvh2(B, beg, l, d);
sort_bvh2(B, r, end, d);
}
}
static void sort_bvh3(geom_bvh3d *B, int beg, int end, int d){
if(end > beg+1){
geom_bvh3d t, piv = B[beg];
int l = beg+1, r = end;
while(l < r){
if(B[l]->b[2*d+0]+B[l]->b[2*d+1] <= piv->b[2*d+0]+piv->b[2*d+1]){
++l;
}else{
--r;
t = B[l];
B[l] = B[r];
B[r] = t;
}
}
--l;
t = B[l];
B[l] = B[beg];
B[beg] = t;
sort_bvh3(B, beg, l, d);
sort_bvh3(B, r, end, d);
}
}
static geom_bvh2d geom_bvh2d_make_blank(){
geom_bvh2d b = (geom_bvh2d)malloc(sizeof(struct geom_bvh2d_struct));
int j;
for(j = 0; j < 4; ++j){
b->child[j] = NULL;
}
b->tag = 0;
b->b[0] = 0;
b->b[1] = 0;
b->b[2] = 0;
b->b[3] = 0;
return b;
}
static geom_bvh3d geom_bvh3d_make_blank(){
geom_bvh3d b = (geom_bvh3d)malloc(sizeof(struct geom_bvh3d_struct));
int j;
for(j = 0; j < 8; ++j){
b->child[j] = NULL;
}
b->tag = 0;
b->b[0] = 0;
b->b[1] = 0;
b->b[2] = 0;
b->b[3] = 0;
b->b[4] = 0;
b->b[5] = 0;
return b;
}
static void geom_bvh2d_STR(unsigned int *n_, geom_bvh2d *B){
int i, j, k;
geom_bvh2d *T = B;
const int n = *n_;
const int P = (n+3)/4;
const int S = isqrt_ceil(P);
*n_ = 0;
// Sort rectangles by first coordinate
sort_bvh2(B, 0, n, 0);
// Partition into S slices and sort each slice by second coordinate
i = n;
while(i > 0){
int slice_size = 4*S;
if(slice_size > i){ slice_size = i; }
BVHDBG(" Slice size = %d\n", slice_size);
sort_bvh2(T, 0, slice_size, 1);
// Now pack all the nodes into runs of length 4
j = slice_size;
while(j > 0){
int pack_size = 4;
if(pack_size > j){ pack_size = j; }
// Make a new node
geom_bvh2d b = geom_bvh2d_make_blank();
b->child[0] = T[0];
b->tag = T[0]->tag;
b->b[0] = T[0]->b[0];
b->b[1] = T[0]->b[1];
b->b[2] = T[0]->b[2];
b->b[3] = T[0]->b[3];
for(k = 1; k < pack_size; ++k){
b->child[k] = T[k];
if(T[k]->tag > b->tag){ b->tag = T[k]->tag; }
if(T[k]->b[0] < b->b[0]){ b->b[0] = T[k]->b[0]; }
if(T[k]->b[1] > b->b[1]){ b->b[1] = T[k]->b[1]; }
if(T[k]->b[2] < b->b[2]){ b->b[2] = T[k]->b[2]; }
if(T[k]->b[3] > b->b[3]){ b->b[3] = T[k]->b[3]; }
}
BVHDBG(" Making internal node %p, tag=%d, b=%f,%f,%f,%f\n", b, b->tag, b->b[0], b->b[1], b->b[2], b->b[3]);
for(k = 0; k < pack_size; ++k){
BVHDBG(" Child node %p, tag=%d, b=%f,%f,%f,%f\n", T[k], T[k]->tag, T[k]->b[0], T[k]->b[1], T[k]->b[2], T[k]->b[3]);
}
*B = b;
T += pack_size;
B++;
(*n_)++;
j -= pack_size;
}
i -= slice_size;
}
BVHDBG("Iteration of STR done; n=%d -> %d\n", n, *n_);
}
geom_bvh2d geom_bvh2d_new(unsigned int n, int (*shape_iterator)(double c[2], double h[2], int *tag, void *data), void *data){
unsigned int i;
// Make n boxes for each of the input boxes
geom_bvh2d *B = (geom_bvh2d*)malloc(sizeof(geom_bvh2d) * n);
geom_bvh2d ret;
for(i = 0; i < n; ++i){
double c[2], h[2];
B[i] = geom_bvh2d_make_blank();
shape_iterator(c, h, &(B[i]->tag), data);
B[i]->b[0] = c[0]-h[0];
B[i]->b[1] = c[0]+h[0];
B[i]->b[2] = c[1]-h[1];
B[i]->b[3] = c[1]+h[1];
BVHDBG("Making leaf %p, tag=%d, b=%f,%f,%f,%f\n", B[i], B[i]->tag, B[i]->b[0], B[i]->b[1], B[i]->b[2], B[i]->b[3]);
}
// Create the tree
while(n > 1){
geom_bvh2d_STR(&n, B);
}
ret = B[0];
free(B);
return ret;
}
static void geom_bvh3d_STR(unsigned int *n_, geom_bvh3d *B){
int i, j, k, l;
geom_bvh3d *T = B;
const unsigned int n = *n_;
const unsigned int P = (n+7)/8;
const unsigned int S = icbrt_ceil(P);
*n_ = 0;
// Sort rectangles by first coordinate
sort_bvh3(B, 0, n, 0);
// Partition into S slices and sort each slice by second coordinate
i = n;
while(i > 0){
int slice_size = 8*S;
if(slice_size > i){ slice_size = i; }
BVHDBG(" Slice size = %d\n", slice_size);
sort_bvh3(T, 0, slice_size, 1);
// Now slice by second coordinate and sort by third coord
j = slice_size;
while(j > 0){
int slice_size2 = 8*S;
if(slice_size2 > j){ slice_size2 = j; }
BVHDBG(" Slice2 size = %d\n", slice_size2);
sort_bvh3(T, 0, slice_size2, 2);
k = slice_size2;
while(k > 0){
int pack_size = 8;
if(pack_size > k){ pack_size = k; }
// Make a new node
geom_bvh3d b = geom_bvh3d_make_blank();
b->child[0] = T[0];
b->tag = T[0]->tag;
b->b[0] = T[0]->b[0];
b->b[1] = T[0]->b[1];
b->b[2] = T[0]->b[2];
b->b[3] = T[0]->b[3];
b->b[4] = T[0]->b[4];
b->b[5] = T[0]->b[5];
for(l = 1; l < pack_size; ++l){
b->child[l] = T[l];
if(T[l]->tag > b->tag){ b->tag = T[l]->tag; }
if(T[l]->b[0] < b->b[0]){ b->b[0] = T[l]->b[0]; }
if(T[l]->b[1] > b->b[1]){ b->b[1] = T[l]->b[1]; }
if(T[l]->b[2] < b->b[2]){ b->b[2] = T[l]->b[2]; }
if(T[l]->b[3] > b->b[3]){ b->b[3] = T[l]->b[3]; }
if(T[l]->b[4] < b->b[4]){ b->b[4] = T[l]->b[4]; }
if(T[l]->b[5] > b->b[5]){ b->b[5] = T[l]->b[5]; }
}
BVHDBG(" Making internal node %p, tag=%d, b=%f,%f,%f,%f,%f,%f\n", b, b->tag, b->b[0], b->b[1], b->b[2], b->b[3], b->b[4], b->b[5]);
for(l = 0; l < pack_size; ++l){
BVHDBG(" Child node %p, tag=%d, b=%f,%f,%f,%f,%f,%f\n", T[l], T[l]->tag, T[l]->b[0], T[l]->b[1], T[l]->b[2], T[l]->b[3], T[l]->b[4], T[l]->b[5]);
}
*B = b;
T += pack_size;
B++;
(*n_)++;
k -= pack_size;
}
j -= slice_size2;
}
i -= slice_size;
}
BVHDBG("Iteration of STR done; n=%d -> %d\n", n, *n_);
}
geom_bvh3d geom_bvh3d_new(unsigned int n, int (*shape_iterator)(double c[3], double h[3], int *tag, void *data), void *data){
unsigned int i;
geom_bvh3d *B = (geom_bvh3d*)malloc(sizeof(geom_bvh3d) * n);
geom_bvh3d ret;
for(i = 0; i < n; ++i){
double c[3], h[3];
B[i] = geom_bvh3d_make_blank();
shape_iterator(c, h, &(B[i]->tag), data);
B[i]->b[0] = c[0]-h[0];
B[i]->b[1] = c[0]+h[0];
B[i]->b[2] = c[1]-h[1];
B[i]->b[3] = c[1]+h[1];
B[i]->b[4] = c[2]-h[2];
B[i]->b[5] = c[2]+h[2];
BVHDBG("Making leaf %p, tag=%d, b=%f,%f,%f,%f,%f,%f\n", B[i], B[i]->tag, B[i]->b[0], B[i]->b[1], B[i]->b[2], B[i]->b[3], B[i]->b[4], B[i]->b[5]);
}
while(n > 1){
geom_bvh3d_STR(&n, B);
}
ret = B[0];
free(B);
return ret;
}
void geom_bvh2d_destroy(geom_bvh2d bvh){
int i;
if(NULL == bvh){ return; }
for(i = 0; i < 4; ++i){
geom_bvh2d_destroy(bvh->child[i]);
}
free(bvh);
}
void geom_bvh3d_destroy(geom_bvh3d bvh){
int i;
if(NULL == bvh){ return; }
for(i = 0; i < 8; ++i){
geom_bvh3d_destroy(bvh->child[i]);
}
free(bvh);
}
int geom_bvh2d_query_pt(geom_bvh2d bvh, const double p[2], int (*query_func)(int tag, const double c[2], const double h[2], void *data), void *data){
if(NULL == bvh){
BVHDBG("bvh == NULL in BVH2_query_pt; this should never happen\n");
return 1;
}
BVHDBG("Visiting node %p, tag=%d, b=%f,%f,%f,%f, int=%d\n", bvh, bvh->tag, bvh->b[0], bvh->b[1], bvh->b[2], bvh->b[3], (NULL != bvh->child[0]));
if(!(bvh->b[0] <= p[0] && p[0] <= bvh->b[1] && bvh->b[2] <= p[1] && p[1] <= bvh->b[3])){
// not in box
return 1;
}
if(NULL == bvh->child[0]){
double c[2], h[2];
c[0] = 0.5*bvh->b[0] + 0.5*bvh->b[1];
c[1] = 0.5*bvh->b[2] + 0.5*bvh->b[3];
h[0] = 0.5*bvh->b[1] - 0.5*bvh->b[0];
h[1] = 0.5*bvh->b[3] - 0.5*bvh->b[2];
return query_func(bvh->tag, c, h, data);
}else{
int i;
for(i = 0; i < 4; ++i){
if(NULL == bvh->child[i]){ break; }
if(0 == geom_bvh2d_query_pt(bvh->child[i], p, query_func, data)){ return 0; }
}
return 1;
}
}
int geom_bvh3d_query_pt(geom_bvh3d bvh, const double p[3], int (*query_func)(int tag, const double c[3], const double h[3], void *data), void *data){
if(NULL == bvh){
BVHDBG("bvh == NULL in BVH3_query_pt; this should never happen\n");
return 1;
}
BVHDBG("Visiting node %p, tag=%d, b=%f,%f,%f,%f,%f,%f, int=%d\n", bvh, bvh->tag, bvh->b[0], bvh->b[1], bvh->b[2], bvh->b[3], bvh->b[4], bvh->b[5], (NULL != bvh->child[0]));
if(!(bvh->b[0] <= p[0] && p[0] <= bvh->b[1] && bvh->b[2] <= p[1] && p[1] <= bvh->b[3] && bvh->b[4] <= p[2] && p[2] <= bvh->b[5])){
// not in box
return 1;
}
if(NULL == bvh->child[0]){
double c[3], h[3];
c[0] = 0.5*bvh->b[0] + 0.5*bvh->b[1];
c[1] = 0.5*bvh->b[2] + 0.5*bvh->b[3];
c[2] = 0.5*bvh->b[4] + 0.5*bvh->b[5];
h[0] = 0.5*bvh->b[1] - 0.5*bvh->b[0];
h[1] = 0.5*bvh->b[3] - 0.5*bvh->b[2];
h[2] = 0.5*bvh->b[5] - 0.5*bvh->b[4];
return query_func(bvh->tag, c, h, data);
}else{
int i;
for(i = 0; i < 8; ++i){
if(NULL == bvh->child[i]){ break; }
if(0 == geom_bvh3d_query_pt(bvh->child[i], p, query_func, data)){ return 0; }
}
return 1;
}
}
int geom_bvh2d_query_box(geom_bvh2d bvh, const double c[2], const double h[2], int (*query_func)(int tag, const double c[2], const double h[2], void *data), void *data){
if(NULL == bvh){
BVHDBG("bvh == NULL in BVH2_query_box; this should never happen\n");
return 1;
}
BVHDBG("Visiting node %p, tag=%d, b=%f,%f,%f,%f, int=%d\n", bvh, bvh->tag, bvh->b[0], bvh->b[1], bvh->b[2], bvh->b[3], (NULL != bvh->child[0]));
if((bvh->b[1] < c[0]-h[0] || c[0]+h[0] < bvh->b[0]) && (bvh->b[3] < c[1]-h[1] || c[1]+h[1] < bvh->b[2])){
// not in box
return 1;
}
if(NULL == bvh->child[0]){
double c[2], h[2];
c[0] = 0.5*bvh->b[0] + 0.5*bvh->b[1];
c[1] = 0.5*bvh->b[2] + 0.5*bvh->b[3];
h[0] = 0.5*bvh->b[1] - 0.5*bvh->b[0];
h[1] = 0.5*bvh->b[3] - 0.5*bvh->b[2];
return query_func(bvh->tag, c, h, data);
}else{
int i;
for(i = 0; i < 4; ++i){
if(NULL == bvh->child[i]){ break; }
if(0 == geom_bvh2d_query_box(bvh->child[i], c, h, query_func, data)){ return 0; }
}
return 1;
}
}
int geom_bvh3d_query_box(geom_bvh3d bvh, const double c[3], const double h[3], int (*query_func)(int tag, const double c[3], const double h[3], void *data), void *data){
if(NULL == bvh){
BVHDBG("bvh == NULL in BVH3_query_box; this should never happen\n");
return 1;
}
BVHDBG("Visiting node %p, tag=%d, b=%f,%f,%f,%f,%f,%f, int=%d\n", bvh, bvh->tag, bvh->b[0], bvh->b[1], bvh->b[2], bvh->b[3], bvh->b[4], bvh->b[5], (NULL != bvh->child[0]));
if((bvh->b[1] < c[0]-h[0] || c[0]+h[0] < bvh->b[0]) && (bvh->b[3] < c[1]-h[1] || c[1]+h[1] < bvh->b[2]) && (bvh->b[5] < c[2]-h[2] || c[2]+h[2] < bvh->b[4])){
// not in box
return 1;
}
if(NULL == bvh->child[0]){
double c[3], h[3];
c[0] = 0.5*bvh->b[0] + 0.5*bvh->b[1];
c[1] = 0.5*bvh->b[2] + 0.5*bvh->b[3];
c[2] = 0.5*bvh->b[4] + 0.5*bvh->b[5];
h[0] = 0.5*bvh->b[1] - 0.5*bvh->b[0];
h[1] = 0.5*bvh->b[3] - 0.5*bvh->b[2];
h[2] = 0.5*bvh->b[5] - 0.5*bvh->b[4];
return query_func(bvh->tag, c, h, data);
}else{
int i;
for(i = 0; i < 8; ++i){
if(NULL == bvh->child[i]){ break; }
if(0 == geom_bvh3d_query_box(bvh->child[i], c, h, query_func, data)){ return 0; }
}
return 1;
}
}
int geom_bvh2d_traverse(geom_bvh2d bvh, int (*func)(int tag, const double c[2], const double h[2], int leaf, void *data), void *data){
if(NULL == bvh){
BVHDBG("bvh == NULL in BVH2_traverse; this should never happen\n");
return 1;
}
BVHDBG("Visiting node %p, tag=%d, b=%f,%f,%f,%f, int=%d\n", bvh, bvh->tag, bvh->b[0], bvh->b[1], bvh->b[2], bvh->b[3], (NULL != bvh->child[0]));
int i;
double c[2], h[2];
c[0] = 0.5*bvh->b[0] + 0.5*bvh->b[1];
c[1] = 0.5*bvh->b[2] + 0.5*bvh->b[3];
h[0] = 0.5*bvh->b[1] - 0.5*bvh->b[0];
h[1] = 0.5*bvh->b[3] - 0.5*bvh->b[2];
if(0 == func(bvh->tag, c, h, (NULL == bvh->child[0]), data)){ return 0; }
for(i = 0; i < 4; ++i){
if(NULL == bvh->child[i]){ break; }
if(0 == geom_bvh2d_traverse(bvh->child[i], func, data)){ return 0; }
}
return 1;
}
int geom_bvh3d_traverse(geom_bvh3d bvh, int (*func)(int tag, const double c[3], const double h[3], int leaf, void *data), void *data){
if(NULL == bvh){
BVHDBG("bvh == NULL in BVH3_traverse; this should never happen\n");
return 1;
}
BVHDBG("Visiting node %p, tag=%d, b=%f,%f,%f,%f,%f,%f, int=%d\n", bvh, bvh->tag, bvh->b[0], bvh->b[1], bvh->b[2], bvh->b[3], bvh->b[4], bvh->b[5], (NULL != bvh->child[0]));
int i;
double c[3], h[3];
c[0] = 0.5*bvh->b[0] + 0.5*bvh->b[1];
c[1] = 0.5*bvh->b[2] + 0.5*bvh->b[3];
c[2] = 0.5*bvh->b[4] + 0.5*bvh->b[5];
h[0] = 0.5*bvh->b[1] - 0.5*bvh->b[0];
h[1] = 0.5*bvh->b[3] - 0.5*bvh->b[2];
h[2] = 0.5*bvh->b[5] - 0.5*bvh->b[4];
if(0 == func(bvh->tag, c, h, (NULL == bvh->child[0]), data)){ return 0; }
for(i = 0; i < 8; ++i){
if(NULL == bvh->child[i]){ break; }
if(0 == geom_bvh3d_traverse(bvh->child[i], func, data)){ return 0; }
}
return 1;
}