BUG: Rotation matrix is changed with scipy.spatial.transform.Rotation
See original GitHub issueDescribe your issue.
Greetings, I think I encountered some bug in processing a 3D rotation matrix. I tried to retrieve Euler angles, but they made the wrong rotation. Even when I retrieve the matrix back, it is altered incorrectly. I think this failure has something to do with the internal storage of rotation objects as quaternions. But I don’t have expertise enough to debug through the source code, and I haven’t found description in the documentation about conversion limitations between rotation formats, except gimbal lock. There is a code example of the initial rotation matrix and the wrong output during conversions. Even the first retrieved matrix rotmatrix11 (without conversions) is wrong. I dubbed my question on StackOverflow https://stackoverflow.com/questions/74265765/rotation-matrix-is-changed-with-scipy-spatial-transform-rotation The rotation matrix seems correct in terms of orthonormality and the determinant.
Reproducing Code Example
from scipy.spatial.transform import Rotation as R
import numpy as np
rotmatrix1 = np.array([[ 0.86345719, -0.48700394, 0.13141101],
[ 0.02362667, -0.221185 , -0.97494563],
[-0.5038685 , -0.84492861, 0.1794775 ]])
print(f"rotmatrix1:\n {rotmatrix1}")
r1 = R.from_matrix(rotmatrix1)
rotmatrix11 = r1.as_matrix()
angles1 = r1.as_euler("xyz")
print(f"angles1: {angles1}")
print(f"rotmatrix11:\n {rotmatrix11}")
print("-------------------------------")
r2 = R.from_euler("xyz", angles1)
rotmatrix2 = r2.as_matrix()
print(f"rotmatrix2:\n {rotmatrix2}")
r3 = R.from_matrix(rotmatrix2)
angles2 = r3.as_euler("xyz")
print(f"angles2: {angles2}")
r4 = R.from_euler("xyz", angles2)
rotmatrix3 = r4.as_matrix()
print(f"rotmatrix3:\n {rotmatrix3}")
Error message
Output (all matrices are expected to be the same or at least do the same rotation job, but they differ):
rotmatrix1:
[[ 0.86345719 -0.48700394 0.13141101]
[ 0.02362667 -0.221185 -0.97494563]
[-0.5038685 -0.84492861 0.1794775 ]]
angles1: [ 2.91754028 0.33066594 -0.51471512]
rotmatrix11:
[[ 0.82327846 -0.4171921 -0.3849199 ]
[-0.46561791 -0.88418854 -0.03755775]
[-0.32467296 0.21014609 -0.9221855 ]]
-------------------------------
rotmatrix2:
[[ 0.82327846 -0.4171921 -0.3849199 ]
[-0.46561791 -0.88418854 -0.03755775]
[-0.32467296 0.21014609 -0.9221855 ]]
angles2: [ 2.91754028 0.33066594 -0.51471512]
rotmatrix3:
[[ 0.82327846 -0.4171921 -0.3849199 ]
[-0.46561791 -0.88418854 -0.03755775]
[-0.32467296 0.21014609 -0.9221855 ]]
SciPy/NumPy/Python version information
1.7.3 1.21.6 sys.version_info(major=3, minor=7, micro=15, releaselevel=‘final’, serial=0)
Issue Analytics
- State:
- Created a year ago
- Comments:7 (2 by maintainers)
rotmatrix1
is not a rotation matrix. It incorporates a reflection.Thank you for your clarification and for your time. My knowledge gap didn’t allow me to reveal it in time. I was misled by some custom code snippets on the web which can handle negative determinants and stated that it was OK for rotation. Now I see that in scipy documentation that determinant +1 is a mandatory condition. Indeed reflection is quite likely to occur when I use PCA results as a basis for the rotational matrix.