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
8 changes: 8 additions & 0 deletions spatialmath/base/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1708,11 +1708,18 @@ def expand_dims(dim: ArrayLike = None, nd: int = 2) -> NDArray:
* A -> [-A, A, -A, A, -A, A]
* [A,B] -> [A, B, A, B, A, B]
* [A,B,C,D,E,F] -> [A, B, C, D, E, F]

A scalar is symmetric about zero, so its sign is ignored: ``-A`` is the
same as ``A``. For the other forms the values are returned as given and
no ordering check is made. If a minimum is greater than the
corresponding maximum, matplotlib will invert that axis when the limits
are applied, which is a convenient way to flip an axis direction.
"""
dim = smb.getvector(dim)

if nd == 2:
if len(dim) == 1:
dim = abs(dim)
return np.r_[-dim, dim, -dim, dim]
elif len(dim) == 2:
return np.r_[dim[0], dim[1], dim[0], dim[1]]
Expand All @@ -1722,6 +1729,7 @@ def expand_dims(dim: ArrayLike = None, nd: int = 2) -> NDArray:
raise ValueError("bad dimension specified")
elif nd == 3:
if len(dim) == 1:
dim = abs(dim)
return np.r_[-dim, dim, -dim, dim, -dim, dim]
elif len(dim) == 2:
return np.r_[dim[0], dim[1], dim[0], dim[1], dim[0], dim[1]]
Expand Down
17 changes: 17 additions & 0 deletions tests/base/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ class TestGraphics(unittest.TestCase):
def teardown_method(self, method):
plt.close("all")

def test_expand_dims(self):
np.testing.assert_array_equal(expand_dims(3), [-3, 3, -3, 3])
np.testing.assert_array_equal(expand_dims(-3), [-3, 3, -3, 3])
np.testing.assert_array_equal(expand_dims([1, 2]), [1, 2, 1, 2])
np.testing.assert_array_equal(expand_dims([1, 2, 3, 4]), [1, 2, 3, 4])
# reversed limits are passed through, matplotlib inverts the axis
np.testing.assert_array_equal(expand_dims([2, 1]), [2, 1, 2, 1])

np.testing.assert_array_equal(expand_dims(-3, nd=3), [-3, 3, -3, 3, -3, 3])
np.testing.assert_array_equal(expand_dims([1, 2], nd=3), [1, 2] * 3)
np.testing.assert_array_equal(expand_dims([1, 2, 3, 4, 5, 6], nd=3), [1, 2, 3, 4, 5, 6])

with self.assertRaises(ValueError):
expand_dims([1, 2, 3])
with self.assertRaises(ValueError):
expand_dims([1, 2, 3, 4], nd=3)

@pytest.mark.skipif(
sys.platform.startswith("darwin") and sys.version_info < (3, 11),
reason="tkinter bug with mac",
Expand Down
Loading