@rig-c/parts 2.43.0 → 2.45.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/rig.ts CHANGED
@@ -137,7 +137,7 @@ import { explicitAttachment, type ExplicitMeshReport } from './explicitmesh.ts';
137
137
  * touches the disk, and the same inputs give the same bytes (key order is the
138
138
  * order the objects are built in, and every number is rounded by `pyRound`).
139
139
  */
140
- import { type AutoSpec, type BoneEntry, type InfluenceMeshSpec, type WeightRule, type CharacterConfig, type ConfigConstraint, type ConfigStop, CONSTRAINT_FOLLOWS, constraintForRig, contourRegionsOf, type ContourSpec, DEFAULT_BUST_BONE_L, DEFAULT_BUST_BONE_R, type Point, type RibsSpec, ROOT_BONE, stopForRig, turnRidersByBone, weightsABone } from './config.ts';
140
+ import { type AutoSpec, type BoneEntry, type InfluenceMeshSpec, type WeightRule, type CharacterConfig, type MeshSpec, type ConfigConstraint, type ConfigStop, CONSTRAINT_FOLLOWS, constraintForRig, contourRegionsOf, type ContourSpec, DEFAULT_BUST_BONE_L, DEFAULT_BUST_BONE_R, type Point, type RibsSpec, ROOT_BONE, stopForRig, turnRidersByBone, weightsABone } from './config.ts';
141
141
  import { RUNTIME_PI } from '@rig-c/core/core';
142
142
  import { type BoneTransform, computeExactFrameTransforms, computeWorldTransforms, cropToSpineY, normaliseDegrees, paintingToSetupPoint, setupToPaintingPoint, toBoneLocal, toWorld } from './coords.ts';
143
143
  import { foldName, type Problem, refuseIfAny, PartsError } from './errors.ts';
@@ -895,6 +895,71 @@ export function influenceReach(points: ReadonlyArray<readonly [number, number]>,
895
895
  return best;
896
896
  }
897
897
 
898
+ /** Hold the complete triangle wherever its interior crosses the cloth root row. */
899
+ function fixedTriangleRow(
900
+ row: number,
901
+ oy: number,
902
+ vertices: ReadonlyArray<readonly [number, number]>,
903
+ triangles: readonly number[],
904
+ ): number {
905
+ let held = row;
906
+ for (let i = 0; i < triangles.length; i += 3) {
907
+ const a = vertices[triangles[i]][1] + oy;
908
+ const b = vertices[triangles[i + 1]][1] + oy;
909
+ const c = vertices[triangles[i + 2]][1] + oy;
910
+ if (Math.min(a, b, c) < row) held = Math.max(held, a, b, c);
911
+ }
912
+ return held;
913
+ }
914
+
915
+ function rasterAlpha(img: Raster): Uint8Array {
916
+ const alpha = new Uint8Array(img.width * img.height);
917
+ for (let i = 0; i < alpha.length; i++) alpha[i] = img.data[i * 4 + 3];
918
+ return alpha;
919
+ }
920
+
921
+ function influenceLattice(img: Raster, alpha: Uint8Array, cell: number): SegmentsLattice {
922
+ return buildSegmentsLattice({ mask: { width: img.width, height: img.height, alpha }, threshold: INFLUENCE_THRESHOLD, cell });
923
+ }
924
+
925
+ function rigContour(p: PartsFile['parts'][number], img: Raster, spec: ContourSpec, ribs: ContourChainRibs[]) {
926
+ const ox = p.x - PAD;
927
+ const oy = p.y - PAD;
928
+ const regions: ContourRegion[] = contourRegionsOf(spec).map((rg) =>
929
+ rg.shape === 'circle'
930
+ ? { name: rg.name, shape: 'circle', cx: rg.cx - ox, cy: rg.cy - oy, r: rg.r, spacing: rg.spacing, band: rg.band }
931
+ : { name: rg.name, shape: 'polygon', points: rg.points.map(([x, y]) => [x - ox, y - oy]), spacing: rg.spacing, band: rg.band },
932
+ );
933
+ return contourMesh(p.name, { width: img.width, height: img.height, alpha: rasterAlpha(img) }, {
934
+ threshold: ART_ALPHA, tolerance: spec.tolerance, margin: spec.margin, spacing: spec.spacing,
935
+ ...(spec.maxEdge === undefined ? {} : { maxEdge: spec.maxEdge }), budget: spec.budget, stray: spec.stray,
936
+ ...(spec.bridges === undefined ? {} : { bridges: spec.bridges }), regions, ...(ribs.length === 0 ? {} : { ribs }),
937
+ });
938
+ }
939
+
940
+ /** The transition the same topology will need when rig binds the proposed mesh. */
941
+ export function draftClothHoldRow(p: PartsFile['parts'][number], image: Raster, mesh: MeshSpec, row: number): number | null {
942
+ const img = pad(image, PAD, PAD, PAD, PAD, [0, 0, 0, 0]);
943
+ const oy = p.y - PAD;
944
+ if ('influence' in mesh) {
945
+ try {
946
+ const m = influenceLattice(img, rasterAlpha(img), mesh.influence.cell);
947
+ return fixedTriangleRow(row, oy, m.points, m.triangles);
948
+ } catch {
949
+ return null;
950
+ }
951
+ }
952
+ if ('contour' in mesh) {
953
+ const m = rigContour(p, img, mesh.contour, []);
954
+ return Array.isArray(m) ? null : fixedTriangleRow(row, oy, m.vertices, m.triangles);
955
+ }
956
+ if ('grid' in mesh) {
957
+ const m = latticeMesh(p.name, alphaAbove(img, ART_ALPHA), mesh.grid);
958
+ return 'code' in m ? null : fixedTriangleRow(row, oy, m.vertices, m.triangles);
959
+ }
960
+ return null;
961
+ }
962
+
898
963
  /**
899
964
  * Issue #518: the figure {@link influenceReach} holds an influence mesh's radius above, measured as the rig stage
900
965
  * measures it — rig-c's segments lattice at `cell` over the part padded by {@link PAD} (alpha above
@@ -904,11 +969,9 @@ export function influenceReach(points: ReadonlyArray<readonly [number, number]>,
904
969
  */
