@kubohiroya/turbowarp-camera-calibration 0.10.0 → 0.11.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.ja.md CHANGED
@@ -101,7 +101,7 @@ bundleは約11 MBあります。
101
101
  検証済みのversionをexact pinします。
102
102
 
103
103
  ```bash
104
- pnpm add --save-exact @kubohiroya/turbowarp-camera-calibration@0.10.0
104
+ pnpm add --save-exact @kubohiroya/turbowarp-camera-calibration@0.11.0
105
105
  ```
106
106
 
107
107
  standalone bundle:
@@ -113,7 +113,7 @@ node_modules/@kubohiroya/turbowarp-camera-calibration/dist/camera-calibration.js
113
113
  version固定CDN URL:
114
114
 
115
115
  ```text
116
- https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-camera-calibration@0.10.0/dist/camera-calibration.js
116
+ https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-camera-calibration@0.11.0/dist/camera-calibration.js
117
117
  ```
118
118
 
119
119
  ## クイックスタート
package/README.md CHANGED
@@ -102,7 +102,7 @@ therefore about 11 MB.
102
102
  Install an exact version that you have reviewed:
103
103
 
104
104
  ```bash
105
- pnpm add --save-exact @kubohiroya/turbowarp-camera-calibration@0.10.0
105
+ pnpm add --save-exact @kubohiroya/turbowarp-camera-calibration@0.11.0
106
106
  ```
107
107
 
108
108
  Load the standalone bundle from:
@@ -114,7 +114,7 @@ node_modules/@kubohiroya/turbowarp-camera-calibration/dist/camera-calibration.js
114
114
  A version-pinned CDN URL is:
115
115
 
116
116
  ```text
117
- https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-camera-calibration@0.10.0/dist/camera-calibration.js
117
+ https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-camera-calibration@0.11.0/dist/camera-calibration.js
118
118
  ```
119
119
 
120
120
  ## Quick start
