diff --git a/src/geo/ellipsoid.js b/src/geo/ellipsoid.js index dbff51e3f..6cd912846 100644 --- a/src/geo/ellipsoid.js +++ b/src/geo/ellipsoid.js @@ -149,3 +149,30 @@ export function toLonLatH(p) { if (!c) return null; 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)); +} diff --git a/src/geo/geo.test.mjs b/src/geo/geo.test.mjs index e0fcde931..381165697 100644 --- a/src/geo/geo.test.mjs +++ b/src/geo/geo.test.mjs @@ -2,9 +2,11 @@ import assert from 'node:assert/strict'; import test from 'node:test'; import * as Cesium from 'cesium'; import { + distance, eastNorthUp, fromDegrees, geodeticSurfaceNormal, + rayEllipsoid, scaleToGeodeticSurface, toCartographicRadians, toDegrees, @@ -167,3 +169,43 @@ test('east-north-up frames match the default renderer', () => { 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'); +}); diff --git a/src/layers/radio/lifecycle.js b/src/layers/radio/lifecycle.js index 45d66892e..972cd2bfe 100644 --- a/src/layers/radio/lifecycle.js +++ b/src/layers/radio/lifecycle.js @@ -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, diff --git a/src/layers/radio/navigation.js b/src/layers/radio/navigation.js index 47ac48a02..6aa8ac485 100644 --- a/src/layers/radio/navigation.js +++ b/src/layers/radio/navigation.js @@ -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, }, }; diff --git a/src/layers/radio/queries.js b/src/layers/radio/queries.js index 5bc4cc573..d2179d178 100644 --- a/src/layers/radio/queries.js +++ b/src/layers/radio/queries.js @@ -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), }; diff --git a/src/layers/radio/state.js b/src/layers/radio/state.js index 99241162d..9166210bc 100644 --- a/src/layers/radio/state.js +++ b/src/layers/radio/state.js @@ -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; diff --git a/src/render/cesiumAllowList.json b/src/render/cesiumAllowList.json index 11d751696..cb4659a3a 100644 --- a/src/render/cesiumAllowList.json +++ b/src/render/cesiumAllowList.json @@ -119,10 +119,7 @@ "src/layers/military/tracking.js", "src/layers/perimeters/index.js", "src/layers/radio/lifecycle.js", - "src/layers/radio/navigation.js", - "src/layers/radio/queries.js", "src/layers/radio/rendering.js", - "src/layers/radio/state.js", "src/layers/recentImagery/rendering.js", "src/layers/satellites/catalog.js", "src/layers/satellites/controls.js", diff --git a/src/render/stub/index.js b/src/render/stub/index.js index e9b9d63c4..82f8ee84d 100644 --- a/src/render/stub/index.js +++ b/src/render/stub/index.js @@ -20,6 +20,7 @@ import { dot, magnitude, normalize, + rayEllipsoid, scale, sub, 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) { const e = eye(); const r = WGS84.oneOverRadii; diff --git a/src/testSupport/fakeCameraPort.mjs b/src/testSupport/fakeCameraPort.mjs index 83f538d39..d4e834cd7 100644 --- a/src/testSupport/fakeCameraPort.mjs +++ b/src/testSupport/fakeCameraPort.mjs @@ -144,9 +144,16 @@ export function bindFakeCesiumCamera(viewer) { value() { const c = fake.positionCartographic; const p = fake.positionWC; + const v = (w) => ({ x: w.x, y: w.y, z: w.z }); return { ...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, lat: c ? toDegrees(c.latitude) : NaN, height: c ? c.height : NaN,