mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
feat(globe): surface height quality
engine.surface.sampleHeightDetailed(place, { exclude, lod }) answers
{ height, source: 'tiles' | 'terrain', tileSizeM? } (src/globe/surfaceDetail.js).
On Navara tileSizeM is the extent of the tile that answered and
lod 'finest' takes the finest loaded tile, drawn or not; Cesium answers
tiles without a size, else the globe's terrain. acceptedSurfaceHeight()
turns an answer into a height or a miss: tiles larger than
MAX_FLOOR_TILE_SIZE_M (4 km) are too coarse, terrain is not the mesh.
The mesh floor sampler and the ground snap are engine-only now (a viewer
or scene reaches its engine through engineFromViewer, which also registers
the scene) and treat a coarse answer as a miss, as they treat undefined.
Measured on Navara at Austin: while the tiles stream, a 27.9 km tile reads
143.2 m where the ground is 122.5 m (a 1.8 km tile).
`exclude` takes the engine's own mover sets (MoverSet.id): Cesium hides a
set's primitive for the synchronous sample (Cesium matches excluded objects
per billboard, not per collection); Navara's ray only meets tiles.
"sampleHeight undefined in the app on Navara" did not reproduce: the
startup Austin sample is 121.9 m (tile 889 m) on Navara and 122.4 m on
Cesium; one ModelMesh class is patched.
S2 QA (scripts/qa-surface-heights.mjs, 20 places at JFK, SFO, AUS, roofs
and streets): Navara within 2 m of Cesium at 19/20 from 800 m and 18/20
from 15 km with lod 'finest' (gates 18 and 15); offsets per region
-0.21 / +0.4 / +0.1 m; the Empire State roof differs by 39 m (spire
geometry). Navara tile sizes at 800 m: 112-314 m.
This commit is contained in:
1 parent
f640890cf6
commit
645f042450
20 files changed
+635
-244
No files matched your search
@@ -64,7 +64,10 @@ export function createCesiumEngine(viewer, options = {}) {
|
||||
...(options.moverFactories ? { factories: options.moverFactories } : {}),
|
||||
})),
|
||||
render,
|
||||
surface: createCesiumSurface(viewer),
|
||||
// Mover sets in a surface `exclude` are hidden for the sample.
|
||||
surface: createCesiumSurface(viewer, {
|
||||
engineExclude: (entry) => movers?.primitiveOf(entry) ?? null,
|
||||
}),
|
||||
input: createCesiumInput(viewer, { ...options, picker: picker.picker }),
|
||||
credits,
|
||||
maps: createCesiumMaps(viewer, {
|
||||
|
||||
@@ -13,11 +13,15 @@ const engines = new WeakMap();
|
||||
* @returns {() => void} Unregister (only while it is still the registered one).
|
||||
*/
|
||||
export function registerViewerEngine(viewer, engine) {
|
||||
// The viewer's Cesium camera names the same engine, so callers that hold
|
||||
// only viewer.camera (camera sensitivity claims) reach the engine camera.
|
||||
const keys = [viewer, viewer?.camera, viewer?.scene?.camera].filter(
|
||||
(key, index, all) => key != null && all.indexOf(key) === index,
|
||||
);
|
||||
// The viewer's scene and Cesium camera name the same engine, so callers
|
||||
// that hold only viewer.scene (surface samplers) or viewer.camera (camera
|
||||
// sensitivity claims) reach the engine.
|
||||
const keys = [
|
||||
viewer,
|
||||
viewer?.scene,
|
||||
viewer?.camera,
|
||||
viewer?.scene?.camera,
|
||||
].filter((key, index, all) => key != null && all.indexOf(key) === index);
|
||||
for (const key of keys) engines.set(key, engine);
|
||||
return () => {
|
||||
for (const key of keys)
|
||||
@@ -26,8 +30,8 @@ export function registerViewerEngine(viewer, engine) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object | null | undefined} viewer A Cesium viewer or its camera,
|
||||
* or an engine (returned as is).
|
||||
* @param {object | null | undefined} viewer A Cesium viewer, its scene or
|
||||
* camera, or an engine (returned as is).
|
||||
* @returns {import('../contract.js').GlobeEngine | null} The registered
|
||||
* engine, or null.
|
||||
*/
|
||||
|
||||
@@ -307,6 +307,7 @@ export function createCesiumMovers(
|
||||
ordered.splice(at, 0, { order: options.order, primitive });
|
||||
rootCollection().add(primitive, at);
|
||||
return {
|
||||
primitive,
|
||||
commit: () => part.commit(store.dirty()),
|
||||
destroy() {
|
||||
part.destroy();
|
||||
@@ -321,6 +322,11 @@ export function createCesiumMovers(
|
||||
});
|
||||
return {
|
||||
movers: registry.movers,
|
||||
/** The primitive drawing a live mover set of this engine, or null. */
|
||||
primitiveOf(set) {
|
||||
const entry = set?.id ? registry.sets.get(set.id) : null;
|
||||
return entry && entry.set === set ? entry.part.primitive : null;
|
||||
},
|
||||
/** The set and instance a Cesium pick result belongs to, or null. */
|
||||
identify(picked) {
|
||||
const known = picked?.primitive ? owners.get(picked.primitive) : null;
|
||||
|
||||
+67
-16
@@ -1,42 +1,93 @@
|
||||
// GlobeSurface on a Cesium scene: the rendered-surface queries that
|
||||
// services/groundSnap and services/meshFloorSampler make today.
|
||||
// services/groundSnap and services/meshFloorSampler make.
|
||||
//
|
||||
// `exclude` takes the engine's own mover sets (engine.movers) as well as
|
||||
// Cesium objects: Cesium's sampleHeight matches excluded objects by the
|
||||
// picked primitive, which for a set is each billboard, point or model, so a
|
||||
// set is hidden for the sample instead (synchronous: nothing draws in
|
||||
// between) and shown again after it.
|
||||
|
||||
import { firstVisibleTileset } from '../../services/groundSnap.js';
|
||||
import { normalizeSurfaceOptions } from '../surfaceDetail.js';
|
||||
import { toGeo3 } from './convert.js';
|
||||
|
||||
const finite = (value) => (Number.isFinite(value) ? value : undefined);
|
||||
|
||||
/**
|
||||
* @param {Cesium.Viewer} viewer
|
||||
* @param {{ engineExclude?: (entry: any) => ({ show: boolean } | null) }} [options]
|
||||
* `engineExclude` maps an engine object (a mover set) to the Cesium
|
||||
* primitive that draws it, or null for a Cesium object.
|
||||
* @returns {{ surface: import('../contract.js').GlobeSurface, destroy: () => void }}
|
||||
*/
|
||||
export function createCesiumSurface(viewer) {
|
||||
export function createCesiumSurface(
|
||||
viewer,
|
||||
{ engineExclude = () => null } = {},
|
||||
) {
|
||||
const { scene } = viewer;
|
||||
const surface = {
|
||||
sampleHeight(cartographic, exclude) {
|
||||
if (!scene.sampleHeightSupported) return undefined;
|
||||
try {
|
||||
// Throws when unsupported (no depth textures); that is a miss.
|
||||
return finite(scene.sampleHeight(cartographic, exclude));
|
||||
} catch {
|
||||
return undefined;
|
||||
|
||||
// Runs `query(nativeExclude)` with the engine objects in `exclude` hidden.
|
||||
function excluding(exclude, query) {
|
||||
if (!exclude?.length) return query(exclude);
|
||||
let native = exclude;
|
||||
const hidden = [];
|
||||
for (const entry of exclude) {
|
||||
const primitive = engineExclude(entry);
|
||||
if (!primitive) continue;
|
||||
if (native === exclude)
|
||||
native = exclude.filter((other) => !engineExclude(other));
|
||||
if (primitive.show) {
|
||||
primitive.show = false;
|
||||
hidden.push(primitive);
|
||||
}
|
||||
}
|
||||
try {
|
||||
return query(native);
|
||||
} finally {
|
||||
for (const primitive of hidden) primitive.show = true;
|
||||
}
|
||||
}
|
||||
|
||||
const sampleHeight = (cartographic, exclude) => {
|
||||
if (!scene.sampleHeightSupported) return undefined;
|
||||
try {
|
||||
// Throws when unsupported (no depth textures); that is a miss.
|
||||
return excluding(exclude, (native) =>
|
||||
finite(scene.sampleHeight(cartographic, native)),
|
||||
);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
const terrainHeight = (cartographic) =>
|
||||
scene.globe ? finite(scene.globe.getHeight(cartographic)) : undefined;
|
||||
|
||||
const surface = {
|
||||
sampleHeight,
|
||||
// Cesium samples the tiles it has loaded (lod 'finest' cannot do better
|
||||
// synchronously) and does not say which tile answered.
|
||||
sampleHeightDetailed(cartographic, options) {
|
||||
const { exclude } = normalizeSurfaceOptions(options);
|
||||
const height = sampleHeight(cartographic, exclude);
|
||||
if (height !== undefined) return { height, source: 'tiles' };
|
||||
const terrain = terrainHeight(cartographic);
|
||||
return terrain === undefined
|
||||
? undefined
|
||||
: { height: terrain, source: 'terrain' };
|
||||
},
|
||||
get supported() {
|
||||
return Boolean(scene.sampleHeightSupported);
|
||||
},
|
||||
terrainHeight(cartographic) {
|
||||
return scene.globe
|
||||
? finite(scene.globe.getHeight(cartographic))
|
||||
: undefined;
|
||||
},
|
||||
terrainHeight,
|
||||
get terrainHeightSupported() {
|
||||
return Boolean(scene.globe);
|
||||
},
|
||||
clampToHeight(position, exclude) {
|
||||
if (!scene.clampToHeightSupported) return undefined;
|
||||
try {
|
||||
return toGeo3(scene.clampToHeight(position, exclude));
|
||||
return excluding(exclude, (native) =>
|
||||
toGeo3(scene.clampToHeight(position, native)),
|
||||
);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@@ -84,3 +84,44 @@ test('tilesLoaded follows the first visible tileset, else the shown globe', () =
|
||||
tileset.tilesLoaded = true;
|
||||
assert.equal(surface.tilesLoaded(), true);
|
||||
});
|
||||
|
||||
test('sampleHeightDetailed: tiles from sampleHeight, else terrain; no tile size', () => {
|
||||
const { surface, scene } = setup();
|
||||
const place = geo.cartographic.fromDegrees(20, 10, 0);
|
||||
assert.deepEqual(surface.sampleHeightDetailed(place), {
|
||||
height: 42,
|
||||
source: 'tiles',
|
||||
});
|
||||
scene.sampleHeight = () => undefined;
|
||||
assert.deepEqual(surface.sampleHeightDetailed(place, { lod: 'finest' }), {
|
||||
height: 12.5,
|
||||
source: 'terrain',
|
||||
});
|
||||
assert.throws(
|
||||
() => surface.sampleHeightDetailed(place, { lod: 2 }),
|
||||
TypeError,
|
||||
);
|
||||
});
|
||||
|
||||
test('an engine mover set in exclude is hidden for the sample, native objects pass', () => {
|
||||
const viewer = createFakeViewer();
|
||||
const collection = { show: true };
|
||||
const set = { id: 'flights' };
|
||||
const { surface } = createCesiumSurface(viewer, {
|
||||
engineExclude: (entry) => (entry === set ? collection : null),
|
||||
});
|
||||
const shownDuring = [];
|
||||
viewer.scene.sampleHeight = (position, excluded) => {
|
||||
shownDuring.push(collection.show, excluded);
|
||||
return 30;
|
||||
};
|
||||
const native = { primitive: 1 };
|
||||
const place = geo.cartographic.fromDegrees(20, 10, 0);
|
||||
assert.equal(surface.sampleHeight(place, [set, native]), 30);
|
||||
assert.deepEqual(shownDuring, [false, [native]]);
|
||||
assert.equal(collection.show, true, 'shown again after the sample');
|
||||
shownDuring.length = 0;
|
||||
surface.sampleHeightDetailed(place, { exclude: [set] });
|
||||
assert.deepEqual(shownDuring, [false, []]);
|
||||
assert.equal(collection.show, true);
|
||||
});
|
||||
@@ -183,6 +183,12 @@
|
||||
* @typedef {object} GlobeSurface
|
||||
* @property {(cartographic: Cartographic, exclude?: any[]) => (number | undefined)} sampleHeight
|
||||
* Height of the rendered scene (tiles, models) under a point.
|
||||
* @property {(cartographic: Cartographic, options?: { exclude?: any[], lod?: 'visible' | 'finest' }) => (SurfaceHeight | undefined)} sampleHeightDetailed
|
||||
* sampleHeight with the answer's quality (src/globe/surfaceDetail.js):
|
||||
* lod 'visible' (default) answers from the tiles drawn now, 'finest' from
|
||||
* the finest loaded ones (Navara; Cesium samples what it has loaded).
|
||||
* Where no tile answers, the terrain. `exclude` takes this engine's mover
|
||||
* sets as well as engine objects.
|
||||
* @property {boolean} supported Whether sampleHeight works.
|
||||
* @property {(cartographic: Cartographic) => (number | undefined)} terrainHeight
|
||||
* Height of the terrain/globe surface only (Cesium's globe.getHeight).
|
||||
@@ -192,6 +198,12 @@
|
||||
* @property {boolean} clampToHeightSupported
|
||||
* @property {() => boolean} tilesLoaded
|
||||
*
|
||||
* @typedef {object} SurfaceHeight
|
||||
* @property {number} height Metres above the ellipsoid.
|
||||
* @property {'tiles' | 'terrain'} source
|
||||
* @property {number} [tileSizeM] The size of the tile that answered
|
||||
* (Navara): the quality signal; Cesium does not say.
|
||||
*
|
||||
* @typedef {object} GlobeRender
|
||||
* @property {(reason?: string) => void} request Renders a frame soon in
|
||||
* request mode; `reason` is for diagnostics only.
|
||||
@@ -585,6 +597,7 @@ const SHAPE = {
|
||||
},
|
||||
surface: {
|
||||
sampleHeight: isFunction,
|
||||
sampleHeightDetailed: isFunction,
|
||||
supported: isBoolean,
|
||||
terrainHeight: isFunction,
|
||||
terrainHeightSupported: isBoolean,
|
||||
|
||||
+4
-1
@@ -309,12 +309,14 @@ export function createMoverStore(options) {
|
||||
* destroy. commit() hands the engine the changed range and cleans the store;
|
||||
* a destroyed set refuses every call but destroy().
|
||||
*/
|
||||
export function createMoverSet(store, { commit, destroy }) {
|
||||
export function createMoverSet(store, { id, commit, destroy }) {
|
||||
let destroyed = false;
|
||||
const alive = () => {
|
||||
if (destroyed) throw new Error('Mover set was destroyed');
|
||||
};
|
||||
return {
|
||||
// The set's id (createSet's `id`): surface exclusion and picks name it.
|
||||
id,
|
||||
set(index, fields) {
|
||||
alive();
|
||||
store.set(index, fields);
|
||||
@@ -359,6 +361,7 @@ export function createMoverRegistry(build) {
|
||||
const part = build(options, store);
|
||||
const entry = { options, store, part, set: null };
|
||||
entry.set = createMoverSet(store, {
|
||||
id: options.id,
|
||||
commit: () => part.commit(),
|
||||
destroy: () => {
|
||||
sets.delete(options.id);
|
||||
|
||||
+75
-28
@@ -20,6 +20,7 @@ import {
|
||||
Vector3,
|
||||
} from 'three';
|
||||
import * as geo from '../../geo/index.js';
|
||||
import { normalizeSurfaceOptions } from '../surfaceDetail.js';
|
||||
|
||||
const { cartesian3, cartographic, math } = geo;
|
||||
const WGS84 = geo.ellipsoid.WGS84;
|
||||
@@ -62,17 +63,22 @@ const hasPositions = (object) =>
|
||||
object.isMesh && Boolean(object.geometry?.attributes?.position?.array);
|
||||
|
||||
// The meshes with CPU positions under `object`, minus `exclude`, into
|
||||
// `out`; with `shownOnly`, only through visible objects.
|
||||
function collectMeshes(object, exclude, shownOnly, out) {
|
||||
// `out`; with `shownOnly`, only through visible objects. `roots` (when
|
||||
// given) gets the tile root of each mesh, at the same index.
|
||||
function collectMeshes(object, exclude, shownOnly, out, roots, root) {
|
||||
if (exclude.has(object) || (shownOnly && !object.visible)) return;
|
||||
if (hasPositions(object)) out.push(object);
|
||||
if (hasPositions(object)) {
|
||||
out.push(object);
|
||||
roots?.push(root);
|
||||
}
|
||||
const children = object.children;
|
||||
for (let i = 0; i < children.length; i++)
|
||||
collectMeshes(children[i], exclude, shownOnly, out);
|
||||
collectMeshes(children[i], exclude, shownOnly, out, roots, root);
|
||||
}
|
||||
|
||||
// Reused between calls (sampleHeight runs per frame in the ground floor).
|
||||
const shownMeshes = [];
|
||||
const shownRoots = [];
|
||||
const tileMeshesScratch = [];
|
||||
|
||||
// Nearest hit along worldRay with one mesh, in metres. Not Mesh.raycast: that
|
||||
@@ -114,25 +120,32 @@ function meshHitDistance(mesh, far) {
|
||||
}
|
||||
|
||||
// Nearest hit over meshes, tried in the order the ray enters their bounding
|
||||
// spheres; stops once the next sphere starts below the best hit.
|
||||
function nearestHit(meshes, ray) {
|
||||
// spheres; stops once the next sphere starts below the best hit. Returns
|
||||
// the distance and the tile root (from `roots`) of the mesh hit.
|
||||
function nearestHit(meshes, roots, ray) {
|
||||
worldRay.origin.set(ray.origin.x, ray.origin.y, ray.origin.z);
|
||||
worldRay.direction.set(ray.direction.x, ray.direction.y, ray.direction.z);
|
||||
const candidates = [];
|
||||
for (const mesh of meshes) {
|
||||
for (let i = 0; i < meshes.length; i++) {
|
||||
const mesh = meshes[i];
|
||||
const geometry = mesh.geometry;
|
||||
if (!geometry.boundingSphere) geometry.computeBoundingSphere();
|
||||
sphere.copy(geometry.boundingSphere).applyMatrix4(mesh.matrixWorld);
|
||||
if (!worldRay.intersectSphere(sphere, entry)) continue;
|
||||
candidates.push([worldRay.origin.distanceTo(entry), mesh]);
|
||||
candidates.push([worldRay.origin.distanceTo(entry), i]);
|
||||
}
|
||||
candidates.sort((x, y) => x[0] - y[0]);
|
||||
let nearest = Infinity;
|
||||
for (const [enter, mesh] of candidates) {
|
||||
let root = null;
|
||||
for (const [enter, i] of candidates) {
|
||||
if (enter > nearest) break;
|
||||
nearest = Math.min(nearest, meshHitDistance(mesh, ray.far));
|
||||
const distance = meshHitDistance(meshes[i], ray.far);
|
||||
if (distance < nearest) {
|
||||
nearest = distance;
|
||||
root = roots[i];
|
||||
}
|
||||
}
|
||||
return nearest;
|
||||
return { distance: nearest, root };
|
||||
}
|
||||
|
||||
// A tile's extent: the bounding sphere around all its meshes, in
|
||||
@@ -167,20 +180,22 @@ const NO_EXCLUDE = new Set();
|
||||
// the top one among them. Tiles the ray passes are tried finest first, and
|
||||
// coarser ones stop being tried once a hit sets the limit, so the coarse
|
||||
// parents' triangles are never walked.
|
||||
// With `includeShown` (lod 'finest') the shown tiles take part too.
|
||||
const hiddenTiles = [];
|
||||
function finestHiddenHit(roots, exclude, ray) {
|
||||
function finestHiddenHit(roots, exclude, ray, includeShown = false) {
|
||||
worldRay.origin.set(ray.origin.x, ray.origin.y, ray.origin.z);
|
||||
worldRay.direction.set(ray.direction.x, ray.direction.y, ray.direction.z);
|
||||
hiddenTiles.length = 0;
|
||||
for (let i = 0; i < roots.length; i++) {
|
||||
const root = roots[i];
|
||||
if (root.visible || exclude.has(root)) continue;
|
||||
if ((root.visible && !includeShown) || exclude.has(root)) continue;
|
||||
const extent = tileExtent(root);
|
||||
if (extent && worldRay.intersectsSphere(extent)) hiddenTiles.push(root);
|
||||
}
|
||||
hiddenTiles.sort((x, y) => extents.get(x).radius - extents.get(y).radius);
|
||||
let limit = Infinity;
|
||||
let nearest = Infinity;
|
||||
let answered = null;
|
||||
for (const root of hiddenTiles) {
|
||||
const radius = extents.get(root).radius;
|
||||
if (radius > limit) break;
|
||||
@@ -191,12 +206,21 @@ function finestHiddenHit(roots, exclude, ray) {
|
||||
hit = Math.min(hit, meshHitDistance(mesh, ray.far));
|
||||
if (!Number.isFinite(hit)) continue;
|
||||
if (limit === Infinity) limit = 1.5 * radius;
|
||||
nearest = Math.min(nearest, hit);
|
||||
if (hit < nearest) {
|
||||
nearest = hit;
|
||||
answered = root;
|
||||
}
|
||||
}
|
||||
hiddenTiles.length = 0;
|
||||
return nearest;
|
||||
return { distance: nearest, root: answered };
|
||||
}
|
||||
|
||||
// The size of a tile that answered: its extent's diameter.
|
||||
const tileSizeOf = (root) => {
|
||||
const extent = root ? tileExtent(root) : null;
|
||||
return extent ? 2 * extent.radius : undefined;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {{ sampleTerrainHeight: (place: { lat: number, lng: number }) => number | undefined }} view
|
||||
* @param {{
|
||||
@@ -221,25 +245,48 @@ export function createNavaraSurface(view, hooks) {
|
||||
});
|
||||
return Number.isFinite(height) ? height : undefined;
|
||||
};
|
||||
const sampleHeight = (place, exclude) => {
|
||||
if (!supported) return undefined;
|
||||
const terrainDetail = (place) => {
|
||||
const height = terrainHeight(place);
|
||||
return height === undefined ? undefined : { height, source: 'terrain' };
|
||||
};
|
||||
const sampleHeightDetailed = (place, options) => {
|
||||
const { exclude, lod } = normalizeSurfaceOptions(options);
|
||||
if (!supported) return terrainDetail(place);
|
||||
const ray = surfaceRay(place);
|
||||
const roots = hooks.tileRoots();
|
||||
const excluded = exclude ? new Set(exclude) : NO_EXCLUDE;
|
||||
// The tiles on screen first; where they do not reach (the ground under
|
||||
// a camera looking ahead is outside the frustum), the hidden ones.
|
||||
shownMeshes.length = 0;
|
||||
for (let i = 0; i < roots.length; i++)
|
||||
collectMeshes(roots[i], excluded, true, shownMeshes);
|
||||
let distance = nearestHit(shownMeshes, ray);
|
||||
shownMeshes.length = 0;
|
||||
if (!Number.isFinite(distance))
|
||||
distance = finestHiddenHit(roots, excluded, ray);
|
||||
return heightAlongRay(ray, distance) ?? terrainHeight(place);
|
||||
const excluded = exclude.length ? new Set(exclude) : NO_EXCLUDE;
|
||||
let hit = { distance: Infinity, root: null };
|
||||
if (lod === 'visible') {
|
||||
// The tiles on screen first; where they do not reach (the ground
|
||||
// under a camera looking ahead is outside the frustum), the hidden
|
||||
// ones.
|
||||
shownMeshes.length = 0;
|
||||
shownRoots.length = 0;
|
||||
for (let i = 0; i < roots.length; i++)
|
||||
collectMeshes(
|
||||
roots[i],
|
||||
excluded,
|
||||
true,
|
||||
shownMeshes,
|
||||
shownRoots,
|
||||
roots[i],
|
||||
);
|
||||
hit = nearestHit(shownMeshes, shownRoots, ray);
|
||||
shownMeshes.length = 0;
|
||||
shownRoots.length = 0;
|
||||
}
|
||||
if (!Number.isFinite(hit.distance))
|
||||
hit = finestHiddenHit(roots, excluded, ray, lod === 'finest');
|
||||
const height = heightAlongRay(ray, hit.distance);
|
||||
if (height === undefined) return terrainDetail(place);
|
||||
return { height, source: 'tiles', tileSizeM: tileSizeOf(hit.root) };
|
||||
};
|
||||
const sampleHeight = (place, exclude) =>
|
||||
sampleHeightDetailed(place, exclude ? { exclude } : undefined)?.height;
|
||||
return {
|
||||
surface: {
|
||||
sampleHeight,
|
||||
sampleHeightDetailed,
|
||||
supported,
|
||||
terrainHeight,
|
||||
terrainHeightSupported: true,
|
||||
|
||||
@@ -215,3 +215,43 @@ test('among hidden tiles the finest tile wins by its extent, not by a small mesh
|
||||
'the fine tile',
|
||||
);
|
||||
});
|
||||
|
||||
test('sampleHeightDetailed names the source and the size of the answering tile', () => {
|
||||
// A shown coarse tile (600 m wide, 3 m high) over a hidden fine one.
|
||||
const fine = tileAt(10, 45, 40);
|
||||
const coarse = tileAt(10, 45, 43, 300);
|
||||
fine.tile.visible = false;
|
||||
const { surface } = setup({
|
||||
tiles: [coarse.tile, fine.tile],
|
||||
terrain: () => 7,
|
||||
});
|
||||
const place = cartographic.fromDegrees(10, 45, 0);
|
||||
const shown = surface.sampleHeightDetailed(place);
|
||||
assert.equal(shown.source, 'tiles');
|
||||
near(shown.height, 43, 0.05, 'the shown tile');
|
||||
near(shown.tileSizeM, 600 * Math.SQRT2, 1, 'its size');
|
||||
const finest = surface.sampleHeightDetailed(place, { lod: 'finest' });
|
||||
near(finest.height, 40, 0.05, 'the finest loaded tile');
|
||||
near(finest.tileSizeM, 200 * Math.SQRT2, 1, 'its size');
|
||||
assert.deepEqual(
|
||||
surface.sampleHeightDetailed(place, { exclude: [coarse.tile, fine.tile] }),
|
||||
{ height: 7, source: 'terrain' },
|
||||
);
|
||||
assert.throws(
|
||||
() => surface.sampleHeightDetailed(place, { lod: 'best' }),
|
||||
TypeError,
|
||||
);
|
||||
const bare = setup({ tiles: [] }).surface;
|
||||
assert.equal(bare.sampleHeightDetailed(place), undefined);
|
||||
});
|
||||
|
||||
test('a hidden tile answer carries its size too', () => {
|
||||
const fine = tileAt(10, 45, 40);
|
||||
fine.tile.visible = false;
|
||||
const { surface } = setup({ tiles: [fine.tile] });
|
||||
const detail = surface.sampleHeightDetailed(
|
||||
cartographic.fromDegrees(10, 45, 0),
|
||||
);
|
||||
near(detail.height, 40, 0.05, 'hidden fine');
|
||||
near(detail.tileSizeM, 200 * Math.SQRT2, 1, 'size');
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
// engine.surface.sampleHeightDetailed, the engine-neutral part: its options
|
||||
// and the quality rule callers apply to an answer. An answer is
|
||||
// { height, source: 'tiles' | 'terrain', tileSizeM? }: `tileSizeM` is the
|
||||
// size of the tile that answered (Navara; Cesium does not say), the quality
|
||||
// signal, since a coarse tile's surface can sit tens of metres off the
|
||||
// ground (a 20 m error measured at Austin while the tiles were streaming).
|
||||
|
||||
const LODS = new Set(['visible', 'finest']);
|
||||
|
||||
/**
|
||||
* @param {{ exclude?: any[], lod?: 'visible' | 'finest' }} [options]
|
||||
* `exclude`: mover sets (or their ids), lines, or engine objects the ray
|
||||
* passes through. `lod`: 'visible' answers from the tiles drawn now (where
|
||||
* they miss, the finest loaded ones); 'finest' from the finest loaded
|
||||
* tiles, drawn or not.
|
||||
*/
|
||||
export function normalizeSurfaceOptions(options = {}) {
|
||||
const exclude = options.exclude ?? [];
|
||||
if (!Array.isArray(exclude))
|
||||
throw new TypeError('surface exclude must be an array');
|
||||
const lod = options.lod ?? 'visible';
|
||||
if (!LODS.has(lod))
|
||||
throw new TypeError(`surface lod must be 'visible' or 'finest': ${lod}`);
|
||||
return { exclude, lod };
|
||||
}
|
||||
|
||||
/**
|
||||
* The height to use from a detailed answer, or undefined (a miss): tiles
|
||||
* larger than `maxTileSizeM` are too coarse, and terrain counts only with
|
||||
* `terrain: true`.
|
||||
*/
|
||||
export function acceptedSurfaceHeight(
|
||||
detail,
|
||||
{ maxTileSizeM = Infinity, terrain = false } = {},
|
||||
) {
|
||||
if (!detail || !Number.isFinite(detail.height)) return undefined;
|
||||
if (detail.source === 'terrain') return terrain ? detail.height : undefined;
|
||||
if (Number.isFinite(detail.tileSizeM) && detail.tileSizeM > maxTileSizeM)
|
||||
return undefined;
|
||||
return detail.height;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import {
|
||||
acceptedSurfaceHeight,
|
||||
normalizeSurfaceOptions,
|
||||
} from './surfaceDetail.js';
|
||||
|
||||
test('surface options: exclude list and lod visible (default) or finest', () => {
|
||||
assert.deepEqual(normalizeSurfaceOptions(), { exclude: [], lod: 'visible' });
|
||||
const set = { id: 'flights' };
|
||||
assert.deepEqual(normalizeSurfaceOptions({ exclude: [set], lod: 'finest' }), {
|
||||
exclude: [set],
|
||||
lod: 'finest',
|
||||
});
|
||||
assert.throws(() => normalizeSurfaceOptions({ lod: 'best' }), TypeError);
|
||||
assert.throws(() => normalizeSurfaceOptions({ exclude: 'x' }), TypeError);
|
||||
});
|
||||
|
||||
test('a coarse tile answer is a miss; terrain only when asked for', () => {
|
||||
const fine = { height: 122.5, source: 'tiles', tileSizeM: 180 };
|
||||
const coarse = { height: 143.2, source: 'tiles', tileSizeM: 5200 };
|
||||
const cesium = { height: 122.4, source: 'tiles' };
|
||||
const terrain = { height: 120, source: 'terrain' };
|
||||
const limit = { maxTileSizeM: 1000 };
|
||||
assert.equal(acceptedSurfaceHeight(fine, limit), 122.5);
|
||||
assert.equal(acceptedSurfaceHeight(coarse, limit), undefined);
|
||||
assert.equal(acceptedSurfaceHeight(coarse), 143.2, 'no limit: kept');
|
||||
assert.equal(acceptedSurfaceHeight(cesium, limit), 122.4, 'no size: kept');
|
||||
assert.equal(acceptedSurfaceHeight(terrain, limit), undefined);
|
||||
assert.equal(acceptedSurfaceHeight(terrain, { terrain: true }), 120);
|
||||
assert.equal(acceptedSurfaceHeight(undefined), undefined);
|
||||
assert.equal(
|
||||
acceptedSurfaceHeight({ height: NaN, source: 'tiles' }),
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
@@ -1475,6 +1475,70 @@ export function createContractSuite(
|
||||
);
|
||||
},
|
||||
],
|
||||
[
|
||||
'surface.sampleHeightDetailed: source, tile size, lod and mover-set exclusion',
|
||||
async () => {
|
||||
const place = cartographic.fromDegrees(20, 10, 0);
|
||||
const shape = (detail, label) => {
|
||||
if (detail === undefined) return;
|
||||
ensure(Number.isFinite(detail.height), `${label}: height`);
|
||||
ensure(
|
||||
detail.source === 'tiles' || detail.source === 'terrain',
|
||||
`${label}: source ${detail.source}`,
|
||||
);
|
||||
ensure(
|
||||
detail.tileSizeM === undefined ||
|
||||
(detail.source === 'tiles' && detail.tileSizeM > 0),
|
||||
`${label}: tileSizeM ${detail.tileSizeM}`,
|
||||
);
|
||||
};
|
||||
const visible = engine.surface.sampleHeightDetailed(place);
|
||||
shape(visible, 'visible');
|
||||
shape(
|
||||
engine.surface.sampleHeightDetailed(place, { lod: 'finest' }),
|
||||
'finest',
|
||||
);
|
||||
if (visible?.source === 'tiles')
|
||||
near(
|
||||
engine.surface.sampleHeight(place),
|
||||
visible.height,
|
||||
1e-6,
|
||||
'sampleHeight and the detailed tiles answer',
|
||||
);
|
||||
let rejected = null;
|
||||
try {
|
||||
engine.surface.sampleHeightDetailed(place, { lod: 'best' });
|
||||
} catch (error) {
|
||||
rejected = error;
|
||||
}
|
||||
ensure(rejected instanceof TypeError, 'an unknown lod was accepted');
|
||||
// A mover set in exclude: the sample works and the set stays.
|
||||
const set = engine.movers.createSet({
|
||||
id: 'contract-surface-exclude',
|
||||
kind: 'point',
|
||||
capacity: 1,
|
||||
point: { sizePx: 8 },
|
||||
});
|
||||
try {
|
||||
set.set(0, { position: cartesian3.fromDegrees(20, 10, 50) });
|
||||
set.setCount(1);
|
||||
set.commit();
|
||||
shape(
|
||||
engine.surface.sampleHeightDetailed(place, { exclude: [set] }),
|
||||
'excluding a set',
|
||||
);
|
||||
ensure(set.id === 'contract-surface-exclude', 'the set has no id');
|
||||
ensure(
|
||||
engine.movers
|
||||
.list()
|
||||
.some(({ id }) => id === 'contract-surface-exclude'),
|
||||
'the excluded set is gone',
|
||||
);
|
||||
} finally {
|
||||
set.destroy();
|
||||
}
|
||||
},
|
||||
],
|
||||
[
|
||||
'surface: flags, tilesLoaded, sampleHeight, terrainHeight and clampToHeight shapes',
|
||||
async () => {
|
||||
|
||||
@@ -31,6 +31,7 @@ import { createLineRegistry } from '../lines.js';
|
||||
import { createMoverRegistry } from '../movers.js';
|
||||
import { createCpuPicker } from '../cpuPicks.js';
|
||||
import { createFollowController } from '../follow.js';
|
||||
import { normalizeSurfaceOptions } from '../surfaceDetail.js';
|
||||
|
||||
const { cartesian2, cartesian3, cartographic, ellipsoid, math, matrix4 } = geo;
|
||||
const WGS84 = ellipsoid.WGS84;
|
||||
@@ -55,6 +56,7 @@ const SCRIPTABLE = new Set([
|
||||
'pickEllipsoid',
|
||||
'viewRectangle',
|
||||
'sampleHeight',
|
||||
'sampleHeightDetailed',
|
||||
'terrainHeight',
|
||||
'clampToHeight',
|
||||
'tilesLoaded',
|
||||
@@ -927,6 +929,28 @@ export function createFakeEngine(options = {}) {
|
||||
record('surface.sampleHeight', [position, exclude]);
|
||||
return scripted('sampleHeight', [position, exclude], () => undefined);
|
||||
},
|
||||
// Scripted as 'sampleHeightDetailed', else from the scripted
|
||||
// sampleHeight (tiles, no size) and terrainHeight.
|
||||
sampleHeightDetailed(position, surfaceOptions) {
|
||||
const { exclude, lod } = normalizeSurfaceOptions(surfaceOptions);
|
||||
record('surface.sampleHeightDetailed', [position, { exclude, lod }]);
|
||||
return scripted(
|
||||
'sampleHeightDetailed',
|
||||
[position, { exclude, lod }],
|
||||
() => {
|
||||
const height = scripted(
|
||||
'sampleHeight',
|
||||
[position, exclude],
|
||||
() => undefined,
|
||||
);
|
||||
if (Number.isFinite(height)) return { height, source: 'tiles' };
|
||||
const terrain = scripted('terrainHeight', [position], () => 0);
|
||||
return Number.isFinite(terrain)
|
||||
? { height: terrain, source: 'terrain' }
|
||||
: undefined;
|
||||
},
|
||||
);
|
||||
},
|
||||
supported: true,
|
||||
terrainHeight(position) {
|
||||
record('surface.terrainHeight', [position]);
|
||||
|
||||
Reference in new issue
Block a user