Draw GNSS integrity cells through the renderer's draw port

Cells are clamped rectangle fills in one draw set per layer
(gnss-integrity:N), made with pickable: false so they never hide what is
under them from clicks, as before. The view anchor comes from the camera
port: where the line of sight meets the ellipsoid, else the middle of the
view rectangle, else the point under the eye. Re-anchoring follows the
camera port's moveEnd and changed events. The ray-ellipsoid intersection
moves from the stub renderer into geo/ellipsoid.js for both to use, and
geo/rectangle.js adds the antimeridian-aware width, height and centre of
a radian rectangle. The layer leaves the allow-list; the QA script finds
the cells among the scene's unpickable ground primitives.
This commit is contained in:
Sameh Khamis committed 2026-11-06 20:30:33 -08:00
1 parent 3f9089eccb
commit 6f82f9cc9c
10 files changed
+418 -232

No files matched your search

+126 -93
View File
@@ -1,9 +1,12 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import * as Cesium from 'cesium';
import { createGnssIntegrityLayer, defaultGnssViewAnchor } from './index.js';
import { LayerLifecycle } from '../../data/lifecycle.js';
import { layerFeedState } from '../../data/feedState.js';
import { fromDegrees } from '../../geo/ellipsoid.js';
import { bindRenderer } from '../../render/rendererOf.js';
import { fakeCameraPort } from '../../testSupport/fakeCameraPort.mjs';
import { bindFakeDraw } from '../../testSupport/fakeDrawPort.mjs';
const row = (hex, lat, degraded) => ({
hex,
@@ -23,22 +26,12 @@ function harness(
} = {},
) {
let anchor = initialAnchor;
const sources = [];
const viewer = {
camera: { moveEnd: new Cesium.Event(), changed: new Cesium.Event() },
scene: {
globe: { show: true },
groundPrimitives: {
add(value) {
sources.push(value);
},
remove(value) {
sources.splice(sources.indexOf(value), 1);
value.destroy();
},
},
},
};
const viewer = { scene: { globe: { show: true } } };
const camera = fakeCameraPort();
const { draw } = bindFakeDraw(viewer, { camera: camera.port });
// The layer's one live set, or undefined once it is disposed.
const fills = () =>
[...draw.sets.values()].find((record) => !record.disposed);
let clock = 1_000;
const anchors = [];
const layer = createGnssIntegrityLayer({
@@ -59,7 +52,8 @@ function harness(
if (initialize) layer.init(viewer);
return {
layer,
sources,
fills,
camera,
anchors,
viewer,
advance(ms) {
@@ -82,25 +76,61 @@ const threeHealthy = () => [
const flush = () => new Promise((resolve) => setImmediate(resolve));
test('a globe-wide rectangle uses the visible screen centre as the query anchor', () => {
const anchor = defaultGnssViewAnchor({
scene: {
canvas: { clientWidth: 1000, clientHeight: 800 },
globe: { ellipsoid: Cesium.Ellipsoid.WGS84 },
},
camera: {
pickEllipsoid(pixel) {
assert.deepEqual(pixel, new Cesium.Cartesian2(500, 400));
return Cesium.Cartesian3.fromDegrees(-97, 30);
},
computeViewRectangle: () => {
throw new Error('centre hit must not compute a rectangle');
const viewer = {};
// The eye high over (-97, 30), looking straight down.
const eye = fromDegrees(-97, 30, 2e7);
const ground = fromDegrees(-97, 30, 0);
const length = Math.hypot(
eye.x - ground.x,
eye.y - ground.y,
eye.z - ground.z,
);
const camera = fakeCameraPort({
pose: {
lon: 0,
lat: 0,
position: eye,
direction: {
x: (ground.x - eye.x) / length,
y: (ground.y - eye.y) / length,
z: (ground.z - eye.z) / length,
},
},
});
camera.port.viewRectangle = () => {
throw new Error('centre hit must not compute a rectangle');
};
bindRenderer(viewer, { camera: camera.port });
const anchor = defaultGnssViewAnchor(viewer);
assert.ok(Math.abs(anchor.latitude - 30) < 1e-8);
assert.ok(Math.abs(anchor.longitude + 97) < 1e-8);
});
test('a line of sight that misses the globe falls back to the view rectangle, then the point under the eye', () => {
const viewer = {};
// The eye over (10, 20), looking straight up.
const eye = fromDegrees(10, 20, 1e6);
const length = Math.hypot(eye.x, eye.y, eye.z);
const camera = fakeCameraPort({
pose: {
lon: 10,
lat: 20,
position: eye,
direction: { x: eye.x / length, y: eye.y / length, z: eye.z / length },
},
});
let rectangle = { west: 170, south: -10, east: -170, north: 10 };
camera.port.viewRectangle = () => rectangle;
bindRenderer(viewer, { camera: camera.port });
const across = defaultGnssViewAnchor(viewer);
assert.ok(Math.abs(across.latitude) < 1e-8);
assert.ok(Math.abs(Math.abs(across.longitude) - 180) < 1e-8);
rectangle = null;
const under = defaultGnssViewAnchor(viewer);
assert.ok(Math.abs(under.latitude - 20) < 1e-8);
assert.ok(Math.abs(under.longitude - 10) < 1e-8);
});
test('pan and zoom refresh through the manager without toggling; nearby changes are coalesced', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout', 'setInterval'] });
let calls = 0;
@@ -118,9 +148,9 @@ test('pan and zoom refresh through the manager without toggling; nearby changes
manager.subscribe((change) => changes.push(change.type));
await manager.setEnabled(h.layer.id, true);
h.setAnchor({ latitude: 54, longitude: 19 });
h.viewer.camera.changed.raiseEvent();
h.camera.emit('changed');
h.setAnchor({ latitude: 55, longitude: 20 });
h.viewer.camera.moveEnd.raiseEvent();
h.camera.emit('moveEnd');
h.advance(2500);
t.mock.timers.tick(2500);
await flush();
@@ -137,14 +167,14 @@ test('pan and zoom refresh through the manager without toggling; nearby changes
});
assert.match(h.layer.getRowControls().info, /Regional sample.*250 nm radius/);
h.setAnchor({ latitude: 55.1, longitude: 20.1 });
h.viewer.camera.changed.raiseEvent();
h.camera.emit('changed');
h.advance(5000);
t.mock.timers.tick(5000);
await flush();
assert.equal(calls, 2, 'nearby views reuse the same query region');
await manager.destroyAll();
assert.equal(h.viewer.camera.moveEnd.numberOfListeners, 0);
assert.equal(h.viewer.camera.changed.numberOfListeners, 0);
assert.equal(h.camera.listenerCount('moveEnd'), 0);
assert.equal(h.camera.listenerCount('changed'), 0);
});
test('movement during a pending refresh follows up at the newest view; disable cancels queued work', async (t) => {
@@ -162,12 +192,12 @@ test('movement during a pending refresh follows up at the newest view; disable c
h.layer.enable();
await h.layer.update();
h.setAnchor({ latitude: 54, longitude: 19 });
h.viewer.camera.moveEnd.raiseEvent();
h.camera.emit('moveEnd');
h.advance(2500);
t.mock.timers.tick(2500);
await flush();
h.setAnchor({ latitude: 35, longitude: 140 });
h.viewer.camera.changed.raiseEvent();
h.camera.emit('changed');
h.advance(2500);
t.mock.timers.tick(2500);
await flush();
@@ -180,7 +210,7 @@ test('movement during a pending refresh follows up at the newest view; disable c
assert.equal(calls.length, 3);
assert.deepEqual(calls.at(-1), { latitude: 35, longitude: 140 });
h.setAnchor({ latitude: 10, longitude: 10 });
h.viewer.camera.moveEnd.raiseEvent();
h.camera.emit('moveEnd');
h.layer.disable();
h.advance(5000);
t.mock.timers.tick(5000);
@@ -196,7 +226,7 @@ test('the layer requires a snapshot source and cannot be initialized twice', ()
});
test('an enabled refresh bins the snapshot into coloured ground cells', async () => {
const { layer, sources, anchors } = harness(async (anchor, { signal }) => {
const { layer, fills, anchors } = harness(async (anchor, { signal }) => {
assert.deepEqual(anchor, { latitude: 50, longitude: 30 });
assert.ok(signal instanceof AbortSignal);
return {
@@ -213,13 +243,17 @@ test('an enabled refresh bins the snapshot into coloured ground cells', async ()
assert.equal(await layer.update(), false, 'disabled layers do not fetch');
assert.equal(anchors.length, 0);
layer.enable();
assert.equal(sources[0].show, true);
assert.equal(fills().show, true);
assert.equal(await layer.update(), true);
const entities = sources[0].get(0).geometryInstances;
assert.equal(entities.length, 1, 'the one-aircraft cell is withheld');
assert.equal(entities[0].id, 'gnss-integrity:280:420');
assert.ok(entities[0].geometry instanceof Cesium.RectangleGeometry);
assert.equal(sources[0].get(0).allowPicking, false);
const cells = [...fills().items.values()];
assert.equal(cells.length, 1, 'the one-aircraft cell is withheld');
assert.equal(cells[0].id, 'gnss-integrity:280:420');
assert.deepEqual(cells[0].geometry, {
kind: 'rectangle',
rect: { west: 30, south: 50, east: 30.5, north: 50.5 },
});
assert.equal(cells[0].height, 'clampGround');
assert.equal(fills().spec.pickable, false, 'cells never take part in picks');
const stats = layer.getStats();
assert.equal(stats.count, 1);
assert.equal(stats.error, null);
@@ -272,7 +306,7 @@ test('the visible layer leads with low navigation accuracy and exposes its prove
test('a failed refresh keeps the last cells and reports the error', async () => {
let fail = false;
const { layer, sources } = harness(async () => {
const { layer, fills } = harness(async () => {
if (fail) throw new Error('adsb.lol HTTP 502');
return {
rows: [
@@ -288,14 +322,14 @@ test('a failed refresh keeps the last cells and reports the error', async () =>
assert.equal(layer.getStats().stale, true);
fail = true;
assert.equal(await layer.update(), true);
assert.equal(sources[0].get(0)?.geometryInstances.length ?? 0, 1);
assert.equal(fills().items.size, 1);
assert.equal(layer.getStats().error, 'adsb.lol HTTP 502');
assert.equal(layer.getStats().count, 1);
});
test('observations expire with the window, and disable aborts the request in flight', async () => {
let pending;
const { layer, sources, advance } = harness(
const { layer, fills, advance } = harness(
(anchor, { signal }) =>
new Promise((resolve, reject) => {
pending = { resolve };
@@ -315,17 +349,17 @@ test('observations expire with the window, and disable aborts the request in fli
pending.resolve({ rows: [] });
assert.equal(await result, true);
assert.equal(layer.getStats().count, 0);
assert.equal(sources[0].get(0)?.geometryInstances.length ?? 0, 0);
assert.equal(fills().items.size, 0);
result = layer.update();
layer.disable();
assert.equal(await result, false);
assert.equal(sources[0].show, false);
assert.equal(fills().show, false);
});
test('no view anchor means no request, and destroy releases the data source', async () => {
let calls = 0;
const { layer, sources } = harness(
const { layer, fills } = harness(
async () => {
calls += 1;
return { rows: [] };
@@ -341,13 +375,13 @@ test('no view anchor means no request, and destroy releases the data source', as
assert.equal(calls, 0);
assert.equal(layer.getStats().error, null);
layer.destroy();
assert.equal(sources.length, 0);
assert.equal(fills(), undefined, 'destroy disposes the set');
assert.equal(layer.getStats().count, 0);
});
test('a replayed stale snapshot keeps its observation age and expires on time', async () => {
// The proxy keeps answering with the snapshot it observed at t=1000.
const { layer, sources, advance, now } = harness(async () => ({
const { layer, fills, advance, now } = harness(async () => ({
rows: threeHealthy(),
ageMs: now() - 1_000,
stale: true,
@@ -369,12 +403,12 @@ test('a replayed stale snapshot keeps its observation age and expires on time',
0,
'the replay did not restart the window',
);
assert.equal(sources[0].get(0)?.geometryInstances.length ?? 0, 0);
assert.equal(fills().items.size, 0);
});
test('cells expire during prolonged fetch failures', async () => {
let fail = false;
const { layer, sources, advance } = harness(async () => {
const { layer, fills, advance } = harness(async () => {
if (fail) throw new Error('adsb.lol HTTP 502');
return { rows: threeHealthy() };
});
@@ -387,13 +421,13 @@ test('cells expire during prolonged fetch failures', async () => {
advance(11 * MIN);
assert.equal(await layer.update(), true);
assert.equal(layer.getStats().count, 0);
assert.equal(sources[0].get(0)?.geometryInstances.length ?? 0, 0);
assert.equal(fills().items.size, 0);
assert.equal(layer.getStats().error, 'adsb.lol HTTP 502');
});
test('cells expire while no view anchor is available', async () => {
let calls = 0;
const { layer, sources, advance, setAnchor } = harness(async () => {
const { layer, fills, advance, setAnchor } = harness(async () => {
calls += 1;
return { rows: threeHealthy() };
});
@@ -407,36 +441,36 @@ test('cells expire while no view anchor is available', async () => {
assert.equal(await layer.update(), true);
assert.equal(calls, 1, 'no anchor means no request');
assert.equal(layer.getStats().count, 0);
assert.equal(sources[0].get(0)?.geometryInstances.length ?? 0, 0);
assert.equal(fills().items.size, 0);
assert.equal(layer.getStats().error, null);
});
test('re-enabling after a long disable does not show cells past the window', async () => {
const { layer, sources, advance } = harness(async () => ({
const { layer, fills, advance } = harness(async () => ({
rows: threeHealthy(),
}));
layer.enable();
await layer.update();
assert.equal(sources[0].get(0)?.geometryInstances.length ?? 0, 1);
assert.equal(fills().items.size, 1);
layer.disable();
advance(31 * MIN);
layer.enable();
assert.equal(layer.getStats().count, 0);
assert.equal(sources[0].get(0)?.geometryInstances.length ?? 0, 0);
assert.equal(fills().items.size, 0);
});
const threeCells = () => ({
rows: [row('a1', 50.1, true), row('a2', 50.1, true), row('a3', 50.2, false)],
});
test('an identical snapshot leaves the drawn entities untouched', async () => {
const { layer, sources } = harness(async () => threeCells());
test('an identical snapshot draws nothing again', async () => {
const { layer, fills } = harness(async () => threeCells());
layer.enable();
await layer.update();
const first = sources[0].get(0);
const writes = fills().writes;
await layer.update();
assert.equal(sources[0].get(0)?.geometryInstances.length ?? 0, 1);
assert.equal(sources[0].get(0), first);
assert.equal(fills().items.size, 1);
assert.equal(fills().writes, writes);
});
test('an initial feed failure stays enabled, reports unavailable, and recovers automatically', async (t) => {
@@ -496,7 +530,7 @@ test('provider cooldown is preserved across movement and toggling; disable cance
h.layer.enable();
await h.layer.update();
h.setAnchor({ latitude: 10, longitude: 20 });
h.viewer.camera.moveEnd.raiseEvent();
h.camera.emit('moveEnd');
h.advance(30000);
t.mock.timers.tick(30000);
await flush();
@@ -583,7 +617,7 @@ test('GERMAN: camera debounce and minimum request gap are pinned', async (t) =>
h.layer.enable();
await h.layer.update();
h.setAnchor({ latitude: 51, longitude: 31 });
h.viewer.camera.changed.raiseEvent();
h.camera.emit('changed');
h.advance(2499);
t.mock.timers.tick(2499);
await flush();
@@ -594,7 +628,7 @@ test('GERMAN: camera debounce and minimum request gap are pinned', async (t) =>
assert.equal(calls, 2);
h.advance(3000);
h.setAnchor({ latitude: 52, longitude: 32 });
h.viewer.camera.changed.raiseEvent();
h.camera.emit('changed');
h.advance(599);
t.mock.timers.tick(599);
await flush();
@@ -619,7 +653,7 @@ test('camera movement during initial activation waits for acquisition before fol
h.layer.enable();
const initial = h.layer.update();
h.setAnchor({ latitude: 51, longitude: 31 });
h.viewer.camera.changed.raiseEvent();
h.camera.emit('changed');
h.advance(2500);
t.mock.timers.tick(2500);
await flush();
@@ -658,7 +692,7 @@ test('monotonic camera scheduling is unaffected by wall-clock rollback', async (
wall -= 3600000;
mono = 3000;
anchor = { latitude: 51, longitude: 31 };
h.viewer.camera.changed.raiseEvent();
h.camera.emit('changed');
mono += 600;
t.mock.timers.tick(600);
await flush();
@@ -666,35 +700,34 @@ test('monotonic camera scheduling is unaffected by wall-clock rollback', async (
layer.destroy();
});
test('GNSS fill emits no pick-pass commands but keeps normal rendering', async (t) => {
const h = harness(async () => ({ rows: threeHealthy() }));
h.layer.enable();
await h.layer.update();
const fill = h.sources[0].get(0);
let updates = 0;
t.mock.method(Cesium.GroundPrimitive.prototype, 'update', () => updates++);
fill.update({ passes: { pick: true } });
assert.equal(updates, 0);
fill.update({ passes: { render: true, pick: false } });
assert.equal(updates, 1);
h.layer.destroy();
assert.equal(fill.isDestroyed(), true);
});
test('view anchor falls back to rectangle, then nadir, then no camera', () => {
assert.equal(defaultGnssViewAnchor({}), null);
const viewer = {
camera: {
computeViewRectangle: () => Cesium.Rectangle.fromDegrees(20, 40, 22, 42),
const viewer = { scene: {} };
// Looking away from the globe: the line of sight misses it.
const camera = fakeCameraPort({
pose: {
lon: 21,
lat: 41,
position: fromDegrees(21, 41, 1e7),
direction: { x: 0, y: 0, z: 1 },
},
};
viewRectangle: { west: 20, south: 40, east: 22, north: 42 },
});
bindRenderer(viewer, { camera: camera.port });
const rectangleAnchor = defaultGnssViewAnchor(viewer);
assert.ok(Math.abs(rectangleAnchor.latitude - 41) < 1e-10);
assert.ok(Math.abs(rectangleAnchor.longitude - 21) < 1e-10);
viewer.camera = {
positionCartographic: Cesium.Cartographic.fromDegrees(21, 41),
};
// Across the antimeridian, the middle is on it.
camera.setViewRectangle({ west: 170, south: 40, east: -170, north: 42 });
assert.ok(
Math.abs(Math.abs(defaultGnssViewAnchor(viewer).longitude) - 180) < 1e-10,
);
camera.setViewRectangle({ west: 175, south: 40, east: -165, north: 42 });
assert.ok(Math.abs(defaultGnssViewAnchor(viewer).longitude + 175) < 1e-10);
camera.setViewRectangle(null);
const nadirAnchor = defaultGnssViewAnchor(viewer);
assert.ok(Math.abs(nadirAnchor.latitude - 41) < 1e-10);
assert.ok(Math.abs(nadirAnchor.longitude - 21) < 1e-10);
camera.setPose({ lon: NaN, lat: NaN });
assert.equal(defaultGnssViewAnchor(viewer), null, 'no settled camera');
});