mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-18 01:02:23 +00:00
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:
1 parent
f1a2df5cfe
commit
c33ee31efc
21 files changed
+155
-130
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import * as geo from '../../geo/index.js';
|
||||
import { appClock } from '../../time/clock.js';
|
||||
import {
|
||||
lerpAngleDeg,
|
||||
speedRamp,
|
||||
@@ -67,25 +67,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),
|
||||
@@ -115,10 +105,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
|
||||
@@ -128,10 +115,7 @@ export function createMotion({
|
||||
// the bracketing interpolation above with NO freeze and NO backward snap. (Holding
|
||||
// the oldest fix froze the icon until enough history accrued, then jumped.)
|
||||
const oldest = history[0];
|
||||
const lookbackSec = Cesium.JulianDate.secondsDifference(
|
||||
oldest.time,
|
||||
renderTime,
|
||||
); // ≥ 0
|
||||
const lookbackSec = (oldest.timeMs - renderTimeMs) / 1000; // ≥ 0
|
||||
return _extrapolateFix(
|
||||
oldest,
|
||||
info,
|
||||
@@ -148,7 +132,7 @@ export function createMotion({
|
||||
// Date for every aircraft twelve times per second.
|
||||
fixEpochMs: Number.isFinite(newest.epochMs)
|
||||
? newest.epochMs
|
||||
: Cesium.JulianDate.toDate(newest.time).getTime(),
|
||||
: newest.timeMs,
|
||||
lastContactEpochMs: info?.lastContactEpochMs,
|
||||
});
|
||||
return _extrapolateFix(
|
||||
@@ -213,12 +197,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. */
|
||||
|
||||
@@ -114,11 +114,9 @@ test('a normalized source can retain its stale reason without changing standalon
|
||||
assert.equal(layer.getStats().stale, true);
|
||||
});
|
||||
|
||||
test('a newly seen military contact appears at its delayed position, not ahead of it', async () => {
|
||||
// The military fleet renders RENDER_DELAY_SEC behind real time, so a fresh
|
||||
// contact's first displayed position is its fix projected back to that
|
||||
// delayed time. Creating the billboard at the raw fix drew it ahead until
|
||||
// the next fleet tick jumped it back.
|
||||
// A fresh contact's first billboard, with the app clock `appClockLagMs`
|
||||
// behind the wall clock (0: the app's default, the wall clock itself).
|
||||
async function firstPlacement(appClockLagMs) {
|
||||
const Cesium = await import('cesium');
|
||||
const { RENDER_DELAY_SEC } = await import('./policy.js');
|
||||
// The application's own scene services, as src/app/layers/militaryFlights.js
|
||||
@@ -146,6 +144,8 @@ test('a newly seen military contact appears at its delayed position, not ahead o
|
||||
const speedMps = 200;
|
||||
const fixAgeMs = 2_000;
|
||||
const nowMs = Date.now();
|
||||
const { appClock } = await import('../../time/clock.js');
|
||||
if (appClockLagMs) appClock.freeze(nowMs - appClockLagMs);
|
||||
const layer = createMilitaryFlightLayer({
|
||||
services: supplied,
|
||||
source: {
|
||||
@@ -193,7 +193,11 @@ test('a newly seen military contact appears at its delayed position, not ahead o
|
||||
viewer: { camera: { positionCartographic: null }, scene: {} },
|
||||
tracked: false,
|
||||
});
|
||||
await layer.update({ camera: { positionCartographic: null }, scene: {} });
|
||||
try {
|
||||
await layer.update({ camera: { positionCartographic: null }, scene: {} });
|
||||
} finally {
|
||||
appClock.reset();
|
||||
}
|
||||
const billboard = added.find((entry) => entry.id === 'ae0001');
|
||||
assert.ok(billboard, 'the new contact gets a billboard');
|
||||
const shown = Cesium.Cartographic.fromCartesian(billboard.position);
|
||||
@@ -201,11 +205,37 @@ test('a newly seen military contact appears at its delayed position, not ahead o
|
||||
Cesium.Cartesian3.fromRadians(shown.longitude, shown.latitude, 0),
|
||||
Cesium.Cartesian3.fromDegrees(fixLon, fixLat, 0),
|
||||
);
|
||||
return {
|
||||
behindM,
|
||||
westOfFix: Cesium.Math.toDegrees(shown.longitude) < fixLon,
|
||||
speedMps,
|
||||
fixAgeMs,
|
||||
RENDER_DELAY_SEC,
|
||||
};
|
||||
}
|
||||
|
||||
test('a newly seen military contact appears at its delayed position, not ahead of it', async () => {
|
||||
// The military fleet renders RENDER_DELAY_SEC behind real time, so a fresh
|
||||
// contact's first displayed position is its fix projected back to that
|
||||
// delayed time. Creating the billboard at the raw fix drew it ahead until
|
||||
// the next fleet tick jumped it back.
|
||||
const { behindM, westOfFix, speedMps, fixAgeMs, RENDER_DELAY_SEC } =
|
||||
await firstPlacement(0);
|
||||
const expectedM = speedMps * (RENDER_DELAY_SEC - fixAgeMs / 1000);
|
||||
assert.ok(westOfFix, 'drawn behind the fix');
|
||||
assert.ok(
|
||||
Cesium.Math.toDegrees(shown.longitude) < fixLon,
|
||||
'drawn behind the fix',
|
||||
Math.abs(behindM - expectedM) < speedMps * 3,
|
||||
`expected about ${Math.round(expectedM)} m behind the fix, got ${Math.round(behindM)} m`,
|
||||
);
|
||||
});
|
||||
|
||||
test('the delayed position is read from the app clock', async () => {
|
||||
// With the app clock 10 s behind the wall clock, the render time moves 10 s
|
||||
// further back from the fix.
|
||||
const { behindM, westOfFix, speedMps, fixAgeMs, RENDER_DELAY_SEC } =
|
||||
await firstPlacement(10_000);
|
||||
const expectedM = speedMps * (RENDER_DELAY_SEC + 10 - fixAgeMs / 1000);
|
||||
assert.ok(westOfFix, 'drawn behind the fix');
|
||||
assert.ok(
|
||||
Math.abs(behindM - expectedM) < speedMps * 3,
|
||||
`expected about ${Math.round(expectedM)} m behind the fix, got ${Math.round(behindM)} m`,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
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 { aircraftIncludedInNearby } from '../../data/aircraftNearbyPolicy.js';
|
||||
@@ -338,7 +337,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) {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import * as geo from '../../geo/index.js';
|
||||
import { CLASS_SCALE_2D } from '../../data/aircraftClass.js';
|
||||
import {
|
||||
@@ -72,18 +71,19 @@ export function createMilitarySnapshotRenderer({
|
||||
// report, so the fix epoch is receipt time minus that age. 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.speedMps,
|
||||
|
||||
@@ -109,7 +109,7 @@ export function createFlightState({ source, services }) {
|
||||
|
||||
flightState._detectionObjects = new Map();
|
||||
|
||||
/** @type {Map<string, Array<{time: Cesium.JulianDate, position: Cesium.Cartesian3}>>} ICAO hex -> recent position samples for dead reckoning */
|
||||
/** @type {Map<string, Array<{timeMs: number, epochMs: number, position: Cesium.Cartesian3}>>} ICAO hex -> recent position samples for dead reckoning (timeMs: whole ms since the epoch, the app clock's unit) */
|
||||
|
||||
flightState._positionHistory = new Map();
|
||||
|
||||
@@ -224,16 +224,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();
|
||||
|
||||
// -- Per-frame dead-reckoning cache for the tracked entity --
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import { appClock } from '../../time/clock.js';
|
||||
import { nextCockpitNearContacts } from '../../data/cockpitAirLod.js';
|
||||
import { aircraftIcon, TRACKED_ICON_PX } from '../../data/aircraftIcons.js';
|
||||
import { trackedModelZoomActive } from '../../data/trackedModelRegime.js';
|
||||
@@ -497,13 +498,9 @@ export function createTracking({
|
||||
// Seed only fixes at/behind the DELAYED display time (now − RENDER_DELAY_SEC). The
|
||||
// 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 via _appendTrailFix as they age.
|
||||
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) {
|
||||
// Floor grounded-history seeds (round 2): a grounded contact's stored
|
||||
// fixes carry the pre-datum default height — the trail copy sits on
|
||||
// the surface instead (pure lift; airborne fixes pass through).
|
||||
@@ -586,7 +583,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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user