title: Quaternion is flipping sign for very similar rotations?
body: <p>Consider the following minimal working example:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;math.h&gt;
  #include &lt;eigen3/Eigen/Dense&gt;
  
  int main() {
    // Set the rotation matrices that give an example of the problem
    Eigen::Matrix3d rotation_matrix_1, rotation_matrix_2;
    rotation_matrix_1 &lt;&lt; 0.15240781108708346, -0.98618841818279246, -0.064840288106743013,
                         -0.98826031445019891, -0.1527775600229907, 0.00075368177315370682,
                         -0.0106494132438156, 0.063964216524108775, -0.99789536976680049;
    rotation_matrix_2 &lt;&lt; -0.12448670851248633, -0.98805453458380521, -0.090836645094957508,
                         -0.99167686914182451, 0.12086367053038971, 0.044372968742129482,
                         -0.03286406263376359, 0.095604444636749664, -0.99487674792051639;
  
    // Convert to Euler angles
    Eigen::Vector3d euler_angles_1 = rotation_matrix_1.eulerAngles(2, 1, 0)*180.0f/M_PI;
    Eigen::Vector3d euler_angles_2 = rotation_matrix_2.eulerAngles(2, 1, 0)*180.0f/M_PI;
  
    // Convert to quaternion
    Eigen::Quaternion&lt;double&gt; quaternion_1(rotation_matrix_1);
    Eigen::Quaternion&lt;double&gt; quaternion_2(rotation_matrix_2);
  
    // Print out results
    std::cout &lt;&lt; "Euler angles 1:\nyaw = " &lt;&lt; euler_angles_1[0] &lt;&lt; "\npitch = " &lt;&lt; euler_angles_1[1] &lt;&lt; "\nroll = " &lt;&lt; euler_angles_1[2] &lt;&lt; std::endl;
    std::cout &lt;&lt; "Quaternion 1:\nw = " &lt;&lt; quaternion_1.w() &lt;&lt; "\nx = " &lt;&lt; quaternion_1.x() &lt;&lt; "\ny = " &lt;&lt; quaternion_1.y() &lt;&lt; "\nz = " &lt;&lt; quaternion_1.z() &lt;&lt; std::endl;
    std::cout &lt;&lt; std::endl;
    std::cout &lt;&lt; "Euler angles 2:\nyaw = " &lt;&lt; euler_angles_2[0] &lt;&lt; "\npitch = " &lt;&lt; euler_angles_2[1] &lt;&lt; "\nroll = " &lt;&lt; euler_angles_2[2] &lt;&lt; std::endl;
    std::cout &lt;&lt; "Quaternion 2:\nw = " &lt;&lt; quaternion_2.w() &lt;&lt; "\nx = " &lt;&lt; quaternion_2.x() &lt;&lt; "\ny = " &lt;&lt; quaternion_2.y() &lt;&lt; "\nz = " &lt;&lt; quaternion_2.z() &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>Whose output is:</p>
  
  <pre><code>Euler angles 1:
  yaw = 98.767
  pitch = 179.39
  roll = -3.66759
  Quaternion 1:
  w = 0.020826
  x = 0.758795
  y = -0.650521
  z = -0.0248716
  
  Euler angles 2:
  yaw = 82.845
  pitch = 178.117
  roll = -5.48908
  Quaternion 2:
  w = -0.0193663
  x = -0.661348
  y = 0.748369
  z = 0.0467608
  </code></pre>
  
  <p>Both rotations are nearly identical (as given by the Euler angles). The expected behavior is that <code>quaternion_2</code> will have values with same sign as <code>quaternion_1</code>, i.e. for the output to be:</p>
  
  <pre><code>Quaternion 2:
  w = 0.0193663
  x = 0.661348
  y = -0.748369
  z = -0.0467608
  </code></pre>
  
  <p>However, Eigen appears to "flip" the quaternion. I am aware that q and -q represent the same rotation - however, it is visually not appealing, and frankly annoying, that the quaternion would flip sign in each of its values. How can this be rectified for the general case (i.e. that the quaternion always preserves its "handedness", rather than flipping sign for certain rotations)?</p>