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.
222 lines
7.3 KiB
JavaScript
222 lines
7.3 KiB
JavaScript
import { fromDegrees } from '../../geo/ellipsoid.js';
|
|
import { distance } from '../../geo/vector.js';
|
|
import {
|
|
TRAIL_HEIGHT_M,
|
|
TRAIL_COLOR,
|
|
TRAIL_MAX_POINTS,
|
|
TRAIL_MIN_MOVE_M,
|
|
followsClock,
|
|
} from './policy.js';
|
|
|
|
export function createTracking({
|
|
vesselState,
|
|
services,
|
|
parts: components,
|
|
layer,
|
|
options,
|
|
}) {
|
|
const { state } = vesselState;
|
|
const { geoidHeight } = services.geoid;
|
|
const { createTrail } = services.trails;
|
|
const { governorRequestRender } = services.render;
|
|
|
|
/**
|
|
* Geoid undulation N at (lat, lon), or null until the grid has loaded.
|
|
* @param {number} lat
|
|
* @param {number} lon
|
|
* @returns {number|null}
|
|
*/
|
|
|
|
function currentGeoidN(lat, lon) {
|
|
return vesselState._geoidReady ? geoidHeight(lat, lon) : null;
|
|
}
|
|
|
|
/**
|
|
* Build a slightly lifted trail vertex for a vessel record — raised
|
|
* TRAIL_HEIGHT_M above the sea surface (geoid, same datum as the anchor)
|
|
* to avoid z-fighting.
|
|
* @param {Object} record - Vessel record with lat/lon.
|
|
* @returns {{x: number, y: number, z: number}|null} Lifted ECEF position, or null without a fix.
|
|
*/
|
|
|
|
function vesselTrailPosition(record) {
|
|
if (!Number.isFinite(record?.lat) || !Number.isFinite(record?.lon))
|
|
return null;
|
|
const heightM = components.queries.vesselDatumHeightM(
|
|
currentGeoidN(record.lat, record.lon),
|
|
TRAIL_HEIGHT_M,
|
|
);
|
|
return fromDegrees(record.lon, record.lat, heightM);
|
|
}
|
|
|
|
/**
|
|
* One-shot datum re-lift when the geoid grid warms mid-session: the first
|
|
* refresh can land before ensureGeoidReady() resolves (anchors at N = 0) and
|
|
* the next refresh is up to REFRESH_MS out — re-derive every record's
|
|
* position in place so chevrons/labels snap to the sea surface as soon as N
|
|
* is known. (A selected-vessel trail cannot exist that early — selection
|
|
* needs a rendered pick — so trail vertices are not revisited.)
|
|
*/
|
|
|
|
function refloorVesselRecords() {
|
|
if (!state.records.all.length) return;
|
|
for (const record of state.records.all) {
|
|
const visual = components.rendering.getVisual(record);
|
|
if (!Number.isFinite(record.lat) || !Number.isFinite(record.lon))
|
|
continue;
|
|
visual.position = components.rendering.anchorPosition(record);
|
|
components.rendering.syncRecordIcon(record);
|
|
// 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');
|
|
}
|
|
|
|
/**
|
|
* Start (or restart) the selected vessel's trail: seed with the current
|
|
* position, render, then fire-and-forget the server ring-buffer backfill.
|
|
* @param {Object} record - Freshly selected vessel record.
|
|
*/
|
|
|
|
function startSelectedVesselTrail(record) {
|
|
state.trailAbort?.abort();
|
|
state.trailAbort = new AbortController();
|
|
state.trailBackfillToken += 1;
|
|
state.trailId = record.id;
|
|
state.trailPositions = [];
|
|
const current = vesselTrailPosition(record);
|
|
if (current) state.trailPositions.push(current);
|
|
if (!state.trail && state.viewer) {
|
|
state.trail = createTrail(state.viewer, {
|
|
color: TRAIL_COLOR,
|
|
width: 2.5,
|
|
});
|
|
}
|
|
if (state.trail) state.trail.setPositions(state.trailPositions);
|
|
backfillVesselTrail(record.id, state.trailBackfillToken, record.reference);
|
|
}
|
|
|
|
/**
|
|
* Fire-and-forget backfill from the server-side per-vessel ring buffer
|
|
* (PRD F3 — "recent path" since server boot, not voyage history). Older
|
|
* samples are spliced AHEAD of the live accumulation, capped at
|
|
* TRAIL_MAX_POINTS (newest kept). Any failure (404/timeout/malformed)
|
|
* silently keeps the live-only trail.
|
|
* @param {string} id - Key of the selected vessel.
|
|
* @param {number} token - Backfill token captured at request time.
|
|
* @returns {Promise<void>}
|
|
*/
|
|
|
|
async function backfillVesselTrail(id, token, reference = id) {
|
|
const owner = state.trailAbort;
|
|
// Following the world clock, the track ends at the world's time.
|
|
const atMs = followsClock(vesselState) ? vesselState.worldNow() : undefined;
|
|
const signal = owner
|
|
? AbortSignal.any([owner.signal, AbortSignal.timeout(8000)])
|
|
: AbortSignal.timeout(8000);
|
|
let samples = null;
|
|
try {
|
|
const track = await vesselState._source.getTrack?.(
|
|
reference,
|
|
atMs === undefined ? { signal } : { signal, atMs },
|
|
);
|
|
samples = track?.records ?? null;
|
|
} catch {
|
|
return; // silent — keep the live-accumulated trail
|
|
}
|
|
if (
|
|
signal.aborted ||
|
|
owner !== state.trailAbort ||
|
|
!samples ||
|
|
token !== state.trailBackfillToken
|
|
)
|
|
return;
|
|
if (state.trailId !== id) return;
|
|
|
|
const older = [];
|
|
for (const sample of samples) {
|
|
const lat = Number(sample?.latitude);
|
|
const lon = Number(sample?.longitude);
|
|
if (!Number.isFinite(lat) || !Number.isFinite(lon)) continue;
|
|
// Following the clock, an undated sample may be later than the time.
|
|
if (
|
|
atMs !== undefined &&
|
|
!(Number.isFinite(sample?.observedAtMs) && sample.observedAtMs <= atMs)
|
|
)
|
|
continue;
|
|
// Per-sample N (≤ TRAIL_MAX_POINTS lookups) — same sea-surface datum as
|
|
// the live vertices so the spliced trail is height-continuous.
|
|
const heightM = components.queries.vesselDatumHeightM(
|
|
currentGeoidN(lat, lon),
|
|
TRAIL_HEIGHT_M,
|
|
);
|
|
older.push(fromDegrees(lon, lat, heightM));
|
|
}
|
|
if (!older.length) return;
|
|
|
|
state.trailPositions = older.concat(state.trailPositions);
|
|
if (state.trailPositions.length > TRAIL_MAX_POINTS) {
|
|
state.trailPositions = state.trailPositions.slice(
|
|
state.trailPositions.length - TRAIL_MAX_POINTS,
|
|
);
|
|
}
|
|
if (state.trail) state.trail.setPositions(state.trailPositions);
|
|
}
|
|
|
|
/**
|
|
* Append the selected vessel's refreshed position to its trail when it has
|
|
* moved more than TRAIL_MIN_MOVE_M from the last trail vertex.
|
|
* @param {Object} record - Selected vessel record after an in-place update.
|
|
*/
|
|
|
|
function appendSelectedVesselTrailFix(record) {
|
|
if (!state.trail) return;
|
|
const next = vesselTrailPosition(record);
|
|
if (!next) return;
|
|
const last = state.trailPositions[state.trailPositions.length - 1];
|
|
if (last && distance(last, next) <= TRAIL_MIN_MOVE_M) return;
|
|
state.trailPositions.push(next);
|
|
if (state.trailPositions.length > TRAIL_MAX_POINTS)
|
|
state.trailPositions.shift();
|
|
state.trail.setPositions(state.trailPositions);
|
|
}
|
|
|
|
/**
|
|
* Clear the rendered trail and accumulation; invalidate pending backfills.
|
|
*/
|
|
|
|
function clearSelectedVesselTrail() {
|
|
state.trailAbort?.abort();
|
|
state.trailAbort = null;
|
|
state.trailBackfillToken += 1;
|
|
state.trailId = null;
|
|
state.trailPositions = [];
|
|
if (state.trail) state.trail.clear();
|
|
}
|
|
|
|
/**
|
|
* Destroy the trail primitive entirely (layer disable/teardown).
|
|
*/
|
|
|
|
function destroySelectedVesselTrail() {
|
|
clearSelectedVesselTrail();
|
|
if (state.trail) {
|
|
state.trail.destroy();
|
|
state.trail = null;
|
|
}
|
|
}
|
|
return {
|
|
currentGeoidN,
|
|
vesselTrailPosition,
|
|
refloorVesselRecords,
|
|
startSelectedVesselTrail,
|
|
backfillVesselTrail,
|
|
appendSelectedVesselTrailFix,
|
|
clearSelectedVesselTrail,
|
|
destroySelectedVesselTrail,
|
|
};
|
|
}
|