@willcgage/module-schematic 0.86.0 → 0.89.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.d.cts CHANGED
@@ -242,6 +242,9 @@ interface SchematicTurnout {
242
242
  interface SchematicSignal {
243
243
  id: string;
244
244
  pos: number;
245
+ /** Hold this signal by the piece it stands beside (ADR 0001). See
246
+ * {@link GraphAnchor} — a mast is planted next to a particular bit of rail. */
247
+ anchor?: GraphAnchor | null;
245
248
  /** Track the signal governs; absent = the primary main (lane 0). */
246
249
  track?: string;
247
250
  facing?: SignalFacing;
@@ -295,11 +298,40 @@ type IndustryLabelMode = "none" | "cars" | "inches";
295
298
  */
296
299
  /** One car-spot span of an industry on a track — an industry may have several
297
300
  * (a house track serving one customer across multiple spot tracks, #54). */
301
+ /**
302
+ * ⭐ A PLACE ON THE TRACK, HELD BY THE PIECE THAT IS THERE (ADR 0001).
303
+ *
304
+ * `pos` — inches from endplate A — is a fine way to *read* where something is
305
+ * and a poor way to *hold* it: it is a number about the whole module, so every
306
+ * edit anywhere upstream silently changes what it means. A car spot belongs to
307
+ * the rail it is beside. Anchored to the piece, moving that piece takes the
308
+ * industry with it and moving anything else leaves it alone, which is what an
309
+ * owner dragging track expects to happen.
310
+ *
311
+ * Present = `pos` / `fromPos` / `toPos` are DERIVED from this by
312
+ * {@link graphToDoc}. Absent = the authored numbers stand, exactly as before —
313
+ * nothing migrates (ADR 0001).
314
+ */
315
+ interface GraphAnchor {
316
+ /** The {@link TrackPiece} it sits on. */
317
+ piece: string;
318
+ /**
319
+ * Inches along that piece from its OWN origin end — joint `a` on flex, the
320
+ * throat on a turnout, and in every case the part's x=0.
321
+ *
322
+ * ⚠️ Deliberately NOT "from the end the walk came in by". Which end that is
323
+ * depends on where the module's endplate A happens to be, so the same spot
324
+ * beside the same rail would mean two different numbers on two modules.
325
+ */
326
+ atInches: number;
327
+ }
298
328
  interface IndustrySpot {
299
329
  track: string;
300
330
  fromPos: number;
301
331
  toPos: number;
302
332
  side?: SignalSide;
333
+ /** Hold this spot by the piece it is beside (ADR 0001). See {@link GraphAnchor}. */
334
+ anchor?: GraphAnchor | null;
303
335
  }
304
336
  interface SchematicIndustry {
305
337
  id: string;
@@ -312,6 +344,10 @@ interface SchematicIndustry {
312
344
  /** The primary car-spot span along `track`, inches from endplate A. */
313
345
  fromPos: number;
314
346
  toPos: number;
347
+ /** Hold this span by the piece it is beside (ADR 0001). See {@link GraphAnchor}.
348
+ * The span's LENGTH stays authored — a dock face is as long as it is built;
349
+ * only where it begins is read off the track. */
350
+ anchor?: GraphAnchor | null;
315
351
  /** Extra car-spot spans on other tracks — the industry's house-track spots. */
316
352
  spots?: IndustrySpot[];
317
353
  /** Which side of the track the building + label sit on. */
@@ -392,6 +428,27 @@ interface ModuleSchematicDoc {
392
428
  * shape; absent = derive it as a lane offset from Main 1. Physical view only
393
429
  * (#131). */
394
430
  main2Path?: BenchworkPoint[] | null;
431
+ /**
432
+ * ⭐⭐ THE AUTHORED TRACK, when this module was drawn as PIECES (ADR 0001).
433
+ *
434
+ * Present = this is the source, and `tracks`, `turnouts` and every anchored
435
+ * feature's position are DERIVED from it by {@link deriveGraphDoc} — they are
436
+ * still written into the document so that everything downstream, Free-
437
+ * Dispatcher included, reads an ordinary document and is never told which way
438
+ * the module was authored.
439
+ *
440
+ * Absent = the module is authored the way it always was, and nothing about it
441
+ * changes. No document is converted (ADR 0001): converting one would mean
442
+ * inventing the leads, frogs and radii nobody measured.
443
+ */
444
+ graph?: {
445
+ pieces: TrackPiece[];
446
+ /** Where the main starts: the joint endplate A's track arrives at. */
447
+ startAt: {
448
+ piece: string;
449
+ joint: string;
450
+ };
451
+ } | null;
395
452
  }
396
453
  /** A benchwork-outline vertex, module-local inches. The edge from this vertex
397
454
  * to the NEXT one is a straight line, unless `bulge` is set — then it's a
@@ -1122,6 +1179,8 @@ interface EditorIndustry {
1122
1179
  carTypes: string[];
1123
1180
  /** freemon_industries row (single source of truth), or null for a new one. */
1124
1181
  moduleIndustryId: number | null;
1182
+ /** The piece this span is held by (ADR 0001). See {@link GraphAnchor}. */
1183
+ anchor?: GraphAnchor | null;
1125
1184
  }
1126
1185
  /** A 3rd+ endplate — a branch/junction connection off the module (#170).
1127
1186
  * A module may have several (e.g. a set carrying a second railroad through:
@@ -1216,6 +1275,10 @@ interface EditorState {
1216
1275
  mainPath: BenchworkPoint[];
1217
1276
  /** Authored Main 2 centre-line (double-track only) — empty = lane offset (#131). */
1218
1277
  main2Path: BenchworkPoint[];
1278
+ /** ⭐ The track as PLACED PIECES (ADR 0001), when the owner drew it that way.
1279
+ * Absent = authored the way it always was, and nothing changes. See
1280
+ * {@link ModuleSchematicDoc.graph} and {@link deriveGraphDoc}. */
1281
+ graph?: ModuleSchematicDoc["graph"];
1219
1282
  }
1220
1283
  /** Build the empty editor state for a module of the given length. */
1221
1284
  declare function emptyEditorState(lengthInches: number): EditorState;
@@ -2446,6 +2509,17 @@ declare function placedJoints(pieces: TrackPiece[], library?: TrackPart[]): Plac
2446
2509
  * part carrying three joints of its own.
2447
2510
  */
2448
2511
  declare function buildTrackGraph(pieces: TrackPiece[], library?: TrackPart[], snapInches?: number): TrackGraph;
2512
+ /** A piece as the route crosses it — what an anchored feature resolves against. */
2513
+ interface RouteSpan {
2514
+ piece: string;
2515
+ /** The joints the route entered and left this piece by. Tell you which way
2516
+ * round the piece's own measurements run against the route's. */
2517
+ entryJoint: string;
2518
+ exitJoint: string;
2519
+ /** Inches from endplate A at the entry joint, and at the one it left by. */
2520
+ fromPos: number;
2521
+ toPos: number;
2522
+ }
2449
2523
  /** A route the walk found: a continuous run of pieces a train can travel. */
2450
2524
  interface GraphRoute {
2451
2525
  id: string;
@@ -2457,6 +2531,8 @@ interface GraphRoute {
2457
2531
  /** The turnout it runs back into. Set = a SIDING; null = it dead-ends. */
2458
2532
  endsAt: string | null;
2459
2533
  pieces: string[];
2534
+ /** The same pieces, each with where it starts and ends along the route. */
2535
+ spans: RouteSpan[];
2460
2536
  /** Furthest lateral offset reached — what gives a lane its side. */
2461
2537
  lateral: number;
2462
2538
  }
@@ -2488,6 +2564,94 @@ declare function walkTrackGraph(graph: TrackGraph, pieces: TrackPiece[], startAt
2488
2564
  piece: string;
2489
2565
  joint: string;
2490
2566
  }, library?: TrackPart[]): GraphWalk;
2567
+ /** Where an anchored feature turned out to be. */
2568
+ interface ResolvedAnchor {
2569
+ /** Inches from endplate A — the number the document and the dispatcher read. */
2570
+ pos: number;
2571
+ /** The route the anchored piece is part of. */
2572
+ routeId: string;
2573
+ /** The piece's own measuring direction runs BACKWARD along the route, so
2574
+ * increasing {@link GraphAnchor.atInches} moves toward endplate A. */
2575
+ reversed: boolean;
2576
+ }
2577
+ /**
2578
+ * Turn a {@link GraphAnchor} into a position along the module.
2579
+ *
2580
+ * The piece is measured from its own origin end, and the walk knows both ends
2581
+ * of it, so whichever end the route arrived by, the sum comes out the same.
2582
+ * Returns null when the anchored piece is not on any route — a spur nobody has
2583
+ * connected yet — which is a thing to report, not to guess at.
2584
+ */
2585
+ declare function resolveGraphAnchor(anchor: GraphAnchor, walk: GraphWalk, pieces: TrackPiece[], library?: TrackPart[]): ResolvedAnchor | null;
2586
+ /** What the graph cannot know, and therefore never invents. */
2587
+ interface GraphDocInput {
2588
+ /** Where the main begins: the joint endplate A's track arrives at. */
2589
+ startAt: {
2590
+ piece: string;
2591
+ joint: string;
2592
+ };
2593
+ /**
2594
+ * The rest of the document — module id, endplate identities, the benchwork,
2595
+ * industries and signals. Merged UNDERNEATH the derived keys, so the graph
2596
+ * wins on length, tracks and turnouts and on nothing else.
2597
+ *
2598
+ * ⏸️ Industries, signals and control points are PASSED THROUGH untouched.
2599
+ * Where they live in a graph is deliberately still open (ADR 0001 defers it
2600
+ * to persistence); carrying them is honest, re-deriving them would be a guess.
2601
+ */
2602
+ base?: Partial<ModuleSchematicDoc>;
2603
+ /**
2604
+ * Owner metadata for a run, keyed by the piece the run STARTS at — the piece
2605
+ * they select and name. Held here rather than on {@link TrackPiece} because
2606
+ * it describes the whole run, not the one piece.
2607
+ */
2608
+ meta?: Record<string, {
2609
+ trackName?: string;
2610
+ capacityFeet?: number | null;
2611
+ moduleTrackId?: number | null;
2612
+ }>;
2613
+ library?: TrackPart[];
2614
+ }
2615
+ interface GraphDocResult {
2616
+ doc: ModuleSchematicDoc;
2617
+ graph: TrackGraph;
2618
+ walk: GraphWalk;
2619
+ /** The walk's warnings, plus anything the emission itself had to leave out. */
2620
+ warnings: string[];
2621
+ }
2622
+ /**
2623
+ * Derive a `ModuleSchematicDoc` from placed pieces.
2624
+ *
2625
+ * ⭐ **NO HAND IS EMITTED.** A turnout's `kind` is left unset on purpose. The
2626
+ * graph knows which side the diverging route is on — it is where the piece IS —
2627
+ * so the lane carries the side and `moduleFeatures` keeps the lane it is given
2628
+ * (`resolveLane` only overrides the sign for a stated left/right). Emitting a
2629
+ * hand as well would be a second source for one fact, which is the ~120 lines
2630
+ * of reconciliation this model exists to remove.
2631
+ *
2632
+ * ⚠️ ONE MAIN. The walk starts at one endplate, so a double-track module's
2633
+ * second main is not reached — it surfaces in `warnings` as unreachable pieces
2634
+ * rather than vanishing quietly. Emitting Main 2 needs a second start point and
2635
+ * is not built yet.
2636
+ */
2637
+ declare function graphToDoc(pieces: TrackPiece[], input: GraphDocInput): GraphDocResult;
2638
+ /**
2639
+ * Bring a document up to date with the track its owner drew.
2640
+ *
2641
+ * A document with no `graph` is returned UNTOUCHED — that is the ADR's
2642
+ * no-migration rule in one line, and it is why every module authored before this
2643
+ * keeps behaving exactly as it did. With a graph, the tracks and turnouts are
2644
+ * re-read off the pieces and written back into the ordinary document keys, so
2645
+ * the module is stored in a form every existing reader already understands.
2646
+ *
2647
+ * ⭐ Owners' names, capacities and `module_tracks` links are carried across from
2648
+ * the tracks already in the document, keyed by id. Re-deriving must never cost
2649
+ * an owner the name they gave a siding.
2650
+ */
2651
+ declare function deriveGraphDoc(doc: ModuleSchematicDoc, library?: TrackPart[]): {
2652
+ doc: ModuleSchematicDoc;
2653
+ warnings: string[];
2654
+ };
2491
2655
  /** A frog casting's parts, in TURNOUT-LOCAL inches: `x` = distance past the
2492
2656
  * points, `y` = lateral offset from the through route. */
2493
2657
  interface FrogCasting {
@@ -2769,4 +2933,4 @@ declare function poseOverridesFromDoc(doc: ModuleSchematicDoc): Record<string, {
2769
2933
  heading: number;
2770
2934
  }>;
2771
2935
 
2772
- export { ATLAS_CODE55_N, BUILT_IN_TRACK_PARTS, type BenchworkPoint, type BranchConnector, CLEARANCE_SPACING_INCHES, CODE55_RAIL_HEIGHT_INCHES, DEFAULT_FLEX_PART_ID, type DimensionSource, type DrawCrossing, type DrawCrossover, type DrawIndustry, type DrawSignal, type DrawTrack, type DrawTurnout, ENDPLATE_FASCIA_CLEAR_INCHES, ENDPLATE_LEAD_INCHES, type EditorBranch, type EditorControlPoint, type EditorCpSignal, type EditorCrossing, type EditorIndustry, type EditorState, type EditorTrack, type EditorTurnout, type EndplateBConfig, type EndplateEdge, type EndplateEdgePose, type EndplatePose, type EndplateTrackIssue, type EndplateWidthIssue, FAST_TRACKS_N_ME55, FAST_TRACKS_N_ME55_CROSSOVERS, FLEX_TRACK_PARTS, FREEMO_ENDPLATE_TRACK_FASCIA_CLEARANCE_INCHES, FREEMO_ENDPLATE_WIDTH_MIN_INCHES, FREEMO_ENDPLATE_WIDTH_RECOMMENDED_INCHES, FREEMO_TRACK_SPACING_INCHES, type FlexPiece, type FlexPieceEnd, type FrogCasting, type GeometryType, type GraphConflict, type GraphConnection, type GraphRoute, type GraphTurnout, type GraphWalk, type ImportedPart, type IndustryLabelMode, type IndustrySpot, JOINT_SNAP_INCHES, type LanePinch, MAIN2_TRACK_ID, MAIN_TRACK_ID, type ModuleFeatures, type ModuleFootprint, type ModuleFootprintInput, type ModuleGeometryInput, type ModuleSchematicDoc, type ModuleTrackRow, N_CAR_LENGTH_INCHES, N_SCALE_RATIO, type OccupiedSpan, type OutlineFace, PINCH_EASE_INCHES, type PartAngle, type PartDimension, type PartEnd, type PartExtent, type PartGeometry, type PartJoint, type PartJointRole, type PartSegment, type PlacedJoint, RAIL_GAUGE_INCHES, type ReturnLoopGeometry, type ReturnLoopShape, type SchematicBlock, type SchematicControlPoint, type SchematicCrossing, type SchematicEndplate, type SchematicEndplateTrack, type SchematicIndustry, type SchematicSection, type SchematicSignal, type SchematicTrack, type SchematicTurnout, type SectionAdjacency, type SectionEnd, type SectionEndAssessment, type SectionFootprint, type SectionJointAssessment, type SectionRelativePos, type SignalFacing, type SignalSide, type SpanOverhang, type StoredTrackPart, TIE_HALF_LENGTH_INCHES, TURNOUT_LEAD_INCHES_PER_FROG, type TrackConfig, type TrackGraph, type TrackPart, type TrackPiece, type TrackRole, type TurnoutClosure, type TurnoutKind, type UsableCapacity, WHOLE_MODULE_SECTION_ID, asModuleSchematic, assessSectionEnd, assessSectionJoint, benchworkBand, benchworkOutline, buildCrossover, buildPassingSiding, buildTrackGraph, buildTransition, carCapacity, checkEndplateWidth, clearancePointPastFrogInches, crossoverPinches, deriveEndplatePoses, divergeSideForHand, docToState, emptyEditorState, endplateCentreOffsetInches, endplateEdgePose, endplateFaceSegments, endplateLead, endplateTrackOffsetInches, endplateWidthInches, flexPartFor, flexParts, flexPieces, flexUsage, frogCasting, frogNumberFromName, fromSectionRelative, geometryTurnDegrees, hasNoFarEndplate, importedPartToTrackPart, inchesToScaleFeet, isLoopDoc, isTransitionTurnout, laneOffsetAt, leadInchesForSize, maxFlexPieceInches, mergeImportedParts, mergeStoredParts, moduleCenterline, moduleFeatures, moduleFootprint, moduleLengthFromSections, moduleSections, nextId, parseXtpLibrary, partExtent, partExtentForSize, partGeometry, partGeometryGap, partOutlineAtFrog, partsPlaceable, pastFrogInchesForSize, pathLengthInches, placedJoints, poseNeedsManual, poseOverridesFromDoc, remapPos, resizeFlexPiece, returnLoop, sampleBenchworkOutline, samplePartSegments, samplePath, scaleFeetToInches, sectionAdjacency, sectionBand, sectionBreaksFromSections, sectionComponents, sectionFootprints, sectionNeighbours, sectionSpans, sectionSpansOrWhole, sectionedCenterline, sectionedEndPose, sliceCenterline, spanOverhang, stateToDoc, storedPartToTrackPart, toSectionRelative, trackMeetsEndplateIssues, trackPart, trackPath, turnoutClosure, turnoutFacing, turnoutOccupiedSpan, turnoutPartForSize, usableCapacity, walkTrackGraph };
2936
+ export { ATLAS_CODE55_N, BUILT_IN_TRACK_PARTS, type BenchworkPoint, type BranchConnector, CLEARANCE_SPACING_INCHES, CODE55_RAIL_HEIGHT_INCHES, DEFAULT_FLEX_PART_ID, type DimensionSource, type DrawCrossing, type DrawCrossover, type DrawIndustry, type DrawSignal, type DrawTrack, type DrawTurnout, ENDPLATE_FASCIA_CLEAR_INCHES, ENDPLATE_LEAD_INCHES, type EditorBranch, type EditorControlPoint, type EditorCpSignal, type EditorCrossing, type EditorIndustry, type EditorState, type EditorTrack, type EditorTurnout, type EndplateBConfig, type EndplateEdge, type EndplateEdgePose, type EndplatePose, type EndplateTrackIssue, type EndplateWidthIssue, FAST_TRACKS_N_ME55, FAST_TRACKS_N_ME55_CROSSOVERS, FLEX_TRACK_PARTS, FREEMO_ENDPLATE_TRACK_FASCIA_CLEARANCE_INCHES, FREEMO_ENDPLATE_WIDTH_MIN_INCHES, FREEMO_ENDPLATE_WIDTH_RECOMMENDED_INCHES, FREEMO_TRACK_SPACING_INCHES, type FlexPiece, type FlexPieceEnd, type FrogCasting, type GeometryType, type GraphAnchor, type GraphConflict, type GraphConnection, type GraphDocInput, type GraphDocResult, type GraphRoute, type GraphTurnout, type GraphWalk, type ImportedPart, type IndustryLabelMode, type IndustrySpot, JOINT_SNAP_INCHES, type LanePinch, MAIN2_TRACK_ID, MAIN_TRACK_ID, type ModuleFeatures, type ModuleFootprint, type ModuleFootprintInput, type ModuleGeometryInput, type ModuleSchematicDoc, type ModuleTrackRow, N_CAR_LENGTH_INCHES, N_SCALE_RATIO, type OccupiedSpan, type OutlineFace, PINCH_EASE_INCHES, type PartAngle, type PartDimension, type PartEnd, type PartExtent, type PartGeometry, type PartJoint, type PartJointRole, type PartSegment, type PlacedJoint, RAIL_GAUGE_INCHES, type ResolvedAnchor, type ReturnLoopGeometry, type ReturnLoopShape, type RouteSpan, type SchematicBlock, type SchematicControlPoint, type SchematicCrossing, type SchematicEndplate, type SchematicEndplateTrack, type SchematicIndustry, type SchematicSection, type SchematicSignal, type SchematicTrack, type SchematicTurnout, type SectionAdjacency, type SectionEnd, type SectionEndAssessment, type SectionFootprint, type SectionJointAssessment, type SectionRelativePos, type SignalFacing, type SignalSide, type SpanOverhang, type StoredTrackPart, TIE_HALF_LENGTH_INCHES, TURNOUT_LEAD_INCHES_PER_FROG, type TrackConfig, type TrackGraph, type TrackPart, type TrackPiece, type TrackRole, type TurnoutClosure, type TurnoutKind, type UsableCapacity, WHOLE_MODULE_SECTION_ID, asModuleSchematic, assessSectionEnd, assessSectionJoint, benchworkBand, benchworkOutline, buildCrossover, buildPassingSiding, buildTrackGraph, buildTransition, carCapacity, checkEndplateWidth, clearancePointPastFrogInches, crossoverPinches, deriveEndplatePoses, deriveGraphDoc, divergeSideForHand, docToState, emptyEditorState, endplateCentreOffsetInches, endplateEdgePose, endplateFaceSegments, endplateLead, endplateTrackOffsetInches, endplateWidthInches, flexPartFor, flexParts, flexPieces, flexUsage, frogCasting, frogNumberFromName, fromSectionRelative, geometryTurnDegrees, graphToDoc, hasNoFarEndplate, importedPartToTrackPart, inchesToScaleFeet, isLoopDoc, isTransitionTurnout, laneOffsetAt, leadInchesForSize, maxFlexPieceInches, mergeImportedParts, mergeStoredParts, moduleCenterline, moduleFeatures, moduleFootprint, moduleLengthFromSections, moduleSections, nextId, parseXtpLibrary, partExtent, partExtentForSize, partGeometry, partGeometryGap, partOutlineAtFrog, partsPlaceable, pastFrogInchesForSize, pathLengthInches, placedJoints, poseNeedsManual, poseOverridesFromDoc, remapPos, resizeFlexPiece, resolveGraphAnchor, returnLoop, sampleBenchworkOutline, samplePartSegments, samplePath, scaleFeetToInches, sectionAdjacency, sectionBand, sectionBreaksFromSections, sectionComponents, sectionFootprints, sectionNeighbours, sectionSpans, sectionSpansOrWhole, sectionedCenterline, sectionedEndPose, sliceCenterline, spanOverhang, stateToDoc, storedPartToTrackPart, toSectionRelative, trackMeetsEndplateIssues, trackPart, trackPath, turnoutClosure, turnoutFacing, turnoutOccupiedSpan, turnoutPartForSize, usableCapacity, walkTrackGraph };
package/dist/index.d.ts CHANGED
@@ -242,6 +242,9 @@ interface SchematicTurnout {
242
242
  interface SchematicSignal {
243
243
  id: string;
244
244
  pos: number;
245
+ /** Hold this signal by the piece it stands beside (ADR 0001). See
246
+ * {@link GraphAnchor} — a mast is planted next to a particular bit of rail. */
247
+ anchor?: GraphAnchor | null;
245
248
  /** Track the signal governs; absent = the primary main (lane 0). */
246
249
  track?: string;
247
250
  facing?: SignalFacing;
@@ -295,11 +298,40 @@ type IndustryLabelMode = "none" | "cars" | "inches";
295
298
  */
296
299
  /** One car-spot span of an industry on a track — an industry may have several
297
300
  * (a house track serving one customer across multiple spot tracks, #54). */
301
+ /**
302
+ * ⭐ A PLACE ON THE TRACK, HELD BY THE PIECE THAT IS THERE (ADR 0001).
303
+ *
304
+ * `pos` — inches from endplate A — is a fine way to *read* where something is
305
+ * and a poor way to *hold* it: it is a number about the whole module, so every
306
+ * edit anywhere upstream silently changes what it means. A car spot belongs to
307
+ * the rail it is beside. Anchored to the piece, moving that piece takes the
308
+ * industry with it and moving anything else leaves it alone, which is what an
309
+ * owner dragging track expects to happen.
310
+ *
311
+ * Present = `pos` / `fromPos` / `toPos` are DERIVED from this by
312
+ * {@link graphToDoc}. Absent = the authored numbers stand, exactly as before —
313
+ * nothing migrates (ADR 0001).
314
+ */
315
+ interface GraphAnchor {
316
+ /** The {@link TrackPiece} it sits on. */
317
+ piece: string;
318
+ /**
319
+ * Inches along that piece from its OWN origin end — joint `a` on flex, the
320
+ * throat on a turnout, and in every case the part's x=0.
321
+ *
322
+ * ⚠️ Deliberately NOT "from the end the walk came in by". Which end that is
323
+ * depends on where the module's endplate A happens to be, so the same spot
324
+ * beside the same rail would mean two different numbers on two modules.
325
+ */
326
+ atInches: number;
327
+ }
298
328
  interface IndustrySpot {
299
329
  track: string;
300
330
  fromPos: number;
301
331
  toPos: number;
302
332
  side?: SignalSide;
333
+ /** Hold this spot by the piece it is beside (ADR 0001). See {@link GraphAnchor}. */
334
+ anchor?: GraphAnchor | null;
303
335
  }
304
336
  interface SchematicIndustry {
305
337
  id: string;
@@ -312,6 +344,10 @@ interface SchematicIndustry {
312
344
  /** The primary car-spot span along `track`, inches from endplate A. */
313
345
  fromPos: number;
314
346
  toPos: number;
347
+ /** Hold this span by the piece it is beside (ADR 0001). See {@link GraphAnchor}.
348
+ * The span's LENGTH stays authored — a dock face is as long as it is built;
349
+ * only where it begins is read off the track. */
350
+ anchor?: GraphAnchor | null;
315
351
  /** Extra car-spot spans on other tracks — the industry's house-track spots. */
316
352
  spots?: IndustrySpot[];
317
353
  /** Which side of the track the building + label sit on. */
@@ -392,6 +428,27 @@ interface ModuleSchematicDoc {
392
428
  * shape; absent = derive it as a lane offset from Main 1. Physical view only
393
429
  * (#131). */
394
430
  main2Path?: BenchworkPoint[] | null;
431
+ /**
432
+ * ⭐⭐ THE AUTHORED TRACK, when this module was drawn as PIECES (ADR 0001).
433
+ *
434
+ * Present = this is the source, and `tracks`, `turnouts` and every anchored
435
+ * feature's position are DERIVED from it by {@link deriveGraphDoc} — they are
436
+ * still written into the document so that everything downstream, Free-
437
+ * Dispatcher included, reads an ordinary document and is never told which way
438
+ * the module was authored.
439
+ *
440
+ * Absent = the module is authored the way it always was, and nothing about it
441
+ * changes. No document is converted (ADR 0001): converting one would mean
442
+ * inventing the leads, frogs and radii nobody measured.
443
+ */
444
+ graph?: {
445
+ pieces: TrackPiece[];
446
+ /** Where the main starts: the joint endplate A's track arrives at. */
447
+ startAt: {
448
+ piece: string;
449
+ joint: string;
450
+ };
451
+ } | null;
395
452
  }
396
453
  /** A benchwork-outline vertex, module-local inches. The edge from this vertex
397
454
  * to the NEXT one is a straight line, unless `bulge` is set — then it's a
@@ -1122,6 +1179,8 @@ interface EditorIndustry {
1122
1179
  carTypes: string[];
1123
1180
  /** freemon_industries row (single source of truth), or null for a new one. */
1124
1181
  moduleIndustryId: number | null;
1182
+ /** The piece this span is held by (ADR 0001). See {@link GraphAnchor}. */
1183
+ anchor?: GraphAnchor | null;
1125
1184
  }
1126
1185
  /** A 3rd+ endplate — a branch/junction connection off the module (#170).
1127
1186
  * A module may have several (e.g. a set carrying a second railroad through:
@@ -1216,6 +1275,10 @@ interface EditorState {
1216
1275
  mainPath: BenchworkPoint[];
1217
1276
  /** Authored Main 2 centre-line (double-track only) — empty = lane offset (#131). */
1218
1277
  main2Path: BenchworkPoint[];
1278
+ /** ⭐ The track as PLACED PIECES (ADR 0001), when the owner drew it that way.
1279
+ * Absent = authored the way it always was, and nothing changes. See
1280
+ * {@link ModuleSchematicDoc.graph} and {@link deriveGraphDoc}. */
1281
+ graph?: ModuleSchematicDoc["graph"];
1219
1282
  }
1220
1283
  /** Build the empty editor state for a module of the given length. */
1221
1284
  declare function emptyEditorState(lengthInches: number): EditorState;
@@ -2446,6 +2509,17 @@ declare function placedJoints(pieces: TrackPiece[], library?: TrackPart[]): Plac
2446
2509
  * part carrying three joints of its own.
2447
2510
  */
2448
2511
  declare function buildTrackGraph(pieces: TrackPiece[], library?: TrackPart[], snapInches?: number): TrackGraph;
2512
+ /** A piece as the route crosses it — what an anchored feature resolves against. */
2513
+ interface RouteSpan {
2514
+ piece: string;
2515
+ /** The joints the route entered and left this piece by. Tell you which way
2516
+ * round the piece's own measurements run against the route's. */
2517
+ entryJoint: string;
2518
+ exitJoint: string;
2519
+ /** Inches from endplate A at the entry joint, and at the one it left by. */
2520
+ fromPos: number;
2521
+ toPos: number;
2522
+ }
2449
2523
  /** A route the walk found: a continuous run of pieces a train can travel. */
2450
2524
  interface GraphRoute {
2451
2525
  id: string;
@@ -2457,6 +2531,8 @@ interface GraphRoute {
2457
2531
  /** The turnout it runs back into. Set = a SIDING; null = it dead-ends. */
2458
2532
  endsAt: string | null;
2459
2533
  pieces: string[];
2534
+ /** The same pieces, each with where it starts and ends along the route. */
2535
+ spans: RouteSpan[];
2460
2536
  /** Furthest lateral offset reached — what gives a lane its side. */
2461
2537
  lateral: number;
2462
2538
  }
@@ -2488,6 +2564,94 @@ declare function walkTrackGraph(graph: TrackGraph, pieces: TrackPiece[], startAt
2488
2564
  piece: string;
2489
2565
  joint: string;
2490
2566
  }, library?: TrackPart[]): GraphWalk;
2567
+ /** Where an anchored feature turned out to be. */
2568
+ interface ResolvedAnchor {
2569
+ /** Inches from endplate A — the number the document and the dispatcher read. */
2570
+ pos: number;
2571
+ /** The route the anchored piece is part of. */
2572
+ routeId: string;
2573
+ /** The piece's own measuring direction runs BACKWARD along the route, so
2574
+ * increasing {@link GraphAnchor.atInches} moves toward endplate A. */
2575
+ reversed: boolean;
2576
+ }
2577
+ /**
2578
+ * Turn a {@link GraphAnchor} into a position along the module.
2579
+ *
2580
+ * The piece is measured from its own origin end, and the walk knows both ends
2581
+ * of it, so whichever end the route arrived by, the sum comes out the same.
2582
+ * Returns null when the anchored piece is not on any route — a spur nobody has
2583
+ * connected yet — which is a thing to report, not to guess at.
2584
+ */
2585
+ declare function resolveGraphAnchor(anchor: GraphAnchor, walk: GraphWalk, pieces: TrackPiece[], library?: TrackPart[]): ResolvedAnchor | null;
2586
+ /** What the graph cannot know, and therefore never invents. */
2587
+ interface GraphDocInput {
2588
+ /** Where the main begins: the joint endplate A's track arrives at. */
2589
+ startAt: {
2590
+ piece: string;
2591
+ joint: string;
2592
+ };
2593
+ /**
2594
+ * The rest of the document — module id, endplate identities, the benchwork,
2595
+ * industries and signals. Merged UNDERNEATH the derived keys, so the graph
2596
+ * wins on length, tracks and turnouts and on nothing else.
2597
+ *
2598
+ * ⏸️ Industries, signals and control points are PASSED THROUGH untouched.
2599
+ * Where they live in a graph is deliberately still open (ADR 0001 defers it
2600
+ * to persistence); carrying them is honest, re-deriving them would be a guess.
2601
+ */
2602
+ base?: Partial<ModuleSchematicDoc>;
2603
+ /**
2604
+ * Owner metadata for a run, keyed by the piece the run STARTS at — the piece
2605
+ * they select and name. Held here rather than on {@link TrackPiece} because
2606
+ * it describes the whole run, not the one piece.
2607
+ */
2608
+ meta?: Record<string, {
2609
+ trackName?: string;
2610
+ capacityFeet?: number | null;
2611
+ moduleTrackId?: number | null;
2612
+ }>;
2613
+ library?: TrackPart[];
2614
+ }
2615
+ interface GraphDocResult {
2616
+ doc: ModuleSchematicDoc;
2617
+ graph: TrackGraph;
2618
+ walk: GraphWalk;
2619
+ /** The walk's warnings, plus anything the emission itself had to leave out. */
2620
+ warnings: string[];
2621
+ }
2622
+ /**
2623
+ * Derive a `ModuleSchematicDoc` from placed pieces.
2624
+ *
2625
+ * ⭐ **NO HAND IS EMITTED.** A turnout's `kind` is left unset on purpose. The
2626
+ * graph knows which side the diverging route is on — it is where the piece IS —
2627
+ * so the lane carries the side and `moduleFeatures` keeps the lane it is given
2628
+ * (`resolveLane` only overrides the sign for a stated left/right). Emitting a
2629
+ * hand as well would be a second source for one fact, which is the ~120 lines
2630
+ * of reconciliation this model exists to remove.
2631
+ *
2632
+ * ⚠️ ONE MAIN. The walk starts at one endplate, so a double-track module's
2633
+ * second main is not reached — it surfaces in `warnings` as unreachable pieces
2634
+ * rather than vanishing quietly. Emitting Main 2 needs a second start point and
2635
+ * is not built yet.
2636
+ */
2637
+ declare function graphToDoc(pieces: TrackPiece[], input: GraphDocInput): GraphDocResult;
2638
+ /**
2639
+ * Bring a document up to date with the track its owner drew.
2640
+ *
2641
+ * A document with no `graph` is returned UNTOUCHED — that is the ADR's
2642
+ * no-migration rule in one line, and it is why every module authored before this
2643
+ * keeps behaving exactly as it did. With a graph, the tracks and turnouts are
2644
+ * re-read off the pieces and written back into the ordinary document keys, so
2645
+ * the module is stored in a form every existing reader already understands.
2646
+ *
2647
+ * ⭐ Owners' names, capacities and `module_tracks` links are carried across from
2648
+ * the tracks already in the document, keyed by id. Re-deriving must never cost
2649
+ * an owner the name they gave a siding.
2650
+ */
2651
+ declare function deriveGraphDoc(doc: ModuleSchematicDoc, library?: TrackPart[]): {
2652
+ doc: ModuleSchematicDoc;
2653
+ warnings: string[];
2654
+ };
2491
2655
  /** A frog casting's parts, in TURNOUT-LOCAL inches: `x` = distance past the
2492
2656
  * points, `y` = lateral offset from the through route. */
2493
2657
  interface FrogCasting {
@@ -2769,4 +2933,4 @@ declare function poseOverridesFromDoc(doc: ModuleSchematicDoc): Record<string, {
2769
2933
  heading: number;
2770
2934
  }>;
2771
2935
 
2772
- export { ATLAS_CODE55_N, BUILT_IN_TRACK_PARTS, type BenchworkPoint, type BranchConnector, CLEARANCE_SPACING_INCHES, CODE55_RAIL_HEIGHT_INCHES, DEFAULT_FLEX_PART_ID, type DimensionSource, type DrawCrossing, type DrawCrossover, type DrawIndustry, type DrawSignal, type DrawTrack, type DrawTurnout, ENDPLATE_FASCIA_CLEAR_INCHES, ENDPLATE_LEAD_INCHES, type EditorBranch, type EditorControlPoint, type EditorCpSignal, type EditorCrossing, type EditorIndustry, type EditorState, type EditorTrack, type EditorTurnout, type EndplateBConfig, type EndplateEdge, type EndplateEdgePose, type EndplatePose, type EndplateTrackIssue, type EndplateWidthIssue, FAST_TRACKS_N_ME55, FAST_TRACKS_N_ME55_CROSSOVERS, FLEX_TRACK_PARTS, FREEMO_ENDPLATE_TRACK_FASCIA_CLEARANCE_INCHES, FREEMO_ENDPLATE_WIDTH_MIN_INCHES, FREEMO_ENDPLATE_WIDTH_RECOMMENDED_INCHES, FREEMO_TRACK_SPACING_INCHES, type FlexPiece, type FlexPieceEnd, type FrogCasting, type GeometryType, type GraphConflict, type GraphConnection, type GraphRoute, type GraphTurnout, type GraphWalk, type ImportedPart, type IndustryLabelMode, type IndustrySpot, JOINT_SNAP_INCHES, type LanePinch, MAIN2_TRACK_ID, MAIN_TRACK_ID, type ModuleFeatures, type ModuleFootprint, type ModuleFootprintInput, type ModuleGeometryInput, type ModuleSchematicDoc, type ModuleTrackRow, N_CAR_LENGTH_INCHES, N_SCALE_RATIO, type OccupiedSpan, type OutlineFace, PINCH_EASE_INCHES, type PartAngle, type PartDimension, type PartEnd, type PartExtent, type PartGeometry, type PartJoint, type PartJointRole, type PartSegment, type PlacedJoint, RAIL_GAUGE_INCHES, type ReturnLoopGeometry, type ReturnLoopShape, type SchematicBlock, type SchematicControlPoint, type SchematicCrossing, type SchematicEndplate, type SchematicEndplateTrack, type SchematicIndustry, type SchematicSection, type SchematicSignal, type SchematicTrack, type SchematicTurnout, type SectionAdjacency, type SectionEnd, type SectionEndAssessment, type SectionFootprint, type SectionJointAssessment, type SectionRelativePos, type SignalFacing, type SignalSide, type SpanOverhang, type StoredTrackPart, TIE_HALF_LENGTH_INCHES, TURNOUT_LEAD_INCHES_PER_FROG, type TrackConfig, type TrackGraph, type TrackPart, type TrackPiece, type TrackRole, type TurnoutClosure, type TurnoutKind, type UsableCapacity, WHOLE_MODULE_SECTION_ID, asModuleSchematic, assessSectionEnd, assessSectionJoint, benchworkBand, benchworkOutline, buildCrossover, buildPassingSiding, buildTrackGraph, buildTransition, carCapacity, checkEndplateWidth, clearancePointPastFrogInches, crossoverPinches, deriveEndplatePoses, divergeSideForHand, docToState, emptyEditorState, endplateCentreOffsetInches, endplateEdgePose, endplateFaceSegments, endplateLead, endplateTrackOffsetInches, endplateWidthInches, flexPartFor, flexParts, flexPieces, flexUsage, frogCasting, frogNumberFromName, fromSectionRelative, geometryTurnDegrees, hasNoFarEndplate, importedPartToTrackPart, inchesToScaleFeet, isLoopDoc, isTransitionTurnout, laneOffsetAt, leadInchesForSize, maxFlexPieceInches, mergeImportedParts, mergeStoredParts, moduleCenterline, moduleFeatures, moduleFootprint, moduleLengthFromSections, moduleSections, nextId, parseXtpLibrary, partExtent, partExtentForSize, partGeometry, partGeometryGap, partOutlineAtFrog, partsPlaceable, pastFrogInchesForSize, pathLengthInches, placedJoints, poseNeedsManual, poseOverridesFromDoc, remapPos, resizeFlexPiece, returnLoop, sampleBenchworkOutline, samplePartSegments, samplePath, scaleFeetToInches, sectionAdjacency, sectionBand, sectionBreaksFromSections, sectionComponents, sectionFootprints, sectionNeighbours, sectionSpans, sectionSpansOrWhole, sectionedCenterline, sectionedEndPose, sliceCenterline, spanOverhang, stateToDoc, storedPartToTrackPart, toSectionRelative, trackMeetsEndplateIssues, trackPart, trackPath, turnoutClosure, turnoutFacing, turnoutOccupiedSpan, turnoutPartForSize, usableCapacity, walkTrackGraph };
2936
+ export { ATLAS_CODE55_N, BUILT_IN_TRACK_PARTS, type BenchworkPoint, type BranchConnector, CLEARANCE_SPACING_INCHES, CODE55_RAIL_HEIGHT_INCHES, DEFAULT_FLEX_PART_ID, type DimensionSource, type DrawCrossing, type DrawCrossover, type DrawIndustry, type DrawSignal, type DrawTrack, type DrawTurnout, ENDPLATE_FASCIA_CLEAR_INCHES, ENDPLATE_LEAD_INCHES, type EditorBranch, type EditorControlPoint, type EditorCpSignal, type EditorCrossing, type EditorIndustry, type EditorState, type EditorTrack, type EditorTurnout, type EndplateBConfig, type EndplateEdge, type EndplateEdgePose, type EndplatePose, type EndplateTrackIssue, type EndplateWidthIssue, FAST_TRACKS_N_ME55, FAST_TRACKS_N_ME55_CROSSOVERS, FLEX_TRACK_PARTS, FREEMO_ENDPLATE_TRACK_FASCIA_CLEARANCE_INCHES, FREEMO_ENDPLATE_WIDTH_MIN_INCHES, FREEMO_ENDPLATE_WIDTH_RECOMMENDED_INCHES, FREEMO_TRACK_SPACING_INCHES, type FlexPiece, type FlexPieceEnd, type FrogCasting, type GeometryType, type GraphAnchor, type GraphConflict, type GraphConnection, type GraphDocInput, type GraphDocResult, type GraphRoute, type GraphTurnout, type GraphWalk, type ImportedPart, type IndustryLabelMode, type IndustrySpot, JOINT_SNAP_INCHES, type LanePinch, MAIN2_TRACK_ID, MAIN_TRACK_ID, type ModuleFeatures, type ModuleFootprint, type ModuleFootprintInput, type ModuleGeometryInput, type ModuleSchematicDoc, type ModuleTrackRow, N_CAR_LENGTH_INCHES, N_SCALE_RATIO, type OccupiedSpan, type OutlineFace, PINCH_EASE_INCHES, type PartAngle, type PartDimension, type PartEnd, type PartExtent, type PartGeometry, type PartJoint, type PartJointRole, type PartSegment, type PlacedJoint, RAIL_GAUGE_INCHES, type ResolvedAnchor, type ReturnLoopGeometry, type ReturnLoopShape, type RouteSpan, type SchematicBlock, type SchematicControlPoint, type SchematicCrossing, type SchematicEndplate, type SchematicEndplateTrack, type SchematicIndustry, type SchematicSection, type SchematicSignal, type SchematicTrack, type SchematicTurnout, type SectionAdjacency, type SectionEnd, type SectionEndAssessment, type SectionFootprint, type SectionJointAssessment, type SectionRelativePos, type SignalFacing, type SignalSide, type SpanOverhang, type StoredTrackPart, TIE_HALF_LENGTH_INCHES, TURNOUT_LEAD_INCHES_PER_FROG, type TrackConfig, type TrackGraph, type TrackPart, type TrackPiece, type TrackRole, type TurnoutClosure, type TurnoutKind, type UsableCapacity, WHOLE_MODULE_SECTION_ID, asModuleSchematic, assessSectionEnd, assessSectionJoint, benchworkBand, benchworkOutline, buildCrossover, buildPassingSiding, buildTrackGraph, buildTransition, carCapacity, checkEndplateWidth, clearancePointPastFrogInches, crossoverPinches, deriveEndplatePoses, deriveGraphDoc, divergeSideForHand, docToState, emptyEditorState, endplateCentreOffsetInches, endplateEdgePose, endplateFaceSegments, endplateLead, endplateTrackOffsetInches, endplateWidthInches, flexPartFor, flexParts, flexPieces, flexUsage, frogCasting, frogNumberFromName, fromSectionRelative, geometryTurnDegrees, graphToDoc, hasNoFarEndplate, importedPartToTrackPart, inchesToScaleFeet, isLoopDoc, isTransitionTurnout, laneOffsetAt, leadInchesForSize, maxFlexPieceInches, mergeImportedParts, mergeStoredParts, moduleCenterline, moduleFeatures, moduleFootprint, moduleLengthFromSections, moduleSections, nextId, parseXtpLibrary, partExtent, partExtentForSize, partGeometry, partGeometryGap, partOutlineAtFrog, partsPlaceable, pastFrogInchesForSize, pathLengthInches, placedJoints, poseNeedsManual, poseOverridesFromDoc, remapPos, resizeFlexPiece, resolveGraphAnchor, returnLoop, sampleBenchworkOutline, samplePartSegments, samplePath, scaleFeetToInches, sectionAdjacency, sectionBand, sectionBreaksFromSections, sectionComponents, sectionFootprints, sectionNeighbours, sectionSpans, sectionSpansOrWhole, sectionedCenterline, sectionedEndPose, sliceCenterline, spanOverhang, stateToDoc, storedPartToTrackPart, toSectionRelative, trackMeetsEndplateIssues, trackPart, trackPath, turnoutClosure, turnoutFacing, turnoutOccupiedSpan, turnoutPartForSize, usableCapacity, walkTrackGraph };