Subversion Repositories seema-scanner

Rev

Rev 238 | Go to most recent revision | 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
figure;
31
%options = optimset('Algorithm', 'levenberg-marquardt', 'Display', 'iter-detailed', 'OutputFcn', @outfun, 'MaxIter', 100, 'TolFun', 0, 'TolX', 0);
32
options = optimoptions('lsqnonlin', 'Display', 'iter-detailed');
213 jakw 33
[x, ~, ~] = lsqnonlin(@orthProcFun, xInit, [], [], options);
34
 
35
alignment = struct('Rotation', {}, 'Translation', {});
241 jakw 36
for i=1:Np
37
    wi = x((i-1)*6+1:(i-1)*6+3);
38
    Ri = initialAlign(i).Rotation * axis2rmat(wi, norm(wi));
39
    Ti =  initialAlign(i).Translation + x((i-1)*6+4:(i-1)*6+6);
40
    alignment(i).Rotation = Ri;
41
    alignment(i).Translation =Ti;
213 jakw 42
end
43
 
44
 
45
    % objective function
46
    function e = orthProcFun(x)
47
 
48
        Nc = size(P, 2);
49
 
50
        % transform all points according to current x
51
        Pbar = cell(size(P));
241 jakw 52
        for j=1:Np
53
            wi = x((j-1)*6+1:(j-1)*6+3);
54
            Ri = initialAlign(j).Rotation * axis2rmat(wi, norm(wi));
55
            Ti = initialAlign(j).Translation + x((j-1)*6+4:(j-1)*6+6);
56
            for k=1:Nc
57
                if(not(isempty(P{j,k})))
58
                    Pbar{j,k} = P{j,k}*Ri' + Ti';
213 jakw 59
                end
60
            end
61
        end
62
 
241 jakw 63
        % include all pairwise distances
213 jakw 64
        e = [];
241 jakw 65
        for j=1:Nc   
66
            % points in current cluster
67
            c = cat(1, Pbar{:,j});
68
            Ncj = size(c, 1);
69
 
70
            if(Ncj < 2)
71
                continue;
72
            end
73
 
74
            for k=1:Ncj-1
75
 
76
                for l=k+1:Ncj
77
 
78
                    dVec = c(k,:) - c(l,:);
79
 
80
                    e(end+1:end+3) = dVec;
213 jakw 81
                end
82
            end
241 jakw 83
 
213 jakw 84
        end
85
 
86
    end
87
 
88
    % output function called at every iteration
89
    function stop = outfun(x, optimValues, ~)
241 jakw 90
        %display(x);
91
 
92
        hold('on');
93
        plot(optimValues.residual);
94
        drawnow;
213 jakw 95
        stop = false;
96
    end
97
 
98
end
99
 
100
function [w, theta] = rmat2axis(R)
101
 
219 jakw 102
w = zeros(3, 1);
237 jakw 103
%theta = zeros(1, 1);
213 jakw 104
 
105
[V,D] = eig(R);
106
[~,ix] = min(abs(diag(D)-1)); 
107
 
108
w(:) = V(:,ix); 
109
t = [R(3,2)-R(2,3),R(1,3)-R(3,1),R(2,1)-R(1,2)];
110
 
111
theta = atan2(t*w(:),trace(R(:,:))-1);
112
 
113
if theta<0
241 jakw 114
    theta = -theta; 
213 jakw 115
    w(:) = -w(:); 
116
end
117
end
118
 
119
function R = axis2rmat(w, theta)
120
 
121
P = w*transpose(w);
122
Q = [0 -w(3) w(2);
123
     w(3) 0 -w(1);
124
     -w(2) w(1) 0];
125
 
126
% using Rodigues' rotation formula
127
R = P + (eye(3) - P)*cos(theta) + Q*sin(theta);
219 jakw 128
 
129
% ensure orthonormal matrix
130
[U,~,V] = svd(R);
131
R = U*V';
132
 
213 jakw 133
end
134
 
135