spine-parts 2.23.0 → 2.23.2
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/docs/AUTHORING.md +46 -0
- package/package.json +1 -1
- package/src/assemble.ts +21 -3
- package/src/build.ts +15 -1
- package/src/paintingedges.ts +166 -2
- package/src/paintingowner.ts +90 -0
package/docs/AUTHORING.md
CHANGED
|
@@ -798,6 +798,28 @@ existing geometry refusal and its message.
|
|
|
798
798
|
Region take entries accept the same optional `crop` and use this transport before
|
|
799
799
|
selecting the seeded component on the target run canvas.
|
|
800
800
|
|
|
801
|
+
### Owner coverage transfers (`painting_allocation.json`, issue #1146)
|
|
802
|
+
|
|
803
|
+
Under `assemble.project: "visible"`, assemble writes `painting_allocation.json`
|
|
804
|
+
beside `parts.json`. Other projection modes remove a stale report on output
|
|
805
|
+
reuse. Its `spec` is `rig-parts-painting-allocation/1`, `grid` is the native rig
|
|
806
|
+
size, `code` is `PAINTING_EDGE_COVERAGE_TRANSFER`, and `stage` names paintingEdges
|
|
807
|
+
after piece guarding and restoration.
|
|
808
|
+
The report includes the owner `supplier`, `refused` reason counts, `lineage`
|
|
809
|
+
faults, `fallbackHeld` count and `coverageTransfers` rows.
|
|
810
|
+
|
|
811
|
+
Each transfer names `front`, `receiver` and raw-plan `owner`, with
|
|
812
|
+
`pixels.count` and `pixels.spans`. Spans are sorted native rig
|
|
813
|
+
`[y, left, right-exclusive]` coordinates. A row requires an accepted allocation
|
|
814
|
+
to a receiver drawn behind FRONT, positive FRONT alpha in the legacy scratch
|
|
815
|
+
evaluation, zero applied FRONT alpha and positive applied receiver alpha.
|
|
816
|
+
Held clears and restored FRONT coverage are excluded. This is an edge-pass
|
|
817
|
+
observation, before later assembly stages. It establishes neither painted
|
|
818
|
+
strand order nor motion-space gaps.
|
|
819
|
+
|
|
820
|
+
`PAINTING_EDGE_COVERAGE_TRANSFER` prints the pixel count and report filename.
|
|
821
|
+
The report observes the existing allocation and leaves its raster writes intact.
|
|
822
|
+
|
|
801
823
|
### Residual front/back edges (`edge_residuals.json`, issue #999)
|
|
802
824
|
|
|
803
825
|
Under `assemble.project: "visible"`, assemble writes `edge_residuals.json` beside
|
|
@@ -4442,6 +4464,29 @@ and 13,872 to 1,110 (`wind`), and the holes from 1,020 to 62, 453 to 40, 259 to
|
|
|
4442
4464
|
291 to 34 and 1,118 to 62; `check` passes the same bars as before on all five. A
|
|
4443
4465
|
painting patch is read placed on the rig canvas, and is never written. `core` is unchanged.
|
|
4444
4466
|
|
|
4467
|
+
**Painting ownership at a seam (#1038).** The edge estimator receives a named
|
|
4468
|
+
owner input. Its current supplier, `owner-a/raw-plan-alpha>=128`, selects the
|
|
4469
|
+
topmost original plan layer whose alpha is at least 128 after placement on the
|
|
4470
|
+
rig canvas. Receivers follow explicit cut `from` to `into`, limb `from` to
|
|
4471
|
+
`part`, and hybrid descendant edges. A receiver needs exactly one raw-plan
|
|
4472
|
+
ancestor, positive pre-edge support and membership in its production region.
|
|
4473
|
+
Missing lineage, cycles, multiple ancestors, overlapping receivers and absent
|
|
4474
|
+
support are named in the assembly result's `paintingEdges.allocation` reading.
|
|
4475
|
+
|
|
4476
|
+
A FRONT-owned positive-coverage pixel keeps the estimator's unmix. BEHIND-owned
|
|
4477
|
+
paint goes to BEHIND and FRONT is cleared. If piece preservation holds only the
|
|
4478
|
+
clear, BEHIND keeps its own paint and FRONT keeps its pre-edge coverage.
|
|
4479
|
+
FRONT-owned pixels at resolved alpha zero take their own painting pixel at
|
|
4480
|
+
alpha 255. A refused allocation retains the existing edge write unless it is a
|
|
4481
|
+
foreign-owner FRONT write or a contract-foreign BEHIND copy. That predicate is
|
|
4482
|
+
computed on scratch rasters through the existing edge path, reading survival at
|
|
4483
|
+
the edge pass's post-restoration output. Later assembly writers are outside its
|
|
4484
|
+
scope. The applied allocation and clean-refusal fallbacks form one plan, guarded
|
|
4485
|
+
once for piece preservation. A held fallback returns to pre-edge bytes and is
|
|
4486
|
+
counted by `fallbackHeld`. Scratch evaluation leaves the real input untouched.
|
|
4487
|
+
The counted public-example rest exceptions are tracked separately in #1038
|
|
4488
|
+
and #1141. The remaining silhouette cause in #1141 is unclosed.
|
|
4489
|
+
|
|
4445
4490
|
**A busy ground (`assemble.ground`, issue #996).** The step above reads the painting
|
|
4446
4491
|
as a figure on a ground of one colour: a painted pixel no part covers is the figure's
|
|
4447
4492
|
when it differs from the border ring's colour, and the colour behind a part's edge is
|
|
@@ -5243,6 +5288,7 @@ those settings. Changing a config seed alone cannot change a no-config run.
|
|
|
5243
5288
|
| <a id="ASSEMBLE_TAKE_DEPTH_AMBIGUOUS"></a>`ASSEMBLE_TAKE_DEPTH_AMBIGUOUS` | different original runs have equal or null depths | use a run with distinct numeric depths; do not invent an inter-run tie order |
|
|
5244
5289
|
| <a id="ASSEMBLE_TAKE_PLAN_ORDER"></a>`ASSEMBLE_TAKE_PLAN_ORDER` | supplied plan reverses an overlapping mixed-run pair: two-run full-layer head roles first, scalar depth within a role band (head-run and single-run rows use scalar depth); names the parts and support intersection | use the order drafted by `assemble --propose-plan`; fallback headwear follows the body and precedes the face, front hair follows the face; disjoint supports impose no plan-order constraint |
|
|
5245
5290
|
| <a id="EDGE_PAIR_RESIDUAL"></a>`EDGE_PAIR_RESIDUAL` | report-only front/back residual candidates after visible edge composition, with pixel spans, boxes and coverage | read measured candidates and explicit skipped/unavailable reasons in `edge_residuals.json`. No pass/fail threshold is applied |
|
|
5291
|
+
| <a id="PAINTING_EDGE_COVERAGE_TRANSFER"></a>`PAINTING_EDGE_COVERAGE_TRANSFER` | accepted allocation clears legacy FRONT coverage into a BEHIND owner | read native rig membership in `painting_allocation.json`. Painted order and motion gaps require separate evidence |
|
|
5246
5292
|
| <a id="ASSEMBLE_REGION_TAKE_REFERENCE_IMPLICIT"></a>`ASSEMBLE_REGION_TAKE_REFERENCE_IMPLICIT` | warning, `reference` is absent and the predicate uses `--source` | declare a painting reference and its PNG hash to keep the mask independent of projection-source changes |
|
|
5247
5293
|
| <a id="ASSEMBLE_REGION_TAKE_REFERENCE"></a>`ASSEMBLE_REGION_TAKE_REFERENCE` | a declared region reference is unreadable | supply the declared PNG, with `source: true` or `painting` and its byte hash |
|
|
5248
5294
|
| <a id="ASSEMBLE_REGION_TAKE_REFERENCE_MATCH"></a>`ASSEMBLE_REGION_TAKE_REFERENCE_MATCH` | a reference hash or painting dimensions differ | supply the declared PNG and preserve the source geometry |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "spine-parts",
|
|
3
|
-
"version": "2.23.
|
|
3
|
+
"version": "2.23.2",
|
|
4
4
|
"description": "AI-authored 2D character parts from one anime painting and its See-through layer decomposition: measured parts, weighted-mesh rig specs, verified through rig-c before anything is written. A CLI for agents that cannot see the image.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
package/src/assemble.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { paintingReceivers, rawPaintingOwner } from './paintingowner.ts';
|
|
1
2
|
import { hiddenExtentProblems, type HiddenExtentDeclaration, type HiddenExtentSource } from './hiddenextent.ts';
|
|
2
3
|
import { headBoxInside } from './headbox.ts';
|
|
3
4
|
import { edgePairResiduals, type EdgeResidualReport } from './edgeresidual.ts';
|
|
4
5
|
import type { SupportDomain } from './coincide.ts';
|
|
5
6
|
import { type SeamRule } from './seamrule.ts';
|
|
6
7
|
import { DEFAULT_GROUND, DEFAULT_PROJECT_RULE, type Ground, type ProjectRule } from './projectrule.ts';
|
|
7
|
-
import { paintingEdges, type PaintingEdgeSkips } from './paintingedges.ts';
|
|
8
|
+
import { paintingEdges, type PaintingAllocationReading, type PaintingEdgeSkips } from './paintingedges.ts';
|
|
8
9
|
import { edgeLength } from './length.ts';
|
|
9
10
|
import { materialSource, type MaterialSource, materialRows, polygonMaterialEdges, type MaterialRow, type MaterialEdge, type PlacedArt } from './cutmaterial.ts';
|
|
10
11
|
import { unionTakenSources } from './takesource.ts';
|
|
@@ -4785,6 +4786,7 @@ export interface AssembleResult {
|
|
|
4785
4786
|
|
|
4786
4787
|
/** Issue #953: the edge pixels `paintingEdges` resolved, the part pixels it changed, and the edge pixels it left, by reason. */
|
|
4787
4788
|
export interface PaintingEdgeReading {
|
|
4789
|
+
allocation?: PaintingAllocationReading;
|
|
4788
4790
|
resolved: number;
|
|
4789
4791
|
unseparated: number;
|
|
4790
4792
|
changed: number;
|
|
@@ -5859,7 +5861,23 @@ function assembleParts(input: AssembleInput, targets?: ReadonlySet<string>, befo
|
|
|
5859
5861
|
}
|
|
5860
5862
|
return whole;
|
|
5861
5863
|
};
|
|
5862
|
-
const
|
|
5864
|
+
const canvases = stack.map(onRig);
|
|
5865
|
+
const owner = rawPaintingOwner(input.plan.map(([name, run, tag]) => {
|
|
5866
|
+
const layer = input[run].layers.find(l => l.name === tag)!;
|
|
5867
|
+
return { name, raster: layerToRig(placeLayer(layer, input.resolution), frame, run) };
|
|
5868
|
+
}), W * H);
|
|
5869
|
+
const lineage: Array<[string, string]> = [
|
|
5870
|
+
...input.cuts.map(c => [c.from, c.into] as [string, string]),
|
|
5871
|
+
...Object.values(input.limbs ?? {}).flatMap(l => l.segments.flatMap(segment => [
|
|
5872
|
+
[segment.from, segment.part] as [string, string],
|
|
5873
|
+
...(segment.hybrid === undefined ? [] : [[segment.from, segment.hybrid.part] as [string, string]]),
|
|
5874
|
+
])),
|
|
5875
|
+
];
|
|
5876
|
+
const receivers = paintingReceivers(owner, order.map((d, i) => ({
|
|
5877
|
+
name: stack[i].record.name, image: canvases[i], region: 'patch' in d ? null : pieceAt(d).b.region,
|
|
5878
|
+
})), lineage);
|
|
5879
|
+
const e = paintingEdges(canvases, srcr, {
|
|
5880
|
+
owner: receivers,
|
|
5863
5881
|
painted: supportPainted,
|
|
5864
5882
|
figure: visibleFigure,
|
|
5865
5883
|
...(input.declaredFigure === undefined ? {} : { declaredFigure: input.declaredFigure }),
|
|
@@ -5880,7 +5898,7 @@ function assembleParts(input: AssembleInput, targets?: ReadonlySet<string>, befo
|
|
|
5880
5898
|
order.forEach((d, i) => {
|
|
5881
5899
|
if (!('patch' in d) && e.changed[i].data.some((v) => v === 1)) edged.set(pieceAt(d).name, e.changed[i]);
|
|
5882
5900
|
});
|
|
5883
|
-
edgeReading = { resolved: e.resolved, unseparated: e.unseparated, changed: e.changed.reduce((a, m) => a + m.data.reduce((x, v) => x + v, 0), 0), skipped: e.skipped };
|
|
5901
|
+
edgeReading = { resolved: e.resolved, unseparated: e.unseparated, changed: e.changed.reduce((a, m) => a + m.data.reduce((x, v) => x + v, 0), 0), skipped: e.skipped, allocation: e.allocation };
|
|
5884
5902
|
}
|
|
5885
5903
|
const pushedOf: FringePushed[][] = plan.map(() => []);
|
|
5886
5904
|
const pushedOfCut: FringePushed[][] = cuts.map(() => []);
|
package/src/build.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { SUPPORT_DOMAINS_FILE, writeSupportDomains } from './supportdomains.ts';
|
|
|
6
6
|
import { COINCIDE_LINE, supportLines } from './coincide.ts';
|
|
7
7
|
import { DEFAULT_SEAM_RULE } from './seamrule.ts';
|
|
8
8
|
import { DEFAULT_PROJECT_RULE } from './projectrule.ts';
|
|
9
|
-
import { EDGE_CONTRAST } from './paintingedges.ts';
|
|
9
|
+
import { EDGE_CONTRAST, PAINTING_ALLOCATION_FILE, PAINTING_EDGE_COVERAGE_TRANSFER } from './paintingedges.ts';
|
|
10
10
|
import { SETUP_PAINTING_LINE, setupPaintingLines } from './setuppainting.ts';
|
|
11
11
|
import { boundaryColourLines } from './boundarycolour.ts';
|
|
12
12
|
import { writeMaterialSources } from './cutmaterialio.ts';
|
|
@@ -846,6 +846,13 @@ export function assembleStage(input: AssembleStageInput, outs: AssembleOutputs,
|
|
|
846
846
|
const residualPath = join(dirname(outs.partsJson), EDGE_RESIDUAL_FILE);
|
|
847
847
|
if (result.edgeResiduals === undefined) rmSync(residualPath, { force: true });
|
|
848
848
|
else writeFileSync(residualPath, JSON.stringify(result.edgeResiduals, null, 2) + '\n');
|
|
849
|
+
const allocationPath = join(dirname(outs.partsJson), PAINTING_ALLOCATION_FILE);
|
|
850
|
+
const allocation = result.paintingEdges?.allocation;
|
|
851
|
+
if (allocation === undefined) rmSync(allocationPath, { force: true });
|
|
852
|
+
else writeFileSync(allocationPath, JSON.stringify({ spec: 'rig-parts-painting-allocation/1',
|
|
853
|
+
code: PAINTING_EDGE_COVERAGE_TRANSFER,
|
|
854
|
+
stage: 'paintingEdges after piece guarding and restoration', grid: result.parts.rig_size,
|
|
855
|
+
...allocation }, null, 2) + '\n');
|
|
849
856
|
for (const { record, image, depthImage } of result.images) {
|
|
850
857
|
writePng(join(outs.partsDir, `${record.name}.png`), image);
|
|
851
858
|
if (depthImage !== undefined && record.depth !== undefined) writePng(join(outs.partsDir, record.depth.image), depthImage);
|
|
@@ -873,6 +880,13 @@ export function assembleStage(input: AssembleStageInput, outs: AssembleOutputs,
|
|
|
873
880
|
if (result.paintingEdges !== undefined) {
|
|
874
881
|
const e = result.paintingEdges;
|
|
875
882
|
log(`painting edges: ${e.resolved} edge px resolved from the painting (${e.unseparated} against the two reference colours where the window's far sides overlap), ${e.changed} part px changed; left as they were: ${e.skipped.cover} under a part the two-layer model does not carry, ${e.skipped.contrast} where the painting draws no edge (contrast below ${EDGE_CONTRAST}), ${e.skipped.locked} kept, feathered, a patch or a turn's moving pair, ${e.skipped.noBehind} with no behind pixel in the window; ${e.skipped.pieces} part px not written so a part's art keeps its pieces`);
|
|
883
|
+
if (e.allocation !== undefined) {
|
|
884
|
+
const a = e.allocation;
|
|
885
|
+
log(`painting owner: ${a.supplier}; ${a.fallbackHeld} clean-fallback part px held by piece preservation`);
|
|
886
|
+
log(`${PAINTING_EDGE_COVERAGE_TRANSFER}: ${a.coverageTransfers.reduce((n, row) => n + row.pixels.count, 0)} native rig px; membership in ${PAINTING_ALLOCATION_FILE}`);
|
|
887
|
+
for (const [reason, px] of Object.entries(a.refused)) log(` painting owner refused: ${reason}, ${px} edge px`);
|
|
888
|
+
for (const fault of a.lineage) log(` painting receiver refused: "${fault.receiver}", ${fault.reason}; one declared raw-plan ancestor is required`);
|
|
889
|
+
}
|
|
876
890
|
// Issue #996: under a declared busy ground, what the two ground rules left.
|
|
877
891
|
if (fields.ground === 'busy') log(` busy ground (assemble.ground): ${e.skipped.mix} behind-side px left that are not a mix of the two reference colours, ${e.skipped.support} part px not raised outside the part's own support`);
|
|
878
892
|
}
|
package/src/paintingedges.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { foreignPaintingReceiver, paintingReceiverAt, type PaintingReceivers } from './paintingowner.ts';
|
|
1
2
|
import { OPAQUE_ALPHA_ABOVE } from './layers.ts';
|
|
2
3
|
import { connectedComponents } from './raster/components.ts';
|
|
3
4
|
import { type Ground } from './projectrule.ts';
|
|
@@ -131,6 +132,12 @@ import { type Mask, newMask, type Raster, RasterError } from './raster/types.ts'
|
|
|
131
132
|
* Under a plain ground (the default) none of the three applies, and the step is
|
|
132
133
|
* the one described above, byte for byte.
|
|
133
134
|
*
|
|
135
|
+
* With a named `owner` input, allocation follows that supplier's explicit
|
|
136
|
+
* receiver relation after coverage estimation. FRONT-owned positive coverage
|
|
137
|
+
* keeps this unmix; zero coverage receives P at alpha 255. BEHIND receives its
|
|
138
|
+
* own P with FRONT cleared, retaining BEHIND if only the clear is held. Clean
|
|
139
|
+
* refusal fallbacks join the allocation before the applied piece guard.
|
|
140
|
+
*
|
|
134
141
|
* Every decision is read off the stack as it stood before the step, then
|
|
135
142
|
* applied, so the result does not depend on the order pixels are visited.
|
|
136
143
|
* Deterministic: integer rig arithmetic, medians by sort, no float reduction
|
|
@@ -141,6 +148,8 @@ export const EDGE_RADIUS = 3;
|
|
|
141
148
|
export const EDGE_OWNER_ALPHA = 128;
|
|
142
149
|
/** The least front contrast resolved, max channel. Equal to `ERROR_LIMIT` in `src/assemble.ts` (held by the selftest). */
|
|
143
150
|
export const EDGE_CONTRAST = 40;
|
|
151
|
+
export const PAINTING_ALLOCATION_FILE = 'painting_allocation.json';
|
|
152
|
+
export const PAINTING_EDGE_COVERAGE_TRANSFER = 'PAINTING_EDGE_COVERAGE_TRANSFER';
|
|
144
153
|
|
|
145
154
|
/** Why an edge pixel was left as it was; see {@link paintingEdges}. */
|
|
146
155
|
export interface PaintingEdgeSkips {
|
|
@@ -159,6 +168,8 @@ export interface PaintingEdgeSkips {
|
|
|
159
168
|
export interface PaintingEdgeResult {
|
|
160
169
|
/** Per part (stack index), the pixels whose RGBA the step changed. */
|
|
161
170
|
changed: Mask[];
|
|
171
|
+
/** Named owner/receiver refusals and held clean-fallback writes. */
|
|
172
|
+
allocation?: PaintingAllocationReading;
|
|
162
173
|
/** Edge pixels resolved (whether or not a value changed), and the ones left. */
|
|
163
174
|
resolved: number;
|
|
164
175
|
/** Of `resolved`, the pixels whose two far sides did not separate along the axis, read against `Cb` and `Cf` themselves. */
|
|
@@ -166,7 +177,25 @@ export interface PaintingEdgeResult {
|
|
|
166
177
|
skipped: PaintingEdgeSkips;
|
|
167
178
|
}
|
|
168
179
|
|
|
180
|
+
export interface PaintingAllocationReading {
|
|
181
|
+
supplier: string;
|
|
182
|
+
refused: Record<string, number>;
|
|
183
|
+
lineage: PaintingReceivers['faults'];
|
|
184
|
+
fallbackHeld: number;
|
|
185
|
+
/** Accepted transfers after piece guarding and restoration. These observe
|
|
186
|
+
* allocation, without inferring painted order or motion-space coverage. */
|
|
187
|
+
coverageTransfers: Array<{
|
|
188
|
+
front: string;
|
|
189
|
+
receiver: string;
|
|
190
|
+
owner: string;
|
|
191
|
+
/** Native rig pixels, sorted [y, left, right-exclusive] spans. */
|
|
192
|
+
pixels: { count: number; spans: Array<[number, number, number]> };
|
|
193
|
+
}>;
|
|
194
|
+
}
|
|
195
|
+
|
|
169
196
|
export interface PaintingEdgeOptions {
|
|
197
|
+
/** Named supplier and explicit receiver contract, frozen before the edge pass. */
|
|
198
|
+
owner?: PaintingReceivers;
|
|
170
199
|
/**
|
|
171
200
|
* Per rig pixel, 1 where the painting shows figure (`paintedFigure` in
|
|
172
201
|
* `src/assemble.ts`). A figure pixel no part covers above
|
|
@@ -250,7 +279,15 @@ interface Decision {
|
|
|
250
279
|
}
|
|
251
280
|
|
|
252
281
|
/** See the invariant above {@link EDGE_RADIUS}. Modifies `parts` (full rig canvases, draw order) in place. */
|
|
253
|
-
|
|
282
|
+
function applyPaintingEdges(
|
|
283
|
+
parts: Raster[], srcr: Raster, options: PaintingEdgeOptions,
|
|
284
|
+
existing?: { parts: Raster[]; defects: Uint8Array },
|
|
285
|
+
readDecision?: (q: Decision, alpha: number) => void,
|
|
286
|
+
): PaintingEdgeResult {
|
|
287
|
+
const allocation = options.owner;
|
|
288
|
+
const events: Array<{ p: number; front: number; behind: number; target: number; affected: number[]; status: string; reason: string }> = [];
|
|
289
|
+
const reading: PaintingAllocationReading | undefined = allocation === undefined ? undefined
|
|
290
|
+
: { supplier: allocation.supplier, refused: {}, lineage: allocation.faults, fallbackHeld: 0, coverageTransfers: [] };
|
|
254
291
|
const W = srcr.width;
|
|
255
292
|
const H = srcr.height;
|
|
256
293
|
const n = parts.length;
|
|
@@ -523,6 +560,32 @@ export function paintingEdges(parts: Raster[], srcr: Raster, options: PaintingEd
|
|
|
523
560
|
if (alpha > 0 && alpha < 255) while (alpha < 255 && 2 * miss(alpha) > EDGE_CONTRAST) alpha++;
|
|
524
561
|
if (alpha >= 255 - OPAQUE_ALPHA_ABOVE) alpha = 255;
|
|
525
562
|
const unmix = (c: number): number => unmixAt(c, alpha);
|
|
563
|
+
readDecision?.(q, alpha);
|
|
564
|
+
if (allocation !== undefined && q.behind >= 0) {
|
|
565
|
+
const { target, reason } = paintingReceiverAt(allocation, q.p);
|
|
566
|
+
const affected = target < 0 ? [] : [...new Set([target, q.front, q.behind].filter(i => i >= target))];
|
|
567
|
+
const event = { p: q.p, front: q.front, behind: q.behind, target, affected, status: 'refused', reason };
|
|
568
|
+
events.push(event);
|
|
569
|
+
if (target < 0) return;
|
|
570
|
+
if (affected.some(i => !options.editable[i] || options.keep?.[i]?.data[q.p] === 1 || options.feathered?.[i]?.data[q.p] === 1) ||
|
|
571
|
+
affected.some(i => i !== target && options.movingPair?.(i, target) === true)) {
|
|
572
|
+
event.reason = 'protected-receiver';
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
if (target === q.front) {
|
|
576
|
+
if (alpha === 0) {
|
|
577
|
+
plan(q.front, q.p, P[0], P[1], P[2], 255);
|
|
578
|
+
event.status = 'front-zero';
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
event.status = 'front-unmix';
|
|
582
|
+
} else {
|
|
583
|
+
plan(target, q.p, P[0], P[1], P[2], 255);
|
|
584
|
+
for (const i of affected) if (i !== target) plan(i, q.p, 0, 0, 0, 0);
|
|
585
|
+
event.status = 'assigned';
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
526
589
|
// Busy ground: a BEHIND raise outside BEHIND's own support is refused in `plan`, so FRONT is not lowered there either —
|
|
527
590
|
// FRONT keeps the coverage it had, and a seam whose two supports do not overlap is never left transparent.
|
|
528
591
|
// The refused BEHIND raise below is the one counted in `skipped.support`.
|
|
@@ -539,6 +602,21 @@ export function paintingEdges(parts: Raster[], srcr: Raster, options: PaintingEd
|
|
|
539
602
|
fills[q.behind].set(q.p, q.front);
|
|
540
603
|
}
|
|
541
604
|
});
|
|
605
|
+
// Clean refusals belong to the same plan as allocations, before its one
|
|
606
|
+
// applied piece guard. A scratch legacy evaluation supplies the predicate.
|
|
607
|
+
const fallback: Array<{ p: number; part: number; desired: readonly number[] }> = [];
|
|
608
|
+
for (const event of events) {
|
|
609
|
+
if (event.status !== 'refused' || existing === undefined || existing.defects[event.p] === 1) continue;
|
|
610
|
+
for (let i = 0; i < n; i++) {
|
|
611
|
+
const rgba = existing.parts[i].data.subarray(event.p * 4, event.p * 4 + 4);
|
|
612
|
+
const pre = parts[i].data.subarray(event.p * 4, event.p * 4 + 4);
|
|
613
|
+
if (pre.every((v, c) => v === rgba[c])) planned[i].delete(event.p);
|
|
614
|
+
else {
|
|
615
|
+
planned[i].set(event.p, [rgba[0], rgba[1], rgba[2], rgba[3]]);
|
|
616
|
+
fallback.push({ p: event.p, part: i, desired: Array.from(rgba) });
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
}
|
|
542
620
|
// A part's art (alpha above the floor) keeps its pieces: rigc takes one closed outline per mesh, read 4-connected
|
|
543
621
|
// (`CONTOUR_ONE_ISLAND`). A new piece holding none of the part's art is not written.
|
|
544
622
|
planned.forEach((writes, i) => {
|
|
@@ -619,6 +697,43 @@ export function paintingEdges(parts: Raster[], srcr: Raster, options: PaintingEd
|
|
|
619
697
|
}
|
|
620
698
|
}
|
|
621
699
|
});
|
|
700
|
+
const effective = (part: number, p: number): ArrayLike<number> => planned[part].get(p) ?? parts[part].data.subarray(p * 4, p * 4 + 4);
|
|
701
|
+
if (reading !== undefined) {
|
|
702
|
+
for (const row of fallback) {
|
|
703
|
+
const rgba = effective(row.part, row.p);
|
|
704
|
+
if (row.desired.some((v, c) => v !== rgba[c])) reading.fallbackHeld++;
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
for (const event of events) {
|
|
708
|
+
const offset = event.p * 4;
|
|
709
|
+
const desired = [srcr.data[offset], srcr.data[offset + 1], srcr.data[offset + 2], 255];
|
|
710
|
+
if (event.status === 'assigned') {
|
|
711
|
+
const held = event.affected.filter(i => {
|
|
712
|
+
const expected = i === event.target ? desired : [0, 0, 0, 0];
|
|
713
|
+
const rgba = effective(i, event.p);
|
|
714
|
+
return expected.some((v, c) => v !== rgba[c]);
|
|
715
|
+
});
|
|
716
|
+
if (held.length > 0) {
|
|
717
|
+
if (!held.includes(event.target) && event.target === event.behind && held.every(i => i === event.front)) {
|
|
718
|
+
// A held clear retains FRONT; BEHIND keeps its accepted own paint.
|
|
719
|
+
planned[event.front].delete(event.p);
|
|
720
|
+
event.status = 'retained-front';
|
|
721
|
+
} else {
|
|
722
|
+
for (const i of event.affected) planned[i].delete(event.p);
|
|
723
|
+
event.status = 'refused';
|
|
724
|
+
event.reason = 'piece-guard-allocation-held';
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
} else if (event.status === 'front-zero') {
|
|
728
|
+
const rgba = effective(event.front, event.p);
|
|
729
|
+
if (desired.some((v, c) => v !== rgba[c])) {
|
|
730
|
+
planned[event.front].delete(event.p);
|
|
731
|
+
event.status = 'refused';
|
|
732
|
+
event.reason = 'front-owner-zero-coverage-held';
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
if (event.status === 'refused' && reading !== undefined) reading.refused[event.reason] = (reading.refused[event.reason] ?? 0) + 1;
|
|
736
|
+
}
|
|
622
737
|
const changed = parts.map(() => newMask(W, H));
|
|
623
738
|
planned.forEach((writes, i) => {
|
|
624
739
|
const d = parts[i].data;
|
|
@@ -727,5 +842,54 @@ export function paintingEdges(parts: Raster[], srcr: Raster, options: PaintingEd
|
|
|
727
842
|
for (const p of writes.keys()) options.written?.(i, p);
|
|
728
843
|
});
|
|
729
844
|
for (const [p, front] of restore) if (!planned[front].has(p)) options.written?.(front, p);
|
|
730
|
-
|
|
845
|
+
// The legacy FRONT surface was moved to an accepted BEHIND owner. Read the
|
|
846
|
+
// applied output, including restoration: a held clear is never a transfer.
|
|
847
|
+
if (reading !== undefined && allocation !== undefined && existing !== undefined) {
|
|
848
|
+
const groups = new Map<string, PaintingAllocationReading['coverageTransfers'][number]>();
|
|
849
|
+
for (const event of events) {
|
|
850
|
+
const { p, front, target } = event;
|
|
851
|
+
if (event.status !== 'assigned' || target >= front ||
|
|
852
|
+
existing.parts[front].data[p * 4 + 3] === 0 || parts[front].data[p * 4 + 3] !== 0 ||
|
|
853
|
+
parts[target].data[p * 4 + 3] === 0) continue;
|
|
854
|
+
const key = `${front}/${target}`;
|
|
855
|
+
let row = groups.get(key);
|
|
856
|
+
if (row === undefined) {
|
|
857
|
+
row = { front: allocation.receiverNames[front], receiver: allocation.receiverNames[target],
|
|
858
|
+
owner: allocation.names[allocation.pixels[p]], pixels: { count: 0, spans: [] } };
|
|
859
|
+
groups.set(key, row);
|
|
860
|
+
}
|
|
861
|
+
const x = p % W, y = Math.floor(p / W), previous = row.pixels.spans.at(-1);
|
|
862
|
+
if (previous !== undefined && previous[0] === y && previous[2] === x) previous[2]++;
|
|
863
|
+
else row.pixels.spans.push([y, x, x + 1]);
|
|
864
|
+
row.pixels.count++;
|
|
865
|
+
}
|
|
866
|
+
reading.coverageTransfers = [...groups.values()];
|
|
867
|
+
}
|
|
868
|
+
return { changed, resolved, unseparated, skipped, ...(reading === undefined ? {} : { allocation: reading }) };
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
/** Apply the owner-aware plan after evaluating clean fallback on scratch art. */
|
|
872
|
+
export function paintingEdges(parts: Raster[], srcr: Raster, options: PaintingEdgeOptions): PaintingEdgeResult {
|
|
873
|
+
const allocation = options.owner;
|
|
874
|
+
if (allocation === undefined) return applyPaintingEdges(parts, srcr, options);
|
|
875
|
+
const scratch = parts.map(r => ({ ...r, data: new Uint8ClampedArray(r.data) }));
|
|
876
|
+
const decisions: Array<{ p: number; front: number; behind: number; alpha: number }> = [];
|
|
877
|
+
const old = applyPaintingEdges(scratch, srcr, { ...options, owner: undefined, written: undefined }, undefined,
|
|
878
|
+
(q, alpha) => decisions.push({ p: q.p, front: q.front, behind: q.behind, alpha }));
|
|
879
|
+
const defects = new Uint8Array(srcr.width * srcr.height);
|
|
880
|
+
// Survival is read at this edge pass's post-restoration output. Later
|
|
881
|
+
// assembly stages may replace a writer; they are outside this predicate.
|
|
882
|
+
for (const { p, front, behind, alpha } of decisions) {
|
|
883
|
+
const owner = allocation.pixels[p];
|
|
884
|
+
if (owner < 0) continue;
|
|
885
|
+
const offset = p * 4;
|
|
886
|
+
if (old.changed[front].data[p] === 1 && scratch[front].data[offset + 3] > 0 &&
|
|
887
|
+
allocation.roots[front] !== owner) defects[p] = 1;
|
|
888
|
+
if (behind < 0 || alpha !== 0 || !foreignPaintingReceiver(allocation, p, behind)) continue;
|
|
889
|
+
const pre = parts[behind].data;
|
|
890
|
+
const post = scratch[behind].data;
|
|
891
|
+
if ([0, 1, 2].some(c => pre[offset + c] !== post[offset + c]) &&
|
|
892
|
+
[0, 1, 2].every(c => post[offset + c] === srcr.data[offset + c]) && post[offset + 3] === 255) defects[p] = 1;
|
|
893
|
+
}
|
|
894
|
+
return applyPaintingEdges(parts, srcr, options, { parts: scratch, defects });
|
|
731
895
|
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { type Mask, type Raster } from './raster/types.ts';
|
|
2
|
+
|
|
3
|
+
/** A named owner supplier, independent of the edge estimator and receivers. */
|
|
4
|
+
export interface PaintingOwner {
|
|
5
|
+
supplier: string;
|
|
6
|
+
names: readonly string[];
|
|
7
|
+
pixels: Int32Array;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface PaintingReceivers extends PaintingOwner {
|
|
11
|
+
receiverNames: readonly string[];
|
|
12
|
+
roots: Int32Array;
|
|
13
|
+
receivers: Int32Array;
|
|
14
|
+
reasons: string[];
|
|
15
|
+
faults: Array<{ receiver: string; reason: string }>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Owner (a), the last raw plan layer at alpha >=128 on the rig canvas. */
|
|
19
|
+
export function rawPaintingOwner(raw: ReadonlyArray<{ name: string; raster: Raster }>, pixels: number): PaintingOwner {
|
|
20
|
+
const owner = new Int32Array(pixels).fill(-1);
|
|
21
|
+
raw.forEach(({ raster }, i) => {
|
|
22
|
+
for (let p = 0; p < pixels; p++) if (raster.data[p * 4 + 3] >= 128) owner[p] = i;
|
|
23
|
+
});
|
|
24
|
+
return { supplier: 'owner-a/raw-plan-alpha>=128', names: raw.map(q => q.name), pixels: owner };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Resolve declared cut/limb ancestry, frozen support and production regions. */
|
|
28
|
+
export function paintingReceivers(
|
|
29
|
+
owner: PaintingOwner,
|
|
30
|
+
parts: ReadonlyArray<{ name: string; image: Raster; region: Mask | null }>,
|
|
31
|
+
lineage: ReadonlyArray<readonly [string, string]>,
|
|
32
|
+
): PaintingReceivers {
|
|
33
|
+
const parents = new Map<string, string[]>();
|
|
34
|
+
for (const [from, into] of lineage) parents.set(into, [...(parents.get(into) ?? []), from]);
|
|
35
|
+
const ancestors = (name: string, path: readonly string[] = []): string[] | null => {
|
|
36
|
+
if (path.includes(name)) return null;
|
|
37
|
+
const sources = parents.get(name) ?? [];
|
|
38
|
+
if (sources.length === 0) return owner.names.includes(name) ? [name] : [];
|
|
39
|
+
const roots: string[] = [];
|
|
40
|
+
for (const source of sources) {
|
|
41
|
+
const found = ancestors(source, [...path, name]);
|
|
42
|
+
if (found === null) return null;
|
|
43
|
+
roots.push(...found);
|
|
44
|
+
}
|
|
45
|
+
return [...new Set(roots)];
|
|
46
|
+
};
|
|
47
|
+
const roots = new Int32Array(parts.length).fill(-1);
|
|
48
|
+
const faults: PaintingReceivers['faults'] = [];
|
|
49
|
+
parts.forEach((part, i) => {
|
|
50
|
+
const found = ancestors(part.name);
|
|
51
|
+
if (found === null || found.length !== 1) {
|
|
52
|
+
faults.push({ receiver: part.name, reason: found === null ? 'cycle' : found.length === 0 ? 'missing-lineage' : 'multiple-ancestors' });
|
|
53
|
+
} else roots[i] = owner.names.indexOf(found[0]);
|
|
54
|
+
});
|
|
55
|
+
const receivers = new Int32Array(owner.pixels.length).fill(-1);
|
|
56
|
+
const reasons = new Array<string>(owner.pixels.length).fill('no-owner');
|
|
57
|
+
for (let p = 0; p < owner.pixels.length; p++) {
|
|
58
|
+
if (owner.pixels[p] < 0) continue;
|
|
59
|
+
let target = -1;
|
|
60
|
+
let ambiguous = false;
|
|
61
|
+
for (let i = 0; i < parts.length; i++) {
|
|
62
|
+
const part = parts[i];
|
|
63
|
+
if (roots[i] !== owner.pixels[p] || part.image.data[p * 4 + 3] === 0 ||
|
|
64
|
+
(part.region !== null && part.region.data[p] !== 1)) continue;
|
|
65
|
+
if (target >= 0) { ambiguous = true; break; }
|
|
66
|
+
target = i;
|
|
67
|
+
}
|
|
68
|
+
if (ambiguous) reasons[p] = 'multiple-receivers';
|
|
69
|
+
else if (target < 0) reasons[p] = 'no-supported-receiver';
|
|
70
|
+
else { receivers[p] = target; reasons[p] = 'unique'; }
|
|
71
|
+
}
|
|
72
|
+
return { ...owner, receiverNames: parts.map(q => q.name), roots, receivers, reasons, faults };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** A refusal names the missing owner or the receiver contract it violates. */
|
|
76
|
+
export function paintingReceiverAt(input: PaintingReceivers, p: number): { target: number; reason: string } {
|
|
77
|
+
const owner = input.pixels[p];
|
|
78
|
+
if (owner === -2) return { target: -1, reason: 'mixed-owner' };
|
|
79
|
+
if (owner < 0) return { target: -1, reason: 'no-owner' };
|
|
80
|
+
if (input.names[owner] === undefined) return { target: -1, reason: 'unknown-owner' };
|
|
81
|
+
const target = input.receivers[p];
|
|
82
|
+
if (target < 0) return { target: -1, reason: input.reasons[p] };
|
|
83
|
+
if (input.roots[target] !== owner) return { target: -1, reason: 'receiver-owner-mismatch' };
|
|
84
|
+
return { target, reason: 'unique' };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Refused known-owner writes remain foreign to the receiver contract. */
|
|
88
|
+
export function foreignPaintingReceiver(input: PaintingReceivers, p: number, receiver: number): boolean {
|
|
89
|
+
return input.pixels[p] >= 0 && (input.receivers[p] !== receiver || input.roots[receiver] !== input.pixels[p]);
|
|
90
|
+
}
|