@hatiolab/figure-model 0.1.85 → 0.1.86

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.
@@ -45,10 +45,18 @@ const clone = (asset) => structuredClone(asset);
45
45
  const nodeById = (m, id) => m.nodes.find((n) => n.id === id);
46
46
  const writerOf = (m, ref) => m.nodes.find((n) => Object.values(n.outputs).includes(ref));
47
47
  /** A part an authoring command can work with, and the pieces of it those commands need. */
48
+ /** The part whose row carries this one -- fastened to it, however far down -- or undefined. */
49
+ function rowHolding(m, id) {
50
+ const place = nodeById(m, id);
51
+ if (place?.op !== 'member@1' || nodeById(m, `${id}.repeat`))
52
+ return undefined;
53
+ const assembly = m.nodes.find((n) => n.op === 'assembly@1' && n.args.includes(place.outputs.placed));
54
+ return assembly ? String(assembly.id).replace(/\.assembly$/, '') : undefined;
55
+ }
48
56
  function partOf(m, id, path) {
49
57
  const place = nodeById(m, id);
50
58
  /* A row's template is the part too: it is placed through its row (`repeatPart`). */
51
- const rowTemplate = place?.op === 'member@1' && !!nodeById(m, `${id}.repeat`);
59
+ const rowTemplate = place?.op === 'member@1' && (!!nodeById(m, `${id}.repeat`) || rowHolding(m, id) !== undefined);
52
60
  if (!place || (place.op !== 'place@1' && !rowTemplate))
53
61
  fail('TARGET_ABSENT', path, `${id} is not a placed part`);
54
62
  const shape = writerOf(m, place.args[0]);
@@ -168,6 +176,25 @@ function facesOfPart(m, id, path) {
168
176
  fail('EDIT_TARGET', path, `${id} is a ${shape.op}; it has no flat face to fasten by`);
169
177
  return { shape, faces: faces };
170
178
  }
179
+ /**
180
+ * The face a joint's child is measured by: its own, or for `centre` the face that would meet the parent's -- the part is
181
+ * measured and turned as that face, and only where its middle sits differs (\`attach\`).
182
+ */
183
+ function childFaceOf(m, j) {
184
+ if (j.body1.face !== 'centre')
185
+ return j.body1.face;
186
+ const path = `joints.${j.id}.body1.face`;
187
+ if (!('part' in j.body0))
188
+ fail('JOINT_SCHEMA', path, 'a part stands on the mounting plane by a face of its own, not by its centre');
189
+ /* One of the six, in the record's words: the face across from it. An opening's face, read from the parent's shape. */
190
+ const named = j.body0.face;
191
+ if (Object.hasOwn(V3_FACES, named))
192
+ return Object.keys(V3_FACES).find(f => V3_FACES[f] === V3_FACES[named] && SIGN[f] === -SIGN[named]);
193
+ const theirs = facesOfPart(m, j.body0.part, path).faces[j.body0.face];
194
+ if (!theirs)
195
+ fail('JOINT_SCHEMA', path, `${j.body0.part} has no face ${j.body0.face}`);
196
+ return Object.keys(V3_FACES).find(f => V3_FACES[f] === theirs.axis && SIGN[f] === -theirs.normal);
197
+ }
171
198
  const planeOf = (shape, face) => face.plane.map(p => ({ ref: shape.args[p.arg], k: p.k }));
172
199
  /** The part's outer faces on each axis in its own frame; an axis without a pair of flat faces has none. */
173
200
  function ownSpansOf(m, id, path) {
@@ -447,7 +474,8 @@ function attach(asset, a, twist) {
447
474
  return standOnPlane(asset, a, path);
448
475
  if (a.part === to.part)
449
476
  fail('ATTACH_CYCLE', path, 'a part cannot be fastened to itself');
450
- if (!Object.hasOwn(V3_FACES, String(a.face)))
477
+ const byCentre = a.face === 'centre';
478
+ if (!byCentre && !Object.hasOwn(V3_FACES, String(a.face)))
451
479
  fail('SCHEMA', path, `${String(a.face)} is not a face`);
452
480
  /* The target's faces are the ones its shape declares: a portal's opening as well as its outer faces. */
453
481
  const target = facesOfPart(m, to.part, path);
@@ -459,6 +487,11 @@ function attach(asset, a, twist) {
459
487
  fail('SCHEMA', path, `${String(facing)} is neither meet nor flush`);
460
488
  const axis = theirFace.axis;
461
489
  const sign = theirFace.normal;
490
+ if (byCentre && facing !== 'meet')
491
+ fail('ATTACH_FACE', path, `${a.part} fastened by its centre sits on ${to.face}; flush has no meaning for a centre`);
492
+ /* By its centre, the part is measured as the face that would meet the target's; only its offset differs. */
493
+ if (byCentre)
494
+ a = { ...a, face: Object.keys(V3_FACES).find(f => V3_FACES[f] === axis && SIGN[f] === -sign) };
462
495
  if (V3_FACES[a.face] !== axis)
463
496
  fail('ATTACH_FACE', path, `${a.face} faces along ${V3_FACES[a.face]} and ${to.face} along ${axis}; fastening them would need a turn, which this command does not do`);
464
497
  const wanted = Object.keys(V3_FACES).find(f => V3_FACES[f] === axis && SIGN[f] === (facing === 'meet' ? -sign : sign));
@@ -480,7 +513,7 @@ function attach(asset, a, twist) {
480
513
  const theirs = ownSpansOf(m, to.part, path);
481
514
  const mine = standingSpansOf(m, asset, a.part, path);
482
515
  const mineAlong = mine[axis];
483
- if (!mineAlong)
516
+ if (!mineAlong && !byCentre)
484
517
  fail('EDIT_TARGET', path, `${a.part} has no flat ${a.face} face`);
485
518
  const name = namer(m, `${a.part}.on.${to.part}`);
486
519
  /*
@@ -496,9 +529,10 @@ function attach(asset, a, twist) {
496
529
  const zero = constantOf(m, 'mm', 0, 'd');
497
530
  const translation = { x: '', y: '', z: '' };
498
531
  const offset = { x: zero, y: zero, z: zero };
499
- translation[axis] = sumOf(m, name, planeOf(target.shape, theirFace), `seat.${axis}`);
500
- const ownFace = SIGN[a.face] > 0 ? mineAlong.hi : mineAlong.lo;
501
- offset[axis] = sumOf(m, name, [...neg(ownFace), { ref: gap, k: outward }], `along.${axis}`);
532
+ /* By its centre, the gap goes into the seat: the point fastened at -- and turned about -- is the part's middle. */
533
+ translation[axis] = sumOf(m, name, [...planeOf(target.shape, theirFace), ...(byCentre ? [{ ref: gap, k: outward }] : [])], `seat.${axis}`);
534
+ const ownFace = byCentre ? (mineAlong && !mineAlong.centred ? [...half(mineAlong.lo), ...half(mineAlong.hi)] : []) : SIGN[a.face] > 0 ? mineAlong.hi : mineAlong.lo;
535
+ offset[axis] = byCentre ? (ownFace.length ? sumOf(m, name, neg(ownFace), `along.${axis}`) : zero) : sumOf(m, name, [...neg(ownFace), { ref: gap, k: outward }], `along.${axis}`);
502
536
  /*
503
537
  Across the face. Centred by default; min and max keep the two parts' edges level as either is resized -- and the
504
538
  point fastened at is where those edges meet: the seat on the target's edge, the part's own edge on it. A door
@@ -610,8 +644,8 @@ function standOnPlane(asset, a, path) {
610
644
  const normal = placement === 'wall' ? 'z' : 'y';
611
645
  if (!takes.includes(a.face))
612
646
  fail('ATTACH_FACE', path, placement === 'wall'
613
- ? `${String(a.face)} faces along ${V3_FACES[a.face] ?? '?'}; the figure hangs on a wall, the plane Z = 0, so a part is fastened to it by its back`
614
- : `${String(a.face)} faces along ${V3_FACES[a.face] ?? '?'}; the plane faces along y, so a part stands on it by its bottom or hangs from it by its top`);
647
+ ? `${String(a.face)} faces along ${V3_FACES[a.face] ?? 'no axis'}; the figure hangs on a wall, the plane Z = 0, so a part is fastened to it by its back`
648
+ : `${String(a.face)} faces along ${V3_FACES[a.face] ?? 'no axis'}; the plane faces along y, so a part stands on it by its bottom or hangs from it by its top`);
615
649
  if (parentOf(m, a.part) && !a.replace)
616
650
  fail('ATTACH_REPLACED', path, `${a.part} is already fastened to ${parentOf(m, a.part)}; pass replace to stand it on the plane instead`);
617
651
  const mine = standingSpansOf(m, asset, a.part, path);
@@ -772,7 +806,11 @@ function reseat(asset, id, to, place) {
772
806
  const parent = /^(.*)\.local$/.exec(to);
773
807
  let world = nodeById(m, `${id}.world`);
774
808
  if (parent && parent[1] !== id) {
775
- const above = nodeById(m, `${parent[1]}.world`)?.outputs?.pose ?? nodeById(m, `${parent[1]}.chain`)?.outputs?.pose ?? nodeById(m, `${parent[1]}.pose`).outputs.pose;
809
+ /* A row's template has no chain of its own: what hangs from it hangs in its copy's frame, turned as it turns. */
810
+ const above = nodeById(m, `${parent[1]}.world`)?.outputs?.pose ??
811
+ nodeById(m, `${parent[1]}.chain`)?.outputs?.pose ??
812
+ nodeById(m, `${parent[1]}.turned`)?.outputs?.pose ??
813
+ nodeById(m, `${parent[1]}.pose`).outputs.pose;
776
814
  if (!world) {
777
815
  world = { id: `${id}.world`, op: 'compose@1', args: [above, chain.outputs.pose], outputs: { pose: `${id}.world.value` } };
778
816
  m.nodes.push(world);
@@ -1128,20 +1166,33 @@ function checkJoint(asset, j) {
1128
1166
  fail('JOINT_SCHEMA', `${path}.origin.twist`, 'a twist is a number of degrees');
1129
1167
  if (j.origin?.turn && Object.values(j.origin.turn).some(t => !Number.isFinite(t) || Math.abs(t / 90 - Math.round(t / 90)) > 1e-9))
1130
1168
  fail('JOINT_SCHEMA', `${path}.origin.turn`, 'a turn is in quarter turns; an angle about the face\'s normal is its twist');
1131
- /* A part in a row is its copies: nothing can hang from one place of it. */
1132
- const repeatedParent = 'part' in j.body0 ? others.find(o => o.repeat && o.body1.part === j.body0.part) : undefined;
1133
- if (repeatedParent)
1134
- fail('JOINT_SCHEMA', path, `${repeatedParent.body1.part} is repeated; its copies have no one place for ${j.body1.part} to hang from`);
1169
+ /*
1170
+ A row repeats its part and everything fastened to it (ADR-0093, 2026-09-26): a part fastened to a part in a row is
1171
+ on every copy. A row inside a row -- a grid -- is not covered yet.
1172
+ */
1173
+ const all = [...others, j];
1174
+ const upOf = (part) => all.find(o => o.body1.part === part && 'part' in o.body0);
1175
+ const rowAbove = (part) => {
1176
+ for (let up = upOf(part); up; up = upOf(up.body0.part))
1177
+ if (up.repeat)
1178
+ return up;
1179
+ return undefined;
1180
+ };
1181
+ if (j.repeat) {
1182
+ const outer = 'part' in j.body0 ? rowAbove(j.body0.part) : undefined;
1183
+ const below = (part) => all.filter(o => 'part' in o.body0 && o.body0.part === part).flatMap(o => [o.body1.part, ...below(o.body1.part)]);
1184
+ const inner = below(j.body1.part).map(p => all.find(o => o.body1.part === p)).find(o => o.repeat);
1185
+ if (outer || inner)
1186
+ fail('JOINT_SCHEMA', `${path}.repeat`, `${outer ? `${j.body1.part} is on ${outer.body1.part}'s row` : `${inner.body1.part} on ${j.body1.part} is a row`}; a row inside a row is not covered yet`);
1187
+ }
1135
1188
  if (j.repeat) {
1136
1189
  const r = j.repeat;
1137
1190
  const row = `${path}.repeat`;
1138
- if (j.type !== 'fixed')
1139
- fail('JOINT_SCHEMA', row, 'a part in a row does not move on a joint of its own (ADR-0093); fasten it fixed to repeat it');
1140
1191
  if (!('part' in j.body0))
1141
1192
  fail('JOINT_SCHEMA', row, 'a row fills the part it is fastened to; the mounting plane has no length to fill');
1142
1193
  if (!['X', 'Y', 'Z'].includes(r.axis))
1143
1194
  fail('JOINT_SCHEMA', row, `a row runs along X, Y or Z, not ${String(r.axis)}`);
1144
- if (r.axis.toLowerCase() === V3_FACES[j.body1.face])
1195
+ if (r.axis.toLowerCase() === V3_FACES[childFaceOf(asset.document.model, j)])
1145
1196
  fail('JOINT_SCHEMA', row, `a row runs across the face it is fastened to; ${r.axis} is out of that face`);
1146
1197
  if (typeof r.pitch === 'number' ? !(Number.isFinite(r.pitch) && r.pitch > 0) : !(r.pitch && typeof r.pitch === 'object' && r.pitch.from))
1147
1198
  fail('GEOMETRY_DOMAIN', row, 'a row has a positive pitch, or one that follows a length');
@@ -1153,11 +1204,6 @@ function checkJoint(asset, j) {
1153
1204
  if (!(Number.isFinite(r.rise.mm) && r.rise.mm !== 0))
1154
1205
  fail('GEOMETRY_DOMAIN', `${row}.rise`, 'a rise is a length in mm, not zero');
1155
1206
  }
1156
- const held = others.filter(o => 'part' in o.body0 && o.body0.part === j.body1.part).map(o => o.body1.part);
1157
- if (held.length)
1158
- fail('JOINT_SCHEMA', row, `${j.body1.part} holds ${held.join(', ')}; a part in a row holds nothing`);
1159
- if (others.some(o => o.mimic?.joint === j.id))
1160
- fail('JOINT_SCHEMA', row, `a joint follows ${j.id}; a part in a row does not move`);
1161
1207
  }
1162
1208
  }
1163
1209
  /**
@@ -1180,11 +1226,12 @@ function writeJoint(asset, j) {
1180
1226
  const turned = !isIdentity(back);
1181
1227
  const own = AXES.map(a => j.origin?.turn?.[a] ?? 0);
1182
1228
  /* The child as it stands: its quarter turn, then its twist about the normal of the face it is fastened by. */
1183
- const normal = V3_FACES[j.body1.face];
1229
+ const childFace = childFaceOf(m, j);
1230
+ const normal = V3_FACES[childFace];
1184
1231
  const standing = j.origin?.twist ? mul3(axisTurn(normal, j.origin.twist), turnMatrix(own)) : turnMatrix(own);
1185
1232
  /* In the parent's frame: its twist about the face's normal there, and the quarter turn that is left. */
1186
1233
  const inParent = mul3(back, standing);
1187
- const localNormal = axisOf(mv3(back, FACE_VECTOR[j.body1.face]), `${j.body1.part}'s ${j.body1.face}`, parent ?? '', back, path);
1234
+ const localNormal = axisOf(mv3(back, FACE_VECTOR[childFace]), `${j.body1.part}'s ${childFace}`, parent ?? '', back, path);
1188
1235
  /* The record's twist, about the normal as it lies in the parent's frame; what is left is a quarter turn. */
1189
1236
  /*
1190
1237
  In the parent's frame the child stands twisted about the face's normal -- by its own twist, and back by its
@@ -1219,13 +1266,14 @@ function writeJoint(asset, j) {
1219
1266
  for (const ref of now)
1220
1267
  dropIfUnused(asset, ref);
1221
1268
  }
1222
- if (j.repeat) {
1223
- if (turned)
1224
- fail('JOINT_SCHEMA', `joints.${j.id}.repeat`, `${parent} stands turned; a row across a turned part is not covered yet`);
1225
- return repeatPart(asset, j);
1226
- }
1227
- if (j.type === 'fixed')
1269
+ if (j.repeat && turned)
1270
+ fail('JOINT_SCHEMA', `joints.${j.id}.repeat`, `${parent} stands turned; a row across a turned part is not covered yet`);
1271
+ /* A row is laid out last, once its part's motion is written: every copy moves on it (a roller row spinning). */
1272
+ if (j.type === 'fixed') {
1273
+ if (j.repeat)
1274
+ repeatPart(asset, j);
1228
1275
  return;
1276
+ }
1229
1277
  const kind = j.type === 'prismatic' ? 'slide' : 'turn';
1230
1278
  const unit = j.type === 'prismatic' ? (j.travel ? 'ratio' : 'mm') : 'deg';
1231
1279
  const range = j.lowerLimit !== undefined && j.upperLimit !== undefined ? { min: j.lowerLimit, max: j.upperLimit } : j.type === 'revolute' ? { min: -180, max: 180 } : { min: 0, max: 1 };
@@ -1249,6 +1297,8 @@ function writeJoint(asset, j) {
1249
1297
  (the telescopic conveyor, 2026-09-25). For a leader whose control is its distance the two are one value.
1250
1298
  */
1251
1299
  leader ? { of: nodeById(m, `${leader.body1.part}.motion`)?.args[3] ?? stateIdOf(leader), k: j.mimic.multiplier, c: j.mimic.offset ?? 0 } : undefined, along[axisIndex] > 0 ? 1 : -1);
1300
+ if (j.repeat)
1301
+ repeatPart(asset, j);
1252
1302
  }
1253
1303
  const IDENTITY = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
1254
1304
  const isIdentity = (r) => r.every((row, i) => row.every((v, k) => Math.abs(v - (i === k ? 1 : 0)) < 1e-12));
@@ -1268,7 +1318,7 @@ function standingTurnOf(asset, part, path) {
1268
1318
  fail('EDIT_TARGET', path, `${part} is turned (${saidTurn(turn)}); fastening to a turned part by its faces is covered only for quarter turns`);
1269
1319
  const quarter = turn.every(t => t === 0) ? IDENTITY : turnMatrix(turn);
1270
1320
  /* A twisted part stands twisted about the normal of the face it is fastened by. */
1271
- return record?.origin?.twist ? mul3(axisTurn(V3_FACES[record.body1.face], record.origin.twist), quarter) : quarter;
1321
+ return record?.origin?.twist ? mul3(axisTurn(V3_FACES[childFaceOf(asset.document.model, record)], record.origin.twist), quarter) : quarter;
1272
1322
  }
1273
1323
  /** How a part stands before its twist: its quarter turn only. */
1274
1324
  function standingQuarterOf(asset, part, path) {
@@ -1396,7 +1446,9 @@ function repeatPart(asset, j) {
1396
1446
  if (!theirs)
1397
1447
  fail('EDIT_TARGET', path, `${parent} has no two faces along ${axis}; there is no length to fill`);
1398
1448
  const mine = standingSpansOf(m, asset, part, path)[axis];
1399
- if (!mine)
1449
+ /* A frustum has no faces across its axis; it is as wide there as its wider end (G6). */
1450
+ const widest = mine ? undefined : frustumWidthAlong(m, asset, part, axis, path);
1451
+ if (!mine && !widest)
1400
1452
  fail('EDIT_TARGET', path, `${part} has no two faces along ${axis}; its copies cannot be spaced`);
1401
1453
  const { place, pose } = partOf(m, part, path);
1402
1454
  const seat = nodeById(m, `${part}.seat.rigid`);
@@ -1404,19 +1456,19 @@ function repeatPart(asset, j) {
1404
1456
  const zero = constantOf(m, 'mm', 0, 'd');
1405
1457
  /* The seat moves to the row's start; each copy's centre is where the layout puts it from there. */
1406
1458
  seat.args = seat.args.map((ref, i) => (i === k ? sumOf(m, name, theirs.lo, 'start') : ref));
1407
- const own = mine.centred ? zero : sumOf(m, name, neg([...half(mine.lo), ...half(mine.hi)]), 'own-centre');
1459
+ const own = widest || mine.centred ? zero : sumOf(m, name, neg([...half(mine.lo), ...half(mine.hi)]), 'own-centre');
1408
1460
  pose.args = pose.args.map((ref, i) => (i === k ? own : ref));
1409
1461
  const length = sumOf(m, name, [...theirs.hi, ...neg(theirs.lo)], 'length');
1410
- const extent = sumOf(m, name, [...mine.hi, ...neg(mine.lo)], 'extent');
1462
+ const extent = widest ?? sumOf(m, name, [...mine.hi, ...neg(mine.lo)], 'extent');
1411
1463
  const centred = constantOf(m, 'ratio', 0.5, 'k');
1412
1464
  const limit = constantOf(m, 'count', REPEAT_LIMIT, 'limit');
1413
1465
  const count = j.repeat.count;
1414
1466
  m.nodes.push(count === undefined
1415
1467
  ? { id: `${part}.layout`, op: 'fit-pitch@1', args: [length, zero, zero, extent, measureRef(m, asset, name, j.repeat.pitch, 'pitch', path), centred, limit], outputs: { layout: `${part}.layout.value` } }
1416
1468
  : { id: `${part}.layout`, op: 'fixed-count@1', args: [constantOf(m, 'count', count, 'count'), length, zero, zero, extent, centred, limit], outputs: { layout: `${part}.layout.value` } });
1417
- /* The part is the row's template now: a member, drawn only through the row. */
1469
+ /* The part is the row's template now: a member, drawn only through the row -- turned by its motion, if it moves. */
1418
1470
  place.op = 'member@1';
1419
- place.args = [place.args[0], pose.outputs.pose];
1471
+ place.args = [place.args[0], nodeById(m, `${part}.turned`)?.outputs?.pose ?? pose.outputs.pose];
1420
1472
  m.nodes.push({ id: `${part}.assembly`, op: 'assembly@1', args: [place.outputs.placed], outputs: { assembly: `${part}.assembly.value` } });
1421
1473
  /* Where the parent stands, as `attach` finds it: fastened (its world or chain), or placed by its own numbers. */
1422
1474
  const above = nodeById(m, `${parent}.world`)?.outputs?.pose ?? nodeById(m, `${parent}.chain`)?.outputs?.pose ?? nodeById(m, `${parent}.pose`)?.outputs?.pose;
@@ -1435,6 +1487,51 @@ function repeatPart(asset, j) {
1435
1487
  params: { axis, ...(rise ? { riseAxis: rise.axis.toLowerCase() } : {}) }
1436
1488
  });
1437
1489
  }
1490
+ /**
1491
+ * Everything fastened to a row's part, however far down, goes into the row's assembly: each copy carries it (ADR-0093,
1492
+ * 2026-09-26). Its placement already reads its copy's frame -- it hangs from the part's pose, not from a chain.
1493
+ */
1494
+ function gatherRows(asset, joints) {
1495
+ const m = asset.document.model;
1496
+ for (const row of joints.filter(j => j.repeat)) {
1497
+ const assembly = nodeById(m, `${row.body1.part}.assembly`);
1498
+ const below = (part) => joints.filter(o => 'part' in o.body0 && o.body0.part === part).flatMap(o => [o.body1.part, ...below(o.body1.part)]);
1499
+ for (const part of below(row.body1.part)) {
1500
+ const place = nodeById(m, part);
1501
+ place.op = 'member@1';
1502
+ assembly.args.push(place.outputs.placed);
1503
+ }
1504
+ }
1505
+ }
1506
+ /**
1507
+ * How wide a frustum is across its own axis, where the row runs: the larger of its two diameters, as the bounds of two
1508
+ * candidates (\`affine-bounds@1\`) -- the room's own way to take the larger of sums, so the gate still reads it as a sum
1509
+ * (chief architect, 2026-09-26). Undefined for any other shape, or a row along the frustum's own axis.
1510
+ */
1511
+ function frustumWidthAlong(m, asset, part, axis, path) {
1512
+ const { shape } = partOf(m, part, path);
1513
+ if (shape.op !== 'frustum-shape@1')
1514
+ return undefined;
1515
+ const r = quarterTurnOf(m, asset, part, path, true);
1516
+ const own = AXES[r[AXES.indexOf(axis)].findIndex(v => v !== 0)];
1517
+ if (own === 'y')
1518
+ return undefined;
1519
+ const id = `${part}.width.${own}`;
1520
+ if (!nodeById(m, id)) {
1521
+ const zero = constantOf(m, 'mm', 0, 'd');
1522
+ const two = constantOf(m, 'ratio', 2, 'k');
1523
+ const [top, bottom] = shape.args;
1524
+ /* xmin, xmax, ymin, ymax, zmin, zmax: only the one maximum is read; the others hold a zero each. */
1525
+ m.nodes.push({
1526
+ id,
1527
+ op: 'affine-bounds@1',
1528
+ args: [zero, zero, top, two, zero, bottom, two, zero, zero, zero, zero],
1529
+ outputs: { xmin: `${id}.unused.xmin`, xmax: `${id}.value`, ymin: `${id}.unused.ymin`, ymax: `${id}.unused.ymax`, zmin: `${id}.unused.zmin`, zmax: `${id}.unused.zmax` },
1530
+ params: { layout: 'L0.1_1.0.0.0.0' }
1531
+ });
1532
+ }
1533
+ return `${id}.value`;
1534
+ }
1438
1535
  /** A row made back into its one part, so its fastening can be taken away or written again. */
1439
1536
  function unrepeatPart(asset, part) {
1440
1537
  const m = asset.document.model;
@@ -1504,10 +1601,15 @@ function rewriteJoints(asset) {
1504
1601
  stableDimension(asset.document.model, j.travel.source.part, j.travel.source.axis, `joints.${j.id}.travel`);
1505
1602
  const order = writingOrder(joints);
1506
1603
  const keep = { ...asset.stateDefaults };
1604
+ /* What hangs from a row's part is drawn through the row; it is a part of its own again while the joints are written. */
1605
+ for (const n of asset.document.model.nodes)
1606
+ if (n.op === 'member@1' && !nodeById(asset.document.model, `${n.id}.repeat`))
1607
+ n.op = 'place@1';
1507
1608
  for (const j of [...order].reverse())
1508
1609
  stripJoint(asset, j);
1509
1610
  for (const j of order)
1510
1611
  writeJoint(asset, j);
1612
+ gatherRows(asset, joints);
1511
1613
  // A joint's value as the author left it (「지금 자세를 기본으로」) is theirs, not the record's.
1512
1614
  for (const j of order)
1513
1615
  if (j.type !== 'fixed' && !j.mimic && keep[stateIdOf(j)] !== undefined)
@@ -1653,7 +1755,7 @@ function setJoint(asset, joint) {
1653
1755
  if (!jointAbove(asset, j.body1.part) && !j.origin?.turn) {
1654
1756
  const turn = turnOf(asset.document.model, asset, j.body1.part, j.body1.part);
1655
1757
  /* A part turned by any angle about the normal of the face it is fastened by keeps that angle as its twist. */
1656
- const n = AXES.indexOf(V3_FACES[j.body1.face]);
1758
+ const n = AXES.indexOf(V3_FACES[childFaceOf(asset.document.model, j)]);
1657
1759
  const quarter = (t) => t !== null && Number.isFinite(t) && Math.abs(t / 90 - Math.round(t / 90)) < 1e-9;
1658
1760
  const twist = !quarter(turn[n]) && turn.every((t, i) => i === n || t === 0) ? turn[n] : undefined;
1659
1761
  if (twist !== undefined && !j.origin?.twist)
@@ -2279,34 +2381,104 @@ function occupancyLins(asset, over) {
2279
2381
  const balls = new Map();
2280
2382
  /* A row's template stands for its copies (`repeatPart`): leaving it out left a conveyor's rollers out of its room. */
2281
2383
  const rowOf = (id) => nodeById(m, `${id}.repeat`);
2282
- for (const place of m.nodes.filter((n) => n.op === 'place@1' || (n.op === 'member@1' && rowOf(n.id)))) {
2384
+ const ends = over === 'rest' ? ['start'] : ['min', 'max'];
2385
+ /* A part's size along the figure's axes, whichever quarter turns it and what carries it stand in. */
2386
+ const sizesOf = (id, turns) => {
2387
+ /* A turned chain's reach is read from one size per axis (reachOf), so a part there must have one. */
2388
+ const own = occupiedExtentsOf(m, id, id, turns);
2389
+ const c = turns ? IDENTITY : worldTurnOf(m, asset, id);
2390
+ if (!c)
2391
+ throw new V3ContractError('EDIT_TARGET', id, `${id} is turned by other than quarter turns`);
2392
+ return Object.fromEntries(AXES.map((axis, i) => [axis, own[AXES[c[i].findIndex(v => v !== 0)]]]));
2393
+ };
2394
+ /*
2395
+ A row runs from its parent's one edge to the other along its axis, every copy inside (fit-pitch and fixed-count lay
2396
+ them out within that length). What it spans there, how long its part is along it, and how far a rising row climbs.
2397
+ */
2398
+ const runOf = (part) => {
2399
+ const row = rowOf(part);
2400
+ const axis = String(row.params?.axis ?? '').toLowerCase();
2401
+ const parent = parentOf(m, part);
2402
+ const straight = parent && worldTurnOf(m, asset, parent);
2403
+ if (!parent || !AXES.includes(axis) || !straight || AXES.some((_, i) => straight[i][i] !== 1))
2404
+ return `${part} is a row whose run this command cannot read`;
2405
+ const theirs = standingRefsOf(m, asset, parent, parent, false)[axis];
2406
+ const half = { terms: [{ ref: theirs.ref, k: 0.5 * theirs.k }], c: 0 };
2407
+ const centres = ends.map(end => centreLin(m, asset, parent, axis, end));
2408
+ const bad = centres.find(c => typeof c === 'string');
2409
+ if (bad)
2410
+ return String(bad);
2411
+ const mine = sizesOf(part, false)[axis][0];
2412
+ const extent = { terms: [{ ref: mine.ref, k: mine.k }], c: 0 };
2413
+ const record = (asset.joints ?? []).find(o => o.body1.part === part && o.repeat)?.repeat;
2414
+ let climb;
2415
+ if (record?.rise) {
2416
+ /*
2417
+ The last copy is (count − 1) rises past the first. A row that fills its length has at most
2418
+ (length − one copy) / pitch copies past the first, so that many rises bound it and follow the parent's size;
2419
+ a counted row has exactly count − 1.
2420
+ */
2421
+ const per = record.rise.mm;
2422
+ let reach;
2423
+ if (record.count !== undefined)
2424
+ reach = { terms: [], c: (record.count - 1) * per };
2425
+ else if (typeof record.pitch === 'number')
2426
+ reach = { terms: [{ ref: theirs.ref, k: (theirs.k * per) / record.pitch }, { ref: mine.ref, k: (-mine.k * per) / record.pitch }], c: 0 };
2427
+ else
2428
+ return `${part} is a rising row whose pitch follows a length; how far it rises is not added up`;
2429
+ climb = { axis: record.rise.axis.toLowerCase(), up: per > 0, reach };
2430
+ }
2431
+ return { axis, lows: centres.map(c => linAdd(c, half, -1)), highs: centres.map(c => linAdd(c, half, 1)), extent, climb };
2432
+ };
2433
+ /* A part in a row, or fastened to one: each copy carries it (ADR-0093, 2026-09-26). */
2434
+ for (const place of m.nodes.filter((n) => n.op === 'place@1' || (n.op === 'member@1' && (rowOf(n.id) || rowHolding(m, n.id))))) {
2283
2435
  const turns = carriedByATurn(m, asset, place.id);
2284
2436
  let sizes = {};
2285
2437
  try {
2286
- /* A turned chain's reach is read from one size per axis (reachOf), so a part there must have one. */
2287
- const own = turns ? occupiedExtentsOf(m, place.id, place.id, true) : occupiedExtentsOf(m, place.id, place.id);
2288
- /* Its size along the figure's axes, whichever quarter turns it and what carries it stand in. */
2289
- const c = turns ? IDENTITY : worldTurnOf(m, asset, place.id);
2290
- if (!c)
2291
- throw new V3ContractError('EDIT_TARGET', place.id, `${place.id} is turned by other than quarter turns`);
2292
- AXES.forEach((axis, i) => (sizes[axis] = own[AXES[c[i].findIndex(v => v !== 0)]]));
2438
+ sizes = sizesOf(place.id, turns);
2293
2439
  }
2294
2440
  catch (e) {
2295
2441
  skipped.push({ part: place.id, reason: e instanceof V3ContractError ? e.message : 'not a part whose size this command can read' });
2296
2442
  continue;
2297
2443
  }
2298
- const row = place.op === 'member@1' ? rowOf(place.id) : undefined;
2299
- if (turns && row) {
2300
- skipped.push({ part: place.id, reason: `${place.id} is a row carried by a turn; where its copies reach is not added up` });
2444
+ /* The part whose row carries this one: itself, or the part it hangs from in a row. */
2445
+ const rowPart = place.op === 'member@1' ? (rowOf(place.id) ? place.id : rowHolding(m, place.id)) : undefined;
2446
+ const run = rowPart ? runOf(rowPart) : undefined;
2447
+ if (typeof run === 'string') {
2448
+ skipped.push({ part: place.id, reason: run });
2301
2449
  continue;
2302
2450
  }
2451
+ const climbed = (at, side, onto, axis) => {
2452
+ if (run?.climb?.axis === axis && (run.climb.up ? 1 : -1) === side)
2453
+ onto[axis].push(linAdd(at, run.climb.reach));
2454
+ };
2303
2455
  if (turns) {
2304
- const reach = reachOf(m, asset, place.id, over === 'rest' ? ['start'] : ['min', 'max']);
2456
+ const reach = reachOf(m, asset, place.id, ends);
2305
2457
  if (typeof reach === 'string') {
2306
2458
  skipped.push({ part: place.id, reason: reach });
2307
2459
  continue;
2308
2460
  }
2309
2461
  parts.push(place.id);
2462
+ if (run) {
2463
+ /*
2464
+ Each copy turns about its own pivot, which lies on its row's run: along the row the copies reach the run's ends
2465
+ and the turn's reach past them; across it, the turn's reach about the pivot.
2466
+ */
2467
+ for (const l of run.lows)
2468
+ low[run.axis].push(linAdd(l, reach.radius, -1));
2469
+ for (const h of run.highs)
2470
+ high[run.axis].push(linAdd(h, reach.radius, 1));
2471
+ for (const at of reach.centres)
2472
+ for (const axis of AXES.filter(a => a !== run.axis)) {
2473
+ const lo = linAdd(at[axis], reach.radius, -1);
2474
+ const hi = linAdd(at[axis], reach.radius, 1);
2475
+ low[axis].push(lo);
2476
+ high[axis].push(hi);
2477
+ climbed(lo, -1, low, axis);
2478
+ climbed(hi, 1, high, axis);
2479
+ }
2480
+ continue;
2481
+ }
2310
2482
  for (const at of reach.centres) {
2311
2483
  const centre = Object.fromEntries(AXES.map(a => [a, linNorm(at[a])]));
2312
2484
  const key = JSON.stringify(centre);
@@ -2322,64 +2494,44 @@ function occupancyLins(asset, over) {
2322
2494
  max, so the answer holds whichever way the travel runs, and holds at every design size, because the
2323
2495
  choice is made when the graph is evaluated rather than when the proposal is written.
2324
2496
  */
2325
- const ends = over === 'rest' ? ['start'] : ['min', 'max'];
2326
2497
  const readings = AXES.map(axis => ({ axis, at: ends.map(end => centreLin(m, asset, place.id, axis, end)) }));
2327
2498
  const bad = readings.flatMap(r => r.at).find(v => typeof v === 'string');
2328
2499
  if (bad) {
2329
2500
  skipped.push({ part: place.id, reason: String(bad) });
2330
2501
  continue;
2331
2502
  }
2332
- /*
2333
- A row runs from its parent's one edge to the other along its axis, every copy inside (fit-pitch and fixed-count
2334
- lay them out within that length): along it the row takes the parent's room, across it the template's.
2335
- */
2336
- let rowAxis;
2337
- /* A rising row (G2): how much further the last copy stands than the first, along the axis it rises on. */
2338
- let climb;
2339
- if (row) {
2340
- rowAxis = String(row.params?.axis ?? '').toLowerCase();
2341
- const parent = parentOf(m, place.id);
2342
- const straight = parent && worldTurnOf(m, asset, parent);
2343
- if (!parent || !AXES.includes(rowAxis) || !straight || AXES.some((_, i) => straight[i][i] !== 1)) {
2344
- skipped.push({ part: place.id, reason: `${place.id} is a row whose run this command cannot read` });
2345
- continue;
2346
- }
2347
- const theirs = standingRefsOf(m, asset, parent, parent, false)[rowAxis];
2348
- const half = { terms: [{ ref: theirs.ref, k: 0.5 * theirs.k }], c: 0 };
2349
- const centres = ends.map(end => centreLin(m, asset, parent, rowAxis, end));
2350
- if (centres.some(c => typeof c === 'string')) {
2351
- skipped.push({ part: place.id, reason: String(centres.find(c => typeof c === 'string')) });
2503
+ if (run && rowPart === place.id) {
2504
+ /* The row's part: along the row it takes the run. */
2505
+ low[run.axis].push(...run.lows);
2506
+ high[run.axis].push(...run.highs);
2507
+ }
2508
+ else if (run) {
2509
+ /*
2510
+ Fastened to a row's part: its copies stand where that part's copies do, offset as it is from it. That part's
2511
+ centres lie half its length in from the run's ends.
2512
+ */
2513
+ const theirs = ends.map(end => centreLin(m, asset, rowPart, run.axis, end));
2514
+ const bad = theirs.find(c => typeof c === 'string');
2515
+ if (bad) {
2516
+ skipped.push({ part: place.id, reason: String(bad) });
2352
2517
  continue;
2353
2518
  }
2354
- const record = (asset.joints ?? []).find(o => o.body1.part === place.id && o.repeat)?.repeat;
2355
- if (record?.rise) {
2356
- /*
2357
- The last copy is (count − 1) rises past the first. A row that fills its length has at most
2358
- (length − one copy) / pitch copies past the first, so that many rises bound it and follow the parent's size;
2359
- a counted row has exactly count − 1.
2360
- */
2361
- const per = record.rise.mm;
2362
- let reach;
2363
- if (record.count !== undefined)
2364
- reach = { terms: [], c: (record.count - 1) * per };
2365
- else if (typeof record.pitch === 'number') {
2366
- const mine = sizes[rowAxis][0];
2367
- reach = { terms: [{ ref: theirs.ref, k: (theirs.k * per) / record.pitch }, { ref: mine.ref, k: (-mine.k * per) / record.pitch }], c: 0 };
2368
- }
2369
- else {
2370
- skipped.push({ part: place.id, reason: `${place.id} is a rising row whose pitch follows a length; how far it rises is not added up` });
2371
- continue;
2519
+ const along = readings.find(r => r.axis === run.axis).at;
2520
+ const inset = linAdd({ terms: [], c: 0 }, run.extent, 0.5);
2521
+ for (const [i, mine] of along.entries()) {
2522
+ const offset = linAdd(mine, theirs[i], -1);
2523
+ for (const e of sizes[run.axis]) {
2524
+ const half = { terms: [{ ref: e.ref, k: 0.5 * e.k }], c: 0 };
2525
+ for (const l of run.lows)
2526
+ low[run.axis].push(linAdd(linAdd(linAdd(l, inset), offset), half, -1));
2527
+ for (const h of run.highs)
2528
+ high[run.axis].push(linAdd(linAdd(linAdd(h, inset, -1), offset), half));
2372
2529
  }
2373
- climb = { axis: record.rise.axis.toLowerCase(), up: per > 0, reach };
2374
- }
2375
- for (const centre of centres) {
2376
- low[rowAxis].push(linAdd(centre, half, -1));
2377
- high[rowAxis].push(linAdd(centre, half, 1));
2378
2530
  }
2379
2531
  }
2380
2532
  parts.push(place.id);
2381
2533
  for (const r of readings) {
2382
- if (r.axis === rowAxis)
2534
+ if (r.axis === run?.axis)
2383
2535
  continue;
2384
2536
  const halves = sizes[r.axis].map(e => ({ terms: [{ ref: e.ref, k: 0.5 * e.k }], c: 0 }));
2385
2537
  const seen = new Set();
@@ -2389,10 +2541,12 @@ function occupancyLins(asset, over) {
2389
2541
  continue; // a part that does not move gives the same candidate twice
2390
2542
  seen.add(key);
2391
2543
  for (const half of halves) {
2392
- low[r.axis].push(linAdd(centre, half, -1));
2393
- high[r.axis].push(linAdd(centre, half, 1));
2394
- if (climb?.axis === r.axis)
2395
- (climb.up ? high : low)[r.axis].push(linAdd(linAdd(centre, half, climb.up ? 1 : -1), climb.reach));
2544
+ const lo = linAdd(centre, half, -1);
2545
+ const hi = linAdd(centre, half, 1);
2546
+ low[r.axis].push(lo);
2547
+ high[r.axis].push(hi);
2548
+ climbed(lo, -1, low, r.axis);
2549
+ climbed(hi, 1, high, r.axis);
2396
2550
  }
2397
2551
  }
2398
2552
  }
@@ -2846,7 +3000,7 @@ function proportionPlan(asset) {
2846
3000
  plan.skipped.push({ what: `${part}.place`, reason: 'stands by no fastening; fasten it for its place to follow' });
2847
3001
  }
2848
3002
  for (const j of (asset.joints ?? [])) {
2849
- const faceAxis = V3_FACES[j.body1.face];
3003
+ const faceAxis = V3_FACES[childFaceOf(m, j)];
2850
3004
  const gap = j.origin?.gap;
2851
3005
  if (typeof gap === 'number' && gap !== 0)
2852
3006
  plan.lengths.push({ joint: j.id, at: 'gap', axis: faceAxis, times: share(gap, faceAxis) });