SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
WAISlamTrackPool.cpp
Go to the documentation of this file.
1 #include "WAISlamTrackPool.h"
2 
3 //-----------------------------------------------------------------------------
4 WAISlamTrackPool::WAISlamTrackPool(const cv::Mat& intrinsic,
5  const cv::Mat& distortion,
6  WAIOrbVocabulary* voc,
7  KPextractor* iniExtractor,
8  KPextractor* relocExtractor,
9  KPextractor* extractor,
10  std::unique_ptr<WAIMap> globalMap,
12 {
13  _iniData.initializer = nullptr;
14  _params = params;
15 
18 
21 
22  _distortion = distortion.clone();
23  _cameraIntrinsic = intrinsic.clone();
24  _voc = voc;
25 
26  _extractor = extractor;
27  _relocExtractor = relocExtractor;
28  _iniExtractor = iniExtractor;
29 
30  if (_iniExtractor == nullptr)
32  if (_relocExtractor == nullptr)
34 
35  if (globalMap == nullptr)
36  {
37  WAIKeyFrameDB* kfDB = new WAIKeyFrameDB(voc);
38  _globalMap = std::make_unique<WAIMap>(kfDB);
40 
43  }
44  else
45  {
46  _globalMap = std::move(globalMap);
48  _initialized = true;
49  }
50 
51  _localMapping = new ORB_SLAM2::LocalMapping(_globalMap.get(),
52  _voc,
53  params.cullRedundantPerc);
54  _loopClosing = new ORB_SLAM2::LoopClosing(_globalMap.get(),
55  _voc,
56  false,
57  false);
58 
59  _localMapping->SetLoopCloser(_loopClosing);
60  _loopClosing->SetLocalMapper(_localMapping);
61 
63  {
64  _mappingThreads.push_back(new std::thread(&LocalMapping::Run, _localMapping));
65  _loopClosingThread = new std::thread(&LoopClosing::Run, _loopClosing);
66  }
67 
68  _iniData.initializer = nullptr;
69  _cameraExtrinsic = cv::Mat::eye(4, 4, CV_32F);
70 
71  _lastFrame = WAIFrame();
72 }
73 //-----------------------------------------------------------------------------
75 {
76  if (!_params.serial)
77  {
78  _localMapping->RequestFinish();
79  _loopClosing->RequestFinish();
80  }
81 
82  // Wait until all thread have effectively stopped
85 
86  for (std::thread* t : _mappingThreads)
87  {
88  t->join();
89  }
90 
92  _loopClosingThread->join();
93 
94  delete _localMapping;
95  delete _loopClosing;
96 }
97 //-----------------------------------------------------------------------------
98 bool WAISlamTrackPool::update(cv::Mat& imageGray)
99 {
100  WAIFrame frame;
101  createFrame(frame, imageGray);
102 
103  // if (_params.ensureKFIntegration)
104  // updatePoseKFIntegration(frame);
105  // else
106  updatePose(frame);
107 
109 }
110 //-----------------------------------------------------------------------------
111 void WAISlamTrackPool::createFrame(WAIFrame& frame, cv::Mat& imageGray)
112 {
113  switch (getTrackingState())
114  {
116  frame = WAIFrame(imageGray,
117  0.0,
120  _distortion,
121  _voc,
123  break;
126  frame = WAIFrame(imageGray,
127  0.0,
130  _distortion,
131  _voc,
133  break;
134  default:
135  frame = WAIFrame(imageGray,
136  0.0,
137  _extractor,
139  _distortion,
140  _voc,
142  }
143 }
144 //-----------------------------------------------------------------------------
146 {
147  switch (_state)
148  {
150  {
151  if (initialize(_iniData, frame, _voc, _localMap))
152  {
153  if (genInitialMap(_globalMap.get(),
155  _loopClosing,
156  _localMap))
157  {
158  _localMapping->InsertKeyFrame(_localMap.keyFrames[0]);
159  _localMapping->InsertKeyFrame(_localMap.keyFrames[1]);
160  _lastKeyFrameFrameId = frame.mnId;
161  _lastRelocFrameId = 0;
163  _initialized = true;
164  if (_params.serial)
165  {
166  _localMapping->RunOnce();
167  _localMapping->RunOnce();
168  }
169  }
170  }
171  }
172  break;
173 
175  {
177  if (_relocFrameCounter > 0)
179  }
181  {
182  int inliers;
183  if (tracking(_globalMap.get(),
184  _localMap,
185  frame,
186  _lastFrame,
187  (int)_lastRelocFrameId,
188  _velocity,
189  inliers))
190  {
191  std::unique_lock<std::mutex> lock(_cameraExtrinsicMutex);
193  lock.unlock();
194  mapping(_globalMap.get(),
195  _localMap,
197  frame,
198  inliers,
201  if (_params.serial)
202  {
203  _localMapping->RunOnce();
204  _loopClosing->RunOnce();
205  }
206  _infoMatchedInliners = inliers;
207  }
208  else
209  {
211  }
212  }
213  break;
215  {
216  int inliers;
218  {
219  _relocFrameCounter = 0;
220  _lastRelocFrameId = frame.mnId;
221  _velocity = cv::Mat();
223  }
224  }
225  break;
226  }
227 
228  _lastFrame = WAIFrame(frame);
229 }
230 //-----------------------------------------------------------------------------
231 void WAISlamTrackPool::changeIntrinsic(cv::Mat intrinsic, cv::Mat distortion)
232 {
233  _cameraIntrinsic = intrinsic;
234  _distortion = distortion;
235 }
236 //-----------------------------------------------------------------------------
238 {
239  std::unique_lock<std::mutex> lock(_cameraExtrinsicMutex);
240  return _cameraExtrinsic;
241 }
242 //-----------------------------------------------------------------------------
243 void WAISlamTrackPool::drawInfo(cv::Mat& imageBGR,
244  float scale,
245  bool showInitLine,
246  bool showKeyPoints,
247  bool showKeyPointsMatched)
248 {
250  {
251  if (showInitLine)
252  drawInitInfo(_iniData, _lastFrame, imageBGR, scale);
253  }
255  {
256  if (showKeyPoints)
257  drawKeyPointInfo(_lastFrame, imageBGR, scale);
258  if (showKeyPointsMatched)
259  drawKeyPointMatches(_lastFrame, imageBGR, scale);
260  }
262  {
263  if (showKeyPoints)
264  drawKeyPointInfo(_lastFrame, imageBGR, scale);
265  }
266 }
267 //-----------------------------------------------------------------------------
268 std::vector<WAIMapPoint*> WAISlamTrackPool::getMatchedMapPoints()
269 {
270  std::vector<WAIMapPoint*> result;
271 
272  for (int i = 0; i < _lastFrame.N; i++)
273  {
274  if (_lastFrame.mvpMapPoints[i])
275  {
276  if (_lastFrame.mvpMapPoints[i]->Observations() > 0)
277  result.push_back(_lastFrame.mvpMapPoints[i]);
278  }
279  }
280 
281  return result;
282 }
283 //-----------------------------------------------------------------------------
@ TrackingOK
Definition: WAISlamTools.h:31
@ TrackingStart
Definition: WAISlamTools.h:33
@ TrackingLost
Definition: WAISlamTools.h:32
@ Initializing
Definition: WAISlamTools.h:30
std::vector< WAIMapPoint * > mvpMapPoints
Definition: WAIFrame.h:147
long unsigned int mnId
Definition: WAIFrame.h:159
static long unsigned int nNextId
Definition: WAIFrame.h:158
int N
Definition: WAIFrame.h:123
static bool mbInitialComputations
Definition: WAIFrame.h:182
AR Keyframe database class.
Definition: WAIKeyFrameDB.h:46
static long unsigned int nNextId
Definition: WAIKeyFrame.h:174
static long unsigned int nNextId
Definition: WAIMapPoint.h:110
static void motionModel(WAIFrame &frame, WAIFrame &lastFrame, cv::Mat &velocity, cv::Mat &pose)
cv::Mat _cameraIntrinsic
Definition: WAISlamTools.h:201
cv::Mat _cameraExtrinsic
Definition: WAISlamTools.h:202
std::vector< std::thread * > _mappingThreads
Definition: WAISlamTools.h:217
LoopClosing * _loopClosing
Definition: WAISlamTools.h:215
static bool initialize(WAIInitializerData &iniData, WAIFrame &frame, WAIOrbVocabulary *voc, LocalMap &localMap, int mapPointsNeeded=100)
std::unique_ptr< WAIMap > _globalMap
Definition: WAISlamTools.h:208
cv::Mat _distortion
Definition: WAISlamTools.h:200
std::thread * _loopClosingThread
Definition: WAISlamTools.h:218
static bool tracking(WAIMap *map, LocalMap &localMap, WAIFrame &frame, WAIFrame &lastFrame, int lastRelocFrameId, cv::Mat &velocity, int &inliers)
static void drawInitInfo(WAIInitializerData &iniData, WAIFrame &frame, cv::Mat &imageBGR, float scale)
LocalMapping * _localMapping
Definition: WAISlamTools.h:214
static bool relocalization(WAIFrame &currentFrame, WAIMap *waiMap, LocalMap &localMap, float minCommonWordFactor, int &inliers, bool minAccScoreFilter=false)
static void drawKeyPointMatches(WAIFrame &frame, cv::Mat &image, float scale)
WAIFrame _lastFrame
Definition: WAISlamTools.h:206
LocalMap _localMap
Definition: WAISlamTools.h:209
WAIOrbVocabulary * _voc
Definition: WAISlamTools.h:210
static bool genInitialMap(WAIMap *globalMap, LocalMapping *localMapper, LoopClosing *loopCloser, LocalMap &localMap)
WAIInitializerData _iniData
Definition: WAISlamTools.h:205
std::thread * _processNewKeyFrameThread
Definition: WAISlamTools.h:216
static void mapping(WAIMap *map, LocalMap &localMap, LocalMapping *localMapper, WAIFrame &frame, int inliers, const unsigned long lastRelocFrameId, unsigned long &lastKeyFrameFrameId)
static void drawKeyPointInfo(WAIFrame &frame, cv::Mat &image, float scale)
cv::Mat _velocity
Definition: WAISlamTools.h:211
unsigned int _relocFrameCounter
void changeIntrinsic(cv::Mat intrinsic, cv::Mat distortion)
KPextractor * _relocExtractor
void createFrame(WAIFrame &frame, cv::Mat &imageGray)
void drawInfo(cv::Mat &imageBGR, float scale, bool showInitLine, bool showKeyPoints, bool showKeyPointsMatched)
virtual WAITrackingState getTrackingState()
WAITrackingState _state
unsigned long _lastKeyFrameFrameId
KPextractor * _extractor
virtual ~WAISlamTrackPool()
bool update(cv::Mat &imageGray)
WAISlamTrackPool(const cv::Mat &intrinsic, const cv::Mat &distortion, WAIOrbVocabulary *voc, KPextractor *iniExtractor, KPextractor *relocExtractor, KPextractor *extractor, std::unique_ptr< WAIMap > globalMap, Params params)
std::vector< WAIMapPoint * > getMatchedMapPoints()
std::mutex _cameraExtrinsicMutex
KPextractor * _iniExtractor
void updatePose(WAIFrame &frame)
unsigned long _lastRelocFrameId
Initializer * initializer
Definition: WAISlamTools.h:19