This repository has been archived by the owner on Mar 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathdcm2q.m
48 lines (40 loc) · 1.41 KB
/
dcm2q.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function q=dcm2q(R)
% DCM2Q(R) converts direction cosine matrices into quaternions.
%
% The resultant quaternion(s) will perform the equivalent vector
% transformation as the input DCM(s), i.e.:
%
% qconj(Q)*V*Q = R*V
%
% where R is the DCM, V is a vector, and Q is the quaternion. Note that
% for purposes of quaternion-vector multiplication, a vector is treated
% as a quaterion with a scalar element of zero.
%
% If the input is a 3x3xN array, the output will be a vector of
% quaternions where input direction cosine matrix R(:,:,k) corresponds
% to the output quaternion Q(k,:).
%
% See also Q2DCM.
% Release: $Name: quaternions-1_2_2 $
% $Revision: 1.9 $
% $Date: 2002/01/21 06:46:20 $
% Copyright (C) 2000-2002, Jay A. St. Pierre. All rights reserved.
if nargin~=1
error('dcm2q() requires one input argument');
else
size_R=size(R);
if ( size_R(1)~=3 | size_R(2)~=3 | length(size_R)>3 )
error(['Invalid input: must be a 3x3xN array'])
end
end
q(1,:)=0.5*sqrt(1 + R(1,1,:) - R(2,2,:) - R(3,3,:)).*sgn(R(2,3,:)-R(3,2,:));
q(2,:)=0.5*sqrt(1 - R(1,1,:) + R(2,2,:) - R(3,3,:)).*sgn(R(3,1,:)-R(1,3,:));
q(3,:)=0.5*sqrt(1 - R(1,1,:) - R(2,2,:) + R(3,3,:)).*sgn(R(1,2,:)-R(2,1,:));
q(4,:)=0.5*sqrt(1 + R(1,1,:) + R(2,2,:) + R(3,3,:));
% Make quaternion vector a column of quaternions
q=q.';
q=real(q);
% Signum function (for our purposes)
function s=sgn(x)
s=sign(x);
s=s+(s==0);