I'm working with a 3D cartesian system $\vec{e'_x},\vec{e'_y},\vec{e'_z}$ that moves around in a global coordinate system. I know the origo position and $\vec{e'_z}$ in global coordinates. There is no rotation around the z-axis, only around x and y.
How can I find rotation angles $\alpha, \beta, \gamma$ around x, y, z, axes respectively, if I know the new z-direction $\vec{e}_z' = (\cos\theta\sin\phi, \sin\theta\sin\phi, \cos\phi)$. I know how to compute $\theta, \phi$. And I assume $\gamma = 0$.
My attempt was as follows but it didn't work out. Compute $cos(\alpha) = ||\vec{e}_{zx}||$, where $\vec{e}_{zx}$ is the projection onto yz-plane i.e. $\vec{e}_z$ with x-component = 0. Then compute $cos(\beta) = ||\vec{e}_{zy}||$, where $\vec{e}_{zy}$ has y-component = 0.
\begin{equation} R = R_xR_y, \;\; \text{where} \label{rotation_matrix} \end{equation} \begin{align*} R_x &= \begin{bmatrix} 1 & 0 & 0\\ 0 & \cos\alpha & -\sin\alpha\\ 0 & \sin\alpha & \cos\alpha \end{bmatrix} \\ R_y &= \begin{bmatrix} \cos\beta & 0 & \sin\beta\\ 0 & 1 & 0\\ -\sin\beta & 0 & \cos\beta \end{bmatrix} \\ \end{align*}
Code:
new_axis = [ 0.03807563 -0.06572909 0.99711079] # computed before
proj_x = np.multiply(new_axis, np.array([0,1,1])) # project to yz plane
proj_y = np.multiply(new_axis, np.array([1,0,1])) # project to xz plane
alfa = np.arccos(norm(proj_x)) # rotation around x
beta = np.arccos(norm(proj_y)) # rotation around y
Rx = np.array([[1,0,0],
[0, cos(alfa), -sin(alfa)],
[0, sin(alfa), cos(alfa)]])
Ry = np.array([[cos(beta), 0, sin(beta)],
[0,1,0],
[-sin(beta), 0, cos(beta)]])
Rz = np.array([[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])
R = np.matmul(np.matmul(Rx, Ry), Rz)
EDIT: I found that Rodrigues' rotation formula solves this https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula.
The $z$ vector is given as $\vec e_z=(z_1,z_2,z_3)^T$. The aim is to decompose it using axis rotations, which in a 3D context are Givens rotations. A rotation around axis $e_3$ is to be avoided. Thus find angles $α,β$ such that $$ R_{3,1}(-β)\vec e_z=\pmatrix{0\\*\\*},~~~R_{3,1}=R_y $$ and $$ R_{2,3}(-α)R_{3,1}(-β)\vec e_z=\pmatrix{0\\0\\*},~~~R_{2,3}=R_x. $$ For the first you need $(\cosβ,\sinβ)\sim (z_3,z_1)$ or
As a result, $(z_1,z_3)$ get replaced by $(z_1',z_3')=(0,\sqrt{z_1^2+z_3^3})$.
Now to determine $α$ we want $(\cosα,\sinα)\sim(z_3',-z_2)$ or