mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
feat(vessels, transit, rail): draw fleet models under the 3D toggle
Vessels, Transit and Rail turn nearby icons into fleet models at the aircraft layers' distances. A vessel is drawn at its AIS length and beam (static dimensions now reach vessel rows), standing at the sea surface; a transit vehicle or train stands on its road or rail, raised by its type's elevation, and is painted in its mode or category colour where the model marks it.
This commit is contained in:
1 parent
fcc44d0eaf
commit
7b93045895
18 files changed
+984
-15
No files matched your search
@@ -258,3 +258,61 @@ test('partial static data keeps newer live metadata ahead of the old static cach
|
||||
assert.equal(rowFor(mmsi).type, '80');
|
||||
assert.equal(rowFor(mmsi).destination, 'HAMBURG');
|
||||
});
|
||||
|
||||
// A static report's dimensions are the antenna's distances to bow (A), stern
|
||||
// (B), port (C) and starboard (D): length A+B, beam C+D. Zero is AIS's "not
|
||||
// available", so a vessel reporting none has no size, not a size of zero.
|
||||
test('a static report gives a vessel its hull length and beam', () => {
|
||||
ingestAisStreamEnvelope(positionReport('636019825', { Sog: 14 }));
|
||||
ingestAisStreamEnvelope({
|
||||
MessageType: 'ShipStaticData',
|
||||
Message: {
|
||||
ShipStaticData: {
|
||||
UserID: 636019825,
|
||||
Name: 'EVER GIVEN',
|
||||
Type: 70,
|
||||
Dimension: { A: 330, B: 70, C: 30, D: 29 },
|
||||
},
|
||||
},
|
||||
MetaData: { MMSI: '636019825' },
|
||||
});
|
||||
const row = rowFor('636019825');
|
||||
assert.equal(row.length, 400);
|
||||
assert.equal(row.beam, 59);
|
||||
|
||||
ingestAisStreamEnvelope(positionReport('636019826', { Sog: 3 }));
|
||||
ingestAisStreamEnvelope({
|
||||
MessageType: 'ShipStaticData',
|
||||
Message: {
|
||||
ShipStaticData: {
|
||||
UserID: 636019826,
|
||||
Type: 52,
|
||||
Dimension: { A: 0, B: 0, C: 0, D: 0 },
|
||||
},
|
||||
},
|
||||
MetaData: { MMSI: '636019826' },
|
||||
});
|
||||
assert.equal(rowFor('636019826').length, null);
|
||||
assert.equal(rowFor('636019826').beam, null);
|
||||
});
|
||||
|
||||
test('a Class B report carries its dimensions in part B', () => {
|
||||
ingestAisStreamEnvelope(positionReport('244000777', { Sog: 6 }));
|
||||
ingestAisStreamEnvelope({
|
||||
MessageType: 'StaticDataReport',
|
||||
Message: {
|
||||
StaticDataReport: {
|
||||
UserID: 244000777,
|
||||
ReportB: {
|
||||
Valid: true,
|
||||
ShipType: 37,
|
||||
Dimension: { A: 8, B: 4, C: 2, D: 2 },
|
||||
},
|
||||
},
|
||||
},
|
||||
MetaData: { MMSI: '244000777' },
|
||||
});
|
||||
const row = rowFor('244000777');
|
||||
assert.equal(row.length, 12);
|
||||
assert.equal(row.beam, 4);
|
||||
});
|
||||
@@ -0,0 +1,360 @@
|
||||
// The icon-to-model handoff the Vessels, Transit and Rail layers share
|
||||
// (fleetModelInstances.js): models only under the DISPLAY 3D toggle and the
|
||||
// altitude ceiling, at most `cap` of the nearest, each vehicle's icon the
|
||||
// visual until its model is loaded, ready and placed, and a tinted model
|
||||
// painted with its route colour.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import * as Cesium from 'cesium';
|
||||
import {
|
||||
createFleetModel,
|
||||
createFleetModelLayer,
|
||||
fleetModelMatrix,
|
||||
fleetTintShader,
|
||||
FLEET_HEADING_OFFSET_DEG,
|
||||
} from './fleetModelInstances.js';
|
||||
import {
|
||||
loadFleetModelSet,
|
||||
transitModelSpec,
|
||||
vesselModelSpec,
|
||||
} from './fleetModels.js';
|
||||
import { fetchPublicFile } from '../testSupport/fleetGlb.mjs';
|
||||
|
||||
const realInfo = console.info;
|
||||
console.info = () => {};
|
||||
const set = await loadFleetModelSet({ fetchImpl: fetchPublicFile });
|
||||
console.info = realInfo;
|
||||
|
||||
const POLICY = Object.freeze({
|
||||
altitudeCeilingM: 800_000,
|
||||
minimumPixelSize: 16,
|
||||
cap: 2,
|
||||
capAll: 4,
|
||||
addM: 5_000,
|
||||
addAllM: 20_000,
|
||||
keepM: 6_000,
|
||||
keepAllM: 24_000,
|
||||
intervalMs: 80,
|
||||
});
|
||||
|
||||
const CENTRE = Cesium.Cartesian3.fromDegrees(-122.4, 37.8, 0);
|
||||
|
||||
function fakeViewer(height = 1_000) {
|
||||
const primitives = [];
|
||||
const position = Cesium.Cartesian3.fromDegrees(-122.4, 37.8, height);
|
||||
const up = Cesium.Ellipsoid.WGS84.geodeticSurfaceNormal(
|
||||
position,
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
const camera = {
|
||||
positionWC: position,
|
||||
positionCartographic: { height },
|
||||
directionWC: Cesium.Cartesian3.negate(up, new Cesium.Cartesian3()),
|
||||
upWC: Cesium.Cartesian3.UNIT_Z,
|
||||
frustum: {
|
||||
computeCullingVolume: () => ({
|
||||
computeVisibility: () => Cesium.Intersect.INSIDE,
|
||||
}),
|
||||
},
|
||||
};
|
||||
return {
|
||||
camera,
|
||||
scene: {
|
||||
primitives: {
|
||||
add(p) {
|
||||
primitives.push(p);
|
||||
return p;
|
||||
},
|
||||
remove(p) {
|
||||
const i = primitives.indexOf(p);
|
||||
if (i >= 0) primitives.splice(i, 1);
|
||||
return i >= 0;
|
||||
},
|
||||
},
|
||||
},
|
||||
primitives,
|
||||
isDestroyed: () => false,
|
||||
};
|
||||
}
|
||||
|
||||
function fakeModel(options) {
|
||||
return {
|
||||
...options,
|
||||
ready: false,
|
||||
show: true,
|
||||
modelMatrix: new Cesium.Matrix4(),
|
||||
destroyed: false,
|
||||
destroy() {
|
||||
this.destroyed = true;
|
||||
},
|
||||
isDestroyed() {
|
||||
return this.destroyed;
|
||||
},
|
||||
update() {},
|
||||
};
|
||||
}
|
||||
|
||||
/** A vehicle `metres` east of the camera's foot. */
|
||||
function itemAt(metres, spec, extra = {}) {
|
||||
const enu = Cesium.Transforms.eastNorthUpToFixedFrame(CENTRE);
|
||||
const position = Cesium.Matrix4.multiplyByPoint(
|
||||
enu,
|
||||
new Cesium.Cartesian3(metres, 0, 0),
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
return { position, headingDeg: 90, spec, tint: null, pickId: null, ...extra };
|
||||
}
|
||||
|
||||
const flush = () => new Promise((resolve) => setImmediate(resolve));
|
||||
|
||||
test('models load from the up-front set, never by URL', async () => {
|
||||
const spec = vesselModelSpec(set, { type: '70', length: 200, beam: 32 });
|
||||
let options = null;
|
||||
await createFleetModel(
|
||||
spec,
|
||||
{ scale: spec.scale },
|
||||
{
|
||||
set,
|
||||
resolveAsset: (url) => `/base${url}`,
|
||||
load: async (o) => {
|
||||
options = o;
|
||||
return {};
|
||||
},
|
||||
},
|
||||
);
|
||||
assert.ok(options.gltf instanceof Uint8Array, 'the loaded GLB bytes');
|
||||
assert.equal(options.gltf, set.bytes.get(spec.model));
|
||||
assert.equal(options.basePath, `/base${spec.url}`);
|
||||
assert.equal(options.url, undefined, 'no fetch by URL');
|
||||
assert.deepEqual({ ...options.environmentMapOptions }, { enabled: false });
|
||||
await assert.rejects(
|
||||
createFleetModel({ model: 'missing' }, {}, { set, load: async () => ({}) }),
|
||||
/not loaded/,
|
||||
);
|
||||
});
|
||||
|
||||
test('the icon stays the visual until the model is ready; only the nearest are drawn', async () => {
|
||||
const viewer = fakeViewer();
|
||||
const display = { models3d: true, models3dMode: 'proximity' };
|
||||
const created = [];
|
||||
const layer = createFleetModelLayer({
|
||||
viewer,
|
||||
display: () => display,
|
||||
policy: POLICY,
|
||||
createModel: async (spec, options) => {
|
||||
const model = fakeModel({ spec, ...options });
|
||||
created.push(model);
|
||||
return model;
|
||||
},
|
||||
});
|
||||
const spec = transitModelSpec(set, { mode: 'tram' });
|
||||
const items = new Map([
|
||||
['near', itemAt(100, spec, { pickId: 'near' })],
|
||||
['mid', itemAt(1_000, spec, { pickId: 'mid' })],
|
||||
['far', itemAt(4_000, spec, { pickId: 'far' })],
|
||||
]);
|
||||
layer.frame(items, 0);
|
||||
await flush();
|
||||
assert.equal(created.length, POLICY.cap, 'the cap holds');
|
||||
assert.deepEqual(
|
||||
created.map((m) => m.id).sort(),
|
||||
['mid', 'near'],
|
||||
'the nearest are modelled',
|
||||
);
|
||||
assert.equal(created[0].scale, spec.scale, 'drawn life size');
|
||||
assert.equal(created[0].minimumPixelSize, POLICY.minimumPixelSize);
|
||||
|
||||
layer.frame(items, 100);
|
||||
for (const item of items.values())
|
||||
assert.equal(item.modelOwnsVisual, false, 'not ready: the icon draws');
|
||||
assert.ok(
|
||||
layer.settling > 0,
|
||||
'the layer asks for frames while models settle',
|
||||
);
|
||||
|
||||
for (const model of created) model.ready = true;
|
||||
layer.frame(items, 200);
|
||||
assert.equal(items.get('near').modelOwnsVisual, true);
|
||||
assert.equal(items.get('mid').modelOwnsVisual, true);
|
||||
assert.equal(items.get('far').modelOwnsVisual, false);
|
||||
assert.equal(layer.settling, 0);
|
||||
|
||||
// A placed model stands on its item's point, nose along its heading.
|
||||
const near = created.find((m) => m.id === 'near');
|
||||
const origin = Cesium.Matrix4.getTranslation(
|
||||
near.modelMatrix,
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
assert.ok(
|
||||
Cesium.Cartesian3.distance(origin, items.get('near').position) < 1e-6,
|
||||
);
|
||||
|
||||
// 3D off: every model goes and every icon comes back.
|
||||
display.models3d = false;
|
||||
layer.frame(items, 300);
|
||||
assert.equal(layer.size, 0);
|
||||
for (const item of items.values()) assert.equal(item.modelOwnsVisual, false);
|
||||
assert.ok(
|
||||
created.every((m) => m.destroyed),
|
||||
'released models are destroyed',
|
||||
);
|
||||
layer.destroy();
|
||||
assert.equal(viewer.primitives.length, 0);
|
||||
});
|
||||
|
||||
test('models stay off above the altitude ceiling', async () => {
|
||||
const viewer = fakeViewer(POLICY.altitudeCeilingM + 1);
|
||||
let calls = 0;
|
||||
const layer = createFleetModelLayer({
|
||||
viewer,
|
||||
display: () => ({ models3d: true }),
|
||||
policy: POLICY,
|
||||
createModel: async (spec, options) => {
|
||||
calls += 1;
|
||||
return fakeModel(options);
|
||||
},
|
||||
});
|
||||
assert.equal(layer.active(), false);
|
||||
layer.frame(new Map([['a', itemAt(0, transitModelSpec(set, {}))]]), 0);
|
||||
await flush();
|
||||
assert.equal(calls, 0);
|
||||
layer.destroy();
|
||||
});
|
||||
|
||||
test('a tinted model is painted with its route colour; an untinted one is not', async () => {
|
||||
const viewer = fakeViewer();
|
||||
const created = [];
|
||||
const layer = createFleetModelLayer({
|
||||
viewer,
|
||||
display: () => ({ models3d: true }),
|
||||
policy: POLICY,
|
||||
createModel: async (spec, options) => {
|
||||
const model = fakeModel(options);
|
||||
created.push(model);
|
||||
return model;
|
||||
},
|
||||
});
|
||||
const red = Cesium.Color.RED;
|
||||
const tram = transitModelSpec(set, { mode: 'tram' });
|
||||
const ship = vesselModelSpec(set, { type: '70', length: 180 });
|
||||
assert.equal(tram.tinted, true);
|
||||
assert.equal(ship.tinted, false);
|
||||
layer.frame(
|
||||
new Map([
|
||||
['tram', itemAt(10, tram, { pickId: 'tram', tint: red })],
|
||||
['ship', itemAt(20, ship, { pickId: 'ship', tint: red })],
|
||||
]),
|
||||
0,
|
||||
);
|
||||
await flush();
|
||||
const byId = Object.fromEntries(created.map((m) => [m.id, m]));
|
||||
assert.equal(byId.tram.customShader, fleetTintShader(red));
|
||||
assert.equal(byId.ship.customShader, undefined);
|
||||
assert.equal(
|
||||
fleetTintShader(red),
|
||||
fleetTintShader(Cesium.Color.RED),
|
||||
'one shader per colour',
|
||||
);
|
||||
layer.destroy();
|
||||
});
|
||||
|
||||
test('an elevated vehicle is raised by its type elevation', async () => {
|
||||
const viewer = fakeViewer();
|
||||
const created = [];
|
||||
const layer = createFleetModelLayer({
|
||||
viewer,
|
||||
display: () => ({ models3d: true }),
|
||||
policy: POLICY,
|
||||
createModel: async (spec, options) => {
|
||||
const model = fakeModel({ ...options, ready: true });
|
||||
model.ready = true;
|
||||
created.push(model);
|
||||
return model;
|
||||
},
|
||||
});
|
||||
const monorail = transitModelSpec(set, { mode: 'monorail' });
|
||||
assert.equal(monorail.elevationM, set.types.transit.elevation.monorail);
|
||||
const item = itemAt(10, monorail, { pickId: 'm' });
|
||||
const items = new Map([['m', item]]);
|
||||
layer.frame(items, 0);
|
||||
await flush();
|
||||
layer.frame(items, 100);
|
||||
const origin = Cesium.Matrix4.getTranslation(
|
||||
created[0].modelMatrix,
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
const lifted = Cesium.Cartographic.fromCartesian(origin).height;
|
||||
const ground = Cesium.Cartographic.fromCartesian(item.position).height;
|
||||
assert.ok(Math.abs(lifted - ground - monorail.elevationM) < 1e-3);
|
||||
layer.destroy();
|
||||
});
|
||||
|
||||
test('a vessel stretched to its reported beam keeps its length scale on the model', () => {
|
||||
const spec = vesselModelSpec(set, { type: '70', length: 200, beam: 60 });
|
||||
assert.ok(spec.scaleXYZ[1] > spec.scaleXYZ[0], 'a wide hull for its length');
|
||||
const matrix = fleetModelMatrix(
|
||||
CENTRE,
|
||||
0,
|
||||
spec,
|
||||
new Cesium.Matrix4(),
|
||||
new Cesium.HeadingPitchRoll(),
|
||||
);
|
||||
const scale = Cesium.Matrix4.getScale(matrix, new Cesium.Cartesian3());
|
||||
// Cesium's axis correction puts the beam on local X, the length on local Y.
|
||||
assert.ok(Math.abs(scale.x - spec.scaleXYZ[1] / spec.scaleXYZ[0]) < 1e-9);
|
||||
assert.ok(Math.abs(scale.y - 1) < 1e-9);
|
||||
assert.equal(FLEET_HEADING_OFFSET_DEG, 180, 'fleet models are built nose -X');
|
||||
});
|
||||
|
||||
test('Vessels, Transit and Rail draw fleet models under the shared 3D toggle', () => {
|
||||
for (const file of [
|
||||
'src/layers/vessels/rendering.js',
|
||||
'src/layers/transit/rendering.js',
|
||||
'src/layers/rail/rendering.js',
|
||||
]) {
|
||||
const source = fs.readFileSync(
|
||||
new URL(`../../${file}`, import.meta.url),
|
||||
'utf8',
|
||||
);
|
||||
assert.match(
|
||||
source,
|
||||
/createFleetModelLayer\(/,
|
||||
`${file}: draws fleet models`,
|
||||
);
|
||||
assert.match(
|
||||
source,
|
||||
/loadFleetModelSet\(/,
|
||||
`${file}: loads the set up front`,
|
||||
);
|
||||
assert.match(
|
||||
source,
|
||||
/modelOwnsVisual/,
|
||||
`${file}: hands its icon to the model`,
|
||||
);
|
||||
}
|
||||
for (const file of [
|
||||
'src/app/layers/aisLiveVessels.js',
|
||||
'src/app/layers/transit.js',
|
||||
'src/app/layers/rail.js',
|
||||
]) {
|
||||
const source = fs.readFileSync(
|
||||
new URL(`../../${file}`, import.meta.url),
|
||||
'utf8',
|
||||
);
|
||||
assert.match(
|
||||
source,
|
||||
/fleet: fleetModelServices\(/,
|
||||
`${file}: given the fleet service`,
|
||||
);
|
||||
}
|
||||
const catalog = fs.readFileSync(
|
||||
new URL('../app/constructCatalog.js', import.meta.url),
|
||||
'utf8',
|
||||
);
|
||||
assert.equal(
|
||||
catalog.match(/displayParams: \(\) => flights\.getParams\(\)/g)?.length,
|
||||
4,
|
||||
'Local ADS-B, Vessels, Transit and Rail follow the Flights 3D toggle',
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user