Quaternion to Euler angle converter?

I would like to write a conversion function from (unnormalized) quaternions to Euler angles representing a 3D rotation (from accelerometer data). Does anyone have such a routine already written and is willing to share it? I know I am being lazy but I am new to quaternions and my head is already spinning (pun intended).

Thank you! I was looking for the inverse of your function, to convert quaternions back to angles, but I see that it's not that difficult to write, and the explanations on your post were very helpful.

It is not clear how you want to actually use or calculate the Quat-->Euler transformation. There is a method which avoids all calculation; simply use Igor's built in Gizmo operations. If you setup a dummy Gizmo, then (as an example)

ModifyGizmo setQuaternion={-0.234854,  0.326305,  0.00631929,  0.915585}
GetGizmo curRotation;print GizmoEulerA,GizmoEulerB,GizmoEulerC
  32.6847  -36.4821  -11.8307

From this example, you should be able to write a simple wrapper function for the Quaternion input, that returns the global Euler angle outputs.

First note that I do not recommend using Euler angles.  There is a fair amount of literature that explains why using Euler angles is usually not a good idea.

However, if you need to convert from a quaternion to Euler angles and you have decided which formats you are using I'd get there in two steps:

1.  Convert your quaternion to a rotation matrix.  This is described e.g., here: https://automaticaddison.com/how-to-convert-a-quaternion-to-a-rotation-…

2.  Convert the rotation matrix to Euler angles.  This is described e.g., here:

http://eecs.qmul.ac.uk/~gslabaugh/publications/euler.pdf

Also, check out MatrixOP functions for quaternions: quatToEuler(qIn,mode) and quatToMatrix(qIn).

 

Thank you both to s.r.chinn and Igor.

s.r.chinn: I was not familiar with the capabilities of gizmo that you pointed out. Those are exactly the operations I was looking for. Thank you!

Igor: I think I am aware of the reasons not to use Euler angles in general. My purpose is to extract quaternion data into a form that I can use to create a plot. The links you provided will help me better understand quaternions and decide whether my intuition about how to visualize the rotations they represent is incorrect. Thanks!