matthew-brett / transforms3d

3 dimensional spatial transformations
http://matthew-brett.github.io/transforms3d/
Other
470 stars 83 forks source link

euler2mat bug? #17

Open creuzige opened 6 years ago

creuzige commented 6 years ago

However, I think I may have found a bug in euler2mat. The code I’ve used is attached below.

My impression is that the intrinsic and extrinsic rotation matrices are inverses of each other. And since they are orthogonal tensors, the inverse is equal to the transpose. However, when I invert the rotation matrix generated from 3 Euler angles using the rzxz convention, I do not get the same rotation matrix as if I perform the ‘szxz’ rotation. Nor does the transpose of the rzxz rotation match the szxz rotation. The values are correct, but the negative signs are in the wrong position. In addition, the axis/angle pair calculated from the szxz rotation has a different axis than the rzxz rotation.

Did I miss something, or are my observations correct?

Thanks!

Code example:

# set rotation of crystal

import math
import transforms3d as t3d
import numpy as np

phi1=(20.0)*(math.pi/180.0)
PHI=(15.0)*(math.pi/180.0)
phi2=(25.0)*(math.pi/180.0)
r=t3d.euler.euler2mat(phi1, PHI, phi2, 'rzxz')
(Raxis, Rangle)=t3d.euler.euler2axangle(phi1, PHI, phi2, 'rzxz')

print "\n RZXZ rotation"
print r
print Raxis
print Rangle

print "\n RZXZ inverse rotation"
print np.linalg.inv(r)
(Raxis, Rangle)=t3d.axangles.mat2axangle(np.linalg.inv(r))
print Raxis
print Rangle

r=t3d.euler.euler2mat(phi1, PHI, phi2, 'szxz')
(Raxis, Rangle)=t3d.euler.euler2axangle(phi1, PHI, phi2, 'szxz')

print "\n SZXZ rotation"
print r
print Raxis
print Rangle

# Doesn't seem quite right, the minus signs are in the same place...

Output:

RZXZ rotation
[[ 0.712032   -0.69654462  0.08852133]
[ 0.69357486  0.67808749 -0.24321035]
[ 0.10938165  0.23456972  0.96592583]]
[ 0.32500232 -0.01418991  0.94560676]
0.82572715704

RZXZ inverse rotation
[[ 0.712032    0.69357486  0.10938165]
[-0.69654462  0.67808749  0.23456972]
[ 0.08852133 -0.24321035  0.96592583]]
[ 0.32500232 -0.01418991  0.94560676]
-0.82572715704

SZXZ rotation
[[ 0.712032   -0.69357486  0.10938165]
[ 0.69654462  0.67808749 -0.23456972]
[ 0.08852133  0.24321035  0.96592583]]
[0.32500232 0.01418991 0.94560676]
0.82572715704
matthew-brett commented 6 years ago

@cgohlke - any thoughts here? I haven't modified that part of the code from transformations.py.

creuzige commented 6 years ago

I started looking into the code to see if I could be any help. I noticed that in mat2euler the axes sequence specified does not seem to match up with the i,j, and k axes listed for all axes. I've assumed that axis 0 is x, then axis 1 is y, and axis 2 is z. If that doesn't hold, then my observations are probably irrelevant.

I modified the euler.py file to test all of the Euler axis combinations listed (attached as a .txt file) as a standalone program. The output is below.

euler.txt

python euler.py Input Axes = sxyz Output Axes: xyz Axes match Input Axes = sxyx Output Axes: xyz Axes do NOT match Input Axes = sxzy Output Axes: xzy Axes match Input Axes = sxzx Output Axes: xzy Axes do NOT match Input Axes = syzx Output Axes: yzx Axes match Input Axes = syzy Output Axes: yzx Axes do NOT match Input Axes = syxz Output Axes: yxz Axes match Input Axes = syxy Output Axes: yxz Axes do NOT match Input Axes = szxy Output Axes: zxy Axes match Input Axes = szxz Output Axes: zxy Axes do NOT match Input Axes = szyx Output Axes: zyx Axes match Input Axes = szyz Output Axes: zyx Axes do NOT match Input Axes = rzyx Output Axes: xyz Axes do NOT match Input Axes = rxyx Output Axes: xyz Axes do NOT match Input Axes = ryzx Output Axes: xzy Axes do NOT match Input Axes = rxzx Output Axes: xzy Axes do NOT match Input Axes = rxzy Output Axes: yzx Axes do NOT match Input Axes = ryzy Output Axes: yzx Axes do NOT match Input Axes = rzxy Output Axes: yxz Axes do NOT match Input Axes = ryxy Output Axes: yxz Axes do NOT match Input Axes = ryxz Output Axes: zxy Axes do NOT match Input Axes = rzxz Output Axes: zxy Axes do NOT match Input Axes = rxyz Output Axes: zyx Axes do NOT match Input Axes = rzyz Output Axes: zyx Axes do NOT match`

matthew-brett commented 6 years ago

Hi - sorry for the flying feedback - but I'm sure you gathered that the angles are in the order specified by the axis labels, so if you have sxyz then the order of the angles is indeed x, y, z, but if you have rzyz then the order is z, y, z.

creuzige commented 6 years ago

Yup, I agree that the angles are in the order given by the axis labels. I should have been more clear on my comment.

It looks like for many axis labels, the axis listed as the rotation axis does not match the order I expect. For example, if I state ‘rzyz’ for the axis labels, by my reading of the function eluer2mat in the Euler code, the rotation axes are set as i=2, j=1, and k=0; based upon the _NEXT_AXIS function. Assuming 2, 1, and 0 are Z, Y, X axes, this seems to imply that the rotation axes are ‘rzyx’, not ‘rzyz’.