mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Build the selected vessel's trail without Cesium
Trail vertices are geo/ellipsoid positions and the minimum move is measured with geo/vector, both giving what the Cesium calls gave. Re-anchoring rows when the geoid loads asks the rendering part for the anchor, now one function prepareRecordVisual shares, so icon positions stay Cesium values. The shared trail renderer takes plain ECEF positions as well, turning them into Cartesian3s for its polyline; the Cartesian3s other layers hand it pass through untouched. tracking leaves the Cesium allow-list.
This commit is contained in:
1 parent
1c840cb66f
commit
e327760074
6 files changed
+224
-26
No files matched your search
@@ -49,7 +49,7 @@ const MIN_SEGMENT_DISTANCE_SQ = 0.01;
|
||||
* @param {object} options - Trail options.
|
||||
* @param {string} options.color - CSS color string for the trail hue.
|
||||
* @param {number} [options.width=2.5] - Polyline width in pixels.
|
||||
* @returns {{setPositions: function(Cesium.Cartesian3[]): void, setVisible: function(boolean): void, clear: function(): void, destroy: function(): void}}
|
||||
* @returns {{setPositions: function(Array<Cesium.Cartesian3|{x: number, y: number, z: number}>): void, setVisible: function(boolean): void, clear: function(): void, destroy: function(): void}}
|
||||
* Trail handle: setPositions replaces the geometry, setVisible temporarily
|
||||
* hides it without discarding history, clear empties it, and destroy removes
|
||||
* the entity permanently.
|
||||
@@ -92,7 +92,8 @@ export function createTrail(viewer, { color, width = 2.5 }) {
|
||||
/**
|
||||
* Replace the trail geometry with a chronological position list
|
||||
* (oldest first). Fewer than 2 distinct positions clears the trail.
|
||||
* @param {Cesium.Cartesian3[]} cartesians - Positions, oldest -> newest.
|
||||
* @param {Array<Cesium.Cartesian3|{x: number, y: number, z: number}>} cartesians
|
||||
* ECEF positions, oldest -> newest.
|
||||
*/
|
||||
setPositions(cartesians) {
|
||||
if (destroyed || !viewer || viewer.isDestroyed()) return;
|
||||
@@ -107,7 +108,12 @@ export function createTrail(viewer, { color, width = 2.5 }) {
|
||||
MIN_SEGMENT_DISTANCE_SQ
|
||||
)
|
||||
continue;
|
||||
positions.push(position);
|
||||
// Callers may hand plain ECEF {x, y, z}: the polyline gets Cartesian3s.
|
||||
positions.push(
|
||||
position instanceof Cesium.Cartesian3
|
||||
? position
|
||||
: new Cesium.Cartesian3(position.x, position.y, position.z),
|
||||
);
|
||||
}
|
||||
current = positions.length >= 2 ? positions : [];
|
||||
ensureEntity();
|
||||
|
||||
@@ -37,3 +37,26 @@ test('trail visibility can change without discarding its accumulated geometry',
|
||||
trail.destroy();
|
||||
assert.deepEqual(removed, [added[0]]);
|
||||
});
|
||||
|
||||
test('trail positions may be plain ECEF: the polyline still draws Cartesian3s', () => {
|
||||
const added = [];
|
||||
const viewer = {
|
||||
isDestroyed: () => false,
|
||||
entities: {
|
||||
add(definition) {
|
||||
added.push(definition);
|
||||
return definition;
|
||||
},
|
||||
remove() {},
|
||||
},
|
||||
};
|
||||
const trail = createTrail(viewer, { color: '#ffffff' });
|
||||
const given = new Cesium.Cartesian3(1, 2, 3);
|
||||
trail.setPositions([given, { x: 1, y: 2, z: 3.01 }, { x: 4, y: 5, z: 6 }]);
|
||||
const drawn = added[0].polyline.positions.getValue();
|
||||
// The near-duplicate merges; a Cartesian3 given passes through as it is.
|
||||
assert.equal(drawn.length, 2);
|
||||
assert.equal(drawn[0], given);
|
||||
assert.ok(drawn[1] instanceof Cesium.Cartesian3);
|
||||
assert.deepEqual([drawn[1].x, drawn[1].y, drawn[1].z], [4, 5, 6]);
|
||||
});
|
||||
@@ -65,16 +65,23 @@ export function createRendering({
|
||||
};
|
||||
visualRecords.set(record, visual);
|
||||
}
|
||||
visual.position = anchorPosition(record);
|
||||
return visual;
|
||||
}
|
||||
|
||||
/**
|
||||
* Where a record's icon is anchored: VESSEL_LIFT_M above the sea surface
|
||||
* (the geoid, once its grid has loaded).
|
||||
* @param {Object} record - Vessel record with a finite lat/lon.
|
||||
* @returns {Cesium.Cartesian3} A Cesium position: icon rows, focus and the
|
||||
* layer's queries take it as it is.
|
||||
*/
|
||||
function anchorPosition(record) {
|
||||
const heightM = components.queries.vesselDatumHeightM(
|
||||
components.tracking.currentGeoidN(record.lat, record.lon),
|
||||
VESSEL_LIFT_M,
|
||||
);
|
||||
visual.position = Cesium.Cartesian3.fromDegrees(
|
||||
record.lon,
|
||||
record.lat,
|
||||
heightM,
|
||||
);
|
||||
return visual;
|
||||
return Cesium.Cartesian3.fromDegrees(record.lon, record.lat, heightM);
|
||||
}
|
||||
|
||||
function resetRecordVisuals() {
|
||||
@@ -594,6 +601,7 @@ export function createRendering({
|
||||
getVisual,
|
||||
iconPosition,
|
||||
prepareRecordVisual,
|
||||
anchorPosition,
|
||||
resetRecordVisuals,
|
||||
renderRowLimit,
|
||||
labelRowLimit,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import { fromDegrees } from '../../geo/ellipsoid.js';
|
||||
import { distance } from '../../geo/vector.js';
|
||||
import {
|
||||
TRAIL_HEIGHT_M,
|
||||
VESSEL_LIFT_M,
|
||||
TRAIL_COLOR,
|
||||
TRAIL_MAX_POINTS,
|
||||
TRAIL_MIN_MOVE_M,
|
||||
@@ -35,7 +35,7 @@ export function createTracking({
|
||||
* 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 {Cesium.Cartesian3|null} Lifted position, or null without a fix.
|
||||
* @returns {{x: number, y: number, z: number}|null} Lifted ECEF position, or null without a fix.
|
||||
*/
|
||||
|
||||
function vesselTrailPosition(record) {
|
||||
@@ -45,7 +45,7 @@ export function createTracking({
|
||||
currentGeoidN(record.lat, record.lon),
|
||||
TRAIL_HEIGHT_M,
|
||||
);
|
||||
return Cesium.Cartesian3.fromDegrees(record.lon, record.lat, heightM);
|
||||
return fromDegrees(record.lon, record.lat, heightM);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,15 +63,7 @@ export function createTracking({
|
||||
const visual = components.rendering.getVisual(record);
|
||||
if (!Number.isFinite(record.lat) || !Number.isFinite(record.lon))
|
||||
continue;
|
||||
const heightM = components.queries.vesselDatumHeightM(
|
||||
currentGeoidN(record.lat, record.lon),
|
||||
VESSEL_LIFT_M,
|
||||
);
|
||||
visual.position = Cesium.Cartesian3.fromDegrees(
|
||||
record.lon,
|
||||
record.lat,
|
||||
heightM,
|
||||
);
|
||||
visual.position = components.rendering.anchorPosition(record);
|
||||
components.rendering.syncRecordIcon(record);
|
||||
}
|
||||
// The rows moved with no poll behind them: draw the frame that shows it.
|
||||
@@ -149,7 +141,7 @@ export function createTracking({
|
||||
currentGeoidN(lat, lon),
|
||||
TRAIL_HEIGHT_M,
|
||||
);
|
||||
older.push(Cesium.Cartesian3.fromDegrees(lon, lat, heightM));
|
||||
older.push(fromDegrees(lon, lat, heightM));
|
||||
}
|
||||
if (!older.length) return;
|
||||
|
||||
@@ -173,8 +165,7 @@ export function createTracking({
|
||||
const next = vesselTrailPosition(record);
|
||||
if (!next) return;
|
||||
const last = state.trailPositions[state.trailPositions.length - 1];
|
||||
if (last && Cesium.Cartesian3.distance(last, next) <= TRAIL_MIN_MOVE_M)
|
||||
return;
|
||||
if (last && distance(last, next) <= TRAIL_MIN_MOVE_M) return;
|
||||
state.trailPositions.push(next);
|
||||
if (state.trailPositions.length > TRAIL_MAX_POINTS)
|
||||
state.trailPositions.shift();
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import * as Cesium from 'cesium';
|
||||
import { createTracking } from './tracking.js';
|
||||
import { TRAIL_HEIGHT_M, TRAIL_MAX_POINTS } from './policy.js';
|
||||
|
||||
/** One tracking part over a fake trail and geoid; `geoidN` is N everywhere. */
|
||||
function setup({ geoidN = 40, track = null } = {}) {
|
||||
const drawn = [];
|
||||
const trail = {
|
||||
setPositions: (positions) => drawn.push([...positions]),
|
||||
clear() {},
|
||||
destroy() {},
|
||||
};
|
||||
const synced = [];
|
||||
const records = [];
|
||||
const vesselState = {
|
||||
_geoidReady: true,
|
||||
_source: { getTrack: async () => track },
|
||||
state: {
|
||||
viewer: {},
|
||||
trail: null,
|
||||
trailPositions: [],
|
||||
trailBackfillToken: 0,
|
||||
trailAbort: null,
|
||||
trailMmsi: null,
|
||||
records: { all: records },
|
||||
},
|
||||
};
|
||||
const visuals = new Map();
|
||||
const parts = {
|
||||
queries: {
|
||||
vesselDatumHeightM: (n, lift) => (Number.isFinite(n) ? n : 0) + lift,
|
||||
},
|
||||
rendering: {
|
||||
getVisual: (record) => visuals.get(record) ?? record,
|
||||
anchorPosition: (record) => ({ anchoredAt: [record.lon, record.lat] }),
|
||||
syncRecordIcon: (record) => synced.push(record),
|
||||
},
|
||||
};
|
||||
const renders = [];
|
||||
const tracking = createTracking({
|
||||
vesselState,
|
||||
services: {
|
||||
geoid: { geoidHeight: () => geoidN },
|
||||
trails: { createTrail: () => trail },
|
||||
render: { governorRequestRender: (reason) => renders.push(reason) },
|
||||
},
|
||||
parts,
|
||||
layer: {},
|
||||
options: {},
|
||||
});
|
||||
return { tracking, vesselState, drawn, synced, records, visuals, renders };
|
||||
}
|
||||
|
||||
const near = (a, b) =>
|
||||
Math.abs(a.x - b.x) < 1e-6 &&
|
||||
Math.abs(a.y - b.y) < 1e-6 &&
|
||||
Math.abs(a.z - b.z) < 1e-6;
|
||||
|
||||
test('trail vertices sit TRAIL_HEIGHT_M above the geoid, as Cesium places them', () => {
|
||||
const { tracking } = setup({ geoidN: 40 });
|
||||
const position = tracking.vesselTrailPosition({ lat: 51.9, lon: 4.05 });
|
||||
const expected = Cesium.Cartesian3.fromDegrees(
|
||||
4.05,
|
||||
51.9,
|
||||
40 + TRAIL_HEIGHT_M,
|
||||
);
|
||||
assert.ok(near(position, expected));
|
||||
assert.equal(tracking.vesselTrailPosition({ lat: NaN, lon: 4 }), null);
|
||||
});
|
||||
|
||||
test('a selected trail backfills older samples ahead of the live fix', async () => {
|
||||
const track = {
|
||||
records: [
|
||||
{ latitude: 51.8, longitude: 4.0 },
|
||||
{ latitude: 'x', longitude: 4.0 },
|
||||
{ latitude: 51.85, longitude: 4.02 },
|
||||
],
|
||||
};
|
||||
const { tracking, vesselState, drawn } = setup({ track });
|
||||
tracking.startSelectedVesselTrail({
|
||||
mmsi: '1',
|
||||
reference: '1',
|
||||
lat: 51.9,
|
||||
lon: 4.05,
|
||||
});
|
||||
assert.equal(drawn.length, 1);
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
const positions = vesselState.state.trailPositions;
|
||||
assert.equal(positions.length, 3);
|
||||
assert.ok(
|
||||
near(
|
||||
positions[0],
|
||||
Cesium.Cartesian3.fromDegrees(4.0, 51.8, 40 + TRAIL_HEIGHT_M),
|
||||
),
|
||||
'oldest first',
|
||||
);
|
||||
assert.ok(
|
||||
near(
|
||||
positions[1],
|
||||
Cesium.Cartesian3.fromDegrees(4.02, 51.85, 40 + TRAIL_HEIGHT_M),
|
||||
),
|
||||
);
|
||||
assert.ok(
|
||||
near(
|
||||
positions[2],
|
||||
Cesium.Cartesian3.fromDegrees(4.05, 51.9, 40 + TRAIL_HEIGHT_M),
|
||||
),
|
||||
);
|
||||
assert.deepEqual(drawn.at(-1), positions);
|
||||
});
|
||||
|
||||
test('a refreshed fix joins the trail only once it has moved far enough', () => {
|
||||
const { tracking, vesselState, drawn } = setup();
|
||||
const record = { mmsi: '1', reference: '1', lat: 51.9, lon: 4.05 };
|
||||
tracking.startSelectedVesselTrail(record);
|
||||
const before = drawn.length;
|
||||
// About 11 m north: under TRAIL_MIN_MOVE_M.
|
||||
tracking.appendSelectedVesselTrailFix({ ...record, lat: 51.9001 });
|
||||
assert.equal(vesselState.state.trailPositions.length, 1);
|
||||
assert.equal(drawn.length, before);
|
||||
// About 111 m north.
|
||||
tracking.appendSelectedVesselTrailFix({ ...record, lat: 51.901 });
|
||||
assert.equal(vesselState.state.trailPositions.length, 2);
|
||||
assert.equal(drawn.length, before + 1);
|
||||
});
|
||||
|
||||
test('refloor re-anchors every fixed record through the rendering part', () => {
|
||||
const { tracking, records, visuals, synced, renders } = setup();
|
||||
const fixed = { lat: 51.9, lon: 4.05 };
|
||||
const unfixed = { lat: NaN, lon: 4.05 };
|
||||
records.push(fixed, unfixed);
|
||||
const visual = {};
|
||||
visuals.set(fixed, visual);
|
||||
tracking.refloorVesselRecords();
|
||||
assert.deepEqual(visual.position, { anchoredAt: [4.05, 51.9] });
|
||||
assert.deepEqual(synced, [fixed]);
|
||||
assert.deepEqual(renders, ['ais-vessels-refloor']);
|
||||
});
|
||||
|
||||
test('a long backfill keeps the newest TRAIL_MAX_POINTS vertices', async () => {
|
||||
const records = [];
|
||||
for (let i = 0; i < TRAIL_MAX_POINTS + 50; i += 1)
|
||||
records.push({ latitude: 50 + i * 0.001, longitude: 4 });
|
||||
const { tracking, vesselState } = setup({ track: { records } });
|
||||
tracking.startSelectedVesselTrail({
|
||||
mmsi: '1',
|
||||
reference: '1',
|
||||
lat: 51.9,
|
||||
lon: 4.05,
|
||||
});
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
const positions = vesselState.state.trailPositions;
|
||||
assert.equal(positions.length, TRAIL_MAX_POINTS);
|
||||
// The oldest 51 (50 backfilled samples and room for the live fix) are gone.
|
||||
const firstKept = 50 + 51 * 0.001;
|
||||
assert.ok(
|
||||
near(
|
||||
positions[0],
|
||||
Cesium.Cartesian3.fromDegrees(4, firstKept, 40 + TRAIL_HEIGHT_M),
|
||||
),
|
||||
);
|
||||
assert.ok(
|
||||
near(
|
||||
positions.at(-1),
|
||||
Cesium.Cartesian3.fromDegrees(4.05, 51.9, 40 + TRAIL_HEIGHT_M),
|
||||
),
|
||||
'the live fix stays newest',
|
||||
);
|
||||
});
|
||||
@@ -130,7 +130,6 @@
|
||||
"src/layers/transit/rendering.js",
|
||||
"src/layers/transit/testing.js",
|
||||
"src/layers/vessels/rendering.js",
|
||||
"src/layers/vessels/tracking.js",
|
||||
"src/layers/weather/index.js",
|
||||
"src/layers/weather/shellRendering.js",
|
||||
"src/layers/wind/gpuRendering.js",
|
||||
|
||||
Reference in new issue
Block a user