mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
The Cesium query port moves out of createCesiumPorts into its own module, unchanged, so it can also be built over a caller's viewer with no renderer bound. findQuery and queryOf find the bound renderer's query port or, for such a viewer, one the Cesium adapter's factory builds, as findDraw and findCamera do. It picks the sets drawn through findDraw on that viewer and its height samples see through them, so a layer can exclude its own sets; it projects, casts rays and picks the ellipsoid as the renderer's does. A renderer bound later answers new callers. The camera factory and the caller's unbound export load it, and the factory walk checks that every public entry reaching a findQuery or queryOf caller loads it, or is a Cesium-free entry whose caller does.
133 lines
4.4 KiB
JavaScript
133 lines
4.4 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import * as Cesium from 'cesium';
|
|
import test from 'node:test';
|
|
import { bindRenderer, drawOf, findQuery, queryOf } from '../rendererOf.js';
|
|
import './query.js';
|
|
|
|
/**
|
|
* A viewer's scene with what the draw and query ports touch: primitives,
|
|
* frame events, picks, and height samples that record what they see
|
|
* through.
|
|
*/
|
|
function fakeViewer() {
|
|
const added = [];
|
|
const sampled = [];
|
|
const scene = {
|
|
added,
|
|
sampled,
|
|
preRender: new Cesium.Event(),
|
|
postRender: new Cesium.Event(),
|
|
frameState: { mode: Cesium.SceneMode.SCENE3D },
|
|
camera: {
|
|
positionWC: new Cesium.Cartesian3(2e7, 0, 0),
|
|
pickEllipsoid: (s) =>
|
|
s.x === 50 && s.y === 50 ? new Cesium.Cartesian3(6378137, 0, 0) : null,
|
|
},
|
|
primitives: {
|
|
add: (primitive) => (added.push(primitive), primitive),
|
|
remove: (primitive) => {
|
|
const at = added.indexOf(primitive);
|
|
if (at >= 0) added.splice(at, 1);
|
|
return at >= 0;
|
|
},
|
|
length: 0,
|
|
},
|
|
picked: undefined,
|
|
pick() {
|
|
return this.picked;
|
|
},
|
|
drillPick() {
|
|
return this.picked ? [this.picked] : [];
|
|
},
|
|
sampleHeightSupported: true,
|
|
sampleHeight(carto, exclude) {
|
|
sampled.push(exclude);
|
|
return 12;
|
|
},
|
|
updateHeight: () => () => {},
|
|
getHeight: () => undefined,
|
|
requestRender() {},
|
|
isDestroyed: () => false,
|
|
};
|
|
return { scene };
|
|
}
|
|
|
|
test('a viewer of its own, with no renderer bound, is queried through a built port over its draw sets', () => {
|
|
const viewer = fakeViewer();
|
|
const { scene } = viewer;
|
|
const query = queryOf(viewer);
|
|
assert.equal(findQuery(viewer), query, 'one per viewer');
|
|
|
|
const ground = drawOf(viewer).points({
|
|
id: 'mine',
|
|
includeInHeightQueries: true,
|
|
});
|
|
ground.set([
|
|
{ id: 'a', pos: { lon: 0, lat: 0 }, pixelSize: 4, meta: { n: 1 } },
|
|
]);
|
|
const other = drawOf(viewer).points({ id: 'theirs' });
|
|
other.set([{ id: 'b', pos: { lon: 1, lat: 0 }, pixelSize: 4 }]);
|
|
const idsOf = (exclude) => exclude.map((id) => `${id.set}:${id.id}`).sort();
|
|
|
|
// Heights see through the sets not drawn as ground, and those excluded.
|
|
assert.equal(query.sampleHeightNow({ lon: 0, lat: 0 }), 12);
|
|
assert.deepEqual(idsOf(scene.sampled.at(-1)), ['theirs:b']);
|
|
query.sampleHeightNow({ lon: 0, lat: 0 }, { exclude: ['mine'] });
|
|
assert.deepEqual(idsOf(scene.sampled.at(-1)), ['mine:a', 'theirs:b']);
|
|
|
|
// Picks name the set's item.
|
|
scene.picked = { id: scene.added[0].get(0).id };
|
|
assert.deepEqual(query.pick(5, 5), {
|
|
set: 'mine',
|
|
id: 'a',
|
|
meta: { n: 1 },
|
|
surface: false,
|
|
});
|
|
|
|
// The screen centre's ellipsoid point, and the horizon from the eye.
|
|
assert.deepEqual(query.pickEllipsoid(50, 50), { x: 6378137, y: 0, z: 0 });
|
|
assert.equal(query.pickEllipsoid(0, 0), null);
|
|
assert.equal(query.isOccluded({ x: 6378137, y: 0, z: 0 }), false);
|
|
assert.equal(query.isOccluded({ x: -6378137, y: 0, z: 0 }), true);
|
|
|
|
// A renderer bound later answers new callers.
|
|
const unbind = bindRenderer(viewer, { query: { id: 'renderer' } });
|
|
assert.equal(findQuery(viewer).id, 'renderer');
|
|
unbind();
|
|
assert.notEqual(findQuery(viewer), query, 'built again once unbound');
|
|
ground.dispose();
|
|
other.dispose();
|
|
});
|
|
|
|
test('nothing to query, no port', () => {
|
|
assert.equal(findQuery({ scene: {} }), null);
|
|
assert.throws(() => queryOf({ scene: {} }), /no renderer to query/);
|
|
assert.throws(
|
|
() => queryOf({ scene: {}, isDestroyed: () => true }),
|
|
/destroyed/,
|
|
);
|
|
});
|
|
|
|
test('a query port built before its viewer can draw sees the sets drawn later, and goes with the viewer', () => {
|
|
const viewer = fakeViewer();
|
|
const { scene } = viewer;
|
|
const primitives = scene.primitives;
|
|
scene.primitives = undefined;
|
|
const query = queryOf(viewer);
|
|
assert.equal(query.pick(5, 5)?.set, undefined, 'nothing drawn yet');
|
|
scene.primitives = primitives;
|
|
const set = drawOf(viewer).points({ id: 'later' });
|
|
set.set([{ id: 'a', pos: { lon: 0, lat: 0 }, pixelSize: 4 }]);
|
|
scene.picked = { id: scene.added[0].get(0).id };
|
|
assert.equal(query.pick(5, 5)?.set, 'later', 'its sets are picked');
|
|
query.sampleHeightNow({ lon: 0, lat: 0 }, { exclude: ['later'] });
|
|
assert.deepEqual(
|
|
scene.sampled.at(-1).map((id) => id.set),
|
|
['later'],
|
|
'and seen through',
|
|
);
|
|
set.dispose();
|
|
viewer.isDestroyed = () => true;
|
|
assert.equal(findQuery(viewer), null, 'gone with its viewer');
|
|
});
|