2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
10 #include "vg/vg_lines.h"
15 * create a bh_system with functions filled out for expand, centroid, and swap.
16 * optionally include item_debug and cast_ray functions if needed, otherwise,
19 * create a bh_tree struct with:
20 * user: a pointer back the base of the data you are ordering
21 * system: the system we created above which will deal with the data
23 * call bh_create( bh_tree *bh, u32 item_count )
24 * static int bh_ray( bh_tree *bh, u32 inode, v3f co, v3f dir, ray_hit *hit )
25 * static int bh_select( bh_tree *bh, boxf box, u32 *buffer, int len )
28 typedef struct bh_node bh_node
;
29 typedef struct bh_tree bh_tree
;
30 typedef struct bh_system bh_system
;
32 typedef struct ray_hit ray_hit
;
50 /* if il is 0, this is a leaf */
52 union{ int ir
, start
; };
59 void (*expand_bound
)( void *user
, boxf bound
, u32 item_index
);
60 float (*item_centroid
)( void *user
, u32 item_index
, int axis
);
61 void (*item_closest
)( void *user
, u32 item_index
, v3f point
, v3f closest
);
62 void (*item_swap
)( void *user
, u32 ia
, u32 ib
);
66 * item_debug - draw this item quickly usually with lines
67 * cast_ray - shoot a ray against the object, if this is not set,
68 * raycasts will simply return the hit on the bvh node
71 void (*item_debug
)( void *user
, u32 item_index
);
72 int (*cast_ray
)( void *user
, u32 index
, v3f co
, v3f dir
, ray_hit
*hit
);
75 static float scene_bh_centroid( void *user
, u32 item_index
, int axis
);
76 static void scene_bh_swap( void *user
, u32 ia
, u32 ib
);
77 static void scene_bh_expand_bound( void *user
, boxf bound
, u32 item_index
);
79 static void bh_update_bounds( bh_tree
*bh
, u32 inode
){
80 bh_node
*node
= &bh
->nodes
[ inode
];
82 box_init_inf( node
->bbx
);
83 for( u32 i
=0; i
<node
->count
; i
++ ){
84 u32 idx
= node
->start
+i
;
85 if( bh
->system
->system_type
== 0x1 )
86 scene_bh_expand_bound( bh
->user
, node
->bbx
, idx
);
88 bh
->system
->expand_bound( bh
->user
, node
->bbx
, idx
);
92 static void bh_subdivide( bh_tree
*bh
, u32 inode
){
93 bh_node
*node
= &bh
->nodes
[ inode
];
95 if( node
->count
<= bh
->max_per_leaf
)
99 v3_sub( node
->bbx
[1], node
->bbx
[0], extent
);
102 if( extent
[1] > extent
[0] ) axis
= 1;
103 if( extent
[2] > extent
[axis
] ) axis
= 2;
105 float split
= node
->bbx
[0][axis
] + extent
[axis
]*0.5f
;
107 for( u32 t
=0; t
<node
->count
; t
++ ){
108 u32 idx
= node
->start
+t
;
110 if( bh
->system
->system_type
== 0x1 )
111 avg
+= scene_bh_centroid( bh
->user
, idx
, axis
);
113 avg
+= bh
->system
->item_centroid( bh
->user
, idx
, axis
);
115 avg
/= (float)node
->count
;
120 j
= i
+ node
->count
-1;
125 if( bh
->system
->system_type
== 0x1 )
126 centroid
= scene_bh_centroid( bh
->user
, i
, axis
);
128 centroid
= bh
->system
->item_centroid( bh
->user
, i
, axis
);
130 if( centroid
< split
)
133 if( bh
->system
->system_type
== 0x1 )
134 scene_bh_swap( bh
->user
, i
, j
);
136 bh
->system
->item_swap( bh
->user
, i
, j
);
141 u32 left_count
= i
- node
->start
;
142 if( left_count
== 0 || left_count
== node
->count
) return;
144 u32 il
= bh
->node_count
++,
145 ir
= bh
->node_count
++;
147 bh_node
*lnode
= &bh
->nodes
[il
],
148 *rnode
= &bh
->nodes
[ir
];
150 lnode
->start
= node
->start
;
151 lnode
->count
= left_count
;
153 rnode
->count
= node
->count
- left_count
;
159 bh_update_bounds( bh
, il
);
160 bh_update_bounds( bh
, ir
);
161 bh_subdivide( bh
, il
);
162 bh_subdivide( bh
, ir
);
165 static bh_tree
*bh_create( void *lin_alloc
, bh_system
*system
,
166 void *user
, u32 item_count
, u32 max_per_leaf
){
167 assert( max_per_leaf
> 0 );
169 u32 alloc_count
= VG_MAX( 1, item_count
);
171 u32 totsize
= sizeof(bh_tree
) + sizeof(bh_node
)*(alloc_count
*2-1);
172 bh_tree
*bh
= vg_linear_alloc( lin_alloc
, vg_align8(totsize
) );
175 bh
->max_per_leaf
= max_per_leaf
;
177 bh_node
*root
= &bh
->nodes
[0];
182 root
->count
= item_count
;
185 bh_update_bounds( bh
, 0 );
188 bh_subdivide( bh
, 0 );
190 totsize
= vg_align8(sizeof(bh_tree
) + sizeof(bh_node
) * bh
->node_count
);
191 bh
= vg_linear_resize( lin_alloc
, bh
, totsize
);
193 vg_success( "BVH done, size: %u/%u\n", bh
->node_count
, (alloc_count
*2-1) );
198 * Draw items in this leaf node.
199 * *item_debug() must be set!
201 static void bh_debug_leaf( bh_tree
*bh
, bh_node
*node
){
202 vg_line_boxf( node
->bbx
, 0xff00ff00 );
204 if( bh
->system
->item_debug
){
205 for( u32 i
=0; i
<node
->count
; i
++ ){
206 u32 idx
= node
->start
+i
;
207 bh
->system
->item_debug( bh
->user
, idx
);
213 * Trace the bh tree all the way down to the leaf nodes where pos is inside
215 static void bh_debug_trace( bh_tree
*bh
, u32 inode
, v3f pos
, u32 colour
){
216 bh_node
*node
= &bh
->nodes
[ inode
];
218 if( (pos
[0] >= node
->bbx
[0][0] && pos
[0] <= node
->bbx
[1][0]) &&
219 (pos
[2] >= node
->bbx
[0][2] && pos
[2] <= node
->bbx
[1][2]) )
222 vg_line_boxf( node
->bbx
, colour
);
224 bh_debug_trace( bh
, node
->il
, pos
, colour
);
225 bh_debug_trace( bh
, node
->ir
, pos
, colour
);
228 if( bh
->system
->item_debug
)
229 bh_debug_leaf( bh
, node
);
234 typedef struct bh_iter bh_iter
;
270 static void bh_iter_init_generic( i32 root
, bh_iter
*it
){
271 it
->stack
[0].id
= root
;
272 it
->stack
[0].depth
= 0;
277 static void bh_iter_init_box( i32 root
, bh_iter
*it
, boxf box
){
278 bh_iter_init_generic( root
, it
);
279 it
->query
= k_bh_query_box
;
281 box_copy( box
, it
->box
.box
);
284 static void bh_iter_init_ray( i32 root
, bh_iter
*it
, v3f co
,
285 v3f dir
, f32 max_dist
){
286 bh_iter_init_generic( root
, it
);
287 it
->query
= k_bh_query_ray
;
289 v3_div( (v3f
){1.0f
,1.0f
,1.0f
}, dir
, it
->ray
.inv_dir
);
290 v3_copy( co
, it
->ray
.co
);
291 it
->ray
.max_dist
= max_dist
;
294 static void bh_iter_init_range( i32 root
, bh_iter
*it
, v3f co
, f32 range
){
295 bh_iter_init_generic( root
, it
);
296 it
->query
= k_bh_query_range
;
298 v3_copy( co
, it
->range
.co
);
299 it
->range
.dist_sqr
= range
*range
;
302 /* NOTE: does not compute anything beyond the leaf level. element level tests
303 * should be implemented by the users code.
305 * this is like a 'broad phase only' deal.
307 static i32
bh_next( bh_tree
*bh
, bh_iter
*it
, i32
*em
){
308 while( it
->depth
>= 0 ){
309 bh_node
*inode
= &bh
->nodes
[ it
->stack
[it
->depth
].id
];
311 /* Only process overlapping nodes */
314 if( it
->i
) /* already checked */
317 if( it
->query
== k_bh_query_box
)
318 q
= box_overlap( inode
->bbx
, it
->box
.box
);
319 else if( it
->query
== k_bh_query_ray
)
320 q
= ray_aabb1( inode
->bbx
, it
->ray
.co
,
321 it
->ray
.inv_dir
, it
->ray
.max_dist
);
324 closest_point_aabb( it
->range
.co
, inode
->bbx
, nearest
);
326 if( v3_dist2( nearest
, it
->range
.co
) <= it
->range
.dist_sqr
)
337 if( it
->i
< inode
->count
){
338 *em
= inode
->start
+it
->i
;
348 if( it
->depth
+1 >= vg_list_size(it
->stack
) ){
349 vg_error( "Maximum stack reached!\n" );
353 it
->stack
[it
->depth
].id
= inode
->il
;
354 it
->stack
[it
->depth
+1].id
= inode
->ir
;
363 static int bh_closest_point( bh_tree
*bh
, v3f pos
,
364 v3f closest
, float max_dist
)
366 if( bh
->node_count
< 2 )
369 max_dist
= max_dist
*max_dist
;
378 bh_node
*inode
= &bh
->nodes
[ queue
[depth
] ];
381 closest_point_aabb( pos
, inode
->bbx
, p1
);
383 /* branch into node if its closer than current best */
384 float node_dist
= v3_dist2( pos
, p1
);
385 if( node_dist
< max_dist
){
387 for( int i
=0; i
<inode
->count
; i
++ ){
389 bh
->system
->item_closest( bh
->user
, inode
->start
+i
, pos
, p2
);
391 float item_dist
= v3_dist2( pos
, p2
);
392 if( item_dist
< max_dist
){
393 max_dist
= item_dist
;
394 v3_copy( p2
, closest
);
395 best_item
= inode
->start
+i
;
402 queue
[depth
] = inode
->il
;
403 queue
[depth
+1] = inode
->ir
;