# Calculate orientation quaternion given two axes of a coordinate system

I know how to use quaternions to rotate my coordinate system about an axis and how to chain those together. Since this is not too complicated I thought it would also be easy to go the other way around, but now my head is smoking and I still couldn't figure out the solution...

Here is my Problem: I have to describe the orientation of a right-handed coordinate system relative to the world coordinate system. The x- and z-axis of this coordinate system are given as vectors (which are orthogonal to each other and normalized).

How can I calculate the quaternion which describes this orientation?

Thank you!

Given your orthonormal x- and z-axis vectors (I assume within the world coordinate system), you can easily construct the orthonormal basis (change) matrix $B=\{\vec{x}, \vec{x}\times\vec{z}, \vec{z}\}$. This one may basically be viewed directly as a rotation that can be transformed into a quaternion, although you may want to transpose/invert (it's the same for orthonormal matrices) $B$ first, as that yields a transformation from the world space to the new basis (see also Change of basis matrix to convert standard basis to another basis ).
Alternatively, you may go a more indirect way and define a world-space vector $\vec{v}$ (ideally with distinctive x-, y- and z-coordinates) that you express in the new basis (as $\vec{v}^*=B^{-1}\vec{v}$) and then compute the relative rotation between $\vec{v}$ and $\vec{v}^*$, assuming both as pure quaternions and computing $q=q_v^{-1}*q_{v^*}$ (as in this question on StackOverflow ).