1717
1818sys .path .append (str (pathlib .Path (__file__ ).parent .parent .parent ))
1919
20- from Mapping .DistanceMap .distance_map import compute_sdf
20+ from Mapping .DistanceMap .distance_map import compute_sdf_scipy
2121
2222# Elastic Bands Params
2323MAX_BUBBLE_RADIUS = 100
@@ -45,8 +45,13 @@ def __init__(self, position, radius):
4545
4646
4747class ElasticBands :
48- def __init__ (self , initial_path , obstacles , rho0 = RHO0 , kc = 0.05 , kr = - 0.1 ):
49- self .distance_map = compute_sdf (obstacles )
48+ def __init__ (self , initial_path , obstacles , rho0 = RHO0 , kc = KC , kr = KR ):
49+ import time
50+
51+ t1 = time .time ()
52+ self .distance_map = compute_sdf_scipy (obstacles )
53+ t2 = time .time ()
54+ print (f"Time taken to compute distance map: { t2 - t1 } seconds" )
5055 self .bubbles = [
5156 Bubble (p , self .compute_rho (p )) for p in initial_path
5257 ] # Initialize bubble chain
@@ -73,7 +78,7 @@ def contraction_force(self, i):
7378 dir_next = (next_ - current ) / (np .linalg .norm (next_ - current ) + 1e-6 )
7479 return self .kc * (dir_prev + dir_next )
7580
76- def external_force (self , i ):
81+ def repulsive_force (self , i ):
7782 """Calculate external repulsive force for the i-th bubble"""
7883 h = STEP_SIZE # Step size
7984 b = self .bubbles [i ].pos
@@ -99,7 +104,7 @@ def update_bubbles(self):
99104 new_bubbles .append (self .bubbles [i ]) # Fixed start and end points
100105 continue
101106
102- f_total = self .contraction_force (i ) + self .external_force (i )
107+ f_total = self .contraction_force (i ) + self .repulsive_force (i )
103108 alpha = self .bubbles [i ].radius # Adaptive step size
104109 new_pos = self .bubbles [i ].pos + alpha * f_total
105110 new_pos = np .clip (new_pos , 0 , 499 )
@@ -146,10 +151,11 @@ def __init__(self):
146151 self .obstacles = np .zeros ((500 , 500 ))
147152 self .path_points = []
148153 self .elastic_band = None
154+ self .running = True
149155
150156 if ENABLE_PLOT :
151157 self .fig , self .ax = plt .subplots (figsize = (8 , 8 ))
152- # Set the display range of the graph
158+ self . fig . canvas . mpl_connect ( "close_event" , self . on_close )
153159 self .ax .set_xlim (0 , 500 )
154160 self .ax .set_ylim (0 , 500 )
155161
@@ -164,15 +170,32 @@ def __init__(self):
164170
165171 self .plot_background ()
166172
173+ def on_close (self , event ):
174+ """Handle window close event"""
175+ self .running = False
176+ plt .close ("all" ) # Close all figure windows
177+
167178 def plot_background (self ):
168179 """Plot the background grid"""
169- if not ENABLE_PLOT :
180+ if not ENABLE_PLOT or not self . running :
170181 return
171182
172183 self .ax .cla ()
173184 self .ax .set_xlim (0 , 500 )
174185 self .ax .set_ylim (0 , 500 )
175186 self .ax .grid (True )
187+
188+ if ENABLE_INTERACTIVE :
189+ self .ax .set_title (
190+ "Elastic Bands Path Planning\n "
191+ "Left click: Add obstacles\n "
192+ "Right click: Add path points\n "
193+ "Middle click: Start planning" ,
194+ pad = 20 ,
195+ )
196+ else :
197+ self .ax .set_title ("Elastic Bands Path Planning" , pad = 20 )
198+
176199 if self .path_points :
177200 self .ax .plot (
178201 [p [0 ] for p in self .path_points ],
@@ -240,6 +263,6 @@ def plan_path(self):
240263
241264
242265if __name__ == "__main__" :
243- _ = ElasticBandsVisualizer ()
266+ visualizer = ElasticBandsVisualizer ()
244267 if ENABLE_PLOT :
245- plt .show ()
268+ plt .show (block = True )
0 commit comments