GLM_GTC_quaternion

Header: <glm/gtc/quaternion.hpp>

Notations in this page

A quaternion \[q=w+ix+jy+kz\in\mathbf{H}\] may be written as \[q=(w;x,y,z)\] or by the scalar part and the vector part \[q=(w;v)\quad\text{where }v=(x,y,z)\in\mathbf{R}^3\]

For \(q=w+ix+jy+kz\), \[\overline{q}:=w-ix-jy-kz\] \[\left\lvert q\right\rvert:=\sqrt{w^2+x^2+y^2+z^2}\] \[\operatorname{Re}q:=w,\quad \operatorname{Im}q:=(x,y,z)=ix+jy+kz\]

Multiplication is induced by the following relations: \[\begin{gathered} i^2=j^2=k^2=ijk=-1, \\ ij=k,\;jk=i,\;ki=j, \\ ji=-k,\;kj=-i,\;ik=-j. \end{gathered}\]

A quaternion whose real part is zero can be viewed as a three-dimensional vector: \[ \begin{array}{ccc} \mathbf{R}^3&\subset&\mathbf{H}\\ (x,y,z)&\leftrightarrow&(0;x,y,z) \end{array} \]

In this page, tquat stands for tquat<T, Q>, tmat3x3 for mat<3, 3, T, Q>, tmat4x4 for mat<4, 4, T, Q>, tvec3 for vec<3, T, Q>, and tvec4 for vec<4, T, Q>.

Types

Arithmetic

Rotation

Euler angles

From Euler angles to quaternion

Let \(\mathsf{eulerAngles}=(\alpha,\beta,\gamma)\) (\(\alpha\): pitch, \(\beta\): yaw, \(\gamma\): roll). \[ \begin{aligned} q=&\Bigl(\cos\frac{\gamma}{2}+k\sin\frac{\gamma}{2}\Bigr) \Bigl(\cos\frac{\beta}{2}+j\sin\frac{\beta}{2}\Bigr) \Bigl(\cos\frac{\alpha}{2}+i\sin\frac{\alpha}{2}\Bigr) \\ =&\Bigl(\cos\frac{\alpha}{2}\cos\frac{\beta}{2}\cos\frac{\gamma}{2}+\sin\frac{\alpha}{2}\sin\frac{\beta}{2}\sin\frac{\gamma}{2}\Bigr) \\ &+i\Bigl(\sin\frac{\alpha}{2}\cos\frac{\beta}{2}\cos\frac{\gamma}{2}-\cos\frac{\alpha}{2}\sin\frac{\beta}{2}\sin\frac{\gamma}{2}\Bigr) \\ &+j\Bigl(\cos\frac{\alpha}{2}\sin\frac{\beta}{2}\cos\frac{\gamma}{2}+\sin\frac{\alpha}{2}\cos\frac{\beta}{2}\sin\frac{\gamma}{2}\Bigr) \\ &+k\Bigl(\cos\frac{\alpha}{2}\cos\frac{\beta}{2}\sin\frac{\gamma}{2}-\sin\frac{\alpha}{2}\sin\frac{\beta}{2}\cos\frac{\gamma}{2}\Bigr) \end{aligned} \]

From quaternion to Euler angles

Let \[q=(w;x,y,z)=\Bigl(\cos\frac{\gamma}{2}+k\sin\frac{\gamma}{2}\Bigr) \Bigl(\cos\frac{\beta}{2}+j\sin\frac{\beta}{2}\Bigr) \Bigl(\cos\frac{\alpha}{2}+i\sin\frac{\alpha}{2}\Bigr).\] By comparing \[qiq^{-1}=i\cos\beta\cos\gamma+j\cos\beta\sin\gamma-k\sin\beta\] and \[qi\overline{q}=i(w^2+x^2-y^2-z^2)+2j(xy+zw)+2k(xz-wy),\] we obtain \[\sin\beta=-2(xz-wy),\quad\tan\gamma=\frac{2(xy+zw)}{w^2+x^2-y^2-z^2}\] and the yaw \(\beta\in[-\pi,\pi]\) and the roll \(\gamma\in[-\pi,\pi]\) can be computed by \[\beta=\arcsin(-2(xz-wy)),\quad\gamma=\arctan\frac{2(xy+zw)}{w^2+x^2-y^2-z^2}.\]

Similarly, by comparing \[q^{-1}kq=-i\sin\beta+j\sin\alpha\cos\beta+k\cos\alpha\cos\beta\] and \[\overline{q}kq=2i(xz-yw)+2j(yz+wx)+k(w^2-x^2-y^2+z^2),\] we obtain \[\tan\alpha=\frac{2(yz+wx)}{w^2-x^2-y^2+z^2}\] and the pitch \(\alpha\in[-\pi,\pi]\) can be computed by \[\alpha=\arctan\frac{2(yz+wx)}{w^2-x^2-y^2+z^2}.\]

Interpolation

Element-wise comparison