How to get Euler angles from Spherical Coordinates?

1.4k Views Asked by At

I'm trying to rotate a cylinder by the same amount of its location on a sphere. For example, I have a cylinder with midpoint at $R = 100$ cm, $\theta = 30$ degrees, $\phi = 45$ degrees using the ISO convention where theta is the polar angle and $\phi$ is the azimuth angle.

The cylinder will need to have its central axis pointing in the same direction as a vector going from the origin to the midpoint and rotation is done by passing Euler angles to a plotting tool.

How do I calculate the Euler angles given this information? I've tried the following in python, but the angles are still wrong:

import numpy as np

def calcRotation(phi,theta):

    v1 = np.array([0, 0, 1])
    v2 = np.array([np.sin(theta)*np.cos(phi), np.sin(theta)*np.sin(phi), np.cos(phi)])
    Z = np.cross(v1,v2)
    Z /= np.sqrt(Z[0]**2 + Z[1]**2 + Z[2]**2)
    Y = np.cross(Z,v1)
    t = m.atan2(-Z[1],Z[2])
    p = m.asin(Z[0])
    psi = m.atan2(-Y[0],v1[0])
    return np.array([t, p, psi])

I wrote this function by following the steps outlined here:

https://www.mathworks.com/matlabcentral/answers/298940-how-to-calculate-roll-pitch-and-yaw-from-xyz-coordinates-of-3-planar-points

Can someone please explain to me how I get the three Euler angles? Thank you!