Search code examples
c++eigenquaternions

Quaternion Components to Float (Eigen)


I have an Eigen::Quaternion and I want to convert it's components x, y, z, w to double.

A simple assignment like this is not working because the components are of type Scalar. This is what I tried:

Eigen::Quaternion<float> q;
q = Eigen::AngleAxis<float>(2, Eigen::Vector3f(0,0,1));
float x = q.x;

The documentation of the Scalar type is weak. Does anyone know how to do this?


Solution

  • Scalar is not a datatype but a template argument of Eigen::Quaternion.

    So if you declare Eigen::Quaternion<float> q this means for this quaternion, Scalar is set to float.

    What you were missing in your example code is that to extract the x component you must call a method named x().

    The following example shows how to do it:

    #include <iostream>
    #include <Eigen/Geometry> 
    
    int main()
    {
      Eigen::Quaternion<float> q;
      q = Eigen::AngleAxis<float>(2, Eigen::Vector3f(0,0,1));
      float x = q.x();
    
      std::cout << x << std::endl;
    }