mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
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.
This commit is contained in:
1 parent
10db177251
commit
a8cf63999b
8 files changed
+204
-75
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import { toCartographicRadians } from '../../geo/ellipsoid.js';
|
||||
import { toDegrees } from '../../geo/vector.js';
|
||||
import { tilesForBounds, tileToBBox } from '../../data/tomtomTiles.js';
|
||||
|
||||
const RETICLE_RANGE_KM = 10;
|
||||
@@ -62,49 +63,60 @@ export function footprintTiles(polygon, zoom, near) {
|
||||
});
|
||||
}
|
||||
|
||||
/** Reusable camera scratch; sampled only on camera events, never in the animation loop. */
|
||||
const NO_DIRECTION = Object.freeze({ x: 0, y: 0, z: 0 });
|
||||
|
||||
/**
|
||||
* The pick ray's direction at a pixel; none (a zero vector, as an unset ray
|
||||
* had) where the renderer has no ray there, such as on a canvas of no size.
|
||||
*/
|
||||
function rayDirection(query, x, y) {
|
||||
try {
|
||||
return query.pickRay(x, y)?.direction ?? NO_DIRECTION;
|
||||
} catch {
|
||||
return NO_DIRECTION;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The ground footprint of the reticle square, sampled through the camera and
|
||||
* query ports on camera events only, never in the animation loop. Null
|
||||
* without both ports.
|
||||
* @returns {(view: {camera: object|null, query: object|null, width: number,
|
||||
* height: number}, altitude: number) => object|null}
|
||||
*/
|
||||
export function createReticleFootprint() {
|
||||
const pixels = Array.from({ length: 25 }, () => new Cesium.Cartesian2());
|
||||
const hits = Array.from({ length: 25 }, () => new Cesium.Cartesian3());
|
||||
const carto = new Cesium.Cartographic();
|
||||
const ray = new Cesium.Ray();
|
||||
return (viewer, altitude) => {
|
||||
const { camera, scene } = viewer;
|
||||
if (!camera.getPickRay) return null;
|
||||
const nadir = camera.positionCartographic;
|
||||
return ({ camera, query, width, height }, altitude) => {
|
||||
if (!camera || !query) return null;
|
||||
const pose = camera.pose();
|
||||
const nadir = toCartographicRadians(pose.position);
|
||||
if (!nadir) return null;
|
||||
const near = {
|
||||
lat: Cesium.Math.toDegrees(nadir.latitude),
|
||||
lon: Cesium.Math.toDegrees(nadir.longitude),
|
||||
lat: toDegrees(nadir.lat),
|
||||
lon: toDegrees(nadir.lon),
|
||||
};
|
||||
const width = scene.canvas.clientWidth || scene.canvas.width,
|
||||
height = scene.canvas.clientHeight || scene.canvas.height;
|
||||
const side = Math.min(width, height),
|
||||
left = (width - side) / 2,
|
||||
top = (height - side) / 2;
|
||||
const points = [];
|
||||
for (let row = 0; row < 5; row++)
|
||||
for (let col = 0; col < 5; col++) {
|
||||
const i = row * 5 + col,
|
||||
p = pixels[i];
|
||||
p.x = left + (side * col) / 4;
|
||||
p.y = top + (side * row) / 4;
|
||||
const hit = camera.pickEllipsoid(p, Cesium.Ellipsoid.WGS84, hits[i]);
|
||||
const x = left + (side * col) / 4,
|
||||
y = top + (side * row) / 4;
|
||||
const hit = query.pickEllipsoid(x, y);
|
||||
const carto = hit && toCartographicRadians(hit);
|
||||
let dx, dy;
|
||||
if (hit) {
|
||||
Cesium.Cartographic.fromCartesian(hit, Cesium.Ellipsoid.WGS84, carto);
|
||||
dy = (Cesium.Math.toDegrees(carto.latitude) - near.lat) * 111.195;
|
||||
let dlon = Cesium.Math.toDegrees(carto.longitude) - near.lon;
|
||||
if (carto) {
|
||||
dy = (toDegrees(carto.lat) - near.lat) * 111.195;
|
||||
let dlon = toDegrees(carto.lon) - near.lon;
|
||||
dlon -= 360 * Math.round(dlon / 360);
|
||||
dx = dlon * 111.195 * Math.cos(nadir.latitude);
|
||||
dx = dlon * 111.195 * Math.cos(nadir.lat);
|
||||
} else {
|
||||
camera.getPickRay(p, ray);
|
||||
const d = ray.direction;
|
||||
dx =
|
||||
-Math.sin(nadir.longitude) * d.x + Math.cos(nadir.longitude) * d.y;
|
||||
const d = rayDirection(query, x, y);
|
||||
dx = -Math.sin(nadir.lon) * d.x + Math.cos(nadir.lon) * d.y;
|
||||
dy =
|
||||
-Math.sin(nadir.latitude) * Math.cos(nadir.longitude) * d.x -
|
||||
Math.sin(nadir.latitude) * Math.sin(nadir.longitude) * d.y +
|
||||
Math.cos(nadir.latitude) * d.z;
|
||||
-Math.sin(nadir.lat) * Math.cos(nadir.lon) * d.x -
|
||||
Math.sin(nadir.lat) * Math.sin(nadir.lon) * d.y +
|
||||
Math.cos(nadir.lat) * d.z;
|
||||
const length = Math.hypot(dx, dy) || 1;
|
||||
dx = (dx / length) * RETICLE_RANGE_KM;
|
||||
dy = (dy / length) * RETICLE_RANGE_KM;
|
||||
@@ -120,8 +132,7 @@ export function createReticleFootprint() {
|
||||
const scale = Math.min(1, range / Math.max(1e-9, Math.hypot(dx, dy)));
|
||||
return [
|
||||
near.lon +
|
||||
(dx * scale) /
|
||||
(111.195 * Math.max(0.05, Math.cos(nadir.latitude))),
|
||||
(dx * scale) / (111.195 * Math.max(0.05, Math.cos(nadir.lat))),
|
||||
near.lat + (dy * scale) / 111.195,
|
||||
];
|
||||
}),
|
||||
@@ -151,11 +162,11 @@ export function createReticleFootprint() {
|
||||
coarse,
|
||||
detail,
|
||||
viewKey: [
|
||||
nadir.latitude,
|
||||
nadir.longitude,
|
||||
nadir.lat,
|
||||
nadir.lon,
|
||||
nadir.height / 10000,
|
||||
camera.heading,
|
||||
camera.pitch,
|
||||
pose.heading,
|
||||
pose.pitch,
|
||||
]
|
||||
.map((v) => Number(v || 0).toFixed(7))
|
||||
.join(','),
|
||||
|
||||
@@ -33,10 +33,7 @@ test('oblique horizon bearings are bounded and tile caps hold at city latitudes'
|
||||
[51.507, -0.128],
|
||||
[70, 10],
|
||||
]) {
|
||||
const camera = {
|
||||
positionCartographic: Cesium.Cartographic.fromDegrees(lon, lat, 300),
|
||||
heading: 0,
|
||||
pitch: -Math.PI / 12,
|
||||
const fake = {
|
||||
pickEllipsoid(p, e, result) {
|
||||
if (p.y < 300) return undefined;
|
||||
return Cesium.Cartesian3.fromDegrees(
|
||||
@@ -56,8 +53,21 @@ test('oblique horizon bearings are bounded and tile caps hold at city latitudes'
|
||||
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, scene: { canvas: { width: 1280, height: 800 } } },
|
||||
{ camera, query, width: 1280, height: 800 },
|
||||
300,
|
||||
);
|
||||
assert.ok(result.coverage.coarse.length <= 16);
|
||||
@@ -90,3 +100,42 @@ test('coarse roads retain far segments but never duplicate a detailed tile core'
|
||||
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, []);
|
||||
});
|
||||
@@ -1,5 +1,9 @@
|
||||
import { observeTrafficSurface } from './surface.js';
|
||||
import * as Cesium from 'cesium';
|
||||
// Still Cesium for its points and events, so this layer loads the unbound
|
||||
// camera factory (with its query port) its other parts reach the renderer
|
||||
// through.
|
||||
import '../../render/cesium/camera.js';
|
||||
import { TRAFFIC_TIMING_ENABLED } from './policy.js';
|
||||
import {
|
||||
claimCameraSensitivity,
|
||||
|
||||
@@ -2,6 +2,7 @@ import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import * as Cesium from 'cesium';
|
||||
import { createTrafficLayer } from './index.js';
|
||||
import { bindFakeCesiumCamera } from '../../testSupport/fakeCameraPort.mjs';
|
||||
|
||||
function deferred() {
|
||||
let resolve;
|
||||
@@ -11,7 +12,12 @@ function deferred() {
|
||||
return { promise, resolve };
|
||||
}
|
||||
|
||||
function setup(t, requestRoads, getStatus = async () => ({ hasKey: false })) {
|
||||
function setup(
|
||||
t,
|
||||
requestRoads,
|
||||
getStatus = async () => ({ hasKey: false }),
|
||||
ports = {},
|
||||
) {
|
||||
t.mock.timers.enable({ apis: ['setTimeout', 'setInterval'] });
|
||||
const camera = {
|
||||
positionCartographic: Cesium.Cartographic.fromDegrees(
|
||||
@@ -49,6 +55,8 @@ function setup(t, requestRoads, getStatus = async () => ({ hasKey: false })) {
|
||||
primitives: { add: (value) => value, remove: () => true },
|
||||
},
|
||||
};
|
||||
// The layer reads the camera through its port.
|
||||
bindFakeCesiumCamera(viewer, ports);
|
||||
const layer = createTrafficLayer({
|
||||
services: {
|
||||
credits: {},
|
||||
@@ -184,7 +192,14 @@ test('arrival below the camera change threshold loads the final city and unsubsc
|
||||
seen.push(bounds);
|
||||
return roads(bounds);
|
||||
});
|
||||
// The camera port listens for itself; the layer's own listeners go.
|
||||
const ported = {
|
||||
moveEnd: viewer.camera.moveEnd.numberOfListeners,
|
||||
changed: viewer.camera.changed.numberOfListeners,
|
||||
};
|
||||
layer.enable(viewer);
|
||||
assert.ok(viewer.camera.moveEnd.numberOfListeners > ported.moveEnd);
|
||||
assert.ok(viewer.camera.changed.numberOfListeners > ported.changed);
|
||||
await tick(400);
|
||||
viewer.camera.positionCartographic = Cesium.Cartographic.fromDegrees(
|
||||
-0.1276,
|
||||
@@ -195,8 +210,8 @@ test('arrival below the camera change threshold loads the final city and unsubsc
|
||||
await tick(400);
|
||||
assert.ok(seen.some((bounds) => bounds.south > 50));
|
||||
layer.disable(viewer);
|
||||
assert.equal(viewer.camera.moveEnd.numberOfListeners, 0);
|
||||
assert.equal(viewer.camera.changed.numberOfListeners, 0);
|
||||
assert.equal(viewer.camera.moveEnd.numberOfListeners, ported.moveEnd);
|
||||
assert.equal(viewer.camera.changed.numberOfListeners, ported.changed);
|
||||
});
|
||||
|
||||
test('parked failures back off and disabling cancels the scheduled retry', async (t) => {
|
||||
@@ -365,17 +380,24 @@ test('coverage changes at the same center reload and disabling invalidates cover
|
||||
|
||||
test('a settled footprint loads new tiles even when the camera centre stays put', async (t) => {
|
||||
const seen = [];
|
||||
// The footprint asks the query port where the reticle meets the ground.
|
||||
const query = {
|
||||
pickEllipsoid: (x, y) =>
|
||||
viewer.camera.pickEllipsoid({ x, y }, Cesium.Ellipsoid.WGS84),
|
||||
pickRay: () => null,
|
||||
};
|
||||
const { layer, viewer, tick } = setup(
|
||||
t,
|
||||
async (bounds, { majorOnly, coverage }) => {
|
||||
if (!majorOnly) seen.push({ ...bounds, coverage });
|
||||
return roads(bounds);
|
||||
},
|
||||
undefined,
|
||||
{ query },
|
||||
);
|
||||
layer.setParams({ roadMode: 'osm' });
|
||||
let reach = 0.005;
|
||||
const camera = viewer.camera;
|
||||
camera.getPickRay = () => null;
|
||||
camera.heading = 0;
|
||||
camera.pitch = -Math.PI / 2;
|
||||
camera.pickEllipsoid = (pixel, ellipsoid, result) =>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { createReticleFootprint } from './footprint.js';
|
||||
import * as Cesium from 'cesium';
|
||||
import { toLonLatH } from '../../geo/ellipsoid.js';
|
||||
import { findCamera, findQuery } from '../../render/rendererOf.js';
|
||||
import {
|
||||
deriveFetchCenter,
|
||||
clampBoundsAroundCenter,
|
||||
@@ -15,6 +16,23 @@ import {
|
||||
|
||||
export function createViewport({ state: layerState, services, parts, source }) {
|
||||
const reticleFootprint = createReticleFootprint();
|
||||
|
||||
/**
|
||||
* The renderer's camera and query ports for the layer's scene: the bound
|
||||
* renderer's, or those the default renderer's adapter builds over a viewer
|
||||
* renderer bound.
|
||||
*/
|
||||
const cameraPort = () => findCamera(layerState._viewer);
|
||||
const queryPort = () => findQuery(layerState._viewer);
|
||||
|
||||
/** The canvas size in CSS pixels, as the layer has always read it. */
|
||||
function canvasSize() {
|
||||
const canvas = layerState._viewer.scene.canvas;
|
||||
return {
|
||||
width: canvas.clientWidth || canvas.width,
|
||||
height: canvas.clientHeight || canvas.height,
|
||||
};
|
||||
}
|
||||
// ─── Camera Monitoring ─────────────────────────────────────
|
||||
|
||||
/**
|
||||
@@ -23,8 +41,8 @@ export function createViewport({ state: layerState, services, parts, source }) {
|
||||
*/
|
||||
|
||||
function getCameraAltitude() {
|
||||
const carto = layerState._viewer.camera.positionCartographic;
|
||||
return carto ? carto.height : Infinity;
|
||||
const height = cameraPort()?.pose().height;
|
||||
return Number.isFinite(height) ? height : Infinity;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,14 +52,7 @@ export function createViewport({ state: layerState, services, parts, source }) {
|
||||
*/
|
||||
|
||||
function getViewBounds() {
|
||||
const rect = layerState._viewer.camera.computeViewRectangle();
|
||||
if (!rect) return null;
|
||||
return {
|
||||
south: Cesium.Math.toDegrees(rect.south),
|
||||
west: Cesium.Math.toDegrees(rect.west),
|
||||
north: Cesium.Math.toDegrees(rect.north),
|
||||
east: Cesium.Math.toDegrees(rect.east),
|
||||
};
|
||||
return cameraPort()?.viewRectangle() ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,25 +70,21 @@ export function createViewport({ state: layerState, services, parts, source }) {
|
||||
*/
|
||||
|
||||
function getFetchCenter() {
|
||||
const carto = layerState._viewer.camera.positionCartographic;
|
||||
if (!carto) return null;
|
||||
const nadirLat = Cesium.Math.toDegrees(carto.latitude);
|
||||
const nadirLon = Cesium.Math.toDegrees(carto.longitude);
|
||||
const pose = cameraPort()?.pose();
|
||||
if (!pose || !Number.isFinite(pose.lat) || !Number.isFinite(pose.lon))
|
||||
return null;
|
||||
const nadirLat = pose.lat;
|
||||
const nadirLon = pose.lon;
|
||||
|
||||
let hitLat;
|
||||
let hitLon;
|
||||
const canvas = layerState._viewer.scene.canvas;
|
||||
const width = canvas.clientWidth || canvas.width;
|
||||
const height = canvas.clientHeight || canvas.height;
|
||||
const { width, height } = canvasSize();
|
||||
if (width > 0 && height > 0) {
|
||||
const hit = layerState._viewer.camera.pickEllipsoid(
|
||||
new Cesium.Cartesian2(width / 2, height / 2),
|
||||
Cesium.Ellipsoid.WGS84,
|
||||
);
|
||||
if (hit) {
|
||||
const hitCarto = Cesium.Cartographic.fromCartesian(hit);
|
||||
hitLat = Cesium.Math.toDegrees(hitCarto.latitude);
|
||||
hitLon = Cesium.Math.toDegrees(hitCarto.longitude);
|
||||
const hit = queryPort()?.pickEllipsoid(width / 2, height / 2);
|
||||
const hitAt = hit && toLonLatH(hit);
|
||||
if (hitAt) {
|
||||
hitLat = hitAt.lat;
|
||||
hitLon = hitAt.lon;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -199,7 +206,10 @@ export function createViewport({ state: layerState, services, parts, source }) {
|
||||
if (!layerState._enabled) return;
|
||||
const alt = getCameraAltitude();
|
||||
if (alt > ACTIVATION_ALTITUDE) return onCameraChanged();
|
||||
const footprint = reticleFootprint(layerState._viewer, alt);
|
||||
const footprint = reticleFootprint(
|
||||
{ camera: cameraPort(), query: queryPort(), ...canvasSize() },
|
||||
alt,
|
||||
);
|
||||
const bounds = footprint || getViewBounds();
|
||||
if (!bounds) return;
|
||||
const center = footprint ? null : getFetchCenter();
|
||||
|
||||
Reference in new issue
Block a user