mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
Resolve annotation targets against the renderer's camera and query ports
The annotation resolver no longer imports Cesium. The view proximity gate reads the camera port's pose and the geocode bias its view rectangle. Screen-point picks go through the query port (depth pick, then ellipsoid, then the globe along the pick ray) on the renderer's canvas, and ground heights through the height gateway. It leaves the Cesium allow-list. Its tests bind a camera port over their camera doubles.
This commit is contained in:
1 parent
8d586e5ff9
commit
9fe3e7633f
7 files changed
+68
-84
No files matched your search
@@ -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. */
|
||||
|
||||
@@ -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'] },
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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. */
|
||||
|
||||
+37
-63
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user