@rig-c/parts 1.54.0 → 1.55.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/cli.ts +40 -7
- package/docs/AUTHORING.md +60 -11
- package/docs/STABILITY.md +6 -5
- package/package.json +1 -1
- package/src/assemble.ts +568 -51
- package/src/build.ts +91 -11
- package/src/check.ts +43 -12
- package/src/cloth.ts +10 -3
- package/src/config.ts +27 -6
- package/src/parts.ts +83 -45
- package/src/propose.ts +10 -2
- package/src/remesh.ts +306 -6
- package/src/retune.ts +25 -3
- package/src/rig.ts +18 -6
package/src/assemble.ts
CHANGED
|
@@ -104,9 +104,10 @@ import { type Problem, refuseIfAny } from './errors.ts';
|
|
|
104
104
|
import { figuresPhrase, implausibleRules, type Layer, type LayerFigures, layerFigures, type LayerSet, NEAR_WHITE_MIN, OPAQUE_ALPHA_ABOVE, ruleSummary } from './layers.ts';
|
|
105
105
|
import { padToSquare, squarePad } from './inputs.ts';
|
|
106
106
|
import { type CapRecord, type LimbRecord, PAINTING_RUN, type PartRecord, type PartsFile, type RecompositeRecord, type SceneRecord, type SingleRunPad } from './parts.ts';
|
|
107
|
-
import { assignCrossedLimbs, assignOverlappingHands, type BonePlace, bonePlaces, capBox, coveredRadius, type CutPlacement, featherWeight, halfWidthAbout, inDisc, jointLine, type JointLine, jointOffsets, limbPolyline, limbRun, luma601, nearestLimb, nearestOnLine, offLineFigure, pastJoint, placeCut, segmentOf } from './limbs.ts';
|
|
107
|
+
import { assignCrossedLimbs, assignOverlappingHands, type BonePlace, bonePlaces, capBox, coveredRadius, type CutPlacement, featherWeight, halfWidthAbout, inDisc, jointLine, type JointLine, jointOffsets, limbPolyline, limbRun, luma601, nearestLimb, nearestOnLine, offLineFigure, pastJoint, placeCut, polylineDist2, segmentOf } from './limbs.ts';
|
|
108
108
|
import {
|
|
109
109
|
alphaComposite,
|
|
110
|
+
type Components,
|
|
110
111
|
connectedComponents,
|
|
111
112
|
crop,
|
|
112
113
|
dilate,
|
|
@@ -350,9 +351,17 @@ export interface AssembleInput {
|
|
|
350
351
|
* every figure a part's record holds — each entry clears `part`'s pixels (alpha, colour and region) where `mask` is
|
|
351
352
|
* 1, and {@link AssembleResult.excluded} counts the ones above `OPAQUE_ALPHA_ABOVE` it took, in entry order (a pixel
|
|
352
353
|
* an earlier entry cleared is not counted again). The set build passes {@link setExclusions}. An entry naming a part
|
|
353
|
-
* this run did not make clears nothing and counts 0. Absent or empty: nothing changes.
|
|
354
|
+
* this run did not make clears nothing and counts 0. Absent or empty: nothing changes. Issue #603: the part's
|
|
355
|
+
* `merged_px` and, on a limb segment's part, its `cap` record are counted after it, so they add up to the PNG written
|
|
356
|
+
* ({@link CapRecord} `excluded_px`).
|
|
354
357
|
*/
|
|
355
358
|
exclude?: readonly SetExclusion[];
|
|
359
|
+
/**
|
|
360
|
+
* Issue #603: `"after"` (the default, the only one the stages pass) counts `merged_px` and a limb segment's `cap`
|
|
361
|
+
* record after `exclude`; `"before"` leaves them as counted before it, the rule before #603, which counted pixels the
|
|
362
|
+
* exclusion took out of the PNG — kept for the selftest's control that shows the counts go red under it.
|
|
363
|
+
*/
|
|
364
|
+
exclusionCounts?: 'after' | 'before';
|
|
356
365
|
/**
|
|
357
366
|
* Issue #588: the parts a limb takes on which a subset set names one limb side ({@link setIslandSources}). On each
|
|
358
367
|
* that holds more than one island, every limb segment part cut from it keeps the island its joints fall on and
|
|
@@ -360,6 +369,39 @@ export interface AssembleInput {
|
|
|
360
369
|
* nothing changes.
|
|
361
370
|
*/
|
|
362
371
|
limbIslands?: readonly string[];
|
|
372
|
+
/**
|
|
373
|
+
* Issue #608: the share of a layer's largest island under which `limbIslands` reads an island as a speck. Absent:
|
|
374
|
+
* {@link ISLAND_FLOOR_SHARE}. Kept for the selftest's mutant, which passes 0 to read every island as a candidate,
|
|
375
|
+
* the rule before #608; no config field and no command reaches it.
|
|
376
|
+
*/
|
|
377
|
+
islandFloor?: number;
|
|
378
|
+
/**
|
|
379
|
+
* Issue #609: the config's limbs a subset set does not carry that take one of `limbIslands` — the other leg of a
|
|
380
|
+
* two-leg layer — with what the set says of their place. On a `limbIslands` part of one island, every limb segment
|
|
381
|
+
* part cut from it keeps the pixels nearer its own limb's segment line than the other limb's ({@link OtherLimbs}),
|
|
382
|
+
* once the limbs are cut and in place of the island rule, which needs two islands. Absent: nothing changes.
|
|
383
|
+
*/
|
|
384
|
+
otherLimbs?: OtherLimbs;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Issue #609: the limbs of a layer a subset set names one side of, which the set does not carry, as the set build
|
|
389
|
+
* hands them to {@link assemble}.
|
|
390
|
+
*/
|
|
391
|
+
export interface OtherLimbs {
|
|
392
|
+
/** The config's limbs, by name, that the set does not carry and that take one of `limbIslands`. */
|
|
393
|
+
limbs: Readonly<Record<string, Limb>>;
|
|
394
|
+
/**
|
|
395
|
+
* Per limb, its segment bones the set does not place: neither its skeleton names it (`SKELETON_LIMB_BONES`) nor
|
|
396
|
+
* its `drawn`, nor an ancestor of it. Empty: the limb stands where the set's own bones (`bones`) put it.
|
|
397
|
+
*/
|
|
398
|
+
unposed: Readonly<Record<string, readonly string[]>>;
|
|
399
|
+
/** Per limb, the rig pixels where the base build's parts of that limb hold art (alpha above `OPAQUE_ALPHA_ABOVE`) at setup. */
|
|
400
|
+
baseArt: Readonly<Record<string, Mask>>;
|
|
401
|
+
/** The base build's parts in draw order (`parts.json`'s order): a later one is drawn in front. */
|
|
402
|
+
drawOrder: readonly string[];
|
|
403
|
+
/** How the set states its pose, for the refusal's words: a `skeleton` file, `drawn` angles, or neither. */
|
|
404
|
+
pose: 'skeleton' | 'drawn' | 'none';
|
|
363
405
|
}
|
|
364
406
|
|
|
365
407
|
/** One base part's cover over one of a subset set's limb parts (issue #587): `part` loses its pixels where `mask` is 1. */
|
|
@@ -419,16 +461,25 @@ export function setExclusions(base: readonly PlacedPart[], W: number, H: number,
|
|
|
419
461
|
/**
|
|
420
462
|
* What one limb segment part kept of a layer that holds more than one island (issue #588): the layer's island count,
|
|
421
463
|
* the joints the kept island lies under, and the part's pixels (alpha above `OPAQUE_ALPHA_ABOVE`) on it and on the
|
|
422
|
-
* islands dropped.
|
|
464
|
+
* islands dropped. Issue #608: `islands` counts the candidates (islands at or above the floor, {@link readIslands});
|
|
465
|
+
* `specks` the islands under it, each of which goes with the candidate nearest it, and `dust` those of them equally
|
|
466
|
+
* near two, which go with none; a joint on no island that was assigned to the nearest within its cap's radius carries
|
|
467
|
+
* `off`, its distance and that radius; `droppedIslands` counts the islands (candidates, and dust specks) the part lost a
|
|
468
|
+
* pixel above `OPAQUE_ALPHA_ABOVE` from.
|
|
423
469
|
*/
|
|
424
470
|
export interface IslandCount {
|
|
425
471
|
part: string;
|
|
426
472
|
/** The part the limb took (the segment's `from`). */
|
|
427
473
|
of: string;
|
|
428
474
|
islands: number;
|
|
429
|
-
|
|
475
|
+
specks: number;
|
|
476
|
+
dust: number;
|
|
477
|
+
/** The floor in pixels: `ISLAND_FLOOR_SHARE` of the largest island's area. */
|
|
478
|
+
floor: number;
|
|
479
|
+
joints: Array<{ bone: string; at: [number, number]; off?: { distance: number; margin: number } }>;
|
|
430
480
|
kept: number;
|
|
431
481
|
dropped: number;
|
|
482
|
+
droppedIslands: number;
|
|
432
483
|
}
|
|
433
484
|
|
|
434
485
|
/**
|
|
@@ -451,20 +502,116 @@ function pixelsAt(at: readonly [number, number], W: number, H: number): number[]
|
|
|
451
502
|
return out;
|
|
452
503
|
}
|
|
453
504
|
|
|
505
|
+
/**
|
|
506
|
+
* Issue #608: the share of a layer's largest island under which an island is a speck, not a candidate for a limb to
|
|
507
|
+
* keep — the ghost clean-up's own share (`GHOST_MIN_FRACTION`, the card's "under 1 %"), over the same islands (8-connected,
|
|
508
|
+
* alpha above `OPAQUE_ALPHA_ABOVE`). A share rather than a pixel count because the islands are read in rig pixels,
|
|
509
|
+
* whose size is the config's `rig_scale`, while the clean-up's `GHOST_MIN_AREA` is a count of run pixels.
|
|
510
|
+
*/
|
|
511
|
+
export const ISLAND_FLOOR_SHARE = GHOST_MIN_FRACTION;
|
|
512
|
+
|
|
513
|
+
/** How {@link readIslands} sorted a layer's islands. */
|
|
514
|
+
export interface IslandReading {
|
|
515
|
+
/** The candidates' labels, numbered as the LINT and the refusals number them: by area, largest first, then top row, then left column. */
|
|
516
|
+
candidates: number[];
|
|
517
|
+
/** The specks' labels, in the same order. */
|
|
518
|
+
specks: number[];
|
|
519
|
+
/** Per label: the candidate it goes with — itself for a candidate, the nearest candidate for a speck, 0 for a speck equally near two (dust) and for the background. */
|
|
520
|
+
owner: Int32Array;
|
|
521
|
+
/** `share` of the largest island's area, in pixels. */
|
|
522
|
+
floor: number;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* Issue #608: a layer's islands sorted into candidates and specks.
|
|
527
|
+
*
|
|
528
|
+
* ⚖️ Invariant: an island is a candidate when its area is at least `share` times the largest island's — the
|
|
529
|
+
* inequality `cleanGhosts` keeps a component by — and a speck otherwise. A speck goes with the candidate nearest it:
|
|
530
|
+
* the one holding the pixel whose centre is nearest a centre of the speck's (squared distances, which are integers);
|
|
531
|
+
* equally near two, it goes with none. A speck beside a leg is the leg's whichever side of it the joints are, and one
|
|
532
|
+
* nearer the other leg is the other leg's; none is a candidate a joint could choose. `share` is
|
|
533
|
+
* {@link ISLAND_FLOOR_SHARE} but in the selftest's mutant, which reads the floor off (0) to show what it decides.
|
|
534
|
+
*/
|
|
535
|
+
export function readIslands(cc: Components, W: number, H: number, share: number): IslandReading {
|
|
536
|
+
const order = cc.stats.slice(1).sort((a, c) => c.area - a.area || a.top - c.top || a.left - c.left);
|
|
537
|
+
const big = order.length === 0 ? 0 : order[0].area;
|
|
538
|
+
const floor = share * big;
|
|
539
|
+
const candidates = order.filter((st) => st.area >= floor).map((st) => st.label);
|
|
540
|
+
const specks = order.filter((st) => st.area < floor).map((st) => st.label);
|
|
541
|
+
const owner = new Int32Array(cc.count);
|
|
542
|
+
for (const l of candidates) owner[l] = l;
|
|
543
|
+
if (specks.length > 0) {
|
|
544
|
+
// The nearest pixel of a set to a point outside it is on the set's edge (a step toward the point from any other
|
|
545
|
+
// pixel of it lands on one of its pixels, nearer), so only the candidates' edge pixels are read.
|
|
546
|
+
const isSpeck = new Uint8Array(cc.count);
|
|
547
|
+
for (const l of specks) isSpeck[l] = 1;
|
|
548
|
+
const edge: Array<[number, number, number]> = [];
|
|
549
|
+
const speckPx = new Map<number, Array<[number, number]>>(specks.map((l) => [l, []]));
|
|
550
|
+
for (let y = 0; y < H; y++) {
|
|
551
|
+
for (let x = 0; x < W; x++) {
|
|
552
|
+
const l = cc.labels[y * W + x];
|
|
553
|
+
if (l === 0) continue;
|
|
554
|
+
if (isSpeck[l] === 1) {
|
|
555
|
+
speckPx.get(l)?.push([x, y]);
|
|
556
|
+
continue;
|
|
557
|
+
}
|
|
558
|
+
const inside = (u: number, v: number): boolean => u >= 0 && v >= 0 && u < W && v < H && cc.labels[v * W + u] === l;
|
|
559
|
+
if (!inside(x - 1, y) || !inside(x + 1, y) || !inside(x, y - 1) || !inside(x, y + 1)) edge.push([x, y, l]);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
for (const s of specks) {
|
|
563
|
+
let best = Infinity;
|
|
564
|
+
let to = 0;
|
|
565
|
+
for (const [ex, ey, l] of edge) {
|
|
566
|
+
for (const [sx, sy] of speckPx.get(s) ?? []) {
|
|
567
|
+
const d = (ex - sx) ** 2 + (ey - sy) ** 2;
|
|
568
|
+
if (d < best) {
|
|
569
|
+
best = d;
|
|
570
|
+
to = l;
|
|
571
|
+
} else if (d === best && l !== to) {
|
|
572
|
+
to = 0;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
owner[s] = to;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
return { candidates, specks, owner, floor };
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/** The squared distance from the point `at` to the nearest pixel centre of island `label` (Infinity for none). */
|
|
583
|
+
function squaredDistanceTo(cc: Components, W: number, label: number, at: readonly [number, number]): number {
|
|
584
|
+
const st = cc.stats[label];
|
|
585
|
+
let best = Infinity;
|
|
586
|
+
for (let y = st.top; y < st.top + st.height; y++) {
|
|
587
|
+
for (let x = st.left; x < st.left + st.width; x++) {
|
|
588
|
+
if (cc.labels[y * W + x] !== label) continue;
|
|
589
|
+
const d = (x + 0.5 - at[0]) ** 2 + (y + 0.5 - at[1]) ** 2;
|
|
590
|
+
if (d < best) best = d;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
return best;
|
|
594
|
+
}
|
|
595
|
+
|
|
454
596
|
/**
|
|
455
597
|
* Issue #588: on each of `sources` that holds more than one island, keep in every limb segment part cut from it the
|
|
456
598
|
* island its joints fall on, and clear the rest (alpha, colour and region).
|
|
457
599
|
*
|
|
458
600
|
* ⚖️ Invariant: an island is an 8-connected component of the part's pixels with alpha above `OPAQUE_ALPHA_ABOVE` —
|
|
459
|
-
* the ghost clean-up's own rule (`cleanGhosts`); a pixel at or below it is on no island and is kept.
|
|
460
|
-
*
|
|
461
|
-
*
|
|
462
|
-
*
|
|
463
|
-
*
|
|
464
|
-
*
|
|
465
|
-
*
|
|
466
|
-
*
|
|
467
|
-
* cap
|
|
601
|
+
* the ghost clean-up's own rule (`cleanGhosts`); a pixel at or below it is on no island and is kept. Issue #608: an
|
|
602
|
+
* island under `floorShare` of the largest is a speck, which goes with its nearest candidate ({@link readIslands}); the
|
|
603
|
+
* rule runs on a source of two candidates or more, so a speck never makes a layer of one leg a layer of two. A
|
|
604
|
+
* segment's joints are its bone's origin and, when a segment follows it, the next segment's bone's origin (a bone's
|
|
605
|
+
* tip is no joint: the limb ends there), placed by `bones` — the set's bones, posed or placed as its cut. A joint is
|
|
606
|
+
* on the island of a pixel whose square holds the point (a speck's pixel puts it on that speck's candidate); with
|
|
607
|
+
* 8-connectivity the pixels that share a point are one island, so a joint is on one island or on none. A joint on
|
|
608
|
+
* none goes to the candidate whose nearest pixel centre lies within the radius of the cap cut at that joint (the
|
|
609
|
+
* lower segment's `cap.radius`, its disc's own reach, the inequality `inDisc` reads) when exactly one does; with none
|
|
610
|
+
* within it, two within it, or no cap at the joint (a limb's first joint carries none), `ASSEMBLE_ALT_LIMB_ISLAND`
|
|
611
|
+
* names the joint, the distances and the radius. A segment whose joints fall on two islands names no island to keep
|
|
612
|
+
* either, and is refused rather than guessed. A source with one candidate is left as cut and counted nowhere, so a
|
|
613
|
+
* set whose layer holds its one leg writes what it wrote before. A pixel the limbs wrote where the source is
|
|
614
|
+
* transparent (a `cap_shape: "ball"` lower half from the cap image) is on no island and kept.
|
|
468
615
|
*/
|
|
469
616
|
function keepJointIslands(
|
|
470
617
|
sources: readonly string[],
|
|
@@ -475,8 +622,11 @@ function keepJointIslands(
|
|
|
475
622
|
sourceOf: (name: string) => Built | null,
|
|
476
623
|
W: number,
|
|
477
624
|
H: number,
|
|
478
|
-
|
|
625
|
+
floorShare: number,
|
|
626
|
+
others?: OtherLimbs,
|
|
627
|
+
): { counts: IslandCount[]; splits: SplitCount[]; problems: Problem[] } {
|
|
479
628
|
const counts: IslandCount[] = [];
|
|
629
|
+
const splits: SplitCount[] = [];
|
|
480
630
|
const problems: Problem[] = [];
|
|
481
631
|
const places = bonePlaces(bones);
|
|
482
632
|
for (const src of sources) {
|
|
@@ -485,42 +635,68 @@ function keepJointIslands(
|
|
|
485
635
|
const m = newMask(W, H);
|
|
486
636
|
for (let p = 0; p < W * H; p++) m.data[p] = b.r.data[p * 4 + 3] > OPAQUE_ALPHA_ABOVE ? 1 : 0;
|
|
487
637
|
const cc = connectedComponents(m, 8);
|
|
488
|
-
const
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
638
|
+
const reading = readIslands(cc, W, H, floorShare);
|
|
639
|
+
const n = reading.candidates.length;
|
|
640
|
+
if (n < 2) {
|
|
641
|
+
// Issue #609: one island may hold both limbs, overlapping; the island rule cannot part them, the segment lines can.
|
|
642
|
+
if (n === 1 && others !== undefined) splitOneIsland(src, b, limbs, places, parts, pieces, others, W, H, splits, problems);
|
|
643
|
+
continue;
|
|
644
|
+
}
|
|
645
|
+
const numberOf = new Map(reading.candidates.map((l, i) => [l, i + 1]));
|
|
493
646
|
const islandText = (label: number): string => {
|
|
494
647
|
const st = cc.stats[label];
|
|
495
648
|
return `island ${numberOf.get(label)} (${st.area} px at ${st.left},${st.top} ${st.width}x${st.height})`;
|
|
496
649
|
};
|
|
497
|
-
const
|
|
650
|
+
const speckText =
|
|
651
|
+
reading.specks.length === 0
|
|
652
|
+
? ''
|
|
653
|
+
: `, and ${reading.specks.length} speck${reading.specks.length === 1 ? '' : 's'} under ${pyRound(floorShare * 100, 3)} % of the largest (${pyRound(reading.floor, 3)} px), each with the island nearest it`;
|
|
654
|
+
const all = reading.candidates.map((l) => islandText(l)).join(', ');
|
|
655
|
+
const dust = reading.specks.filter((l) => reading.owner[l] === 0).length;
|
|
498
656
|
parts.forEach((q, i) => {
|
|
499
657
|
if (q.of !== src) return;
|
|
500
658
|
const segs = limbs[q.limb].segments;
|
|
501
659
|
const object = `config.limbs.${q.limb}.segments[${q.segment}] (part "${q.part}")`;
|
|
502
|
-
const
|
|
503
|
-
const
|
|
660
|
+
const jointSegs = [q.segment, ...(q.segment + 1 < segs.length ? [q.segment + 1] : [])];
|
|
661
|
+
const head = `takes "${src}", which holds ${n} islands (8-connected, alpha above ${OPAQUE_ALPHA_ABOVE}): ${all}${speckText}; the set names one of the limbs that take "${src}", so the segment keeps the island its joints fall on`;
|
|
662
|
+
const joints: Array<{ bone: string; at: [number, number]; label: number; off?: { distance: number; margin: number } }> = [];
|
|
663
|
+
for (const k of jointSegs) {
|
|
664
|
+
const bone = segs[k].bone;
|
|
504
665
|
const at = (places.get(bone) as BonePlace).at;
|
|
505
|
-
const
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
});
|
|
516
|
-
|
|
666
|
+
const rounded: [number, number] = [pyRound(at[0], 3), pyRound(at[1], 3)];
|
|
667
|
+
const where = `"${bone}"'s origin at [${rounded.join(', ')}]`;
|
|
668
|
+
const on = [...new Set(pixelsAt(at, W, H).map((p) => reading.owner[cc.labels[p]]).filter((l) => l > 0))];
|
|
669
|
+
if (on.length > 0) {
|
|
670
|
+
joints.push({ bone, at: rounded, label: on[0] });
|
|
671
|
+
continue;
|
|
672
|
+
}
|
|
673
|
+
// Issue #608: a joint on no island goes to the one candidate within its cap's radius.
|
|
674
|
+
const cap = k === 0 ? undefined : parts.find((r) => r.limb === q.limb && r.segment === k)?.record.cap;
|
|
675
|
+
const d2 = reading.candidates.map((l) => ({ l, d2: squaredDistanceTo(cc, W, l, at) }));
|
|
676
|
+
const figures = d2.map((e) => `island ${numberOf.get(e.l)} at ${pyRound(Math.sqrt(e.d2), 3)} px`).join(', ');
|
|
677
|
+
const stem = `${head}, and its joint ${where} is on none of them — no pixel touching the point has alpha above ${OPAQUE_ALPHA_ABOVE}; the nearest pixel centre of each: ${figures}`;
|
|
678
|
+
const fix = 'a bone origin on its limb\'s art is required (move the bone onto the joint), or a layer that holds this limb alone';
|
|
679
|
+
if (cap === undefined) {
|
|
680
|
+
problems.push({ code: 'ASSEMBLE_ALT_LIMB_ISLAND', object, detail: `${stem}; no cap is cut at this joint (a limb's first joint carries none), so no radius says which island is near enough to be the limb's; ${fix}` });
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
const within = d2.filter((e) => e.d2 <= cap.radius * cap.radius);
|
|
684
|
+
if (within.length !== 1) {
|
|
685
|
+
problems.push({
|
|
686
|
+
code: 'ASSEMBLE_ALT_LIMB_ISLAND',
|
|
687
|
+
object,
|
|
688
|
+
detail: `${stem}; ${within.length === 0 ? 'none lies' : `${within.length} lie`} within the radius of the cap cut at it, ${pyRound(cap.radius, 3)} px, and exactly one is required to take a joint off the art; ${fix}`,
|
|
689
|
+
});
|
|
690
|
+
return;
|
|
691
|
+
}
|
|
692
|
+
joints.push({ bone, at: rounded, label: within[0].l, off: { distance: pyRound(Math.sqrt(within[0].d2), 3), margin: pyRound(cap.radius, 3) } });
|
|
517
693
|
}
|
|
518
|
-
const on = [...new Set(joints.
|
|
694
|
+
const on = [...new Set(joints.map((j) => j.label))];
|
|
519
695
|
if (on.length > 1) {
|
|
520
696
|
problems.push({
|
|
521
697
|
code: 'ASSEMBLE_ALT_LIMB_ISLAND',
|
|
522
698
|
object,
|
|
523
|
-
detail:
|
|
699
|
+
detail: `${head}, and they fall on ${on.length}: ${joints.map((j) => `"${j.bone}"'s origin at [${j.at.join(', ')}] ${j.off === undefined ? 'on' : `${j.off.distance} px off, within its cap's radius ${j.off.margin} of`} ${islandText(j.label)}`).join(', ')}; every joint of a segment on one island is required — one stroke of art from joint to joint, or a layer that holds this limb alone`,
|
|
524
700
|
});
|
|
525
701
|
return;
|
|
526
702
|
}
|
|
@@ -528,30 +704,302 @@ function keepJointIslands(
|
|
|
528
704
|
const piece = pieces[i];
|
|
529
705
|
let kept = 0;
|
|
530
706
|
let dropped = 0;
|
|
707
|
+
const lost = new Set<number>();
|
|
531
708
|
for (let p = 0; p < W * H; p++) {
|
|
532
709
|
const a = piece.r.data[p * 4 + 3];
|
|
533
710
|
const l = cc.labels[p];
|
|
534
711
|
if (a === 0 || l === 0) continue;
|
|
535
|
-
|
|
712
|
+
const o = reading.owner[l];
|
|
713
|
+
if (o === keep) {
|
|
536
714
|
if (a > OPAQUE_ALPHA_ABOVE) kept++;
|
|
537
715
|
continue;
|
|
538
716
|
}
|
|
539
|
-
if (a > OPAQUE_ALPHA_ABOVE)
|
|
717
|
+
if (a > OPAQUE_ALPHA_ABOVE) {
|
|
718
|
+
dropped++;
|
|
719
|
+
// A dust speck is an island of its own; any other pixel counts as its candidate's.
|
|
720
|
+
lost.add(o === 0 ? -l : o);
|
|
721
|
+
}
|
|
540
722
|
piece.r.data.fill(0, p * 4, p * 4 + 4);
|
|
541
723
|
if (piece.region !== null) piece.region.data[p] = 0;
|
|
542
724
|
}
|
|
543
|
-
counts.push({
|
|
725
|
+
counts.push({
|
|
726
|
+
part: q.part,
|
|
727
|
+
of: src,
|
|
728
|
+
islands: n,
|
|
729
|
+
specks: reading.specks.length,
|
|
730
|
+
dust,
|
|
731
|
+
floor: pyRound(reading.floor, 3),
|
|
732
|
+
joints: joints.map((j) => ({ bone: j.bone, at: j.at, ...(j.off === undefined ? {} : { off: j.off }) })),
|
|
733
|
+
kept,
|
|
734
|
+
dropped,
|
|
735
|
+
droppedIslands: lost.size,
|
|
736
|
+
});
|
|
544
737
|
});
|
|
545
738
|
}
|
|
546
|
-
return { counts, problems };
|
|
739
|
+
return { counts, splits, problems };
|
|
547
740
|
}
|
|
548
741
|
|
|
549
|
-
/**
|
|
742
|
+
/**
|
|
743
|
+
* Issue #609: the overlap band's width — a pixel whose centre's distances to the two limbs' segment lines differ by at
|
|
744
|
+
* most this is equally near both, and goes to the limb drawn in front. Half a pixel: the reach by which `src/limbs.ts`
|
|
745
|
+
* reads a pixel centre as on a line (`limbWidth`: the pixels "whose centre lies within half a pixel of the joint
|
|
746
|
+
* line"), here read on the line of points equally near both.
|
|
747
|
+
*/
|
|
748
|
+
export const OVERLAP_BAND_PX = 0.5;
|
|
749
|
+
|
|
750
|
+
/**
|
|
751
|
+
* Issue #609: the share of the overlap at which the two limbs' segment lines run closer than the limb's half-width
|
|
752
|
+
* over most of it — more than half, the plain majority. A straight crossing at angle θ reads cos θ / 2 of its overlap
|
|
753
|
+
* there ({@link skeletonsTooClose}), under half at any angle; only lines that run alongside each other reach it.
|
|
754
|
+
*/
|
|
755
|
+
export const OVERLAP_TOO_CLOSE_SHARE = 0.5;
|
|
756
|
+
|
|
757
|
+
/** What one limb segment part kept of a layer of one island that also holds another limb (issue #609). */
|
|
758
|
+
export interface SplitCount {
|
|
759
|
+
part: string;
|
|
760
|
+
/** The part the limb took (the segment's `from`). */
|
|
761
|
+
of: string;
|
|
762
|
+
/** The part's own limb. */
|
|
763
|
+
limb: string;
|
|
764
|
+
/** The part's pixels (alpha above `OPAQUE_ALPHA_ABOVE`) kept, and those given to each other limb, in `config.limbs` order. */
|
|
765
|
+
kept: number;
|
|
766
|
+
given: Array<{ limb: string; px: number }>;
|
|
767
|
+
/** Of `kept` and `given`, the pixels in the overlap band ({@link OVERLAP_BAND_PX}), which went to the limb drawn in front: those kept, those given. */
|
|
768
|
+
bandKept: number;
|
|
769
|
+
bandGiven: number;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
/** A point and its distance to a set of line pieces. */
|
|
773
|
+
type LinePt = readonly [number, number];
|
|
774
|
+
|
|
775
|
+
/** The distance from `p` along the unit normal `n` to the piece `c`–`d` (|t| with `p + t n` on it), or Infinity when the normal line misses it. */
|
|
776
|
+
function normalGap(p: LinePt, n: LinePt, c: LinePt, d: LinePt): number {
|
|
777
|
+
const ex = d[0] - c[0];
|
|
778
|
+
const ey = d[1] - c[1];
|
|
779
|
+
const cx = c[0] - p[0];
|
|
780
|
+
const cy = c[1] - p[1];
|
|
781
|
+
const det = ex * n[1] - n[0] * ey;
|
|
782
|
+
const len = Math.sqrt(ex * ex + ey * ey);
|
|
783
|
+
if (Math.abs(det) <= 1e-9 * len) {
|
|
784
|
+
// The piece is parallel to the normal: it meets the normal line only when it lies on it.
|
|
785
|
+
if (Math.abs(cx * n[1] - cy * n[0]) > 1e-9) return Infinity;
|
|
786
|
+
const tc = cx * n[0] + cy * n[1];
|
|
787
|
+
const td = (d[0] - p[0]) * n[0] + (d[1] - p[1]) * n[1];
|
|
788
|
+
return tc * td <= 0 ? 0 : Math.min(Math.abs(tc), Math.abs(td));
|
|
789
|
+
}
|
|
790
|
+
const t = (ex * cy - ey * cx) / det;
|
|
791
|
+
const v = (n[0] * cy - n[1] * cx) / det;
|
|
792
|
+
return v < 0 || v > 1 ? Infinity : Math.abs(t);
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
/**
|
|
796
|
+
* Issue #609: whether two limbs' segment lines run closer than `half` (the own limb's half-width) over most of where
|
|
797
|
+
* their art can overlap. The own line is read every 1 px along each of its pieces, from the piece's start; a reading
|
|
798
|
+
* is in the overlap when the other line passes within `2 * half` of it (two limbs of that half-width can meet there),
|
|
799
|
+
* and close when the other line crosses the own limb's normal there within `half` — the gap measured across the
|
|
800
|
+
* limb, as its width is. A crossing at angle θ is close over cos θ / 2 of its overlap; two lines alongside each other
|
|
801
|
+
* less than `half` apart, over all of it.
|
|
802
|
+
*/
|
|
803
|
+
function skeletonsTooClose(own: readonly LinePt[], other: readonly LinePt[], half: number): { overlap: number; close: number } {
|
|
804
|
+
let overlap = 0;
|
|
805
|
+
let close = 0;
|
|
806
|
+
for (let i = 0; i + 1 < own.length; i++) {
|
|
807
|
+
const a = own[i];
|
|
808
|
+
const b = own[i + 1];
|
|
809
|
+
const len = Math.sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2);
|
|
810
|
+
if (len === 0) continue;
|
|
811
|
+
const u: LinePt = [(b[0] - a[0]) / len, (b[1] - a[1]) / len];
|
|
812
|
+
const n: LinePt = [-u[1], u[0]];
|
|
813
|
+
for (let k = 0; k <= Math.floor(len); k++) {
|
|
814
|
+
const s: LinePt = [a[0] + k * u[0], a[1] + k * u[1]];
|
|
815
|
+
if (Math.sqrt(polylineDist2(other as Array<[number, number]>, s[0], s[1])) >= 2 * half) continue;
|
|
816
|
+
overlap++;
|
|
817
|
+
let gap = Infinity;
|
|
818
|
+
for (let j = 0; j + 1 < other.length; j++) gap = Math.min(gap, normalGap(s, n, other[j], other[j + 1]));
|
|
819
|
+
if (other.length === 1) gap = Math.sqrt((other[0][0] - s[0]) ** 2 + (other[0][1] - s[1]) ** 2);
|
|
820
|
+
if (gap < half) close++;
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
return { overlap, close };
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
/**
|
|
827
|
+
* Issue #609: on a layer of one island that the set's limbs take and `others`' limbs take too, give each pixel of the
|
|
828
|
+
* set's limb parts to the limb whose segment line is nearest it, in the set's pose, and keep the set's.
|
|
829
|
+
*
|
|
830
|
+
* ⚖️ Invariant: a limb's segment line is its polyline ({@link limbPolyline}: its segments' bone origins, then the last
|
|
831
|
+
* bone's end) at `places`, the set's own bones — the set's limb where the set cut it, the other limb where the set
|
|
832
|
+
* placed it (its skeleton's keypoints). A pixel of a part (alpha above 0, where the layer has art) is nearer the
|
|
833
|
+
* other limb when its centre's distance to that limb's line is less than its distance to its own limb's by more than
|
|
834
|
+
* {@link OVERLAP_BAND_PX}; within it, in the overlap band, it goes to the limb whose part there (the part of the
|
|
835
|
+
* segment nearest it) is drawn later in the base's draw order. A pixel given away is cleared (alpha, colour and
|
|
836
|
+
* region). An other limb the set does not place is never put at the base's place: when the layer has art where the
|
|
837
|
+
* base build draws that limb, the layer holds it and the set is refused by name (`ASSEMBLE_ALT_LIMB_POSE`); when it
|
|
838
|
+
* has none, nothing says the layer holds it, and it takes no pixel. Two limbs whose lines run closer than the limb's
|
|
839
|
+
* half-width (the least `cap.radius` of its joints) over more than {@link OVERLAP_TOO_CLOSE_SHARE} of their overlap
|
|
840
|
+
* ({@link skeletonsTooClose}) have no line between them to split by, and are refused by name
|
|
841
|
+
* (`ASSEMBLE_ALT_LIMB_OVERLAP`) rather than cut down the middle.
|
|
842
|
+
*/
|
|
843
|
+
function splitOneIsland(
|
|
844
|
+
src: string,
|
|
845
|
+
source: Built,
|
|
846
|
+
limbs: Readonly<Record<string, Limb>>,
|
|
847
|
+
places: ReadonlyMap<string, BonePlace>,
|
|
848
|
+
parts: readonly LimbPart[],
|
|
849
|
+
pieces: readonly Built[],
|
|
850
|
+
others: OtherLimbs,
|
|
851
|
+
W: number,
|
|
852
|
+
H: number,
|
|
853
|
+
splits: SplitCount[],
|
|
854
|
+
problems: Problem[],
|
|
855
|
+
): void {
|
|
856
|
+
const mine = parts.map((q, i) => ({ q, i })).filter(({ q }) => q.of === src);
|
|
857
|
+
if (mine.length === 0) return;
|
|
858
|
+
const ownNames = [...new Set(mine.map(({ q }) => q.limb))];
|
|
859
|
+
const otherNames = Object.keys(others.limbs).filter((l) => others.limbs[l].segments.some((sg) => sg.from === src));
|
|
860
|
+
if (otherNames.length === 0) return;
|
|
861
|
+
const owners = ownNames.map((l) => `config.limbs.${l}`).join(' and ');
|
|
862
|
+
const problemsBefore = problems.length;
|
|
863
|
+
let refused = false;
|
|
864
|
+
const posed: string[] = [];
|
|
865
|
+
for (const l of otherNames) {
|
|
866
|
+
const unposed = others.unposed[l] ?? [];
|
|
867
|
+
if (unposed.length === 0) {
|
|
868
|
+
posed.push(l);
|
|
869
|
+
continue;
|
|
870
|
+
}
|
|
871
|
+
const art = others.baseArt[l];
|
|
872
|
+
let on = 0;
|
|
873
|
+
for (let p = 0; p < W * H; p++) if (source.r.data[p * 4 + 3] > OPAQUE_ALPHA_ABOVE && art !== undefined && art.data[p] === 1) on++;
|
|
874
|
+
if (on === 0) continue;
|
|
875
|
+
const baseParts = others.limbs[l].segments.map((sg) => `"${sg.part}"`).join(', ');
|
|
876
|
+
const stated =
|
|
877
|
+
others.pose === 'skeleton'
|
|
878
|
+
? `its skeleton places none of ${unposed.map((b) => `"${b}"`).join(', ')} (the body-18 table names only the arm and leg bones), nor an ancestor of one`
|
|
879
|
+
: others.pose === 'drawn'
|
|
880
|
+
? `its drawn names none of ${unposed.map((b) => `"${b}"`).join(', ')}, nor an ancestor of one`
|
|
881
|
+
: `it gives neither skeleton nor drawn, so none of ${unposed.map((b) => `"${b}"`).join(', ')} is placed`;
|
|
882
|
+
problems.push({
|
|
883
|
+
code: 'ASSEMBLE_ALT_LIMB_POSE',
|
|
884
|
+
object: `part "${src}" (${owners}, and config.limbs.${l})`,
|
|
885
|
+
detail: `holds one island (8-connected, alpha above ${OPAQUE_ALPHA_ABOVE}), and ${on} of its px lie where the base build draws ${l} (${baseParts}) at setup, so the layer holds both ${ownNames.join(', ')} and ${l}; the set carries ${ownNames.join(', ')} alone, so each pixel goes to the limb whose segment line is nearest it in the set's pose, and the set does not pose ${l}: ${stated}; its place in the set is never taken from the base's — drawn would need ${unposed.map((b) => `"${b}"`).join(', ')}, which a subset set may not state for a bone it does not cover (CONFIG_SET_COVERS_BONE), so a skeleton file whose keypoints place ${l} is required, or a set that carries ${l}'s parts too, or a layer that holds ${ownNames.join(', ')} alone`,
|
|
886
|
+
});
|
|
887
|
+
refused = true;
|
|
888
|
+
}
|
|
889
|
+
if (refused || posed.length === 0) return;
|
|
890
|
+
const polyOf = (l: string, all: Readonly<Record<string, Limb>>): Array<[number, number]> => limbPolyline(all[l], places);
|
|
891
|
+
// The too-close refusal, per own limb and other limb, on the own limb's half-width.
|
|
892
|
+
for (const own of ownNames) {
|
|
893
|
+
const radii = parts.filter((q) => q.limb === own && q.record.cap !== undefined).map((q) => (q.record.cap as CapRecord).radius);
|
|
894
|
+
for (const l of posed) {
|
|
895
|
+
if (radii.length === 0) {
|
|
896
|
+
problems.push({
|
|
897
|
+
code: 'ASSEMBLE_ALT_LIMB_OVERLAP',
|
|
898
|
+
object: `config.limbs.${own} (part "${src}", and config.limbs.${l})`,
|
|
899
|
+
detail: `takes "${src}", which holds ${own} and ${l} in one island; no joint of ${own} carries a cap, so no half-width is measured to tell whether the two segment lines run apart; a limb of two segments or more is required`,
|
|
900
|
+
});
|
|
901
|
+
continue;
|
|
902
|
+
}
|
|
903
|
+
const half = Math.min(...radii);
|
|
904
|
+
const t = skeletonsTooClose(polyOf(own, limbs), polyOf(l, others.limbs), half);
|
|
905
|
+
if (t.overlap > 0 && t.close > OVERLAP_TOO_CLOSE_SHARE * t.overlap) {
|
|
906
|
+
problems.push({
|
|
907
|
+
code: 'ASSEMBLE_ALT_LIMB_OVERLAP',
|
|
908
|
+
object: `config.limbs.${own} (part "${src}", and config.limbs.${l})`,
|
|
909
|
+
detail: `takes "${src}", which holds ${own} and ${l} in one island, and in the set's pose ${l}'s segment line runs within ${pyRound(half, 3)} px (${own}'s half-width, the least cap radius of its joints) of ${own}'s, across ${own}, at ${t.close} of the ${t.overlap} readings 1 px apart along ${own}'s line where ${l}'s passes within ${pyRound(2 * half, 3)} px (${pyRound(t.close / t.overlap, 3)}; more than ${OVERLAP_TOO_CLOSE_SHARE} is most); the two limbs lie over each other there, so no line between them says whose a pixel is — a layer that holds ${own} alone, or a set that carries ${l}'s parts too, is required`,
|
|
910
|
+
});
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
if (problems.length > problemsBefore) return;
|
|
915
|
+
const drawAt = new Map(others.drawOrder.map((name, i) => [name, i]));
|
|
916
|
+
// Each other limb's pieces, and the part each piece's segment is.
|
|
917
|
+
const otherLines = posed.map((l) => ({ l, line: polyOf(l, others.limbs), segs: others.limbs[l].segments }));
|
|
918
|
+
for (const { q, i } of mine) {
|
|
919
|
+
const piece = pieces[i];
|
|
920
|
+
const ownLine = polyOf(q.limb, limbs);
|
|
921
|
+
const ownAt = drawAt.get(q.part) ?? -1;
|
|
922
|
+
let kept = 0;
|
|
923
|
+
let bandKept = 0;
|
|
924
|
+
let bandGiven = 0;
|
|
925
|
+
const given = new Map<string, number>(posed.map((l) => [l, 0]));
|
|
926
|
+
for (let y = 0; y < H; y++) {
|
|
927
|
+
for (let x = 0; x < W; x++) {
|
|
928
|
+
const p = y * W + x;
|
|
929
|
+
const a = piece.r.data[p * 4 + 3];
|
|
930
|
+
if (a === 0) continue;
|
|
931
|
+
if (source.r.data[p * 4 + 3] === 0) {
|
|
932
|
+
if (a > OPAQUE_ALPHA_ABOVE) kept++;
|
|
933
|
+
continue;
|
|
934
|
+
}
|
|
935
|
+
const dOwn = Math.sqrt(polylineDist2(ownLine, x + 0.5, y + 0.5));
|
|
936
|
+
let best: { l: string; d: number; part: string } | null = null;
|
|
937
|
+
for (const o of otherLines) {
|
|
938
|
+
// The nearest piece of the other limb's line, and so its segment's part.
|
|
939
|
+
let dk = Infinity;
|
|
940
|
+
let seg = 0;
|
|
941
|
+
for (let k = 0; k + 1 < o.line.length; k++) {
|
|
942
|
+
const d = polylineDist2([o.line[k], o.line[k + 1]], x + 0.5, y + 0.5);
|
|
943
|
+
if (d < dk) {
|
|
944
|
+
dk = d;
|
|
945
|
+
seg = k;
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
if (o.line.length === 1) dk = polylineDist2(o.line, x + 0.5, y + 0.5);
|
|
949
|
+
const d = Math.sqrt(dk);
|
|
950
|
+
if (best === null || d < best.d) best = { l: o.l, d, part: o.segs[Math.min(seg, o.segs.length - 1)].part };
|
|
951
|
+
}
|
|
952
|
+
const nearest = best as { l: string; d: number; part: string };
|
|
953
|
+
const inBand = Math.abs(nearest.d - dOwn) <= OVERLAP_BAND_PX;
|
|
954
|
+
const give = inBand ? (drawAt.get(nearest.part) ?? -1) > ownAt : nearest.d < dOwn;
|
|
955
|
+
if (!give) {
|
|
956
|
+
if (a > OPAQUE_ALPHA_ABOVE) {
|
|
957
|
+
kept++;
|
|
958
|
+
if (inBand) bandKept++;
|
|
959
|
+
}
|
|
960
|
+
continue;
|
|
961
|
+
}
|
|
962
|
+
if (a > OPAQUE_ALPHA_ABOVE) {
|
|
963
|
+
given.set(nearest.l, (given.get(nearest.l) ?? 0) + 1);
|
|
964
|
+
if (inBand) bandGiven++;
|
|
965
|
+
}
|
|
966
|
+
piece.r.data.fill(0, p * 4, p * 4 + 4);
|
|
967
|
+
if (piece.region !== null) piece.region.data[p] = 0;
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
splits.push({ part: q.part, of: src, limb: q.limb, kept, given: posed.map((l) => ({ limb: l, px: given.get(l) ?? 0 })), bandKept, bandGiven });
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
/**
|
|
975
|
+
* One `LINT` line per {@link SplitCount} (issue #609), in `config.limbs` order: the part's pixels kept and given to each
|
|
976
|
+
* other limb, each with its share of the part's pixels before the split (3 places), and the overlap band's pixels and
|
|
977
|
+
* which way they went.
|
|
978
|
+
*/
|
|
979
|
+
export function splitLines(set: string, counts: readonly SplitCount[]): string[] {
|
|
980
|
+
return counts.map((c) => {
|
|
981
|
+
const all = c.kept + c.given.reduce((s, g) => s + g.px, 0);
|
|
982
|
+
const share = (n: number): string => (all === 0 ? '0' : String(pyRound(n / all, 3)));
|
|
983
|
+
const gave = c.given.map((g) => `gave ${g.px} px (${share(g.px)}) to ${g.limb}`).join(', ');
|
|
984
|
+
return ` LINT set "${set}" ${c.part}: ${c.of} holds ${c.limb} and ${c.given.map((g) => g.limb).join(', ')} in one island (8-connected, alpha above ${OPAQUE_ALPHA_ABOVE}), split by the nearest segment line at the set's bones: kept ${c.kept} px (${share(c.kept)}), ${gave}; ${c.bandKept + c.bandGiven} px in the overlap band (nearer one line than the other by ${OVERLAP_BAND_PX} px or less) went to the limb drawn in front, ${c.bandKept} kept and ${c.bandGiven} given`;
|
|
985
|
+
});
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
/**
|
|
989
|
+
* One `LINT` line per {@link IslandCount} (issue #588), in `config.limbs` order. Issue #608: the specks under the floor
|
|
990
|
+
* are named when there are any, a joint taken to the nearest island carries its distance and its cap's radius, and
|
|
991
|
+
* the islands dropped are the ones the part lost a pixel from — `dropped none` when it lost none, never `dropped 1 (0 px)`.
|
|
992
|
+
*/
|
|
550
993
|
export function islandLines(set: string, counts: readonly IslandCount[]): string[] {
|
|
551
|
-
return counts.map(
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
994
|
+
return counts.map((c) => {
|
|
995
|
+
const specks =
|
|
996
|
+
c.specks === 0
|
|
997
|
+
? ''
|
|
998
|
+
: `, and ${c.specks} speck${c.specks === 1 ? '' : 's'} under ${pyRound(ISLAND_FLOOR_SHARE * 100, 3)} % of the largest (${c.floor} px), each with the island nearest it${c.dust === 0 ? '' : ` (${c.dust} equally near two, with none)`}`;
|
|
999
|
+
const joints = c.joints.map((j) => `${j.bone} [${j.at.join(', ')}]${j.off === undefined ? '' : ` (${j.off.distance} px off it, within its cap's radius ${j.off.margin})`}`).join(' and ');
|
|
1000
|
+
const dropped = c.droppedIslands === 0 ? 'dropped none' : `dropped ${c.droppedIslands} (${c.dropped} px)`;
|
|
1001
|
+
return ` LINT set "${set}" ${c.part}: ${c.of} holds ${c.islands} islands (8-connected, alpha above ${OPAQUE_ALPHA_ABOVE})${specks}, kept the one under ${joints} (${c.kept} px), ${dropped}`;
|
|
1002
|
+
});
|
|
555
1003
|
}
|
|
556
1004
|
|
|
557
1005
|
/** One `LINT` line per {@link ExcludedCount} that took a pixel (issue #587); none for a count of 0. */
|
|
@@ -2910,6 +3358,46 @@ export interface LimbPart {
|
|
|
2910
3358
|
record: LimbRecord;
|
|
2911
3359
|
}
|
|
2912
3360
|
|
|
3361
|
+
/**
|
|
3362
|
+
* Issue #603: which count of a cap record each pixel of its box was counted in — {@link CAP_FROM_LAYER} and the
|
|
3363
|
+
* constants after it, 0 where the pixel is off the disc or was counted missing (the cap wrote no pixel there) — the
|
|
3364
|
+
* box's pixel (i, j) at `codes[j * box[2] + i]`, rig pixel `(box[0] + i, box[1] + j)`.
|
|
3365
|
+
*/
|
|
3366
|
+
interface CapFrom {
|
|
3367
|
+
box: [number, number, number, number];
|
|
3368
|
+
codes: Uint8Array;
|
|
3369
|
+
}
|
|
3370
|
+
/** Issue #603: a {@link CapFrom} code per count of {@link CapRecord} a pixel the cap wrote or kept was counted in. */
|
|
3371
|
+
const CAP_FROM_LAYER = 1;
|
|
3372
|
+
const CAP_FROM_IMAGE = 2;
|
|
3373
|
+
const CAP_FROM_UPPER = 3;
|
|
3374
|
+
const CAP_FROM_LOWER = 4;
|
|
3375
|
+
const CAP_FROM_LOWER_KEPT = 5;
|
|
3376
|
+
|
|
3377
|
+
/**
|
|
3378
|
+
* Issue #603: the set exclusion cleared rig pixel `(x, y)` of a lower segment's part; when the cap wrote or kept it,
|
|
3379
|
+
* move it from the count it was in to `excluded_px`, so the record's counts are the PNG's. A pixel off the cap's disc,
|
|
3380
|
+
* or one the cap counted missing (it wrote none), changes nothing.
|
|
3381
|
+
*/
|
|
3382
|
+
function excludeCapPixel(cap: CapRecord, from: CapFrom, x: number, y: number): void {
|
|
3383
|
+
const [bx, by, bw, bh] = from.box;
|
|
3384
|
+
if (x < bx || y < by || x >= bx + bw || y >= by + bh) return;
|
|
3385
|
+
const i = (y - by) * bw + (x - bx);
|
|
3386
|
+
const code = from.codes[i];
|
|
3387
|
+
if (code === 0) return;
|
|
3388
|
+
from.codes[i] = 0;
|
|
3389
|
+
if (code === CAP_FROM_LAYER) cap.layer_px--;
|
|
3390
|
+
else if (code === CAP_FROM_IMAGE) cap.image_px = (cap.image_px as number) - 1;
|
|
3391
|
+
else if (code === CAP_FROM_UPPER) {
|
|
3392
|
+
cap.upper_px = (cap.upper_px as number) - 1;
|
|
3393
|
+
// Without a shape, upper_px is written only above 0 (issue #557).
|
|
3394
|
+
if (cap.upper_px === 0 && cap.shape === undefined) delete cap.upper_px;
|
|
3395
|
+
}
|
|
3396
|
+
else if (code === CAP_FROM_LOWER) cap.lower_px = (cap.lower_px as number) - 1;
|
|
3397
|
+
else cap.lower_kept_px = (cap.lower_kept_px as number) - 1;
|
|
3398
|
+
cap.excluded_px = (cap.excluded_px ?? 0) + 1;
|
|
3399
|
+
}
|
|
3400
|
+
|
|
2913
3401
|
/** What the limbs did to one part they took: its opaque pixels and each segment's share (`limbLines`). */
|
|
2914
3402
|
export interface LimbCount {
|
|
2915
3403
|
of: string;
|
|
@@ -3195,9 +3683,9 @@ function cutLimbs(
|
|
|
3195
3683
|
count: (m: Mask | null, region: Mask) => number,
|
|
3196
3684
|
hiddenOf: (part: string) => HiddenArt | null = () => null,
|
|
3197
3685
|
readHidden: (layer: string, by: string) => void = () => undefined,
|
|
3198
|
-
): { parts: LimbPart[]; pieces: Built[]; counts: LimbCount[] } {
|
|
3686
|
+
): { parts: LimbPart[]; pieces: Built[]; counts: LimbCount[]; capFrom: Array<CapFrom | null> } {
|
|
3199
3687
|
const names = Object.keys(limbs);
|
|
3200
|
-
if (names.length === 0) return { parts: [], pieces: [], counts: [] };
|
|
3688
|
+
if (names.length === 0) return { parts: [], pieces: [], counts: [], capFrom: [] };
|
|
3201
3689
|
const problems: Problem[] = [];
|
|
3202
3690
|
const places = bonePlaces(bones);
|
|
3203
3691
|
const parts: LimbPart[] = [];
|
|
@@ -3243,6 +3731,8 @@ function cutLimbs(
|
|
|
3243
3731
|
/** For each part taken, which limb (index into names) owns each pixel, -1 where it has none. */
|
|
3244
3732
|
const owners = new Map<string, Int32Array>();
|
|
3245
3733
|
const counts: LimbCount[] = [];
|
|
3734
|
+
// Issue #603: which count of its cap each pixel of a lower segment's cap box was counted in, for the set exclusion.
|
|
3735
|
+
const capFrom: Array<CapFrom | null> = parts.map(() => null);
|
|
3246
3736
|
for (const src of sources) {
|
|
3247
3737
|
const b = builtOf(src);
|
|
3248
3738
|
if (b === null) continue;
|
|
@@ -3444,6 +3934,10 @@ function cutLimbs(
|
|
|
3444
3934
|
let lowerPx = 0;
|
|
3445
3935
|
let lowerKept = 0;
|
|
3446
3936
|
let lowerMissing = 0;
|
|
3937
|
+
const from: CapFrom = { box, codes: new Uint8Array(Math.max(0, box[2]) * Math.max(0, box[3])) };
|
|
3938
|
+
const mark = (x: number, y: number, code: number): void => {
|
|
3939
|
+
from.codes[(y - box[1]) * box[2] + (x - box[0])] = code;
|
|
3940
|
+
};
|
|
3447
3941
|
for (let y = Math.max(0, box[1]); y < Math.min(H, box[1] + box[3]); y++) {
|
|
3448
3942
|
for (let x = Math.max(0, box[0]); x < Math.min(W, box[0] + box[2]); x++) {
|
|
3449
3943
|
if (!inDisc(line.at, radius, x, y)) continue;
|
|
@@ -3453,10 +3947,13 @@ function cutLimbs(
|
|
|
3453
3947
|
if (!ball) continue;
|
|
3454
3948
|
// Transparent is alpha 0, the reading the upper half's fill gives a layer's and the image's pixel: no pixel the
|
|
3455
3949
|
// lower piece paints, however faint, changes.
|
|
3456
|
-
if (lower.data[p * 4 + 3] > 0)
|
|
3457
|
-
|
|
3950
|
+
if (lower.data[p * 4 + 3] > 0) {
|
|
3951
|
+
lowerKept++;
|
|
3952
|
+
mark(x, y, CAP_FROM_LOWER_KEPT);
|
|
3953
|
+
} else if (img !== null && img.data[u + 3] > 0) {
|
|
3458
3954
|
lower.data.set(img.data.subarray(u, u + 4), p * 4);
|
|
3459
3955
|
lowerPx++;
|
|
3956
|
+
mark(x, y, CAP_FROM_LOWER);
|
|
3460
3957
|
} else {
|
|
3461
3958
|
lowerMissing++;
|
|
3462
3959
|
missing++;
|
|
@@ -3468,15 +3965,18 @@ function cutLimbs(
|
|
|
3468
3965
|
if (img !== null && img.data[u + 3] > 0) {
|
|
3469
3966
|
lower.data.set(img.data.subarray(u, u + 4), p * 4);
|
|
3470
3967
|
imagePx++;
|
|
3968
|
+
mark(x, y, CAP_FROM_IMAGE);
|
|
3471
3969
|
} else if (ownLower[p] === li && lowerSrc.r.data[p * 4 + 3] > 0) {
|
|
3472
3970
|
lower.data.set(lowerSrc.r.data.subarray(p * 4, p * 4 + 4), p * 4);
|
|
3473
3971
|
layerPx++;
|
|
3972
|
+
mark(x, y, CAP_FROM_LAYER);
|
|
3474
3973
|
} else if (upper.data[p * 4 + 3] > 0) {
|
|
3475
3974
|
// Issue #360: the disc is filled from the art — the upper segment's, where the lower's layer has none.
|
|
3476
3975
|
// Issue #557: under either shape — the half-disc too takes the neighbouring segment's pixels, so a cap is
|
|
3477
3976
|
// short only where no layer of the limb gives a pixel.
|
|
3478
3977
|
lower.data.set(upper.data.subarray(p * 4, p * 4 + 4), p * 4);
|
|
3479
3978
|
upperPx++;
|
|
3979
|
+
mark(x, y, CAP_FROM_UPPER);
|
|
3480
3980
|
} else {
|
|
3481
3981
|
missing++;
|
|
3482
3982
|
if (jointOffsets(line, x, y).across >= 0) shortLeft++;
|
|
@@ -3556,12 +4056,13 @@ function cutLimbs(
|
|
|
3556
4056
|
};
|
|
3557
4057
|
if (hidden !== null) readHidden(hidden.layer, `config.limbs.${l}.segments[${k}].cap (part "${sg.part}")`);
|
|
3558
4058
|
parts[at].record.cap = cap;
|
|
4059
|
+
capFrom[at] = from;
|
|
3559
4060
|
const c = counts.find((q) => q.of === sg.from);
|
|
3560
4061
|
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 });
|
|
3561
4062
|
});
|
|
3562
4063
|
}
|
|
3563
4064
|
refuseIfAny(problems);
|
|
3564
|
-
return { parts, pieces: pieces as Built[], counts };
|
|
4065
|
+
return { parts, pieces: pieces as Built[], counts, capFrom };
|
|
3565
4066
|
}
|
|
3566
4067
|
|
|
3567
4068
|
// ---------------------------------------------------------------------------
|
|
@@ -3598,6 +4099,8 @@ export interface AssembleResult {
|
|
|
3598
4099
|
excluded: ExcludedCount[];
|
|
3599
4100
|
/** Issue #588: what each limb segment part cut from a `limbIslands` part of more than one island kept; empty otherwise. */
|
|
3600
4101
|
islands: IslandCount[];
|
|
4102
|
+
/** Issue #609: what each limb segment part cut from a `limbIslands` part of one island that `otherLimbs` take kept; empty otherwise. */
|
|
4103
|
+
splits: SplitCount[];
|
|
3601
4104
|
}
|
|
3602
4105
|
|
|
3603
4106
|
/** A hidden-art layer of a run (issue #359) and what read it: `cuts[i].under`, or a limb segment's cap; none is said. */
|
|
@@ -4115,26 +4618,39 @@ export function assemble(input: AssembleInput): AssembleResult {
|
|
|
4115
4618
|
'plan' in d ? { name: plan[d.plan][0], b: built[d.plan] as Built } : 'cut' in d ? { name: cuts[d.cut].into, b: pieces[d.cut] as Built } : { name: lim.parts[d.limb].part, b: limbPieces[d.limb] as Built };
|
|
4116
4619
|
|
|
4117
4620
|
// Issue #588: a set that names one limb side keeps, in each part cut from a layer of more than one island, the island its joints fall on.
|
|
4118
|
-
|
|
4621
|
+
// Issue #609: on a layer of one island that also holds a limb the set does not carry, by the nearest segment line.
|
|
4622
|
+
const islandKept =
|
|
4623
|
+
(input.limbIslands ?? []).length === 0
|
|
4624
|
+
? { counts: [], splits: [], problems: [] }
|
|
4625
|
+
: keepJointIslands(input.limbIslands ?? [], input.limbs ?? {}, input.bones ?? [], lim.parts, limbPieces, limbSource, W, H, input.islandFloor ?? ISLAND_FLOOR_SHARE, input.otherLimbs);
|
|
4119
4626
|
refuseIfAny(islandKept.problems);
|
|
4120
4627
|
|
|
4121
4628
|
// Issue #587: a set's limb part loses the pixels a base part it does not replace covers, before anything is counted.
|
|
4629
|
+
// Issue #603: its merged_px and its cap record lose them too, so they count what its PNG holds.
|
|
4122
4630
|
const excluded: ExcludedCount[] = [];
|
|
4631
|
+
const recount = input.exclusionCounts !== 'before';
|
|
4123
4632
|
for (const ex of input.exclude ?? []) {
|
|
4124
4633
|
const d = order.find((q) => !('patch' in q) && pieceAt(q).name === ex.part);
|
|
4125
4634
|
const b = d === undefined || 'patch' in d ? null : pieceAt(d).b;
|
|
4635
|
+
const capOf = d !== undefined && 'limb' in d ? lim.parts[d.limb].record.cap : undefined;
|
|
4636
|
+
const from = d !== undefined && 'limb' in d ? lim.capFrom[d.limb] : null;
|
|
4126
4637
|
let px = 0;
|
|
4127
4638
|
if (b !== null) {
|
|
4128
4639
|
if (b.region === null) {
|
|
4129
4640
|
b.region = newMask(W, H);
|
|
4130
4641
|
b.region.data.fill(1);
|
|
4131
4642
|
}
|
|
4643
|
+
let mergedOut = 0;
|
|
4132
4644
|
for (let p = 0; p < W * H; p++) {
|
|
4133
4645
|
if (ex.mask.data[p] === 0 || b.r.data[p * 4 + 3] === 0) continue;
|
|
4134
4646
|
if (b.r.data[p * 4 + 3] > OPAQUE_ALPHA_ABOVE) px++;
|
|
4647
|
+
// merged_px counts the last merge's copied pixels and its ring inside the part's region (`mergedIn`).
|
|
4648
|
+
if (b.region.data[p] === 1) mergedOut += (b.trace.lastSel?.data[p] ?? 0) + (b.trace.lastRing?.data[p] ?? 0);
|
|
4135
4649
|
b.r.data.fill(0, p * 4, p * 4 + 4);
|
|
4136
4650
|
b.region.data[p] = 0;
|
|
4651
|
+
if (recount && capOf !== undefined && from !== null) excludeCapPixel(capOf, from, p % W, Math.floor(p / W));
|
|
4137
4652
|
}
|
|
4653
|
+
if (recount) b.merged -= mergedOut;
|
|
4138
4654
|
}
|
|
4139
4655
|
excluded.push({ part: ex.part, under: ex.under, px });
|
|
4140
4656
|
}
|
|
@@ -4302,6 +4818,7 @@ export function assemble(input: AssembleInput): AssembleResult {
|
|
|
4302
4818
|
...(headFacePrep.underfillPx > 0 ? { headFaceUnderfillPx: headFacePrep.underfillPx } : {}),
|
|
4303
4819
|
excluded,
|
|
4304
4820
|
islands: islandKept.counts,
|
|
4821
|
+
splits: islandKept.splits,
|
|
4305
4822
|
};
|
|
4306
4823
|
}
|
|
4307
4824
|
|