Determine the rotation necessary to transform one point on a sphere to another.

Solution 1:

In general, suppose you want to start with $(a,b,c)$ and transform it to $(d,e,f)$ (where $a^2 + b^2 + c^2 = d^2 + e^2 + f^2 = 1$) using a rotation around the $z$ axis followed by a rotation around the $x$ axis.
Now rotation by angle $\alpha$ around the $z$ axis takes $(a,b,c)$ to $(a \cos(\alpha) - b \sin(\alpha), a \sin(\alpha) + b \cos(\alpha), c)$. Since a rotation around the $x$ axis preserves the $x$ coordinate, you need $a \cos(\alpha) - b \sin(\alpha) = d$. Write $a = r \cos(\theta)$, $b = r \sin(\theta)$ where $r = \sqrt{a^2 + b^2}$ and $\theta = \text{signum}(b) \arccos(a/r)$, and this equation becomes $r \cos(\alpha + \theta) = d$. For this to be possible you need $|d|\le r$, and then you can take $\alpha = \arccos(d/r) - \theta$. If the result of this rotation is $(d, g, h)$, we will have $g^2 + h^2 = 1 - d^2 = e^2 + f^2$, and rotation by angle $\beta$ around the $x$ axis will take this to $(d, g \cos(\beta) - h \sin(\beta), g \sin(\beta) + h \cos(\beta))$. Write $g = \sqrt{g^2+h^2} \cos(\phi)$, $h = \sqrt{g^2 + h^2} \sin(\phi)$, $e = \sqrt{g^2+h^2} \cos(\tau)$ and $f = \sqrt{g^2+h^2} \sin(\tau)$, and you'll see that $\beta = \tau - \phi$ will work.

