diff --git a/Utilities/Generic/MATLAB/Functions/rotMat3d.m b/Utilities/Generic/MATLAB/Functions/rotMat3d.m index 73be7904b056bc0d0dd017855a6dc9e63ca5145e..65c3733852e1108b5560658786063877d56f5070 100644 --- a/Utilities/Generic/MATLAB/Functions/rotMat3d.m +++ b/Utilities/Generic/MATLAB/Functions/rotMat3d.m @@ -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 diff --git a/Utilities/Generic/MATLAB/Functions/rotMat3dDeg.m b/Utilities/Generic/MATLAB/Functions/rotMat3dDeg.m deleted file mode 100644 index e832ed348f36d2ad0c5e35e734a4534acc81ef56..0000000000000000000000000000000000000000 --- a/Utilities/Generic/MATLAB/Functions/rotMat3dDeg.m +++ /dev/null @@ -1,85 +0,0 @@ -%% 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