@@ -0,0 +1,28 @@
1
+ /**
2
+ * The marker patterns of OpenCV's `DICT_4X4_50`.
3
+ *
4
+ * Fifty markers of sixteen bits: a hundred bytes, which is why the board can be
5
+ * drawn in the page at all. The alternative was shipping OpenCV to the display
6
+ * side to ask it, and OpenCV is eleven megabytes.
7
+ *
8
+ * Extracted from `cv.getPredefinedDictionary(cv.DICT_4X4_50)` rather than
9
+ * transcribed, and checked against a board OpenCV drew itself: the first ten
10
+ * markers of a 10x7 board read back bit for bit. A dictionary is an arbitrary
11
+ * table, so a copy made any other way would be a copy nothing could check.
12
+ *
13
+ * Each entry is the 4x4 data grid, row-major, most significant bit at the top
14
+ * left, a set bit meaning white.
15
+ */
16
+ export declare const DICT_4X4_50: readonly number[];
17
+ /** Data cells across a marker, before its border. */
18
+ export declare const MARKER_BITS = 4;
19
+ /** Cells across a whole marker: the data grid inside a one-cell black border. */
20
+ export declare const MARKER_CELLS: number;
21
+ /**
22
+ * Which cells of a marker are white, as a `MARKER_CELLS` square.
23
+ *
24
+ * The border ring is always black -- that is what the detector finds first, and
25
+ * it is why a marker has to sit inside a light square with white left around
26
+ * it.
27
+ */
28
+ export declare function markerCells(id: number): boolean[][];
@@ -0,0 +1,58 @@
1
+ // SPDX-License-Identifier: MPL-2.0
2
+ /**
3
+ * The marker patterns of OpenCV's `DICT_4X4_50`.
4
+ *
5
+ * Fifty markers of sixteen bits: a hundred bytes, which is why the board can be
6
+ * drawn in the page at all. The alternative was shipping OpenCV to the display
7
+ * side to ask it, and OpenCV is eleven megabytes.
8
+ *
9
+ * Extracted from `cv.getPredefinedDictionary(cv.DICT_4X4_50)` rather than
10
+ * transcribed, and checked against a board OpenCV drew itself: the first ten
11
+ * markers of a 10x7 board read back bit for bit. A dictionary is an arbitrary
12
+ * table, so a copy made any other way would be a copy nothing could check.
13
+ *
14
+ * Each entry is the 4x4 data grid, row-major, most significant bit at the top
15
+ * left, a set bit meaning white.
16
+ */
17
+ export const DICT_4X4_50 = [
18
+ 0xb532, 0x0f9a, 0x332d, 0x9946, 0x549e, 0x79cd, 0x9e2e, 0xc4f2, 0xfeda,
19
+ 0xcf56, 0xf991, 0x11a7, 0x0eb7, 0x2a0f, 0x24b1, 0x263e, 0x4665, 0x6600,
20
+ 0x6c5e, 0x76af, 0x868b, 0xb02b, 0xccd5, 0xdd82, 0xfe47, 0x9471, 0xace4,
21
+ 0xa554, 0x2123, 0x346f, 0x4415, 0x57b2, 0x9ecf, 0xf0cb, 0x08ae, 0x0929,
22
+ 0x1875, 0x04ff, 0x0df6, 0x1c5a, 0x1718, 0x2a28, 0x328c, 0x38b2, 0x24e8,
23
+ 0x2eeb, 0x2d3f, 0x4b64, 0x502e, 0x5013,
24
+ ];
25
+ /** Data cells across a marker, before its border. */
26
+ export const MARKER_BITS = 4;
27
+ /** Cells across a whole marker: the data grid inside a one-cell black border. */
28
+ export const MARKER_CELLS = MARKER_BITS + 2;
29
+ /**
30
+ * Which cells of a marker are white, as a `MARKER_CELLS` square.
31
+ *
32
+ * The border ring is always black -- that is what the detector finds first, and
33
+ * it is why a marker has to sit inside a light square with white left around
34
+ * it.
35
+ */
36
+ export function markerCells(id) {
37
+ const bits = DICT_4X4_50[id];
38
+ if (bits === undefined)
39
+ throw new Error(`DICT_4X4_50 has no marker ${id}.`);
40
+ const cells = [];
41
+ for (let row = 0; row < MARKER_CELLS; row += 1) {
42
+ const line = [];
43
+ for (let column = 0; column < MARKER_CELLS; column += 1) {
44
+ const border = row === 0 ||
45
+ column === 0 ||
46
+ row === MARKER_CELLS - 1 ||
47
+ column === MARKER_CELLS - 1;
48
+ if (border) {
49
+ line.push(false);
50
+ continue;
51
+ }
52
+ const index = (row - 1) * MARKER_BITS + (column - 1);
53
+ line.push(((bits >> (MARKER_BITS * MARKER_BITS - 1 - index)) & 1) === 1);
54
+ }
55
+ cells.push(line);
56
+ }
57
+ return cells;
58
+ }
@@ -0,0 +1,100 @@
1
+ /**
2
+ * The ChArUco board this extension looks for, drawn.
3
+ *
4
+ * Here rather than in whatever app shows it, because the same numbers have to
5
+ * produce the drawing and the detector. Kept apart, the two agree until one of
6
+ * them changes, and the way that failure arrives is an operator holding a
7
+ * board being told to show one.
8
+ *
9
+ * It was apart: an app drew the board and this extension looked for it, and a
10
+ * browser check ran across the two repositories to confirm they still matched.
11
+ * That check was worth writing, and it is worth not needing.
12
+ *
13
+ * Geometry lives here rather than in the markup so that the printed sheet, the
14
+ * full-screen view and the saved file are the same board.
15
+ *
16
+ * A marker sits in every light square. That is what lets a view that runs off
17
+ * the edge of the frame still be used: the markers name the corners around
18
+ * them, where a plain chessboard has to be found whole because nothing in it
19
+ * says which corner is which. The corners near the frame edge are the ones that
20
+ * decide the principal point and the distortion, so being able to keep those
21
+ * views matters more than it sounds.
22
+ */
23
+ export interface BoardSpec {
24
+ /** Inner corners across, which is one fewer than the squares across. */
25
+ readonly columns: number;
26
+ /** Inner corners down. */
27
+ readonly rows: number;
28
+ }
29
+ export interface BoardLayout extends BoardSpec {
30
+ /** Side of one square, in the units the board is drawn in. */
31
+ readonly cell: number;
32
+ readonly boardWidth: number;
33
+ readonly boardHeight: number;
34
+ /** Blank margin on each side. Never less than one square. */
35
+ readonly quietX: number;
36
+ readonly quietY: number;
37
+ }
38
+ /**
39
+ * The boards offered for display, smallest grid last.
40
+ *
41
+ * More inner corners constrain the solve better; fewer survive a camera that is
42
+ * far away or short of resolution, where a 40-unit square lands on too few
43
+ * pixels to locate its corners.
44
+ */
45
+ /**
46
+ * How much of a light square the marker fills.
47
+ *
48
+ * White has to remain around it, or the detector cannot separate the marker's
49
+ * black border from the dark squares the light square touches.
50
+ */
51
+ export declare const MARKER_RATIO = 0.72;
52
+ export declare const BOARDS: readonly BoardSpec[];
53
+ /**
54
+ * Fits a board into a box with a blank margin around it.
55
+ *
56
+ * The margin is at least one square on every side. OpenCV's chessboard finder
57
+ * needs the outer squares to be bounded by background it can trace; a board run
58
+ * to the edge of the frame loses its outermost corners, and the pattern is
59
+ * refused as incomplete rather than found with fewer points.
60
+ *
61
+ * The box is the drawing's own coordinate space. Nothing here knows the size of
62
+ * the screen it will end up on: the board is drawn once and scaled uniformly,
63
+ * because scaling one axis alone would turn the squares into rectangles and the
64
+ * calibration would solve for a lens that is not there.
65
+ */
66
+ /**
67
+ * The sheet `patternFile` writes: A4, landscape, in millimetres.
68
+ *
69
+ * Here rather than in the writer because the printed square size is derived
70
+ * from it, and a page size that moved without the derivation moving with it
71
+ * would hand the calibration a measurement of a sheet nobody printed.
72
+ */
73
+ export declare const PRINT_WIDTH_MM = 297;
74
+ export declare const PRINT_HEIGHT_MM = 210;
75
+ /**
76
+ * How large one square comes out on that sheet, printed at full size.
77
+ *
78
+ * The SVG scales uniformly and letterboxes the rest, so the scale is whichever
79
+ * axis runs out first -- the same on both, which is what keeps the squares
80
+ * square.
81
+ *
82
+ * Nominal, not measured. It is what the sheet should be if the printer was
83
+ * told not to resize; a ruler laid on the paper is the only thing that knows
84
+ * whether it was. Intrinsic calibration does not care either way -- the fit is
85
+ * unchanged by scale -- but a board pose is metric, and its distance is wrong
86
+ * by exactly however much this is wrong.
87
+ */
88
+ export declare function printedCellMillimetres(board: BoardSpec): number;
89
+ export declare function layout(board: BoardSpec, width?: number, height?: number): BoardLayout;
90
+ /** The costume name a board is switched to by. */
91
+ export declare function boardName(board: BoardSpec): string;
92
+ /**
93
+ * Draws the board as an SVG.
94
+ *
95
+ * Only the dark squares are emitted, over a white ground. Painting both colours
96
+ * would put a seam between every pair of rectangles, and a renderer that leaves
97
+ * a half-unit of background showing along it gives the corner finder a line of
98
+ * light where the board has an edge.
99
+ */
100
+ export declare function patternSvg(board: BoardSpec, width?: number, height?: number): string;
@@ -0,0 +1,169 @@
1
+ // SPDX-License-Identifier: MPL-2.0
2
+ /**
3
+ * The ChArUco board this extension looks for, drawn.
4
+ *
5
+ * Here rather than in whatever app shows it, because the same numbers have to
6
+ * produce the drawing and the detector. Kept apart, the two agree until one of
7
+ * them changes, and the way that failure arrives is an operator holding a
8
+ * board being told to show one.
9
+ *
10
+ * It was apart: an app drew the board and this extension looked for it, and a
11
+ * browser check ran across the two repositories to confirm they still matched.
12
+ * That check was worth writing, and it is worth not needing.
13
+ *
14
+ * Geometry lives here rather than in the markup so that the printed sheet, the
15
+ * full-screen view and the saved file are the same board.
16
+ *
17
+ * A marker sits in every light square. That is what lets a view that runs off
18
+ * the edge of the frame still be used: the markers name the corners around
19
+ * them, where a plain chessboard has to be found whole because nothing in it
20
+ * says which corner is which. The corners near the frame edge are the ones that
21
+ * decide the principal point and the distortion, so being able to keep those
22
+ * views matters more than it sounds.
23
+ */
24
+ import { MARKER_CELLS, markerCells } from './aruco.js';
25
+ /**
26
+ * The boards offered for display, smallest grid last.
27
+ *
28
+ * More inner corners constrain the solve better; fewer survive a camera that is
29
+ * far away or short of resolution, where a 40-unit square lands on too few
30
+ * pixels to locate its corners.
31
+ */
32
+ /**
33
+ * How much of a light square the marker fills.
34
+ *
35
+ * White has to remain around it, or the detector cannot separate the marker's
36
+ * black border from the dark squares the light square touches.
37
+ */
38
+ export const MARKER_RATIO = 0.72;
39
+ export const BOARDS = [
40
+ { columns: 9, rows: 6 },
41
+ { columns: 7, rows: 5 },
42
+ { columns: 5, rows: 4 },
43
+ ];
44
+ /**
45
+ * Fits a board into a box with a blank margin around it.
46
+ *
47
+ * The margin is at least one square on every side. OpenCV's chessboard finder
48
+ * needs the outer squares to be bounded by background it can trace; a board run
49
+ * to the edge of the frame loses its outermost corners, and the pattern is
50
+ * refused as incomplete rather than found with fewer points.
51
+ *
52
+ * The box is the drawing's own coordinate space. Nothing here knows the size of
53
+ * the screen it will end up on: the board is drawn once and scaled uniformly,
54
+ * because scaling one axis alone would turn the squares into rectangles and the
55
+ * calibration would solve for a lens that is not there.
56
+ */
57
+ /**
58
+ * The sheet `patternFile` writes: A4, landscape, in millimetres.
59
+ *
60
+ * Here rather than in the writer because the printed square size is derived
61
+ * from it, and a page size that moved without the derivation moving with it
62
+ * would hand the calibration a measurement of a sheet nobody printed.
63
+ */
64
+ export const PRINT_WIDTH_MM = 297;
65
+ export const PRINT_HEIGHT_MM = 210;
66
+ /**
67
+ * How large one square comes out on that sheet, printed at full size.
68
+ *
69
+ * The SVG scales uniformly and letterboxes the rest, so the scale is whichever
70
+ * axis runs out first -- the same on both, which is what keeps the squares
71
+ * square.
72
+ *
73
+ * Nominal, not measured. It is what the sheet should be if the printer was
74
+ * told not to resize; a ruler laid on the paper is the only thing that knows
75
+ * whether it was. Intrinsic calibration does not care either way -- the fit is
76
+ * unchanged by scale -- but a board pose is metric, and its distance is wrong
77
+ * by exactly however much this is wrong.
78
+ */
79
+ export function printedCellMillimetres(board) {
80
+ const drawn = layout(board);
81
+ const scale = Math.min(PRINT_WIDTH_MM / 1000, PRINT_HEIGHT_MM / 750);
82
+ return drawn.cell * scale;
83
+ }
84
+ export function layout(board, width = 1000, height = 750) {
85
+ const squaresX = board.columns + 1;
86
+ const squaresY = board.rows + 1;
87
+ // Two extra squares in each direction: one of margin on each side.
88
+ const cell = Math.floor(Math.min(width / (squaresX + 2), height / (squaresY + 2)));
89
+ const boardWidth = cell * squaresX;
90
+ const boardHeight = cell * squaresY;
91
+ return {
92
+ ...board,
93
+ cell,
94
+ boardWidth,
95
+ boardHeight,
96
+ quietX: (width - boardWidth) / 2,
97
+ quietY: (height - boardHeight) / 2,
98
+ };
99
+ }
100
+ /** The costume name a board is switched to by. */
101
+ export function boardName(board) {
102
+ return `board-${board.columns}x${board.rows}`;
103
+ }
104
+ /**
105
+ * Draws the board as an SVG.
106
+ *
107
+ * Only the dark squares are emitted, over a white ground. Painting both colours
108
+ * would put a seam between every pair of rectangles, and a renderer that leaves
109
+ * a half-unit of background showing along it gives the corner finder a line of
110
+ * light where the board has an edge.
111
+ */
112
+ export function patternSvg(board, width = 1000, height = 750) {
113
+ const { cell, quietX, quietY } = layout(board, width, height);
114
+ const squares = [];
115
+ const markers = [];
116
+ // Markers go in the light squares, scanned row by row, numbered from zero --
117
+ // the order OpenCV assigns when it builds the same board, checked against a
118
+ // board it drew itself rather than assumed.
119
+ let markerId = 0;
120
+ for (let row = 0; row <= board.rows; row += 1) {
121
+ for (let column = 0; column <= board.columns; column += 1) {
122
+ const x = quietX + column * cell;
123
+ const y = quietY + row * cell;
124
+ if ((row + column) % 2 === 0) {
125
+ squares.push(`<rect x="${x}" y="${y}" width="${cell}" height="${cell}"/>`);
126
+ continue;
127
+ }
128
+ markers.push(markerSvg(markerId, x, y, cell));
129
+ markerId += 1;
130
+ }
131
+ }
132
+ // preserveAspectRatio keeps the scaling uniform and letterboxes the rest. A
133
+ // viewer that stretched one axis would hand the solver a grid whose squares
134
+ // are not square, and the bias that follows does not show up as reprojection
135
+ // error -- it is simply a wrong calibration that looks like a good one.
136
+ return (`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}"` +
137
+ ` preserveAspectRatio="xMidYMid meet" width="100%" height="100%">` +
138
+ `<rect width="${width}" height="${height}" fill="#ffffff"/>` +
139
+ `<g fill="#000000">${squares.join('')}${markers.join('')}</g>` +
140
+ '</svg>');
141
+ }
142
+ /**
143
+ * One marker, centred in its light square.
144
+ *
145
+ * Only the black cells are drawn, over the white square already beneath. The
146
+ * border ring is part of the marker and is what the detector finds first, so it
147
+ * is drawn as a single rectangle with the white data cells punched out of it --
148
+ * fewer shapes than one rectangle per cell, and no seams between them for a
149
+ * renderer to leave a hairline in.
150
+ */
151
+ function markerSvg(id, squareX, squareY, cell) {
152
+ const side = cell * MARKER_RATIO;
153
+ const originX = squareX + (cell - side) / 2;
154
+ const originY = squareY + (cell - side) / 2;
155
+ const step = side / MARKER_CELLS;
156
+ const cells = markerCells(id);
157
+ const dark = [];
158
+ for (let row = 0; row < MARKER_CELLS; row += 1) {
159
+ for (let column = 0; column < MARKER_CELLS; column += 1) {
160
+ if (cells[row]?.[column])
161
+ continue;
162
+ dark.push(`<rect x="${round(originX + column * step)}" y="${round(originY + row * step)}" width="${round(step)}" height="${round(step)}"/>`);
163
+ }
164
+ }
165
+ return dark.join('');
166
+ }
167
+ function round(value) {
168
+ return (Math.round(value * 1000) / 1000).toString();
169
+ }
package/lib/runtime.d.ts CHANGED
@@ -18,6 +18,15 @@
18
18
  export { CALIBRATION_GATES, CALIBRATION_PROGRESS_STEPS, CALIBRATION_STEPS_PER_GATE } from './calibration/contract.js';
