Blame examples/slam/common/read_g2o.h

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2016 Google Inc. All rights reserved.
Packit ea1746
// http://ceres-solver.org/
Packit ea1746
//
Packit ea1746
// Redistribution and use in source and binary forms, with or without
Packit ea1746
// modification, are permitted provided that the following conditions are met:
Packit ea1746
//
Packit ea1746
// * Redistributions of source code must retain the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer.
Packit ea1746
// * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer in the documentation
Packit ea1746
//   and/or other materials provided with the distribution.
Packit ea1746
// * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
//   used to endorse or promote products derived from this software without
Packit ea1746
//   specific prior written permission.
Packit ea1746
//
Packit ea1746
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
// POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
//
Packit ea1746
// Author: vitus@google.com (Michael Vitus)
Packit ea1746
//
Packit ea1746
// Reads a file in the g2o filename format that describes a pose graph problem.
Packit ea1746
Packit ea1746
#ifndef EXAMPLES_CERES_READ_G2O_H_
Packit ea1746
#define EXAMPLES_CERES_READ_G2O_H_
Packit ea1746
Packit ea1746
#include <fstream>
Packit ea1746
#include <string>
Packit ea1746
Packit ea1746
#include "glog/logging.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace examples {
Packit ea1746
Packit ea1746
// Reads a single pose from the input and inserts it into the map. Returns false
Packit ea1746
// if there is a duplicate entry.
Packit ea1746
template <typename Pose, typename Allocator>
Packit ea1746
bool ReadVertex(std::ifstream* infile,
Packit ea1746
                std::map<int, Pose, std::less<int>, Allocator>* poses) {
Packit ea1746
  int id;
Packit ea1746
  Pose pose;
Packit ea1746
  *infile >> id >> pose;
Packit ea1746
Packit ea1746
  // Ensure we don't have duplicate poses.
Packit ea1746
  if (poses->find(id) != poses->end()) {
Packit ea1746
    LOG(ERROR) << "Duplicate vertex with ID: " << id;
Packit ea1746
    return false;
Packit ea1746
  }
Packit ea1746
  (*poses)[id] = pose;
Packit ea1746
Packit ea1746
  return true;
Packit ea1746
}
Packit ea1746
Packit ea1746
// Reads the contraints between two vertices in the pose graph
Packit ea1746
template <typename Constraint, typename Allocator>
Packit ea1746
void ReadConstraint(std::ifstream* infile,
Packit ea1746
                    std::vector<Constraint, Allocator>* constraints) {
Packit ea1746
  Constraint constraint;
Packit ea1746
  *infile >> constraint;
Packit ea1746
Packit ea1746
  constraints->push_back(constraint);
Packit ea1746
}
Packit ea1746
Packit ea1746
// Reads a file in the g2o filename format that describes a pose graph
Packit ea1746
// problem. The g2o format consists of two entries, vertices and constraints.
Packit ea1746
//
Packit ea1746
// In 2D, a vertex is defined as follows:
Packit ea1746
//
Packit ea1746
// VERTEX_SE2 ID x_meters y_meters yaw_radians
Packit ea1746
//
Packit ea1746
// A constraint is defined as follows:
Packit ea1746
//
Packit ea1746
// 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
Packit ea1746
//
Packit ea1746
// where I_ij is the (i, j)-th entry of the information matrix for the
Packit ea1746
// measurement.
Packit ea1746
//
Packit ea1746
//
Packit ea1746
// In 3D, a vertex is defined as follows:
Packit ea1746
//
Packit ea1746
// VERTEX_SE3:QUAT ID x y z q_x q_y q_z q_w
Packit ea1746
//
Packit ea1746
// where the quaternion is in Hamilton form.
Packit ea1746
// A constraint is defined as follows:
Packit ea1746
//
Packit ea1746
// 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
Packit ea1746
//
Packit ea1746
// where I_ij is the (i, j)-th entry of the information matrix for the
Packit ea1746
// measurement. Only the upper-triangular part is stored. The measurement order
Packit ea1746
// is the delta position followed by the delta orientation.
Packit ea1746
template 
Packit ea1746
          typename VectorAllocator>
Packit ea1746
bool ReadG2oFile(const std::string& filename,
Packit ea1746
                 std::map<int, Pose, std::less<int>, MapAllocator>* poses,
Packit ea1746
                 std::vector<Constraint, VectorAllocator>* constraints) {
Packit ea1746
  CHECK(poses != NULL);
Packit ea1746
  CHECK(constraints != NULL);
Packit ea1746
Packit ea1746
  poses->clear();
Packit ea1746
  constraints->clear();
Packit ea1746
Packit ea1746
  std::ifstream infile(filename.c_str());
Packit ea1746
  if (!infile) {
Packit ea1746
    return false;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  std::string data_type;
Packit ea1746
  while (infile.good()) {
Packit ea1746
    // Read whether the type is a node or a constraint.
Packit ea1746
    infile >> data_type;
Packit ea1746
    if (data_type == Pose::name()) {
Packit ea1746
      if (!ReadVertex(&infile, poses)) {
Packit ea1746
        return false;
Packit ea1746
      }
Packit ea1746
    } else if (data_type == Constraint::name()) {
Packit ea1746
      ReadConstraint(&infile, constraints);
Packit ea1746
    } else {
Packit ea1746
      LOG(ERROR) << "Unknown data type: " << data_type;
Packit ea1746
      return false;
Packit ea1746
    }
Packit ea1746
Packit ea1746
    // Clear any trailing whitespace from the line.
Packit ea1746
    infile >> std::ws;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  return true;
Packit ea1746
}
Packit ea1746
Packit ea1746
}  // namespace examples
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // EXAMPLES_CERES_READ_G2O_H_