mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Contacts move back as smoothly as they move on
A snapshot timeline may declare spanMs: each record of a snapshot at a time then carries its own fixes from spanMs before the time to spanMs after it. Flights, military flights and vessels following the clock are placed on those fixes, so a seek or a move back within the fixes loaded is no jump: nothing is cleared and play does not wait. The step there is asked for once and stands in for every contact; those it has keep their place. Fixes are merged across snapshots at least 20 s apart, at most 240 a contact, and a contact is not drawn more than a minute before its first fix.
This commit is contained in:
1 parent
b8a320edce
commit
2b5fb51684
27 files changed
+1377
-182
No files matched your search
@@ -0,0 +1,171 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import {
|
||||
mergeTimelineRuns,
|
||||
readTimelineFixes,
|
||||
TIMELINE_UNSEEN_MS,
|
||||
} from '../time/timelineFixes.js';
|
||||
|
||||
/** Fixes nearer than this are too close to read a course between. */
|
||||
const COURSE_MIN_M = 5;
|
||||
const EARTH_RADIUS_M = 6_371_000;
|
||||
|
||||
/** The initial great-circle course from one fix to another (degrees from
|
||||
* north), or null when they are too close to tell. */
|
||||
function courseBetween(from, to) {
|
||||
const lat1 = Cesium.Math.toRadians(from.lat);
|
||||
const lat2 = Cesium.Math.toRadians(to.lat);
|
||||
const dLon = Cesium.Math.toRadians(to.lon - from.lon);
|
||||
const dLat = lat2 - lat1;
|
||||
const a =
|
||||
Math.sin(dLat / 2) ** 2 +
|
||||
Math.cos(lat1) * Math.cos(lat2) * Math.sin(dLon / 2) ** 2;
|
||||
if (2 * EARTH_RADIUS_M * Math.asin(Math.min(1, Math.sqrt(a))) < COURSE_MIN_M)
|
||||
return null;
|
||||
const y = Math.sin(dLon) * Math.cos(lat2);
|
||||
const x =
|
||||
Math.cos(lat1) * Math.sin(lat2) -
|
||||
Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
|
||||
return (Cesium.Math.toDegrees(Math.atan2(y, x)) + 360) % 360;
|
||||
}
|
||||
|
||||
/**
|
||||
* An aircraft's motion history with the fixes its record carries at a time
|
||||
* (`observation.fixes`, see src/time/timelineFixes.js) merged in, so the
|
||||
* layer places it on its own track at any time they span; null when the
|
||||
* record carries none. Each fix is drawn at the record's render height,
|
||||
* moved by the fix's own barometric change from the record's, and heads
|
||||
* where its track goes next (the last, where it came from). Entries are
|
||||
* marked `timeline`.
|
||||
* @param {object[]|undefined} history The contact's history entries.
|
||||
* @param {object} observation The record, with `fixes` and `baroAltitudeM`.
|
||||
* @param {{renderAltitudeM: number, renderAltitudeInvented?: boolean}} meta
|
||||
* @param {number|null} velocity The record's ground speed (m/s).
|
||||
* @param {number} centreMs The snapshot's time: the fixes kept are nearest it.
|
||||
* @returns {object[]|null}
|
||||
*/
|
||||
export function mergeAircraftTimelineFixes(
|
||||
history,
|
||||
observation,
|
||||
meta,
|
||||
velocity,
|
||||
centreMs,
|
||||
) {
|
||||
const fixes = readTimelineFixes(observation?.fixes);
|
||||
if (!fixes) return null;
|
||||
const baroM = Number.isFinite(observation.baroAltitudeM)
|
||||
? observation.baroAltitudeM
|
||||
: null;
|
||||
const entries = fixes.map((fix, index) => {
|
||||
const course =
|
||||
index + 1 < fixes.length
|
||||
? courseBetween(fix, fixes[index + 1])
|
||||
: courseBetween(fixes[index - 1], fix);
|
||||
return {
|
||||
time: Cesium.JulianDate.fromDate(new Date(fix.atMs)),
|
||||
epochMs: fix.atMs,
|
||||
position: Cesium.Cartesian3.fromDegrees(
|
||||
fix.lon,
|
||||
fix.lat,
|
||||
baroM !== null && fix.altitudeM !== null
|
||||
? meta.renderAltitudeM + (fix.altitudeM - baroM)
|
||||
: meta.renderAltitudeM,
|
||||
),
|
||||
velocity,
|
||||
// No reported track per fix: the course is read from the fixes.
|
||||
track: course ?? undefined,
|
||||
inventedHeight: meta.renderAltitudeInvented === true,
|
||||
timeline: true,
|
||||
};
|
||||
});
|
||||
return mergeTimelineRuns(
|
||||
history ?? [],
|
||||
entries,
|
||||
(entry) => entry.epochMs,
|
||||
centreMs,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The index of the first history entry at or after `renderTime` (the last
|
||||
* entry when none is), by halving: a contact following the clock keeps up to
|
||||
* TIMELINE_FIXES_LIMIT entries, and it is placed on every fleet tick. The
|
||||
* bracketing pair is the entry before it and it.
|
||||
* @param {Array<{time: Cesium.JulianDate}>} history In time order.
|
||||
* @param {Cesium.JulianDate} renderTime
|
||||
* @returns {number}
|
||||
*/
|
||||
export function historyBracketIndex(history, renderTime) {
|
||||
let low = 0;
|
||||
let high = history.length - 1;
|
||||
while (low < high) {
|
||||
const mid = (low + high) >> 1;
|
||||
if (Cesium.JulianDate.lessThan(history[mid].time, renderTime))
|
||||
low = mid + 1;
|
||||
else high = mid;
|
||||
}
|
||||
return low;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a contact following the clock is not to be drawn at `renderTime`:
|
||||
* its history is its own fixes around the time (`timeline` entries), and the
|
||||
* time is more than TIMELINE_UNSEEN_MS before the first, when it was not
|
||||
* there yet.
|
||||
* @param {Array<{time: Cesium.JulianDate, timeline?: boolean}>|undefined} history
|
||||
* @param {Cesium.JulianDate} renderTime
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function historyUnseenAt(history, renderTime) {
|
||||
const first = history?.[0];
|
||||
return (
|
||||
first?.timeline === true &&
|
||||
Cesium.JulianDate.secondsDifference(first.time, renderTime) >
|
||||
TIMELINE_UNSEEN_MS / 1000
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lift a grounded contact's history onto the floor: its newest fix to
|
||||
* `position`, or, when the history is its own fixes around the time
|
||||
* (`timeline` entries, the newest of them later than the time drawn), every
|
||||
* fix by the lift from `fromM` to `toM`, so it is not drawn rising to it.
|
||||
* @param {object[]|undefined} history
|
||||
* @param {Cesium.Cartesian3} position The newest fix lifted.
|
||||
* @param {number|null} fromM The height it was drawn at.
|
||||
* @param {number} toM The height it is lifted to.
|
||||
*/
|
||||
export function liftGroundedHistory(history, position, fromM, toM) {
|
||||
const newest = history?.[history.length - 1];
|
||||
if (!newest) return;
|
||||
if (history[0].timeline !== true || !Number.isFinite(fromM)) {
|
||||
newest.position = Cesium.Cartesian3.clone(position, newest.position);
|
||||
return;
|
||||
}
|
||||
for (const entry of history) {
|
||||
const carto = Cesium.Cartographic.fromCartesian(entry.position);
|
||||
if (!carto) continue;
|
||||
entry.position = Cesium.Cartesian3.fromRadians(
|
||||
carto.longitude,
|
||||
carto.latitude,
|
||||
carto.height + (toM - fromM),
|
||||
undefined,
|
||||
entry.position,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The history entries a tracked contact's trail has yet to take: those after
|
||||
* its newest point (`trail` entries have `timeMs`) up to `untilMs`, the
|
||||
* snapshot's time, so the trail grows as the snapshots bring fixes.
|
||||
* @param {Array<{epochMs: number}>} history
|
||||
* @param {Array<{timeMs: number}>} trail
|
||||
* @param {number} untilMs
|
||||
* @returns {object[]}
|
||||
*/
|
||||
export function historyAfterTrail(history, trail, untilMs) {
|
||||
const lastMs = trail.length ? trail[trail.length - 1].timeMs : -Infinity;
|
||||
return history.filter(
|
||||
(entry) => entry.epochMs > lastMs && entry.epochMs <= untilMs,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import * as Cesium from 'cesium';
|
||||
import {
|
||||
historyAfterTrail,
|
||||
historyBracketIndex,
|
||||
historyUnseenAt,
|
||||
liftGroundedHistory,
|
||||
mergeAircraftTimelineFixes,
|
||||
} from './timelineHistory.js';
|
||||
|
||||
const META = { renderAltitudeM: 3_000 };
|
||||
const fix = (atMs, lat, altitudeM = null, lon = 0) => ({
|
||||
atMs,
|
||||
lat,
|
||||
lon,
|
||||
altitudeM,
|
||||
});
|
||||
const julian = (ms) => Cesium.JulianDate.fromDate(new Date(ms));
|
||||
|
||||
test('an aircraft with no fixes merges nothing', () => {
|
||||
assert.equal(
|
||||
mergeAircraftTimelineFixes([], { fixes: null }, META, 100, 0),
|
||||
null,
|
||||
);
|
||||
});
|
||||
|
||||
test("an aircraft's fixes join its history in order, each at the render height moved by its own climb", () => {
|
||||
const history = mergeAircraftTimelineFixes(
|
||||
[],
|
||||
{
|
||||
baroAltitudeM: 1_000,
|
||||
fixes: [fix(120_000, 1, 1_200), fix(60_000, 0, 1_000), fix(180_000, 2)],
|
||||
},
|
||||
META,
|
||||
100,
|
||||
120_000,
|
||||
);
|
||||
assert.deepEqual(
|
||||
history.map((entry) => entry.epochMs),
|
||||
[60_000, 120_000, 180_000],
|
||||
);
|
||||
const height = (entry) =>
|
||||
Cesium.Cartographic.fromCartesian(entry.position).height;
|
||||
assert.ok(Math.abs(height(history[0]) - 3_000) < 1e-3);
|
||||
assert.ok(Math.abs(height(history[1]) - 3_200) < 1e-3);
|
||||
assert.ok(
|
||||
Math.abs(height(history[2]) - 3_000) < 1e-3,
|
||||
'no altitude: the render height',
|
||||
);
|
||||
// A later snapshot's fixes add to the earlier ones.
|
||||
const more = mergeAircraftTimelineFixes(
|
||||
history,
|
||||
{ baroAltitudeM: 1_000, fixes: [fix(180_000, 2), fix(240_000, 3)] },
|
||||
META,
|
||||
100,
|
||||
180_000,
|
||||
);
|
||||
assert.deepEqual(
|
||||
more.map((entry) => entry.epochMs),
|
||||
[60_000, 120_000, 180_000, 240_000],
|
||||
);
|
||||
});
|
||||
|
||||
test('the bracketing entry is the first at or after the time, else the last', () => {
|
||||
const history = [1_000, 2_000, 3_000].map((ms) => ({
|
||||
time: Cesium.JulianDate.fromDate(new Date(ms)),
|
||||
}));
|
||||
const at = (ms) =>
|
||||
historyBracketIndex(history, Cesium.JulianDate.fromDate(new Date(ms)));
|
||||
assert.equal(at(500), 0);
|
||||
assert.equal(at(1_000), 0);
|
||||
assert.equal(at(1_500), 1);
|
||||
assert.equal(at(3_000), 2);
|
||||
assert.equal(at(9_000), 2);
|
||||
});
|
||||
|
||||
test('each fix heads where the track goes next, the last where it came from', () => {
|
||||
const history = mergeAircraftTimelineFixes(
|
||||
[],
|
||||
{
|
||||
fixes: [
|
||||
fix(0, 0, null, 0),
|
||||
fix(60_000, 0, null, 0.01),
|
||||
fix(120_000, 0.01, null, 0.01),
|
||||
fix(180_000, 0.01, null, 0.01),
|
||||
],
|
||||
},
|
||||
META,
|
||||
100,
|
||||
0,
|
||||
);
|
||||
const tracks = history.map((entry) => entry.track);
|
||||
assert.ok(Math.abs(tracks[0] - 90) < 0.5, 'east');
|
||||
assert.ok(Math.abs(tracks[1]) < 0.5, 'north');
|
||||
assert.equal(tracks[2], undefined, 'no move to read');
|
||||
assert.equal(tracks[3], undefined);
|
||||
assert.ok(history.every((entry) => entry.timeline === true));
|
||||
});
|
||||
|
||||
test('a contact is unseen only well before the first of its own fixes', () => {
|
||||
const history = mergeAircraftTimelineFixes(
|
||||
[],
|
||||
{ fixes: [fix(600_000, 0), fix(660_000, 1)] },
|
||||
META,
|
||||
100,
|
||||
600_000,
|
||||
);
|
||||
assert.equal(historyUnseenAt(history, julian(0)), true);
|
||||
assert.equal(historyUnseenAt(history, julian(560_000)), false);
|
||||
const live = [{ time: julian(600_000) }];
|
||||
assert.equal(historyUnseenAt(live, julian(0)), false, 'not on its fixes');
|
||||
assert.equal(historyUnseenAt(undefined, julian(0)), false);
|
||||
});
|
||||
|
||||
test('a grounded contact on its own fixes is lifted whole; else its newest fix', () => {
|
||||
const height = (entry) =>
|
||||
Cesium.Cartographic.fromCartesian(entry.position).height;
|
||||
const history = mergeAircraftTimelineFixes(
|
||||
[],
|
||||
{ fixes: [fix(0, 0), fix(60_000, 0.001)] },
|
||||
{ renderAltitudeM: 5 },
|
||||
0,
|
||||
0,
|
||||
);
|
||||
const lifted = Cesium.Cartesian3.fromDegrees(0, 0.001, 30);
|
||||
liftGroundedHistory(history, lifted, 5, 30);
|
||||
assert.ok(history.every((entry) => Math.abs(height(entry) - 30) < 1e-3));
|
||||
const live = [
|
||||
{ position: Cesium.Cartesian3.fromDegrees(0, 0, 5) },
|
||||
{ position: Cesium.Cartesian3.fromDegrees(0, 0.001, 5) },
|
||||
];
|
||||
liftGroundedHistory(live, lifted, 5, 30);
|
||||
assert.ok(Math.abs(height(live[0]) - 5) < 1e-3);
|
||||
assert.ok(Math.abs(height(live[1]) - 30) < 1e-3);
|
||||
});
|
||||
|
||||
test("a tracked trail takes the fixes after its newest point, up to the snapshot's time", () => {
|
||||
const history = [0, 60_000, 120_000, 180_000].map((epochMs) => ({ epochMs }));
|
||||
assert.deepEqual(
|
||||
historyAfterTrail(history, [{ timeMs: 60_000 }], 120_000).map(
|
||||
(entry) => entry.epochMs,
|
||||
),
|
||||
[120_000],
|
||||
);
|
||||
assert.equal(historyAfterTrail(history, [], 120_000).length, 3);
|
||||
});
|
||||
Reference in new issue
Block a user