MLPnPsolver.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. * This file is part of ORB-SLAM3
  3. *
  4. * Copyright (C) 2017-2021 Carlos Campos, Richard Elvira, Juan J. Gómez Rodríguez, José M.M. Montiel and Juan D. Tardós, University of Zaragoza.
  5. * Copyright (C) 2014-2016 Raúl Mur-Artal, José M.M. Montiel and Juan D. Tardós, University of Zaragoza.
  6. *
  7. * ORB-SLAM3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * ORB-SLAM3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  12. * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with ORB-SLAM3.
  16. * If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /******************************************************************************
  19. * Author: Steffen Urban *
  20. * Contact: urbste@gmail.com *
  21. * License: Copyright (c) 2016 Steffen Urban, ANU. All rights reserved. *
  22. * *
  23. * Redistribution and use in source and binary forms, with or without *
  24. * modification, are permitted provided that the following conditions *
  25. * are met: *
  26. * * Redistributions of source code must retain the above copyright *
  27. * notice, this list of conditions and the following disclaimer. *
  28. * * Redistributions in binary form must reproduce the above copyright *
  29. * notice, this list of conditions and the following disclaimer in the *
  30. * documentation and/or other materials provided with the distribution. *
  31. * * Neither the name of ANU nor the names of its contributors may be *
  32. * used to endorse or promote products derived from this software without *
  33. * specific prior written permission. *
  34. * *
  35. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"*
  36. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE *
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
  38. * ARE DISCLAIMED. IN NO EVENT SHALL ANU OR THE CONTRIBUTORS BE LIABLE *
  39. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL *
  40. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR *
  41. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER *
  42. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
  43. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY *
  44. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF *
  45. * SUCH DAMAGE. *
  46. ******************************************************************************/
  47. #ifndef ORB_SLAM3_MLPNPSOLVER_H
  48. #define ORB_SLAM3_MLPNPSOLVER_H
  49. #include "MapPoint.h"
  50. #include "Frame.h"
  51. #include<Eigen/Dense>
  52. #include<Eigen/Sparse>
  53. namespace ORB_SLAM3{
  54. class MLPnPsolver {
  55. public:
  56. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  57. MLPnPsolver(const Frame &F, const vector<MapPoint*> &vpMapPointMatches);
  58. ~MLPnPsolver();
  59. void SetRansacParameters(double probability = 0.99, int minInliers = 8, int maxIterations = 300, int minSet = 6, float epsilon = 0.4,
  60. float th2 = 5.991);
  61. //Find metod is necessary?
  62. bool iterate(int nIterations, bool &bNoMore, vector<bool> &vbInliers, int &nInliers, Eigen::Matrix4f &Tout);
  63. //Type definitions needed by the original code
  64. /** A 3-vector of unit length used to describe landmark observations/bearings
  65. * in camera frames (always expressed in camera frames)
  66. */
  67. typedef Eigen::Vector3d bearingVector_t;
  68. /** An array of bearing-vectors */
  69. typedef std::vector<bearingVector_t, Eigen::aligned_allocator<bearingVector_t> >
  70. bearingVectors_t;
  71. /** A 2-matrix containing the 2D covariance information of a bearing vector
  72. */
  73. typedef Eigen::Matrix2d cov2_mat_t;
  74. /** A 3-matrix containing the 3D covariance information of a bearing vector */
  75. typedef Eigen::Matrix3d cov3_mat_t;
  76. /** An array of 3D covariance matrices */
  77. typedef std::vector<cov3_mat_t, Eigen::aligned_allocator<cov3_mat_t> >
  78. cov3_mats_t;
  79. /** A 3-vector describing a point in 3D-space */
  80. typedef Eigen::Vector3d point_t;
  81. /** An array of 3D-points */
  82. typedef std::vector<point_t, Eigen::aligned_allocator<point_t> >
  83. points_t;
  84. /** A homogeneous 3-vector describing a point in 3D-space */
  85. typedef Eigen::Vector4d point4_t;
  86. /** An array of homogeneous 3D-points */
  87. typedef std::vector<point4_t, Eigen::aligned_allocator<point4_t> >
  88. points4_t;
  89. /** A 3-vector containing the rodrigues parameters of a rotation matrix */
  90. typedef Eigen::Vector3d rodrigues_t;
  91. /** A rotation matrix */
  92. typedef Eigen::Matrix3d rotation_t;
  93. /** A 3x4 transformation matrix containing rotation \f$ \mathbf{R} \f$ and
  94. * translation \f$ \mathbf{t} \f$ as follows:
  95. * \f$ \left( \begin{array}{cc} \mathbf{R} & \mathbf{t} \end{array} \right) \f$
  96. */
  97. typedef Eigen::Matrix<double,3,4> transformation_t;
  98. /** A 3-vector describing a translation/camera position */
  99. typedef Eigen::Vector3d translation_t;
  100. private:
  101. void CheckInliers();
  102. bool Refine();
  103. //Functions from de original MLPnP code
  104. /*
  105. * Computes the camera pose given 3D points coordinates (in the camera reference
  106. * system), the camera rays and (optionally) the covariance matrix of those camera rays.
  107. * Result is stored in solution
  108. */
  109. void computePose(
  110. const bearingVectors_t & f,
  111. const points_t & p,
  112. const cov3_mats_t & covMats,
  113. const std::vector<int>& indices,
  114. transformation_t & result);
  115. void mlpnp_gn(Eigen::VectorXd& x,
  116. const points_t& pts,
  117. const std::vector<Eigen::MatrixXd>& nullspaces,
  118. const Eigen::SparseMatrix<double> Kll,
  119. bool use_cov);
  120. void mlpnp_residuals_and_jacs(
  121. const Eigen::VectorXd& x,
  122. const points_t& pts,
  123. const std::vector<Eigen::MatrixXd>& nullspaces,
  124. Eigen::VectorXd& r,
  125. Eigen::MatrixXd& fjac,
  126. bool getJacs);
  127. void mlpnpJacs(
  128. const point_t& pt,
  129. const Eigen::Vector3d& nullspace_r,
  130. const Eigen::Vector3d& nullspace_s,
  131. const rodrigues_t& w,
  132. const translation_t& t,
  133. Eigen::MatrixXd& jacs);
  134. //Auxiliar methods
  135. /**
  136. * \brief Compute a rotation matrix from Rodrigues axis angle.
  137. *
  138. * \param[in] omega The Rodrigues-parameters of a rotation.
  139. * \return The 3x3 rotation matrix.
  140. */
  141. Eigen::Matrix3d rodrigues2rot(const Eigen::Vector3d & omega);
  142. /**
  143. * \brief Compute the Rodrigues-parameters of a rotation matrix.
  144. *
  145. * \param[in] R The 3x3 rotation matrix.
  146. * \return The Rodrigues-parameters.
  147. */
  148. Eigen::Vector3d rot2rodrigues(const Eigen::Matrix3d & R);
  149. //----------------------------------------------------
  150. //Fields of the solver
  151. //----------------------------------------------------
  152. vector<MapPoint*> mvpMapPointMatches;
  153. // 2D Points
  154. vector<cv::Point2f> mvP2D;
  155. //Substitued by bearing vectors
  156. bearingVectors_t mvBearingVecs;
  157. vector<float> mvSigma2;
  158. // 3D Points
  159. //vector<cv::Point3f> mvP3Dw;
  160. points_t mvP3Dw;
  161. // Index in Frame
  162. vector<size_t> mvKeyPointIndices;
  163. // Current Estimation
  164. double mRi[3][3];
  165. double mti[3];
  166. Eigen::Matrix4f mTcwi;
  167. vector<bool> mvbInliersi;
  168. int mnInliersi;
  169. // Current Ransac State
  170. int mnIterations;
  171. vector<bool> mvbBestInliers;
  172. int mnBestInliers;
  173. Eigen::Matrix4f mBestTcw;
  174. // Refined
  175. Eigen::Matrix4f mRefinedTcw;
  176. vector<bool> mvbRefinedInliers;
  177. int mnRefinedInliers;
  178. // Number of Correspondences
  179. int N;
  180. // Indices for random selection [0 .. N-1]
  181. vector<size_t> mvAllIndices;
  182. // RANSAC probability
  183. double mRansacProb;
  184. // RANSAC min inliers
  185. int mRansacMinInliers;
  186. // RANSAC max iterations
  187. int mRansacMaxIts;
  188. // RANSAC expected inliers/total ratio
  189. float mRansacEpsilon;
  190. // RANSAC Threshold inlier/outlier. Max error e = dist(P1,T_12*P2)^2
  191. float mRansacTh;
  192. // RANSAC Minimun Set used at each iteration
  193. int mRansacMinSet;
  194. // Max square error associated with scale level. Max error = th*th*sigma(level)*sigma(level)
  195. vector<float> mvMaxError;
  196. GeometricCamera* mpCamera;
  197. };
  198. }
  199. #endif //ORB_SLAM3_MLPNPSOLVER_H