Skip to content

Commit dfe0bb7

Browse files
committed
fix: preserve rotation quadrants in trlog2
1 parent 3332e9c commit dfe0bb7

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

‎spatialmath/base/transforms2d.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ def trlog2(
549549
else:
550550
st = T[1, 0]
551551
ct = T[0, 0]
552-
theta = math.atan(st / ct)
552+
theta = math.atan2(st, ct)
553553
if abs(theta) < tol * _eps:
554554
tr = T[:2, 2].flatten()
555555
else:
@@ -564,7 +564,7 @@ def trlog2(
564564

565565
elif isrot2(T, check=check, tol=tol):
566566
# SO(2) rotation matrix
567-
theta = math.atan(T[1, 0] / T[0, 0])
567+
theta = math.atan2(T[1, 0], T[0, 0])
568568
if twist:
569569
return theta
570570
else:

‎tests/base/test_transforms2d.py‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import unittest
1313
from math import pi
1414
import math
15-
from scipy.linalg import logm
15+
from scipy.linalg import expm, logm
1616
import pytest
1717
import sys
1818

@@ -88,6 +88,25 @@ def test_trlog2(self):
8888

8989
nt.assert_array_almost_equal(trlog2(T, twist=True), vexa(logm(T)))
9090

91+
def test_trlog2_quadrants(self):
92+
for c, s in (
93+
(1, 0),
94+
(math.sqrt(3) / 2, 0.5),
95+
(-0.5, math.sqrt(3) / 2),
96+
(-0.5, -math.sqrt(3) / 2),
97+
(-1, 0),
98+
(0, 1),
99+
(0, -1),
100+
):
101+
R = np.array([[c, -s], [s, c]])
102+
for T in (R, rt2tr(R, [1, 2])):
103+
for twist in (False, True):
104+
with self.subTest(c=c, s=s, shape=T.shape, twist=twist):
105+
S = trlog2(T, twist=twist)
106+
if twist:
107+
S = skew(S) if T.shape == (2, 2) else skewa(S)
108+
nt.assert_allclose(expm(S), T, atol=1e-12, rtol=0)
109+
91110
def test_trexp2(self):
92111
R = trexp2(skew(0.5))
93112
nt.assert_array_almost_equal(R, rot2(0.5))

0 commit comments

Comments
 (0)