@willcgage/module-schematic 0.98.0 → 0.101.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/dist/index.cjs CHANGED
@@ -1799,8 +1799,8 @@ function turnoutFacing(input) {
1799
1799
  function turnoutOccupiedSpan(input) {
1800
1800
  const e = input.extent;
1801
1801
  if (!e) return null;
1802
- const a = input.pos - input.facing * e.behindPoints;
1803
- const b = input.pos + input.facing * e.aheadOfPoints;
1802
+ const a = input.pos - input.facing * e.behindFrog;
1803
+ const b = input.pos + input.facing * e.pastFrog;
1804
1804
  return { fromPos: Math.min(a, b), toPos: Math.max(a, b) };
1805
1805
  }
1806
1806
  function resizeFlexPiece(pieces, index, nextLengthInches) {
@@ -1833,10 +1833,12 @@ function partExtent(part) {
1833
1833
  if (pts.source !== "measured" || overall.source !== "measured") return null;
1834
1834
  const frog = part?.frogOffset;
1835
1835
  const aheadOfPoints = overall.inches - pts.inches;
1836
+ const frogMeasured = frog && frog.source === "measured";
1836
1837
  return {
1837
1838
  behindPoints: pts.inches,
1838
1839
  aheadOfPoints,
1839
- pastFrog: frog && frog.source === "measured" ? overall.inches - frog.inches : aheadOfPoints
1840
+ pastFrog: frogMeasured ? overall.inches - frog.inches : aheadOfPoints,
1841
+ behindFrog: frogMeasured ? frog.inches : pts.inches
1840
1842
  };
1841
1843
  }
1842
1844
  function partExtentForSize(size, library = BUILT_IN_TRACK_PARTS) {
@@ -2700,12 +2702,13 @@ function walkTrackGraph(graph, pieces, startAt, library = BUILT_IN_TRACK_PARTS)
2700
2702
  const through = byKey.get(`${here.piece}.through`) ?? byKey.get(`${here.piece}.legA`);
2701
2703
  const body = gap(throat, through);
2702
2704
  const lead = part.lead?.inches ?? body / 2;
2705
+ const frogAxial = part.frogOffset?.inches ?? (part.pointsOffset?.inches ?? 0) + lead;
2703
2706
  const dvj = byKey.get(`${here.piece}.diverge`) ?? byKey.get(`${here.piece}.legB`);
2704
2707
  const frogPt = throat && body > 0 && dvj ? {
2705
- x: throat.x + (dvj.x - throat.x) * lead / body,
2706
- y: throat.y + (dvj.y - throat.y) * lead / body
2708
+ x: throat.x + (dvj.x - throat.x) * frogAxial / body,
2709
+ y: throat.y + (dvj.y - throat.y) * frogAxial / body
2707
2710
  } : null;
2708
- const toFrog = here.joint === "throat" ? lead : here.joint === "diverge" || here.joint === "legB" ? frogPt && dvj ? Math.hypot(dvj.x - frogPt.x, dvj.y - frogPt.y) : Math.max(0, body - lead) : Math.max(0, body - lead);
2711
+ const toFrog = here.joint === "throat" ? frogAxial : here.joint === "diverge" || here.joint === "legB" ? frogPt && dvj ? Math.hypot(dvj.x - frogPt.x, dvj.y - frogPt.y) : Math.max(0, body - frogAxial) : Math.max(0, body - frogAxial);
2709
2712
  const frogPos = pos + toFrog;
2710
2713
  if (here.joint === "diverge" || here.joint === "legB") {
2711
2714
  route.endsAt = here.piece;
@@ -2720,8 +2723,8 @@ function walkTrackGraph(graph, pieces, startAt, library = BUILT_IN_TRACK_PARTS)
2720
2723
  if (!d || queued.has(dk)) continue;
2721
2724
  queued.add(dk);
2722
2725
  const fp = throat && body > 0 ? {
2723
- x: throat.x + (d.x - throat.x) * lead / body,
2724
- y: throat.y + (d.y - throat.y) * lead / body
2726
+ x: throat.x + (d.x - throat.x) * frogAxial / body,
2727
+ y: throat.y + (d.y - throat.y) * frogAxial / body
2725
2728
  } : null;
2726
2729
  pending.push({
2727
2730
  from: here.piece,
@@ -3007,6 +3010,323 @@ function deriveGraphDoc(doc, library = BUILT_IN_TRACK_PARTS) {
3007
3010
  });
3008
3011
  return { doc: out.doc, warnings: out.warnings };
3009
3012
  }
3013
+ function placeableTurnoutParts(library, kind) {
3014
+ return library.filter((p) => kind.includes(p.kind) && partGeometryGap(p) == null);
3015
+ }
3016
+ function moduleConversionReport(doc, library = BUILT_IN_TRACK_PARTS) {
3017
+ if (doc.graph?.pieces?.length) {
3018
+ return {
3019
+ alreadyGraph: true,
3020
+ offerable: false,
3021
+ readyWithoutAsking: false,
3022
+ turnouts: [],
3023
+ unanswered: [],
3024
+ orphanTracks: [],
3025
+ blockers: []
3026
+ };
3027
+ }
3028
+ const straight = placeableTurnoutParts(library, ["turnout"]);
3029
+ const curved = placeableTurnoutParts(library, ["curved-turnout"]);
3030
+ const blockers = [];
3031
+ const turnouts = (doc.turnouts ?? []).map((t) => {
3032
+ const wantCurved = t.curved === true;
3033
+ const pool = wantCurved ? curved : straight;
3034
+ const base = {
3035
+ id: t.id,
3036
+ name: t.name,
3037
+ pos: t.pos,
3038
+ size: t.size,
3039
+ statedPartId: t.partId
3040
+ };
3041
+ const exact = t.size == null ? [] : pool.filter((p) => p.frogNumber === t.size);
3042
+ const candidates = [...exact, ...pool.filter((p) => !exact.includes(p))].map((p) => p.id);
3043
+ const resolve = (part, from) => ({
3044
+ ...base,
3045
+ partId: part.id,
3046
+ from,
3047
+ source: partGeometry(part, library)?.source ?? null,
3048
+ why: null,
3049
+ candidates
3050
+ });
3051
+ if (t.partId) {
3052
+ const named = library.find((p) => p.id === t.partId);
3053
+ if (named && partGeometryGap(named) == null) return resolve(named, "named");
3054
+ return {
3055
+ ...base,
3056
+ partId: null,
3057
+ from: "unresolved",
3058
+ source: null,
3059
+ why: named ? `this turnout names ${named.id}, which cannot be placed yet \u2014 ${partGeometryGap(named)}` : `this turnout names a part the library does not have (${t.partId})`,
3060
+ candidates
3061
+ };
3062
+ }
3063
+ if (t.size != null && exact.length) return resolve(exact[0], "frog-number");
3064
+ return {
3065
+ ...base,
3066
+ partId: null,
3067
+ from: "unresolved",
3068
+ source: null,
3069
+ why: t.size == null ? "the document never says what this turnout is \u2014 no part, no frog number" : wantCurved ? `no curved turnout is placeable yet, so a curved #${t.size} cannot be converted` : `no measured #${t.size} in the parts library yet`,
3070
+ candidates
3071
+ };
3072
+ });
3073
+ for (const c of doc.crossings ?? [])
3074
+ blockers.push({
3075
+ kind: "crossing",
3076
+ ref: c.id,
3077
+ why: "crossing geometry is not modelled yet, so a diamond has no piece to become"
3078
+ });
3079
+ if (doc.loop)
3080
+ blockers.push({
3081
+ kind: "loop",
3082
+ why: "a balloon's curve radii were never recorded, and laying the loop would mean inventing them \u2014 the one thing ADR 0001 forbids"
3083
+ });
3084
+ const reached = new Set((doc.turnouts ?? []).map((t) => t.divergeTrack));
3085
+ const orphanTracks = (doc.tracks ?? []).filter((t) => t.role !== "main" && !reached.has(t.id)).map((t) => ({
3086
+ id: t.id,
3087
+ ...t.trackName ? { trackName: t.trackName } : {},
3088
+ role: t.role,
3089
+ why: "no turnout in the document diverges onto this track, so there is nothing to join it to"
3090
+ }));
3091
+ const unanswered = turnouts.filter((t) => !t.partId).map((t) => t.id);
3092
+ return {
3093
+ alreadyGraph: false,
3094
+ offerable: blockers.length === 0,
3095
+ readyWithoutAsking: blockers.length === 0 && unanswered.length === 0 && orphanTracks.length === 0,
3096
+ turnouts,
3097
+ unanswered,
3098
+ orphanTracks,
3099
+ blockers
3100
+ };
3101
+ }
3102
+ var CONVERSION_TIGHT_RADIUS_INCHES = 12;
3103
+ function docToGraph(doc, answers = {}, library = BUILT_IN_TRACK_PARTS) {
3104
+ const refuse = (why) => ({
3105
+ graph: null,
3106
+ refused: why,
3107
+ notLaid: [],
3108
+ warnings: []
3109
+ });
3110
+ const report = moduleConversionReport(doc, library);
3111
+ if (report.alreadyGraph) return refuse("this module is already authored as pieces");
3112
+ if (!report.offerable) return refuse(report.blockers.map((b) => b.why).join("; "));
3113
+ if ((doc.mainPath?.length ?? 0) > 2)
3114
+ return refuse(
3115
+ "this module's mainline is drawn with bends, and laying it as pieces would need each bend's radius \u2014 convert it once curved runs are supported"
3116
+ );
3117
+ const partById = new Map(library.map((p) => [p.id, p]));
3118
+ const chosen = /* @__PURE__ */ new Map();
3119
+ const unknown = [];
3120
+ for (const t of report.turnouts) {
3121
+ const id = answers.overrides?.[t.id] ?? (t.partId ? t.partId : answers.turnoutPartId) ?? null;
3122
+ const part = id ? partById.get(id) : void 0;
3123
+ if (!part || partGeometryGap(part)) unknown.push(t.id);
3124
+ else chosen.set(t.id, part);
3125
+ }
3126
+ if (unknown.length)
3127
+ return refuse(
3128
+ `${unknown.length} turnout${unknown.length === 1 ? "" : "s"} still need identifying: ${unknown.join(", ")}`
3129
+ );
3130
+ const flexId = DEFAULT_FLEX_PART_ID;
3131
+ if (!partById.get(flexId)) return refuse("the parts library has no flex track to lay plain track with");
3132
+ const maxPiece = maxFlexPieceInches(flexId, library);
3133
+ const tracks = doc.tracks ?? [];
3134
+ const trackById = new Map(tracks.map((t) => [t.id, t]));
3135
+ const turnouts = doc.turnouts ?? [];
3136
+ const mainLen = doc.lengthInches ?? Math.max(0, ...tracks.map((t) => t.toPos ?? 0), ...turnouts.map((t) => t.pos));
3137
+ if (!(mainLen > 0)) return refuse("this module has no length to lay track along");
3138
+ const pieces = [];
3139
+ const notLaid = [];
3140
+ const warnings = [];
3141
+ const rad = (d) => d * Math.PI / 180;
3142
+ const norm180 = (d) => ((d + 180) % 360 + 360) % 360 - 180;
3143
+ const r3 = (n) => Math.round(n * 1e3) / 1e3;
3144
+ const jointsOf = (p) => placedJoints([p], library);
3145
+ const jointAt = (p, id) => jointsOf(p).find((j) => j.joint === id) ?? null;
3146
+ const transition = (id, from, toY, dirSign = 1) => {
3147
+ const theta = rad(norm180(from.headingDeg - (dirSign > 0 ? 0 : 180)));
3148
+ const dy = (toY - from.y) * dirSign;
3149
+ if (Math.abs(theta) < 1e-9 || Math.abs(dy) < 1e-9) return null;
3150
+ const R = dy / (1 - Math.cos(theta));
3151
+ if (!Number.isFinite(R) || R === 0) return null;
3152
+ const mag = Math.abs(R);
3153
+ if (mag < CONVERSION_TIGHT_RADIUS_INCHES)
3154
+ warnings.push(
3155
+ `the curve bringing ${id} back parallel works out at ${mag.toFixed(1)}\u2033 radius \u2014 tighter than anyone lays by hand, which usually means the document's lane and its turnout disagree`
3156
+ );
3157
+ return {
3158
+ id,
3159
+ partId: flexId,
3160
+ x: from.x,
3161
+ y: from.y,
3162
+ rotationDeg: from.headingDeg,
3163
+ // Curving BACK toward the host: opposite the way we are already heading.
3164
+ // `radiusInches` is signed and the sign IS the side.
3165
+ radiusInches: r3(-Math.sign(theta) * mag),
3166
+ lengthInches: r3(mag * Math.abs(theta))
3167
+ };
3168
+ };
3169
+ const layTurnout = (t, hostY) => {
3170
+ const part = chosen.get(t.id);
3171
+ const geom = partGeometry(part, library);
3172
+ const branch = trackById.get(t.divergeTrack);
3173
+ const ends = [branch?.fromPos, branch?.toPos].filter(
3174
+ (n) => typeof n === "number" && Number.isFinite(n)
3175
+ );
3176
+ const divergeFarPos = ends.length ? ends.reduce((a, b) => Math.abs(b - t.pos) > Math.abs(a - t.pos) ? b : a) : void 0;
3177
+ const facing = turnoutFacing({
3178
+ pos: t.pos,
3179
+ divergeFarPos,
3180
+ flipped: t.flipped ?? false
3181
+ });
3182
+ const body = part.overallLength.inches;
3183
+ const lead = part.lead?.inches ?? body / 2;
3184
+ const frogAxial = part.frogOffset?.inches ?? (part.pointsOffset?.inches ?? 0) + lead;
3185
+ const throatPos = facing > 0 ? t.pos - frogAxial : t.pos + frogAxial;
3186
+ const hostLane = trackById.get(t.onTrack)?.lane ?? 0;
3187
+ const want = Math.sign((branch?.lane ?? hostLane) - hostLane) || 1;
3188
+ const unflipped = facing > 0 ? 1 : -1;
3189
+ const piece = {
3190
+ id: `t-${t.id}`,
3191
+ partId: part.id,
3192
+ x: throatPos,
3193
+ y: hostY,
3194
+ rotationDeg: facing > 0 ? 0 : 180,
3195
+ ...unflipped !== want ? { flipped: true } : {},
3196
+ ...t.name ? { name: t.name } : {}
3197
+ };
3198
+ const bodyEnds = [throatPos, facing > 0 ? throatPos + body : throatPos - body];
3199
+ const span = { fromPos: Math.min(...bodyEnds), toPos: Math.max(...bodyEnds) };
3200
+ const divergeId = geom.joints.find((j) => j.role === "diverge")?.id ?? "diverge";
3201
+ return { t, piece, span, divergeId };
3202
+ };
3203
+ const layFlex = (label, hostY, fromPos, toPos, occupied, cuts) => {
3204
+ const out = flexPieces({ fromPos, toPos, maxPieceInches: maxPiece, occupied, cuts: cuts ?? null });
3205
+ out.forEach(
3206
+ (f, i) => pieces.push({
3207
+ id: `f-${label}-${i}`,
3208
+ partId: flexId,
3209
+ x: f.fromPos,
3210
+ y: hostY,
3211
+ rotationDeg: 0,
3212
+ lengthInches: r3(f.lengthInches)
3213
+ })
3214
+ );
3215
+ return out;
3216
+ };
3217
+ const turnoutsOn = (trackId, hostY) => turnouts.filter((t) => t.onTrack === trackId && chosen.has(t.id)).sort((a, b) => a.pos - b.pos).map((t) => layTurnout(t, hostY));
3218
+ const main = trackById.get(MAIN_TRACK_ID) ?? tracks.find((t) => t.role === "main");
3219
+ if (!main) return refuse("this module's document has no mainline to convert");
3220
+ const mains = [main, ...tracks.filter((t) => t.role === "main" && t.id !== main.id)];
3221
+ const laidTurnouts = /* @__PURE__ */ new Map();
3222
+ const done = /* @__PURE__ */ new Set();
3223
+ for (const m of mains) {
3224
+ const y = laneOffsetAt(m.lane ?? 0, 0);
3225
+ const on = turnoutsOn(m.id, y);
3226
+ for (const l of on) {
3227
+ pieces.push(l.piece);
3228
+ laidTurnouts.set(l.t.id, l);
3229
+ }
3230
+ layFlex(m.id, y, m.fromPos ?? 0, m.toPos ?? mainLen, on.map((l) => l.span), m.flexCuts);
3231
+ done.add(m.id);
3232
+ }
3233
+ let progressed = true;
3234
+ while (progressed) {
3235
+ progressed = false;
3236
+ for (const t of turnouts) {
3237
+ if (!done.has(t.onTrack)) continue;
3238
+ const branch = trackById.get(t.divergeTrack);
3239
+ if (!branch || done.has(branch.id)) continue;
3240
+ const near = laidTurnouts.get(t.id);
3241
+ if (!near) continue;
3242
+ done.add(branch.id);
3243
+ progressed = true;
3244
+ const dj = jointAt(near.piece, near.divergeId);
3245
+ if (!dj) {
3246
+ notLaid.push({ id: branch.id, why: `turnout ${t.id} has no diverging end to leave from` });
3247
+ continue;
3248
+ }
3249
+ const branchY = laneOffsetAt(branch.lane ?? 0, 0);
3250
+ const curve = transition(branch.id, { x: dj.x, y: dj.y, headingDeg: dj.headingDeg }, branchY);
3251
+ let start = { x: dj.x, y: dj.y, headingDeg: dj.headingDeg };
3252
+ if (curve) {
3253
+ pieces.push(curve);
3254
+ const e = jointAt(curve, "b");
3255
+ if (e) start = { x: e.x, y: e.y, headingDeg: e.headingDeg };
3256
+ }
3257
+ const farSw = turnouts.find(
3258
+ (o) => o.id !== t.id && o.divergeTrack === branch.id && laidTurnouts.has(o.id)
3259
+ );
3260
+ const farJoint = farSw ? (() => {
3261
+ const f = laidTurnouts.get(farSw.id);
3262
+ return jointAt(f.piece, f.divergeId);
3263
+ })() : null;
3264
+ let endX = farJoint != null ? farJoint.x : Math.max(branch.fromPos ?? t.pos, branch.toPos ?? t.pos);
3265
+ let farCurve = null;
3266
+ if (farJoint) {
3267
+ farCurve = transition(
3268
+ `${branch.id}-far`,
3269
+ { x: farJoint.x, y: farJoint.y, headingDeg: farJoint.headingDeg },
3270
+ branchY,
3271
+ -1
3272
+ );
3273
+ if (farCurve) {
3274
+ pieces.push(farCurve);
3275
+ const e = jointAt(farCurve, "b");
3276
+ if (e) endX = e.x;
3277
+ }
3278
+ }
3279
+ if (endX - start.x <= 1e-6) {
3280
+ notLaid.push({
3281
+ id: branch.id,
3282
+ why: `the turnout and the curve bringing this track parallel already reach ${start.x.toFixed(1)}\u2033, past where it has to end at ${endX.toFixed(1)}\u2033 \u2014 with the turnout chosen there is no room for this track where the document places it`
3283
+ });
3284
+ continue;
3285
+ }
3286
+ const on = turnoutsOn(branch.id, branchY).filter((l) => {
3287
+ if (l.span.fromPos >= start.x - 1e-6) return true;
3288
+ notLaid.push({
3289
+ id: l.t.divergeTrack,
3290
+ why: `turnout ${l.t.id} sits at ${l.t.pos}\u2033 but this track has only reached ${start.x.toFixed(1)}\u2033 by then \u2014 the ladder is pitched tighter than the chosen turnout allows`
3291
+ });
3292
+ return false;
3293
+ });
3294
+ for (const l of on) {
3295
+ pieces.push(l.piece);
3296
+ laidTurnouts.set(l.t.id, l);
3297
+ }
3298
+ layFlex(branch.id, branchY, start.x, endX, on.map((l) => l.span), branch.flexCuts);
3299
+ }
3300
+ }
3301
+ const orphanWhy = new Map(report.orphanTracks.map((o) => [o.id, o.why]));
3302
+ const alreadySaid = new Set(notLaid.map((n) => n.id));
3303
+ for (const t of tracks) {
3304
+ if (t.role === "main" || done.has(t.id) || alreadySaid.has(t.id)) continue;
3305
+ notLaid.push({
3306
+ id: t.id,
3307
+ why: orphanWhy.get(t.id) ?? "the track it branches from could not be laid, so there is nothing to join it to"
3308
+ });
3309
+ }
3310
+ const startOf = (t) => {
3311
+ const y = laneOffsetAt(t.lane ?? 0, 0);
3312
+ let best = null;
3313
+ for (const p of pieces)
3314
+ for (const j of jointsOf(p)) {
3315
+ if (Math.abs(j.y - y) > 0.05) continue;
3316
+ if (!best || j.x < best.x) best = { piece: p.id, joint: j.joint, x: j.x };
3317
+ }
3318
+ return best ? { piece: best.piece, joint: best.joint } : null;
3319
+ };
3320
+ const startAt = startOf(main);
3321
+ if (!startAt) return refuse("nothing was laid on the mainline, so the module has no starting point");
3322
+ const second = mains[1] ? startOf(mains[1]) : null;
3323
+ return {
3324
+ graph: { pieces, startAt, ...second ? { start2: second } : {} },
3325
+ refused: null,
3326
+ notLaid,
3327
+ warnings
3328
+ };
3329
+ }
3010
3330
  function frogCasting(cl, opts = {}) {
3011
3331
  const g = opts.gaugeInches ?? RAIL_GAUGE_INCHES;
3012
3332
  const w = opts.reachInches ?? g * 1.5;
@@ -3685,6 +4005,7 @@ exports.crossoverPinches = crossoverPinches;
3685
4005
  exports.deriveEndplatePoses = deriveEndplatePoses;
3686
4006
  exports.deriveGraphDoc = deriveGraphDoc;
3687
4007
  exports.divergeSideForHand = divergeSideForHand;
4008
+ exports.docToGraph = docToGraph;
3688
4009
  exports.docToState = docToState;
3689
4010
  exports.emptyEditorState = emptyEditorState;
3690
4011
  exports.endplateCentreOffsetInches = endplateCentreOffsetInches;
@@ -3716,6 +4037,7 @@ exports.maxFlexPieceInches = maxFlexPieceInches;
3716
4037
  exports.mergeImportedParts = mergeImportedParts;
3717
4038
  exports.mergeStoredParts = mergeStoredParts;
3718
4039
  exports.moduleCenterline = moduleCenterline;
4040
+ exports.moduleConversionReport = moduleConversionReport;
3719
4041
  exports.moduleFeatures = moduleFeatures;
3720
4042
  exports.moduleFootprint = moduleFootprint;
3721
4043
  exports.moduleLengthFromSections = moduleLengthFromSections;