2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
13 * create a bh_system with functions filled out for expand, centroid, and swap.
14 * optionally include item_debug and cast_ray functions if needed, otherwise,
17 * create a bh_tree struct with:
18 * user: a pointer back the base of the data you are ordering
19 * system: the system we created above which will deal with the data
21 * call bh_create( bh_tree *bh, u32 item_count )
22 * VG_STATIC int bh_ray( bh_tree *bh, u32 inode, v3f co, v3f dir, ray_hit *hit )
23 * VG_STATIC int bh_select( bh_tree *bh, boxf box, u32 *buffer, int len )
26 typedef struct bh_node bh_node
;
27 typedef struct bh_tree bh_tree
;
28 typedef struct bh_system bh_system
;
42 /* if il is 0, this is a leaf */
44 union{ int ir
, start
; };
51 void (*expand_bound
)( void *user
, boxf bound
, u32 item_index
);
52 float (*item_centroid
)( void *user
, u32 item_index
, int axis
);
53 void (*item_closest
)( void *user
, u32 item_index
, v3f point
, v3f closest
);
54 void (*item_swap
)( void *user
, u32 ia
, u32 ib
);
58 * item_debug - draw this item quickly usually with lines
59 * cast_ray - shoot a ray against the object, if this is not set,
60 * raycasts will simply return the hit on the bvh node
63 void (*item_debug
)( void *user
, u32 item_index
);
64 int (*cast_ray
)( void *user
, u32 index
, v3f co
, v3f dir
, ray_hit
*hit
);
67 VG_STATIC
void bh_update_bounds( bh_tree
*bh
, u32 inode
)
69 bh_node
*node
= &bh
->nodes
[ inode
];
71 box_init_inf( node
->bbx
);
72 for( u32 i
=0; i
<node
->count
; i
++ )
74 u32 idx
= node
->start
+i
;
75 bh
->system
->expand_bound( bh
->user
, node
->bbx
, idx
);
79 VG_STATIC
void bh_subdivide( bh_tree
*bh
, u32 inode
)
81 bh_node
*node
= &bh
->nodes
[ inode
];
83 if( node
->count
<= bh
->max_per_leaf
)
87 v3_sub( node
->bbx
[1], node
->bbx
[0], extent
);
90 if( extent
[1] > extent
[0] ) axis
= 1;
91 if( extent
[2] > extent
[axis
] ) axis
= 2;
93 float split
= node
->bbx
[0][axis
] + extent
[axis
]*0.5f
;
96 for( u32 t
=0; t
<node
->count
; t
++ )
98 u32 idx
= node
->start
+t
;
99 avg
+= bh
->system
->item_centroid( bh
->user
, idx
, axis
);
101 avg
/= (float)node
->count
;
106 j
= i
+ node
->count
-1;
110 if( bh
->system
->item_centroid( bh
->user
, i
, axis
) < split
)
114 bh
->system
->item_swap( bh
->user
, i
, j
);
119 u32 left_count
= i
- node
->start
;
120 if( left_count
== 0 || left_count
== node
->count
) return;
122 u32 il
= bh
->node_count
++,
123 ir
= bh
->node_count
++;
125 bh_node
*lnode
= &bh
->nodes
[il
],
126 *rnode
= &bh
->nodes
[ir
];
128 lnode
->start
= node
->start
;
129 lnode
->count
= left_count
;
131 rnode
->count
= node
->count
- left_count
;
137 bh_update_bounds( bh
, il
);
138 bh_update_bounds( bh
, ir
);
139 bh_subdivide( bh
, il
);
140 bh_subdivide( bh
, ir
);
143 VG_STATIC bh_tree
*bh_create( void *lin_alloc
, bh_system
*system
,
144 void *user
, u32 item_count
, u32 max_per_leaf
)
146 assert( max_per_leaf
> 0 );
148 if( item_count
== 0 )
150 bh_tree
*bh
= vg_linear_alloc( lin_alloc
, sizeof(bh_tree
) );
157 u32 totsize
= sizeof(bh_tree
) + sizeof(bh_node
)*(item_count
*2-1);
158 bh_tree
*bh
= vg_linear_alloc( lin_alloc
, totsize
);
161 bh
->max_per_leaf
= max_per_leaf
;
163 bh_node
*root
= &bh
->nodes
[0];
168 root
->count
= item_count
;
171 bh_update_bounds( bh
, 0 );
172 bh_subdivide( bh
, 0 );
174 totsize
= sizeof(bh_tree
) + sizeof(bh_node
) * bh
->node_count
;
175 bh
= vg_linear_resize( lin_alloc
, bh
, totsize
);
177 vg_success( "BVH done, size: %u/%u\n", bh
->node_count
, (item_count
*2-1) );
181 VG_STATIC
void bh_debug_node( bh_tree
*bh
, u32 inode
, v3f pos
, u32 colour
)
183 bh_node
*node
= &bh
->nodes
[ inode
];
185 if( (pos
[0] >= node
->bbx
[0][0] && pos
[0] <= node
->bbx
[1][0]) &&
186 (pos
[2] >= node
->bbx
[0][2] && pos
[2] <= node
->bbx
[1][2]) )
190 vg_line_boxf( node
->bbx
, colour
);
192 bh_debug_node( bh
, node
->il
, pos
, colour
);
193 bh_debug_node( bh
, node
->ir
, pos
, colour
);
197 vg_line_boxf( node
->bbx
, 0xff00ff00 );
199 if( bh
->system
->item_debug
)
201 for( u32 i
=0; i
<node
->count
; i
++ )
203 u32 idx
= node
->start
+i
;
204 bh
->system
->item_debug( bh
->user
, idx
);
211 VG_STATIC
int bh_ray( bh_tree
*bh
, v3f co
, v3f dir
, ray_hit
*hit
)
213 if( bh
->node_count
< 2 )
221 stack
[1] = bh
->nodes
[0].il
;
222 stack
[2] = bh
->nodes
[0].ir
;
226 bh_node
*inode
= &bh
->nodes
[ stack
[depth
] ];
227 if( ray_aabb( inode
->bbx
, co
, dir
, hit
->dist
) )
231 for( u32 i
=0; i
<inode
->count
; i
++ )
233 u32 idx
= inode
->start
+i
;
235 if( bh
->system
->cast_ray
)
236 count
+= bh
->system
->cast_ray( bh
->user
, idx
, co
, dir
, hit
);
245 if( depth
+1 >= vg_list_size(stack
) )
247 vg_error( "Maximum stack reached!\n" );
251 stack
[depth
] = inode
->il
;
252 stack
[depth
+1] = inode
->ir
;
265 typedef struct bh_iter bh_iter
;
277 VG_STATIC
void bh_iter_init( int root
, bh_iter
*it
)
279 it
->stack
[0].id
= root
;
280 it
->stack
[0].depth
= 0;
285 VG_STATIC
int bh_next( bh_tree
*bh
, bh_iter
*it
, boxf box
, int *em
)
287 while( it
->depth
>= 0 )
289 bh_node
*inode
= &bh
->nodes
[ it
->stack
[it
->depth
].id
];
291 if( box_overlap( inode
->bbx
, box
) )
295 if( it
->i
< inode
->count
)
297 *em
= inode
->start
+it
->i
;
309 if( it
->depth
+1 >= vg_list_size(it
->stack
) )
311 vg_error( "Maximum stack reached!\n" );
315 it
->stack
[it
->depth
].id
= inode
->il
;
316 it
->stack
[it
->depth
+1].id
= inode
->ir
;
330 VG_STATIC
int bh_closest_point( bh_tree
*bh
, v3f pos
,
331 v3f closest
, float max_dist
)
333 if( bh
->node_count
< 2 )
336 max_dist
= max_dist
*max_dist
;
346 bh_node
*inode
= &bh
->nodes
[ queue
[depth
] ];
349 closest_point_aabb( pos
, inode
->bbx
, p1
);
351 /* branch into node if its closer than current best */
352 float node_dist
= v3_dist2( pos
, p1
);
353 if( node_dist
< max_dist
)
357 for( int i
=0; i
<inode
->count
; i
++ )
360 bh
->system
->item_closest( bh
->user
, inode
->start
+i
, pos
, p2
);
362 float item_dist
= v3_dist2( pos
, p2
);
363 if( item_dist
< max_dist
)
365 max_dist
= item_dist
;
366 v3_copy( p2
, closest
);
367 best_item
= inode
->start
+i
;
375 queue
[depth
] = inode
->il
;
376 queue
[depth
+1] = inode
->ir
;