Files
gods-eye-view/src/layers/traffic/footprint.test.mjs
T
Sameh Khamis a8cf63999b Read the street traffic viewport through the camera and query ports
The traffic layer's camera altitude, view bounds and fetch centre come from
the renderer's camera port (its pose and view rectangle), and the reticle
footprint samples the ground with the query port's ellipsoid picks and
rays instead of the camera's own. The footprint's nadir is the pose
position in geodetic radians, as the camera's own cartographic position
was. A pixel the renderer has no ray for (a canvas of no size) samples no
direction, as the unset scratch ray did. The lifecycle part loads the
default renderer's camera factory, so a viewer of a caller's own gets
both ports.

Footprint and viewport leave the allow-list.
2026-11-07 00:09:10 -08:00

142 lines
4.0 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import * as Cesium from 'cesium';
import {
convexFootprint,
footprintTiles,
createReticleFootprint,
} from './footprint.js';
import { tileToBBox, tilesForBounds } from '../../data/tomtomTiles.js';
import { outsideDetailTiles } from './source.js';
test('footprint selection excludes bbox corners and orders tiles nearest first', () => {
const polygon = convexFootprint([
[0, 0],
[0.15, 0],
[0, 0.15],
[0.01, 0.01],
]);
const tiles = footprintTiles(polygon, 12, { lat: 0, lon: 0 });
const bbox = tilesForBounds(
{ west: 0, east: 0.15, south: 0, north: 0.15 },
12,
);
assert.ok(tiles.length < bbox.length);
const b = tileToBBox(tiles[0].z, tiles[0].x, tiles[0].y);
assert.ok(b.west <= 0 && b.east >= 0 && b.south <= 0 && b.north >= 0);
});
test('oblique horizon bearings are bounded and tile caps hold at city latitudes', () => {
for (const [lat, lon] of [
[24.964, 55.037],
[30.2672, -97.7431],
[51.507, -0.128],
[70, 10],
]) {
const fake = {
pickEllipsoid(p, e, result) {
if (p.y < 300) return undefined;
return Cesium.Cartesian3.fromDegrees(
lon + (p.x - 640) / 100000,
lat + (800 - p.y) / 50000,
0,
e,
result,
);
},
getPickRay(p, ray) {
ray.direction.x =
-Math.sin((lat * Math.PI) / 180) * Math.cos((lon * Math.PI) / 180);
ray.direction.y =
-Math.sin((lat * Math.PI) / 180) * Math.sin((lon * Math.PI) / 180);
ray.direction.z = Math.cos((lat * Math.PI) / 180);
return ray;
},
};
const camera = {
pose: () => ({
position: Cesium.Cartesian3.fromDegrees(lon, lat, 300),
heading: 0,
pitch: -Math.PI / 12,
}),
};
const query = {
pickEllipsoid: (x, y) =>
fake.pickEllipsoid({ x, y }, Cesium.Ellipsoid.WGS84) ?? null,
pickRay: (x, y) =>
fake.getPickRay({ x, y }, { direction: new Cesium.Cartesian3() }),
};
const result = createReticleFootprint()(
{ camera, query, width: 1280, height: 800 },
300,
);
assert.ok(result.coverage.coarse.length <= 16);
assert.ok(result.coverage.detail.length <= 16);
assert.ok(
result.north > lat + 0.04,
'upper footprint extends past old fixed box',
);
assert.ok(result.coverage.rangeKm <= 10);
}
});
test('coarse roads retain far segments but never duplicate a detailed tile core', () => {
const tile = { z: 14, x: 8192, y: 8192 },
b = tileToBBox(tile.z, tile.x, tile.y);
const y = (b.south + b.north) / 2;
const roads = outsideDetailTiles(
[
{
coordinates: [
[b.west - 0.01, y],
[b.east + 0.01, y],
],
type: 'primary',
},
],
[tile],
);
assert.equal(roads.length, 2);
assert.equal(roads[0].coordinates.at(-1)[0], b.west);
assert.equal(roads[1].coordinates[0][0], b.east);
});
test('without a camera or query port there is no footprint', () => {
const footprint = createReticleFootprint();
const query = { pickEllipsoid: () => null, pickRay: () => null };
assert.equal(
footprint({ camera: null, query, width: 10, height: 10 }, 300),
null,
);
assert.equal(
footprint(
{ camera: { pose: () => ({}) }, query: null, width: 10, height: 10 },
300,
),
null,
);
});
test('a pixel with no pick ray samples no direction, as before', () => {
const query = {
pickEllipsoid: () => null,
pickRay: () => {
throw new TypeError('no ray on a canvas of no size');
},
};
const camera = {
pose: () => ({
position: Cesium.Cartesian3.fromDegrees(10, 20, 300),
heading: 0,
pitch: -Math.PI / 2,
}),
};
const result = createReticleFootprint()(
{ camera, query, width: 0, height: 0 },
300,
);
assert.ok(Math.abs(result.coverage.near.lat - 20) < 1e-9);
assert.ok(Math.abs(result.coverage.near.lon - 10) < 1e-9);
assert.deepEqual(result.coverage.coarse, []);
});