EULERANGLESTOROTATION3D Convert 3D Euler angles to 3D rotation matrix. MAT = eulerAnglesToRotation3d(PHI, THETA, PSI) Creates a rotation matrix from the 3 euler angles PHI THETA and PSI, given in degrees, using the 'XYZ' convention (local basis), or the 'ZYX' convention (global basis). The result MAT is a 4-by-4 rotation matrix in homogeneous coordinates. PHI: rotation angle around Z-axis, in degrees, corresponding to the 'Yaw'. PHI is between -180 and +180. THETA: rotation angle around Y-axis, in degrees, corresponding to the 'Pitch'. THETA is between -90 and +90. PSI: rotation angle around X-axis, in degrees, corresponding to the 'Roll'. PSI is between -180 and +180. These angles correspond to the "Yaw-Pitch-Roll" convention, also known as "Tait-Bryan angles". The resulting rotation is equivalent to a rotation around X-axis by an angle PSI, followed by a rotation around the Y-axis by an angle THETA, followed by a rotation around the Z-axis by an angle PHI. That is: ROT = Rz * Ry * Rx; MAT = eulerAnglesToRotation3d(ANGLES) Concatenates all angles in a single 1-by-3 array. ... = eulerAnglesToRotation3d(ANGLES, CONVENTION) CONVENTION specifies the axis rotation sequence. Default is 'ZYX'. Supported conventions are: 'ZYX','ZXY','YXZ','YZX','XYZ','XZY' 'ZYZ','ZXZ','YZY','YXY','XZX','XYX' Example [n e f] = createCube; phi = 20; theta = 30; psi = 10; rot = eulerAnglesToRotation3d(phi, theta, psi); n2 = transformPoint3d(n, rot); drawPolyhedron(n2, f); See also transforms3d, createRotationOx, createRotationOy, createRotationOz rotation3dAxisAndAngle
0001 function mat = eulerAnglesToRotation3d(phi, theta, psi, varargin) 0002 %EULERANGLESTOROTATION3D Convert 3D Euler angles to 3D rotation matrix. 0003 % 0004 % MAT = eulerAnglesToRotation3d(PHI, THETA, PSI) 0005 % Creates a rotation matrix from the 3 euler angles PHI THETA and PSI, 0006 % given in degrees, using the 'XYZ' convention (local basis), or the 0007 % 'ZYX' convention (global basis). The result MAT is a 4-by-4 rotation 0008 % matrix in homogeneous coordinates. 0009 % 0010 % PHI: rotation angle around Z-axis, in degrees, corresponding to the 0011 % 'Yaw'. PHI is between -180 and +180. 0012 % THETA: rotation angle around Y-axis, in degrees, corresponding to the 0013 % 'Pitch'. THETA is between -90 and +90. 0014 % PSI: rotation angle around X-axis, in degrees, corresponding to the 0015 % 'Roll'. PSI is between -180 and +180. 0016 % These angles correspond to the "Yaw-Pitch-Roll" convention, also known 0017 % as "Tait-Bryan angles". 0018 % 0019 % The resulting rotation is equivalent to a rotation around X-axis by an 0020 % angle PSI, followed by a rotation around the Y-axis by an angle THETA, 0021 % followed by a rotation around the Z-axis by an angle PHI. 0022 % That is: 0023 % ROT = Rz * Ry * Rx; 0024 % 0025 % MAT = eulerAnglesToRotation3d(ANGLES) 0026 % Concatenates all angles in a single 1-by-3 array. 0027 % 0028 % ... = eulerAnglesToRotation3d(ANGLES, CONVENTION) 0029 % CONVENTION specifies the axis rotation sequence. Default is 'ZYX'. 0030 % Supported conventions are: 0031 % 'ZYX','ZXY','YXZ','YZX','XYZ','XZY' 0032 % 'ZYZ','ZXZ','YZY','YXY','XZX','XYX' 0033 % 0034 % Example 0035 % [n e f] = createCube; 0036 % phi = 20; 0037 % theta = 30; 0038 % psi = 10; 0039 % rot = eulerAnglesToRotation3d(phi, theta, psi); 0040 % n2 = transformPoint3d(n, rot); 0041 % drawPolyhedron(n2, f); 0042 % 0043 % See also 0044 % transforms3d, createRotationOx, createRotationOy, createRotationOz 0045 % rotation3dAxisAndAngle 0046 % 0047 0048 % ------ 0049 % Author: David Legland 0050 % E-mail: david.legland@inrae.fr 0051 % Created: 2010-07-22, using Matlab 7.9.0.529 (R2009b) 0052 % Copyright 2010-2024 INRA - Cepia Software Platform 0053 0054 % Process input arguments 0055 if size(phi, 2) == 3 0056 if nargin > 1 0057 varargin{1} = theta; 0058 end 0059 % manages arguments given as one array 0060 psi = phi(:, 3); 0061 theta = phi(:, 2); 0062 phi = phi(:, 1); 0063 end 0064 0065 p = inputParser; 0066 validStrings = {... 0067 'ZYX','ZXY','YXZ','YZX','XYZ','XZY',... 0068 'ZYZ','ZXZ','YZY','YXY','XZX','XYX'}; 0069 addOptional(p,'convention','ZYX',@(x) any(validatestring(x,validStrings))); 0070 parse(p,varargin{:}); 0071 convention=p.Results.convention; 0072 0073 % create individual rotation matrices 0074 k = pi / 180; 0075 0076 switch convention 0077 case 'ZYX' 0078 rot1 = createRotationOx(psi * k); 0079 rot2 = createRotationOy(theta * k); 0080 rot3 = createRotationOz(phi * k); 0081 case 'ZXY' 0082 rot1 = createRotationOy(psi * k); 0083 rot2 = createRotationOx(theta * k); 0084 rot3 = createRotationOz(phi * k); 0085 case 'YXZ' 0086 rot1 = createRotationOz(psi * k); 0087 rot2 = createRotationOx(theta * k); 0088 rot3 = createRotationOy(phi * k); 0089 case 'YZX' 0090 rot1 = createRotationOx(psi * k); 0091 rot2 = createRotationOz(theta * k); 0092 rot3 = createRotationOy(phi * k); 0093 case 'XYZ' 0094 rot1 = createRotationOz(psi * k); 0095 rot2 = createRotationOy(theta * k); 0096 rot3 = createRotationOx(phi * k); 0097 case 'XZY' 0098 rot1 = createRotationOy(psi * k); 0099 rot2 = createRotationOz(theta * k); 0100 rot3 = createRotationOx(phi * k); 0101 case 'ZYZ' 0102 rot1 = createRotationOz(psi * k); 0103 rot2 = createRotationOy(theta * k); 0104 rot3 = createRotationOz(phi * k); 0105 case 'ZXZ' 0106 rot1 = createRotationOz(psi * k); 0107 rot2 = createRotationOx(theta * k); 0108 rot3 = createRotationOz(phi * k); 0109 case 'YZY' 0110 rot1 = createRotationOy(psi * k); 0111 rot2 = createRotationOz(theta * k); 0112 rot3 = createRotationOy(phi * k); 0113 case 'YXY' 0114 rot1 = createRotationOy(psi * k); 0115 rot2 = createRotationOx(theta * k); 0116 rot3 = createRotationOy(phi * k); 0117 case 'XZX' 0118 rot1 = createRotationOx(psi * k); 0119 rot2 = createRotationOz(theta * k); 0120 rot3 = createRotationOx(phi * k); 0121 case 'XYX' 0122 rot1 = createRotationOx(psi * k); 0123 rot2 = createRotationOy(theta * k); 0124 rot3 = createRotationOx(phi * k); 0125 end 0126 0127 % concatenate matrices 0128 mat = rot3 * rot2 * rot1;