37 #ifndef OMPL_DATASTRUCTURES_GRID_
38 #define OMPL_DATASTRUCTURES_GRID_
44 #include <unordered_map>
50 template <
typename _T>
55 using Coord = Eigen::VectorXi;
68 virtual ~Cell() =
default;
75 explicit Grid(
unsigned int dimension)
111 return getCell(coord) !=
nullptr;
117 auto pos =
hash_.find(
const_cast<Coord *
>(&coord));
118 Cell *c = (pos !=
hash_.end()) ? pos->second :
nullptr;
145 auto pos =
hash_.find(&coord);
146 Cell *cell = (pos !=
hash_.end()) ? pos->second :
nullptr;
149 list.push_back(cell);
152 pos =
hash_.find(&coord);
153 cell = (pos !=
hash_.end()) ? pos->second :
nullptr;
156 list.push_back(cell);
162 std::vector<std::vector<Cell *>>
components()
const
164 using ComponentHash = std::unordered_map<Coord *, int, HashFunCoordPtr, EqualCoordPtr>;
168 std::vector<std::vector<Cell *>> res;
173 auto pos = ch.find(&c0->coord);
174 int comp = (pos != ch.end()) ? pos->second : -1;
178 res.resize(res.size() + 1);
179 std::vector<Cell *> &q = res.back();
181 std::size_t index = 0;
182 while (index < q.size())
184 Cell *c = q[index++];
185 pos = ch.find(&c->coord);
186 comp = (pos != ch.end()) ? pos->second : -1;
190 ch.insert(std::make_pair(&c->coord,
components));
191 std::vector<Cell *> nbh;
193 for (
const auto &n : nbh)
195 pos = ch.find(&n->coord);
196 comp = (pos != ch.end()) ? pos->second : -1;
204 q.erase(q.begin() + index);
210 std::sort(res.begin(), res.end(), SortComponents());
220 auto *cell =
new Cell();
229 virtual bool remove(Cell *cell)
233 auto pos =
hash_.find(&cell->coord);
234 if (pos !=
hash_.end())
244 virtual void add(Cell *cell)
246 hash_.insert(std::make_pair(&cell->coord, cell));
256 void getContent(std::vector<_T> &content)
const
258 for (
const auto &h :
hash_)
259 content.push_back(h.second->data);
265 for (
const auto &h :
hash_)
266 coords.push_back(h.first);
272 for (
const auto &h :
hash_)
273 cells.push_back(h.second);
281 out << coord[i] <<
" ";
282 out <<
"]" << std::endl;
288 return hash_.empty();
292 unsigned int size()
const
298 virtual void status(std::ostream &out = std::cout)
const
300 out <<
size() <<
" total cells " << std::endl;
301 const std::vector<std::vector<Cell *>> &comp =
components();
302 out << comp.size() <<
" connected components: ";
303 for (
const auto &c : comp)
304 out << c.size() <<
" ";
316 for (
auto &c : content)
322 struct HashFunCoordPtr
328 for (
int i = s->size() - 1; i >= 0; --i)
330 int high = h & 0xf8000000;
332 h = h ^ (high >> 27);
335 return (std::size_t)h;
350 using CoordHash = std::unordered_map<Coord *, Cell *, HashFunCoordPtr, EqualCoordPtr>;
353 struct SortComponents
356 bool operator()(
const std::vector<Cell *> &a,
const std::vector<Cell *> &b)
const
358 return a.size() > b.size();
364 using iterator =
typename CoordHash::const_iterator;
369 return hash_.begin();
bool empty() const
Check if the grid is empty.
iterator begin() const
Return the begin() iterator for the grid.
virtual ~Grid()
Destructor.
virtual Cell * createCell(const Coord &coord, CellArray *nbh=nullptr)
std::vector< std::vector< Cell * > > components() const
Get the connected components formed by the cells in this grid (based on neighboring relation)
virtual bool remove(Cell *cell)
void getCoordinates(std::vector< Coord * > &coords) const
Get the set of coordinates where there are cells.
typename CoordHash::const_iterator iterator
We only allow const iterators.
unsigned int getDimension() const
Return the dimension of the grid.
Grid(unsigned int dimension)
The constructor takes the dimension of the grid as argument.
void getContent(std::vector< _T > &content) const
Get the data stored in the cells we are aware of.
void neighbors(const Cell *cell, CellArray &list) const
Get the list of neighbors for a given cell.
Cell * getCell(const Coord &coord) const
Get the cell at a specified coordinate.
bool operator()(const std::vector< Cell * > &a, const std::vector< Cell * > &b) const
Helper to sort components by size.
std::vector< Cell * > CellArray
The datatype for arrays of cells.
unsigned int dimension_
The dimension of the grid.
std::size_t operator()(const Coord *const s) const
Hash function for coordinates.
void freeMemory()
Free the allocated memory.
Eigen::VectorXi Coord
Definition of a coordinate within this grid.
virtual void destroyCell(Cell *cell) const
Clear the memory occupied by a cell; do not call this function unless remove() was called first.
Definition of a cell in this grid.
void printCoord(Coord &coord, std::ostream &out=std::cout) const
Print the value of a coordinate to a stream.
virtual void status(std::ostream &out=std::cout) const
Print information about the data in this grid structure.
Coord coord
The coordinate of the cell.
iterator end() const
Return the end() iterator for the grid.
unsigned int size() const
Check the size of the grid.
virtual void add(Cell *cell)
Add an instantiated cell to the grid.
bool has(const Coord &coord) const
Check if a cell exists at the specified coordinate.
CoordHash hash_
The hash holding the cells.
std::unordered_map< Coord *, Cell *, HashFunCoordPtr, EqualCoordPtr > CoordHash
Define the datatype for the used hash structure.
bool operator()(const Coord *const c1, const Coord *const c2) const
Equality operator for coordinate pointers.
_T data
The data we store in the cell.
unsigned int maxNeighbors_
The maximum number of neighbors a cell can have (2 * dimension)
void getCells(CellArray &cells) const
Get the set of instantiated cells in the grid.
void setDimension(unsigned int dimension)
virtual void clear()
Clear all cells in the grid.
Main namespace. Contains everything in this library.