blob: c427939a53c8dd712fd652a9a96f1081df5ce171 [file] [log] [blame]
Milind Upadhyay7c205222022-11-16 18:20:58 -08001// 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
33#ifndef EXAMPLES_CERES_READ_G2O_H_
34#define EXAMPLES_CERES_READ_G2O_H_
35
36#include <fstream>
37#include <string>
38
39#include "glog/logging.h"
40
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080041namespace ceres::examples {
Milind Upadhyay7c205222022-11-16 18:20:58 -080042
43// Reads a single pose from the input and inserts it into the map. Returns false
44// if there is a duplicate entry.
45template <typename Pose, typename Allocator>
Philipp Schrader790cb542023-07-05 21:06:52 -070046bool ReadVertex(std::ifstream *infile,
47 std::map<int, Pose, std::less<int>, Allocator> *poses) {
Milind Upadhyay7c205222022-11-16 18:20:58 -080048 int id;
49 Pose pose;
50 *infile >> id >> pose;
51
52 // Ensure we don't have duplicate poses.
53 if (poses->find(id) != poses->end()) {
54 LOG(ERROR) << "Duplicate vertex with ID: " << id;
55 return false;
56 }
57 (*poses)[id] = pose;
58
59 return true;
60}
61
62// Reads the contraints between two vertices in the pose graph
63template <typename Constraint, typename Allocator>
Philipp Schrader790cb542023-07-05 21:06:52 -070064void ReadConstraint(std::ifstream *infile,
65 std::vector<Constraint, Allocator> *constraints) {
Milind Upadhyay7c205222022-11-16 18:20:58 -080066 Constraint constraint;
67 *infile >> constraint;
68
69 constraints->push_back(constraint);
70}
71
72// Reads a file in the g2o filename format that describes a pose graph
73// problem. The g2o format consists of two entries, vertices and constraints.
74//
75// In 2D, a vertex is defined as follows:
76//
77// VERTEX_SE2 ID x_meters y_meters yaw_radians
78//
79// A constraint is defined as follows:
80//
81// 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
82//
83// where I_ij is the (i, j)-th entry of the information matrix for the
84// measurement.
85//
86//
87// In 3D, a vertex is defined as follows:
88//
89// VERTEX_SE3:QUAT ID x y z q_x q_y q_z q_w
90//
91// where the quaternion is in Hamilton form.
92// A constraint is defined as follows:
93//
Philipp Schrader790cb542023-07-05 21:06:52 -070094// 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
95// I_13 ... I_16 I_22 I_23 ... I_26 ... I_66 // NOLINT
Milind Upadhyay7c205222022-11-16 18:20:58 -080096//
97// where I_ij is the (i, j)-th entry of the information matrix for the
98// measurement. Only the upper-triangular part is stored. The measurement order
99// is the delta position followed by the delta orientation.
Philipp Schrader790cb542023-07-05 21:06:52 -0700100template <typename Pose, typename Constraint, typename MapAllocator,
Milind Upadhyay7c205222022-11-16 18:20:58 -0800101 typename VectorAllocator>
Philipp Schrader790cb542023-07-05 21:06:52 -0700102bool ReadG2oFile(const std::string &filename,
103 std::map<int, Pose, std::less<int>, MapAllocator> *poses,
104 std::vector<Constraint, VectorAllocator> *constraints) {
Milind Upadhyay7c205222022-11-16 18:20:58 -0800105 CHECK(poses != NULL);
106 CHECK(constraints != NULL);
107
108 poses->clear();
109 constraints->clear();
110
111 std::ifstream infile(filename.c_str());
112 if (!infile) {
113 return false;
114 }
115
116 std::string data_type;
117 while (infile.good()) {
118 // Read whether the type is a node or a constraint.
119 infile >> data_type;
120 if (data_type == Pose::name()) {
121 if (!ReadVertex(&infile, poses)) {
122 return false;
123 }
124 } else if (data_type == Constraint::name()) {
125 ReadConstraint(&infile, constraints);
126 } else {
127 LOG(ERROR) << "Unknown data type: " << data_type;
128 return false;
129 }
130
131 // Clear any trailing whitespace from the line.
132 infile >> std::ws;
133 }
134
135 return true;
136}
137
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800138} // namespace ceres::examples
Milind Upadhyay7c205222022-11-16 18:20:58 -0800139
140#endif // EXAMPLES_CERES_READ_G2O_H_