Home > rest_20090422 > rest_spm5_files > @read_nifti > private > M2Q.m

M2Q

PURPOSE ^

Convert from rotation matrix to quaternion form

SYNOPSIS ^

function Q = M2Q(M)

DESCRIPTION ^

 Convert from rotation matrix to quaternion form
 See: http://skal.planet-d.net/demo/matrixfaq.htm
 _______________________________________________________________________
 Copyright (C) 2005 Wellcome Department of Imaging Neuroscience

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

M2Q.m

SOURCE CODE ^

0001 function Q = M2Q(M)
0002 % Convert from rotation matrix to quaternion form
0003 % See: http://skal.planet-d.net/demo/matrixfaq.htm
0004 % _______________________________________________________________________
0005 % Copyright (C) 2005 Wellcome Department of Imaging Neuroscience
0006 
0007 %
0008 % $Id: M2Q.m 253 2005-10-13 15:31:34Z guillaume $
0009 
0010 
0011 d = diag(M(1:3,1:3));
0012 t = sum(d) + 1;
0013 if t>0.5,
0014     s = sqrt(t)*2;
0015     Q = [(M(3,2)-M(2,3))/s (M(1,3)-M(3,1))/s (M(2,1)-M(1,2))/s 0.25*s]';
0016 else
0017     t = find(d==max(d));
0018     t = t(1);
0019     switch(t),
0020     case 1,
0021         s = 2*sqrt(1 + M(1,1) - M(2,2) - M(3,3));
0022         Q = [0.25*s (M(1,2)+M(2,1))/s (M(3,1)+M(1,3))/s (M(3,2)-M(2,3))/s]';
0023     case 2,
0024         s = 2*sqrt(1 + M(2,2) - M(1,1) - M(3,3));
0025         Q = [(M(1,2)+M(2,1))/s 0.25*s (M(2,3)+M(3,2))/s (M(1,3)-M(3,1))/s ]';
0026     case 3,
0027         s = 2*sqrt(1 + M(3,3) - M(1,1) - M(2,2));
0028         Q = [(M(3,1)+M(1,3))/s (M(2,3)+M(3,2))/s 0.25*s (M(2,1)-M(1,2))/s]';
0029     end;
0030 end;
0031 if Q(4)<0, Q = -Q; end; % w must be +ve
0032 return;
0033

Generated on Wed 29-Apr-2009 01:06:38 by m2html © 2005