36

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.

Josh
  • 463
  • 1
  • 4
  • 5
  • 1
    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 01:29

1 Answers1

62

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.

Jim Belk
  • 46,369
  • 2
  • 122
  • 188
  • Thanks Jim, that makes things a lot clearer. – Josh Dec 10 '11 at 05:09
  • Are you sure that angle formula is right? If $\cos(\theta/2) = $, then $\theta = 2\arccos()$ and a measure would be $d(q_1,q_2) = 2\pi - 2\arccos()$, where 0 means the same and $2\pi$ is the farthest. (Assuming unit quaternions) – Alec Jacobson Jan 09 '14 at 16:34
  • 3
    Should have been: $d(q_1,q_2) = 2\arccos()$ – Alec Jacobson Jan 09 '14 at 17:01
  • 3
    @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 09 '14 at 17:56
  • @JimBelk, I see. can you provide an intuition for $ < 0$? For unit quaternions, does this correspond to living on the "other" covering? – Alec Jacobson Jan 09 '14 at 23:35
  • 2
    @mangledorf Something like that, although it isn't possible to separate the two sheets of the cover. But $\langle q_1,q_2\rangle<0$ corresponds to the case where $-q_2$ is "closer" to $q_1$ than $q_2$ is. – Jim Belk Jan 09 '14 at 23:58
  • 4
    According to ["Metrics for 3D rotations: Comparison and Analysis"][1] which compares a number of metrics, you might want to use the geodesic distance of the normed quaternions, since these lie on the unit 3-sphere (the euclidean distance is not appropriate there). It so happens that it is exactly the formula for $\theta$ that @JimBelk mentions. [1]: http://dx.doi.org/10.1007/s10851-009-0161-2 – yannick Apr 14 '14 at 09:11
  • @JimBelk Thanks for the answer! Isn't the max distance between quaternions $\sqrt2$? Or do you have a suggestion for how to get a more exact relationship between quaternion distance and angle distance? – amos Sep 17 '14 at 17:41
  • 1
    @JimBelk could it be possible to get a refference to where you got the formula of the angle between the two quaternions? I can see that the distance is equation 20 in the paper Yannick mentioned (dx.doi.org/10.1007/s10851-009-0161-2) but I cannot find the other one. – apalomer May 15 '18 at 10:02
  • How can this answer be extended to handle averaging of similar quaternions? If the distance measure $d(q_1,q_2)$ is used, by design it's impossible to tell the difference between a quaternion and its negation. Quaternions can't be averaged element-wise without accounting for $q$ and $-q$ representing the same rotation. https://math.stackexchange.com/questions/3888504 – Luke Hutchison Nov 04 '20 at 08:31
  • @apalomer did you ever find a reference to where these equations came from? The Equation 20 that you referred to is not computing the same thing as this distance measure -- that paper uses the absolute value of the dot product, not the squared value. – Luke Hutchison Nov 08 '20 at 04:47
  • 2
    @LukeHutchison In the paper Yannick mentioned, the angle $\theta$ I describe above is the bi-invariant metric denoted $\Phi_6$. Equation 34 says that $\Phi_6=2\Phi_3$ and equation 19 says that $\Phi_3=\arccos(|\textbf{q}_1\cdot \textbf{q}_2|)$. The equation for $\theta$ that I give now follows from the trig identity $2 \arccos |t| = \arccos(2t^2-1)$. – Jim Belk Nov 08 '20 at 11:55
  • Excuse my ignorance, but `(1-⟨q1,q2⟩²)*π` seems to work very well and thank you for that. However, I get very random results from: `acos(2*⟨q1,q2⟩²−1)` which I understood to be equivalent but require more processing. Could this be clarified for curiosity sake? – Mike Weir May 13 '21 at 23:14
  • 1
    @MikeWeir It's not "equivalent" exactly. The quantity $1-\langle q_1,q_2\rangle^2$ gives results between 0 and 1, with a 0 if the orientations are the same and a 1 if the orientations differ by 180 degrees. The quantity $\cos^{-1}(2\langle q_1,q_2\rangle^2-1)$ gives results between 0 and $\pi$ (or between 0 and 180 if you're using degrees for your inverse cosine), with a 0 if the orientations are the same and a $\pi$ (or 180) if the orientations differ by 180 degrees. The latter is the actual angle, but requires more processing, while the former is only an approximation. – Jim Belk May 14 '21 at 12:04