Subversion Repositories seema-scanner

Rev

Rev 241 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
213 jakw 1
function [alignment] = groupwiseOrthogonalProcrustes(P, initialAlign)
241 jakw 2
%groupwiseOrthogonalProcrustes Computes rototranslations to bring Np
213 jakw 3
% matched point sets into alignment using non-linear optimization.
4
%
5
% Input:
6
%   P: (Np x Nc) cell array containing point coordinates in matched order.
7
%       empty cells indicate that the corresponding point set does not
8
%       include a point matched to that cluster.
241 jakw 9
%   initialAlignment: a Np length struct with fields Rotation (3x3 matrix)  
213 jakw 10
%       and Translation (3-vector) for each of the point sets.
11
%
12
% Output:
13
%   alignment: a struct with fields Rotation (3x3 matrix) and Translation
14
%       (3-vector) for each of the point sets.
15
%
16
 
17
Np = size(P, 1);
241 jakw 18
assert(length(initialAlign) >= Np);
213 jakw 19
 
241 jakw 20
% parameter vector contains relative axis angle and translations for all Np
21
% point sets
213 jakw 22
xInit = zeros(6*Np, 1);
23
 
241 jakw 24
% for i=1:Np
25
%     [w, theta] = rmat2axis(initialAlign(i).Rotation);
26
%     xInit((i-1)*6+1:(i-1)*6+3) = w*theta;
27
%     xInit((i-1)*6+4:(i-1)*6+6) = initialAlign(i).Translation;
28
% end
213 jakw 29
 
241 jakw 30
options = optimoptions('lsqnonlin', 'Display', 'iter-detailed');
213 jakw 31
[x, ~, ~] = lsqnonlin(@orthProcFun, xInit, [], [], options);
32
 
33
alignment = struct('Rotation', {}, 'Translation', {});
241 jakw 34
for i=1:Np
35
    wi = x((i-1)*6+1:(i-1)*6+3);
36
    Ri = initialAlign(i).Rotation * axis2rmat(wi, norm(wi));
37
    Ti =  initialAlign(i).Translation + x((i-1)*6+4:(i-1)*6+6);
38
    alignment(i).Rotation = Ri;
39
    alignment(i).Translation =Ti;
213 jakw 40
end
41
 
42
 
43
    % objective function
44
    function e = orthProcFun(x)
45
 
46
        Nc = size(P, 2);
47
 
48
        % transform all points according to current x
49
        Pbar = cell(size(P));
241 jakw 50
        for j=1:Np
51
            wi = x((j-1)*6+1:(j-1)*6+3);
52
            Ri = initialAlign(j).Rotation * axis2rmat(wi, norm(wi));
53
            Ti = initialAlign(j).Translation + x((j-1)*6+4:(j-1)*6+6);
54
            for k=1:Nc
55
                if(not(isempty(P{j,k})))
56
                    Pbar{j,k} = P{j,k}*Ri' + Ti';
213 jakw 57
                end
58
            end
59
        end
60
 
241 jakw 61
        % include all pairwise distances
213 jakw 62
        e = [];
241 jakw 63
        for j=1:Nc   
64
            % points in current cluster
65
            c = cat(1, Pbar{:,j});
66
            Ncj = size(c, 1);
67
 
68
            if(Ncj < 2)
69
                continue;
70
            end
71
 
72
            for k=1:Ncj-1
73
 
74
                for l=k+1:Ncj
75
 
76
                    dVec = c(k,:) - c(l,:);
77
 
78
                    e(end+1:end+3) = dVec;
213 jakw 79
                end
80
            end
241 jakw 81
 
213 jakw 82
        end
83
 
84
    end
85
 
86
    % output function called at every iteration
87
    function stop = outfun(x, optimValues, ~)
241 jakw 88
        %display(x);
89
 
90
        hold('on');
91
        plot(optimValues.residual);
92
        drawnow;
213 jakw 93
        stop = false;
94
    end
95
 
96
end
97
 
98
function [w, theta] = rmat2axis(R)
99
 
219 jakw 100
w = zeros(3, 1);
237 jakw 101
%theta = zeros(1, 1);
213 jakw 102
 
103
[V,D] = eig(R);
104
[~,ix] = min(abs(diag(D)-1)); 
105
 
106
w(:) = V(:,ix); 
107
t = [R(3,2)-R(2,3),R(1,3)-R(3,1),R(2,1)-R(1,2)];
108
 
109
theta = atan2(t*w(:),trace(R(:,:))-1);
110
 
111
if theta<0
241 jakw 112
    theta = -theta; 
213 jakw 113
    w(:) = -w(:); 
114
end
115
end
116
 
117
function R = axis2rmat(w, theta)
118
 
119
P = w*transpose(w);
120
Q = [0 -w(3) w(2);
121
     w(3) 0 -w(1);
122
     -w(2) w(1) 0];
123
 
124
% using Rodigues' rotation formula
125
R = P + (eye(3) - P)*cos(theta) + Q*sin(theta);
219 jakw 126
 
127
% ensure orthonormal matrix
128
[U,~,V] = svd(R);
129
R = U*V';
130
 
213 jakw 131
end
132
 
133