@kubohiroya/turbowarp-camera-calibration 0.9.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.9.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.9.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.9.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.9.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
@@ -268,6 +268,16 @@ Returns which way the board still has to be turned: top-near, top-far, left-near
268
268
  | Opcode | `cameraCalibrationTiltDirection` |
269
269
  | `CAMERA_ID` | String, default: `default` |
270
270
 
271
+ ### `camera calibration progress [CAMERA_ID]`
272
+
273
+ Returns how far the session has come, from 0 to 16. Four gates of four steps: enough views to solve from, enough tilt among them, enough views to hold some back, and the answer holding up on the views it was not fitted to. Counted in order and stopped at the first unfinished gate, so the number says which gate is being worked on as well as how far into it. Never falls. Sixteen because that is enough to sound like progress and few enough to hear as distinct.
274
+
275
+ | Property | Value |
276
+ |---|---|
277
+ | Type | Reporter |
278
+ | Opcode | `cameraCalibrationProgress` |
279
+ | `CAMERA_ID` | String, default: `default` |
280
+
271
281
  ### `solve calibration for camera [CAMERA_ID]`
272
282
 
273
283
  Solves the intrinsic matrix and distortion coefficients from at least eight accepted samples, then releases the camera lease. No external pose is produced.
@@ -128,6 +128,16 @@
128
128
  "defaultValue": "default"
129
129
  } }
130
130
  },
131
+ {
132
+ "opcode": "cameraCalibrationProgress",
133
+ "blockType": "REPORTER",
134
+ "text": "camera calibration progress [CAMERA_ID]",
135
+ "description": "Returns how far the session has come, from 0 to 16. Four gates of four steps: enough views to solve from, enough tilt among them, enough views to hold some back, and the answer holding up on the views it was not fitted to. Counted in order and stopped at the first unfinished gate, so the number says which gate is being worked on as well as how far into it. Never falls. Sixteen because that is enough to sound like progress and few enough to hear as distinct.",
136
+ "arguments": { "CAMERA_ID": {
137
+ "type": "STRING",
138
+ "defaultValue": "default"
139
+ } }
140
+ },
131
141
  {
132
142
  "opcode": "solveCameraCalibration",
133
143
  "blockType": "COMMAND",
@@ -962,6 +972,7 @@
962
972
  this.automatic = false;
963
973
  this.guidanceCode = "";
964
974
  this.noveltyNow = 0;
975
+ this.progressReached = 0;
965
976
  this.solvedFrom = 0;
966
977
  }
