Repository navigation
trinterp() returns invalid rotations in transform matrix #165
Description
Activity
- added a commit that references this issue
on Feb 19, 2025 - linked a pull request that will close this issue#165 use qunit in trinterp #166
on Feb 19, 2025 - added a commit that references this issue
on Feb 19, 2025 Guys, I think that allowing direct assignment to the
Rproperty is a mistake. It allows skewed rotation matrices into the SE(3) matrix which causes all sorts of grief down the track.The
.Rt(R, t)method checks the rotation matrix, but doesn't explicitly normalise. It should, by default.The given rotation matrices have 17 digits which is full float64 precision but I don't know how they were obtained or how skewed they are.
.isvalid()callsishom()which callsisRwith a default tolerance of 20 (in units of eps, that's about +/- one on the least significant digit).t = np.array([0.5705748101710814, 0.29623210833184527, 0.10764106509086407]) R = np.array( [ [0.2852875203191073, 0.9581330588259315, -0.024332536551692617], [0.9582072394229962, -0.28568756930438033, -0.014882844564011068], [-0.021211248608609852, -0.019069722856395098, -0.9995931315303468], ] ) assert isrot(R, check=True, tol=5) se3_1 = SE3.Rt(trnorm(R), t) assert SE3.isvalid(se3_1.A)If I apply a more stringent test for orthogonality with the
assert isrotline this fails.tol=10is OK,tol=5is not. The other matrix has the same issues.If I add a line
R = trnorm(R)then the stringent
isRtest passes, as does the rest of the example.There is a philosophical issue here. I actually think we should be more stringent in checking rotation matrices into an
SO3orSE3instance. Then we don't have to do hacky things downstream. The constructor should be a gatekeeper. ForUnitQuaternionnormalisation is the default action. UnfortunatelySO3andSE3don't have anormoption, but I think they should. Normalisation is a bit expensive, but for folk who know what they're doing they can set that option toFalse.I've seen in older issues where people set the rotation matrix to 6-digit float values and wonder why things don't work. We should protect these people, which in turn is less effort for us longer term.
Another design error is that the tolerance for the
isRtest should be passed in from.isvalid()rather than defaulting to 20.Suggested changes:
SO3andSE3constructors should accept anormargument which defaults toTrue- the
.Rtpseudo-constructor should accept anormargument which defaults toTrue - delete the
Rproperty assignment unless it always does a normalisation of the value first .isvalid()should accept atolargument
I'm happy to put these into a PR.
PS. Remember also that
SE3inherits from a python list so instead offor i in range(len(path_se3) - 1): assert SE3.isvalid(path_se3[i].A) if angle is None: angle = path_se3[i].angdist(path_se3[i + 1]) else: test_angle = path_se3[i].angdist(path_se3[i + 1]) assert abs(test_angle - angle) < 1e-6you could write
from itertools import pairwise for T1, T2 in pairwise(path_se3): assert SE3.isvalid(T1.A) if angle is None: angle = T1.angdist(T2) else: test_angle = T1.angdist(T2) assert abs(test_angle - angle) < 1e-6Hey Peter, just viewing your comment now, I definitely agree that performing normalization by default in all these cases would be a good improvement.
I probably won't be able to put up a PR or review one until next week.
the trinterp() method does not make sure quaternions are valid before converting them to transforms. It calls qslerp which can sometimes generate invalid quaternions.
Repro:
The interp() method returns an SE3 object that holds the SE3 transformation matrices created from the interpolation: https://github.com/bdaiinstitute/spatialmath-python/blob/4c68fa923bc90047a0d79a2eab5c5a84b6cee7b7/spatialmath/baseposematrix.py#L449-L455.
However, there is a validity check in the SE3 object that will turn any invalid transforms into identity matrices.
A possible solution is to modify the trinterp() method to turn all quaternions into unit quaternions before converting them into rotation matrices: https://github.com/bdaiinstitute/spatialmath-python/blob/4c68fa923bc90047a0d79a2eab5c5a84b6cee7b7/spatialmath/base/transforms3d.py#L1697-L1700.
I am not sure if this is the only location in the spatialmath codebase that would benefit from this change.