Metadata-Version: 2.4
Name: close-paving
Version: 0.2
Summary: Toolkit for hexagonal, rectangular, irregular and spatial grids
Author-email: "A.Star" <astar@snowland.ltd>
Maintainer-email: "A.Star" <astar@snowland.ltd>
License: Apache v2.0
Project-URL: Homepage, https://gitee.com/hoops/close-paving
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: Implementation
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Libraries
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: astartool>=0.1
Requires-Dist: numpy
Provides-Extra: plot
Requires-Dist: matplotlib; extra == "plot"
Dynamic: license-file

# 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`.

```text
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` / `SpatialGrid`
    - `cell.py`: `Cell` base class
  - `geometry/`
    - `cells.py`: `Hexagon` / `HorizontalHexagon` / `VerticalHexagon` / `Rectangle` / `Cuboid`
    - `layout.py`: `Direction`, `get_direction_tuple` and three shared `Layout` classes
  - `grids/`
    - `hexagonal_grid.py` / `rectangular_grid.py`: regular grids
    - `irregular_grid.py` / `irregular4_grid.py` / `irregular6_grid.py`: irregular grids
    - `spatial6_grid.py`: spatial grid
- `error`: custom exceptions
- `kernel`: convolution kernel generation
- `plot_helper`: plotting helpers
- `test`: unittest cases


#### Installation

This project uses `pyproject.toml` (PEP 621) for packaging; `setup.py` is
no longer provided.

1.  Pypi
```commandline
pip install close-paving
```

2.  Source code

- clone repo
```commandline
git clone https://gitcode.net/astar/a.star/close-paving.git
```

or 

```commandline
git clone https://gitee.com/hoops/close-paving.git
```

- cd close_paving
```commandline
cd close_paving
```
- download requirements.txt
```commandline
pip install -r requirements.txt
```
- run commandline
```commandline
pip install .
```

Editable install:
```commandline
pip install -e .
```

The `plot_helper` module needs `matplotlib`, which is provided as an
optional extra. Install it when plotting is required:
```commandline
pip install ".[plot]"
```



#### how to use

Hexagon grid layout directions:

![direction type](./docs/images/direction.png)

##### 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 count `abs(dr) + abs(dc)`;
- `adjacency=8`: the 4 corners are neighbours too, and a diagonal move
  also counts as one step; distance is `max(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

```python
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

```commandline
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

1.  Fork repo
2.  create feature `Feat_xxx`
3.  commit 
4.  create `Pull Request`

