-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRTfromCloud.m
60 lines (50 loc) · 2.1 KB
/
RTfromCloud.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
function RT = RTfromCloud(coords_s, coords_t, patching, corr_flag)
if nargin < 3
RT = cell(1,2);
num_ptr = length(coords_s(:,1));
data_s = coords_s; mean_s = mean(data_s);
data_t = coords_t; mean_t = mean(data_t);
%% estimation of rotation matrix, ref: http://www.kwon3d.com/theory/jkinem/rotmat.html
cross_cov = (data_t'*data_s)/num_ptr - mean_t'*mean_s;
[U,~,V] = svd(cross_cov);
R = U*[1 0 0;0 1 0; 0 0 det(U*V')]*V';
T = mean_t - (R*mean_s')';
%%
RT{1,1} = R;
RT{1,2} = T;
elseif nargin==3
num_P = max(patching) + 1;
RT = cell(num_P,2);
for pi = 1:num_P
idx = (patching==pi-1);
num_ptr = sum(idx);
data_s = coords_s(idx,:); mean_s = mean(data_s);
data_t = coords_t(idx,:); mean_t = mean(data_t);
%% estimation of rotation matrix, ref: http://www.kwon3d.com/theory/jkinem/rotmat.html
cross_cov = (data_t'*data_s)/num_ptr - mean_t'*mean_s;
[U,~,V] = svd(cross_cov);
R = U*[1 0 0;0 1 0; 0 0 det(U*V')]*V';
T = mean_t - (R*mean_s')';
%%
RT{pi,1} = R;
RT{pi,2} = T;
end
else
num_P = max(patching) + 1;
RT = cell(num_P,2);
for pi = 1:num_P
idx = (patching==pi-1) & corr_flag;
num_ptr = sum(idx);
data_s = coords_s(idx,:); mean_s = mean(data_s);
data_t = coords_t(idx,:); mean_t = mean(data_t);
%% estimation of rotation matrix, ref: http://www.kwon3d.com/theory/jkinem/rotmat.html
cross_cov = (data_t'*data_s)/num_ptr - mean_t'*mean_s;
[U,~,V] = svd(cross_cov);
R = U*[1 0 0;0 1 0; 0 0 det(U*V')]*V';
T = mean_t - (R*mean_s')';
%%
RT{pi,1} = R;
RT{pi,2} = T;
end
end
end