Skip to content

Commit afed0b1

Browse files
committed
add Python code for log of SO(2) and SE(2) rather than use scipy
1 parent 37fdd26 commit afed0b1

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

spatialmath/base/transforms2d.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -374,31 +374,42 @@ def trlog2(T, check=True, twist=False):
374374
>>> trlog2(rot2(0.3))
375375
>>> trlog2(rot2(0.3), twist=True)
376376
377-
:seealso: :func:`~trexp`, :func:`~spatialmath.base.transformsNd.vex`,
378-
:func:`~spatialmath.base.transformsNd.vexa`
377+
:seealso: :func:`~trexp`, :func:`~spatialmath.smb.transformsNd.vex`,
378+
:func:`~spatialmath.smb.transformsNd.vexa`
379379
"""
380380

381381
if ishom2(T, check=check):
382382
# SE(2) matrix
383383

384-
if base.iseye(T):
384+
if smb.iseye(T):
385385
# is identity matrix
386386
if twist:
387387
return np.zeros((3,))
388388
else:
389389
return np.zeros((3, 3))
390390
else:
391+
st = T[1,0]
392+
ct = T[0,0]
393+
theta = math.atan(st / ct)
394+
395+
V = np.array([[st, -(1-ct)], [1-ct, st]])
396+
tr = (np.linalg.inv(V) @ T[:2, 2]) * theta
397+
print(tr)
391398
if twist:
392-
return base.vexa(scipy.linalg.logm(T))
399+
return np.array([tr, theta])
393400
else:
394-
return scipy.linalg.logm(T)
401+
return np.block([
402+
[smb.skew(theta), tr[:, np.newaxis]],
403+
[np.zeros((1,3))]
404+
])
395405

396406
elif isrot2(T, check=check):
397407
# SO(2) rotation matrix
408+
theta = math.atan(T[1,0] / T[0,0])
398409
if twist:
399-
return base.vex(scipy.linalg.logm(T))
410+
return theta
400411
else:
401-
return scipy.linalg.logm(T)
412+
return smb.skew(theta)
402413
else:
403414
raise ValueError("Expect SO(2) or SE(2) matrix")
404415

tests/base/test_transforms2d.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from scipy.linalg import logm, expm
1616

1717
from spatialmath.base.transforms2d import *
18-
from spatialmath.base.transformsNd import isR, t2r, r2t, rt2tr
18+
from spatialmath.base.transformsNd import isR, t2r, r2t, rt2tr, skew
1919

2020
import matplotlib.pyplot as plt
2121

@@ -66,6 +66,20 @@ def test_Rt(self):
6666
nt.assert_array_almost_equal(transl2(T), np.array(t))
6767
# TODO
6868

69+
def test_trlog2(self):
70+
R = rot2(0.5)
71+
nt.assert_array_almost_equal(trlog2(R), skew(0.5))
72+
73+
T = transl2(1, 2) @ trot2(0.5)
74+
nt.assert_array_almost_equal(logm(T), trlog2(T))
75+
76+
def test_trexp2(self):
77+
R = trexp2(skew(0.5))
78+
nt.assert_array_almost_equal(R, rot2(0.5))
79+
80+
T = transl2(1, 2) @ trot2(0.5)
81+
nt.assert_array_almost_equal(trexp2(logm(T)), T)
82+
6983
def test_transl2(self):
7084
nt.assert_array_almost_equal(
7185
transl2(1, 2), np.array([[1, 0, 1], [0, 1, 2], [0, 0, 1]])

0 commit comments

Comments
 (0)