19
19
  export type { CalibrationErrorCode, CalibrationGuidance, CalibrationStartOptions, CalibrationState, TiltDirection } from './calibration/contract.js';
20
20
  export type { CalibrationBoard } from './calibration/types.js';
21
+ /**
22
+ * The board, drawn.
23
+ *
24
+ * Published so an app that shows or prints one is showing the board this
25
+ * extension looks for, rather than its own drawing of the same idea. Pure
26
+ * arithmetic and string building: nothing here reaches for a document, a
27
+ * camera, or OpenCV.
28
+ */
29
+ export { BOARDS, MARKER_RATIO, PRINT_HEIGHT_MM, PRINT_WIDTH_MM, boardName, layout, patternSvg, printedCellMillimetres, type BoardLayout, type BoardSpec } from './board/pattern.js';
21
30
  export { createRuntimeCapability, runtimeCapabilityKey, runtimeCapabilityVersion, type CameraCalibrationCapabilityV1 } from './runtime-capability.js';
22
31
  import { type CameraCalibrationCapabilityV1 } from './runtime-capability.js';
23
32
  /** The extension ID, as it appears in opcodes and in a project's extension list. */
package/lib/runtime.js CHANGED
@@ -16,6 +16,15 @@
16
16
  * nothing to do with versions.
