Skip to content

Commit 16710e9

Browse files
committed
integrate animation classes with axes_logic
1 parent 74d1ba0 commit 16710e9

4 files changed

Lines changed: 61 additions & 24 deletions

File tree

‎spatialmath/base/animate.py‎

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def trplot(self, end, start=None, **kwargs):
118118
:seealso: :func:`run`
119119
120120
"""
121+
self.trajectory = None
121122
if not isinstance(end, (np.ndarray, np.generic)) and isinstance(end, Iterable):
122123
try:
123124
if len(end) == 1:
@@ -143,7 +144,7 @@ def trplot(self, end, start=None, **kwargs):
143144
self.start = start
144145

145146
# draw axes at the origin
146-
base.trplot(self.start, axes=self, **kwargs)
147+
base.trplot(self.start, ax=self, **kwargs)
147148

148149
def run(
149150
self,
@@ -280,8 +281,9 @@ def _draw(self, T):
280281

281282
class _Line:
282283
def __init__(self, anim, h, xs, ys, zs):
283-
# form 4x2 matrix, columns are first/last point in homogeneous form
284-
self.p = np.vstack([xs, ys, zs, [1, 1]])
284+
# form 4xN matrix, columns are first/last point in homogeneous form
285+
p = np.vstack([xs, ys, zs])
286+
self.p = np.vstack([p, np.ones((p.shape[1],))])
285287
self.h = h
286288
self.type = "line"
287289
self.anim = anim
@@ -311,6 +313,7 @@ def plot(self, x, y, z, *args, **kwargs):
311313

312314
(h,) = self.ax.plot(x, y, z, *args, **kwargs)
313315
self.displaylist.append(Animate._Line(self, h, x, y, z))
316+
return h
314317

315318
# ------------------- quiver()
316319

@@ -416,8 +419,9 @@ def text(self, x, y, z, *args, **kwargs):
416419

417420
# ------------------- scatter()
418421

419-
def scatter(self, **kwargs):
420-
pass
422+
def scatter(self, xs, ys, zs, s=0, **kwargs):
423+
h = self.plot(xs, ys, zs, '.', markersize=0, **kwargs)
424+
self.displaylist.append(Animate._Line(self, h, xs, ys, zs))
421425

422426
# ------------------- wrappers for Axes primitives
423427

@@ -503,6 +507,8 @@ def __init__(self, axes=None, dims=None, labels=("X", "Y"), **kwargs):
503507
axes.set_xlim(dims[0:2])
504508
axes.set_ylim(dims[2:4])
505509
# ax.set_aspect('equal')
510+
else:
511+
axes.autoscale(enable=True, axis='both')
506512

507513
self.ax = axes
508514

@@ -544,7 +550,7 @@ def trplot2(self, end, start=None, **kwargs):
544550
self.start = start
545551

546552
# draw axes at the origin
547-
base.trplot2(self.start, axes=self, block=False, **kwargs)
553+
base.trplot2(self.start, ax=self, block=False, **kwargs)
548554

549555
def run(
550556
self, movie=None, axes=None, repeat=False, interval=50, nframes=100, **kwargs
@@ -641,10 +647,18 @@ def _draw(self, T):
641647

642648
# ------------------- plot()
643649

650+
def set_aspect(self, *args, **kwargs):
651+
self.ax.set_aspect(*args, **kwargs)
652+
653+
def autoscale(self, *args, **kwargs):
654+
#self.ax.autoscale(*args, **kwargs)
655+
pass
656+
644657
class _Line:
645658
def __init__(self, anim, h, xs, ys):
646-
# form 3x2 matrix, columns are first/last point in homogeneous form
647-
self.p = np.vstack([xs, ys, [1, 1]])
659+
# form 3xN matrix, columns are first/last point in homogeneous form
660+
p = np.vstack([xs, ys])
661+
self.p = np.vstack([p, np.ones((p.shape[1],))])
648662
self.h = h
649663
self.type = "line"
650664
self.anim = anim
@@ -671,6 +685,7 @@ def plot(self, x, y, *args, **kwargs):
671685

672686
(h,) = self.ax.plot(x, y, *args, **kwargs)
673687
self.displaylist.append(Animate2._Line(self, h, x, y))
688+
return h
674689

675690
# ------------------- quiver()
676691

@@ -755,8 +770,9 @@ def text(self, x, y, *args, **kwargs):
755770

756771
# ------------------- scatter()
757772

758-
def scatter(self, **kwargs):
759-
pass
773+
def scatter(self, x, y, s=0, **kwargs):
774+
h = self.plot(x, y, '.', markersize=0, **kwargs)
775+
self.displaylist.append(Animate2._Line(self, h, x, y))
760776

761777
# ------------------- wrappers for Axes primitives
762778

@@ -791,8 +807,11 @@ def set_ylabel(self, *args, **kwargs):
791807
# plotvol3(2)
792808
# tranimate(attitude())
793809

794-
# from spatialmath import base
810+
from spatialmath import base
795811

796-
# T = base.rpy2r(0.3, 0.4, 0.5)
812+
T = base.rpy2r(0.3, 0.4, 0.5)
797813
# base.tranimate(T, wait=True)
814+
815+
T = base.rot2(2)
816+
base.tranimate2(T, wait=True)
798817
pass

‎spatialmath/base/graphics.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,9 +1022,9 @@ def _axes_dimensions(ax):
10221022
"""
10231023
classname = ax.__class__.__name__
10241024

