# Convert Quaternion to Euler angle extrinsically

106 views (last 30 days)
Frank Martin on 24 Jan 2024
Edited: Bruno Luong on 26 Jan 2024
Hello,
I need to convert my results which are stored as quaternions into euler representation. The quat2eul and quat2angle functions seem the same and both will convert quaternions to euler angles. However, it is stated that they use intrinsic calculation (AKA rotation is done around Z axis, then Y' axis, then X'' axis). I need to convert extrinsically. I do not want each rotation to be based on the newly rotated axis. Is there any function in matlab to do this?
James Tursa on 25 Jan 2024
Please give an example of input and desired output. Maybe all you need to do is reorder.

Matt J on 25 Jan 2024
Edited: Matt J on 25 Jan 2024
Intrinsic euler angles can be converted to extrinsic euler angles just be reversing their order.
EDIT: In other words, a Z-Y'-X'' intrinsic rotation by psi, theta, and phi is the same as an X-Y-Z extrinsic rotation by phi, theta, and psi.
Paul on 26 Jan 2024
In this line:
rotmat(quaternion([phi theta psi], 'eulerd', 'XYZ', 'point'),'point')
we've specified one of the attributes as a point rotation. The usage of rotmat in the doc page ""Rotations, Orientation, and Quaternions" all show (at least as I recall) the output of rotmat is intended for right-multiplication by a vector. Given those two attributes are specified, are the Euler angles in that line of code intrinsic or extrinsic?
Bruno Luong on 26 Jan 2024
Edited: Bruno Luong on 26 Jan 2024
q = quaternion([phi theta psi], 'eulerd', 'XYZ', 'point') returns quaternion for extrinsic point rotation (usage right multiplication by column vector of coordinates or q*v*conj(q))
phi = rand()*360;
theta = rand()*360;
psi = rand()*360;
q = quaternion([phi theta psi], 'eulerd', 'XYZ', 'point')
q = quaternion
-0.52669 + 0.56213i - 0.22555j - 0.59644k
% q = conj(quaternion(-[phi theta psi], 'eulerd', 'XYZ', 'frame'))
R = rotmat(q,'point')
R = 3×3
0.1868 -0.8819 -0.4330 0.3747 -0.3435 0.8612 -0.9081 -0.3231 0.2663
% Check euler decomposition of R
R = Rz*Ry*Rx; R = R(1:3,1:3)
R = 3×3
0.1868 -0.8819 -0.4330 0.3747 -0.3435 0.8612 -0.9081 -0.3231 0.2663
% Verify point rotation using R and q
P = randn(3,1); % random point
RP1 = R*P
RP1 = 3×1
-0.2398 1.4719 -0.3082
v = quaternion(0,P(1),P(2),P(3));
tmp = q*v*conj(q);
[a,b,c,d] = parts(tmp);
RP2 = [b; c; d]
RP2 = 3×1
-0.2398 1.4719 -0.3082

Bruno Luong on 26 Jan 2024
Edited: Bruno Luong on 26 Jan 2024
Here is the answer by MATLAB code: extrinsic angles is flip intrinsic angles
% Generate random unit quaternion
q = quaternion(randn, randn, randn, randn);
q = q ./ norm(q)
q = quaternion
-0.50199 - 0.41335i + 0.75491j + 0.085223k
Order = 'ZYX'
Order = 'ZYX'
Ei = quat2eul(q, Order) % Intrinsic Euler angle
Ei = 1×3
-1.7849 -0.7580 2.2956
% Extrinsic Euler angle, simply flip order then flip resulting angles
Ee = fliplr(quat2eul(q, fliplr(Order)))
Ee = 1×3
1.8498 -0.9762 2.6051
% The rotation matrix corresponds to q
R = rotmat(q, 'frame')
R = 3×3
-0.1543 -0.7096 0.6875 -0.5385 0.6438 0.5437 -0.8284 -0.2863 -0.4815
% Check intrinsic frame decomposition, revers angle sign because we deal
% with "frame" rotation type, basic rotation compatible with specified
% Order 'ZYX'
Riz = makehgtform('zrotate', -Ei(1));
Riy = makehgtform('yrotate', -Ei(2));
Rix = makehgtform('xrotate', -Ei(3));
Ri = Rix*Riy*Riz; Ri = Ri(1:3,1:3) % it must match R
Ri = 3×3
-0.1543 -0.7096 0.6875 -0.5385 0.6438 0.5437 -0.8284 -0.2863 -0.4815
norm(Ri-R) % or this must be very small
ans = 4.8438e-16
% Check extrinsic frame decomposition
Rez = makehgtform('zrotate', -Ee(1));
Rey = makehgtform('yrotate', -Ee(2));
Rex = makehgtform('xrotate', -Ee(3));
Re = Rez*Rey*Rex; Re = Re(1:3,1:3) % it must match R
Re = 3×3
-0.1543 -0.7096 0.6875 -0.5385 0.6438 0.5437 -0.8284 -0.2863 -0.4815
norm(Re-R) % or this must be very small
ans = 6.5414e-16

Paul on 25 Jan 2024
Hi Frank,
According to a comment in this answer, there appears to be no function in any toolbox that works with extrinsic Euler angles.
It's possible that new functionality has been added in the intervening time or that the commenter was unaware of such functionality at that time.
Also, keep in mind that participants with a Staff flair are not providing official TMW responses here on Answers.

### Categories

Find more on 6DOF in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!