Template Class CDynamicGrid

Class Documentation

template<class T>
class CDynamicGrid

A 2D grid of dynamic size which stores any kind of data at each cell.

Template Parameters:

T – The type of each cell in the 2D grid.

Direct and range-based access to data

inline const grid_data_t &data() const
inline iterator begin()
inline iterator end()
inline const_iterator begin() const
inline const_iterator end() const

Public Types

using grid_data_t = std::vector<T>
using iterator = typename grid_data_t::iterator
using const_iterator = typename grid_data_t::const_iterator

Public Functions

inline CDynamicGrid(double x_min = -10., double x_max = 10., double y_min = -10., double y_max = 10., double resolution = 0.1)

Constructor

virtual ~CDynamicGrid() = default

Destructor

inline void setSize(const double x_min, const double x_max, const double y_min, const double y_max, const double resolution, const T *fill_value = nullptr)

Changes the size of the grid, ERASING all previous contents. If fill_value is left as nullptr, the contents of cells may be undefined (some will remain with their old values, the new ones will have the default cell value, but the location of old values may change wrt their old places). If fill_value is not nullptr, it is assured that all cells will have a copy of that value after resizing.

See also

resize, fill

inline void clear()

Erase the contents of all the cells.

inline void fill(const T &value)

Fills all the cells with the same value

inline virtual void resize(double new_x_min, double new_x_max, double new_y_min, double new_y_max, const T &defaultValueNewCells, double additionalMarginMeters = 2.0)

Changes the size of the grid, maintaining previous contents.

See also

setSize

inline T *cellByPos(double x, double y)

Returns a pointer to the contents of a cell given by its coordinates, or nullptr if it is out of the map extensions.

inline const T *cellByPos(double x, double y) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline T *cellByIndex(unsigned int cx, unsigned int cy)

Returns a pointer to the contents of a cell given by its cell indexes, or nullptr if it is out of the map extensions.

inline const T *cellByIndex(unsigned int cx, unsigned int cy) const

Returns a pointer to the contents of a cell given by its cell indexes, or nullptr if it is out of the map extensions.

inline size_t getSizeX() const

Returns the horizontal size of grid map in cells count

inline size_t getSizeY() const

Returns the vertical size of grid map in cells count

inline double getXMin() const

Returns the “x” coordinate of left side of grid map

inline double getXMax() const

Returns the “x” coordinate of right side of grid map

inline double getYMin() const

Returns the “y” coordinate of top side of grid map

inline double getYMax() const

Returns the “y” coordinate of bottom side of grid map

inline double getResolution() const

Returns the resolution of the grid map

inline int x2idx(double x) const

Transform a coordinate values into cell indexes

inline int y2idx(double y) const
inline int xy2idx(double x, double y) const
inline void idx2cxcy(int idx, int &cx, int &cy) const

Transform a global (linear) cell index value into its corresponding (x,y) cell indexes.

inline double idx2x(int cx) const

Transform a cell index into a coordinate value of the cell central point

inline double idx2y(int cy) const
template<class MAT>
inline void getAsMatrix(MAT &m) const

Get the entire grid as a matrix.

Note

This method will compile only for cell types that can be converted to the type of the matrix elements (e.g. double).

Template Parameters:

MAT – The type of the matrix, typically a mrpt::math::CMatrixDouble.

Parameters:

m[out] The output matrix; will be set automatically to the correct size. Entry (cy,cx) in the matrix contains the grid cell with indices (cx,cy).

inline virtual float cell2float(const T&) const

The user must implement this in order to provide “saveToTextFile” a way to convert each cell into a numeric value

inline bool saveToTextFile(const std::string &fileName) const

Saves a float representation of the grid (via “cell2float()”) to a text file.

Returns:

false on error

Protected Functions

template<class STREAM>
inline void dyngridcommon_writeToStream(STREAM &out) const
template<class STREAM>
inline void dyngridcommon_readFromStream(STREAM &in, bool cast_from_float = false)
inline grid_data_t &m_map_castaway_const() const

Used only from logically const method that really need to modify the object

Protected Attributes

grid_data_t m_map

The cells

double m_x_min = {0}
double m_x_max = {0}
double m_y_min = {0}
double m_y_max = {0}
double m_resolution = {0}
size_t m_size_x = {0}
size_t m_size_y = {0}