read_g2o.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2016 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  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: vitus@google.com (Michael Vitus)
  30. //
  31. // Reads a file in the g2o filename format that describes a pose graph problem.
  32. #ifndef EXAMPLES_CERES_READ_G2O_H_
  33. #define EXAMPLES_CERES_READ_G2O_H_
  34. #include <fstream>
  35. #include <string>
  36. #include "glog/logging.h"
  37. namespace ceres::examples {
  38. // Reads a single pose from the input and inserts it into the map. Returns false
  39. // if there is a duplicate entry.
  40. template <typename Pose, typename Allocator>
  41. bool ReadVertex(std::ifstream* infile,
  42. std::map<int, Pose, std::less<int>, Allocator>* poses) {
  43. int id;
  44. Pose pose;
  45. *infile >> id >> pose;
  46. // Ensure we don't have duplicate poses.
  47. if (poses->find(id) != poses->end()) {
  48. LOG(ERROR) << "Duplicate vertex with ID: " << id;
  49. return false;
  50. }
  51. (*poses)[id] = pose;
  52. return true;
  53. }
  54. // Reads the constraints between two vertices in the pose graph
  55. template <typename Constraint, typename Allocator>
  56. void ReadConstraint(std::ifstream* infile,
  57. std::vector<Constraint, Allocator>* constraints) {
  58. Constraint constraint;
  59. *infile >> constraint;
  60. constraints->push_back(constraint);
  61. }
  62. // Reads a file in the g2o filename format that describes a pose graph
  63. // problem. The g2o format consists of two entries, vertices and constraints.
  64. //
  65. // In 2D, a vertex is defined as follows:
  66. //
  67. // VERTEX_SE2 ID x_meters y_meters yaw_radians
  68. //
  69. // A constraint is defined as follows:
  70. //
  71. // EDGE_SE2 ID_A ID_B A_x_B A_y_B A_yaw_B I_11 I_12 I_13 I_22 I_23 I_33
  72. //
  73. // where I_ij is the (i, j)-th entry of the information matrix for the
  74. // measurement.
  75. //
  76. //
  77. // In 3D, a vertex is defined as follows:
  78. //
  79. // VERTEX_SE3:QUAT ID x y z q_x q_y q_z q_w
  80. //
  81. // where the quaternion is in Hamilton form.
  82. // A constraint is defined as follows:
  83. //
  84. // EDGE_SE3:QUAT ID_a ID_b x_ab y_ab z_ab q_x_ab q_y_ab q_z_ab q_w_ab I_11 I_12 I_13 ... I_16 I_22 I_23 ... I_26 ... I_66 // NOLINT
  85. //
  86. // where I_ij is the (i, j)-th entry of the information matrix for the
  87. // measurement. Only the upper-triangular part is stored. The measurement order
  88. // is the delta position followed by the delta orientation.
  89. template <typename Pose,
  90. typename Constraint,
  91. typename MapAllocator,
  92. typename VectorAllocator>
  93. bool ReadG2oFile(const std::string& filename,
  94. std::map<int, Pose, std::less<int>, MapAllocator>* poses,
  95. std::vector<Constraint, VectorAllocator>* constraints) {
  96. CHECK(poses != nullptr);
  97. CHECK(constraints != nullptr);
  98. poses->clear();
  99. constraints->clear();
  100. std::ifstream infile(filename.c_str());
  101. if (!infile) {
  102. return false;
  103. }
  104. std::string data_type;
  105. while (infile.good()) {
  106. // Read whether the type is a node or a constraint.
  107. infile >> data_type;
  108. if (data_type == Pose::name()) {
  109. if (!ReadVertex(&infile, poses)) {
  110. return false;
  111. }
  112. } else if (data_type == Constraint::name()) {
  113. ReadConstraint(&infile, constraints);
  114. } else {
  115. LOG(ERROR) << "Unknown data type: " << data_type;
  116. return false;
  117. }
  118. // Clear any trailing whitespace from the line.
  119. infile >> std::ws;
  120. }
  121. return true;
  122. }
  123. } // namespace ceres::examples
  124. #endif // EXAMPLES_CERES_READ_G2O_H_