SerializationUtils.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #ifndef SERIALIZATION_UTILS_H
  19. #define SERIALIZATION_UTILS_H
  20. #include <boost/serialization/serialization.hpp>
  21. #include <boost/serialization/vector.hpp>
  22. #include <sophus/se3.hpp>
  23. #include <Eigen/Core>
  24. #include <opencv2/core/core.hpp>
  25. #include <opencv2/features2d/features2d.hpp>
  26. #include <vector>
  27. namespace ORB_SLAM3
  28. {
  29. template <class Archive>
  30. void serializeSophusSE3(Archive &ar, Sophus::SE3f &T, const unsigned int version)
  31. {
  32. Eigen::Vector4f quat;
  33. Eigen::Vector3f transl;
  34. if (Archive::is_saving::value)
  35. {
  36. Eigen::Quaternionf q = T.unit_quaternion();
  37. quat << q.w(), q.x(), q.y(), q.z();
  38. transl = T.translation();
  39. }
  40. ar & boost::serialization::make_array(quat.data(), quat.size());
  41. ar & boost::serialization::make_array(transl.data(), transl.size());
  42. if (Archive::is_loading::value)
  43. {
  44. Eigen::Quaternionf q(quat[0], quat[1], quat[2], quat[3]);
  45. T = Sophus::SE3f(q, transl);
  46. }
  47. }
  48. /*template <class Archive, size_t dim>
  49. void serializeDiagonalMatrix(Archive &ar, Eigen::DiagonalMatrix<float, dim> &D, const unsigned int version)
  50. {
  51. Eigen::Matrix<float,dim,dim> dense;
  52. if(Archive::is_saving::value)
  53. {
  54. dense = D.toDenseMatrix();
  55. }
  56. ar & boost::serialization::make_array(dense.data(), dense.size());
  57. if (Archive::is_loading::value)
  58. {
  59. D = dense.diagonal().asDiagonal();
  60. }
  61. }*/
  62. template<class Archive>
  63. void serializeMatrix(Archive& ar, cv::Mat& mat, const unsigned int version)
  64. {
  65. int cols, rows, type;
  66. bool continuous;
  67. if (Archive::is_saving::value) {
  68. cols = mat.cols; rows = mat.rows; type = mat.type();
  69. continuous = mat.isContinuous();
  70. }
  71. ar & cols & rows & type & continuous;
  72. if (Archive::is_loading::value)
  73. mat.create(rows, cols, type);
  74. if (continuous) {
  75. const unsigned int data_size = rows * cols * mat.elemSize();
  76. ar & boost::serialization::make_array(mat.ptr(), data_size);
  77. } else {
  78. const unsigned int row_size = cols*mat.elemSize();
  79. for (int i = 0; i < rows; i++) {
  80. ar & boost::serialization::make_array(mat.ptr(i), row_size);
  81. }
  82. }
  83. }
  84. template<class Archive>
  85. void serializeMatrix(Archive& ar, const cv::Mat& mat, const unsigned int version)
  86. {
  87. cv::Mat matAux = mat;
  88. serializeMatrix(ar, matAux,version);
  89. if (Archive::is_loading::value)
  90. {
  91. cv::Mat* ptr;
  92. ptr = (cv::Mat*)( &mat );
  93. *ptr = matAux;
  94. }
  95. }
  96. template<class Archive>
  97. void serializeVectorKeyPoints(Archive& ar, const std::vector<cv::KeyPoint>& vKP, const unsigned int version)
  98. {
  99. int NumEl;
  100. if (Archive::is_saving::value) {
  101. NumEl = vKP.size();
  102. }
  103. ar & NumEl;
  104. std::vector<cv::KeyPoint> vKPaux = vKP;
  105. if (Archive::is_loading::value)
  106. vKPaux.reserve(NumEl);
  107. for(int i=0; i < NumEl; ++i)
  108. {
  109. cv::KeyPoint KPi;
  110. if (Archive::is_loading::value)
  111. KPi = cv::KeyPoint();
  112. if (Archive::is_saving::value)
  113. KPi = vKPaux[i];
  114. ar & KPi.angle;
  115. ar & KPi.response;
  116. ar & KPi.size;
  117. ar & KPi.pt.x;
  118. ar & KPi.pt.y;
  119. ar & KPi.class_id;
  120. ar & KPi.octave;
  121. if (Archive::is_loading::value)
  122. vKPaux.push_back(KPi);
  123. }
  124. if (Archive::is_loading::value)
  125. {
  126. std::vector<cv::KeyPoint> *ptr;
  127. ptr = (std::vector<cv::KeyPoint>*)( &vKP );
  128. *ptr = vKPaux;
  129. }
  130. }
  131. } // namespace ORB_SLAM3
  132. #endif // SERIALIZATION_UTILS_H