@vitavision/overlays 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE-APACHE +202 -0
- package/LICENSE-MIT +21 -0
- package/README.md +77 -0
- package/package.json +55 -0
- package/src/EllipseSet.tsx +228 -0
- package/src/TargetOverlay.tsx +241 -0
- package/src/glyphs.ts +139 -0
- package/src/index.ts +60 -0
- package/src/lattice.ts +197 -0
- package/src/layerItems.ts +148 -0
- package/src/model.ts +184 -0
- package/src/puzzleboard.ts +93 -0
- package/src/styles.css +8 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What each stage2d layer is given, for each part of a detection: the pure mapping from the
|
|
3
|
+
* normalised `TargetDetection` to the items of `GridLayer`, `AreaSet`, `PointSet` and
|
|
4
|
+
* `EllipseSet`. `TargetOverlay` is these plus the wiring, so this is where the overlay's
|
|
5
|
+
* decisions are, and where they are tested without a DOM.
|
|
6
|
+
*
|
|
7
|
+
* Pure: no React, no DOM.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { AreaSetItem, GridNode, PointSetItem } from "@vitavision/stage2d";
|
|
11
|
+
|
|
12
|
+
import type { EllipseSetItem } from "./EllipseSet";
|
|
13
|
+
import { packAxes } from "./glyphs";
|
|
14
|
+
import { markerPolygons } from "./lattice";
|
|
15
|
+
import { isLatticeKind, type TargetCircle, type TargetCorner, type TargetDetection, type TargetEdgeBit, type TargetRing } from "./model";
|
|
16
|
+
|
|
17
|
+
/** The label of a corner: its own, else the lattice index on a board, else the id. */
|
|
18
|
+
function cornerLabel(corner: TargetCorner, lattice: boolean): string {
|
|
19
|
+
if (corner.label !== undefined) return corner.label;
|
|
20
|
+
if (lattice && Number.isInteger(corner.i) && Number.isInteger(corner.j)) return `${corner.i},${corner.j}`;
|
|
21
|
+
return String(corner.id);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The nodes of the corner layer.
|
|
26
|
+
*
|
|
27
|
+
* A corner with an `angle` is a `directed` marker (its edge directions, one or two), any other a
|
|
28
|
+
* `plus`. On a board kind the lattice indices are kept, so `GridLayer` joins neighbours; on any
|
|
29
|
+
* other kind they are `NaN`, which `latticeEdges` reads as "not part of a lattice", so no edge is
|
|
30
|
+
* drawn.
|
|
31
|
+
*
|
|
32
|
+
* @param detection - The detection.
|
|
33
|
+
* @returns One node per corner, in order.
|
|
34
|
+
*/
|
|
35
|
+
export function cornerNodes(detection: TargetDetection): GridNode[] {
|
|
36
|
+
const lattice = isLatticeKind(detection.kind);
|
|
37
|
+
return (detection.corners ?? []).map((corner): GridNode => {
|
|
38
|
+
const node: GridNode = {
|
|
39
|
+
id: corner.id,
|
|
40
|
+
x: corner.x,
|
|
41
|
+
y: corner.y,
|
|
42
|
+
i: lattice && Number.isInteger(corner.i) ? corner.i! : Number.NaN,
|
|
43
|
+
j: lattice && Number.isInteger(corner.j) ? corner.j! : Number.NaN,
|
|
44
|
+
label: cornerLabel(corner, lattice),
|
|
45
|
+
};
|
|
46
|
+
if (corner.angle !== undefined) {
|
|
47
|
+
node.kind = "directed";
|
|
48
|
+
node.angle = packAxes(corner.angle, corner.angle2);
|
|
49
|
+
}
|
|
50
|
+
return node;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The regions of the marker layer: each marker's quad, labelled with its text or its id.
|
|
56
|
+
*
|
|
57
|
+
* @param detection - The detection.
|
|
58
|
+
* @returns One area per marker with a usable quad.
|
|
59
|
+
*/
|
|
60
|
+
export function markerAreas(detection: TargetDetection): AreaSetItem[] {
|
|
61
|
+
return markerPolygons(detection.markers ?? []).map((polygon) => ({
|
|
62
|
+
id: polygon.id,
|
|
63
|
+
points: polygon.points,
|
|
64
|
+
label: polygon.label ?? String(polygon.id),
|
|
65
|
+
}));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** The label of a circle: its own, else `(i, j)` when it matched a cell. */
|
|
69
|
+
function circleLabel(circle: TargetCircle): string | undefined {
|
|
70
|
+
if (circle.label !== undefined) return circle.label;
|
|
71
|
+
return Number.isInteger(circle.i) && Number.isInteger(circle.j) ? `(${circle.i}, ${circle.j})` : undefined;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The points of the circle layer. Polarity picks the glyph: `circle-white` is a hollow ring,
|
|
76
|
+
* `circle-black` a ring with a dot.
|
|
77
|
+
*
|
|
78
|
+
* @param detection - The detection.
|
|
79
|
+
* @returns One point per circle.
|
|
80
|
+
*/
|
|
81
|
+
export function circlePoints(detection: TargetDetection): PointSetItem[] {
|
|
82
|
+
return (detection.circles ?? []).map((circle) => {
|
|
83
|
+
const item: PointSetItem = { id: circle.id, x: circle.x, y: circle.y, kind: `circle-${circle.polarity}` };
|
|
84
|
+
const label = circleLabel(circle);
|
|
85
|
+
if (label !== undefined) item.label = label;
|
|
86
|
+
return item;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The points of the ring layer: a `plus` at each centre, picked anywhere inside the outer ellipse
|
|
92
|
+
* (`pickRadius` is the larger semi-axis), labelled with the ring's text or its id.
|
|
93
|
+
*
|
|
94
|
+
* @param rings - The rings.
|
|
95
|
+
* @returns One point per ring.
|
|
96
|
+
*/
|
|
97
|
+
export function ringCentres(rings: readonly TargetRing[]): PointSetItem[] {
|
|
98
|
+
return rings.map((ring) => ({
|
|
99
|
+
id: ring.id,
|
|
100
|
+
x: ring.x,
|
|
101
|
+
y: ring.y,
|
|
102
|
+
kind: "plus",
|
|
103
|
+
pickRadius: Math.max(ring.outer.rx, ring.outer.ry),
|
|
104
|
+
label: ring.label ?? String(ring.id),
|
|
105
|
+
}));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The ellipses of the rings' two edges. The outer is `feature`, the inner `model` (a fitted
|
|
110
|
+
* second edge), and the smaller inner one is told from the outer by size as well as by colour.
|
|
111
|
+
*
|
|
112
|
+
* @param rings - The rings.
|
|
113
|
+
* @returns The outer ellipses, and the inner ones of the rings that have one.
|
|
114
|
+
*/
|
|
115
|
+
export function ringEllipses(rings: readonly TargetRing[]): { outer: EllipseSetItem[]; inner: EllipseSetItem[] } {
|
|
116
|
+
const outer: EllipseSetItem[] = [];
|
|
117
|
+
const inner: EllipseSetItem[] = [];
|
|
118
|
+
for (const ring of rings) {
|
|
119
|
+
outer.push({ id: ring.id, x: ring.x, y: ring.y, rx: ring.outer.rx, ry: ring.outer.ry, angle: ring.outer.angle, role: "feature" });
|
|
120
|
+
if (ring.inner) {
|
|
121
|
+
inner.push({ id: ring.id, x: ring.x, y: ring.y, rx: ring.inner.rx, ry: ring.inner.ry, angle: ring.inner.angle, role: "model" });
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return { outer, inner };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Opacity of a dot at confidence 0 and at confidence 1: a weak bit is visible, a sure one is solid. */
|
|
128
|
+
const BIT_OPACITY_MIN = 0.35;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* The dots of the edge-bit layer: bit 1 is a solid `feature` circle, bit 0 a dashed `model`
|
|
132
|
+
* one, and the opacity runs from 35 % at confidence 0 to 100 % at confidence 1.
|
|
133
|
+
*
|
|
134
|
+
* @param bits - The edge bits.
|
|
135
|
+
* @returns One ellipse (a circle) per bit.
|
|
136
|
+
*/
|
|
137
|
+
export function edgeBitEllipses(bits: readonly TargetEdgeBit[]): EllipseSetItem[] {
|
|
138
|
+
return bits.map((bit) => ({
|
|
139
|
+
id: bit.id,
|
|
140
|
+
x: bit.x,
|
|
141
|
+
y: bit.y,
|
|
142
|
+
rx: bit.radius,
|
|
143
|
+
ry: bit.radius,
|
|
144
|
+
role: bit.bit === 1 ? "feature" : "model",
|
|
145
|
+
dashed: bit.bit === 0,
|
|
146
|
+
opacity: BIT_OPACITY_MIN + (1 - BIT_OPACITY_MIN) * Math.min(1, Math.max(0, bit.confidence)),
|
|
147
|
+
}));
|
|
148
|
+
}
|
package/src/model.ts
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The input model: one detection run, independent of the library that produced it.
|
|
3
|
+
*
|
|
4
|
+
* Every detector in the vitavision family (calib-targets' chessboard, ChArUco, marker board and
|
|
5
|
+
* PuzzleBoard, ringgrid, chess-corners) returns its own result shape. An app maps that shape to a
|
|
6
|
+
* `TargetDetection` once, and the overlay never learns which library ran. Nothing here imports a
|
|
7
|
+
* detector package: these are plain data types.
|
|
8
|
+
*
|
|
9
|
+
* Conventions, the same as `@vitavision/stage2d`:
|
|
10
|
+
* - Positions are **image pixels, with the centre of pixel `i` at coordinate `i`** (the frame
|
|
11
|
+
* the WASM detectors report, `image_px_center`). The stage places them; do not add 0.5.
|
|
12
|
+
* - `y` points down. Angles are **radians, clockwise on screen** (from +x towards +y).
|
|
13
|
+
* - Lengths named `rx`, `ry` and `radius` are image pixels, so they scale with the image.
|
|
14
|
+
*
|
|
15
|
+
* Pure: no React, no DOM.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** An item's identity. Ids are unique within one detection, across corners, markers, circles and rings. */
|
|
19
|
+
export type TargetId = string | number;
|
|
20
|
+
|
|
21
|
+
/** What a detection is of. It chooses defaults, not what is drawn: whatever is present is drawn. */
|
|
22
|
+
export type TargetKind =
|
|
23
|
+
| "chessboard"
|
|
24
|
+
| "charuco"
|
|
25
|
+
| "markerboard"
|
|
26
|
+
| "puzzleboard"
|
|
27
|
+
| "ringgrid"
|
|
28
|
+
| "corners"
|
|
29
|
+
| "circles";
|
|
30
|
+
|
|
31
|
+
/** Every `TargetKind`. */
|
|
32
|
+
export const TARGET_KINDS: readonly TargetKind[] = [
|
|
33
|
+
"chessboard",
|
|
34
|
+
"charuco",
|
|
35
|
+
"markerboard",
|
|
36
|
+
"puzzleboard",
|
|
37
|
+
"ringgrid",
|
|
38
|
+
"corners",
|
|
39
|
+
"circles",
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
/** The kinds whose corners form a lattice, so their neighbours are joined by edges. */
|
|
43
|
+
const LATTICE_KINDS: ReadonlySet<TargetKind> = new Set(["chessboard", "charuco", "markerboard", "puzzleboard"]);
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Whether a detection of this kind draws lattice edges between corner neighbours.
|
|
47
|
+
*
|
|
48
|
+
* @param kind - The detection's kind.
|
|
49
|
+
* @returns `true` for the four calibration boards, `false` for loose corners, circles and rings.
|
|
50
|
+
*/
|
|
51
|
+
export function isLatticeKind(kind: TargetKind): boolean {
|
|
52
|
+
return LATTICE_KINDS.has(kind);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** One detected corner or X-junction. */
|
|
56
|
+
export interface TargetCorner {
|
|
57
|
+
/** Its identity. Callbacks report this id back. */
|
|
58
|
+
id: TargetId;
|
|
59
|
+
/** Horizontal position, image pixels. */
|
|
60
|
+
x: number;
|
|
61
|
+
/** Vertical position, image pixels. */
|
|
62
|
+
y: number;
|
|
63
|
+
/** Lattice index along the first board axis, an integer. Absent for a corner with no place on a board. */
|
|
64
|
+
i?: number | undefined;
|
|
65
|
+
/** Lattice index along the second board axis, an integer. */
|
|
66
|
+
j?: number | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* The detector's confidence. It is carried for the app (a list, a filter) and never drawn as a
|
|
69
|
+
* colour: a score is not a verdict (visual-language §5).
|
|
70
|
+
*/
|
|
71
|
+
score?: number | undefined;
|
|
72
|
+
/** Orientation of the first edge through the corner, radians. Axes are lines, so `angle` and `angle + π` are the same. */
|
|
73
|
+
angle?: number | undefined;
|
|
74
|
+
/** Orientation of the second edge through the corner, radians. Needs `angle`. */
|
|
75
|
+
angle2?: number | undefined;
|
|
76
|
+
/** Text beside it. Defaults to the lattice index `i,j` on a board, else the id. */
|
|
77
|
+
label?: string | undefined;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** One decoded fiducial marker (ArUco, AprilTag): a quad. */
|
|
81
|
+
export interface TargetMarker {
|
|
82
|
+
/** Its identity. For an ArUco marker, usually its dictionary id. */
|
|
83
|
+
id: TargetId;
|
|
84
|
+
/**
|
|
85
|
+
* The quad's corners in the marker's own order, `[x0, y0, x1, y1, x2, y2, x3, y3]`. Corner 0 is
|
|
86
|
+
* ticked, so the order reads as the marker's orientation.
|
|
87
|
+
*/
|
|
88
|
+
corners: readonly number[];
|
|
89
|
+
/** Text drawn inside it. Defaults to the id. */
|
|
90
|
+
label?: string | undefined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The printed colour of a circle in a marker board. */
|
|
94
|
+
export type CirclePolarity = "white" | "black";
|
|
95
|
+
|
|
96
|
+
/** One detected circle of a marker board, matched or not to the board model. */
|
|
97
|
+
export interface TargetCircle {
|
|
98
|
+
/** Its identity. */
|
|
99
|
+
id: TargetId;
|
|
100
|
+
/** Centre x, image pixels. */
|
|
101
|
+
x: number;
|
|
102
|
+
/** Centre y, image pixels. */
|
|
103
|
+
y: number;
|
|
104
|
+
/** Printed polarity. It is drawn as a shape (hollow ring, or ring with a centre dot), not as a colour. */
|
|
105
|
+
polarity: CirclePolarity;
|
|
106
|
+
/** The board cell it matched, first axis. */
|
|
107
|
+
i?: number | undefined;
|
|
108
|
+
/** The board cell it matched, second axis. */
|
|
109
|
+
j?: number | undefined;
|
|
110
|
+
/** Text beside it. Defaults to `(i, j)` when both are set. */
|
|
111
|
+
label?: string | undefined;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** A fitted ellipse. */
|
|
115
|
+
export interface TargetEllipse {
|
|
116
|
+
/** Semi-axis along the rotated x axis, image pixels. */
|
|
117
|
+
rx: number;
|
|
118
|
+
/** Semi-axis along the rotated y axis, image pixels. */
|
|
119
|
+
ry: number;
|
|
120
|
+
/** Rotation of the `rx` axis, radians clockwise on screen. */
|
|
121
|
+
angle: number;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** One ring marker of a ring grid: two concentric fitted ellipses and a centre. */
|
|
125
|
+
export interface TargetRing {
|
|
126
|
+
/** Its identity. */
|
|
127
|
+
id: TargetId;
|
|
128
|
+
/** Centre x, image pixels. */
|
|
129
|
+
x: number;
|
|
130
|
+
/** Centre y, image pixels. */
|
|
131
|
+
y: number;
|
|
132
|
+
/** The outer edge of the ring. */
|
|
133
|
+
outer: TargetEllipse;
|
|
134
|
+
/** The inner edge of the ring, when the detector fitted one. */
|
|
135
|
+
inner?: TargetEllipse | undefined;
|
|
136
|
+
/** Text beside the centre. Defaults to the id. */
|
|
137
|
+
label?: string | undefined;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* One decoded bit of a PuzzleBoard, drawn as a dot on the edge between two corners.
|
|
142
|
+
* `edgeBitsFromPuzzleboard` builds these from a PuzzleBoard result.
|
|
143
|
+
*/
|
|
144
|
+
export interface TargetEdgeBit {
|
|
145
|
+
/** Its identity. Not reported by callbacks: edge bits are not picked. */
|
|
146
|
+
id: TargetId;
|
|
147
|
+
/** Midpoint x of the edge, image pixels. */
|
|
148
|
+
x: number;
|
|
149
|
+
/** Midpoint y of the edge, image pixels. */
|
|
150
|
+
y: number;
|
|
151
|
+
/** The dot's radius, image pixels: about a quarter of the edge. */
|
|
152
|
+
radius: number;
|
|
153
|
+
/** The decoded bit. 1 is a solid dot, 0 a dashed one. */
|
|
154
|
+
bit: 0 | 1;
|
|
155
|
+
/** Decode confidence in [0, 1]. It sets the dot's opacity. */
|
|
156
|
+
confidence: number;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** One detection run: what a detector found in one image. */
|
|
160
|
+
export interface TargetDetection {
|
|
161
|
+
/** What was detected. */
|
|
162
|
+
kind: TargetKind;
|
|
163
|
+
/** Corners and X-junctions. */
|
|
164
|
+
corners?: readonly TargetCorner[] | undefined;
|
|
165
|
+
/** Decoded fiducial markers. */
|
|
166
|
+
markers?: readonly TargetMarker[] | undefined;
|
|
167
|
+
/** Marker-board circles. */
|
|
168
|
+
circles?: readonly TargetCircle[] | undefined;
|
|
169
|
+
/** Ring markers. */
|
|
170
|
+
rings?: readonly TargetRing[] | undefined;
|
|
171
|
+
/** PuzzleBoard edge bits. */
|
|
172
|
+
edgeBits?: readonly TargetEdgeBit[] | undefined;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** The part of a detection an item belongs to. */
|
|
176
|
+
export type TargetPart = "corner" | "marker" | "circle" | "ring";
|
|
177
|
+
|
|
178
|
+
/** Which item the pointer is on. */
|
|
179
|
+
export interface TargetHit {
|
|
180
|
+
/** The item's id, as given in the detection. */
|
|
181
|
+
id: TargetId;
|
|
182
|
+
/** The collection it came from. */
|
|
183
|
+
part: TargetPart;
|
|
184
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PuzzleBoard edge bits: from a decoder's observed edges to the dots the overlay draws.
|
|
3
|
+
*
|
|
4
|
+
* A PuzzleBoard encodes its position in the bumps along the edges between corners. The decoder
|
|
5
|
+
* reports each observed edge in the image's own local frame (`row`, `col`), and the alignment
|
|
6
|
+
* that maps that frame into the board's master frame, which is periodic with period 501. The
|
|
7
|
+
* corners are labelled with master indices, so an edge's two corners are found by mapping its
|
|
8
|
+
* local end points through the alignment, modulo the period. This is the arithmetic of
|
|
9
|
+
* vitavision's `PuzzleboardOverlay.buildEdgeMarkers`, as a pure function over structural types,
|
|
10
|
+
* so no detector package is imported.
|
|
11
|
+
*
|
|
12
|
+
* Pure: no React, no DOM.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { gridKey } from "./lattice";
|
|
16
|
+
import type { TargetCorner, TargetEdgeBit } from "./model";
|
|
17
|
+
|
|
18
|
+
/** The period of the PuzzleBoard master pattern. */
|
|
19
|
+
export const PUZZLEBOARD_PERIOD = 501;
|
|
20
|
+
|
|
21
|
+
/** One observed edge of a PuzzleBoard decode. */
|
|
22
|
+
export interface PuzzleboardEdge {
|
|
23
|
+
/** Row of the edge's first corner, in the image's local frame. */
|
|
24
|
+
row: number;
|
|
25
|
+
/** Column of the edge's first corner, in the image's local frame. */
|
|
26
|
+
col: number;
|
|
27
|
+
/** `"horizontal"` joins local `(col, row)` to `(col + 1, row)`; `"vertical"` to `(col, row + 1)`. */
|
|
28
|
+
orientation: "horizontal" | "vertical";
|
|
29
|
+
/** The decoded bit. */
|
|
30
|
+
bit: 0 | 1;
|
|
31
|
+
/** Decode confidence, [0, 1]. */
|
|
32
|
+
confidence: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The map from the local frame to master indices: `master = M · local + translation`, modulo the period. */
|
|
36
|
+
export interface PuzzleboardAlignment {
|
|
37
|
+
/** The 2×2 integer matrix `[[a, b], [c, d]]`, acting on local `(i, j)`. */
|
|
38
|
+
transform: { a: number; b: number; c: number; d: number };
|
|
39
|
+
/** The translation, added after the matrix. */
|
|
40
|
+
translation: readonly [number, number];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** `value` modulo `period`, in `[0, period)` for any sign. */
|
|
44
|
+
function wrap(value: number, period: number): number {
|
|
45
|
+
return ((value % period) + period) % period;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The edge bits of a PuzzleBoard decode, placed on the image.
|
|
50
|
+
*
|
|
51
|
+
* An edge is dropped when either of its corners was not detected. Each dot sits at the midpoint
|
|
52
|
+
* of its edge with a radius of a quarter of the edge's length (the bump is about half an edge
|
|
53
|
+
* wide); its opacity is the app's to take from `confidence`.
|
|
54
|
+
*
|
|
55
|
+
* @param edges - The observed edges.
|
|
56
|
+
* @param corners - The labelled corners, whose `i` and `j` are **master** indices.
|
|
57
|
+
* @param alignment - The local-to-master map.
|
|
58
|
+
* @param period - The master pattern's period. Defaults to 501.
|
|
59
|
+
* @returns One `TargetEdgeBit` per edge whose corners were both found, in input order. Empty when there is no alignment.
|
|
60
|
+
*/
|
|
61
|
+
export function edgeBitsFromPuzzleboard(
|
|
62
|
+
edges: readonly PuzzleboardEdge[],
|
|
63
|
+
corners: readonly TargetCorner[],
|
|
64
|
+
alignment: PuzzleboardAlignment | null | undefined,
|
|
65
|
+
period: number = PUZZLEBOARD_PERIOD,
|
|
66
|
+
): TargetEdgeBit[] {
|
|
67
|
+
if (!alignment || edges.length === 0 || corners.length === 0) return [];
|
|
68
|
+
|
|
69
|
+
const byMaster = new Map<string, TargetCorner>();
|
|
70
|
+
for (const corner of corners) {
|
|
71
|
+
if (Number.isInteger(corner.i) && Number.isInteger(corner.j)) byMaster.set(gridKey(corner.i!, corner.j!), corner);
|
|
72
|
+
}
|
|
73
|
+
const { a, b, c, d } = alignment.transform;
|
|
74
|
+
const [tx, ty] = alignment.translation;
|
|
75
|
+
const toMaster = (i: number, j: number): string => gridKey(wrap(a * i + b * j + tx, period), wrap(c * i + d * j + ty, period));
|
|
76
|
+
|
|
77
|
+
const out: TargetEdgeBit[] = [];
|
|
78
|
+
edges.forEach((edge, index) => {
|
|
79
|
+
// Local coordinates are (i = col, j = row).
|
|
80
|
+
const from = byMaster.get(toMaster(edge.col, edge.row));
|
|
81
|
+
const to = byMaster.get(edge.orientation === "horizontal" ? toMaster(edge.col + 1, edge.row) : toMaster(edge.col, edge.row + 1));
|
|
82
|
+
if (!from || !to) return;
|
|
83
|
+
out.push({
|
|
84
|
+
id: `${edge.orientation}-${edge.row}-${edge.col}-${index}`,
|
|
85
|
+
x: 0.5 * (from.x + to.x),
|
|
86
|
+
y: 0.5 * (from.y + to.y),
|
|
87
|
+
radius: 0.25 * Math.hypot(to.x - from.x, to.y - from.y),
|
|
88
|
+
bit: edge.bit,
|
|
89
|
+
confidence: Math.min(1, Math.max(0, edge.confidence)),
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
return out;
|
|
93
|
+
}
|
package/src/styles.css
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* `@source` makes Tailwind v4 find this package's class names: it resolves against the
|
|
3
|
+
* stylesheet that declares it, and this file is exported from `src/` (which ships), so it scans
|
|
4
|
+
* the package's sources. Import it after `@vitavision/stage2d/styles.css`, which defines the
|
|
5
|
+
* overlay role colours (`--stage-feature`, …) every mark here is painted in. This package adds
|
|
6
|
+
* no colours of its own: overlay colours are roles, never hexes (visual-language §5).
|
|
7
|
+
*/
|
|
8
|
+
@source "./";
|