Move radio navigation and viewport queries onto the camera port

Radio's camera state, globe recenter plan and viewport anchor now read the
camera pose instead of the Cesium camera. The anchor intersects the view
ray with the ellipsoid through a shared rayEllipsoid helper, moved into
geo/ellipsoid from the stub renderer and checked against Cesium. Scratch
vectors are plain objects. navigation, queries and state leave the Cesium
allow-list; rendering and lifecycle stay, since they rely on entity
clustering, which the draw port does not offer yet.
This commit is contained in:
Sameh Khamis committed 2026-11-06 20:26:35 -08:00
1 parent 8c6fa6258b
commit 96d860854f
9 files changed
+127 -80

No files matched your search

+27
View File
@@ -149,3 +149,30 @@ export function toLonLatH(p) {
if (!c) return null; if (!c) return null;
return { lon: toDegrees(c.lon), lat: toDegrees(c.lat), height: c.height }; return { lon: toDegrees(c.lon), lat: toDegrees(c.lat), height: c.height };
} }
/**
* Where a ray first meets the ellipsoid ahead of its origin, as Cesium's
* `camera.pickEllipsoid` takes the hit, or null when it misses.
* @param {{origin: {x: number, y: number, z: number},
* direction: {x: number, y: number, z: number}}} ray
*/
export function rayEllipsoid(ray) {
const r = WGS84.oneOverRadii;
const q = vec(ray.origin.x * r.x, ray.origin.y * r.y, ray.origin.z * r.z);
const w = vec(
ray.direction.x * r.x,
ray.direction.y * r.y,
ray.direction.z * r.z,
);
const a = dot(w, w);
const b = 2 * dot(q, w);
const c = dot(q, q) - 1;
const disc = b * b - 4 * a * c;
if (disc < 0) return null;
const sq = Math.sqrt(disc);
const t0 = (-b - sq) / (2 * a);
const t1 = (-b + sq) / (2 * a);
const t = t0 >= 0 ? t0 : t1 >= 0 ? t1 : null;
if (t === null) return null;
return add(ray.origin, scale(ray.direction, t));
}
+42
View File
@@ -2,9 +2,11 @@ import assert from 'node:assert/strict';
import test from 'node:test'; import test from 'node:test';
import * as Cesium from 'cesium'; import * as Cesium from 'cesium';
import { import {
distance,
eastNorthUp, eastNorthUp,
fromDegrees, fromDegrees,
geodeticSurfaceNormal, geodeticSurfaceNormal,
rayEllipsoid,
scaleToGeodeticSurface, scaleToGeodeticSurface,
toCartographicRadians, toCartographicRadians,
toDegrees, toDegrees,
@@ -167,3 +169,43 @@ test('east-north-up frames match the default renderer', () => {
closeVec(ours.origin, column(m, 3), 0, `${where} origin`); closeVec(ours.origin, column(m, 3), 0, `${where} origin`);
} }
}); });
test('a ray meets the ellipsoid where Cesium picks it', () => {
const rand = random(7);
let misses = 0;
for (let i = 0; i < 200; i++) {
const origin = new Cesium.Cartesian3(
(rand() - 0.5) * 4e7,
(rand() - 0.5) * 4e7,
(rand() - 0.5) * 4e7,
);
const toward = new Cesium.Cartesian3(
(rand() - 0.5) * 2.4e7,
(rand() - 0.5) * 2.4e7,
(rand() - 0.5) * 2.4e7,
);
const direction = Cesium.Cartesian3.normalize(
Cesium.Cartesian3.subtract(toward, origin, new Cesium.Cartesian3()),
new Cesium.Cartesian3(),
);
const ray = new Cesium.Ray(origin, direction);
// As `camera.pickEllipsoid` takes the hit.
const span = Cesium.IntersectionTests.rayEllipsoid(
ray,
Cesium.Ellipsoid.WGS84,
);
const ours = rayEllipsoid({ origin, direction });
if (!span) {
misses += 1;
assert.equal(ours, null);
continue;
}
const expected = Cesium.Ray.getPoint(
ray,
span.start > 0 ? span.start : span.stop,
);
assert.ok(ours, 'a hit');
assert.ok(distance(ours, expected) < 1e-3, `ray ${i}`);
}
assert.ok(misses > 0 && misses < 200, 'hits and misses both tried');
});
+3
View File
@@ -1,4 +1,7 @@
import * as Cesium from 'cesium'; import * as Cesium from 'cesium';
// The Cesium camera factory: given a viewer of a caller's own, with no
// renderer bound, the layer's flights and camera reads find a camera port.
import '../../render/cesium/camera.js';
import { import {
RADIO_OVERLAY_SOURCE_ID, RADIO_OVERLAY_SOURCE_ID,
DEFAULT_RADIO_VOLUME, DEFAULT_RADIO_VOLUME,
+21 -25
View File
@@ -1,11 +1,12 @@
import { isFollowing } from '../../data/followed.js'; import { isFollowing } from '../../data/followed.js';
import * as Cesium from 'cesium'; import { fromDegrees } from '../../geo/ellipsoid.js';
import { dot, magnitude } from '../../geo/vector.js';
import { import {
HORIZON_CAMERA_MOVE_EPSILON_M, HORIZON_CAMERA_MOVE_EPSILON_M,
GLOBAL_RADIO_ALTITUDE_M, GLOBAL_RADIO_ALTITUDE_M,
RADIO_GLOBE_RECENTER_MAX_HEIGHT_M, RADIO_GLOBE_RECENTER_MAX_HEIGHT_M,
} from './policy.js'; } from './policy.js';
import { cameraOf } from '../../render/cesium/camera.js'; import { cameraOf, findCamera } from '../../render/rendererOf.js';
export function createNavigation({ export function createNavigation({
state: layerState, state: layerState,
@@ -137,13 +138,14 @@ export function createNavigation({
: currentHeight; : currentHeight;
} }
function radioCameraState(camera = layerState._viewer?.camera) { function radioCameraState() {
if (!camera) return null; const pose = findCamera(layerState._viewer)?.pose();
if (!pose) return null;
return { return {
height: camera.positionCartographic?.height, height: pose.height,
heading: camera.heading, heading: pose.heading,
pitch: camera.pitch, pitch: pose.pitch,
roll: camera.roll, roll: pose.roll,
}; };
} }
@@ -154,12 +156,13 @@ export function createNavigation({
} }
function radioGlobeRecenterPlan(viewer = layerState._viewer) { function radioGlobeRecenterPlan(viewer = layerState._viewer) {
const camera = viewer?.camera; const pose = viewer?.camera ? findCamera(viewer)?.pose() : null;
const canvas = viewer?.scene?.canvas; const canvas = viewer?.scene?.canvas;
const width = canvas?.clientWidth || canvas?.width; const width = canvas?.clientWidth || canvas?.width;
const height = canvas?.clientHeight || canvas?.height; const height = canvas?.clientHeight || canvas?.height;
const cartographic = camera?.positionCartographic; // The eye at the earth's centre has no geodetic position.
if (!camera || !cartographic || !(width > 0) || !(height > 0)) return null; if (!pose || !Number.isFinite(pose.height) || !(width > 0) || !(height > 0))
return null;
const geometry = projectEarthDiscToViewport( const geometry = projectEarthDiscToViewport(
viewer, viewer,
width, width,
@@ -180,36 +183,29 @@ export function createNavigation({
); );
} else { } else {
const earthRadius = earthDiscScreenRadius( const earthRadius = earthDiscScreenRadius(
Cesium.Cartesian3.magnitude(camera.positionWC), magnitude(pose.position),
height, height,
camera.frustum?.fovy, pose.fovy,
); );
const facingEarthCenter = const facingEarthCenter =
Cesium.Cartesian3.dot( dot(pose.direction, layerState._radioEarthToCenter) > 0;
camera.directionWC,
layerState._radioEarthToCenter,
) > 0;
if (!earthRadius || facingEarthCenter) return null; if (!earthRadius || facingEarthCenter) return null;
const keyhole = getKeyholeGeometry(width, height); const keyhole = getKeyholeGeometry(width, height);
fullGlobeCapable = fullGlobeCapable =
earthRadius + GLOBE_ENTER_CLEARANCE_PX <= keyhole.radius; earthRadius + GLOBE_ENTER_CLEARANCE_PX <= keyhole.radius;
} }
const recenterHeight = radioGlobeRecenterHeight( const recenterHeight = radioGlobeRecenterHeight(
cartographic.height, pose.height,
fullGlobeCapable, fullGlobeCapable,
); );
if (recenterHeight == null) return null; if (recenterHeight == null) return null;
return { return {
destination: Cesium.Cartesian3.fromRadians( destination: fromDegrees(pose.lon, pose.lat, recenterHeight),
cartographic.longitude,
cartographic.latitude,
recenterHeight,
),
cameraState: { cameraState: {
...radioCameraState(camera), ...radioCameraState(),
height: recenterHeight, height: recenterHeight,
heading: 0, heading: 0,
pitch: -Cesium.Math.PI_OVER_TWO, pitch: -Math.PI / 2,
roll: 0, roll: 0,
}, },
}; };
+22 -26
View File
@@ -1,12 +1,14 @@
import * as Cesium from 'cesium'; import { rayEllipsoid, toLonLatH } from '../../geo/ellipsoid.js';
import { toRadians } from '../../geo/vector.js';
import { findCamera } from '../../render/rendererOf.js';
import { normalizeRadioCountryInput } from '../../data/radioCountry.js'; import { normalizeRadioCountryInput } from '../../data/radioCountry.js';
export function createQueries({ state: layerState, services, parts, source }) { export function createQueries({ state: layerState, services, parts, source }) {
function radioAngularDistance(station, anchor) { function radioAngularDistance(station, anchor) {
const lat1 = Cesium.Math.toRadians(Number(anchor?.lat)); const lat1 = toRadians(Number(anchor?.lat));
const lon1 = Cesium.Math.toRadians(Number(anchor?.lon)); const lon1 = toRadians(Number(anchor?.lon));
const lat2 = Cesium.Math.toRadians(Number(station?.lat)); const lat2 = toRadians(Number(station?.lat));
const lon2 = Cesium.Math.toRadians(Number(station?.lon)); const lon2 = toRadians(Number(station?.lon));
if (![lat1, lon1, lat2, lon2].every(Number.isFinite)) if (![lat1, lon1, lat2, lon2].every(Number.isFinite))
return Number.POSITIVE_INFINITY; return Number.POSITIVE_INFINITY;
const deltaLat = lat2 - lat1; const deltaLat = lat2 - lat1;
@@ -106,28 +108,22 @@ export function createQueries({ state: layerState, services, parts, source }) {
} }
function viewportRadioAnchor() { function viewportRadioAnchor() {
const camera = layerState._viewer?.camera; const pose = findCamera(layerState._viewer)?.pose();
const scene = layerState._viewer?.scene; if (!pose) return null;
if (!camera) return null; const altitudeM = Number(pose.height);
const altitudeM = Number(camera.positionCartographic?.height); // Where the view's centre meets the ellipsoid (the canvas centre's ray
let cartographic = null; // is the view direction), else the point under the eye.
const canvas = scene?.canvas; const hit = rayEllipsoid({
if (canvas && typeof camera.pickEllipsoid === 'function') { origin: pose.position,
const center = new Cesium.Cartesian2( direction: pose.direction,
canvas.clientWidth / 2, });
canvas.clientHeight / 2, const at = hit ? toLonLatH(hit) : null;
); const lat = at ? at.lat : pose.lat;
const position = camera.pickEllipsoid( const lon = at ? at.lon : pose.lon;
center, if (!Number.isFinite(lat) || !Number.isFinite(lon)) return null;
scene.globe?.ellipsoid || Cesium.Ellipsoid.WGS84,
);
if (position) cartographic = Cesium.Cartographic.fromCartesian(position);
}
cartographic ||= camera.positionCartographic || null;
if (!cartographic) return null;
return { return {
lat: Cesium.Math.toDegrees(cartographic.latitude), lat,
lon: Cesium.Math.toDegrees(cartographic.longitude), lon,
altitudeM, altitudeM,
globalView: parts.navigation.radioViewIsGlobal(altitudeM), globalView: parts.navigation.radioViewIsGlobal(altitudeM),
}; };
+3 -3
View File
@@ -1,4 +1,3 @@
import * as Cesium from 'cesium';
import { import {
EMPTY_ACCEPTED_CATALOG_SNAPSHOT, EMPTY_ACCEPTED_CATALOG_SNAPSHOT,
DEFAULT_RADIO_FILTER, DEFAULT_RADIO_FILTER,
@@ -8,9 +7,10 @@ import {
export function createState({ services }) { export function createState({ services }) {
const layerState = {}; const layerState = {};
layerState._radioEarthScreenCenter = new Cesium.Cartesian2(); // Scratch results the Earth-disc projection writes into.
layerState._radioEarthScreenCenter = { x: 0, y: 0 };
layerState._radioEarthToCenter = new Cesium.Cartesian3(); layerState._radioEarthToCenter = { x: 0, y: 0, z: 0 };
layerState._viewer = null; layerState._viewer = null;
-3
View File
@@ -119,10 +119,7 @@
"src/layers/military/tracking.js", "src/layers/military/tracking.js",
"src/layers/perimeters/index.js", "src/layers/perimeters/index.js",
"src/layers/radio/lifecycle.js", "src/layers/radio/lifecycle.js",
"src/layers/radio/navigation.js",
"src/layers/radio/queries.js",
"src/layers/radio/rendering.js", "src/layers/radio/rendering.js",
"src/layers/radio/state.js",
"src/layers/recentImagery/rendering.js", "src/layers/recentImagery/rendering.js",
"src/layers/satellites/catalog.js", "src/layers/satellites/catalog.js",
"src/layers/satellites/controls.js", "src/layers/satellites/controls.js",
+1 -22
View File
@@ -20,6 +20,7 @@ import {
dot, dot,
magnitude, magnitude,
normalize, normalize,
rayEllipsoid,
scale, scale,
sub, sub,
toCartographicRadians, toCartographicRadians,
@@ -502,28 +503,6 @@ export function createStubRenderer(options = {}) {
}; };
} }
/** Nearest hit of a ray on the ellipsoid. */
function rayEllipsoid(ray) {
const r = WGS84.oneOverRadii;
const q = vec(ray.origin.x * r.x, ray.origin.y * r.y, ray.origin.z * r.z);
const w = vec(
ray.direction.x * r.x,
ray.direction.y * r.y,
ray.direction.z * r.z,
);
const a = dot(w, w);
const b = 2 * dot(q, w);
const c = dot(q, q) - 1;
const disc = b * b - 4 * a * c;
if (disc < 0) return null;
const sq = Math.sqrt(disc);
const t0 = (-b - sq) / (2 * a);
const t1 = (-b + sq) / (2 * a);
const t = t0 >= 0 ? t0 : t1 >= 0 ? t1 : null;
if (t === null) return null;
return add(ray.origin, scale(ray.direction, t));
}
function isOccluded(position) { function isOccluded(position) {
const e = eye(); const e = eye();
const r = WGS84.oneOverRadii; const r = WGS84.oneOverRadii;
+8 -1
View File
@@ -144,9 +144,16 @@ export function bindFakeCesiumCamera(viewer) {
value() { value() {
const c = fake.positionCartographic; const c = fake.positionCartographic;
const p = fake.positionWC; const p = fake.positionWC;
const v = (w) => ({ x: w.x, y: w.y, z: w.z });
return { return {
...DEFAULT_POSE, ...DEFAULT_POSE,
...(p ? { position: { x: p.x, y: p.y, z: p.z } } : {}), ...(p ? { position: v(p) } : {}),
...(fake.directionWC ? { direction: v(fake.directionWC) } : {}),
...(fake.upWC ? { up: v(fake.upWC) } : {}),
...(fake.rightWC ? { right: v(fake.rightWC) } : {}),
...(Number.isFinite(fake.frustum?.fovy)
? { fovy: fake.frustum.fovy }
: {}),
lon: c ? toDegrees(c.longitude) : NaN, lon: c ? toDegrees(c.longitude) : NaN,
lat: c ? toDegrees(c.latitude) : NaN, lat: c ? toDegrees(c.latitude) : NaN,
height: c ? c.height : NaN, height: c ? c.height : NaN,