GSLAM  3.0.0
Optimizer.h
1 // GSLAM - A general SLAM framework and benchmark
2 // Copyright 2018 PILAB Inc. All rights reserved.
3 // https://github.com/zdzhaoyong/GSLAM
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors may be
14 // used to endorse or promote products derived from this software without
15 // specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Author: zd5945@126.com (Yong Zhao)
30 //
31 // Optimizer of GSLAM aims to provide an unified interface for most of nonlinear SLAM problems such as PnP
32 // solver, bundle adjustment, pose graph optimization.
33 
34 #ifndef GSLAM_CORE_OPTIMIZER_H_
35 #define GSLAM_CORE_OPTIMIZER_H_
36 
37 #include <string>
38 #include <utility>
39 #include <vector>
40 #include "GSLAM.h"
41 
42 #define GSLAM_REGISTER_OPTIMIZER(OPT_CLASS) \
43  extern "C" std::shared_ptr<GSLAM::Optimizer> createOptimizerInstance() { \
44  return std::shared_ptr<GSLAM::Optimizer>(new OPT_CLASS()); \
45  } \
46  class OPT_CLASS##_Register { \
47  public: \
48  OPT_CLASS##_Register() { \
49  GSLAM::Optimizer::buildin()=createOptimizerInstance; \
50  } \
51  } OPT_CLASS##_Register_instance;
52 
53 namespace GSLAM {
54 
55 class Optimizer;
56 typedef std::shared_ptr<Optimizer> (*funcCreateOptimizerInstance)();
57 
58 enum CameraProjectionType {
59  PROJECTION_PINHOLE, // z = 1
60  PROJECTION_SPHERE // ||x,y,z||=1
61 };
62 
63 enum InvDepthEstimationDOF {
64  UPDATE_ID_NONE = 0,
65  UPDATE_ID_IDEPTH = 1,
66  UPDATE_ID_SIGMA = 2,
67  UPDATE_ID_IDEPTHSIGMA = UPDATE_ID_IDEPTH | UPDATE_ID_SIGMA
68 };
69 
70 enum KeyFrameEstimzationDOF {
71  UPDATE_KF_NONE = 0,
72  UPDATE_KF_X = 1,
73  UPDATE_KF_Y = 2,
74  UPDATE_KF_Z = 4,
75  UPDATE_KF_RX = 8,
76  UPDATE_KF_RY = 16,
77  UPDATE_KF_RZ = 32,
78  UPDATE_KF_SCALE = 64,
79  UPDATE_KF_TRANSLATION = UPDATE_KF_X | UPDATE_KF_Y | UPDATE_KF_Z,
80  UPDATE_KF_XYZ = UPDATE_KF_TRANSLATION,
81  UPDATE_KF_ROTATION = UPDATE_KF_RX | UPDATE_KF_RY | UPDATE_KF_RZ,
82  UPDATE_KF_SE3 = UPDATE_KF_TRANSLATION | UPDATE_KF_ROTATION,
83  UPDATE_KF_SIM3 = UPDATE_KF_SE3 | UPDATE_KF_SCALE
84 };
85 
86 enum CameraEstimationDOF {
87  UPDATE_CAMERA_NONE = 0,
88  UPDATE_CAMERA_FOCAL = 1,
89  UPDATE_CAMERA_CENTER = 2,
90  UPDATE_CAMERA_K1 = 4,
91  UPDATE_CAMERA_K2 = 8,
92  UPDATE_CAMERA_P1 = 16,
93  UPDATE_CAMERA_P2 = 32,
94  UPDATE_CAMERA_K3 = 64,
95  UPDATE_CAMERA_K = UPDATE_CAMERA_K1 | UPDATE_CAMERA_K2 | UPDATE_CAMERA_K3,
96  UPDATE_CAMERA_P = UPDATE_CAMERA_P1 | UPDATE_CAMERA_P2,
97  UPDATE_CAMERA_KP = UPDATE_CAMERA_K | UPDATE_CAMERA_P,
98  UPDATE_CAMERA_PINHOLE = UPDATE_CAMERA_FOCAL | UPDATE_CAMERA_CENTER,
99  UPDATE_CAMERA_FISHEYE = UPDATE_CAMERA_PINHOLE | UPDATE_CAMERA_KP
100 };
101 
102 typedef GSLAM::Point3d
103  CameraAnchor; // for both Pinhole projection and Sphere projection
104 typedef GSLAM::Point2d IdepthEstimation; // [idepth,sigma]^T
105 
107  GSLAM::PointID frameId;
108  CameraAnchor anchor;
109  IdepthEstimation estimation; // idepth,sigma
110  InvDepthEstimationDOF dof;
111 };
112 
113 typedef std::pair<GSLAM::Point3d, bool>
114  MapPointEstimation; // false:FIX true:NOT FIXED
115 
117  GSLAM::SIM3 estimation; // T_{wc} : the transform from camera to world
118  KeyFrameEstimzationDOF dof;
119 };
120 
121 struct BundleEdge {
122  GSLAM::FrameID pointId, frameId;
123  CameraAnchor measurement;
124  double* information; // 2*2
125 };
126 
127 // Consider the first frame as origin, transform of the second frame should be
128 // SE3_{12}
129 struct SE3Edge {
130  GSLAM::FrameID firstId, secondId;
131  GSLAM::SE3 measurement; // SE3_{12}:=SE3_1^{-1}*SE3_2 =>
132  // P_{world}=SE3_2*P_{C2}=SE3_1*SE3_{12}*P_{C2}=SE3_1*P_{C1}
133  double* information; // 6*6
134 };
135 
136 // Consider the first frame as origin, transform of the second frame should be
137 // SIM3_{12}
138 struct SIM3Edge {
139  GSLAM::FrameID firstId, secondId;
140  GSLAM::SIM3 measurement; // SIM3_{12}:=SIM3_1^{-1}*SIM3_2
141  double* information; // 7*7
142 };
143 
144 struct GPSEdge {
145  GSLAM::FrameID frameId;
146  GSLAM::SE3 measurement; // SE3_{gps}:=SE3_{frameId}
147  double* information; // 6*6
148 };
149 
150 struct BundleGraph {
151  // VERTICALS : Estimations
152  std::vector<InvDepthEstimation>
153  invDepths; // mappoint representation with inverse depth
154  std::vector<MapPointEstimation>
155  mappoints; // mappoint representation with [x,y,z]
156  std::vector<KeyFrameEstimzation>
157  keyframes; // keyframe representation with SIM3:[rx,ry,rz,w,x,y,z,scale]
158 
159  // EDGES : Residual blocks from measurements
160  std::vector<BundleEdge> invDepthObserves,
161  mappointObserves; // [pt_id,kf_id],[x,y,z]
162  std::vector<SE3Edge>
163  se3Graph; // [firstId,secondId,SE3_{12}] SE3_{12}:=SE3_1^{-1}*SE3_2
164  std::vector<SIM3Edge>
165  sim3Graph; // [firstId,secondId,SIM3_{12}] SIM3_{12}:=SIM3_1^{-1}*SIM3_2
166  std::vector<GPSEdge>
167  gpsGraph; // [frameId,SE3_{gps}] SE3_{gps}:=SE3_{frameId}
168 
169  // CAMERA : Invalid camera indicates idea camera
170  GSLAM::Camera camera;
171  CameraEstimationDOF cameraDOF;
172 };
173 
175  CameraProjectionType cameraProjectionType = PROJECTION_PINHOLE;
176 
177  double projectErrorHuberThreshold = 0.01;
178 
179  int maxIterations = 500;
180 
181  bool verbose = false;
182 };
183 
184 class Optimizer {
185  public:
186  explicit Optimizer(OptimzeConfig config = OptimzeConfig())
187  : _config(config) {}
188 
189  virtual ~Optimizer() {}
190 
191  // TRACKING: Update relative pose agaist the first frame with known or unknown
192  // depth
193  virtual bool optimizePose(
194  std::vector<std::pair<CameraAnchor, CameraAnchor> >& matches, // NOLINT
195  std::vector<IdepthEstimation>& firstIDepth, // NOLINT
196  GSLAM::SE3& relativePose, // T_{12} // NOLINT
197  KeyFrameEstimzationDOF dof = UPDATE_KF_SE3, double* information = NULL) {
198  return false;
199  }
200 
201  // Update pose with 3D-2D corrospondences
202  virtual bool optimizePnP(
203  const std::vector<std::pair<GSLAM::Point3d, CameraAnchor> >& matches,
204  GSLAM::SE3& pose, KeyFrameEstimzationDOF dof = UPDATE_KF_SE3, // NOLINT
205  double* information = NULL) {
206  return false;
207  }
208 
209  // Update pose with 3D-3D corrospondences
210  virtual bool optimizeICP(
211  const std::vector<std::pair<GSLAM::Point3d, GSLAM::Point3d> >&
212  matches, // T_{12}
213  GSLAM::SIM3& pose, // NOLINT
214  KeyFrameEstimzationDOF dof = UPDATE_KF_SE3,
215  double* information = NULL) {
216  return false;
217  }
218 
219  // Fit the sim3 transform between 2 synchronized trajectory
220  virtual bool fitSim3(
221  const std::vector<std::pair<GSLAM::SE3, GSLAM::SE3> >& matches, // T_{12}
222  GSLAM::SIM3& sim3, KeyFrameEstimzationDOF dof = UPDATE_KF_SIM3, // NOLINT
223  double* information = NULL) {
224  return false;
225  }
226 
227  // MAPPING: Do bundle adjust with auto calibration or not: BUNDLEADJUST,
228  // INVDEPTH_BUNDLE, POSEGRAPH
229  virtual bool optimize(BundleGraph& graph) { return false; } // NOLINT
230  virtual bool magin(BundleGraph& graph) { // NOLINT
231  return false;
232  } // Convert bundle graph to pose graph
233 
234  static std::shared_ptr<Optimizer> create(std::string pluginName = "") {
235  if (pluginName.empty()) {
236  if (buildin()) return buildin()();
237  pluginName = svar.GetString("OptimizerPlugin", "libgslam_optimizer");
238  }
239  std::shared_ptr<SharedLibrary> plugin = Registry::get(pluginName);
240  if (!plugin) return std::shared_ptr<Optimizer>();
241  funcCreateOptimizerInstance createFunc =
242  (funcCreateOptimizerInstance)plugin->getSymbol(
243  "createOptimizerInstance");
244  if (!createFunc)
245  return std::shared_ptr<Optimizer>();
246  else
247  return createFunc();
248  }
249 
250  static funcCreateOptimizerInstance& buildin(){static funcCreateOptimizerInstance v=nullptr;return v;}
251 
252  OptimzeConfig _config;
253 };
254 
255 typedef std::shared_ptr<Optimizer> OptimizerPtr;
256 
257 } // namespace GSLAM
258 #endif // GSLAM_CORE_OPTIMIZER_H_
Definition: Optimizer.h:116
Definition: Point.h:48
Definition: Point.h:162
Definition: Optimizer.h:150
Definition: Camera.h:175
Definition: Optimizer.h:121
Definition: Optimizer.h:174
Definition: Optimizer.h:144
Definition: Camera.h:45
Definition: Optimizer.h:129
Definition: Optimizer.h:138
Represent a three-dimensional similarity transformation,7 degrees of freedom a rotation: R (Here is r...
Definition: SIM3.h:57
Represent a three-dimensional Euclidean transformation (a rotation and a translation).
Definition: SE3.h:69
Definition: Optimizer.h:106
Definition: Optimizer.h:184