@vitavision/overlays 0.1.0 → 0.2.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/README.md +2 -2
- package/dist/index.d.ts +421 -0
- package/dist/index.js +651 -0
- package/dist/index.js.map +1 -0
- package/package.json +4 -4
- package/src/TargetOverlay.tsx +4 -4
- package/src/glyphs.ts +9 -67
- package/src/index.ts +2 -2
- package/src/layerItems.ts +3 -4
- package/src/EllipseSet.tsx +0 -228
package/dist/index.js
ADDED
|
@@ -0,0 +1,651 @@
|
|
|
1
|
+
import { AreaSet, EllipseSet, EllipseSet as EllipseSet$1, GridLayer, PointSet, circlePath, ellipsePath, latticeEdges } from "@vitavision/stage2d";
|
|
2
|
+
import { useCallback, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
3
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
//#region src/glyphs.ts
|
|
5
|
+
/**
|
|
6
|
+
* The glyphs of a calibration-target overlay, as path generators.
|
|
7
|
+
*
|
|
8
|
+
* A glyph is a `MarkerShape` for `@vitavision/stage2d`'s `PointSet`: a pure function from a
|
|
9
|
+
* position to SVG path data, so a layer appends thousands of them into one `<path>` (ADR-0004).
|
|
10
|
+
* They are registered under the names in `TARGET_MARKERS`, which `TargetOverlay` passes as
|
|
11
|
+
* `markers`. Sizes are screen pixels, from `unit` (image pixels per screen pixel).
|
|
12
|
+
*
|
|
13
|
+
* Pure: no React, no DOM.
|
|
14
|
+
*/
|
|
15
|
+
/** Round to 1/1000 of an image pixel. */
|
|
16
|
+
function round(value) {
|
|
17
|
+
return Math.round(value * 1e3) / 1e3;
|
|
18
|
+
}
|
|
19
|
+
/** An angle as a direction of a line: modulo π, in [0, π). */
|
|
20
|
+
function lineAngle(angle) {
|
|
21
|
+
const wrapped = angle % Math.PI;
|
|
22
|
+
return wrapped < 0 ? wrapped + Math.PI : wrapped;
|
|
23
|
+
}
|
|
24
|
+
/** Half-length of a directed corner's axes, and the gap left at its centre, in screen pixels. */
|
|
25
|
+
const AXIS_REACH = 8;
|
|
26
|
+
const AXIS_GAP = 2;
|
|
27
|
+
/**
|
|
28
|
+
* A corner with its edge directions: one or two lines through the point, 8 px each way with a
|
|
29
|
+
* 2 px gap at the centre, so the corner itself stays visible. Give it `angle` and, for the
|
|
30
|
+
* second direction, `angle2`. Directions are lines, so each is read modulo π.
|
|
31
|
+
*/
|
|
32
|
+
const directed = {
|
|
33
|
+
paint: "line",
|
|
34
|
+
size: AXIS_REACH,
|
|
35
|
+
path: (x, y, unit, angle, angle2) => {
|
|
36
|
+
let d = "";
|
|
37
|
+
for (const direction of angle2 === void 0 ? [angle] : [angle, angle2]) {
|
|
38
|
+
if (!Number.isFinite(direction)) continue;
|
|
39
|
+
const line = lineAngle(direction);
|
|
40
|
+
const c = Math.cos(line);
|
|
41
|
+
const s = Math.sin(line);
|
|
42
|
+
const near = AXIS_GAP * unit;
|
|
43
|
+
const far = AXIS_REACH * unit;
|
|
44
|
+
d += `M${round(x - far * c)} ${round(y - far * s)}L${round(x - near * c)} ${round(y - near * s)}M${round(x + near * c)} ${round(y + near * s)}L${round(x + far * c)} ${round(y + far * s)}`;
|
|
45
|
+
}
|
|
46
|
+
return d;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
/** Radius of a matched circle's glyph, screen pixels. */
|
|
50
|
+
const CIRCLE_RADIUS = 5.5;
|
|
51
|
+
/** Radius of the centre mark of a black circle, screen pixels; stroked, it reads as a filled dot. */
|
|
52
|
+
const CENTRE_RADIUS = .8;
|
|
53
|
+
/** The marker names `TargetOverlay` uses, to pass to `PointSet`'s `markers`. */
|
|
54
|
+
const TARGET_MARKERS = {
|
|
55
|
+
/** Edge directions of a corner, from `angle` and `angle2`. */
|
|
56
|
+
directed,
|
|
57
|
+
/** A white matched circle. */
|
|
58
|
+
"circle-white": {
|
|
59
|
+
paint: "line",
|
|
60
|
+
size: CIRCLE_RADIUS,
|
|
61
|
+
path: (x, y, unit) => circlePath(x, y, CIRCLE_RADIUS * unit)
|
|
62
|
+
},
|
|
63
|
+
/** A black matched circle. */
|
|
64
|
+
"circle-black": {
|
|
65
|
+
paint: "line",
|
|
66
|
+
size: CIRCLE_RADIUS,
|
|
67
|
+
path: (x, y, unit) => circlePath(x, y, CIRCLE_RADIUS * unit) + circlePath(x, y, CENTRE_RADIUS * unit)
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region src/lattice.ts
|
|
72
|
+
/**
|
|
73
|
+
* The lattice of a detected board: which corner sits at which `(i, j)`, which corners are
|
|
74
|
+
* neighbours, and the polygons of the markers.
|
|
75
|
+
*
|
|
76
|
+
* Ported from vitavision's `overlayData.ts` (`buildCornerGrid`, `buildGridEdges`,
|
|
77
|
+
* `buildFeatureIdByGrid`, `buildMarkerPolygons`), where ChArUco, chessboard, marker board and
|
|
78
|
+
* PuzzleBoard each carried a copy (PuzzleBoard's `buildPuzzleGrid` was a fourth). One
|
|
79
|
+
* implementation now serves all four, on the normalised `TargetDetection` input.
|
|
80
|
+
*
|
|
81
|
+
* Pure: no React, no DOM.
|
|
82
|
+
*/
|
|
83
|
+
/**
|
|
84
|
+
* The key of a lattice position, as `idByGrid` and `CornerGrid.byIndex` use.
|
|
85
|
+
*
|
|
86
|
+
* @param i - Index along the first axis.
|
|
87
|
+
* @param j - Index along the second axis.
|
|
88
|
+
* @returns `"i:j"`.
|
|
89
|
+
*/
|
|
90
|
+
function gridKey(i, j) {
|
|
91
|
+
return `${i}:${j}`;
|
|
92
|
+
}
|
|
93
|
+
/** Whether a corner carries an integer lattice index. */
|
|
94
|
+
function isGridCorner(corner) {
|
|
95
|
+
return Number.isInteger(corner.i) && Number.isInteger(corner.j);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Index a board's corners by lattice position.
|
|
99
|
+
*
|
|
100
|
+
* Corners without an integer `(i, j)` are not part of the grid. When two corners claim one
|
|
101
|
+
* position the first wins, as it does in `latticeEdges`.
|
|
102
|
+
*
|
|
103
|
+
* @param corners - The detected corners, in any order, with gaps allowed.
|
|
104
|
+
* @returns The grid.
|
|
105
|
+
*/
|
|
106
|
+
function cornerGrid(corners) {
|
|
107
|
+
const nodes = [];
|
|
108
|
+
const byIndex = /* @__PURE__ */ new Map();
|
|
109
|
+
const rowMap = /* @__PURE__ */ new Map();
|
|
110
|
+
const colMap = /* @__PURE__ */ new Map();
|
|
111
|
+
let bounds = null;
|
|
112
|
+
for (const corner of corners) {
|
|
113
|
+
if (!isGridCorner(corner)) continue;
|
|
114
|
+
const key = gridKey(corner.i, corner.j);
|
|
115
|
+
if (byIndex.has(key)) continue;
|
|
116
|
+
const position = nodes.length;
|
|
117
|
+
nodes.push(corner);
|
|
118
|
+
byIndex.set(key, position);
|
|
119
|
+
const row = rowMap.get(corner.i);
|
|
120
|
+
if (row) row.push(position);
|
|
121
|
+
else rowMap.set(corner.i, [position]);
|
|
122
|
+
const col = colMap.get(corner.j);
|
|
123
|
+
if (col) col.push(position);
|
|
124
|
+
else colMap.set(corner.j, [position]);
|
|
125
|
+
bounds = bounds ? {
|
|
126
|
+
minI: Math.min(bounds.minI, corner.i),
|
|
127
|
+
maxI: Math.max(bounds.maxI, corner.i),
|
|
128
|
+
minJ: Math.min(bounds.minJ, corner.j),
|
|
129
|
+
maxJ: Math.max(bounds.maxJ, corner.j)
|
|
130
|
+
} : {
|
|
131
|
+
minI: corner.i,
|
|
132
|
+
maxI: corner.i,
|
|
133
|
+
minJ: corner.j,
|
|
134
|
+
maxJ: corner.j
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
const ordered = (groups, by) => [...groups.entries()].sort(([a], [b]) => a - b).map(([, group]) => group.sort((p, q) => nodes[p][by] - nodes[q][by]));
|
|
138
|
+
return {
|
|
139
|
+
nodes,
|
|
140
|
+
byIndex,
|
|
141
|
+
rows: ordered(rowMap, "j"),
|
|
142
|
+
cols: ordered(colMap, "i"),
|
|
143
|
+
bounds
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* The edges between neighbouring corners of a grid: `(i, j)` to `(i, j + 1)` are the row edges,
|
|
148
|
+
* `(i, j)` to `(i + 1, j)` the column edges. A missed corner leaves a gap, never a long edge.
|
|
149
|
+
* It is `latticeEdges` from `@vitavision/stage2d` with positions attached.
|
|
150
|
+
*
|
|
151
|
+
* @param grid - From `cornerGrid`.
|
|
152
|
+
* @returns The two edge lists.
|
|
153
|
+
*/
|
|
154
|
+
function gridEdges(grid) {
|
|
155
|
+
const rowEdges = [];
|
|
156
|
+
const colEdges = [];
|
|
157
|
+
for (const edge of latticeEdges(grid.nodes)) {
|
|
158
|
+
const a = grid.nodes[edge.a];
|
|
159
|
+
const b = grid.nodes[edge.b];
|
|
160
|
+
(edge.axis === "j" ? rowEdges : colEdges).push({
|
|
161
|
+
a: a.id,
|
|
162
|
+
b: b.id,
|
|
163
|
+
x1: a.x,
|
|
164
|
+
y1: a.y,
|
|
165
|
+
x2: b.x,
|
|
166
|
+
y2: b.y
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
rowEdges,
|
|
171
|
+
colEdges
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* The id of the corner at each lattice position, to map a board cell back to an app's feature.
|
|
176
|
+
*
|
|
177
|
+
* @param corners - The detected corners.
|
|
178
|
+
* @returns Ids by `gridKey(i, j)`. A corner without an integer index is left out; the first of two sharing a position wins.
|
|
179
|
+
*/
|
|
180
|
+
function idByGrid(corners) {
|
|
181
|
+
const map = /* @__PURE__ */ new Map();
|
|
182
|
+
for (const corner of corners) {
|
|
183
|
+
if (!isGridCorner(corner)) continue;
|
|
184
|
+
const key = gridKey(corner.i, corner.j);
|
|
185
|
+
if (!map.has(key)) map.set(key, corner.id);
|
|
186
|
+
}
|
|
187
|
+
return map;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* The markers as polygons, with their centres.
|
|
191
|
+
*
|
|
192
|
+
* @param markers - The detected markers.
|
|
193
|
+
* @returns A polygon per marker that has at least three finite vertices; the others are dropped.
|
|
194
|
+
*/
|
|
195
|
+
function markerPolygons(markers) {
|
|
196
|
+
const out = [];
|
|
197
|
+
for (const marker of markers) {
|
|
198
|
+
const n = marker.corners.length;
|
|
199
|
+
if (n < 6 || n % 2 !== 0 || !marker.corners.every(Number.isFinite)) continue;
|
|
200
|
+
let sx = 0;
|
|
201
|
+
let sy = 0;
|
|
202
|
+
for (let k = 0; k < n; k += 2) {
|
|
203
|
+
sx += marker.corners[k];
|
|
204
|
+
sy += marker.corners[k + 1];
|
|
205
|
+
}
|
|
206
|
+
const polygon = {
|
|
207
|
+
id: marker.id,
|
|
208
|
+
points: [...marker.corners],
|
|
209
|
+
centre: {
|
|
210
|
+
x: sx / (n / 2),
|
|
211
|
+
y: sy / (n / 2)
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
if (marker.label !== void 0) polygon.label = marker.label;
|
|
215
|
+
out.push(polygon);
|
|
216
|
+
}
|
|
217
|
+
return out;
|
|
218
|
+
}
|
|
219
|
+
//#endregion
|
|
220
|
+
//#region src/model.ts
|
|
221
|
+
/** Every `TargetKind`. */
|
|
222
|
+
const TARGET_KINDS = [
|
|
223
|
+
"chessboard",
|
|
224
|
+
"charuco",
|
|
225
|
+
"markerboard",
|
|
226
|
+
"puzzleboard",
|
|
227
|
+
"ringgrid",
|
|
228
|
+
"corners",
|
|
229
|
+
"circles"
|
|
230
|
+
];
|
|
231
|
+
/** The kinds whose corners form a lattice, so their neighbours are joined by edges. */
|
|
232
|
+
const LATTICE_KINDS = /* @__PURE__ */ new Set([
|
|
233
|
+
"chessboard",
|
|
234
|
+
"charuco",
|
|
235
|
+
"markerboard",
|
|
236
|
+
"puzzleboard"
|
|
237
|
+
]);
|
|
238
|
+
/**
|
|
239
|
+
* Whether a detection of this kind draws lattice edges between corner neighbours.
|
|
240
|
+
*
|
|
241
|
+
* @param kind - The detection's kind.
|
|
242
|
+
* @returns `true` for the four calibration boards, `false` for loose corners, circles and rings.
|
|
243
|
+
*/
|
|
244
|
+
function isLatticeKind(kind) {
|
|
245
|
+
return LATTICE_KINDS.has(kind);
|
|
246
|
+
}
|
|
247
|
+
//#endregion
|
|
248
|
+
//#region src/layerItems.ts
|
|
249
|
+
/** The label of a corner: its own, else the lattice index on a board, else the id. */
|
|
250
|
+
function cornerLabel(corner, lattice) {
|
|
251
|
+
if (corner.label !== void 0) return corner.label;
|
|
252
|
+
if (lattice && Number.isInteger(corner.i) && Number.isInteger(corner.j)) return `${corner.i},${corner.j}`;
|
|
253
|
+
return String(corner.id);
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* The nodes of the corner layer.
|
|
257
|
+
*
|
|
258
|
+
* A corner with an `angle` is a `directed` marker (its edge directions, one or two), any other a
|
|
259
|
+
* `plus`. On a board kind the lattice indices are kept, so `GridLayer` joins neighbours; on any
|
|
260
|
+
* other kind they are `NaN`, which `latticeEdges` reads as "not part of a lattice", so no edge is
|
|
261
|
+
* drawn.
|
|
262
|
+
*
|
|
263
|
+
* @param detection - The detection.
|
|
264
|
+
* @returns One node per corner, in order.
|
|
265
|
+
*/
|
|
266
|
+
function cornerNodes(detection) {
|
|
267
|
+
const lattice = isLatticeKind(detection.kind);
|
|
268
|
+
return (detection.corners ?? []).map((corner) => {
|
|
269
|
+
const node = {
|
|
270
|
+
id: corner.id,
|
|
271
|
+
x: corner.x,
|
|
272
|
+
y: corner.y,
|
|
273
|
+
i: lattice && Number.isInteger(corner.i) ? corner.i : NaN,
|
|
274
|
+
j: lattice && Number.isInteger(corner.j) ? corner.j : NaN,
|
|
275
|
+
label: cornerLabel(corner, lattice)
|
|
276
|
+
};
|
|
277
|
+
if (corner.angle !== void 0) {
|
|
278
|
+
node.kind = "directed";
|
|
279
|
+
node.angle = corner.angle;
|
|
280
|
+
if (corner.angle2 !== void 0) node.angle2 = corner.angle2;
|
|
281
|
+
}
|
|
282
|
+
return node;
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* The regions of the marker layer: each marker's quad, labelled with its text or its id.
|
|
287
|
+
*
|
|
288
|
+
* @param detection - The detection.
|
|
289
|
+
* @returns One area per marker with a usable quad.
|
|
290
|
+
*/
|
|
291
|
+
function markerAreas(detection) {
|
|
292
|
+
return markerPolygons(detection.markers ?? []).map((polygon) => ({
|
|
293
|
+
id: polygon.id,
|
|
294
|
+
points: polygon.points,
|
|
295
|
+
label: polygon.label ?? String(polygon.id)
|
|
296
|
+
}));
|
|
297
|
+
}
|
|
298
|
+
/** The label of a circle: its own, else `(i, j)` when it matched a cell. */
|
|
299
|
+
function circleLabel(circle) {
|
|
300
|
+
if (circle.label !== void 0) return circle.label;
|
|
301
|
+
return Number.isInteger(circle.i) && Number.isInteger(circle.j) ? `(${circle.i}, ${circle.j})` : void 0;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* The points of the circle layer. Polarity picks the glyph: `circle-white` is a hollow ring,
|
|
305
|
+
* `circle-black` a ring with a dot.
|
|
306
|
+
*
|
|
307
|
+
* @param detection - The detection.
|
|
308
|
+
* @returns One point per circle.
|
|
309
|
+
*/
|
|
310
|
+
function circlePoints(detection) {
|
|
311
|
+
return (detection.circles ?? []).map((circle) => {
|
|
312
|
+
const item = {
|
|
313
|
+
id: circle.id,
|
|
314
|
+
x: circle.x,
|
|
315
|
+
y: circle.y,
|
|
316
|
+
kind: `circle-${circle.polarity}`
|
|
317
|
+
};
|
|
318
|
+
const label = circleLabel(circle);
|
|
319
|
+
if (label !== void 0) item.label = label;
|
|
320
|
+
return item;
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* The points of the ring layer: a `plus` at each centre, picked anywhere inside the outer ellipse
|
|
325
|
+
* (`pickRadius` is the larger semi-axis), labelled with the ring's text or its id.
|
|
326
|
+
*
|
|
327
|
+
* @param rings - The rings.
|
|
328
|
+
* @returns One point per ring.
|
|
329
|
+
*/
|
|
330
|
+
function ringCentres(rings) {
|
|
331
|
+
return rings.map((ring) => ({
|
|
332
|
+
id: ring.id,
|
|
333
|
+
x: ring.x,
|
|
334
|
+
y: ring.y,
|
|
335
|
+
kind: "plus",
|
|
336
|
+
pickRadius: Math.max(ring.outer.rx, ring.outer.ry),
|
|
337
|
+
label: ring.label ?? String(ring.id)
|
|
338
|
+
}));
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* The ellipses of the rings' two edges. The outer is `feature`, the inner `model` (a fitted
|
|
342
|
+
* second edge), and the smaller inner one is told from the outer by size as well as by colour.
|
|
343
|
+
*
|
|
344
|
+
* @param rings - The rings.
|
|
345
|
+
* @returns The outer ellipses, and the inner ones of the rings that have one.
|
|
346
|
+
*/
|
|
347
|
+
function ringEllipses(rings) {
|
|
348
|
+
const outer = [];
|
|
349
|
+
const inner = [];
|
|
350
|
+
for (const ring of rings) {
|
|
351
|
+
outer.push({
|
|
352
|
+
id: ring.id,
|
|
353
|
+
x: ring.x,
|
|
354
|
+
y: ring.y,
|
|
355
|
+
rx: ring.outer.rx,
|
|
356
|
+
ry: ring.outer.ry,
|
|
357
|
+
angle: ring.outer.angle,
|
|
358
|
+
role: "feature"
|
|
359
|
+
});
|
|
360
|
+
if (ring.inner) inner.push({
|
|
361
|
+
id: ring.id,
|
|
362
|
+
x: ring.x,
|
|
363
|
+
y: ring.y,
|
|
364
|
+
rx: ring.inner.rx,
|
|
365
|
+
ry: ring.inner.ry,
|
|
366
|
+
angle: ring.inner.angle,
|
|
367
|
+
role: "model"
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
return {
|
|
371
|
+
outer,
|
|
372
|
+
inner
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
/** Opacity of a dot at confidence 0 and at confidence 1: a weak bit is visible, a sure one is solid. */
|
|
376
|
+
const BIT_OPACITY_MIN = .35;
|
|
377
|
+
/**
|
|
378
|
+
* The dots of the edge-bit layer: bit 1 is a solid `feature` circle, bit 0 a dashed `model`
|
|
379
|
+
* one, and the opacity runs from 35 % at confidence 0 to 100 % at confidence 1.
|
|
380
|
+
*
|
|
381
|
+
* @param bits - The edge bits.
|
|
382
|
+
* @returns One ellipse (a circle) per bit.
|
|
383
|
+
*/
|
|
384
|
+
function edgeBitEllipses(bits) {
|
|
385
|
+
return bits.map((bit) => ({
|
|
386
|
+
id: bit.id,
|
|
387
|
+
x: bit.x,
|
|
388
|
+
y: bit.y,
|
|
389
|
+
rx: bit.radius,
|
|
390
|
+
ry: bit.radius,
|
|
391
|
+
role: bit.bit === 1 ? "feature" : "model",
|
|
392
|
+
dashed: bit.bit === 0,
|
|
393
|
+
opacity: BIT_OPACITY_MIN + .65 * Math.min(1, Math.max(0, bit.confidence))
|
|
394
|
+
}));
|
|
395
|
+
}
|
|
396
|
+
//#endregion
|
|
397
|
+
//#region src/TargetOverlay.tsx
|
|
398
|
+
/**
|
|
399
|
+
* A calibration-target detection over the image: one component for a chessboard, ChArUco,
|
|
400
|
+
* marker board, PuzzleBoard, ring grid or a loose set of corners.
|
|
401
|
+
*
|
|
402
|
+
* It replaces the per-board overlays of vitavision (`ChessboardOverlay`, `CharucoOverlay`,
|
|
403
|
+
* `MarkerboardOverlay`, `PuzzleboardOverlay`) and the glyph components behind them. Each part of
|
|
404
|
+
* a `TargetDetection` goes to the stage2d layer that fits it, so a detection of thousands of
|
|
405
|
+
* items is a few dozen DOM nodes (ADR-0004), and the pointer is resolved by the stage's hit-test
|
|
406
|
+
* registry, not the DOM:
|
|
407
|
+
*
|
|
408
|
+
* | Part | Layer | Glyph (visual-language §5) |
|
|
409
|
+
* |---|---|---|
|
|
410
|
+
* | `corners` | `GridLayer` | plus; edge directions when `angle` is set; lattice edges and `i,j` labels |
|
|
411
|
+
* | `markers` | `AreaSet` | quad outline and 12 % fill, corner 0 ticked, id inside |
|
|
412
|
+
* | `circles` | `PointSet` | ring; a centre dot marks black |
|
|
413
|
+
* | `rings` | `PointSet` + `EllipseSet` | fitted outer and inner ellipse, plus at the centre |
|
|
414
|
+
* | `edgeBits` | `EllipseSet` | dot on the edge: solid for 1, dashed for 0, opacity from confidence |
|
|
415
|
+
*/
|
|
416
|
+
/** The ids of each part, to route a hovered or selected id to the layers that have it. */
|
|
417
|
+
function idsByPart(detection) {
|
|
418
|
+
const ids = (items) => new Set((items ?? []).map((item) => item.id));
|
|
419
|
+
return {
|
|
420
|
+
corner: ids(detection.corners),
|
|
421
|
+
marker: ids(detection.markers),
|
|
422
|
+
circle: ids(detection.circles),
|
|
423
|
+
ring: ids(detection.rings)
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
/** The `dimmed` prop as the predicate the layers take, or `undefined` for none. */
|
|
427
|
+
function dimPredicate(dimmed) {
|
|
428
|
+
if (dimmed === void 0 || dimmed === false) return void 0;
|
|
429
|
+
if (dimmed === true) return () => true;
|
|
430
|
+
if (typeof dimmed === "function") return dimmed;
|
|
431
|
+
const set = new Set(dimmed);
|
|
432
|
+
return (id) => set.has(id);
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* A detected calibration target inside an `ImageStage`.
|
|
436
|
+
*
|
|
437
|
+
* - **Batched**: one path per appearance in each layer, picking through spatial indices. Labels
|
|
438
|
+
* appear only where items are 24 screen px apart, at most 200 at a time.
|
|
439
|
+
* - **States**: hover 2 px, selected 2.5 px in the selection colour with a ring, dimmed at 35 %.
|
|
440
|
+
* - **Colour**: roles only (`feature`, `model`, `structure`, `selection`). Scores are never drawn
|
|
441
|
+
* as red, amber or green; polarity and bits are carried by shape (hollow or dotted, solid or
|
|
442
|
+
* dashed), not by hue.
|
|
443
|
+
* - **Callbacks** report `{ id, part }` with the id from the detection. A press and a hover move
|
|
444
|
+
* between layers (a corner, then the marker under it) without a flicker: the change is reported
|
|
445
|
+
* once.
|
|
446
|
+
*
|
|
447
|
+
* The layers it renders carry their own accessible names ("Corners: 54 points, 0 selected").
|
|
448
|
+
*/
|
|
449
|
+
function TargetOverlay({ detection, showEdges = true, showLabels = true, hoveredId, selectedIds, onHoverChange, onItemPress, dimmed, layerIdPrefix }) {
|
|
450
|
+
const nodes = useMemo(() => cornerNodes(detection), [detection]);
|
|
451
|
+
const areas = useMemo(() => markerAreas(detection), [detection]);
|
|
452
|
+
const circles = useMemo(() => circlePoints(detection), [detection]);
|
|
453
|
+
const rings = detection.rings;
|
|
454
|
+
const ringPoints = useMemo(() => ringCentres(rings ?? []), [rings]);
|
|
455
|
+
const ringShapes = useMemo(() => ringEllipses(rings ?? []), [rings]);
|
|
456
|
+
const bits = useMemo(() => edgeBitEllipses(detection.edgeBits ?? []), [detection.edgeBits]);
|
|
457
|
+
const ids = useMemo(() => idsByPart(detection), [detection]);
|
|
458
|
+
const selectedSet = useMemo(() => new Set(selectedIds ?? []), [selectedIds]);
|
|
459
|
+
const selected = useMemo(() => {
|
|
460
|
+
const pick = (part) => new Set([...selectedSet].filter((id) => ids[part].has(id)));
|
|
461
|
+
return {
|
|
462
|
+
corner: pick("corner"),
|
|
463
|
+
marker: pick("marker"),
|
|
464
|
+
circle: pick("circle"),
|
|
465
|
+
ring: pick("ring")
|
|
466
|
+
};
|
|
467
|
+
}, [selectedSet, ids]);
|
|
468
|
+
const isDimmed = useMemo(() => dimPredicate(dimmed), [dimmed]);
|
|
469
|
+
const [ownHover, setOwnHover] = useState(null);
|
|
470
|
+
const hoverRef = useRef(null);
|
|
471
|
+
const reportedRef = useRef(null);
|
|
472
|
+
const scheduledRef = useRef(false);
|
|
473
|
+
const onHoverChangeRef = useRef(onHoverChange);
|
|
474
|
+
useLayoutEffect(() => {
|
|
475
|
+
onHoverChangeRef.current = onHoverChange;
|
|
476
|
+
});
|
|
477
|
+
const hoverFrom = useCallback((part) => (id) => {
|
|
478
|
+
if (id !== null) hoverRef.current = {
|
|
479
|
+
id,
|
|
480
|
+
part
|
|
481
|
+
};
|
|
482
|
+
else if (hoverRef.current?.part === part) hoverRef.current = null;
|
|
483
|
+
setOwnHover(hoverRef.current);
|
|
484
|
+
if (scheduledRef.current) return;
|
|
485
|
+
scheduledRef.current = true;
|
|
486
|
+
queueMicrotask(() => {
|
|
487
|
+
scheduledRef.current = false;
|
|
488
|
+
const now = hoverRef.current;
|
|
489
|
+
const before = reportedRef.current;
|
|
490
|
+
if (now?.id === before?.id && now?.part === before?.part) return;
|
|
491
|
+
reportedRef.current = now;
|
|
492
|
+
onHoverChangeRef.current?.(now);
|
|
493
|
+
});
|
|
494
|
+
}, []);
|
|
495
|
+
const hoverOf = (part) => {
|
|
496
|
+
if (hoveredId !== void 0) return hoveredId !== null && ids[part].has(hoveredId) ? hoveredId : null;
|
|
497
|
+
return ownHover?.part === part ? ownHover.id : null;
|
|
498
|
+
};
|
|
499
|
+
const pressFrom = (part) => onItemPress ? (id, event) => onItemPress({
|
|
500
|
+
id,
|
|
501
|
+
part
|
|
502
|
+
}, event) : void 0;
|
|
503
|
+
const layerId = (name) => layerIdPrefix === void 0 ? void 0 : `${layerIdPrefix}-${name}`;
|
|
504
|
+
const hasCorners = nodes.length > 0;
|
|
505
|
+
const hasMarkers = areas.length > 0;
|
|
506
|
+
const hasCircles = circles.length > 0;
|
|
507
|
+
const hasRings = ringPoints.length > 0;
|
|
508
|
+
const edgeStyle = { visible: showEdges };
|
|
509
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
510
|
+
hasMarkers && /* @__PURE__ */ jsx(AreaSet, {
|
|
511
|
+
items: areas,
|
|
512
|
+
firstVertexTick: true,
|
|
513
|
+
labels: showLabels,
|
|
514
|
+
hoveredId: hoverOf("marker"),
|
|
515
|
+
selectedIds: selected.marker,
|
|
516
|
+
dimmed: isDimmed,
|
|
517
|
+
onHoverChange: hoverFrom("marker"),
|
|
518
|
+
onItemPress: pressFrom("marker"),
|
|
519
|
+
layerId: layerId("markers"),
|
|
520
|
+
label: "Markers"
|
|
521
|
+
}),
|
|
522
|
+
bits.length > 0 && /* @__PURE__ */ jsx(EllipseSet$1, {
|
|
523
|
+
items: bits,
|
|
524
|
+
minRadius: 3,
|
|
525
|
+
dimmed: isDimmed,
|
|
526
|
+
pickable: false
|
|
527
|
+
}),
|
|
528
|
+
hasRings && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
529
|
+
/* @__PURE__ */ jsx(EllipseSet$1, {
|
|
530
|
+
items: ringShapes.outer,
|
|
531
|
+
hoveredId: hoverOf("ring"),
|
|
532
|
+
selectedIds: selected.ring,
|
|
533
|
+
dimmed: isDimmed,
|
|
534
|
+
pickable: false
|
|
535
|
+
}),
|
|
536
|
+
ringShapes.inner.length > 0 && /* @__PURE__ */ jsx(EllipseSet$1, {
|
|
537
|
+
items: ringShapes.inner,
|
|
538
|
+
role: "model",
|
|
539
|
+
hoveredId: hoverOf("ring"),
|
|
540
|
+
selectedIds: selected.ring,
|
|
541
|
+
dimmed: isDimmed,
|
|
542
|
+
pickable: false
|
|
543
|
+
}),
|
|
544
|
+
/* @__PURE__ */ jsx(PointSet, {
|
|
545
|
+
items: ringPoints,
|
|
546
|
+
kind: "plus",
|
|
547
|
+
markers: TARGET_MARKERS,
|
|
548
|
+
labels: showLabels,
|
|
549
|
+
hoveredId: hoverOf("ring"),
|
|
550
|
+
selectedIds: selected.ring,
|
|
551
|
+
dimmed: isDimmed,
|
|
552
|
+
onHoverChange: hoverFrom("ring"),
|
|
553
|
+
onItemPress: pressFrom("ring"),
|
|
554
|
+
layerId: layerId("rings"),
|
|
555
|
+
label: "Rings"
|
|
556
|
+
})
|
|
557
|
+
] }),
|
|
558
|
+
hasCorners && /* @__PURE__ */ jsx(GridLayer, {
|
|
559
|
+
nodes,
|
|
560
|
+
markers: TARGET_MARKERS,
|
|
561
|
+
edges: {
|
|
562
|
+
i: edgeStyle,
|
|
563
|
+
j: {
|
|
564
|
+
...edgeStyle,
|
|
565
|
+
dashed: true
|
|
566
|
+
}
|
|
567
|
+
},
|
|
568
|
+
labels: showLabels,
|
|
569
|
+
hoveredId: hoverOf("corner"),
|
|
570
|
+
selectedIds: selected.corner,
|
|
571
|
+
dimmed: isDimmed,
|
|
572
|
+
onHoverChange: hoverFrom("corner"),
|
|
573
|
+
onItemPress: pressFrom("corner"),
|
|
574
|
+
layerId: layerId("corners"),
|
|
575
|
+
label: "Corners"
|
|
576
|
+
}),
|
|
577
|
+
hasCircles && /* @__PURE__ */ jsx(PointSet, {
|
|
578
|
+
items: circles,
|
|
579
|
+
markers: TARGET_MARKERS,
|
|
580
|
+
labels: showLabels,
|
|
581
|
+
hoveredId: hoverOf("circle"),
|
|
582
|
+
selectedIds: selected.circle,
|
|
583
|
+
dimmed: isDimmed,
|
|
584
|
+
onHoverChange: hoverFrom("circle"),
|
|
585
|
+
onItemPress: pressFrom("circle"),
|
|
586
|
+
layerId: layerId("circles"),
|
|
587
|
+
label: "Circles"
|
|
588
|
+
})
|
|
589
|
+
] });
|
|
590
|
+
}
|
|
591
|
+
//#endregion
|
|
592
|
+
//#region src/puzzleboard.ts
|
|
593
|
+
/**
|
|
594
|
+
* PuzzleBoard edge bits: from a decoder's observed edges to the dots the overlay draws.
|
|
595
|
+
*
|
|
596
|
+
* A PuzzleBoard encodes its position in the bumps along the edges between corners. The decoder
|
|
597
|
+
* reports each observed edge in the image's own local frame (`row`, `col`), and the alignment
|
|
598
|
+
* that maps that frame into the board's master frame, which is periodic with period 501. The
|
|
599
|
+
* corners are labelled with master indices, so an edge's two corners are found by mapping its
|
|
600
|
+
* local end points through the alignment, modulo the period. This is the arithmetic of
|
|
601
|
+
* vitavision's `PuzzleboardOverlay.buildEdgeMarkers`, as a pure function over structural types,
|
|
602
|
+
* so no detector package is imported.
|
|
603
|
+
*
|
|
604
|
+
* Pure: no React, no DOM.
|
|
605
|
+
*/
|
|
606
|
+
/** The period of the PuzzleBoard master pattern. */
|
|
607
|
+
const PUZZLEBOARD_PERIOD = 501;
|
|
608
|
+
/** `value` modulo `period`, in `[0, period)` for any sign. */
|
|
609
|
+
function wrap(value, period) {
|
|
610
|
+
return (value % period + period) % period;
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* The edge bits of a PuzzleBoard decode, placed on the image.
|
|
614
|
+
*
|
|
615
|
+
* An edge is dropped when either of its corners was not detected. Each dot sits at the midpoint
|
|
616
|
+
* of its edge with a radius of a quarter of the edge's length (the bump is about half an edge
|
|
617
|
+
* wide); its opacity is the app's to take from `confidence`.
|
|
618
|
+
*
|
|
619
|
+
* @param edges - The observed edges.
|
|
620
|
+
* @param corners - The labelled corners, whose `i` and `j` are **master** indices.
|
|
621
|
+
* @param alignment - The local-to-master map.
|
|
622
|
+
* @param period - The master pattern's period. Defaults to 501.
|
|
623
|
+
* @returns One `TargetEdgeBit` per edge whose corners were both found, in input order. Empty when there is no alignment.
|
|
624
|
+
*/
|
|
625
|
+
function edgeBitsFromPuzzleboard(edges, corners, alignment, period = 501) {
|
|
626
|
+
if (!alignment || edges.length === 0 || corners.length === 0) return [];
|
|
627
|
+
const byMaster = /* @__PURE__ */ new Map();
|
|
628
|
+
for (const corner of corners) if (Number.isInteger(corner.i) && Number.isInteger(corner.j)) byMaster.set(gridKey(corner.i, corner.j), corner);
|
|
629
|
+
const { a, b, c, d } = alignment.transform;
|
|
630
|
+
const [tx, ty] = alignment.translation;
|
|
631
|
+
const toMaster = (i, j) => gridKey(wrap(a * i + b * j + tx, period), wrap(c * i + d * j + ty, period));
|
|
632
|
+
const out = [];
|
|
633
|
+
edges.forEach((edge, index) => {
|
|
634
|
+
const from = byMaster.get(toMaster(edge.col, edge.row));
|
|
635
|
+
const to = byMaster.get(edge.orientation === "horizontal" ? toMaster(edge.col + 1, edge.row) : toMaster(edge.col, edge.row + 1));
|
|
636
|
+
if (!from || !to) return;
|
|
637
|
+
out.push({
|
|
638
|
+
id: `${edge.orientation}-${edge.row}-${edge.col}-${index}`,
|
|
639
|
+
x: .5 * (from.x + to.x),
|
|
640
|
+
y: .5 * (from.y + to.y),
|
|
641
|
+
radius: .25 * Math.hypot(to.x - from.x, to.y - from.y),
|
|
642
|
+
bit: edge.bit,
|
|
643
|
+
confidence: Math.min(1, Math.max(0, edge.confidence))
|
|
644
|
+
});
|
|
645
|
+
});
|
|
646
|
+
return out;
|
|
647
|
+
}
|
|
648
|
+
//#endregion
|
|
649
|
+
export { EllipseSet, PUZZLEBOARD_PERIOD, TARGET_KINDS, TARGET_MARKERS, TargetOverlay, circlePoints, cornerGrid, cornerNodes, edgeBitEllipses, edgeBitsFromPuzzleboard, ellipsePath, gridEdges, gridKey, idByGrid, isLatticeKind, markerAreas, markerPolygons, ringCentres, ringEllipses };
|
|
650
|
+
|
|
651
|
+
//# sourceMappingURL=index.js.map
|