title: GLM conversion from euler angles to quaternion and back does not hold
body: <p>I am trying to convert the orientation of an OpenVR controller that I have stored as a <code>glm::vec3</code> of Euler angles into a <code>glm::fquat</code> and back, but I get wildly different results and the in-game behavior is just wrong <em>(hard to explain, but the orientation of the object behaves normally for a small range of angles, then flips in weird axes)</em>.</p>
  
  <p>This is my conversion code:</p>
  
  <pre><code>// get `orientation` from OpenVR controller sensor data
  
  const glm::vec3 eulerAnglesInDegrees{orientation[PITCH], orientation[YAW], orientation[ROLL]};
  debugPrint(eulerAnglesInDegrees);
  
  const glm::fquat quaternion{glm::radians(eulerAnglesInDegrees)};
  const glm::vec3 result{glm::degrees(glm::eulerAngles(quaternion))};
  debugPrint(result);
  
  // `result` should represent the same orientation as `eulerAnglesInDegrees`
  </code></pre>
  
  <p>I would expect <code>eulerAnglesInDegrees</code> and <code>result</code> to either be the same or equivalent representations of the same orientation, but that is apparently not the case. These are some example values I get printed out:</p>
  
  <pre><code>39.3851 5.17816 3.29104 
  39.3851 5.17816 3.29104 
  
  32.7636 144.849 44.3845 
  -147.236 35.1512 -135.616 
  
  39.3851 5.17816 3.29104 
  39.3851 5.17816 3.29104 
  
  32.0103 137.415 45.1592 
  -147.99 42.5846 -134.841 
  </code></pre>
  
  <p>As you can see above, for some orientation ranges the conversion is correct, but for others it is completely different.</p>
  
  <p><strong>What am I doing wrong?</strong></p>
  
  <p>I've looked at existing questions and attempted a few things, including trying out <a href="https://stackoverflow.com/a/27497022/598696">every possible rotation order listed here</a>, <a href="https://stackoverflow.com/a/29992778/598696">conjugating the quaternion</a>, and other random things like flipping pitch/yaw/roll. Nothing gave me the expected result.</p>
  
  <p><strong>How can I convert euler angles to quaternions and back, representing the original orientation, using <code>glm</code>?</strong> </p>
  
  <hr>
  
  <p>Some more examples of discrepancies:</p>
  
  <pre><code>original:      4; 175;   26; 
  computed:   -175;   4; -153; 
  difference:  179; 171;  179; 
  
  original:     -6; 173;   32; 
  computed:    173;   6; -147; 
  difference: -179; 167;  179; 
  
  original:      9; 268;  -46; 
  computed:   -170; -88;  133; 
  difference:  179; 356; -179; 
  
  original:    -27; -73;  266; 
  computed:    -27; -73;  -93; 
  difference:    0;   0;  359; 
  
  original:    -33; 111;  205; 
  computed:    146;  68;   25; 
  difference: -179;  43;  180; 
  </code></pre>
  
  <p>I tried to find a pattern to fix the final <code>computed</code> results, but it doesn't seem like there's one easy to identify.</p>
  
  <hr>
  
  <p>GIF + video of the behavior:</p>
  
  <p><a href="https://i.stack.imgur.com/Dpcqw.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Dpcqw.gif" alt="Video excerpt"></a></p>
  
  <ul>
  <li><a href="https://www.youtube.com/watch?v=gSXpWEYKCYU" rel="nofollow noreferrer">Full video on YouTube</a></li>
  </ul>
  
  <hr>
  
  <p>Visual representation of my intuition/current understanding:</p>
  
  <p><a href="https://i.stack.imgur.com/RSDsh.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RSDsh.jpg" alt="Visual diagram"></a></p>
  
  <ul>
  <li>The above picture shows a sphere, and I'm in the center. When I aim the gun towards the green half of the sphere, the orientation is correct. When I aim the gun towards the red half of the sphere, it is incorrect - it seems like every axis is inverted, but I am not 100% sure that is the case.</li>
  </ul>