ORBSLAM2代码阅读-头文件- LoopClosing.h

 

 

源码

/**
* This file is part of ORB-SLAM2.
*
* Copyright (C) 2014-2016 Raúl Mur-Artal <raulmur at unizar dot es> (University of Zaragoza)
* For more information see <https://github.com/raulmur/ORB_SLAM2>
*
* ORB-SLAM2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ORB-SLAM2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ORB-SLAM2. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef LOOPCLOSING_H
#define LOOPCLOSING_H

#include "KeyFrame.h"
#include "LocalMapping.h"
#include "Map.h"
#include "ORBVocabulary.h"
#include "Tracking.h"

#include "KeyFrameDatabase.h"

#include <thread>
#include <mutex>
#include "Thirdparty/g2o/g2o/types/types_seven_dof_expmap.h"

namespace ORB_SLAM2
{

class Tracking;
class LocalMapping;
class KeyFrameDatabase;


class LoopClosing
{
public:

    typedef pair<set<KeyFrame*>,int> ConsistentGroup;    //pair<set<KeyFrame*>,int>
    typedef map<KeyFrame*,g2o::Sim3,std::less<KeyFrame*>,
        Eigen::aligned_allocator<std::pair<const KeyFrame*, g2o::Sim3> > > KeyFrameAndPose;

public:

    LoopClosing(Map* pMap, KeyFrameDatabase* pDB, ORBVocabulary* pVoc,const bool bFixScale);

    void SetTracker(Tracking* pTracker);

    void SetLocalMapper(LocalMapping* pLocalMapper);

    // Main function
    void Run();

    void InsertKeyFrame(KeyFrame *pKF);

    void RequestReset();

    // This function will run in a separate thread
    void RunGlobalBundleAdjustment(unsigned long nLoopKF);

    bool isRunningGBA(){
        unique_lock<std::mutex> lock(mMutexGBA);
        return mbRunningGBA;
    }
    bool isFinishedGBA(){
        unique_lock<std::mutex> lock(mMutexGBA);
        return mbFinishedGBA;
    }   

    void RequestFinish();

    bool isFinished();

    EIGEN_MAKE_ALIGNED_OPERATOR_NEW

protected:

    bool CheckNewKeyFrames();

    bool DetectLoop();

    bool ComputeSim3();

    void SearchAndFuse(const KeyFrameAndPose &CorrectedPosesMap);

    void CorrectLoop();

    void ResetIfRequested();
    bool mbResetRequested;
    std::mutex mMutexReset;

    bool CheckFinish();
    void SetFinish();
    bool mbFinishRequested;
    bool mbFinished;
    std::mutex mMutexFinish;

    Map* mpMap;
    Tracking* mpTracker;

    KeyFrameDatabase* mpKeyFrameDB;
    ORBVocabulary* mpORBVocabulary;

    LocalMapping *mpLocalMapper;

    std::list<KeyFrame*> mlpLoopKeyFrameQueue;

    std::mutex mMutexLoopQueue;

    // Loop detector parameters
    float mnCovisibilityConsistencyTh;

    // Loop detector variables
    KeyFrame* mpCurrentKF;
    KeyFrame* mpMatchedKF;
    std::vector<ConsistentGroup> mvConsistentGroups;
    std::vector<KeyFrame*> mvpEnoughConsistentCandidates;
    std::vector<KeyFrame*> mvpCurrentConnectedKFs;
    std::vector<MapPoint*> mvpCurrentMatchedPoints;
    std::vector<MapPoint*> mvpLoopMapPoints;
    cv::Mat mScw;
    g2o::Sim3 mg2oScw;

    long unsigned int mLastLoopKFid;

    // Variables related to Global Bundle Adjustment
    bool mbRunningGBA;
    bool mbFinishedGBA;
    bool mbStopGBA;
    std::mutex mMutexGBA;
    std::thread* mpThreadGBA;

    // Fix scale in the stereo/RGB-D case
    bool mbFixScale;


    bool mnFullBAIdx;
};

} //namespace ORB_SLAM

#endif // LOOPCLOSING_H
View Code

结构

loopclosing类主要定义了一下loopclosing.cpp中用到的各种变量和函数,并且通过下面这行代码创建了线程

std::thread* mpThreadGBA;

定义了下面这些比较重要的变量

Map* mpMap;
Tracking* mpTracker;
KeyFrameDatabase* mpKeyFrameDB;
ORBVocabulary* mpORBVocabulary;
LocalMapping *mpLocalMapper;
KeyFrame* mpCurrentKF;
KeyFrame* mpMatchedKF;
std::list<KeyFrame*> mlpLoopKeyFrameQueue;

 

 

c++知识

1.Ifndef
#ifndef <标识> 
#define <标识> 
......
#endif
#ifndef是宏定义里面的一种,用来防止防止头文件的重复包含和编译。比如有两个C文件,这两个C文件都include了同一个头文件。而编译时,这两个C文件要一同编译成一个可运行文件,于是问题来了,大量的声明冲突。如果把头文件的内容都放在#ifndef和#endif中,就可以避免冲突的问题;不管的头文件会不会被多个文件引用,都要加上这个。

2. map的用法
map<key,value>
key, 关键字,每个关键字只能在map中出现一次;
Value,称为该关键字的值(value);
map以模板(泛型)方式实现,可以存储任意类型的数据,包括使用者自定义的数据类型。
Map主要用于资料一对一映射(one-to-one)的情況,map內部的实现自建一颗红黑树,这颗树具有对数据自动排序的功能。
在map内部所有的数据都是有序的,比如一个班级中,每个学生的学号跟他的姓名就存在著一对一映射的关系。

3.pair 用法
pair是将2个数据组合成一个数据,当需要这样的需求时就可以使用pair。
在创建pair对象时,必须提供两个类型名,两个对应的类型名的类型可自由定义。
pair<string, string> ob;
参考:https://www.cnblogs.com/Sunrises/p/10363394.html
4.set 用法
set作为一个容器也是用来存储同一数据类型的数据类型,并且能从一个数据集合中取出数据。
在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序。
应该注意的是set中数元素的值不能直接被改变。
1、set中的元素都是排好序的
2、set集合中没有重复的元素
set<int> s;
s.insert(1);
参考:https:
//blog.csdn.net/byn12345/article/details/79523516

5.  std::mutex
std::mutex是C++11中最基本的互斥量,std::mutex对象提供了独占所有权的特性,不支持递归地对std::mutex对象上锁。
主要用于多个线程之间防止相互干扰
参考: https://blog.csdn.net/fengbingchun/article/details/73521630

6.创建线程
参考:https://blog.csdn.net/fengbingchun/article/details/73521630

7.std::list<KeyFrame*> c
创建一个list容器c,元素类型为KeyFrame

 

posted @ 2020-06-12 22:00  九里九里  阅读(379)  评论(0)    收藏  举报