@rig-c/parts 2.40.0 → 2.42.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 +1 -0
- package/cli.ts +76 -20
- package/docs/AUTHORING.md +361 -58
- package/docs/STABILITY.md +136 -11
- package/package.json +4 -2
- package/src/behindseams.ts +622 -0
- package/src/build.ts +76 -9
- package/src/check.ts +157 -25
- package/src/checkrow.ts +21 -0
- package/src/config.ts +29 -3
- package/src/configrelocation.ts +25 -0
- package/src/configwrite.ts +20 -2
- package/src/depthworkflow.ts +3 -4
- package/src/instruments.ts +7 -3
- package/src/meshdraft.ts +10 -10
- package/src/motion.ts +26 -24
- package/src/remesh.ts +18 -12
- package/src/requirements.ts +30 -4
- package/src/retune.ts +8 -5
- package/src/rig.ts +25 -14
- package/src/runstamp.ts +54 -1
- package/tools/configwrite.ts +83 -1
- package/tools/edited-part-cli.ts +61 -0
|
@@ -0,0 +1,622 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { isAbsolute, join, relative, resolve } from 'node:path';
|
|
3
|
+
import type { Plate } from '@rig-c/core/plate';
|
|
4
|
+
import type { AttachmentRest, Frame } from '@rig-c/core/render-shared';
|
|
5
|
+
import { seamGridOf, type SeamCell, type SeamGrid } from '@rig-c/core/seam';
|
|
6
|
+
import { DRAWN_ALPHA } from '@rig-c/core/passthrough';
|
|
7
|
+
import { checkRow } from './checkrow.ts';
|
|
8
|
+
import { seamSourceAttachment, type MotionSeamPair } from './check.ts';
|
|
9
|
+
import type { Problem } from './errors.ts';
|
|
10
|
+
|
|
11
|
+
export const BEHIND_SEAM_LINE = 'BEHIND_PART_SEAMS';
|
|
12
|
+
export const UNDECLARED_BEHIND_SEAM_REASON = 'HOLE: no requirements.behind_seams declares an authored threshold T; not measured, never a pass';
|
|
13
|
+
export const BEHIND_SEAM_CODE = 'CHECK_BEHIND_PART_SEAMS';
|
|
14
|
+
export const BEHIND_SEAM_UNMEASURED_CODE = 'CHECK_BEHIND_PART_SEAMS_UNMEASURED';
|
|
15
|
+
|
|
16
|
+
export interface BehindSeams {
|
|
17
|
+
threshold?: number;
|
|
18
|
+
allowance?: number;
|
|
19
|
+
pairs?: Array<{ part: string; neighbour: string; threshold?: number; allowance?: number }>;
|
|
20
|
+
}
|
|
21
|
+
export type BehindSeamsApi = Pick<typeof import('@rig-c/core/render-shared'), 'projector' | 'pageFor' | 'rasterisePiece' | 'renderFrame'>;
|
|
22
|
+
export interface BehindSeamsStockPair {
|
|
23
|
+
pair: string;
|
|
24
|
+
frames: number;
|
|
25
|
+
drawn: number;
|
|
26
|
+
banded: number;
|
|
27
|
+
gapCovered: Array<{ index: number; time: number; cell: number; cells: readonly SeamCell[] }>;
|
|
28
|
+
}
|
|
29
|
+
export interface BehindSeamsFramesInput {
|
|
30
|
+
pairs: readonly MotionSeamPair[];
|
|
31
|
+
setup: Frame | null;
|
|
32
|
+
idle: readonly Frame[];
|
|
33
|
+
rest: readonly AttachmentRest[];
|
|
34
|
+
pages: Map<string, Plate>;
|
|
35
|
+
stock: readonly BehindSeamsStockPair[] | null;
|
|
36
|
+
fps: number;
|
|
37
|
+
}
|
|
38
|
+
export interface BehindSeamsSample {
|
|
39
|
+
x: number;
|
|
40
|
+
y: number;
|
|
41
|
+
rear_rgb: [number, number, number];
|
|
42
|
+
rear_weight: number;
|
|
43
|
+
contributors: Array<{ slot: string; weight: number }>;
|
|
44
|
+
distances: [number, number] | null;
|
|
45
|
+
counted: boolean;
|
|
46
|
+
}
|
|
47
|
+
export interface BehindSeamsCount {
|
|
48
|
+
status: 'PASS' | 'FAIL' | 'SKIP';
|
|
49
|
+
boundary_cells: number;
|
|
50
|
+
geometric_px: number;
|
|
51
|
+
raw_px: number;
|
|
52
|
+
px: number;
|
|
53
|
+
box: [number, number, number, number] | null;
|
|
54
|
+
max_spatial_run_cells: number;
|
|
55
|
+
samples: BehindSeamsSample[];
|
|
56
|
+
}
|
|
57
|
+
export type BehindSeamsEndpoint = [grid_x: number, grid_y: number, world_x: number, world_y: number, owner: number];
|
|
58
|
+
export interface BehindSeamsUnreadCorridor {
|
|
59
|
+
axis: 'horizontal' | 'vertical';
|
|
60
|
+
/** Spatial order: left/right on a row, top/bottom on a column; owners are not inferred. */
|
|
61
|
+
endpoints: [BehindSeamsEndpoint, BehindSeamsEndpoint];
|
|
62
|
+
cells: Array<[number, number]>;
|
|
63
|
+
cell_count: number;
|
|
64
|
+
}
|
|
65
|
+
export interface BehindSeamsStanding extends BehindSeamsCount {
|
|
66
|
+
/** Distinct grid cells across all unread corridors, including cross-axis intersections. */
|
|
67
|
+
unread_cells?: number;
|
|
68
|
+
/** Omitted, together with unread_cells, when every corridor's endpoints are readable. */
|
|
69
|
+
unread_corridors?: BehindSeamsUnreadCorridor[];
|
|
70
|
+
}
|
|
71
|
+
export interface BehindSeamsFrame {
|
|
72
|
+
animation: 'idle' | null;
|
|
73
|
+
index: number;
|
|
74
|
+
time: number;
|
|
75
|
+
behind_slots: string[];
|
|
76
|
+
grid: SeamGrid | null;
|
|
77
|
+
motion_opened: BehindSeamsCount;
|
|
78
|
+
standing: BehindSeamsStanding;
|
|
79
|
+
}
|
|
80
|
+
export interface BehindSeamsHit {
|
|
81
|
+
index: number;
|
|
82
|
+
time: number;
|
|
83
|
+
px: number;
|
|
84
|
+
cell: number;
|
|
85
|
+
area: number;
|
|
86
|
+
box: [number, number, number, number];
|
|
87
|
+
}
|
|
88
|
+
export interface BehindSeamsReading {
|
|
89
|
+
status: 'PASS' | 'FAIL' | 'SKIP';
|
|
90
|
+
frames: BehindSeamsHit[];
|
|
91
|
+
hit_frames: number;
|
|
92
|
+
max_px: number;
|
|
93
|
+
worst: BehindSeamsHit | null;
|
|
94
|
+
spans: Array<{ first: number; last: number; frames: number; seconds: number }>;
|
|
95
|
+
max_run: { frames: number; seconds: number };
|
|
96
|
+
max_spatial_run_cells: number;
|
|
97
|
+
max_spatial_run_world: number;
|
|
98
|
+
}
|
|
99
|
+
export interface BehindSeamsPair {
|
|
100
|
+
animation: 'idle';
|
|
101
|
+
pair: string;
|
|
102
|
+
parts: [string, string];
|
|
103
|
+
declared: string[];
|
|
104
|
+
threshold: number | null;
|
|
105
|
+
allowance: number;
|
|
106
|
+
setup: BehindSeamsFrame | null;
|
|
107
|
+
frames: BehindSeamsFrame[];
|
|
108
|
+
motion_opened: BehindSeamsReading;
|
|
109
|
+
standing: BehindSeamsReading;
|
|
110
|
+
/** Idle counts only; a read frame has both halves available, even when failing. */
|
|
111
|
+
sampled_frames?: number;
|
|
112
|
+
read_frames?: number;
|
|
113
|
+
unmeasured_frames?: number;
|
|
114
|
+
}
|
|
115
|
+
export interface BehindSeamsMeasured {
|
|
116
|
+
status: 'PASS' | 'FAIL' | 'SKIP';
|
|
117
|
+
fps: number;
|
|
118
|
+
pairs: BehindSeamsPair[];
|
|
119
|
+
unmeasured: Array<{ pair: string | null; frame: string; reason: string }>;
|
|
120
|
+
limitations: string;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The orchestration input: only an already gated build and its full stock seam
|
|
124
|
+
* render. Unsupported active worlds/sets must be named, never silently defaulted. */
|
|
125
|
+
export interface BehindSeamsInput {
|
|
126
|
+
build: string;
|
|
127
|
+
rig: Record<string, unknown>;
|
|
128
|
+
pairs: readonly MotionSeamPair[];
|
|
129
|
+
stockDir: string | null;
|
|
130
|
+
fps: number;
|
|
131
|
+
unsupported?: string;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const LIMITATIONS = 'setup and every idle frame; motion uses transported setup-overlap support; standing certifies horizontal and vertical bracketed runs, not all curved or diagonal openings; rear-set attribution is not individual-slot blame; other animations and alternate skins are not measured';
|
|
135
|
+
const record = (v: unknown): v is Record<string, unknown> => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
136
|
+
const freeKey = (k: string, v: unknown): boolean => (k.startsWith('x-') && k.length > 2 && record(v)) || ((k === 'note' || k.endsWith('_note')) && typeof v === 'string');
|
|
137
|
+
const keyOf = (a: string, b: string): string => JSON.stringify(a < b ? [a, b] : [b, a]);
|
|
138
|
+
|
|
139
|
+
/** Shape only. Missing T is a valid undeclared reading, never an invented default. */
|
|
140
|
+
export function behindSeamsProblems(value: unknown, at: string): Problem[] {
|
|
141
|
+
const problems: Problem[] = [];
|
|
142
|
+
const bad = (object: string, detail: string): void => { problems.push({ code: 'REQUIREMENTS_FIELD', object, detail }); };
|
|
143
|
+
const block = (v: unknown, where: string, pair: boolean): void => {
|
|
144
|
+
if (!record(v)) { bad(where, `is ${JSON.stringify(v)}; requires a behind_seams record with an authored threshold and optional allowance/pair overrides`); return; }
|
|
145
|
+
for (const k of Object.keys(v)) if (!(pair ? ['part', 'neighbour', 'threshold', 'allowance'] : ['threshold', 'allowance', 'pairs']).includes(k) && !freeKey(k, v[k])) bad(`${where}.${k}`, 'is not a field this block reads (threshold, allowance, pairs; pair entries name part and neighbour; note or *_note as a string; x-<name> as a record)');
|
|
146
|
+
if ('threshold' in v && (typeof v.threshold !== 'number' || !Number.isInteger(v.threshold) || v.threshold < 0 || v.threshold > 255)) bad(`${where}.threshold`, `is ${JSON.stringify(v.threshold)}; requires an authored integer from 0 through 255; no threshold is derived from the art`);
|
|
147
|
+
if ('allowance' in v && (typeof v.allowance !== 'number' || !Number.isSafeInteger(v.allowance) || v.allowance < 0)) bad(`${where}.allowance`, `is ${JSON.stringify(v.allowance)}; requires a nonnegative safe integer count of seam cells`);
|
|
148
|
+
if (pair) {
|
|
149
|
+
for (const name of ['part', 'neighbour']) if (typeof v[name] !== 'string' || (v[name] as string).trim().length === 0) bad(`${where}.${name}`, 'requires a nonempty named member of a selected motion seam pair');
|
|
150
|
+
if (typeof v.part === 'string' && v.part === v.neighbour) bad(where, `names the same part ${JSON.stringify(v.part)} twice; two different members are required`);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
block(value, at, false);
|
|
154
|
+
if (record(value) && 'pairs' in value) {
|
|
155
|
+
if (!Array.isArray(value.pairs)) bad(`${at}.pairs`, 'requires an array of {part, neighbour, threshold?, allowance?} overrides');
|
|
156
|
+
else {
|
|
157
|
+
const seen = new Set<string>();
|
|
158
|
+
value.pairs.forEach((p, i) => {
|
|
159
|
+
block(p, `${at}.pairs[${i}]`, true);
|
|
160
|
+
if (record(p) && typeof p.part === 'string' && typeof p.neighbour === 'string') {
|
|
161
|
+
const key = keyOf(p.part, p.neighbour);
|
|
162
|
+
if (seen.has(key)) bad(`${at}.pairs[${i}]`, `duplicates pair ${JSON.stringify(p.part + '|' + p.neighbour)}; reversed members name the same pair`);
|
|
163
|
+
seen.add(key);
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return problems;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const emptyCount = (status: BehindSeamsCount['status'] = 'SKIP'): BehindSeamsCount => ({ status, boundary_cells: 0, geometric_px: 0, raw_px: 0, px: 0, box: null, max_spatial_run_cells: 0, samples: [] });
|
|
172
|
+
type RGB = [number, number, number];
|
|
173
|
+
interface Material { alpha: Float64Array; base: Float64Array; delta: Float64Array }
|
|
174
|
+
const material = (n: number): Material => ({ alpha: new Float64Array(n), base: new Float64Array(n * 3), delta: new Float64Array(n * 3) });
|
|
175
|
+
|
|
176
|
+
/** The official callback supplies straight RGB, after tint, before composite byte rounding.
|
|
177
|
+
* Accumulate deviations from the first colour: a constant colour has exactly zero
|
|
178
|
+
* deviation at every alpha, including T=0. There is no epsilon or unpremultiplication
|
|
179
|
+
* of the callback's already straight colour. */
|
|
180
|
+
function add(m: Material, k: number, rgb: RGB, a: number): void {
|
|
181
|
+
if (m.alpha[k] === 0) for (let c = 0; c < 3; c++) { m.base[3 * k + c] = rgb[c]; m.delta[3 * k + c] = 0; }
|
|
182
|
+
for (let c = 0; c < 3; c++) m.delta[3 * k + c] = m.delta[3 * k + c] * (1 - a) + (rgb[c] - m.base[3 * k + c]) * a;
|
|
183
|
+
m.alpha[k] = a + m.alpha[k] * (1 - a);
|
|
184
|
+
}
|
|
185
|
+
function colour(m: Material, k: number): RGB {
|
|
186
|
+
return [0, 1, 2].map(c => m.base[k * 3 + c] + m.delta[k * 3 + c] / m.alpha[k]) as RGB;
|
|
187
|
+
}
|
|
188
|
+
function distance(m: Material, k: number, to: RGB): number {
|
|
189
|
+
return Math.max(...[0, 1, 2].map(c => Math.abs((m.base[k * 3 + c] - to[c]) + m.delta[k * 3 + c] / m.alpha[k])));
|
|
190
|
+
}
|
|
191
|
+
function spatialRun(mask: ReadonlySet<number>, width: number, height: number): number {
|
|
192
|
+
let longest = 0;
|
|
193
|
+
for (const [length, lines, step, start] of [[width, height, 1, width], [height, width, width, 1]]) for (let l = 0; l < lines; l++) {
|
|
194
|
+
let run = 0;
|
|
195
|
+
for (let j = 0; j < length; j++) { run = mask.has(l * start + j * step) ? run + 1 : 0; longest = Math.max(longest, run); }
|
|
196
|
+
}
|
|
197
|
+
return longest;
|
|
198
|
+
}
|
|
199
|
+
function centre(grid: SeamGrid, k: number): [number, number] {
|
|
200
|
+
return [grid.viewport.minX + (k % grid.viewport.width + 0.5) * grid.cell, grid.viewport.maxY - (Math.floor(k / grid.viewport.width) + 0.5) * grid.cell];
|
|
201
|
+
}
|
|
202
|
+
function counted(count: BehindSeamsCount, cells: ReadonlySet<number>, grid: SeamGrid, allowance: number): void {
|
|
203
|
+
count.px = cells.size;
|
|
204
|
+
count.status = cells.size > allowance ? 'FAIL' : 'PASS';
|
|
205
|
+
count.max_spatial_run_cells = spatialRun(cells, grid.viewport.width, grid.viewport.height);
|
|
206
|
+
for (const k of cells) {
|
|
207
|
+
const [x, y] = centre(grid, k), d = grid.cell / 2;
|
|
208
|
+
if (count.box === null) count.box = [x - d, y - d, x + d, y + d];
|
|
209
|
+
else count.box = [Math.min(count.box[0], x - d), Math.min(count.box[1], y - d), Math.max(count.box[2], x + d), Math.max(count.box[3], y + d)];
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
interface Corridor { endpoints: Array<[number, number]> }
|
|
214
|
+
interface RasterCache { full: Plate; hidden: Plate }
|
|
215
|
+
|
|
216
|
+
function readBehindFrame(api: BehindSeamsApi, input: BehindSeamsFramesInput, pair: MotionSeamPair, frame: Frame, setup: boolean,
|
|
217
|
+
stock: BehindSeamsStockPair | undefined, threshold: number, allowance: number, unread: (frame: string, reason: string) => void,
|
|
218
|
+
cache: Map<Frame, Map<string, RasterCache>>): BehindSeamsFrame {
|
|
219
|
+
const label = setup ? 'setup' : `idle frame ${frame.index}`;
|
|
220
|
+
const row: BehindSeamsFrame = { ...checkRow(setup ? 'BEHIND_PART_SEAMS.pairs[].setup' : 'BEHIND_PART_SEAMS.pairs[].frames', 'pair', false, [pair.a, pair.b]), animation: setup ? null : 'idle', index: frame.index, time: frame.time, behind_slots: [], grid: null, motion_opened: emptyCount(), standing: emptyCount() };
|
|
221
|
+
const fail = (reason: string): BehindSeamsFrame => { unread(label, reason); return row; };
|
|
222
|
+
if (pair.unread !== undefined) return fail(pair.unread);
|
|
223
|
+
if (frame.attachments === undefined) return fail('draw order/whole attachment geometry is unavailable');
|
|
224
|
+
const order = frame.attachments.map(p => p.slot);
|
|
225
|
+
if (new Set(order).size !== order.length || frame.pieces.some(p => !order.includes(p.slot))) return fail('drawn slots have missing or duplicate whole attachments; actual draw order cannot be read');
|
|
226
|
+
const positions = frame.pieces.map(p => order.indexOf(p.slot));
|
|
227
|
+
if (positions.some((p, i) => i > 0 && p < positions[i - 1])) return fail('fragment order disagrees with whole attachment draw order');
|
|
228
|
+
const ia = order.indexOf(pair.a), ib = order.indexOf(pair.b);
|
|
229
|
+
if (ia < 0 || ib < 0 || !frame.pieces.some(p => p.slot === pair.a) || !frame.pieces.some(p => p.slot === pair.b)) return fail('both named front slots must draw on this frame');
|
|
230
|
+
row.behind_slots = order.slice(0, Math.min(ia, ib)).filter(slot => frame.pieces.some(p => p.slot === slot));
|
|
231
|
+
if (row.behind_slots.length === 0) return fail('no eligible slot is drawn strictly behind both pair members (including every fragment)');
|
|
232
|
+
let grid: SeamGrid | null;
|
|
233
|
+
try { grid = seamGridOf({ a: pair.a, b: pair.b }, input.rest, { ...frame, attachments: frame.attachments }, input.pages); }
|
|
234
|
+
catch (e) { return fail(`native seam grid unavailable: ${e instanceof Error ? e.message : String(e)}`); }
|
|
235
|
+
if (grid === null) return fail('native seamGridOf returned no pair grid');
|
|
236
|
+
row.grid = grid;
|
|
237
|
+
const { viewport, cell } = grid, { width, height } = viewport, n = width * height;
|
|
238
|
+
const A = material(n), B = material(n), project = api.projector(viewport);
|
|
239
|
+
try {
|
|
240
|
+
for (const piece of frame.pieces) if (piece.slot === pair.a || piece.slot === pair.b) {
|
|
241
|
+
api.rasterisePiece(api.pageFor(input.pages, piece), piece, project, viewport, (x, y, r, g, b, alpha) => add(piece.slot === pair.a ? A : B, y * width + x, [r, g, b], alpha / 255));
|
|
242
|
+
}
|
|
243
|
+
const half = DRAWN_ALPHA / 255;
|
|
244
|
+
const low = (k: number): boolean => A.alpha[k] + B.alpha[k] * (1 - A.alpha[k]) < half;
|
|
245
|
+
const owner = (k: number): number => A.alpha[k] >= half ? B.alpha[k] >= half ? 3 : 1 : B.alpha[k] >= half ? 2 : 0;
|
|
246
|
+
const corridors = new Map<number, Corridor>();
|
|
247
|
+
const support = new Set<number>();
|
|
248
|
+
const unreadCells = new Set<number>(), unreadCorridors: BehindSeamsUnreadCorridor[] = [];
|
|
249
|
+
// A measured overlap is support even when the opening population is empty.
|
|
250
|
+
for (let k = 0; k < n; k++) if (owner(k) === 3) support.add(k);
|
|
251
|
+
for (const [length, lines, step, start] of [[width, height, 1, width], [height, width, width, 1]]) for (let l = 0; l < lines; l++) {
|
|
252
|
+
let j = 0;
|
|
253
|
+
while (j < length) {
|
|
254
|
+
const k = l * start + j * step;
|
|
255
|
+
if (!low(k)) {
|
|
256
|
+
if (j > 0) { const prev = k - step; if (!low(prev) && owner(prev) > 0 && owner(prev) < 3 && owner(k) > 0 && owner(k) < 3 && owner(prev) !== owner(k)) { support.add(prev); support.add(k); } }
|
|
257
|
+
j++; continue;
|
|
258
|
+
}
|
|
259
|
+
const first = j;
|
|
260
|
+
while (j < length && low(l * start + j * step)) j++;
|
|
261
|
+
if (first === 0 || j === length) continue; // outer silhouette, never a bracketed opening
|
|
262
|
+
const left = l * start + (first - 1) * step, right = l * start + j * step;
|
|
263
|
+
const a = owner(left), b = owner(right);
|
|
264
|
+
if (a === 0 || b === 0 || a === 3 || b === 3) {
|
|
265
|
+
const endpoint = (at: number, owner: number): BehindSeamsEndpoint => [at % width, Math.floor(at / width), ...centre(grid, at), owner];
|
|
266
|
+
const cells: Array<[number, number]> = [];
|
|
267
|
+
for (let t = first; t < j; t++) {
|
|
268
|
+
const at = l * start + t * step;
|
|
269
|
+
unreadCells.add(at); cells.push(centre(grid, at));
|
|
270
|
+
}
|
|
271
|
+
unreadCorridors.push({ ...checkRow('BEHIND_PART_SEAMS.unread_corridors', 'pair', false, [pair.a, pair.b]),
|
|
272
|
+
axis: step === 1 ? 'horizontal' : 'vertical', endpoints: [endpoint(left, a), endpoint(right, b)], cells, cell_count: cells.length });
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
if (a === b) continue;
|
|
276
|
+
support.add(left); support.add(right);
|
|
277
|
+
const endpoints: [number, number] = a === 1 ? [left, right] : [right, left];
|
|
278
|
+
for (let t = first; t < j; t++) {
|
|
279
|
+
const at = l * start + t * step;
|
|
280
|
+
const c = corridors.get(at) ?? { endpoints: [] };
|
|
281
|
+
c.endpoints.push(endpoints); corridors.set(at, c);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
row.standing.boundary_cells = support.size;
|
|
286
|
+
if (unreadCorridors.length > 0) {
|
|
287
|
+
row.standing.unread_cells = unreadCells.size;
|
|
288
|
+
row.standing.unread_corridors = unreadCorridors;
|
|
289
|
+
}
|
|
290
|
+
let motionReadable = setup;
|
|
291
|
+
const motionCells = new Map<number, SeamCell>();
|
|
292
|
+
if (!setup) {
|
|
293
|
+
if (stock === undefined || stock.frames !== input.idle.length || stock.drawn !== stock.frames || stock.banded !== stock.frames) unread(label, 'stock transported setup-overlap support is unavailable on every idle frame; motion-opened is not measured');
|
|
294
|
+
else {
|
|
295
|
+
motionReadable = true;
|
|
296
|
+
const hits = stock.gapCovered.filter(h => h.index === frame.index);
|
|
297
|
+
if (hits.length > 1) { motionReadable = false; unread(label, 'duplicate stock gapCovered hit for this frame'); }
|
|
298
|
+
for (const hit of hits) {
|
|
299
|
+
if (hit.cell !== cell || hit.time !== frame.time) { motionReadable = false; unread(label, 'stock gapCovered grid side or sample time differs from the native posed grid'); continue; }
|
|
300
|
+
for (const c of hit.cells) {
|
|
301
|
+
const x = Math.round((c.x - viewport.minX) / cell - 0.5), y = Math.round((viewport.maxY - c.y) / cell - 0.5), k = y * width + x;
|
|
302
|
+
const actual = centre(grid, k);
|
|
303
|
+
if (x < 0 || x >= width || y < 0 || y >= height || actual[0] !== c.x || actual[1] !== c.y || motionCells.has(k)) {
|
|
304
|
+
motionReadable = false; unread(label, 'stock gapCovered cell is duplicate or not an exact native seam-grid centre'); continue;
|
|
305
|
+
}
|
|
306
|
+
motionCells.set(k, c);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
// Ordinary official full/hidden rendering, both transparent and on the SAME
|
|
312
|
+
// native seam grid. No --seam/--hide combination and no PNG nearest sampling.
|
|
313
|
+
let frameCache = cache.get(frame);
|
|
314
|
+
if (frameCache === undefined) { frameCache = new Map(); cache.set(frame, frameCache); }
|
|
315
|
+
const cacheKey = JSON.stringify([grid, row.behind_slots]);
|
|
316
|
+
let rasters = frameCache.get(cacheKey);
|
|
317
|
+
const behind = new Set(row.behind_slots);
|
|
318
|
+
if (rasters === undefined) {
|
|
319
|
+
rasters = { full: api.renderFrame(frame, input.pages, viewport, [0, 0, 0, 0]), hidden: api.renderFrame({ ...frame, pieces: frame.pieces.filter(p => !behind.has(p.slot)) }, input.pages, viewport, [0, 0, 0, 0]) };
|
|
320
|
+
frameCache.set(cacheKey, rasters);
|
|
321
|
+
}
|
|
322
|
+
const targets = new Set([...corridors.keys(), ...motionCells.keys()]);
|
|
323
|
+
const rear = material(n), weights = new Map<number, Map<string, number>>();
|
|
324
|
+
const surviving = new Set<number>(), underflow = new Set<number>();
|
|
325
|
+
for (const piece of frame.pieces) api.rasterisePiece(api.pageFor(input.pages, piece), piece, project, viewport, (x, y, r, g, b, alpha) => {
|
|
326
|
+
const k = y * width + x;
|
|
327
|
+
if (!targets.has(k)) return;
|
|
328
|
+
const a = alpha / 255, keep = 1 - a;
|
|
329
|
+
const w = weights.get(k) ?? new Map<string, number>();
|
|
330
|
+
for (const [name, weight] of w) {
|
|
331
|
+
const next = weight * keep;
|
|
332
|
+
if (weight > 0 && keep > 0 && next === 0) underflow.add(k);
|
|
333
|
+
w.set(name, next);
|
|
334
|
+
}
|
|
335
|
+
if (behind.has(piece.slot)) {
|
|
336
|
+
add(rear, k, [r, g, b], a); w.set(piece.slot, (w.get(piece.slot) ?? 0) + a); surviving.add(k);
|
|
337
|
+
} else {
|
|
338
|
+
rear.alpha[k] *= keep;
|
|
339
|
+
for (let c = 0; c < 3; c++) rear.delta[k * 3 + c] *= keep;
|
|
340
|
+
// Symbolic survival, independent of transparent output-byte rounding.
|
|
341
|
+
if (alpha === 255) surviving.delete(k);
|
|
342
|
+
}
|
|
343
|
+
weights.set(k, w);
|
|
344
|
+
});
|
|
345
|
+
const sample = (k: number, distances: [number, number] | null, counts: boolean): BehindSeamsSample => {
|
|
346
|
+
const [x, y] = centre(grid, k);
|
|
347
|
+
return { ...checkRow('BEHIND_PART_SEAMS.samples', 'pair', false, [pair.a, pair.b]), x, y, rear_rgb: colour(rear, k), rear_weight: rear.alpha[k],
|
|
348
|
+
contributors: row.behind_slots.filter(slot => (weights.get(k)?.get(slot) ?? 0) > 0).map(slot => ({ ...checkRow('BEHIND_PART_SEAMS.contributors', 'pair', false, [pair.a, pair.b]), slot, weight: weights.get(k)!.get(slot)! })), distances, counted: counts };
|
|
349
|
+
};
|
|
350
|
+
const standingHits = new Set<number>(), motionHits = new Set<number>();
|
|
351
|
+
let standingUnderflow = false;
|
|
352
|
+
for (const [k, corridor] of corridors) {
|
|
353
|
+
if (!surviving.has(k)) continue;
|
|
354
|
+
if (rear.alpha[k] <= 0 || underflow.has(k)) { unread(label, 'positive symbolic rear contribution underflowed its numeric colour weight'); standingUnderflow = true; continue; }
|
|
355
|
+
row.standing.geometric_px++;
|
|
356
|
+
let chosen: [number, number] | null = null, raw = false, qualifies = false;
|
|
357
|
+
for (const [a, b] of corridor.endpoints) {
|
|
358
|
+
const d: [number, number] = [distance(rear, k, colour(A, a)), distance(rear, k, colour(B, b))];
|
|
359
|
+
const r = d[0] > 0 && d[1] > 0, q = d[0] > threshold && d[1] > threshold;
|
|
360
|
+
if (chosen === null || q && !qualifies || r && !raw) chosen = d;
|
|
361
|
+
raw ||= r; qualifies ||= q;
|
|
362
|
+
}
|
|
363
|
+
if (raw) row.standing.raw_px++;
|
|
364
|
+
if (qualifies) standingHits.add(k);
|
|
365
|
+
row.standing.samples.push(sample(k, chosen, qualifies));
|
|
366
|
+
}
|
|
367
|
+
counted(row.standing, standingHits, grid, allowance);
|
|
368
|
+
// Incomplete coverage cannot certify PASS, but cannot hide a measured failure.
|
|
369
|
+
if (support.size === 0 || unreadCorridors.length > 0 || standingUnderflow) {
|
|
370
|
+
if (row.standing.status !== 'FAIL') row.standing.status = 'SKIP';
|
|
371
|
+
if (unreadCorridors.length > 0) unread(label, 'standing endpoints include ambiguous half-alpha owners; unread corridors are not zero');
|
|
372
|
+
else if (support.size === 0) unread(label, 'no A/B boundary support was measured (no overlap, contact or bracketed corridor)');
|
|
373
|
+
}
|
|
374
|
+
for (const k of motionCells.keys()) {
|
|
375
|
+
if (rasters.hidden.data[k * 4 + 3] !== 0 || rasters.full.data[k * 4 + 3] === 0 || !surviving.has(k)) continue;
|
|
376
|
+
// Stock gapCovered already transports the A/B setup-overlap material.
|
|
377
|
+
// setupTop/setupUnder describe the UNTRANSPORTED world centre and must
|
|
378
|
+
// never be used as a rest-visible mask after a common translation.
|
|
379
|
+
const setupAttachments = input.setup?.attachments;
|
|
380
|
+
const setupOrder = setupAttachments?.map(p => p.slot);
|
|
381
|
+
const setupFront = setupOrder === undefined ? -1 : Math.min(setupOrder.indexOf(pair.a), setupOrder.indexOf(pair.b));
|
|
382
|
+
const sameDrawings = [pair.a, pair.b].every(slot => {
|
|
383
|
+
const restDrawing = setupAttachments?.find(a => a.slot === slot), posedDrawing = frame.attachments!.find(a => a.slot === slot);
|
|
384
|
+
return restDrawing !== undefined && restDrawing.attachment === posedDrawing?.attachment && input.setup!.pieces.some(p => p.slot === slot);
|
|
385
|
+
});
|
|
386
|
+
if (!sameDrawings || setupOrder === undefined || setupFront < 0 || row.behind_slots.some(slot => setupOrder.indexOf(slot) < 0 || setupOrder.indexOf(slot) >= setupFront)) {
|
|
387
|
+
motionReadable = false; unread(label, 'stock transported setup-overlap support cannot be bound to the actually drawn setup pair strictly in front of the complete rear set'); continue;
|
|
388
|
+
}
|
|
389
|
+
if (rear.alpha[k] <= 0 || underflow.has(k)) { motionReadable = false; unread(label, 'positive symbolic rear contribution underflowed its numeric colour weight'); continue; }
|
|
390
|
+
motionHits.add(k); row.motion_opened.samples.push(sample(k, null, true));
|
|
391
|
+
}
|
|
392
|
+
row.motion_opened.boundary_cells = motionCells.size;
|
|
393
|
+
row.motion_opened.geometric_px = motionHits.size; row.motion_opened.raw_px = motionHits.size;
|
|
394
|
+
counted(row.motion_opened, motionHits, grid, allowance);
|
|
395
|
+
if (!motionReadable) row.motion_opened.status = 'SKIP';
|
|
396
|
+
return { ...row, ...checkRow(setup ? 'BEHIND_PART_SEAMS.pairs[].setup' : 'BEHIND_PART_SEAMS.pairs[].frames', 'pair', row.standing.status === 'FAIL' || row.motion_opened.status === 'FAIL', [pair.a, pair.b]) };
|
|
397
|
+
} catch (e) { return fail(`official raster/source reading unavailable: ${e instanceof Error ? e.message : String(e)}`); }
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
function reading(frames: readonly BehindSeamsFrame[], kind: 'motion_opened' | 'standing', fps: number, allowance: number, parts: [string, string]): BehindSeamsReading {
|
|
401
|
+
const hits: BehindSeamsHit[] = [];
|
|
402
|
+
let worst: BehindSeamsHit | null = null, spatial = 0, world = 0;
|
|
403
|
+
for (const frame of frames) {
|
|
404
|
+
const r = frame[kind];
|
|
405
|
+
if (frame.grid === null) continue;
|
|
406
|
+
spatial = Math.max(spatial, r.max_spatial_run_cells); world = Math.max(world, r.max_spatial_run_cells * frame.grid.cell);
|
|
407
|
+
if (r.px === 0 || r.box === null) continue;
|
|
408
|
+
const hit: BehindSeamsHit = { ...checkRow('BEHIND_PART_SEAMS.hits', 'pair', r.px > allowance, parts), index: frame.index, time: frame.time, px: r.px, cell: frame.grid.cell, area: r.px * frame.grid.cell ** 2, box: r.box };
|
|
409
|
+
hits.push(hit);
|
|
410
|
+
if (worst === null || hit.px > worst.px) worst = hit;
|
|
411
|
+
}
|
|
412
|
+
const spans: BehindSeamsReading['spans'] = [];
|
|
413
|
+
for (const hit of hits) {
|
|
414
|
+
const prev = spans[spans.length - 1];
|
|
415
|
+
if (prev !== undefined && hit.index === prev.last + 1) { prev.last = hit.index; prev.frames++; prev.seconds = prev.frames / fps; }
|
|
416
|
+
else spans.push({ ...checkRow('BEHIND_PART_SEAMS.spans', 'pair', false, parts), first: hit.index, last: hit.index, frames: 1, seconds: 1 / fps });
|
|
417
|
+
}
|
|
418
|
+
const maxRun = Math.max(0, ...spans.map(s => s.frames));
|
|
419
|
+
return { status: frames.some(f => f[kind].status === 'FAIL') ? 'FAIL' : frames.length === 0 || frames.some(f => f[kind].status === 'SKIP') ? 'SKIP' : 'PASS', frames: hits, hit_frames: hits.length, max_px: worst?.px ?? 0, worst, spans, max_run: { frames: maxRun, seconds: maxRun === 0 ? 0 : maxRun / fps }, max_spatial_run_cells: spatial, max_spatial_run_world: world };
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/** Pure frame-level entry for generated controls. Production supplies the same
|
|
423
|
+
* official frames and the existing full render's stock cells; no second seamsOf. */
|
|
424
|
+
export function measureBehindSeamFrames(api: BehindSeamsApi, input: BehindSeamsFramesInput, declared: BehindSeams, problems: Problem[]): BehindSeamsMeasured {
|
|
425
|
+
const line: BehindSeamsMeasured = { status: 'SKIP', fps: input.fps, pairs: [], unmeasured: [], limitations: LIMITATIONS };
|
|
426
|
+
const cache = new Map<Frame, Map<string, RasterCache>>();
|
|
427
|
+
const selected = new Set(input.pairs.map(p => keyOf(p.a, p.b)));
|
|
428
|
+
const unread = (pair: MotionSeamPair | null, frame: string, reason: string, required: boolean): void => {
|
|
429
|
+
const entry = { ...checkRow('BEHIND_PART_SEAMS.unmeasured', pair === null ? 'rig' : 'pair', required, pair === null ? [] : [pair.a, pair.b]), pair: pair?.pair ?? null, frame, reason: `HOLE: ${reason}` };
|
|
430
|
+
if (line.unmeasured.some(u => u.pair === entry.pair && u.frame === frame && u.reason === entry.reason)) return;
|
|
431
|
+
line.unmeasured.push(entry);
|
|
432
|
+
if (required) problems.push({ code: BEHIND_SEAM_UNMEASURED_CODE, object: `${pair?.pair ?? BEHIND_SEAM_LINE} (${frame})`, detail: `${entry.reason}; a declared required reading cannot pass unmeasured` });
|
|
433
|
+
};
|
|
434
|
+
const start = problems.length;
|
|
435
|
+
const shape = behindSeamsProblems(declared, 'requirements.behind_seams');
|
|
436
|
+
problems.push(...shape);
|
|
437
|
+
if (shape.length > 0) { line.status = 'FAIL'; return line; }
|
|
438
|
+
for (const p of declared.pairs ?? []) if (!selected.has(keyOf(p.part, p.neighbour))) unread(null, 'declaration', `override pair "${p.part}|${p.neighbour}" is not in motionSeamSelection; an override does not create an adjacent pair`, true);
|
|
439
|
+
if (input.pairs.length === 0) unread(null, 'selection', 'no selected motion seam pair; no boundary population was measured', declared.threshold !== undefined);
|
|
440
|
+
const rows = input.pairs.map(pair => {
|
|
441
|
+
const override = declared.pairs?.find(p => keyOf(p.part, p.neighbour) === keyOf(pair.a, pair.b));
|
|
442
|
+
const threshold = override?.threshold ?? declared.threshold ?? null, allowance = override?.allowance ?? declared.allowance ?? 0;
|
|
443
|
+
const stockRows = input.stock?.filter(s => s.pair === pair.pair) ?? [];
|
|
444
|
+
return { pair, threshold, allowance, stock: stockRows.length === 1 ? stockRows[0] : undefined,
|
|
445
|
+
setup: null as BehindSeamsFrame | null, frames: [] as BehindSeamsFrame[] };
|
|
446
|
+
});
|
|
447
|
+
const rate = Number.isFinite(input.fps) && input.fps > 0;
|
|
448
|
+
const cadence = rate && input.idle.every((f, i) => f.index === i && (f.time === i / input.fps || f.time === i * (1 / input.fps)));
|
|
449
|
+
for (const r of rows) {
|
|
450
|
+
if (r.threshold === null) { unread(r.pair, 'declaration', 'no authored threshold T; this pair is SKIP, never a pass', false); continue; }
|
|
451
|
+
if (!rate) unread(r.pair, 'idle', 'frame cadence is unavailable', true);
|
|
452
|
+
if (input.setup === null) unread(r.pair, 'setup', 'actual setup pose is unavailable; idle frame zero is not setup', true);
|
|
453
|
+
if (input.idle.length === 0) unread(r.pair, 'idle', 'no idle frames were read', true);
|
|
454
|
+
else if (!cadence) unread(r.pair, 'idle', 'missing, duplicate or off-cadence idle frame; every frame at the declared fps is required', true);
|
|
455
|
+
}
|
|
456
|
+
// All pairs share a frame's hidden-set cache. Release its native rasters before
|
|
457
|
+
// the next pose: memory grows with one frame, not animation length.
|
|
458
|
+
const observe = (frame: Frame, setup: boolean): void => {
|
|
459
|
+
for (const r of rows) if (r.threshold !== null) {
|
|
460
|
+
const note = (where: string, reason: string): void => unread(r.pair, where, reason, true);
|
|
461
|
+
const value = readBehindFrame(api, input, r.pair, frame, setup, r.stock, r.threshold, r.allowance, note, cache);
|
|
462
|
+
if (setup) r.setup = value;
|
|
463
|
+
else r.frames.push(value);
|
|
464
|
+
}
|
|
465
|
+
cache.clear();
|
|
466
|
+
};
|
|
467
|
+
if (rate && input.setup !== null) observe(input.setup, true);
|
|
468
|
+
if (cadence) for (const frame of input.idle) observe(frame, false);
|
|
469
|
+
for (const r of rows) {
|
|
470
|
+
const { pair, threshold, allowance, setup, frames } = r;
|
|
471
|
+
const parts: [string, string] = [pair.a, pair.b];
|
|
472
|
+
const motion = reading(frames, 'motion_opened', input.fps, allowance, parts), standing = reading(frames, 'standing', input.fps, allowance, parts);
|
|
473
|
+
if ((setup === null || setup.motion_opened.status === 'SKIP') && motion.status !== 'FAIL') motion.status = 'SKIP';
|
|
474
|
+
if ((setup === null || setup.standing.status === 'SKIP') && standing.status !== 'FAIL') standing.status = 'SKIP';
|
|
475
|
+
if (setup?.standing.status === 'FAIL') standing.status = 'FAIL';
|
|
476
|
+
for (const [kind, value] of [['motion_opened', motion], ['standing', standing]] as const) {
|
|
477
|
+
if (value.max_px > allowance || kind === 'standing' && (setup?.standing.px ?? 0) > allowance) {
|
|
478
|
+
const at = kind === 'standing' && (setup?.standing.px ?? 0) > value.max_px ? `setup (${setup!.standing.px} cells)` : `idle frame ${value.worst?.index} (${value.max_px} cells)`;
|
|
479
|
+
problems.push({ code: BEHIND_SEAM_CODE, object: `pair "${pair.pair}" ${kind}`, detail: `${at} exceeds allowance ${allowance}; authored threshold ${threshold}, native seam cells, no one-cell floor or rest mask` });
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
const failed = motion.status === 'FAIL' || standing.status === 'FAIL' || line.unmeasured.some(u => u.pair === pair.pair && threshold !== null);
|
|
483
|
+
const readFrames = frames.filter(f => f.motion_opened.status !== 'SKIP' && f.standing.status !== 'SKIP' &&
|
|
484
|
+
(f.standing.unread_corridors?.length ?? 0) === 0 && !line.unmeasured.some(u => u.pair === pair.pair && u.frame === `idle frame ${f.index}`)).length;
|
|
485
|
+
line.pairs.push({ ...checkRow('BEHIND_PART_SEAMS.pairs', 'pair', failed, parts), animation: 'idle', pair: pair.pair, parts, declared: [...pair.declared], threshold, allowance, setup, frames, motion_opened: motion, standing,
|
|
486
|
+
sampled_frames: input.idle.length, read_frames: readFrames, unmeasured_frames: input.idle.length - readFrames });
|
|
487
|
+
}
|
|
488
|
+
line.status = problems.length > start ? 'FAIL' : line.pairs.length === 0 || line.pairs.some(p => p.threshold === null || p.motion_opened.status === 'SKIP' || p.standing.status === 'SKIP') ? 'SKIP' : 'PASS';
|
|
489
|
+
return line;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function jsonAt(path: string): Record<string, unknown> {
|
|
493
|
+
const value: unknown = JSON.parse(readFileSync(path, 'utf8'));
|
|
494
|
+
if (!record(value)) throw new Error('required sidecar is not an object');
|
|
495
|
+
return value;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/** Consume the existing render, including zero-hit pairs. Shape, pose, cadence,
|
|
499
|
+
* world and cell detail are checked before an absent hit can mean zero. */
|
|
500
|
+
function stockOf(dir: string, pairs: readonly MotionSeamPair[], fps: number, setup: Frame, idle: readonly Frame[], rest: readonly AttachmentRest[]): BehindSeamsStockPair[] {
|
|
501
|
+
const sidecar = jsonAt(join(dir, 'frames.json'));
|
|
502
|
+
if (sidecar.spec !== 'rigc-frames/1' || ['skin', 'slots', 'hidden', 'environment', 'preroll', 'physicsMode'].some(k => sidecar[k] !== undefined)) throw new Error('stock full render has an unavailable specification, skin, slot subset, environment, preroll or physics mode');
|
|
503
|
+
const sets = Array.isArray(sidecar.sets) ? sidecar.sets.filter(record).filter(s => s.animation === 'idle') : [];
|
|
504
|
+
if (sets.length !== 1) throw new Error('stock full render requires exactly one idle frame set');
|
|
505
|
+
const set = sets[0];
|
|
506
|
+
if (set.fps !== fps || set.stride !== 1 || set.sampled !== idle.length || set.written !== idle.length || idle.length === 0 || typeof set.dir !== 'string' || isAbsolute(set.dir)) throw new Error('stock idle must have every sampled frame written, stride 1, at the same fps as the official sampled poses');
|
|
507
|
+
const home = resolve(dir, set.dir), rel = relative(resolve(dir), home);
|
|
508
|
+
if (isAbsolute(rel) || /^\.\.(?:[\\/]|$)/.test(rel)) throw new Error('stock idle directory leaves its full-render root');
|
|
509
|
+
const geometry = jsonAt(join(home, 'geometry.json'));
|
|
510
|
+
if (geometry.spec !== 'rigc-geometry/1' || geometry.animation !== 'idle' || geometry.fps !== fps || ['skin', 'environment', 'preroll', 'physicsMode'].some(k => geometry[k] !== undefined) || !record(geometry.setup) || !Array.isArray(geometry.frames)) throw new Error('stock whole attachment geometry is unavailable or belongs to a different pose/world');
|
|
511
|
+
const posed = geometry.frames;
|
|
512
|
+
const different = idle.some((f, i) => { const p: unknown = posed[i]; return !record(p) || p.index !== f.index || p.time !== f.time || JSON.stringify(p.attachments) !== JSON.stringify(f.attachments); });
|
|
513
|
+
if (posed.length !== idle.length || JSON.stringify(geometry.setup.attachments) !== JSON.stringify(setup.attachments) || JSON.stringify(geometry.rest) !== JSON.stringify(rest) || different) throw new Error('official setup/rest/idle geometry or draw order differs from the existing full stock seam render');
|
|
514
|
+
const file = jsonAt(join(home, 'seam.json'));
|
|
515
|
+
if (file.spec !== 'rigc-seam/1' || file.animation !== 'idle' || file.fps !== fps || file.skin !== undefined || !Array.isArray(file.pairs) || file.pairs.length !== pairs.length) throw new Error('stock seam pair order, animation, skin or fps is unavailable');
|
|
516
|
+
const finite = (v: unknown): v is number => typeof v === 'number' && Number.isFinite(v);
|
|
517
|
+
return pairs.map((want, i) => {
|
|
518
|
+
const p: unknown = (file.pairs as unknown[])[i];
|
|
519
|
+
if (!record(p) || p.a !== want.a || p.b !== want.b || !Number.isSafeInteger(p.drawn) || !Number.isSafeInteger(p.banded) || typeof p.drawn !== 'number' || typeof p.banded !== 'number' || p.banded < 0 || p.drawn < p.banded || p.drawn > idle.length || p.status !== (p.banded > 0 ? 'read' : 'skip') || !record(p.gapCovered) || !Array.isArray(p.gapCovered.frames)) throw new Error(`stock pair "${want.pair}" has unavailable drawn/banded/gapCovered evidence`);
|
|
520
|
+
const seen = new Set<number>();
|
|
521
|
+
const gapCovered = p.gapCovered.frames.map((hit: unknown) => {
|
|
522
|
+
if (!record(hit) || !Number.isSafeInteger(hit.index) || typeof hit.index !== 'number' || hit.index < 0 || hit.index >= idle.length || seen.has(hit.index) || !finite(hit.time) || hit.time !== idle[hit.index].time || !finite(hit.cell) || hit.cell <= 0 || !Number.isSafeInteger(hit.px) || typeof hit.px !== 'number' || hit.px <= 0 || !Array.isArray(hit.cells) || hit.cells.length !== hit.px || !Array.isArray(hit.box) || hit.box.length !== 4 || !hit.box.every(finite)) throw new Error(`stock pair "${want.pair}" requires unique complete gapCovered hits and --seam-cells detail`);
|
|
523
|
+
seen.add(hit.index);
|
|
524
|
+
const cells: SeamCell[] = hit.cells.map((c: unknown) => {
|
|
525
|
+
if (!record(c) || !finite(c.x) || !finite(c.y) || !(c.setupTop === null || typeof c.setupTop === 'string' && c.setupTop.length > 0) || !(c.setupUnder === null || typeof c.setupUnder === 'string' && c.setupUnder.length > 0) || c.setupTop === null && c.setupUnder !== null || c.setupTop !== null && c.setupTop === c.setupUnder) throw new Error(`stock pair "${want.pair}" has unread world cell/setup attribution`);
|
|
526
|
+
return { x: c.x, y: c.y, setupTop: c.setupTop, setupUnder: c.setupUnder };
|
|
527
|
+
});
|
|
528
|
+
return { index: hit.index, time: hit.time, cell: hit.cell, cells };
|
|
529
|
+
});
|
|
530
|
+
return { pair: want.pair, frames: idle.length, drawn: p.drawn, banded: p.banded, gapCovered };
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
function sourceProblems(rig: Record<string, unknown>, frames: readonly Frame[]): string[] {
|
|
535
|
+
const slots = (Array.isArray(rig.slots) ? rig.slots : []).filter(record);
|
|
536
|
+
const reasons = new Set<string>();
|
|
537
|
+
const seen = new Set<string>();
|
|
538
|
+
for (const frame of frames) for (const piece of frame.pieces) {
|
|
539
|
+
const matches = slots.filter(s => s.name === piece.slot);
|
|
540
|
+
if (matches.length !== 1) { reasons.add(`slot "${piece.slot}" has no unique rig source/order declaration`); continue; }
|
|
541
|
+
if (matches[0].blend !== undefined && matches[0].blend !== 'normal') { reasons.add(`slot "${piece.slot}" uses unsupported blend ${JSON.stringify(matches[0].blend)}; source-over contribution is unmeasured`); continue; }
|
|
542
|
+
const attached = frame.attachments?.filter(a => a.slot === piece.slot) ?? [];
|
|
543
|
+
if (attached.length !== 1 || piece.texture === undefined || piece.texture.artUvs.length !== piece.uvs.length) { reasons.add(`slot "${piece.slot}" has unavailable whole attachment/art-UV source evidence`); continue; }
|
|
544
|
+
const key = JSON.stringify([piece.slot, attached[0].attachment]);
|
|
545
|
+
if (seen.has(key)) continue;
|
|
546
|
+
seen.add(key);
|
|
547
|
+
try { seamSourceAttachment(rig, piece.slot, attached[0].attachment); }
|
|
548
|
+
catch (e) { reasons.add(`slot "${piece.slot}" attachment "${attached[0].attachment}" source unread: ${e instanceof Error ? e.message : String(e)}`); }
|
|
549
|
+
}
|
|
550
|
+
return [...reasons];
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/** Production reader. It neither builds nor emits files, and never substitutes
|
|
554
|
+
* its own seam algorithm for the full stock gapCovered result. */
|
|
555
|
+
export function measureBehindSeams(api: typeof import('@rig-c/core/render-shared'), input: BehindSeamsInput, declared: BehindSeams, problems: Problem[]): BehindSeamsMeasured {
|
|
556
|
+
const empty: BehindSeamsFramesInput = { pairs: input.pairs, setup: null, idle: [], rest: [], pages: new Map(), stock: null, fps: input.fps };
|
|
557
|
+
const active = declared.threshold !== undefined || declared.pairs?.some(p => p.threshold !== undefined) === true;
|
|
558
|
+
if (!active) return measureBehindSeamFrames(api, empty, declared, problems);
|
|
559
|
+
const fail = (reason: string): BehindSeamsMeasured => {
|
|
560
|
+
const line = measureBehindSeamFrames(api, empty, declared, problems);
|
|
561
|
+
line.status = 'FAIL';
|
|
562
|
+
line.unmeasured.push({ ...checkRow('BEHIND_PART_SEAMS.unmeasured', 'rig', true, []), pair: null, frame: 'render/source', reason: `HOLE: ${reason}` });
|
|
563
|
+
problems.push({ code: BEHIND_SEAM_UNMEASURED_CODE, object: BEHIND_SEAM_LINE, detail: `HOLE: ${reason}; required setup and idle cannot pass unmeasured` });
|
|
564
|
+
return line;
|
|
565
|
+
};
|
|
566
|
+
if (input.unsupported !== undefined) return fail(input.unsupported);
|
|
567
|
+
let candidate: ReturnType<typeof api.loadCandidate>;
|
|
568
|
+
let sampled: { setup: Frame | null; idle: Frame[]; rest: AttachmentRest[] };
|
|
569
|
+
try {
|
|
570
|
+
const skeleton = join(input.build, 'skeleton.json'), atlas = join(input.build, 'skeleton.atlas');
|
|
571
|
+
candidate = api.loadCandidate({ skeletonText: readFileSync(skeleton, 'utf8'), atlasText: readFileSync(atlas, 'utf8'), atlasDir: input.build, label: 'behind-part seams' }, { skeleton, atlas }, undefined);
|
|
572
|
+
sampled = api.throughPoser(candidate.choice, p => {
|
|
573
|
+
const opts = { geometry: true, texture: true };
|
|
574
|
+
const setup = api.sampleSetupPose(p, opts)[0] ?? null;
|
|
575
|
+
const idle = p.animations.some(a => a.name === 'idle') ? api.sampleAnimation(p, 'idle', input.fps, opts) : [];
|
|
576
|
+
const rest = p.rest(undefined, [...(setup?.attachments === undefined ? [] : [setup.attachments]), ...idle.map(f => f.attachments ?? [])]);
|
|
577
|
+
return { setup, idle, rest };
|
|
578
|
+
}).value;
|
|
579
|
+
const sources = sourceProblems(input.rig, [...(sampled.setup === null ? [] : [sampled.setup]), ...sampled.idle]);
|
|
580
|
+
if (sources.length > 0) return fail(sources.join('; '));
|
|
581
|
+
} catch (e) { return fail(`official renderer/source unavailable: ${e instanceof Error ? e.message : String(e)}`); }
|
|
582
|
+
let stock: BehindSeamsStockPair[] | null = null, stockError: string | null = null;
|
|
583
|
+
try {
|
|
584
|
+
if (input.stockDir === null || sampled.setup === null) throw new Error('existing full stock seam render/setup is unavailable');
|
|
585
|
+
stock = stockOf(input.stockDir, input.pairs.filter(p => p.unread === undefined), input.fps, sampled.setup, sampled.idle, sampled.rest);
|
|
586
|
+
} catch (e) { stockError = e instanceof Error ? e.message : String(e); }
|
|
587
|
+
const line = measureBehindSeamFrames(api, { ...sampled, pairs: input.pairs, pages: candidate.pages, stock, fps: input.fps }, declared, problems);
|
|
588
|
+
if (stockError !== null) {
|
|
589
|
+
line.status = 'FAIL';
|
|
590
|
+
line.unmeasured.push({ ...checkRow('BEHIND_PART_SEAMS.unmeasured', 'rig', true, []), pair: null, frame: 'stock render', reason: `HOLE: ${stockError}` });
|
|
591
|
+
problems.push({ code: BEHIND_SEAM_UNMEASURED_CODE, object: 'stock full seam render', detail: `HOLE: ${stockError}; unavailable stock coverage cannot become zero motion-opened cells` });
|
|
592
|
+
}
|
|
593
|
+
return line;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/** Null has no JSON/tally effect, but its console declaration is always honest. */
|
|
597
|
+
export function behindSeamLines(line: BehindSeamsMeasured | null): string[] {
|
|
598
|
+
if (line === null) return [`${BEHIND_SEAM_LINE}: SKIP — ${UNDECLARED_BEHIND_SEAM_REASON}`];
|
|
599
|
+
return [
|
|
600
|
+
`${BEHIND_SEAM_LINE}: ${line.status} — ${line.pairs.length} selected pair(s), setup separately and every idle frame at ${line.fps} fps; ${line.limitations}`,
|
|
601
|
+
...line.pairs.flatMap(p => {
|
|
602
|
+
const setup = p.setup;
|
|
603
|
+
const summary = (kind: 'motion_opened' | 'standing'): string => {
|
|
604
|
+
const r = p[kind], w = r.worst;
|
|
605
|
+
return `${BEHIND_SEAM_LINE}: pair "${p.pair}" ${kind} ${r.status} — ${r.hit_frames}/${p.frames.length} idle hit frames, sampled/read/unmeasured ${p.sampled_frames ?? p.frames.length}/${p.read_frames ?? 0}/${p.unmeasured_frames ?? 0}, max_px ${r.max_px}, max_run ${r.max_run.frames} frames (${r.max_run.seconds} s), max_spatial_run_cells ${r.max_spatial_run_cells} (${r.max_spatial_run_world} world units), earliest worst ${w === null ? 'none' : `frame ${w.index} t ${w.time}, cell ${w.cell}, box [${w.box.join(', ')}]`}; threshold ${p.threshold === null ? 'undeclared' : p.threshold}, allowance ${p.allowance}`;
|
|
606
|
+
};
|
|
607
|
+
const setupStanding = setup === null ? '' : setup.standing.px > 0 && setup.standing.status === 'SKIP' ? `${setup.standing.px} cells` : `${setup.standing.status}, ${setup.standing.px} cells`;
|
|
608
|
+
return [summary('motion_opened'), summary('standing'), `${BEHIND_SEAM_LINE}: pair "${p.pair}" setup ${setup === null ? 'SKIP — HOLE: setup not read' : `standing ${setupStanding}; motion_opened ${setup.motion_opened.status}, ${setup.motion_opened.px} cells; behind [${setup.behind_slots.join(', ')}]`}`];
|
|
609
|
+
}),
|
|
610
|
+
...line.unmeasured.map(u => {
|
|
611
|
+
const target = u.pair === null ? 'rig' : `pair "${u.pair}"`;
|
|
612
|
+
const pair = u.pair === null ? undefined : line.pairs.find(p => p.pair === u.pair);
|
|
613
|
+
const frame = pair === undefined ? undefined : u.frame === 'setup' ? pair.setup ?? undefined : pair.frames.find(f => `idle frame ${f.index}` === u.frame);
|
|
614
|
+
const corridors = frame?.standing.unread_corridors;
|
|
615
|
+
if (frame !== undefined && frame.standing.px > 0 && corridors !== undefined && corridors.length > 0 && u.reason.includes('unread corridors')) {
|
|
616
|
+
const status = frame.standing.status === 'FAIL' ? `FAIL, ${frame.standing.px} cells` : `${frame.standing.px} cells`;
|
|
617
|
+
return `${BEHIND_SEAM_LINE}: ${target} ${u.frame} standing ${status} — unread corridors: ${corridors.length} (cells ${frame.standing.unread_cells ?? 0}); ${u.reason}`;
|
|
618
|
+
}
|
|
619
|
+
return `${BEHIND_SEAM_LINE}: ${target} ${u.frame} SKIP — ${u.reason}`;
|
|
620
|
+
}),
|
|
621
|
+
];
|
|
622
|
+
}
|