NDEVR
API Documentation
KeyFrame.h
1#pragma once
2#include "DLLInfo.h"
3#include <NDEVR/RWLock.h>
4#include "OrbSLAM/Headers/MapPoint.h"
5#include "OrbSLAM/Thirdparty/DBoW2/DBoW2/BowVector.h"
6#include "OrbSLAM/Headers/ORBVocabulary.h"
7#include "OrbSLAM/Headers/ORBextractor.h"
8#include "OrbSLAM/Headers/Frame.h"
9#include <NDEVR/IntegratedRotation.h>
10#include <NDEVR/GlobalPosition.h>
11#include "GraphOptimization/Headers/GeometricCamera.h"
12#include "Base/Headers/Buffer.hpp"
13#include "Base/Headers/Set.h"
14#include <mutex>
15namespace NDEVR
16{
17 class Map;
18 class MapPoint;
19 class Frame;
20 class KeyFrameDatabase;
21 class InfoPipe;
22 class GeometricCamera;
23
30 class KeyFrame : public Frame
31 {
32 public:
38 KeyFrame(Frame& F, Map* pMap, KeyFrameDatabase* pKFDB);
42 void setPose(const Sophus::SE3<g_type>& Tcw) final override;
46 void setLinearVelocity(const Eigen::Vector3<g_type>& Vw_) final override;
50 bool doNotReduce() const { return m_do_not_reduce; }
54 void setDoNotReduce(bool do_not_reduce) const { m_do_not_reduce = do_not_reduce; }
58 Sophus::SE3<g_type> pose() const override;
62 Map* map() const
63 {
64 return mpMap;
65 }
66 Sophus::SE3<g_type> inversePose() const override
67 {
68 std::unique_lock<std::mutex> lock(m_mutex_pose);
69 return mTwc;
70 }
71 Sophus::SE3<g_type> poseInverse() const
72 {
73 std::unique_lock<std::mutex> lock(m_mutex_pose);
74 if (!isBad())
75 return mTwc;
76 //computer for bad
77 const KeyFrame* pKF = this;
78 Sophus::SE3<g_type> Trw;
79 // If the reference keyframe was culled, traverse the spanning tree to get a suitable keyframe.
80 while (pKF && pKF->isBad())
81 {
82 Trw = Trw * pKF->mTcp;
83 pKF = pKF->parent();
84 }
85 if(pKF)
86 Trw = Trw * pKF->mTcw;
87 auto lit = mTcw * mpReferenceKF->mTwc;
88 Sophus::SE3<g_type> Tcw = lit * Trw;
89 return Tcw.inverse();
90 }
91 std::mutex& poseMutex() const { return m_mutex_pose; }
92 Eigen::Vector3<g_type> cameraCenter() const
93 {
94 std::unique_lock<std::mutex> lock(m_mutex_pose);
95 return mTwc.translation();
96 }
97
98 Eigen::Vector3<g_type> GetImuPosition() const final override;
99 Eigen::Matrix3<g_type> GetImuRotation() const final override;
100 Sophus::SE3<g_type> imuPose() const final override;
101 Eigen::Matrix3<g_type> GetRotation() const final override
102 {
103 std::unique_lock<std::mutex> lock(m_mutex_pose);
104 return mTcw.rotationMatrix();
105 }
106 Eigen::Vector3<g_type> GetTranslation() const final override
107 {
108 std::unique_lock<std::mutex> lock(m_mutex_pose);
109 return mTcw.translation();
110 }
111 Eigen::Vector3<g_type> linearVelocity() const final override;
112 bool isVelocitySet() const;
122
140 void covisiblesByWeight(uint04 w, Buffer<KeyFrame*>& frames) const;
145 uint04 weight(KeyFrame* pKF) const;
146
150 void AddChild(KeyFrame* pKF);
167 {
168 return mpParent;
169 }
170
174 bool hasChild(KeyFrame* pKF);
178 void SetFirstConnection(bool bFirst);
179 bool isFirstConnection() const
180 {
181 return mbFirstConnection;
182 }
195
209 bool IsInImage(const float& x, const float& y) const;
210
214 void SetErase();
223 bool isBad() const
224 {
225 return mbBad;
226 }
227
232 float ComputeSceneMedianDepth(const int q);
233
234 static bool weightComp(const std::pair<uint04, KeyFrame*>& a, const std::pair<uint04, KeyFrame*>& b)
235 {
236 return a.first > b.first;
237 }
238
239 static bool lId(KeyFrame* pKF1, KeyFrame* pKF2) { return pKF1->id < pKF2->id; }
247 void UpdateMap(Map* pMap);
251 void addGlobalPositionMeas(const GlobalPosition* meas);
255 void SetNewBias(const IMU::Bias& b);
259 Eigen::Vector3f GetGyroBias();
263 Eigen::Vector3f GetAccBias();
267 IMU::Bias GetImuBias();
276
277 bool bImu = false;
278
279 public:
280
282
287 {
288 uint04 query_id = Constant<uint04>::Invalid;
290 fltp04 score = 0.0f;
291 };
292
301 // Variables used by loop closing
302 Sophus::SE3<g_type> mTcwGBA;
303 Sophus::SE3<g_type> mTcwBefGBA;
304 Eigen::Vector3<g_type> mVwbGBA;
305 IMU::Bias mBiasGBA;
306 uint04 mnBAGlobalForKF = 0U;
307
308 // Variables used by merging
309 Sophus::SE3<g_type> mTcwMerge;
310 Sophus::SE3<g_type> mTcwBefMerge;
311 Sophus::SE3<g_type> mTwcBefMerge;
312 Eigen::Vector3<g_type> mVwbMerge;
313 Eigen::Vector3<g_type> mVwbBefMerge;
314 IMU::Bias mBiasMerge;
315 uint04 mnBALocalForMerge = 0U;
316
317 static volatile uint04 s_next_kf_id;
318
319
320 Sophus::SE3<g_type> mTcp;
321
322
323 KeyFrame* mPrevKF = nullptr;
324 KeyFrame* mNextKF = nullptr;
325 protected:
326 bool m_has_global_position_measurement = false;
327 Buffer<const GlobalPosition*> m_global_position_meas;
328 Buffer<const GlobalPosition*> m_global_position_meas_before_prop;
329 // BoW
330 KeyFrameDatabase* mpKeyFrameDB = nullptr;
331
332 // Grid over the image to speed up feature matching
333
334 Dictionary<KeyFrame*, uint04> m_connected_keyframe_weights;
335 Buffer<std::pair<uint04, KeyFrame*>> m_ordered_connected_key_frames;
336
337 // Spanning Tree and Loop Edges
338 bool mbFirstConnection = false;
339 KeyFrame* mpParent = nullptr;
340 Set<KeyFrame*> m_child_frames;
341 Set<KeyFrame*> mspLoopEdges;
342 Set<KeyFrame*> mspMergeEdges;
343
344 std::map<double, Sophus::SE3f> frameRelativePoses;
345 std::map<double, Sophus::SE3f> posesRelativeToKF;
346 // Bad flags
347 bool mbNotErase = false;
348 bool mbToBeErased = false;
349 bool mbBad = false;
350 mutable bool m_do_not_reduce = false;
351 Map* mpMap = nullptr;
352
353
354 // Mutex
355 mutable std::mutex m_mutex_gps;
356 mutable std::mutex m_mutex_pose; // for pose, velocity and biases
357 mutable std::mutex mMutexConnections;
358 mutable std::mutex mMutexMap;
359
360 public:
361 uint04 frame_id = Constant<uint04>::Invalid;
362 uint04 keyframe_id = Constant<uint04>::Invalid;
363
367 Sophus::SE3f GetRelativePoseTrl();
371 Sophus::SE3f GetRelativePoseTlr();
372
376 Sophus::SE3<float> GetRightPose();
380 Sophus::SE3<float> GetRightPoseInverse();
381
385 Eigen::Vector3f GetRightCameraCenter();
389 Eigen::Matrix<float, 3, 3> GetRightRotation();
393 Eigen::Vector3f GetRightTranslation();
394
395 };
396
397}
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
A hash-based key-value store, useful for quick associative lookups.
Definition Dictionary.h:64
Represents a single image frame with extracted features and pose information.
Definition Frame.h:111
Frame()=default
Default constructor.
uint04 id
Unique frame ID.
Definition Frame.h:588
KeyFrame * mpReferenceKF
Reference keyframe.
Definition Frame.h:576
Sophus::SE3< g_type > mTcw
World-to-camera transform.
Definition Frame.h:557
Sophus::SE3< g_type > mTwc
Camera-to-world transform (inverse of mTcw).
Definition Frame.h:558
const Eigen::Vector3< g_type > & cameraCenter() const
Returns the camera center in world coordinates.
Definition Frame.h:379
uint04 N
Number of keypoints.
Definition Frame.h:592
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
void updateBestCovisibles()
Sorts covisible keyframes by weight.
Eigen::Vector3f GetRightTranslation()
Returns the right camera translation vector.
Eigen::Vector3< g_type > GetImuPosition() const final override
Returns the IMU position in world coordinates.
uint04 keyframe_id
Unique keyframe ID.
Definition KeyFrame.h:362
void ChangeParent(KeyFrame *pKF)
Changes the parent of this keyframe in the spanning tree.
Buffer< const GlobalPosition * > getGlobalPositionMeas() const
Returns global position measurements associated with this keyframe.
void setLinearVelocity(const Eigen::Vector3< g_type > &Vw_) final override
Sets the IMU linear velocity (thread-safe).
Map * GetMap()
Returns the map this keyframe belongs to.
void EraseChild(KeyFrame *pKF)
Removes a child keyframe from the spanning tree.
Set< KeyFrame * > GetMergeEdges()
Returns all merge edges.
void bestCovisibilityKeyFrames(Buffer< KeyFrame * > &frames, uint04 N=Constant< uint04 >::Max) const
Returns the N best covisible keyframes by weight.
Sophus::SE3< float > GetRightPose()
Returns the right camera pose in world coordinates.
Eigen::Vector3< g_type > GetTranslation() const final override
Returns the camera translation vector (camera-to-world).
Definition KeyFrame.h:106
Map * map() const
Returns the map this keyframe belongs to.
Definition KeyFrame.h:62
Sophus::SE3< g_type > mTcp
Pose relative to parent (computed when bad flag is set).
Definition KeyFrame.h:320
static volatile uint04 s_next_kf_id
Global keyframe ID counter.
Definition KeyFrame.h:317
void SetNotErase()
Prevents this keyframe from being erased.
void SetBadFlag()
Marks this keyframe as bad and removes it from the map.
KeyFrameScore scores[4]
Scores indexed by ScoreEnum.
Definition KeyFrame.h:300
Eigen::Vector3< g_type > linearVelocity() const final override
Returns the linear velocity of the frame.
void setPose(const Sophus::SE3< g_type > &Tcw) final override
Sets the camera pose (thread-safe).
Set< KeyFrame * > GetChilds()
Returns the children of this keyframe in the spanning tree.
Sophus::SE3f GetRelativePoseTrl()
Returns the relative pose from right to left camera.
uint04 frame_id
Original frame ID.
Definition KeyFrame.h:361
Eigen::Matrix< float, 3, 3 > GetRightRotation()
Returns the right camera rotation matrix.
uint04 mnFuseTargetForKF
Fuse target keyframe ID for tracking.
Definition KeyFrame.h:281
bool hasChild(KeyFrame *pKF)
Checks whether a keyframe is a child of this one.
Set< KeyFrame * > GetConnectedKeyFrames()
Returns all connected keyframes in the covisibility graph.
Sophus::SE3< g_type > inversePose() const override
Returns the inverse camera pose (camera-to-world transform).
Definition KeyFrame.h:66
Eigen::Vector3f GetGyroBias()
Returns the gyroscope bias.
Eigen::Vector3f GetRightCameraCenter()
Returns the right camera center in world coordinates.
void processGlobalPositionMeas()
Processes global position measurements for this keyframe.
float ComputeSceneMedianDepth(const int q)
Computes the median scene depth.
void SetFirstConnection(bool bFirst)
Sets the first-connection flag.
KeyFrame * mPrevKF
Previous keyframe in temporal order.
Definition KeyFrame.h:323
Set< KeyFrame * > GetLoopEdges()
Returns all loop closure edges.
ScoreEnum
Enumerates the types of place-recognition scoring.
Definition KeyFrame.h:294
@ e_place_recognition
General place recognition score.
Definition KeyFrame.h:298
@ e_merge
Merge detection score.
Definition KeyFrame.h:295
@ e_relocation
Relocalization score.
Definition KeyFrame.h:297
@ e_loop
Loop detection score.
Definition KeyFrame.h:296
void eraseMapPoint(MapPoint *pMP)
Removes a map point observation from this keyframe.
void SetErase()
Allows this keyframe to be erased.
void AddMergeEdge(KeyFrame *pKF)
Adds a merge edge to another keyframe.
void covisiblesByWeight(uint04 w, Buffer< KeyFrame * > &frames) const
Returns keyframes with covisibility weight at least w.
uint04 weight(KeyFrame *pKF) const
Returns the covisibility weight with another keyframe.
bool doNotReduce() const
Checks whether this keyframe should not be reduced/culled.
Definition KeyFrame.h:50
void SetKeyFrameDatabase(KeyFrameDatabase *pKFDB)
Sets the keyframe database for this keyframe.
KeyFrame * parent() const
Returns the parent keyframe in the spanning tree.
Definition KeyFrame.h:166
void SetNewBias(const IMU::Bias &b)
Updates the IMU bias for this keyframe.
bool bImu
Whether this keyframe uses IMU data.
Definition KeyFrame.h:277
void EraseConnection(KeyFrame *pKF)
Removes a covisibility connection.
Sophus::SE3< g_type > pose() const override
Returns the camera pose (thread-safe).
void SetORBVocabulary(ORBVocabulary *pORBVoc)
Sets the ORB vocabulary for this keyframe.
Eigen::Vector3f GetAccBias()
Returns the accelerometer bias.
KeyFrame(Frame &F, Map *pMap, KeyFrameDatabase *pKFDB)
Constructs a KeyFrame from a Frame.
void addConnection(KeyFrame *pKF, uint04 weight)
Adds a covisibility connection to another keyframe.
void updateConnections()
Updates all covisibility connections based on shared map points.
KeyFrame * mNextKF
Next keyframe in temporal order.
Definition KeyFrame.h:324
Eigen::Matrix3< g_type > GetRotation() const final override
Returns the camera rotation matrix (camera-to-world).
Definition KeyFrame.h:101
bool IsInImage(const float &x, const float &y) const
Checks whether a point is within the image bounds.
void AddLoopEdge(KeyFrame *pKF)
Adds a loop closure edge to another keyframe.
void addGlobalPositionMeas(const GlobalPosition *meas)
Adds a global position measurement to this keyframe.
void UpdateMap(Map *pMap)
Updates the map association for this keyframe.
Sophus::SE3< float > GetRightPoseInverse()
Returns the inverse of the right camera pose.
Sophus::SE3< g_type > imuPose() const final override
Returns the IMU pose as an SE3 transform.
Sophus::SE3f GetRelativePoseTlr()
Returns the relative pose from left to right camera.
uint04 mnBAFixedForKF
Fixed BA keyframe ID marker.
Definition KeyFrame.h:284
IMU::Bias GetImuBias()
Returns the full IMU bias (gyroscope + accelerometer).
void setDoNotReduce(bool do_not_reduce) const
Sets whether this keyframe should not be reduced/culled.
Definition KeyFrame.h:54
uint04 mnBALocalForKF
Local BA keyframe ID marker.
Definition KeyFrame.h:283
void AddChild(KeyFrame *pKF)
Adds a child keyframe in the spanning tree.
Eigen::Matrix3< g_type > GetImuRotation() const final override
Returns the IMU rotation matrix.
A 3D point in the SLAM map observed by multiple keyframes.
Definition MapPoint.h:24
Represents a single SLAM map containing keyframes and map points.
Definition Map.h:22
Container that stores unique elements in no particular order, and which allow for fast retrieval or i...
Definition Set.h:59
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...
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Stores place-recognition query scores for a keyframe.
Definition KeyFrame.h:287
fltp04 score
Similarity score.
Definition KeyFrame.h:290
uint04 query_id
Query keyframe ID.
Definition KeyFrame.h:288
uint04 word_count
Shared word count.
Definition KeyFrame.h:289