-
Notifications
You must be signed in to change notification settings - Fork 0
/
rot2quat.m
56 lines (44 loc) · 1.13 KB
/
rot2quat.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
49
50
51
52
53
54
55
56
function Q = rot2quat(R)
% qGetQ: converts 3x3 rotation matrix into equivalent quaternion
% Q = qGetQ( R );
[r,c] = size( R );
if( r ~= 3 | c ~= 3 )
fprintf( 'R must be a 3x3 matrix\n\r' );
return;
end
% [ Rxx, Rxy, Rxz ] = R(1,1:3);
% [ Ryx, Ryy, Ryz ] = R(2,1:3);
% [ Rzx, Rzy, Rzz ] = R(3,1:3);
Rxx = R(1,1); Rxy = R(1,2); Rxz = R(1,3);
Ryx = R(2,1); Ryy = R(2,2); Ryz = R(2,3);
Rzx = R(3,1); Rzy = R(3,2); Rzz = R(3,3);
w = sqrt( trace( R ) + 1 ) / 2;
% check if w is real. Otherwise, zero it.
if( imag( w ) > 0 )
w = 0;
end
x = sqrt( 1 + Rxx - Ryy - Rzz ) / 2;
y = sqrt( 1 + Ryy - Rxx - Rzz ) / 2;
z = sqrt( 1 + Rzz - Ryy - Rxx ) / 2;
[element, i ] = max( [w,x,y,z] );
if( i == 1 )
x = ( Rzy - Ryz ) / (4*w);
y = ( Rxz - Rzx ) / (4*w);
z = ( Ryx - Rxy ) / (4*w);
end
if( i == 2 )
w = ( Rzy - Ryz ) / (4*x);
y = ( Rxy + Ryx ) / (4*x);
z = ( Rzx + Rxz ) / (4*x);
end
if( i == 3 )
w = ( Rxz - Rzx ) / (4*y);
x = ( Rxy + Ryx ) / (4*y);
z = ( Ryz + Rzy ) / (4*y);
end
if( i == 4 )
w = ( Ryx - Rxy ) / (4*z);
x = ( Rzx + Rxz ) / (4*z);
y = ( Ryz + Rzy ) / (4*z);
end
Q = [ w x y z ];