KieranWynn / pyquaternion

A fully featured, pythonic library for representing and using quaternions
http://kieranwynn.github.io/pyquaternion/
MIT License
343 stars 73 forks source link

Enhancement for converting to yaw-pitch-roll #52

Open FiorixF1 opened 4 years ago

FiorixF1 commented 4 years ago

Hi, I saw that this library offers the function yaw_pitch_roll to get the Euler angles from a quaternion. However I see they are calculated based on a fixed application of the rotations (I guess XYZ). When using this library, I had the necessity to calculate these angles with respect to a different rotation order (YXZ). Looking deeply on the internet, I found this wonderful paper where a guy explains how to convert from a quaternion to Euler angles for EVERY possible rotation order. https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/Quaternions.pdf

To make it short, a quaternion Q is represented by four numbers p0, p1, p2, p3 where p0 is always Q.w while p1, p2, p3 are the components of the quaternion with respect of rotation order (e.g. if the rotation order is XYZ then p1 = Q.x, p2 = Q.y, p3 = Q.z, if the rotation order is ZYX then p1 = Q.z, p2 = Q.y, p3 = Q.x...) plus a variable "e" which equals 1 for right handed systems and -1 for left-handed systems. The three angles alpha, beta, gamma are then calculated as:

and it magically works.

This could be implemented in the library by adding a parameter to yaw_pitch_roll expressing the rotation order (it could be optional for backward compatibility).

Achllle commented 4 years ago

My two cents: more options might mean more confusion: Euler angles are horrible and you don't need them. I'd even vote to remove them.

Omdiwan commented 1 year ago

how do we decide if it's a left or right handed system. Also, is the rotation order specified somewhere.