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

8.4k Views Asked by At

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?

2

There are 2 best solutions below

7
On BEST ANSWER

There are two equivalent ways of doing this.

Let $\hat x, \hat y, \hat z$ be your basis vectors. the first approach is to construct a rotation map that maps $\hat z \mapsto \hat z' = \hat n$. Then, the images $\hat x', \hat y', \hat z'$ are equal to the new coordinate basis vectors $\hat u, \hat v, \hat w$. You can extract the components of $p$ using dot products (i.e. $p_u = p \cdot \hat u$, and so on) in the usual manner. Geometrically, this is about rotating the axes to find the new basis vectors.

The other option is to construct the inverse map, which maps $\hat n \mapsto \hat z$. Then, the $xyz$-components of any image vector are the $uvw$-components of the corresponding input vector. Geometrically, you're rotating all inputs while keeping the coordinate system fixed, so you can extract new components while using the old coordinate basis. You never find the new basis vectors this way, but you don't absolutely need them. Thus, the image of $p$ under this rotation would be $p_u \hat x + p_v \hat y + p_w \hat z$.


The big question, then, is how to compute the rotation map. Doing this with matrices could be pretty onerous. I suggest a solution using quaternions. Find the axis perpendicular to $\hat z$ and $n$, probably using the cross product. Call this axis $\hat b$. Find the angle $\theta$ between $\hat z$ and $n$, probably using $\hat z \cdot \hat n = \cos \theta$. Write all the vectors using pure imaginary quaternions, and you can create a rotation map

$$R(a) = e^{\hat b \theta/2} a e^{-\hat b \theta/2}$$

If you found $\hat b$ by using $\hat z \times \hat n$, then this corresponds to the first case I described, rotating $\hat z \mapsto \hat n$. If you chose $-\hat b$ instead, then this rotates $\hat n \mapsto \hat z$, and it corresponds to the second case.

Quaternions (or their big brothers, rotors in a clifford algebra) are very useful for calculating rotations of single vectors, or rotations that do not conform to using rotations only with respect to coordinate axes.

4
On

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}) $$