[R,s]=rotmat2vec(u,v) the rotation matrix from vector u to v, satisfying R*u*s=v author: Bruno Luong URL:http://www.mathworks.com/matlabcentral/newsreader/view_original/827969 input: u: a 3D vector in the source coordinate system; v: a 3D vector in the target coordinate system; output: R: a rotation matrix to transform normalized u to normalized v s: a scaling factor, so that R*u(:)*s=v(:) -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
0001 function [R,s]=rotmat2vec(u,v) 0002 % 0003 % [R,s]=rotmat2vec(u,v) 0004 % 0005 % the rotation matrix from vector u to v, satisfying R*u*s=v 0006 % 0007 % author: Bruno Luong 0008 % URL:http://www.mathworks.com/matlabcentral/newsreader/view_original/827969 0009 % 0010 % input: 0011 % u: a 3D vector in the source coordinate system; 0012 % v: a 3D vector in the target coordinate system; 0013 % 0014 % output: 0015 % R: a rotation matrix to transform normalized u to normalized v 0016 % s: a scaling factor, so that R*u(:)*s=v(:) 0017 % 0018 % -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net) 0019 % 0020 0021 s=norm(v(:))/norm(u(:)); 0022 u1=u(:)/norm(u(:)); 0023 v1=v(:)/norm(v(:)); 0024 0025 k = cross(u1,v1); 0026 if(~any(k)) % u and v are parallel 0027 R=eye(3); 0028 return; 0029 end 0030 % Rodrigues's formula: 0031 costheta = dot(u1,v1); 0032 R =[ 0 -k(3) k(2); 0033 k(3) 0 -k(1); 0034 -k(2) k(1) 0]; 0035 R = costheta*eye(3) + R + k*k'*(1-costheta)/sum(k.^2);