How to swap the roll and pitch of a quaternion

179 Views Asked by At

I'm writing code that receives quaternion values that are used to rotate a $3$D model. To display the model with the correct orientation, though, I'll need to swap the roll and pitch (rotation about the $x$ and $y$ axes). Since I can't change the code that produces the input quaternion, is it possible to swap the roll and pitch of a quaternion? I would like to do this without converting the quaternion to Euler angles to avoid issues if pitch is $90$ degrees.

In MATLAB, I converted various Euler angles to quaternions and then swapped the roll and pitch components of the Euler angles and converted that to quaternions. It's not obvious to me how to convert the original quaternion to the swapped version.

2

There are 2 best solutions below

0
On

Let $q=a+bi+cj+dk\in \mathbb H$ be a quaternion and let us denote its imaginary part as a vector in $\mathbb R^3$: $$\left( \begin{matrix} b \\ c \\ d\end{matrix}\right) \in \mathbb R^3.$$

Then, we can apply the rotation matrices:

$${\displaystyle {\begin{alignedat}{1}R_{x}(\theta )&={\begin{pmatrix}1&0&0\\0&\cos \theta &-\sin \theta \\[3pt]0&\sin \theta &\cos \theta \\[3pt]\end{pmatrix},}\\[6pt]R_{y}(\theta )&={\begin{pmatrix}\cos \theta &0&\sin \theta \\[3pt]0&1&0\\[3pt]-\sin \theta &0&\cos \theta \\\end{pmatrix},}\\[6pt]R_{z}(\theta )&={\begin{pmatrix}\cos \theta &-\sin \theta &0\\[3pt]\sin \theta &\cos \theta &0\\[3pt]0&0&1\\\end{pmatrix},}\end{alignedat}}}$$

so that any rotation of angles $\alpha$, $\beta$, $\gamma$ in the $z$, $y$, $x$ directions respectively, can be expressed as the following product, which will give you a new vector:

$$R_{z}(\alpha )\,R_{y}(\beta )\,R_{x}(\gamma )\left( \begin{matrix} b \\ c \\ d\end{matrix}\right)= \left( \begin{matrix} \hat b \\ \hat c \\ \hat d\end{matrix}\right).$$

Then, your rotated quaternion will be $\hat q = a + \hat b i + \hat c j + \hat d k$.

0
On

Edit: As David K points out in the comments, there is an inherent well-definedness issue when pitch is $\pm90^\circ$. This manifests in my method when $I_i^2+I_j^2=0$. I address this case a bit at the end.


Let $q$ be our (normalized) quaternion and let $\alpha,\beta,\gamma$ be yaw, pitch, and roll. In terms of an airplane, I am assuming that $i$ gives the current heading, $j$ is in the direction of the left wing, and $k$ is perpendicular to these pointing out of the top.

The pitch is exactly the angle that the new heading $I = qi\bar q$ makes with the original $ij$-plane, and the yaw is the angle that the projection of $I$ onto this plane makes with the original heading $i$. This means that we can write $$ \cos\beta = \sqrt{I_i^2 + I_j^2},\quad \sin\beta = I_k, $$$$ \cos\alpha = \frac{I_i}{\sqrt{I_i^2+I_j^2}},\quad \sin\alpha = \frac{I_j}{\sqrt{I_i^2+I_j^2}}. $$ Here $I_i$ means the $i$-component of $I$, and likewise for $I_j, I_k$.

Now consider that the yaw, pitch, roll quaternions $Y, P, R$. We have $q = RPY$ and want $q' = RYP$. The yaw and pitch have the form $$ Y = \cos\alpha/2 + k\sin\alpha/2,\quad P = \cos\beta/2 + Yj\bar Y\sin\beta/2. $$ We compute $$ YP = PY + [Y, P] = PY + \sin\frac\alpha2\sin\frac\beta2\,[k, Yj\bar Y] = PY - 2\sin\frac\alpha2\sin\frac\beta2\,Yi\bar Y $$$$ \implies q' = q - 2\sin\frac\alpha2\sin\frac\beta2\,RYi\bar Y = q - 2\sin\frac\alpha2\sin\frac\beta2\,q'\bar Pi\bar Y $$$$ \implies q' = q(1 + 2\sin\frac\alpha2\sin\frac\beta2\,\bar Pi\bar Y)^{-1}. $$ Assuming $Y, P, R$ to be units, so are $q$ and $q'$ and so the last factor on the RHS is a unit as well. Thus $$ q' = qs,\quad s = 1 - 2\sin\frac\alpha2\sin\frac\beta2\,YiP. $$

For the moment write $C_\alpha, C_\beta, S_\alpha, S_\beta$ for the half-angle cosines and sines. It's easy to see that $P$ takes the form $$ P = C_\beta + S_\beta(-i\sin\alpha + j\cos\alpha). $$ So now we compute $$\begin{aligned} YiP &= (C_\alpha + S_\alpha k)i[C_\beta + S_\beta(-i\sin\alpha + j\cos\alpha)] \\ &= (C_\alpha i + S_\alpha j)[C_\beta + S_\beta(-i\sin\alpha + j\cos\alpha)] \\ &= C_\beta(C_\alpha i + S_\alpha j) + S_\beta[C_\alpha\cos\alpha + S_\alpha\sin\alpha]k + S_\beta[C_\alpha\sin\alpha - S_\alpha\cos\alpha] \\ &= C_\beta C_\alpha i + C_\beta S_\alpha j + S_\beta C_\alpha k - S_\beta S_\alpha \end{aligned}$$ and thus $$\begin{aligned} 2S_\alpha S_\beta YiP &= i\frac12\sin\alpha\sin\beta + jS_\alpha^2\sin\beta + kS_\beta^2\sin\alpha - 2S_\beta^2S_\alpha^2 \\ &= \frac12\Bigl[i\sin\alpha\sin\beta + j\left(1 - \cos\alpha\right)\sin\beta + k\left(1 - \cos\beta\right)\sin\alpha - \left(1 - \cos\beta\right)\left(1 - \cos\alpha\right)\Bigr]. \end{aligned}$$ Finally, we write $$ 2s = 3 + I_i - \sqrt{I_i^2 + I_j^2} - \frac{I_i}{\sqrt{I_i^2+I_j^2}} - i\frac{I_jI_k}{\sqrt{I_i^2+I_j^2}} - jI_k\left[1 - \frac{I_i}{\sqrt{I_i^2+I_j^2}}\right] + kI_j\left[1 - \frac{1}{\sqrt{I_i^2+I_j^2}}\right]. $$


When $\beta = \pm90^\circ$ this doesn't work; in fact we can't distinguish between roll and yaw. What we can do in this case is define the turn $\delta$ as the angle between $j$ and $J = qj\bar q$: $$ \cos\delta = J_j,\quad \sin\delta = J_k. $$ Then we have to decide how much of the turn is due to yaw and how much is due to roll. The two most natural cases are

  • All roll: In this case we can take $Y = 1$ whence $q = q'$.

  • All pitch: In this case we can take $R = 1$ and $\alpha = \delta$. Reusing our work from before, $$ q' = qs,\quad 2s = 3 - J_j \mp J_ki \mp (1-J_j)j - J_kk $$ where $\pm$ corresponds to $\beta = \pm90^\circ$, which can be distinguished via $qi\bar q = \pm k$.