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
9 changes: 6 additions & 3 deletions spatialmath/twist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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):
Expand Down
67 changes: 67 additions & 0 deletions tests/test_twist_unit_predicate.py
Original file line number Diff line number Diff line change
@@ -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