48 import matplotlib.pyplot
as plt
49 from matplotlib.patches
import PathPatch
50 from matplotlib.path
import Path
51 from matplotlib
import animation, cm, colors
55 ax = plt.axes(xlim=(-1, 1), ylim=(-1, 1))
56 ln, = plt.plot([], [], animated=
True)
58 def getPositions(pose, linkLength, color='red'):
59 angles = np.cumsum(pose)
60 return PathPatch(Path(
61 np.insert(linkLength * np.cumsum([np.cos(angles), np.sin(angles)], \
62 axis=1), 0, 0., axis=1).T), facecolor=
'none', edgecolor=color)
64 def drawEnvironment(env):
67 plt.gca().add_patch(env)
69 def drawPose(index, env, poses, linkLength):
72 cMap = cm.ScalarMappable(
73 norm=colors.Normalize(vmin=0, vmax=poses.shape[0] - 1),
74 cmap=plt.get_cmap(
'viridis'))
75 for (i, pose)
in enumerate(poses):
76 plt.gca().add_patch(getPositions(pose, linkLength, \
79 plt.gca().add_patch(getPositions(poses[index, :], linkLength))
82 def makeMovie(fname, env, poses, linkLength):
83 ani = animation.FuncAnimation(plt.gcf(), drawPose, \
84 fargs=(env, poses, linkLength), frames=poses.shape[0], \
86 ani.save(fname, bitrate=300, fps=20)
88 if __name__ ==
'__main__':
90 print(
'Usage: %s num_links [movie]' % argv[0])
97 coords = np.loadtxt(
'environment_%d.dat' % dims)
98 env = PathPatch(Path(coords), facecolor=
'none', edgecolor=
'black')
102 poses = np.loadtxt(
'kinematic_path_%d.dat' % dims)
103 linkLength = 1. / dims
106 makeMovie(
'kinematic_%d.mp4' % dims, env, poses, linkLength)
108 drawPose(-1, env, poses, linkLength)
110 fig.savefig(
'kinematic_%d.pdf' % dims)