28 #ifndef NDGRIDMAP_HPP_
29 #define NDGRIDMAP_HPP_
41 #include <fast_methods/console/console.h>
49 friend std::ostream&
operator <<
52 os <<
"\t" << g.getCell(0).type() << std::endl;
53 os <<
"\t" << g.ncells_ <<
" cells." << std::endl;
54 os <<
"\t" << g.leafsize_ <<
" leafsize (m)." << std::endl;
55 os <<
"\t" << ndims <<
" dimensions:" << std::endl;
57 for (
unsigned int i = 0; i < ndims; ++i)
58 os <<
"\t\t" <<
"d" << i <<
"\tsize: " << g.dimsize_[i] << std::endl;
70 (
const std::array<unsigned int, ndims> & dimsize,
double leafsize = 1.0f) :
78 (
const std::array<unsigned int, ndims> & dimsize) {
82 for (
unsigned int i = 0; i < ndims; ++i) {
92 for (
unsigned int i = 0; i <
cells_.size(); ++i)
106 inline void setLeafSize(
const double leafsize) {
leafsize_ = leafsize; }
119 (
unsigned int idx,
unsigned int dim) {
131 (
int idx, std::array<unsigned int, ndims> &m,
unsigned int dim) {
142 (
unsigned int idx, std::array<unsigned int, 2*ndims> & neighs) {
144 for (
unsigned int i = 0; i < ndims; ++i)
155 (
unsigned int idx, std::array<unsigned int, 2*ndims>& neighs,
unsigned int dim) {
166 if ((c1 >= 0) && (c1/
d_[dim] == idx/
d_[dim]))
169 if (c2/
d_[dim] == idx/
d_[dim])
175 (
unsigned int idx, std::array<unsigned int, 2>& neighs,
unsigned int dim) {
187 if ((c1 >= 0) && (c1/
d_[dim] == idx/
d_[dim]))
190 if (c2/
d_[dim] == idx/
d_[dim])
196 (
unsigned int idx, std::array<unsigned int, ndims> & coords) {
197 if (coords.size() != ndims)
200 coords[ndims-1] = idx/
d_[ndims-2];
201 unsigned int aux = idx - coords[ndims-1]*
d_[ndims-2];
202 for (
unsigned int i = ndims - 2; i > 0; --i) {
203 coords[i] = aux/d_[i-1];
204 aux -= coords[i]*d_[i-1];
213 (
const std::array<unsigned int, ndims> & coords,
unsigned int & idx) {
214 if (coords.size() != ndims)
218 for(
unsigned int i = 1; i < ndims; ++i)
219 idx += coords[i]*
d_[i-1];
227 std::array<unsigned int, ndims> coords;
229 for (
unsigned int i = 0; i < ndims; ++i)
230 std::cout << coords[i] <<
"\t";
236 (std::array<unsigned int, ndims> coords) {
237 for (
unsigned int i = 0; i < ndims; ++i)
238 std::cout << coords[i] <<
"\t";
244 (
const std::array<unsigned int, ndims> & coords) {
247 std::cout << idx <<
'\n';
251 inline unsigned int size
260 for (
const T & c:
cells_) {
261 if (!isinf(c.getValue()) && c.getValue() > max)
299 std::stringstream ss;
307 (
const std::vector<unsigned int> & obs) {
313 (std::vector<unsigned int>&& obs) {
319 (std::vector<unsigned int> & obs)
const {
330 unsigned int nObs = 0;
331 for (
const T & c :
cells_) {
333 sum += c.getVelocity();
344 for (
const T & c :
cells_)
345 if (max < c.getVelocity())
346 max = c.getVelocity();
370 std::array<unsigned int, ndims>
d_;
374 std::array<unsigned int, 2>
n_;
void getOccupiedCells(std::vector< unsigned int > &obs) const
Returns the indices of the occupied cells of the grid.
std::array< unsigned int, ndims > dimsize_
Size of each dimension.
void resize(const std::array< unsigned int, ndims > &dimsize)
Resizes each dimension of the grid according dimsize.
static constexpr size_t getNDims()
Makes the number of dimensions of the grid available at compilation time.
static std::string str_info(const std::string &val)
double getMinValueInDim(unsigned int idx, unsigned int dim)
Returns the minimum value of neighbors of cell idx in dimension dim.
unsigned int getNumberNeighborsInDim(int idx, std::array< unsigned int, ndims > &m, unsigned int dim)
Returns number of valid neighbors for cell idx in dimension dim, stored in m.
void setOccupiedCells(const std::vector< unsigned int > &obs)
Sets the cells which are occupied. Usually called by grid loaders.
bool clean_
Flag to indicate if the grid is ready to use.
double getMaxValue() const
Returns the maximum value of the cells in the grid.
std::vector< T > cells_
Main container for the class.
Templated class which represents a n-dimensional grid map. Its cells are assumed to be cubic...
double getMaxSpeed()
Returns the maximum speed (occupancy value) in the grid.
void getNeighborsInDim(unsigned int idx, std::array< unsigned int, 2 *ndims > &neighs, unsigned int dim)
Computes the indices of the 4-connectivity neighbors of cell idx in a specified direction dim...
double getAvgSpeed()
Returns the avegare velocity ignoring those with 0 velocitie (obstacles).
void clean()
Cleans the grid if it is not clean already. Calls Cell::setDefault()
double leafsize_
Real size of the cells. It is assumed that the cells in the grid are cubic.
unsigned int n_neighs
Internal variable that counts the number of neighbors found in every iteration. Modified by getNeighb...
unsigned int idx2coord(unsigned int idx, std::array< unsigned int, ndims > &coords)
Transforms from index to coordinates.
unsigned int ncells_
Number of cells in the grid (size)
std::vector< unsigned int > occupied_
Caches the occupied cells (obstacles).
void clear()
Erases the content of the grid. Must be resized later.
T & getCell(unsigned int idx)
Returns the cell with index idx.
std::array< unsigned int, ndims > getDimSizes() const
Returns the size of each dimension.
unsigned int size() const
Returns number of cells in the grid.
std::array< unsigned int, 2 > n_
Auxiliar array to speed up neighbor and indexing generalization: for getMinValueInDim() function...
void showIdx(const std::array< unsigned int, ndims > &coords)
Shows the index from the coordinates.
unsigned int getNeighbors(unsigned int idx, std::array< unsigned int, 2 *ndims > &neighs)
Computes the indices of the 4-connectivity neighbors. As it is based on arrays (to improve performanc...
void setClean(bool c)
Sets the state of the grid. True means clean.
unsigned int coord2idx(const std::array< unsigned int, ndims > &coords, unsigned int &idx)
Transforms from coordinates to index.
std::string getDimSizesStr()
Returns "size(dim(0)) \t size(dim(1)) \t...".
double getLeafSize() const
Returns the leaf size of the grid.
void showCoords(unsigned int idx)
Shows the coordinates from an index.
std::array< unsigned int, ndims > d_
Auxiliar array to speed up neighbor and indexing generalization: stores parcial multiplications of di...
bool isClean() const
Returns if the grid is clean (ready to use)