Skip to content

Instantly share code, notes, and snippets.

@SIRHAMY
Created March 17, 2016 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SIRHAMY/9767ed75bddf0b87b929 to your computer and use it in GitHub Desktop.
Save SIRHAMY/9767ed75bddf0b87b929 to your computer and use it in GitHub Desktop.
Example of quaternion on quaternion multiplication using the C++ Eigen library.
Eigen::Quaterniond MyWorld::quatMult(Eigen::Quaterniond q1, Eigen::Quaterniond q2) {
Eigen::Quaterniond resultQ;
resultQ.setIdentity();
resultQ.w() = q1.w() * q2.w() - q1.vec().dot(q2.vec());
resultQ.vec() = q1.w() * q2.vec() + q2.w() * q1.vec() + q1.vec().cross(q2.vec());
return resultQ;
}
@cwreynolds
Copy link

Are you sure? A coworker added some code that is nearly identical to yours. (Including the initialization of resultQ to identity. Which is probably redundant since you overwrite all of its state.) On your blog you say “Unfortunately, it looks like the standard * operator performs normal multiplication, not the special quaternion multiplication required by an actual quaternion.” I am not sure what that means, but when I tried an example, they appear to compute the same value:

Eigen::Quaterniond a(1, 2, 3, 4);
Eigen::Quaterniond b(5, 6, 7, 8);
a.normalize();
b.normalize();
Eigen::Quaterniond c = a * b;
Eigen::Quaterniond d = quatMult(a, b);

produces:

a: (w=0.182574, x=0.365148, y=0.547723, z=0.730297)
b: (w=0.379049, x=0.454859, y=0.530669, z=0.606478)
c: (w=-0.830455, x=0.166091, y=0.415227, z=0.332182)
d: (w=-0.830455, x=0.166091, y=0.415227, z=0.332182)

Not a rigorous proof, but at least brings into question your original claim about Eigen’s operator* for quaternions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment