From df4d76384c4715801ac4f99a6a538d0fa85b4f9c Mon Sep 17 00:00:00 2001 From: German Khokhlov Date: Wed, 18 Nov 2026 15:07:06 +0400 Subject: [PATCH] Tidy the Rail and Transit ride fixes after review - A train first seen standing takes its direction from the operator's course, not from a course drawn out of GPS jitter. - Moving a hold to another vehicle releases the first one the same way as ending the hold. - repaint always goes through placeSample, which hides the held vehicle and puts it on its current segment, so setShown is no longer exported. - Tests cover the icon right after a hold, a segment with one known floor, a known floor at 0 m and selection by click after the layer comes back; shared helpers replace repeated setup. - Comments, CURRENT-STATE and the release note say what the code does without internal figures. --- changelog.d/1786-cockpit-ride-transit-rail.md | 11 +- docs/CURRENT-STATE.md | 20 +-- src/layers/rail/cockpitSubject.js | 17 +- src/layers/rail/cockpitSubject.test.mjs | 33 ++++ src/layers/rail/pathBinding.test.mjs | 37 ++--- src/layers/transit/cockpitSubject.js | 34 ++-- src/layers/transit/lifecycle.test.mjs | 146 +++++++++++++++--- src/layers/transit/rendering.js | 1 - 8 files changed, 209 insertions(+), 90 deletions(-) diff --git a/changelog.d/1786-cockpit-ride-transit-rail.md b/changelog.d/1786-cockpit-ride-transit-rail.md index 4e29e30d74..995e0b491e 100644 --- a/changelog.d/1786-cockpit-ride-transit-rail.md +++ b/changelog.d/1786-cockpit-ride-transit-rail.md @@ -1,8 +1,5 @@ -- Fix rides on a Rail train or Transit vehicle for an application's Cockpit - subjects. A held parked vehicle no longer leaves its icon drawn under the - view, and turning Transit off ends a hold, so the vehicle shows again when - the layer is back on. A train or vehicle drawn on the ellipsoid while no - floor is known reports no height, so the view waits for the ground instead - of standing on the ellipsoid. A train standing at a platform keeps the way - it runs: GPS jitter of a few metres no longer turns the ride around +- Fix application Cockpit rides on Rail trains and Transit vehicles: a held + parked vehicle no longer leaves its icon under the view, turning Transit off + ends a hold, a view waits for the ground instead of standing on the + ellipsoid, and GPS jitter at a platform no longer turns a ride around (German, @gekh, #1786). diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index 95b73b39ec..d83e762add 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -2769,7 +2769,7 @@ HUD view is. The Rail layer offers a read API for such a view: `getSelectedTrain()` and `getCockpitSubject()` (a snapshot of the train: where it is along its line, which way it runs (`pathBinding.js` learns it), its rail height (null while -no floor is known and it is drawn on the ellipsoid), speed as reported or +no floor is known), speed as reported or derived, position source, feed credit and stops, and `selectedAtMs`, when the user last selected it by a click or `focusTrain`, even again; null after a deselection and for a train only a hold selected), @@ -2789,15 +2789,17 @@ view. Turning the layer off ends the hold. Its snapshot's `position.groundHeightM` is the floor where playback draws it (the drawn marker's height while it is placed, else its segment's two reports' floors at the playback fraction), not the newest report's, and null while that floor is -unknown, also far above the ground, where such a vehicle is drawn on the -ellipsoid. Rail's `path.direction` is the way the drawn train runs: once -playback has placed it, a report that runs back turns it only when playback -reaches that report. A train turns once it is 15 m back from the farthest point -it reached, however slowly it backs, so a standing train's GPS jitter keeps the -direction it had. `docs/APPLICATION.md` (Cockpit subjects) shows a minimal provider. `src/ui/cockpitSubjectView.test.mjs`, +unknown, also while the camera is too high for the layer to sample the ground +and such a vehicle is drawn on the ellipsoid. Rail's `path.direction` is the +way the drawn train runs: once playback has placed it, a report that runs back +turns it only when playback starts the segment to that report. A train turns +once it has backed away from the farthest point it reached by more than a +standing train's GPS jitter, however slowly it backs, so that jitter keeps the +direction it had. `docs/APPLICATION.md` (Cockpit subjects) shows a minimal +provider. `src/ui/cockpitSubjectView.test.mjs`, `src/cockpitSubjectProviders.test.mjs`, `src/cockpitProgress.test.mjs`, -`src/layers/rail/cockpitSubject.test.mjs` and the Transit lifecycle tests -cover them. +`src/layers/rail/cockpitSubject.test.mjs`, `src/layers/rail/pathBinding.test.mjs` +and the Transit lifecycle tests cover them. ## Context coordination diff --git a/src/layers/rail/cockpitSubject.js b/src/layers/rail/cockpitSubject.js index 286160d3aa..1d9c02cb39 100644 --- a/src/layers/rail/cockpitSubject.js +++ b/src/layers/rail/cockpitSubject.js @@ -173,14 +173,16 @@ export function createRailCockpitSubject({ /** * Which way the train runs along its shape: as its binding learnt it from - * successive reports, else from its drawn course against the line's - * bearing there; 0 while neither tells. + * successive reports, else from its reported bearing against the line's + * bearing there; 0 while neither tells. Not the drawn course: until the + * binding tells, the train has not moved past GPS jitter, and a course + * drawn between such reports is that jitter. */ function directionOf(entry) { const binding = entry.path; if (binding.direction === 1 || binding.direction === -1) return binding.direction; - const course = finiteOrNull(entry.courseDeg); + const course = finiteOrNull(entry.record?.bearing); const point = pointAtS(binding.path, binding.drawS, scratch); if (course === null || !Number.isFinite(point?.bearingDeg)) return 0; const turn = Math.abs(((course - point.bearingDeg + 540) % 360) - 180); @@ -205,11 +207,12 @@ export function createRailCockpitSubject({ finiteOrNull(drawn?.lat) ?? finiteOrNull(sample?.lat) ?? record.lat; position.longitude = finiteOrNull(drawn?.lon) ?? finiteOrNull(sample?.lon) ?? record.lon; - // Quality 0 is the ellipsoid a train is drawn on while no floor is - // known: no height, so a view waits for the surface. + const quality = Number.isFinite(drawn?.q) ? drawn.q : 0; + // Quality 0: no floor known here, so no height; a view waits for the + // surface. position.railHeightM = - Number.isFinite(drawn?.h) && drawn.q > 0 ? drawn.h - liftM : null; - position.heightQuality = Number.isFinite(drawn?.q) ? drawn.q : 0; + quality > 0 && Number.isFinite(drawn.h) ? drawn.h - liftM : null; + position.heightQuality = quality; position.atMs = finiteOrNull(drawn?.at); let path = null; diff --git a/src/layers/rail/cockpitSubject.test.mjs b/src/layers/rail/cockpitSubject.test.mjs index c8c764aad3..08bf66d15c 100644 --- a/src/layers/rail/cockpitSubject.test.mjs +++ b/src/layers/rail/cockpitSubject.test.mjs @@ -151,6 +151,39 @@ test('a train drawn on an unknown floor reports no rail height', async (t) => { ); }); +test('a train on a known floor at sea level reports a rail height of 0 m', async (t) => { + const app = await onTheLine(t, { + ground: { ...WARM_GROUND, cachedGroundFloor: () => 0 }, + }); + app.parts().selection.selectTrain(T1); + const { position } = app.layer.getSelectedTrain(); + assert.ok(position.heightQuality > 0, 'the floor is known'); + assert.equal(position.railHeightM, 0, 'a height, not none'); +}); + +test('a train first seen standing takes no direction from its GPS jitter', async (t) => { + // Neither reports 13 m apart nor the course drawn between them say which way + // it runs; an operator's bearing does. + const app = harness(t, { paths: { 'mbta-rail': PACK }, at: BOSTON }); + let jitterM = 0; + app.feeds.set('mbta-rail', () => [ + train('T1', LAT, LON0 + (1000 + jitterM) / KX), + train('T2', LAT, LON0 + (3000 + jitterM) / KX, { bearing: 270 }), + ]); + await app.enable(); + const seen = []; + for (const next of [13, 0, 13, 0, 13, 0]) { + jitterM = next; + app.run(RAIL_POLL_MS); + await app.poll(); + app.parts().selection.selectTrain(T1); + const standing = app.layer.getSelectedTrain().path.direction; + app.parts().selection.selectTrain(T2); + seen.push([standing, app.layer.getSelectedTrain().path.direction]); + } + assert.deepEqual(seen, Array(6).fill([0, -1])); +}); + test('a snapshot read again while nothing changed is the one read before', async (t) => { let list = stopList('Alpha', 'Bravo', 'Charlie'); const app = await onTheLine(t, { diff --git a/src/layers/rail/pathBinding.test.mjs b/src/layers/rail/pathBinding.test.mjs index 2e01da82c9..6dcc9fab99 100644 --- a/src/layers/rail/pathBinding.test.mjs +++ b/src/layers/rail/pathBinding.test.mjs @@ -209,8 +209,8 @@ test('a binding learns which way its train runs, and keeps it through a stop', ( assert.equal(along.direction, 0, 'one report tells no direction'); assert.ok(followFix(along, fix(10_000, 700, 0))); assert.equal(along.direction, 1); - // Standing at a stop, its reports jitter back a few metres: it keeps what - // it learnt. + // Standing at a stop, its reports jitter back 10 m: it keeps what it + // learnt. assert.ok(followFix(along, fix(20_000, 690, 0))); assert.equal(along.direction, 1); const against = bindTrainPath(p, ids, fix(0, 1500, 0)); @@ -269,33 +269,22 @@ test('jitter segments played back at a standing train keep the direction it lear const ids = { tripId: 'full', routeId: 'main' }; const binding = bindTrainPath(p, ids, fix(0, 1000, 0)); assert.ok(followFix(binding, fix(15_000, 1600, 0))); - // Playback runs the train east, 1000 m -> 1600 m: direction +1. - const out = {}; let seq = 0; const play = (fromM, toM, t0) => { - const segment = { - from: { seq, ...fix(t0, fromM, 0) }, - to: { seq: seq + 1, ...fix(t0 + 15_000, toM, 0) }, - }; - assert.ok( - placeOnPath( - binding, - { fromSeq: seq, toSeq: seq + 1, fraction: 0.5 }, - segment, - out, - ), + const { sample, segment } = playback( + seq, + seq + 1, + fix(t0, fromM, 0), + fix(t0 + 15_000, toM, 0), + 0.5, ); + assert.ok(placeOnPath(binding, sample, segment, {})); seq += 1; return binding.direction; }; - assert.equal(play(1000, 1600, 0), 1); - assert.equal( - binding.directionPlayed, - true, - 'playback owns the direction now', - ); - // At the platform: reports 3-5 m apart, back and forth, polled and played. - const learnt = []; + // It runs east, then stands at the platform: reports 3-5 m apart, back and + // forth, polled and played. + const learnt = [play(1000, 1600, 0)]; let t0 = 15_000; for (const [fromM, toM] of [ [1600, 1597], @@ -307,7 +296,7 @@ test('jitter segments played back at a standing train keep the direction it lear learnt.push(play(fromM, toM, t0)); t0 += 15_000; } - assert.deepEqual(learnt, [1, 1, 1, 1], 'from the played-back segments'); + assert.deepEqual(learnt, [1, 1, 1, 1, 1], 'from the played-back segments'); }); test('GPS jitter in the newest reports before playback keeps the direction', () => { diff --git a/src/layers/transit/cockpitSubject.js b/src/layers/transit/cockpitSubject.js index e135835a95..ff31f0722f 100644 --- a/src/layers/transit/cockpitSubject.js +++ b/src/layers/transit/cockpitSubject.js @@ -84,9 +84,10 @@ export function createTransitCockpitSubject({ state, parts, services }) { * or held), which near the ground follows the terrain between reports; * else its segment's two reports' floors at the playback fraction. Null * while either floor is unknown, as on cold terrain, so a view waits for - * the surface: far above the ground such a marker is drawn on the - * ellipsoid, which is no floor. Not `entry.heightM`, the floor at the - * newest report, which playback has not reached yet. + * the surface: with the camera above HEIGHT_SAMPLE_MAX_ALTITUDE_M such a + * marker is drawn on the ellipsoid, which is no floor. Not + * `entry.heightM`, the floor at the newest report, which playback has not + * reached yet. */ function drawnFloorOf(entry) { const segment = entry.segment; @@ -175,21 +176,26 @@ export function createTransitCockpitSubject({ state, parts, services }) { } /** - * Show or hide a vehicle's sprite and trail for the hold. The sprite goes - * through the renderer (setShown, flushMarkers): its set draws a marker - * only as last pushed. + * Place a vehicle and show or hide its sprite and trail for the hold now: + * the icon set draws a marker as it was last pushed, not as it is. */ function repaint(entry) { if (!entry?.marker) return; - if (entry.key === state._cockpitSubjectKey) - parts.rendering.setShown(entry, false); - else parts.rendering.placeSample(entry); + parts.rendering.placeSample(entry); parts.rendering.flushMarkers(); parts.rendering.schedulePlayback(entry); parts.trails.update(); state._detectRevision += 1; } + /** A vehicle let go of is out of play until a sweep finds it in view. */ + function release(entry) { + if (!entry) return; + state._visible.delete(entry); + parts.rendering.requestVisibility(); + repaint(entry); + } + const methods = { /** The selected vehicle (see createTransitCockpitSubject), or null. */ getSelectedVehicle() { @@ -209,13 +215,7 @@ export function createTransitCockpitSubject({ state, parts, services }) { const previous = state._vehicles.get(state._cockpitSubjectKey); if (key === null || key === undefined) { state._cockpitSubjectKey = null; - // The hold kept it in play out of view: it leaves until the next - // visibility sweep finds it in view again. - if (previous) { - state._visible.delete(previous); - parts.rendering.requestVisibility(); - } - repaint(previous); + release(previous); parts.selection.refreshSelectedCard(true); return true; } @@ -224,7 +224,7 @@ export function createTransitCockpitSubject({ state, parts, services }) { if (state._selectedKey !== key) parts.selection.selectVehicle(key); state._cockpitSubjectKey = key; state._visible.add(entry); - if (previous && previous !== entry) repaint(previous); + if (previous !== entry) release(previous); repaint(entry); parts.selection.refreshSelectedCard(true); return true; diff --git a/src/layers/transit/lifecycle.test.mjs b/src/layers/transit/lifecycle.test.mjs index 3fcfa3f9ea..4c2e217073 100644 --- a/src/layers/transit/lifecycle.test.mjs +++ b/src/layers/transit/lifecycle.test.mjs @@ -957,45 +957,36 @@ test('disable aborts in-flight work and a response that lands late is ignored', assert.equal(app.vehicles().length, 1); }); -test('a held vehicle with no floor yet tells its view no height', async (t) => { - // Not the ellipsoid: a first-person view waits for the surface instead. - const app = harness(t, { floorAt: () => undefined }); +/** A bus at `place.lat`, read on each poll, polled once and settled; its entry. */ +async function oneBus(app, place = { lat: 42.36 }) { app.serve('mbta', () => ({ status: 200, body: snapshot( 'mbta', 'MBTA', - [vehicle('bus-1', 42.36, -71.06, Date.now() / 1000)], + [vehicle('bus-1', place.lat, -71.06, reported())], { fetchedAt: Date.now() }, ), })); app.layer.enable(app.viewer); await app.layer.update(); app.settle(); - const entry = app.vehicles()[0]; + return app.vehicles()[0]; +} + +test('a held vehicle with no floor yet tells its view no height', async (t) => { + // Not the ellipsoid: a first-person view waits for the surface instead. + const app = harness(t, { floorAt: () => undefined }); + const entry = await oneBus(app); assert.equal(entry.heightPending, true, 'it has nothing to stand on'); assert.ok(app.layer.setCockpitSubject(entry.key) !== false); assert.equal(app.layer.getCockpitSubject().position.groundHeightM, null); }); -test('far above the ground, a held vehicle drawn on the ellipsoid tells its view no height', async (t) => { - // Above the height gate a vehicle with no floor is drawn on the ellipsoid - // at once, not held back; the ellipsoid is still no floor to stand a view - // on. +test('with the camera above the height gate, a held vehicle drawn on the ellipsoid tells its view no height', async (t) => { + // There a vehicle with no floor is drawn on the ellipsoid, which is no floor. const app = harness(t, { altitude: 100_000, floorAt: () => undefined }); - app.serve('mbta', () => ({ - status: 200, - body: snapshot( - 'mbta', - 'MBTA', - [vehicle('bus-1', 42.36, -71.06, Date.now() / 1000)], - { fetchedAt: Date.now() }, - ), - })); - app.layer.enable(app.viewer); - await app.layer.update(); - app.settle(); - const [entry] = app.vehicles(); + const entry = await oneBus(app); assert.ok(app.layer.setCockpitSubject(entry.key) !== false); app.frame(); assert.equal(entry.surfaceReady, true, 'it is drawn'); @@ -1006,6 +997,28 @@ test('far above the ground, a held vehicle drawn on the ellipsoid tells its view assert.equal(app.layer.getCockpitSubject().position.groundHeightM, null); }); +test('with the camera above the height gate, a held vehicle with one floor unknown tells its view no height', async (t) => { + // Drawn between a floor its view could stand on and none. + for (const knownLat of [42.36, 42.361]) { + await t.test(`floor known at ${knownLat}`, async (t) => { + const app = harness(t, { altitude: 100_000, floorAt: () => undefined }); + app.ground._warm.set(app.ground._key(knownLat, -71.06), 12); + const start = Date.now(); + const place = { lat: 42.36 }; + const entry = await oneBus(app, place); + place.lat = 42.361; + app.run(TRANSIT_POLL_MS); + await app.layer.update(); + assert.equal(app.layer.setCockpitSubject(entry.key), true); + app.run(start - AGE_MS + LAG_MS + TRANSIT_POLL_MS / 2 - Date.now()); + const { from, to } = entry.segment; + assert.notEqual(Number.isFinite(from.h), Number.isFinite(to.h)); + assert.equal(entry.surfaceReady, true, 'it is drawn'); + assert.equal(app.layer.getCockpitSubject().position.groundHeightM, null); + }); + } +}); + test('a vehicle waits for its floor rather than standing on the ellipsoid', async (t) => { // The shared ground-floor system answers from the rendered mesh cell or the // real DEM cell; until one of them is warm there is no honest height to draw @@ -2146,7 +2159,8 @@ test('a segment climbs from the ground it left to the ground it reaches', async `its snapshot stands where it is drawn, got ${floor().toFixed(1)} m`, ); - // Halfway along the segment it is halfway up; at the end, on the high ground. + // Halfway along the segment it still follows the low ground it crosses; at + // the end, it is on the high ground. const firstFixAt = start - AGE_MS; app.run(firstFixAt + LAG_MS + TRANSIT_POLL_MS / 2 - Date.now()); assert.ok( @@ -4914,7 +4928,7 @@ test('a held vehicle hides, keeps moving out of view, and lets go when it goes', assert.equal(app.layer.getCockpitSubject(), null); }); -test('turning Transit off ends a hold; on again, Escape deselects and the vehicle shows', async (t) => { +test('turning Transit off ends a hold; on again, the vehicle shows and clicks and Escape select it', async (t) => { const app = harness(t); const key = await busRunningNorth(app); assert.equal(app.layer.setCockpitSubject(key), true); @@ -4931,10 +4945,12 @@ test('turning Transit off ends a hold; on again, Escape deselects and the vehicl app.run(TRANSIT_POLL_MS); await app.layer.update(); app.settle(); - assert.equal(app.layer.focusVehicle(key), true); const [entry] = app.vehicles(); app.frame(); assert.equal(entry.marker.show, true, 'the vehicle is drawn, not held'); + app.viewer.scene.pick = () => app.pickOf(entry); + app.input.click(new Cesium.Cartesian2(500, 400)); + assert.equal(app.state()._selectedKey, key, 'a click selects it'); globalThis.document.dispatchEvent( Object.assign(new Event('keydown'), { key: 'Escape' }), ); @@ -4968,6 +4984,7 @@ test('a held parked vehicle draws no icon; let go, it draws its icon again', asy app.markers('still').get(key) ?? app.markers('moving').get(key); assert.equal(icon()?.show, true, 'drawn before the hold'); assert.equal(app.layer.setCockpitSubject(key), true); + assert.equal(icon()?.show ?? false, false, 'hidden at once'); // The view rides it for a while: frames, a sweep, a poll. app.run(2_000, 16); app.settle(); @@ -4986,3 +5003,82 @@ test('a held parked vehicle draws no icon; let go, it draws its icon again', asy app.settle(); assert.equal(icon()?.show, true, 'let go: drawn again'); }); + +test('a hold moved to another vehicle lets the first go out of view', async (t) => { + const app = harness(t); + app.serve('mbta', () => ({ + status: 200, + body: snapshot( + 'mbta', + 'MBTA', + [ + vehicle('bus-1', 42.36, -71.06, reported()), + vehicle('bus-2', 42.37, -71.05, reported()), + ], + { fetchedAt: Date.now() }, + ), + })); + app.layer.enable(app.viewer); + await app.layer.update(); + app.settle(); + const bus = (id) => app.vehicles().find((entry) => entry.key === id); + const first = bus('mbta:bus-1'); + assert.equal(app.layer.setCockpitSubject(first.key), true); + // The view looks away from both, as a view from inside the first does. + app.lookAt({ west: -70.98, south: 42.3, east: -70.9, north: 42.42 }); + app.advance(250); + app.settle(); + assert.equal(app.state()._visible.has(bus('mbta:bus-2')), false); + assert.equal(app.layer.setCockpitSubject('mbta:bus-2'), true); + assert.equal(app.state()._visible.has(first), false, 'let go, out of play'); + assert.equal(first.marker.show, false, 'and not drawn out of view'); +}); + +test('holding a vehicle out of view tells its view at once the floor it stands on', async (t) => { + // Out of view a vehicle plays on but its marker is not moved: the hold + // places it, so its view reads neither the floor where it was last drawn + // nor nothing. + const app = harness(t, { floorAt: (lat) => (lat > 42.3625 ? 100 : 10) }); + for (const [lat, floorM] of [ + [42.361, 10], + [42.362, 10], + [42.363, 100], + [42.364, 100], + ]) + app.ground._warm.set(app.ground._key(lat, -71.06), floorM); + let lat = 42.36; + app.serve('mbta', () => ({ + status: 200, + body: snapshot( + 'mbta', + 'MBTA', + [vehicle('bus-1', lat, -71.06, reported())], + { + fetchedAt: Date.now(), + }, + ), + })); + app.layer.enable(app.viewer); + await app.layer.update(); + app.settle(); + const [entry] = app.vehicles(); + const drawnM = () => + Cesium.Cartographic.fromCartesian(entry.marker.position).height; + assert.ok(Math.abs(drawnM() - 11.5) < 1, 'drawn on the low ground'); + app.lookAt({ west: -70.98, south: 42.3, east: -70.9, north: 42.42 }); + app.advance(250); + app.settle(); + assert.equal(app.state()._visible.has(entry), false, 'out of view'); + // Unseen, it drives up the hill and stands there. + for (const next of [42.361, 42.362, 42.363, 42.364, 42.364, 42.364]) { + lat = next; + app.run(TRANSIT_POLL_MS); + await app.layer.update(); + } + app.run(LAG_MS); + app.settle(); + assert.ok(drawnM() < 30, 'its marker was left on the low ground'); + assert.equal(app.layer.setCockpitSubject(entry.key), true); + const floorM = app.layer.getCockpitSubject().position.groundHeightM; + assert.ok(Math.abs(floorM - 100) < 1.5, `up the hill, got ${floorM} m`); +}); diff --git a/src/layers/transit/rendering.js b/src/layers/transit/rendering.js index 8b0ecc1e8e..194d042a3e 100644 --- a/src/layers/transit/rendering.js +++ b/src/layers/transit/rendering.js @@ -1225,7 +1225,6 @@ export function createRendering({ state, services, parts }) { setNameOf, markerShown, markDirty, - setShown, flushMarkers, removeMarker, watchCamera,