LoopClosing.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**
  2. * This file is part of ORB-SLAM3
  3. *
  4. * Copyright (C) 2017-2021 Carlos Campos, Richard Elvira, Juan J. Gómez Rodríguez, José M.M. Montiel and Juan D. Tardós, University of Zaragoza.
  5. * Copyright (C) 2014-2016 Raúl Mur-Artal, José M.M. Montiel and Juan D. Tardós, University of Zaragoza.
  6. *
  7. * ORB-SLAM3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * ORB-SLAM3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  12. * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with ORB-SLAM3.
  16. * If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef LOOPCLOSING_H
  19. #define LOOPCLOSING_H
  20. #include "KeyFrame.h"
  21. #include "LocalMapping.h"
  22. #include "Atlas.h"
  23. #include "ORBVocabulary.h"
  24. #include "Tracking.h"
  25. #include "KeyFrameDatabase.h"
  26. #include <boost/algorithm/string.hpp>
  27. #include <thread>
  28. #include <mutex>
  29. #include "Thirdparty/g2o/g2o/types/types_seven_dof_expmap.h"
  30. namespace ORB_SLAM3
  31. {
  32. class Tracking;
  33. class LocalMapping;
  34. class KeyFrameDatabase;
  35. class Map;
  36. class LoopClosing
  37. {
  38. public:
  39. typedef pair<set<KeyFrame*>,int> ConsistentGroup;
  40. typedef map<KeyFrame*,g2o::Sim3,std::less<KeyFrame*>,
  41. Eigen::aligned_allocator<std::pair<KeyFrame* const, g2o::Sim3> > > KeyFrameAndPose;
  42. public:
  43. LoopClosing(Atlas* pAtlas, KeyFrameDatabase* pDB, ORBVocabulary* pVoc,const bool bFixScale, const bool bActiveLC);
  44. void SetTracker(Tracking* pTracker);
  45. void SetLocalMapper(LocalMapping* pLocalMapper);
  46. // Main function
  47. void Run();
  48. void InsertKeyFrame(KeyFrame *pKF);
  49. void RequestReset();
  50. void RequestResetActiveMap(Map* pMap);
  51. // This function will run in a separate thread
  52. void RunGlobalBundleAdjustment(Map* pActiveMap, unsigned long nLoopKF);
  53. bool isRunningGBA(){
  54. unique_lock<std::mutex> lock(mMutexGBA);
  55. return mbRunningGBA;
  56. }
  57. bool isFinishedGBA(){
  58. unique_lock<std::mutex> lock(mMutexGBA);
  59. return mbFinishedGBA;
  60. }
  61. void RequestFinish();
  62. bool isFinished();
  63. Viewer* mpViewer;
  64. #ifdef REGISTER_TIMES
  65. vector<double> vdDataQuery_ms;
  66. vector<double> vdEstSim3_ms;
  67. vector<double> vdPRTotal_ms;
  68. vector<double> vdMergeMaps_ms;
  69. vector<double> vdWeldingBA_ms;
  70. vector<double> vdMergeOptEss_ms;
  71. vector<double> vdMergeTotal_ms;
  72. vector<int> vnMergeKFs;
  73. vector<int> vnMergeMPs;
  74. int nMerges;
  75. vector<double> vdLoopFusion_ms;
  76. vector<double> vdLoopOptEss_ms;
  77. vector<double> vdLoopTotal_ms;
  78. vector<int> vnLoopKFs;
  79. int nLoop;
  80. vector<double> vdGBA_ms;
  81. vector<double> vdUpdateMap_ms;
  82. vector<double> vdFGBATotal_ms;
  83. vector<int> vnGBAKFs;
  84. vector<int> vnGBAMPs;
  85. int nFGBA_exec;
  86. int nFGBA_abort;
  87. #endif
  88. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  89. protected:
  90. bool CheckNewKeyFrames();
  91. //Methods to implement the new place recognition algorithm
  92. bool NewDetectCommonRegions();
  93. bool DetectAndReffineSim3FromLastKF(KeyFrame* pCurrentKF, KeyFrame* pMatchedKF, g2o::Sim3 &gScw, int &nNumProjMatches,
  94. std::vector<MapPoint*> &vpMPs, std::vector<MapPoint*> &vpMatchedMPs);
  95. bool DetectCommonRegionsFromBoW(std::vector<KeyFrame*> &vpBowCand, KeyFrame* &pMatchedKF, KeyFrame* &pLastCurrentKF, g2o::Sim3 &g2oScw,
  96. int &nNumCoincidences, std::vector<MapPoint*> &vpMPs, std::vector<MapPoint*> &vpMatchedMPs);
  97. bool DetectCommonRegionsFromLastKF(KeyFrame* pCurrentKF, KeyFrame* pMatchedKF, g2o::Sim3 &gScw, int &nNumProjMatches,
  98. std::vector<MapPoint*> &vpMPs, std::vector<MapPoint*> &vpMatchedMPs);
  99. int FindMatchesByProjection(KeyFrame* pCurrentKF, KeyFrame* pMatchedKFw, g2o::Sim3 &g2oScw,
  100. set<MapPoint*> &spMatchedMPinOrigin, vector<MapPoint*> &vpMapPoints,
  101. vector<MapPoint*> &vpMatchedMapPoints);
  102. void SearchAndFuse(const KeyFrameAndPose &CorrectedPosesMap, vector<MapPoint*> &vpMapPoints);
  103. void SearchAndFuse(const vector<KeyFrame*> &vConectedKFs, vector<MapPoint*> &vpMapPoints);
  104. void CorrectLoop();
  105. void MergeLocal();
  106. void MergeLocal2();
  107. void CheckObservations(set<KeyFrame*> &spKFsMap1, set<KeyFrame*> &spKFsMap2);
  108. void ResetIfRequested();
  109. bool mbResetRequested;
  110. bool mbResetActiveMapRequested;
  111. Map* mpMapToReset;
  112. std::mutex mMutexReset;
  113. bool CheckFinish();
  114. void SetFinish();
  115. bool mbFinishRequested;
  116. bool mbFinished;
  117. std::mutex mMutexFinish;
  118. Atlas* mpAtlas;
  119. Tracking* mpTracker;
  120. KeyFrameDatabase* mpKeyFrameDB;
  121. ORBVocabulary* mpORBVocabulary;
  122. LocalMapping *mpLocalMapper;
  123. std::list<KeyFrame*> mlpLoopKeyFrameQueue;
  124. std::mutex mMutexLoopQueue;
  125. // Loop detector parameters
  126. float mnCovisibilityConsistencyTh;
  127. // Loop detector variables
  128. KeyFrame* mpCurrentKF;
  129. KeyFrame* mpLastCurrentKF;
  130. KeyFrame* mpMatchedKF;
  131. std::vector<ConsistentGroup> mvConsistentGroups;
  132. std::vector<KeyFrame*> mvpEnoughConsistentCandidates;
  133. std::vector<KeyFrame*> mvpCurrentConnectedKFs;
  134. std::vector<MapPoint*> mvpCurrentMatchedPoints;
  135. std::vector<MapPoint*> mvpLoopMapPoints;
  136. cv::Mat mScw;
  137. g2o::Sim3 mg2oScw;
  138. //-------
  139. Map* mpLastMap;
  140. bool mbLoopDetected;
  141. int mnLoopNumCoincidences;
  142. int mnLoopNumNotFound;
  143. KeyFrame* mpLoopLastCurrentKF;
  144. g2o::Sim3 mg2oLoopSlw;
  145. g2o::Sim3 mg2oLoopScw;
  146. KeyFrame* mpLoopMatchedKF;
  147. std::vector<MapPoint*> mvpLoopMPs;
  148. std::vector<MapPoint*> mvpLoopMatchedMPs;
  149. bool mbMergeDetected;
  150. int mnMergeNumCoincidences;
  151. int mnMergeNumNotFound;
  152. KeyFrame* mpMergeLastCurrentKF;
  153. g2o::Sim3 mg2oMergeSlw;
  154. g2o::Sim3 mg2oMergeSmw;
  155. g2o::Sim3 mg2oMergeScw;
  156. KeyFrame* mpMergeMatchedKF;
  157. std::vector<MapPoint*> mvpMergeMPs;
  158. std::vector<MapPoint*> mvpMergeMatchedMPs;
  159. std::vector<KeyFrame*> mvpMergeConnectedKFs;
  160. g2o::Sim3 mSold_new;
  161. //-------
  162. long unsigned int mLastLoopKFid;
  163. // Variables related to Global Bundle Adjustment
  164. bool mbRunningGBA;
  165. bool mbFinishedGBA;
  166. bool mbStopGBA;
  167. std::mutex mMutexGBA;
  168. std::thread* mpThreadGBA;
  169. // Fix scale in the stereo/RGB-D case
  170. bool mbFixScale;
  171. bool mnFullBAIdx;
  172. vector<double> vdPR_CurrentTime;
  173. vector<double> vdPR_MatchedTime;
  174. vector<int> vnPR_TypeRecogn;
  175. //DEBUG
  176. string mstrFolderSubTraj;
  177. int mnNumCorrection;
  178. int mnCorrectionGBA;
  179. // To (de)activate LC
  180. bool mbActiveLC = true;
  181. #ifdef REGISTER_LOOP
  182. string mstrFolderLoop;
  183. #endif
  184. };
  185. } //namespace ORB_SLAM
  186. #endif // LOOPCLOSING_H