@rig-c/parts 1.50.0 → 1.52.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/src/assemble.ts CHANGED
@@ -2571,6 +2571,8 @@ export interface CutCount {
2571
2571
  underEmpty?: number;
2572
2572
  /** With `under_fill: "colour"` only (issue #360): the `under` pixels took the image's colour at the alpha the cut removed; absent without it. */
2573
2573
  underFill?: 'colour';
2574
+ /** Issue #564, only on a cut from a part a limb takes, lying past the limb: the joint it lies past and the segment below it, with its bone. */
2575
+ limb?: { limb: string; joint: string; part: string; bone: string };
2574
2576
  }
2575
2577
 
2576
2578
  /**
@@ -2588,7 +2590,8 @@ export function cutLines(cuts: readonly CutCount[]): string[] {
2588
2590
  const colour = cs.some((c) => c.underFill === 'colour') ? ` (${cs.reduce((a, c) => a + (c.underFill === 'colour' ? (c.under ?? 0) : 0), 0)} colour only, at the alpha the cut removed)` : '';
2589
2591
  return (
2590
2592
  ` cut: "${from}" opaque ${cs[0].before} = "${from}" ${cs[0].kept} + ${cs.map((c) => `"${c.into}" ${c.taken}`).join(' + ')}; band ${cs.reduce((a, c) => a + c.band, 0)} px held by both` +
2591
- (cs.some((c) => c.under !== undefined) ? `; under ${cs.reduce((a, c) => a + (c.under ?? 0), 0)} px filled${colour}, ${cs.reduce((a, c) => a + (c.underEmpty ?? 0), 0)} px left empty` : '')
2593
+ (cs.some((c) => c.under !== undefined) ? `; under ${cs.reduce((a, c) => a + (c.under ?? 0), 0)} px filled${colour}, ${cs.reduce((a, c) => a + (c.underEmpty ?? 0), 0)} px left empty` : '') +
2594
+ cs.map((c) => (c.limb === undefined ? '' : `; "${c.into}" lies past joint "${c.limb.joint}" of limb "${c.limb.limb}", in the band of "${c.limb.part}" on bone "${c.limb.bone}"`)).join('')
2592
2595
  );
2593
2596
  });
2594
2597
  }
@@ -2701,7 +2704,18 @@ export interface LimbCount {
2701
2704
  before: number;
2702
2705
  shares: Array<{ part: string; px: number }>;
2703
2706
  /** The joints whose cap this part's segments carry, in `config.limbs` order. */
2704
- joints: Array<{ limb: string; joint: string; part: string; upper: string; cap: CapRecord }>;
2707
+ joints: Array<{
2708
+ limb: string;
2709
+ joint: string;
2710
+ part: string;
2711
+ upper: string;
2712
+ cap: CapRecord;
2713
+ /** Issue #557: the parts the lower and the upper segment take, the half-width about the joint, and the short pixels within it. */
2714
+ from?: string;
2715
+ upperFrom?: string;
2716
+ half?: number;
2717
+ shortInHalf?: number;
2718
+ }>;
2705
2719
  }
2706
2720
 
