Skip to content

Commit 81c4ee4

Browse files
authored
Defer SymPy loading in transforms2d.py
1 parent 2690a37 commit 81c4ee4

1 file changed

Lines changed: 18 additions & 13 deletions

File tree

‎spatialmath/base/transforms2d.py‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@
3939

4040
_eps = np.finfo(np.float64).eps
4141

42-
try: # pragma: no cover
43-
# print('Using SymPy')
44-
import sympy
45-
46-
_symbolics = True
47-
48-
except ImportError: # pragma: no cover
49-
_symbolics = False
50-
5142

5243
# ---------------------------------------------------------------------------------------#
5344
def rot2(theta: float, unit: str = "rad") -> SO2Array:
@@ -170,8 +161,13 @@ def tr2xyt(T: SE2Array, unit: str = "rad") -> R3:
170161
:seealso: trot2
171162
"""
172163

173-
if T.dtype == "O" and _symbolics:
174-
angle = sympy.atan2(T[1, 0], T[0, 0])
164+
if T.dtype == "O":
165+
try:
166+
import sympy
167+
except ImportError:
168+
angle = math.atan2(T[1, 0], T[0, 0])
169+
else:
170+
angle = sympy.atan2(T[1, 0], T[0, 0])
175171
else:
176172
angle = math.atan2(T[1, 0], T[0, 0])
177173
return np.r_[T[0, 2], T[1, 2], angle]
@@ -367,7 +363,12 @@ def ishom2(T: Any, check: bool = False, tol: float = 20) -> bool: # TypeGuard(S
367363
and T.shape == (3, 3)
368364
and (
369365
not check
370-
or (smb.isR(T[:2, :2], tol=tol) and all(T[2, :] == np.array([0, 0, 1])))
366+
or (
367+
smb.isR(T[:2, :2], tol=tol)
368+
and T[2, 0] == 0
369+
and T[2, 1] == 0
370+
and T[2, 2] == 1
371+
)
371372
)
372373
)
373374

@@ -1312,7 +1313,11 @@ def _FindCorrespondences(
13121313

13131314
num_iter += 1
13141315

1315-
return T
1316+
# T accumulates roundoff error through repeated composition
1317+
# (T = T @ new_T) over the iterations above, and can drift just
1318+
# outside ishom2()'s orthonormality tolerance -- restore exact
1319+
# orthonormality before returning.
1320+
return trnorm2(T)
13161321

13171322

13181323
if _matplotlib_exists:

0 commit comments

Comments
 (0)