mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-18 01:02:23 +00:00
Let a host application place live contacts
createApplicationCatalog takes an optional placement provider. Its layer(kind) hands Flights, Military Flights, AIS Vessels, Transit and Rail a place(id, wallMs), and optionally observe(id, fix) and forget(id). While live, each layer tells it every fix it accepts and draws a contact where it places it: position, height (the layer's own when none is given), course and speed. A null answer, a provider that throws, a kind it leaves out, or no provider at all keeps the layer's own motion unchanged, and following the world clock the layers never ask it. A transit vehicle or train it holds still lets the render loop idle and is asked again each second; vessels ask for frames only while it places one, and stop when the layer turns off. Tests show the layers draw and reconcile exactly as before without a provider and with one that places nothing, draw where a provider places them, and idle while it holds them still.
This commit is contained in:
1 parent
e53cfef1d1
commit
6f830a2e70
46 files changed
+1417
-34
No files matched your search
@@ -31,6 +31,7 @@ export function createVesselLayer({ source, services, options = {} } = {}) {
|
||||
parts.queries = createQueries(context);
|
||||
parts.snapshots = createVesselSnapshotRenderer({
|
||||
state: vesselState.state,
|
||||
placement: services?.placement ?? null,
|
||||
records: vesselState.state.records,
|
||||
rendering: parts.rendering,
|
||||
tracking: parts.tracking,
|
||||
|
||||
@@ -115,6 +115,8 @@ export function createLifecycle({
|
||||
state.feed.firstConnectTimer = null;
|
||||
state.feed.abort = null;
|
||||
state.billboardCollection = null;
|
||||
if (services.placement)
|
||||
for (const id of state.records.byId.keys()) services.placement.forget(id);
|
||||
state.records.all = [];
|
||||
state.records.byId = new Map();
|
||||
state.records.unkeyed = [];
|
||||
@@ -211,6 +213,7 @@ export function createLifecycle({
|
||||
invalidateAisSession();
|
||||
releaseContinuousRender('ais-vessels');
|
||||
components.rendering.cancelTrailingVisibilityPass();
|
||||
components.rendering.cancelPlacementFrame();
|
||||
unregisterPickOwner('ais-live-vessels');
|
||||
components.rendering.setVisible(false);
|
||||
vesselState._vesselOverlayHost.clearSource(VESSEL_OVERLAY_SOURCE_ID);
|
||||
@@ -232,6 +235,7 @@ export function createLifecycle({
|
||||
invalidateAisSession();
|
||||
releaseContinuousRender('ais-vessels'); // direct-destroy path (perf wave 2 fix)
|
||||
components.rendering.cancelTrailingVisibilityPass();
|
||||
components.rendering.cancelPlacementFrame();
|
||||
if (state.feed.abort) state.feed.abort.abort();
|
||||
unregisterPickOwner('ais-live-vessels');
|
||||
components.selection.clearVesselInspection();
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { VesselRecords } from './records.js';
|
||||
import { createVesselSnapshotRenderer } from './snapshotRenderer.js';
|
||||
import { layerPlacement } from '../../data/placementProvider.js';
|
||||
|
||||
const row = (id, fields = {}) => ({
|
||||
id,
|
||||
mmsi: id,
|
||||
lat: 51.93,
|
||||
lon: 4.05,
|
||||
speed: 10,
|
||||
course: 90,
|
||||
heading: 92,
|
||||
last_position_epoch: 1_000,
|
||||
...fields,
|
||||
});
|
||||
|
||||
function harness({ placement = null, following = false } = {}) {
|
||||
const visuals = [];
|
||||
const state = { selectedRecord: null, trailId: null };
|
||||
const records = new VesselRecords({ now: () => 5_000_000 });
|
||||
const rendering = {
|
||||
ensureCollections() {},
|
||||
makeOccluder: () => null,
|
||||
renderRowLimit: () => Infinity,
|
||||
prepareRecordVisual(record) {
|
||||
visuals.push(record.id);
|
||||
return { billboard: null };
|
||||
},
|
||||
addRecordPrimitives() {},
|
||||
removeRecordPrimitives() {},
|
||||
shipIcon: () => 'icon',
|
||||
shipScale: () => 1,
|
||||
updateVisibility() {},
|
||||
};
|
||||
const renderer = createVesselSnapshotRenderer({
|
||||
state,
|
||||
placement,
|
||||
records,
|
||||
rendering,
|
||||
tracking: {},
|
||||
selection: {},
|
||||
cards: {},
|
||||
followsClock: () => following,
|
||||
});
|
||||
return {
|
||||
visuals,
|
||||
reconcile: (rows, options) => renderer.reconcileVessels({}, rows, options),
|
||||
};
|
||||
}
|
||||
|
||||
function recording() {
|
||||
const told = [];
|
||||
const forgotten = [];
|
||||
const placement = layerPlacement(
|
||||
{
|
||||
layer: () => ({
|
||||
observe: (id, fix) => told.push([id, fix]),
|
||||
place: () => null,
|
||||
forget: (id) => forgotten.push(id),
|
||||
}),
|
||||
},
|
||||
'vessels',
|
||||
);
|
||||
return { told, forgotten, placement };
|
||||
}
|
||||
|
||||
test('vessels reconcile as before with no placement', () => {
|
||||
const own = harness();
|
||||
own.reconcile([row('a'), row('b')]);
|
||||
own.reconcile([row('a', { lat: 52, last_position_epoch: 1_060 })]);
|
||||
const { placement } = recording();
|
||||
const placed = harness({ placement });
|
||||
placed.reconcile([row('a'), row('b')]);
|
||||
placed.reconcile([row('a', { lat: 52, last_position_epoch: 1_060 })]);
|
||||
assert.deepEqual(placed.visuals, own.visuals);
|
||||
});
|
||||
|
||||
test('each live vessel fix is told to the placement, in m/s, and a vessel that leaves is forgotten', () => {
|
||||
const { told, forgotten, placement } = recording();
|
||||
const probe = harness({ placement });
|
||||
probe.reconcile([row('a'), row('b', { speed: null, course: null })]);
|
||||
probe.reconcile([row('a', { lat: 52, last_position_epoch: 1_060 })]);
|
||||
assert.deepEqual(
|
||||
told.map(([id, fix]) => [id, fix.timeMs, fix.lat]),
|
||||
[
|
||||
['a', 1_000_000, 51.93],
|
||||
['b', 1_000_000, 51.93],
|
||||
['a', 1_060_000, 52],
|
||||
],
|
||||
);
|
||||
assert.ok(Math.abs(told[0][1].speedMps - 10 * (1852 / 3600)) < 1e-12);
|
||||
assert.equal(told[0][1].courseDeg, 90);
|
||||
assert.equal(told[0][1].headingDeg, 92);
|
||||
assert.ok(Number.isNaN(told[1][1].speedMps));
|
||||
assert.ok(Number.isNaN(told[1][1].courseDeg));
|
||||
assert.deepEqual(forgotten, ['b']);
|
||||
});
|
||||
|
||||
test('following the world clock, or rows at another time, tell the placement nothing', () => {
|
||||
const { told, placement } = recording();
|
||||
harness({ placement, following: true }).reconcile([row('a')]);
|
||||
harness({ placement }).reconcile([row('a')], { replace: true });
|
||||
assert.deepEqual(told, []);
|
||||
});
|
||||
@@ -2,6 +2,7 @@ import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createVesselLayer } from './index.js';
|
||||
import { VISIBILITY_UPDATE_MS } from './policy.js';
|
||||
import { layerPlacement } from '../../data/placementProvider.js';
|
||||
|
||||
// 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
|
||||
@@ -9,7 +10,7 @@ import { VISIBILITY_UPDATE_MS } from './policy.js';
|
||||
|
||||
const noop = () => {};
|
||||
|
||||
function setup() {
|
||||
function setup({ placement = null } = {}) {
|
||||
let clock = 10_000;
|
||||
const requests = [];
|
||||
const holds = new Set();
|
||||
@@ -46,6 +47,7 @@ function setup() {
|
||||
releaseContinuousRender: (id) => holds.delete(id),
|
||||
governorRequestRender: (reason) => requests.push(reason),
|
||||
},
|
||||
...(placement ? { placement: layerPlacement(placement, 'vessels') } : {}),
|
||||
},
|
||||
});
|
||||
const camera = {
|
||||
@@ -136,3 +138,61 @@ test('destroy cancels a pending trailing frame', (t) => {
|
||||
advance(VISIBILITY_UPDATE_MS);
|
||||
assert.deepEqual(requests, []);
|
||||
});
|
||||
|
||||
test('a placement that places nothing asks for no frames; one that places a drawn vessel moves it in place', (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 = null;
|
||||
const { layer, camera, requests, timers, advance } = setup({
|
||||
placement: { layer: () => ({ place: () => spot }) },
|
||||
});
|
||||
const record = {
|
||||
mmsi: '111',
|
||||
lat: 51.93,
|
||||
lon: 4.05,
|
||||
billboard: { show: true, position: null },
|
||||
};
|
||||
layer.testing._setVesselStateForTest({
|
||||
viewer: { camera, scene: {} },
|
||||
records: [record],
|
||||
});
|
||||
const visual = layer.testing._prepareVesselVisualForTest(record);
|
||||
const position = visual.position;
|
||||
const before = { ...position };
|
||||
|
||||
// Nothing placed: no frame is asked for, and the vessel stays put.
|
||||
for (let i = 0; i < 8; i += 1) {
|
||||
mono += 250;
|
||||
layer.testing._runPlacementPassForTest();
|
||||
advance(250);
|
||||
}
|
||||
assert.equal(timers.size, 0);
|
||||
assert.deepEqual(requests, []);
|
||||
assert.deepEqual({ ...visual.position }, before);
|
||||
|
||||
// Placed: moved in place, no new positions, and a frame asked for.
|
||||
spot = { lat: 51.94, lon: 4.05 };
|
||||
mono += 250;
|
||||
layer.testing._runPlacementPassForTest();
|
||||
assert.equal(visual.position, position, 'written in place');
|
||||
assert.notDeepEqual({ ...visual.position }, before);
|
||||
assert.equal(record.billboard.position, position);
|
||||
assert.equal(timers.size, 1);
|
||||
advance(1_000);
|
||||
assert.deepEqual(requests, ['ais-vessels-placement']);
|
||||
|
||||
// Turning the layer off drops the pending frame.
|
||||
mono += 250;
|
||||
layer.testing._runPlacementPassForTest();
|
||||
assert.equal(timers.size, 1);
|
||||
layer.destroy();
|
||||
assert.equal(timers.size, 0);
|
||||
advance(1_000);
|
||||
assert.deepEqual(requests, ['ais-vessels-placement']);
|
||||
});
|
||||
@@ -33,6 +33,8 @@ 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. */
|
||||
const PLACEMENT_FRAME_MS = 1000;
|
||||
|
||||
/** How often the vessels near enough for a model are gathered again (wall ms). */
|
||||
const MODEL_CANDIDATES_MS = 500;
|
||||
@@ -58,10 +60,12 @@ export function createRendering({
|
||||
visual = { billboard: record.billboard || null };
|
||||
visualRecords.set(record, visual);
|
||||
}
|
||||
// Following the world clock, a vessel is drawn where it was at the time.
|
||||
// Following the world clock, a vessel is drawn where it was at the time;
|
||||
// live, where a host-supplied placement draws it, if one does.
|
||||
const { lat, lon } = followsClock(vesselState)
|
||||
? vesselPositionAt(record, vesselState.worldNow())
|
||||
: record;
|
||||
: (services.placement?.place(record.id, vesselState._aisRuntime.now()) ??
|
||||
record);
|
||||
const heightM = components.queries.vesselDatumHeightM(
|
||||
components.tracking.currentGeoidN(lat, lon),
|
||||
VESSEL_LIFT_M,
|
||||
@@ -78,9 +82,50 @@ export function createRendering({
|
||||
visual.position,
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
if (services.placement) {
|
||||
visual.drawnLat = lat;
|
||||
visual.drawnLon = lon;
|
||||
}
|
||||
return visual;
|
||||
}
|
||||
|
||||
/**
|
||||
* Move a prepared vessel visual to `lat`/`lon`, writing into its own
|
||||
* positions rather than allocating new ones: the placement pass runs for
|
||||
* every drawn vessel several times a second.
|
||||
*/
|
||||
function moveRecordVisual(visual, lat, lon) {
|
||||
const heightM = components.queries.vesselDatumHeightM(
|
||||
components.tracking.currentGeoidN(lat, lon),
|
||||
VESSEL_LIFT_M,
|
||||
);
|
||||
const ellipsoid = Cesium.Ellipsoid.WGS84;
|
||||
Cesium.Cartesian3.fromDegrees(
|
||||
lon,
|
||||
lat,
|
||||
heightM,
|
||||
ellipsoid,
|
||||
visual.position,
|
||||
);
|
||||
Cesium.Cartesian3.fromDegrees(
|
||||
lon,
|
||||
lat,
|
||||
heightM - VESSEL_LIFT_M,
|
||||
ellipsoid,
|
||||
visual.waterline,
|
||||
);
|
||||
Cesium.Cartesian3.fromDegrees(
|
||||
lon,
|
||||
lat,
|
||||
0,
|
||||
ellipsoid,
|
||||
visual.surfacePosition,
|
||||
);
|
||||
ellipsoid.geodeticSurfaceNormal(visual.position, visual.normal);
|
||||
visual.drawnLat = lat;
|
||||
visual.drawnLon = lon;
|
||||
}
|
||||
|
||||
function resetRecordVisuals() {
|
||||
visualRecords = new WeakMap();
|
||||
}
|
||||
@@ -391,6 +436,7 @@ export function createRendering({
|
||||
if (state.preRenderRemover || !viewer) return;
|
||||
state.preRenderRemover = viewer.scene.preRender.addEventListener(() => {
|
||||
if (followsClock(vesselState)) followClockPositions();
|
||||
else if (services.placement && state.feed.enabled) followPlacements();
|
||||
updateVisibility();
|
||||
if (state.feed.enabled) updateModels();
|
||||
});
|
||||
@@ -413,6 +459,50 @@ export function createRendering({
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
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;
|
||||
if (!visual.billboard?.show && !visual.modelOwnsVisual) 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(visual, lat, lon);
|
||||
if (visual.billboard) visual.billboard.position = visual.position;
|
||||
}
|
||||
}
|
||||
if (placing && placementFrameTimer === null)
|
||||
placementFrameTimer = vesselState._aisRuntime.setTimeout(() => {
|
||||
placementFrameTimer = null;
|
||||
if (state.feed.enabled && !followsClock(vesselState))
|
||||
governorRequestRender('ais-vessels-placement');
|
||||
}, PLACEMENT_FRAME_MS);
|
||||
}
|
||||
|
||||
/** Stop asking for placement frames (the layer turned off or went away). */
|
||||
function cancelPlacementFrame() {
|
||||
placedAtWallMs = -Infinity;
|
||||
placing = false;
|
||||
if (placementFrameTimer === null) return;
|
||||
vesselState._aisRuntime.clearTimeout(placementFrameTimer);
|
||||
placementFrameTimer = null;
|
||||
}
|
||||
|
||||
function scheduleTrailingVisibilityPass(now) {
|
||||
if (state.trailingVisibilityTimer) return;
|
||||
const waitMs = Math.max(
|
||||
@@ -787,6 +877,8 @@ export function createRendering({
|
||||
installRuntime,
|
||||
updateVisibility,
|
||||
cancelTrailingVisibilityPass,
|
||||
cancelPlacementFrame,
|
||||
followPlacements,
|
||||
applyVesselFocusDeemphasis,
|
||||
makeOccluder,
|
||||
isVisible,
|
||||
|
||||
@@ -1,6 +1,30 @@
|
||||
/** Connect portable vessel reconciliation to scene and selection operations. */
|
||||
const KNOT_MPS = 1852 / 3600;
|
||||
const finiteOrNaN = (value) =>
|
||||
typeof value === 'number' && Number.isFinite(value) ? value : NaN;
|
||||
|
||||
/**
|
||||
* A vessel record's fix as a host-supplied placement takes it: its own time
|
||||
* (when it was received, if the row gives none), position, speed in m/s
|
||||
* (rows give knots), course and heading.
|
||||
*/
|
||||
function placementFix(record) {
|
||||
const epoch = record.lastPositionEpoch;
|
||||
return {
|
||||
timeMs: Number.isFinite(epoch) ? epoch * 1000 : record.receivedAtMs,
|
||||
lat: record.lat,
|
||||
lon: record.lon,
|
||||
altitudeM: NaN,
|
||||
speedMps: Number.isFinite(record.speed) ? record.speed * KNOT_MPS : NaN,
|
||||
courseDeg: finiteOrNaN(record.course),
|
||||
headingDeg: finiteOrNaN(record.heading),
|
||||
};
|
||||
}
|
||||
|
||||
/** Connect portable vessel reconciliation to scene and selection operations.
|
||||
* `placement`, a host-supplied placement, is told each live fix. */
|
||||
export function createVesselSnapshotRenderer({
|
||||
state,
|
||||
placement = null,
|
||||
records,
|
||||
rendering,
|
||||
tracking,
|
||||
@@ -17,6 +41,13 @@ export function createVesselSnapshotRenderer({
|
||||
rendering.ensureCollections(viewer);
|
||||
const occluder = rendering.makeOccluder();
|
||||
const follow = followsClock();
|
||||
// Live fixes only: following the clock, the layer places every vessel.
|
||||
const observe =
|
||||
placement && !follow && !replace
|
||||
? (record) => {
|
||||
if (record.id) placement.observe(record.id, placementFix(record));
|
||||
}
|
||||
: () => {};
|
||||
records.reconcile(
|
||||
rows,
|
||||
{
|
||||
@@ -28,6 +59,7 @@ export function createVesselSnapshotRenderer({
|
||||
},
|
||||
{
|
||||
add(record) {
|
||||
observe(record);
|
||||
rendering.prepareRecordVisual(record);
|
||||
rendering.addRecordPrimitives(record, occluder);
|
||||
},
|
||||
@@ -35,6 +67,7 @@ export function createVesselSnapshotRenderer({
|
||||
return rendering.shipIcon(record, record === state.selectedRecord);
|
||||
},
|
||||
updated(record, prevIcon) {
|
||||
observe(record);
|
||||
const selected = record === state.selectedRecord;
|
||||
const visual = rendering.prepareRecordVisual(record);
|
||||
if (visual.billboard) {
|
||||
@@ -56,6 +89,7 @@ export function createVesselSnapshotRenderer({
|
||||
rendering.removeRecordPrimitives(record);
|
||||
},
|
||||
removed(id) {
|
||||
placement?.forget(id);
|
||||
if (state.trailId === id) tracking.clearSelectedVesselTrail();
|
||||
},
|
||||
staleSelected(record) {
|
||||
|
||||
@@ -162,6 +162,18 @@ export function createTesting({
|
||||
components.rendering.updateVisibility(force);
|
||||
}
|
||||
|
||||
/** Draw a record's visual where it stands now. Test-only seam. */
|
||||
|
||||
function _prepareVesselVisualForTest(record) {
|
||||
return components.rendering.prepareRecordVisual(record);
|
||||
}
|
||||
|
||||
/** Run one live placement pass (a host-supplied placement). Test-only seam. */
|
||||
|
||||
function _runPlacementPassForTest() {
|
||||
components.rendering.followPlacements();
|
||||
}
|
||||
|
||||
/** True while a trailing visibility frame is scheduled. Test-only seam. */
|
||||
|
||||
function _hasTrailingVisibilityPassForTest() {
|
||||
@@ -194,6 +206,8 @@ export function createTesting({
|
||||
_getVesselFeedStateForTest,
|
||||
_getVesselStateForTest,
|
||||
_runVisibilityPassForTest,
|
||||
_prepareVesselVisualForTest,
|
||||
_runPlacementPassForTest,
|
||||
_hasTrailingVisibilityPassForTest,
|
||||
};
|
||||
}
|
||||
@@ -73,6 +73,11 @@ export function createTracking({
|
||||
heightM,
|
||||
);
|
||||
if (visual.billboard) visual.billboard.position = visual.position;
|
||||
// A placed vessel is drawn again at its placement on the next pass.
|
||||
if (services.placement) {
|
||||
visual.drawnLat = NaN;
|
||||
visual.drawnLon = NaN;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user