16-Sector Coding & Codebook
Each ringgrid marker carries a unique identity encoded as a binary pattern in the annular code band between its inner and outer rings. The code band is divided into 16 equal angular sectors, each rendered as either black or white, forming a 16-bit codeword. The detector ships with a stable 893-codeword baseline profile and an opt-in extended profile for larger ID spaces, while still handling unknown orientation through cyclic matching.
Sector layout
The code band is the annular region between the inner and outer dark ring bands. It is divided into 16 sectors of equal angular extent (22.5 degrees each), numbered 0 through 15 proceeding counterclockwise. Each sector is filled with either a dark (0) or light (1) value, producing a 16-bit binary word.
Because the marker can appear at any in-plane rotation in the image, the absolute angular reference of sector 0 is unknown at detection time. The codebook matching algorithm handles this by trying all 16 cyclic rotations (see below).
The codebook
ringgrid embeds two related profiles:
| Profile | Size | Minimum cyclic Hamming distance | Intended use |
|---|---|---|---|
base | 893 codewords | 2 | Default shipped profile with stable IDs 0..892 |
extended | 2180 codewords | 1 | Explicit opt-in profile when ID capacity matters more than the baseline ambiguity guarantee |
Key codebook properties:
| Property | Value |
|---|---|
| Codeword length | 16 bits |
| Baseline size | 893 codewords |
| Extended size | 2180 codewords |
| Baseline minimum cyclic Hamming distance | 2 |
| Extended minimum cyclic Hamming distance | 1 |
| Generator seed | 1 |
The baseline minimum cyclic Hamming distance of 2 means that for any two distinct baseline codewords A and B, the Hamming distance between A and every cyclic rotation of B is at least 2. This guarantees that a single-bit error will not silently produce a different valid baseline codeword, though it is not sufficient for guaranteed single-bit error correction (which would require a minimum distance of 3).
The extended profile keeps the same baseline prefix but appends the remaining
rotationally unique 16-bit words whose complement classes are not already
claimed by the shipped profile. That expands capacity substantially without
introducing new polarity ambiguity beyond the fixed baseline, but still lowers
the minimum cyclic Hamming distance to 1 and therefore weakens the baseline
profile’s ambiguity guarantee. This is why extended is explicit opt-in.
Note that the codebook minimum distance (2 for base) is a global property across all codewords. The minimum distance between hex-adjacent markers on the board depends on which IDs are assigned to which positions. With sequential assignment, adjacent markers may have distance as low as 2. The ID assignment optimizer raises this to 5 on the default board, making decode errors far less likely to produce a valid neighbor’s ID.
Additional constraints enforced during codebook generation:
- No rotational symmetry: no codeword equals any of its own non-trivial cyclic rotations. This ensures that each physical marker has a unique observed pattern regardless of orientation.
- Pairwise uniqueness under rotation: no two distinct codewords share any cyclic rotation, preventing ambiguous matches.
The codebook is embedded as a compile-time constant array in
crates/ringgrid/src/marker/codebook.rs:
#![allow(unused)]
fn main() {
pub const CODEBOOK_BITS: usize = 16;
pub const CODEBOOK_N: usize = 893;
pub const CODEBOOK_MIN_CYCLIC_DIST: usize = 2;
pub const CODEBOOK_SEED: u64 = 1;
pub const CODEBOOK_EXTENDED_N: usize = 2180;
pub const CODEBOOK_EXTENDED_MIN_CYCLIC_DIST: usize = 1;
pub const CODEBOOK: [u16; 893] = [
0x035D, 0x1F95, 0x0B1D, /* ... */
];
pub const CODEBOOK_EXTENDED: [u16; 2180] = [
0x035D, 0x1F95, 0x0B1D, /* ... */
];
}
This file is generated by tools/gen_codebook.py and should never be edited
by hand. To regenerate:
python3 tools/gen_codebook.py \
--n 893 --seed 1 \
--out_json tools/codebook.json \
--out_rs crates/ringgrid/src/marker/codebook.rs
cargo build # rebuild after regenerating
Decoding process
The detector decodes a marker’s identity after fitting its outer ellipse. The process has four stages: sampling, thresholding, binarization, and codebook matching.
1. Sampling sector intensities
The code band is sampled along the fitted outer ellipse at uniform
parametric angle, scaled to the code-band radius. Sampling is controlled by
DecodeConfig:
#![allow(unused)]
fn main() {
pub struct DecodeConfig {
/// Ratio of code band center radius to outer ellipse semi-major axis
/// (derived from the target).
pub code_band_ratio: f32,
/// Angular samples per sector; also the number of sub-sector phases tried.
pub samples_per_sector: usize, // default: 8
/// Number of radial rings to sample.
pub n_radial_rings: usize, // default: 3
/// Minimum sector contrast before decoding.
pub min_decode_contrast: f32, // default: 0.03
// … 2-means threshold iteration controls
}
}
Where the printed sector boundaries fall in parametric angle is a continuous
unknown, and cyclic matching only absorbs whole sectors. The decoder
therefore samples 16 × samples_per_sector points (each averaged over
n_radial_rings radii spanning ±10 % of the code-band ratio), and for every
sub-sector phase averages the central half of each sector; the phase whose
16 means vary the most is the one aligned with the printed sectors (a sector
straddling a boundary averages bright and dark toward the middle).
2. Thresholding
The 16 sector means are binarized using an iterative 2-means clustering algorithm:
- Initialize the threshold at the midpoint of the intensity range.
- Split sectors into two groups (above and below threshold).
- Recompute the threshold as the midpoint of the two group means.
- Repeat until convergence (up to 10 iterations).
A minimum contrast check rejects markers where the intensity range is too narrow (less than 0.03 on a [0, 1] scale), which would indicate a featureless or uniform code band.
3. Binarization
Each sector with intensity above the threshold is assigned bit 1 (bright); below gets bit 0. This produces a 16-bit observed word. Targets are printed dark on light; the complement word is never tried.
4. Matching against the target’s codewords
The observed word is matched against every cyclic rotation of every codeword the target’s cells carry (not the whole profile):
distance = popcount(observed_word XOR rotate(codeword, k))
and accepted only when the distance is 0. With the baseline’s minimum cyclic Hamming distance of 2, 99.9 % of all 16-bit words lie within one bit of some codeword, so a non-zero distance is no evidence for the nearest codeword; and restricting matching to the target’s own IDs (203 of 893 on the default board) lowers the share of random words that match exactly from 22 % to 5 % (ADR-027).
The generator preserves the committed baseline profile as the fixed prefix, then
appends the remaining valid rotational equivalence classes whose complement
classes are not already claimed by the shipped profile to form the extended
profile. For the committed --n 893 --seed 1 artifacts:
- baseline minimum cyclic Hamming distance:
2 - extended minimum cyclic Hamming distance:
1
Decode metrics
An accepted decode produces a DecodeMetrics record (diagnostics channel):
#![allow(unused)]
fn main() {
pub struct DecodeMetrics {
/// 16-bit word sampled on the phase-aligned grid.
pub observed_word: u16,
/// Matched codebook ID.
pub best_id: usize,
/// Code rotation in sectors, [0, 16): printed sector j starts at
/// parametric angle (j + rotation)·2π/16 on the fitted outer ellipse.
pub rotation: f32,
}
}
There is no distance or margin: every accepted decode is an exact match.
Cyclic matching in detail
Because a marker can appear at any in-plane rotation, the detector does not know which physical sector corresponds to bit 0 of the codeword. The cyclic matching algorithm compensates by testing all 16 rotational alignments:
#![allow(unused)]
fn main() {
pub fn rotate_left_16(word: u16, k: u32) -> u16 {
word.rotate_left(k % 16)
}
}
For each codeword in the codebook, the matcher rotates the codeword by
0, 1, 2, …, 15 positions and computes the Hamming distance to the
observed word at each offset. The rotation that produces the minimum distance
is the whole part of rotation; the estimated sub-sector phase is its
fractional part.
Rotation recovery is a free byproduct of the match, and the anchor stage uses it: under a candidate board placement the code rotation at each ring is predictable, and a decode votes only for the placements it agrees with.
Polarity
Targets are printed dark rings on a light background, and the sampled word is matched as is. Earlier versions also matched the complement word as a fallback; on the rtv3d captures every one of the 508 decodes it produced was wrong (ADR-027), so it was removed.