SaDVIO
slamCore.h
Go to the documentation of this file.
1 #ifndef SLAMCORE_H
2 #define SLAMCORE_H
3 
4 #include <filesystem>
5 #include <fstream>
6 #include <iostream>
7 #include <mutex>
8 #include <thread>
9 
26 #include "isaeslam/typedefs.h"
27 #include "utilities/timer.h"
28 
29 namespace isae {
30 
40 class SLAMCore {
41  public:
42  SLAMCore(){};
43  SLAMCore(std::shared_ptr<isae::SLAMParameters> slam_param);
44 
48  virtual bool init() = 0;
49 
53  virtual bool frontEndStep() = 0;
54 
58  virtual bool backEndStep() = 0;
59 
63  void runBackEnd();
64 
68  void runFrontEnd();
69 
73  void runFullOdom();
74 
75  bool _is_init = false;
77 
78  // Public variables for display
79  std::shared_ptr<isae::SLAMParameters> _slam_param;
80  std::shared_ptr<Frame> _frame_to_display;
81  std::shared_ptr<isae::LocalMap> _local_map_to_display;
82  std::shared_ptr<isae::GlobalMap> _global_map_to_display;
83  std::shared_ptr<Mesh3D> _mesh_to_display;
84 
88  typed_vec_features detectFeatures(std::shared_ptr<ImageSensor> &sensor);
89 
93  void cleanFeatures(std::shared_ptr<Frame> &f);
94 
106  uint matchFeatures(std::shared_ptr<ImageSensor> &sensor0,
107  std::shared_ptr<ImageSensor> &sensor1,
108  typed_vec_match &matches,
109  typed_vec_match &matches_lmk,
110  typed_vec_features features_to_track);
111 
123  uint trackFeatures(std::shared_ptr<ImageSensor> &sensor0,
124  std::shared_ptr<ImageSensor> &sensor1,
125  typed_vec_match &matches,
126  typed_vec_match &matches_lmk,
127  typed_vec_features features_to_track);
128 
137  void predictFeature(std::vector<std::shared_ptr<AFeature>> features,
138  std::shared_ptr<ImageSensor> sensor,
139  std::vector<std::shared_ptr<AFeature>> &features_init,
140  vec_match previous_matches);
141 
152  epipolarFiltering(std::shared_ptr<ImageSensor> &cam0, std::shared_ptr<ImageSensor> &cam1, typed_vec_match matches);
153 
157  void outlierRemoval();
158 
166  bool predict(std::shared_ptr<Frame> &f);
167 
171  void initLandmarks(std::shared_ptr<Frame> &f);
172  void updateLandmarks(typed_vec_match matches_lmk);
173 
182  uint recoverFeatureFromMapLandmarks(std::shared_ptr<isae::AMap> localmap, std::shared_ptr<Frame> &f);
183 
187  bool shouldInsertKeyframe(std::shared_ptr<Frame> &f);
188  std::shared_ptr<Frame> getLastKF() { return _local_map->getLastFrame(); }
189 
193  void profiling();
194 
195  protected:
196  std::shared_ptr<Frame> _frame;
197 
198  // Typed vector for matches
203 
204  // Local Map, Mesh and Keyframe voting policy
205  std::shared_ptr<isae::LocalMap> _local_map;
206  std::shared_ptr<isae::GlobalMap> _global_map;
207  std::shared_ptr<Mesher> _mesher;
211  double _parallax;
213 
214  // To ensure safe communication between threads
215  std::mutex _map_mutex;
216  std::shared_ptr<Frame> _frame_to_optim;
217 
218  // Profiling variables
219  uint _nframes;
231  float _avg_marg_t;
235  float _lmk_inmap;
239 
240  // For timing statistics
241  std::vector<std::vector<float>> _timings_frate;
242  std::vector<std::vector<float>> _timings_kfrate_fe;
243  std::vector<std::vector<float>> _timings_kfrate_be;
244 };
245 
249 class SLAMBiMono : public SLAMCore {
250 
251  public:
252  SLAMBiMono(std::shared_ptr<SLAMParameters> slam_param) : SLAMCore(slam_param) {}
253 
254  bool init() override;
255  bool frontEndStep() override;
256  bool backEndStep() override;
257 };
258 
262 class SLAMBiMonoVIO : public SLAMCore {
263 
264  public:
265  SLAMBiMonoVIO(std::shared_ptr<SLAMParameters> slam_param) : SLAMCore(slam_param) {}
266 
267  bool init() override;
268  bool frontEndStep() override;
269  bool backEndStep() override;
270 
274  bool step_init();
275 
279  void IMUprofiling();
280 
281  private:
282  std::shared_ptr<IMU> _last_IMU;
283 };
284 
288 class SLAMMonoVIO : public SLAMCore {
289 
290  public:
291  SLAMMonoVIO(std::shared_ptr<SLAMParameters> slam_param) : SLAMCore(slam_param) {}
292 
293  bool init() override;
294  bool frontEndStep() override;
295  bool backEndStep() override;
296 
300  bool step_init();
301 
302  private:
303  std::shared_ptr<IMU> _last_IMU;
304 };
305 
309 class SLAMMono : public SLAMCore {
310 
311  public:
312  SLAMMono(std::shared_ptr<SLAMParameters> slam_param) : SLAMCore(slam_param) {}
313 
314  bool init() override;
315  bool frontEndStep() override;
316  bool backEndStep() override;
317 };
318 
323 
324  public:
326  SLAMNonOverlappingFov(std::shared_ptr<SLAMParameters> slam_param) : SLAMCore(slam_param) {}
327 
328  bool init() override;
329  bool frontEndStep() override;
330  bool backEndStep() override;
331 
335  void outlierRemoval();
336 
340  void initLandmarks(std::shared_ptr<Frame> &f);
341 
352  int scaleEstimationRANSAC(const Eigen::Affine3d T_cam0_cam1,
353  const Eigen::Affine3d T_cam0_cam0p,
354  typed_vec_match matches_cam1,
355  double &lambda);
356 
360  bool isDegenerativeMotion(Eigen::Affine3d T_cam0_cam0p, Eigen::Affine3d T_cam0_cam1, typed_vec_match matches);
361 
365  bool predict(std::shared_ptr<Frame> &f);
366 
367  private:
368  typed_vec_match _matches_in_time_cam1;
370  _matches_in_time_cam1_lmk;
371 };
372 
373 } // namespace isae
374 
375 #endif // SLAMCORE_H
adataprovider.h
isae::SLAMCore::_timings_kfrate_be
std::vector< std::vector< float > > _timings_kfrate_be
Definition: slamCore.h:243
isae::SLAMBiMono::frontEndStep
bool frontEndStep() override
Front End: detection, tracking, pose estimation and landmark triangulation.
Definition: slamBiMono.cpp:59
isae::SLAMNonOverlappingFov::SLAMNonOverlappingFov
SLAMNonOverlappingFov(std::shared_ptr< SLAMParameters > slam_param)
Definition: slamCore.h:326
isae::SLAMBiMono::SLAMBiMono
SLAMBiMono(std::shared_ptr< SLAMParameters > slam_param)
Definition: slamCore.h:252
isae::SLAMCore::predictFeature
void predictFeature(std::vector< std::shared_ptr< AFeature >> features, std::shared_ptr< ImageSensor > sensor, std::vector< std::shared_ptr< AFeature >> &features_init, vec_match previous_matches)
Predicts the position of a set of features on a given sensor.
Definition: slamCore.cpp:241
DoubleSphere.h
isae::SLAMNonOverlappingFov::isDegenerativeMotion
bool isDegenerativeMotion(Eigen::Affine3d T_cam0_cam0p, Eigen::Affine3d T_cam0_cam1, typed_vec_match matches)
To check if the scale can't be recovered.
Definition: slamNonOverlappingFov.cpp:634
isae::SLAMCore::_mesh_to_display
std::shared_ptr< Mesh3D > _mesh_to_display
Definition: slamCore.h:83
isae::SLAMCore::_max_movement_parallax
double _max_movement_parallax
Max parallax until a KF is voted.
Definition: slamCore.h:208
isae::SLAMCore::_avg_wdw_opt_t
float _avg_wdw_opt_t
Definition: slamCore.h:232
isae::SLAMCore::_avg_marg_t
float _avg_marg_t
Definition: slamCore.h:231
isae::SLAMCore::_min_movement_parallax
double _min_movement_parallax
Under this parallax, no motion is considered.
Definition: slamCore.h:209
isae::SLAMCore::cleanFeatures
void cleanFeatures(std::shared_ptr< Frame > &f)
Clean all the features that are outliers or are linked to outlier landmark.
Definition: slamCore.cpp:67
isae::SLAMNonOverlappingFov
A SLAM class for non overlapping FoV setups.
Definition: slamCore.h:322
isae::SLAMBiMono::init
bool init() override
Initialization step : create the first 3D landmarks and keyframe(s)
Definition: slamBiMono.cpp:7
isae::SLAMBiMonoVIO::init
bool init() override
Initialization step : create the first 3D landmarks and keyframe(s)
Definition: slamBiMonoVIO.cpp:7
isae::SLAMCore::_avg_lmk_init_t
float _avg_lmk_init_t
Definition: slamCore.h:226
isae::SLAMMonoVIO::init
bool init() override
Initialization step : create the first 3D landmarks and keyframe(s)
Definition: slamMonoVIO.cpp:5
isae::SLAMNonOverlappingFov::init
bool init() override
Initialization step : create the first 3D landmarks and keyframe(s)
Definition: slamNonOverlappingFov.cpp:18
isae::SLAMCore::_map_mutex
std::mutex _map_mutex
Definition: slamCore.h:215
isae::SLAMCore::_local_map_to_display
std::shared_ptr< isae::LocalMap > _local_map_to_display
Definition: slamCore.h:81
isae::SLAMCore::frontEndStep
virtual bool frontEndStep()=0
Front End: detection, tracking, pose estimation and landmark triangulation.
isae::SLAMNonOverlappingFov::scaleEstimationRANSAC
int scaleEstimationRANSAC(const Eigen::Affine3d T_cam0_cam1, const Eigen::Affine3d T_cam0_cam0p, typed_vec_match matches_cam1, double &lambda)
To compute the scale using a single point in a RANSAC fashion.
Definition: slamNonOverlappingFov.cpp:423
isae::SLAMCore::_avg_clean_t
float _avg_clean_t
Definition: slamCore.h:230
ESKFEstimator.h
isae::typed_vec_match
std::unordered_map< std::string, vec_match > typed_vec_match
An unordered map to link match vector with their type.
Definition: typedefs.h:26
isae::SLAMCore::_avg_frame_opt_t
float _avg_frame_opt_t
Definition: slamCore.h:229
isae::SLAMCore::matchFeatures
uint matchFeatures(std::shared_ptr< ImageSensor > &sensor0, std::shared_ptr< ImageSensor > &sensor1, typed_vec_match &matches, typed_vec_match &matches_lmk, typed_vec_features features_to_track)
Predicts the position of the features and matches all the features between sensors.
Definition: slamCore.cpp:285
isae::SLAMCore::_global_map
std::shared_ptr< isae::GlobalMap > _global_map
Current global map.
Definition: slamCore.h:206
isae::SLAMNonOverlappingFov::predict
bool predict(std::shared_ptr< Frame > &f)
Prediction that takes into account both camera motions.
Definition: slamNonOverlappingFov.cpp:588
isae::SLAMBiMonoVIO::SLAMBiMonoVIO
SLAMBiMonoVIO(std::shared_ptr< SLAMParameters > slam_param)
Definition: slamCore.h:265
AngularAdjustmentCERESAnalytic.h
isae::SLAMNonOverlappingFov::frontEndStep
bool frontEndStep() override
Front End: detection, tracking, pose estimation and landmark triangulation.
Definition: slamNonOverlappingFov.cpp:175
isae::typed_vec_features
std::unordered_map< std::string, std::vector< std::shared_ptr< isae::AFeature > > > typed_vec_features
A typed vector of features to handle hetereogeneous feature sets.
Definition: typedefs.h:28
isae::SLAMCore::_avg_resur_lmk
float _avg_resur_lmk
Definition: slamCore.h:238
isae::SLAMCore::_avg_matches_frame
float _avg_matches_frame
Definition: slamCore.h:237
isae::SLAMCore::_avg_match_time_t
float _avg_match_time_t
Definition: slamCore.h:224
isae::SLAMCore::_avg_filter_t
float _avg_filter_t
Definition: slamCore.h:225
isae::SLAMCore::_matches_in_time
typed_vec_match _matches_in_time
Typed vector of the matches between the last KF and _frame.
Definition: slamCore.h:199
AFeature2D.h
isae::SLAMCore::_local_map
std::shared_ptr< isae::LocalMap > _local_map
Current local map.
Definition: slamCore.h:205
isae::SLAMCore::_avg_match_frame_t
float _avg_match_frame_t
Definition: slamCore.h:223
globalmap.h
isae::SLAMCore::detectFeatures
typed_vec_features detectFeatures(std::shared_ptr< ImageSensor > &sensor)
Detect all types of features for a given sensor with bucketting.
Definition: slamCore.cpp:184
isae::SLAMCore::_min_lmk_number
double _min_lmk_number
Under this number of landmark in _frame a KF is voted.
Definition: slamCore.h:210
isae::SLAMBiMonoVIO::frontEndStep
bool frontEndStep() override
Front End: detection, tracking, pose estimation and landmark triangulation.
Definition: slamBiMonoVIO.cpp:314
isae::SLAMNonOverlappingFov::outlierRemoval
void outlierRemoval()
To remove outliers on both cameras.
Definition: slamNonOverlappingFov.cpp:355
isae::SLAMCore::initLandmarks
void initLandmarks(std::shared_ptr< Frame > &f)
Initialize all the landmarks of the current frame using _matches_in_time and _matches_in_frame.
Definition: slamCore.cpp:96
isae::SLAMCore::_removed_lmk
float _removed_lmk
Definition: slamCore.h:233
isae::SLAMCore::_successive_fails
int _successive_fails
Number of successive failure to trigger reinitialization.
Definition: slamCore.h:76
isae::SLAMCore::_mesher
std::shared_ptr< Mesher > _mesher
Mesher of the SLAM.
Definition: slamCore.h:207
slamParameters.h
isae::SLAMCore::_avg_predict_t
float _avg_predict_t
Definition: slamCore.h:228
isae::SLAMCore::_parallax
double _parallax
Parallax between the last KF and _frame.
Definition: slamCore.h:211
isae::SLAMCore
The core abstract class of the SLAM system. It handles front-end, back-end and initialization.
Definition: slamCore.h:40
Point2DFeatureMatcher.h
isae::SLAMCore::_frame_to_display
std::shared_ptr< Frame > _frame_to_display
Definition: slamCore.h:80
Point3DlandmarkInitializer.h
isae::SLAMBiMonoVIO::IMUprofiling
void IMUprofiling()
For profiling at IMU rate.
Definition: slamBiMonoVIO.cpp:616
isae::SLAMCore::trackFeatures
uint trackFeatures(std::shared_ptr< ImageSensor > &sensor0, std::shared_ptr< ImageSensor > &sensor1, typed_vec_match &matches, typed_vec_match &matches_lmk, typed_vec_features features_to_track)
Predicts the position of the features and tracks all the features.
Definition: slamCore.cpp:315
typedefs.h
isae::SLAMCore::backEndStep
virtual bool backEndStep()=0
brief description Back End: marginalization, local map optimization
EpipolarPoseEstimator.h
isae::SLAMCore::_avg_detect_t
float _avg_detect_t
Definition: slamCore.h:221
timer.h
isae::vec_match
std::vector< feature_pair > vec_match
A vector of feature pairs i.e. matches.
Definition: typedefs.h:24
isae::SLAMMono::init
bool init() override
Initialization step : create the first 3D landmarks and keyframe(s)
Definition: slamMono.cpp:5
isae::SLAMCore::_timings_frate
std::vector< std::vector< float > > _timings_frate
Definition: slamCore.h:241
isae::SLAMCore::_matches_in_frame_lmk
typed_vec_match _matches_in_frame_lmk
Typed vector of the matches with landmarks between the sensors of _frame.
Definition: slamCore.h:202
isae::SLAMBiMono
A SLAM class for bi-monocular setups.
Definition: slamCore.h:249
isae::SLAMMono::SLAMMono
SLAMMono(std::shared_ptr< SLAMParameters > slam_param)
Definition: slamCore.h:312
isae::SLAMCore::epipolarFiltering
typed_vec_match epipolarFiltering(std::shared_ptr< ImageSensor > &cam0, std::shared_ptr< ImageSensor > &cam1, typed_vec_match matches)
Filters the matches between two sensors (with consistent pose estimates) using epipolar plane check.
Definition: slamCore.cpp:201
mesh.h
isae::SLAMCore::runFullOdom
void runFullOdom()
Thread for the backend.
Definition: slamCore.cpp:607
isae::SLAMCore::_nkeyframes
uint _nkeyframes
Definition: slamCore.h:220
isae::SLAMCore::_slam_param
std::shared_ptr< isae::SLAMParameters > _slam_param
Definition: slamCore.h:79
isae::SLAMMono::backEndStep
bool backEndStep() override
brief description Back End: marginalization, local map optimization
Definition: slamMono.cpp:272
isae
Definition: AFeature2D.h:8
isae::SLAMBiMono::backEndStep
bool backEndStep() override
brief description Back End: marginalization, local map optimization
Definition: slamBiMono.cpp:239
isae::SLAMCore::getLastKF
std::shared_ptr< Frame > getLastKF()
Definition: slamCore.h:188
isae::SLAMMonoVIO::frontEndStep
bool frontEndStep() override
Front End: detection, tracking, pose estimation and landmark triangulation.
Definition: slamMonoVIO.cpp:386
isae::SLAMCore::_matches_in_time_lmk
typed_vec_match _matches_in_time_lmk
Typed vector of the matches with landmarks between the last KF and _frame.
Definition: slamCore.h:200
isae::SLAMCore::profiling
void profiling()
A function to monitor the SLAM behaviour.
Definition: slamCore.cpp:450
isae::SLAMMonoVIO::backEndStep
bool backEndStep() override
brief description Back End: marginalization, local map optimization
Definition: slamMonoVIO.cpp:587
isae::SLAMMonoVIO::SLAMMonoVIO
SLAMMonoVIO(std::shared_ptr< SLAMParameters > slam_param)
Definition: slamCore.h:291
isae::SLAMCore::_avg_processing_t
float _avg_processing_t
Definition: slamCore.h:222
isae::SLAMMono::frontEndStep
bool frontEndStep() override
Front End: detection, tracking, pose estimation and landmark triangulation.
Definition: slamMono.cpp:115
isae::SLAMCore::runBackEnd
void runBackEnd()
Thread for the backend.
Definition: slamCore.cpp:598
isae::SLAMNonOverlappingFov::SLAMNonOverlappingFov
SLAMNonOverlappingFov()
Definition: slamCore.h:325
isae::SLAMBiMonoVIO::step_init
bool step_init()
Initialization steps with bi-monocular only for IMU initialization.
Definition: slamBiMonoVIO.cpp:122
isae::SLAMCore::runFrontEnd
void runFrontEnd()
Thread for the frontend.
Definition: slamCore.cpp:584
isae::SLAMCore::_avg_lmk_resur_t
float _avg_lmk_resur_t
Definition: slamCore.h:227
isae::SLAMMonoVIO
A SLAM class for monocular + IMU setups.
Definition: slamCore.h:288
isae::SLAMMono
A SLAM class for monocular setups.
Definition: slamCore.h:309
isae::SLAMBiMonoVIO::backEndStep
bool backEndStep() override
brief description Back End: marginalization, local map optimization
Definition: slamBiMonoVIO.cpp:547
cvORBFeatureDetector.h
isae::SLAMCore::_avg_matches_time
float _avg_matches_time
Definition: slamCore.h:236
isae::SLAMCore::_frame
std::shared_ptr< Frame > _frame
Current frame.
Definition: slamCore.h:196
isae::SLAMMonoVIO::step_init
bool step_init()
Initialization steps with bi-monocular only for IMU initialization.
Definition: slamMonoVIO.cpp:209
isae::Vector6d
Eigen::Matrix< double, 6, 1 > Vector6d
A double Eigen vector in 6D.
Definition: typedefs.h:16
isae::SLAMCore::predict
bool predict(std::shared_ptr< Frame > &f)
Estimates the pose of a given frame.
Definition: slamCore.cpp:413
isae::SLAMNonOverlappingFov::initLandmarks
void initLandmarks(std::shared_ptr< Frame > &f)
To init landmarks on both cameras.
Definition: slamNonOverlappingFov.cpp:519
isae::SLAMCore::_matches_in_frame
typed_vec_match _matches_in_frame
Typed vector of the matches between the sensors of _frame.
Definition: slamCore.h:201
isae::SLAMCore::_nframes
uint _nframes
Definition: slamCore.h:219
isae::SLAMBiMonoVIO
A SLAM class for bi-monocular + IMU setups.
Definition: slamCore.h:262
isae::SLAMNonOverlappingFov::backEndStep
bool backEndStep() override
brief description Back End: marginalization, local map optimization
Definition: slamNonOverlappingFov.cpp:317
isae::SLAMCore::_is_init
bool _is_init
Flag for initialization.
Definition: slamCore.h:75
isae::SLAMCore::init
virtual bool init()=0
Initialization step : create the first 3D landmarks and keyframe(s)
isae::SLAMCore::shouldInsertKeyframe
bool shouldInsertKeyframe(std::shared_ptr< Frame > &f)
Determines if the frame in argument is a KF.
Definition: slamCore.cpp:351
isae::SLAMCore::_removed_feat
float _removed_feat
Definition: slamCore.h:234
isae::SLAMCore::_timings_kfrate_fe
std::vector< std::vector< float > > _timings_kfrate_fe
Definition: slamCore.h:242
isae::SLAMCore::_lmk_inmap
float _lmk_inmap
Definition: slamCore.h:235
ASensor.h
isae::SLAMCore::_6d_velocity
Vector6d _6d_velocity
Current Velocity as a Twist vector.
Definition: slamCore.h:212
isae::SLAMCore::outlierRemoval
void outlierRemoval()
Remove all the outlier features of _frame.
Definition: slamCore.cpp:43
isae::SLAMCore::_global_map_to_display
std::shared_ptr< isae::GlobalMap > _global_map_to_display
Definition: slamCore.h:82
isae::SLAMCore::recoverFeatureFromMapLandmarks
uint recoverFeatureFromMapLandmarks(std::shared_ptr< isae::AMap > localmap, std::shared_ptr< Frame > &f)
Performs landmark resurection.
Definition: slamCore.cpp:230
mesher.h
Point2DFeatureTracker.h
isae::SLAMCore::updateLandmarks
void updateLandmarks(typed_vec_match matches_lmk)
Definition: slamCore.cpp:85
localmap.h
isae::SLAMCore::SLAMCore
SLAMCore()
Definition: slamCore.h:42
isae::SLAMCore::_frame_to_optim
std::shared_ptr< Frame > _frame_to_optim
For communication between front-end and back-end.
Definition: slamCore.h:216