map_dumpers.h
Go to the documentation of this file.
1 #ifndef SLAM_CTOR_UTIL_MAP_DUMPERS
2 #define SLAM_CTOR_UTIL_MAP_DUMPERS
3 
4 #include <cmath>
5 #include <fstream>
6 #include <string>
7 
8 #include "../core/math_utils.h"
9 #include "../core/states/world.h"
10 #include "../core/states/state_data.h"
11 #include "../core/maps/grid_map.h"
12 
13 template <typename GridMapType>
14 class GridMapToPgmDumber : public WorldMapObserver<GridMapType> {
15  // Used PGM format discription: http://netpbm.sourceforge.net/doc/pgm.html
16 private:
17  using IntensityType = unsigned char;
18  static constexpr IntensityType Max_Intensity = 255;
19 private:
20  class PgmHeader {
21  public:
22  PgmHeader(int w, int h, unsigned max_v)
23  : _width{w}, _height{h}, _max_val{max_v} {}
24 
25  void write(std::ostream &os) const {
26  os.write(_magic, sizeof(_magic));
27  write_whitespace(os);
29  write_whitespace(os);
31  write_whitespace(os);
33  }
34  private:
35 
36  void write_whitespace(std::ostream &os) const {
37  char new_line = '\n';
38  os.write(&new_line, sizeof(new_line));
39  }
40 
41  template <typename T>
42  void write_integral(std::ostream &os, T value) const {
43  auto str = std::to_string(value);
44  os.write(str.c_str(), str.size());
45  }
46 
47  private:
48  char _magic[2] = {'P', '5'};
50  unsigned _max_val;
51  };
52 
53 public:
54  GridMapToPgmDumber(const std::string &base_fname)
55  : _base_fname{base_fname + "_"}
56  , _id{0} {}
57 
58  void on_map_update(const GridMapType &map) override {
59  std::ofstream dst(_base_fname + std::to_string(_id) + ".pgm",
60  std::ios::binary | std::ios::out);
61  dump_map(dst, map);
62  dst.close();
63  ++_id;
64  }
65 
66  static void dump_map(std::ofstream &os, const GridMapType &map) {
67  auto w = map.width(), h = map.height();
68  auto origin = map.origin();
69  // write pgm header
70  PgmHeader{w, h, Max_Intensity}.write(os);
71  char new_line = '\n';
72  os.write(&new_line, sizeof(new_line));
73 
74  // write map content
75  using AreaId = typename GridMapType::Coord;
76  auto area_id = AreaId{};
77 
78  int val_nm = 0;
79  for (area_id.y = h - origin.y - 1; -origin.y <= area_id.y; --area_id.y) {
80  for (area_id.x = -origin.x; area_id.x < w - origin.x; ++area_id.x) {
81  auto occ = map.occupancy(area_id);
82  auto value = 1.0 - (occ == -1 ? 0.5 : bound_value(0.0, occ, 1.0));
83  auto intensity = static_cast<IntensityType>(Max_Intensity * value);
84  static_assert(sizeof(intensity) == 1, "PGM insensity is not char");
85  os.write(reinterpret_cast<char*>(&intensity), sizeof(intensity));
86  ++val_nm;
87  }
88  }
89  assert(val_nm == w * h); // sanity
90  }
91 private:
92  std::string _base_fname;
93  unsigned long long _id;
94 };
95 
96 #endif
void write_integral(std::ostream &os, T value) const
Definition: map_dumpers.h:42
unsigned long long _id
Definition: map_dumpers.h:93
void write(std::ostream &os) const
Definition: map_dumpers.h:25
static void dump_map(std::ofstream &os, const GridMapType &map)
Definition: map_dumpers.h:66
void write_whitespace(std::ostream &os) const
Definition: map_dumpers.h:36
double bound_value(double left, double v, double right)
Definition: math_utils.h:40
void on_map_update(const GridMapType &map) override
Definition: map_dumpers.h:58
TFSIMD_FORCE_INLINE const tfScalar & w() const
std::string _base_fname
Definition: map_dumpers.h:92
static constexpr IntensityType Max_Intensity
Definition: map_dumpers.h:18
PgmHeader(int w, int h, unsigned max_v)
Definition: map_dumpers.h:22
unsigned char IntensityType
Definition: map_dumpers.h:17
GridMapToPgmDumber(const std::string &base_fname)
Definition: map_dumpers.h:54


slam_constructor
Author(s): JetBrains Research, OSLL team
autogenerated on Mon Jun 10 2019 15:08:25