Iterators

These functions allow iterating efficiently over a given shape in a 2D array. On every cell lying in the shape it calls a given functor. The functions iterate over all cells whose center lies in the shape and calls the given functor with the cell indexes as arguments. Three different shapes are available, namely rectangle, polygon and circle.

Examples

Examples, can be seen in the figures:

The image displays a polygon on a grid and marks all cells lying in the polygon.

The figure shows a blue polygon which can be passed to the function iteratePolygon(). The blue points mark the centers of all cells of the 2D map. The red circles mark the points over which the iterator iterated. A comparison with the green groundtruth markers shows that all selected points are correct, because they belong to cells whose center lies in the polygon.

The image displays a different polygon on a grid. In comparison to before, limits additionally constraint the marked cells.

In addition to the polygon, the function iteratePolygon can be provided minimum and maximum x- and y-axis values. Then the function iterates only over cells whose center lies in the given shape and rectangle defined by the extreme values. In the figure, you can see that the function selects only the cells that lie in the polygon and the rectangle.

API

The iterator functions are overloaded. In total there are three versions each, differing in how the minimum and maximum values are specified:

  • no minimum and maximum specified

  • minimum set to zero and maximum specified

  • minimum and maximum specified

Iterate Circle

template<typename T, typename Functor>
void hector_math::iterateCircle(const Vector2<T> &center, double radius, Eigen::Index row_min, Eigen::Index row_max, Eigen::Index col_min, Eigen::Index col_max, Functor functor)

Iterates over all indexes that lie in the given circle and for each index (x, y) calls the given functor. This method will iterate all cells where the center of the cell (x+0.5, y+0.5) is inside the circle. Note: The circle has to be in the index space, hence, if it is in map coordinates it might be necessary to divide it by the map resolution.

The indexes can be limited using the ranges [row_min, row_max) and [col_min, col_max) where row/col_min is included but row/col_max is excluded, i.e., the largest x index functor may be called with will be row_max - 1.

Template Parameters:

Functor – A function or lambda method with the signature: void(Eigen::Index x, Eigen::Index y).

Parameters:
  • center – The center of the circle that is iterated over.

  • radius – The radius of the circle that is iterated over.

  • functor – The function that will be called for each index (x, y) inside the circle.

template<typename T, typename Functor>
void hector_math::iterateCircle(const Vector2<T> &center, double radius, Eigen::Index rows, Eigen::Index cols, Functor functor)

Overload of iterateCircle where row_min and col_min are set to 0 to allow for bounded iteration of 2D matrices and arrays.

template<typename T, typename Functor>
void hector_math::iterateCircle(const Vector2<T> &center, double radius, Functor functor)

Overload of iterateCircle where the indexes are not bounded.

Iterate Rectangle

template<typename T, typename Functor>
void hector_math::iterateRectangle(const Vector2<T> &a, const Vector2<T> &b, const Vector2<T> &c, Eigen::Index row_min, Eigen::Index row_max, Eigen::Index col_min, Eigen::Index col_max, Functor functor)

Iterates over all indexes that lie in the rectangle formed by the three points a, b and c - where ab, and ac form adjacent edges of the rectangle - and for each index (x, y) calls the given functor. This method will iterate all cells where the center of the cell (x+0.5, y+0.5) is inside the rectangle. Note: The polygon has to be in the index space, hence, if it is in map coordinates it might be necessary to divide it by the map resolution.

The indexes can be limited using the ranges [row_min, row_max) and [col_min, col_max) where row/col_min is included but row/col_max is excluded, i.e., the largest x index functor may be called with will be row_max - 1.

Note: This method can actually iterate not only rectangles but also parallelograms. Theoretically, it would work for any convex quadrilateral (alt. quadrangle).

Template Parameters:

Functor

A function or lambda method with the signature: void(Eigen::Index x, Eigen::Index

y).

Parameters:
  • polygon – The polygon that is iterated over.

  • functor – The function that will be called for each index (x, y) inside the polygon.

template<typename T, typename Functor>
void hector_math::iterateRectangle(const Vector2<T> &a, const Vector2<T> &b, const Vector2<T> &c, Eigen::Index rows, Eigen::Index cols, Functor functor)

Overload of iterateRectangle where row_min and col_min are set to 0 to allow for bounded iteration of 2D matrices and arrays.

template<typename T, typename Functor>
void hector_math::iterateRectangle(const Vector2<T> &a, const Vector2<T> &b, const Vector2<T> &c, Functor functor)

Overload of iterateRectangle where the indexes are not bounded.

Iterate Polygon

template<typename T, typename Functor>
void hector_math::iteratePolygon(const Polygon<T> &polygon, Eigen::Index row_min, Eigen::Index row_max, Eigen::Index col_min, Eigen::Index col_max, Functor functor)

Iterates over all indexes that lie in the given polygon and for each index (x, y) calls the given functor. This method will iterate all cells where the center of the cell (x+0.5, y+0.5) is inside the polygon. Note: The polygon has to be in the index space, hence, if it is in map coordinates it might be necessary to divide it by the map resolution.

The indexes can be limited using the ranges [row_min, row_max) and [col_min, col_max) where row/col_min is included but row/col_max is excluded, i.e., the largest x index functor may be called with will be row_max - 1.

Template Parameters:

Functor – A function or lambda method with the signature: void(Eigen::Index x, Eigen::Index y).

Parameters:
  • polygon – The polygon that is iterated over.

  • functor – The function that will be called for each index (x, y) inside the polygon.

template<typename T, typename Functor>
void hector_math::iteratePolygon(const Polygon<T> &polygon, Eigen::Index rows, Eigen::Index cols, Functor functor)

Overload of iteratePolygon where row_min and col_min are set to 0 to allow for bounded iteration of 2D matrices and arrays.

template<typename T, typename Functor>
void hector_math::iteratePolygon(const Polygon<T> &polygon, Functor functor)

Overload of iteratePolygon where the indexes are not bounded.