Stop Navara's memory growth on engine switches, hide polygon zones reliably, and drop the Cesium camera rig

- Navara 0.2.1 never frees pick batch ids, and the mover banks rebuilt
  their instanced meshes on each attach, leaking about 260 KiB of
  WebAssembly memory per switch. A per-view pool hands released ids back
  out; the long soak now asserts native growth of at most 32 KiB per switch.
- Navara 0.2.1 sometimes drops the first show:false update on a new
  vector layer, so a zone turned off right after a switch back stayed
  drawn. Polygon displays hide by deleting their layers and rebuild them
  when shown.
- Camera verbs drive both engines through the globe contract; the Cesium
  viewer rig and the tests of states it can no longer reach are gone.
- Flights and military tracking share src/layers/aircraft/tracking.js.
- The geo import scenario reads movers only where the engine has them.
- Add the Navara section to docs/CURRENT-STATE.md and a changelog fragment.
This commit is contained in:
German Khokhlov committed 2026-11-05 21:18:23 +04:00
1 parent 7abf74518f
commit a6c64713ee
46 files changed
+2367 -2894

No files matched your search

@@ -43,6 +43,12 @@ function createViewer({ cameraPosition, target, pickPosition = null } = {}) {
positionWC: position,
directionWC: new Cesium.Cartesian3(-1, 0, 0),
upWC: new Cesium.Cartesian3(0, 0, 1),
// The basis in the camera's reference frame (the world: identity), which
// the engine's setView writes.
direction: new Cesium.Cartesian3(-1, 0, 0),
up: new Cesium.Cartesian3(0, 0, 1),
right: new Cesium.Cartesian3(0, 1, 0),
inverseTransform: Cesium.Matrix4.IDENTITY,
heading: Cesium.Math.toRadians(90),
pitch: Cesium.Math.toRadians(-90),
roll: 0,
+27 -43
View File
@@ -3,7 +3,6 @@ import * as Cesium from 'cesium';
import { guardCameraAboveGround } from '../cameraGroundGuard.js';
import { rangeForBoundingSphere } from '../data/cameraFraming.js';
import { cancelCameraArrival } from '../data/cameraArrival.js';
import { isGlobeEngine } from '../globe/contract.js';
/** All visible positions, path vertices, polygon rings, walls and overlay bounds. */
export function dataSourcePoints(dataSource, time = Cesium.JulianDate.now()) {
@@ -72,17 +71,17 @@ export function dataSourcePoints(dataSource, time = Cesium.JulianDate.now()) {
/**
* Fly synchronously to the footprint; later navigation always owns its
* camera. `viewer` is a Cesium viewer or a globe engine (Navara), which flies
* the same sphere through the contract's flyToSphere.
* camera. `engine` is the globe engine (Cesium or Navara), which flies the
* sphere through the contract's flyToSphere.
*/
export function flyToDataSource(
viewer,
engine,
dataSource,
{ duration = 1.5, ground = null } = {},
) {
const points = dataSourcePoints(dataSource);
if (!points.length) return false;
cancelCameraArrival(viewer);
cancelCameraArrival(engine);
const positions = points.map(([lon, lat, height]) =>
Cesium.Cartesian3.fromDegrees(lon, lat, height),
);
@@ -92,12 +91,9 @@ export function flyToDataSource(
Cesium.Cartographic.fromDegrees(points[0][0], points[0][1]);
const lat = Cesium.Math.toDegrees(center.latitude),
lon = Cesium.Math.toDegrees(center.longitude);
const engine = isGlobeEngine(viewer) ? viewer : null;
const terrain = engine
? engine.surface.terrainHeightSupported
? engine.surface.terrainHeight(center)
: undefined
: viewer.scene?.globe?.getHeight?.(center);
const terrain = engine.surface.terrainHeightSupported
? engine.surface.terrainHeight(center)
: undefined;
const cached = ground?.cachedGroundFloor?.(lat, lon);
const height =
Number.isFinite(terrain) && terrain > 0
@@ -119,38 +115,26 @@ export function flyToDataSource(
sphere.radius,
Cesium.Cartesian3.distance(sphere.center, position),
);
if (engine) {
const pose = engine.camera.pose();
const aspect = pose.aspectRatio ?? 1;
// Cesium's frustum fov: the wider side's angle.
const fov =
aspect > 1 ? 2 * Math.atan(Math.tan(pose.fovy / 2) * aspect) : pose.fovy;
engine.camera.flyToSphere(
{ center: sphere.center, radius: sphere.radius },
{
heading: pose.heading || 0,
pitch: -Math.PI / 2,
range: Math.max(
800,
rangeForBoundingSphere(
{ camera: { frustum: { fov, aspectRatio: aspect } } },
sphere.radius,
),
const pose = engine.camera.pose();
const aspect = pose.aspectRatio ?? 1;
// Cesium's frustum fov: the wider side's angle.
const fov =
aspect > 1 ? 2 * Math.atan(Math.tan(pose.fovy / 2) * aspect) : pose.fovy;
engine.camera.flyToSphere(
{ center: sphere.center, radius: sphere.radius },
{
heading: pose.heading || 0,
pitch: -Math.PI / 2,
range: Math.max(
800,
rangeForBoundingSphere(
{ camera: { frustum: { fov, aspectRatio: aspect } } },
sphere.radius,
),
duration,
complete: () => guardCameraAboveGround(engine, { lat, lon }),
},
);
return true;
}
viewer.camera?.flyToBoundingSphere?.(sphere, {
offset: new Cesium.HeadingPitchRange(
viewer.camera.heading || 0,
-Math.PI / 2,
Math.max(800, rangeForBoundingSphere(viewer, sphere.radius)),
),
duration,
complete: () => guardCameraAboveGround(viewer, { lat, lon }),
});
),
duration,
complete: () => guardCameraAboveGround(engine, { lat, lon }),
},
);
return true;
}
+39 -32
View File
@@ -2,8 +2,21 @@ import test from 'node:test';
import assert from 'node:assert/strict';
import * as Cesium from 'cesium';
import { cancelCameraArrival } from '../data/cameraArrival.js';
import { createCesiumEngine } from '../globe/cesium/createCesiumEngine.js';
import { createFakeViewer } from '../globe/cesium/testing/fakeViewer.mjs';
import { dataSourcePoints, flyToDataSource } from './flyToDataSource.js';
/** The Cesium engine over a recording viewer, and the sphere flights it made. */
function cesiumEngine() {
const viewer = createFakeViewer();
const engine = createCesiumEngine(viewer, { requestRender() {} });
const calls = () =>
viewer
.callsOf('camera.flyToBoundingSphere')
.map(({ args: [sphere, options] }) => ({ sphere, options }));
return { viewer, engine, calls };
}
function source() {
const ds = new Cesium.CustomDataSource('t');
ds.entities.add({
@@ -35,29 +48,23 @@ test('points come from positions, lines and polygons, as lon/lat only', () => {
});
test('the camera frames the footprint at the surface, never a buried center', () => {
const calls = [];
const viewer = {
camera: {
heading: 0,
flyToBoundingSphere: (sphere, options) => calls.push({ sphere, options }),
},
flyTo: () => calls.push('viewer.flyTo'),
};
assert.equal(flyToDataSource(viewer, source()), true);
const center = Cesium.Cartographic.fromCartesian(calls[0].sphere.center);
const { engine, calls } = cesiumEngine();
assert.equal(flyToDataSource(engine, source()), true);
const [flight] = calls();
const center = Cesium.Cartographic.fromCartesian(flight.sphere.center);
assert.ok(Math.abs(center.height) < 50, `center height ${center.height}`);
assert.ok(calls[0].options.offset.range >= 800);
assert.ok(flight.options.offset.range >= 800);
assert.equal(
typeof calls[0].options.complete,
typeof flight.options.complete,
'function',
'the ground guard runs on landing',
);
assert.equal(
flyToDataSource(viewer, new Cesium.CustomDataSource('empty')),
flyToDataSource(engine, new Cesium.CustomDataSource('empty')),
false,
);
assert.equal(
calls.length,
calls().length,
1,
'empty sources never start deferred navigation',
);
@@ -112,24 +119,24 @@ test('portrait framing accounts for FOV, high ground and symmetric extents', ()
positions: Cesium.Cartesian3.fromDegreesArray([-105, 39, -104.9, 39]),
},
});
const calls = [];
const viewer = {
scene: { globe: { getHeight: () => 2600 } },
camera: {
frustum: { fov: Math.PI / 3, aspectRatio: 390 / 844 },
flyToBoundingSphere: (sphere, options) => calls.push({ sphere, options }),
},
};
flyToDataSource(viewer, ds);
const { viewer, engine, calls } = cesiumEngine();
viewer.scene.globe.getHeight = () => 2600;
// A 60° field of view on the narrower side, as Cesium's frustum keeps it:
// vertical in portrait, horizontal in landscape.
const { frustum } = viewer.camera;
frustum.aspectRatio = 390 / 844;
frustum.fovy = Math.PI / 3;
flyToDataSource(engine, ds);
assert.ok(
Math.abs(
Cesium.Cartographic.fromCartesian(calls[0].sphere.center).height - 2600,
Cesium.Cartographic.fromCartesian(calls()[0].sphere.center).height - 2600,
) < 1e-6,
);
assert.equal(calls[0].options.offset.pitch, -Math.PI / 2);
viewer.camera.frustum.aspectRatio = 16 / 9;
flyToDataSource(viewer, ds);
assert.ok(calls[0].options.offset.range > calls[1].options.offset.range);
assert.equal(calls()[0].options.offset.pitch, -Math.PI / 2);
frustum.aspectRatio = 16 / 9;
frustum.fovy = 2 * Math.atan(Math.tan(Math.PI / 6) / frustum.aspectRatio);
flyToDataSource(engine, ds);
assert.ok(calls()[0].options.offset.range > calls()[1].options.offset.range);
const symmetric = {
entities: {
values: [
@@ -138,10 +145,10 @@ test('portrait framing accounts for FOV, high ground and symmetric extents', ()
],
},
};
assert.doesNotThrow(() => flyToDataSource(viewer, symmetric));
assert.ok(Number.isFinite(calls.at(-1).options.offset.range));
assert.doesNotThrow(() => calls.at(-1).options.complete());
cancelCameraArrival(viewer);
assert.doesNotThrow(() => flyToDataSource(engine, symmetric));
assert.ok(Number.isFinite(calls().at(-1).options.offset.range));
assert.doesNotThrow(() => calls().at(-1).options.complete());
cancelCameraArrival(engine);
});
test('non-finite positions and hidden features never enter the camera footprint', () => {
+7 -5
View File
@@ -30,7 +30,8 @@ function localizedValues(values) {
* and returns their disconnect.
*
* The imported files belong to the session; their drawing belongs to the
* attached globe. `attachScene({ viewer })` draws them on a Cesium viewer;
* attached globe. `attachScene({ viewer, engine })` draws them on a Cesium
* viewer (its engine flies the camera to them);
* `attachScene({ engine, draw })` on a globe engine without one (Navara),
* where `draw(dataSource)` resolves with that engine's drawing of a file
* ({ report, setShow, refresh, destroy }: src/layers/shared/render/navaraStory.js).
@@ -67,7 +68,8 @@ export function createGeoFileImport({
let hint = '';
const lifetime = new AbortController();
// The attached globe ({ viewer, engine, draw, controller }): a Cesium
// viewer, or an engine and its drawing; null while none is attached.
// viewer and its engine, or an engine and its drawing; null while none is
// attached.
let scene = null;
const listen = (target, type, listener, options) => {
@@ -186,9 +188,9 @@ export function createGeoFileImport({
}
const fly = (item) => {
if (!scene || !item.dataSource) return false;
if (!scene?.engine || !item.dataSource) return false;
if (!item.visible) setVisible(item.id, true);
flyToSource(scene.viewer ?? scene.engine, item.dataSource, { ground });
flyToSource(scene.engine, item.dataSource, { ground });
return true;
};
@@ -471,7 +473,7 @@ export function createGeoFileImport({
if (typeof offMapSource === 'function') groundRemovers.push(offMapSource);
const attached = (scene = {
viewer,
engine: viewer ? null : engine,
engine,
draw: viewer ? null : drawOnEngine,
controller: new AbortController(),
});
+13 -2
View File
@@ -90,6 +90,9 @@ function setup({ load, navigation, flyToSource, subscribeMapSource } = {}) {
},
flyTo: (target) => flights.push(target),
};
// The viewer's globe engine, which flies the camera to a file.
const engine = { kind: 'cesium' };
const flownOn = [];
const revoked = [];
const loads = [];
const defaultLoad = async (file, opts) => {
@@ -110,7 +113,12 @@ function setup({ load, navigation, flyToSource, subscribeMapSource } = {}) {
document,
...(navigation ? { navigation } : {}),
...(subscribeMapSource ? { subscribeMapSource } : {}),
flyToSource: flyToSource || ((_viewer, ds) => flights.push(ds)),
flyToSource:
flyToSource ||
((target, ds) => {
flownOn.push(target);
flights.push(ds);
}),
publish: (view) => {
shown = view;
},
@@ -119,15 +127,17 @@ function setup({ load, navigation, flyToSource, subscribeMapSource } = {}) {
return () => (actions = null);
},
});
tool?.attachScene({ viewer });
tool?.attachScene({ viewer, engine });
return {
tool,
els,
document,
viewer,
engine,
added,
removed,
flights,
flownOn,
revoked,
loads,
renders: () => renders,
@@ -166,6 +176,7 @@ test('a pick imports each file', async () => {
);
assert.equal(rows(t).length, 2);
assert.equal(t.flights.length, 1, 'the first file of a batch is flown to');
assert.equal(t.flownOn[0], t.engine, "the viewer's engine flies there");
assert.equal(t.shown().hint, 'Added 2 files.');
assert.deepEqual(
t.tool.list().map((f) => f.name),