diff --git a/scripts/package-boundaries.json b/scripts/package-boundaries.json index ccba92f43..93212a729 100644 --- a/scripts/package-boundaries.json +++ b/scripts/package-boundaries.json @@ -5860,6 +5860,7 @@ "src/data/satellitePass.js", "src/tools/queries/atmosphere.js", "src/layers/wind/inspection.js", + "src/geo/ellipsoid.js", "src/layers/wind/model.js", "src/layers/wind/fields.js", "src/i18n/marker.js", @@ -5884,7 +5885,8 @@ "src/tools/globePanel.js", "src/tools/queries/panelRequest.js", "src/geoDistance.js", - "src/visualStylePolicy.js" + "src/visualStylePolicy.js", + "src/geo/vector.js" ], "external": ["satellite.js"] }, diff --git a/src/layers/wind/index.js b/src/layers/wind/index.js index 016df9a0c..49da8e834 100644 --- a/src/layers/wind/index.js +++ b/src/layers/wind/index.js @@ -1,8 +1,4 @@ -import * as Cesium from 'cesium'; import { createWindRendering } from './rendering.js'; -// The renderer drapes through `findGlobe`; a viewer with no renderer bound -// gets its globe port from the factory this module provides. -import '../../render/cesium/globe.js'; import { WIND_FIELDS } from './fields.js'; import { formatWindReading, @@ -11,6 +7,7 @@ import { } from './presentation.js'; import { inspectWindAtCenter, WIND_UNITS } from './inspection.js'; import { createWindInspectionMarker } from './inspectionMarker.js'; +import { findQuery } from '../../render/rendererOf.js'; import { t, msg, translateKnown } from '../../i18n/index.js'; import { unitLabel } from '../../ui/unitLabels.js'; @@ -57,7 +54,6 @@ export function windStats(manifest) { export function createWindLayer({ feed, clock, - cesium = Cesium, container, createRendering = createWindRendering, } = {}) { @@ -84,7 +80,10 @@ export function createWindLayer({ inspectionMarker?.clear(); }; const sample = (position) => { - reading = inspectWindAtCenter(manifest, viewer, cesium, { + const view = viewer + ? { query: findQuery(viewer), canvas: viewer.scene?.canvas } + : {}; + reading = inspectWindAtCenter(manifest, view, { position, units, overlay, @@ -116,14 +115,10 @@ export function createWindLayer({ init(nextViewer) { viewer = nextViewer; rendering = createRendering({ - cesium, container: container ?? nextViewer.container, getViewer: () => viewer, - getHost: () => - imageryHost?.() ?? { - collection: viewer.imageryLayers ?? viewer.scene?.imageryLayers, - kind: 'globe', - }, + // Without a host from the shell, the rendering drapes on the globe. + getHost: () => imageryHost?.(), onStatusChange: notify, }); rendering.attach(); @@ -132,7 +127,6 @@ export function createWindLayer({ inspectionMarker = createWindInspectionMarker({ container: target, viewer, - cesium, }); }, attachShellServices(services) { diff --git a/src/layers/wind/index.test.mjs b/src/layers/wind/index.test.mjs index c0b738643..05b8c275b 100644 --- a/src/layers/wind/index.test.mjs +++ b/src/layers/wind/index.test.mjs @@ -3,6 +3,8 @@ import assert from 'node:assert/strict'; import { createWindLayer, formatWindValidTime, windStats } from './index.js'; import { useWindInspectionStore } from '../../ui/stores/windInspection.js'; import { applicationPinia } from '../../ui/vueRuntime.js'; +import { fromDegrees } from '../../geo/ellipsoid.js'; +import { bindRenderer } from '../../render/rendererOf.js'; const snapshot = (model) => ({ model, @@ -312,25 +314,27 @@ test('sample stays fixed across model, field and unit changes; dismissal and dis ownerDocument: {}, getBoundingClientRect: () => ({ left: 0, top: 0 }), }; + let center = fromDegrees(0, 0); const viewer = { container, - camera: { positionWC: {}, pickEllipsoid: () => { samples++; return { longitude: 0, latitude: 0 }; } }, scene: { - mode: 3, canvas: { clientWidth: 800, clientHeight: 600, getBoundingClientRect: () => ({ left: 0, top: 0 }) }, - cartesianToCanvasCoordinates: () => ({ x: 400, y: 300 }), - postRender: { addEventListener(fn) { listener = fn; return () => { listener = null; }; } }, - requestRender() {}, }, }; + bindRenderer(viewer, { + query: { + pickEllipsoid: () => { samples++; return center; }, + project: () => ({ x: 400, y: 300 }), + isOccluded: () => false, + }, + frame: { + on(phase, fn) { listener = fn; return () => { listener = null; }; }, + requestRender() {}, + }, + }); const rendering = Object.fromEntries(['attach', 'start', 'stop', 'clear', 'destroy', 'setField'].map(name => [name, () => {}])); const layer = createWindLayer({ feed: { getSnapshot: async ({ model }) => complete(model) }, - cesium: { - Cartesian2: class {}, Ellipsoid: { WGS84: {} }, SceneMode: { SCENE3D: 3 }, - Cartographic: { fromCartesian: point => point }, Math: { toDegrees: value => value }, - EllipsoidalOccluder: class { isPointVisible() { return true; } }, - }, createRendering: () => rendering, }); @@ -352,7 +356,7 @@ test('sample stays fixed across model, field and unit changes; dismissal and dis assert.deepEqual(layer.getRowControls().summary.settings.map(({ label }) => label), ['MODEL', 'FIELD', 'UNITS', 'MOTION']); assert.ok(marker()); assert.equal(typeof listener, 'function'); - viewer.camera.pickEllipsoid = () => { samples++; return { longitude: 70, latitude: 20 }; }; + center = fromDegrees(70, 20); layer.setParams({ overlay: 'speed' }); assert.equal(layer.getRowControls().summary.reading.coordinates, captured.coordinates); layer.setParams({ model: 'ifs' }); diff --git a/src/layers/wind/inspection.js b/src/layers/wind/inspection.js index 99d6c3e5a..cf9e63376 100644 --- a/src/layers/wind/inspection.js +++ b/src/layers/wind/inspection.js @@ -1,5 +1,6 @@ import { sampleWind, windSpeed } from './model.js'; import { sampleScalar } from './fields.js'; +import { toLonLatH } from '../../geo/ellipsoid.js'; // The reading stays English: AI tools read it too. Text is marked for i18n // extraction and translated where the WEATHER card shows it (presentation.js). @@ -41,24 +42,22 @@ export function formatWindSpeed(speed, units = 'km/h') { return `${(speed * WIND_UNITS[unit]).toFixed(1)} ${unit}`; } -/** A deliberate snapshot at the map center; does not own pointer input. */ +/** + * A deliberate snapshot at the map center; does not own pointer input. The + * center is where `view.query` (a renderer query port) meets the ellipsoid + * under the middle of `view.canvas`, unless `position` (ECEF) is given. + */ export function inspectWindAtCenter( snapshot, - viewer, - cesium, + { query, canvas } = {}, { units, overlay, model, validTime, status, position } = {}, ) { - const scene = viewer?.scene; - const canvas = scene?.canvas; - const ellipsoid = scene?.globe?.ellipsoid || cesium.Ellipsoid?.WGS84; const point = position || (canvas && - viewer.camera?.pickEllipsoid?.( - new cesium.Cartesian2(canvas.clientWidth / 2, canvas.clientHeight / 2), - ellipsoid, - )); - if (!point || !snapshot?.u || !snapshot?.v) + query?.pickEllipsoid(canvas.clientWidth / 2, canvas.clientHeight / 2)); + const location = point && snapshot?.u && snapshot?.v && toLonLatH(point); + if (!location) return { coordinates: msg('No surface reading'), wind: msg('Aim the map center at Earth'), @@ -69,9 +68,7 @@ export function inspectWindAtCenter( 'A location reading needs a loaded forecast and the Earth at the center of the view.', ), }; - const location = cesium.Cartographic.fromCartesian(point, ellipsoid); - const lon = cesium.Math.toDegrees(location.longitude); - const lat = cesium.Math.toDegrees(location.latitude); + const { lon, lat } = location; const vector = sampleWind( { ...snapshot.grid, u: snapshot.u, v: snapshot.v }, lon, diff --git a/src/layers/wind/inspection.test.mjs b/src/layers/wind/inspection.test.mjs index 52adb07f8..f1d3c2130 100644 --- a/src/layers/wind/inspection.test.mjs +++ b/src/layers/wind/inspection.test.mjs @@ -5,6 +5,7 @@ import { formatWindSpeed, inspectWindAtCenter, } from './inspection.js'; +import { fromDegrees } from '../../geo/ellipsoid.js'; test('wind directions describe the source of the flow and calm is not north', () => { assert.equal(windFrom(10, 0), 'W'); @@ -26,22 +27,18 @@ test('center inspection uses a fixed forecast reading without double-converting values: Float32Array.from([20, 20, 20, 20]), }, }; - const viewer = { - camera: { pickEllipsoid: () => ({ longitude: 0, latitude: 0 }) }, - scene: { canvas: { clientWidth: 800, clientHeight: 600 } }, - }; - const cesium = { - Cartesian2: class { - constructor(x, y) { - this.x = x; - this.y = y; - } + let center = fromDegrees(0, 0); + const picks = []; + const view = { + canvas: { clientWidth: 800, clientHeight: 600 }, + query: { + pickEllipsoid(x, y) { + picks.push([x, y]); + return center; + }, }, - Ellipsoid: { WGS84: {} }, - Cartographic: { fromCartesian: (p) => p }, - Math: { toDegrees: (x) => (x * 180) / Math.PI }, }; - const result = inspectWindAtCenter(snapshot, viewer, cesium, { + const result = inspectWindAtCenter(snapshot, view, { overlay: 'temperature', units: 'km/h', model: 'GFS', @@ -52,9 +49,21 @@ test('center inspection uses a fixed forecast reading without double-converting assert.equal(result.scalarValue, '20.0 °C'); assert.equal(result.validTime, 'fixed'); assert.equal(result.coordinates, '0.00°N · 0.00°E'); - viewer.camera.pickEllipsoid = () => null; + assert.deepEqual(picks, [[400, 300]], 'the pick is at the canvas center'); + assert.equal(result.position, center); + center = fromDegrees(70, 20); assert.equal( - inspectWindAtCenter(snapshot, viewer, cesium).coordinates, + inspectWindAtCenter(snapshot, view).coordinates, + '20.00°N · 70.00°E', + ); + center = null; + assert.equal( + inspectWindAtCenter(snapshot, view).coordinates, 'No surface reading', ); + assert.equal( + inspectWindAtCenter(snapshot, { query: view.query }).coordinates, + 'No surface reading', + 'no canvas, no reading', + ); }); diff --git a/src/layers/wind/inspectionMarker.js b/src/layers/wind/inspectionMarker.js index e263eaf45..f23a27715 100644 --- a/src/layers/wind/inspectionMarker.js +++ b/src/layers/wind/inspectionMarker.js @@ -1,16 +1,17 @@ import { useWindInspectionStore } from '../../ui/stores/windInspection.js'; import { applicationPinia } from '../../ui/vueRuntime.js'; +import { findFrame, findQuery } from '../../render/rendererOf.js'; /** * One passive marker for the captured location; it never samples on camera - * movement. Where the point falls is presented to the wind inspection store - * after every rendered frame, and the scene chrome renders the marker in - * `container`. + * movement. Where the point falls, as the renderer's query port projects + * it, is presented to the wind inspection store after every rendered frame, + * and the scene chrome renders the marker in `container`. A point beyond + * the ellipsoid's horizon or off the canvas hides the marker. */ export function createWindInspectionMarker({ container, viewer, - cesium, store = useWindInspectionStore(applicationPinia()), }) { const owner = {}; @@ -27,42 +28,33 @@ export function createWindInspectionMarker({ removeRender = null; if (shown()) store.publish(null); marker = null; - viewer?.scene?.requestRender?.(); + if (viewer) findFrame(viewer)?.requestRender('wind inspection marker'); }; return { show(position) { clear(); - const scene = viewer?.scene; - if ( - !position || - !container?.ownerDocument || - !scene?.postRender?.addEventListener || - !scene.cartesianToCanvasCoordinates - ) + const canvas = viewer?.scene?.canvas; + const query = viewer ? findQuery(viewer) : null; + const frame = viewer ? findFrame(viewer) : null; + if (!position || !container?.ownerDocument || !canvas || !query || !frame) return; present({ owner, container, visible: false, left: 0, top: 0 }); - const occluder = new cesium.EllipsoidalOccluder( - scene.globe?.ellipsoid || cesium.Ellipsoid.WGS84, - viewer.camera.positionWC, - ); const render = () => { if (!shown()) return; - occluder.cameraPosition = viewer.camera.positionWC; - const point = scene.cartesianToCanvasCoordinates(position); + const point = query.project(position); const visible = point && point.x >= 0 && point.y >= 0 && - point.x <= scene.canvas.clientWidth && - point.y <= scene.canvas.clientHeight && - (scene.mode !== cesium.SceneMode.SCENE3D || - occluder.isPointVisible(position)); + point.x <= canvas.clientWidth && + point.y <= canvas.clientHeight && + !query.isOccluded(position); if (!visible) { // A hidden marker keeps its last place, as the element did. if (marker.visible) present({ visible: false }); return; } - const canvasRect = scene.canvas.getBoundingClientRect(); + const canvasRect = canvas.getBoundingClientRect(); const containerRect = container.getBoundingClientRect(); const left = point.x + canvasRect.left - containerRect.left; const top = point.y + canvasRect.top - containerRect.top; @@ -70,9 +62,9 @@ export function createWindInspectionMarker({ return; present({ visible: true, left, top }); }; - removeRender = scene.postRender.addEventListener(render); + removeRender = frame.on('postRender', render); render(); - scene.requestRender?.(); + frame.requestRender('wind inspection marker'); }, clear, destroy: clear, diff --git a/src/layers/wind/inspectionMarker.test.mjs b/src/layers/wind/inspectionMarker.test.mjs index 49f0a6574..9d808ae77 100644 --- a/src/layers/wind/inspectionMarker.test.mjs +++ b/src/layers/wind/inspectionMarker.test.mjs @@ -9,17 +9,10 @@ import { createPinia } from 'pinia'; import { useWindInspectionStore } from '../../ui/stores/windInspection.js'; import WindInspectionMarker from '../../ui/views/panels/WindInspectionMarker.vue'; import { createWindInspectionMarker } from './inspectionMarker.js'; +import { bindRenderer } from '../../render/rendererOf.js'; -const cesium = { - SceneMode: { SCENE3D: 3 }, - Ellipsoid: { WGS84: {} }, - EllipsoidalOccluder: class { - isPointVisible() { - return cesium.visible; - } - }, - visible: true, -}; +/** Whether the fake query port sees the point over the horizon. */ +const horizon = { visible: true }; function fixture(t) { document.body.replaceChildren(); @@ -31,49 +24,54 @@ function fixture(t) { t.after(() => app.unmount()); const frames = { listener: null }; const projected = []; + const requests = []; const viewer = { - camera: { positionWC: {} }, scene: { - mode: 3, canvas: { clientWidth: 800, clientHeight: 600, getBoundingClientRect: () => ({ left: 15, top: 30 }), }, - postRender: { - addEventListener(fn) { - assert.equal(frames.listener, null); - frames.listener = fn; - return () => { - frames.listener = null; - }; - }, - }, - cartesianToCanvasCoordinates(point) { + }, + }; + bindRenderer(viewer, { + query: { + project(point) { projected.push(point); return { x: 100, y: 200 }; }, - requestRender() {}, + isOccluded: () => !horizon.visible, }, - }; + frame: { + on(phase, fn) { + assert.equal(phase, 'postRender'); + assert.equal(frames.listener, null); + frames.listener = fn; + return () => { + frames.listener = null; + }; + }, + requestRender: (reason) => requests.push(reason), + }, + }); const store = useWindInspectionStore(pinia); - cesium.visible = true; + horizon.visible = true; const owner = createWindInspectionMarker({ container, viewer, - cesium, store, }); const nodes = () => [ ...container.querySelectorAll('.gev-wind-inspection-marker'), ]; - return { owner, viewer, store, frames, projected, nodes, container, cesium }; + return { owner, viewer, store, frames, projected, requests, nodes, container }; } test('the marker follows only its captured point, hides beyond the limb, and releases its listener', async (t) => { - const { owner, viewer, frames, projected, nodes } = fixture(t); + const { owner, frames, projected, requests, nodes } = fixture(t); const point = { x: 1, y: 2, z: 3 }; owner.show(point); + assert.ok(requests.length > 0, 'showing asks the frame port for a frame'); await nextTick(); assert.equal(nodes().length, 1); const [marker] = nodes(); @@ -84,14 +82,13 @@ test('the marker follows only its captured point, hides beyond the limb, and rel assert.equal(marker.style.left, '110px'); assert.equal(marker.style.top, '220px'); assert.equal(marker.hidden, false); - viewer.camera.positionWC = { moved: true }; - cesium.visible = false; + horizon.visible = false; frames.listener(); await nextTick(); assert.equal(marker.hidden, true); assert.equal(marker.style.left, '110px', 'a hidden marker keeps its place'); assert.ok(projected.every((value) => value === point)); - cesium.visible = true; + horizon.visible = true; frames.listener(); await nextTick(); assert.equal(marker.hidden, false); @@ -125,7 +122,6 @@ test('an unchanged frame publishes nothing, and an older owner leaves a newer ma const newer = createWindInspectionMarker({ container, viewer, - cesium, store, }); newer.show({ x: 2 }); diff --git a/src/layers/wind/rendering.js b/src/layers/wind/rendering.js index 83dec7dc0..21735a93e 100644 --- a/src/layers/wind/rendering.js +++ b/src/layers/wind/rendering.js @@ -1,3 +1,9 @@ +import * as Cesium from 'cesium'; +// The renderer drapes through `findGlobe`, and the layer's inspection marker +// reads `findQuery` and `findFrame`; a viewer with no renderer bound gets +// those ports from the factories these modules provide. +import '../../render/cesium/globe.js'; +import '../../render/cesium/camera.js'; import { createShellSurface, WEATHER_SHELL_HEIGHTS, @@ -60,19 +66,22 @@ function sameWind(previous, next, previousField, nextField) { /** Globe-projected surface flow plus one owned, static colour field: globe * imagery on the globe host, a raised shell on the 3D Tiles host. */ export function createWindRendering({ - cesium, + cesium = Cesium, container, getViewer, - getHost = () => ({ - collection: - getViewer?.()?.imageryLayers ?? getViewer?.()?.scene?.imageryLayers, - kind: 'globe', - }), + getHost: hostOf, eventTarget = globalThis.window, createGpuRendering = createWindGpuRendering, globeOf = findGlobe, onStatusChange, } = {}) { + // The host the shell names, else the viewer's globe imagery. + const getHost = () => + hostOf?.() ?? { + collection: + getViewer?.()?.imageryLayers ?? getViewer?.()?.scene?.imageryLayers, + kind: 'globe', + }; let canvas = null; let context = null; let snapshot = null; diff --git a/src/render/cesiumAllowList.json b/src/render/cesiumAllowList.json index 77bf1a947..01c8e79d0 100644 --- a/src/render/cesiumAllowList.json +++ b/src/render/cesiumAllowList.json @@ -98,9 +98,6 @@ "src/layers/weather/index.js", "src/layers/weather/shellRendering.js", "src/layers/wind/gpuRendering.js", - "src/layers/wind/index.js", - "src/layers/wind/inspection.js", - "src/layers/wind/inspectionMarker.js", "src/layers/wind/relief.js", "src/layers/wind/rendering.js", "src/locations.js",