NDEVR
API Documentation
LoopClosing.h
1#pragma once
2#include "OrbSLAM/Headers/Optimizer.h"
3#include "OrbSLAM/Headers/KeyFrame.h"
4#include "OrbSLAM/Headers/Atlas.h"
5#include "OrbSLAM/Headers/ORBVocabulary.h"
6#include "OrbSLAM/Headers/Tracking.h"
7#include "OrbSLAM/Headers/KeyFrameDatabase.h"
8#include "GraphOptimization/Headers/types_seven_dof_expmap.h"
9
10#include <thread>
11#include <mutex>
12
13namespace NDEVR
14{
15
16 class Tracking;
17 class LocalMapping;
18 class KeyFrameDatabase;
19 class Map;
20 class InfoPipe;
21
29 {
30 public:
31
32 typedef std::pair<Set<KeyFrame*>, int> ConsistentGroup;
33 typedef std::map<KeyFrame*, Sim3, std::less<KeyFrame*>,
34 Eigen::aligned_allocator<std::pair<KeyFrame* const, Sim3>>> KeyFrameAndPose;
35
36 public:
37
46 LoopClosing(Atlas* pAtlas, KeyFrameDatabase* pDB, ORBVocabulary* pVoc, const bool bFixScale, const bool bActiveLC, LogPtr log);
47
48 void setParams(const bool bFixScale, const bool bActiveLC);
49 void SetTracker(Tracking* pTracker);
50
51 void SetLocalMapper(LocalMapping* pLocalMapper);
52
53 // Main function
54 void Run();
55
56 void InsertKeyFrame(KeyFrame* pKF);
57
58 void RequestReset();
59 void RequestResetActiveMap(Map* pMap);
60
61 // This function will run in a separate thread
62 void RunGlobalBundleAdjustment(Map* pActiveMap, uint04 nLoopKF);
63
64 bool isRunningGBA() {
65 std::unique_lock<std::mutex> lock(mMutexGBA);
66 return mbRunningGBA;
67 }
68 bool isFinishedGBA() {
69 std::unique_lock<std::mutex> lock(mMutexGBA);
70 return mbFinishedGBA;
71 }
72
73 void RequestFinish();
74
75 bool isFinished();
76
77 Viewer* mpViewer;
78
79 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
80
81 protected:
82
83 bool CheckNewKeyFrames();
84
85
86 //Methods to implement the new place recognition algorithm
87 bool NewDetectCommonRegions();
88 bool DetectAndReffineSim3FromLastKF(KeyFrame* pCurrentKF, KeyFrame* pMatchedKF, Sim3& gScw, uint04& nNumProjMatches,
89 Buffer<MapPoint*>& vpMPs, Buffer<MapPoint*>& vpMatchedMPs);
90 bool DetectCommonRegionsFromBoW(Buffer<KeyFrame*>& vpBowCand, KeyFrame*& pMatchedKF, KeyFrame*& pLastCurrentKF, Sim3& ParkeScw,
91 int& nNumCoincidences, Buffer<MapPoint*>& vpMPs, Buffer<MapPoint*>& vpMatchedMPs);
92 uint04 FindMatchesByProjection(KeyFrame* pCurrentKF, KeyFrame* pMatchedKFw, Sim3& ParkeScw, Buffer<MapPoint*>& vpMapPoints,
93 Buffer<MapPoint*>& vpMatchedMapPoints);
94
95
96 void SearchAndFuse(const KeyFrameAndPose& CorrectedPosesMap, Buffer<MapPoint*>& vpMapPoints);
97 void SearchAndFuse(const Buffer<KeyFrame*>& vConectedKFs, Buffer<MapPoint*>& vpMapPoints);
98
99 void CorrectLoop();
100
101 void MergeLocal();
102 void MergeLocal2();
103
104 void ResetIfRequested();
105 Buffer<KeyFrame*> m_covisible_frames;
106 ORBmatcher m_orb_matcher;
107 ORBmatcher m_bow_matcher;
108 bool mbResetRequested = false;
109 bool mbResetActiveMapRequested = false;
110 Map* mpMapToReset = nullptr;
111 std::mutex mMutexReset;
112
113 bool CheckFinish();
114 void SetFinish();
115 bool mbFinishRequested = false;
116 bool mbFinished = true;
117 std::mutex mMutexFinish;
118
119 Atlas* mpAtlas;
120 Tracking* mpTracker;
121
122 KeyFrameDatabase* mpKeyFrameDB;
123 ORBVocabulary* mpORBVocabulary;
124
125 LocalMapping* mpLocalMapper;
126
127 std::list<KeyFrame*> mlpLoopKeyFrameQueue;
128
129 std::mutex mMutexLoopQueue;
130
131 // Loop detector parameters
132 float mnCovisibilityConsistencyTh = 3.0f;
133
134 // Loop detector variables
135 KeyFrame* mpCurrentKF = nullptr;
136 KeyFrame* mpLastCurrentKF = nullptr;
137 KeyFrame* mpMatchedKF = nullptr;
138 Buffer<ConsistentGroup> mvConsistentGroups;
139 Buffer<KeyFrame*> mvpEnoughConsistentCandidates;
140 Buffer<KeyFrame*> mvpCurrentConnectedKFs;
141 Buffer<MapPoint*> mvpCurrentMatchedPoints;
142 Buffer<MapPoint*> mvpLoopMapPoints;
143 cv::Mat mScw;
144 Sim3 mParkeScw;
145
146 //-------
147 Map* mpLastMap = nullptr;
148
149 bool mbLoopDetected = false;
150 int mnLoopNumCoincidences = 0;
151 int mnLoopNumNotFound = 0;
152 KeyFrame* mpLoopLastCurrentKF = nullptr;
153 Sim3 mParkeLoopSlw;
154 Sim3 mParkeLoopScw;
155 KeyFrame* mpLoopMatchedKF = nullptr;
156 Buffer<MapPoint*> mvpLoopMPs;
157 Buffer<MapPoint*> mvpLoopMatchedMPs;
158 bool mbMergeDetected = false;
159 int mnMergeNumCoincidences = 0;
160 int mnMergeNumNotFound = 0;
161 KeyFrame* mpMergeLastCurrentKF = nullptr;
162 Sim3 mParkeMergeSlw;
163 Sim3 mParkeMergeSmw;
164 Sim3 mParkeMergeScw;
165 KeyFrame* mpMergeMatchedKF;
166 Buffer<MapPoint*> mvpMergeMPs;
167 Buffer<MapPoint*> mvpMergeMatchedMPs;
168 Buffer<KeyFrame*> mvpMergeConnectedKFs;
169
170 Sim3 mSold_new;
171 // Variables related to Global Bundle Adjustment
172 bool mbRunningGBA = false;
173 bool mbFinishedGBA = true;
174 bool mbStopGBA = false;
175 std::mutex mMutexGBA;
176 std::thread* mpThreadGBA = nullptr;
177
178 // Fix scale in the stereo/RGB-t_dim case
179 bool mbFixScale;
180
181
182 int mnFullBAIdx = 0;
183
184 //DEBUG
185 int mnNumCorrection = 0;
186 int mnCorrectionGBA = 0;
187
188
189 // To (de)activate LC
190 bool mbActiveLC = true;
191 LogPtr m_log;
192 private:
193 Optimizer m_optimizer;
194 };
195
196}
Manages a collection of maps for the ORB-SLAM system.
Definition Atlas.h:29
A light-weight base class for Log that allows processes to update, without the need for additional in...
Database of keyframes for place recognition and relocalization.
A keyframe in the SLAM map, derived from Frame.
Definition KeyFrame.h:31
Manages local map building, keyframe processing, and local bundle adjustment.
A light-weight wrapper that will be a no-op if there is not a valid log reference,...
LoopClosing(Atlas *pAtlas, KeyFrameDatabase *pDB, ORBVocabulary *pVoc, const bool bFixScale, const bool bActiveLC, LogPtr log)
Constructs the loop closer.
std::map< KeyFrame *, Sim3, std::less< KeyFrame * >, Eigen::aligned_allocator< std::pair< KeyFrame *const, Sim3 > > > KeyFrameAndPose
Map from keyframe to Sim3 pose.
Definition LoopClosing.h:34
std::pair< Set< KeyFrame * >, int > ConsistentGroup
A group of keyframes with a consistency count.
Definition LoopClosing.h:32
Represents a single SLAM map containing keyframes and map points.
Definition Map.h:22
Main tracking thread that processes each frame and estimates camera pose.
Definition Tracking.h:33
The primary namespace for the NDEVR SDK.
TemplatedVocabulary< FORB::TDescriptor, FORB > ORBVocabulary
ORB visual vocabulary type for Bag-of-Words place recognition.
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Similarity transformation in 3D (rotation + translation + scale).
Definition sim3.h:13