mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Main brings satellites and vessels moving every frame, smooth rewind for contacts, global clouds drawn to the level a feed names, and search for degrees-minutes-seconds, UTM, MGRS and plus codes. They keep this branch's renderer work: satellites write their reckoned positions into the points set once a frame; vessels move every frame and ask for frames from the camera pose and canvas, and a vessel not there yet is hidden through its icon; flights and military keep the branch's rendering, with main's timeline fixes, trail top-up and grounded lift; the cloud tiles keep their infrared transfer. The timeline history module uses the geo helpers instead of Cesium, and main's new tests use the draw port's items.
This commit is contained in:
commit
4b47d5e058
49 files changed
+3367
-348
No files matched your search
@@ -0,0 +1,180 @@
|
||||
import {
|
||||
fromDegrees,
|
||||
fromRadians,
|
||||
toCartographicRadians,
|
||||
} from '../geo/ellipsoid.js';
|
||||
import {
|
||||
julianFromEpochMs,
|
||||
julianLessThan,
|
||||
secondsDifference,
|
||||
} from '../geo/julian.js';
|
||||
import { clone, copyInto, toDegrees, toRadians } from '../geo/vector.js';
|
||||
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 = toRadians(from.lat);
|
||||
const lat2 = toRadians(to.lat);
|
||||
const dLon = 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 (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: julianFromEpochMs(fix.atMs),
|
||||
epochMs: fix.atMs,
|
||||
position: 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: {dayNumber: number, secondsOfDay: number}}>} history
|
||||
* In time order (Julian dates, geo/julian).
|
||||
* @param {{dayNumber: number, secondsOfDay: number}} 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 (julianLessThan(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: {dayNumber: number, secondsOfDay: number},
|
||||
* timeline?: boolean}>|undefined} history
|
||||
* @param {{dayNumber: number, secondsOfDay: number}} renderTime
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function historyUnseenAt(history, renderTime) {
|
||||
const first = history?.[0];
|
||||
return (
|
||||
first?.timeline === true &&
|
||||
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 {{x: number, y: number, z: number}} position The newest fix lifted (ECEF).
|
||||
* @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 = newest.position
|
||||
? copyInto(position, newest.position)
|
||||
: clone(position);
|
||||
return;
|
||||
}
|
||||
for (const entry of history) {
|
||||
const carto = toCartographicRadians(entry.position);
|
||||
if (!carto) continue;
|
||||
copyInto(
|
||||
fromRadians(carto.lon, carto.lat, carto.height + (toM - fromM)),
|
||||
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