Path: news.mathworks.com!not-for-mail
From: "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
Newsgroups: comp.soft-sys.matlab
Subject: Re: angle from rotation matrix
Date: Sat, 15 Dec 2007 11:11:41 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 43
Message-ID: <fk0cpd$g0v$1@fred.mathworks.com>
References: <fjvbqk$gbt$1@fred.mathworks.com>
Reply-To: "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1197717101 16415 172.30.248.37 (15 Dec 2007 11:11:41 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 15 Dec 2007 11:11:41 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:442584


"Pinpress " <nospam__@yahoo.com> wrote in message <fjvbqk$gbt
$1@fred.mathworks.com>...
> Hi,
> 
> Any one knows how to extract the rotational axis and the
> angle from a 3-by-3 rotation matrix?  Thanks very much. 
> I've googled, but haven't got the luck for the solution.
-------
  If matrix A is a 3 x 3 rotation matrix about the origin, then it must be a real 
orthogonal (unitary) matrix (that is, its transpose must be equal to its 
inverse), and its determinant must equal +1.  Since any point on the axis of 
rotation obviously remains fixed, a unit vector along this axis must 
necessarily be an eigenvector of A with eigenvalue 1, and there clearly can be 
no other real eigenvectors or eigenvalues.  Hence, we can determine the axis 
of rotation by selecting that unique eigenvector, w, which has an eigenvalue 
of 1.  It can be shown, 1) that the trace of A must equal 1+2*cos(theta) where 
theta is the counterclockwise angle of rotation, and 2) that the dot product of 
the vector

 t = [A(3,2)-A(2,3),A(1,3)-A(3,1),A(2,1)-A(1,2)]

with w must be 2*sin(theta), which means that sin(theta) and cos(theta) can 
be computed.  We can then use matlab's 'atan2' function to determine theta 
itself from these values.  Note that the signs of both w and theta can be 
reversed and still correspond to the same rotation matrix, so the convention 
has been adopted below that the positive value of theta will always be 
selected and the sign of w adjusted accordingly if necessary.

  The following then is the needed matlab computation:

 [V,D] = eig(A);
 [ignore,ix] = min(abs(diag(D)-1));
 w = V(:,ix);
 t = [A(3,2)-A(2,3),A(1,3)-A(3,1),A(2,1)-A(1,2)];
 theta = atan2(t*w,trace(A)-1);
 if theta<0, theta = -theta; w = -w; end

The quantities w and theta will be a unit vector along the rotation axis and 
the counterclockwise rotation about it, respectively.  Note that this 
computation does not work unless A is a valid rotation matrix.

Roger Stafford