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

+3
View File
@@ -1,4 +1,7 @@
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 {
RADIO_OVERLAY_SOURCE_ID,
DEFAULT_RADIO_VOLUME,
+21 -25
View File
@@ -1,11 +1,12 @@
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 {
HORIZON_CAMERA_MOVE_EPSILON_M,
GLOBAL_RADIO_ALTITUDE_M,
RADIO_GLOBE_RECENTER_MAX_HEIGHT_M,
} from './policy.js';
import { cameraOf } from '../../render/cesium/camera.js';
import { cameraOf, findCamera } from '../../render/rendererOf.js';
export function createNavigation({
state: layerState,
@@ -137,13 +138,14 @@ export function createNavigation({
: currentHeight;
}
function radioCameraState(camera = layerState._viewer?.camera) {
if (!camera) return null;
function radioCameraState() {
const pose = findCamera(layerState._viewer)?.pose();
if (!pose) return null;
return {
height: camera.positionCartographic?.height,
heading: camera.heading,
pitch: camera.pitch,
roll: camera.roll,
height: pose.height,
heading: pose.heading,
pitch: pose.pitch,
roll: pose.roll,
};
}
@@ -154,12 +156,13 @@ export function createNavigation({
}
function radioGlobeRecenterPlan(viewer = layerState._viewer) {
const camera = viewer?.camera;
const pose = viewer?.camera ? findCamera(viewer)?.pose() : null;
const canvas = viewer?.scene?.canvas;
const width = canvas?.clientWidth || canvas?.width;
const height = canvas?.clientHeight || canvas?.height;
const cartographic = camera?.positionCartographic;
if (!camera || !cartographic || !(width > 0) || !(height > 0)) return null;
// The eye at the earth's centre has no geodetic position.
if (!pose || !Number.isFinite(pose.height) || !(width > 0) || !(height > 0))
return null;
const geometry = projectEarthDiscToViewport(
viewer,
width,
@@ -180,36 +183,29 @@ export function createNavigation({
);
} else {
const earthRadius = earthDiscScreenRadius(
Cesium.Cartesian3.magnitude(camera.positionWC),
magnitude(pose.position),
height,
camera.frustum?.fovy,
pose.fovy,
);
const facingEarthCenter =
Cesium.Cartesian3.dot(
camera.directionWC,
layerState._radioEarthToCenter,
) > 0;
dot(pose.direction, layerState._radioEarthToCenter) > 0;
if (!earthRadius || facingEarthCenter) return null;
const keyhole = getKeyholeGeometry(width, height);
fullGlobeCapable =
earthRadius + GLOBE_ENTER_CLEARANCE_PX <= keyhole.radius;
}
const recenterHeight = radioGlobeRecenterHeight(
cartographic.height,
pose.height,
fullGlobeCapable,
);
if (recenterHeight == null) return null;
return {
destination: Cesium.Cartesian3.fromRadians(
cartographic.longitude,
cartographic.latitude,
recenterHeight,
),
destination: fromDegrees(pose.lon, pose.lat, recenterHeight),
cameraState: {
...radioCameraState(camera),
...radioCameraState(),
height: recenterHeight,
heading: 0,
pitch: -Cesium.Math.PI_OVER_TWO,
pitch: -Math.PI / 2,
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';
export function createQueries({ state: layerState, services, parts, source }) {
function radioAngularDistance(station, anchor) {
const lat1 = Cesium.Math.toRadians(Number(anchor?.lat));
const lon1 = Cesium.Math.toRadians(Number(anchor?.lon));
const lat2 = Cesium.Math.toRadians(Number(station?.lat));
const lon2 = Cesium.Math.toRadians(Number(station?.lon));
const lat1 = toRadians(Number(anchor?.lat));
const lon1 = toRadians(Number(anchor?.lon));
const lat2 = toRadians(Number(station?.lat));
const lon2 = toRadians(Number(station?.lon));
if (![lat1, lon1, lat2, lon2].every(Number.isFinite))
return Number.POSITIVE_INFINITY;
const deltaLat = lat2 - lat1;
@@ -106,28 +108,22 @@ export function createQueries({ state: layerState, services, parts, source }) {
}
function viewportRadioAnchor() {
const camera = layerState._viewer?.camera;
const scene = layerState._viewer?.scene;
if (!camera) return null;
const altitudeM = Number(camera.positionCartographic?.height);
let cartographic = null;
const canvas = scene?.canvas;
if (canvas && typeof camera.pickEllipsoid === 'function') {
const center = new Cesium.Cartesian2(
canvas.clientWidth / 2,
canvas.clientHeight / 2,
);
const position = camera.pickEllipsoid(
center,
scene.globe?.ellipsoid || Cesium.Ellipsoid.WGS84,
);
if (position) cartographic = Cesium.Cartographic.fromCartesian(position);
}
cartographic ||= camera.positionCartographic || null;
if (!cartographic) return null;
const pose = findCamera(layerState._viewer)?.pose();
if (!pose) return null;
const altitudeM = Number(pose.height);
// Where the view's centre meets the ellipsoid (the canvas centre's ray
// is the view direction), else the point under the eye.
const hit = rayEllipsoid({
origin: pose.position,
direction: pose.direction,
});
const at = hit ? toLonLatH(hit) : null;
const lat = at ? at.lat : pose.lat;
const lon = at ? at.lon : pose.lon;
if (!Number.isFinite(lat) || !Number.isFinite(lon)) return null;
return {
lat: Cesium.Math.toDegrees(cartographic.latitude),
lon: Cesium.Math.toDegrees(cartographic.longitude),
lat,
lon,
altitudeM,
globalView: parts.navigation.radioViewIsGlobal(altitudeM),
};
+3 -3
View File
@@ -1,4 +1,3 @@
import * as Cesium from 'cesium';
import {
EMPTY_ACCEPTED_CATALOG_SNAPSHOT,
DEFAULT_RADIO_FILTER,
@@ -8,9 +7,10 @@ import {
export function createState({ services }) {
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;