Ask for heights through one service per scene

About 20 sites called scene.sampleHeight, globe.getHeight,
scene.clampToHeight or the MostDetailed variants directly, each with its own
idea of a miss (undefined, NaN, a throw). They now ask heightsFor(scene) in
src/queries/heights.js:

- now(point, { against, exclude }) answers from what is loaded, or null;
  against is 'surface' (what is drawn) or 'terrain' (loaded terrain only).
- clampNow(position) moves a position onto the drawn surface, or null.
- batch() and clampBatch() wait for the most detailed tiles: null per
  missed point, a rejection when the whole query fails.
- supported() reports { surface, clamp } without allocating.

Cesium still answers now() synchronously, so nothing changes on screen and
nothing is slower: in the live app 1,000 calls cost the same through the
service as direct (terrain 0.5-0.7 ms, surface 2-6 ms a call, run-to-run
noise larger than the difference). Every site already treated a miss as
"hold the last value or fall back"; a renderer that can only answer from a
cache or asynchronously can now plug in at one place.

- A Bhote Koshi evidence point whose terrain sample misses stays at its
  fallback elevation instead of reading the miss as sea level.
- The mesh floor sampler skips a scene without depth-texture support
  instead of spending its budget on probes that throw.
- The world annotation probe's trace reads hit/missed.
- A source scan forbids direct height calls outside the service.

The service is exported as ./queries/heights and documented in
docs/APPLICATION.md.
This commit is contained in:
Sameh Khamis committed 2026-11-04 09:24:30 -08:00
1 parent 02d492a079
commit 9bdeffeca3
32 files changed
+704 -210

No files matched your search

+13 -22
View File
@@ -28,6 +28,7 @@ import { createImagerySplit } from '../ui/imagerySplit.js';
import { formatDate, t, tn, msg, onLocaleChange } from '../i18n/index.js';
import { useBhoteKoshiPanelStore } from '../ui/stores/bhoteKoshiPanel.js';
import { applicationPinia } from '../ui/vueRuntime.js';
import { heightsFor } from '../queries/heights.js';
export const BHOTE_KOSHI_LAYER_ID = 'bhote-koshi-2026';
export const BHOTE_KOSHI_OVERLAY_SOURCE_ID = 'bhote-koshi-witnesses';
@@ -122,20 +123,11 @@ const DEFAULT_EVENT_LOADER = async () => {
const DEFAULT_IMAGERY_PROVIDER_FACTORY = (url, options) =>
Cesium.SingleTileImageryProvider.fromUrl(url, options);
const DEFAULT_TERRAIN_SAMPLER = async (viewer, observations) => {
if (
!viewer?.terrainProvider ||
!viewer.terrainProvider.availability ||
typeof Cesium.sampleTerrainMostDetailed !== 'function'
) {
return null;
}
const cartographics = observations.map((observation) =>
Cesium.Cartographic.fromDegrees(observation.lon, observation.lat),
);
return Cesium.sampleTerrainMostDetailed(
viewer.terrainProvider,
cartographics,
);
if (!viewer?.scene || !viewer.terrainProvider?.availability) return null;
const heights = await heightsFor(viewer.scene).batch(observations, {
against: 'terrain',
});
return heights.map((height) => ({ height }));
};
const DEFAULT_MEDIA_LOADER = async (url, { signal } = {}) => {
if (typeof createImageBitmap !== 'function') {
@@ -1197,7 +1189,8 @@ export function createBhoteKoshiEventLayer({
}
throwIfEnableCancelled(signal, _enabled);
return observations.map((observation, index) => {
const sampledHeight = Number(sampled?.[index]?.height);
// A miss is null or undefined, never sea level.
const sampledHeight = Number(sampled?.[index]?.height ?? Number.NaN);
const fallbackHeight = Number(observation.fallbackElevationM) || 0;
return Cesium.Cartesian3.fromDegrees(
observation.lon,
@@ -1573,14 +1566,12 @@ export function createBhoteKoshiEventLayer({
// a single high sample can make the front dive backward in screen space.
if (_sceneControls.evidencePathElevation !== 'source') {
// Sample once while preparing the shot, never from an animation frame.
try {
height = _viewer?.scene?.sampleHeight?.(
position,
[_floodCore, _floodHalo, _surgePoint].filter(Boolean),
);
} catch {}
const heights = _viewer?.scene ? heightsFor(_viewer.scene) : null;
height = heights?.now(position, {
exclude: [_floodCore, _floodHalo, _surgePoint].filter(Boolean),
});
if (!Number.isFinite(height))
height = _viewer?.scene?.globe?.getHeight?.(position);
height = heights?.now(position, { against: 'terrain' });
}
if (!Number.isFinite(height)) height = point.fallbackElevationM;
return Cesium.Cartesian3.fromDegrees(point.lon, point.lat, height + 8);