# How to calculate rotation quaternion between two orientation quaternions?

I have some device (3D pointer) connected to my computer which returns it's position (in cartesian XYZ system) and orientation (in quaternions). I receive this values about 30 times/sec.

Now I need to draw a figure on a screen that has the same position and orientation that my pointer. The position is simple, I can directly pass X,Y and Z to my software.

The problem is that the software doesn't accept orientation (a state) but only rotation (an operation). So I think I should calculate a difference between current and previous orientation - this would be my rotation operation.

Any ideas on how to do this? What quaternion operations should be done to get rotation out of two consecutive orientations?

If $$\mathbf{q_1} = w_1 + x_1\mathbf{i} + y_1\mathbf{j} + z_1\mathbf{k}$$ is an unit quaternion, $$w_1^2 + x_1^2 + y_1^2 + z_1^2 = 1$$representing the first (earlier) rotation, and $$\mathbf{q_2} = w_2 + x_2\mathbf{i} + y_2\mathbf{j} + z_2\mathbf{k}$$ is a second (later) rotation, then their Hamilton product represents the combined rotation, \begin{align}\mathbf{q} = \mathbf{q_2} \mathbf{q_1} = \; & w_1 w_2 - x_1 x_2 - y_1 y_2 - z_1 z_2\\ + \; & (w_1 x_2 + w_2 x_1 - y_1 z_2 + y_2 z_1) \; \mathbf{i}\\ + \; & (w_1 y_2 + w_2 y_1 + x_1 z_2 - x_2 z_1) \; \mathbf{j}\\ + \; & (w_1 z_2 + w_2 z_1 - x_1 y_2 + x_2 y_1) \; \mathbf{k} \end{align} Note that the Hamilton product is not commutative; the order of the quaternions matters. The result is also an unit quaternion, except for any numerical errors that might creep in. Fortunately, you can always normalize the rotation quaternion, $$\mathbf{q'} = \frac{w + x \;\mathbf{i} + y \;\mathbf{j} + z \;\mathbf{k}}{\sqrt{w^2 + x^2 + y^2 + z^2}}$$ to avoid compounding errors. (It is basically safe to do after each operation, but usually necessary only after a few products.)
The inverse of a rotation is $$\mathbf{q}^{-1} = w - x \;\mathbf{i} - y \;\mathbf{j} - z \;\mathbf{k}$$ ie. negating all components of an unit quaternion, except for the scalar component $w$, inverts the rotation. (Negating all components does not change the rotation it represents.)
You can also interpolate between two unit quaternions, $0 \le p \le 1$, \begin{align}\mathbf{q'} = (1-p)\mathbf{q_1} + p\mathbf{q_2} & = w_1 + p (w_2 - w_1)\\ & + \left( x_1 + p (x_2 - x_1) \right ) \; \mathbf{i} \\ & + \left( y_1 + p (y_2 - y_1) \right ) \; \mathbf{j} \\ & + \left( z_1 + p (z_2 - z_1) \right ) \; \mathbf{k}\end{align} if you normalize the result to unit length, $$\mathbf{q} = \frac{w + x' \;\mathbf{i} + y' \;\mathbf{j} + z' \;\mathbf{k}}{\sqrt{w'^2 + x'^2 + y'^2 + z'^2}}$$ This is very useful in camera movement between two orientations. To get a really smooth change, use e.g. $$p = 3 p'^2 - 2 p'^3$$ or $$p = 6 p'^5 - 15 p'^4 + 10 p'^3$$ with $0 \le p' \le 1$. Both start and stop with zero velocity, but the former has a fixed rate of change of acceleration ("jerk"), and the latter starts and stops with zero acceleration.