Search code examples
vectoreigenquaternions

Eigen, rotate a Vector3d with a quaternion, from a given point?


I have two 3d points, as Eigen::Vector3d.

I need to rotate the second point by a quaternion, around the first point'

I usually use this to rotate a Vector:

Eigen::Vector3d point; //point to rotate
Eigen::Quaterniond quat, p2; //quat to rotate by, temp

p2.w() = 0;
p2.vec() = point;
Eigen::Quaterniond rotatedP1 = quat * p2 * quat.inverse();
Eigen::Vector3d rotatedPoint = rotatedP1.vec();

This works, but it rotates the point around zero.

How can I rotate the point around another point? As if the first point is rotating, and the second point is 'parented' to it.

In the image below, I am getting a rotation around the green axis, and i want it around the red axis, by passing point2 into the function somehow.

enter image description here


Solution

  • To rotate a vector point around the origin by quat, you just write quat * point (because the * operator is overloaded accordingly).

    To rotate around a different point, you need to shift the point then rotate and shift it back.

     quat * (point - point2) + point2
    

    If quat and point2 are known before you can also calculate

     quat * point + (point2 - quat * point2)
    

    where the part in parentheses can be precalculated.