@@ -554,7 +554,9 @@ where
554554 for x in 0 ..=cell_count {
555555 let cell = [ x, y, z] ;
556556 let cell_idx = grid. get_cell_idx ( & cell) ;
557- intersections[ cell_idx] [ 0 ] += 1 ;
557+ if cell_idx < intersections. len ( ) {
558+ intersections[ cell_idx] [ 0 ] += 1 ;
559+ }
558560 }
559561 }
560562 }
@@ -576,7 +578,9 @@ where
576578 for y in 0 ..=cell_count {
577579 let cell = [ x, y, z] ;
578580 let cell_idx = grid. get_cell_idx ( & cell) ;
579- intersections[ cell_idx] [ 1 ] += 1 ;
581+ if cell_idx < intersections. len ( ) {
582+ intersections[ cell_idx] [ 1 ] += 1 ;
583+ }
580584 }
581585 }
582586 }
@@ -598,7 +602,9 @@ where
598602 for z in 0 ..=cell_count {
599603 let cell = [ x, y, z] ;
600604 let cell_idx = grid. get_cell_idx ( & cell) ;
601- intersections[ cell_idx] [ 2 ] += 1 ;
605+ if cell_idx < intersections. len ( ) {
606+ intersections[ cell_idx] [ 2 ] += 1 ;
607+ }
602608 }
603609 }
604610 }
@@ -774,6 +780,42 @@ mod tests {
774780 }
775781 }
776782
783+ // Make sure the raycasts on grid do not access out of bounds cells.
784+ #[ test]
785+ fn test_grid_raycast ( ) {
786+ let model = & easy_gltf:: load ( "assets/ferris3d.glb" ) . unwrap ( ) [ 0 ] . models [ 0 ] ;
787+ let vertices = model. vertices ( ) . iter ( ) . map ( |v| v. position ) . collect_vec ( ) ;
788+ let indices = model. indices ( ) . unwrap ( ) ;
789+
790+ let bbox_min = vertices. iter ( ) . fold (
791+ cgmath:: Vector3 :: new ( f32:: MAX , f32:: MAX , f32:: MAX ) ,
792+ |acc, v| cgmath:: Vector3 {
793+ x : acc. x . min ( v. x ) ,
794+ y : acc. y . min ( v. y ) ,
795+ z : acc. z . min ( v. z ) ,
796+ } ,
797+ ) ;
798+ let mut bbox_max = vertices. iter ( ) . fold (
799+ cgmath:: Vector3 :: new ( -f32:: MAX , -f32:: MAX , -f32:: MAX ) ,
800+ |acc, v| cgmath:: Vector3 {
801+ x : acc. x . max ( v. x ) ,
802+ y : acc. y . max ( v. y ) ,
803+ z : acc. z . max ( v. z ) ,
804+ } ,
805+ ) ;
806+
807+ bbox_max *= 0.5 ;
808+
809+ let grid = Grid :: from_bounding_box ( & bbox_min, & bbox_max, [ 32 , 32 , 32 ] ) ;
810+
811+ generate_grid_sdf (
812+ & vertices,
813+ crate :: Topology :: TriangleList ( Some ( & indices) ) ,
814+ & grid,
815+ SignMethod :: Raycast ,
816+ ) ;
817+ }
818+
777819 #[ test]
778820 fn test_topology ( ) {
779821 let grid = Grid :: from_bounding_box ( & [ 0. , 0. , 0. ] , & [ 5. , 5. , 5. ] , [ 25 , 25 , 25 ] ) ;
0 commit comments