Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions spatialmath/geom2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ def transformed(self, T: SE2) -> Self:
"""
new = Polygon2()
new.path = self.path.transformed(Affine2D(T.A))
new.path0 = new.path
return new

def vertices(self, unique: bool = True) -> Points2:
Expand Down
48 changes: 48 additions & 0 deletions tests/test_transformed_polygon_animation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""A transformed polygon has its own animation reference geometry."""

import matplotlib.pyplot as plt
import numpy as np
import numpy.testing as nt
import pytest
from spatialmath import Polygon2, SE2


@pytest.mark.parametrize("first", [SE2(), SE2(2, -1), SE2(1, 2, 0.4)])
@pytest.mark.parametrize("second", [SE2(), SE2(-1, 3, -0.2)])
def test_transformed_copy_can_animate(first, second):
original = Polygon2([(0, 0), (2, 0), (0, 1)])
original_vertices = original.vertices(unique=False).copy()
transformed = original.transformed(first)
initial = first.R @ original_vertices + first.t[:, None]
nt.assert_allclose(transformed.vertices(unique=False), initial, atol=1e-14)
fig, ax = plt.subplots()
try:
transformed.plot(ax=ax, facecolor="blue")
old_patch = transformed.patch
transformed.animate(second)
assert old_patch not in ax.patches
assert len(ax.patches) == 1
expected = second.R @ initial + second.t[:, None]
nt.assert_allclose(transformed.vertices(unique=False), expected, atol=1e-14)
# Frames are absolute relative to the transformed copy, not cumulative.
transformed.animate(SE2())
nt.assert_allclose(transformed.vertices(unique=False), initial, atol=1e-14)
nt.assert_allclose(original.vertices(unique=False), original_vertices)
assert len(ax.patches) == 1
finally:
plt.close(fig)


def test_chained_transformed_copy_has_latest_reference():
polygon = Polygon2([(0, 0), (2, 0), (0, 1)])
polygon = polygon.transformed(SE2(1, 2)).transformed(SE2.Rot(0.3))
initial = polygon.vertices(unique=False).copy()
fig, ax = plt.subplots()
try:
polygon.plot(ax=ax)
polygon.animate(SE2(1, 0))
nt.assert_allclose(
polygon.vertices(unique=False), initial + np.array([[1], [0]])
)
finally:
plt.close(fig)