Home > Mobile >  Scipy rotation matrix
Scipy rotation matrix

Time:04-23

I have this function for computing a rotation matrix:

import numpy as np
from scipy.spatial.transform import Rotation as R

def rotation_matrix(phi,theta,psi):
    
    # Pure rotation in X
    def Rx(phi):
      return np.matrix([[ 1, 0           , 0           ],
                       [  0, np.cos(phi) ,-np.sin(phi) ],
                       [  0, np.sin(phi) , np.cos(phi)]])
    
    # Pure rotation in y
    def Ry(theta):
      return np.matrix([[ np.cos(theta), 0, np.sin(theta)],
                       [ 0             , 1, 0            ],
                       [-np.sin(theta) , 0, np.cos(theta)]])
    
    # Pure rotation in z
    def Rz(psi):
      return np.matrix([[ np.cos(psi), -np.sin(psi) , 0  ],
                       [ np.sin(psi) , np.cos(psi)  , 0  ],
                       [ 0           , 0            , 1 ]])
    return Rx(phi)*Ry(theta)*Rz(psi)

I know that scipy has a built-in function to compute rotation matrices, however I am being able to get the same results as the function.

I have the following example:

rotVec = np.array([np.pi/3, np.pi/4, 0]) # 60º in x-axis, 45º in y-axis
rotMat1 = rotation_matrix(*rotVec)
rotMat2 = R.from_euler('xyz', rotVec).as_matrix() # not the same result

What am I doing wrong?

CodePudding user response:

OK...

It appears to be with capital letters in the rotation axis

rotVec = np.array([np.pi/3, np.pi/4, 0]) # 60º in x-axis, 45º in y-axis
rotMat1 = rotation_matrix(*rotVec)
rotMat2 = R.from_euler('XYZ', rotVec).as_matrix() 

CodePudding user response:

soo, for rotational matrix you need to multiplication from right to left, so this return Rx(phi)*Ry(theta)*Rz(psi)

should be:

return   Rz(psi)* Ry(theta)*Rx(phi)

instead

  • Related