mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-18 01:02:23 +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
@@ -101,7 +101,10 @@ test('the subject says whether its track is the heading or the course', () => {
|
||||
test('a vessel without identity or position is not a subject', () => {
|
||||
assert.equal(vesselCockpitInfo(null), null);
|
||||
assert.equal(vesselCockpitInfo({ lat: 1, lon: 1 }), null);
|
||||
assert.equal(vesselCockpitInfo({ id: '1', mmsi: '1', lat: 'x', lon: 1 }), null);
|
||||
assert.equal(
|
||||
vesselCockpitInfo({ id: '1', mmsi: '1', lat: 'x', lon: 1 }),
|
||||
null,
|
||||
);
|
||||
});
|
||||
|
||||
test('a vessel missing from recent refreshes reads as stale', () => {
|
||||
|
||||
@@ -39,8 +39,9 @@ export function createIngestion({
|
||||
* @returns {Promise<boolean>}
|
||||
*/
|
||||
async function loadAt(atMs, { signal = null, replace = false } = {}) {
|
||||
// Whatever polls or loads for another time no longer applies.
|
||||
if (replace && feed.loading) {
|
||||
// Whatever polls or loads for another time no longer applies: the
|
||||
// timeline asks for one time at a time.
|
||||
if (feed.loading) {
|
||||
feed.abort?.abort();
|
||||
feed.loading = false;
|
||||
feed.abort = null;
|
||||
@@ -209,6 +210,7 @@ export function createIngestion({
|
||||
reference: record.reference,
|
||||
lat: record.latitude,
|
||||
lon: record.longitude,
|
||||
fixes: record.fixes,
|
||||
name: record.name,
|
||||
imo: record.imo,
|
||||
type: record.type,
|
||||
|
||||
@@ -1,14 +1,39 @@
|
||||
/**
|
||||
* Where a vessel is at world time `tMs` while the layer follows the world
|
||||
* clock: between the fix it moved from and the fix it moved to, along the
|
||||
* great circle; at the earlier fix before it, at the newer fix after it. A
|
||||
* vessel is never projected past its newest fix.
|
||||
* clock: on its own fixes around the time when its record carries them
|
||||
* (`fixes`, see src/time/timelineFixes.js), else between the fix it moved
|
||||
* from and the fix it moved to; along the great circle between two fixes, at
|
||||
* the first before them and at the last after them. A vessel is never
|
||||
* projected past its newest fix.
|
||||
* @param {{lat: number, lon: number, motion?: {fromLat: number, fromLon: number,
|
||||
* fromMs: number, toLat: number, toLon: number, toMs: number}|null}} record
|
||||
* @param {number} tMs
|
||||
* @returns {{lat: number, lon: number}}
|
||||
*/
|
||||
export function vesselPositionAt(record, tMs) {
|
||||
const fixes = record?.fixes;
|
||||
if (fixes?.length >= 2 && Number.isFinite(tMs)) {
|
||||
const first = fixes[0];
|
||||
const last = fixes[fixes.length - 1];
|
||||
if (tMs <= first.atMs) return { lat: first.lat, lon: first.lon };
|
||||
if (tMs >= last.atMs) return { lat: last.lat, lon: last.lon };
|
||||
let low = 1;
|
||||
let high = fixes.length - 1;
|
||||
while (low < high) {
|
||||
const mid = (low + high) >> 1;
|
||||
if (fixes[mid].atMs < tMs) low = mid + 1;
|
||||
else high = mid;
|
||||
}
|
||||
const a = fixes[low - 1];
|
||||
const b = fixes[low];
|
||||
return greatCircleAt(
|
||||
a.lat,
|
||||
a.lon,
|
||||
b.lat,
|
||||
b.lon,
|
||||
b.atMs > a.atMs ? (tMs - a.atMs) / (b.atMs - a.atMs) : 1,
|
||||
);
|
||||
}
|
||||
const motion = record?.motion;
|
||||
if (
|
||||
!motion ||
|
||||
@@ -60,3 +85,28 @@ function unit(lat, lon) {
|
||||
Math.sin(phi),
|
||||
];
|
||||
}
|
||||
|
||||
// Whether each fixes array moves, read once per array: records keep theirs
|
||||
// until a snapshot brings new ones.
|
||||
const moving = new WeakMap();
|
||||
|
||||
/**
|
||||
* Whether a vessel's fixes (`record.fixes`) put it in more than one place,
|
||||
* so following the clock moves it; a moored vessel's do not.
|
||||
* @param {Array<{lat: number, lon: number}>|null|undefined} fixes
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function fixesMove(fixes) {
|
||||
if (!fixes?.length) return false;
|
||||
let moves = moving.get(fixes);
|
||||
if (moves === undefined) {
|
||||
const [first] = fixes;
|
||||
moves = fixes.some(
|
||||
(fix) =>
|
||||
Math.abs(fix.lat - first.lat) > 1e-6 ||
|
||||
Math.abs(fix.lon - first.lon) > 1e-6,
|
||||
);
|
||||
moving.set(fixes, moves);
|
||||
}
|
||||
return moves;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { vesselPositionAt } from './motion.js';
|
||||
import { fixesMove, vesselPositionAt } from './motion.js';
|
||||
|
||||
const record = {
|
||||
lat: 1,
|
||||
@@ -29,3 +29,35 @@ test('a vessel with no move is at its record', () => {
|
||||
assert.equal(vesselPositionAt(still, 2_000), still);
|
||||
assert.equal(vesselPositionAt({ ...still, motion: null }, 2_000).lat, 4);
|
||||
});
|
||||
|
||||
test('a vessel with fixes around the time is placed on them, held at either end', () => {
|
||||
const tracked = {
|
||||
lat: 9,
|
||||
lon: 9,
|
||||
motion: record.motion,
|
||||
fixes: [
|
||||
{ atMs: 1_000, lat: 0, lon: 0, altitudeM: null },
|
||||
{ atMs: 2_000, lat: 1, lon: 0, altitudeM: null },
|
||||
{ atMs: 4_000, lat: 1, lon: 2, altitudeM: null },
|
||||
],
|
||||
};
|
||||
assert.deepEqual(vesselPositionAt(tracked, 0), { lat: 0, lon: 0 });
|
||||
assert.deepEqual(vesselPositionAt(tracked, 9_000), { lat: 1, lon: 2 });
|
||||
const back = vesselPositionAt(tracked, 1_500);
|
||||
assert.ok(Math.abs(back.lat - 0.5) < 1e-3 && Math.abs(back.lon) < 1e-6);
|
||||
const on = vesselPositionAt(tracked, 3_000);
|
||||
assert.ok(Math.abs(on.lat - 1) < 1e-3 && Math.abs(on.lon - 1) < 1e-3);
|
||||
});
|
||||
|
||||
test('fixes that stay in one place do not move a vessel', () => {
|
||||
const still = [
|
||||
{ atMs: 0, lat: 1, lon: 2 },
|
||||
{ atMs: 60_000, lat: 1, lon: 2 },
|
||||
];
|
||||
assert.equal(fixesMove(still), false);
|
||||
assert.equal(
|
||||
fixesMove([...still, { atMs: 90_000, lat: 1.001, lon: 2 }]),
|
||||
true,
|
||||
);
|
||||
assert.equal(fixesMove(null), false);
|
||||
});
|
||||
@@ -1,3 +1,7 @@
|
||||
import {
|
||||
mergeTimelineRuns,
|
||||
readTimelineFixes,
|
||||
} from '../../time/timelineFixes.js';
|
||||
import {
|
||||
PARTIAL_RETENTION_MS,
|
||||
SELECTED_PIN_REFRESHES,
|
||||
@@ -35,6 +39,9 @@ export function normalizeVessel(row) {
|
||||
virtual: row.virtual === true,
|
||||
lastPositionUtc: String(row.last_position_UTC || ''),
|
||||
lastPositionEpoch: finiteNumber(row.last_position_epoch),
|
||||
// Its own track around the time, from a snapshot at a time (see
|
||||
// src/time/timelineFixes.js); null when the row carries none.
|
||||
fixes: readTimelineFixes(row.fixes),
|
||||
missedRefreshes: 0,
|
||||
};
|
||||
}
|
||||
@@ -145,8 +152,22 @@ export class VesselRecords {
|
||||
record.fixReceivedAtMs = next.fixReceivedAtMs;
|
||||
record.fixFeedEpoch = next.fixFeedEpoch;
|
||||
}
|
||||
if (replace) record.motion = null;
|
||||
else if (follow) record.motion = motionBetween(record, next);
|
||||
if (replace) {
|
||||
record.motion = null;
|
||||
record.fixes = next.fixes;
|
||||
} else if (follow) {
|
||||
record.motion = motionBetween(record, next);
|
||||
// Without fixes now, it is placed by its record, not by fixes
|
||||
// that end before it.
|
||||
record.fixes = next.fixes
|
||||
? mergeTimelineRuns(
|
||||
record.fixes ?? [],
|
||||
next.fixes,
|
||||
(fix) => fix.atMs,
|
||||
(next.lastPositionEpoch ?? 0) * 1000,
|
||||
)
|
||||
: null;
|
||||
} else record.fixes = null;
|
||||
record.reference = next.reference;
|
||||
record.mmsi = next.mmsi;
|
||||
record.receivedAtMs = next.receivedAtMs;
|
||||
|
||||
@@ -187,3 +187,15 @@ test('an AIS row with no id is keyed by its MMSI; one with neither is unkeyed',
|
||||
assert.equal(probe.store.byId.size, 0);
|
||||
assert.equal(probe.store.unkeyed.length, 2);
|
||||
});
|
||||
|
||||
const fixes = (...pairs) =>
|
||||
pairs.map(([atMs, lat]) => ({ atMs, lat, lon: 4.05, altitudeM: null }));
|
||||
|
||||
test('following the clock, a vessel whose next record carries no fixes is placed by the record', () => {
|
||||
const probe = setup();
|
||||
probe.reconcile([row('111', { fixes: fixes([0, 1], [60_000, 2]) })], {
|
||||
replace: true,
|
||||
});
|
||||
probe.reconcile([row('111', { lat: 3 })], { follow: true });
|
||||
assert.equal(probe.store.byId.get('111').fixes, null);
|
||||
});
|
||||
@@ -4,10 +4,11 @@ import { createVesselLayer } from './index.js';
|
||||
import { VISIBILITY_UPDATE_MS } from './policy.js';
|
||||
import { bindFakeCesiumScene } from '../../testSupport/fakeCesiumScene.mjs';
|
||||
import { layerPlacement } from '../../data/placementProvider.js';
|
||||
import * as Cesium from 'cesium';
|
||||
|
||||
// Vessels move only when a poll lands, so the layer must let the scene idle:
|
||||
// it asks for frames for forced passes and one trailing frame after the camera
|
||||
// stops, and never schedules a frame from a pass the camera did not cause.
|
||||
// The layer must let the scene idle: it asks for frames for forced passes,
|
||||
// for vessels that move, and one trailing frame after the camera stops, and
|
||||
// never schedules a frame from a pass nothing caused.
|
||||
|
||||
const noop = () => {};
|
||||
|
||||
@@ -78,7 +79,16 @@ function setup({ focusPassIsNeeded = () => false, placement = null } = {}) {
|
||||
}
|
||||
};
|
||||
const frame = () => layer.testing._runVisibilityPassForTest();
|
||||
return { layer, camera, requests, holds, timers, advance, frame };
|
||||
return {
|
||||
layer,
|
||||
camera,
|
||||
requests,
|
||||
holds,
|
||||
timers,
|
||||
advance,
|
||||
frame,
|
||||
now: () => clock,
|
||||
};
|
||||
}
|
||||
|
||||
test('a parked camera schedules no frames after the cadence pass', () => {
|
||||
@@ -117,6 +127,26 @@ test('a camera that stops inside the cadence gets exactly one trailing frame', (
|
||||
assert.equal(timers.size, 0);
|
||||
});
|
||||
|
||||
test('following the clock, a time moved inside the cadence gets one trailing frame', () => {
|
||||
const { layer, requests, timers, advance, frame } = setup();
|
||||
layer.testing._followClockForTest(1_000_000);
|
||||
frame();
|
||||
advance(100);
|
||||
// A seek while paused: the camera is still, the time drawn is not.
|
||||
layer.testing._followClockForTest(940_000);
|
||||
frame();
|
||||
assert.equal(timers.size, 1);
|
||||
advance(VISIBILITY_UPDATE_MS);
|
||||
assert.deepEqual(requests, ['ais-vessels-visibility']);
|
||||
frame();
|
||||
for (let i = 0; i < 20; i += 1) {
|
||||
advance(100);
|
||||
frame();
|
||||
}
|
||||
assert.deepEqual(requests, ['ais-vessels-visibility']);
|
||||
assert.equal(timers.size, 0);
|
||||
});
|
||||
|
||||
test('a forced pass requests its own frame', () => {
|
||||
const { layer, requests } = setup();
|
||||
layer.testing._runVisibilityPassForTest(true);
|
||||
@@ -239,3 +269,200 @@ test('a placement that places nothing asks for no frames; one that places a draw
|
||||
advance(1_000);
|
||||
assert.deepEqual(requests, ['ais-vessels-placement']);
|
||||
});
|
||||
|
||||
test('a vessel moving visibly asks for its next frame as soon as it moves half a pixel; a slow one once a second', (t) => {
|
||||
let mono = 0;
|
||||
t.mock.method(performance, 'now', () => mono);
|
||||
const originalDocument = globalThis.document;
|
||||
globalThis.document = { getElementById: () => null };
|
||||
t.after(() => {
|
||||
if (originalDocument === undefined) delete globalThis.document;
|
||||
else globalThis.document = originalDocument;
|
||||
});
|
||||
let spot = { lat: 51.93, lon: 4.05 };
|
||||
const { layer, camera, timers, advance, now } = setup({
|
||||
placement: { layer: () => ({ place: () => spot }) },
|
||||
});
|
||||
// 20 km above the vessel, in a 1,000 px tall view about 57° tall.
|
||||
camera.positionWC = Cesium.Cartesian3.fromDegrees(4.05, 51.93, 20_000);
|
||||
camera.frustum = { fovy: 1 };
|
||||
const record = { mmsi: '111', lat: 51.93, lon: 4.05 };
|
||||
// Drawn: on the camera's side of the horizon.
|
||||
const drawnVisual = (viewer) => {
|
||||
bindFakeCesiumScene(viewer);
|
||||
layer.testing._setVesselStateForTest({ viewer, records: [record] });
|
||||
layer.testing._prepareVesselVisualForTest(record).visible = true;
|
||||
};
|
||||
drawnVisual({ camera, scene: {}, canvas: { clientHeight: 1000 } });
|
||||
// How long after a pass the next frame is asked for, once the vessel moved
|
||||
// `dLat` in the 16 ms since the pass before.
|
||||
const waitAfterStep = (dLat) => {
|
||||
layer.testing._runPlacementPassForTest();
|
||||
advance(1_000); // whatever frame that asked for comes and goes
|
||||
assert.equal(timers.size, 0);
|
||||
mono += 16;
|
||||
spot = { lat: spot.lat + dLat, lon: spot.lon };
|
||||
layer.testing._runPlacementPassForTest();
|
||||
assert.equal(timers.size, 1);
|
||||
const [timer] = [...timers.values()];
|
||||
return timer.at - now();
|
||||
};
|
||||
// About 11 m in a frame, 20 km away: over half a pixel, so the next frame
|
||||
// is asked for within the frame.
|
||||
assert.ok(waitAfterStep(0.0001) <= 16);
|
||||
// About 1 cm in a frame: half a pixel takes seconds, so once a second.
|
||||
assert.equal(waitAfterStep(0.0000001), 1000);
|
||||
// A 2D view 40 km across in 1,000 px: 11 m is over a quarter pixel, so the
|
||||
// next frame comes within two frames.
|
||||
camera.frustum = new Cesium.OrthographicOffCenterFrustum({
|
||||
left: -20_000,
|
||||
right: 20_000,
|
||||
bottom: -20_000,
|
||||
top: 20_000,
|
||||
near: 1,
|
||||
far: 1e8,
|
||||
});
|
||||
camera.viewMatrix = Cesium.Matrix4.IDENTITY;
|
||||
drawnVisual({ camera, scene: {}, canvas: { clientWidth: 1000 } });
|
||||
assert.ok(waitAfterStep(0.0001) <= 32);
|
||||
});
|
||||
|
||||
test('following the clock moves drawn vessels in place each world time, prepares missing ones and leaves hidden ones be', (t) => {
|
||||
let mono = 0;
|
||||
t.mock.method(performance, 'now', () => mono);
|
||||
const originalDocument = globalThis.document;
|
||||
globalThis.document = { getElementById: () => null };
|
||||
t.after(() => {
|
||||
if (originalDocument === undefined) delete globalThis.document;
|
||||
else globalThis.document = originalDocument;
|
||||
});
|
||||
const { layer, camera, timers, advance } = setup();
|
||||
camera.positionWC = Cesium.Cartesian3.fromDegrees(4.05, 51.93, 20_000);
|
||||
camera.frustum = { fovy: 1 };
|
||||
const motion = {
|
||||
fromLat: 51.93,
|
||||
fromLon: 4.05,
|
||||
fromMs: 0,
|
||||
toLat: 51.94,
|
||||
toLon: 4.05,
|
||||
toMs: 60_000,
|
||||
};
|
||||
const vessel = (mmsi) => ({
|
||||
mmsi,
|
||||
lat: 51.94,
|
||||
lon: 4.05,
|
||||
motion: { ...motion },
|
||||
});
|
||||
const drawn = vessel('111');
|
||||
const hidden = vessel('222');
|
||||
const owned = vessel('333');
|
||||
const missing = vessel('444');
|
||||
const viewer = { camera, scene: {}, canvas: { clientHeight: 1000 } };
|
||||
bindFakeCesiumScene(viewer);
|
||||
layer.testing._setVesselStateForTest({
|
||||
viewer,
|
||||
records: [drawn, hidden, owned, missing],
|
||||
});
|
||||
const visuals = [drawn, hidden, owned].map((record) =>
|
||||
layer.testing._prepareVesselVisualForTest(record),
|
||||
);
|
||||
// Drawn as an icon (on the camera's side of the horizon), hidden, and
|
||||
// drawn by its model.
|
||||
visuals[0].visible = true;
|
||||
visuals[1].visible = false;
|
||||
visuals[2].visible = false;
|
||||
visuals[2].modelOwnsVisual = true;
|
||||
const before = visuals.map((visual) => ({ ...visual.position }));
|
||||
const positions = visuals.map((visual) => visual.position);
|
||||
|
||||
// The first pass also moves hidden vessels, so the next stays within
|
||||
// HIDDEN_FOLLOW_MS of it.
|
||||
layer.testing._runClockPassForTest(10_000);
|
||||
mono += 16;
|
||||
layer.testing._runClockPassForTest(20_000);
|
||||
assert.equal(visuals[0].position, positions[0], 'written in place');
|
||||
assert.notDeepEqual({ ...visuals[0].position }, before[0]);
|
||||
assert.notDeepEqual({ ...visuals[2].position }, before[2], 'model moves');
|
||||
const hiddenAt = { ...visuals[1].position };
|
||||
assert.ok(
|
||||
layer.testing._vesselVisualForTest(missing).position,
|
||||
'a missing visual is prepared',
|
||||
);
|
||||
assert.equal(timers.size, 1, 'moving vessels ask for a frame');
|
||||
|
||||
// The hidden vessel waits for HIDDEN_FOLLOW_MS.
|
||||
mono += 16;
|
||||
layer.testing._runClockPassForTest(30_000);
|
||||
assert.deepEqual({ ...visuals[1].position }, hiddenAt);
|
||||
mono += 250;
|
||||
layer.testing._runClockPassForTest(40_000);
|
||||
assert.notDeepEqual({ ...visuals[1].position }, hiddenAt);
|
||||
|
||||
// The same world time again moves nothing and asks for no frame.
|
||||
advance(1_000);
|
||||
assert.equal(timers.size, 0);
|
||||
const still = { ...visuals[0].position };
|
||||
mono += 16;
|
||||
layer.testing._runClockPassForTest(40_000);
|
||||
assert.deepEqual({ ...visuals[0].position }, still);
|
||||
assert.equal(timers.size, 0);
|
||||
|
||||
// Scrubbed back, a vessel moves back.
|
||||
mono += 16;
|
||||
layer.testing._runClockPassForTest(20_000);
|
||||
assert.notDeepEqual({ ...visuals[0].position }, still);
|
||||
|
||||
// Each vessel moves once per world time: new fixes at the same world time
|
||||
// are drawn when the snapshot that brings them prepares the visual.
|
||||
const backAt = { ...visuals[0].position };
|
||||
drawn.motion = { ...motion, toLat: 52 };
|
||||
mono += 16;
|
||||
layer.testing._runClockPassForTest(20_000);
|
||||
assert.deepEqual({ ...visuals[0].position }, backAt);
|
||||
|
||||
// Past the newest fix, vessels hold still and ask for nothing.
|
||||
mono += 16;
|
||||
layer.testing._runClockPassForTest(70_000);
|
||||
advance(1_000);
|
||||
mono += 16;
|
||||
layer.testing._runClockPassForTest(80_000);
|
||||
assert.equal(timers.size, 0);
|
||||
});
|
||||
|
||||
test('following the clock, a vessel draws no icon before it was there, and draws it once the time comes', (t) => {
|
||||
const originalDocument = globalThis.document;
|
||||
globalThis.document = { getElementById: () => null };
|
||||
t.after(() => {
|
||||
if (originalDocument === undefined) delete globalThis.document;
|
||||
else globalThis.document = originalDocument;
|
||||
});
|
||||
const { layer, camera } = setup();
|
||||
const viewer = { camera, scene: {} };
|
||||
const { draw } = bindFakeCesiumScene(viewer);
|
||||
layer.testing._setVesselStateForTest({ viewer, icons: null });
|
||||
const first = 1_000_000;
|
||||
layer.testing._followClockForTest(first - 10 * 60_000);
|
||||
layer.testing._reconcileVesselsForTest(viewer, [
|
||||
{
|
||||
mmsi: '111',
|
||||
lat: 51.93,
|
||||
lon: 4.05,
|
||||
last_position_epoch: first / 1000,
|
||||
fixes: [
|
||||
{ atMs: first, lat: 51.93, lon: 4.05 },
|
||||
{ atMs: first + 60_000, lat: 51.94, lon: 4.05 },
|
||||
],
|
||||
},
|
||||
]);
|
||||
const shown = () => {
|
||||
const [set] = [...draw.sets.values()].filter(
|
||||
(record) => record.kind === 'icons',
|
||||
);
|
||||
return set.items.get('111').show;
|
||||
};
|
||||
layer.testing._runVisibilityPassForTest(true);
|
||||
assert.equal(shown(), false, 'ten minutes before its first fix');
|
||||
layer.testing._followClockForTest(first - 30_000);
|
||||
layer.testing._runVisibilityPassForTest(true);
|
||||
assert.equal(shown(), true, 'half a minute before it: reaching it');
|
||||
});
|
||||
+212
-70
@@ -9,7 +9,7 @@ import {
|
||||
} from '../../data/vesselLabels.js';
|
||||
import { fromDegrees, fromDegreesInto } from '../../geo/ellipsoid.js';
|
||||
import { horizonFrom, liftedForHorizon } from '../../geo/horizon.js';
|
||||
import { distance, distanceSquared } from '../../geo/vector.js';
|
||||
import { copyInto, distance, distanceSquared, vec } from '../../geo/vector.js';
|
||||
import {
|
||||
canvasOf,
|
||||
drawOf,
|
||||
@@ -29,7 +29,8 @@ import {
|
||||
CARD_MIN_SEP_PX,
|
||||
followsClock,
|
||||
} from './policy.js';
|
||||
import { vesselPositionAt } from './motion.js';
|
||||
import { fixesMove, vesselPositionAt } from './motion.js';
|
||||
import { TIMELINE_UNSEEN_MS } from '../../time/timelineFixes.js';
|
||||
import {
|
||||
fleetModelSet,
|
||||
loadFleetModelSet,
|
||||
@@ -38,10 +39,15 @@ import {
|
||||
import { createFleetModelLayer } from '../../data/fleetModelInstances.js';
|
||||
import { isAidToNavigation } from '../../data/vesselTypes.js';
|
||||
|
||||
/** How often vessels move on as the world clock plays (wall ms). */
|
||||
const FOLLOW_POSITION_MS = 250;
|
||||
/** A frame is asked for this often while a placement moves live vessels. */
|
||||
/** A moving vessel's next frame is asked for once it would move this far on
|
||||
* screen (pixels), so it glides when it moves visibly and the scene idles
|
||||
* when it does not. */
|
||||
const VESSEL_STEP_PX = 0.5;
|
||||
/** A frame is asked for at least this often while any drawn vessel moves. */
|
||||
const PLACEMENT_FRAME_MS = 1000;
|
||||
/** How often vessels not drawn move on as the world clock plays (wall ms),
|
||||
* so one that comes into view is judged where it is. */
|
||||
const HIDDEN_FOLLOW_MS = 250;
|
||||
|
||||
/** The ship artwork's own raster size (px). */
|
||||
const SHIP_ICON_PX = 32;
|
||||
@@ -51,6 +57,9 @@ let instances = 0;
|
||||
/** How often the vessels near enough for a model are gathered again (wall ms). */
|
||||
const MODEL_CANDIDATES_MS = 500;
|
||||
|
||||
/** Scratch for where a moved vessel was drawn. */
|
||||
const _movedFrom = vec();
|
||||
|
||||
export function createRendering({
|
||||
vesselState,
|
||||
services,
|
||||
@@ -87,10 +96,8 @@ export function createRendering({
|
||||
visual.position = anchorAt(lat, lon);
|
||||
// A ship model's origin is its waterline: the sea surface, unlifted.
|
||||
visual.waterline = anchorAt(lat, lon, 0);
|
||||
if (services.placement) {
|
||||
visual.drawnLat = lat;
|
||||
visual.drawnLon = lon;
|
||||
}
|
||||
visual.drawnLat = lat;
|
||||
visual.drawnLon = lon;
|
||||
return visual;
|
||||
}
|
||||
|
||||
@@ -255,10 +262,13 @@ export function createRendering({
|
||||
function iconItem(record, visual) {
|
||||
const selected = record === state.selectedRecord;
|
||||
const courseDeg = vesselCourseDeg(record);
|
||||
// Following the clock, not there yet at the time drawn.
|
||||
visual.seen = seenYet(record);
|
||||
return {
|
||||
id: visual.iconId,
|
||||
// Hidden while the bridge camera rides it, or its model draws it.
|
||||
show: !isCockpitSubject(record) && !visual.modelOwnsVisual,
|
||||
// Hidden while the bridge camera rides it, its model draws it, or it
|
||||
// is not there yet.
|
||||
show: !isCockpitSubject(record) && !visual.modelOwnsVisual && visual.seen,
|
||||
pos: visual.position,
|
||||
image: shipIcon(record, selected),
|
||||
size: [SHIP_ICON_PX, SHIP_ICON_PX],
|
||||
@@ -289,7 +299,7 @@ export function createRendering({
|
||||
state.icons.put({ ...iconItem(record, visual), focus: 1 });
|
||||
if (visual.position)
|
||||
visual.cullPosition = liftedForHorizon(visual.position);
|
||||
visual.visible = isVisible(visual.cullPosition, occluder);
|
||||
visual.visible = isVisible(visual.cullPosition, occluder, record);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -429,7 +439,7 @@ export function createRendering({
|
||||
item.position = getVisual(record).waterline;
|
||||
// An aid to navigation is a fixed mark: it does not turn to a heading.
|
||||
item.headingDeg = item.spec?.fixed ? 0 : vesselCourseDeg(record);
|
||||
item.show = record.id !== state.cockpitSubjectId;
|
||||
item.show = record.id !== state.cockpitSubjectId && seenYet(record);
|
||||
return item;
|
||||
}
|
||||
|
||||
@@ -511,11 +521,11 @@ export function createRendering({
|
||||
modelCandidatesAtMs = -Infinity;
|
||||
}
|
||||
|
||||
// Vessels move only when a poll lands, so the layer does not hold the
|
||||
// continuous render loop. Its passes run in preRender, which idle mode fires
|
||||
// only for rendered frames: a forced pass (new data, selection) requests its
|
||||
// frame, a frame that moved the camera but fell inside the 800 ms cadence
|
||||
// leaves a trailing frame so the view the camera stopped on is always
|
||||
// The layer does not hold the continuous render loop. Its passes run in
|
||||
// preRender, which idle mode fires only for rendered frames: moving vessels
|
||||
// ask for the frame that next moves them visibly (askMotionFrame), a forced
|
||||
// pass (new data, selection) requests its frame, a frame that moved the
|
||||
// camera but fell inside the 800 ms cadence leaves a trailing frame so the view the camera stopped on is always
|
||||
// evaluated, and the continuous hold is kept only while focus emphasis is
|
||||
// animating. The trailing frame is keyed to camera motion alone: the pass's
|
||||
// own label publication requests a frame, and scheduling on that would loop.
|
||||
@@ -523,71 +533,181 @@ export function createRendering({
|
||||
if (state.preRenderRemover || !viewer) return;
|
||||
state.preRenderRemover =
|
||||
findFrame(viewer)?.on('preRender', () => {
|
||||
if (followsClock(vesselState)) followClockPositions();
|
||||
else if (services.placement && state.feed.enabled) followPlacements();
|
||||
if (state.feed.enabled) {
|
||||
if (followsClock(vesselState)) followClockPositions();
|
||||
else if (services.placement) followPlacements();
|
||||
}
|
||||
updateVisibility();
|
||||
if (state.feed.enabled) updateModels();
|
||||
}) ?? null;
|
||||
}
|
||||
|
||||
// As the world clock plays, vessels between two fixes move on along them.
|
||||
let followedAtWallMs = -Infinity;
|
||||
let followedAtMs = null;
|
||||
// As the world clock plays, vessels between two fixes move on along them:
|
||||
// drawn ones every frame, each once per world time, hidden ones (which may
|
||||
// come into view) every HIDDEN_FOLLOW_MS.
|
||||
let hiddenFollowedAtWallMs = -Infinity;
|
||||
function followClockPositions() {
|
||||
const wallMs = performance.now();
|
||||
if (wallMs - followedAtWallMs < FOLLOW_POSITION_MS) return;
|
||||
followedAtWallMs = wallMs;
|
||||
const atMs = vesselState.worldNow();
|
||||
if (atMs === followedAtMs) return;
|
||||
followedAtMs = atMs;
|
||||
const motion = startMotionPass();
|
||||
const moveHidden =
|
||||
motion.wallMs - hiddenFollowedAtWallMs >= HIDDEN_FOLLOW_MS;
|
||||
if (moveHidden) hiddenFollowedAtWallMs = motion.wallMs;
|
||||
for (const record of state.records.all) {
|
||||
if (!record.motion) continue;
|
||||
prepareRecordVisual(record);
|
||||
syncRecordIcon(record);
|
||||
if (!record.motion && !fixesMove(record.fixes)) continue;
|
||||
let visual = visualRecords.get(record);
|
||||
if (!visual?.position) {
|
||||
visual = prepareRecordVisual(record);
|
||||
visual.followedAtMs = atMs;
|
||||
syncRecordIcon(record);
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
visual.followedAtMs === atMs ||
|
||||
(!drawnNow(record, visual) && !moveHidden)
|
||||
)
|
||||
continue;
|
||||
visual.followedAtMs = atMs;
|
||||
const { lat, lon } = vesselPositionAt(record, atMs);
|
||||
// At either end of its fixes a vessel holds still.
|
||||
if (lat === visual.drawnLat && lon === visual.drawnLon) continue;
|
||||
moveDrawnVisual(record, visual, lat, lon, motion);
|
||||
}
|
||||
askMotionFrame(motion);
|
||||
}
|
||||
|
||||
// Live with a host-supplied placement, vessels move on between refreshes:
|
||||
// drawn where it places them at most every FOLLOW_POSITION_MS, with a frame
|
||||
// asked for every PLACEMENT_FRAME_MS while it places any drawn vessel, so
|
||||
// they move while the scene idles.
|
||||
let placedAtWallMs = -Infinity;
|
||||
let placing = false;
|
||||
let placementFrameTimer = null;
|
||||
// drawn where it places them every frame, with the next frame asked for
|
||||
// as soon as a drawn vessel would move visibly, so they move while the
|
||||
// scene idles.
|
||||
function followPlacements() {
|
||||
const wallMs = performance.now();
|
||||
if (wallMs - placedAtWallMs >= FOLLOW_POSITION_MS) {
|
||||
placedAtWallMs = wallMs;
|
||||
placing = false;
|
||||
const nowMs = vesselState._aisRuntime.now();
|
||||
// Drawn vessels only, as a sprite or a model: a hidden one is placed
|
||||
// again when it is drawn.
|
||||
for (const record of state.records.all) {
|
||||
const visual = visualRecords.get(record);
|
||||
if (!visual?.position) continue;
|
||||
const drawn =
|
||||
(visual.visible && !isCockpitSubject(record)) ||
|
||||
visual.modelOwnsVisual;
|
||||
if (!drawn) continue;
|
||||
const placed = services.placement.place(record.id, nowMs);
|
||||
if (placed) placing = true;
|
||||
const { lat, lon } = placed ?? record;
|
||||
if (lat === visual.drawnLat && lon === visual.drawnLon) continue;
|
||||
moveRecordVisual(record, visual, lat, lon);
|
||||
}
|
||||
const nowMs = vesselState._aisRuntime.now();
|
||||
const motion = startMotionPass();
|
||||
// Drawn vessels only, as a sprite or a model: a hidden one is placed
|
||||
// again when it is drawn.
|
||||
for (const record of state.records.all) {
|
||||
const visual = visualRecords.get(record);
|
||||
if (!visual?.position) continue;
|
||||
if (!drawnNow(record, visual)) continue;
|
||||
const placed = services.placement.place(record.id, nowMs);
|
||||
if (placed) motion.moving = true;
|
||||
const { lat, lon } = placed ?? record;
|
||||
if (lat === visual.drawnLat && lon === visual.drawnLon) continue;
|
||||
moveDrawnVisual(record, visual, lat, lon, motion);
|
||||
}
|
||||
if (placing && placementFrameTimer === null)
|
||||
placementFrameTimer = vesselState._aisRuntime.setTimeout(() => {
|
||||
placementFrameTimer = null;
|
||||
if (state.feed.enabled && !followsClock(vesselState))
|
||||
governorRequestRender('ais-vessels-placement');
|
||||
}, PLACEMENT_FRAME_MS);
|
||||
askMotionFrame(motion);
|
||||
}
|
||||
|
||||
// One pass of moving vessels: whether any drawn vessel is moving, how far
|
||||
// the fastest moved (as an angle seen from the camera, radians, and in
|
||||
// metres), the pass's wall time and the wall time since the pass before.
|
||||
let motionPassWallMs = null;
|
||||
const motionPass = {
|
||||
pose: undefined,
|
||||
wallMs: 0,
|
||||
sinceMs: 0,
|
||||
moving: false,
|
||||
angle: 0,
|
||||
metres: 0,
|
||||
};
|
||||
function startMotionPass() {
|
||||
const wallMs = performance.now();
|
||||
// Read once, when a drawn vessel first moves in the pass.
|
||||
motionPass.pose = undefined;
|
||||
motionPass.wallMs = wallMs;
|
||||
motionPass.sinceMs =
|
||||
motionPassWallMs === null ? 0 : wallMs - motionPassWallMs;
|
||||
motionPass.moving = false;
|
||||
motionPass.angle = 0;
|
||||
motionPass.metres = 0;
|
||||
motionPassWallMs = wallMs;
|
||||
return motionPass;
|
||||
}
|
||||
|
||||
/** The camera's pose for a motion pass, read once in it (null without one). */
|
||||
function motionPose(motion) {
|
||||
if (motion.pose === undefined)
|
||||
motion.pose = state.viewer
|
||||
? (findCamera(state.viewer)?.pose() ?? null)
|
||||
: null;
|
||||
return motion.pose;
|
||||
}
|
||||
|
||||
/** Whether a vessel is drawn now: its icon on the camera's side of the
|
||||
* horizon and not the bridge camera's own vessel, or its model. */
|
||||
function drawnNow(record, visual) {
|
||||
return (
|
||||
(visual.visible && !isCockpitSubject(record)) ||
|
||||
Boolean(visual.modelOwnsVisual)
|
||||
);
|
||||
}
|
||||
|
||||
/** Move a drawn visual, noting how far it moved as seen from the camera. */
|
||||
function moveDrawnVisual(record, visual, lat, lon, motion) {
|
||||
const from = copyInto(visual.position, _movedFrom);
|
||||
moveRecordVisual(record, visual, lat, lon);
|
||||
if (!drawnNow(record, visual)) return;
|
||||
motion.moving = true;
|
||||
const metres = distance(from, visual.position);
|
||||
motion.metres = Math.max(motion.metres, metres);
|
||||
const eye = motionPose(motion)?.position;
|
||||
if (!eye) return;
|
||||
const range = distance(eye, from);
|
||||
if (range > 0) motion.angle = Math.max(motion.angle, metres / range);
|
||||
}
|
||||
|
||||
/** How far the fastest drawn vessel moved on screen in a pass (pixels):
|
||||
* through the perspective view's angle, or across the 2D view's width
|
||||
* (an orthographic projection's, `2 / m00` metres). */
|
||||
function movedPixels(motion) {
|
||||
const canvas = state.viewer ? canvasOf(state.viewer) : null;
|
||||
const pose = motion.pose;
|
||||
if (!canvas || !pose) return 0;
|
||||
if (pose.fovy > 0 && canvas.clientHeight > 0)
|
||||
return (
|
||||
(motion.angle * canvas.clientHeight) / (2 * Math.tan(pose.fovy / 2))
|
||||
);
|
||||
if (pose.fovy > 0 || !(canvas.clientWidth > 0)) return 0;
|
||||
let width = NaN;
|
||||
try {
|
||||
width = 2 / findCamera(state.viewer).matrices().projection[0];
|
||||
} catch {
|
||||
return 0;
|
||||
}
|
||||
return width > 0 ? (motion.metres * canvas.clientWidth) / width : 0;
|
||||
}
|
||||
|
||||
// The timer for the next vessel frame while one is asked for, and when it
|
||||
// is due (the AIS runtime's clock).
|
||||
let placementFrameTimer = null;
|
||||
let placementFrameDueMs = 0;
|
||||
/**
|
||||
* While a drawn vessel moves, ask for the next frame when the fastest one
|
||||
* will have moved VESSEL_STEP_PX on screen at the speed this pass saw, and
|
||||
* at most PLACEMENT_FRAME_MS away.
|
||||
*/
|
||||
function askMotionFrame(motion) {
|
||||
if (!motion.moving) return;
|
||||
let waitMs = PLACEMENT_FRAME_MS;
|
||||
const movedPx = movedPixels(motion);
|
||||
if (movedPx > 0 && motion.sinceMs > 0)
|
||||
waitMs = Math.min(waitMs, (VESSEL_STEP_PX * motion.sinceMs) / movedPx);
|
||||
// A frame already asked for no later than this one stands.
|
||||
const runtime = vesselState._aisRuntime;
|
||||
const dueMs = runtime.now() + waitMs;
|
||||
if (placementFrameTimer !== null) {
|
||||
if (placementFrameDueMs <= dueMs) return;
|
||||
runtime.clearTimeout(placementFrameTimer);
|
||||
}
|
||||
placementFrameDueMs = dueMs;
|
||||
placementFrameTimer = runtime.setTimeout(() => {
|
||||
placementFrameTimer = null;
|
||||
if (state.feed.enabled) governorRequestRender('ais-vessels-placement');
|
||||
}, waitMs);
|
||||
}
|
||||
|
||||
/** Stop asking for placement frames (the layer turned off or went away). */
|
||||
function cancelPlacementFrame() {
|
||||
placedAtWallMs = -Infinity;
|
||||
placing = false;
|
||||
motionPassWallMs = null;
|
||||
if (placementFrameTimer === null) return;
|
||||
vesselState._aisRuntime.clearTimeout(placementFrameTimer);
|
||||
placementFrameTimer = null;
|
||||
@@ -629,14 +749,21 @@ export function createRendering({
|
||||
(regularPass || focusPass || !state.trailingVisibilityTimer)
|
||||
? findCamera(state.viewer)?.pose()
|
||||
: null;
|
||||
// Following the clock, the time drawn decides which vessels are there
|
||||
// yet: a time that moved since the last pass (a seek while paused) gets
|
||||
// its pass too.
|
||||
const worldMs = followsClock(vesselState) ? vesselState.worldNow() : null;
|
||||
if (
|
||||
!regularPass &&
|
||||
pose &&
|
||||
poseSignature(pose) !== vesselState._lastCamPoseSig
|
||||
((pose && poseSignature(pose) !== vesselState._lastCamPoseSig) ||
|
||||
worldMs !== state.visibilityWorldMs)
|
||||
)
|
||||
scheduleTrailingVisibilityPass(now);
|
||||
if (!regularPass && !focusPass) return;
|
||||
if (regularPass) state.lastVisibilityUpdate = now;
|
||||
if (regularPass) {
|
||||
state.lastVisibilityUpdate = now;
|
||||
state.visibilityWorldMs = worldMs;
|
||||
}
|
||||
if (focusPass) state.lastFocusUpdate = now;
|
||||
if (!state.records.all.length) {
|
||||
// No records — flush any lingering card entries (vanished-feed case).
|
||||
@@ -665,7 +792,9 @@ export function createRendering({
|
||||
const labelCandidates = [];
|
||||
for (const record of state.records.all) {
|
||||
const visual = getVisual(record);
|
||||
visual.visible = isVisible(visual.cullPosition, occluder);
|
||||
visual.visible = isVisible(visual.cullPosition, occluder, record);
|
||||
// A vessel the time drawn reaches (or leaves) shows (or hides) its icon.
|
||||
if (visual.seen !== seenYet(record)) syncRecordIcon(record);
|
||||
if (visual.visible) labelCandidates.push(record);
|
||||
}
|
||||
updateClusteredLabels(labelCandidates);
|
||||
@@ -760,11 +889,23 @@ export function createRendering({
|
||||
return pose?.position ? horizonFrom(pose.position) : null;
|
||||
}
|
||||
|
||||
function isVisible(cullPosition, occluder) {
|
||||
function isVisible(cullPosition, occluder, record) {
|
||||
if (record && !seenYet(record)) return false;
|
||||
if (!cullPosition || !occluder) return true;
|
||||
return occluder.isPointVisible(cullPosition);
|
||||
}
|
||||
|
||||
/** Whether a vessel is there at the time drawn: following the clock, not
|
||||
* more than TIMELINE_UNSEEN_MS before its first fix. */
|
||||
function seenYet(record) {
|
||||
const first = record.fixes?.[0];
|
||||
return (
|
||||
!first ||
|
||||
!followsClock(vesselState) ||
|
||||
vesselState.worldNow() >= first.atMs - TIMELINE_UNSEEN_MS
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Source selector for the shared world-overlay pipeline: the
|
||||
* grid declutter picks which vessels get cards — one winner per screen-space
|
||||
@@ -957,6 +1098,7 @@ export function createRendering({
|
||||
cancelTrailingVisibilityPass,
|
||||
cancelPlacementFrame,
|
||||
followPlacements,
|
||||
followClockPositions,
|
||||
applyVesselFocusDeemphasis,
|
||||
makeOccluder,
|
||||
isVisible,
|
||||
|
||||
@@ -84,6 +84,8 @@ export function createVesselState({ source, services }) {
|
||||
/** Pending timer that renders one frame after a cadence-skipped pass. */
|
||||
trailingVisibilityTimer: null,
|
||||
lastVisibilityUpdate: 0,
|
||||
/** Following the clock, the world time the last visibility pass drew. */
|
||||
visibilityWorldMs: null,
|
||||
lastFocusUpdate: 0,
|
||||
/** Sprites whose animated emphasis remains outside the 1.0 deadband. */
|
||||
activeFocusCount: 0,
|
||||
|
||||
@@ -170,12 +170,32 @@ export function createTesting({
|
||||
return components.rendering.prepareRecordVisual(record);
|
||||
}
|
||||
|
||||
/** A record's visual (the record itself before it has one). Test-only seam. */
|
||||
|
||||
function _vesselVisualForTest(record) {
|
||||
return components.rendering.getVisual(record);
|
||||
}
|
||||
|
||||
/** Run one live placement pass (a host-supplied placement). Test-only seam. */
|
||||
|
||||
function _runPlacementPassForTest() {
|
||||
components.rendering.followPlacements();
|
||||
}
|
||||
|
||||
/** Run one world-clock pass at world time `atMs`. Test-only seam. */
|
||||
|
||||
function _runClockPassForTest(atMs) {
|
||||
vesselState.worldNow = () => atMs;
|
||||
components.rendering.followClockPositions();
|
||||
}
|
||||
|
||||
/** Follow the world clock, standing at world time `atMs`. Test-only seam. */
|
||||
|
||||
function _followClockForTest(atMs) {
|
||||
vesselState.timeline = { active: () => true };
|
||||
vesselState.worldNow = () => atMs;
|
||||
}
|
||||
|
||||
/** True while a trailing visibility frame is scheduled. Test-only seam. */
|
||||
|
||||
function _hasTrailingVisibilityPassForTest() {
|
||||
@@ -204,6 +224,7 @@ export function createTesting({
|
||||
return {
|
||||
_bindVesselInteractionForTest,
|
||||
_setVesselStateForTest,
|
||||
_followClockForTest,
|
||||
_setVesselOverlayHostForTest,
|
||||
_updateVesselCardsForTest,
|
||||
_reconcileVesselsForTest,
|
||||
@@ -216,7 +237,9 @@ export function createTesting({
|
||||
_vesselRecordForTest,
|
||||
_runVisibilityPassForTest,
|
||||
_prepareVesselVisualForTest,
|
||||
_vesselVisualForTest,
|
||||
_runPlacementPassForTest,
|
||||
_runClockPassForTest,
|
||||
_hasTrailingVisibilityPassForTest,
|
||||
};
|
||||
}
|
||||
@@ -66,11 +66,10 @@ export function createTracking({
|
||||
continue;
|
||||
visual.position = components.rendering.anchorPosition(record);
|
||||
components.rendering.syncRecordIcon(record);
|
||||
// A placed vessel is drawn again at its placement on the next pass.
|
||||
if (services.placement) {
|
||||
visual.drawnLat = NaN;
|
||||
visual.drawnLon = NaN;
|
||||
}
|
||||
// A moving vessel is drawn again where it is on the next pass.
|
||||
visual.drawnLat = NaN;
|
||||
visual.drawnLon = NaN;
|
||||
visual.followedAtMs = undefined;
|
||||
}
|
||||
// The rows moved with no poll behind them: draw the frame that shows it.
|
||||
governorRequestRender('ais-vessels-refloor');
|
||||
|
||||
Reference in new issue
Block a user