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
7 changes: 6 additions & 1 deletion spatialmath/base/transforms2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,12 @@ def ishom2(T: Any, check: bool = False, tol: float = 20) -> bool: # TypeGuard(S
and T.shape == (3, 3)
and (
not check
or (smb.isR(T[:2, :2], tol=tol) and all(T[2, :] == np.array([0, 0, 1])))
or (
smb.isR(T[:2, :2], tol=tol)
and T[2, 0] == 0
and T[2, 1] == 0
and T[2, 2] == 1
)
)
)

Expand Down
8 changes: 7 additions & 1 deletion spatialmath/base/transforms3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,13 @@ def ishom(T: Any, check: bool = False, tol: float = 20) -> bool:
and T.shape == (4, 4)
and (
not check
or (isR(T[:3, :3], tol=tol) and all(T[3, :] == np.array([0, 0, 0, 1])))
or (
isR(T[:3, :3], tol=tol)
and T[3, 0] == 0
and T[3, 1] == 0
and T[3, 2] == 0
and T[3, 3] == 1
)
)
)

Expand Down
32 changes: 28 additions & 4 deletions spatialmath/base/transformsNd.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,34 @@ def isR(R: NDArray, tol: float = 20) -> bool: # -> TypeGuard[SOnArray]:

:seealso: isrot2, isrot
"""
return bool(
np.linalg.norm(R @ R.T - np.eye(R.shape[0])) < tol * _eps
and np.linalg.det(R) > 0
)
n = R.shape[0]
if n == 3:
# explicit cofactor expansion avoids the dispatch overhead of
# np.linalg.det/norm, which dominates cost for such a small matrix
det = (
R[0, 0] * (R[1, 1] * R[2, 2] - R[1, 2] * R[2, 1])
- R[0, 1] * (R[1, 0] * R[2, 2] - R[1, 2] * R[2, 0])
+ R[0, 2] * (R[1, 0] * R[2, 1] - R[1, 1] * R[2, 0])
)
if det <= 0:
return False
D = R @ R.T
D[0, 0] -= 1.0
D[1, 1] -= 1.0
D[2, 2] -= 1.0
return bool(np.sum(D * D) < (tol * _eps) ** 2)
elif n == 2:
det = R[0, 0] * R[1, 1] - R[0, 1] * R[1, 0]
if det <= 0:
return False
D = R @ R.T
D[0, 0] -= 1.0
D[1, 1] -= 1.0
return bool(np.sum(D * D) < (tol * _eps) ** 2)
else:
return bool(
np.linalg.norm(R @ R.T - np.eye(n)) < tol * _eps and np.linalg.det(R) > 0
)


def isskew(S: NDArray, tol: float = 20) -> bool: # -> TypeGuard[sonArray]:
Expand Down
Loading