bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
occupancy_grid.h
Go to the documentation of this file.
1#ifndef BITSIM_PLANNING_OCCUPANCY_GRID_H
2#define BITSIM_PLANNING_OCCUPANCY_GRID_H
3
5
6#include <array>
7#include <cstddef>
8#include <functional>
9#include <vector>
10
11namespace bitsim
12{
13namespace planning
14{
15
21{
24
25 bool operator==(const GridCell &other) const noexcept
26 {
27 return col == other.col && row == other.row;
28 }
29 bool operator!=(const GridCell &other) const noexcept { return !(*this == other); }
30};
31
43{
44 public:
53
54 uint_t n_cols() const noexcept { return n_cols_; }
55 uint_t n_rows() const noexcept { return n_rows_; }
56 real_t cell_size() const noexcept { return cell_size_; }
57
58 bool in_bounds(const GridCell &c) const noexcept;
59
63 bool is_free(const GridCell &c) const noexcept;
64
65 void set_occupied(const GridCell &c, bool occupied = true);
66
70 void add_rectangle(real_t x_min, real_t y_min, real_t x_max, real_t y_max);
71
75 void add_circle(real_t x, real_t y, real_t radius);
76
80 GridCell to_cell(real_t x, real_t y) const noexcept;
81
85 std::array<real_t, 2> to_world(const GridCell &c) const noexcept;
86
92 std::vector<GridCell> neighbours(const GridCell &c, bool diagonal = true) const;
93
97 uint_t n_occupied() const;
98
99 private:
100 std::size_t index_(const GridCell &c) const noexcept;
101
102 uint_t n_cols_;
103 uint_t n_rows_;
104 real_t cell_size_;
105 real_t x0_;
106 real_t y0_;
107 std::vector<bool> occupied_;
108};
109
110} // namespace planning
111} // namespace bitsim
112
114// So that a GridCell can be the key of an std::unordered_map, as in a_star
115template <> struct std::hash<bitsim::planning::GridCell>
116{
117 std::size_t operator()(const bitsim::planning::GridCell &c) const noexcept
118 {
119 return std::hash<long long>()((static_cast<long long>(c.col) << 32) ^
120 static_cast<long long>(c.row & 0xffffffff));
121 }
122};
124
125#endif // BITSIM_PLANNING_OCCUPANCY_GRID_H
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
std::vector< GridCell > neighbours(const GridCell &c, bool diagonal=true) const
The free cells next to c: the four that share a side, and with diagonal the four that share a corner ...
Definition occupancy_grid.cpp:85
bool in_bounds(const GridCell &c) const noexcept
Definition occupancy_grid.cpp:22
uint_t n_occupied() const
The number of occupied cells.
Definition occupancy_grid.cpp:116
void add_circle(real_t x, real_t y, real_t radius)
Occupy the cells whose centres are within radius of (x, y).
Definition occupancy_grid.cpp:58
real_t cell_size() const noexcept
Definition occupancy_grid.h:56
OccupancyGrid(uint_t n_cols, uint_t n_rows, real_t cell_size, real_t x0=0.0, real_t y0=0.0)
Definition occupancy_grid.cpp:12
std::array< real_t, 2 > to_world(const GridCell &c) const noexcept
The centre of a cell [m].
Definition occupancy_grid.cpp:80
bool is_free(const GridCell &c) const noexcept
True if the cell is in the grid and not occupied.
Definition occupancy_grid.cpp:28
void add_rectangle(real_t x_min, real_t y_min, real_t x_max, real_t y_max)
Occupy the cells whose centres are in the rectangle [x_min, x_max] x [y_min, y_max].
Definition occupancy_grid.cpp:42
GridCell to_cell(real_t x, real_t y) const noexcept
The cell that the point (x, y) is in. It may be out of bounds.
Definition occupancy_grid.cpp:74
uint_t n_cols() const noexcept
Definition occupancy_grid.h:54
uint_t n_rows() const noexcept
Definition occupancy_grid.h:55
void set_occupied(const GridCell &c, bool occupied=true)
Definition occupancy_grid.cpp:33
Definition a_star_search.cpp:8
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
int int_t
integer type
Definition bitsim_types.h:30
A cell of an OccupancyGrid: its column, along x, and row, along y.
Definition occupancy_grid.h:21
bool operator==(const GridCell &other) const noexcept
Definition occupancy_grid.h:25
int_t row
Definition occupancy_grid.h:23
bool operator!=(const GridCell &other) const noexcept
Definition occupancy_grid.h:29
int_t col
Definition occupancy_grid.h:22