Euler angles to rotation matrix manual transformation for iOS devices
<h3>This is a small background and introduction to the problem:</h3>
<p>I have some functionality in my motion- and location-based iOS app, which needs a rotation matrix as an input. Some graphical output is dependent on this matrix. With every movement of the device, graphical output is changed. This is a part of the code which makes that:</p>
<pre><code>[motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical
                                                   toQueue:motionQueue
                                               withHandler:
 ^(CMDeviceMotion* motion, NSError* error){
  //get and process matrix data
 }
</code></pre>
<p>In this structure only 4 frames are available:</p>
<p>XArbitraryZVertical</p>
<p>XArbitraryCorrectedZVertical</p>
<p>XMagneticNorthZVertical</p>
<p>XTrueNorthZVertical</p>
<p>I need to have another reference, f.e. gyroscope value instead of North and these frames can not offer me exactly what I want.</p>
<p>In order to reach my goal, I use next structure:</p>
<pre><code>[motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryCorrectedZVertical
                                                   toQueue:motionQueue
                                               withHandler:
 ^(CMDeviceMotion* motion, NSError* error){
  //get Euler angles and transform it to rotation matrix
 }
</code></pre>
<p>You may ask me, why I do not use built in rotation matrix? The answer is simple. I need to make some kind of own reference frame and I can make this via putting inside modified values of angles.</p>
<h3>The problem:</h3>
<p>In order to get rotation matrix from Euler angles we need to make matrix for each angle and after that multiply them. For 3D case we will have matrix for each axis (3 of them). After that we multiply matrixes. The problem is that the output is dependent on the order of multiplication. X<em>Y</em>Z is not equal to Z<em>Y</em>X. <a href="https://en.wikipedia.org/wiki/Euler_angles#Geometric_algebra" rel="nofollow noreferrer">Wikipedia</a> tells me, that there are 12 variants and I do not know which one is the right one for iOS implementation. I need to know in which order I need to multiply them. In addition, I need to know which angles represents X, Y, Z. For example, X - roll, Y - pitch, Z - yaw.</p>
<p>Actually, this problem was solved by Apple years ago, but I do not have access to .m-files and I do not know which order of multiplication is the right one for iOS device.</p>
<p>Similar question was published <a href="https://stackoverflow.com/questions/10010579/explanation-of-ios-rotation-matrix">here</a>, but order from <a href="http://planning.cs.uiuc.edu/node102.html" rel="nofollow noreferrer">that math example</a> in the solution does not work for me.</p>