map_primitives.h
Go to the documentation of this file.
1 #ifndef SLAM_CTOR_UTILS_DG_MAP_PRIMITIVES_INCLUDED
2 #define SLAM_CTOR_UTILS_DG_MAP_PRIMITIVES_INCLUDED
3 
4 #include <string>
5 #include <iostream>
6 #include <sstream>
7 #include <utility>
8 #include <vector>
9 
10 #include "../../core/geometry_utils.h"
11 
12 class MapPrimitive {
13 public:
14  static constexpr int Unknown_Value = -1;
15 public: // methods
16  virtual int width() const { return Unknown_Value; };
17  virtual int height() const { return Unknown_Value; };
18  // any rectangle guaranteed to be free; origin - top-left
19  virtual std::vector<Rectangle> free_space() const {
20  return {};
21  };
22  virtual std::istream& to_stream() const = 0;
23 };
24 
26 public: // consts
27  static constexpr char Completely_Occupied_Marker = '+';
28  static constexpr char Completely_Free_Marker = ' ';
29  static constexpr char Row_End_Marker = '\n';
30 public: // methods
31  std::istream& to_stream() const override {
32  return _stream_pin = std::stringstream{text_raster()};
33  }
34 protected: // methods
35  virtual std::string text_raster() const = 0;
36 private:
37  mutable std::stringstream _stream_pin;
38 };
39 
40 // TODO: move to *.cpp
44 
45 //------------------------------------------------------------------------------
46 // Actual Text Raster Primitives
47 //
48 // Responsibilities:
49 // * provides a stream with text raster
50 // * knows actual geometry of the primitive (opt)
51 // * generates the primitive (opt)
52 
53 
54 /*
55 
56  <-- w -->
57  +++++++++ ^ <-- BoundPosition::Top
58  + + |
59  + + h
60  + + |
61  + + v
62 
63  */
65 public: // const
66  enum class BoundPosition { Left, Right, Top, Bot};
67 public: // methods
69  BoundPosition bnd_pos = BoundPosition::Top)
70  : _width{w}, _height{h}, _bnd_pos{bnd_pos} {
71 
72  assert(0 < _width && 0 < _height);
73  bool is_bnd_horiz = _bnd_pos == BoundPosition::Top ||
74  _bnd_pos == BoundPosition::Bot;
75  if (is_bnd_horiz) {
76  _raw_cecum = generate_horizontally_bounded_cecum(_bnd_pos);
77  } else {
78  _raw_cecum = generate_vertically_bounded_cecum(_bnd_pos);
79  }
80  }
81 
82  int width() const override { return _width; };
83  int height() const override { return _height; };
84  std::vector<Rectangle> free_space() const override {
85  if (height() <= 2 || width() <= 2) {
86  return {Rectangle{0, 0, 0, 0}};
87  }
88  switch (_bnd_pos) {
89  case BoundPosition::Top:
90  return {Rectangle(-height() + 1, 0, 1, width() - 1)};
91  case BoundPosition::Bot:
92  return {Rectangle(-height() + 2, 1, 1, width() - 1)};
93  case BoundPosition::Left:
94  return {Rectangle(-height() + 2, 0, 1, width())};
95  case BoundPosition::Right:
96  return {Rectangle(-height() + 2, 0, 0, width() - 1)};
97  }
99  };
100 
101 protected: // methods
102  std::string text_raster() const override { return _raw_cecum; };
103 private: // methods
105  assert(bnd_pos == BoundPosition::Top || bnd_pos == BoundPosition::Bot);
106 
107  std::string cecum;
108  for (int row_i = 0; row_i < height(); ++row_i) {
109  if ((row_i == 0 && bnd_pos == BoundPosition::Top) ||
110  (row_i == height() - 1 && bnd_pos == BoundPosition::Bot)) {
111  // generate horizontal bound
112  cecum += std::string(width(), Completely_Occupied_Marker);
113  cecum += Row_End_Marker;
114  } else {
115  cecum += Completely_Occupied_Marker;
116  for (int col_i = 1; col_i < width(); ++col_i) {
117  cecum += col_i == width() - 1 ? Completely_Occupied_Marker
118  : Completely_Free_Marker;
119  }
120  cecum += Row_End_Marker;
121  }
122  }
123  return cecum;
124  }
125 
127  assert(bnd_pos == BoundPosition::Left || bnd_pos == BoundPosition::Right);
128 
129  std::string cecum(width(), Completely_Occupied_Marker);
130  cecum += Row_End_Marker;
131  for (int row_i = 1; row_i < height(); ++row_i) {
132  if (row_i == height() - 1) {
133  cecum += std::string(width(), Completely_Occupied_Marker);
134  continue;
135  }
136 
137  for (int col_i = 0; col_i < width(); ++col_i) {
138  bool is_occ = width() == 1 ||
139  (col_i == 0 && bnd_pos == BoundPosition::Left) ||
140  (col_i == width() - 1 && bnd_pos == BoundPosition::Right);
141 
142  cecum += is_occ ? Completely_Occupied_Marker : Completely_Free_Marker;
143  }
144  cecum += Row_End_Marker;
145  }
146  cecum += Row_End_Marker;
147  return cecum;
148  }
149 
150 private: // fields
151  int _width, _height;
152  std::string _raw_cecum;
154 };
155 
156 #endif
std::vector< Rectangle > free_space() const override
static constexpr char Row_End_Marker
int height() const override
virtual std::vector< Rectangle > free_space() const
std::istream & to_stream() const override
std::string generate_vertically_bounded_cecum(BoundPosition bnd_pos)
std::string text_raster() const override
virtual int width() const
int width() const override
static constexpr int Unknown_Value
static constexpr char Completely_Free_Marker
virtual std::istream & to_stream() const =0
static constexpr char Completely_Occupied_Marker
CecumTextRasterMapPrimitive(int w, int h, BoundPosition bnd_pos=BoundPosition::Top)
virtual int height() const
std::string generate_horizontally_bounded_cecum(BoundPosition bnd_pos)
std::stringstream _stream_pin


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