# Distance between quaternion with opposite signs

I want to compute the distance between two unit quaternions to know if the angle between them is too big, because I don't want to apply the rotation if the angle between the quaternions is higher than $\pi$.

I have a problem with quaternion of opposite sign. Between $Q_1 = (-0.33, 0.13, -0.93, 0.05)$ and $Q_2 = (-0.37, 0.10, -0.92, 0.00)$ I compute a distance of $0.15$, but between $Q_1$ and $Q_3 = (0.37, -0.09, 0.92, -0.00)$ I've got $6.14 = 2\pi -0.15$. Both $Q_2$ and $Q_3$ represents the same orientation but with opposite sign. I wonder why I don't have the same result.

The calculations I have to compute the distance between $Q_1$ and $Q_2$ are :

(1)$$QuatProjection = Q_1 \times Q_2^{-1}$$ where $\times$ is the Hamilton product and $Q_2^{-1}$ is the conjugate of $Q_2$.

(2)$$angle = 2arctan \Bigl(\frac{||QuatProjection||}{qw}\Bigr)$$ with $qw$ the $lambda$ of the quaternion $QuatProjection = (qx, qy, qz, qw)$ and $||Q||$ the norm of $Q$

(3)$$QuatProjection = QuatProjection.\frac{angle}{||QuatProjection||}$$ with $.$ the scalar multiplication with each members of the quaternion.

Finally,

(4)$$distance = || QuatProjection ||$$

It seems that if I use the absolute value of $qw$, I've got the correct result but I don't understand why. It might just be fortuitous.

Although, I don't understand the purpose of the (3) equation, the scalar multiplication. I will be really grateful if someone can help me with my problem.

• It seems that my use of atan2() instead of atan() is part of the answer, so I edited the question to match exactly my computation – mlel Nov 20 '17 at 16:04

It took some days to figure out the answer.

The purpose of equation (3)

We are using unit quaternions so $distance$ in (4) is equivalent to $angle$ in (2):

(3) + (4) gives

$distance = ||\Bigl(\frac{QuatProjection}{||QuatProjection||}\Bigr).angle||$

$distance = ||\Bigl(\frac{QuatProjection}{||QuatProjection||}\Bigr)||.||angle||$

with angle a scalar and $\frac{QuatProjection}{||QuatProjection||} = 1$ , for unit quaternion

$distance = angle$

So, in this case equations (3) and (4) are useless.

atan() vs atan2()

The use of atan or atan2 has a big influence on the result because:

$0 <= atan() <= π$

$0 <= atan2() <= 2π$

To detect angles higher than $\pi$, we shall use $atan2()$.

Quaternion with opposite sign

To get the same angle value with $Q_2$ and $Q_3$, we can make the $QuatProjection$ positive if its scalar part is negative, between (1) and (2):

(1b)

if $qw < 0$

$qx = -qx$, $qy = -qy$, $qz = -qz$ and $qw=-qw$

It conserves the value of the norm of $QuatProjection$.