Skip to content

Commit fe08e39

Browse files
committed
feat: ElasticBands reduce occupation
1 parent e784790 commit fe08e39

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

PathPlanning/ElasticBands/elastic_bands.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
# Visualization Params
3232
ENABLE_PLOT = True
3333
ENABLE_INTERACTIVE = False
34+
ENABLE_SAVE_DATA = False
3435
MAX_ITER = 50
3536

3637

@@ -144,6 +145,7 @@ def _maintain_overlap(self):
144145
class ElasticBandsVisualizer:
145146
def __init__(self):
146147
self.obstacles = np.zeros((500, 500))
148+
self.obstacles_points = []
147149
self.path_points = []
148150
self.elastic_band = None
149151
self.running = True
@@ -159,8 +161,12 @@ def __init__(self):
159161
# Connect mouse events
160162
self.fig.canvas.mpl_connect("button_press_event", self.on_click)
161163
else:
162-
self.path_points = np.load(pathlib.Path(__file__).parent / "points.npy")
163-
self.obstacles = np.load(pathlib.Path(__file__).parent / "obstacles.npy")
164+
self.path_points = np.load(pathlib.Path(__file__).parent / "path.npy")
165+
self.obstacles_points = np.load(
166+
pathlib.Path(__file__).parent / "obstacles.npy"
167+
)
168+
for x, y in self.obstacles_points:
169+
self.add_obstacle(x, y)
164170
self.plan_path()
165171

166172
self.plot_background()
@@ -216,6 +222,16 @@ def plot_background(self):
216222
plt.draw()
217223
plt.pause(0.01)
218224

225+
def add_obstacle(self, x, y):
226+
"""Add an obstacle at the given coordinates"""
227+
size = 30 # Side length of the square
228+
half_size = size // 2
229+
x_start = max(0, x - half_size)
230+
x_end = min(self.obstacles.shape[0], x + half_size)
231+
y_start = max(0, y - half_size)
232+
y_end = min(self.obstacles.shape[1], y + half_size)
233+
self.obstacles[x_start:x_end, y_start:y_end] = 1
234+
219235
def on_click(self, event):
220236
"""Handle mouse click events"""
221237
if event.inaxes != self.ax:
@@ -224,23 +240,22 @@ def on_click(self, event):
224240
x, y = int(event.xdata), int(event.ydata)
225241

226242
if event.button == 1: # Left click to add obstacles
227-
size = 30 # Side length of the square
228-
half_size = size // 2
229-
230-
# Ensure not out of the map boundary
231-
x_start = max(0, x - half_size)
232-
x_end = min(self.obstacles.shape[0], x + half_size)
233-
y_start = max(0, y - half_size)
234-
y_end = min(self.obstacles.shape[1], y + half_size)
235-
236-
# Set the square area as obstacles (value set to 1)
237-
self.obstacles[x_start:x_end, y_start:y_end] = 1
243+
self.add_obstacle(x, y)
244+
self.obstacles_points.append([x, y])
238245

239246
elif event.button == 3: # Right click to add path points
240247
self.path_points.append([x, y])
241248

242249
elif event.button == 2: # Middle click to end path input and start planning
243250
if len(self.path_points) >= 2:
251+
if ENABLE_SAVE_DATA:
252+
np.save(
253+
pathlib.Path(__file__).parent / "path.npy", self.path_points
254+
)
255+
np.save(
256+
pathlib.Path(__file__).parent / "obstacles.npy",
257+
self.obstacles_points,
258+
)
244259
self.plan_path()
245260

246261
self.plot_background()
-1.91 MB
Binary file not shown.

PathPlanning/ElasticBands/path.npy

400 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)