[Math] Rotate 3D coordinate system such that z-axis is parallel to a given vector

coordinate systemsgeometryrotations

Assume I have a Cartesian $xyz$ coordinate system with a point $p=(p_x,p_y,p_z)$. In addition a vector $n=(n_x,n_y,n_z)$ is given.

  1. How do I rotate the coordinate system to obtain a new $uvw$ system such that the $w$-axis is parallel to the vector $n$?

  2. How do I obtain the coordinates of $p$ in the $uvw$-system?

Best Answer

Based on the answer of @Muphrid and using the formula given at:

http://answers.google.com/answers/threadview/id/361441.html

I summarize the steps for later reference:

Let $\hat{n} = n/\|n\|$, $\theta=\cos^{-1}(\hat{k}\cdot \hat{n})$, $b=\hat{k}\times \hat{n}$, and $\hat{b}=(\hat{b}_x,\hat{b}_y,\hat{b}_z)=b/\|b\|$. Then define:

  • $q_0=\cos(\theta/2)$,
  • $q_1=\sin(\theta/2)\hat{b}_x$,
  • $q_2=\sin(\theta/2)\hat{b}_y$,
  • $q_3=\sin(\theta/2)\hat{b}_z$,

and

$$ Q=\begin{bmatrix} q_0^2+q_1^2-q_2^2-q_3^2 & 2(q_1q_2-q_0q_3) & 2(q_1q_3+q_0q_2)\\ 2(q_2q_1+q_0q_3) & q_0^2-q_1^2+q_2^2-q_3^2 & 2(q_2q_3-q_0q_1\\ 2(q_3q_1-q_0q_2) & 2(q_3q_2+q_0q_1) & q_0^2-q_1^2-q_2^2+q_3^2 \end{bmatrix}. $$

We then have:

  • $\hat{u}=Q\hat{i}$,
  • $\hat{v}=Q\hat{j}$,
  • $\hat{w}=Q\hat{k}=\hat{n}$.

The new coordinate of $p$ becomes $$ (p_u,p_v,p_w)=(p\cdot\hat{u},p\cdot\hat{v},p\cdot\hat{w}) $$

Related Question