# Quaternion distance

I am using quaternions to represent orientation as a rotational offset from a global coordinate frame.

Is it correct in thinking that quaternion distance gives a metric that defines the closeness of two orientations? i.e. similar orientations give low distances, and dissimilar orientations give high distances.

Does zero distance mean that the orientations are exactly the same?

This seems obvious, but I want to ensure that there are no subtleties that get in the way of logic.

-
The quaternions are a normed vector space, thus $\|x-y\|=0$ implies $x-y=0$. That is, distance zero implies $x=y$. Any norm induces a metric, $d(x,y)=\|x-y\|$. So your question is, for quaterions $q_1,q_2$ which are close, is $q_1uq_1^{-1}$ close to $q_2uq_2^{-1}$ for vector $u \in \mathbb{R}^3$? I'm unsure how to answer this part of the question. – dls Dec 10 '11 at 1:29

First, I assume that you're using unit quaternions, i.e. quaternions $a+b\,\textbf{i}+c\,\textbf{j}+d\,\textbf{k}$ that satisfy $a^2 + b^2 + c^2 + d^2 = 1$. If not, you'll want to scale your quaternions before computing distance.

Distance between quaternions will correspond roughly to distance between orientations as long as the quaternions are fairly close to each other. However, if you're comparing quaternions globally, you should remember that $q$ and $-q$ always represent the same orientation, even though the distance between them is $2$.

There are better ways to compute the closeness of two orientations that avoid this problem. For example, the angle $\theta$ of rotation required to get from one orientation to another is given by the formula $$\theta \;=\; \cos^{-1}\bigl(2\langle q_1,q_2\rangle^2 -1\bigr)$$ where $\langle q_1,q_2\rangle$ denotes the inner product of the corresponding quaternions: $$\langle a_1 +b_1 \textbf{i} + c_1 \textbf{j} + d_1 \textbf{k},\; a_2 + b_2 \textbf{i} + c_2 \textbf{j} + d_2 \textbf{k}\rangle \;=\; a_1a_2 + b_1b_2 + c_1 c_2 + d_1d_2.$$ (This formula follows from the double-angle formula for cosine, together with the fact that the angle between orientations is precisely twice the angle between unit quaternions.)

If you want a notion of distance that can be computed without trig functions, the quantity $$d(q_1,q_2) \;=\; 1 - \langle q_1,q_2\rangle^2$$ is equal to $(1-\cos\theta)/2$, and gives a rough estimate of the distance. In particular, it gives $0$ whenever the quaternions represent the same orientation, and it gives $1$ whenever the two orientations are $180^\circ$ apart.

-
Thanks Jim, that makes things a lot clearer. – Josh Dec 10 '11 at 5:09
Are you sure that angle formula is right? If $\cos(\theta/2) = <q_1,q_2>$, then $\theta = 2\arccos(<q_1,q_2>)$ and a measure would be $d(q_1,q_2) = 2\pi - 2\arccos(<q_1,q_2>)$, where 0 means the same and $2\pi$ is the farthest. (Assuming unit quaternions) – Alec Jacobson Jan 9 '14 at 16:34
Should have been: $d(q_1,q_2) = 2\arccos(<q_1,q_2>)$ – Alec Jacobson Jan 9 '14 at 17:01
@mangledorf The formula I give for $\theta$ is correct in all cases. The formula $\theta = 2 \arccos(\langle q_1,q_2\rangle)$ doesn't work when $\langle q_1,q_2\rangle < 0$, although $\theta = 2\arccos(|\langle q_1q_2\rangle |)$ would be fine. – Jim Belk Jan 9 '14 at 17:56
@JimBelk, I see. can you provide an intuition for $<q_1,q_2> < 0$? For unit quaternions, does this correspond to living on the "other" covering? – Alec Jacobson Jan 9 '14 at 23:35