@@ -32,6 +32,15 @@ def __init__(self, x, y):
3232            self .path_y  =  []
3333            self .parent  =  None 
3434
35+     class  AreaBounds :
36+ 
37+         def  __init__ (self , area ):
38+             self .xmin  =  float (area [0 ])
39+             self .xmax  =  float (area [1 ])
40+             self .ymin  =  float (area [2 ])
41+             self .ymax  =  float (area [3 ])
42+ 
43+ 
3544    def  __init__ (self ,
3645                 start ,
3746                 goal ,
@@ -40,20 +49,27 @@ def __init__(self,
4049                 expand_dis = 3.0 ,
4150                 path_resolution = 0.5 ,
4251                 goal_sample_rate = 5 ,
43-                  max_iter = 500 ):
52+                  max_iter = 500 ,
53+                  play_area = None 
54+                  ):
4455        """ 
4556        Setting Parameter 
4657
4758        start:Start Position [x,y] 
4859        goal:Goal Position [x,y] 
4960        obstacleList:obstacle Positions [[x,y,size],...] 
5061        randArea:Random Sampling Area [min,max] 
62+         play_area:stay inside this area [xmin,xmax,ymin,ymax] 
5163
5264        """ 
5365        self .start  =  self .Node (start [0 ], start [1 ])
5466        self .end  =  self .Node (goal [0 ], goal [1 ])
5567        self .min_rand  =  rand_area [0 ]
5668        self .max_rand  =  rand_area [1 ]
69+         if  play_area  is  not None :
70+             self .play_area  =  self .AreaBounds (play_area )
71+         else :
72+             self .play_area  =  None 
5773        self .expand_dis  =  expand_dis 
5874        self .path_resolution  =  path_resolution 
5975        self .goal_sample_rate  =  goal_sample_rate 
@@ -76,7 +92,8 @@ def planning(self, animation=True):
7692
7793            new_node  =  self .steer (nearest_node , rnd_node , self .expand_dis )
7894
79-             if  self .check_collision (new_node , self .obstacle_list ):
95+             if  self .check_if_outside_play_area (new_node , self .play_area ) and  \
96+                self .check_collision (new_node , self .obstacle_list ):
8097                self .node_list .append (new_node )
8198
8299            if  animation  and  i  %  5  ==  0 :
@@ -163,6 +180,15 @@ def draw_graph(self, rnd=None):
163180        for  (ox , oy , size ) in  self .obstacle_list :
164181            self .plot_circle (ox , oy , size )
165182
183+         if  self .play_area  is  not None :
184+             plt .plot ([self .play_area .xmin , self .play_area .xmax ,
185+                       self .play_area .xmax , self .play_area .xmin ,
186+                       self .play_area .xmin ],
187+                      [self .play_area .ymin , self .play_area .ymin ,
188+                       self .play_area .ymax , self .play_area .ymax ,
189+                       self .play_area .ymin ],
190+                      "-k" )
191+ 
166192        plt .plot (self .start .x , self .start .y , "xr" )
167193        plt .plot (self .end .x , self .end .y , "xr" )
168194        plt .axis ("equal" )
@@ -186,6 +212,18 @@ def get_nearest_node_index(node_list, rnd_node):
186212
187213        return  minind 
188214
215+     @staticmethod  
216+     def  check_if_outside_play_area (node , play_area ):
217+ 
218+         if  play_area  is  None :
219+             return  True   # no play_area was defined, every pos should be ok 
220+ 
221+         if  node .x  <  play_area .xmin  or  node .x  >  play_area .xmax  or  \
222+            node .y  <  play_area .ymin  or  node .y  >  play_area .ymax :
223+             return  False   # outside - bad 
224+         else :
225+             return  True   # inside - ok 
226+ 
189227    @staticmethod  
190228    def  check_collision (node , obstacleList ):
191229
@@ -222,7 +260,9 @@ def main(gx=6.0, gy=10.0):
222260        start = [0 , 0 ],
223261        goal = [gx , gy ],
224262        rand_area = [- 2 , 15 ],
225-         obstacle_list = obstacleList )
263+         obstacle_list = obstacleList ,
264+         # play_area=[0, 10, 0, 14] 
265+         )
226266    path  =  rrt .planning (animation = show_animation )
227267
228268    if  path  is  None :
0 commit comments