Of course you can always throw in a rotation around the $y$ axis too (and you'll need one if $|d| > r$).

Solution 2:

Given to unitary vectors $\bf u$ and $\bf v$, thus two points on the unitary sphere, there are of course infinite paths leading from $\bf u$ to $\bf v$.
A neat and simple (as much as spherical geometry allows) approach I think is that of taking the shortest path, i.e. the path along the great-circle or orthodrome.
That will lay on the plane through the origin and parallel to $\bf u$ and $\bf v$.

2_p_su_sfera_1

The unit normal to that plane (Right_Hand rule $\bf u \to \bf v$) is $$ {\bf n} = {{{\bf u} \times {\bf v}} \over {\left| {{\bf u} \times {\bf v}} \right|}} $$

It is useful to consider also another unit vector $\bf t$, lying on the plane and normal to $\bf u$ $$ {\bf t} = {\bf n} \times {\bf u} $$ which already normalized by definition.

Note that the right handed ref. system $x,y,z$ transforms into the right handed ref. system $\bf u, \bf t, \bf n$.
If we put these last three vectors as the columns of a matrix, we obtain $$ \left( {\begin{array}{c|c|c} {\bf u} & {\bf t} & {\bf n} \\ \end{array}} \right) = {\bf T}\quad \Rightarrow \quad \left( {\begin{array}{c|c|c} {\bf u} & {\bf t} & {\bf n} \\ \end{array}} \right) = {\bf T}\left( {\begin{array}{c|c|c} {\bf i} & {\bf j} & {\bf k} \\ \end{array}} \right) $$ and $\bf T$ is by definition orthogonal, unitary and with $|\bf T|=1$. Thus it is also $\bf T^{-1}= \bf T^{T}$.

The great circle angle from $\bf u$ to $\bf v$ is given, in absolute value and sign by $$ \alpha = \arctan _2 \left( {{\bf v} \cdot {\bf u},\;{\bf v} \cdot {\bf t}} \right) = \arctan _2 \left( {\cos \alpha ,\;\sin \alpha } \right) $$ that is we will use both the cosine and sine value to determine $\alpha$.

So $(\cos \alpha, \sin \alpha,0)$ are the coordinates of $\bf v$ in the $(\bf u, \bf t, \bf n)$ sytem, that is we can put $$ {\bf v} = {\bf R}_{\,{\bf n}} (\alpha )\;{\bf u} = \left( {\matrix{ {\cos \alpha } & { - \sin \alpha } & 0 \cr {\sin \alpha } & {\cos \alpha } & 0 \cr 0 & 0 & 1 \cr } } \right)\;{\bf u} $$ Since we can write $$ \begin{array}{l} {\bf v} = \left( {\begin{array}{*{20}c} {\bf u} &| & {\bf t} &| & {\bf n} \\ \end{array}} \right)\left( {\begin{array}{*{20}c} {v_{\,u} } \\ {v_{\,t} } \\ {v_{\,n} } \\ \end{array}} \right) = {\bf T}\left( {\begin{array}{*{20}c} {\bf i} &| & {\bf j} &| & {\bf k} \\ \end{array}} \right)\left( {\begin{array}{*{20}c} {v_{\,u} } \\ {v_{\,t} } \\ {v_{\,n} } \\ \end{array}} \right) = \\ = {\bf T}\;\left( {\begin{array}{*{20}c} {\bf i} &| & {\bf j} &| & {\bf k} \\ \end{array}} \right)\;{\bf T}^{\, - \,{\bf 1}} \;{\bf T}\;\left( {\begin{array}{*{20}c} {v_{\,u} } \\ {v_{\,t} } \\ {v_{\,n} } \\ \end{array}} \right) = {\bf T}\;{\bf I}\;{\bf T}^{\, - \,{\bf 1}} \;{\bf T}\;\left( {\begin{array}{*{20}c} {v_{\,u} } \\ {v_{\,t} } \\ {v_{\,n} } \\ \end{array}} \right) = \\ = \;{\bf T}\;\left( {\begin{array}{*{20}c} {v_{\,u} } \\ {v_{\,t} } \\ {v_{\,n} } \\ \end{array}} \right) = \left( {\begin{array}{*{20}c} {\bf i} &| & {\bf j} &| & {\bf k} \\ \end{array}} \right)\left( {\begin{array}{*{20}c} {v_{\,x} } \\ {v_{\,y} } \\ {v_{\,z} } \\ \end{array}} \right) \\ \end{array} $$ then $$ \begin{array}{l} \left( {\begin{array}{*{20}c} {v_{\,x} } \\ {v_{\,y} } \\ {v_{\,z} } \\ \end{array}} \right) = \;{\bf T}\;\left( {\begin{array}{*{20}c} {v_{\,u} } \\ {v_{\,t} } \\ {v_{\,n} } \\ \end{array}} \right) = {\bf TR}_{\,{\bf n}} (\alpha )\;{\bf u}_{u,\,t,\,n} = {\bf TR}_{\,{\bf n}} (\alpha )\;{\bf T}^{\, - \,{\bf 1}} \;{\bf T}\;{\bf u}_{u,\,t,\,n} = \\ = {\bf TR}_{\,{\bf n}} (\alpha )\;{\bf T}^{\, - \,{\bf 1}} \;{\bf T}\;{\bf u}_{x,\,y,\,z} \\ \end{array} $$ and finally, with $\bf u, \; \bf v$ expressed in the $(x,y,z)$ ref. system: $$ {\bf v} = {\bf T}\,{\bf R}_{\,{\bf n}} (\alpha )\;{\bf T}^{\, - \,{\bf 1}} \,{\bf u} $$

As you can see it is a quite fluid and general procedure.

As for the example you give, we have $$ \begin{array}{l} {\bf u} = \left( {\begin{array}{*{20}c} 1 \\ 0 \\ 0 \\ \end{array}} \right)\quad \quad {\bf v} = \left( {\begin{array}{*{20}c} 0 \\ {\sqrt 2 /2} \\ {\sqrt 2 /2} \\ \end{array}} \right) \\ {\bf n} = \frac{{{\bf u} \times {\bf v}}}{{\left| {{\bf u} \times {\bf v}} \right|}} = \left( {\begin{array}{*{20}c} 0 \\ { - \sqrt 2 /2} \\ {\sqrt 2 /2} \\ \end{array}} \right)\quad \quad {\bf t} = {\bf n} \times {\bf u} = \left( {\begin{array}{*{20}c} 0 \\ {\sqrt 2 /2} \\ {\sqrt 2 /2} \\ \end{array}} \right) \\ \alpha = \arctan _2 \left( {{\bf v} \cdot {\bf u},\;{\bf v} \cdot {\bf t}} \right) = \arctan _2 \left( {0,\;1} \right) = \pi /2 \\ {\bf T} = \left( {\begin{array}{*{20}c} {\bf u} &| & {\bf t} &| & {\bf n} \\ \end{array}} \right) = \left( {\begin{array}{*{20}c} 1 & 0 & 0 \\ 0 & {\sqrt 2 /2} & { - \sqrt 2 /2} \\ 0 & {\sqrt 2 /2} & {\sqrt 2 /2} \\ \end{array}} \right) \\ {\bf R}_{\,{\bf n}} (\alpha ) = \left( {\begin{array}{*{20}c} {\cos \alpha } & { - \sin \alpha } & 0 \\ {\sin \alpha } & {\cos \alpha } & 0 \\ 0 & 0 & 1 \\ \end{array}} \right) = \left( {\begin{array}{*{20}c} 0 & { - 1} & 0 \\ 1 & 0 & 0 \\ 0 & 0 & 1 \\ \end{array}} \right) \\ {\bf T}\,{\bf R}_{\,{\bf n}} (\alpha )\;{\bf T}^{\, - \,{\bf 1}} = \\ = \left( {\begin{array}{*{20}c} 1 & 0 & 0 \\ 0 & {\sqrt 2 /2} & { - \sqrt 2 /2} \\ 0 & {\sqrt 2 /2} & {\sqrt 2 /2} \\ \end{array}} \right)\left( {\begin{array}{*{20}c} 0 & { - 1} & 0 \\ 1 & 0 & 0 \\ 0 & 0 & 1 \\ \end{array}} \right)\left( {\begin{array}{*{20}c} 1 & 0 & 0 \\ 0 & {\sqrt 2 /2} & {\sqrt 2 /2} \\ 0 & { - \sqrt 2 /2} & {\sqrt 2 /2} \\ \end{array}} \right) = \\ = \left( {\begin{array}{*{20}c} 0 & { - \sqrt 2 /2} & { - \sqrt 2 /2} \\ {\sqrt 2 /2} & {1/2} & { - 1/2} \\ {\sqrt 2 /2} & { - 1/2} & {1/2} \\ \end{array}} \right) \\ \end{array} $$ and in fact $$ \left( {\begin{array}{*{20}c} 0 \\ {\sqrt 2 /2} \\ {\sqrt 2 /2} \\ \end{array}} \right) = \left( {\begin{array}{*{20}c} 0 & { - \sqrt 2 /2} & { - \sqrt 2 /2} \\ {\sqrt 2 /2} & {1/2} & { - 1/2} \\ {\sqrt 2 /2} & { - 1/2} & {1/2} \\ \end{array}} \right)\left( {\begin{array}{*{20}c} 1 \\ 0 \\ 0 \\ \end{array}} \right) $$

Going your way instead, just for the cited example, you shall first rotate around $z$ by $+\pi/2$ (RH rule), and then around the new $y$ axis, which has become the $-x$, i.e. $x$ axis, by $+\pi/4$ (RH rule again). The matrices are $$ {\bf R}_{\,{\bf x}} (\alpha ) = \left( {\begin{array}{*{20}c} 1 & 0 & 0 \\ 0 & {\cos \alpha } & { - \sin \alpha } \\ 0 & {\sin \alpha } & {\cos \alpha } \\ \end{array}} \right)\quad {\bf R}_{\,{\bf y}} (\beta ) = \left( {\begin{array}{*{20}c} {\cos \beta } & 0 & {\sin \beta } \\ 0 & 1 & 0 \\ { - \sin \beta } & 0 & {\cos \beta } \\ \end{array}} \right)\quad {\bf R}_{\,{\bf z}} (\gamma ) = \left( {\begin{array}{*{20}c} {\cos \gamma } & { - \sin \gamma } & 0 \\ {\sin \gamma } & {\cos \gamma } & 0 \\ 0 & 0 & 1 \\ \end{array}} \right) $$ they always act on the base reference, and the sign of the angle is according to the RH rule, so $$ \begin{array}{l} \left( {\begin{array}{*{20}c} 0 \\ {\sqrt 2 /2} \\ {\sqrt 2 /2} \\ \end{array}} \right) = {\bf R}_{\,{\bf x}} (\pi /4){\bf R}_{\,{\bf z}} (\pi /2)\left( {\begin{array}{*{20}c} 1 \\ 0 \\ 0 \\ \end{array}} \right) = \\ = \left( {\begin{array}{*{20}c} 1 & 0 & 0 \\ 0 & {\sqrt 2 /2} & { - \sqrt 2 /2} \\ 0 & {\sqrt 2 /2} & {\sqrt 2 /2} \\ \end{array}} \right)\left( {\begin{array}{*{20}c} 0 & { - 1} & 0 \\ 1 & 0 & 0 \\ 0 & 0 & 1 \\ \end{array}} \right)\left( {\begin{array}{*{20}c} 1 \\ 0 \\ 0 \\ \end{array}} \right) = \\ = \left( {\begin{array}{*{20}c} 0 & { - 1} & 0 \\ {\sqrt 2 /2} & 0 & { - \sqrt 2 /2} \\ {\sqrt 2 /2} & 0 & {\sqrt 2 /2} \\ \end{array}} \right)\left( {\begin{array}{*{20}c} 1 \\ 0 \\ 0 \\ \end{array}} \right) \\ \end{array} $$

Solution 3:

As is mentioned here, another approach to the problem is to use quaternions. The basic wiki page and the rotation wiki page are very good resources for understanding how these generalizations of complex numbers work. The procedure is as follows:

  1. Compute the cross product of the vectors going to the two points: this will be the axis of rotation, and gets inserted into the $\mathbf{i}, \mathbf{j},$ and $\mathbf{k}$ components of the rotation quaternion.
  2. Compute the dot product of the vectors going to the two points: this allows you to find the angle of rotation needed, which goes into the real part of the quaternion, as well as multiplying the complex parts. The angle is the $\arccos$ of the dot product (recall that $\mathbf{x}\cdot\mathbf{y}=\|x\|\,\|y\|\,\cos(\theta)$, so if $\|x\|=\|y\|=1$, we have $\theta=\arccos(\mathbf{x}\cdot\mathbf{y})$).

So, for your example: you want to find the rotation needed to get from point $\mathbf{v}_1=(1,0,0)$ to point $\mathbf{v}_2=\left(0,\frac{1}{\sqrt{2}},\frac{1}{\sqrt{2}}\right)$. We compute: \begin{align*} \mathbf{v}_1\times\mathbf{v}_2&=\left(0,-\frac{1}{\sqrt{2}},\frac{1}{\sqrt{2}}\right)\quad\text{and}\\ \mathbf{v}_1\cdot\mathbf{v}_2&=0. \end{align*} Hence, the angle of rotation is $\theta=\arccos(0)=\dfrac{\pi}{2}.$ From the rotation quaternion wiki page, we see that the necessary rotation quaternion is \begin{align*}q&=\cos\left(\frac{\theta}{2}\right)+(u_x\mathbf{i}+u_y\mathbf{j}+u_z\mathbf{k})\,\sin\left(\frac{\theta}{2}\right)\\ &=\frac{1}{\sqrt{2}}+\left(0\mathbf{i}-\frac{\mathbf{j}}{\sqrt{2}}+\frac{\mathbf{k}}{\sqrt{2}}\right)\frac{1}{\sqrt{2}}\\ &=\frac{1}{\sqrt{2}}+\left(0\mathbf{i}-\frac{\mathbf{j}}{2}+\frac{\mathbf{k}}{2}\right).\end{align*} Now we double-check that the rotation represented by this quaternion actually works, and here's where the fun really begins: quaternion multiplication! The rotation from $\mathbf{v}_1$ to $\mathbf{v}_2$ ought to work as follows: $$\mathbf{v}_2=q\mathbf{v}_1q^{-1}.$$ Here I'm going to refer to the Wolfram Language (Mathematica), as implemented on the Wolfram Dev Platform. We use the following code:

<<Quaternions`
q=Quaternion[1/Sqrt[2],0,-1/2,1/2]
v1=Quaternion[0,1,0,0]
NonCommutativeMultiply[q,NonCommutativeMultiply[v1,Conjugate[q]]]

Mathematica produces the answer $$\operatorname{Quaternion}\left[0,0,\frac{1}{\sqrt{2}},\frac{1}{\sqrt{2}}\right]$$

Notice here we must use the NonCommutativeMultiply function to get the correct quaternion multiplication. Also notice that for a unit quaternion (which rotation quaternions are), $q^{-1}=\overline{q}$. Finally, notice that we must "quaternion-ify" the vector $\mathbf{v}_1$ as the quaternion $(0,v_{1x},v_{1y},v_{1z})$, and the result is a quaternion which we can "vector-ify" by extracting the last three components. The result, as you can see, is correct.

If you prefer to have the rotation matrix, you can get such an $\mathbf{R}$ as explained on the rotation quaternion wiki page under the section "Quaternion-derived rotation matrix".