17
17
  */
18
18
  export { CALIBRATION_GATES, CALIBRATION_PROGRESS_STEPS, CALIBRATION_STEPS_PER_GATE } from './calibration/contract.js';
19
+ /**
20
+ * The board, drawn.
21
+ *
22
+ * Published so an app that shows or prints one is showing the board this
23
+ * extension looks for, rather than its own drawing of the same idea. Pure
24
+ * arithmetic and string building: nothing here reaches for a document, a
25
+ * camera, or OpenCV.
26
+ */
27
+ export { BOARDS, MARKER_RATIO, PRINT_HEIGHT_MM, PRINT_WIDTH_MM, boardName, layout, patternSvg, printedCellMillimetres } from './board/pattern.js';
19
28
  export { createRuntimeCapability, runtimeCapabilityKey, runtimeCapabilityVersion } from './runtime-capability.js';
20
29
  import { runtimeCapabilityKey } from './runtime-capability.js';
21
30
  /** The extension ID, as it appears in opcodes and in a project's extension list. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubohiroya/turbowarp-camera-calibration",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "A TurboWarp extension that calibrates a shared Camera Source camera and publishes its intrinsic calibration profile.",
5
5
  "author": "Hiroya Kubo <hiroya@cuc.ac.jp>",
6
6
  "license": "MPL-2.0",
@@ -0,0 +1,61 @@
1
+ // SPDX-License-Identifier: MPL-2.0
2
+ /**
3
+ * The marker patterns of OpenCV's `DICT_4X4_50`.
4
+ *
5
+ * Fifty markers of sixteen bits: a hundred bytes, which is why the board can be
6
+ * drawn in the page at all. The alternative was shipping OpenCV to the display
7
+ * side to ask it, and OpenCV is eleven megabytes.
8
+ *
9
+ * Extracted from `cv.getPredefinedDictionary(cv.DICT_4X4_50)` rather than
10
+ * transcribed, and checked against a board OpenCV drew itself: the first ten
11
+ * markers of a 10x7 board read back bit for bit. A dictionary is an arbitrary
12
+ * table, so a copy made any other way would be a copy nothing could check.
13
+ *
14
+ * Each entry is the 4x4 data grid, row-major, most significant bit at the top
15
+ * left, a set bit meaning white.
16
+ */
17
+ export const DICT_4X4_50: readonly number[] = [
18
+ 0xb532, 0x0f9a, 0x332d, 0x9946, 0x549e, 0x79cd, 0x9e2e, 0xc4f2, 0xfeda,
19
+ 0xcf56, 0xf991, 0x11a7, 0x0eb7, 0x2a0f, 0x24b1, 0x263e, 0x4665, 0x6600,
20
+ 0x6c5e, 0x76af, 0x868b, 0xb02b, 0xccd5, 0xdd82, 0xfe47, 0x9471, 0xace4,
21
+ 0xa554, 0x2123, 0x346f, 0x4415, 0x57b2, 0x9ecf, 0xf0cb, 0x08ae, 0x0929,
22
+ 0x1875, 0x04ff, 0x0df6, 0x1c5a, 0x1718, 0x2a28, 0x328c, 0x38b2, 0x24e8,
23
+ 0x2eeb, 0x2d3f, 0x4b64, 0x502e, 0x5013,
24
+ ];
25
+
26
+ /** Data cells across a marker, before its border. */
27
+ export const MARKER_BITS = 4;
28
+
29
+ /** Cells across a whole marker: the data grid inside a one-cell black border. */
30
+ export const MARKER_CELLS = MARKER_BITS + 2;
31
+
32
+ /**
33
+ * Which cells of a marker are white, as a `MARKER_CELLS` square.
34
+ *
35
+ * The border ring is always black -- that is what the detector finds first, and
36
+ * it is why a marker has to sit inside a light square with white left around
37
+ * it.
38
+ */
39
+ export function markerCells(id: number): boolean[][] {
40
+ const bits = DICT_4X4_50[id];
41
+ if (bits === undefined) throw new Error(`DICT_4X4_50 has no marker ${id}.`);
42
+ const cells: boolean[][] = [];
43
+ for (let row = 0; row < MARKER_CELLS; row += 1) {
44
+ const line: boolean[] = [];
45
+ for (let column = 0; column < MARKER_CELLS; column += 1) {
46
+ const border =
47
+ row === 0 ||
48
+ column === 0 ||
49
+ row === MARKER_CELLS - 1 ||
50
+ column === MARKER_CELLS - 1;
51
+ if (border) {
52
+ line.push(false);
53
+ continue;
54
+ }
55
+ const index = (row - 1) * MARKER_BITS + (column - 1);
56
+ line.push(((bits >> (MARKER_BITS * MARKER_BITS - 1 - index)) & 1) === 1);
57
+ }
58
+ cells.push(line);
59
+ }
60
+ return cells;
61
+ }
@@ -0,0 +1,216 @@
1
+ // SPDX-License-Identifier: MPL-2.0
2
+ /**
3
+ * The ChArUco board this extension looks for, drawn.
4
+ *
5
+ * Here rather than in whatever app shows it, because the same numbers have to
6
+ * produce the drawing and the detector. Kept apart, the two agree until one of
7
+ * them changes, and the way that failure arrives is an operator holding a
8
+ * board being told to show one.
9
+ *
10
+ * It was apart: an app drew the board and this extension looked for it, and a
11
+ * browser check ran across the two repositories to confirm they still matched.
12
+ * That check was worth writing, and it is worth not needing.
13
+ *
14
+ * Geometry lives here rather than in the markup so that the printed sheet, the
15
+ * full-screen view and the saved file are the same board.
16
+ *
17
+ * A marker sits in every light square. That is what lets a view that runs off
18
+ * the edge of the frame still be used: the markers name the corners around
19
+ * them, where a plain chessboard has to be found whole because nothing in it
20
+ * says which corner is which. The corners near the frame edge are the ones that
21
+ * decide the principal point and the distortion, so being able to keep those
22
+ * views matters more than it sounds.
23
+ */
24
+
25
+ import { MARKER_CELLS, markerCells } from './aruco.js';
26
+
27
+ export interface BoardSpec {
28
+ /** Inner corners across, which is one fewer than the squares across. */
29
+ readonly columns: number;
30
+ /** Inner corners down. */
31
+ readonly rows: number;
32
+ }
33
+
34
+ export interface BoardLayout extends BoardSpec {
35
+ /** Side of one square, in the units the board is drawn in. */
36
+ readonly cell: number;
37
+ readonly boardWidth: number;
38
+ readonly boardHeight: number;
39
+ /** Blank margin on each side. Never less than one square. */
40
+ readonly quietX: number;
41
+ readonly quietY: number;
42
+ }
43
+
44
+ /**
45
+ * The boards offered for display, smallest grid last.
46
+ *
47
+ * More inner corners constrain the solve better; fewer survive a camera that is
48
+ * far away or short of resolution, where a 40-unit square lands on too few
49
+ * pixels to locate its corners.
50
+ */
51
+ /**
52
+ * How much of a light square the marker fills.
53
+ *
54
+ * White has to remain around it, or the detector cannot separate the marker's
55
+ * black border from the dark squares the light square touches.
56
+ */
57
+ export const MARKER_RATIO = 0.72;
58
+
59
+ export const BOARDS: readonly BoardSpec[] = [
60
+ { columns: 9, rows: 6 },
61
+ { columns: 7, rows: 5 },
62
+ { columns: 5, rows: 4 },
63
+ ];
64
+
65
+ /**
66
+ * Fits a board into a box with a blank margin around it.
67
+ *
68
+ * The margin is at least one square on every side. OpenCV's chessboard finder
69
+ * needs the outer squares to be bounded by background it can trace; a board run
70
+ * to the edge of the frame loses its outermost corners, and the pattern is
71
+ * refused as incomplete rather than found with fewer points.
72
+ *
73
+ * The box is the drawing's own coordinate space. Nothing here knows the size of
74
+ * the screen it will end up on: the board is drawn once and scaled uniformly,
75
+ * because scaling one axis alone would turn the squares into rectangles and the
76
+ * calibration would solve for a lens that is not there.
77
+ */
78
+ /**
79
+ * The sheet `patternFile` writes: A4, landscape, in millimetres.
80
+ *
81
+ * Here rather than in the writer because the printed square size is derived
82
+ * from it, and a page size that moved without the derivation moving with it
83
+ * would hand the calibration a measurement of a sheet nobody printed.
84
+ */
85
+ export const PRINT_WIDTH_MM = 297;
86
+ export const PRINT_HEIGHT_MM = 210;
87
+
88
+ /**
89
+ * How large one square comes out on that sheet, printed at full size.
90
+ *
91
+ * The SVG scales uniformly and letterboxes the rest, so the scale is whichever
92
+ * axis runs out first -- the same on both, which is what keeps the squares
93
+ * square.
94
+ *
95
+ * Nominal, not measured. It is what the sheet should be if the printer was
96
+ * told not to resize; a ruler laid on the paper is the only thing that knows
97
+ * whether it was. Intrinsic calibration does not care either way -- the fit is
98
+ * unchanged by scale -- but a board pose is metric, and its distance is wrong
99
+ * by exactly however much this is wrong.
100
+ */
101
+ export function printedCellMillimetres(board: BoardSpec): number {
102
+ const drawn = layout(board);
103
+ const scale = Math.min(PRINT_WIDTH_MM / 1000, PRINT_HEIGHT_MM / 750);
104
+ return drawn.cell * scale;
105
+ }
106
+
107
+ export function layout(
108
+ board: BoardSpec,
109
+ width = 1000,
110
+ height = 750,
111
+ ): BoardLayout {
112
+ const squaresX = board.columns + 1;
113
+ const squaresY = board.rows + 1;
114
+ // Two extra squares in each direction: one of margin on each side.
115
+ const cell = Math.floor(
116
+ Math.min(width / (squaresX + 2), height / (squaresY + 2)),
117
+ );
118
+ const boardWidth = cell * squaresX;
119
+ const boardHeight = cell * squaresY;
120
+ return {
121
+ ...board,
122
+ cell,
123
+ boardWidth,
124
+ boardHeight,
125
+ quietX: (width - boardWidth) / 2,
126
+ quietY: (height - boardHeight) / 2,
127
+ };
128
+ }
129
+
130
+ /** The costume name a board is switched to by. */
131
+ export function boardName(board: BoardSpec): string {
132
+ return `board-${board.columns}x${board.rows}`;
133
+ }
134
+
135
+ /**
136
+ * Draws the board as an SVG.
137
+ *
138
+ * Only the dark squares are emitted, over a white ground. Painting both colours
139
+ * would put a seam between every pair of rectangles, and a renderer that leaves
140
+ * a half-unit of background showing along it gives the corner finder a line of
141
+ * light where the board has an edge.
142
+ */
143
+ export function patternSvg(
144
+ board: BoardSpec,
145
+ width = 1000,
146
+ height = 750,
147
+ ): string {
148
+ const { cell, quietX, quietY } = layout(board, width, height);
149
+ const squares: string[] = [];
150
+ const markers: string[] = [];
151
+ // Markers go in the light squares, scanned row by row, numbered from zero --
152
+ // the order OpenCV assigns when it builds the same board, checked against a
153
+ // board it drew itself rather than assumed.
154
+ let markerId = 0;
155
+ for (let row = 0; row <= board.rows; row += 1) {
156
+ for (let column = 0; column <= board.columns; column += 1) {
157
+ const x = quietX + column * cell;
158
+ const y = quietY + row * cell;
159
+ if ((row + column) % 2 === 0) {
160
+ squares.push(
161
+ `<rect x="${x}" y="${y}" width="${cell}" height="${cell}"/>`,
162
+ );
163
+ continue;
164
+ }
165
+ markers.push(markerSvg(markerId, x, y, cell));
166
+ markerId += 1;
167
+ }
168
+ }
169
+ // preserveAspectRatio keeps the scaling uniform and letterboxes the rest. A
170
+ // viewer that stretched one axis would hand the solver a grid whose squares
171
+ // are not square, and the bias that follows does not show up as reprojection
172
+ // error -- it is simply a wrong calibration that looks like a good one.
173
+ return (
174
+ `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}"` +
175
+ ` preserveAspectRatio="xMidYMid meet" width="100%" height="100%">` +
176
+ `<rect width="${width}" height="${height}" fill="#ffffff"/>` +
177
+ `<g fill="#000000">${squares.join('')}${markers.join('')}</g>` +
178
+ '</svg>'
179
+ );
180
+ }
181
+
182
+ /**
183
+ * One marker, centred in its light square.
184
+ *
185
+ * Only the black cells are drawn, over the white square already beneath. The
186
+ * border ring is part of the marker and is what the detector finds first, so it
187
+ * is drawn as a single rectangle with the white data cells punched out of it --
188
+ * fewer shapes than one rectangle per cell, and no seams between them for a
189
+ * renderer to leave a hairline in.
190
+ */
191
+ function markerSvg(
192
+ id: number,
193
+ squareX: number,
194
+ squareY: number,
195
+ cell: number,
196
+ ): string {
197
+ const side = cell * MARKER_RATIO;
198
+ const originX = squareX + (cell - side) / 2;
199
+ const originY = squareY + (cell - side) / 2;
200
+ const step = side / MARKER_CELLS;
201
+ const cells = markerCells(id);
202
+ const dark: string[] = [];
203
+ for (let row = 0; row < MARKER_CELLS; row += 1) {
204
+ for (let column = 0; column < MARKER_CELLS; column += 1) {
205
+ if (cells[row]?.[column]) continue;
206
+ dark.push(
207
+ `<rect x="${round(originX + column * step)}" y="${round(originY + row * step)}" width="${round(step)}" height="${round(step)}"/>`,
208
+ );
209
+ }
210
+ }
211
+ return dark.join('');
212
+ }
213
+
214
+ function round(value: number): string {
215
+ return (Math.round(value * 1000) / 1000).toString();
216
+ }
package/src/runtime.ts CHANGED
@@ -32,6 +32,27 @@ export type {
32
32
 
33
33
  export type {CalibrationBoard} from './calibration/types.js';
34
34
 
35
+ /**
36
+ * The board, drawn.
37
+ *
38
+ * Published so an app that shows or prints one is showing the board this
39
+ * extension looks for, rather than its own drawing of the same idea. Pure
40
+ * arithmetic and string building: nothing here reaches for a document, a
41
+ * camera, or OpenCV.
42
+ */
43
+ export {
44
+ BOARDS,
45
+ MARKER_RATIO,
46
+ PRINT_HEIGHT_MM,
47
+ PRINT_WIDTH_MM,
48
+ boardName,
49
+ layout,
50
+ patternSvg,
51
+ printedCellMillimetres,
52
+ type BoardLayout,
53
+ type BoardSpec
54
+ } from './board/pattern.js';
55
+
35
56
  export {
36
57
  createRuntimeCapability,
37
58
  runtimeCapabilityKey,