serialization.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /**
  2. * @file serialization.cpp
  3. *
  4. * @date Jun 12, 2013
  5. * @author Alex Cunningham
  6. */
  7. #include <gtsam/base/deprecated/LieMatrix_Deprecated.h>
  8. #include <gtsam/base/deprecated/LieVector_Deprecated.h>
  9. #include <gtsam/slam/serialization.h>
  10. #include <gtsam/base/serialization.h>
  11. //#include <gtsam/slam/AntiFactor.h>
  12. #include <gtsam/sam/BearingRangeFactor.h>
  13. #include <gtsam/slam/BetweenFactor.h>
  14. //#include <gtsam/slam/BoundingConstraint.h>
  15. #include <gtsam/slam/GeneralSFMFactor.h>
  16. #include <gtsam/nonlinear/PriorFactor.h>
  17. #include <gtsam/slam/ProjectionFactor.h>
  18. #include <gtsam/sam/RangeFactor.h>
  19. #include <gtsam/slam/StereoFactor.h>
  20. #include <gtsam/nonlinear/NonlinearEquality.h>
  21. #include <gtsam/inference/Symbol.h>
  22. #include <gtsam/linear/GaussianISAM.h>
  23. #include <gtsam/linear/GaussianMultifrontalSolver.h>
  24. #include <gtsam/geometry/Pose2.h>
  25. #include <gtsam/geometry/Pose3.h>
  26. #include <gtsam/geometry/Cal3DS2.h>
  27. //#include <gtsam/geometry/Cal3_S2Stereo.h>
  28. using namespace gtsam;
  29. // Creating as many permutations of factors as possible
  30. typedef PriorFactor<LieVector> PriorFactorLieVector;
  31. typedef PriorFactor<LieMatrix> PriorFactorLieMatrix;
  32. typedef PriorFactor<Point2> PriorFactorPoint2;
  33. typedef PriorFactor<StereoPoint2> PriorFactorStereoPoint2;
  34. typedef PriorFactor<Point3> PriorFactorPoint3;
  35. typedef PriorFactor<Rot2> PriorFactorRot2;
  36. typedef PriorFactor<Rot3> PriorFactorRot3;
  37. typedef PriorFactor<Pose2> PriorFactorPose2;
  38. typedef PriorFactor<Pose3> PriorFactorPose3;
  39. typedef PriorFactor<Cal3_S2> PriorFactorCal3_S2;
  40. typedef PriorFactor<Cal3DS2> PriorFactorCal3DS2;
  41. typedef PriorFactor<CalibratedCamera> PriorFactorCalibratedCamera;
  42. typedef PriorFactor<PinholeCameraCal3_S2> PriorFactorPinholeCameraCal3_S2;
  43. typedef PriorFactor<StereoCamera> PriorFactorStereoCamera;
  44. typedef BetweenFactor<LieVector> BetweenFactorLieVector;
  45. typedef BetweenFactor<LieMatrix> BetweenFactorLieMatrix;
  46. typedef BetweenFactor<Point2> BetweenFactorPoint2;
  47. typedef BetweenFactor<Point3> BetweenFactorPoint3;
  48. typedef BetweenFactor<Rot2> BetweenFactorRot2;
  49. typedef BetweenFactor<Rot3> BetweenFactorRot3;
  50. typedef BetweenFactor<Pose2> BetweenFactorPose2;
  51. typedef BetweenFactor<Pose3> BetweenFactorPose3;
  52. typedef NonlinearEquality<LieVector> NonlinearEqualityLieVector;
  53. typedef NonlinearEquality<LieMatrix> NonlinearEqualityLieMatrix;
  54. typedef NonlinearEquality<Point2> NonlinearEqualityPoint2;
  55. typedef NonlinearEquality<StereoPoint2> NonlinearEqualityStereoPoint2;
  56. typedef NonlinearEquality<Point3> NonlinearEqualityPoint3;
  57. typedef NonlinearEquality<Rot2> NonlinearEqualityRot2;
  58. typedef NonlinearEquality<Rot3> NonlinearEqualityRot3;
  59. typedef NonlinearEquality<Pose2> NonlinearEqualityPose2;
  60. typedef NonlinearEquality<Pose3> NonlinearEqualityPose3;
  61. typedef NonlinearEquality<Cal3_S2> NonlinearEqualityCal3_S2;
  62. typedef NonlinearEquality<Cal3DS2> NonlinearEqualityCal3DS2;
  63. typedef NonlinearEquality<CalibratedCamera> NonlinearEqualityCalibratedCamera;
  64. typedef NonlinearEquality<PinholeCameraCal3_S2> NonlinearEqualityPinholeCameraCal3_S2;
  65. typedef NonlinearEquality<StereoCamera> NonlinearEqualityStereoCamera;
  66. typedef RangeFactor<Pose2, Point2> RangeFactor2D;
  67. typedef RangeFactor<Pose3, Point3> RangeFactor3D;
  68. typedef RangeFactor<Pose2, Pose2> RangeFactorPose2;
  69. typedef RangeFactor<Pose3, Pose3> RangeFactorPose3;
  70. typedef RangeFactor<CalibratedCamera, Point3> RangeFactorCalibratedCameraPoint;
  71. typedef RangeFactor<PinholeCameraCal3_S2, Point3> RangeFactorPinholeCameraCal3_S2Point;
  72. typedef RangeFactor<CalibratedCamera, CalibratedCamera> RangeFactorCalibratedCamera;
  73. typedef RangeFactor<PinholeCameraCal3_S2, PinholeCameraCal3_S2> RangeFactorPinholeCameraCal3_S2;
  74. typedef BearingRangeFactor<Pose2, Point2> BearingRangeFactor2D;
  75. typedef BearingRangeFactor<Pose3, Point3> BearingRangeFactor3D;
  76. typedef GenericProjectionFactor<Pose3, Point3, Cal3_S2> GenericProjectionFactorCal3_S2;
  77. typedef GenericProjectionFactor<Pose3, Point3, Cal3DS2> GenericProjectionFactorCal3DS2;
  78. typedef gtsam::GeneralSFMFactor<gtsam::PinholeCameraCal3_S2, gtsam::Point3> GeneralSFMFactorCal3_S2;
  79. typedef gtsam::GeneralSFMFactor<gtsam::PinholeCameraCal3DS2, gtsam::Point3> GeneralSFMFactorCal3DS2;
  80. typedef gtsam::GeneralSFMFactor2<gtsam::Cal3_S2> GeneralSFMFactor2Cal3_S2;
  81. typedef gtsam::GenericStereoFactor<gtsam::Pose3, gtsam::Point3> GenericStereoFactor3D;
  82. /* Create GUIDs for Noisemodels */
  83. /* ************************************************************************* */
  84. BOOST_CLASS_EXPORT_GUID(gtsam::noiseModel::Constrained, "gtsam_noiseModel_Constrained");
  85. BOOST_CLASS_EXPORT_GUID(gtsam::noiseModel::Diagonal, "gtsam_noiseModel_Diagonal");
  86. BOOST_CLASS_EXPORT_GUID(gtsam::noiseModel::Gaussian, "gtsam_noiseModel_Gaussian");
  87. BOOST_CLASS_EXPORT_GUID(gtsam::noiseModel::Unit, "gtsam_noiseModel_Unit");
  88. BOOST_CLASS_EXPORT_GUID(gtsam::noiseModel::Isotropic, "gtsam_noiseModel_Isotropic");
  89. BOOST_CLASS_EXPORT_GUID(gtsam::noiseModel::mEstimator::Base , "gtsam_noiseModel_mEstimator_Base");
  90. BOOST_CLASS_EXPORT_GUID(gtsam::noiseModel::mEstimator::Null , "gtsam_noiseModel_mEstimator_Null");
  91. BOOST_CLASS_EXPORT_GUID(gtsam::noiseModel::mEstimator::Fair , "gtsam_noiseModel_mEstimator_Fair");
  92. BOOST_CLASS_EXPORT_GUID(gtsam::noiseModel::mEstimator::Huber, "gtsam_noiseModel_mEstimator_Huber");
  93. BOOST_CLASS_EXPORT_GUID(gtsam::noiseModel::mEstimator::Tukey, "gtsam_noiseModel_mEstimator_Tukey");
  94. BOOST_CLASS_EXPORT_GUID(gtsam::SharedNoiseModel, "gtsam_SharedNoiseModel");
  95. BOOST_CLASS_EXPORT_GUID(gtsam::SharedDiagonal, "gtsam_SharedDiagonal");
  96. /* Create GUIDs for geometry */
  97. /* ************************************************************************* */
  98. GTSAM_VALUE_EXPORT(gtsam::LieVector);
  99. GTSAM_VALUE_EXPORT(gtsam::LieMatrix);
  100. GTSAM_VALUE_EXPORT(gtsam::Point2);
  101. GTSAM_VALUE_EXPORT(gtsam::StereoPoint2);
  102. GTSAM_VALUE_EXPORT(gtsam::Point3);
  103. GTSAM_VALUE_EXPORT(gtsam::Rot2);
  104. GTSAM_VALUE_EXPORT(gtsam::Rot3);
  105. GTSAM_VALUE_EXPORT(gtsam::Pose2);
  106. GTSAM_VALUE_EXPORT(gtsam::Pose3);
  107. GTSAM_VALUE_EXPORT(gtsam::Cal3_S2);
  108. GTSAM_VALUE_EXPORT(gtsam::Cal3DS2);
  109. GTSAM_VALUE_EXPORT(gtsam::Cal3_S2Stereo);
  110. GTSAM_VALUE_EXPORT(gtsam::CalibratedCamera);
  111. GTSAM_VALUE_EXPORT(gtsam::PinholeCameraCal3_S2);
  112. GTSAM_VALUE_EXPORT(gtsam::StereoCamera);
  113. /* Create GUIDs for factors */
  114. /* ************************************************************************* */
  115. BOOST_CLASS_EXPORT_GUID(gtsam::JacobianFactor, "gtsam::JacobianFactor");
  116. BOOST_CLASS_EXPORT_GUID(gtsam::HessianFactor , "gtsam::HessianFactor");
  117. BOOST_CLASS_EXPORT_GUID(PriorFactorLieVector, "gtsam::PriorFactorLieVector");
  118. BOOST_CLASS_EXPORT_GUID(PriorFactorLieMatrix, "gtsam::PriorFactorLieMatrix");
  119. BOOST_CLASS_EXPORT_GUID(PriorFactorPoint2, "gtsam::PriorFactorPoint2");
  120. BOOST_CLASS_EXPORT_GUID(PriorFactorStereoPoint2, "gtsam::PriorFactorStereoPoint2");
  121. BOOST_CLASS_EXPORT_GUID(PriorFactorPoint3, "gtsam::PriorFactorPoint3");
  122. BOOST_CLASS_EXPORT_GUID(PriorFactorRot2, "gtsam::PriorFactorRot2");
  123. BOOST_CLASS_EXPORT_GUID(PriorFactorRot3, "gtsam::PriorFactorRot3");
  124. BOOST_CLASS_EXPORT_GUID(PriorFactorPose2, "gtsam::PriorFactorPose2");
  125. BOOST_CLASS_EXPORT_GUID(PriorFactorPose3, "gtsam::PriorFactorPose3");
  126. BOOST_CLASS_EXPORT_GUID(PriorFactorCal3_S2, "gtsam::PriorFactorCal3_S2");
  127. BOOST_CLASS_EXPORT_GUID(PriorFactorCal3DS2, "gtsam::PriorFactorCal3DS2");
  128. BOOST_CLASS_EXPORT_GUID(PriorFactorCalibratedCamera, "gtsam::PriorFactorCalibratedCamera");
  129. BOOST_CLASS_EXPORT_GUID(PriorFactorStereoCamera, "gtsam::PriorFactorStereoCamera");
  130. BOOST_CLASS_EXPORT_GUID(BetweenFactorLieVector, "gtsam::BetweenFactorLieVector");
  131. BOOST_CLASS_EXPORT_GUID(BetweenFactorLieMatrix, "gtsam::BetweenFactorLieMatrix");
  132. BOOST_CLASS_EXPORT_GUID(BetweenFactorPoint2, "gtsam::BetweenFactorPoint2");
  133. BOOST_CLASS_EXPORT_GUID(BetweenFactorPoint3, "gtsam::BetweenFactorPoint3");
  134. BOOST_CLASS_EXPORT_GUID(BetweenFactorRot2, "gtsam::BetweenFactorRot2");
  135. BOOST_CLASS_EXPORT_GUID(BetweenFactorRot3, "gtsam::BetweenFactorRot3");
  136. BOOST_CLASS_EXPORT_GUID(BetweenFactorPose2, "gtsam::BetweenFactorPose2");
  137. BOOST_CLASS_EXPORT_GUID(BetweenFactorPose3, "gtsam::BetweenFactorPose3");
  138. BOOST_CLASS_EXPORT_GUID(NonlinearEqualityLieVector, "gtsam::NonlinearEqualityLieVector");
  139. BOOST_CLASS_EXPORT_GUID(NonlinearEqualityLieMatrix, "gtsam::NonlinearEqualityLieMatrix");
  140. BOOST_CLASS_EXPORT_GUID(NonlinearEqualityPoint2, "gtsam::NonlinearEqualityPoint2");
  141. BOOST_CLASS_EXPORT_GUID(NonlinearEqualityStereoPoint2, "gtsam::NonlinearEqualityStereoPoint2");
  142. BOOST_CLASS_EXPORT_GUID(NonlinearEqualityPoint3, "gtsam::NonlinearEqualityPoint3");
  143. BOOST_CLASS_EXPORT_GUID(NonlinearEqualityRot2, "gtsam::NonlinearEqualityRot2");
  144. BOOST_CLASS_EXPORT_GUID(NonlinearEqualityRot3, "gtsam::NonlinearEqualityRot3");
  145. BOOST_CLASS_EXPORT_GUID(NonlinearEqualityPose2, "gtsam::NonlinearEqualityPose2");
  146. BOOST_CLASS_EXPORT_GUID(NonlinearEqualityPose3, "gtsam::NonlinearEqualityPose3");
  147. BOOST_CLASS_EXPORT_GUID(NonlinearEqualityCal3_S2, "gtsam::NonlinearEqualityCal3_S2");
  148. BOOST_CLASS_EXPORT_GUID(NonlinearEqualityCal3DS2, "gtsam::NonlinearEqualityCal3DS2");
  149. BOOST_CLASS_EXPORT_GUID(NonlinearEqualityCalibratedCamera, "gtsam::NonlinearEqualityCalibratedCamera");
  150. BOOST_CLASS_EXPORT_GUID(NonlinearEqualityStereoCamera, "gtsam::NonlinearEqualityStereoCamera");
  151. BOOST_CLASS_EXPORT_GUID(RangeFactor2D, "gtsam::RangeFactor2D");
  152. BOOST_CLASS_EXPORT_GUID(RangeFactor3D, "gtsam::RangeFactor3D");
  153. BOOST_CLASS_EXPORT_GUID(RangeFactorPose2, "gtsam::RangeFactorPose2");
  154. BOOST_CLASS_EXPORT_GUID(RangeFactorPose3, "gtsam::RangeFactorPose3");
  155. BOOST_CLASS_EXPORT_GUID(RangeFactorCalibratedCameraPoint, "gtsam::RangeFactorCalibratedCameraPoint");
  156. BOOST_CLASS_EXPORT_GUID(RangeFactorCalibratedCamera, "gtsam::RangeFactorCalibratedCamera");
  157. BOOST_CLASS_EXPORT_GUID(BearingRangeFactor2D, "gtsam::BearingRangeFactor2D");
  158. BOOST_CLASS_EXPORT_GUID(GenericProjectionFactorCal3_S2, "gtsam::GenericProjectionFactorCal3_S2");
  159. BOOST_CLASS_EXPORT_GUID(GenericProjectionFactorCal3DS2, "gtsam::GenericProjectionFactorCal3DS2");
  160. BOOST_CLASS_EXPORT_GUID(GeneralSFMFactorCal3_S2, "gtsam::GeneralSFMFactorCal3_S2");
  161. BOOST_CLASS_EXPORT_GUID(GeneralSFMFactorCal3DS2, "gtsam::GeneralSFMFactorCal3DS2");
  162. BOOST_CLASS_EXPORT_GUID(GeneralSFMFactor2Cal3_S2, "gtsam::GeneralSFMFactor2Cal3_S2");
  163. BOOST_CLASS_EXPORT_GUID(GenericStereoFactor3D, "gtsam::GenericStereoFactor3D");
  164. #ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V41
  165. typedef PriorFactor<SimpleCamera> PriorFactorSimpleCamera;
  166. typedef NonlinearEquality<SimpleCamera> NonlinearEqualitySimpleCamera;
  167. typedef RangeFactor<SimpleCamera, Point3> RangeFactorSimpleCameraPoint;
  168. typedef RangeFactor<SimpleCamera, SimpleCamera> RangeFactorSimpleCamera;
  169. GTSAM_VALUE_EXPORT(gtsam::SimpleCamera);
  170. BOOST_CLASS_EXPORT_GUID(PriorFactorSimpleCamera, "gtsam::PriorFactorSimpleCamera");
  171. BOOST_CLASS_EXPORT_GUID(NonlinearEqualitySimpleCamera, "gtsam::NonlinearEqualitySimpleCamera");
  172. BOOST_CLASS_EXPORT_GUID(RangeFactorSimpleCameraPoint, "gtsam::RangeFactorSimpleCameraPoint");
  173. BOOST_CLASS_EXPORT_GUID(RangeFactorSimpleCamera, "gtsam::RangeFactorSimpleCamera");
  174. #endif
  175. /* ************************************************************************* */
  176. // Actual implementations of functions
  177. /* ************************************************************************* */
  178. std::string gtsam::serializeGraph(const NonlinearFactorGraph& graph) {
  179. return serialize<NonlinearFactorGraph>(graph);
  180. }
  181. /* ************************************************************************* */
  182. NonlinearFactorGraph::shared_ptr gtsam::deserializeGraph(const std::string& serialized_graph) {
  183. NonlinearFactorGraph::shared_ptr result(new NonlinearFactorGraph());
  184. deserialize<NonlinearFactorGraph>(serialized_graph, *result);
  185. return result;
  186. }
  187. /* ************************************************************************* */
  188. std::string gtsam::serializeGraphXML(const NonlinearFactorGraph& graph, const std::string& name) {
  189. return serializeXML<NonlinearFactorGraph>(graph, name);
  190. }
  191. /* ************************************************************************* */
  192. NonlinearFactorGraph::shared_ptr gtsam::deserializeGraphXML(const std::string& serialized_graph,
  193. const std::string& name) {
  194. NonlinearFactorGraph::shared_ptr result(new NonlinearFactorGraph());
  195. deserializeXML<NonlinearFactorGraph>(serialized_graph, *result, name);
  196. return result;
  197. }
  198. /* ************************************************************************* */
  199. std::string gtsam::serializeValues(const Values& values) {
  200. return serialize<Values>(values);
  201. }
  202. /* ************************************************************************* */
  203. Values::shared_ptr gtsam::deserializeValues(const std::string& serialized_values) {
  204. Values::shared_ptr result(new Values());
  205. deserialize<Values>(serialized_values, *result);
  206. return result;
  207. }
  208. /* ************************************************************************* */
  209. std::string gtsam::serializeValuesXML(const Values& values, const std::string& name) {
  210. return serializeXML<Values>(values, name);
  211. }
  212. /* ************************************************************************* */
  213. Values::shared_ptr gtsam::deserializeValuesXML(const std::string& serialized_values,
  214. const std::string& name) {
  215. Values::shared_ptr result(new Values());
  216. deserializeXML<Values>(serialized_values, *result, name);
  217. return result;
  218. }
  219. /* ************************************************************************* */
  220. bool gtsam::serializeGraphToFile(const NonlinearFactorGraph& graph, const std::string& fname) {
  221. return serializeToFile<NonlinearFactorGraph>(graph, fname);
  222. }
  223. /* ************************************************************************* */
  224. bool gtsam::serializeGraphToXMLFile(const NonlinearFactorGraph& graph,
  225. const std::string& fname, const std::string& name) {
  226. return serializeToXMLFile<NonlinearFactorGraph>(graph, fname, name);
  227. }
  228. /* ************************************************************************* */
  229. bool gtsam::serializeValuesToFile(const Values& values, const std::string& fname) {
  230. return serializeToFile<Values>(values, fname);
  231. }
  232. /* ************************************************************************* */
  233. bool gtsam::serializeValuesToXMLFile(const Values& values,
  234. const std::string& fname, const std::string& name) {
  235. return serializeToXMLFile<Values>(values, fname, name);
  236. }
  237. /* ************************************************************************* */
  238. NonlinearFactorGraph::shared_ptr gtsam::deserializeGraphFromFile(const std::string& fname) {
  239. NonlinearFactorGraph::shared_ptr result(new NonlinearFactorGraph());
  240. if (!deserializeFromFile<NonlinearFactorGraph>(fname, *result))
  241. throw std::invalid_argument("Failed to open file" + fname);
  242. return result;
  243. }
  244. /* ************************************************************************* */
  245. NonlinearFactorGraph::shared_ptr gtsam::deserializeGraphFromXMLFile(const std::string& fname,
  246. const std::string& name) {
  247. NonlinearFactorGraph::shared_ptr result(new NonlinearFactorGraph());
  248. if (!deserializeFromXMLFile<NonlinearFactorGraph>(fname, *result, name))
  249. throw std::invalid_argument("Failed to open file" + fname);
  250. return result;
  251. }
  252. /* ************************************************************************* */
  253. Values::shared_ptr gtsam::deserializeValuesFromFile(const std::string& fname) {
  254. Values::shared_ptr result(new Values());
  255. if (!deserializeFromFile<Values>(fname, *result))
  256. throw std::invalid_argument("Failed to open file" + fname);
  257. return result;
  258. }
  259. /* ************************************************************************* */
  260. Values::shared_ptr gtsam::deserializeValuesFromXMLFile(const std::string& fname,
  261. const std::string& name) {
  262. Values::shared_ptr result(new Values());
  263. if (!deserializeFromXMLFile<Values>(fname, *result, name))
  264. throw std::invalid_argument("Failed to open file" + fname);
  265. return result;
  266. }
  267. /* ************************************************************************* */