21 #include "glog/logging.h" 22 #include "yaml-cpp/yaml.h" 28 void WriteOccupancyGridToPgm(const ::nav_msgs::OccupancyGrid& grid,
29 const std::string& filename) {
30 LOG(INFO) <<
"Saving map to '" << filename <<
"'...";
31 std::ofstream pgm_file(filename, std::ios::out | std::ios::binary);
32 const std::string
header =
"P5\n# Cartographer map; " +
33 std::to_string(grid.info.resolution) +
34 " m/pixel\n" + std::to_string(grid.info.width) +
35 " " + std::to_string(grid.info.height) +
"\n255\n";
36 pgm_file.write(header.data(), header.size());
37 for (
size_t y = 0;
y < grid.info.height; ++
y) {
38 for (
size_t x = 0;
x < grid.info.width; ++
x) {
39 const size_t i =
x + (grid.info.height -
y - 1) * grid.info.width;
40 if (grid.data[i] >= 0 && grid.data[i] <= 100) {
41 pgm_file.put((100 - grid.data[i]) * 255 / 100);
44 constexpr uint8_t kUnknownValue = 128;
45 pgm_file.put(kUnknownValue);
50 CHECK(pgm_file) <<
"Writing '" << filename <<
"' failed.";
53 void WriteOccupancyGridInfoToYaml(const ::nav_msgs::OccupancyGrid& grid,
54 const std::string& map_filename,
55 const std::string& yaml_filename) {
56 LOG(INFO) <<
"Saving map info to '" << yaml_filename <<
"'...";
57 std::ofstream yaml_file(yaml_filename, std::ios::out | std::ios::binary);
59 YAML::Emitter out(yaml_file);
60 out << YAML::BeginMap;
62 out << YAML::Key <<
"image" << YAML::Value << map_filename;
63 out << YAML::Key <<
"resolution" << YAML::Value << grid.info.resolution;
66 constexpr
double kYawButMaybeIgnored = 0.;
67 out << YAML::Key <<
"origin" << YAML::Value << YAML::Flow << YAML::BeginSeq
68 << grid.info.origin.position.x << grid.info.origin.position.y
69 << kYawButMaybeIgnored << YAML::EndSeq;
70 out << YAML::Key <<
"occupied_thresh" << YAML::Value << 0.51;
71 out << YAML::Key <<
"free_thresh" << YAML::Value << 0.49;
72 out << YAML::Key <<
"negate" << YAML::Value << 0;
74 CHECK(out.good()) << out.GetLastError();
77 CHECK(yaml_file) <<
"Writing '" << yaml_filename <<
"' failed.";
83 const ::nav_msgs::OccupancyGrid& occupancy_grid,
const std::string& stem) {
84 const std::string pgm_filename = stem +
".pgm";
85 WriteOccupancyGridToPgm(occupancy_grid, pgm_filename);
86 WriteOccupancyGridInfoToYaml(occupancy_grid, pgm_filename, stem +
".yaml");
std_msgs::Header * header(M &m)
TFSIMD_FORCE_INLINE const tfScalar & y() const
TFSIMD_FORCE_INLINE const tfScalar & x() const
void WriteOccupancyGridToPgmAndYaml(const ::nav_msgs::OccupancyGrid &occupancy_grid, const std::string &stem)