refactor(layers): W1 timestamps without JulianDate

Flights and military keep their fix history in plain milliseconds
(timeMs, whole ms as the Date the JulianDate came from held) and read
"now" from the app clock: the render-delay time, bracketing,
extrapolation, the warm-up check and the trail seed are number
arithmetic (motion.js, tracking.js, snapshotRenderer.js), as is the turn
rate and the forward synthetic fix in data/motionModel.js, which no
longer imports Cesium. The JulianDate scratch fields in state.js and the
unused JulianDate.now() in queries.js go. Launches (mission frame, panel,
selection), the tracked satellite's SGP4 epoch and the celestial ring's
sun and moon read the app clock; where a Cesium property still takes a
JulianDate it is built from the app clock's now (as JulianDate.now did
from Date.now). Wall-clock throttles and feed receipt stamps stay on
Date.now; property reads at viewer.clock.currentTime (render, frozen at
boot) move with the render code in the *a tasks.

Tests that built JulianDate history fixtures now build ms ones with the
same instants (motionModel.test, flights.test, militaryFlights.test); new
tests show the military delayed position and the tracked satellite follow
the app clock (both fail on the previous code).
This commit is contained in:
German Khokhlov committed 2026-10-16 15:01:46 +04:00
1 parent f1a2df5cfe
commit c33ee31efc
21 files changed
+155 -130

No files matched your search

