a fairly major physics update
[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
94 float avg = 0.0;
95 for( u32 t=0; t<node->count; t++ )
96 {
97 u32 idx = node->start+t;
98 avg += bh->system->item_centroid( bh->user, idx, axis );
99 }
100 avg /= (float)node->count;
101
102 split = avg;
103
104 i32 i = node->start,
105 j = i + node->count-1;
106
107 while( i <= j )
108 {
109 if( bh->system->item_centroid( bh->user, i, axis ) < split )
110 i ++;
111 else
112 {
113 bh->system->item_swap( bh->user, i, j );
114 j --;
115 }
116 }
117
118 u32 left_count = i - node->start;
119 if( left_count == 0 || left_count == node->count ) return;
120
121 u32 il = bh->node_count ++,
122 ir = bh->node_count ++;
123
124 bh_node *lnode = &bh->nodes[il],
125 *rnode = &bh->nodes[ir];
126
127 lnode->start = node->start;
128 lnode->count = left_count;
129 rnode->start = i;
130 rnode->count = node->count - left_count;
131
132 node->il = il;
133 node->ir = ir;
134 node->count = 0;
135
136 bh_update_bounds( bh, il );
137 bh_update_bounds( bh, ir );
138 bh_subdivide( bh, il );
139 bh_subdivide( bh, ir );
140 }
141
142 VG_STATIC bh_tree *bh_create( void *lin_alloc, bh_system *system,
143 void *user, u32 item_count, u32 max_per_leaf )
144 {
145 assert( max_per_leaf > 0 );
146
147 if( item_count == 0 )
148 {
149 bh_tree *bh = vg_linear_alloc( lin_alloc, sizeof(bh_tree) );
150 bh->node_count = 0;
151 bh->system = system;
152 bh->user = user;
153 return bh;
154 }
155
156 u32 totsize = sizeof(bh_tree) + sizeof(bh_node)*(item_count*2-1);
157 bh_tree *bh = vg_linear_alloc( lin_alloc, vg_align8(totsize) );
158 bh->system = system;
159 bh->user = user;
160 bh->max_per_leaf = max_per_leaf;
161
162 bh_node *root = &bh->nodes[0];
163 bh->node_count = 1;
164
165 root->il = 0;
166 root->ir = 0;
167 root->count = item_count;
168 root->start = 0;
169
170 bh_update_bounds( bh, 0 );
171 bh_subdivide( bh, 0 );
172
173 totsize = sizeof(bh_tree) + sizeof(bh_node) * bh->node_count;
174 bh = vg_linear_resize( lin_alloc, bh, totsize );
175
176 vg_success( "BVH done, size: %u/%u\n", bh->node_count, (item_count*2-1) );
177 return bh;
178 }
179
180 /*
181 * Draw items in this leaf node.
182 * *item_debug() must be set!
183 */
184 VG_STATIC void bh_debug_leaf( bh_tree *bh, bh_node *node )
185 {
186 vg_line_boxf( node->bbx, 0xff00ff00 );
187
188 if( bh->system->item_debug )
189 {
190 for( u32 i=0; i<node->count; i++ )
191 {
192 u32 idx = node->start+i;
193 bh->system->item_debug( bh->user, idx );
194 }
195 }
196 }
197
198 /*
199 * Trace the bh tree all the way down to the leaf nodes where pos is inside
200 */
201 VG_STATIC void bh_debug_trace( bh_tree *bh, u32 inode, v3f pos, u32 colour )
202 {
203 bh_node *node = &bh->nodes[ inode ];
204
205 if( (pos[0] >= node->bbx[0][0] && pos[0] <= node->bbx[1][0]) &&
206 (pos[2] >= node->bbx[0][2] && pos[2] <= node->bbx[1][2]) )
207 {
208 if( !node->count )
209 {
210 vg_line_boxf( node->bbx, colour );
211
212 bh_debug_trace( bh, node->il, pos, colour );
213 bh_debug_trace( bh, node->ir, pos, colour );
214 }
215 else
216 {
217 if( bh->system->item_debug )
218 bh_debug_leaf( bh, node );
219 }
220 }
221 }
222
223 VG_STATIC int bh_ray( bh_tree *bh, v3f co, v3f dir, ray_hit *hit )
224 {
225 if( bh->node_count < 2 )
226 return 0;
227
228 int count = 0;
229 u32 stack[100];
230 u32 depth = 2;
231
232 stack[0] = 0;
233 stack[1] = bh->nodes[0].il;
234 stack[2] = bh->nodes[0].ir;
235
236 while(depth)
237 {
238 bh_node *inode = &bh->nodes[ stack[depth] ];
239 if( ray_aabb( inode->bbx, co, dir, hit->dist ) )
240 {
241 if( inode->count )
242 {
243 for( u32 i=0; i<inode->count; i++ )
244 {
245 u32 idx = inode->start+i;
246
247 if( bh->system->cast_ray )
248 count += bh->system->cast_ray( bh->user, idx, co, dir, hit );
249 else
250 count ++;
251 }
252
253 depth --;
254 }
255 else
256 {
257 if( depth+1 >= vg_list_size(stack) )
258 {
259 vg_error( "Maximum stack reached!\n" );
260 return count;
261 }
262
263 stack[depth] = inode->il;
264 stack[depth+1] = inode->ir;
265 depth ++;
266 }
267 }
268 else
269 {
270 depth --;
271 }
272 }
273
274 return count;
275 }
276
277 typedef struct bh_iter bh_iter;
278 struct bh_iter
279 {
280 struct
281 {
282 int id, depth;
283 }
284 stack[64];
285
286 int depth, i;
287 };
288
289 VG_STATIC void bh_iter_init( int root, bh_iter *it )
290 {
291 it->stack[0].id = root;
292 it->stack[0].depth = 0;
293 it->depth = 0;
294 it->i = 0;
295 }
296
297 VG_STATIC int bh_next( bh_tree *bh, bh_iter *it, boxf box, int *em )
298 {
299 while( it->depth >= 0 )
300 {
301 bh_node *inode = &bh->nodes[ it->stack[it->depth].id ];
302
303 /* Only process overlapping nodes */
304 if( !box_overlap( inode->bbx, box ) )
305 {
306 it->depth --;
307 continue;
308 }
309
310 if( inode->count )
311 {
312 if( it->i < inode->count )
313 {
314 *em = inode->start+it->i;
315 it->i ++;
316 return 1;
317 }
318 else
319 {
320 it->depth --;
321 it->i = 0;
322 }
323 }
324 else
325 {
326 if( it->depth+1 >= vg_list_size(it->stack) )
327 {
328 vg_error( "Maximum stack reached!\n" );
329 return 0;
330 }
331
332 it->stack[it->depth ].id = inode->il;
333 it->stack[it->depth+1].id = inode->ir;
334 it->depth ++;
335 it->i = 0;
336 }
337 }
338
339 return 0;
340 }
341
342 VG_STATIC int bh_closest_point( bh_tree *bh, v3f pos,
343 v3f closest, float max_dist )
344 {
345 if( bh->node_count < 2 )
346 return -1;
347
348 max_dist = max_dist*max_dist;
349
350 int queue[ 128 ],
351 depth = 0,
352 best_item = -1;
353
354 queue[0] = 0;
355
356 while( depth >= 0 )
357 {
358 bh_node *inode = &bh->nodes[ queue[depth] ];
359
360 v3f p1;
361 closest_point_aabb( pos, inode->bbx, p1 );
362
363 /* branch into node if its closer than current best */
364 float node_dist = v3_dist2( pos, p1 );
365 if( node_dist < max_dist )
366 {
367 if( inode->count )
368 {
369 for( int i=0; i<inode->count; i++ )
370 {
371 v3f p2;
372 bh->system->item_closest( bh->user, inode->start+i, pos, p2 );
373
374 float item_dist = v3_dist2( pos, p2 );
375 if( item_dist < max_dist )
376 {
377 max_dist = item_dist;
378 v3_copy( p2, closest );
379 best_item = inode->start+i;
380 }
381 }
382
383 depth --;
384 }
385 else
386 {
387 queue[depth] = inode->il;
388 queue[depth+1] = inode->ir;
389
390 depth ++;
391 }
392 }
393 else
394 depth --;
395 }
396
397 return best_item;
398 }
399
400 #endif /* BVH_H */