GSLAM  3.0.0
Estimator.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 // Estimator of GSLAM aims to provide a collection of close-form solvers covering all interesting cases with robust sample consensus (RANSAC) methods.
32 
33 #ifndef GSLAM_CORE_ESTIMATOR_H_
34 #define GSLAM_CORE_ESTIMATOR_H_
35 
36 #include <string>
37 #include <vector>
38 #include "GSLAM/core/SIM3.h"
39 #include "GSLAM/core/Svar.h"
40 #include "GSLAM/core/Registry.h"
41 
42 #define USE_ESTIMATOR_PLUGIN(EST_CLASS) \
43  extern "C" GSLAM::Estimator* createEstimatorInstance() { \
44  return new EST_CLASS(); \
45  } \
46  class EST_CLASS##_Register { \
47  public: \
48  EST_CLASS##_Register() { \
49  GSLAM::Estimator::buildinEstimators()\
50 .Set<funcCreateEstimatorInstance>\
51 ("Default",createEstimatorInstance); \
52  } \
53  } EST_CLASS##_Register_instance;
54 
55 namespace GSLAM {
56 
57 class Estimator;
58 typedef std::shared_ptr<Estimator> EstimatorPtr;
59 typedef Estimator* (*funcCreateEstimatorInstance)();
60 typedef Matrix3d Homography2D;
61 typedef Matrix<double,2,3> Affine2D;
62 typedef Matrix3d Fundamental;
63 typedef Fundamental Essential;
64 typedef Matrix<double,3,4> Affine3D;
65 typedef unsigned char uchar;
66 
67 enum EstimatorMethod {
68  MODEL_METHOD=0xFF,
69  F8_Point,
70  F7_Point,
71  E5_Stewenius,
72  E5_Nister,
73  E5_Kneip,
74  H4_Point,
75  A3_Point,
76  P4_EPnP,
77  P3_Gao,
78  P3_Kneip,
79  P3_GPnP,
80  P3_ITERATIVE,
81  P2_Kneip,
82  T2_Triangulate,
83  A4_Point,
84  S3_Horn,
85  P3_Plane,
86  SAMPLE_METHOD=0xF00,
87  RANSAC = 0<<8, //!< RANSAC algorithm
88  LMEDS = 1<<8, //!< least-median algorithm
89  NOSAMPLE= 2<<8
90 };
91 
92 class Estimator {
93  public:
94  Estimator() {}
95 
96  virtual ~Estimator() {}
97 
98  virtual std::string type() const { return "Estimator"; }
99 
100  // 2D corrospondences
101  virtual bool findHomography(Homography2D* H, // 3x3 dof=8
102  const std::vector<Point2d>& srcPoints,
103  const std::vector<Point2d>& dstPoints,
104  int method = H4_Point&RANSAC,
105  double threshold = 3,
106  double confidence = 0.99,
107  std::vector<uchar>* mask = NULL) const = 0;
108 
109  virtual bool findAffine2D(Affine2D* A, // 2X3
110  const std::vector<Point2d>& srcPoints,
111  const std::vector<Point2d>& dstPoints,
112  int method = A3_Point&RANSAC,
113  double threshold = 3,
114  double confidence = 0.99,
115  std::vector<uchar>* mask = NULL) const =0;
116 
117  virtual bool findFundamental(Fundamental* F, // 3x3
118  const std::vector<Point2d>& points1,
119  const std::vector<Point2d>& points2,
120  int method = F8_Point&RANSAC,
121  double threshold = 3.,
122  double confidence = 0.99,
123  std::vector<uchar>* mask = NULL) const = 0;
124 
125  virtual bool findEssentialMatrix(Essential* E, // 3x3 dof=5
126  const std::vector<Point2d>& points1,
127  const std::vector<Point2d>& points2,
128  int method = E5_Nister&RANSAC,
129  double threshold = 0.01,
130  double confidence = 0.99,
131  std::vector<uchar>* mask = NULL) const = 0;
132 
133  // 3D corrospondences
134  virtual bool findSIM3(SIM3* S,
135  const std::vector<Point3d>& from,
136  const std::vector<Point3d>& to,
137  int method = S3_Horn&RANSAC,
138  double threshold = 0.01,
139  double confidence = 0.99,
140  std::vector<uchar>* mask = NULL) const = 0;
141 
142  virtual bool findAffine3D(Affine3D* A,
143  const std::vector<Point3d>& src,
144  const std::vector<Point3d>& dst,
145  int method = A4_Point&RANSAC,
146  double threshold = 0.01,
147  double confidence = 0.99,
148  std::vector<uchar>* mask = NULL) const = 0;
149 
150  virtual bool findPlane(SE3* plane,
151  const std::vector<Point3d>& points, // NOLINT
152  int method = P3_Plane&RANSAC,
153  double threshold = 0.01,
154  double confidence = 0.99,
155  std::vector<uchar>* mask = NULL) const = 0;
156 
157  // 2D&3D corrospondences
158  virtual bool findPnP( SE3* world2camera,
159  const std::vector<Point3d>& objectPoints,
160  const std::vector<Point2d>& imagePoints,
161  int method = P3_ITERATIVE&RANSAC,
162  double threshold = 0.01,
163  double confidence = 0.99,
164  std::vector<uchar>* mask = NULL) const = 0;
165 
166  virtual bool trianglate(Point3d* refPt,
167  const SE3& ref2cur,
168  const Point3d& refDirection, // camera.UnProject(ref2d)
169  const Point3d& curDirection) const = 0; // camera.UnProject(cur2d)
170 
171  static Svar& buildinEstimators(){
172  static Svar var=Svar::object();return var;
173  }
174 
175  static EstimatorPtr create(std::string pluginName = "") {
176  funcCreateEstimatorInstance createFunc =buildinEstimators()
177  .Get<funcCreateEstimatorInstance>("Default",NULL);
178  if (createFunc) return EstimatorPtr(createFunc());
179 
180  if (pluginName.empty()) {
181  pluginName = svar.GetString("EstimatorPlugin", "libgslam_estimator");
182  }
183  SharedLibraryPtr plugin = Registry::get(pluginName);
184  if (!plugin) return EstimatorPtr();
185  createFunc = (funcCreateEstimatorInstance)plugin->getSymbol(
186  "createEstimatorInstance");
187  if (!createFunc)
188  return EstimatorPtr();
189  else
190  return EstimatorPtr(createFunc());
191  }
192 };
193 } // namespace GSLAM
194 #endif // GSLAM_CORE_ESTIMATOR_H_
static Svar object(const std::map< std::string, Svar > &m={})
Create an Object instance.
Definition: Svar.h:632
Definition: Point.h:162
Definition: Estimator.h:92
Definition: Camera.h:45
The Svar class, A Tiny Modern C++ Header Brings Unified Interface for Different Languages.
Definition: Svar.h:561
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