1
$\begingroup$

I want to align the x-axis of my coordinate system, with an arbitrary direction in space $\hat{n}$. About which axis should I rotate? Ceratinty rotation about x-axis or $\hat{n}$-axis will not serve the purpose. What would be the rotation matrix acting on my coordinate system that will serve my purpose?

$\endgroup$

3 Answers 3

4
$\begingroup$

Let $\theta>0$ denote the angle between the $\hat{\mathbf x}$ and $\hat{\mathbf n}$. Notice that

$$ \hat{\mathbf u} = \frac{\hat{\mathbf x}\times\hat{\mathbf n}}{\sin\theta} $$

is a unit vector perpendicular to both $\hat{\mathbf x}$ and $\hat{\mathbf n}$. The desired rotation is a right-handed rotation around $\hat{\mathbf u}$ by the angle $\theta$.

$\endgroup$
3
$\begingroup$

There is a very quick and clean way of doing this, which is presented in

Building an Orthonormal Basis from a 3D Unit Vector Without Normalization. JR Frisvad. J. Graphics Tools 16 no. 3, 151 (2012).

Suppose you have a normalized vector $\vec n=(n_x,n_y,n_z)^T$, and you want a rotation matrix that will take the $z$ axis into $\vec n$. (Here it's important to note that this will never be unique, as a further rotation about $\vec n$ is always possible, but the question sort of assumes that you don't care about this degree of freedom.)

As Josh mentions, this is accomplished by doing a rotation by $\theta=\arccos(\vec n\cdot\hat z)$ about the axis $\hat u=\vec n\times\hat z/\sin(\theta)$, and this can be used to build up a rotation matrix using these techniques from Wikipedia but that's a lot of work.

Alternatively, you can find two other vectors orthogonal to $\vec n$ and stick them on a matrix, by choosing e.g. the $\hat x$ unit vector and taking $\hat n\times\hat x$ as your initial vector, but normalizing them is generally a huge pain.

Instead, Frisvad gets, via quaternionic methods, a simple triplet of orthogonal vectors,

$$ \begin{pmatrix}1-\frac{n_x^2}{1+n_z} \\ -\frac{n_x n_y}{1+n_z} \\ -n_x \end{pmatrix} ,\ \begin{pmatrix}-\frac{n_x n_y}{1+n_z} \\ 1-\frac{n_y^2}{1+n_z} \\ -n_y \end{pmatrix} \ \text{and} \ \begin{pmatrix}n_x \\ n_y \\ n_z\end{pmatrix} $$

and these give you your matrix directly, $$ \begin{pmatrix} 1-\frac{n_x^2}{1+n_z} & -\frac{n_x n_y}{1+n_z} & n_x \\ -\frac{n_x n_y}{1+n_z} & 1-\frac{n_y^2}{1+n_z} & n_y \\ -n_x & -n_y & n_z \end{pmatrix}. $$ This can be verified directly to be an orthogonal matrix, and it satisfies the criterion, so you're essentially set. In addition, this is simple both conceptually and numerically, and it only faces numerical trouble when $n_z$ is close to $-1$, in which case $n_x$ and $n_y$ are quadratically closer to zero than $1+n_z$, so even then it should be quite stable numerically.

$\endgroup$
5
  • $\begingroup$ What if you do care about the remaining degree fo freedom? Is there a simple way to modify this answer to enforce a rotation about $\hat{n}$? I suppose since, after preforming the rotation as you describe, we have $\hat{z} = \hat{n}$, we can just rotate about $\hat{z}$ in the standard way as a second step. Though it may be nice to instead include the constraint in the original rotation matrix. $\endgroup$ Commented Dec 7, 2022 at 16:48
  • $\begingroup$ @pretzlstyle It depends on what you want to do with that extra DOF. Do you have a particular direction vector, orthogonal to $\hat n$, that you want the x or y axis to point along? If so, you can just make your rotation matrix out of those two unit vectors and their cross product. $\endgroup$ Commented Dec 7, 2022 at 22:33
  • $\begingroup$ Oh, meaning that I cannot use the approach of Frisvad? I'll need to build a rotation matrix in the standard way? Or is there a way to adjust Frisvad's result? I've gone through the paper, but the quaternion calculation is lost on me $\endgroup$ Commented Dec 8, 2022 at 7:20
  • $\begingroup$ It's not that you can't use it, it's that you don't need to. The Frisvad result is a simple way to break the ambiguity of finding two unit vectors which are orthogonal to your chosen $\hat n$. However, if you already have a preferred way to break the ambiguity, then you should use that instead. The details depend on what information you have, and if you know less than a full knowledge of a second unit vector $\hat m$ (orthogonal to $\hat n$) then you'll need to work harder (with the details depending on the details). $\endgroup$ Commented Dec 8, 2022 at 15:17
  • $\begingroup$ If you do have $\hat n$ and $\hat m$, then you also have their cross product and you're done, though -- you have a complete frame, and that directly gives you the rotation matrix by putting all three unit vectors in a row. $\endgroup$ Commented Dec 8, 2022 at 15:17
1
$\begingroup$

In 3 dimensions you can use the cross product to get an appropriate rotation axis.

If $\hat{x}$ and $\hat{n}$ are non-parallel 3-dimensional unit vectors then $\vec{s}=\hat{n}\times \hat{x}$ is non-zero. Since $\vec{s}$ is orthogonal to both $\hat{n}$ and $\hat{x}$ there is some rotation about $\vec{s}$ that takes $\hat{n}$ to $\hat{x}$. You can get the requires angle of rotation with the dot product $\hat{n}\cdot \hat{x}=\cos \theta$.

Caveats: 1)If $\hat{x}$ is parallel or anti-parallel to $\hat{n}$ then $\vec{s} = 0$ and this technique will fail (but then it is obvious how to make $\hat{n}=\hat{x}$). 2) Remember that $\vec{s}$ is not necessarily a unit vector, which might affect the construction of your rotation matrix. 3) This technique might not be great numerically, though I bet you can use some vector identities to make it more stable.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.