mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-17 00:32:21 +00:00
Vessel tracks end at the vessel
The selected vessel's track drew every fix it held. Following the world clock, it ran ahead of the icon to the vessel's newest fix; live, it stopped at the last fix while the icon moved on from it. A change of time restarted it from a single point until the track at the new time arrived, so it went blank at every step. - The track draws the fixes at or before the time the vessel is drawn at; live, every fix. - Each frame a head joins its last drawn fix to the icon where it is drawn, as flight tracks do; none past TRAIL_HEAD_MAX_M (20 km), where the clock has moved beyond the fixes the track holds. - A change of time keeps the track, drawn up to the new time, until the track at that time replaces it.
This commit is contained in:
1 parent
76d203c8f1
commit
26893ae37e
7 files changed
+280
-18
No files matched your search
+126
-13
@@ -1,13 +1,22 @@
|
||||
import { fromDegrees } from '../../geo/ellipsoid.js';
|
||||
import { distance } from '../../geo/vector.js';
|
||||
import { findFrame } from '../../render/rendererOf.js';
|
||||
import {
|
||||
TRAIL_HEIGHT_M,
|
||||
TRAIL_COLOR,
|
||||
TRAIL_HEAD_MAX_M,
|
||||
TRAIL_MAX_POINTS,
|
||||
TRAIL_MIN_MOVE_M,
|
||||
followsClock,
|
||||
} from './policy.js';
|
||||
|
||||
/** A vessel record's own fix time, epoch ms (NaN when it gives none). */
|
||||
function recordFixMs(record) {
|
||||
const epoch = record?.lastPositionEpoch;
|
||||
if (Number.isFinite(epoch)) return epoch * 1000;
|
||||
return Number.isFinite(record?.receivedAtMs) ? record.receivedAtMs : NaN;
|
||||
}
|
||||
|
||||
export function createTracking({
|
||||
vesselState,
|
||||
services,
|
||||
@@ -89,18 +98,94 @@ export function createTracking({
|
||||
state.trailBackfillToken += 1;
|
||||
state.trailId = record.id;
|
||||
state.trailPositions = [];
|
||||
state.trailTimes = [];
|
||||
const current = vesselTrailPosition(record);
|
||||
if (current) state.trailPositions.push(current);
|
||||
if (current) {
|
||||
state.trailPositions.push(current);
|
||||
state.trailTimes.push(recordFixMs(record));
|
||||
}
|
||||
if (!state.trail && state.viewer) {
|
||||
state.trail = createTrail(state.viewer, {
|
||||
color: TRAIL_COLOR,
|
||||
width: 2.5,
|
||||
});
|
||||
}
|
||||
if (state.trail) state.trail.setPositions(state.trailPositions);
|
||||
if (!state.trailHeadOff && state.viewer)
|
||||
state.trailHeadOff =
|
||||
findFrame(state.viewer)?.on('preRender', drawSelectedVesselTrail) ??
|
||||
null;
|
||||
drawTrailBody();
|
||||
backfillVesselTrail(record.id, state.trailBackfillToken, record.reference);
|
||||
}
|
||||
|
||||
/**
|
||||
* The world clock moved to another time: the trail keeps what it holds,
|
||||
* drawn up to the new time, until the vessel's track at that time
|
||||
* replaces it. Clearing it would blank the trail at every step of the clock.
|
||||
* @param {Object} record - The selected vessel at the new time.
|
||||
*/
|
||||
function retimeSelectedVesselTrail(record) {
|
||||
if (state.trailId !== record.id || !state.trail) {
|
||||
startSelectedVesselTrail(record);
|
||||
return;
|
||||
}
|
||||
state.trailAbort?.abort();
|
||||
state.trailAbort = new AbortController();
|
||||
state.trailBackfillToken += 1;
|
||||
drawSelectedVesselTrail();
|
||||
backfillVesselTrail(record.id, state.trailBackfillToken, record.reference, {
|
||||
replace: true,
|
||||
});
|
||||
}
|
||||
|
||||
/** The world time the selected vessel is drawn at; Infinity live, where
|
||||
* every fix is behind it. */
|
||||
function drawnTimeMs() {
|
||||
return followsClock(vesselState) ? vesselState.worldNow() : Infinity;
|
||||
}
|
||||
|
||||
/** How many trail vertices are at or before `atMs` (undated ones always). */
|
||||
function bodyCountAt(atMs) {
|
||||
let count = state.trailTimes.length;
|
||||
while (count > 0 && state.trailTimes[count - 1] > atMs) count -= 1;
|
||||
return count;
|
||||
}
|
||||
|
||||
/** Draw the trail's body: its fixes up to the time the vessel is drawn at.
|
||||
* A fix ahead of the vessel would draw the trail in front of it. */
|
||||
function drawTrailBody() {
|
||||
if (!state.trail) return;
|
||||
state.trailBodyCount = bodyCountAt(drawnTimeMs());
|
||||
state.trail.setPositions(
|
||||
state.trailPositions.slice(0, state.trailBodyCount),
|
||||
);
|
||||
}
|
||||
|
||||
/** The head: the body's last vertex to where the vessel is drawn, or none
|
||||
* (no vessel drawn, or one too far from the body to be on its path). */
|
||||
function trailHeadPositions() {
|
||||
const record = state.selectedRecord;
|
||||
if (!record || record.id !== state.trailId || state.trailBodyCount < 1)
|
||||
return null;
|
||||
// Where its icon is drawn now (the renderer's, while it moves the icons).
|
||||
const at = components.rendering.drawnPositionNow(record);
|
||||
const last = state.trailPositions[state.trailBodyCount - 1];
|
||||
if (!at || !last) return null;
|
||||
const metres = distance(last, at);
|
||||
if (metres < 1 || metres > TRAIL_HEAD_MAX_M) return null;
|
||||
return [last, { x: at.x, y: at.y, z: at.z }];
|
||||
}
|
||||
|
||||
/**
|
||||
* Each frame before it draws: the body moved on when the drawn time has
|
||||
* passed a fix, then the head to where the vessel is drawn now.
|
||||
*/
|
||||
function drawSelectedVesselTrail() {
|
||||
if (!state.trail || !state.trailId) return;
|
||||
if (bodyCountAt(drawnTimeMs()) !== state.trailBodyCount) drawTrailBody();
|
||||
state.trail.setHead?.(trailHeadPositions());
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire-and-forget backfill from the server-side per-vessel ring buffer
|
||||
* (PRD F3 — "recent path" since server boot, not voyage history). Older
|
||||
@@ -112,7 +197,12 @@ export function createTracking({
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
|
||||
async function backfillVesselTrail(id, token, reference = id) {
|
||||
async function backfillVesselTrail(
|
||||
id,
|
||||
token,
|
||||
reference = id,
|
||||
{ replace = false } = {},
|
||||
) {
|
||||
const owner = state.trailAbort;
|
||||
// Following the world clock, the track ends at the world's time.
|
||||
const atMs = followsClock(vesselState) ? vesselState.worldNow() : undefined;
|
||||
@@ -139,6 +229,7 @@ export function createTracking({
|
||||
if (state.trailId !== id) return;
|
||||
|
||||
const older = [];
|
||||
const olderTimes = [];
|
||||
for (const sample of samples) {
|
||||
const lat = Number(sample?.latitude);
|
||||
const lon = Number(sample?.longitude);
|
||||
@@ -156,16 +247,29 @@ export function createTracking({
|
||||
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,
|
||||
olderTimes.push(
|
||||
Number.isFinite(sample?.observedAtMs) ? sample.observedAtMs : NaN,
|
||||
);
|
||||
}
|
||||
if (state.trail) state.trail.setPositions(state.trailPositions);
|
||||
if (replace) {
|
||||
// The track at the new time, then the vessel's own fix at it.
|
||||
const record = state.selectedRecord;
|
||||
const current = record?.id === id ? vesselTrailPosition(record) : null;
|
||||
state.trailPositions = current ? [...older, current] : older;
|
||||
state.trailTimes = current
|
||||
? [...olderTimes, recordFixMs(record)]
|
||||
: olderTimes;
|
||||
} else {
|
||||
if (!older.length) return;
|
||||
state.trailPositions = older.concat(state.trailPositions);
|
||||
state.trailTimes = olderTimes.concat(state.trailTimes);
|
||||
}
|
||||
if (state.trailPositions.length > TRAIL_MAX_POINTS) {
|
||||
const drop = state.trailPositions.length - TRAIL_MAX_POINTS;
|
||||
state.trailPositions = state.trailPositions.slice(drop);
|
||||
state.trailTimes = state.trailTimes.slice(drop);
|
||||
}
|
||||
drawTrailBody();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -181,9 +285,12 @@ export function createTracking({
|
||||
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.trailTimes.push(recordFixMs(record));
|
||||
if (state.trailPositions.length > TRAIL_MAX_POINTS) {
|
||||
state.trailPositions.shift();
|
||||
state.trail.setPositions(state.trailPositions);
|
||||
state.trailTimes.shift();
|
||||
}
|
||||
drawTrailBody();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -196,6 +303,10 @@ export function createTracking({
|
||||
state.trailBackfillToken += 1;
|
||||
state.trailId = null;
|
||||
state.trailPositions = [];
|
||||
state.trailTimes = [];
|
||||
state.trailBodyCount = -1;
|
||||
state.trailHeadOff?.();
|
||||
state.trailHeadOff = null;
|
||||
if (state.trail) state.trail.clear();
|
||||
}
|
||||
|
||||
@@ -215,6 +326,8 @@ export function createTracking({
|
||||
vesselTrailPosition,
|
||||
refloorVesselRecords,
|
||||
startSelectedVesselTrail,
|
||||
retimeSelectedVesselTrail,
|
||||
drawSelectedVesselTrail,
|
||||
backfillVesselTrail,
|
||||
appendSelectedVesselTrailFix,
|
||||
clearSelectedVesselTrail,
|
||||
|
||||
Reference in new issue
Block a user