honeycomb
Description
A compact grid toolkit built on a common Grid abstract class. It provides
hexagonal, rectangular, irregular and spatial grids with cell generation,
index filtering, distance and neighbourhood queries.
Software Architecture
Grid first splits into two dimension layers, PlaneGrid and
SpatialGrid; concrete grids then implement to_cells, filter_index,
get_distance and neighbours.
Grid (ABC)
+-- PlaneGrid (ABC, DIMENSION=2)
| +-- HexagonGrid
| +-- RectangularGrid
| +-- IrregularGrid (ABC)
| +-- Irregular4Grid
| +-- Irregular6Grid
+-- SpatialGrid (ABC, DIMENSION=3)
+-- Spatial6Grid
Both layers only classify index dimensions (DIMENSION is the length of
an index tuple). They implement no interface and change no constructor or
grid behaviour, so they remain abstract and cannot be instantiated.
core(single entry point, e.g.from close_paving.core import HexagonGrid)base/grid.py:Grid/PlaneGrid/SpatialGridcell.py:Cellbase class
geometry/cells.py:Hexagon/HorizontalHexagon/VerticalHexagon/Rectangle/Cuboidlayout.py:Direction,get_direction_tupleand three sharedLayoutclasses
grids/hexagonal_grid.py/rectangular_grid.py: regular gridsirregular_grid.py/irregular4_grid.py/irregular6_grid.py: irregular gridsspatial6_grid.py: spatial grid
error: custom exceptionskernel: convolution kernel generationplot_helper: plotting helperstest: unittest cases
Installation
This project uses pyproject.toml (PEP 621) for packaging; setup.py is
no longer provided.
- Pypi
pip install close-paving
- Source code
- clone repo
git clone https://gitcode.net/astar/a.star/close-paving.git
or
git clone https://gitee.com/hoops/close-paving.git
- cd close_paving
cd close_paving
- download requirements.txt
pip install -r requirements.txt
- run commandline
pip install .
Editable install:
pip install -e .
The plot_helper module needs matplotlib, which is provided as an
optional extra. Install it when plotting is required:
pip install ".[plot]"
how to use
Hexagon grid layout directions:
Grid types
| Class | Neighbours | Index | Storage |
|---|---|---|---|
HexagonGrid |
6 | (row, col) |
numpy coordinate calculation |
RectangularGrid |
4 / 8 | (row, col) |
2-D numpy array |
Irregular4Grid |
4 | (row, col), per-row column limit |
ragged nested list |
Irregular6Grid |
6 | (row, col), per-row column limit |
ragged nested list |
Spatial6Grid |
6 | (layer, row, col) |
3-D numpy array, one 2-D array per layer |
Each grid family shares one Layout: HexagonGrid and Irregular6Grid
share HexagonalLayout (all 8 direction combinations, radius,
Hexagon cells), RectangularGrid and Irregular4Grid share
RectangularLayout, and Spatial6Grid uses SpatialLayout. The only
difference of an irregular grid is that row/column counts come from
row_lengths.
Regular grids (rectangular and spatial) use numpy arrays for coordinates,
values and cell matrices. Irregular grids use nested lists because each row
may contain a different number of cells; their distance is the shortest path
on the adjacency graph (BFS), which accounts for detours around missing
cells and returns inf when unreachable.
Rectangular grid adjacency and distance
A square (or rectangular) grid has two common adjacency modes, selected by
adjacency:
adjacency=4(default): only up, down, left and right are neighbours; distance is the Manhattan step countabs(dr) + abs(dc);adjacency=8: the 4 corners are neighbours too, and a diagonal move also counts as one step; distance ismax(abs(dr), abs(dc)).
Both modes match HexagonGrid: distance is the shortest number of steps
along adjacency edges, always an integer. No diagonal weighting is used,
because a diagonal step of a non-square cell is not sqrt(2) long and an
irrational weight would introduce floating point errors.
distance_func still accepts a custom
(src_index, dst_index) -> number function for other scenarios. It drives
both get_distance and the threshold filtering in neighbours, overriding
the integer step count; neighbours then enumerates the whole grid and the
meaning of the return value is defined by that function.
Default cells
Every grid now produces a concrete cell from to_cells() instead of the
empty Cell shell:
| Grid | Default cell | Stored data |
|---|---|---|
HexagonGrid, Irregular6Grid |
Hexagon |
center / radius / direction / points |
RectangularGrid, Irregular4Grid |
Rectangle |
center / width / height / points |
Spatial6Grid |
Cuboid |
center / width / height / layer_spacing / vertices |
Each cell also keeps values from value_list and from the **kwargs given
to to_cells. Rectangle is a square cell when width == height; its
points is a closed 4-corner outline of shape (5, 2) ready for plotting,
while Cuboid.vertices returns 8 three-dimensional corners. A custom cell
class can still be passed, e.g. to_cells(MyCell).
Positional arguments of to_cells are appended after the geometry
arguments when constructing each cell. Without positional arguments the
geometry is passed by keyword (e.g. center=); with them it is passed by
position as (center, width, height[, layer_spacing]), so a custom cell
class can accept extra positional arguments. Geometry always wins over a
value with the same name.
Quick example
import math
from close_paving.core import RectangularGrid
from close_paving.core import Irregular4Grid
grid = RectangularGrid(shape=(4, 5), width=2, height=2)
grid.get_distance((0, 0), (2, 3)) # 5, default 4-neighbour steps
grid.neighbours((1, 1), 1) # 4 direct neighbours
cells = grid.to_cells() # 4 x 5 numpy array of Rectangle
cells[0, 0].center # cell centre
cells[0, 0].points # closed corners, shape (5, 2)
eight = RectangularGrid(shape=(4, 5), adjacency=8)
eight.get_distance((0, 0), (2, 2)) # 2 steps, diagonal counts as one
eight.neighbours((1, 1), 1) # 8 direct neighbours
def euclidean(src, dst):
return math.hypot(src[0] - dst[0], src[1] - dst[1])
custom = RectangularGrid(shape=(4, 5), distance_func=euclidean)
custom.get_distance((0, 0), (3, 4)) # 5.0, custom function is unrestricted
irregular = Irregular4Grid([3, 5, 4]) # rows of 3, 5 and 4 cells
irregular.get_distance((0, 0), (2, 3))
irregular.neighbours((1, 1), 2)
Run tests
python -m unittest discover -s close_paving/test -p "test_grid_*.py" -v
test_grid_base.py and test_grid_types.py contain 86 unittest cases
covering the abstract contract, the dimension inheritance layers, the
shared layouts and the adjacency, distance and value handling of all four
grids. The plotting cases (test_Hexagon*.py) additionally require
matplotlib.
contribute
- Fork repo
- create feature
Feat_xxx - commit
- create
Pull Request
Metadata
Release files for close-paving 0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| close_paving-0.2.tar.gz | 27.9 kB | Without provenance | Details for close_paving-0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| close_paving-0.2-py3-none-any.whl | Python 3 | none | any | Details for close_paving-0.2-py3-none-any.whl |
Total release size: 58.1 kB
Release files / close_paving-0.2.tar.gz
| Download URL | close_paving-0.2.tar.gz |
|---|---|
| Size | 27.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7dd2ba6d04f7b6089b629a4a8e6132246b72c31d228eb9c8b81c78e97be086b1
|
|
BLAKE2b-256 checksum How to use checksums |
f6f65838f56169f745f1f4769c0c1615b00077469dc20805ff5893aecc7f4051
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Release files / close_paving-0.2-py3-none-any.whl
| Download URL | close_paving-0.2-py3-none-any.whl |
|---|---|
| Size | 30.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b04eaa54cf7dea36522ba2375a9c1054fe2623ac8c3400bcdcfa6bbcfdcd3c63
|
|
BLAKE2b-256 checksum How to use checksums |
0b5d29f7458e23f782d43870576bbb164d5b885ac5d08aba1da369dc53ad0c06
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Security for close-paving 0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.