@@ -6,6 +6,8 @@ use super::{
66 mapf_post, IntersectionType , LeaderFollowerZones , MapfResult , SemanticPlan , SemanticWaypoint ,
77} ;
88
9+ use crate :: negotiation:: Scenario ;
10+
911#[ derive( Clone ) ]
1012pub struct CurrentPosition {
1113 pub semantic_position : SemanticWaypoint ,
@@ -20,6 +22,49 @@ pub struct Grid2D {
2022}
2123
2224impl Grid2D {
25+ pub fn from_scenario ( scenario : & Scenario ) -> Self {
26+ let mut max_x = 0i64 ;
27+ let mut max_y = 0i64 ;
28+
29+ for ( y, row) in & scenario. occupancy {
30+ max_y = max_y. max ( * y) ;
31+ for x in row {
32+ max_x = max_x. max ( * x) ;
33+ }
34+ }
35+
36+ for agent in scenario. agents . values ( ) {
37+ max_x = max_x. max ( agent. start [ 0 ] ) . max ( agent. goal [ 0 ] ) ;
38+ max_y = max_y. max ( agent. start [ 1 ] ) . max ( agent. goal [ 1 ] ) ;
39+ }
40+
41+ for obstacle in & scenario. obstacles {
42+ for ( _, x, y) in & obstacle. trajectory {
43+ max_x = max_x. max ( * x) ;
44+ max_y = max_y. max ( * y) ;
45+ }
46+ }
47+
48+ if let Some ( bounds) = scenario. camera_bounds {
49+ max_x = max_x. max ( ( bounds[ 1 ] [ 0 ] / scenario. cell_size as f32 ) as i64 ) ;
50+ max_y = max_y. max ( ( bounds[ 1 ] [ 1 ] / scenario. cell_size as f32 ) as i64 ) ;
51+ }
52+
53+ let width = ( max_x + 1 ) as usize ;
54+ let height = ( max_y + 1 ) as usize ;
55+
56+ let mut static_obstacles = vec ! [ vec![ 0i8 ; height] ; width] ;
57+ for ( y, row) in & scenario. occupancy {
58+ for x in row {
59+ if * x >= 0 && * y >= 0 {
60+ static_obstacles[ * x as usize ] [ height - 1 - * y as usize ] = 100 ;
61+ }
62+ }
63+ }
64+
65+ Self :: new ( static_obstacles, scenario. cell_size )
66+ }
67+
2368 pub fn new ( static_obstacles : Vec < Vec < i8 > > , cell_size : f64 ) -> Self {
2469 let width = static_obstacles. len ( ) ;
2570 let height = if width > 0 {
@@ -59,7 +104,7 @@ impl Grid2D {
59104 positions. iter ( ) . map ( |p| p. semantic_position ) . collect ( ) ;
60105 let assignment = semantic_plan. get_claim_dict ( & semantic_positions) ;
61106
62- let mut allocation_field = AllocationField :: create ( semantic_plan, 1000 , 1000 ) ;
107+ let mut allocation_field = AllocationField :: create ( semantic_plan, self . width , self . height ) ;
63108
64109 for ( agent, traj) in trajectories. trajectories . iter ( ) . enumerate ( ) {
65110 let Some ( region) = assignment. get ( & agent) else {
@@ -357,3 +402,50 @@ impl AllocationField {
357402 Some ( p. priority )
358403 }
359404}
405+
406+ impl From < & Scenario > for Grid2D {
407+ fn from ( scenario : & Scenario ) -> Self {
408+ Self :: from_scenario ( scenario)
409+ }
410+ }
411+
412+ #[ cfg( test) ]
413+ mod tests {
414+ use super :: * ;
415+ use std:: collections:: BTreeMap ;
416+
417+ #[ test]
418+ fn test_grid2d_from_scenario ( ) {
419+ let mut occupancy = HashMap :: new ( ) ;
420+ occupancy. insert ( 0 , vec ! [ 0 , 1 ] ) ; // y=0, x=0,1
421+ occupancy. insert ( 1 , vec ! [ 0 ] ) ; // y=1, x=0
422+
423+ let scenario = Scenario {
424+ agents : BTreeMap :: new ( ) ,
425+ obstacles : vec ! [ ] ,
426+ occupancy,
427+ cell_size : 1.0 ,
428+ camera_bounds : None ,
429+ } ;
430+
431+ let grid = Grid2D :: from_scenario ( & scenario) ;
432+
433+ // max_x = 1, max_y = 1
434+ // width = 2, height = 2
435+ assert_eq ! ( grid. width, 2 ) ;
436+ assert_eq ! ( grid. height, 2 ) ;
437+
438+ // In Grid2D, y_grid = height - 1 - y_scenario
439+ // y_scenario=0 -> y_grid=1
440+ // y_scenario=1 -> y_grid=0
441+
442+ // (0,0) scenario -> (0,1) grid
443+ assert_eq ! ( grid. static_obstacles[ 0 ] [ 1 ] , 100 ) ;
444+ // (1,0) scenario -> (1,1) grid
445+ assert_eq ! ( grid. static_obstacles[ 1 ] [ 1 ] , 100 ) ;
446+ // (0,1) scenario -> (0,0) grid
447+ assert_eq ! ( grid. static_obstacles[ 0 ] [ 0 ] , 100 ) ;
448+ // (1,1) scenario -> (1,0) grid (not occupied)
449+ assert_eq ! ( grid. static_obstacles[ 1 ] [ 0 ] , 0 ) ;
450+ }
451+ }
0 commit comments