NDEVR
API Documentation
Optimizer.h
1#pragma once
2#include "OrbSLAM/Headers/Map.h"
3#include "OrbSLAM/Headers/MapPoint.h"
4#include "OrbSLAM/Headers/Frame.h"
5#include "Base/Headers/Dictionary.h"
6#include <math.h>
7
8#include "GraphOptimization/Headers/types_seven_dof_expmap.h"
9#include "GraphOptimization/Headers/types_six_dof_expmap.h"
10#include "GraphOptimization/Headers/sparse_block_matrix.h"
11#include "GraphOptimization/Headers/block_solver.h"
12#include "GraphOptimization/Headers/optimization_algorithm_levenberg.h"
13#include "GraphOptimization/Headers/optimization_algorithm_gauss_newton.h"
14#include "GraphOptimization/Headers/linear_solver_eigen.h"
15#include "GraphOptimization/Headers/robust_kernel_impl.h"
16#include "GraphOptimization/Headers/linear_solver_dense.h"
17#include "GraphOptimization/Headers/OptimizableTypes.h"
18#include "GraphOptimization/Headers/sparse_optimizer.h"
19#include "GraphOptimization/Headers/edge_se3_expmap.h"
20#include "OrbSLAM/Headers/G2oTypes.h"
21#include "OrbSLAM/Headers/Converter.h"
22namespace NDEVR
23{
24
25class LoopClosing;
26
34{
35public:
36 typedef std::pair<Set<KeyFrame*>, int> ConsistentGroup;
37 typedef std::map<KeyFrame*, Sim3, std::less<KeyFrame*>,
38 Eigen::aligned_allocator<std::pair<KeyFrame* const, Sim3>>> KeyFrameAndPose;
39 void bundleAdjustment(const Buffer<KeyFrame*> &vpKF, const Buffer<MapPoint*> &vpMP, int nIterations = 5, bool *pbStopFlag = nullptr, const uint04 nLoopKF=0, const bool bRobust = true);
40 void globalBundleAdjustment(Map* pMap, int nIterations=5, bool *pbStopFlag=nullptr, const uint04 nLoopKF = 0, const bool bRobust = true);
41 void fullInertialBA(Map *pMap, int its, const bool bFixLocal=false, const uint04 nLoopKF = 0, bool* pbStopFlag = nullptr, bool bInit=false, float priorG = 1e2, float priorA=1e6);
42
43 void localBundleAdjustment(KeyFrame* pKF, bool *pbStopFlag, Map *pMap);
44
45 uint04 poseOptimization(Frame* pFrame);
46 uint04 poseInertialOptimizationLastFrame(Frame& pFrame, Frame* reference_frame, bool locked_frame, bool bRecInit = false);
47 template<class t_solver_type>
48 uint04 fillOutMPData(const Buffer<MapPoint*>& map_points, uint04 id_offset, t_solver_type& solver
49 , std::function<bool(KeyFrame* pKFi, MapPoint* pMP)> filter, bool use_kernels);
50 template<class t_solver_type>
51 uint04 fillOutInertialMPData(const Buffer<MapPoint*>& map_points, uint04 id_offset, t_solver_type& solver
52 , std::function<bool(KeyFrame* pKFi, MapPoint* pMP)> filter, bool use_kernels);
53 // if bFixScale is true, 6DoF optimization (stereo,rgbd), 7DoF otherwise (mono)
54 void optimizeEssentialGraph(Map* pMap, KeyFrame* pLoopKF, KeyFrame* pCurKF,
55 const KeyFrameAndPose &NonCorrectedSim3,
56 const KeyFrameAndPose &CorrectedSim3,
57 const Dictionary<KeyFrame *, Set<KeyFrame *> > &LoopConnections,
58 const bool &bFixScale);
59 void optimizeEssentialGraph(KeyFrame* pCurKF, Buffer<KeyFrame*> &vpFixedKFs, Buffer<KeyFrame*> &vpFixedCorrectedKFs,
60 Buffer<KeyFrame*> &vpNonFixedKFs, Buffer<MapPoint*> &vpNonCorrectedMPs);
61
62 // For inertial loopclosing
63 void optimizeEssentialGraph4DoF(Map* pMap, KeyFrame* pLoopKF, KeyFrame* pCurKF,
64 const KeyFrameAndPose &NonCorrectedSim3,
65 const KeyFrameAndPose &CorrectedSim3,
66 const Dictionary<KeyFrame*, Set<KeyFrame*>>& LoopConnections);
67
68
69 // if bFixScale is true, optimize SE3 (stereo,rgbd), Sim3 otherwise (mono) (NEW)
70 uint04 optimizeSim3(KeyFrame* pKF1, KeyFrame* pKF2, Buffer<MapPoint *> &vpMatches1,
71 Sim3 &ParkeS12, const float th2, const bool bFixScale, const bool bAllPoints=false);
72
73 // For inertial systems
74
75 void localInertialBA(KeyFrame* pKF, bool *pbStopFlag, bool bLarge = false, bool bRecInit = false);
76 void mergeInertialBA(KeyFrame* pCurrKF, KeyFrame* pMergeKF, bool *pbStopFlag, Map *pMap, KeyFrameAndPose &corrPoses);
77
78 // Local BA in welding area when two maps are merged
79 void localBundleAdjustment(KeyFrame* pMainKF, Buffer<KeyFrame*> vpAdjustKF, Buffer<KeyFrame*> vpFixedKF, bool *pbStopFlag);
80
81 // Marginalize block element (start:end,start:end). Perform Schur complement.
82 // Marginalized elements are filled with zeros.
83 static Eigen::MatrixX<g_type> Marginalize(const Eigen::MatrixX<g_type>& H, int start, int end);
84
85 // Inertial pose-graph
86 void inertialOptimization(Map *pMap, Eigen::Matrix3<g_type>& Rwg, fltp04& scale, Eigen::Vector3<g_type>& bg, Eigen::Vector3<g_type>& ba, bool bMono, bool bFixedVel=false, float priorG = 1e2, float priorA = 1e6);
87 void inertialOptimization(Map *pMap, Eigen::Vector3<g_type>& bg, Eigen::Vector3<g_type>& ba, float priorG = 1e2, float priorA = 1e6);
88 void inertialOptimization(Map *pMap, Eigen::Matrix3<g_type>& Rwg, fltp04& scale);
89 uint04 sensorPoseOptimization(Frame* pFrame, Frame* pLastFrame);
90 //static int Opti_MapFitModel(KeyFrame* pKF, Dictionary<int, Dictionary<int, Buffer<cv::Point3f>>>& map_2d_building, Config::PointSet all_seenbuilding)
91 EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
92 template<class t_edge>
93 struct MP : public t_edge
94 {
95 MP(t_edge&& e = t_edge())
96 : t_edge(std::forward<t_edge>(e))
97 {}
98 void eraseIfNeeded(g_type chi)
99 {
100 if (point->isBad())
101 return;
102 if (t_edge::chi2() <= chi && t_edge::isDepthPositive())
103 return;
104 {
105 WLock lock(frame->mapPointsLockPtr());
106 frame->eraseMapPoint(point);
107 }
108 {
109 WLock lock(point->featureLockPtr());
110 point->eraseObservation(frame);
111 }
112 }
113 KeyFrame* frame;
114 MapPoint* point;
115 };
116 template<class t_edge>
117 struct MI : public t_edge
118 {
119 MI(t_edge&& e = t_edge())
120 : t_edge(std::forward<t_edge>(e))
121 {}
122 uint04 index;
123 };
124
125 IndexScratch m_scratch_pad;
126 Buffer<MapPoint*> m_local_map_points;
127 Buffer<VertexSE3Expmap> m_se3_exp_map;
128 Buffer<MP<EdgeSE3ProjectXYZ>> m_edges_mono;
129 Buffer<MP<EdgeSE3ProjectXYZToBody>> m_edges_body;
130 Buffer<MP<EdgeStereoSE3ProjectXYZ>> m_edges_stereo;
131 Buffer<VertexSim3Expmap> m_exp_map;
132 Buffer<EdgeSim3> m_sim_3_edge;
133 Buffer<VertexSBAPointXYZ> m_sba_points;
134 Buffer<RobustKernelHuber> m_robust_kernels;
135
136 Buffer<MI<EdgeMonoOnlyPose>> m_inertial_mono;
137 Buffer<MI<EdgeStereoOnlyPose>> m_inertial_stereo;
138 Buffer<MI<EdgeSE3ProjectXYZOnlyPose>> m_edges_pose_mono;
139 Buffer<MI<EdgeSE3ProjectXYZOnlyPose>> m_edges_pose_mono_last;
140 Buffer<MI<EdgeSE3ProjectXYZOnlyPoseToBody>> m_edges_pose_body;
141 Buffer<MI<EdgeStereoSE3ProjectXYZOnlyPose>> m_edges_pose_stereo;
142 //inertial
143 Buffer<VertexPose> m_vertex_pose;
144 Buffer<VertexVelocity> m_vertex_velocity;
145 Buffer<VertexGyroBias> m_gyro_biases;
146 Buffer<VertexAccBias> m_acc_biases;
147 Buffer<EdgeInertialGS> m_edge_inertial_gs;
148 Buffer<EdgeInertial> m_vei;
149 Buffer<EdgeGyroRW> m_vegr;
150 Buffer<EdgeAccRW> m_vear;
151 Buffer<MP<EdgeMono>> m_inertial_edges_mono;
152 Buffer<MP<EdgeStereo>> m_inertial_edges_stereo;
153};
154
155
156}
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
RWLock & mapPointsLockPtr() const
Returns the read-write lock for map point access.
Definition Frame.h:427
A keyframe in the SLAM map, derived from Frame.
Definition KeyFrame.h:31
void eraseMapPoint(MapPoint *pMP)
Removes a map point observation from this keyframe.
Detects and corrects loop closures and map merges.
Definition LoopClosing.h:29
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
Provides graph optimization routines for the ORB-SLAM system.
Definition Optimizer.h:34
std::map< KeyFrame *, Sim3, std::less< KeyFrame * >, Eigen::aligned_allocator< std::pair< KeyFrame *const, Sim3 > > > KeyFrameAndPose
Map from keyframe to Sim3 pose.
Definition Optimizer.h:38
std::pair< Set< KeyFrame * >, int > ConsistentGroup
A group of keyframes with a consistency count.
Definition Optimizer.h:36
Container that stores unique elements in no particular order, and which allow for fast retrieval or i...
Definition Set.h:59
Used to lock a particular variable for writing.
Definition RWLock.h:272
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...
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
STL namespace.
Similarity transformation in 3D (rotation + translation + scale).
Definition sim3.h:13