-
Notifications
You must be signed in to change notification settings - Fork 5
/
eul2quat.m
55 lines (48 loc) · 1.84 KB
/
eul2quat.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function q = eul2quat( eul, varargin )
%EUL2QUAT Convert Euler angles to quaternion
% Q = EUL2QUAT(EUL) converts a given set of 3D Euler angles, EUL, into
% the corresponding unit quaternion, Q. EUL is an N-by-3 matrix of Euler
% rotation angles.
% The output, Q, is an N-by-4 matrix containing N quaternions. Each
% quaternion is of the form q = [w x y z], with a scalar number as
% the first value.
%
% Q = EUL2QUAT(EUL, SEQ) converts a set of 3D Euler angles into a unit
% quaternion. The Euler angles are specified by the axis rotation
% sequence, SEQ.
%
% The default rotation sequence is 'ZYX', where the order of rotation
% angles is Z Axis Rotation, Y Axis Rotation, and X Axis Rotation.
%
% The following rotation sequences, SEQ, are supported: 'ZYX' and 'ZYZ'.
%
% Example:
% % Calculate the quaternion for a set of Euler angles
% % By default, the ZYX axis order will be used.
% angles = [0 pi/2 0];
% q = eul2quat(angles)
%
% % Calculate the quaternion based on a ZYZ rotation
% qzyz = eul2quat(angles, 'ZYZ')
%
% See also quat2eul
seq = 'ZYX';
% Compute sines and cosines of half angles
c = cos(eul/2);
s = sin(eul/2);
% The parsed sequence will be in all upper-case letters and validated
switch seq
case 'ZYX'
% Construct quaternion
q = [c(:,1).*c(:,2).*c(:,3)+s(:,1).*s(:,2).*s(:,3), ...
c(:,1).*c(:,2).*s(:,3)-s(:,1).*s(:,2).*c(:,3), ...
c(:,1).*s(:,2).*c(:,3)+s(:,1).*c(:,2).*s(:,3), ...
s(:,1).*c(:,2).*c(:,3)-c(:,1).*s(:,2).*s(:,3)];
case 'ZYZ'
% Construct quaternion
q = [c(:,1).*c(:,2).*c(:,3)-s(:,1).*c(:,2).*s(:,3), ...
c(:,1).*s(:,2).*s(:,3)-s(:,1).*s(:,2).*c(:,3), ...
c(:,1).*s(:,2).*c(:,3)+s(:,1).*s(:,2).*s(:,3), ...
s(:,1).*c(:,2).*c(:,3)+c(:,1).*c(:,2).*s(:,3)];
end
end