title: Conversion between Euler <=> Quaternion like in Unity3d engine
body: <p>I've used two examples (from this site too), but results are not the same as those that said Unity.</p>
  
  <p>Quaternion.Euler and .eulerAngles are Unity functions. FromQ doesn't perform singularity check, FromQ2 does.</p>
  
  <p>Results:</p>
  
  <pre><code>eulers = (100,55,-11):
  Quaternion.Euler(eulers) == (0.6, 0.4, -0.4, 0.5)
  ToQ(eulers)); == (0.5, -0.4, 0.2, 0.7) // 0.5, -0.4 right but in wrong order
  
  FromQ(ToQ(eulers)) == (55.0, 100.0, -11.0)
  FromQ2(ToQ(eulers)) == (-55.5, -6.3, 71.0) // something right
  
  Quaternion.Euler(eulers).eulerAngles == (80.0, 235.0, 169.0)
  FromQ2(Quaternion.Euler(eulers)) == (65.8, 1.9, 99.8)
  ToQ(eulers).eulerAngles == (70.0, 286.9, 341.4)
  FromQ(Quaternion.Euler(eulers)) == (-65.8, 76.0, 4.6)
  
  It must be:
  FromQ() = FromQ2() = .eulerAngles,
  ToQ() = Quaternion.Euler()
  </code></pre>
  
  <p>The code is here: <a href="http://pastebin.ru/eAlTHdYf" rel="nofollow">http://pastebin.ru/eAlTHdYf</a></p>
  
  <p>Can anyone correct this code? I need code that will return the values ​​that are identical to the values that Unity functions returns.</p>
  
  <h2><strong>UPDATE</strong></h2>
  
  <p>Here is fixed code: <a href="http://pastebin.com/riRLRvch" rel="nofollow">http://pastebin.com/riRLRvch</a>.
  Both functions (FromQ and ToQ) work well. But I have a problem with a singularity. It can't detect the singularity properly.</p>
  
  <pre><code>For example (90, 0, 50) in quaternion is (0.6, -0.3, 0.3, 0.6).
  test = x * y + z * w = 0 (must be close to 0.5 or -0.5)
  </code></pre>
  
  <p>FromQ can't calculate correct result so we have the singularity here. The same for (90, 50, 0) - (0.6, 0.3, -0.3, 0.6).</p>
  
  <p>I see only one solution - calculate "test" as xw-yz. But I'm not sure this is right.</p>
  
  <p>How to fix it?</p>