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
;
40 /* if il is 0, this is a leaf */
42 union{ u32 ir
, start
; };
49 void (*expand_bound
)( void *user
, boxf bound
, u32 item_index
);
50 float (*item_centroid
)( void *user
, u32 item_index
, int axis
);
51 void (*item_swap
)( void *user
, u32 ia
, u32 ib
);
55 * item_debug - draw this item quickly usually with lines
56 * cast_ray - shoot a ray against the object, if this is not set,
57 * raycasts will simply return the hit on the bvh node
60 void (*item_debug
)( void *user
, u32 item_index
);
61 int (*cast_ray
)( void *user
, u32 index
, v3f co
, v3f dir
, ray_hit
*hit
);
64 VG_STATIC
void bh_update_bounds( bh_tree
*bh
, u32 inode
)
66 bh_node
*node
= &bh
->nodes
[ inode
];
68 box_init_inf( node
->bbx
);
69 for( u32 i
=0; i
<node
->count
; i
++ )
71 u32 idx
= node
->start
+i
;
72 bh
->system
->expand_bound( bh
->user
, node
->bbx
, idx
);
76 VG_STATIC
void bh_subdivide( bh_tree
*bh
, u32 inode
)
78 bh_node
*node
= &bh
->nodes
[ inode
];
81 v3_sub( node
->bbx
[1], node
->bbx
[0], extent
);
84 if( extent
[1] > extent
[0] ) axis
= 1;
85 if( extent
[2] > extent
[axis
] ) axis
= 2;
87 float split
= node
->bbx
[0][axis
] + extent
[axis
]*0.5f
;
90 for( u32 t
=0; t
<node
->count
; t
++ )
92 u32 idx
= node
->start
+t
;
93 avg
+= bh
->system
->item_centroid( bh
->user
, idx
, axis
);
95 avg
/= (float)node
->count
;
100 j
= i
+ node
->count
-1;
104 if( bh
->system
->item_centroid( bh
->user
, i
, axis
) < split
)
108 bh
->system
->item_swap( bh
->user
, i
, j
);
113 u32 left_count
= i
- node
->start
;
114 if( left_count
== 0 || left_count
== node
->count
) return;
116 u32 il
= bh
->node_count
++,
117 ir
= bh
->node_count
++;
119 bh_node
*lnode
= &bh
->nodes
[il
],
120 *rnode
= &bh
->nodes
[ir
];
122 lnode
->start
= node
->start
;
123 lnode
->count
= left_count
;
125 rnode
->count
= node
->count
- left_count
;
131 bh_update_bounds( bh
, il
);
132 bh_update_bounds( bh
, ir
);
133 bh_subdivide( bh
, il
);
134 bh_subdivide( bh
, ir
);
137 VG_STATIC bh_tree
*bh_create( void *lin_alloc
, bh_system
*system
,
138 void *user
, u32 item_count
)
140 if( item_count
== 0 )
142 bh_tree
*bh
= vg_linear_alloc( lin_alloc
, sizeof(bh_tree
) );
149 u32 totsize
= sizeof(bh_tree
) + sizeof(bh_node
)*(item_count
*2-1);
150 bh_tree
*bh
= vg_linear_alloc( lin_alloc
, totsize
);
154 bh_node
*root
= &bh
->nodes
[0];
159 root
->count
= item_count
;
162 bh_update_bounds( bh
, 0 );
163 bh_subdivide( bh
, 0 );
165 totsize
= sizeof(bh_tree
) + sizeof(bh_node
) * bh
->node_count
;
166 bh
= vg_linear_resize( lin_alloc
, bh
, totsize
);
168 vg_success( "BVH done, size: %u/%u\n", bh
->node_count
, (item_count
*2-1) );
172 VG_STATIC
void bh_debug_node( bh_tree
*bh
, u32 inode
, v3f pos
, u32 colour
)
174 bh_node
*node
= &bh
->nodes
[ inode
];
176 if( (pos
[0] >= node
->bbx
[0][0] && pos
[0] <= node
->bbx
[1][0]) &&
177 (pos
[2] >= node
->bbx
[0][2] && pos
[2] <= node
->bbx
[1][2]) )
181 vg_line_boxf( node
->bbx
, colour
);
183 bh_debug_node( bh
, node
->il
, pos
, colour
);
184 bh_debug_node( bh
, node
->ir
, pos
, colour
);
188 vg_line_boxf( node
->bbx
, 0xff00ff00 );
190 if( bh
->system
->item_debug
)
192 for( u32 i
=0; i
<node
->count
; i
++ )
194 u32 idx
= node
->start
+i
;
195 bh
->system
->item_debug( bh
->user
, idx
);
202 VG_STATIC
int bh_ray( bh_tree
*bh
, v3f co
, v3f dir
, ray_hit
*hit
)
204 if( bh
->node_count
< 2 )
212 stack
[1] = bh
->nodes
[0].il
;
213 stack
[2] = bh
->nodes
[0].ir
;
217 bh_node
*inode
= &bh
->nodes
[ stack
[depth
] ];
218 if( ray_aabb( inode
->bbx
, co
, dir
, hit
->dist
) )
222 for( u32 i
=0; i
<inode
->count
; i
++ )
224 u32 idx
= inode
->start
+i
;
226 if( bh
->system
->cast_ray
)
227 count
+= bh
->system
->cast_ray( bh
->user
, idx
, co
, dir
, hit
);
236 if( depth
+1 >= vg_list_size(stack
) )
238 vg_error( "Maximum stack reached!\n" );
242 stack
[depth
] = inode
->il
;
243 stack
[depth
+1] = inode
->ir
;
256 VG_STATIC
int bh_select( bh_tree
*bh
, boxf box
, u32
*buffer
, int len
)
258 if( bh
->node_count
< 2 )
266 stack
[1] = bh
->nodes
[0].il
;
267 stack
[2] = bh
->nodes
[0].ir
;
271 bh_node
*inode
= &bh
->nodes
[ stack
[depth
] ];
272 if( box_overlap( inode
->bbx
, box
) )
276 if( count
+ inode
->count
>= len
)
279 for( u32 i
=0; i
<inode
->count
; i
++ )
280 buffer
[ count
++ ] = inode
->start
+i
;
286 if( depth
+1 >= vg_list_size(stack
) )
288 vg_error( "Maximum stack reached!\n" );
292 stack
[depth
] = inode
->il
;
293 stack
[depth
+1] = inode
->ir
;