diff --git a/spatialmath/twist.py b/spatialmath/twist.py index fa3c5db0..41cc5923 100644 --- a/spatialmath/twist.py +++ b/spatialmath/twist.py @@ -141,7 +141,9 @@ def isunit(self): :return: Whether twist is a unit-twist :rtype: bool - A unit twist is one with a norm of 1, ie. :math:`\| S \| = 1`. + A unit twist has a unit-length angular component, or a zero angular + component and a unit-length translational component. The full vector's + Euclidean norm need not be one for a rotational screw. Example: @@ -153,10 +155,11 @@ def isunit(self): >>> S = Twist3.UnitRevolute([1,2,3], [4,5,6]) >>> S.isunit """ + predicate = smb.isunittwist2 if self.N == 2 else smb.isunittwist if len(self) == 1: - return smb.isunitvec(self.S) + return predicate(self.S) else: - return [smb.isunitvec(x) for x in self.data] + return [predicate(x) for x in self.data] @property def theta(self): diff --git a/tests/test_twist_unit_predicate.py b/tests/test_twist_unit_predicate.py new file mode 100644 index 00000000..8f538635 --- /dev/null +++ b/tests/test_twist_unit_predicate.py @@ -0,0 +1,67 @@ +import numpy as np +import pytest +from spatialmath import Twist2, Twist3 +from spatialmath.base import isunittwist, isunittwist2 + + +@pytest.mark.parametrize( + "twist, expected", + [ + (Twist2.UnitRevolute([2, 3]), True), + (Twist2.UnitRevolute([-4, 1]), True), + (Twist2([3, -2, -1]), True), + (Twist3.UnitRevolute([0, 0, 1], [2, 3, 0]), True), + (Twist3.UnitRevolute([1, 0, 0], [2, 3, 4], pitch=2), True), + (Twist3([3, -2, 5, 0, 0, -1]), True), + (Twist2([0.6, 0, 0.8]), False), + (Twist3([0.6, 0, 0, 0.8, 0, 0]), False), + (Twist2.UnitPrismatic([3, 4]), True), + (Twist3.UnitPrismatic([0, 3, 4]), True), + (Twist2([0, 0, 1]), True), + (Twist3([0, 0, 0, 0, 1, 0]), True), + (Twist2([0, 0, 2]), False), + (Twist3([0, 0, 0, 0, 2, 0]), False), + (Twist2([2, 0, 0]), False), + (Twist3([2, 0, 0, 0, 0, 0]), False), + (Twist2(), False), + (Twist3(), False), + ], +) +def test_unit_screw_criteria(twist, expected): + original = twist.S.copy() + assert bool(twist.isunit) is expected + predicate = isunittwist if twist.N == 3 else isunittwist2 + assert twist.isunit == predicate(twist.S) + np.testing.assert_array_equal(twist.S, original) + + +@pytest.mark.parametrize("cls", [Twist2, Twist3]) +def test_sequence_returns_elementwise_list(cls): + if cls is Twist2: + entries = [ + cls.UnitRevolute([2, 3]), + cls([0.6, 0, 0.8]), + cls.UnitPrismatic([3, 4]), + cls(), + ] + else: + entries = [ + cls.UnitRevolute([0, 0, 1], [2, 3, 0]), + cls([0.6, 0, 0, 0.8, 0, 0]), + cls.UnitPrismatic([0, 3, 4]), + cls(), + ] + sequence = cls(entries) + assert isinstance(sequence.isunit, list) + assert sequence.isunit == [True, False, True, False] + assert cls.Empty().isunit == [] + + +@pytest.mark.parametrize("cls", [Twist2, Twist3]) +@pytest.mark.parametrize("scale", [0.25, -2.0, 3.0]) +def test_normalizing_nonzero_twist_yields_unit_screw(cls, scale): + vector = ( + np.array([3.0, -2, 1]) if cls is Twist2 else np.array([3.0, -2, 5, 0, 0, 1]) + ) + normalized = cls(vector * scale).unit() + assert normalized.isunit