


obtain rotation matrix based on a vector (or set of vectors) Syntax: rot = getRotationMatrix(normalVector) The rotation matrix is calculated such that the given vector points to the x-axis after the rotation. If a matrix of vectors is provided (each column one vector), the vectors point to the first, second, third and so on coordinate axis. Therefore, the given vectors must be orthogonal to each other. See also: getRotationMatrix2d, getRotationMatrix4d, rotatePolytope


0001 function rot = getRotationMatrix(vector, zerotol) 0002 % obtain rotation matrix based on a vector (or set of vectors) 0003 % 0004 % Syntax: rot = getRotationMatrix(normalVector) 0005 % 0006 % The rotation matrix is calculated such that the given vector points to 0007 % the x-axis after the rotation. If a matrix of vectors is provided (each 0008 % column one vector), the vectors point to the first, second, third and 0009 % so on coordinate axis. Therefore, the given vectors must be orthogonal 0010 % to each other. 0011 % 0012 % See also: getRotationMatrix2d, getRotationMatrix4d, rotatePolytope 0013 0014 % The elk-library: convex geometry applied to crystallization modeling. 0015 % Copyright (C) 2012 Alexander Reinhold 0016 % 0017 % This program is free software: you can redistribute it and/or modify it 0018 % under the terms of the GNU General Public License as published by the 0019 % Free Software Foundation, either version 3 of the License, or (at your 0020 % option) any later version. 0021 % 0022 % This program is distributed in the hope that it will be useful, but 0023 % WITHOUT ANY WARRANTY; without even the implied warranty of 0024 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0025 % General Public License for more details. 0026 % 0027 % You should have received a copy of the GNU General Public License along 0028 % with this program. If not, see <http://www.gnu.org/licenses/>. 0029 0030 %% input handling 0031 if ~exist('zerotol', 'var') || isempty(zerotol) 0032 zerotol = elkZerotol; 0033 end 0034 0035 %% check orthogonality 0036 nVec = size(vector, 1); 0037 for iVecOne = 1:nVec 0038 for iVecTwo = (iVecOne+1):nVec 0039 if abs(vector(iVecOne, :) * vector(iVecTwo,:)') > zerotol 0040 error('elk:polytope:wrongInput', ... 0041 'provided vectors are not orthogonal'); 0042 end 0043 end 0044 end 0045 0046 %% obtain rotation matrix by svd 0047 % normalize vectors 0048 vector = bsxfun(@times, ... 0049 1./sqrt(sum(vector.^2, 2)), ... 0050 vector); 0051 0052 [U, S, V] = svd(vector); 0053 rot = V';