Converting quaternions to spherical angles

Consider a situation where a beam is shot at a cube C from an arbitrary position P. The cube detects the angle of incidence relative to its $x$ axis. The cube can be rotated and moved, and the orientation (rotation) of the cube is being tracked.

Below is an example in two dimensions. In the reference orientation, $C_\text{ref}$, the angle of incidence is $0°$. The cube is then moved to an arbitrary position and rotated with an arbitrary quaternion rotation $\mathbf{q}$. The measured angle of incidence is now $\gamma$. By subtracting the rotation angle $\beta$, the angle of incidence relative to the reference orientation is found to be $\alpha$. ($\alpha = \gamma - \beta$) In three dimensions, the same problem would also have an elevation angle.

My question is this: how do I convert the (three-dimensional) rotation from quaternions $\mathbf{q}=(q_0,q_1,q_2,q_3)$ to spherical angles $(\theta,\phi)$?

Any rotation in three-dimensions can be represented as a rotation by an angle $\theta$ about an axis determined by a unit vector $\hat{n}=(n_1,n_2,n_3)$ -- that is three parameters and not two as the OP indicates. Such a rotation can be represented by $$\exp(\theta\ \sum_{j=1}^3 n_j \cdot T^j)\ ,$$ where $(T^1,T^2,T^3)$ are a basis in the fundamental (three) representation of $so(3)$ and are given by the matrices $(T_{i})_{jk}=\epsilon_{ijk}$. The connection with quaternions that I know makes use of the isomorphism of the (complex) Lie algebras $so(3)$ and $su(2)$. We just replace $\mathbf{T}$ with the corresponding generators in the fundamental (two) dimensional representation of $su(2)$. A basis for this Lie algebra are given in terms of the Pauli matrices. Using the identification $$T^j \longleftrightarrow i\sigma^j/2\ ,$$ one obtains $$\exp(\theta\ \sum_{j=1}^3n_j \tfrac{i\ \sigma^j}2)= \cos \tfrac{\theta}2 \mathbf{1} + i \sin \tfrac\theta2 \sum_{j=1}^3 n_j\ \sigma^j$$ where I have used a standard identity involving Pauli matrices. The right hand side is a quaternion, if you wish. Note that this is not a 1-1 map as group manifold of $SU(2)$ is a double cover of $SO(3)$.
• The three parameters that parametrise a three-dimensional rotation are the angle $\theta$ and the two parameters that specify a unit vector $\hat{n}$. You should be able to work out the change of variables from this to azimuth and elevation. Jul 21, 2014 at 0:21
$$\newcommand\Cref{C_{\text{ref}}}$$The quaternion $$q = q_0 + q_1i + q_2j + q_3k$$ takes the $$x$$-axis of $$\Cref$$ to that of $$C_q$$ via $$qi\bar q = (q_0^2 + q_1^2 - q_2^2 - q_3^2)i + 2(q_1q_2 + q_0q_3)j + 2(q_1q_3 - q_0q_2)k$$ where we represent vectors $$(x,y,z)$$ as imaginary quaternions $$xi + yi + zi$$ (and assume $$q$$ is normalized). Now we just convert from Cartesian to spherical coordinates: $$\tan\phi = 2\frac{q_1q_2 + q_0q_3}{q_0^2+q_1^2-q_2^2-q_3^2},\quad \tan\theta = \frac12\frac{\sqrt{(q_0^2+q_1^2-q_2^2-q_3^2)^2 + 4(q_1q_2+q_0q_3)^2}}{q_1q_3-q_0q_2}.$$ (Something like this, I'm not going to promise I got this conversion exactly correct.) Here I've used $$\phi$$ for the azimuthal angle and $$\theta$$ for the polar angle.