-
Notifications
You must be signed in to change notification settings - Fork 20
/
ceres_vertigo.cpp
708 lines (565 loc) · 21 KB
/
ceres_vertigo.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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
using namespace std;
#include <ceres/ceres.h>
#include <Eigen/Dense>
/**
Sünderhauf, Niko, and Peter Protzel. "Switchable constraints for robust pose graph SLAM."
Intelligent Robots and Systems (IROS), 2012 IEEE/RSJ International Conference on. IEEE, 2012.
*/
#define ODOMETRY_EDGE 0
#define CLOSURE_EDGE 1
#define BOGUS_EDGE 2
// Class to represent Nodes
class Node
{
public:
/*Node()
{
}*/
Node(int index, double x, double y, double theta)
{
this->index = index;
p = new double[3];
p[0] = x;
p[1] = y;
p[2] = theta;
}
int index;
double *p;
};
// Class to represent Edges
class Edge
{
public:
// Type:
// 0 : Odometry edge
// 1 : Loop CLosure Edge
// 2 : Bogus Edge
Edge(const Node* a, const Node* b, int edge_type )
{
this->a = a;
this->b = b;
this->edge_type = edge_type;
}
void setEdgePose( double x, double y, double theta )
{
this->x = x;
this->y = y;
this->theta = theta;
}
void setInformationMatrix( double I11, double I12, double I13, double I22, double I23, double I33 )
{
this->I11 = I11;
this->I12 = I12;
this->I13 = I13;
this->I22 = I22;
this->I23 = I23;
this->I33 = I33;
}
const Node *a, *b;
double x, y, theta;
double I11, I12, I13, I22, I23, I33;
int edge_type;
};
class ReadG2O
{
public:
ReadG2O(const string& fName)
{
// Read the file in g2o format
fstream fp;
fp.open(fName.c_str(), ios::in);
string line;
int v = 0;
int e = 0;
while( std::getline(fp, line) )
{
vector<string> words;
boost::split(words, line, boost::is_any_of(" "), boost::token_compress_on);
if( words[0].compare( "VERTEX_SE2") == 0 )
{
v++;
int node_index = boost::lexical_cast<int>( words[1] );
double x = boost::lexical_cast<double>( words[2] );
double y = boost::lexical_cast<double>( words[3] );
double theta = boost::lexical_cast<double>( words[4] );
Node * node = new Node(node_index, x, y, theta);
nNodes.push_back( node );
}
if( words[0].compare( "EDGE_SE2") == 0 )
{
// cout << e << words[0] << endl;
int a_indx = boost::lexical_cast<int>( words[1] );
int b_indx = boost::lexical_cast<int>( words[2] );
double dx = boost::lexical_cast<double>( words[3] );
double dy = boost::lexical_cast<double>( words[4] );
double dtheta = boost::lexical_cast<double>( words[5] );
double I11, I12, I13, I22, I23, I33;
I11 = boost::lexical_cast<double>( words[6] );
I12 = boost::lexical_cast<double>( words[7] );
I13 = boost::lexical_cast<double>( words[8] );
I22 = boost::lexical_cast<double>( words[9] );
I23 = boost::lexical_cast<double>( words[10] );
I33 = boost::lexical_cast<double>( words[11] );
if( abs(a_indx - b_indx) < 5 )
{
Edge * edge = new Edge( nNodes[a_indx], nNodes[b_indx], ODOMETRY_EDGE );
edge->setEdgePose(dx, dy, dtheta);
edge->setInformationMatrix(I11, I12, I13, I22, I23, I33);
nEdgesOdometry.push_back(edge);
}
else
{
Edge * edge = new Edge( nNodes[a_indx], nNodes[b_indx], CLOSURE_EDGE );
edge->setEdgePose(dx, dy, dtheta);
edge->setInformationMatrix(I11, I12, I13, I22, I23, I33);
nEdgesClosure.push_back(edge);
}
e++;
}
}
}
// write nodes to file to be visualized with python script
void writePoseGraph_nodes( const string& fname )
{
cout << "writePoseGraph nodes: " << fname << endl;
fstream fp;
fp.open( fname.c_str(), ios::out );
for( int i=0 ; i<this->nNodes.size() ; i++ )
{
fp << nNodes[i]->index << " " << nNodes[i]->p[0] << " " << nNodes[i]->p[1] << " " << nNodes[i]->p[2] << endl;
}
}
void writePoseGraph_edges( const string& fname )
{
cout << "writePoseGraph Edges : "<< fname << endl;
fstream fp;
fp.open( fname.c_str(), ios::out );
write_edges( fp, this->nEdgesOdometry );
write_edges( fp, this->nEdgesClosure );
write_edges( fp, this->nEdgesBogus );
}
void writePoseGraph_switches( const string& fname, vector<double>& priors, vector<double*>& optimized )
{
cout << "#Closure Edges : "<< nEdgesClosure.size() << endl;
cout << "#Bogus Edges : "<< nEdgesBogus.size()<< endl;
cout << "#priors : "<< priors.size()<< endl;
cout << "#optimized " << optimized.size()<< endl;
fstream fp;
fp.open( fname.c_str(), ios::out );
fp << "Odometry EDGES AHEAD\n";
for( int i=0 ; i<nEdgesOdometry.size() ; i++ )
{
Edge * ed = nEdgesOdometry[i];
fp << ed->a->index << " " << ed->b->index << " " << ed->edge_type <<
" " << 1.0 << " " << 1.0 << endl;
}
fp << "Closure EDGES AHEAD\n";
for( int i=0 ; i<nEdgesClosure.size() ; i++ )
{
Edge * ed = nEdgesClosure[i];
fp << ed->a->index << " " << ed->b->index << " " << ed->edge_type <<
" " << priors[i] << " " << *(optimized[i]) << endl;
}
fp << "BOGUS EDGES AHEAD\n";
int ofset = nEdgesClosure.size();
for( int i=0 ; i<nEdgesBogus.size() ; i++ )
{
Edge * ed = nEdgesBogus[i];
fp << ed->a->index << " " << ed->b->index << " " << ed->edge_type <<
" " << priors[ofset+i] << " " << *(optimized[ofset+i]) << endl;
}
}
// Adding Bogus edges as described in Vertigo paper
void add_random_C(int count )
{
int MIN = 0 ;
int MAX = nNodes.size();
for( int i = 0 ; i<count ; i++ )
{
int a = rand() % MAX;
int b = rand() % MAX;
cout << a << "<--->" << b << endl;
Edge * edge = new Edge( nNodes[a], nNodes[b], BOGUS_EDGE );
edge->setEdgePose( rand()/RAND_MAX, rand()/RAND_MAX, rand()/RAND_MAX );
nEdgesBogus.push_back( edge );
}
}
//private:
vector<Node*> nNodes; //storage for node
vector<Edge*> nEdgesOdometry; //storage for edges - odometry
vector<Edge*> nEdgesClosure; //storage for edges - odometry
vector<Edge*> nEdgesBogus; //storage for edges - odometry
void write_edges( fstream& fp, vector<Edge*>& vec )
{
for( int i=0 ; i<vec.size() ; i++ )
{
// fp << nEdges[i]->a->index << " " << nEdges[i]->b->index << " " << (nEdges[i]->bogus_edge?1:0) << " " << nEdges[i]->switch_var[0]<< endl;
fp << vec[i]->a->index << " " << vec[i]->b->index << " " << vec[i]->edge_type << endl;
}
}
};
// Odometry Residue
struct OdometryResidue
{
// Observation for the edge
OdometryResidue(double dx, double dy, double dtheta)
{
this->dx = dx;
this->dy = dy;
this->dtheta = dtheta;
// make a_Tcap_b
{
double cos_t = cos( this->dtheta );
double sin_t = sin( this->dtheta );
a_Tcap_b(0,0) = cos_t;
a_Tcap_b(0,1) = -sin_t;
a_Tcap_b(1,0) = sin_t;
a_Tcap_b(1,1) = cos_t;
a_Tcap_b(0,2) = this->dx;
a_Tcap_b(1,2) = this->dy;
a_Tcap_b(2,0) = 0.0;
a_Tcap_b(2,1) = 0.0;
a_Tcap_b(2,2) = 1.0;
}
}
// Define the residue for each edge. P1 and P2 are 3-vectors representing state of the node ie. x,y,theta
template <typename T>
bool operator()(const T* const P1, const T* const P2, T* e) const
{
// Convert P1 to T1 ^w_T_a
Eigen::Matrix<T,3,3> w_T_a;
{
T cos_t = T(cos( P1[2] ));
T sin_t = T(sin( P1[2] ));
w_T_a(0,0) = cos_t;
w_T_a(0,1) = -sin_t;
w_T_a(1,0) = sin_t;
w_T_a(1,1) = cos_t;
w_T_a(0,2) = P1[0];
w_T_a(1,2) = P1[1];
w_T_a(2,0) = T(0.0);
w_T_a(2,1) = T(0.0);
w_T_a(2,2) = T(1.0);
}
// Convert P2 to T2 ^w_T_a
Eigen::Matrix<T,3,3> w_T_b;
{
T cos_t = cos( P2[2] );
T sin_t = sin( P2[2] );
w_T_b(0,0) = cos_t;
w_T_b(0,1) = -sin_t;
w_T_b(1,0) = sin_t;
w_T_b(1,1) = cos_t;
w_T_b(0,2) = P2[0];
w_T_b(1,2) = P2[1];
w_T_b(2,0) = T(0.0);
w_T_b(2,1) = T(0.0);
w_T_b(2,2) = T(1.0);
}
// cast from double to T
Eigen::Matrix<T, 3, 3> T_a_Tcap_b;
T_a_Tcap_b << T(a_Tcap_b(0,0)), T(a_Tcap_b(0,1)),T(a_Tcap_b(0,2)),
T(a_Tcap_b(1,0)), T(a_Tcap_b(1,1)),T(a_Tcap_b(1,2)),
T(a_Tcap_b(2,0)), T(a_Tcap_b(2,1)),T(a_Tcap_b(2,2));
// now we have :: w_T_a, w_T_b and a_Tcap_b
// compute pose difference
Eigen::Matrix<T,3,3> diff = T_a_Tcap_b.inverse() * (w_T_a.inverse() * w_T_b);
e[0] = diff(0,2);
e[1] = diff(1,2);
e[2] = asin( diff(1,0) );
return true;
}
double dx;
double dy;
double dtheta;
Eigen::Matrix<double,3,3> a_Tcap_b;
static ceres::CostFunction* Create(const double dx, const double dy, const double dtheta){
return (new ceres::AutoDiffCostFunction<OdometryResidue, 3, 3, 3>(
new OdometryResidue(dx, dy, dtheta)));
};
};
// Switchable Loop Closure Residue
struct SwitchableClosureResidue
{
// Observation for the edge
SwitchableClosureResidue(double dx, double dy, double dtheta, double s_prior )
{
this->dx = dx;
this->dy = dy;
this->dtheta = dtheta;
this->s_prior = s_prior;
// make a_Tcap_b
{
double cos_t = cos( this->dtheta );
double sin_t = sin( this->dtheta );
a_Tcap_b(0,0) = cos_t;
a_Tcap_b(0,1) = -sin_t;
a_Tcap_b(1,0) = sin_t;
a_Tcap_b(1,1) = cos_t;
a_Tcap_b(0,2) = this->dx;
a_Tcap_b(1,2) = this->dy;
a_Tcap_b(2,0) = 0.0;
a_Tcap_b(2,1) = 0.0;
a_Tcap_b(2,2) = 1.0;
}
}
// Define the residue for each edge. P1 and P2 are 3-vectors representing state of the node ie. x,y,theta
template <typename T>
bool operator()(const T* const P1, const T* const P2, const T* const s, T* e) const
{
// Convert P1 to T1 ^w_T_a
Eigen::Matrix<T,3,3> w_T_a;
{
T cos_t = T(cos( P1[2] ));
T sin_t = T(sin( P1[2] ));
w_T_a(0,0) = cos_t;
w_T_a(0,1) = -sin_t;
w_T_a(1,0) = sin_t;
w_T_a(1,1) = cos_t;
w_T_a(0,2) = P1[0];
w_T_a(1,2) = P1[1];
w_T_a(2,0) = T(0.0);
w_T_a(2,1) = T(0.0);
w_T_a(2,2) = T(1.0);
}
// Convert P2 to T2 ^w_T_a
Eigen::Matrix<T,3,3> w_T_b;
{
T cos_t = cos( P2[2] );
T sin_t = sin( P2[2] );
w_T_b(0,0) = cos_t;
w_T_b(0,1) = -sin_t;
w_T_b(1,0) = sin_t;
w_T_b(1,1) = cos_t;
w_T_b(0,2) = P2[0];
w_T_b(1,2) = P2[1];
w_T_b(2,0) = T(0.0);
w_T_b(2,1) = T(0.0);
w_T_b(2,2) = T(1.0);
}
// cast from double to T
Eigen::Matrix<T, 3, 3> T_a_Tcap_b;
T_a_Tcap_b << T(a_Tcap_b(0,0)), T(a_Tcap_b(0,1)),T(a_Tcap_b(0,2)),
T(a_Tcap_b(1,0)), T(a_Tcap_b(1,1)),T(a_Tcap_b(1,2)),
T(a_Tcap_b(2,0)), T(a_Tcap_b(2,1)),T(a_Tcap_b(2,2));
// now we have :: w_T_a, w_T_b and a_Tcap_b
// compute pose difference
Eigen::Matrix<T,3,3> diff = T_a_Tcap_b.inverse() * (w_T_a.inverse() * w_T_b);
// psi - scalar
// T psi = T(1.0) / (T(1.0) + exp( T(-2.0)*s[0] )); // zero-penalization factor 0.1
T psi = max( T(0.0), min( T(1.0), s[0] ) ); //zero-penalization factor 1.5. This needs aggresive zero penalization
e[0] = psi*diff(0,2);
e[1] = psi*diff(1,2);
e[2] = psi*asin( diff(1,0) );
e[3] = T(1.5) * (this->s_prior - s[0]);
return true;
}
double dx;
double dy;
double dtheta;
double s_prior;
Eigen::Matrix<double,3,3> a_Tcap_b;
static ceres::CostFunction* Create(const double dx, const double dy, const double dtheta, const double s_prior_obs){
return (new ceres::AutoDiffCostFunction<SwitchableClosureResidue, 4, 3, 3, 1>(
new SwitchableClosureResidue(dx, dy, dtheta, s_prior_obs)));
};
};
// Dynamic Covariance Scaling
struct DCSClosureResidue
{
// Observation for the edge
DCSClosureResidue(double dx, double dy, double dtheta )
{
this->dx = dx;
this->dy = dy;
this->dtheta = dtheta;
this->s_cap = drand48() * .1 + .9;
// make a_Tcap_b
{
double cos_t = cos( this->dtheta );
double sin_t = sin( this->dtheta );
a_Tcap_b(0,0) = cos_t;
a_Tcap_b(0,1) = -sin_t;
a_Tcap_b(1,0) = sin_t;
a_Tcap_b(1,1) = cos_t;
a_Tcap_b(0,2) = this->dx;
a_Tcap_b(1,2) = this->dy;
a_Tcap_b(2,0) = 0.0;
a_Tcap_b(2,1) = 0.0;
a_Tcap_b(2,2) = 1.0;
}
}
// Define the residue for each edge. P1 and P2 are 3-vectors representing state of the node ie. x,y,theta
template <typename T>
bool operator()(const T* const P1, const T* const P2, T* e) const
{
// Convert P1 to T1 ^w_T_a
Eigen::Matrix<T,3,3> w_T_a;
{
T cos_t = T(cos( P1[2] ));
T sin_t = T(sin( P1[2] ));
w_T_a(0,0) = cos_t;
w_T_a(0,1) = -sin_t;
w_T_a(1,0) = sin_t;
w_T_a(1,1) = cos_t;
w_T_a(0,2) = P1[0];
w_T_a(1,2) = P1[1];
w_T_a(2,0) = T(0.0);
w_T_a(2,1) = T(0.0);
w_T_a(2,2) = T(1.0);
}
// Convert P2 to T2 ^w_T_a
Eigen::Matrix<T,3,3> w_T_b;
{
T cos_t = cos( P2[2] );
T sin_t = sin( P2[2] );
w_T_b(0,0) = cos_t;
w_T_b(0,1) = -sin_t;
w_T_b(1,0) = sin_t;
w_T_b(1,1) = cos_t;
w_T_b(0,2) = P2[0];
w_T_b(1,2) = P2[1];
w_T_b(2,0) = T(0.0);
w_T_b(2,1) = T(0.0);
w_T_b(2,2) = T(1.0);
}
// cast from double to T
Eigen::Matrix<T, 3, 3> T_a_Tcap_b;
T_a_Tcap_b << T(a_Tcap_b(0,0)), T(a_Tcap_b(0,1)),T(a_Tcap_b(0,2)),
T(a_Tcap_b(1,0)), T(a_Tcap_b(1,1)),T(a_Tcap_b(1,2)),
T(a_Tcap_b(2,0)), T(a_Tcap_b(2,1)),T(a_Tcap_b(2,2));
// now we have :: w_T_a, w_T_b and a_Tcap_b
// compute pose difference
Eigen::Matrix<T,3,3> diff = T_a_Tcap_b.inverse() * (w_T_a.inverse() * w_T_b);
// psi - scalar (covariance term. See the paper on DCS for derivation)
// T psi = T(1.0) / (T(1.0) + exp( T(-2.0)*s[0] ));
// T psi = max( T(0.0), min( T(1.0), s[0] ) );
T res = diff(0,2)*diff(0,2) + diff(1,2)*diff(1,2); // + asin( diff(1,0) )*asin( diff(1,0) );
// T psi_org = T(.3) * T(s_cap) / ( T(1.0) + res ) ;
T psi_org = sqrt( T(2.0) * T( .5 ) / ( T(.5) + res ) );
// e[0] = psi ;
// e[1] = T(0.0);
// e[2] = T(0.0);
// return true;
T psi = min( T(1.0), psi_org ) ;
e[0] = psi*diff(0,2);
e[1] = psi*diff(1,2);
e[2] = psi*asin( diff(1,0) );
return true;
}
double dx;
double dy;
double dtheta;
double s_cap;
Eigen::Matrix<double,3,3> a_Tcap_b;
static ceres::CostFunction* Create(const double dx, const double dy, const double dtheta){
return (new ceres::AutoDiffCostFunction<DCSClosureResidue, 3, 3, 3>(
new DCSClosureResidue(dx, dy, dtheta)));
};
};
int main()
{
/////////////////////////////////////////////
// // // // // Read g2o file // // // // //
/////////////////////////////////////////////
std::string BASE_PATH = std::string( "../");
// string fname = "/home/mpkuse/catkin_ws/src/nap/slam_data/input_M3500_g2o.g2o";
string fname = BASE_PATH + "/input_M3500_g2o.g2o";
cout << "Start Reading PoseGraph\n";
ReadG2O g( fname );
g.add_random_C(25);
g.writePoseGraph_nodes(BASE_PATH+"/init_nodes.txt");
g.writePoseGraph_edges(BASE_PATH+"/init_edges.txt");
cout << "total nodes : "<< g.nNodes.size() << endl;
cout << "total nEdgesOdometry : "<< g.nEdgesOdometry.size() << endl;
cout << "total nEdgesClosure : "<< g.nEdgesClosure.size() << endl;
cout << "total nEdgesBogus : "<< g.nEdgesBogus.size() << endl;
////////////////////////////////////////////////////
// // // // // Make the cost function // // // // //
////////////////////////////////////////////////////
ceres::Problem problem;
ceres::LossFunction * loss_function = NULL;
loss_function = new ceres::HuberLoss(0.01);
// A - Odometry Constraints
for( int i=0 ; i<g.nEdgesOdometry.size() ; i++ )
{
Edge* ed = g.nEdgesOdometry[i];
ceres::CostFunction * cost_function = OdometryResidue::Create( ed->x, ed->y, ed->theta );
problem.AddResidualBlock( cost_function, loss_function, ed->a->p, ed->b->p );
// cout << ed->a->index << "---> " << ed->b->index << endl;
}
// B - Loop Closure Constaints (switchable)
#if 0
vector<double*> switches;
vector<double> switches_priors;
for( int i=0 ; i<g.nEdgesClosure.size() ; i++ )
{
double switch_prior = .5 + drand48() / 10.0;
// cout << "switch_prior : "<< switch_prior << endl;
// switches.push_back( new double[1] );
double * switch_opt_var = new double[1]; //optimizable
switches.push_back(switch_opt_var);
switches_priors.push_back(switch_prior);
Edge* ed = g.nEdgesClosure[i];
ceres::CostFunction * cost_function = SwitchableClosureResidue::Create( ed->x, ed->y, ed->theta, switch_prior );
problem.AddResidualBlock( cost_function, loss_function, ed->a->p, ed->b->p, switch_opt_var );
// cout << ed->a->index << "---> " << ed->b->index << endl;
}
for( int i=0 ; i<g.nEdgesBogus.size() ; i++ )
{
double switch_prior = .5 + drand48() / 10.0;
// cout << "switch_prior : "<< switch_prior << endl;
// switches.push_back( new double[1] );
double * switch_opt_var = new double[1]; //optimizable
switches.push_back(switch_opt_var);
switches_priors.push_back(switch_prior);
Edge* ed = g.nEdgesBogus[i];
ceres::CostFunction * cost_function = SwitchableClosureResidue::Create( ed->x, ed->y, ed->theta, switch_prior );
problem.AddResidualBlock( cost_function, loss_function, ed->a->p, ed->b->p, switch_opt_var );
// cout << ed->a->index << "---> " << ed->b->index << endl;
}
#endif // end of Switchable constrained edges
#if 1
// Loop closure constraints (dynamic covariance scaling)
for( int i=0 ; i<g.nEdgesClosure.size() ; i++ )
{
Edge* ed = g.nEdgesClosure[i];
ceres::CostFunction * cost_function = DCSClosureResidue::Create( ed->x, ed->y, ed->theta );
problem.AddResidualBlock( cost_function, loss_function, ed->a->p, ed->b->p );
// cout << ed->a->index << "---> " << ed->b->index << endl;
}
for( int i=0 ; i<g.nEdgesBogus.size() ; i++ )
{
Edge* ed = g.nEdgesBogus[i];
ceres::CostFunction * cost_function = DCSClosureResidue::Create( ed->x, ed->y, ed->theta );
problem.AddResidualBlock( cost_function, loss_function, ed->a->p, ed->b->p );
// cout << ed->a->index << "---> " << ed->b->index << endl;
}
#endif
///////////////////////////////////////////////
// // // // // Iteratively Solve // // // // //
//////////////////////////////////////////////
problem.SetParameterBlockConstant(g.nNodes[0]->p); //1st pose be origin
ceres::Solver::Options options;
options.minimizer_progress_to_stdout = true;
// options.linear_solver_type = ceres::SPARSE_SCHUR;
// options.trust_region_strategy_type = ceres::DOGLEG;
// options.dogleg_type = ceres::SUBSPACE_DOGLEG;
options.linear_solver_type = ceres::SPARSE_NORMAL_CHOLESKY;
// options.linear_solver_type = ceres::ITERATIVE_SCHUR;
// options.preconditioner_type = ceres::SCHUR_JACOBI;
ceres::Solver::Summary summary;
ceres::Solve(options, &problem, &summary);
cout << summary.FullReport() << endl;
// Write Pose Graph after Optimization
g.writePoseGraph_nodes(BASE_PATH+"/after_opt_nodes.txt");
g.writePoseGraph_edges(BASE_PATH+"/after_opt_edges.txt");
// g.writePoseGraph_switches(BASE_PATH+"/switches.txt", switches_priors, switches);
}