Skip to content

fix(base): trlog loses accuracy for rotations just below pi - #323

Open
ranjithguggilla wants to merge 1 commit into
rai-opensource:masterfrom
ranjithguggilla:fix/trlog-near-pi
Open

ranjithguggilla wants to merge 1 commit into
rai-opensource:masterfrom
ranjithguggilla:fix/trlog-near-pi

Conversation

@ranjithguggilla

Copy link
Copy Markdown

trlog gets the angle from acos((trace(R) - 1) / 2). Close to a half turn that loses most of its digits. The exact-pi branch only covers rotations within about 6.6e-8 of pi, so there is a band just below it where the result is wrong.

Example, with axis [1, 2, 3] and angle pi - 1e-7:

import numpy as np
from scipy.spatial.transform import Rotation
import spatialmath.base as smb
axis = np.r_[1.0, 2.0, 3.0] / np.sqrt(14)
R = Rotation.from_rotvec(axis * (np.pi - 1e-7)).as_matrix()
print(smb.tr2angvec(R)[0])   # 3.1428487998129593 on master, larger than pi

Over 2,000 random axes, the worst angle error is 9e-4 at pi - 1e-6 and 0.099 at pi - 1e-7. scipy gives 1.8e-15 on the same matrices. trexp(trlog(R)) is off by the same amount. This goes through tr2angvec, SO3.angvec, SE3.log and Twist3(SE3). UnitQuaternion(R).angvec() is already accurate, so the two disagree today.

The change takes the angle from atan2(|vex(R - R^T)| / 2, (trace(R) - 1) / 2) and normalises the axis by that same sine. The exact-pi branch is unchanged.

Tests:

  • Added test_trlog_near_pi for trlog, tr2angvec and an SE(3) matrix at four distances from pi. It fails on master.
  • pytest -s --timeout=50 --timeout_method=thread: 567 passed, 3 skipped on Python 3.12 and 3.13 (566 on master).
  • test_trlog_near_identity still passes.

trlog computed the rotation angle as acos((trace(R) - 1) / 2). Close to
a half turn acos is ill-conditioned, so for pi - 1e-7 the angle was off
by up to 0.1 rad and could be larger than pi. The axis was then scaled
by sin of that wrong angle. tr2angvec, SO3.angvec, SE3.log and
Twist3(SE3) all go through this code.

Take the angle from atan2(|vex(R - R^T)| / 2, (trace(R) - 1) / 2)
instead, which is accurate for every angle, and normalise the axis by
the same sine. The exact-pi branch is unchanged.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant