-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingleAlignCorre-twostep-ransac.cc
378 lines (352 loc) · 12.9 KB
/
singleAlignCorre-twostep-ransac.cc
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include <iostream>
#include <fstream>
#include <sstream>
#include <unordered_set>
#include <time.h>
#include "ceres/ceres.h"
#include "glog/logging.h"
using ceres::AutoDiffCostFunction;
using ceres::DynamicAutoDiffCostFunction;
using ceres::CostFunction;
using ceres::CauchyLoss;
using ceres::Problem;
using ceres::Solver;
using ceres::Solve;
using namespace std;
typedef vector<int> VecInt;
typedef vector<VecInt> MatInt;
static constexpr int kStride = 100;
struct Corres{
int id1;
int id2;
double dist;
};
class Ronin{
public:
~Ronin(){
delete[] mags;
delete[] thetas;
delete[] biases;
delete[] biases_copy;
}
double* get_biases() {return biases;}
MatInt& get_cluster_indices() {return cluster_indices;}
VecInt& get_cluster_indices(int i) {return cluster_indices[i];}
double* get_mags() {return mags;}
double* get_thetas() {return thetas;}
int get_n_velocity() {return n_velocity;}
int get_n_corres() {return n_corres;}
vector<Corres>& get_corres_pairs() {return corres_pairs;}
VecInt& get_r_corres_idx() {return r_corres_idx;}
bool write_mag_theta(string &filename){
for(int i=0;i<n_velocity;i++){
biases[i] = biases_copy[i];
}
ofstream fout(filename);
fout<<n_velocity<<endl;
double updated_x=0., updated_y=0.;
for (int i=0; i<n_velocity; i++){
updated_x += mags[i]*cos(thetas[i]+biases[i]);
updated_y += mags[i]*sin(thetas[i]+biases[i]);
fout<<mags[i]<<"\t"<<thetas[i]<<"\t"<<biases[i]<<"\t"<<updated_x<<"\t"<<updated_y<<endl;
}
fout.close();
return true;
}
void reset_bias(){
for (int i=0;i<n_velocity;i++){
biases[i] = 0.;
}
}
bool read_mag_theta(string &filename){
ifstream fin;
// read mag and theta
fin.open(filename);
if (!fin){
cerr<<"cannot read mag theta"<<endl;
exit(1);
}
string tmp;
fin>>tmp>>n_velocity;
mags = new double[n_velocity];
thetas = new double[n_velocity];
biases = new double[n_velocity];
biases_copy = new double[n_velocity];
for (int i=0; i<n_velocity; i++){
fin>>mags[i]>>thetas[i];
biases[i] = 0.;
biases_copy[i] = 0.;
}
fin.close();
// for (int i=0; i<30; i++){
// cout<<mags[i]<<"\t"<<thetas[i]<<endl;
// }
return true;
}
bool read_corres_data(string& filename){
ifstream fin;
fin.open(filename);
if (!fin){
cerr<<"cannot read corres data"<<endl;
exit(1);
}
string tmp;
fin>>tmp>>n_corres;
corres_pairs.resize(n_corres);
for (int i=0; i<n_corres; i++){
double t1,t2,t3;
fin>>t1>>t2>>t3;
//cout<<t1<<" "<<t2<<" "<<t3<<endl;
corres_pairs[i].id1 = t1;
corres_pairs[i].id2 = t2;
corres_pairs[i].dist = t3;
//cout<<corres_pairs[i].id1<<"\t"<<corres_pairs[i].id2<<"\t"<<corres_pairs[i].dist<<endl;
}
fin.close();
double t_sum = 0.;
for (int i=0; i<n_corres; i++){
corres_pairs[i].dist = std::exp(-corres_pairs[i].dist/50.);
t_sum += corres_pairs[i].dist;
}
for (int i=0; i<n_corres; i++){
corres_pairs[i].dist /= t_sum;
}
// cout<<n_corres<<endl;
// for (int i=0; i<n_corres; i++){
// cout<<corres_pairs[i].id1<<"\t"<<corres_pairs[i].id2<<"\t"<<corres_pairs[i].dist<<endl;
// }
return true;
}
bool read_cluster_indices(string &filename){
ifstream fin;
// read mag and theta
fin.open(filename);
if (!fin){
cerr<<"cannot read cluster indices"<<endl;
exit(1);
}
int tmp;
fin>>tmp;
cluster_indices.resize(tmp);
for (size_t i=0;i<cluster_indices.size();i++){
fin>>tmp;
fin>>tmp;
cluster_indices[i].resize(tmp);
for (int j=0;j<tmp;j++){
fin>>cluster_indices[i][j];
}
}
fin.close();
// print
// for (size_t i=0;i<cluster_indices.size();i++){
// for (size_t j=0;j<cluster_indices[i].size();j++){
// cout<<cluster_indices[i][j]<<"\t";
// }
// cout<<endl;
// }
return true;
}
void sample_corres_pairs(int n_reduce){
r_corres_idx.resize(n_reduce);
std::unordered_set<unsigned int> indices;
while (indices.size() < n_reduce)
indices.insert(rand()%n_corres);
int i=0;
for (auto iter=indices.begin(); iter!=indices.end();iter++){
r_corres_idx[i]=(*iter);
i++;
}
// for (i=0;i<r_corres_idx.size();i++){
// cout<<r_corres_idx[i]<<"\t";
// }
// cout<<endl;
}
bool comp_inlier_ratio(string& in_path){
double xstart=0., ystart= 0.;
vector<double> updated_x(n_velocity+1);
vector<double> updated_y(n_velocity+1);
updated_y[0] = ystart;
updated_x[0] = xstart;
for (int i=0;i<n_velocity;i++){
xstart += mags[i]*cos(thetas[i]+biases[i]);
ystart += mags[i]*sin(thetas[i]+biases[i]);
updated_x[i+1] = xstart;
updated_y[i+1] = ystart;
}
vector<double> corres_dist_2d(corres_pairs.size());
double avg_dist_2d = 0.;
for(size_t i=0;i<corres_pairs.size();i++){
int id1 = corres_pairs[i].id1;
int id2 = corres_pairs[i].id2;
corres_dist_2d[i] = fabs(updated_x[id1] - updated_x[id2])+fabs(updated_y[id1] - updated_y[id2]);
avg_dist_2d += corres_dist_2d[i];
}
avg_dist_2d /= corres_pairs.size();
// sort(corres_dist_2d.begin(), corres_dist_2d.end());
// double med_dist_2d = corres_dist_2d[int(corres_dist_2d.size()/2.)];
double inlier_ratio = 0.;
VecInt r_corres_idx_copy(r_corres_idx);
r_corres_idx.clear();
for(size_t i=0;i<corres_pairs.size();i++){
if (corres_dist_2d[i]<avg_dist_2d){
r_corres_idx.push_back(i);
inlier_ratio += 1.;
}
}
cout<<r_corres_idx.size()<<"\t"<<corres_pairs.size()<<endl;
string filename = in_path + "c_single_corres_sample_incremental.txt";
ofstream fout(filename);
for (size_t i=0;i<r_corres_idx.size();i++){
fout<<r_corres_idx[i]<<"\t";
}
fout<<endl;
fout.close();
if (r_corres_idx_copy==r_corres_idx)
return true;
return false;
}
void copy_bias(){
for(int i=0;i<n_velocity;i++){
biases_copy[i] = biases[i];
}
}
private:
int n_velocity;
int n_corres;
double* mags;
double* thetas;
MatInt cluster_indices;
double* biases;
double* biases_copy;
vector<Corres> corres_pairs;
VecInt r_corres_idx; //sampled reduced corres pair idx
};
struct StatConstraint {
typedef DynamicAutoDiffCostFunction<StatConstraint, kStride>
StatCostFunction;
StatConstraint(Ronin* ronin_ptr)
: ronin_ptr(ronin_ptr) {}
template <typename T>
bool operator()(T const* const* biases, T* residuals) const {
double* mags = ronin_ptr->get_mags();
double* thetas = ronin_ptr->get_thetas();
int n_velocity = ronin_ptr->get_n_velocity();
T xstart=T(0.), ystart= T(0.);
vector<T> updated_x(n_velocity+1);
vector<T> updated_y(n_velocity+1);
updated_y[0] = ystart;
updated_x[0] = xstart;
for (int i=0;i<n_velocity;i++){
xstart += mags[i]*cos(thetas[i]+biases[i][0]);
ystart += mags[i]*sin(thetas[i]+biases[i][0]);
updated_x[i+1] = xstart;
updated_y[i+1] = ystart;
}
vector<Corres>& corres_pairs = ronin_ptr->get_corres_pairs();
VecInt& r_corres_idx = ronin_ptr->get_r_corres_idx();
size_t pair_size = r_corres_idx.size();
for (size_t k=0; k<pair_size; k++) {
int i = r_corres_idx[k];
int id1 = corres_pairs[i].id1;
int id2 = corres_pairs[i].id2;
double dist = corres_pairs[i].dist;
residuals[k] = (updated_x[id1] - updated_x[id2]);//*dist;
residuals[k+pair_size] = (updated_y[id1] - updated_y[id2]);//*dist;
}
return true;
}
static StatCostFunction* Create(Ronin* ronin_ptr,
vector<double*>* parameter_blocks) {
StatConstraint* constraint = new StatConstraint(ronin_ptr);
StatCostFunction* cost_function = new StatCostFunction(constraint);
// delete this new memory?
parameter_blocks->clear();
double* biases = ronin_ptr->get_biases();
int n_velocity = ronin_ptr->get_n_velocity();
for (int i=0; i<n_velocity; i++) {
parameter_blocks->push_back(&(biases[i]));
cost_function->AddParameterBlock(1);
}
size_t n_corres = ronin_ptr->get_r_corres_idx().size();
cost_function->SetNumResiduals(n_corres*2);
return (cost_function);
}
///////
int cluster_id;
Ronin* ronin_ptr;
};
struct BiasRegConstraint {
template <typename T>
bool operator()(const T* const b1, const T* const b2, T* residual) const {
//residual[0] = min(ceres::abs(b1[0]-b2[0]), T(2.*M_PI)-ceres::abs(b1[0]-b2[0]));
residual[0] = b1[0]-b2[0];
return true;
}
};
int main(int argc, char** argv) {
google::InitGoogleLogging(argv[0]);
srand(time(NULL));
string folder_id = argv[1];
string in_path = "/local-scratch/yimingq/wifi/stationary/outputs/"+folder_id+"/";
Ronin ronin;
string txtname = in_path + "c_mag_theta.txt";
ronin.read_mag_theta(txtname);
txtname = in_path + "c_single_corres.txt";
ronin.read_corres_data(txtname);
cout<<"finish reading"<<endl;
//MatInt& cluster_indices = ronin.get_cluster_indices();
double* biases = ronin.get_biases();
int n_velocity = ronin.get_n_velocity();
double* thetas = ronin.get_thetas();
int n_corres = ronin.get_n_corres();
// do ransac
if (n_corres>0){
int iter_num = 10;
int n_chosen = min(0.15*n_corres, 30.);
if (n_chosen<=1){
n_chosen=n_corres;
iter_num=1;
}
ronin.sample_corres_pairs(n_chosen);
double best_inratio = 0.;
for (int r=0;r<iter_num;r++){
cout<<"--------iteration: "<<r<<"--------"<<endl;
ronin.reset_bias();
// ronin.sample_corres_pairs(n_chosen);
Problem problem;
vector<double*> parameter_blocks;
StatConstraint::StatCostFunction* stat_cost_function =
StatConstraint::Create(&ronin, ¶meter_blocks);
problem.AddResidualBlock(stat_cost_function, new CauchyLoss(0.5), parameter_blocks);
for (int i=1; i<n_velocity; i++){
problem.AddResidualBlock(
new AutoDiffCostFunction<BiasRegConstraint, 1, 1, 1>(new BiasRegConstraint),
new CauchyLoss(0.5),
&(biases[i]),
&(biases[i-1]));
}
// for (int i=0; i<n_velocity; i++){
// problem.SetParameterLowerBound(&(biases[i]), 0, -M_PI-thetas[i]);
// problem.SetParameterUpperBound(&(biases[i]), 0, M_PI-thetas[i]);
// }
ceres::Solver::Options options;
//options.linear_solver_type = ceres::DENSE_QR;
//options.max_num_iterations = 30;
options.minimizer_progress_to_stdout = true;
ceres::Solver::Summary summary;
ceres::Solve(options, &problem, &summary);
cout << summary.FullReport() << "\n";
// check whetehr the best
//double inlier_ratio = ronin.comp_inlier_ratio();
if (ronin.comp_inlier_ratio(in_path)){
//best_inratio=inlier_ratio;
ronin.copy_bias();
break;
}
}
}
txtname = in_path + "c_single_corres_align_incremental.txt";
ronin.write_mag_theta(txtname);
cout<<"success!"<<endl;
return 0;
}