2707
2721
  /**
@@ -2720,6 +2734,41 @@ function cutPhrase(k: CapRecord, upper: string, lower: string): string {
2720
2734
  return `cut_band ${k.cut_band ?? 0}: the cut ${where}, through [${(k.cut_at ?? k.at).join(', ')}], crosses ${k.cut_crossed ?? 0} stroke px; the joint line crosses ${k.joint_crossed ?? 0} (${stroke})`;
2721
2735
  }
2722
2736
 
2737
+ /**
2738
+ * Issue #557: the `LINT` line of a short cap — one whose disc, on the upper
2739
+ * side of the cut, holds pixels no layer of the limb gives (`short_px`, by
2740
+ * side of the limb's axis: `left` where a pixel centre's offset across the
2741
+ * axis is 0 or above, the side toward smaller x when the limb hangs down the
2742
+ * rig, `right` the other) — naming the limb, the joint, the part, both
2743
+ * figures, the radius and what filled the rest; with the half-disc, how many
2744
+ * of them `cap_shape: "disc"` would keep. Null for a cap that is not short.
2745
+ */
2746
+ export function shortCapLint(j: LimbCount['joints'][number]): string | null {
2747
+ const k = j.cap;
2748
+ if (k.short_px === undefined) return null;
2749
+ const n = k.short_px.left + k.short_px.right;
2750
+ const where =
2751
+ j.from === undefined || j.upperFrom === undefined || j.upperFrom === j.from
2752
+ ? `the layer of "${j.from ?? j.part}" has no pixel of the limb`
2753
+ : `neither the layer of "${j.from}" nor that of "${j.upperFrom}" has a pixel of the limb`;
2754
+ const filled = [
2755
+ ...(k.image_px === undefined ? [] : [`${k.image_px} px from ${k.image_layer === undefined ? 'the cap image' : `the hidden-art layer ${k.image_layer}`}`]),
2756
+ `${k.layer_px} px from "${j.part}"'s layer`,
2757
+ `${k.upper_px ?? 0} px from "${j.upper}"`,
2758
+ ].join(', ');
2759
+ const half = j.half ?? 0;
2760
+ const disc =
2761
+ k.shape === 'disc' || j.shortInHalf === undefined
2762
+ ? ''
2763
+ : j.shortInHalf === 0
2764
+ ? `; under cap_shape "disc" the disc is the half-width about the joint, ${half} px, and none of them lies within it`
2765
+ : `; under cap_shape "disc" the disc is the half-width about the joint, ${half} px, and ${j.shortInHalf} of them lie within it`;
2766
+ return (
2767
+ ` LINT "${j.part}" cap at [${k.at[0]}, ${k.at[1]}] (limb "${j.limb}", joint "${j.joint}", radius ${k.radius} px): short_px left ${k.short_px.left}, right ${k.short_px.right}: ` +
2768
+ `no layer gives them — ${n} px of the disc on the upper side of the cut, where ${where}; the rest of the disc is filled, ${filled}${disc}`
2769
+ );
2770
+ }
2771
+
2723
2772
  /**
2724
2773
  * The lines `assemble` prints for the limbs (issue #349), one per part a limb
2725
2774
  * took — ` limb cut: "<of>" opaque <n> = "<part>" <px> + …` — and one per
@@ -2733,7 +2782,8 @@ export function limbLines(counts: readonly LimbCount[]): string[] {
2733
2782
  const k = j.cap;
2734
2783
  const base = k.image_px === undefined ? `${k.layer_px} px from the layer` : `${k.image_px} px from ${k.image_layer === undefined ? 'the cap image' : `the hidden-art layer ${k.image_layer}`}, ${k.layer_px} px from the layer`;
2735
2784
  // Issue #360: what cap_shape "disc" and edge "feather" did, said only when declared, so a limb without them prints what it did.
2736
- const from = k.shape === undefined ? base : `${base}, ${k.upper_px ?? 0} px from "${j.upper}"`;
2785
+ // Issue #557: the half-disc says it only when the neighbouring segment gave a pixel, so a full cap prints what it printed.
2786
+ const from = k.upper_px === undefined ? base : `${base}, ${k.upper_px} px from "${j.upper}"`;
2737
2787
  const radius =
2738
2788
  k.shape === undefined
2739
2789
  ? `a disc of radius ${k.radius} px (the limb ${k.width} px across the joint line)`
@@ -2749,6 +2799,8 @@ export function limbLines(counts: readonly LimbCount[]): string[] {
2749
2799
  out.push(
2750
2800
  ` limb cap: "${j.limb}" joint "${j.joint}" at [${k.at[0]}, ${k.at[1]}], on "${j.part}": ${radius}, box [${k.box.join(', ')}] — on the upper side of the line ${from}, ${miss}${shaped.map((q) => `; ${q}`).join('')}`,
2751
2801
  );
2802
+ const lint = shortCapLint(j);
2803
+ if (lint !== null) out.push(lint);
2752
2804
  }
2753
2805
  }
2754
2806
  return out;
@@ -2789,6 +2841,108 @@ function shiftedAt(line: JointLine, offset: number): [number, number] {
2789
2841
  return [pyRound(line.at[0] + offset * line.axis[0], 3), pyRound(line.at[1] + offset * line.axis[1], 3)];
2790
2842
  }
2791
2843
 
2844
+ /**
2845
+ * Issue #564: where an `assemble.cuts` entry from a part a limb takes lies
2846
+ * against that limb. The segments of a limb that take the part are a run
2847
+ * `k0 .. k1` (`CONFIG_LIMB_FROM_RUN` keeps them consecutive), and the run's
2848
+ * pixels lie between two joint lines: the line of the joint above `k0` when
2849
+ * `k0` sits below one (a joint between two layers), and the line of the joint
2850
+ * below `k1` when a segment follows it (the ankle under a shin that takes the
2851
+ * legwear). A pixel the cut takes (alpha above 0 in its piece) is outside the
2852
+ * run when it lies past the lower line, or not past the upper line and — read
2853
+ * after the limbs are cut, with `caps` — outside the disc of that joint's cap,
2854
+ * which the run's own layer fills there first. Anything else lies in a
2855
+ * segment's band, and the cut is refused: the pixels would leave a segment
2856
+ * that holds them, and the bend would open where the layer was cut. Every
2857
+ * limb taking the part is read. `past` is the segment a taken pixel lies in
2858
+ * when every one lies past the lower line — the one below that joint, or
2859
+ * below the deepest joint line all of them are past — and is what the `cut:`
2860
+ * line names. Without `caps` the lines alone are read; with them only the
2861
+ * discs, for the pixels the lines let through.
2862
+ */
2863
+ function limbBands(
2864
+ cuts: readonly Cut[],
2865
+ pieces: ReadonlyArray<Built | null>,
2866
+ limbs: Readonly<Record<string, Limb>>,
2867
+ bones: readonly BoneEntry[],
2868
+ W: number,
2869
+ H: number,
2870
+ caps: ((limb: string, segment: number) => CapRecord | undefined) | null,
2871
+ ): { problems: Problem[]; past: Map<number, { limb: string; joint: string; part: string; bone: string }> } {
2872
+ const problems: Problem[] = [];
2873
+ const past = new Map<number, { limb: string; joint: string; part: string; bone: string }>();
2874
+ const places = bonePlaces(bones);
2875
+ const lineAbove = (l: string, k: number): JointLine | null => {
2876
+ const segs = limbs[l].segments;
2877
+ const up = places.get(segs[k - 1].bone);
2878
+ const me = places.get(segs[k].bone);
2879
+ if (up === undefined || me === undefined) return null;
2880
+ const end = me.end ?? (k + 1 < segs.length ? (places.get(segs[k + 1].bone)?.at ?? null) : null);
2881
+ return jointLine(up.at, me.at, end);
2882
+ };
2883
+ const fmt = (q: readonly number[]): string => `[${q.map((n) => pyRound(n, 3)).join(', ')}]`;
2884
+ cuts.forEach((q, i) => {
2885
+ const piece = pieces[i];
2886
+ if (piece === null) return;
2887
+ const taken: number[] = [];
2888
+ for (let p = 0; p < W * H; p++) if (piece.r.data[p * 4 + 3] > 0) taken.push(p);
2889
+ for (const l of Object.keys(limbs)) {
2890
+ const segs = limbs[l].segments;
2891
+ const k0 = segs.findIndex((sg) => sg.from === q.from);
2892
+ if (k0 < 0) continue;
2893
+ let k1 = k0;
2894
+ while (k1 + 1 < segs.length && segs[k1 + 1].from === q.from) k1++;
2895
+ const upper = k0 > 0 ? lineAbove(l, k0) : null;
2896
+ const lower = k1 + 1 < segs.length ? lineAbove(l, k1 + 1) : null;
2897
+ // A joint with no line is ASSEMBLE_LIMB_JOINT, refused by cutLimbs by name; nothing is read about it here.
2898
+ if ((k0 > 0 && upper === null) || (k1 + 1 < segs.length && lower === null)) continue;
2899
+ const cap = caps === null || upper === null ? undefined : caps(l, k0);
2900
+ const inside: number[] = [];
2901
+ let below = 0;
2902
+ for (const p of taken) {
2903
+ const x = p % W;
2904
+ const y = Math.floor(p / W);
2905
+ if (lower !== null && pastJoint(lower, x, y)) {
2906
+ below++;
2907
+ continue;
2908
+ }
2909
+ if (upper !== null && !pastJoint(upper, x, y)) {
2910
+ if (caps === null || cap === undefined || !inDisc(cap.at, cap.radius, x, y)) continue;
2911
+ } else if (caps !== null) continue;
2912
+ inside.push(p);
2913
+ }
2914
+ const object = `config.assemble.cuts[${i}] (part "${q.into}")`;
2915
+ const wanted = [
2916
+ ...(lower === null ? [] : [`past joint "${String(segs[k1 + 1].joint)}"'s line (through ${fmt(lower.at)}, axis ${fmt(lower.axis)})`]),
2917
+ ...(upper === null ? [] : [`before joint "${String(segs[k0].joint)}"'s line (through ${fmt(upper.at)}, axis ${fmt(upper.axis)})${cap === undefined ? '' : ` and outside its cap's disc, radius ${cap.radius} px about ${fmt(cap.at)}`}`]),
2918
+ ];
2919
+ if (inside.length > 0) {
2920
+ const f = inside[0];
2921
+ const fx = f % W;
2922
+ const fy = Math.floor(f / W);
2923
+ let k = k0;
2924
+ if (caps === null) {
2925
+ for (let ln = k < k1 ? lineAbove(l, k + 1) : null; ln !== null && pastJoint(ln, fx, fy); ln = k < k1 ? lineAbove(l, k + 1) : null) k++;
2926
+ }
2927
+ const band =
2928
+ caps !== null
2929
+ ? `the cap of joint "${String(segs[k0].joint)}" on "${segs[k0].part}" (radius ${(cap as CapRecord).radius} px about ${fmt((cap as CapRecord).at)}), which "${q.from}"'s pixels fill there`
2930
+ : `the band of "${segs[k].part}"${k > 0 ? ` below joint "${String(segs[k].joint)}"` : ''}${k + 1 < segs.length ? `${k > 0 ? ' and' : ''} above joint "${String(segs[k + 1].joint)}"` : ' to the limb\'s end'}`;
2931
+ problems.push({
2932
+ code: 'ASSEMBLE_CUT_CROSSES_LIMB',
2933
+ object,
2934
+ detail: `takes ${inside.length} of its ${taken.length} pixel(s) of "${q.from}" inside limb "${l}", the first at ${fx},${fy}, in ${band}; "${q.from}" is the layer config.limbs.${l} cuts at its joints, and a cut from it may lie only where no segment of the limb holds it — every pixel it takes ${wanted.length === 0 ? `outside the limb, and the limb has no joint before or past its segments that take "${q.from}" (they are all of it), so no pixel is` : wanted.join(', or ')} is required: move the polygon, or give the pixels to a segment of the limb instead`,
2935
+ });
2936
+ } else if (caps === null && lower !== null && below === taken.length && taken.length > 0) {
2937
+ let j = k1 + 1;
2938
+ for (let ln = j + 1 < segs.length ? lineAbove(l, j + 1) : null; ln !== null && taken.every((p) => pastJoint(ln as JointLine, p % W, Math.floor(p / W))); ln = j + 1 < segs.length ? lineAbove(l, j + 1) : null) j++;
2939
+ if (!past.has(i)) past.set(i, { limb: l, joint: String(segs[j].joint), part: segs[j].part, bone: segs[j].bone });
2940
+ }
2941
+ }
2942
+ });
2943
+ return { problems, past };
2944
+ }
2945
+
2792
2946
  /**
2793
2947
  * Cut every declared limb (module comment of `src/limbs.ts`): each part a limb
2794
2948
  * takes is split among the limbs that take it (nearest polyline), then each
@@ -3051,6 +3205,11 @@ function cutLimbs(
3051
3205
  let imagePx = 0;
3052
3206
  let upperPx = 0;
3053
3207
  let missing = 0;
3208
+ // Issue #557: the cap's shortfall by side of the limb's axis (left: across >= 0), and how much of it lies within
3209
+ // the half-width about the joint, the disc cap_shape "disc" would keep.
3210
+ let shortLeft = 0;
3211
+ let shortRight = 0;
3212
+ let shortInHalf = 0;
3054
3213
  for (let y = Math.max(0, box[1]); y < Math.min(H, box[1] + box[3]); y++) {
3055
3214
  for (let x = Math.max(0, box[0]); x < Math.min(W, box[0] + box[2]); x++) {
3056
3215
  if (!inDisc(line.at, radius, x, y) || pastJoint(cut, x, y)) continue;
@@ -3062,11 +3221,18 @@ function cutLimbs(
3062
3221
  } else if (ownLower[p] === li && lowerSrc.r.data[p * 4 + 3] > 0) {
3063
3222
  lower.data.set(lowerSrc.r.data.subarray(p * 4, p * 4 + 4), p * 4);
3064
3223
  layerPx++;
3065
- } else if (disc && upper.data[p * 4 + 3] > 0) {
3224
+ } else if (upper.data[p * 4 + 3] > 0) {
3066
3225
  // Issue #360: the disc is filled from the art — the upper segment's, where the lower's layer has none.
3226
+ // Issue #557: under either shape — the half-disc too takes the neighbouring segment's pixels, so a cap is
3227
+ // short only where no layer of the limb gives a pixel.
3067
3228
  lower.data.set(upper.data.subarray(p * 4, p * 4 + 4), p * 4);
3068
3229
  upperPx++;
3069
- } else missing++;
3230
+ } else {
3231
+ missing++;
3232
+ if (jointOffsets(line, x, y).across >= 0) shortLeft++;
3233
+ else shortRight++;
3234
+ if (inDisc(line.at, half, x, y)) shortInHalf++;
3235
+ }
3070
3236
  }
3071
3237
  }
3072
3238
  if (img !== null && imagePx === 0 && hidden === null) {
@@ -3111,7 +3277,8 @@ function cutLimbs(
3111
3277
  ...(img === null ? {} : { image_px: imagePx }),
3112
3278
  ...(hidden === null ? {} : { image_layer: hidden.layer }),
3113
3279
  missing_px: missing,
3114
- ...(disc ? { shape: 'disc' as const, upper_px: upperPx } : {}),
3280
+ ...(missing === 0 ? {} : { short_px: { left: shortLeft, right: shortRight } }),
3281
+ ...(disc ? { shape: 'disc' as const, upper_px: upperPx } : upperPx === 0 ? {} : { upper_px: upperPx }),
3115
3282
  ...(sg.edge === 'feather' ? { edge: 'feather' as const, feather_radius: band, feathered_px: featheredPx } : {}),
3116
3283
  ...(placed === undefined
3117
3284
  ? {}
@@ -3128,7 +3295,7 @@ function cutLimbs(
3128
3295
  if (hidden !== null) readHidden(hidden.layer, `config.limbs.${l}.segments[${k}].cap (part "${sg.part}")`);
3129
3296
  parts[at].record.cap = cap;
3130
3297
  const c = counts.find((q) => q.of === sg.from);
3131
- if (c !== undefined) c.joints.push({ limb: l, joint: String(sg.joint), part: sg.part, upper: segs[k - 1].part, cap });
3298
+ if (c !== undefined) c.joints.push({ limb: l, joint: String(sg.joint), part: sg.part, upper: segs[k - 1].part, cap, from: sg.from, upperFrom: upFrom, half: pyRound(half, 3), shortInHalf });
3132
3299
  });
3133
3300
  }
3134
3301
  refuseIfAny(problems);
@@ -3657,6 +3824,15 @@ export function assemble(input: AssembleInput): AssembleResult {
3657
3824
  // In cut order, as the config lists them.
3658
3825
  cutCounts.sort((a, b) => cuts.findIndex((q) => q.into === a.into) - cuts.findIndex((q) => q.into === b.into));
3659
3826
 
3827
+ // Issue #564: a cut from a part a limb takes lies outside the limb's segments, read before the limbs are cut.
3828
+ const bands = input.limbs === undefined || cuts.length === 0 ? null : limbBands(cuts, pieces, input.limbs, input.bones ?? [], W, H, null);
3829
+ if (bands !== null) {
3830
+ refuseIfAny(bands.problems);
3831
+ for (const [i, at] of bands.past) {
3832
+ const c = cutCounts.find((q) => q.into === cuts[i].into);
3833
+ if (c !== undefined) c.limb = at;
3834
+ }
3835
+ }
3660
3836
  // Issue #349: the limbs, after the cuts (a limb may take a cut's piece, and a cut's under image fills its base first).
3661
3837
  const lim = cutLimbs(input.limbs ?? {}, input.bones ?? [], input.caps ?? new Map(), W, H, (name) => {
3662
3838
  const pi = plan.findIndex((e) => e[0] === name);
@@ -3664,6 +3840,8 @@ export function assemble(input: AssembleInput): AssembleResult {
3664
3840
  const ci = cuts.findIndex((q) => q.into === name);
3665
3841
  return ci >= 0 ? pieces[ci] : null;
3666
3842
  }, opaqueOf, mergedIn, count, (name) => hiddenArt.get(name) ?? null, (layer, by) => hiddenUses.get(layer)?.push(by));
3843
+ // Issue #564: and the cap a run's own layer fills above its upper joint, read once the limbs are cut.
3844
+ if (bands !== null) refuseIfAny(limbBands(cuts, pieces, input.limbs ?? {}, input.bones ?? [], W, H, (l, k) => lim.parts.find((q) => q.limb === l && q.segment === k)?.record.cap).problems);
3667
3845
  const limbPieces = lim.pieces;
3668
3846
  const order = limbOrder(order0, plan, cuts, lim.parts);
3669
3847
  const pieceAt = (d: { plan: number } | { cut: number } | { limb: number }): { name: string; b: Built } =>
package/src/build.ts CHANGED
@@ -1146,21 +1146,39 @@ function multiIntervalRun(
1146
1146
  * the gate is green — and, for every automatic mesh, only when its motion gate
1147
1147
  * accepted it (issue #126 item 3; {@link motionGates}).
1148
1148
  */
1149
- export function rigStage(input: RigStageInput, rigc: RigcRunner, scratch: string, log: Log): RigOutput {
1150
- const { config: cfg, animations: from } = loadConfigAndAnimations(input.config);
1151
- const parts = readParts(join(input.parts, 'parts.json'));
1149
+ /** What the rig stage reads before it builds anything: the config with its `animations_from` table, parts.json, and each part's PNG (and each alternate set's) that exists. */
1150
+ export interface RigInputs {
1151
+ cfg: ReturnType<typeof loadConfigAndAnimations>['config'];
1152
+ from: ReturnType<typeof loadConfigAndAnimations>['animations'];
1153
+ parts: ReturnType<typeof readParts>;
1154
+ images: Map<string, Raster>;
1155
+ }
1156
+
1157
+ /**
1158
+ * The rig stage's inputs, read as {@link rigStage} reads them: `rigStage` and `mesh-stats` (issue #566,
1159
+ * `src/meshstats.ts`) both start here, so the two read the same config, the same parts and the same images, and a
1160
+ * PNG that is missing is left out of the map for `buildRig` to refuse by name (`RIG_PNG_PRESENT`).
1161
+ */
1162
+ export function readRigInputs(config: string, partsDir: string): RigInputs {
1163
+ const { config: cfg, animations: from } = loadConfigAndAnimations(config);
1164
+ const parts = readParts(join(partsDir, 'parts.json'));
1152
1165
  const images = new Map<string, Raster>();
1153
1166
  for (const p of parts.parts) {
1154
- const png = join(input.parts, 'parts', `${p.name}.png`);
1167
+ const png = join(partsDir, 'parts', `${p.name}.png`);
1155
1168
  if (existsSync(png)) images.set(p.name, readPng(png));
1156
1169
  }
1157
1170
  for (const alt of parts.alternates ?? []) {
1158
1171
  for (const p of alt.records) {
1159
1172
  const attName = `${alt.name}_${p.name}`;
1160
- const png = join(input.parts, 'parts', `${attName}.png`);
1173
+ const png = join(partsDir, 'parts', `${attName}.png`);
1161
1174
  if (existsSync(png)) images.set(attName, readPng(png));
1162
1175
  }
1163
1176
  }
1177
+ return { cfg, from, parts, images };
1178
+ }
1179
+
1180
+ export function rigStage(input: RigStageInput, rigc: RigcRunner, scratch: string, log: Log): RigOutput {
1181
+ const { cfg, from, parts, images } = readRigInputs(input.config, input.parts);
1164
1182
  const first = buildRig(cfg, parts, images, undefined, input.idleKeys ?? DEFAULT_IDLE_KEYS, input.reduce);
1165
1183
  const textsOf = (r: RigOutput): Array<[string, string]> => {
1166
1184
  const mergedMotion = withAnimations(r.motion, from === null ? null : from.table);
@@ -1504,14 +1522,18 @@ export function limbCheckLines(line: LimbLine): string[] {
1504
1522
  * The seam over the range (issue #360) as the console prints it: the status, what was posed and the bar, then each
1505
1523
  * joint at each angle on a line of its own — its status and the seam figures there — or, not read, why.
1506
1524
  */
1507
- export function sweepCheckLines(line: SweepLine): string[] {
1525
+ export function sweepCheckLines(line: SweepLine, steps: ReadonlyMap<string, { left: number; right: number }> = new Map()): string[] {
1508
1526
  const s = line.frame_scale;
1509
1527
  return [
1510
1528
  `${SWEEP_LINE}: ${line.status} — ${line.joints.length} joint(s) bent to their declared range's ends and middle${line.physics_left_out === 0 ? '' : `, ${line.physics_left_out} physics constraint(s) left out`}; at each, the pose against the same pose with the joint's own art drawn under the limb, on the rest-pose seam's bar (mean |d| <= ${line.bar.mean.toFixed(1)}, <= ${line.bar.rig_px_over_40} rig px over ${line.bar.level})${s === null ? '' : ` at frame scale ${s}`}`,
1511
1529
  ...line.joints.flatMap((j) => {
1512
1530
  const head = `joint "${j.joint}" (${j.limb}, "${j.part}" on "${j.bone}", range [${j.range.join(', ')}]${j.shape === 'disc' ? `, cap_shape "disc": no pixel over ${line.bar.level} in the disc` : ''})`;
1513
1531
  if (j.unread !== undefined) return [`${head}: FAIL — not read: ${j.unread}`];
1514
- return j.poses.map((p) => `${head} at ${p.degrees}°: ${p.status} — mean |d| ${p.mean}/255, ${p.px_over_40} frame px over ${line.bar.level} (${p.px_over_80} over ${SEAM_PX_LEVEL_HIGH}), ${p.rig_px_over_40.toFixed(2)} rig px`);
1532
+ // Issue #557: the contour step, a reading on every row (check.json carries it only for a short cap).
1533
+ return j.poses.map((p) => {
1534
+ const st = p.step_px ?? steps.get(`${j.part} ${p.degrees}`);
1535
+ return `${head} at ${p.degrees}°: ${p.status} — mean |d| ${p.mean}/255, ${p.px_over_40} frame px over ${line.bar.level} (${p.px_over_80} over ${SEAM_PX_LEVEL_HIGH}), ${p.rig_px_over_40.toFixed(2)} rig px${st === undefined ? '' : `; contour step ${st.left} px left, ${st.right} px right (a reading)`}`;
1536
+ });
1515
1537
  }),
1516
1538
  ];
1517
1539
  }
@@ -1610,7 +1632,7 @@ export function checkStage(input: CheckStageInput, rigc: RigcRunner, bin: string
1610
1632
  const limbs = fig[LIMB_LINE];
1611
1633
  if (limbs !== undefined) for (const [i, l] of limbCheckLines(limbs).entries()) log(` ${i === 0 ? '' : ' '}${l}`);
1612
1634
  const sweep = fig[SWEEP_LINE];
1613
- if (sweep !== undefined) for (const [i, l] of sweepCheckLines(sweep).entries()) log(` ${i === 0 ? '' : ' '}${l}`);
1635
+ if (sweep !== undefined) for (const [i, l] of sweepCheckLines(sweep, r.sweepSteps).entries()) log(` ${i === 0 ? '' : ' '}${l}`);
1614
1636
  for (const [i, l] of motionSeamLines(r.motionSeam).entries()) log(` ${i === 0 ? '' : ' '}${l}`);
1615
1637
  for (const [i, l] of clothLines(r.cloth).entries()) log(` ${i === 0 ? '' : ' '}${l}`);
1616
1638
  for (const name of REPORTED_LINES) log(` ${judgementLine(name, fig[name])}`);
package/src/check.ts CHANGED
@@ -2141,6 +2141,11 @@ export interface CheckReport {
2141
2141
  motionSeam: MotionSeamLine;
2142
2142
  /** `CLOTH_SETTLES` (issue #501): a warning, never a bar — in `check.json` only when the rig declares a physics constraint and has an idle; its SKIP is printed either way. */
2143
2143
  cloth: ClothLine;
2144
+ /**
2145
+ * Issue #557: the contour step at each pose `SEAM_OVER_RANGE` read, by `<part> <degrees>` ({@link contourStep}) —
2146
+ * printed on every row; `check.json` carries it only for a joint whose cap is short ({@link SweepPose}).
2147
+ */
2148
+ sweepSteps?: Map<string, { left: number; right: number }>;
2144
2149
  }
2145
2150
 
2146
2151
  /**
@@ -2980,6 +2985,10 @@ export interface SweepJoint {
2980
2985
  radius: number;
2981
2986
  /** Issue #360: the cap's record says `shape: "disc"` — the disc is held to no pixel uncovered ({@link sweepLine}). */
2982
2987
  disc?: true;
2988
+ /** Issue #557: the upper segment's bone (the limb record one segment up), whose direction the contour step is read across; null when parts.json has none. */
2989
+ upperBone?: string | null;
2990
+ /** Issue #557: the cap's record carries `short_px` — the step is then written into check.json ({@link SweepPose}). */
2991
+ short?: { left: number; right: number };
2983
2992
  }
2984
2993
 
2985
2994
  /** Every limb record in `parts.json` that declares a `range`, in `parts.json`'s order. */
@@ -2988,7 +2997,20 @@ export function sweepJoints(parts: PartsFile): SweepJoint[] {
2988
2997
  for (const p of parts.parts) {
2989
2998
  const l = p.limb;
2990
2999
  if (l === undefined || l.range === undefined || l.cap === undefined || l.joint === undefined) continue;
2991
- out.push({ part: p.name, limb: l.name, segment: l.segment, joint: l.joint, bone: l.bone, range: [l.range[0], l.range[1]], at: [l.cap.at[0], l.cap.at[1]], radius: l.cap.radius, ...(l.cap.shape === 'disc' ? { disc: true as const } : {}) });
3000
+ const up = parts.parts.find((q) => q.limb?.name === l.name && q.limb.segment === l.segment - 1)?.limb?.bone ?? null;
3001
+ out.push({
3002
+ part: p.name,
3003
+ limb: l.name,
3004
+ segment: l.segment,
3005
+ joint: l.joint,
3006
+ bone: l.bone,
3007
+ range: [l.range[0], l.range[1]],
3008
+ at: [l.cap.at[0], l.cap.at[1]],
3009
+ radius: l.cap.radius,
3010
+ ...(l.cap.shape === 'disc' ? { disc: true as const } : {}),
3011
+ upperBone: up,
3012
+ ...(l.cap.short_px === undefined ? {} : { short: { left: l.cap.short_px.left, right: l.cap.short_px.right } }),
3013
+ });
2992
3014
  }
2993
3015
  return out;
2994
3016
  }
@@ -3052,6 +3074,90 @@ export function sweepUnread(j: SweepJoint, rig: Record<string, unknown>, limbSlo
3052
3074
  return null;
3053
3075
  }
3054
3076
 
3077
+ /** The setup pose's world transform of every bone `rig.json` declares (rigc's `computeExactFrameTransforms`, through `src/coords.ts`). */
3078
+ function setupWorld(rig: Record<string, unknown>): Map<string, BoneTransform> {
3079
+ const bones = (Array.isArray(rig.bones) ? rig.bones : []).filter(isRecord);
3080
+ const posable: Parameters<typeof computeExactFrameTransforms>[0][number][] = bones.map((b) => ({
3081
+ name: String(b.name),
3082
+ ...(typeof b.parent === 'string' && b.parent !== '' ? { parent: b.parent } : {}),
3083
+ ...Object.fromEntries((['x', 'y', 'rotation', 'scaleX', 'scaleY', 'shearX', 'shearY'] as const).filter((k) => typeof b[k] === 'number').map((k) => [k, b[k] as number])),
3084
+ ...(typeof b.inherit === 'string' ? { inherit: b.inherit } : {}),
3085
+ }));
3086
+ return computeExactFrameTransforms(posable);
3087
+ }
3088
+
3089
+ /**
3090
+ * Issue #557: the direction the contour step is read across — the upper
3091
+ * segment's, from its bone's origin to the joint's bone's origin at the setup
3092
+ * pose (the first of the two directions `src/limbs.ts` `jointLine` sums), as a
3093
+ * unit vector in rig px, y down: the Spine world's difference with its y
3094
+ * turned. The upper segment does not move while the joint bends, so lines
3095
+ * across it are the same lines at every pose. Null when either bone is not in
3096
+ * `rig.json` or the two origins coincide.
3097
+ */
3098
+ export function sweepAxis(rig: Record<string, unknown>, j: Pick<SweepJoint, 'bone' | 'upperBone'>): [number, number] | null {
3099
+ if (j.upperBone === undefined || j.upperBone === null) return null;
3100
+ const world = setupWorld(rig);
3101
+ const up = world.get(j.upperBone);
3102
+ const me = world.get(j.bone);
3103
+ if (up === undefined || me === undefined) return null;
3104
+ const dx = me.worldX - up.worldX;
3105
+ const dy = up.worldY - me.worldY;
3106
+ const l = edgeLength(dx, dy);
3107
+ return l === 0 ? null : [dx / l, dy / l];
3108
+ }
3109
+
3110
+ /** Issue #557: how far the contour step's lines run out from the axis, in cap radii — past the limb's edge on either side, which lies at one radius. */
3111
+ export const STEP_REACH_RADII = 2;
3112
+
3113
+ /**
3114
+ * Issue #557: the contour step at a bent pose — the step the eye sees in the
3115
+ * limb's outline where the cut meets it. On the frame `image` (rigc's render,
3116
+ * over `background`), lines across the limb perpendicular to `axis` (rig px,
3117
+ * unit, y down) at every frame pixel along it from one cap radius before the
3118
+ * joint line to one radius past it; on each, from the axis outward on each
3119
+ * side, the silhouette's extent: the distance, in rig px, of the first sample
3120
+ * whose largest channel lies within {@link SEAM_PX_LEVEL} of the background
3121
+ * (samples one frame pixel apart, out to {@link STEP_REACH_RADII} radii; a
3122
+ * sample is the frame pixel `map` carries the rig point onto, nearest). The
3123
+ * step on a side is the largest change in that extent between two adjacent
3124
+ * lines, to 3 places. `left` is the side the cap's `short_px` calls left
3125
+ * (across the axis at or above 0, `src/limbs.ts` `jointOffsets`), `right` the
3126
+ * other. A straight full limb reads about one frame pixel — the sampling's
3127
+ * own; a notch the bend opens in the outline reads its depth.
3128
+ */
3129
+ export function contourStep(image: Raster, background: readonly number[], map: ScaleTranslate, at: readonly [number, number], axis: readonly [number, number], radius: number): { left: number; right: number } {
3130
+ const d = 1 / map.sx;
3131
+ const across: [number, number] = [-axis[1], axis[0]];
3132
+ const solid = (px: number, py: number): boolean => {
3133
+ const fx = Math.round(map.sx * (px - 0.5) + map.tx);
3134
+ const fy = Math.round(map.sy * (py - 0.5) + map.ty);
3135
+ if (fx < 0 || fy < 0 || fx >= image.width || fy >= image.height) return false;
3136
+ const o = (fy * image.width + fx) * 4;
3137
+ return Math.max(Math.abs(image.data[o] - background[0]), Math.abs(image.data[o + 1] - background[1]), Math.abs(image.data[o + 2] - background[2])) > SEAM_PX_LEVEL;
3138
+ };
3139
+ const along = Math.floor(radius * map.sx);
3140
+ const out = Math.ceil(STEP_REACH_RADII * radius * map.sx);
3141
+ const extent = (k: number, side: 1 | -1): number => {
3142
+ const cx = at[0] + k * d * axis[0];
3143
+ const cy = at[1] + k * d * axis[1];
3144
+ let m = 0;
3145
+ while (m <= out && solid(cx + side * m * d * across[0], cy + side * m * d * across[1])) m++;
3146
+ return m * d;
3147
+ };
3148
+ const step = (side: 1 | -1): number => {
3149
+ let best = 0;
3150
+ let prev = extent(-along, side);
3151
+ for (let k = -along + 1; k <= along; k++) {
3152
+ const e = extent(k, side);
3153
+ best = Math.max(best, Math.abs(e - prev));
3154
+ prev = e;
3155
+ }
3156
+ return round3(best);
3157
+ };
3158
+ return { left: step(1), right: step(-1) };
3159
+ }
3160
+
3055
3161
  /**
3056
3162
  * The throwaway copy (see {@link SWEEP_LINE}): `rig` with one underlay slot per
3057
3163
  * read joint — a region on the parent of the joint's bone, centred on its box
@@ -3076,13 +3182,7 @@ export function sweepCopy(
3076
3182
  files: ReadonlySet<string> = new Set(),
3077
3183
  ): { rig: Record<string, unknown>; motion: Record<string, unknown>; slots: string[]; images: Array<[string, Raster]> } {
3078
3184
  const bones = (Array.isArray(rig.bones) ? rig.bones : []).filter(isRecord);
3079
- const posable: Parameters<typeof computeExactFrameTransforms>[0][number][] = bones.map((b) => ({
3080
- name: String(b.name),
3081
- ...(typeof b.parent === 'string' && b.parent !== '' ? { parent: b.parent } : {}),
3082
- ...Object.fromEntries((['x', 'y', 'rotation', 'scaleX', 'scaleY', 'shearX', 'shearY'] as const).filter((k) => typeof b[k] === 'number').map((k) => [k, b[k] as number])),
3083
- ...(typeof b.inherit === 'string' ? { inherit: b.inherit } : {}),
3084
- }));
3085
- const world = computeExactFrameTransforms(posable);
3185
+ const world = setupWorld(rig);
3086
3186
  const slots = (Array.isArray(rig.slots) ? rig.slots : []).filter(isRecord).map((s) => ({ ...s }));
3087
3187
  const skins = isRecord(rig.skins) ? rig.skins : {};
3088
3188
  const skin: Record<string, unknown> = { ...(isRecord(skins.default) ? skins.default : {}) };
@@ -3119,6 +3219,13 @@ export interface SweepPose {
3119
3219
  px_over_40: number;
3120
3220
  px_over_80: number;
3121
3221
  rig_px_over_40: number;
3222
+ /**
3223
+ * Issue #557, written only for a joint whose cap record carries `short_px`:
3224
+ * the contour step at this pose ({@link contourStep}), rig px, per side of
3225
+ * the limb. A reading, never a bar; a full cap's row prints it and writes
3226
+ * what it wrote.
3227
+ */
3228
+ step_px?: { left: number; right: number };
3122
3229
  }
3123
3230
 
3124
3231
  /** One joint's row: its limb record, its disc, how many of the joint's own art pixels the disc holds, and each pose — or, not read, why. */
@@ -3163,6 +3270,8 @@ export function sweepLine(
3163
3270
  frames: (animation: string) => { under: FrameSet; bare: FrameSet },
3164
3271
  physicsLeftOut: number,
3165
3272
  problems: Problem[],
3273
+ step: (j: SweepJoint, bare: FrameSet) => { left: number; right: number } | null = () => null,
3274
+ printed: Map<string, { left: number; right: number }> = new Map(),
3166
3275
  ): SweepLine {
3167
3276
  const seen: { grid: FrameSet | null } = { grid: null };
3168
3277
  const rows: SweepRow[] = joints.map((j, i) => {
@@ -3212,7 +3321,10 @@ export function sweepLine(
3212
3321
  `mean <= ${SEAM_MEAN_BAR.toFixed(1)} and <= ${SEAM_PX_BAR} rig px over ${SEAM_PX_LEVEL} are required, the rest-pose seam's bar — the bend uncovers the joint, and a cap that covers the turn (the limb's art within that radius on the upper side of the joint line, carried by "${j.part}") keeps it covered`,
3213
3322
  });
3214
3323
  }
3215
- return { degrees: a, status: fail ? 'FAIL' : 'PASS', mean, px_over_40: f.over40, px_over_80: f.over80, rig_px_over_40: Number(seamRigPxText(f.over40, scale)) };
3324
+ // Issue #557: the contour step on the bent frame — a reading, recorded only where the cap is short.
3325
+ const st = step(j, bare);
3326
+ if (st !== null) printed.set(`${j.part} ${a}`, st);
3327
+ return { degrees: a, status: fail ? 'FAIL' : 'PASS', mean, px_over_40: f.over40, px_over_80: f.over80, rig_px_over_40: Number(seamRigPxText(f.over40, scale)), ...(st === null || j.short === undefined ? {} : { step_px: st }) };
3216
3328
  });
3217
3329
  return { ...head, status: poses.some((p) => p.status === 'FAIL') ? 'FAIL' : 'PASS', poses };
3218
3330
  });
@@ -4068,6 +4180,7 @@ export function runCheck(rigDir: string, outDir: string, rigc: RigcRunner, parts
4068
4180
  // joints' own art under the limbs, physics left out, rendered twice by rigc on one grid — none without a range
4069
4181
  let sweep: SweepLine | null = null;
4070
4182
  const sweepProblems: Problem[] = [];
4183
+ const sweepSteps = new Map<string, { left: number; right: number }>();
4071
4184
  const joints = inp.parts === null ? [] : sweepJoints(inp.parts);
4072
4185
  if (joints.length > 0) {
4073
4186
  const parts = inp.parts as PartsFile;
@@ -4126,7 +4239,13 @@ export function runCheck(rigDir: string, outDir: string, rigc: RigcRunner, parts
4126
4239
  }
4127
4240
  });
4128
4241
  }
4129
- sweep = sweepLine(joints, under.map((u) => u.px), unread, (a) => sets.get(a) as { under: FrameSet; bare: FrameSet }, stripped === null ? 0 : stripped.physics.length, sweepProblems);
4242
+ // Issue #557: the contour step, read across the upper segment on each bent frame without the underlays.
4243
+ const axes = joints.map((j) => sweepAxis(base, j));
4244
+ const stepOf = (j: SweepJoint, bare: FrameSet): { left: number; right: number } | null => {
4245
+ const axis = axes[joints.indexOf(j)];
4246
+ return axis === null ? null : contourStep(bare.frames[0].image, bare.background, rigToFrameMap(inp.stage, parts.rig_size[1], bare.viewport), j.at, axis, j.radius);
4247
+ };
4248
+ sweep = sweepLine(joints, under.map((u) => u.px), unread, (a) => sets.get(a) as { under: FrameSet; bare: FrameSet }, stripped === null ? 0 : stripped.physics.length, sweepProblems, stepOf, sweepSteps);
4130
4249
  } finally {
4131
4250
  rmSync(sweepDir, { recursive: true, force: true });
4132
4251
  }
@@ -4450,6 +4569,7 @@ export function runCheck(rigDir: string, outDir: string, rigc: RigcRunner, parts
4450
4569
  besideIdle: named,
4451
4570
  motionSeam,
4452
4571
  cloth,
4572
+ ...(sweepSteps.size === 0 ? {} : { sweepSteps }),
4453
4573
  };
4454
4574
  }
4455
4575