plain_grid_map.h
Go to the documentation of this file.
1 #ifndef SLAM_CTOR_CORE_PLAIN_GRID_MAP_H_INCLUDED
2 #define SLAM_CTOR_CORE_PLAIN_GRID_MAP_H_INCLUDED
3 
4 #include <cmath>
5 #include <vector>
6 #include <memory>
7 #include <cassert>
8 #include <algorithm>
9 
10 #include "grid_map.h"
11 
12 class PlainGridMap : public GridMap {
13 public:
14  // TODO: cp, mv ctors, dtor
15  PlainGridMap(std::shared_ptr<GridCell> prototype,
16  const GridMapParams& params = MapValues::gmp)
17  : GridMap{prototype, params}, _cells(GridMap::height()) {
18  for (auto &row : _cells) {
19  row.reserve(GridMap::width());
20  for (int i = 0; i < GridMap::width(); i++) {
21  row.push_back(prototype->clone());
22  }
23  }
24  }
25 
26  const GridCell &operator[](const Coord& c) const override {
27  auto coord = external2internal(c);
28  assert(has_internal_cell(coord));
29  return cell_internal(coord);
30  }
31 
32 protected: // fields
33 
34  const GridCell& cell_internal(const Coord& ic) const {
35  return *_cells[ic.y][ic.x];
36  }
37 
38  std::vector<std::vector<std::unique_ptr<GridCell>>> _cells;
39 };
40 
41 /* Unbounded implementation */
42 
44 private: // fields
45  static constexpr double Expansion_Rate = 1.2;
46 public: // methods
47  UnboundedPlainGridMap(std::shared_ptr<GridCell> prototype,
48  const GridMapParams &params = MapValues::gmp)
49  : PlainGridMap{prototype, params}
50  , _origin{GridMap::origin()}, _unknown_cell{prototype->clone()} {}
51 
52  void update(const Coord &area_id,
53  const AreaOccupancyObservation &aoo) override {
54  ensure_inside(area_id);
55  PlainGridMap::update(area_id, aoo);
56  }
57 
58  void reset(const Coord &area_id, const GridCell &new_area) override {
59  ensure_inside(area_id);
60  PlainGridMap::reset(area_id, new_area);
61  }
62 
63  const GridCell &operator[](const Coord& ec) const override {
64  auto ic = external2internal(ec);
65  if (!PlainGridMap::has_internal_cell(ic)) { return *_unknown_cell; }
66  return PlainGridMap::cell_internal(ic);
67  }
68 
69  Coord origin() const override { return _origin; }
70 
71  bool has_cell(const Coord &) const override { return true; }
72 
73  std::vector<char> save_state() const override {
74  auto w = width(), h = height();
75  size_t map_size_bytes = w * h * _unknown_cell->serialize().size();
76 
77  Serializer s(sizeof(GridMapParams) + sizeof(Coord) + map_size_bytes);
78  s << h << w << scale() << origin().x << origin().y;
79 
80  Serializer ms(map_size_bytes);
81  for (auto &row : _cells) {
82  for (auto &cell : row) {
83  ms.append(cell->serialize());
84  }
85  }
86  #ifdef COMPRESSED_SERIALIZATION
87  s.append(ms.compressed());
88  #else
89  s.append(ms.result());
90  #endif
91  return s.result();
92  }
93 
94  void load_state(const std::vector<char>& data) override {
95  decltype(width()) w, h;
96  decltype(scale()) s;
97 
98  Deserializer d(data);
99  d >> h >> w >> s >> _origin.x >> _origin.y;
100 
101  set_width(w);
102  set_height(h);
103  set_scale(s);
104  #ifdef COMPRESSED_SERIALIZATION
105  std::vector<char> map_data = Deserializer::decompress(
106  data.data() + d.pos(), data.size() - d.pos(),
107  w * h * _unknown_cell->serialize().size());
108  size_t pos = 0;
109  #else
110  const std::vector<char> &map_data = data;
111  size_t pos = d.pos();
112  #endif
113  _cells.clear();
114  _cells.resize(h);
115  for (auto &row : _cells) {
116  row.reserve(w);
117  for (int i = 0; i < w; ++i) {
118  auto cell = new_cell();
119  pos = cell->deserialize(map_data, pos);
120  row.push_back(std::move(cell));
121  }
122  }
123  }
124 
125 protected: // methods
126 
127  bool ensure_inside(const Coord &c) {
128  auto coord = external2internal(c);
129  if (PlainGridMap::has_internal_cell(coord)) return false;
130 
131  unsigned w = width(), h = height();
132  unsigned prep_x = 0, app_x = 0, prep_y = 0, app_y = 0;
133  std::tie(prep_x, app_x) = determine_cells_nm(0, coord.x, w);
134  std::tie(prep_y, app_y) = determine_cells_nm(0, coord.y, h);
135 
136  unsigned new_w = prep_x + w + app_x, new_h = prep_y + h + app_y;
137  #define UPDATE_DIM(dim, elem) \
138  if (dim < new_##dim && new_##dim < Expansion_Rate * dim) { \
139  double scale = prep_##elem / (new_##dim - dim); \
140  prep_##elem += (Expansion_Rate * dim - new_##dim) * scale; \
141  new_##dim = Expansion_Rate * dim; \
142  app_##elem = new_##dim - (prep_##elem + dim); \
143  }
144 
145  UPDATE_DIM(w, x);
146  UPDATE_DIM(h, y);
147  #undef UPDATE_DIM
148 
149  // PERFORMANCE: _cells can be reused
150  std::vector<std::vector<std::unique_ptr<GridCell>>> new_cells{new_h};
151  for (size_t y = 0; y != new_h; ++y) {
152  std::generate_n(std::back_inserter(new_cells[y]), new_w,
153  [this](){ return this->_unknown_cell->clone(); });
154  if (y < prep_y || prep_y + h <= y) { continue; }
155 
156  std::move(_cells[y - prep_y].begin(), _cells[y - prep_y].end(),
157  &new_cells[y][prep_x]);
158  }
159 
160  std::swap(_cells, new_cells);
161  set_height(new_h);
162  set_width(new_w);
163  _origin += Coord(prep_x, prep_y);
164 
165  assert(PlainGridMap::has_cell(c));
166  return true;
167  }
168 
169  std::tuple<unsigned, unsigned> determine_cells_nm(
170  int min, int val, int max) const {
171  assert(min <= max);
172  unsigned prepend_nm = 0, append_nm = 0;
173  if (val < min) {
174  prepend_nm = min - val;
175  } else if (max <= val) {
176  append_nm = val - max + 1;
177  }
178  return std::make_tuple(prepend_nm, append_nm);
179  }
180 
181 private: // fields
183  std::shared_ptr<GridCell> _unknown_cell;
184 };
185 
186 #endif
d
void update(const Coord &area_id, const AreaOccupancyObservation &aoo) override
Updates area with a given observation.
Definition: grid_map.h:41
Coord external2internal(const Coord &coord) const
bool ensure_inside(const Coord &c)
std::unique_ptr< GridCell > new_cell() const
Definition: grid_map.h:31
const GridCell & operator[](const Coord &c) const override
PlainGridMap(std::shared_ptr< GridCell > prototype, const GridMapParams &params=MapValues::gmp)
static constexpr GridMapParams gmp
Definition: grid_map.h:19
Coord origin() const override
void update(const Coord &area_id, const AreaOccupancyObservation &aoo) override
Updates area with a given observation.
virtual bool has_cell(const Coord &c) const
size_t pos() const
Definition: serialization.h:73
std::tuple< unsigned, unsigned > determine_cells_nm(int min, int val, int max) const
void set_width(unsigned w)
XmlRpcServer s
TFSIMD_FORCE_INLINE const tfScalar & y() const
std::vector< std::vector< std::unique_ptr< GridCell > > > _cells
virtual DiscretePoint2D origin() const
bool has_internal_cell(const Coord &c) const
void append(const std::vector< char > &new_data)
Definition: serialization.h:35
#define UPDATE_DIM(dim, elem)
void set_height(unsigned h)
const GridCell & cell_internal(const Coord &ic) const
TFSIMD_FORCE_INLINE const tfScalar & x() const
bool has_cell(const Coord &) const override
std::vector< char > save_state() const override
UnboundedPlainGridMap(std::shared_ptr< GridCell > prototype, const GridMapParams &params=MapValues::gmp)
TFSIMD_FORCE_INLINE const tfScalar & w() const
virtual int height() const
std::shared_ptr< GridCell > _unknown_cell
std::vector< char > result() const
Definition: serialization.h:24
void reset(const Coord &area_id, const GridCell &new_area) override
void load_state(const std::vector< char > &data) override
virtual void reset(const Coord &area_id, const GridCell &new_area)
Definition: grid_map.h:54
virtual double scale() const
const GridCell & operator[](const Coord &ec) const override
virtual int width() const


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