1313import matplotlib .pyplot as plt
1414from matplotlib import animation
1515from spatialmath import base
16+ from collections .abc import Iterable
1617
1718
1819class Animate :
@@ -57,6 +58,7 @@ def __init__(self, axes=None, dims=None, projection='ortho', labels=('X', 'Y', '
5758 Will setup to plot into an existing or a new Axes3D instance.
5859
5960 """
61+ self .trajectory = None
6062 self .displaylist = []
6163
6264 if axes is None :
@@ -104,6 +106,14 @@ def trplot(self, end, start=None, **kwargs):
104106 :seealso: :func:`run`
105107
106108 """
109+ if isinstance (end , Iterable ):
110+ if len (end ) == 1 :
111+ end = end [0 ]
112+ elif len (end ) >= 2 :
113+ self .trajectory = end
114+ start = self .trajectory [0 ]
115+ end = self .trajectory [- 1 ]
116+
107117 # stash the final value
108118 if base .isrot (end ):
109119 self .end = base .r2t (end )
@@ -129,7 +139,7 @@ def run(self, movie=None, axes=None, repeat=False, interval=50, nframes=100, pau
129139 :type axes: Axes3D reference
130140 :param repeat: animate in endless loop [default False]
131141 :type repeat: bool
132- :param nframes: number of steps in the animation [defaault 100]
142+ :param nframes: number of steps in the animation [default 100]
133143 :type nframes: int
134144 :param interval: number of milliseconds between frames [default 50]
135145 :type interval: int
@@ -147,7 +157,11 @@ def run(self, movie=None, axes=None, repeat=False, interval=50, nframes=100, pau
147157 """
148158
149159 def update (frame , a ):
150- T = base .trinterp (start = self .start , end = self .end , s = frame / nframes )
160+ # if contains trajectory:
161+ if self .trajectory is not None :
162+ T = self .trajectory [frame ]
163+ else :
164+ T = base .trinterp (start = self .start , end = self .end , s = frame / nframes )
151165 a ._draw (T )
152166 if frame == nframes - 1 :
153167 a .done = True
@@ -158,6 +172,8 @@ def update(frame, a):
158172 repeat = False
159173
160174 self .done = False
175+ if self .trajectory is not None :
176+ nframes = len (self .trajectory )
161177 ani = animation .FuncAnimation (fig = plt .gcf (), func = update , frames = range (0 , nframes ), fargs = (self ,), blit = False , interval = interval , repeat = repeat )
162178 if movie is None :
163179 while repeat or not self .done :
0 commit comments