projective-grid
Generic 2D projective grid graph construction, traversal, and homography tools for square and hexagonal grids.
This crate provides reusable algorithms for building grid graphs from detected 2D corners, assigning grid coordinates via BFS traversal, and computing projective mappings (homographies) for grid rectification. It supports both 4-connected square grids and 6-connected hexagonal grids (pointy-top, axial coordinates). It is pattern-agnostic and has no dependency on image types or calibration-specific logic.
Quickstart
Square grid
use ;
use Point2;
/// Trivial validator: accepts any neighbor within range, classifies by quadrant.
;
Hex grid
use ;
use ;
use Point2;
/// Classify neighbors into hex sextants by angle.
;
Modules
Square grid (4-connected)
| Module | Description |
|---|---|
graph |
GridGraph::build() with pluggable NeighborValidator trait |
traverse |
connected_components(), assign_grid_coordinates() |
grid_smoothness |
Neighbor-based position prediction (2 axis pairs) and outlier detection |
grid_alignment |
GridTransform, GridAlignment, dihedral group D4 (8 transforms) |
grid_rectify |
GridHomography -- single global homography from grid corners |
grid_mesh |
GridHomographyMesh -- per-cell homographies for distortion-robust rectification |
direction |
NeighborDirection (Right/Left/Up/Down), NodeNeighbor |
Hexagonal grid (6-connected)
| Module | Description |
|---|---|
hex::graph |
HexGridGraph::build() with pluggable HexNeighborValidator trait |
hex::traverse |
hex_connected_components(), hex_assign_grid_coordinates() |
hex::smoothness |
Neighbor-based position prediction (3 axis pairs) and outlier detection |
hex::alignment |
Dihedral group D6 (12 transforms) via GridTransform |
hex::rectify |
HexGridHomography -- global homography with axial-to-rectified mapping |
hex::mesh |
HexGridHomographyMesh -- per-triangle affine/homography mesh |
hex::direction |
HexDirection (E/W/NE/SW/NW/SE), HexNodeNeighbor |
Shared
| Module | Description |
|---|---|
homography |
Homography struct, DLT estimation, 4-point solver with Hartley normalization |
grid_index |
GridIndex { i, j } -- used as (col, row) for square and (q, r) for hex |
grid_alignment |
GridTransform, GridAlignment -- generic 2x2 integer matrix transforms |
Design
The validator traits (NeighborValidator for square, HexNeighborValidator for
hex) are the main extension points. Implementors decide which spatially close
points qualify as grid neighbors and assign them a direction and quality score.
This lets the same graph construction algorithm work for chessboards
(orientation-based validation), ChArUco grids (marker-anchored validation),
ring calibration targets on hex lattices, or any other 2D grid pattern.
Hex coordinate convention
Hex grids use pointy-top orientation with axial coordinates (q, r),
stored in GridIndex as i = q, j = r:
qincreases eastwardrincreases south-eastward- Six directions: E
(+1, 0), W(-1, 0), NE(+1, -1), SW(-1, +1), NW(0, -1), SE(0, +1) - Rectified mapping:
x = s * (q + r/2),y = s * (r * sqrt(3)/2)wheres= pixels per cell
Hex mesh triangulation
The hex mesh (HexGridHomographyMesh) decomposes the axial grid into
parallelogram cells, each split into two triangles. Each triangle stores both
an AffineTransform2D (exact 3-point mapping) and a Homography (4-point,
using the centroid), giving callers the choice between speed and projective
accuracy.
The crate is standalone: it depends only on nalgebra, kiddo, serde, and
thiserror. No image types, no calibration-specific logic.
Features
tracing: enables tracing instrumentation (currently reserved for future use).