# Derive a quaternion from three axis

My problem originates from some code that I'm writing to parse an obscure file-type in which a geometric entity is defined in it's own 'local space', and a rotation and translation are provided to convert into 'global 3D space'.

My issue is that rotations aren't given in the form of a matrix, quaternion, euler, or other "useful" form of rotation. Instead, they provide 2 of the 3 axes for the local-space (the 3rd is derived via the cross-product).

Normally this isn't an issue, as a point would be transformed by separately multiplying it's xyz values with the appropriate xyz local-axes adding them up, and then adding the translation to that. However, there are times in which I need to combine these rotations together, so my thought is to convert them to quaternions, 'add' them up, and then pull the resulting axes out of that.

To explicitly state my question: how would I derive a quaternion given the three principle axes after the rotation?

• What do you mean by "2 of the 3 axis for the local-space"? The best guess I can make, is that a vector perpendicular to the axis is given, together with its image. – Klaas van Aarsen Aug 1 '14 at 19:49
• The x and z axis are given, the y is calculated via cross. – codesmith Aug 1 '14 at 19:50
• How do an x and z axis define a rotation? – Klaas van Aarsen Aug 1 '14 at 19:50
• @IlikeSerena He means axes, not axis. He has an object whose orientation is specified by three directions of known features, and he has to figure out how that object is rotated with respect to a global frame. – Muphrid Aug 1 '14 at 19:53
• For example, the they may give me (0,0,1) as the x-axis, and (-1,0,0) as the y-axis. This would represent a rotation about the y-axis. – codesmith Aug 1 '14 at 19:53

Given the axes of the geometric body (hereafter: "body axes"), described with respect to some fixed global axes, you should be able to derive the corresponding rotation matrix that would rotate the global axes onto the body axes and vice versa.

In other words, if the body axes are $b_1, b_2, b_3$ and the global axes are $g_1, g_2, g_3$, then you want a rotation $R$ that does

$$R(g_1) = b_1, R(g_2) = b_2, R(g_3) = b_3$$

Or perhaps vice versa. Since inversion is the same as transposition for rotations, vice versa isn't hard to do either.

Now, again, let the body axes be described in terms of the global axes like so:

$$b_1 = B^{11} g_1 + B^{21} g_2 + B^{31} g_3$$

And similarly for $b_2, b_3$. Then, the matrix is basically computed for you. If you wrote out the above relations in matrix language, using the $g$ basis, you would have

$$R \begin{bmatrix} 1 \\ 0 \\ 0 \end{bmatrix} = \begin{bmatrix} B^{11} \\ B^{21} \\ B^{31}\end{bmatrix}$$

You should recognize now that this is just the first column of the matrix $R$. The columns of $R$ are just the body axes' unit vectors, expressed in terms of the global basis vectors.

Now that the matrix is known, you can use well-established algorithms to translate that into quaternions, if you should need to.

• So I just basically need to add the different axes up and then normalize them? – codesmith Aug 1 '14 at 20:03
• Add? I don't think so. Each column of $R$ is one (and only one) of the body axes. They should be normalized, yes. – Muphrid Aug 1 '14 at 20:04
• My bad, so basically the three columns are the three body axes, with the rows being the xyz components? Sorry for the confusion, I'm really rusty on my matrix math. – codesmith Aug 1 '14 at 20:22
• That is correct. – Muphrid Aug 1 '14 at 20:23

What it says, is that: $R(1,0,0)=(0,0,1)$, and $R(0,0,1)=(-1,0,0)$.
When calculating the cross product, we find furthermore that $R(0,1,0)=(0,1,0)$.
That means that: $$R \begin{bmatrix}1&0&0\\0&1&0\\0&0&1\end{bmatrix} = \begin{bmatrix}0&0&-1\\0&1&0\\1&0&0\end{bmatrix}$$ That is: $$R I = R = \begin{bmatrix}0&0&-1\\0&1&0\\1&0&0\end{bmatrix}$$