mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
pickGroundPosition reads Cesium's depth and ellipsoid picks, so it moves unchanged from scenePick into the listed input dispatcher, its only caller. Weather rendering's default imagery host comes from the listed shell rendering's globeImageryHost instead of reading the viewer's imagery layers.
149 lines
4.6 KiB
JavaScript
149 lines
4.6 KiB
JavaScript
import assert from 'node:assert/strict';
|
||
import test from 'node:test';
|
||
import * as Cesium from 'cesium';
|
||
import { createGroundClick, groundClickHeightOk } from './groundClick.js';
|
||
import * as scenePick from '../../data/scenePick.js';
|
||
import { pickGroundPosition } from '../../input/dispatcher.js';
|
||
import { bindFakeCesiumCamera } from '../../testSupport/fakeCameraPort.mjs';
|
||
import { streetPorts } from '../../testSupport/streetLevelFakes.mjs';
|
||
|
||
const GROUND = Cesium.Cartesian3.fromDegrees(-121.4944, 38.5816, 10);
|
||
const TILESET = { isCesium3DTileset: true };
|
||
|
||
/** A viewer double bound to the camera and query ports, as the app binds its renderer. */
|
||
const viewerWith = (viewer) =>
|
||
bindFakeCesiumCamera(viewer, streetPorts(viewer));
|
||
|
||
/**
|
||
* A layer state with providers `{id, on, groundClick, configured, available}`;
|
||
* `picked` is where the depth buffer lands, `ellipsoid` the fallback hit.
|
||
* `click(pick)` clicks as the click dispatcher reports it: whether `pick`
|
||
* is the map itself, and the ground under the click.
|
||
*/
|
||
function harness({
|
||
providers = [
|
||
{ id: 'mapillary', groundClick: false },
|
||
{ id: 'google', groundClick: true },
|
||
],
|
||
height = 400,
|
||
enabled = true,
|
||
picked = GROUND,
|
||
ellipsoid = undefined,
|
||
} = {}) {
|
||
const opened = [];
|
||
const state = {
|
||
enabled,
|
||
services: {},
|
||
providers: new Map(
|
||
providers.map(
|
||
({
|
||
id,
|
||
on = true,
|
||
groundClick,
|
||
configured = true,
|
||
available = true,
|
||
}) => [
|
||
id,
|
||
{ on, def: { id, groundClick }, status: { configured }, available },
|
||
],
|
||
),
|
||
),
|
||
viewer: viewerWith({
|
||
scene: { pickPositionSupported: true, pickPosition: () => picked },
|
||
camera: {
|
||
pickEllipsoid: () => ellipsoid,
|
||
positionCartographic: Cesium.Cartographic.fromDegrees(
|
||
-121.4944,
|
||
38.5816,
|
||
height,
|
||
),
|
||
},
|
||
}),
|
||
};
|
||
const openAtGround = createGroundClick({
|
||
state,
|
||
openNearest: (point, options) => {
|
||
opened.push({ point, options });
|
||
return Promise.resolve(true);
|
||
},
|
||
isAvailable: (entry) => entry.available,
|
||
});
|
||
const click = (pick) =>
|
||
openAtGround({
|
||
surface: scenePick.isSurfacePick(pick),
|
||
ground: () => pickGroundPosition(state.viewer, { x: 1, y: 1 }),
|
||
});
|
||
return { opened, click };
|
||
}
|
||
|
||
const near = (point) =>
|
||
Math.abs(point.lon - -121.4944) < 1e-9 &&
|
||
Math.abs(point.lat - 38.5816) < 1e-9;
|
||
|
||
test('a click on the map opens the click-to-open providers there, without moving the camera', () => {
|
||
for (const picked of [undefined, { primitive: TILESET }, { content: {} }]) {
|
||
const { opened, click } = harness();
|
||
click(picked);
|
||
assert.equal(opened.length, 1, JSON.stringify(picked));
|
||
assert.ok(near(opened[0].point));
|
||
assert.deepEqual(opened[0].options, {
|
||
providerIds: ['google'],
|
||
frame: false,
|
||
});
|
||
}
|
||
});
|
||
|
||
test('where the depth buffer names no place, the ellipsoid answers', () => {
|
||
const { opened, click } = harness({
|
||
picked: new Cesium.Cartesian3(500, 0, 0),
|
||
ellipsoid: GROUND,
|
||
});
|
||
click(undefined);
|
||
assert.ok(near(opened[0].point));
|
||
});
|
||
|
||
test('nothing opens over another layer’s object, the sky, from high up, off its map stack, or with the provider off', () => {
|
||
const cases = [
|
||
{ picked: { id: { id: 'flight:1' } }, label: 'an entity' },
|
||
{ picked: { id: 'cctv:7', primitive: TILESET }, label: 'an id on a tile' },
|
||
{ options: { picked: null, ellipsoid: undefined }, label: 'the sky' },
|
||
{ options: { height: 5000 }, label: 'high up' },
|
||
{
|
||
options: {
|
||
providers: [{ id: 'google', groundClick: true, available: false }],
|
||
},
|
||
label: 'not Google 3D',
|
||
},
|
||
{
|
||
options: { providers: [{ id: 'google', groundClick: true, on: false }] },
|
||
label: 'off',
|
||
},
|
||
{
|
||
options: {
|
||
providers: [{ id: 'google', groundClick: true, configured: false }],
|
||
},
|
||
label: 'no key',
|
||
},
|
||
{ options: { enabled: false }, label: 'layer off' },
|
||
];
|
||
for (const { picked, options = {}, label } of cases) {
|
||
const { opened, click } = harness(options);
|
||
assert.equal(click(picked), false, label);
|
||
assert.equal(opened.length, 0, label);
|
||
}
|
||
});
|
||
|
||
test('street zoom is measured above the ground under the camera', () => {
|
||
const viewer = (height) =>
|
||
viewerWith({
|
||
camera: {
|
||
positionCartographic: Cesium.Cartographic.fromDegrees(0, 0, height),
|
||
},
|
||
scene: { globe: { getHeight: () => 1500 } },
|
||
});
|
||
// 1,500 m up a plateau, 2,000 m above it: still a street.
|
||
assert.equal(groundClickHeightOk(viewer(3500)), true);
|
||
assert.equal(groundClickHeightOk(viewer(5000)), false);
|
||
assert.equal(groundClickHeightOk(null), false);
|
||
});
|