<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[The ryg blog]]></provider_name><provider_url><![CDATA[https://fgiesen.wordpress.com]]></provider_url><author_name><![CDATA[fgiesen]]></author_name><author_url><![CDATA[https://fgiesen.wordpress.com/author/fgiesen/]]></author_url><title><![CDATA[Small note on Quaternion distance&nbsp;metrics]]></title><type><![CDATA[link]]></type><html><![CDATA[<p>There&#8217;s multiple ways to measure distances between unit quaternions (a popular rotation representation in 3D). What&#8217;s interesting is that the popular choices are essentially all equivalent.</p>
<h3>Polar form</h3>
<p>A standard way to build quaternions is using the polar (axis-angle) form<br />
<img src="https://s0.wp.com/latex.php?latex=q+%3D+%5Cexp%28%5Cfrac%7B1%7D%7B2%7D+%5Ctheta+%5Cmathbf%7Bn%7D%29+%3D+%5Ccos%28%5Ctheta%2F2%29+%2B+%5Csin%28%5Ctheta%2F2%29+%28i+n_x+%2B+j+n_y+%2B+k+n_z%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=q+%3D+%5Cexp%28%5Cfrac%7B1%7D%7B2%7D+%5Ctheta+%5Cmathbf%7Bn%7D%29+%3D+%5Ccos%28%5Ctheta%2F2%29+%2B+%5Csin%28%5Ctheta%2F2%29+%28i+n_x+%2B+j+n_y+%2B+k+n_z%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=q+%3D+%5Cexp%28%5Cfrac%7B1%7D%7B2%7D+%5Ctheta+%5Cmathbf%7Bn%7D%29+%3D+%5Ccos%28%5Ctheta%2F2%29+%2B+%5Csin%28%5Ctheta%2F2%29+%28i+n_x+%2B+j+n_y+%2B+k+n_z%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="q = &#92;exp(&#92;frac{1}{2} &#92;theta &#92;mathbf{n}) = &#92;cos(&#92;theta/2) + &#92;sin(&#92;theta/2) (i n_x + j n_y + k n_z)" class="latex" />, where n is the (unit length) axis of rotation, &theta; is the angle, and i, j and k are the imaginary basis vectors.</p>
<p>For a rotation in this form, we know how &#8220;far&#8221; it goes: it&#8217;s just the angle &theta;. Since the real component of q is just cos(&theta;/2), we can read off the angle as<br />
<img src="https://s0.wp.com/latex.php?latex=%5Ctheta%28q%29+%3D+%5Ctheta%28q%2C+1%29+%3A%3D+2+%5Carccos%28%5Ctextrm%7Breal%7D%28q%29%29+%3D+2+%5Carccos%28q+%5Ccdot+1%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Ctheta%28q%29+%3D+%5Ctheta%28q%2C+1%29+%3A%3D+2+%5Carccos%28%5Ctextrm%7Breal%7D%28q%29%29+%3D+2+%5Carccos%28q+%5Ccdot+1%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Ctheta%28q%29+%3D+%5Ctheta%28q%2C+1%29+%3A%3D+2+%5Carccos%28%5Ctextrm%7Breal%7D%28q%29%29+%3D+2+%5Carccos%28q+%5Ccdot+1%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;theta(q) = &#92;theta(q, 1) := 2 &#92;arccos(&#92;textrm{real}(q)) = 2 &#92;arccos(q &#92;cdot 1)" class="latex" /><br />
where the dot denotes the quaternion dot product.</p>
<p>This measures, in a sense, how far away the quaternion is from the identity element 1. To get a distance between two unit quaternions q and r, we rotate both of them such that one of them becomes the identity element. To do this for our pair q, r, we simply multiply both by r&#8217;s inverse from the left, and since r is a unit quaternion its inverse and conjugate are the same:<br />
<img src="https://s0.wp.com/latex.php?latex=%5Ctheta%28q%2Cr%29+%3A%3D+%5Ctheta%28r%5E%2Aq%2C+r%5E%2Ar%29+%3D+%5Ctheta%28r%5E%2Aq%2C+1%29+%3D+2+%5Carccos%28%28r%5E%2Aq%29+%5Ccdot+1%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Ctheta%28q%2Cr%29+%3A%3D+%5Ctheta%28r%5E%2Aq%2C+r%5E%2Ar%29+%3D+%5Ctheta%28r%5E%2Aq%2C+1%29+%3D+2+%5Carccos%28%28r%5E%2Aq%29+%5Ccdot+1%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Ctheta%28q%2Cr%29+%3A%3D+%5Ctheta%28r%5E%2Aq%2C+r%5E%2Ar%29+%3D+%5Ctheta%28r%5E%2Aq%2C+1%29+%3D+2+%5Carccos%28%28r%5E%2Aq%29+%5Ccdot+1%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;theta(q,r) := &#92;theta(r^*q, r^*r) = &#92;theta(r^*q, 1) = 2 &#92;arccos((r^*q) &#92;cdot 1)" class="latex" /></p>
<p>Note that cosine is a monotonic function over the interval we care about, so in any numerical work, there&#8217;s basically never the need to actually calculate that arc cosine: instead of checking, say, whether the angle is less than some maximum error threshold T, we can simple check that the dot product is larger than cos(T/2). If you&#8217;re actually taking the arc cosine for anything other than display purposes, you&#8217;re likely doing something wrong.</p>
<h3>Dot product</h3>
<p>Another way is to use the dot product directly as a distance measure between two quaternions. How does this relate to the angle from the polar form? It&#8217;s the same, as we quickly find out when we use the fact that the dot product is invariant under rotations:</p>
<p><img src="https://s0.wp.com/latex.php?latex=%28q+%5Ccdot+r%29+%3D+%28r%5E%2Aq+%5Ccdot+r%5E%2Ar%29+%3D+%28r%5E%2Aq+%5Ccdot+1%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%28q+%5Ccdot+r%29+%3D+%28r%5E%2Aq+%5Ccdot+r%5E%2Ar%29+%3D+%28r%5E%2Aq+%5Ccdot+1%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%28q+%5Ccdot+r%29+%3D+%28r%5E%2Aq+%5Ccdot+r%5E%2Ar%29+%3D+%28r%5E%2Aq+%5Ccdot+1%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="(q &#92;cdot r) = (r^*q &#92;cdot r^*r) = (r^*q &#92;cdot 1)" class="latex" /></p>
<p>and hence also</p>
<p><img src="https://s0.wp.com/latex.php?latex=%5Ctheta%28q%2Cr%29+%3D+2+%5Carccos%28q+%5Ccdot+r%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Ctheta%28q%2Cr%29+%3D+2+%5Carccos%28q+%5Ccdot+r%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Ctheta%28q%2Cr%29+%3D+2+%5Carccos%28q+%5Ccdot+r%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;theta(q,r) = 2 &#92;arccos(q &#92;cdot r)" class="latex" /></p>
<p>So again, whether we minimize the angle between q and r (as measured in the polar form) or maximize the dot product between q and r boils down to the same thing. But there&#8217;s one final choice left.</p>
<h3>L<sub>2</sub> distance</h3>
<p>The third convenient metric is just using the norm of the difference between the two quaternions: <img src="https://s0.wp.com/latex.php?latex=%7C%7Cq-r%7C%7C&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%7C%7Cq-r%7C%7C&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%7C%7Cq-r%7C%7C&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="||q-r||" class="latex" />. The question is, can we relate this somehow to the other two? We can, and as is often the case, it&#8217;s easier to work with the square of the norm:</p>
<p><img src="https://s0.wp.com/latex.php?latex=%7C%7Cq-r%7C%7C%5E2+%3D+%7C%7Cq%7C%7C%5E2+-+2+%28q+%5Ccdot+r%29+%2B+%7C%7Cr%7C%7C%5E2+%3D+1+-+2+%28q+%5Ccdot+r%29+%2B+1+%3D+2+%281+-+q+%5Ccdot+r%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%7C%7Cq-r%7C%7C%5E2+%3D+%7C%7Cq%7C%7C%5E2+-+2+%28q+%5Ccdot+r%29+%2B+%7C%7Cr%7C%7C%5E2+%3D+1+-+2+%28q+%5Ccdot+r%29+%2B+1+%3D+2+%281+-+q+%5Ccdot+r%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%7C%7Cq-r%7C%7C%5E2+%3D+%7C%7Cq%7C%7C%5E2+-+2+%28q+%5Ccdot+r%29+%2B+%7C%7Cr%7C%7C%5E2+%3D+1+-+2+%28q+%5Ccdot+r%29+%2B+1+%3D+2+%281+-+q+%5Ccdot+r%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="||q-r||^2 = ||q||^2 - 2 (q &#92;cdot r) + ||r||^2 = 1 - 2 (q &#92;cdot r) + 1 = 2 (1 - q &#92;cdot r)" class="latex" />.</p>
<p>In other words, the distance between two unit quaternions again just boils down to the dot product between them &#8211; albeit with a scale and bias this time.</p>
<h3>Conclusion</h3>
<p>The popular choices of distance metrics between quaternions all boil down to the same thing. The relationships between them are simple enough that it&#8217;s easy to convert, say, an exact error bound on the norm between two quaternions into an exact error bound on the angle of the corresponding rotation. Each of these three representations is the most convenient to use in some context; feel free to convert back and forth between them for different solvers; they&#8217;re all compatible in the sense that their minima will always agree.</p>
<p><b>UPDATE:</b> As Sam points out in the comments, you need to be careful about the distinction between quaternions and rotations here (I cleared up the language in the article slightly). Each rotation in 3-dimensional real Euclidean space has two representations as a quaternion: the quaternion group double-covers the rotation group. If you want to measure the distances between rotations not quaternions, you need to use slightly modified metrics (see his comment for details).</p>
]]></html></oembed>