967
978
  async start(options) {
@@ -1020,6 +1031,7 @@
1020
1031
  this.lease = lease;
1021
1032
  this.samples = [];
1022
1033
  this.solvedFrom = 0;
1034
+ this.progressReached = 0;
1023
1035
  this.sessionSampleCount = 0;
1024
1036
  this.sampleQuality = 0;
1025
1037
  this.reprojectionError = 0;
@@ -1068,6 +1080,34 @@
1068
1080
  novelty() {
1069
1081
  return this.noveltyNow;
1070
1082
  }
1083
+ /**
1084
+ * How far the session has come, in steps of the sixteen.
1085
+ *
1086
+ * Counted gate by gate in the order they have to be passed, stopping at the
1087
+ * first one not finished -- so the number says which gate is being worked on
1088
+ * as well as how far into it. Held at its highest: a set's spread can fall
1089
+ * when a view is replaced, and a count that went backwards would tell the
1090
+ * operator they had broken something they had not.
1091
+ */
1092
+ progress() {
1093
+ if (this.calibrationState === "solved") return 16;
1094
+ const session = this.session;
1095
+ if (!session) return this.progressReached;
1096
+ const shares = [
1097
+ this.samples.length / MINIMUM_SAMPLES,
1098
+ poseSpread(this.samples, session.board) / MINIMUM_POSE_SPREAD,
1099
+ (this.samples.length - MINIMUM_SAMPLES) / 4,
1100
+ this.holdoutCount > 0 && this.holdoutError > 0 ? session.maximumReprojectionErrorPx / this.holdoutError : 0
1101
+ ];
1102
+ let steps = 0;
1103
+ for (const share of shares) {
1104
+ const passed = Math.max(0, Math.min(4, Math.floor(share * 4)));
1105
+ steps += passed;
1106
+ if (passed < 4) break;
1107
+ }
1108
+ this.progressReached = Math.max(this.progressReached, steps);
1109
+ return this.progressReached;
1110
+ }
1071
1111
  /** Which way to turn the board next. Empty outside a live session. */
1072
1112
  tiltDirection() {
1073
1113
  if (!this.session || !this.lease) return "";
@@ -1664,6 +1704,9 @@
1664
1704
  tiltDirection(cameraId) {
1665
1705
  return this.existing(cameraId)?.tiltDirection() ?? "";
1666
1706
  }
1707
+ progress(cameraId) {
1708
+ return this.existing(cameraId)?.progress() ?? 0;
1709
+ }
1667
1710
  cancel(cameraId) {
1668
1711
  return this.existing(cameraId)?.cancel() ?? Promise.resolve();
1669
1712
  }
@@ -2207,9 +2250,9 @@
2207
2250
  var runtimeCapabilityKey = "kubohiroyaCameraCalibrationCapability";
2208
2251
  function createRuntimeCapability(host) {
2209
2252
  const capability = {
2210
- version: 3,
2253
+ version: 4,
2211
2254
  requireVersion(version) {
2212
- if (!Number.isInteger(version) || version < 1 || version > 3) throw new Error(`Unsupported Camera Calibration runtime capability version: ${version}; this build provides 3.`);
2255
+ if (!Number.isInteger(version) || version < 1 || version > 4) throw new Error(`Unsupported Camera Calibration runtime capability version: ${version}; this build provides 4.`);
2213
2256
  return capability;
2214
2257
  },
2215
2258
  start: (options) => host.start(options),
@@ -2219,6 +2262,7 @@
2219
2262
  guidance: (cameraId) => host.guidance(cameraId),
2220
2263
  novelty: (cameraId) => host.novelty(cameraId),
2221
2264
  tiltDirection: (cameraId) => host.tiltDirection(cameraId),
2265
+ progress: (cameraId) => host.progress(cameraId),
2222
2266
  solve: (cameraId) => host.solve(cameraId),
2223
2267
  publish: (cameraId) => host.publish(cameraId),
2224
2268
  cancel: (cameraId) => host.cancel(cameraId),
@@ -2326,6 +2370,9 @@
2326
2370
  cameraCalibrationTiltDirection(args) {
2327
2371
  return this.controller.tiltDirection(normalizeId(args.CAMERA_ID));
2328
2372
  }
2373
+ cameraCalibrationProgress(args) {
2374
+ return this.controller.progress(normalizeId(args.CAMERA_ID));
2375
+ }
2329
2376
  async solveCameraCalibration(args) {
2330
2377
  await this.controller.solve(normalizeId(args.CAMERA_ID));
2331
2378
  }
@@ -2414,6 +2461,7 @@
2414
2461
  guidance: (cameraId) => this.controller.guidance(normalizeId(cameraId)),
2415
2462
  novelty: (cameraId) => this.controller.novelty(normalizeId(cameraId)),
2416
2463
  tiltDirection: (cameraId) => this.controller.tiltDirection(normalizeId(cameraId)),
2464
+ progress: (cameraId) => this.controller.progress(normalizeId(cameraId)),
2417
2465
  solve: (cameraId) => this.controller.solve(normalizeId(cameraId)),
2418
2466
  publish: (cameraId) => this.controller.publishProfile(normalizeId(cameraId)),
2419
2467
  cancel: (cameraId) => this.controller.cancel(normalizeId(cameraId)),
@@ -131,6 +131,16 @@
131
131
  }
132
132
  ]
133
133
  },
134
+ {
135
+ "opcode": "cameraCalibrationProgress",
136
+ "blockType": "REPORTER",
137
+ "arguments": [
138
+ {
139
+ "id": "CAMERA_ID",
140
+ "type": "STRING"
141
+ }
142
+ ]
143
+ },
134
144
  {
135
145
  "opcode": "cameraCalibrationReady",
136
146
  "blockType": "BOOLEAN",
@@ -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
+ }
@@ -60,6 +60,30 @@ export type CalibrationGuidance = ''
60
60
  | 'solving'
61
61
  /** Solved and validated. Nothing further is needed. */
62
62
  | 'complete';
63
+ /**
64
+ * How far a session has come, counted in sixteen steps.
65
+ *
66
+ * Four gates of four steps, in the order they have to be passed. A gate is
67
+ * what actually stops the session finishing, so this is not a guess at how
68
+ * long is left: it is how much of what is required has been done.
69
+ *
70
+ * | steps | gate | done when |
71
+ * |---|---|---|
72
+ * | 0-3 | enough views to solve from at all | the minimum sample count |
73
+ * | 4-7 | enough tilt to solve from | the minimum pose spread |
74
+ * | 8-11 | enough views to hold some back | the automatic completion count |
75
+ * | 12-15 | the answer holding up on the views it was not fitted to | the hold-out error under the limit |
76
+ * | 16 | solved | -- |
77
+ *
78
+ * Counted in order and stopped at the first unfinished gate, so the step
79
+ * number says which gate is being worked on as well as how far into it. And
80
+ * never decreasing: the spread of a set can fall when a view is replaced, and
81
+ * an operator hearing the count go backwards would reasonably think they had
82
+ * broken something.
83
+ */
84
+ export declare const CALIBRATION_GATES = 4;
85
+ export declare const CALIBRATION_STEPS_PER_GATE = 4;
86
+ export declare const CALIBRATION_PROGRESS_STEPS: number;
63
87
  /**
64
88
  * Which way the board still has to be turned.
65
89
  *
@@ -1 +1,24 @@
1
- export {};
1
+ /**
2
+ * How far a session has come, counted in sixteen steps.
3
+ *
4
+ * Four gates of four steps, in the order they have to be passed. A gate is
5
+ * what actually stops the session finishing, so this is not a guess at how
6
+ * long is left: it is how much of what is required has been done.
7
+ *
8
+ * | steps | gate | done when |
9
+ * |---|---|---|
10
+ * | 0-3 | enough views to solve from at all | the minimum sample count |
11
+ * | 4-7 | enough tilt to solve from | the minimum pose spread |
12
+ * | 8-11 | enough views to hold some back | the automatic completion count |
13
+ * | 12-15 | the answer holding up on the views it was not fitted to | the hold-out error under the limit |
14
+ * | 16 | solved | -- |
15
+ *
16
+ * Counted in order and stopped at the first unfinished gate, so the step
17
+ * number says which gate is being worked on as well as how far into it. And
18
+ * never decreasing: the spread of a set can fall when a view is replaced, and
19
+ * an operator hearing the count go backwards would reasonably think they had
20
+ * broken something.
21
+ */
22
+ export const CALIBRATION_GATES = 4;
23
+ export const CALIBRATION_STEPS_PER_GATE = 4;
24
+ export const CALIBRATION_PROGRESS_STEPS = CALIBRATION_GATES * CALIBRATION_STEPS_PER_GATE;
@@ -14,7 +14,7 @@
14
14
  */
15
15
  import type { BoardPoseOptions, CalibrationErrorCode, CalibrationGuidance, CalibrationStartOptions, CalibrationState, TiltDirection } from './calibration/contract.js';
16
16
  export declare const runtimeCapabilityKey = "kubohiroyaCameraCalibrationCapability";
17
- export declare const runtimeCapabilityVersion: 3;
17
+ export declare const runtimeCapabilityVersion: 4;
18
18
  export interface CameraCalibrationCapabilityV1 {
19
19
  readonly version: typeof runtimeCapabilityVersion;
20
20
  requireVersion(version: number): CameraCalibrationCapabilityV1;
@@ -71,6 +71,18 @@ export interface CameraCalibrationCapabilityV1 {
71
71
  * becomes one they can carry out.
72
72
  */
73
73
  tiltDirection(cameraId: string): TiltDirection;
74
+ /**
75
+ * How far the session has come, 0 to 16. Since v4.
76
+ *
77
+ * Four gates of four steps, counted in the order they have to be passed and
78
+ * stopped at the first unfinished one -- so the number says which gate is
79
+ * being worked on as well as how far into it. Never falls.
80
+ *
81
+ * For telling the operator where they are without their reading anything:
82
+ * sixteen steps is enough to sound like progress and few enough to be heard
83
+ * as distinct. `CALIBRATION_PROGRESS_STEPS` names the total.
84
+ */
85
+ progress(cameraId: string): number;
74
86
  /** Solves from the accepted samples and releases the camera. */
75
87
  solve(cameraId: string): Promise<void>;
76
88
  /** Hands the solved profile to Camera Source, which owns the profile contract. */
@@ -1,5 +1,5 @@
1
1
  export const runtimeCapabilityKey = 'kubohiroyaCameraCalibrationCapability';
2
- export const runtimeCapabilityVersion = 3;
2
+ export const runtimeCapabilityVersion = 4;
3
3
  export function createRuntimeCapability(host) {
4
4
  const capability = {
5
5
  version: runtimeCapabilityVersion,
@@ -22,6 +22,7 @@ export function createRuntimeCapability(host) {
22
22
  guidance: (cameraId) => host.guidance(cameraId),
23
23
  novelty: (cameraId) => host.novelty(cameraId),
24
24
  tiltDirection: (cameraId) => host.tiltDirection(cameraId),
25
+ progress: (cameraId) => host.progress(cameraId),
25
26
  solve: (cameraId) => host.solve(cameraId),
26
27
  publish: (cameraId) => host.publish(cameraId),
27
28
  cancel: (cameraId) => host.cancel(cameraId),
package/lib/runtime.d.ts CHANGED
@@ -15,8 +15,18 @@
15
15
  * every attempt to publish a profile failed with a version mismatch that had
16
16
  * nothing to do with versions.
17
17
  */
18
+ export { CALIBRATION_GATES, CALIBRATION_PROGRESS_STEPS, CALIBRATION_STEPS_PER_GATE } from './calibration/contract.js';
18
19
  export type { CalibrationErrorCode, CalibrationGuidance, CalibrationStartOptions, CalibrationState, TiltDirection } from './calibration/contract.js';
19
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';
20
30
  export { createRuntimeCapability, runtimeCapabilityKey, runtimeCapabilityVersion, type CameraCalibrationCapabilityV1 } from './runtime-capability.js';
21
31
  import { type CameraCalibrationCapabilityV1 } from './runtime-capability.js';
22
32
  /** The extension ID, as it appears in opcodes and in a project's extension list. */
package/lib/runtime.js CHANGED
@@ -15,6 +15,16 @@
15
15
  * every attempt to publish a profile failed with a version mismatch that had
16
16
  * nothing to do with versions.
17
17
  */
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';
18
28
  export { createRuntimeCapability, runtimeCapabilityKey, runtimeCapabilityVersion } from './runtime-capability.js';
19
29
  import { runtimeCapabilityKey } from './runtime-capability.js';
20
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.9.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",
@@ -121,6 +121,18 @@
121
121
  }
122
122
  }
123
123
  },
124
+ {
125
+ "opcode": "cameraCalibrationProgress",
126
+ "blockType": "REPORTER",
127
+ "text": "camera calibration progress [CAMERA_ID]",
128
+ "description": "Returns how far the session has come, from 0 to 16. Four gates of four steps: enough views to solve from, enough tilt among them, enough views to hold some back, and the answer holding up on the views it was not fitted to. Counted in order and stopped at the first unfinished gate, so the number says which gate is being worked on as well as how far into it. Never falls. Sixteen because that is enough to sound like progress and few enough to hear as distinct.",
129
+ "arguments": {
130
+ "CAMERA_ID": {
131
+ "type": "STRING",
132
+ "defaultValue": "default"
133
+ }
134
+ }
135
+ },
124
136
  {
125
137
  "opcode": "solveCameraCalibration",
126
138
  "blockType": "COMMAND",
@@ -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
+ }
@@ -97,6 +97,31 @@ export type CalibrationGuidance =
97
97
  /** Solved and validated. Nothing further is needed. */
98
98
  | 'complete';
99
99
 
100
+ /**
101
+ * How far a session has come, counted in sixteen steps.
102
+ *
103
+ * Four gates of four steps, in the order they have to be passed. A gate is
104
+ * what actually stops the session finishing, so this is not a guess at how
105
+ * long is left: it is how much of what is required has been done.
106
+ *
107
+ * | steps | gate | done when |
108
+ * |---|---|---|
109
+ * | 0-3 | enough views to solve from at all | the minimum sample count |
110
+ * | 4-7 | enough tilt to solve from | the minimum pose spread |
111
+ * | 8-11 | enough views to hold some back | the automatic completion count |
112
+ * | 12-15 | the answer holding up on the views it was not fitted to | the hold-out error under the limit |
113
+ * | 16 | solved | -- |
114
+ *
115
+ * Counted in order and stopped at the first unfinished gate, so the step
116
+ * number says which gate is being worked on as well as how far into it. And
117
+ * never decreasing: the spread of a set can fall when a view is replaced, and
118
+ * an operator hearing the count go backwards would reasonably think they had
119
+ * broken something.
120
+ */
121
+ export const CALIBRATION_GATES = 4;
122
+ export const CALIBRATION_STEPS_PER_GATE = 4;
123
+ export const CALIBRATION_PROGRESS_STEPS = CALIBRATION_GATES * CALIBRATION_STEPS_PER_GATE;
124
+
100
125
  /**
101
126
  * Which way the board still has to be turned.
102
127
  *
@@ -23,6 +23,10 @@ export type {
23
23
  CalibrationState,
24
24
  TiltDirection
25
25
  } from './contract.js';
26
+ import {
27
+ CALIBRATION_PROGRESS_STEPS,
28
+ CALIBRATION_STEPS_PER_GATE
29
+ } from './contract.js';
26
30
  import type {
27
31
  BoardPoseOptions,
28
32
  CalibrationErrorCode,
@@ -177,6 +181,8 @@ class CameraCalibration {
177
181
  private guidanceCode: CalibrationGuidance = '';
178
182
  /** How much the view the shutter last looked at would add. */
179
183
  private noveltyNow = 0;
184
+ /** The furthest along this session has been, in steps. Never falls. */
185
+ private progressReached = 0;
180
186
  /** Sample count the last automatic solve was started from. */
181
187
  private solvedFrom = 0;
182
188
 
@@ -252,6 +258,7 @@ class CameraCalibration {
252
258
  this.lease = lease;
253
259
  this.samples = [];
254
260
  this.solvedFrom = 0;
261
+ this.progressReached = 0;
255
262
  this.sessionSampleCount = 0;
256
263
  this.sampleQuality = 0;
257
264
  this.reprojectionError = 0;
@@ -308,6 +315,47 @@ class CameraCalibration {
308
315
  return this.noveltyNow;
309
316
  }
310
317
 
318
+ /**
319
+ * How far the session has come, in steps of the sixteen.
320
+ *
321
+ * Counted gate by gate in the order they have to be passed, stopping at the
322
+ * first one not finished -- so the number says which gate is being worked on
323
+ * as well as how far into it. Held at its highest: a set's spread can fall
324
+ * when a view is replaced, and a count that went backwards would tell the
325
+ * operator they had broken something they had not.
326
+ */
327
+ public progress(): number {
328
+ if (this.calibrationState === 'solved') return CALIBRATION_PROGRESS_STEPS;
329
+ const session = this.session;
330
+ if (!session) return this.progressReached;
331
+ const shares = [
332
+ // Enough views to solve from at all.
333
+ this.samples.length / MINIMUM_SAMPLES,
334
+ // Enough tilt among them to solve from. Nothing else can substitute:
335
+ // this is the gate a set of forty look-alikes never passes.
336
+ poseSpread(this.samples, session.board) / MINIMUM_POSE_SPREAD,
337
+ // Enough views that some can be held back from the fit.
338
+ (this.samples.length - MINIMUM_SAMPLES) /
339
+ (AUTOMATIC_COMPLETE_SAMPLES - MINIMUM_SAMPLES),
340
+ // The answer holding up on the views it was not fitted to. Before a
341
+ // solve has run there is nothing to hold up, and that is not progress.
342
+ this.holdoutCount > 0 && this.holdoutError > 0
343
+ ? session.maximumReprojectionErrorPx / this.holdoutError
344
+ : 0
345
+ ];
346
+ let steps = 0;
347
+ for (const share of shares) {
348
+ const passed = Math.max(
349
+ 0,
350
+ Math.min(CALIBRATION_STEPS_PER_GATE, Math.floor(share * CALIBRATION_STEPS_PER_GATE))
351
+ );
352
+ steps += passed;
353
+ if (passed < CALIBRATION_STEPS_PER_GATE) break;
354
+ }
355
+ this.progressReached = Math.max(this.progressReached, steps);
356
+ return this.progressReached;
357
+ }
358
+
311
359
  /** Which way to turn the board next. Empty outside a live session. */
312
360
  public tiltDirection(): TiltDirection {
313
361
  if (!this.session || !this.lease) return '';
@@ -1150,6 +1198,10 @@ export class CameraCalibrationController {
1150
1198
  return this.existing(cameraId)?.tiltDirection() ?? '';
1151
1199
  }
1152
1200
 
1201
+ public progress(cameraId: string): number {
1202
+ return this.existing(cameraId)?.progress() ?? 0;
1203
+ }
1204
+
1153
1205
  public cancel(cameraId: string): Promise<void> {
1154
1206
  return this.existing(cameraId)?.cancel() ?? Promise.resolve();
1155
1207
  }
package/src/extension.ts CHANGED
@@ -133,6 +133,10 @@ export class CameraCalibrationExtension implements TurboWarpExtension {
133
133
  return this.controller.tiltDirection(normalizeId(args.CAMERA_ID));
134
134
  }
135
135
 
136
+ public cameraCalibrationProgress(args: {CAMERA_ID: unknown}): number {
137
+ return this.controller.progress(normalizeId(args.CAMERA_ID));
138
+ }
139
+
136
140
  public async solveCameraCalibration(args: {CAMERA_ID: unknown}): Promise<void> {
137
141
  await this.controller.solve(normalizeId(args.CAMERA_ID));
138
142
  }
@@ -272,6 +276,7 @@ export class CameraCalibrationExtension implements TurboWarpExtension {
272
276
  guidance: (cameraId) => this.controller.guidance(normalizeId(cameraId)),
273
277
  novelty: (cameraId) => this.controller.novelty(normalizeId(cameraId)),
274
278
  tiltDirection: (cameraId) => this.controller.tiltDirection(normalizeId(cameraId)),
279
+ progress: (cameraId) => this.controller.progress(normalizeId(cameraId)),
275
280
  solve: (cameraId) => this.controller.solve(normalizeId(cameraId)),
276
281
  publish: (cameraId) => this.controller.publishProfile(normalizeId(cameraId)),
277
282
  cancel: (cameraId) => this.controller.cancel(normalizeId(cameraId)),
@@ -22,7 +22,7 @@ import type {
22
22
  } from './calibration/contract.js';
23
23
 
24
24
  export const runtimeCapabilityKey = 'kubohiroyaCameraCalibrationCapability';
25
- export const runtimeCapabilityVersion = 3 as const;
25
+ export const runtimeCapabilityVersion = 4 as const;
26
26
 
27
27
  export interface CameraCalibrationCapabilityV1 {
28
28
  readonly version: typeof runtimeCapabilityVersion;
@@ -81,6 +81,18 @@ export interface CameraCalibrationCapabilityV1 {
81
81
  * becomes one they can carry out.
82
82
  */
83
83
  tiltDirection(cameraId: string): TiltDirection;
84
+ /**
85
+ * How far the session has come, 0 to 16. Since v4.
86
+ *
87
+ * Four gates of four steps, counted in the order they have to be passed and
88
+ * stopped at the first unfinished one -- so the number says which gate is
89
+ * being worked on as well as how far into it. Never falls.
90
+ *
91
+ * For telling the operator where they are without their reading anything:
92
+ * sixteen steps is enough to sound like progress and few enough to be heard
93
+ * as distinct. `CALIBRATION_PROGRESS_STEPS` names the total.
94
+ */
95
+ progress(cameraId: string): number;
84
96
  /** Solves from the accepted samples and releases the camera. */
85
97
  solve(cameraId: string): Promise<void>;
86
98
  /** Hands the solved profile to Camera Source, which owns the profile contract. */
@@ -180,6 +192,7 @@ export function createRuntimeCapability(
180
192
  guidance: (cameraId) => host.guidance(cameraId),
181
193
  novelty: (cameraId) => host.novelty(cameraId),
182
194
  tiltDirection: (cameraId) => host.tiltDirection(cameraId),
195
+ progress: (cameraId) => host.progress(cameraId),
183
196
  solve: (cameraId) => host.solve(cameraId),
184
197
  publish: (cameraId) => host.publish(cameraId),
185
198
  cancel: (cameraId) => host.cancel(cameraId),
package/src/runtime.ts CHANGED
@@ -16,6 +16,12 @@
16
16
  * nothing to do with versions.
17
17
  */
18
18
 
19
+ export {
20
+ CALIBRATION_GATES,
21
+ CALIBRATION_PROGRESS_STEPS,
22
+ CALIBRATION_STEPS_PER_GATE
23
+ } from './calibration/contract.js';
24
+
19
25
  export type {
20
26
  CalibrationErrorCode,
21
27
  CalibrationGuidance,
@@ -26,6 +32,27 @@ export type {
26
32
 
27
33
  export type {CalibrationBoard} from './calibration/types.js';
28
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
+
29
56
  export {
30
57
  createRuntimeCapability,
31
58
  runtimeCapabilityKey,