refactor(model): extract kinematics and MVT logic out of SimModel - #22
Merged
Conversation
SimModel was doing too much — mix of state management, math, and physics. - Extract `computeTrackKinematics` + `finiteDifference` into `KinematicsComputer.ts` (pure functions, no Axon/SceneryStack deps) - Extract `buildModelViewTransform` into `ModelViewTransformFactory.ts` (pure function, no Axon/SceneryStack deps) - SimModel now imports and delegates to these modules; drops `Matrix3`, `MIN_CALIB_DISTANCE`, `MIN_PIXEL_DISTANCE`, and `KinematicPoint` from its own import list SimModel retains all Properties and wiring — only the math implementations move. Net result: SimModel shrinks from 475 → 341 lines; each extracted module is independently readable and testable. https://claude.ai/code/session_01P58fvbxUgRZn9eqbgDL4uV
The old type had two optional fields (property, accessor) with no static
guarantee that at least one was present. ConfigurableGraph.getValueForAxis
branched on accessor truthiness then fell back to property?.value ?? null —
defensive code that hid a missing constraint.
Replace with two concrete named types:
RecordPlottable — carries an accessor function; value extracted from a
data-point record (all current kinematics quantities).
LivePlottable — carries a TReadOnlyProperty<number>; value read live.
PlottableProperty is now their union. TypeScript narrows the correct branch
in getValueForAxis via 'accessor' in axisProperty; no optional chaining or
null fallback remains. A PlottableProperty with neither field is now a
compile-time error rather than a silent runtime null.
KinematicsGraphNode.createPlottableProperty already returns { name, unit,
accessor }, satisfying RecordPlottable without any changes.
https://claude.ai/code/session_01P58fvbxUgRZn9eqbgDL4uV
The MVT re-projection logic was an inline anonymous lambda with a single line of explanation. Any contributor writing to tracksProperty without reading that specific listener would silently corrupt point positions. Two changes: 1. Extract the re-projection loop into retransformTrackPoints(prev, next). The JSDoc explains the invariant, the pixel → model math, and the warning about direct writes to tracksProperty. 2. Add an INVARIANT comment block at the tracksProperty declaration — the first thing a contributor reads when they find the field — summarising the constraint and pointing to retransformTrackPoints(). The listener itself shrinks to two lines; all the 'why' is now in the named method. Behaviour is identical. https://claude.ai/code/session_01P58fvbxUgRZn9eqbgDL4uV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SimModel was doing too much — mix of state management, math, and physics.
computeTrackKinematics+finiteDifferenceintoKinematicsComputer.ts(pure functions, no Axon/SceneryStack deps)buildModelViewTransformintoModelViewTransformFactory.ts(pure function, no Axon/SceneryStack deps)
Matrix3,MIN_CALIB_DISTANCE,MIN_PIXEL_DISTANCE, andKinematicPointfrom its own import list
SimModel retains all Properties and wiring — only the math implementations
move. Net result: SimModel shrinks from 475 → 341 lines; each extracted
module is independently readable and testable.
https://claude.ai/code/session_01P58fvbxUgRZn9eqbgDL4uV