System.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 SYSTEM_H
  19. #define SYSTEM_H
  20. #include <unistd.h>
  21. #include<stdio.h>
  22. #include<stdlib.h>
  23. #include<string>
  24. #include<thread>
  25. #include<opencv2/core/core.hpp>
  26. #include "Tracking.h"
  27. #include "FrameDrawer.h"
  28. #include "MapDrawer.h"
  29. #include "Atlas.h"
  30. #include "LocalMapping.h"
  31. #include "LoopClosing.h"
  32. #include "KeyFrameDatabase.h"
  33. #include "ORBVocabulary.h"
  34. #include "Viewer.h"
  35. #include "ImuTypes.h"
  36. #include "Settings.h"
  37. namespace ORB_SLAM3
  38. {
  39. class Verbose
  40. {
  41. public:
  42. enum eLevel
  43. {
  44. VERBOSITY_QUIET=0,
  45. VERBOSITY_NORMAL=1,
  46. VERBOSITY_VERBOSE=2,
  47. VERBOSITY_VERY_VERBOSE=3,
  48. VERBOSITY_DEBUG=4
  49. };
  50. static eLevel th;
  51. public:
  52. static void PrintMess(std::string str, eLevel lev)
  53. {
  54. if(lev <= th)
  55. cout << str << endl;
  56. }
  57. static void SetTh(eLevel _th)
  58. {
  59. th = _th;
  60. }
  61. };
  62. class Viewer;
  63. class FrameDrawer;
  64. class MapDrawer;
  65. class Atlas;
  66. class Tracking;
  67. class LocalMapping;
  68. class LoopClosing;
  69. class Settings;
  70. class System
  71. {
  72. public:
  73. // Input sensor
  74. enum eSensor{
  75. MONOCULAR=0,
  76. STEREO=1,
  77. RGBD=2,
  78. IMU_MONOCULAR=3,
  79. IMU_STEREO=4,
  80. IMU_RGBD=5,
  81. };
  82. // File type
  83. enum FileType{
  84. TEXT_FILE=0,
  85. BINARY_FILE=1,
  86. };
  87. public:
  88. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  89. // Initialize the SLAM system. It launches the Local Mapping, Loop Closing and Viewer threads.
  90. System(const string &strVocFile, const string &strSettingsFile, const eSensor sensor, const bool bUseViewer = true, const int initFr = 0, const string &strSequence = std::string());
  91. // Proccess the given stereo frame. Images must be synchronized and rectified.
  92. // Input images: RGB (CV_8UC3) or grayscale (CV_8U). RGB is converted to grayscale.
  93. // Returns the camera pose (empty if tracking fails).
  94. Sophus::SE3f TrackStereo(const cv::Mat &imLeft, const cv::Mat &imRight, const double &timestamp, const vector<IMU::Point>& vImuMeas = vector<IMU::Point>(), string filename="");
  95. // Process the given rgbd frame. Depthmap must be registered to the RGB frame.
  96. // Input image: RGB (CV_8UC3) or grayscale (CV_8U). RGB is converted to grayscale.
  97. // Input depthmap: Float (CV_32F).
  98. // Returns the camera pose (empty if tracking fails).
  99. Sophus::SE3f TrackRGBD(const cv::Mat &im, const cv::Mat &depthmap, const double &timestamp, const vector<IMU::Point>& vImuMeas = vector<IMU::Point>(), string filename="");
  100. // Proccess the given monocular frame and optionally imu data
  101. // Input images: RGB (CV_8UC3) or grayscale (CV_8U). RGB is converted to grayscale.
  102. // Returns the camera pose (empty if tracking fails).
  103. Sophus::SE3f TrackMonocular(const cv::Mat &im, const double &timestamp, const vector<IMU::Point>& vImuMeas = vector<IMU::Point>(), string filename="");
  104. // This stops local mapping thread (map building) and performs only camera tracking.
  105. void ActivateLocalizationMode();
  106. // This resumes local mapping thread and performs SLAM again.
  107. void DeactivateLocalizationMode();
  108. // Returns true if there have been a big map change (loop closure, global BA)
  109. // since last call to this function
  110. bool MapChanged();
  111. // Reset the system (clear Atlas or the active map)
  112. void Reset();
  113. void ResetActiveMap();
  114. // All threads will be requested to finish.
  115. // It waits until all threads have finished.
  116. // This function must be called before saving the trajectory.
  117. void Shutdown();
  118. bool isShutDown();
  119. // Save camera trajectory in the TUM RGB-D dataset format.
  120. // Only for stereo and RGB-D. This method does not work for monocular.
  121. // Call first Shutdown()
  122. // See format details at: http://vision.in.tum.de/data/datasets/rgbd-dataset
  123. void SaveTrajectoryTUM(const string &filename);
  124. // Save keyframe poses in the TUM RGB-D dataset format.
  125. // This method works for all sensor input.
  126. // Call first Shutdown()
  127. // See format details at: http://vision.in.tum.de/data/datasets/rgbd-dataset
  128. void SaveKeyFrameTrajectoryTUM(const string &filename);
  129. void SaveTrajectoryEuRoC(const string &filename);
  130. void SaveKeyFrameTrajectoryEuRoC(const string &filename);
  131. void SaveTrajectoryEuRoC(const string &filename, Map* pMap);
  132. void SaveKeyFrameTrajectoryEuRoC(const string &filename, Map* pMap);
  133. // Save data used for initialization debug
  134. void SaveDebugData(const int &iniIdx);
  135. // Save camera trajectory in the KITTI dataset format.
  136. // Only for stereo and RGB-D. This method does not work for monocular.
  137. // Call first Shutdown()
  138. // See format details at: http://www.cvlibs.net/datasets/kitti/eval_odometry.php
  139. void SaveTrajectoryKITTI(const string &filename);
  140. // TODO: Save/Load functions
  141. // SaveMap(const string &filename);
  142. // LoadMap(const string &filename);
  143. // Information from most recent processed frame
  144. // You can call this right after TrackMonocular (or stereo or RGBD)
  145. int GetTrackingState();
  146. std::vector<MapPoint*> GetTrackedMapPoints();
  147. std::vector<cv::KeyPoint> GetTrackedKeyPointsUn();
  148. // For debugging
  149. double GetTimeFromIMUInit();
  150. bool isLost();
  151. bool isFinished();
  152. void ChangeDataset();
  153. float GetImageScale();
  154. #ifdef REGISTER_TIMES
  155. void InsertRectTime(double& time);
  156. void InsertResizeTime(double& time);
  157. void InsertTrackTime(double& time);
  158. #endif
  159. private:
  160. void SaveAtlas(int type);
  161. bool LoadAtlas(int type);
  162. string CalculateCheckSum(string filename, int type);
  163. // Input sensor
  164. eSensor mSensor;
  165. // ORB vocabulary used for place recognition and feature matching.
  166. ORBVocabulary* mpVocabulary;
  167. // KeyFrame database for place recognition (relocalization and loop detection).
  168. KeyFrameDatabase* mpKeyFrameDatabase;
  169. // Map structure that stores the pointers to all KeyFrames and MapPoints.
  170. //Map* mpMap;
  171. Atlas* mpAtlas;
  172. // Tracker. It receives a frame and computes the associated camera pose.
  173. // It also decides when to insert a new keyframe, create some new MapPoints and
  174. // performs relocalization if tracking fails.
  175. Tracking* mpTracker;
  176. // Local Mapper. It manages the local map and performs local bundle adjustment.
  177. LocalMapping* mpLocalMapper;
  178. // Loop Closer. It searches loops with every new keyframe. If there is a loop it performs
  179. // a pose graph optimization and full bundle adjustment (in a new thread) afterwards.
  180. LoopClosing* mpLoopCloser;
  181. // The viewer draws the map and the current camera pose. It uses Pangolin.
  182. Viewer* mpViewer;
  183. FrameDrawer* mpFrameDrawer;
  184. MapDrawer* mpMapDrawer;
  185. // System threads: Local Mapping, Loop Closing, Viewer.
  186. // The Tracking thread "lives" in the main execution thread that creates the System object.
  187. std::thread* mptLocalMapping;
  188. std::thread* mptLoopClosing;
  189. std::thread* mptViewer;
  190. // Reset flag
  191. std::mutex mMutexReset;
  192. bool mbReset;
  193. bool mbResetActiveMap;
  194. // Change mode flags
  195. std::mutex mMutexMode;
  196. bool mbActivateLocalizationMode;
  197. bool mbDeactivateLocalizationMode;
  198. // Shutdown flag
  199. bool mbShutDown;
  200. // Tracking state
  201. int mTrackingState;
  202. std::vector<MapPoint*> mTrackedMapPoints;
  203. std::vector<cv::KeyPoint> mTrackedKeyPointsUn;
  204. std::mutex mMutexState;
  205. //
  206. string mStrLoadAtlasFromFile;
  207. string mStrSaveAtlasToFile;
  208. string mStrVocabularyFilePath;
  209. Settings* settings_;
  210. };
  211. }// namespace ORB_SLAM
  212. #endif // SYSTEM_H