32 mbMonocular(bMonocular), mbResetRequested(false), mbFinishRequested(false), mbFinished(true), mpMap(pMap),
33 mbAbortBA(false), mbStopped(false), mbStopRequested(false), mbNotStop(false), mbAcceptKeyFrames(true)
94 std::this_thread::sleep_for(std::chrono::microseconds(3000));
108 std::this_thread::sleep_for(std::chrono::microseconds(3000));
142 for(
size_t i=0; i<vpMapPointMatches.size(); i++)
144 MapPoint* pMP = vpMapPointMatches[i];
181 const int cnThObs = nThObs;
200 else if(((
int)nCurrentKFid-(
int)pMP->
mnFirstKFid)>=3)
218 cv::Mat Rwc1 = Rcw1.t();
220 cv::Mat Tcw1(3,4,CV_32F);
221 Rcw1.copyTo(Tcw1.colRange(0,3));
222 tcw1.copyTo(Tcw1.col(3));
237 for(
size_t i=0; i<vpNeighKFs.size(); i++)
246 cv::Mat vBaseline = Ow2-Ow1;
247 const float baseline = cv::norm(vBaseline);
251 if(baseline<pKF2->mb)
257 const float ratioBaselineDepth = baseline/medianDepthKF2;
259 if(ratioBaselineDepth<0.01)
267 vector<pair<size_t,size_t> > vMatchedIndices;
271 cv::Mat Rwc2 = Rcw2.t();
273 cv::Mat Tcw2(3,4,CV_32F);
274 Rcw2.copyTo(Tcw2.colRange(0,3));
275 tcw2.copyTo(Tcw2.col(3));
277 const float &fx2 = pKF2->
fx;
278 const float &fy2 = pKF2->
fy;
279 const float &cx2 = pKF2->
cx;
280 const float &cy2 = pKF2->
cy;
281 const float &invfx2 = pKF2->
invfx;
282 const float &invfy2 = pKF2->
invfy;
285 const int nmatches = vMatchedIndices.size();
286 for(
int ikp=0; ikp<nmatches; ikp++)
288 const int &idx1 = vMatchedIndices[ikp].first;
289 const int &idx2 = vMatchedIndices[ikp].second;
293 bool bStereo1 = kp1_ur>=0;
295 const cv::KeyPoint &kp2 = pKF2->
mvKeysUn[idx2];
296 const float kp2_ur = pKF2->
mvuRight[idx2];
297 bool bStereo2 = kp2_ur>=0;
300 cv::Mat xn1 = (cv::Mat_<float>(3,1) << (kp1.pt.x-cx1)*invfx1, (kp1.pt.y-cy1)*invfy1, 1.0);
301 cv::Mat xn2 = (cv::Mat_<float>(3,1) << (kp2.pt.x-cx2)*invfx2, (kp2.pt.y-cy2)*invfy2, 1.0);
303 cv::Mat ray1 = Rwc1*xn1;
304 cv::Mat ray2 = Rwc2*xn2;
305 const float cosParallaxRays = ray1.dot(ray2)/(cv::norm(ray1)*cv::norm(ray2));
307 float cosParallaxStereo = cosParallaxRays+1;
308 float cosParallaxStereo1 = cosParallaxStereo;
309 float cosParallaxStereo2 = cosParallaxStereo;
314 cosParallaxStereo2 = cos(2*atan2(pKF2->
mb/2,pKF2->
mvDepth[idx2]));
316 cosParallaxStereo =
min(cosParallaxStereo1,cosParallaxStereo2);
319 if(cosParallaxRays<cosParallaxStereo && cosParallaxRays>0 && (bStereo1 || bStereo2 || cosParallaxRays<0.9998))
322 cv::Mat A(4,4,CV_32F);
323 A.row(0) = xn1.at<
float>(0)*Tcw1.row(2)-Tcw1.row(0);
324 A.row(1) = xn1.at<
float>(1)*Tcw1.row(2)-Tcw1.row(1);
325 A.row(2) = xn2.at<
float>(0)*Tcw2.row(2)-Tcw2.row(0);
326 A.row(3) = xn2.at<
float>(1)*Tcw2.row(2)-Tcw2.row(1);
329 cv::SVD::compute(A,w,u,vt,cv::SVD::MODIFY_A| cv::SVD::FULL_UV);
333 if(x3D.at<
float>(3)==0)
337 x3D = x3D.rowRange(0,3)/x3D.at<
float>(3);
340 else if(bStereo1 && cosParallaxStereo1<cosParallaxStereo2)
344 else if(bStereo2 && cosParallaxStereo2<cosParallaxStereo1)
351 cv::Mat x3Dt = x3D.t();
354 float z1 = Rcw1.row(2).dot(x3Dt)+tcw1.at<
float>(2);
358 float z2 = Rcw2.row(2).dot(x3Dt)+tcw2.at<
float>(2);
364 const float x1 = Rcw1.row(0).dot(x3Dt)+tcw1.at<
float>(0);
365 const float y1 = Rcw1.row(1).dot(x3Dt)+tcw1.at<
float>(1);
366 const float invz1 = 1.0/z1;
370 float u1 = fx1*x1*invz1+cx1;
371 float v1 = fy1*y1*invz1+cy1;
372 float errX1 = u1 - kp1.pt.x;
373 float errY1 = v1 - kp1.pt.y;
374 if((errX1*errX1+errY1*errY1)>5.991*sigmaSquare1)
379 float u1 = fx1*x1*invz1+cx1;
381 float v1 = fy1*y1*invz1+cy1;
382 float errX1 = u1 - kp1.pt.x;
383 float errY1 = v1 - kp1.pt.y;
384 float errX1_r = u1_r - kp1_ur;
385 if((errX1*errX1+errY1*errY1+errX1_r*errX1_r)>7.8*sigmaSquare1)
391 const float x2 = Rcw2.row(0).dot(x3Dt)+tcw2.at<
float>(0);
392 const float y2 = Rcw2.row(1).dot(x3Dt)+tcw2.at<
float>(1);
393 const float invz2 = 1.0/z2;
396 float u2 = fx2*x2*invz2+cx2;
397 float v2 = fy2*y2*invz2+cy2;
398 float errX2 = u2 - kp2.pt.x;
399 float errY2 = v2 - kp2.pt.y;
400 if((errX2*errX2+errY2*errY2)>5.991*sigmaSquare2)
405 float u2 = fx2*x2*invz2+cx2;
407 float v2 = fy2*y2*invz2+cy2;
408 float errX2 = u2 - kp2.pt.x;
409 float errY2 = v2 - kp2.pt.y;
410 float errX2_r = u2_r - kp2_ur;
411 if((errX2*errX2+errY2*errY2+errX2_r*errX2_r)>7.8*sigmaSquare2)
416 cv::Mat normal1 = x3D-Ow1;
417 float dist1 = cv::norm(normal1);
419 cv::Mat normal2 = x3D-Ow2;
420 float dist2 = cv::norm(normal2);
422 if(dist1==0 || dist2==0)
425 const float ratioDist = dist2/dist1;
430 if(ratioDist*ratioFactor<ratioOctave || ratioDist>ratioOctave*ratioFactor)
461 vector<KeyFrame*> vpTargetKFs;
462 for(vector<KeyFrame*>::const_iterator vit=vpNeighKFs.begin(), vend=vpNeighKFs.end(); vit!=vend; vit++)
467 vpTargetKFs.push_back(pKFi);
472 for(vector<KeyFrame*>::const_iterator vit2=vpSecondNeighKFs.begin(), vend2=vpSecondNeighKFs.end(); vit2!=vend2; vit2++)
477 vpTargetKFs.push_back(pKFi2);
485 for(vector<KeyFrame*>::iterator vit=vpTargetKFs.begin(), vend=vpTargetKFs.end(); vit!=vend; vit++)
489 matcher.
Fuse(pKFi,vpMapPointMatches);
493 vector<MapPoint*> vpFuseCandidates;
494 vpFuseCandidates.reserve(vpTargetKFs.size()*vpMapPointMatches.size());
496 for(vector<KeyFrame*>::iterator vitKF=vpTargetKFs.begin(), vendKF=vpTargetKFs.end(); vitKF!=vendKF; vitKF++)
502 for(vector<MapPoint*>::iterator vitMP=vpMapPointsKFi.begin(), vendMP=vpMapPointsKFi.end(); vitMP!=vendMP; vitMP++)
510 vpFuseCandidates.push_back(pMP);
519 for(
size_t i=0, iend=vpMapPointMatches.size(); i<iend; i++)
543 cv::Mat R12 = R1w*R2w.t();
544 cv::Mat t12 = -R1w*R2w.t()*t2w+t1w;
548 const cv::Mat &K1 = pKF1->
mK;
549 const cv::Mat &K2 = pKF2->
mK;
552 return K1.t().inv()*t12x*R12*K2.inv();
569 cout <<
"Local Mapping STOP" << endl;
600 cout <<
"Local Mapping RELEASE" << endl;
640 for(vector<KeyFrame*>::iterator vit=vpLocalKeyFrames.begin(), vend=vpLocalKeyFrames.end(); vit!=vend; vit++)
648 const int thObs=nObs;
649 int nRedundantObservations=0;
651 for(
size_t i=0, iend=vpMapPoints.size(); i<iend; i++)
667 const int &scaleLevel = pKF->
mvKeysUn[i].octave;
670 for(map<KeyFrame*, size_t>::const_iterator mit=observations.begin(), mend=observations.end(); mit!=mend; mit++)
675 const int &scaleLeveli = pKFi->
mvKeysUn[mit->second].octave;
677 if(scaleLeveli<=scaleLevel+1)
686 nRedundantObservations++;
693 if(nRedundantObservations>0.9*nMPs)
700 return (cv::Mat_<float>(3,3) << 0, -v.at<
float>(2), v.at<
float>(1),
701 v.at<
float>(2), 0,-v.at<
float>(0),
702 -v.at<
float>(1), v.at<
float>(0), 0);
719 std::this_thread::sleep_for(std::chrono::microseconds(3000));
std::vector< KeyFrame * > GetBestCovisibilityKeyFrames(const int &N)
const std::vector< cv::KeyPoint > mvKeysUn
KeyFrame * mpCurrentKeyFrame
cv::Mat GetCameraCenter()
void CreateNewMapPoints()
const std::vector< float > mvScaleFactors
void SetTracker(Tracking *pTracker)
const float mfScaleFactor
void AddMapPoint(MapPoint *pMP, const size_t &idx)
void InsertKeyFrame(KeyFrame *pKF)
cv::Mat UnprojectStereo(int i)
const std::vector< float > mvLevelSigma2
int SearchForTriangulation(KeyFrame *pKF1, KeyFrame *pKF2, cv::Mat F12, std::vector< pair< size_t, size_t > > &vMatchedPairs, const bool bOnlyStereo)
std::vector< MapPoint * > GetMapPointMatches()
bool SetNotStop(bool flag)
void AddObservation(KeyFrame *pKF, size_t idx)
cv::Mat ComputeF12(KeyFrame *&pKF1, KeyFrame *&pKF2)
void UpdateNormalAndDepth()
LocalMapping(Map *pMap, const float bMonocular)
void AddMapPoint(MapPoint *pMP)
cv::Mat SkewSymmetricMatrix(const cv::Mat &v)
std::map< KeyFrame *, size_t > GetObservations()
void ProcessNewKeyFrame()
void InsertKeyFrame(KeyFrame *pKF)
long unsigned KeyFramesInMap()
const std::vector< float > mvDepth
long unsigned int mnFuseTargetForKF
LoopClosing * mpLoopCloser
void SetLoopCloser(LoopClosing *pLoopCloser)
float ComputeSceneMedianDepth(const int q)
std::vector< KeyFrame * > GetVectorCovisibleKeyFrames()
void SetAcceptKeyFrames(bool flag)
static void LocalBundleAdjustment(KeyFrame *pKF, bool *pbStopFlag, Map *pMap)
TFSIMD_FORCE_INLINE const tfScalar & w() const
const std::vector< float > mvuRight
void AddKeyFrame(KeyFrame *pKF)
void ComputeDistinctiveDescriptors()
std::list< MapPoint * > mlpRecentAddedMapPoints
bool IsInKeyFrame(KeyFrame *pKF)
long unsigned int mnFuseCandidateForKF
std::list< KeyFrame * > mlNewKeyFrames
int Fuse(KeyFrame *pKF, const vector< MapPoint * > &vpMapPoints, const float th=3.0)