Draw the installations layer through the renderer ports

The site dots, outlines and selected footprint fill are now draw-port sets
(points, draped lines, clamped shapes) instead of Cesium entities. The
selected site's dot is drawn by a set of its own, so selecting a site
redraws no other dot. Selection is registered through plain carrier
objects in the context store, as the local GeoJSON layer does, and the
tracked card reads the site's label and position from the carrier's entity
record.

A double-click on a site dot now follows it through camera.follow (gated on
the stock double-click follow), centred on the surface under the dot when
its height is loaded; one on an outline or fill zooms to it. Hiding the
layer stops a follow it started. The layer no longer lowers the shared
camera change sensitivity: its own camera-port listener carries its
threshold.

The follow offset fitting moves to src/geo/frames.js and is shared with
the local GeoJSON layer. rendering.js leaves the Cesium allow-list
(33 -> 32); the layer's entry joins DRAW_FROM_CALLER.
This commit is contained in:
Sameh Khamis committed 2026-11-08 12:13:29 -08:00
1 parent a59f9b10b2
commit 1bff078097
21 files changed
+864 -337

No files matched your search

+10 -28
View File
@@ -8,13 +8,8 @@ import {
findQuery,
} from '../render/rendererOf.js';
import { cssColor } from '../render/color.js';
import {
WGS84,
fromDegrees,
fromDegreesInto,
toLonLatH,
} from '../geo/ellipsoid.js';
import { offsetFromHeadingPitchRange } from '../geo/frames.js';
import { fromDegrees, fromDegreesInto, toLonLatH } from '../geo/ellipsoid.js';
import { followOffsetFitting } from '../geo/frames.js';
import { sphereFromPoints } from '../geo/sphere.js';
import { densifyRhumb } from '../geo/rhumb.js';
import { heightsOf } from '../queries/heights.js';
@@ -321,32 +316,19 @@ const STEM_WIDTH_PX = 3.5;
const PIN_SIZE_PX = 8;
const BLACK = Object.freeze([0, 0, 0, 1]);
/** The range the default renderer frames a sphere of no size from. */
const MINIMUM_FIT_RANGE_M = 100;
/**
* Where the camera frames a part it starts following, as the default
* renderer framed an entity it started following with no view of its own:
* looking at the bounds' centre, heading north, pitched down 45° (steeper
* far from the ground), at the range that fits the bounds in view, within
* the camera's zoom limits. Returned as the eye's offset from the centre,
* east-north-up there; `at` is where the part stands.
* renderer framed an entity it started following with no view of its own
* (`followOffsetFitting`); `at` is where the part stands.
*/
function followViewFrom(sphere, at, camera) {
const pose = camera.pose?.();
const fovy = Number.isFinite(pose?.fovy) ? pose.fovy : Math.PI / 3;
const aspect = Number.isFinite(pose?.aspectRatio) ? pose.aspectRatio : 1;
const tanPhi = Math.tan(fovy / 2);
const tanTheta = aspect * tanPhi;
let range =
sphere.radius === 0
? MINIMUM_FIT_RANGE_M
: Math.max(sphere.radius / tanTheta, sphere.radius / tanPhi);
const { minDistance = 0, maxDistance = Infinity } = camera.controls ?? {};
range = Math.min(Math.max(range, minDistance), maxDistance);
const factor =
2 - 1 / Math.max(1, Math.hypot(at.x, at.y, at.z) / WGS84.radii.x);
return offsetFromHeadingPitchRange(0, (-Math.PI / 4) * factor, range);
return followOffsetFitting(sphere, at, {
fovy: pose?.fovy,
aspectRatio: pose?.aspectRatio,
minDistance: camera.controls?.minDistance,
maxDistance: camera.controls?.maxDistance,
});
}
/** The GeoJSON loader's `marker-size` names, in pixels. */
+59 -19
View File
@@ -28,6 +28,26 @@ import {
import * as Cesium from 'cesium';
import { registerEntityContext, selectEntityContext, getSelectedEntityContext, clearSelectedEntityContextForLayer } from './contextStore.js';
import { readEntityRecord } from './entityRecords.js';
import { drawOf } from '../render/rendererOf.js';
/** The installations layer's live site-dot set on `viewer`'s draw port. */
function dotSetOf(viewer) {
return [...drawOf(viewer).sets.values()].find(
(set) => !set.disposed && /^military-installations:\d+:dots$/.test(set.spec.id),
);
}
/** The installations layer's live selected-dot set on `viewer`'s draw port. */
function selectedSetOf(viewer) {
return [...drawOf(viewer).sets.values()].find(
(set) => !set.disposed && /^military-installations:\d+:selected$/.test(set.spec.id),
);
}
/** The drawn site dots on `viewer`, in drawing order. */
function dotsOf(viewer) {
return [...(dotSetOf(viewer)?.items.values() ?? [])];
}
test('clicking a selected installation again or empty map clears it through refresh', async () => {
const run = await runInstallationLoad({ elements: [{ type: 'node', id: 42,
@@ -40,7 +60,7 @@ test('clicking a selected installation again or empty map clears it through refr
assert.equal(getSelectedEntityContext(), null);
await militaryInstallationsLayer.update();
assert.equal(getSelectedEntityContext(), null, 'refresh must not resurrect selection');
assert.equal(run.entities()[0].point.pixelSize.getValue(), 9);
assert.equal(run.dots()[0].pixelSize, 9);
}
} finally { run.restore(); }
});
@@ -58,7 +78,7 @@ test('background installation paints neither reopen Contacts nor resurrect a cle
await militaryInstallationsLayer.update();
assert.equal(getSelectedEntityContext(),null);
assert.equal(selected(),1,'an external clear must survive the next paint');
assert.equal(run.entities()[0].point.pixelSize.getValue(),9);
assert.equal(run.dots()[0].pixelSize,9);
} finally { run.restore(); }
});
@@ -90,8 +110,9 @@ test('switching sites keeps the new selection through refresh and disable clears
assert.equal(getSelectedEntityContext()?.id, 'osm:node:43');
await militaryInstallationsLayer.update();
assert.equal(getSelectedEntityContext()?.id, 'osm:node:43');
assert.equal(run.entities().find(e => e.id === 'osm:node:42').point.pixelSize.getValue(), 9);
assert.equal(run.entities().find(e => e.id === 'osm:node:43').point.pixelSize.getValue(), 13);
assert.equal(run.dots().find(e => e.id === 'osm:node:42').show, true);
assert.equal(run.dots().find(e => e.id === 'osm:node:43').show, false);
assert.deepEqual(run.selectedDots().map(e => [e.id, e.pixelSize]), [['osm:node:43', 13]]);
militaryInstallationsLayer.disable();
run.click('osm:node:42');
assert.equal(getSelectedEntityContext(), null, 'disabled layer ignores clicks');
@@ -133,7 +154,7 @@ test('places installation anchors on the shared cached rendered floor', () => {
_clearMeshFloorCellsForTest();
});
test('real enabled installation entities carry no native label graphics', async () => {
test('real enabled installation markers carry no native label graphics', async () => {
const originalDocument = globalThis.document;
const originalWindow = globalThis.window;
const originalFetch = globalThis.fetch;
@@ -205,9 +226,11 @@ test('real enabled installation entities carry no native label graphics', async
militaryInstallationsLayer.init(viewer);
militaryInstallationsLayer.enable();
await militaryInstallationsLayer.update();
const entities = dataSources[0].entities.values;
assert.ok(entities.length > 0, 'runtime guard requires rendered installation records');
assert.ok(entities.every((entity) => entity.label === undefined));
// The dots are a draw-port point set, which draws no text of its own.
const dots = dotsOf(viewer);
assert.ok(dots.length > 0, 'runtime guard requires rendered installation records');
assert.equal(dotSetOf(viewer).kind, 'points');
assert.ok(dots.every((dot) => dot.text === undefined && dot.label === undefined));
} finally {
militaryInstallationsLayer.destroy(viewer);
globalThis.fetch = originalFetch;
@@ -362,12 +385,20 @@ async function runInstallationLoad({
return {
requests,
cameraFlights,
entities: () => dataSources[0]?.entities?.values || [],
// Each drawn site's carrier: its id, and its record of what the tracked
// card and the shared selection read.
entities: () => dotsOf(viewer).map((dot) => dot.meta),
dots: () => dotsOf(viewer),
dotWrites: () => dotSetOf(viewer)?.writes ?? 0,
selectedDots: () => [...(selectedSetOf(viewer)?.items.values() ?? [])],
contextLabels: () => contextEvents,
stats: () => militaryInstallationsLayer.getStats(),
click(target) {
const entity = typeof target === 'string' ? dataSources[0].entities.getById(target) : target;
picked = entity ? { id: entity } : undefined;
// A site's dot, picked as the draw port's pick id names it.
const dot = typeof target === 'string' ? dotSetOf(viewer)?.items.get(target) : null;
picked = dot
? { id: { set: dotSetOf(viewer).spec.id, id: dot.id, meta: dot.meta } }
: target ? { id: target } : undefined;
clickAction({ position: { x: 0, y: 0 } });
},
renderRequests: () => getRenderGovernorDiagnostics().recentRequests.map((item) => item.reason),
@@ -638,16 +669,23 @@ test('a floor that lands after the render deadline lifts the dots off the ellips
};
bindFakeCesiumCamera(viewer, fakeScenePorts(viewer));
const heightOf = (entity) => Cesium.Cartographic.fromCartesian(
entity.position.getValue(Cesium.JulianDate.now()),
).height;
// Where the dot stands (its display position, which the follow and the
// tracked card read), and the height it is drawn at before clamping.
const heightOf = (dot) => {
const at = readEntityRecord(dot.meta).displayPosition();
const height = Cesium.Cartographic.fromCartesian(
new Cesium.Cartesian3(at.x, at.y, at.z),
).height;
assert.ok(Math.abs(dot.pos.height - height) < 1e-6);
return height;
};
try {
militaryInstallationsLayer.init(viewer);
militaryInstallationsLayer.enable();
await militaryInstallationsLayer.update();
const buried = dataSources[0].entities.values[0];
const buried = dotsOf(viewer)[0];
assert.ok(buried, 'the cold-floor pass still renders the record');
assert.ok(Math.abs(heightOf(buried)) < 1, 'a cold floor anchors at the ellipsoid, as before');
@@ -657,7 +695,7 @@ test('a floor that lands after the render deadline lifts the dots off the ellips
}
assert.equal(cachedGroundFloor(lat, lon), 2400, 'the late floor landed in the shared cache');
const lifted = dataSources[0].entities.values[0];
const lifted = dotsOf(viewer)[0];
assert.ok(
Math.abs(heightOf(lifted) - 2401.5) < 0.5,
`re-render must lift the dot onto the resolved floor, got ${heightOf(lifted)}`,
@@ -885,16 +923,18 @@ test('a Contacts anchor replaces the viewport and ignores small subject moves',
} finally { run.restore(); }
});
test('unchanged installations keep their entities across reloads', async () => {
test('unchanged installations keep their markers across reloads', async () => {
const run = await runInstallationLoad({ elements: [{ type: 'node', id: 42,
lat: 30.2, lon: -97.7, tags: { military: 'base', name: 'Site' } }] });
try {
const before = run.entities().slice();
assert.ok(before.length > 0);
const writes = run.dotWrites();
await militaryInstallationsLayer.update();
const after = run.entities();
assert.equal(after.length, before.length);
assert.ok(after.every((entity, index) => entity === before[index]),
'a reload with the same geometry must not recreate entities');
assert.ok(after.every((carrier, index) => carrier === before[index]),
'a reload with the same geometry must not recreate markers');
assert.equal(run.dotWrites(), writes, 'nor write them again');
} finally { run.restore(); }
});