1
+ import imageio .v2 as imageio
1
2
import numpy as np
2
3
import matplotlib .pyplot as plt
3
4
from PathPlanning .TimeBasedPathPlanning .GridWithDynamicObstacles import (
@@ -44,7 +45,7 @@ def PlotNodePath(grid: Grid, start: Position, goal: Position, path: NodePath):
44
45
'''
45
46
Plot a series of agent paths.
46
47
'''
47
- def PlotNodePaths (grid : Grid , start_and_goals : list [StartAndGoal ], paths : list [NodePath ]):
48
+ def PlotNodePaths (grid : Grid , start_and_goals : list [StartAndGoal ], paths : list [NodePath ], save_gif : bool = False ):
48
49
fig = plt .figure (figsize = (10 , 7 ))
49
50
50
51
ax = fig .add_subplot (
@@ -98,7 +99,12 @@ def PlotNodePaths(grid: Grid, start_and_goals: list[StartAndGoal], paths: list[N
98
99
max_time = max (path .goal_reached_time () for path in paths )
99
100
100
101
# Animation loop
102
+ frames = []
101
103
for i in range (0 , max_time + 1 ):
104
+ if save_gif :
105
+ plt .savefig (f"frame_{ i :03d} .png" ) # Save each frame as an image
106
+ frames .append (imageio .imread (f"frame_{ i :03d} .png" ))
107
+
102
108
# Update obstacle positions
103
109
obs_positions = grid .get_obstacle_positions_at_time (i )
104
110
obs_points .set_data (obs_positions [0 ], obs_positions [1 ])
@@ -122,4 +128,7 @@ def PlotNodePaths(grid: Grid, start_and_goals: list[StartAndGoal], paths: list[N
122
128
123
129
plt .pause (0.2 )
124
130
131
+ if save_gif :
132
+ imageio .mimsave ("path_animation2.gif" , frames , fps = 5 ) # Convert images to GIF
133
+
125
134
plt .show ()
0 commit comments