@@ -31,6 +31,7 @@ class Animate:
3131 - ``plot``
3232 - ``quiver``
3333 - ``text``
34+ - ``scatter``
3435
3536 which renders them and also places corresponding objects into a display
3637 list. These objects are ``Line``, ``Quiver`` and ``Text``. Only these
@@ -43,26 +44,26 @@ class Animate:
4344
4445 anim = animate.Animate(dims=[0,2]) # set up the 3D axes
4546 anim.trplot(T, frame='A', color='green') # draw the frame
46- anim.run(loop =True) # animate it
47+ anim.run(repeat =True) # animate it
4748 """
4849
4950 def __init__ (
5051 self ,
51- axes : Optional [plt .Axes ] = None ,
52- dims : Optional [ArrayLike ] = None ,
52+ ax : Optional [plt .Axes ] = None ,
53+ dim : Optional [ArrayLike ] = None ,
5354 projection : Optional [str ] = "ortho" ,
5455 labels : Optional [Tuple [str , str , str ]] = ("X" , "Y" , "Z" ),
5556 ** kwargs ,
5657 ):
5758 """
5859 Construct an Animate object
5960
60- :param axes : the axes to plot into, defaults to current axes
61- :type axes : Axes3D reference
62- :param dims : dimension of plot volume as [xmin, xmax, ymin, ymax,
61+ :param ax : the axes to plot into, defaults to current axes
62+ :type ax : Axes3D reference
63+ :param dim : dimension of plot volume as [xmin, xmax, ymin, ymax,
6364 zmin, zmax]. If dims is [min, max] those limits are applied
6465 to the x-, y- and z-axes.
65- :type dims : array_like(6) or array_like(2)
66+ :type dim : array_like(6) or array_like(2)
6667 :param projection: 3D projection: ortho [default] or persp
6768 :type projection: str
6869 :param labels: labels for the axes, defaults to X, Y and Z
@@ -74,36 +75,37 @@ def __init__(
7475 self .trajectory = None
7576 self .displaylist = []
7677
77- if axes is None :
78- # no axes specified
79-
80- fig = plt .gcf ()
81- # check any current axes
82- for a in fig .axes :
83- if a .name != "3d" :
84- # if they are not 3D axes, remove them, otherwise will
85- # get plot errors
86- a .remove ()
87- if len (fig .axes ) == 0 :
88- # no axes in the figure, create a 3D axes
89- axes = fig .add_subplot (111 , projection = "3d" , proj_type = projection )
90- axes .set_xlabel (labels [0 ])
91- axes .set_ylabel (labels [1 ])
92- axes .set_zlabel (labels [2 ])
93- axes .autoscale (enable = True , axis = "both" )
94- else :
95- # reuse an existing axis
96- axes = plt .gca ()
97-
98- if dims is not None :
99- if len (dims ) == 2 :
100- dims = dims * 3
101- axes .set_xlim (dims [0 :2 ])
102- axes .set_ylim (dims [2 :4 ])
103- axes .set_zlim (dims [4 :6 ])
104- # ax.set_aspect('equal')
105-
106- self .ax = axes
78+ if ax is None :
79+ # # no axes specified
80+
81+ # fig = plt.gcf()
82+ # # check any current axes
83+ # for a in fig.axes:
84+ # if a.name != "3d":
85+ # # if they are not 3D axes, remove them, otherwise will
86+ # # get plot errors
87+ # a.remove()
88+ # if len(fig.axes) == 0:
89+ # # no axes in the figure, create a 3D axes
90+ # axes = fig.add_subplot(111, projection="3d", proj_type=projection)
91+ # ax.set_xlabel(labels[0])
92+ # ax.set_ylabel(labels[1])
93+ # ax.set_zlabel(labels[2])
94+ # ax.autoscale(enable=True, axis="both")
95+ # else:
96+ # # reuse an existing axis
97+ # axes = plt.gca()
98+
99+ # if dims is not None:
100+ # if len(dims) == 2:
101+ # dims = dims * 3
102+ # ax.set_xlim(dims[0:2])
103+ # ax.set_ylim(dims[2:4])
104+ # ax.set_zlim(dims[4:6])
105+ # # ax.set_aspect('equal')
106+ ax = smb .plotvol3 (ax = ax , dim = dim )
107+
108+ self .ax = ax
107109
108110 # TODO set flag for 2d or 3d axes, flag errors on the methods called later
109111
@@ -163,10 +165,10 @@ def run(
163165 self ,
164166 movie : Optional [str ] = None ,
165167 axes : Optional [plt .Axes ] = None ,
166- repeat : Optional [ bool ] = False ,
167- interval : Optional [ int ] = 50 ,
168- nframes : Optional [ int ] = 100 ,
169- wait : Optional [ bool ] = False ,
168+ repeat : bool = False ,
169+ interval : int = 50 ,
170+ nframes : int = 100 ,
171+ wait : bool = False ,
170172 ** kwargs ,
171173 ):
172174 """
@@ -209,18 +211,14 @@ def update(frame, animation):
209211 else :
210212 # assume it is an SO(3) or SE(3)
211213 T = frame
212-
213214 # ensure result is SE(3)
214215 if T .shape == (3 , 3 ):
215216 T = smb .r2t (T )
216217
217218 # update the scene
218219 animation ._draw (T )
219-
220220 self .count += 1 # say we're still running
221221
222- return animation .artists ()
223-
224222 if movie is not None :
225223 repeat = False
226224
@@ -234,13 +232,13 @@ def update(frame, animation):
234232 frames = iter (np .linspace (0 , 1 , nframes ))
235233
236234 global _ani
237- fig = plt . gcf ()
235+ fig = self . ax . get_figure ()
238236 _ani = animation .FuncAnimation (
239237 fig = fig ,
240238 func = update ,
241239 frames = frames ,
242240 fargs = (self ,),
243- blit = False , # blit leaves a trail and first frame, set to False
241+ # blit=False, # blit leaves a trail and first frame, set to False
244242 interval = interval ,
245243 repeat = repeat ,
246244 save_count = nframes ,
@@ -597,13 +595,20 @@ def trplot2(
597595 smb .trplot2 (self .start , ax = self , block = False , ** kwargs )
598596
599597 def run (
600- self , movie = None , axes = None , repeat = False , interval = 50 , nframes = 100 , ** kwargs
598+ self ,
599+ movie : Optional [str ] = None ,
600+ axes : Optional [plt .Axes ] = None ,
601+ repeat : bool = False ,
602+ interval : int = 50 ,
603+ nframes : int = 100 ,
604+ wait : bool = False ,
605+ ** kwargs
601606 ):
602607 """
603608 Run the animation
604609
605610 :param axes: the axes to plot into, defaults to current axes
606- :type axes: Axes3D reference
611+ :type axes: Axes reference
607612 :param nframes: number of steps in the animation [defaault 100]
608613 :type nframes: int
609614 :param repeat: animate in endless loop [default False]
@@ -616,7 +621,7 @@ def run(
616621 :rtype: Matplotlib animation object
617622
618623 Animates a 3D coordinate frame moving from the world frame to a frame
619- represented by the SO(3 ) or SE(3 ) matrix to the current axes.
624+ represented by the SO(2 ) or SE(2 ) matrix to the current axes.
620625
621626 .. note::
622627
@@ -627,35 +632,50 @@ def run(
627632 - invokes the draw() method of every object in the display list
628633 """
629634
630- def update (frame , a ):
631- # if contains trajectory:
632- if self .trajectory is not None :
633- T = self .trajectory [frame ]
635+ def update (frame , animation ):
636+ # frame is the result of calling next() on a iterator or generator
637+ # seemingly the animation framework isn't checking StopException
638+ # so there is no way to know when this is no longer called.
639+ # we implement a rather hacky heartbeat style timeout
640+
641+ if isinstance (frame , float ):
642+ # passed a single transform, interpolate it
643+ T = smb .trinterp2 (start = self .start , end = self .end , s = frame )
634644 else :
635- T = base .trinterp2 (start = self .start , end = self .end , s = frame / nframes )
636- a ._draw (T )
637- if frame == nframes - 1 :
638- a .done = True
639- return a .artists ()
645+ # assume it is an SO(2) or SE(2)
646+ T = frame
647+ # ensure result is SE(2)
648+ if T .shape == (2 , 2 ):
649+ T = smb .r2t (T )
650+
651+ # update the scene
652+ animation ._draw (T )
653+ self .count += 1 # say we're still running
654+
640655
641- # blit leaves a trail and first frame
642656 if movie is not None :
643657 repeat = False
644658
645- self .done = False
659+ self .count = 1
646660 if self .trajectory is not None :
647- nframes = len (self .trajectory )
661+ if not isinstance (self .trajectory , Iterator ):
662+ # make it iterable, eg. if a list or tuple
663+ self .trajectory = iter (self .trajectory )
664+ frames = self .trajectory
665+ else :
666+ frames = iter (np .linspace (0 , 1 , nframes ))
648667
649668 global _ani
650- fig = plt . gcf ()
669+ fig = self . ax . get_figure ()
651670 _ani = animation .FuncAnimation (
652671 fig = fig ,
653672 func = update ,
654- frames = range ( 0 , nframes ) ,
673+ frames = frames ,
655674 fargs = (self ,),
656- blit = False ,
675+ # blit=False,
657676 interval = interval ,
658677 repeat = repeat ,
678+ save_count = nframes ,
659679 )
660680
661681 if movie is True :
@@ -667,9 +687,17 @@ def update(frame, a):
667687 print ("overwriting movie" , movie )
668688 else :
669689 print ("creating movie" , movie )
670- FFwriter = animation .FFMpegWriter (fps = 10 , extra_args = ["-vcodec" , "libx264" ])
690+ FFwriter = animation .FFMpegWriter (fps = 1000 / interval , extra_args = ["-vcodec" , "libx264" ])
671691 _ani .save (movie , writer = FFwriter )
672692
693+ if wait :
694+ # wait for the animation to finish. Dig into the timer for this
695+ # animation and wait for its callback to be deregistered.
696+ while True :
697+ plt .pause (0.25 )
698+ if _ani .event_source is None or len (_ani .event_source .callbacks ) == 0 :
699+ break
700+
673701 return _ani
674702
675703 def __repr__ (self ):
@@ -865,8 +893,14 @@ def set_ylabel(self, *args, **kwargs):
865893
866894 from spatialmath import base
867895
868- # T = base.rpy2r(0.3, 0.4, 0.5)
869- # base.tranimate(T, wait=True)
870-
871- T = base .rot2 (2 )
872- base .tranimate2 (T , wait = True )
896+ # T = smb.rpy2r(0.3, 0.4, 0.5)
897+ # # smb.tranimate(T, wait=True)
898+ # s = smb.tranimate(T, movie=True)
899+ # with open("zz.html", "w") as f:
900+ # print(f"<html>{s}</html>", file=f)
901+
902+ T = smb .rot2 (2 )
903+ # smb.tranimate2(T, wait=True)
904+ s = smb .tranimate2 (T , movie = True )
905+ with open ("zz.html" , "w" ) as f :
906+ print (f"<html>{ s } </html>" , file = f )
0 commit comments