NDEVR
API Documentation
Tracking.h
1#pragma once
2#include <opencv2/core/core.hpp>
3#include <opencv2/features2d/features2d.hpp>
4#include "OrbTrackingInfo.h"
5#include "OrbSLAM/Headers/Optimizer.h"
6#include "OrbSLAM/Headers/Frame.h"
7#include "OrbSLAM/Headers/SLAMParameters.h"
8#include "OrbSLAM/Headers/ORBmatcher.h"
9#include "Base/Headers/Vector.hpp"
10#include <NDEVR/GlobalPosition.h>
11#include <mutex>
12
13namespace NDEVR
14{
15 struct FrameInfo;
16 class Atlas;
17 class LocalMapping;
18 class LoopClosing;
19 class OrbSLAM;
20 class Settings;
21 class MapDrawer;
22 class SLAMParameters;
23 class KeyFrameDatabase;
24 class Map;
25 class InfoPipe;
33 {
34 public:
46
54 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
63 Tracking(OrbSLAM* pSys, ORBVocabulary* pVoc, Atlas* pAtlas
64 , KeyFrameDatabase* pKFDB, const SLAMParameters& params, LogPtr log);
65
71 void setParams(const SLAMParameters& params);
76 bool parseCamParamFile(const SLAMParameters& params);
81 bool parseORBParamFile(const SLAMParameters& params);
86 bool parseIMUParamFile(const SLAMParameters& params);
87
92 Sophus::SE3f GrabImageStereo(OrbTrackingInfo& info);
97 Sophus::SE3f GrabImageRGBD(OrbTrackingInfo& info);
107 void appendImuData(Buffer<IMU::Point> imu_measurements, fltp08 time_offset);
112 void appendGPSData(const Buffer<GlobalPosition*> gps_measurements, fltp08 time_offset);
113
117 void SetLocalMapper(LocalMapping* pLocalMapper);
121 void SetLoopClosing(LoopClosing* pLoopClosing);
125 eTrackingState state() const { return m_state; };
129 void setState(eTrackingState state) { m_state = state; };
130
134 void InformOnlyTracking(bool flag);
135
141 void UpdateFrameIMU(fltp04 scale, const IMU::Bias& b, KeyFrame* pCurrentKeyFrame);
145 KeyFrame* GetLastKeyFrame() { return m_last_keyframe; }
149 const Sensor& sensor() const { return m_sensor; }
155 int GetMatchesInliers() { return mnMatchesInliers; };
160 bool ensureValidData(const cv::Mat& imRectLeft) const;
161
162 public:
163
164 eTrackingState m_state;
165
166 // Input sensor
167 Sensor m_sensor;
168
169 // Current Frame
170 Frame m_pending_frame;
171 Frame m_current_frame;
172 Frame m_last_frame;
173 Frame m_mono_initial_frame;
174
175 // Initialization Variables (Monocular)
176 Buffer<uint04> m_mono_initial_matches;
177 Buffer<cv::Point2f> m_mono_prev_matched;
178 Buffer<cv::Point3f> mvIniP3D;
179
180 // Lists used to recover the full camera trajectory at the end of the execution.
181 // Basically we store the reference keyframe for each frame and its relative transformation
182 Buffer<Sophus::SE3f> mlRelativeFramePoses;
183 Buffer<KeyFrame*> mlpReferences;
184 Buffer<bool> mlbLost;
185
186 // frames with estimated pose
187 bool mbStep = false;
188
189 // True if local mapping is deactivated and we are performing only localization
190 bool mbOnlyTracking = false;
191
192 void reset(bool bLocMap = false);
193 void ResetActiveMap(bool bLocMap = false);
194
195 double t0 = 0.0; // time-stamp of first read frame
196 double t0IMU = 0.0; // time-stamp of IMU initialization
197 bool mFastInit = false;
198
199
200 Buffer<MapPoint*> GetLocalMapMPS();
201 protected:
202
203 bool hugechangeornot(const cv::Mat& depth1, const cv::Mat& depth2);
204 bool TrackWithMotionSensor(OrbTrackingInfo& info);
205 bool TrackLocalMapwithSensor(OrbTrackingInfo& info);
206 void Track_withsensor(OrbTrackingInfo& info);
207 void mergeImg(cv::Mat& dst, cv::Mat& src1, cv::Mat& src2);
208
209protected:
210 //addby ruyu
211 void RGBDInitialiuliu();
212 void updateStateIMU();
213
214 // Main tracking function. It is independent of the input sensor.
215 void Track(OrbTrackingInfo& info);
216 //Tracks and creates new keypoints
217 bool trackWithLocalMapping(Map* map, OrbTrackingInfo& info);
218 //Tracks using existing keypoints
219 bool trackWithoutLocalMapping(Map* map, OrbTrackingInfo& info);
220 // Map initialization for stereo and RGB-t_dim
221 void StereoInitialization();
222
223 // Map initialization for monocular
224 void MonocularInitialization();
225 //void CreateNewMapPoints();
226 void CreateInitialMapMonocular();
227
228 void CheckReplacedInLastFrame();
229 bool TrackReferenceKeyFrame(OrbTrackingInfo& info);
230 void UpdateLastFrame();
231 bool TrackWithMotionModel(OrbTrackingInfo& info);
232 bool PredictStateIMU();
233
234 bool relocalization(OrbTrackingInfo& info);
235
236 void UpdateLocalMap();
237 void UpdateLocalPoints();
238 void UpdateLocalKeyFrames();
239
240 bool TrackLocalMap(OrbTrackingInfo& info);
241 void SearchLocalPoints();
242
243 bool needsNewKeyFrame();
244 void createNewKeyFrame();
245
246 // Perform preintegration from last frame
247 void preIntegrateIMU();
248
249 // reset IMU biases and compute frame velocity
250 void ResetFrameIMU();
251 protected:
252 Optimizer m_optimizer;
253 fltp08 m_last_sensor_pose_time = Constant<fltp08>::Invalid;
254 fltp08 m_sensor_pose_time = Constant<fltp08>::Invalid;
255 Sophus::SE3<g_type> m_sensor_pose;
256 Sophus::SE3<g_type> m_last_sensor_pose;
257 ORBmatcher m_matcher;
258 bool mbMapUpdated = false;
259
260 // Imu preintegration from last frame
261 IMU::Preintegrated* mpImuPreintegratedFromLastKF;
262
263 // Queue of IMU measurements between frames
264 Buffer<IMU::Point> m_imu_queue;
265 std::mutex m_mutex_imu_queue;
266 std::mutex m_mutex_gps_queue;
267 std::mutex m_mutex_current_image;
268 // Imu calibration parameters
269 IMU::Calib* m_imu_calib;
270
271 // Last Bias Estimation (at keyframe creation)
272 IMU::Bias mLastBias;
273
274 // In case of performing only localization, this flag is true when there are no matches to
275 // points in the map. Still tracking will continue if there are enough matches with temporal points.
276 // In that case we are doing visual odometry. The system will try to do relocalization to recover
277 // "zero-drift" localization to the map.
278 bool mbVO = false;
279
280 //Other Thread Pointers
281 LocalMapping* mpLocalMapper;
282 LoopClosing* mpLoopClosing;
283
284 //ORB
285 Vector<2, ORBextractor*> m_orb_extractor;
286
287 //BoW
288 ORBVocabulary* mpORBVocabulary;
289 KeyFrameDatabase* mpKeyFrameDB;
290
291 // Initialization (only for monocular)
292 bool m_mono_ready_to_initialize = false;
293
294 //Local Map
295 KeyFrame* m_reference_keyframe;
296 Buffer<KeyFrame*> m_local_key_frames;
297 Buffer<MapPoint*> mvpLocalMapPoints;
298 Buffer<MapPoint*> m_map_point_matches;
299 Buffer<MapPointProjection> m_local_projected_points;
300 // System
301 OrbSLAM* mpSystem = nullptr;
302
303 //Atlas
304 Atlas* m_atlas = nullptr;
305
306 //Calibration matrix
307 cv::Mat mK;
308 cv::Mat mDistCoef;
309 fltp04 mbf = 0.0f;
310
311 fltp08 mImuPer = 0.0f;
312 //New KeyFrame rules (according to fps)
313 uint04 mMinFrames = 0;
314 uint04 mMaxFrames = 0;
315
316 uint04 mnFramesToResetIMU = 0;
317
318 // Threshold close/far points
319 // Points seen as close by the stereo/RGBD sensor are considered reliable
320 // and inserted from just one frame. Far points requiere a match in two keyframes.
321 fltp04 near_far_threshold = 0.0f;
322
323 // Depth map scale factor (converts raw depth values to meters)
324 fltp04 m_depth_map_factor = 1.0f;
325
326 //Current matches in frame
327 uint04 mnMatchesInliers = 0;
328
329 //Last Frame, KeyFrame and Relocalisation Info
330 KeyFrame* m_last_keyframe = nullptr;
331 uint04 m_frame_id_of_last_kf = 0U;
332 uint04 mnLastRelocFrameId = 0U;
333 fltp08 m_time_stamp_lost = 0.0;
334 fltp08 time_recently_lost = 0.0;
335
336 uint04 mn_first_frame_ID = 0U;
337 uint04 mnInitialFrameId = 0U;
338
339 bool mbCreatedMap = false;
340
341 //Motion Model
342 bool mbVelocity = false;
343 Sophus::SE3<g_type>::Tangent mVelocity;
344
345 Buffer<MapPoint*> mlpTemporalPoints;
346
348 const GlobalPosition* globalPositionOrigin;
349 bool m_set_global_pos_origin = false;
350 GPSCalib mpGlobalMeasCalib;
351 GlobalFrameAlignmentState m_global_frame_alignment_state;
352 //int nMapChangeIndex;
353 LogPtr m_log;
354 FrameInfo* m_frame_info = nullptr;
355 GeometricCamera* mpCamera = nullptr;
356 GeometricCamera* mpCamera2 = nullptr;
357
358 uint04 initID = 0;
359 uint04 m_last_monocular_ID = 0;
360
361 Sophus::SE3f m_stereo_tlr;
362 protected:
363 bool mInsertKFsLost = false;
364 };
365
366}
Manages a collection of maps for the ORB-SLAM system.
Definition Atlas.h:29
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
Represents a single image frame with extracted features and pose information.
Definition Frame.h:111
Abstract interface for geometric camera models (pinhole, fisheye, etc.).
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,...
Detects and corrects loop closures and map merges.
Definition LoopClosing.h:29
Represents a single SLAM map containing keyframes and map points.
Definition Map.h:22
Matches ORB features between frames, keyframes, and map points.
Definition ORBmatcher.h:26
Provides graph optimization routines for the ORB-SLAM system.
Definition Optimizer.h:34
Main entry point for the ORB-SLAM3 system.
Definition System.h:37
Configuration parameters for the ORB-SLAM system.
eTrackingState state() const
Returns the current tracking state.
Definition Tracking.h:125
bool parseIMUParamFile(const SLAMParameters &params)
Parses IMU parameters from SLAM configuration.
void UpdateFrameIMU(fltp04 scale, const IMU::Bias &b, KeyFrame *pCurrentKeyFrame)
Updates frame IMU data after scale/bias refinement.
KeyFrame * GetLastKeyFrame()
Returns the last keyframe.
Definition Tracking.h:145
Sophus::SE3f GrabImageMonocular(OrbTrackingInfo &info)
Processes a monocular image and returns the estimated pose.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Tracking(OrbSLAM *pSys, ORBVocabulary *pVoc, Atlas *pAtlas, KeyFrameDatabase *pKFDB, const SLAMParameters &params, LogPtr log)
Constructs the tracker.
Sophus::SE3f GrabImageRGBD(OrbTrackingInfo &info)
Processes an RGB-D frame and returns the estimated pose.
void setState(eTrackingState state)
Sets the tracking state.
Definition Tracking.h:129
eTrackingState
Tracking state enumeration.
Definition Tracking.h:37
@ OK
Tracking is successful.
Definition Tracking.h:41
@ OK_KLT
Tracking via KLT optical flow.
Definition Tracking.h:44
@ NOT_INITIALIZED
Map not yet initialized.
Definition Tracking.h:40
@ RECENTLY_LOST
Recently lost tracking.
Definition Tracking.h:42
@ SYSTEM_NOT_READY
System not yet ready.
Definition Tracking.h:38
@ LOST
Tracking is lost.
Definition Tracking.h:43
@ NO_IMAGES_YET
No images have been received.
Definition Tracking.h:39
bool parseCamParamFile(const SLAMParameters &params)
Parses camera parameters from SLAM configuration.
void CreateMapInAtlas()
Creates a new map in the Atlas.
void appendGPSData(const Buffer< GlobalPosition * > gps_measurements, fltp08 time_offset)
Appends GPS measurements to the processing queue.
void appendImuData(Buffer< IMU::Point > imu_measurements, fltp08 time_offset)
Appends IMU measurements to the processing queue.
bool ensureValidData(const cv::Mat &imRectLeft) const
Validates that the input image data is usable.
int GetMatchesInliers()
Returns the number of inlier matches in the current frame.
Definition Tracking.h:155
void SetLocalMapper(LocalMapping *pLocalMapper)
Sets the local mapper reference.
void InformOnlyTracking(bool flag)
Enables or disables localization-only tracking.
GlobalFrameAlignmentState
State of global frame alignment (GPS/global position).
Definition Tracking.h:48
@ NO_MEAS_YET
No global measurements received.
Definition Tracking.h:49
@ FIRST_GLOBAL_MEAS_SET
First global measurement has been set.
Definition Tracking.h:50
@ ALIGNING
Alignment in progress.
Definition Tracking.h:51
@ ALIGNED
Global alignment complete.
Definition Tracking.h:52
~Tracking()
Destructor.
bool parseORBParamFile(const SLAMParameters &params)
Parses ORB extractor parameters from SLAM configuration.
const Sensor & sensor() const
Returns the sensor configuration.
Definition Tracking.h:149
void SetLoopClosing(LoopClosing *pLoopClosing)
Sets the loop closer reference.
Sophus::SE3f GrabImageStereo(OrbTrackingInfo &info)
Processes a stereo image pair and returns the estimated pose.
void setParams(const SLAMParameters &params)
Updates SLAM parameters.
A fixed-size array with N dimensions used as the basis for geometric and mathematical types.
Definition Vector.hpp:62
The primary namespace for the NDEVR SDK.
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
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...
double fltp08
Defines an alias representing an 8 byte floating-point number.
Shared camera intrinsic and image metadata for frames.
Definition Frame.h:70
Aggregates input and output data for a single tracking frame.
Describes the input sensor type and IMU usage.