<p>We can do this mostly by measuring angles between the quaternions, with a bit of sign flip to get the orientations right. Angles between normalized quaternions are given by the arccos of their dot product (as a 4D vector).
<span class="math-container">$$
t = \frac{\arccos(q_1 \cdot q)}{\arccos(q_1 \cdot q_2)} \ \text{sign}\bigl(q \cdot [q_2 - (q_1 \cdot q_2) q_1]\bigr)
$$</span>
The first factor is measuring the angle from <span class="math-container">$q_1$</span> to <span class="math-container">$q$</span> as a fraction of the angle from <span class="math-container">$q_1$</span> to <span class="math-container">$q_2$</span>. This ensures that <span class="math-container">$t$</span> will be 0 at <span class="math-container">$q_1$</span> and 1 at <span class="math-container">$q_2$</span>. The other factor ensures that <span class="math-container">$t$</span> increases as you move from <span class="math-container">$q_1$</span> toward <span class="math-container">$q_2$</span> and goes negative on the other side. The quantity in brackets is the projection of <span class="math-container">$q_2$</span> on the (hyper)plane perpendicular to <span class="math-container">$q_1$</span>, then we're dotting <span class="math-container">$q$</span> with that to decide which side of the circle we're on.</p>
<p>Note that the point where <span class="math-container">$t$</span> swaps from negative to positive will be the point opposite <span class="math-container">$q_1$</span> on the circle—not a point centered across from <span class="math-container">$q_1$</span> and <span class="math-container">$q_2$</span> as your diagram has it.</p>
<p>(Also, it's worth noting that this boils down to essentially the same thing as the logarithm solution in the other answer. Their factor <span class="math-container">$\check p/\|\check p\|$</span> corresponds to our sign factor: all the quaternions in the problem are in the same plane and so will have <span class="math-container">$\check p$</span> along the same axis; the only thing that can differ between the normalized <span class="math-container">$\check p$</span>s is the sign.)</p>

CC BY-SA 4.0
2020-09-05T00:38:34.707Z
2020-09-05T00:44:00.940Z
2020-09-05T00:44:00.940Z
Nathan Reed
<p>I am looking for a way to calculate the value of <code>t</code> of quaternion SLERP by having three quaternions.</p>
<p><strong>Forward direction:</strong></p>
<pre><code>Eigen::Quaternionf q = q1.slerp(t, q2);
</code></pre>
<p>where <code>q</code>, <code>q1</code> and <code>q2</code> are quaternions from the Eigen library. And <code>t</code> is a float which interpolates between <code>q1</code> and <code>q2</code>.</p>
<p>I am interested in the backward calculation, where <code>q</code>, <code>q1</code> and <code>q2</code> is given and the output is t.</p>
<p>To make it easier all quaternions are normalized and also lying on the same plane which crosses the origin. See the illustration below:</p>
<p><a href="https://i.sstatic.net/qwYmt.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/qwYmt.png" alt="enter image description here" /></a></p>
<p><code>q</code>does <strong>NOT</strong> need to be within <code>q1</code> and <code>q2</code>.</p>
<p>I found this link <a href="https://math.stackexchange.com/questions/2346982/slerp-inverse-given-3-quaternions-find-t">here</a> but I don't know how to use that.</p>

CC BY-SA 4.0
2020-09-02T16:54:51.410Z
"*I found this link here but I don't know how to use that.*" What do you mean by that? The answer there seems to give a pretty clear equation. What's the difficulty in using it?
Nicol Bolas
CC BY-SA 4.0
2020-09-02T23:42:29.480Z
@Thomas is this related to your other question? If the quaternions here are all actually 3D vectors, there's a simpler way to solve this using cross products. If it needs to work for full 4D quaternions, it's slightly more involved.
Nathan Reed
CC BY-SA 4.0
2020-09-03T06:25:46.580Z
@NathanReed yes it is related to the other question. I think when solving this problem, I am aware of solving the other question too. It doesn't matter if q, q1 and q2 are quaternions or vectors. The only important thing is, that the result t is in both cases equal.
Thomas
CC BY-SA 4.0
2020-09-03T06:33:03.620Z
@NicolBolas I can't see t as a result in the posted link. also Eigen seems to not have an operator to calculate the log of quaternions. To calculate the logarithm on quaternions is not really the problem, I'll find a way of doing that.
Thomas
CC BY-SA 4.0
2020-09-03T13:27:15.153Z
@Thomas: "*I can't see t as a result in the posted link.*" It's the first sentence of the third paragraph. And even if he didn't spell it out, all you need to do is apply basic rules of algebra to the second equation to solve for `t`.
Nicol Bolas
<eigen>
Compute the interpolation factor between the SLERP of 2 quaternions, given those quaternions and the SLERP result
Thomas
# Compute the interpolation factor between the SLERP of 2 quaternions, given those quaternions and the SLERP result

(Asked by: username_0 on 2020-09-02)

I am looking for a way to calculate the value of `t` of quaternion SLERP by having three quaternions.

**Forward direction:**

```
Eigen::Quaternionf q = q1.slerp(t, q2);

```

where `q`, `q1` and `q2` are quaternions from the Eigen library. And `t` is a float which interpolates between `q1` and `q2`.

I am interested in the backward calculation, where `q`, `q1` and `q2` is given and the output is t.

To make it easier all quaternions are normalized and also lying on the same plane which crosses the origin. See the illustration below:

[![enter image description here](https://i.sstatic.net/qwYmt.png)](https://i.sstatic.net/qwYmt.png)

`q`does **NOT** need to be within `q1` and `q2`.

I found this link [here](https://math.stackexchange.com/questions/2346982/slerp-inverse-given-3-quaternions-find-t) but I don't know how to use that.

**Tags:** eigen

> **Comments:**
> **username_1**: "*I found this link here but I don't know how to use that.*" What do you mean by that? The answer there seems to give a pretty clear equation. What's the difficulty in using it?  
> **username_2**: @username_0 is this related to your other question? If the quaternions here are all actually 3D vectors, there's a simpler way to solve this using cross products. If it needs to work for full 4D quaternions, it's slightly more involved.  
> **username_0**: @username_2 yes it is related to the other question. I think when solving this problem, I am aware of solving the other question too. It doesn't matter if q, q1 and q2 are quaternions or vectors. The only important thing is, that the result t is in both cases equal.  
> **username_0**: @username_1 I can't see t as a result in the posted link. also Eigen seems to not have an operator to calculate the log of quaternions. To calculate the logarithm on quaternions is not really the problem, I'll find a way of doing that.  
> **username_1**: @username_0: "*I can't see t as a result in the posted link.*" It's the first sentence of the third paragraph. And even if he didn't spell it out, all you need to do is apply basic rules of algebra to the second equation to solve for `t`.  

---

## 1 answers

### Answer by username_2 on 2020-09-05. Score: 2 | Accepted answer

We can do this mostly by measuring angles between the quaternions, with a bit of sign flip to get the orientations right. Angles between normalized quaternions are given by the arccos of their dot product (as a 4D vector).
$$
t = \frac{\arccos(q\_1 \cdot q)}{\arccos(q\_1 \cdot q\_2)} \ \text{sign}\bigl(q \cdot [q\_2 - (q\_1 \cdot q\_2) q\_1]\bigr)
$$
The first factor is measuring the angle from $q\_1$ to $q$ as a fraction of the angle from $q\_1$ to $q\_2$. This ensures that $t$ will be 0 at $q\_1$ and 1 at $q\_2$. The other factor ensures that $t$ increases as you move from $q\_1$ toward $q\_2$ and goes negative on the other side. The quantity in brackets is the projection of $q\_2$ on the (hyper)plane perpendicular to $q\_1$, then we're dotting $q$ with that to decide which side of the circle we're on.

Note that the point where $t$ swaps from negative to positive will be the point opposite $q\_1$ on the circle—not a point centered across from $q\_1$ and $q\_2$ as your diagram has it.

(Also, it's worth noting that this boils down to essentially the same thing as the logarithm solution in the other answer. Their factor $\check p/\|\check p\|$ corresponds to our sign factor: all the quaternions in the problem are in the same plane and so will have $\check p$ along the same axis; the only thing that can differ between the normalized $\check p$s is the sign.)


---

