-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bundle.cc
652 lines (556 loc) · 19.2 KB
/
Bundle.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
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
// Copyright 2008 Isis Innovation Limited
// The bundle adjuster class implemented with stereo measurements
// measurements are passed as a four-vector and the camera pose is updated to
// minimise the reproejction error over all 4 parameters
#include "Bundle.h"
#include "MEstimator.h"
#include <TooN/helpers.h>
#include <TooN/Cholesky.h>
#include <fstream>
#include <iomanip>
#include <gvars3/instances.h>
using namespace GVars3;
using namespace std;
#ifdef WIN32
inline bool isnan(double d) {return !(d==d);}
#endif
#define cout if(*mgvnBundleCout) cout
// Requires 2nd arg to now be 4x6 matrix due to new meas structure
inline void BundleTriangle_UpdateM6U_LL(Matrix<6> &m6U, Matrix<4,6> &m46A)
{
for(int r=0; r<6; r++)
for(int c=0; c<=r; c++)
m6U(r,c)+= m46A.T()(r,0)*m46A(0,c) + m46A.T()(r,1)*m46A(1,c);
}
// requires second arg to be a 4x3 due to new meas structure
inline void BundleTriangle_UpdateM3V_LL(Matrix<3> &m3V, Matrix<4,3> &m43B)
{
for(int r=0; r<3; r++)
for(int c=0; c<=r; c++)
m3V(r,c)+= m43B.T()(r,0)*m43B(0,c) + m43B.T()(r,1)*m43B(1,c);
}
Bundle::Bundle(const ATANCamera &TCamL, const ATANCamera &TCamR)
: mCamera_Left(TCamL), mCamera_Right(TCamR)
{
mnCamsToUpdate = 0;
mnStartRow = 0;
GV3::Register(mgvnMaxIterations, "Bundle.MaxIterations", 20, SILENT);
GV3::Register(mgvdUpdateConvergenceLimit, "Bundle.UpdateSquaredConvergenceLimit", 1e-06, SILENT);
GV3::Register(mgvnBundleCout, "Bundle.Cout", 0, SILENT);
};
// Add a camera to the system, return value is the bundle adjuster's ID for the camera
// the camera pose if the tracker pose - left camera
int Bundle::AddCamera(SE3<> se3CamFromWorld, bool bFixed)
{
int n = mvCameras.size();
Camera c;
c.bFixed = bFixed;
c.se3CfW = se3CamFromWorld;
if(!bFixed)
{
c.nStartRow = mnStartRow;
mnStartRow += 6;
mnCamsToUpdate++;
}
else
c.nStartRow = -999999999;
mvCameras.push_back(c);
return n;
}
int Bundle::AddPoint(Vector<3> v3Pos)
{
int n = mvPoints.size();
Point p;
if(isnan(v3Pos * v3Pos))
{
cerr << " You sucker, tried to give me a nan " << v3Pos << endl;
v3Pos = Zeros;
}
p.v3Pos = v3Pos;
mvPoints.push_back(p);
return n;
}
// Add a measurement of one point with one camera - this is now a 4 vector
void Bundle::AddMeas(int nCam, int nPoint, Vector<4> v4Pos, double dSigmaSquared)
{
assert(nCam < (int) mvCameras.size());
assert(nPoint < (int) mvPoints.size());
mvPoints[nPoint].nMeasurements++;
mvPoints[nPoint].sCameras.insert(nCam);
Meas m;
m.p = nPoint;
m.c = nCam;
// set the measurement image parameters
m.v2FoundInLeft = v4Pos.slice(0,2);
m.v2FoundInRight = v4Pos.slice(2,2);
m.dSqrtInvNoise = sqrt(1.0 / dSigmaSquared);
mMeasList.push_back(m);
}
void Bundle::ClearAccumulators()
{
for(size_t i=0; i<mvPoints.size(); ++i)
{
mvPoints[i].m3V = Zeros;
mvPoints[i].v3EpsilonB = Zeros;
}
for(size_t i=0; i<mvCameras.size(); ++i)
{
mvCameras[i].m6U = Zeros;
mvCameras[i].v6EpsilonA = Zeros;
}
}
int Bundle::Compute(bool *pbAbortSignal)
{
mpbAbortSignal = pbAbortSignal;
GenerateMeasLUTs();
GenerateOffDiagScripts();
mdLambda = 0.0001;
mdLambdaFactor = 2.0;
mbConverged = false;
mbHitMaxIterations = false;
mnCounter = 0;
mnAccepted = 0;
static gvar3<string> gvsMEstimator("BundleMEstimator", "Tukey", SILENT);
while(!mbConverged && !mbHitMaxIterations && !*pbAbortSignal)
{
bool bNoError;
if(*gvsMEstimator == "Cauchy")
bNoError = Do_LM_Step<Cauchy>(pbAbortSignal);
else if(*gvsMEstimator == "Tukey")
bNoError = Do_LM_Step<Tukey>(pbAbortSignal);
else if(*gvsMEstimator == "Huber")
bNoError = Do_LM_Step<Huber>(pbAbortSignal);
else
{
cout << "Invalid BundleMEstimator selected !! " << endl;
cout << "Defaulting to Tukey." << endl;
*gvsMEstimator = "Tukey";
bNoError = Do_LM_Step<Tukey>(pbAbortSignal);
};
if(!bNoError)
return -1;
}
if(mbHitMaxIterations)
cout << " Hit max iterations." << endl;
cout << "Final Sigma Squared: " << mdSigmaSquared << " (= " << sqrt(mdSigmaSquared) / 4.685 << " pixels.)" << endl;
return mnAccepted;
};
// Reproject a single measurement, find error
// do this into the correct camera frame!
inline void Bundle::ProjectAndFindSquaredError(Meas &meas)
{
Camera &cam = mvCameras[meas.c];
Point &point = mvPoints[meas.p];
meas.v3Cam = cam.se3CfW * point.v3Pos;
if(meas.v3Cam[2] <= 0)
{
meas.bBad = true;
return;
}
meas.bBad = false;
// project to the left camera first
Vector<2> v2ImPlane = project(meas.v3Cam);
Vector<2> v2Image = mCamera_Left.Project(v2ImPlane);
// check the matrix is zero...
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
meas.m4CamDerivs(i,j) = 0;
// set the 4x4 of camera derivs with the left cam derivs
meas.m4CamDerivs.slice(0,0,2,2) = mCamera_Left.GetProjectionDerivs();
meas.v2EpsilonLeft = meas.dSqrtInvNoise * (meas.v2FoundInLeft - v2Image);
meas.dErrorSquared = meas.v2EpsilonLeft * meas.v2EpsilonLeft;
// perform the same proccess with the right camera
v2ImPlane = project(mCamera_Left.GetExtrinsic()*meas.v3Cam);
v2Image = mCamera_Right.Project(v2ImPlane);
meas.m4CamDerivs.slice(2,2,2,2) = mCamera_Right.GetProjectionDerivs();
meas.v2EpsilonRight = meas.dSqrtInvNoise * (meas.v2FoundInRight - v2Image);
meas.dErrorSquared += meas.v2EpsilonRight * meas.v2EpsilonRight;
// set the measurement error to the mean error between the measurements
meas.dErrorSquared /= 2;
}
template<class MEstimator>
bool Bundle::Do_LM_Step(bool *pbAbortSignal)
{
ClearAccumulators();
vector<double> vdErrorSquared;
for(list<Meas>::iterator itr = mMeasList.begin(); itr!=mMeasList.end(); itr++)
{
Meas &meas = *itr;
//finds error over both views
ProjectAndFindSquaredError(meas);
if(!meas.bBad)
vdErrorSquared.push_back(meas.dErrorSquared);
};
mdSigmaSquared = MEstimator::FindSigmaSquared(vdErrorSquared);
static gvar3<double> gvdMinSigma("Bundle.MinTukeySigma", 0.4, SILENT);
const double dMinSigmaSquared = *gvdMinSigma * *gvdMinSigma;
if(mdSigmaSquared < dMinSigmaSquared)
mdSigmaSquared = dMinSigmaSquared;
double dCurrentError = 0.0;
for(list<Meas>::iterator itr = mMeasList.begin(); itr!=mMeasList.end(); itr++)
{
Meas &meas = *itr;
Camera &cam = mvCameras[meas.c];
Point &point = mvPoints[meas.p];
if(meas.bBad)
{
dCurrentError += 1.0;
continue;
};
double dWeight= MEstimator::SquareRootWeight(meas.dErrorSquared, mdSigmaSquared);
// reweight both left and right measurements
meas.v2EpsilonLeft = dWeight * meas.v2EpsilonLeft;
meas.v2EpsilonRight = dWeight * meas.v2EpsilonRight;
if(dWeight == 0)
{
meas.bBad = true;
dCurrentError += 1.0;
continue;
}
dCurrentError += MEstimator::ObjectiveScore(meas.dErrorSquared, mdSigmaSquared);
//
//calculate the jacobian for camera pose over the 4 measurement values
//
//get the camera derivtives for both cameras
Matrix<4> m4CamDerivs = dWeight * meas.m4CamDerivs;
// get the point coords in left camera frame
const double dOneOverLeftCameraZ = 1.0 / meas.v3Cam[2];
const Vector<4> v4LeftCam = unproject(meas.v3Cam);
// get the point coods in right camera frame
const double dOneOverRightCameraZ = 1.0 / (mCamera_Left.GetExtrinsic()*meas.v3Cam)[2];
const Vector<4> v4RightCam = unproject(mCamera_Left.GetExtrinsic()*meas.v3Cam);
if(cam.bFixed)
meas.m46A = Zeros;
else
{
for(int m=0;m<6;m++)
{
// get the generators for both left and right cameras
const Vector<4> v4LeftMotion = SE3<>::generator_field(m, v4LeftCam);
const Vector<4> v4RightMotion = SE3<>::generator_field(m, v4RightCam);
// calculat teh derivative dLeft/d mu_{i}
Vector<4> v4CamFrameMotion;
v4CamFrameMotion[0] = (v4LeftMotion[0] - v4LeftCam[0] * v4LeftMotion[2] * dOneOverLeftCameraZ) * dOneOverLeftCameraZ;
v4CamFrameMotion[1] = (v4LeftMotion[1] - v4LeftCam[1] * v4LeftMotion[2] * dOneOverLeftCameraZ) * dOneOverLeftCameraZ;
// calculate the derivative dRight/d mu_{i}
Vector<2> v2RightCamFrameMotion;
v4CamFrameMotion[2] = (v4RightMotion[0] - v4RightCam[0] * v4RightMotion[2] * dOneOverRightCameraZ) * dOneOverRightCameraZ;
v4CamFrameMotion[3] = (v4RightMotion[1] - v4RightCam[1] * v4RightMotion[2] * dOneOverRightCameraZ) * dOneOverRightCameraZ;
meas.m46A.T()[m] = meas.dSqrtInvNoise * m4CamDerivs * v4CamFrameMotion;
};
}
// calculate the derivatives for the left view of the point
Matrix<3> m3LeftMotion =
cam.se3CfW.get_rotation().get_matrix();
// calculate the derivatives of right camera view of point
Matrix<3> m3RightMotion =
(mCamera_Left.Extrinsic * cam.se3CfW).get_rotation().get_matrix();
for(int m=0;m<3;m++)
{
// get the matrix rows
const Vector<3> v3LeftMotion = m3LeftMotion.T()[m];
const Vector<3> v3RightMotion = m3RightMotion.T()[m];
// derivs for left cam in each degree of (x,y,z) freedom
Vector<4> v4CamFrameMotion;
v4CamFrameMotion[0] = (v3LeftMotion[0] - v4LeftCam[0] * v3LeftMotion[2] * dOneOverLeftCameraZ) * dOneOverLeftCameraZ;
v4CamFrameMotion[1] = (v3LeftMotion[1] - v4LeftCam[1] * v3LeftMotion[2] * dOneOverLeftCameraZ) * dOneOverLeftCameraZ;
// derivs for right cam in (x,y,z) parmeters
v4CamFrameMotion[2] = (v3RightMotion[0]-v4RightCam[0] * v3RightMotion[2] * dOneOverRightCameraZ)*dOneOverRightCameraZ;
v4CamFrameMotion[3] = (v3RightMotion[1]-v4RightCam[1] * v3RightMotion[2] * dOneOverRightCameraZ)*dOneOverRightCameraZ;
// multiply each deriv by the respective camera projection derivs
meas.m43B.T()[m] = meas.dSqrtInvNoise * m4CamDerivs * v4CamFrameMotion;
};
// get the error vectors epsilon
Vector<4> v4Ep;
v4Ep.slice(0,2) = meas.v2EpsilonLeft;
v4Ep.slice(2,2) = meas.v2EpsilonRight;
if(!cam.bFixed)
{
// add the 4-vector measurement
BundleTriangle_UpdateM6U_LL(cam.m6U, meas.m46A);
cam.v6EpsilonA += meas.m46A.T() * v4Ep;
}
// add the 4-vector measurement
BundleTriangle_UpdateM3V_LL(point.m3V, meas.m43B);
// add the 4-vector error
point.v3EpsilonB += meas.m43B.T() * v4Ep;
if(cam.bFixed)
meas.m63W = Zeros;
else
meas.m63W = meas.m46A.T() * meas.m43B;
} // end for all measurements
double dNewError = dCurrentError + 9999;
while(dNewError > dCurrentError && !mbConverged && !mbHitMaxIterations && !*pbAbortSignal)
{
for(vector<Point>::iterator itr = mvPoints.begin(); itr!=mvPoints.end(); itr++)
{
Point &point = *itr;
Matrix<3> m3VStar = point.m3V;
if(m3VStar[0][0] * m3VStar[1][1] * m3VStar[2][2] == 0)
point.m3VStarInv = Zeros;
else
{
m3VStar[0][1] = m3VStar[1][0];
m3VStar[0][2] = m3VStar[2][0];
m3VStar[1][2] = m3VStar[2][1];
for(int i=0; i<3; i++)
m3VStar[i][i] *= (1.0 + mdLambda);
Cholesky<3> chol(m3VStar);
point.m3VStarInv = chol.get_inverse();
};
}
Matrix<> mS(mnCamsToUpdate * 6, mnCamsToUpdate * 6);
mS = Zeros;
Vector<> vE(mnCamsToUpdate * 6);
vE = Zeros;
Matrix<6> m6; // Temp working space
Vector<6> v6; // Temp working space
for(unsigned int j=0; j<mvCameras.size(); j++)
{
Camera &cam_j = mvCameras[j];
if(cam_j.bFixed) continue;
int nCamJStartRow = cam_j.nStartRow;
for(int r=0; r<6; r++)
{
for(int c=0; c<r; c++)
m6[r][c] = m6[c][r] = cam_j.m6U[r][c];
m6[r][r] = cam_j.m6U[r][r];
};
for(int nn = 0; nn< 6; nn++)
m6[nn][nn] *= (1.0 + mdLambda);
v6 = cam_j.v6EpsilonA;
vector<Meas*> &vMeasLUTj = mvMeasLUTs[j];
for(unsigned int i=0; i<mvPoints.size(); i++)
{
Meas* pMeas = vMeasLUTj[i];
if(pMeas == NULL || pMeas->bBad)
continue;
m6 -= pMeas->m63W * mvPoints[i].m3VStarInv * pMeas->m63W.T();
v6 -= pMeas->m63W * (mvPoints[i].m3VStarInv * mvPoints[i].v3EpsilonB);
}
mS.slice(nCamJStartRow, nCamJStartRow, 6, 6) = m6;
vE.slice(nCamJStartRow,6) = v6;
}
for(unsigned int i=0; i<mvPoints.size(); i++)
{
Point &p = mvPoints[i];
int nCurrentJ = -1;
int nJRow = -1;
Meas* pMeas_ij;
Meas* pMeas_ik;
Matrix<6,3> m63_MIJW_times_m3VStarInv;
for(vector<OffDiagScriptEntry>::iterator it=p.vOffDiagonalScript.begin();
it!=p.vOffDiagonalScript.end();
it++)
{
OffDiagScriptEntry &e = *it;
pMeas_ik = mvMeasLUTs[e.k][i];
if(pMeas_ik == NULL || pMeas_ik->bBad)
continue;
if(e.j != nCurrentJ)
{
pMeas_ij = mvMeasLUTs[e.j][i];
if(pMeas_ij == NULL || pMeas_ij->bBad)
continue;
nCurrentJ = e.j;
nJRow = mvCameras[e.j].nStartRow;
m63_MIJW_times_m3VStarInv = pMeas_ij->m63W * p.m3VStarInv;
}
int nKRow = mvCameras[pMeas_ik->c].nStartRow;
#ifndef WIN32
mS.slice(nJRow, nKRow, 6, 6) -= m63_MIJW_times_m3VStarInv * pMeas_ik->m63W.T();
#else
Matrix<6> m = mS.slice(nJRow, nKRow, 6, 6);
m -= m63_MIJW_times_m3VStarInv * pMeas_ik->m63W.T();
mS.slice(nJRow, nKRow, 6, 6) = m;
#endif
assert(nKRow < nJRow);
}
}
for(int i=0; i<mS.num_rows(); i++)
for(int j=0; j<i; j++)
mS[j][i] = mS[i][j];
Vector<> vCamerasUpdate(mS.num_rows());
vCamerasUpdate = Cholesky<>(mS).backsub(vE);
Vector<> vMapUpdates(mvPoints.size() * 3);
for(unsigned int i=0; i<mvPoints.size(); i++)
{
Vector<3> v3Sum;
v3Sum = Zeros;
for(unsigned int j=0; j<mvCameras.size(); j++)
{
Camera &cam = mvCameras[j];
if(cam.bFixed)
continue;
Meas *pMeas = mvMeasLUTs[j][i];
if(pMeas == NULL || pMeas->bBad)
continue;
v3Sum+=pMeas->m63W.T() * vCamerasUpdate.slice(cam.nStartRow,6);
}
Vector<3> v3 = mvPoints[i].v3EpsilonB - v3Sum;
vMapUpdates.slice(i * 3, 3) = mvPoints[i].m3VStarInv * v3;
if(isnan(vMapUpdates.slice(i * 3, 3) * vMapUpdates.slice(i * 3, 3)))
{
cerr << "NANNERY! " << endl;
cerr << mvPoints[i].m3VStarInv << endl;
};
}
double dSumSquaredUpdate = vCamerasUpdate * vCamerasUpdate + vMapUpdates * vMapUpdates;
if(dSumSquaredUpdate< *mgvdUpdateConvergenceLimit)
mbConverged = true;
for(unsigned int j=0; j<mvCameras.size(); j++)
{
if(mvCameras[j].bFixed)
mvCameras[j].se3CfWNew = mvCameras[j].se3CfW;
else
mvCameras[j].se3CfWNew = SE3<>::exp(vCamerasUpdate.slice(mvCameras[j].nStartRow, 6)) * mvCameras[j].se3CfW;
}
for(unsigned int i=0; i<mvPoints.size(); i++)
mvPoints[i].v3PosNew = mvPoints[i].v3Pos + vMapUpdates.slice(i*3, 3);
dNewError = FindNewError<MEstimator>();
cout <<setprecision(1) << "L" << mdLambda << setprecision(3) << "\tOld " << dCurrentError << " New " << dNewError << " Diff " << dCurrentError - dNewError << "\t";
// Was the step good? If not, modify lambda and try again!!
// (if it was good, will break from this loop.)
if(dNewError > dCurrentError)
{
cout << " TRY AGAIN " << endl;
ModifyLambda_BadStep();
};
mnCounter++;
if(mnCounter >= *mgvnMaxIterations)
mbHitMaxIterations = true;
} // End of while error too big loop
if(dNewError < dCurrentError) // Was the last step a good one?
{
cout << " WINNER ------------ " << endl;
// Woo! got somewhere. Update lambda and make changes permanent.
ModifyLambda_GoodStep();
for(unsigned int j=0; j<mvCameras.size(); j++)
mvCameras[j].se3CfW = mvCameras[j].se3CfWNew;
for(unsigned int i=0; i<mvPoints.size(); i++)
mvPoints[i].v3Pos = mvPoints[i].v3PosNew;
mnAccepted++;
}
// Finally, ditch all the outliers.
vector<list<Meas>::iterator> vit;
for(list<Meas>::iterator itr = mMeasList.begin(); itr!=mMeasList.end(); itr++)
if(itr->bBad)
{
vit.push_back(itr);
mvOutlierMeasurementIdx.push_back(make_pair(itr->p, itr->c));
mvPoints[itr->p].nOutliers++;
mvMeasLUTs[itr->c][itr->p] = NULL;
};
for(unsigned int i=0; i<vit.size(); i++)
mMeasList.erase(vit[i]);
cout << "Nuked " << vit.size() << " measurements." << endl;
return true;
}
// Find the new total error if cameras and points used their
// new coordinates- this is done with both camera new positions
template<class MEstimator>
double Bundle::FindNewError()
{
ofstream ofs;
double dNewError = 0;
vector<double> vdErrorSquared;
for(list<Meas>::iterator itr = mMeasList.begin(); itr!=mMeasList.end(); itr++)
{
Meas &meas = *itr;
SE3<> se3Cam = mvCameras[meas.c].se3CfWNew;
Vector<3> v3Cam = se3Cam * mvPoints[meas.p].v3PosNew;
if(v3Cam[2] <= 0)
{
dNewError += 1.0;
cout << ".";
continue;
};
// project to image plane of the two cameras
// and find the new projection error
Vector<2> v2ImPlane = project(v3Cam);
Vector<2> v2Image = mCamera_Left.Project(v2ImPlane);
Vector<2> v2Error = meas.dSqrtInvNoise * (meas.v2FoundInLeft - v2Image);
double dErrorSquared = v2Error * v2Error;
v2ImPlane = project(mCamera_Left.GetExtrinsic()*v3Cam);
v2Image = mCamera_Right.Project(v2ImPlane);
v2Error = meas.dSqrtInvNoise * (meas.v2FoundInRight - v2Image);
// average error over the two views
dErrorSquared += v2Error * v2Error;
dErrorSquared /= 2;
dNewError += MEstimator::ObjectiveScore(dErrorSquared, mdSigmaSquared);
}
return dNewError;
}
void Bundle::GenerateMeasLUTs()
{
mvMeasLUTs.clear();
for(unsigned int nCam = 0; nCam < mvCameras.size(); nCam++)
{
mvMeasLUTs.push_back(vector<Meas*>());
mvMeasLUTs.back().resize(mvPoints.size(), NULL);
};
for(list<Meas>::iterator it = mMeasList.begin(); it!=mMeasList.end(); it++)
mvMeasLUTs[it->c][it->p] = &(*it);
}
void Bundle::GenerateOffDiagScripts()
{
for(unsigned int i=0; i<mvPoints.size(); i++)
{
Point &p = mvPoints[i];
p.vOffDiagonalScript.clear();
for(set<int>::iterator it_j = p.sCameras.begin(); it_j!=p.sCameras.end(); it_j++)
{
int j = *it_j;
if(mvCameras[j].bFixed)
continue;
Meas *pMeas_j = mvMeasLUTs[j][i];
assert(pMeas_j != NULL);
for(set<int>::iterator it_k = p.sCameras.begin(); it_k!=it_j; it_k++)
{
int k = *it_k;
if(mvCameras[k].bFixed)
continue;
Meas *pMeas_k = mvMeasLUTs[k][i];
assert(pMeas_k != NULL);
OffDiagScriptEntry e;
e.j = j;
e.k = k;
p.vOffDiagonalScript.push_back(e);
}
}
}
}
void Bundle::ModifyLambda_GoodStep()
{
mdLambdaFactor = 2.0;
mdLambda *= 0.3;
};
void Bundle::ModifyLambda_BadStep()
{
mdLambda = mdLambda * mdLambdaFactor;
mdLambdaFactor = mdLambdaFactor * 2;
};
Vector<3> Bundle::GetPoint(int n)
{
return mvPoints.at(n).v3Pos;
}
SE3<> Bundle::GetCamera(int n)
{
return mvCameras.at(n).se3CfW;
}
set<int> Bundle::GetOutliers()
{
set<int> sOutliers;
set<int>::iterator hint = sOutliers.begin();
for(unsigned int i=0; i<mvPoints.size(); i++)
{
Point &p = mvPoints[i];
if(p.nMeasurements > 0 && p.nMeasurements == p.nOutliers)
hint = sOutliers.insert(hint, i);
}
return sOutliers;
};
vector<pair<int, int> > Bundle::GetOutlierMeasurements()
{
return mvOutlierMeasurementIdx;
}