C++: Implement Quaternion Multiplication with Eigen Library

Date: 2016-04-08 |

**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_1) (r_2,\ \vec_2) = (r_1 r_2 - \vec_1\cdot\vec_2, r_1\vec_2+r_2\vec_1 + \vec_1\times\vec_2)

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

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

Want more like this?

The best / easiest way to support my work is by subscribing for future updates and sharing with your network.