diff --git a/src/annotations/adminOutline.test.mjs b/src/annotations/adminOutline.test.mjs index 2028fe711..4bb5cba35 100644 --- a/src/annotations/adminOutline.test.mjs +++ b/src/annotations/adminOutline.test.mjs @@ -4,11 +4,13 @@ import { test } from 'node:test'; import assert from 'node:assert/strict'; import { FEATURE_SOURCE_METHODS } from '../sources/featureSource.js'; import { createAnnotationResolver } from './resolver.js'; +import { bindFakeCesiumCamera } from '../testSupport/fakeCameraPort.mjs'; import { createAnnotationEngine } from './annotationEngine.js'; import { _resetRenderGovernorForTest } from '../renderGovernor.js'; function viewerAt(lat, lon, height = 20_000) { - return { + // The resolver reads the camera through the renderer's camera port. + return bindFakeCesiumCamera({ camera: { positionCartographic: { latitude: (lat * Math.PI) / 180, @@ -16,7 +18,7 @@ function viewerAt(lat, lon, height = 20_000) { height, }, }, - }; + }); } /** A resolver whose boundary lookups and geocoder record every call. */ diff --git a/src/annotations/annotationResolver.test.mjs b/src/annotations/annotationResolver.test.mjs index 2c51213c1..acd84e878 100644 --- a/src/annotations/annotationResolver.test.mjs +++ b/src/annotations/annotationResolver.test.mjs @@ -13,6 +13,7 @@ import { createPlaceSearch } from '../search/placeSearch.js'; // Run with: npm test (node --test) import { test } from 'node:test'; import assert from 'node:assert/strict'; +import { bindFakeCesiumCamera } from '../testSupport/fakeCameraPort.mjs'; import { resolveAnnotationTarget as resolveTarget, selectFootprint, @@ -157,7 +158,8 @@ test('refineScope: entityKind refines only an unresolved (auto) scope', () => { }); function closeViewportViewer() { - return { + // The resolver reads the camera through the renderer's camera port. + return bindFakeCesiumCamera({ camera: { positionCartographic: { latitude: 30.2672 * Math.PI / 180, @@ -165,7 +167,7 @@ function closeViewportViewer() { height: 1000, }, }, - }; + }); } function geocodePayload({ lat, lon, types, label }) { @@ -369,11 +371,11 @@ test('ask-side admin bypass: admin level 2/3 result types never grant a township // Synthetic footprints reproduce the real Capitol/YOTEL selection without live APIs. const CAPITOL_ANCHOR = { lat: 38.8899389, lon: -77.0090505 }; function capitolViewer() { - return { camera: { positionCartographic: { + return bindFakeCesiumCamera({ camera: { positionCartographic: { latitude: CAPITOL_ANCHOR.lat * Math.PI / 180, longitude: CAPITOL_ANCHOR.lon * Math.PI / 180, height: 1000, - } } }; + } } }); } function installCapitolMocks(t, elements, components = [ { long_name: 'Washington', types: ['locality', 'political'] }, diff --git a/src/annotations/outlineRungs.test.mjs b/src/annotations/outlineRungs.test.mjs index 8b4defe4e..17d742830 100644 --- a/src/annotations/outlineRungs.test.mjs +++ b/src/annotations/outlineRungs.test.mjs @@ -18,6 +18,7 @@ import { outlineQueryText, } from './outlineRungs.js'; import { createAnnotationResolver } from './resolver.js'; +import { bindFakeCesiumCamera } from '../testSupport/fakeCameraPort.mjs'; import { decodeOpenFreeMapOutlineTile } from '../sources/openFreeMapOutlines.js'; import { createNominatimOutlineClient } from '../sources/nominatimOutlines.js'; import { createApplicationRequestServices } from '../services/requests.js'; @@ -902,15 +903,17 @@ const PLACES = { }, }; -const viewerAt = (lat, lon, height = 4000) => ({ - camera: { - positionCartographic: { - latitude: (lat * Math.PI) / 180, - longitude: (lon * Math.PI) / 180, - height, +// The resolver reads the camera through the renderer's camera port. +const viewerAt = (lat, lon, height = 4000) => + bindFakeCesiumCamera({ + camera: { + positionCartographic: { + latitude: (lat * Math.PI) / 180, + longitude: (lon * Math.PI) / 180, + height, + }, }, - }, -}); + }); test('default settings: outline, street and building asks send nothing to Overpass', async (t) => { const prior = process.env.OVERPASS_UPSTREAMS; diff --git a/src/annotations/placeOutline.test.mjs b/src/annotations/placeOutline.test.mjs index 946245d72..b5e42f451 100644 --- a/src/annotations/placeOutline.test.mjs +++ b/src/annotations/placeOutline.test.mjs @@ -5,12 +5,14 @@ import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import { FEATURE_SOURCE_METHODS } from '../sources/featureSource.js'; import { createAnnotationResolver } from './resolver.js'; +import { bindFakeCesiumCamera } from '../testSupport/fakeCameraPort.mjs'; import { polygonsContain } from '../data/adminBoundaries.js'; import { streetFootprint } from './outlineRungs.js'; import { bindFakeCesiumCredits } from '../testSupport/fakeCreditsPort.mjs'; function viewerAt(lat, lon, height = 20_000) { - return { + // The resolver reads the camera through the renderer's camera port. + return bindFakeCesiumCamera({ camera: { positionCartographic: { latitude: (lat * Math.PI) / 180, @@ -18,7 +20,7 @@ function viewerAt(lat, lon, height = 20_000) { height, }, }, - }; + }); } /** A resolver whose boundary lookups and geocoder record every call. */ diff --git a/src/annotations/resolver.js b/src/annotations/resolver.js index 412a4a257..2f6bde6ef 100644 --- a/src/annotations/resolver.js +++ b/src/annotations/resolver.js @@ -14,7 +14,6 @@ import { nearestPointOnLines, } from '../sources/featureGeometry.js'; import { defaultGeospatial } from '../search/defaults.js'; -import * as Cesium from 'cesium'; import { lookupNeighborhoodRing } from '../data/neighborhoodPolygons.js'; import { lookupNaturalRegionOutline, @@ -45,12 +44,9 @@ import { import { createOutlineRungs } from './outlineRungs.js'; import { unavailablePlaceSearch } from '../search/placeSearch.js'; import { isPickedWorldPosition } from '../data/scenePick.js'; -import { heightsFor } from '../render/cesium/heights.js'; -// The application operations entry reaches locations.js, whose flights go -// through the camera and query ports and which cannot load the Cesium adapter -// itself; this listed module loads the factories a viewer with no renderer -// bound gets them from. -import '../render/cesium/camera.js'; +import { heightsOf } from '../queries/heights.js'; +import { fromDegrees, toLonLatH } from '../geo/ellipsoid.js'; +import { findCamera, findQuery, rendererOf } from '../render/rendererOf.js'; /** Prepare only local dateline outlines for planar admission and centering. */ function localOutlineFrame(fp) { @@ -184,7 +180,7 @@ export function createAnnotationResolver({ * Resolve a single annotation target to a normalized world anchor. * * @param {object} opts - * @param {Cesium.Viewer} opts.viewer + * @param {object} opts.viewer The viewer the renderer is bound to. * @param {string} [opts.target] Place name to geocode. * @param {number} [opts.latitude] Explicit latitude (wins over target). * @param {number} [opts.longitude] Explicit longitude. @@ -2216,11 +2212,10 @@ export function createAnnotationResolver({ */ function viewportProximity(viewer) { try { - const carto = viewer?.camera?.positionCartographic; - if (!carto) return null; - const lat = Cesium.Math.toDegrees(carto.latitude); - const lon = Cesium.Math.toDegrees(carto.longitude); - const radiusKm = Math.max((carto.height / 1000) * 3, 5); + const pose = viewer ? findCamera(viewer)?.pose() : null; + if (!pose) return null; + const { lat, lon } = pose; + const radiusKm = Math.max((pose.height / 1000) * 3, 5); if (![lat, lon, radiusKm].every(Number.isFinite)) return null; return { lat, lon, radiusKm }; } catch { @@ -2231,12 +2226,12 @@ export function createAnnotationResolver({ /** Exported for searchAndFlyTo (src/locations.js), which shares this bias. */ function viewportBias(viewer) { try { - const rect = viewer?.camera?.computeViewRectangle?.(); + const rect = viewer ? findCamera(viewer)?.viewRectangle() : null; if (!rect) return null; - const swLat = Cesium.Math.toDegrees(rect.south).toFixed(4); - const swLng = Cesium.Math.toDegrees(rect.west).toFixed(4); - const neLat = Cesium.Math.toDegrees(rect.north).toFixed(4); - const neLng = Cesium.Math.toDegrees(rect.east).toFixed(4); + const swLat = rect.south.toFixed(4); + const swLng = rect.west.toFixed(4); + const neLat = rect.north.toFixed(4); + const neLng = rect.east.toFixed(4); if ([swLat, swLng, neLat, neLng].some((v) => v === 'NaN')) return null; return `${swLat},${swLng}|${neLat},${neLng}`; } catch { @@ -2289,58 +2284,39 @@ export function createAnnotationResolver({ * name the place, and we anchor the mark to the actual world point under it. */ function pickWorldFromScreen(viewer, nx, ny) { - const scene = viewer?.scene; - if (!scene) return null; - const canvas = scene.canvas; + const query = viewer ? findQuery(viewer) : null; + // The renderer's canvas; a viewer with no renderer bound, its own. + const canvas = rendererOf(viewer)?.canvas ?? viewer?.canvas; + if (!query || !canvas) return null; const w = canvas.clientWidth || canvas.width || 0; const h = canvas.clientHeight || canvas.height || 0; if (!w || !h) return null; const px = Math.max(0, Math.min(1, nx)) * w; const py = Math.max(0, Math.min(1, ny)) * h; - const pos = new Cesium.Cartesian2(px, py); // Each stage is validated before it is accepted: a depth pick over empty sky - // can return a NaN or centre-of-the-earth Cartesian, and converting one of - // those throws inside Cesium. A degenerate pick IS a missed pick, so it falls - // through to the next stage and ultimately to the caller's null. - let cart = null; - if ( - scene.pickPositionSupported && - typeof scene.pickPosition === 'function' - ) { + // can return a NaN or centre-of-the-earth position, and converting one of + // those throws inside the renderer. A degenerate pick IS a missed pick, so it + // falls through to the next stage and ultimately to the caller's null. + const attempt = (pick) => { try { - cart = scene.pickPosition(pos); + return pick(); } catch { - cart = null; + return null; } - } - if ( - !isPickedWorldPosition(cart) && - typeof viewer.camera.pickEllipsoid === 'function' - ) { - try { - cart = viewer.camera.pickEllipsoid(pos, Cesium.Ellipsoid.WGS84); - } catch { - cart = null; - } - } - if ( - !isPickedWorldPosition(cart) && - typeof viewer.camera.getPickRay === 'function' - ) { - try { - const ray = viewer.camera.getPickRay(pos); - cart = ray ? scene.globe?.pick(ray, scene) || null : null; - } catch { - cart = null; - } - } + }; + // The depth pick answers null where the renderer has no depth support. + let cart = attempt(() => query.pickPosition(px, py)); + if (!isPickedWorldPosition(cart)) + cart = attempt(() => query.pickEllipsoid(px, py)); + if (!isPickedWorldPosition(cart)) + cart = attempt(() => query.pickGlobe(query.pickRay(px, py))); if (!isPickedWorldPosition(cart)) return null; - const carto = Cesium.Cartographic.fromCartesian(cart); + const carto = toLonLatH(cart); if (!carto) return null; return { - lat: Cesium.Math.toDegrees(carto.latitude), - lon: Cesium.Math.toDegrees(carto.longitude), + lat: carto.lat, + lon: carto.lon, // The height the pick actually landed on — a roof, a hillside, or 0 on // the ellipsoid. Callers that only want a coordinate ignore it; the draw // tool's live preview needs it, or the rubber band sinks to sea level @@ -2355,15 +2331,13 @@ export function createAnnotationResolver({ * the tiles for that spot aren't loaded we fall back to the ellipsoid (0). */ function sampleGroundHeight(viewer, lon, lat) { - const scene = viewer?.scene; - if (!scene) return 0; - const heights = heightsFor(scene); + if (!viewer) return 0; + const heights = heightsOf(viewer); // A miss is tiles not ready: fall through to the terrain, then 0. if (heights.supported().clamp) { - const surface = Cesium.Cartesian3.fromDegrees(lon, lat, 0); - const clamped = heights.clampNow(surface); + const clamped = heights.clampNow(fromDegrees(lon, lat, 0)); if (clamped) { - const h = Cesium.Cartographic.fromCartesian(clamped)?.height; + const h = toLonLatH(clamped)?.height; if (Number.isFinite(h)) return h; } } diff --git a/src/data/boundaryLabels.test.mjs b/src/data/boundaryLabels.test.mjs index a46a0aed5..917bfb8ce 100644 --- a/src/data/boundaryLabels.test.mjs +++ b/src/data/boundaryLabels.test.mjs @@ -8,6 +8,7 @@ import { readFileSync } from 'node:fs'; import * as Cesium from 'cesium'; import { FEATURE_SOURCE_METHODS } from '../sources/featureSource.js'; import { createAnnotationResolver } from '../annotations/resolver.js'; +import { bindFakeCesiumCamera } from '../testSupport/fakeCameraPort.mjs'; import { createWorldAnnotationRenderer } from '../annotations/worldAnnotationRenderer.js'; const read = (path) => readFileSync(new URL(path, import.meta.url), 'utf8'); @@ -110,7 +111,8 @@ test('drawing bundled place and neighborhood outlines creates no Cesium label', }); const primitives = []; const dataSources = []; - const viewer = { + // The resolver reads the camera through the renderer's camera port. + const viewer = bindFakeCesiumCamera({ camera: { positionCartographic: Cesium.Cartographic.fromDegrees( -97.7431, @@ -123,7 +125,7 @@ test('drawing bundled place and neighborhood outlines creates no Cesium label', add: (ds) => dataSources.push(ds), remove() {}, }, - }; + }); const renderer = createWorldAnnotationRenderer(viewer); for (const [target, lon, lat] of [ ['Austin', -97.7431, 30.2672], @@ -135,8 +137,8 @@ test('drawing bundled place and neighborhood outlines creates no Cesium label', 3000, ); const resolved = await resolveAnnotationTarget({ - // The resolver reads only the camera (no scene to pick from). - viewer: { camera: viewer.camera }, + // The resolver reads only the camera (no query port to pick with). + viewer, target, footprint: true, deferFootprint: true, diff --git a/src/render/cesiumAllowList.json b/src/render/cesiumAllowList.json index 1bd58dc7a..ddb3a2546 100644 --- a/src/render/cesiumAllowList.json +++ b/src/render/cesiumAllowList.json @@ -2,7 +2,6 @@ "dev/conformance/main.js", "src/annotations/annotationEngine.js", "src/annotations/drawTool.js", - "src/annotations/resolver.js", "src/annotations/worldAnnotationRenderer.js", "src/app/embed.js", "src/app/scene.js",