2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
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,
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
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 )
25 typedef struct bh_node bh_node
;
26 typedef struct bh_tree bh_tree
;
27 typedef struct bh_system bh_system
;
41 /* if il is 0, this is a leaf */
43 union{ int ir
, start
; };
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
);
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
62 void (*item_debug
)( void *user
, u32 item_index
);
63 int (*cast_ray
)( void *user
, u32 index
, v3f co
, v3f dir
, ray_hit
*hit
);
66 VG_STATIC
void bh_update_bounds( bh_tree
*bh
, u32 inode
)
68 bh_node
*node
= &bh
->nodes
[ inode
];
70 box_init_inf( node
->bbx
);
71 for( u32 i
=0; i
<node
->count
; i
++ )
73 u32 idx
= node
->start
+i
;
74 bh
->system
->expand_bound( bh
->user
, node
->bbx
, idx
);
78 VG_STATIC
void bh_subdivide( bh_tree
*bh
, u32 inode
)
80 bh_node
*node
= &bh
->nodes
[ inode
];
82 if( node
->count
<= bh
->max_per_leaf
)
86 v3_sub( node
->bbx
[1], node
->bbx
[0], extent
);
89 if( extent
[1] > extent
[0] ) axis
= 1;
90 if( extent
[2] > extent
[axis
] ) axis
= 2;
92 float split
= node
->bbx
[0][axis
] + extent
[axis
]*0.5f
;
95 for( u32 t
=0; t
<node
->count
; t
++ )
97 u32 idx
= node
->start
+t
;
98 avg
+= bh
->system
->item_centroid( bh
->user
, idx
, axis
);
100 avg
/= (float)node
->count
;
105 j
= i
+ node
->count
-1;
109 if( bh
->system
->item_centroid( bh
->user
, i
, axis
) < split
)
113 bh
->system
->item_swap( bh
->user
, i
, j
);
118 u32 left_count
= i
- node
->start
;
119 if( left_count
== 0 || left_count
== node
->count
) return;
121 u32 il
= bh
->node_count
++,
122 ir
= bh
->node_count
++;
124 bh_node
*lnode
= &bh
->nodes
[il
],
125 *rnode
= &bh
->nodes
[ir
];
127 lnode
->start
= node
->start
;
128 lnode
->count
= left_count
;
130 rnode
->count
= node
->count
- left_count
;
136 bh_update_bounds( bh
, il
);
137 bh_update_bounds( bh
, ir
);
138 bh_subdivide( bh
, il
);
139 bh_subdivide( bh
, ir
);
142 VG_STATIC bh_tree
*bh_create( void *lin_alloc
, bh_system
*system
,
143 void *user
, u32 item_count
, u32 max_per_leaf
)
145 assert( max_per_leaf
> 0 );
147 if( item_count
== 0 )
149 bh_tree
*bh
= vg_linear_alloc( lin_alloc
, sizeof(bh_tree
) );
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
) );
160 bh
->max_per_leaf
= max_per_leaf
;
162 bh_node
*root
= &bh
->nodes
[0];
167 root
->count
= item_count
;
170 bh_update_bounds( bh
, 0 );
171 bh_subdivide( bh
, 0 );
173 totsize
= sizeof(bh_tree
) + sizeof(bh_node
) * bh
->node_count
;
174 bh
= vg_linear_resize( lin_alloc
, bh
, totsize
);
176 vg_success( "BVH done, size: %u/%u\n", bh
->node_count
, (item_count
*2-1) );
181 * Draw items in this leaf node.
182 * *item_debug() must be set!
184 VG_STATIC
void bh_debug_leaf( bh_tree
*bh
, bh_node
*node
)
186 vg_line_boxf( node
->bbx
, 0xff00ff00 );
188 if( bh
->system
->item_debug
)
190 for( u32 i
=0; i
<node
->count
; i
++ )
192 u32 idx
= node
->start
+i
;
193 bh
->system
->item_debug( bh
->user
, idx
);
199 * Trace the bh tree all the way down to the leaf nodes where pos is inside
201 VG_STATIC
void bh_debug_trace( bh_tree
*bh
, u32 inode
, v3f pos
, u32 colour
)
203 bh_node
*node
= &bh
->nodes
[ inode
];
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]) )
210 vg_line_boxf( node
->bbx
, colour
);
212 bh_debug_trace( bh
, node
->il
, pos
, colour
);
213 bh_debug_trace( bh
, node
->ir
, pos
, colour
);
217 if( bh
->system
->item_debug
)
218 bh_debug_leaf( bh
, node
);
223 VG_STATIC
int bh_ray( bh_tree
*bh
, v3f co
, v3f dir
, ray_hit
*hit
)
225 if( bh
->node_count
< 2 )
233 stack
[1] = bh
->nodes
[0].il
;
234 stack
[2] = bh
->nodes
[0].ir
;
237 dir_inv
[0] = 1.0f
/dir
[0];
238 dir_inv
[1] = 1.0f
/dir
[1];
239 dir_inv
[2] = 1.0f
/dir
[2];
243 bh_node
*inode
= &bh
->nodes
[ stack
[depth
] ];
244 if( ray_aabb1( inode
->bbx
, co
, dir_inv
, hit
->dist
) )
248 for( u32 i
=0; i
<inode
->count
; i
++ )
250 u32 idx
= inode
->start
+i
;
252 if( bh
->system
->cast_ray
)
253 count
+= bh
->system
->cast_ray( bh
->user
, idx
, co
, dir
, hit
);
262 if( depth
+1 >= vg_list_size(stack
) )
264 vg_error( "Maximum stack reached!\n" );
268 stack
[depth
] = inode
->il
;
269 stack
[depth
+1] = inode
->ir
;
282 typedef struct bh_iter bh_iter
;
294 VG_STATIC
void bh_iter_init( int root
, bh_iter
*it
)
296 it
->stack
[0].id
= root
;
297 it
->stack
[0].depth
= 0;
302 VG_STATIC
int bh_next( bh_tree
*bh
, bh_iter
*it
, boxf box
, int *em
)
304 while( it
->depth
>= 0 )
306 bh_node
*inode
= &bh
->nodes
[ it
->stack
[it
->depth
].id
];
308 /* Only process overlapping nodes */
309 if( !box_overlap( inode
->bbx
, box
) )
317 if( it
->i
< inode
->count
)
319 *em
= inode
->start
+it
->i
;
331 if( it
->depth
+1 >= vg_list_size(it
->stack
) )
333 vg_error( "Maximum stack reached!\n" );
337 it
->stack
[it
->depth
].id
= inode
->il
;
338 it
->stack
[it
->depth
+1].id
= inode
->ir
;
347 VG_STATIC
int bh_closest_point( bh_tree
*bh
, v3f pos
,
348 v3f closest
, float max_dist
)
350 if( bh
->node_count
< 2 )
353 max_dist
= max_dist
*max_dist
;
363 bh_node
*inode
= &bh
->nodes
[ queue
[depth
] ];
366 closest_point_aabb( pos
, inode
->bbx
, p1
);
368 /* branch into node if its closer than current best */
369 float node_dist
= v3_dist2( pos
, p1
);
370 if( node_dist
< max_dist
)
374 for( int i
=0; i
<inode
->count
; i
++ )
377 bh
->system
->item_closest( bh
->user
, inode
->start
+i
, pos
, p2
);
379 float item_dist
= v3_dist2( pos
, p2
);
380 if( item_dist
< max_dist
)
382 max_dist
= item_dist
;
383 v3_copy( p2
, closest
);
384 best_item
= inode
->start
+i
;
392 queue
[depth
] = inode
->il
;
393 queue
[depth
+1] = inode
->ir
;