#ifndef EXAMPLES_CERES_READ_G2O_H_
#define EXAMPLES_CERES_READ_G2O_H_
#include <fstream>
#include <string>
#include "glog/logging.h"
namespace ceres {
namespace examples {
template <typename Pose, typename Allocator>
bool ReadVertex(std::ifstream* infile,
std::map<int, Pose, std::less<int>, Allocator>* poses) {
int id;
Pose pose;
*infile >> id >> pose;
if (poses->find(id) != poses->end()) {
LOG(ERROR) << "Duplicate vertex with ID: " << id;
return false;
}
(*poses)[id] = pose;
return true;
}
template <typename Constraint, typename Allocator>
void ReadConstraint(std::ifstream* infile,
std::vector<Constraint, Allocator>* constraints) {
Constraint constraint;
*infile >> constraint;
constraints->push_back(constraint);
}
template <typename Pose, typename Constraint, typename MapAllocator,
typename VectorAllocator>
bool ReadG2oFile(const std::string& filename,
std::map<int, Pose, std::less<int>, MapAllocator>* poses,
std::vector<Constraint, VectorAllocator>* constraints) {
CHECK(poses != NULL);
CHECK(constraints != NULL);
poses->clear();
constraints->clear();
std::ifstream infile(filename.c_str());
if (!infile) {
return false;
}
std::string data_type;
while (infile.good()) {
infile >> data_type;
if (data_type == Pose::name()) {
if (!ReadVertex(&infile, poses)) {
return false;
}
} else if (data_type == Constraint::name()) {
ReadConstraint(&infile, constraints);
} else {
LOG(ERROR) << "Unknown data type: " << data_type;
return false;
}
infile >> std::ws;
}
return true;
}
} }
#endif