Utopia 2
Framework for studying models of complex & adaptive systems.
|
A grid discretization using hexagonal cells. More...
#include <hexagonal.hh>
Public Types | |
using | Base = Grid< Space > |
Base class type. | |
using | SpaceVec = typename Space::SpaceVec |
The type of vectors that have a relation to physical space. | |
using | MultiIndex = MultiIndexType< dim > |
The type of multi-index like arrays, e.g. the grid shape. | |
using | Config = DataIO::Config |
The configuration type. | |
Public Types inherited from Utopia::Grid< Space > | |
using | Self = Grid< Space > |
Type of this class, i.e. the base grid class. | |
using | SpaceVec = typename Space::SpaceVec |
The type of vectors that have a relation to physical space. | |
using | MultiIndex = MultiIndexType< dim > |
The type of multi-index like arrays, e.g. the grid shape. | |
using | Config = DataIO::Config |
The configuration type. | |
Public Member Functions | |
HexagonalGrid (std::shared_ptr< Space > space, const Config &cfg) | |
Construct a hexagonal grid discretization. | |
IndexType | num_cells () const override |
Number of hexagonal cells required to fill the physical space. | |
SpaceVec | effective_resolution () const override |
The effective cell resolution into each physical space dimension. | |
MultiIndex | shape () const override |
Get shape of the hexagonal grid. | |
GridStructure | structure () const override |
Structure of the grid. | |
MultiIndex | midx_of (const IndexType id) const override |
Returns the multi-index of the cell with the given ID. | |
SpaceVec | barycenter_of (const IndexType id) const override |
Returns the barycenter of the cell with the given ID. | |
SpaceVec | extent_of (const IndexType) const override |
Returns the extent of the cell with the given ID. | |
std::vector< SpaceVec > | vertices_of (const IndexType id) const override |
Returns the vertices of the cell with the given ID. | |
IndexType | cell_at (const SpaceVec &pos) const override |
Return the ID of the cell covering the given point in physical space. | |
std::set< IndexType > | boundary_cells (std::string select="all") const override |
Retrieve a set of cell indices that are at a specified boundary. | |
Public Member Functions inherited from Utopia::Grid< Space > | |
Grid (std::shared_ptr< Space > space, const Config &cfg) | |
Construct a grid discretization. | |
virtual | ~Grid ()=default |
Virtual destructor to allow polymorphic destruction. | |
IndexContainer | neighbors_of (const IndexType id) const |
Returns the indices of the neighbors of the cell with the given ID. | |
void | select_neighborhood (NBMode nb_mode, const Config &nb_params={}) |
const NBMode & | nb_mode () const |
Const reference to the currently selected neighborhood mode. | |
const Config & | nb_params () const |
The neighborhood parameters of the currently selected neighborhood. | |
auto | nb_size () const |
Maximum size of the currently selected neighborhood. | |
auto | resolution () const |
Get scalar resolution value of this grid. | |
std::string | structure_name () const |
Structure of the grid as std::string. | |
const std::shared_ptr< Space > & | space () const |
Const reference to the space this grid maps to. | |
bool | is_periodic () const |
Whether the space this grid maps to is periodic. | |
Static Public Attributes | |
static constexpr DimType | dim = Space::dim |
The dimensionality of the space to be discretized (for easier access) | |
Static Public Attributes inherited from Utopia::Grid< Space > | |
static constexpr DimType | dim = Space::dim |
The dimensionality of the space to be discretized (for easier access) | |
Protected Member Functions | |
NBFuncID< Base > | get_nb_func (NBMode nb_mode, const Config &nb_params) override |
Retrieve the neighborhood function depending on the mode. | |
DistType | expected_num_neighbors (const NBMode &nb_mode, const Config &) const override |
Computes the expected number of neighbors for a neighborhood mode. | |
NBFuncID< Base > | get_nb_func_hexagonal (const Config &nb_params) |
Returns a standalone hexagonal neighborhood function. | |
template<DimType axis, bool periodic> | |
void | add_neighbors_in_ (const IndexType root_id, IndexContainer &neighbor_ids) const |
Add both direct neighbors to a container of indices. | |
template<bool check_shape = false> | |
DistType | get_nb_param_distance (const Config ¶ms) const |
Extract the distance neighborhood parameter from the given config. | |
Protected Attributes | |
NBFuncID< Base > | _nb_hexagonal_periodic |
The Von-Neumann neighborhood for periodic grids. | |
NBFuncID< Base > | _nb_hexagonal_nonperiodic |
The Von-Neumann neighborhood for non-periodic grids. | |
Protected Attributes inherited from Utopia::Grid< Space > | |
const std::shared_ptr< Space > | _space |
The space that is to be discretized. | |
const DistType | _resolution |
How many cells to place per length unit of space. | |
NBMode | _nb_mode |
Neighborhood mode. | |
Config | _nb_params |
Neighborhood parameters. | |
NBFuncID< Self > | _nb_func |
Neighborhood function (working on cell IDs) | |
NBFuncID< Self > | _nb_empty |
A neighborhood function for empty neighborhood. | |
Private Member Functions | |
MultiIndex | determine_shape () const |
Get shape of the hexagonal grid. | |
Private Attributes | |
const MultiIndex | _shape |
The (multi-index) shape of the grid, resulting from resolution. | |
const SpaceVec | _cell_extent |
The extent of each cell of this discretization (same for all) | |
A grid discretization using hexagonal cells.
This is a grid discretization using hexagonal cells
The hexagonal tiling is constructed in pointy-topped orientation (one vertex of the hexagon pointing up) with an even row offset (every even row is displaced half a cell width to the right).
The hexagonal cellular arrangement covers a periodic space perfectly. In this setup the right-most cell in offset rows is half cut, covering the remaining space at the left boundary. Similarly, the tip of the cells in the upper-most row covers the free space at the lower boundary. In non-periodic space these cells are cut and at the opposite boundary space remains under-represented.
0, 1, …, N_x, N_x + 1, …, 2*N_x, 2*N_x + 1, …, N_x * N_y - 1
For more information on hexagonal grids, see this tutorial.
This class has been implemented following the above tutorial written by Amit Patel and published on www.redblobgames.com (last accessed version from May 2020).
using Utopia::HexagonalGrid< Space >::Config = DataIO::Config |
The configuration type.
using Utopia::HexagonalGrid< Space >::MultiIndex = MultiIndexType<dim> |
The type of multi-index like arrays, e.g. the grid shape.
using Utopia::HexagonalGrid< Space >::SpaceVec = typename Space::SpaceVec |
The type of vectors that have a relation to physical space.
|
inline |
Construct a hexagonal grid discretization.
space | The space to construct the discretization for |
cfg | Further configuration parameters |
|
inlineprotected |
Add both direct neighbors to a container of indices.
This function takes an index container and populates it with the indices of neighboring cells in different dimensions, specified by template parameter 0 < axis
< number of dimensions - 1.
The algorithm first calculates whether the given root cell index has a front or back boundary in the chosen dimension. If so, the neighboring cell is only added if the grid is periodic.
root_id | Which cell to find the agents of |
neighbor_ids | The container to populate with the indices |
axis | The axis along which to add the neighbors (0-based!) |
periodic | Whether the grid is periodic |
|
inlineoverridevirtual |
Returns the barycenter of the cell with the given ID.
Implements Utopia::Grid< Space >.
|
inlineoverridevirtual |
Retrieve a set of cell indices that are at a specified boundary.
select | Which boundary to return the cell IDs of. If 'all', all boundary cells are returned. Other available values depend on the dimensionality of the grid: 2D: left, right, bottom, top |
Implements Utopia::Grid< Space >.
|
inlineoverridevirtual |
Return the ID of the cell covering the given point in physical space.
Cells are interpreted as covering half-open intervals in space, i.e., including their low-value edges and excluding their high-value edges. The special case of points on high-value edges for non-periodic space behaves such that these points are associated with the cells at the boundary.
For non-periodic space only: The offset geometry of the hexagonal lattice does not permit to cover a rectangular non-periodic space perfectly. At the boundary, a position in uncovered space is mapped to the closest cell. Details: The hexagonal cellular arrangement is covering the periodic equivalent perfectly. In this setup the right-most cell in offset rows is half cut, covering the space at the left boundary. Similarly, the tip of the cells in the upper-most row covers the free space at the lower boundary. In non-periodic space these cells are cut and at the opposite boundary space remains under-represented. Positions in this space are mapped to the closest cell.
Implements Utopia::Grid< Space >.
|
inlineprivate |
Get shape of the hexagonal grid.
Integer rounding takes place here. A physical space of extents of 2.1 length units in each dimension (resp. the shape of the hexagon) and a resolution of two cells per unit length will result in 4 cells in each dimension, each cell's size scaled up slightly and the effective resolution thus slightly smaller than the specified resolution.
|
inlineoverridevirtual |
The effective cell resolution into each physical space dimension.
Implements Utopia::Grid< Space >.
|
inlineoverrideprotectedvirtual |
Computes the expected number of neighbors for a neighborhood mode.
Implements Utopia::Grid< Space >.
|
inlineoverridevirtual |
Returns the extent of the cell with the given ID.
The cell's extent corresponds to the tip-to-tip distance of the cell
Implements Utopia::Grid< Space >.
|
inlineoverrideprotectedvirtual |
Retrieve the neighborhood function depending on the mode.
Implements Utopia::Grid< Space >.
|
inlineprotected |
Returns a standalone hexagonal neighborhood function.
It extracts the distance parameter from the configuration and depending on the distance parameter and the periodicity of the space decides between four different neighborhood calculation functions. The returned callable does rely on the SquareGrid object, but it includes all parameters from the configuration that can be computed once and then captured; this avoids recomputation.
nb_params | The configuration for the hexagonal neighborhood method. Expected keys: distance (optional, defaults to 1), which refers to the Manhattan distance of included neighbors. |
|
inlineprotected |
Extract the distance
neighborhood parameter from the given config.
1
.check_shape | Whether to check the shape of the grid is large enough for this distance. For all SquareGrid neighborhoods in periodic space, the grid needs to be at least 2 * distance + 1 cells wide in each dimension. |
params | The neighborhood parameters to extract the distance parameter from. |
|
inlineoverridevirtual |
Returns the multi-index of the cell with the given ID.
Implements Utopia::Grid< Space >.
|
inlineoverridevirtual |
Number of hexagonal cells required to fill the physical space.
This is calculated simply from the _shape member.
Implements Utopia::Grid< Space >.
|
inlineoverridevirtual |
|
inlineoverridevirtual |
|
inlineoverridevirtual |
Returns the vertices of the cell with the given ID.
The vertices (pointy-topped arrangement) are given in counter-clockwise order, starting with the position of the bottom left-hand vertex (8 o'clock) of the cell.
Implements Utopia::Grid< Space >.
The extent of each cell of this discretization (same for all)
The cell's extent corresponds to the tip-to-tip distance
|
protected |
The Von-Neumann neighborhood for non-periodic grids.
|
protected |
The Von-Neumann neighborhood for periodic grids.
|
private |
The (multi-index) shape of the grid, resulting from resolution.
|
staticconstexpr |
The dimensionality of the space to be discretized (for easier access)