large reduction, redoing things
[carveJwlIkooP6JGAAIwe30JlM.git] / bvh.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef BVH_H
6 #define BVH_H
7 #include "common.h"
8
9 /*
10 * Usage:
11 *
12 * create a bh_system with functions filled out for expand, centroid, and swap.
13 * optionally include item_debug and cast_ray functions if needed, otherwise,
14 * set them to null
15 *
16 * create a bh_tree struct with:
17 * user: a pointer back the base of the data you are ordering
18 * system: the system we created above which will deal with the data
19 *
20 * call bh_create( bh_tree *bh, u32 item_count )
21 * VG_STATIC int bh_ray( bh_tree *bh, u32 inode, v3f co, v3f dir, ray_hit *hit )
22 * VG_STATIC int bh_select( bh_tree *bh, boxf box, u32 *buffer, int len )
23 */
24
25 typedef struct bh_node bh_node;
26 typedef struct bh_tree bh_tree;
27 typedef struct bh_system bh_system;
28
29 struct bh_tree
30 {
31 u32 node_count;
32
33 bh_system *system;
34 void *user;
35 u32 max_per_leaf;
36
37 struct bh_node
38 {
39 boxf bbx;
40
41 /* if il is 0, this is a leaf */
42 int il, count;
43 union{ int ir, start; };
44 }
45 nodes[];
46 };
47
48 struct bh_system
49 {
50 void (*expand_bound)( void *user, boxf bound, u32 item_index );
51 float (*item_centroid)( void *user, u32 item_index, int axis );
52 void (*item_closest)( void *user, u32 item_index, v3f point, v3f closest );
53 void (*item_swap)( void *user, u32 ia, u32 ib );
54
55 /*
56 * Optional:
57 * item_debug - draw this item quickly usually with lines
58 * cast_ray - shoot a ray against the object, if this is not set,
59 * raycasts will simply return the hit on the bvh node
60 */
61
62 void (*item_debug)( void *user, u32 item_index );
63 int (*cast_ray)( void *user, u32 index, v3f co, v3f dir, ray_hit *hit );
64 };
65
66 VG_STATIC void bh_update_bounds( bh_tree *bh, u32 inode )
67 {
68 bh_node *node = &bh->nodes[ inode ];
69
70 box_init_inf( node->bbx );
71 for( u32 i=0; i<node->count; i++ )
72 {
73 u32 idx = node->start+i;
74 bh->system->expand_bound( bh->user, node->bbx, idx );
75 }
76 }
77
78 VG_STATIC void bh_subdivide( bh_tree *bh, u32 inode )
79 {
80 bh_node *node = &bh->nodes[ inode ];
81
82 if( node->count <= bh->max_per_leaf )
83 return;
84
85 v3f extent;
86 v3_sub( node->bbx[1], node->bbx[0], extent );
87
88 int axis = 0;
89 if( extent[1] > extent[0] ) axis = 1;
90 if( extent[2] > extent[axis] ) axis = 2;
91
92 float split = node->bbx[0][axis] + extent[axis]*0.5f;
93 float avg = 0.0;
94 for( u32 t=0; t<node->count; t++ )
95 {
96 u32 idx = node->start+t;
97 avg += bh->system->item_centroid( bh->user, idx, axis );
98 }
99 avg /= (float)node->count;
100 split = avg;
101
102
103 i32 i = node->start,
104 j = i + node->count-1;
105
106 while( i <= j )
107 {
108 if( bh->system->item_centroid( bh->user, i, axis ) < split )
109 i ++;
110 else
111 {
112 bh->system->item_swap( bh->user, i, j );
113 j --;
114 }
115 }
116
117 u32 left_count = i - node->start;
118 if( left_count == 0 || left_count == node->count ) return;
119
120 u32 il = bh->node_count ++,
121 ir = bh->node_count ++;
122
123 bh_node *lnode = &bh->nodes[il],
124 *rnode = &bh->nodes[ir];
125
126 lnode->start = node->start;
127 lnode->count = left_count;
128 rnode->start = i;
129 rnode->count = node->count - left_count;
130
131 node->il = il;
132 node->ir = ir;
133 node->count = 0;
134
135 bh_update_bounds( bh, il );
136 bh_update_bounds( bh, ir );
137 bh_subdivide( bh, il );
138 bh_subdivide( bh, ir );
139 }
140
141 VG_STATIC bh_tree *bh_create( void *lin_alloc, bh_system *system,
142 void *user, u32 item_count, u32 max_per_leaf )
143 {
144 assert( max_per_leaf > 0 );
145
146 u32 alloc_count = VG_MAX( 1, item_count );
147
148 u32 totsize = sizeof(bh_tree) + sizeof(bh_node)*(alloc_count*2-1);
149 bh_tree *bh = vg_linear_alloc( lin_alloc, vg_align8(totsize) );
150 bh->system = system;
151 bh->user = user;
152 bh->max_per_leaf = max_per_leaf;
153
154 bh_node *root = &bh->nodes[0];
155 bh->node_count = 1;
156
157 root->il = 0;
158 root->ir = 0;
159 root->count = item_count;
160 root->start = 0;
161
162 bh_update_bounds( bh, 0 );
163
164 if( item_count > 2 )
165 bh_subdivide( bh, 0 );
166
167 totsize = sizeof(bh_tree) + sizeof(bh_node) * bh->node_count;
168 bh = vg_linear_resize( lin_alloc, bh, totsize );
169
170 vg_success( "BVH done, size: %u/%u\n", bh->node_count, (alloc_count*2-1) );
171 return bh;
172 }
173
174 /*
175 * Draw items in this leaf node.
176 * *item_debug() must be set!
177 */
178 VG_STATIC void bh_debug_leaf( bh_tree *bh, bh_node *node )
179 {
180 vg_line_boxf( node->bbx, 0xff00ff00 );
181
182 if( bh->system->item_debug )
183 {
184 for( u32 i=0; i<node->count; i++ )
185 {
186 u32 idx = node->start+i;
187 bh->system->item_debug( bh->user, idx );
188 }
189 }
190 }
191
192 /*
193 * Trace the bh tree all the way down to the leaf nodes where pos is inside
194 */
195 VG_STATIC void bh_debug_trace( bh_tree *bh, u32 inode, v3f pos, u32 colour )
196 {
197 bh_node *node = &bh->nodes[ inode ];
198
199 if( (pos[0] >= node->bbx[0][0] && pos[0] <= node->bbx[1][0]) &&
200 (pos[2] >= node->bbx[0][2] && pos[2] <= node->bbx[1][2]) )
201 {
202 if( !node->count )
203 {
204 vg_line_boxf( node->bbx, colour );
205
206 bh_debug_trace( bh, node->il, pos, colour );
207 bh_debug_trace( bh, node->ir, pos, colour );
208 }
209 else
210 {
211 if( bh->system->item_debug )
212 bh_debug_leaf( bh, node );
213 }
214 }
215 }
216
217 VG_STATIC int bh_ray( bh_tree *bh, v3f co, v3f dir, ray_hit *hit )
218 {
219 if( bh->node_count < 2 )
220 return 0;
221
222 int count = 0;
223 u32 stack[100];
224 u32 depth = 2;
225
226 stack[0] = 0;
227 stack[1] = bh->nodes[0].il;
228 stack[2] = bh->nodes[0].ir;
229
230 v3f dir_inv;
231 dir_inv[0] = 1.0f/dir[0];
232 dir_inv[1] = 1.0f/dir[1];
233 dir_inv[2] = 1.0f/dir[2];
234
235 while(depth)
236 {
237 bh_node *inode = &bh->nodes[ stack[depth] ];
238 if( ray_aabb1( inode->bbx, co, dir_inv, hit->dist ) )
239 {
240 if( inode->count )
241 {
242 for( u32 i=0; i<inode->count; i++ )
243 {
244 u32 idx = inode->start+i;
245
246 if( bh->system->cast_ray )
247 count += bh->system->cast_ray( bh->user, idx, co, dir, hit );
248 else
249 count ++;
250 }
251
252 depth --;
253 }
254 else
255 {
256 if( depth+1 >= vg_list_size(stack) )
257 {
258 vg_error( "Maximum stack reached!\n" );
259 return count;
260 }
261
262 stack[depth] = inode->il;
263 stack[depth+1] = inode->ir;
264 depth ++;
265 }
266 }
267 else
268 {
269 depth --;
270 }
271 }
272
273 return count;
274 }
275
276 typedef struct bh_iter bh_iter;
277 struct bh_iter
278 {
279 struct
280 {
281 int id, depth;
282 }
283 stack[64];
284
285 int depth, i;
286 };
287
288 VG_STATIC void bh_iter_init( int root, bh_iter *it )
289 {
290 it->stack[0].id = root;
291 it->stack[0].depth = 0;
292 it->depth = 0;
293 it->i = 0;
294 }
295
296 VG_STATIC int bh_next( bh_tree *bh, bh_iter *it, boxf box, int *em )
297 {
298 while( it->depth >= 0 )
299 {
300 bh_node *inode = &bh->nodes[ it->stack[it->depth].id ];
301
302 /* Only process overlapping nodes */
303 if( !box_overlap( inode->bbx, box ) )
304 {
305 it->depth --;
306 continue;
307 }
308
309 if( inode->count )
310 {
311 if( it->i < inode->count )
312 {
313 *em = inode->start+it->i;
314 it->i ++;
315 return 1;
316 }
317 else
318 {
319 it->depth --;
320 it->i = 0;
321 }
322 }
323 else
324 {
325 if( it->depth+1 >= vg_list_size(it->stack) )
326 {
327 vg_error( "Maximum stack reached!\n" );
328 return 0;
329 }
330
331 it->stack[it->depth ].id = inode->il;
332 it->stack[it->depth+1].id = inode->ir;
333 it->depth ++;
334 it->i = 0;
335 }
336 }
337
338 return 0;
339 }
340
341 VG_STATIC int bh_closest_point( bh_tree *bh, v3f pos,
342 v3f closest, float max_dist )
343 {
344 if( bh->node_count < 2 )
345 return -1;
346
347 max_dist = max_dist*max_dist;
348
349 int queue[ 128 ],
350 depth = 0,
351 best_item = -1;
352
353 queue[0] = 0;
354
355 while( depth >= 0 )
356 {
357 bh_node *inode = &bh->nodes[ queue[depth] ];
358
359 v3f p1;
360 closest_point_aabb( pos, inode->bbx, p1 );
361
362 /* branch into node if its closer than current best */
363 float node_dist = v3_dist2( pos, p1 );
364 if( node_dist < max_dist )
365 {
366 if( inode->count )
367 {
368 for( int i=0; i<inode->count; i++ )
369 {
370 v3f p2;
371 bh->system->item_closest( bh->user, inode->start+i, pos, p2 );
372
373 float item_dist = v3_dist2( pos, p2 );
374 if( item_dist < max_dist )
375 {
376 max_dist = item_dist;
377 v3_copy( p2, closest );
378 best_item = inode->start+i;
379 }
380 }
381
382 depth --;
383 }
384 else
385 {
386 queue[depth] = inode->il;
387 queue[depth+1] = inode->ir;
388
389 depth ++;
390 }
391 }
392 else
393 depth --;
394 }
395
396 return best_item;
397 }
398
399 #endif /* BVH_H */