+9 -30
View File
@@ -1,5 +1,5 @@
import * as Cesium from 'cesium';
import * as geo from '../../geo/index.js';
import { appClock } from '../../time/clock.js';
import {
COURSE_HOLD_SPEED_MPS,
lerpAngleDeg,
@@ -150,25 +150,15 @@ export function createMotion({
const out = result || geo.cartesian3.create();
// Render one poll interval behind real time so we interpolate between
// two KNOWN fixes whenever possible (see RENDER_DELAY_SEC rationale).
const renderTime = Cesium.JulianDate.addSeconds(
Cesium.JulianDate.now(),
-RENDER_DELAY_SEC,
flightState._scratchRenderTime,
);
const renderTimeMs = appClock.now() - RENDER_DELAY_SEC * 1000;
// Bracketing pair: interpolate — no extrapolation error, no snap-back.
for (let i = history.length - 1; i >= 1; i--) {
const a = history[i - 1];
const b = history[i];
if (
Cesium.JulianDate.lessThanOrEquals(a.time, renderTime) &&
Cesium.JulianDate.lessThanOrEquals(renderTime, b.time)
) {
const span = Cesium.JulianDate.secondsDifference(b.time, a.time);
const t =
span > 0
? Cesium.JulianDate.secondsDifference(renderTime, a.time) / span
: 1.0;
if (a.timeMs <= renderTimeMs && renderTimeMs <= b.timeMs) {
const span = (b.timeMs - a.timeMs) / 1000;
const t = span > 0 ? (renderTimeMs - a.timeMs) / 1000 / span : 1.0;
// Course of the DISPLAYED motion. The chord is only trustworthy when the
// segment covers real ground (at hover its direction is GPS jitter; on a
// slow tight turn it STEPS the whole per-segment turn at each boundary),
@@ -199,10 +189,7 @@ export function createMotion({
}
const newest = history[history.length - 1];
const elapsedSec = Cesium.JulianDate.secondsDifference(
renderTime,
newest.time,
);
const elapsedSec = (renderTimeMs - newest.timeMs) / 1000;
if (elapsedSec <= 0) {
// Warm-up: renderTime predates ALL history (freshly seen / just-started-tracking
// aircraft, before RENDER_DELAY_SEC of history has accumulated, so no bracketing
@@ -213,10 +200,7 @@ export function createMotion({
// the oldest fix froze the icon; extrapolating the NEWEST fix to wall-clock now made
// the icon jump back ~one poll interval the instant interpolation took over.)
const oldest = history[0];
const lookbackSec = Cesium.JulianDate.secondsDifference(
oldest.time,
renderTime,
); // ≥ 0
const lookbackSec = (oldest.timeMs - renderTimeMs) / 1000; // ≥ 0
return _extrapolateFix(
oldest,
info,
@@ -236,7 +220,7 @@ export function createMotion({
// a Date per aircraft on every 12 Hz fleet tick.
fixEpochMs: Number.isFinite(newest.epochMs)
? newest.epochMs
: Cesium.JulianDate.toDate(newest.time).getTime(),
: newest.timeMs,
lastContactEpochMs: info?.lastContactEpochMs,
// Permit one minute of contact grace but cap any cached-feed drift at
// five minutes. Source backoff is exposed separately as a STALE cue.
@@ -307,12 +291,7 @@ export function createMotion({
if (!flightState._trackedIcao) return false;
const history = flightState._positionHistory.get(flightState._trackedIcao);
if (!history || history.length === 0) return true;
const renderTime = Cesium.JulianDate.addSeconds(
Cesium.JulianDate.now(),
-RENDER_DELAY_SEC,
flightState._scratchWarmupTime,
);
return Cesium.JulianDate.lessThan(renderTime, history[0].time);
return appClock.now() - RENDER_DELAY_SEC * 1000 < history[0].timeMs;
}
/** Resolve the selected aircraft's actual rendered square extent this frame. */
-2
View File
@@ -1,6 +1,5 @@
import { approxDistanceKm as _approxDistanceKm } from './recordPolicy.js';
import { displayedKinematics } from '../../data/motionModel.js';
import * as Cesium from 'cesium';
import * as geo from '../../geo/index.js';
import { isExplicitLayerStateOrigin } from '../../data/layerState.js';
import { modelVisualAnchor } from '../../data/modelVisualAnchor.js';
@@ -550,7 +549,6 @@ export function createQueries({
const maxRange =
Number.isFinite(range) && range > 0 ? range : Number.POSITIVE_INFINITY;
const now = Cesium.JulianDate.now();
const nearby = [];
for (const [icao24, bb] of flightState._billboards) {
+4 -3
View File
@@ -97,18 +97,19 @@ export function createFlightSnapshotRenderer({
// 5-15s stale and receipt-time stamping is what caused the
// back/forward oscillation. Only append when the fix actually
// advances, so stale repeats don't create zero-dt segments.
const fixTime = Cesium.JulianDate.fromDate(new Date(fixEpochMs));
// Milliseconds since the epoch, whole as a Date holds them.
const fixTimeMs = Math.trunc(fixEpochMs);
if (!flightState._positionHistory.has(icao24)) {
flightState._positionHistory.set(icao24, []);
}
const history = flightState._positionHistory.get(icao24);
const newest = history[history.length - 1];
if (!newest || Cesium.JulianDate.greaterThan(fixTime, newest.time)) {
if (!newest || fixTimeMs > newest.timeMs) {
// Per-fix kinematics: the fix's own velocity/track ride along so the
// extrapolation paths use the values that BELONG to the fix they
// extend, not whatever the latest poll reported.
history.push({
time: fixTime,
timeMs: fixTimeMs,
epochMs: fixEpochMs,
position: geo.cartesian3.clone(position),
velocity: meta.velocity,
+1 -5
View File
@@ -117,7 +117,7 @@ export function createFlightState({ source, services }) {
flightState._focusEvidenceIds = new Set();
/** @type {Map<string, Array<{time:Cesium.JulianDate, position:Cesium.Cartesian3}>>} */
/** @type {Map<string, Array<{timeMs: number, epochMs: number, position: Cesium.Cartesian3}>>} timeMs: the fix time in whole ms since the epoch (the app clock's unit) */
flightState._positionHistory = new Map();
@@ -247,16 +247,12 @@ export function createFlightState({ source, services }) {
flightState._scratchArc = { east: 0, north: 0, endCourseDeg: 0 };
flightState._scratchRenderTime = new Cesium.JulianDate();
flightState._scratchFleetPos = geo.cartesian3.create();
flightState._scratchSonarScreen = geo.cartesian2.create();
flightState._scratchDrRaw = geo.cartesian3.create();
flightState._scratchWarmupTime = new Cesium.JulianDate();
flightState._trackedPosHolder = geo.cartesian3.create();
// ---------------------------------------------------------------------------
+4 -7
View File
@@ -1,4 +1,5 @@
import * as Cesium from 'cesium';
import { appClock } from '../../time/clock.js';
import { nextCockpitNearContacts } from '../../data/cockpitAirLod.js';
import { trackedModelZoomActive } from '../../data/trackedModelRegime.js';
import {
@@ -378,13 +379,9 @@ export function createTracking({
// newest ~RENDER_DELAY_SEC of fixes are AHEAD of the displayed icon; including them
// would draw the trail in front of the plane. They join the trail via _appendTrailFix
// as they age past the delay.
const seedRenderTime = Cesium.JulianDate.addSeconds(
Cesium.JulianDate.now(),
-RENDER_DELAY_SEC,
flightState._scratchWarmupTime,
);
const seedRenderTimeMs = appClock.now() - RENDER_DELAY_SEC * 1000;
for (const fix of history) {
if (Cesium.JulianDate.lessThanOrEquals(fix.time, seedRenderTime)) {
if (fix.timeMs <= seedRenderTimeMs) {
flightState._trailPositions.push(Cesium.Cartesian3.clone(fix.position));
}
}
@@ -458,7 +455,7 @@ export function createTracking({
_refreshTrailDisplay();
const oldestFixEpochSec = history.length
? Cesium.JulianDate.toDate(history[0].time).getTime() / 1000
? history[0].timeMs / 1000
: Infinity;
_backfillTrail(icao24, flightState._trailBackfillToken, oldestFixEpochSec);
}