In my work, I needed to transfer the coordinates of an object from Euler angles to quaternions and back.
During the trial, I had to read several articles on Habré devoted to quaternions and Euler angles, Wikipedia and just manuals and articles from different universities. For convenience, I will provide links to articles from Habr:
Tricky quaternions
Quaternion rotation notes
Quaternions for dummies
Twist-twirl, I want to confuse. Euler and Gimbal lock angles
')
Formulas for converting Euler angles to quaternions and back can be found, but
I will describe briefly the essence of the problem:
- The body in three-dimensional space has 6 degrees of freedom: 3 coordinates and 3 angles of rotation.
- With coordinates everything is fine, for example, if they are (4,5,2), then this means that the body needs to be shifted relative to the origin by +4 units along the X axis, +5 units along the Y axis and +2 units along the axis Z. However, the order of the shift is not important. You can first move along X, then Y, then Z, or in a different sequence. From changing the terms of the sum does not change.
- With turns, everything is much worse. Sometimes it may feel that for them it is just enough to set the angles of rotation around three axes and this will be enough (for example: turn the object 180 degrees around the X axis, then 180 degrees around the Y axis, and then 90 degrees around the Z axis - in what order do not turn - the result will be the same). This trap arises from the fact that it is easiest for us to operate with angles of the type of 90 or 180 degrees, but they are just a very special case. In general, the order of turns matters.
But what about the law saying that the sum does not change from the rearrangement of the places of the components? The fact is that the composition of several rotations does not correspond to the sum of the vectors (as is the case with parallel transfer operations), but to the product. And the product is not just numbers, but special objects - rotation matrices, for example - to which the commutativity of "ordinary" multiplication does not apply. Depending on the order of choice of the axes of rotation and on whether the axes will rotate together with the object or only the object rotates, 24 types of rotation descriptions can be distinguished. Very often, the angles of rotation around the axes are called Euler angles. Sometimes, in some sources, these angles are called Tate-Brian angles or Euler angles, depending on whether all three axes around which rotation is made are different (Tate-Brian angles), or the first and last axes are the same. Also, these angles are called angles of extrinsic rotation - if the axes are fixed or angles of intrinsic rotation - if the axes rotate with the object.
Not to get confused, I will give all types of rotations here:
Tate Bryan, internal:
ZYXr; YZXr; XZYr; ZXYr; YXZr; XYZr.
Euler, internal:
XYXr; XZXr; YZYr; YXYr; ZXZr; ZYZr.
Tate Bryan, external:
ZYXs; YZXs; XZYs; ZXYs; YXZs; XYZs.
Euler, external:
XYXs; XZXs; YZYs; YXYs; ZXZs; ZYZs.
The outer corners are complementary to the inner, read backwards, for example: the outer corners of Euler 10, 20, 30 degrees in the XYXs format is the same as the inner corners of Euler 30, 20, 10 degrees in the XYXr format.
Actually, this has already been said many times. Why write a new article? The fact is that there is not so much information about how to transfer from Euler to Quaternion corners and back. And in most cases only 1 or 2, 3, 6 systems of Euler angles are described. But not all 24. And by analogy, to bring the rest (and not to be mistaken) is not very easy. During the “digging up the truth” I managed to find several online converters from corners to quaternions and by the direction in which their ability to convert increases, one can understand how many more options are left uncovered:
quat.zachbennett.com - one type of angle
energid.com - one type of angle
onlineconversion.com - one type of angle
quaternions.online - three types of angles
andre-gaschler.com - six types of angles
The only place where I could find a description of the transformations for all 24 types of angles is the book “Graphics Gems IV”. The source repository for this book is located here:
Source code for the book Graphics Gems IV . If we talk about the conversion code from Euler angles to quaternions and back, then these sources in the repository are located here: ... / GraphicsGems / gemsiv / euler_angle. But they have one drawback: in order to make the most common function for calculating angles and quaternions, the author has greatly complicated the code. Those. the code is very compact, but poorly suitable for translation into other languages ​​or for optimization for specific cases. Since I really needed to deal with all 24 cases, I had to search this code for a bit and deploy it to a set of simple cases. I also wrote small unit tests and checked that my code works correctly. Since Since these unit tests use code compiled from source files from the Graphics Gems book, I didn’t upload them (unit tests).
I will not give my sources in the text of the article (they are written in the Octave language). I will give only a
link to the repository and comment on its contents:
eul_to_quat.m - analog of the
matlab function eul2quat
quat_to_eul.m - analogue of the
matlab function quat2eul
Both functions in the Octave not. In Matlab, only 6 types of Euler angles on fixed axes are supported. All 24 types are supported in my implementations. In this case, the types with the letter r at the end (for example, XYZr) mean that the axes rotate with the object. Types with an s at the end (for example, XYZs) mean that the axes remain stationary.