1025-
if classname == "Axes3DSubplot":
1025+
if classname in ("Axes3DSubplot", "Animate"):
10261026
return 3
1027-
elif classname == "AxesSubplot":
1027+
elif classname in ("AxesSubplot", "Animate2"):
10281028
return 2
10291029

10301030
def axes_get_limits(ax):

‎spatialmath/base/transforms2d.py‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -829,10 +829,13 @@ def trplot2(
829829

830830
ax = base.axes_logic(ax, 2)
831831

832-
if not ax.get_xlabel():
833-
ax.set_xlabel(labels[0])
834-
if not ax.get_ylabel():
835-
ax.set_ylabel(labels[0])
832+
try:
833+
if not ax.get_xlabel():
834+
ax.set_xlabel(labels[0])
835+
if not ax.get_ylabel():
836+
ax.set_ylabel(labels[0])
837+
except AttributeError:
838+
pass # if axes are an Animate object
836839

837840
if not hasattr(ax, '_plotvol'):
838841
ax.set_aspect("equal")
@@ -957,6 +960,11 @@ def tranimate2(T, **kwargs):
957960
tranimate2(transl(1,2)@trot2(1), frame='A', arrow=False, dims=[0, 5], movie='spin.mp4')
958961
"""
959962
anim = base.animate.Animate2(**kwargs)
963+
try:
964+
del kwargs['dims']
965+
except KeyError:
966+
pass
967+
960968
anim.trplot2(T, **kwargs)
961969
anim.run(**kwargs)
962970

‎spatialmath/base/transforms3d.py‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,12 +2573,15 @@ def trplot(
25732573
else:
25742574
ax = base.plotvol3(dims, ax=ax)
25752575

2576-
if not ax.get_xlabel():
2577-
ax.set_xlabel(labels[0])
2578-
if not ax.get_ylabel():
2579-
ax.set_ylabel(labels[0])
2580-
if not ax.get_zlabel():
2581-
ax.set_zlabel(labels[0])
2576+
try:
2577+
if not ax.get_xlabel():
2578+
ax.set_xlabel(labels[0])
2579+
if not ax.get_ylabel():
2580+
ax.set_ylabel(labels[0])
2581+
if not ax.get_zlabel():
2582+
ax.set_zlabel(labels[0])
2583+
except AttributeError:
2584+
pass # if axes are an Animate object
25822585

25832586
if anaglyph is not None:
25842587
# enforce perspective projection
@@ -2808,6 +2811,8 @@ def trplot(
28082811

28092812
if block:
28102813
# calling this at all, causes FuncAnimation to fail so when invoked from tranimate skip this bit
2814+
import matplotlib.pyplot as plt
2815+
# TODO move blocking into graphics
28112816
plt.show(block=block)
28122817
return ax
28132818

@@ -2862,6 +2867,11 @@ def tranimate(T, **kwargs):
28622867
kwargs["block"] = False
28632868

28642869
anim = base.animate.Animate(**kwargs)
2870+
try:
2871+
del kwargs['dims']
2872+
except KeyError:
2873+
pass
2874+
28652875
anim.trplot(T, **kwargs)
28662876
anim.run(**kwargs)
28672877

0 commit comments

Comments
 (0)