diff --git a/src/data/aircraftMotionRegime.js b/src/data/aircraftMotionRegime.js index 8a254f37c..9c1d09b7f 100644 --- a/src/data/aircraftMotionRegime.js +++ b/src/data/aircraftMotionRegime.js @@ -270,3 +270,50 @@ export function staleCoastSeconds(newest, info, cachedGroundFloor) { : 0; return Math.max(0, Math.min(limitSec, STALE_COAST_MAX_SEC - alreadySec)); } + +/** A raw position this far (m) off its own path, plus half its step, is a jump. */ +const RAW_JUMP_SLACK_M = 5; + +/** + * Whether a tracked aircraft's raw position jumped this frame: a new report + * moved its path. Judged against the path itself, where its last frame's + * step carries it, not against a reported speed (which can be stale or 0 + * while the drawn motion, minutes behind the newest report, is fast). A + * frame with no step to go by (the first on a track, or the one after a + * jump) is never a jump. + * @param {{x: number, y: number, z: number}} raw This frame's raw position. + * @param {{x: number, y: number, z: number}} prevRaw Last frame's. + * @param {{x: number, y: number, z: number}} prevRawStep Last frame's step. + * @param {number} prevRawStepSec Last frame's length, s (0: no step). + * @param {number} dtSec This frame's length, s. + * @returns {boolean} + */ +export function rawPositionJumped( + raw, + prevRaw, + prevRawStep, + prevRawStepSec, + dtSec, +) { + if (!(prevRawStepSec > 0)) return false; + const k = dtSec / prevRawStepSec; + const dx = raw.x - (prevRaw.x + prevRawStep.x * k); + const dy = raw.y - (prevRaw.y + prevRawStep.y * k); + const dz = raw.z - (prevRaw.z + prevRawStep.z * k); + const stepM = + Math.hypot(prevRawStep.x, prevRawStep.y, prevRawStep.z) * Math.abs(k); + return Math.hypot(dx, dy, dz) > RAW_JUMP_SLACK_M + 0.5 * stepM; +} + +/** + * How fast the drawn path moves (m/s), from its last raw step; `fallback` + * (a reported speed) with no step to go by. + * @param {{x: number, y: number, z: number}} step + * @param {number} stepSec + * @param {number} fallback + * @returns {number} + */ +export function rawStepSpeedMps(step, stepSec, fallback) { + if (!(stepSec > 0)) return fallback; + return Math.hypot(step.x, step.y, step.z) / stepSec; +} diff --git a/src/layers/flights/motion.js b/src/layers/flights/motion.js index 0b23f3bcb..4054ad792 100644 --- a/src/layers/flights/motion.js +++ b/src/layers/flights/motion.js @@ -78,6 +78,8 @@ import { fixSpeedMps, fixTrackDeg, staleCoastSeconds, + rawPositionJumped, + rawStepSpeedMps, } from '../../data/aircraftMotionRegime.js'; /** Whether two ECEF points are the same point, component for component. */ @@ -453,12 +455,18 @@ export function createMotion({ (worldMs - flightState._drPrevWorldMs) / 1000, ); let snapped = false; + let jumpedThisFrame = false; if (sameTrack) { const speed = (info && info.velocity) || 0; - // Plausible single-frame motion (m): its speed × frame Δt, half again - // for slack, plus 5 m. More is a jump (a new report put it elsewhere). - const plausible = speed * dtSec * 1.5 + 5; - if (distance(raw, flightState._drPrevRaw) > plausible) { + // A jump: a new report moved the path the raw position is on. + const jumped = rawPositionJumped( + raw, + flightState._drPrevRaw, + flightState._drPrevRawStep, + flightState._drPrevRawStepSec, + dtSec, + ); + if (jumped) { // Discontinuity — re-anchor so the DISPLAYED position stays continuous, // then ease onto the new path no faster than a little over its speed; // farther off than RECOVER_SNAP_M it is a break, and it jumps. @@ -476,12 +484,22 @@ export function createMotion({ ) : flightState._drPrevDisplay; const errorM = distance(anchor, raw); + jumpedThisFrame = true; snapped = errorM >= RECOVER_SNAP_M; copyInto(snapped ? vec() : sub(anchor, raw), flightState._drCorrection); flightState._drCorrectionStartMs = nowMs; flightState._drCorrectionMs = Math.max( DR_CORRECTION_MS, - recoveryMs(errorM, speed), + // As fast as the path it was drawn on moves: the newest report's + // speed can be stale or 0 minutes ahead of it. + recoveryMs( + errorM, + rawStepSpeedMps( + flightState._drPrevRawStep, + flightState._drPrevRawStepSec, + speed, + ), + ), ); } } else { @@ -523,6 +541,12 @@ export function createMotion({ ); flightState._drPrevStepSec = dtSec; } else flightState._drPrevStepSec = 0; + // The raw step, to judge the next frame by; none after a jump (its + // step is the jump) or a track switch. + if (sameTrack && !jumpedThisFrame) { + copyInto(sub(raw, flightState._drPrevRaw), flightState._drPrevRawStep); + flightState._drPrevRawStepSec = dtSec; + } else flightState._drPrevRawStepSec = 0; copyInto(raw, flightState._drPrevRaw); copyInto(display, flightState._drPrevDisplay); flightState._drPrevMs = nowMs; diff --git a/src/layers/flights/state.js b/src/layers/flights/state.js index 9fdc6280a..f054648d8 100644 --- a/src/layers/flights/state.js +++ b/src/layers/flights/state.js @@ -340,6 +340,9 @@ export function createFlightState({ source, services }) { /** How the tracked icon moved over the last frame, and that frame's length (s; 0 for none). */ flightState._drPrevStep = { x: 0, y: 0, z: 0 }; flightState._drPrevStepSec = 0; + /** The raw position's last step, and that frame's length (s; 0 for none). */ + flightState._drPrevRawStep = { x: 0, y: 0, z: 0 }; + flightState._drPrevRawStepSec = 0; flightState._drPrevMs = 0; diff --git a/src/layers/military/motion.js b/src/layers/military/motion.js index bdc0233cf..499b4c247 100644 --- a/src/layers/military/motion.js +++ b/src/layers/military/motion.js @@ -61,6 +61,8 @@ import { fixSpeedMps, fixTrackDeg, staleCoastSeconds, + rawPositionJumped, + rawStepSpeedMps, } from '../../data/aircraftMotionRegime.js'; /** Where a military record carries its reported speed and track. */ @@ -355,12 +357,18 @@ export function createMotion({ (worldMs - flightState._drPrevWorldMs) / 1000, ); let snapped = false; + let jumpedThisFrame = false; if (sameTrack) { const speed = (info && info.speedMps) || 0; - // Plausible single-frame motion (m): its speed × frame Δt, half again - // for slack, plus 5 m. More is a jump (a new report put it elsewhere). - const plausible = speed * dtSec * 1.5 + 5; - if (distance(raw, flightState._drPrevRaw) > plausible) { + // A jump: a new report moved the path the raw position is on. + const jumped = rawPositionJumped( + raw, + flightState._drPrevRaw, + flightState._drPrevRawStep, + flightState._drPrevRawStepSec, + dtSec, + ); + if (jumped) { // Discontinuity — re-anchor so the DISPLAYED position stays continuous, // then ease onto the new path no faster than a little over its speed; // farther off than RECOVER_SNAP_M it is a break, and it jumps. @@ -378,12 +386,22 @@ export function createMotion({ ) : flightState._drPrevDisplay; const errorM = distance(anchor, raw); + jumpedThisFrame = true; snapped = errorM >= RECOVER_SNAP_M; copyInto(snapped ? vec() : sub(anchor, raw), flightState._drCorrection); flightState._drCorrectionStartMs = nowMs; flightState._drCorrectionMs = Math.max( DR_CORRECTION_MS, - recoveryMs(errorM, speed), + // As fast as the path it was drawn on moves: the newest report's + // speed can be stale or 0 minutes ahead of it. + recoveryMs( + errorM, + rawStepSpeedMps( + flightState._drPrevRawStep, + flightState._drPrevRawStepSec, + speed, + ), + ), ); } } else { @@ -408,6 +426,12 @@ export function createMotion({ ); flightState._drPrevStepSec = dtSec; } else flightState._drPrevStepSec = 0; + // The raw step, to judge the next frame by; none after a jump (its + // step is the jump) or a track switch. + if (sameTrack && !jumpedThisFrame) { + copyInto(sub(raw, flightState._drPrevRaw), flightState._drPrevRawStep); + flightState._drPrevRawStepSec = dtSec; + } else flightState._drPrevRawStepSec = 0; copyInto(raw, flightState._drPrevRaw); copyInto(display, flightState._drPrevDisplay); flightState._drPrevMs = nowMs; diff --git a/src/layers/military/state.js b/src/layers/military/state.js index fd1fefcd7..7a14c30cd 100644 --- a/src/layers/military/state.js +++ b/src/layers/military/state.js @@ -272,6 +272,9 @@ export function createFlightState({ source, services }) { /** How the tracked icon moved over the last frame, and that frame's length (s; 0 for none). */ flightState._drPrevStep = { x: 0, y: 0, z: 0 }; flightState._drPrevStepSec = 0; + /** The raw position's last step, and that frame's length (s; 0 for none). */ + flightState._drPrevRawStep = { x: 0, y: 0, z: 0 }; + flightState._drPrevRawStepSec = 0; flightState._drPrevMs = 0; diff --git a/src/testSupport/aircraftFeedLatency.mjs b/src/testSupport/aircraftFeedLatency.mjs index e1836c130..2c6d44bf7 100644 --- a/src/testSupport/aircraftFeedLatency.mjs +++ b/src/testSupport/aircraftFeedLatency.mjs @@ -38,7 +38,11 @@ function sharpestTurnDeg(line) { return worst; } -function run(layer, pollOptions = {}, { drop = [] } = {}) { +function run( + layer, + pollOptions = {}, + { drop = [], reportedSpeedMps, frameMs } = {}, +) { const polls = slowFeedPolls({ startMs: T0, count: 30, @@ -53,6 +57,8 @@ function run(layer, pollOptions = {}, { drop = [] } = {}) { // From the seventh poll: the delay has settled and the trail has begun. measureFromMs: polls[6].receiptMs, contacts: CONTACTS, + reportedSpeedMps, + ...(frameMs ? { frameMs } : {}), }); } @@ -99,6 +105,15 @@ export function feedLatencyTests(layer, floorSec) { assertTrail(sim, 60); }); + test(`${layer}: reports that give a speed of 0 still draw the tracked aircraft along its path, with no jump or stall`, () => { + // The drawn motion is minutes behind the newest report, whose speed may + // be stale or 0: a jump is judged by the path, not by that speed. + // At 10 fps a frame moves 13 m, more than a speed of 0 allows. + const sim = run(layer, {}, { reportedSpeedMps: 0, frameMs: 100 }); + assertSmoothMotion(sim); + assert.ok(placementError(sim, sim.frames, 'tracked', 0) < 100); + }); + test(`${layer}: a fresh fallback feed for four polls and reports that do not move on: no jump, no stall, no kink`, () => { const sim = run(layer, { fallback: [12, 13, 14, 15], diff --git a/src/testSupport/aircraftFeedSim.mjs b/src/testSupport/aircraftFeedSim.mjs index 1ccbf75d4..c6e9908e7 100644 --- a/src/testSupport/aircraftFeedSim.mjs +++ b/src/testSupport/aircraftFeedSim.mjs @@ -227,6 +227,7 @@ export function runAircraftFeedSim({ contacts, followsClock = false, onFrame = null, + reportedSpeedMps = null, }) { const mod = layer === 'flights' ? flights : military; const truth = arrivalPath(); @@ -336,7 +337,9 @@ export function runAircraftFeedSim({ baroAltitudeM: p.heightM, ellipsoidAltitudeM: p.heightM, onGround: false, - speedMps: p.speedMps, + // A feed may report a speed that is not how fast the contact flies + // (stale, or 0 when it gave none). + speedMps: reportedSpeedMps ?? p.speedMps, courseDeg: p.trackDeg, verticalRateMps: -1.5, positionTimeMs: timeMs,