905
970
  export function influenceFarthest(img: Raster, x: number, y: number, segs: readonly Segment[], cell: number): number | null {
906
971
  const padded = pad(img, PAD, PAD, PAD, PAD, [0, 0, 0, 0]);
907
- const alpha = new Uint8Array(padded.width * padded.height);
908
- for (let i = 0; i < alpha.length; i++) alpha[i] = padded.data[i * 4 + 3];
909
972
  let lattice: SegmentsLattice;
910
973
  try {
911
- lattice = buildSegmentsLattice({ mask: { width: padded.width, height: padded.height, alpha }, threshold: INFLUENCE_THRESHOLD, cell });
974
+ lattice = influenceLattice(padded, rasterAlpha(padded), cell);
912
975
  } catch {
913
976
  return null;
914
977
  }
@@ -1995,7 +2058,13 @@ export function buildWaistPairSource(
1995
2058
  if (cur === 'hip') hipChainLinks.add(l);
1996
2059
  }
1997
2060
  }
1998
- const dressSegsAt = (p: PartsFile['parts'][number], segs: Segment[]): ((wy: number) => Segment[]) => {
2061
+ const dressSegsAt = (p: PartsFile['parts'][number], segs: Segment[], heldRow?: number): ((wy: number) => Segment[]) => {
2062
+ const fixed = cfg.meshes[p.name]?.fixed_above;
2063
+ const hold = fixed === undefined ? null : segs.filter((s) => fixed.bones.includes(s.bone));
2064
+ const legacy = legacyDressSegsAt(p, segs);
2065
+ return (wy) => fixed !== undefined && hold !== null && wy <= (heldRow ?? fixed.row) ? hold : legacy(wy);
2066
+ };
2067
+ const legacyDressSegsAt = (p: PartsFile['parts'][number], segs: Segment[]): ((wy: number) => Segment[]) => {
1999
2068
  const hipBone = B.get('hip');
2000
2069
  if (hipBone === undefined) return () => segs;
2001
2070
  const isDress =
@@ -2033,16 +2102,9 @@ export function buildWaistPairSource(
2033
2102
  const oy = p.y - PAD;
2034
2103
  const w = img.width;
2035
2104
  const h = img.height;
2036
- const alpha = new Uint8Array(w * h);
2037
- for (let i = 0; i < alpha.length; i++) alpha[i] = img.data[i * 4 + 3];
2038
2105
  const regions = contourRegionsOf(spec);
2039
- const inPart: ContourRegion[] = regions.map((rg) =>
2040
- rg.shape === 'circle'
2041
- ? { name: rg.name, shape: 'circle', cx: rg.cx - ox, cy: rg.cy - oy, r: rg.r, spacing: rg.spacing, band: rg.band }
2042
- : { name: rg.name, shape: 'polygon', points: rg.points.map(([x, y]) => [x - ox, y - oy] as [number, number]), spacing: rg.spacing, band: rg.band },
2043
- );
2044
2106
  const ribs = chainRibs(ribSpec.segments, ribSpec.ribs, ox, oy);
2045
- const cm = contourMesh(p.name, { width: w, height: h, alpha }, { threshold: ART_ALPHA, tolerance: spec.tolerance, margin: spec.margin, spacing: spec.spacing, ...(spec.maxEdge === undefined ? {} : { maxEdge: spec.maxEdge }), budget: spec.budget, stray: spec.stray, ...(spec.bridges === undefined ? {} : { bridges: spec.bridges }), regions: inPart, ...(ribs.length === 0 ? {} : { ribs }) });
2107
+ const cm = rigContour(p, img, spec, ribs);
2046
2108
  if (Array.isArray(cm)) {
2047
2109
  out.push(...cm);
2048
2110
  return null;
@@ -2054,7 +2116,9 @@ export function buildWaistPairSource(
2054
2116
  out.push(...heat);
2055
2117
  return null;
2056
2118
  }
2057
- const segsAt = dressSegsAt(p, segs);
2119
+ const fixed = cfg.meshes[p.name]?.fixed_above;
2120
+ const heldRow = fixed === undefined ? undefined : fixedTriangleRow(fixed.row, oy, cm.vertices, cm.triangles);
2121
+ const segsAt = dressSegsAt(p, segs, heldRow);
2058
2122
  const weights: WeightEntry[][] = [];
2059
2123
  const reached = regions.map(() => 0);
2060
2124
  const whole = regions.map(() => 0);
@@ -2165,11 +2229,10 @@ export function buildWaistPairSource(
2165
2229
  const w = img.width;
2166
2230
  const h = img.height;
2167
2231
  const at = `config.meshes.${p.name}.influence`;
2168
- const alpha = new Uint8Array(w * h);
2169
- for (let i = 0; i < alpha.length; i++) alpha[i] = img.data[i * 4 + 3];
2232
+ const alpha = rasterAlpha(img);
2170
2233
  let lattice: SegmentsLattice;
2171
2234
  try {
2172
- lattice = buildSegmentsLattice({ mask: { width: w, height: h, alpha }, threshold: INFLUENCE_THRESHOLD, cell: spec.influence.cell });
2235
+ lattice = influenceLattice(img, alpha, spec.influence.cell);
2173
2236
  } catch (err) {
2174
2237
  out.push({ code: 'RIG_INFLUENCE_LATTICE', object: `${at}.cell`, detail: `rig-c's segments lattice refused part "${p.name}" at cell ${spec.influence.cell}: ${(err as Error).message}` });
2175
2238
  return null;
@@ -2201,10 +2264,16 @@ export function buildWaistPairSource(
2201
2264
  // Issue #615: `hold_tip: true`, declared on the mesh (propose writes it from the recipe's `split.hold_tip`), smooths
2202
2265
  // the pulled shares by the lattice's held-fork passes, capped at the declared `max`; absent, the shares are rig-c's.
2203
2266
  const shares = spec.influence.hold_tip === true ? smoothForkShares(pulled, lattice.triangles, spec.influence.max) : pulled;
2267
+ const fixed = cfg.meshes[p.name]?.fixed_above;
2268
+ const heldRow = fixed === undefined ? undefined : fixedTriangleRow(fixed.row, oy, lattice.points, lattice.triangles);
2269
+ const segsAt = dressSegsAt(p, [...segs], heldRow);
2204
2270
  for (const [vi, [x, y]] of lattice.points.entries()) {
2205
2271
  const wx = x + ox;
2206
2272
  const wy = y + oy;
2207
- const ent: WeightEntry[] = shares[vi].map(({ bone, weight }) => {
2273
+ const row = heldRow !== undefined && wy <= heldRow
2274
+ ? roundShares(influences([wx, wy], segsAt(wy), spec.influence.cell, { maxInfluences: spec.influence.max, minWeight: 0 }))
2275
+ : shares[vi];
2276
+ const ent: WeightEntry[] = row.map(({ bone, weight }) => {
2208
2277
  const [bx, by] = toBoneLocal(world.get(bone) as BoneTransform, spineX(wx), spineY(wy));
2209
2278
  return { bone, x: places(bx), y: places(by), weight };
2210
2279
  });
@@ -2648,7 +2717,9 @@ export function buildWaistPairSource(
2648
2717
  let maxInfl = 0;
2649
2718
  // issue #284: the mesh's declared cap and floor form, or DEFAULT_LIMITS itself when it declares none.
2650
2719
  const limits: InfluenceLimits = meshLimits(mesh.influences);
2651
- const segsAt = dressSegsAt(p, segs);
2720
+ const fixed = mesh.fixed_above;
2721
+ const heldRow = fixed === undefined ? undefined : fixedTriangleRow(fixed.row, oy, lm.vertices, lm.triangles);
2722
+ const segsAt = dressSegsAt(p, segs, heldRow);
2652
2723
  let emptied: { vertex: number; at: Point; capped: Influence[] } | null = null;
2653
2724
  let emptiedMore = 0;
2654
2725
  const rawShares: Influence[][] = [];
@@ -2685,7 +2756,8 @@ export function buildWaistPairSource(
2685
2756
  for (const [vi, [vx0, vy0]] of lm.vertices.entries()) {
2686
2757
  const wx = vx0 + ox;
2687
2758
  const wy = vy0 + oy;
2688
- const ent: WeightEntry[] = finalShares[vi].map(({ bone, weight }) => {
2759
+ const row = heldRow !== undefined && wy <= heldRow ? rawShares[vi] : finalShares[vi];
2760
+ const ent: WeightEntry[] = row.map(({ bone, weight }) => {
2689
2761
  const [x, y] = toBoneLocal(world.get(bone) as BoneTransform, spineX(wx), spineY(wy));
2690
2762
  return { bone, x: places(x), y: places(y), weight };
2691
2763
  });
@@ -2943,7 +3015,9 @@ export function buildWaistPairSource(
2943
3015
  const w = img.width;
2944
3016
  const h = img.height;
2945
3017
  const limits: InfluenceLimits = meshLimits('influences' in mesh ? mesh.influences : undefined);
2946
- const segsAt = dressSegsAt(p, segs);
3018
+ const fixed = mesh.fixed_above;
3019
+ const heldRow = fixed === undefined ? undefined : fixedTriangleRow(fixed.row, oy, lm.vertices, lm.triangles);
3020
+ const segsAt = dressSegsAt(p, segs, heldRow);
2947
3021
  const exponent = ('exponent' in mesh ? mesh.exponent : undefined) ?? DISTANCE_EXPONENT;
2948
3022
  let touchesArt: boolean[] | null = null;
2949
3023
  if (band === null && drawn !== undefined && limits.maxInfluences === 1) {
package/src/runstamp.ts CHANGED
@@ -16,6 +16,8 @@ import { SUPPORT_DOMAINS_FILE } from './supportdomains.ts';
16
16
 
17
17
  export const RUN_STAMP_SPEC = 'rig-run/1' as const;
18
18
  export const RUN_STAMP_FILE = 'run.json' as const;
19
+ export const COMPOSE_RUN_SPEC = 'rig-compose-run/1' as const;
20
+ export const COMPOSE_RUN_FILE = 'compose-run.json' as const;
19
21
  export const RUN_STAMP_INPUT_ROLES = ['source', 'full', 'head'] as const;
20
22
 
21
23
  export type RunStampCommand = 'rig' | 'assemble';
@@ -31,6 +33,15 @@ export interface RunStampFileEntry {
31
33
  sha256: string;
32
34
  }
33
35
 
36
+ /** Compose's consumed inputs, captured before its gate; no clock or output inventory. */
37
+ export interface ComposeRun {
38
+ spec: typeof COMPOSE_RUN_SPEC;
39
+ scene: RunStampFileEntry;
40
+ characters: Array<{ id: string; build: string; files: RunStampFileEntry[] }>;
41
+ plate: RunStampFileEntry | null;
42
+ plates: Array<{ name: string; path: string; sha256: string }>;
43
+ }
44
+
34
45
  export interface RunStampInputEntry {
35
46
  role: RunStampInputRole;
36
47
  path: string;
@@ -119,6 +130,11 @@ export function clearRunStamp(outDir: string): void {
119
130
  rmSync(join(outDir, RUN_STAMP_FILE), { force: true });
120
131
  }
121
132
 
133
+ /** Remove a previous compose record before CLI prevalidation or the compose stage begins. */
134
+ export function clearComposeRun(outDir: string): void {
135
+ rmSync(join(outDir, COMPOSE_RUN_FILE), { force: true });
136
+ }
137
+
122
138
  /**
123
139
  * Read package `name` and `version` from `package.json`, refusing with `RUN_STAMP_PACKAGE`
124
140
  * when the file is missing, invalid JSON, or lacks a non-empty string `name` or `version`.