Skip to content
Snippets Groups Projects
Commit 23d69c18 authored by Achilles Kappis's avatar Achilles Kappis
Browse files

Combine functionality of rotMat3d.m and rotMat3dDeg.m to the former and remove the latter

parent 7aac2c0e
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,7 @@
% Author: Achilles Kappis
% e-mail: axilleaz@protonmail.com
%
% Date: 03/02/2024 (DD/MM/YYYY)
% Date: 16/07/2024 (DD/MM/YYYY)
%
% Copyright: MIT
% --------------------------------------------------
......@@ -17,6 +17,12 @@
%
% zAng [numeric] (Optional) : Rotation angle along the z-axis.
% [Default: 0].
%
% radDeg [chat/string] (Optional): This value declares if the provided
% angles are in radians or degrees. It can
% have one of the following values and is
% not case sensitive: "Degs", "Degrees",
% "Rads", "Radians". [Default: "Rads"].
% --------------------------------------------------
% Output
%
......@@ -34,11 +40,11 @@
% Notes
%
% --------------------------------------------------
function [rotMat, xRotMat, yRotMat, zRotMat] = rotMat3d(xAng, yAng, zAng)
function [rotMat, xRotMat, yRotMat, zRotMat] = rotMat3d(xAng, yAng, zAng, radDeg)
% ====================================================
% Check for number of arguments
% ====================================================
narginchk(1, 3);
narginchk(1, 4);
nargoutchk(0, 4);
% ====================================================
......@@ -48,18 +54,35 @@ function [rotMat, xRotMat, yRotMat, zRotMat] = rotMat3d(xAng, yAng, zAng)
validateattributes(xAng, "numeric", {'scalar', 'nonnan', 'finite', 'real'}, mfilename, 'Roll angle', 1);
% Optional arguments
if nargin > 1
if nargin > 1 && ~isempty(yAng)
validateattributes(yAng, "numeric", {'scalar', 'nonnan', 'finite', 'real'}, mfilename, 'Pitch angle', 2);
else
yAng = 0;
end
if nargin > 2
if nargin > 2 && ~isempty(zAng)
validateattributes(zAng, "numeric", {'scalar', 'nonnan', 'finite', 'real'}, mfilename, 'Yaw angle', 2);
else
zAng = 0;
end
if nargin > 3 && ~isempty(radDeg)
validateattributes(radDeg, {'char', 'string'}, {'scalartext'}, mfilename, "The 'type' of the provided angle values (radians or degrees)", 4);
validatestring(radDeg, ["Rads", "Radians", "Degs", "Degrees"], mfilename, "The 'type' of the provided angle values (radians or degrees)", 4);
else
radDeg = "Rads";
end
% ====================================================
% Pre-process angle values
% ====================================================
if sum(strcmpi(radDeg, ["Degs", "Degrees"])) > 0
xAng = deg2rad(xAng);
yAng = deg2rad(yAng);
zAng = deg2rad(zAng);
end
% ====================================================
% Create respective matrices
......
%% Calculate a 3D rotation matrix
% --------------------------------------------------
% Author: Achilles Kappis
% e-mail: axilleaz@protonmail.com
%
% Date: 03/02/2024 (DD/MM/YYYY)
%
% Copyright: MIT
% --------------------------------------------------
% Functionality: Calculate a 3D rotation matrix.
% --------------------------------------------------
% Input
%
% xAng [numeric]: Rotation angle along the x-axis in degrees.
%
% yAng [numeric] (Optional): Rotation angle along the y-axis in degrees.
% [Default: 0].
%
% zAng [numeric] (Optional) : Rotation angle along the z-axis in degrees.
% [Default: 0].
% --------------------------------------------------
% Output
%
% rotMat [numeric]: A 3x3 matrix which applies the rotation.
%
% xRotMat[numeric]: A 3x3 matrix which applies the rotation along the
% x-axis.
%
% yRotMat[numeric]: A 3x3 matrix which applies the rotation along the
% y-axis.
%
% zRotMat[numeric]: A 3x3 matrix which applies the rotation along the
% z-axis.
% --------------------------------------------------
% Notes
%
% --------------------------------------------------
function [rotMat, xRotMat, yRotMat, zRotMat] = rotMat3dDeg(xAng, yAng, zAng)
% ====================================================
% Check for number of arguments
% ====================================================
narginchk(1, 3);
nargoutchk(0, 4);
% ====================================================
% Validate input arguments
% ====================================================
% Mandatory arguments
validateattributes(xAng, "numeric", {'scalar', 'nonnan', 'finite', 'real'}, mfilename, 'Roll angle', 1);
% Optional arguments
if nargin > 1
validateattributes(yAng, "numeric", {'scalar', 'nonnan', 'finite', 'real'}, mfilename, 'Pitch angle', 2);
else
yAng = 0;
end
if nargin > 2
validateattributes(zAng, "numeric", {'scalar', 'nonnan', 'finite', 'real'}, mfilename, 'Yaw angle', 2);
else
zAng = 0;
end
% ====================================================
% Create respective matrices
% ====================================================
% Rolling rotation matrix
xRotMat = [1, 0, 0;...
0, cosd(xAng), -sind(xAng);...
0, sind(xAng), cosd(xAng)];
% Pitch rotation matrix
yRotMat = [cosd(yAng), 0, sind(yAng);...
0, 1, 0;...
-sind(yAng), 0, cosd(yAng)];
% Yaw rotation matrix
zRotMat = [cosd(zAng), -sind(zAng), 0;...
sind(zAng), cosd(zAng), 0;...
0, 0, 1];
% Complete rotation matrix
rotMat = zRotMat * yRotMat * xRotMat;
end
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment