13 #include "../geometry_utils.h" 24 using Cell = std::shared_ptr<GridCell>;
26 using Row = std::vector<Cell>;
27 using Map = std::vector<Row>;
35 GridMap(std::shared_ptr<GridCellFactory> cell_factory,
40 _cell_factory(cell_factory), _cells(_height) {
42 _map_center_x = std::floor((
float)_width /2);
43 _map_center_y = std::floor((
float)_height/2);
45 for (
auto &row : _cells) {
46 for (
int i = 0; i < _width; i++) {
47 row.push_back(cell_factory->create_cell());
50 _unvisited_cell = cell_factory->create_cell();
54 int width()
const {
return _width; }
57 int height()
const {
return _height; }
60 double scale()
const {
return _m_per_cell; }
63 const std::vector<std::vector<Cell>>
cells()
const {
return _cells; }
65 #define COL_IND(cell_coord) \ 66 ((cell_coord).x + _map_center_x) 67 #define ROW_IND(cell_coord) \ 68 ((cell_coord).y + _map_center_y) 77 const Occupancy &new_value,
double quality = 1.0) {
79 update_size(cell_coord);
83 _cells[row][col]->set_value(new_value, quality);
91 if (!has_cell(cell_coord))
92 return _unvisited_cell->value();
102 #define METERS_TO_CELLS(var) \ 103 std::floor((var)/_m_per_cell) 106 #undef METERS_TO_CELLS 119 bounds.
bot = cell_coord.
y * _m_per_cell;
120 bounds.
top = bounds.
bot + _m_per_cell;
121 bounds.
left = cell_coord.
x * _m_per_cell;
122 bounds.
right = bounds.
left + _m_per_cell;
137 return 0 <=
COL_IND(cell_coord) &&
COL_IND(cell_coord) < _width &&
142 resize_bound(RESIZE_HORZ,
COL_IND(cell_coord));
143 resize_bound(RESIZE_VERT,
ROW_IND(cell_coord));
146 #define RESIZE_DIR(DIM,DIR) \ 147 (((DIM) << RESIZE_DIM_BIT) | ((DIR) << RESIZE_DIR_BIT)) 150 if (dim != RESIZE_HORZ && dim != RESIZE_VERT)
153 int bound = (dim == RESIZE_HORZ ?
width() :
height());
155 int new_bound = calc_sufficient_bound(container_coord, bound);
156 if (new_bound == bound)
160 int expand_rate = closest_bounded_power_two(new_bound/bound) - 1;
161 resize_in_direction(expand_rate*bound,
RESIZE_DIR(dim, container_coord<0));
165 if (container_coord < 0)
166 return (-container_coord)+map_bound;
167 if (map_bound <= container_coord)
168 return container_coord + 1;
189 if (direction_flag == RESIZE_DOWN) {
190 add_rows(delta, _cells.begin());
191 _map_center_y += delta;
192 }
else if (direction_flag == RESIZE_UP) {
193 add_rows(delta, _cells.end());
194 }
else if (direction_flag == RESIZE_LEFT) {
195 add_cols(delta, [](
Row& vector) {
return vector.begin(); });
196 _map_center_x += delta;
197 }
else if (direction_flag == RESIZE_RIGHT) {
198 add_cols(delta, [](
Row& vector) {
return vector.end(); });
202 void add_rows(
const int rows_count, Map::iterator it) {
204 for (
auto& row : new_rows) {
205 for (
auto& cell : row) {
206 cell = _cell_factory->create_cell();
209 _cells.insert(it, new_rows.begin(), new_rows.end());
210 _height += rows_count;
213 void add_cols(
const int cols_count, std::function<Row::iterator(
Row&)> it) {
215 for (
auto& row : new_cols) {
216 for (
auto& cell : row) {
217 cell = _cell_factory->create_cell();
220 for (
size_t i = 0; i < new_cols.size(); i++) {
221 _cells[i].insert(it(_cells[i]), new_cols[i].begin(), new_cols[i].end());
223 _width += cols_count;
229 const int RESIZE_VERT = 0;
230 const int RESIZE_HORZ = 1;
231 const int RESIZE_DIM_BIT = 1;
233 const int RESIZE_FWD = 0;
234 const int RESIZE_BWD = 1;
235 const int RESIZE_DIR_BIT = 0;
239 const int RESIZE_RIGHT =
RESIZE_DIR(RESIZE_HORZ, RESIZE_FWD);
double cell_value(const DiscretePoint2D &cell_coord) const
#define METERS_TO_CELLS(var)
double top
The top of a rectangle.
GridMap(std::shared_ptr< GridCellFactory > cell_factory, const GridMapParams &init_params)
double bot
The bottom of a rectangle.
#define ROW_IND(cell_coord)
int width() const
Returns the width of the map.
Rectangle world_cell_bounds(const DiscretePoint2D &cell_coord)
int height() const
Returns thr height of the map.
double right
The right side of a rectangle.
An occupancy grid implementation.
#define COL_IND(cell_coord)
void update_size(const DiscretePoint2D &cell_coord)
void resize_in_direction(const int delta, const int direction_flag)
void update_cell(const DiscretePoint2D &cell_coord, const Occupancy &new_value, double quality=1.0)
std::shared_ptr< GridCellFactory > _cell_factory
int calc_sufficient_bound(const int container_coord, const int map_bound)
void add_rows(const int rows_count, Map::iterator it)
double cell_scale() const
Returnes the scale.
Defines interface Cell Occupancy Estimator. There are structures Occupancy and Beam that are used in ...
int y
Coordinates of point.
double scale() const
Returns the scale.
void add_cols(const int cols_count, std::function< Row::iterator(Row &)> it)
double left
The left side of a rectangle.
void resize_bound(const int dim, const int container_coord)
long closest_bounded_power_two(long x)
Defines an axis-aligned rectangle.
DiscretePoint2D world_to_cell(double x, double y) const
#define RESIZE_DIR(DIM, DIR)
bool has_cell(const DiscretePoint2D &cell_coord) const
std::shared_ptr< GridCell > Cell
Defines a point with integer coordinates on a plane.
const std::vector< std::vector< Cell > > cells() const
Returns map's cells.
The following classes are defined in this file GridCell - base class for GridMap's cell; GridCellFact...