-
Notifications
You must be signed in to change notification settings - Fork 6
/
MultiRegistration.cpp
346 lines (260 loc) · 6.7 KB
/
MultiRegistration.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "MultiRegistration.h"
#include <Eigen/SVD>
MultiRegistration::MultiRegistration( int m, int p, std::vector<Pair> *pair, std::vector<PointList> *pointlist )
{
M = m;
P = p;
pair_index = pair;
pl = pointlist;
}
MultiRegistration::~MultiRegistration(void)
{
}
void MultiRegistration::init()
{
R.resize(3, 3 * M);
T.resize(1, 3 * M);
Ca.resize(3 * M, 3 * P);
Cb.resize(3 * M, 3 * P);
H_xx.resize( 3 * P, 3 * P);
H_xy.resize( 3 * P, 3 * P);
H_yx.resize( 3 * P, 3 * P);
H_yy.resize( 3 * P, 3 * P);
Q_r.resize( 3 * M, 3 * M);
N.resize( 3 * P, 3 * P);
int b_x, b_y; // block的index
int g_x, g_y; // 原矩阵的index
int a, b;
/*** begin:构建Ca和Cb矩阵 ***/
for(int i = 0; i != M; i++)
{
for(int j = 0; j != P; j++)
{
a = (*pair_index)[j].a;
b = (*pair_index)[j].b;
b_x = i;
b_y = j;
block_index(b_x, b_y, g_x, g_y);
if( i == a )
{
Ca.block<3, 3>(g_x, g_y) = MyMatrix3f::Identity();
}else
{
Ca.block<3, 3>(g_x, g_y) = MyMatrix3f::Zero();
}
if( i == b )
{
Cb.block<3, 3>(g_x, g_y) = MyMatrix3f::Identity();
}else
{
Cb.block<3, 3>(g_x, g_y) = MyMatrix3f::Zero();
}
}
}
/*** end ***/
/*** begin:构建H_xx,N等矩阵***/
MyVector3f x, y;
MyRowVector3f x_r, y_r;
int Nu;
MyMatrix3f xx, xy, yx, yy;
H_xx.setZero();
H_yy.setZero();
H_xy.setZero();
H_yx.setZero();
N.setZero();
for (int u = 0; u != P; u++)
{
Nu = (*pl)[u].size();
xx = MyMatrix3f::Zero();
xy = MyMatrix3f::Zero();
yx = MyMatrix3f::Zero();
yy = MyMatrix3f::Zero();
for (int i = 0; i != Nu; i++)
{
x = (*pl)[u][i].a;
y = (*pl)[u][i].b;
x_r = x.transpose();
y_r = y.transpose();
xx += x * x_r;
xy += x * y_r;
yx += y * x_r;
yy += y * y_r;
}
b_x = u;
b_y = u;
block_index(b_x, b_y, g_x, g_y);
N.block<3, 3>(g_x, g_y) = Nu * MyMatrix3f::Identity();
H_xx.block<3, 3>(g_x, g_y) = xx;
H_xy.block<3, 3>(g_x, g_y) = xy;
H_yx.block<3, 3>(g_x, g_y) = yx;
H_yy.block<3, 3>(g_x, g_y) = yy;
}
/*** end ***/
/*** begin: 计算Q_r ***/
Q_r = Ca * H_xx * Ca.transpose() + Cb * H_yy * Cb.transpose() - Ca * H_xy * Cb.transpose() - Cb * H_yx * Ca.transpose();
MyMatrixXf Ca_Cb = Ca - Cb;
MyMatrixXf Ca_Cb_t = Ca_Cb.transpose();
MyMatrixXf tmp_pinv = Ca_Cb * N * Ca_Cb_t;
pinv(tmp_pinv, tmp_pinv);
MyMatrixXf ZK = N * Ca_Cb_t * tmp_pinv * Ca_Cb * N;
gamma.resize( P, P);
for (int i = 0; i != P; i++)
{
for (int j = 0; j != P; j++)
{
b_x = i;
b_y = j;
block_index(b_x, b_y, g_x, g_y);
gamma(i, j) = ZK(g_x, g_y);
}
}
std::vector<MyVector3f> x_mean;
std::vector<MyVector3f> y_mean;
MyVector3f x_sum, y_sum;
for (int u = 0; u != P; u++)
{
x_sum = y_sum = MyVector3f::Zero();
Nu = (*pl)[u].size();
for (int i = 0; i != Nu; i++)
{
x_sum += (*pl)[u][i].a;
y_sum += (*pl)[u][i].b;
}
x_mean.push_back( x_sum / Nu);
y_mean.push_back( y_sum / Nu);
}
MyMatrixXf G_xx;
MyMatrixXf G_xy;
MyMatrixXf G_yx;
MyMatrixXf G_yy;
G_xx.resize( 3 * P, 3 * P);
G_xy.resize( 3 * P, 3 * P);
G_yx.resize( 3 * P, 3 * P);
G_yy.resize( 3 * P, 3 * P);
for (int u = 0; u != P; u++)
{
for (int n = 0; n != P; n++)
{
b_x = u;
b_y = n;
block_index(b_x, b_y, g_x, g_y);
xx = gamma(u, n) * x_mean[u] * x_mean[n].transpose();
xy = gamma(u, n) * x_mean[u] * y_mean[n].transpose();
yx = gamma(u, n) * y_mean[u] * x_mean[n].transpose();
yy = gamma(u, n) * y_mean[u] * y_mean[n].transpose();
G_xx.block<3, 3>(g_x, g_y) = xx;
G_xy.block<3, 3>(g_x, g_y) = xy;
G_yx.block<3, 3>(g_x, g_y) = yx;
G_yy.block<3, 3>(g_x, g_y) = yy;
}
}
Q_xx = Ca * G_xx * Ca.transpose();
Q_xy = Ca * G_xy * Cb.transpose();
Q_yx = Cb * G_yx * Ca.transpose();
Q_yy = Cb * G_yy * Cb.transpose();
Q_rt = Q_xy + Q_yx - Q_yy - Q_xx;
Q = Q_r + Q_rt;
MyMatrixXf Sj;
MyMatrixXf V, U;
MyMatrixXf last_R;
last_R.resize( 3, M * 3);
last_R.setZero();
int count = MAX_ITERATION_R;
while( count-- )
{
for (int j= 0; j != M; j++)
{
Sj = Q.block(j * 3, 0, 3, j * 3) * R.block(0, 0, 3, j * 3).transpose() + Q.block( 3 * j , j * 3 + 3, 3, (M - j - 1) * 3 ) * R.block(0, j*3 + 3, 3, (M-j-1)*3).transpose();
Sj = -1 * Sj;
Eigen::JacobiSVD<MyMatrixXf> svd( Sj, Eigen::ComputeThinU | Eigen::ComputeThinV);
V = svd.matrixV();
U = svd.matrixU();
if( V.determinant() < 0 )
{
for (int i = 0; i != 3; i++)
{
V(i, 2) *= -1;
}
}
if( U.determinant() < 0 )
{
for (int i = 0; i != 3; i++)
{
U(i, 2) *= -1;
}
}
R.block<3, 3>(0, j * 3) = V * U.transpose();
}
MyMatrixXf diff = R - last_R;
if( fabs( diff.maxCoeff() ) < 1 * 1e-5 && fabs( diff.minCoeff() ) < 1 * 1e-5 )
{
break;
}
last_R = R;
}
std::cout<<count<<std::endl;
MyMatrixXf Z;
MyMatrixXf RCa;
MyMatrixXf RCb;
MyMatrixXf RCa_x;
MyMatrixXf RCb_y;
RCa = R * Ca;
RCb = R * Cb;
RCa_x.resize( 3 * P, 1);
RCb_y.resize( 3*P, 1);
for (int i = 0; i != P; i++)
{
b_x = i;
b_y = 0;
block_index(b_x, b_y, g_x, g_y);
RCa_x.block<3, 1>(g_x, 0) = RCa.block<3, 3>(0, g_x) * x_mean[i];
RCb_y.block<3, 1>(g_x, 0) = RCb.block<3, 3>(0, g_x) * y_mean[i];
}
Z = RCa_x - RCb_y;
T = (-1) * tmp_pinv * Ca_Cb * N * Z;
}
void MultiRegistration::block_index( int &b_x, int &b_y, int &g_x, int &g_y, int block_size_x/*=3*/, int block_size_y/*=3*/ )
{
g_x = b_x * block_size_x;
g_y = b_y * block_size_y;
}
void MultiRegistration::pinv( MyMatrixXf &input, MyMatrixXf &pinv_matrix )
{
double epsilon = 1e-5;
int x, y;
x = input.rows();
y = input.cols();
Eigen::JacobiSVD<MyMatrixXf> svd(input, Eigen::ComputeThinU | Eigen::ComputeThinV);
Eigen::Matrix<double, Eigen::Dynamic, 1> inv_singular(y, 1);
for(int i = 0; i < y; i++)
{
if ( fabs( svd.singularValues()[i] ) < epsilon )
{
inv_singular[i] = 0;
}else
{
inv_singular[i] = 1 / svd.singularValues()[i];
}
}
pinv_matrix = svd.matrixV() * inv_singular.asDiagonal() * svd.matrixU().transpose();
}
void MultiRegistration::test()
{
MyMatrixXf m = MyMatrixXf::Random(3, 2);
MyMatrixXf output;
std::cout<<m.block(0, 0, 1,0)<<std::endl;
pinv(m , output);
}
void MultiRegistration::get_R_and_T( std::vector<MyMatrix3f> &_R, std::vector<MyVector3f> &_T )
{
int b_x, b_y; // block的index
int g_x, g_y; // 原矩阵的index
for (int i = 0; i != M; i++)
{
b_x = 0;
b_y = i;
block_index(b_x, b_y, g_x, g_y);
_R.push_back( R.block<3, 3>(g_x, g_y) );
_T.push_back( T.block<3, 1>(g_y, 0) );
}
}