An open API service indexing awesome lists of open source software.

https://github.com/r-barnes/grid_engine

Provides accessors and iterators for hex, D4, and D8 grids with toroid options
https://github.com/r-barnes/grid_engine

Last synced: 2 months ago
JSON representation

Provides accessors and iterators for hex, D4, and D8 grids with toroid options

Awesome Lists containing this project

README

        

grid_engine
===========
**grid_engine** is a class for flexibily working with different kinds of
two-dimensional grids.

It can handle hexagonal, 4-connected, and 8-connected grids.

Any of these connectivities can be used in a toroidal fashion, such that the
edges of the grid wrap around.

The same grid may be treated as toroidal, non-toroidal, 4-, 8-, or
hex-connected without needing to perform any modifications to the data
structure. Toroidness and connectivity is not treated as a fundamental aspect
of a grid, but, rather, as an artefact of the way a grid is traversed.

Summing into a second grid the neighbours at distances 2-4 of a central cell on
a randomly-generated hexagonal first grid wrapped in a toroid is as sample as:

#include "grid_engine.h"
#include

int main(){
typedef grid_engine::grid_engine gtype;

gtype grid1(40,40), grid2(40,40);

for(gtype::parser i=grid1.begin();i.good();++i)
*i=rand();

grid2.fill(0);
for(gtype::parser i=grid2.begin();i.good();++i)
for(gtype::nparser n=i.hextring(2,4);n.good();++n)
*i+=grid1(n);
}

Getting Started
===============

First Steps
-----------
1. Download and unpack the code.

2. By default the code provides neighbours at distances from 0-100 from a
central cell. If this makes you happy, skip to Step

3. If you are not happy with this default, you will need to rebuild the code:

make nsize=100

Change 100 to suit your needs. Remember, larger neighbourhood sizes will
increase the size of your executable.

4. The code will run its test suite. If it passes, continue. If not, panic.

5. Move the files **grid_engine.h** and **grid_engine_neighbours.h** into your
project directory. The second file is included in the first and should not
be included directly by your project.

6. Use `#include "grid_engine.h"` to import grid_engine into your project.

Creating a Grid
---------------
It is best to use a line analogous to

typedef grid_engine::grid_engine gtype;

in order to save yourself typing and decrease visual noise in your code.

To define a new grid 40 cells wide and 50 cells high use:

gtype grid(40,50);

To define a new grid with all cells set to 3 use:

gtype grid(40,50,3);

Parsing a Grid
--------------
To do something to every cell in a grid, we create a **parser**. Parsers can be
used in **for** loops, like so:

//Give every cell a random number
for(gtype::parser i=grid.begin();i.good();++i){
*i=rand();
cout<<"Cell "< gtype;

//Define two 40x40 grids of integers
gtype grid(40,40), grid2(40,40);

//Set each cell in the grid to a random number
for(gtype::parser i=grid.begin();i.good();++i)
*i=rand();

//Set each cell in the second grid to the sum of the D8 neighbours
//at distance 1 from its corresponding cell in the first grid
grid2.fill(0);
for(gtype::parser i=grid2.begin();i.good();++i)
for(gtype::nparser n=i.d8ring(1);n.good();++n)
*i+=grid(n);

//Set each cell in the second grid to the sum of the D8 neighbours
//at distance 2 from its corresponding cell in the first grid
grid2.fill(0);
for(gtype::parser i=grid2.begin();i.good();++i)
for(gtype::nparser n=i.d8ring(2);n.good();++n)
*i+=grid(n);

//Set each cell in the second grid to the sum of the D8 neighbours
//at distances 2-3 (inclusive) from its corresponding cell in the first
//grid
grid2.fill(0);
for(gtype::parser i=grid2.begin();i.good();++i)
for(gtype::nparser n=i.d8ring(2,3);n.good();++n)
*i+=grid(n);

//Set each cell in the second grid to the sum of the D8 neighbours
//at distances 1 from its corresponding cell in the first
//grid, assuming that the grid is a toroid
grid2.fill(0);
for(gtype::parser i=grid2.begin();i.good();++i)
for(gtype::nparser n=i.d8tring(1);n.good();++n)
*i+=grid(n);

//Set each cell in the second grid to the sum of the D4 neighbours
//at distance 1 from its corresponding cell in the first grid
grid2.fill(0);
for(gtype::parser i=grid2.begin();i.good();++i)
for(gtype::nparser n=i.d4ring(1);n.good();++n)
*i+=grid(n);

//Set each cell in the second grid to the sum of the hexagonal neighbours
//at distance 1 from its corresponding cell in the first grid
grid2.fill(0);
for(gtype::parser i=grid2.begin();i.good();++i)
for(gtype::nparser n=i.hexring(1);n.good();++n)
*i+=grid(n);

Notes
=====
The hexagonal data is stored in a 2D rectangular grid which is internally
mapped to a hexagonal space. This mapping shifts columns with odd
x-coordinates down a half-cell with respect to columns with even x-coordinates.
The user should not normally need to worry about this. A depiction of the grid
system is as follows, along with its corresponding rectangular representation.

![Hexagonal grid coordinate system](readme_imgs/hex_array.png)
![Hexagonal grid coordinate system, rectangularized](readme_imgs/hex_array_rect.png)

The file **generators/plotter.py** contains code for plotting the generated
neighbourhoods in order to verify their correctness.

Author
======
grid_engine was composed and is maintained by Richard Barnes ([email protected]).