-
Notifications
You must be signed in to change notification settings - Fork 0
/
tc_transmat2vec.m
executable file
·68 lines (55 loc) · 1.54 KB
/
tc_transmat2vec.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
57
58
59
60
61
62
63
64
65
66
67
68
function transvec = tc_transmat2vec(R,varargin)
% #### hacked by Tommy ####
% original function is the MATLAB function "rotm2axang.m" from the robotics
% toolbox
%
% takes dlm-readable filename or 4x4 matrix as input
%
% Please check beforehand if the matrix is ortho-normal
% transvec 1 x 6 "matrix" giving the respective translation
% along x, y, z and rotation around x, y, z axis
inv_=0;
if size(varargin,2)>0
inv_=varargin{1};
end
if ischar(R)
R = dlmread(R);
end
if inv_
R=inv(R);
end
trans=R(1:3,4)';
R(:,4)=[];
R(4,:)=[];
% Compute theta
theta = real(acos(complex((1/2)*(R(1,1,:)+R(2,2,:)+R(3,3,:)-1))));
% Determine initial axis vectors from theta
v = [ R(3,2,:)-R(2,3,:),...
R(1,3,:)-R(3,1,:),...
R(2,1,:)-R(1,2,:)] ./ (repmat(2*sin(theta),[1,3]));
% Handle the degenerate cases where theta is divisible by pi
singularLogical = mod(theta, cast(pi,'like',R)) == 0;
numSingular = sum(singularLogical,3);
assert(numSingular <= length(singularLogical));
if any(singularLogical)
vspecial = zeros(3,numSingular,'like',R);
inds = find(singularLogical);
for i = 1:sum(singularLogical)
[~,~,V] = svd(eye(3)-R(:,:,inds(i)));
vspecial(:,i) = V(:,end);
end
v(1,:,singularLogical) = vspecial;
end
% Extract final values
theta = reshape(theta,[numel(theta) 1]);
v = reshape(v,[3, numel(v)/3]).';
axang = cat(2, v, theta);
%%% CHANGE ORDER HERE IF NECCESSARY %%%
X_Trans=trans(1);
Y_Trans=trans(2);
Z_Trans=trans(3);
X_Rot=axang(1);
Y_Rot=axang(2);
Z_Rot=axang(3);
transvec=[X_Trans,Y_Trans,Z_Trans,X_Rot,Y_Rot,Z_Rot];
end