


obtain rotation matrix based on angle and coordinate axis OR a vector
Syntax: rot = getRotationMatrix3d(axis, angle)
or rot = getRotationMatrix3d('vector', v)
The axis is 'x', 'y' or 'z' and the angle must be provided in radian.
With the case 'vector', the the rotation matrix is calculated so that
the normalized vector v matches the unit vector if the z-axis.
See also: getRotationMatrix2d, getRotationMatrix4d, rotatePolytope

0001 function rot = getRotationMatrix3d(axis, a) 0002 % obtain rotation matrix based on angle and coordinate axis OR a vector 0003 % 0004 % Syntax: rot = getRotationMatrix3d(axis, angle) 0005 % or rot = getRotationMatrix3d('vector', v) 0006 % 0007 % The axis is 'x', 'y' or 'z' and the angle must be provided in radian. 0008 % With the case 'vector', the the rotation matrix is calculated so that 0009 % the normalized vector v matches the unit vector if the z-axis. 0010 % 0011 % See also: getRotationMatrix2d, getRotationMatrix4d, rotatePolytope 0012 0013 % The elk-library: convex geometry applied to crystallization modeling. 0014 % Copyright (C) 2012 Alexander Reinhold 0015 % 0016 % This program is free software: you can redistribute it and/or modify it 0017 % under the terms of the GNU General Public License as published by the 0018 % Free Software Foundation, either version 3 of the License, or (at your 0019 % option) any later version. 0020 % 0021 % This program is distributed in the hope that it will be useful, but 0022 % WITHOUT ANY WARRANTY; without even the implied warranty of 0023 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0024 % General Public License for more details. 0025 % 0026 % You should have received a copy of the GNU General Public License along 0027 % with this program. If not, see <http://www.gnu.org/licenses/>. 0028 0029 switch lower(axis) 0030 case 'x' 0031 rot = [1 0 0; 0032 0 cos(a) -sin(a); 0033 0 sin(a) cos(a)]; 0034 case 'y' 0035 rot = [cos(a) 0 sin(a); 0036 0 1 0; 0037 -sin(a) 0 cos(a)]; 0038 case 'z' 0039 rot = [cos(a) -sin(a) 0; 0040 sin(a) cos(a) 0; 0041 0 0 1]; 0042 case 'vector' 0043 % correct v to be a column vector 0044 if size(a, 1) == 1 0045 a = a'; 0046 end 0047 0048 % rotate vector so that it lies in xz-plane (eliminates y-direction) 0049 if a(2) ~=0 0050 anglez = acos(dot(a(1:2) / norm(a(1:2)), [1,0])) * ... 0051 -sign(a(2)); 0052 matrix_z = getRotationMatrix3d('z', anglez); 0053 a = matrix_z*a; 0054 else 0055 matrix_z = eye(3); 0056 end 0057 0058 % rotate vector so that it lies in yz-plane (eliminates z-direction) 0059 % normal is then colinear with z-axis 0060 if a(1) ~= 0 0061 angley = acos(dot(a([1,3]) / norm(a([1,3])),[0,1])) * ... 0062 -sign(a(1)); 0063 matrix_y = getRotationMatrix3d('y', angley); 0064 else 0065 matrix_y = eye(3); 0066 end 0067 0068 % assign rotation matrix 0069 rot = matrix_y*matrix_z; 0070 0071 otherwise 0072 error(['Axis ''' axis ''' unknown, use ''x'', ''y'', ''z'' or ''vector''.']) 0073 end