Essay record

C++: Implement Quaternion Multiplication with Eigen Library

Record
Essay / / 1 min read / 126 words
Tags
Unfiled

DISCLOSURE: Some links may be affiliate links. Details

**Problem: **I’m using Eigen::Quaterniond, the built in Quaternion structure for the Eigen library. Unfortunately, it looks like the standard * operator performs normal multiplication, not the special quaternion multiplication required by an actual quaternion. How can I implement the special function to multiply a quaternion by a quaternion?

**Solution: **First, notice that your quaternion is composed of two parts – a scalar and a vector. With Eigen::Quaterniond, you can access these parts with myQuaternion.w() and myQuaternion.vec() respectively.

According to Wikipedia, the formula for multiplying a quaternion by a quaternion is as follows:

(r_1,\ \vec{v}_1) (r_2,\ \vec{v}_2) = (r_1 r_2 - \vec{v}_1\cdot\vec{v}_2, r_1\vec{v}_2+r_2\vec{v}_1 + \vec{v}_1\times\vec{v}_2)

So, to implement this in C++, you might write something like this:

 

[https://gist.github.com/SIRHAMY/9767ed75bddf0b87b929]

Built with CloudSeed Rust