Files
gods-eye-view/src/layers/cctv/render/displayGizmo.test.mjs
T
German Khokhlov aa6959c97b Restore ground, picking and horizon parity through the globe contract
Draw launch-pad zones as radial ground discs with the original fill, rim,
transitions, emission and depth bias. Drape Transit history and its backing
on ground corridors. Keep the current age bands and once-per-second updates;
advance the active head with a reveal fraction and rebuild only at segment
crossings. Its wider dark edge and colour core blend once, as on Cesium.
Navara composes these looks after tone mapping.

Build reveal corridors and dynamic Transit history synchronously once shared
terrain bounds are loaded. Keep requesting frames until the visible globe
tiles and committed primitives are ready: classification needs current
surface depth after a camera move. Retain the reference radial material,
its alpha envelope and the head's material-only reveal updates.

Remove the empty Navara ground effect from the render chain when the last
shown set is hidden or destroyed. It otherwise remains as the insertion
anchor for later pulse effects after its packed textures have been disposed.
Cover reveal, destruction and replacement with fresh-state comparisons for
corridors, polygons, discs and cells. Cesium drawing is unchanged by this fix.

Keep the radial pixel check at a 500 metre radius and 4 kilometre view,
sample the brightest of six inner points around the native fill gap, and
sample across the rim width. Restore its view during arrival flights and
run it last among ground checks. Both suite launchers wait for the linked
view to arrive before checking. Keep these checks with the radial look.

Pass rectangular pick windows and drill limits through input subscriptions.
Street Level uses its 9 pixel window and CCTV uses its 14 pixel window and
six-result drill. Down picks stay synchronous, moves coalesce per frame and
Navara clicks keep the GPU answer from their gesture. Add a projector
ellipsoid option and use WGS84 radii reduced by 1,000 metres for Street Level.

These changes restore the original Cesium pad fill and ground trail rather
than the temporary ring and raised lines. Navara follows the same appearance
and horizon policy, and now honours explicit CPU pick windows. Existing
callers without these options retain their defaults. The four debts share
the contract, suite and engine implementations, so keep them together.

No coupling count grew or fell: renderer lines remain 0 and 189, external
Cesium imports 3, external viewer lines 891, engine-kind checks 1, engine
resolution calls 0, extension reads 3, ground passes 1, SDK loads 3, screen
handler files 4 and engine-name lines 32.

The contract and ground suites now wait until the camera has reached the
view in their link before checking, because the app's restore flight could
land in the middle of a check; that was also the cause of the intermittent
camera-events failure. The radial check uses a launch pad's size and samples
the fill at several inner points: Cesium's own old pad leaves the same gap in
a small ground disc's fill. The pulse-band check takes its base frame once
the surface has settled.

Checked in the browser, keyless: the contract suite passes on both engines
(84 and 85 checks), the ground suite passes on both, CCTV v2 passes 58
checks, and the space, transit, Street Level and CCTV engine switches pass.
The transit and Street Level scripts fail as they do before this change.
2026-11-08 00:30:36 +04:00

392 lines
13 KiB
JavaScript

// The CCTV calibration gizmo drawn by the display, on the fake engine and
// on the Cesium engine: drags through engine.input that patch only the
// pinned record and hand the camera back, picks that need the gizmo's own
// handles, the handles' geometry, hover sizes, cancellation and teardown.
import test from 'node:test';
import assert from 'node:assert/strict';
import * as geo from '../../../geo/index.js';
import { createCalibrationGizmo } from '../../../data/cctvGizmo.js';
import { CCTV_HARNESSES } from '../../../testSupport/cctvEngines.mjs';
import { IDLE_CAMERA_COLOR } from '../policy.js';
import { createCctvDisplay } from './display.js';
const p = (x, y, z) => geo.cartesian3.fromElements(x, y, z);
const near = (actual, expected, label) =>
assert.ok(Math.abs(actual - expected) < 1e-5, `${label}: ${actual}`);
function setup(makeHarness, t) {
const harness = makeHarness();
const display = createCctvDisplay(harness.engine, { state: {} });
display.attach();
// A fixed ray per screen x: along -X from 100 m over the mount.
harness.engine.camera.pickRay = ({ x }) => ({
origin: p(6378237, x, 0),
direction: p(-1, 0, 0),
});
const renderer = display.createGizmoRenderer();
const record = {
camera: {
headingDeg: 0,
pitchDeg: 0,
rangeM: 100,
calibration: {
headingDeg: 0,
pitchDeg: 0,
offsetEastM: 0,
offsetNorthM: 0,
heightM: 0,
rangeScale: 1,
fovDeg: 0,
},
basePose: { rangeM: 100, fovDeg: 60 },
},
frustumGeometry: { rangeM: 100 },
frustumPositions: {
mount: p(6378137, 0, 0),
capCenter: p(6378137, 0, 100),
tl: p(6378150, -20, 100),
tr: p(6378150, 20, 100),
bl: p(6378124, -20, 100),
br: p(6378124, 20, 100),
},
};
let active = record;
const patches = [];
const ends = [];
const gizmo = createCalibrationGizmo({
renderer,
getActiveRecord: () => active,
applyPatch: (patch, pinned) => patches.push({ patch, pinned }),
endPatch: (pinned) => ends.push(pinned),
});
t.after(() => {
gizmo.destroy();
display.destroy();
harness.destroy();
});
gizmo.setEnabled(true);
const event = (type, x = 10) =>
harness.fire(type, {
position: { x, y: 0 },
startPosition: { x, y: 0 },
endPosition: { x, y: 0 },
});
return {
...harness,
display,
gizmo,
renderer,
record,
patches,
ends,
event,
choose: (target = { line: 'cctv-gizmo-move-east' }) =>
harness.scriptPicks(target),
switchCamera: () => {
active = { ...record };
},
};
}
for (const makeHarness of CCTV_HARNESSES) {
const { name } = makeHarness();
for (const ending of ['release', 'disable', 'destroy', 'selection']) {
test(`a gizmo ${ending} hands the camera back and ends only the pinned record (${name})`, (t) => {
const h = setup(makeHarness, t);
h.choose();
h.event('down');
assert.equal(h.gizmo.isDragging(), true);
assert.deepEqual(h.cameraInputs(), [false]);
if (ending === 'selection') {
h.switchCamera();
h.event('move', 25);
} else {
h.event('move', 25);
assert.equal(h.patches.length, 1);
assert.equal(h.patches[0].pinned, h.record);
assert.ok(Math.abs(h.patches[0].patch.offsetEastM - 15) < 1e-8);
if (ending === 'release') h.event('up');
if (ending === 'disable') h.gizmo.setEnabled(false);
if (ending === 'destroy') h.gizmo.destroy();
}
assert.deepEqual(h.cameraInputs(), [false, true]);
assert.deepEqual(h.ends, [h.record]);
assert.equal(h.gizmo.isDragging(), false);
h.event('up');
assert.equal(h.ends.length, 1);
if (ending === 'selection') assert.equal(h.patches.length, 0);
});
}
test(`gizmo input uses its window and drill to reach an overlapping handle (${name})`, (t) => {
const h = setup(makeHarness, t);
h.choose();
const calls = [];
const drill = h.engine.picker.drillPick;
h.engine.picker.pick = (at, width, height) => {
calls.push(['pick', width, height]);
return { id: 'foreign', owner: 'foreign' };
};
h.engine.picker.drillPick = (at, limit, width, height) => {
calls.push(['drill', limit, width, height]);
return drill(at, limit, width, height);
};
h.event('down');
assert.equal(h.gizmo.isDragging(), true);
assert.deepEqual(calls, [
['pick', 14, 14],
['drill', 6, 14, 14],
]);
h.event('up');
});
test(`the gizmo needs its own handle, not a sibling with its id (${name})`, (t) => {
const h = setup(makeHarness, t);
const sibling = h.engine.lines.createLine({
id: 'sibling',
pickId: 'cctv-gizmo-move-east',
widthPx: 1,
color: [1, 1, 1, 1],
});
sibling.setPositions([p(6378137, 0, 0), p(6378137, 1, 0)]);
h.choose({ line: 'sibling' });
h.event('down');
assert.equal(h.gizmo.isDragging(), false);
h.choose({ set: 'cctv-gizmo-points', index: 0 });
h.event('down');
assert.equal(h.gizmo.isDragging(), true, 'an arrow tip drags its arrow');
sibling.destroy();
});
test(`the gizmo draws eight handles and releases only those (${name})`, (t) => {
const h = setup(makeHarness, t);
const foreign = h.engine.lines.createLine({
id: 'foreign',
widthPx: 1,
color: [1, 1, 1, 1],
});
const lines = h.engine.lines.list().filter(({ id }) => id !== 'foreign');
assert.deepEqual(
lines.map(({ id }) => id),
[
'cctv-gizmo-ring-heading',
'cctv-gizmo-ring-pitch',
'cctv-gizmo-move-east',
'cctv-gizmo-move-north',
'cctv-gizmo-move-up',
],
);
assert.ok(lines.every(({ shown }) => shown));
assert.equal(lines[0].points, 97);
const [points] = h.engine.movers.list();
assert.deepEqual(
{ id: points.id, kind: points.kind, count: points.count },
{ id: 'cctv-gizmo-points', kind: 'point', count: 6 },
);
if (h.cesium) {
const ring = h.primitive('cctv-gizmo-ring-heading');
const first = ring.geometryInstances.geometry._positions[0];
assert.ok(
Math.abs(
geo.cartesian3.distance(first, h.record.frustumPositions.mount) - 12,
) < 1e-6,
);
const east = h.primitive('cctv-gizmo-move-east');
assert.equal(east.geometryInstances.geometry._width, 12);
assert.equal(east.appearance.material.type, 'PolylineArrow');
assert.ok(
Math.abs(
east.depthFailAppearance.material.uniforms.color.alpha - 0.45,
) < 1e-6,
);
const heading = h.primitive('cctv-gizmo-ring-heading');
assert.ok(
Math.abs(heading.appearance.material.uniforms.color.alpha - 0.9) < 1e-6,
);
assert.ok(
Math.abs(
heading.depthFailAppearance.material.uniforms.color.alpha - 0.45,
) < 1e-6,
);
const tips = h.primitive('cctv-gizmo-points');
const tip = tips.get(0);
near(tip.pixelSize, 14, 'tip size');
assert.equal(tip.outlineWidth, 2);
assert.ok(Math.abs(tip.outlineColor.alpha - 0.7) < 1e-6);
assert.equal(tip.disableDepthTestDistance, Number.POSITIVE_INFINITY);
assert.ok(Math.abs(tip.position.y - 19.2) < 1e-6);
near(tips.get(3).pixelSize, 13, 'range handle size');
} else {
const ring = h.engine.fake.line('cctv-gizmo-ring-heading');
assert.ok(
Math.abs(
geo.cartesian3.distance(
p(...ring.positions.slice(0, 3)),
h.record.frustumPositions.mount,
) - 12,
) < 1e-6,
);
const east = h.lineState('cctv-gizmo-move-east');
assert.equal(east.widthPx, 12);
assert.equal(east.arrow, true);
assert.ok(Math.abs(east.depthFailAlpha - 0.45) < 1e-6);
const heading = h.lineState('cctv-gizmo-ring-heading');
assert.ok(Math.abs(heading.color[3] - 0.9) < 1e-6);
assert.ok(Math.abs(heading.depthFailAlpha - 0.5) < 1e-6);
const set = h.engine.fake.moverSet('cctv-gizmo-points');
assert.equal(set.options.depthTest, 'always');
assert.equal(set.options.order, 100);
assert.deepEqual(set.options.point.outlineColor, [0, 0, 0, 0.7]);
assert.equal(set.options.point.outlinePx, 2);
assert.equal(set.store.pickIds[0], 'cctv-gizmo-move-east');
assert.ok(Math.abs(set.store.positions[1] - 19.2) < 1e-6);
near(set.store.scales[0] * set.options.point.sizePx, 14, 'tip size');
near(set.store.scales[3] * set.options.point.sizePx, 13, 'range size');
}
h.gizmo.destroy();
h.gizmo.destroy();
assert.deepEqual(
h.engine.lines.list().map(({ id }) => id),
['foreign'],
);
assert.equal(h.engine.movers.list().length, 0);
assert.equal(h.canvas.style.cursor, '');
foreign.destroy();
});
test(`hovering a handle grows it and shows the grab cursor (${name})`, (t) => {
const h = setup(makeHarness, t);
h.choose({ set: 'cctv-gizmo-points', index: 3 });
h.event('move');
assert.equal(h.canvas.style.cursor, 'grab');
const size = (index) =>
h.cesium
? h.primitive('cctv-gizmo-points').get(index).pixelSize
: h.engine.fake.moverSet('cctv-gizmo-points').store.scales[index] * 10;
near(size(3), 17, 'the hovered range handle');
near(size(0), 10, 'an arrow tip takes the handle size');
near(size(4), 10, 'a field-of-view handle');
});
for (const cancel of ['pointercancel', 'blur']) {
test(`a ${cancel} ends a pinned drag and releases the camera (${name})`, (t) => {
const h = setup(makeHarness, t);
h.choose();
h.event('down');
const target =
cancel === 'blur' ? h.canvas.ownerDocument.defaultView : h.canvas;
target.dispatchEvent(new Event(cancel));
assert.equal(h.gizmo.isDragging(), false);
assert.deepEqual(h.cameraInputs(), [false, true]);
assert.deepEqual(h.ends, [h.record]);
h.gizmo.destroy();
target.dispatchEvent(new Event(cancel));
assert.equal(h.ends.length, 1);
});
}
test(`a second down cannot start another drag (${name})`, (t) => {
const h = setup(makeHarness, t);
h.choose();
h.event('down');
h.event('down');
h.event('up');
assert.deepEqual(h.cameraInputs(), [false, true]);
assert.deepEqual(h.ends, [h.record]);
});
test(`a handle id another owner holds stops the gizmo cleanly (${name})`, (t) => {
const h = setup(makeHarness, t);
assert.throws(
() => h.renderer.createPoint('move-east', IDLE_CAMERA_COLOR, 10),
/already owned/,
);
h.gizmo.destroy();
const sibling = h.engine.lines.createLine({
id: 'cctv-gizmo-move-east',
widthPx: 1,
color: [1, 1, 1, 1],
});
assert.throws(
() =>
createCalibrationGizmo({
renderer: h.display.createGizmoRenderer(),
getActiveRecord: () => null,
applyPatch() {},
endPatch() {},
}),
/already exists/,
);
assert.deepEqual(
h.engine.lines.list().map(({ id }) => id),
['cctv-gizmo-move-east'],
);
assert.equal(h.engine.movers.list().length, 0);
sibling.destroy();
});
test(`a destroyed gizmo drops its input and listeners (${name})`, (t) => {
const h = setup(makeHarness, t);
let cancels = 0;
let moves = 0;
h.renderer.on('cancel', () => cancels++);
h.renderer.on('move', () => moves++);
h.canvas.dispatchEvent(new Event('pointercancel'));
h.canvas.ownerDocument.defaultView.dispatchEvent(new Event('blur'));
h.event('move');
assert.equal(cancels, 2);
assert.equal(moves, 1);
h.renderer.setCursor('grabbing');
h.gizmo.destroy();
h.canvas.dispatchEvent(new Event('pointercancel'));
h.event('move');
assert.equal(cancels, 2);
assert.equal(moves, 1);
assert.equal(h.canvas.style.cursor, '');
assert.throws(
() => h.renderer.createPoint('new', IDLE_CAMERA_COLOR, 10),
/destroyed/,
);
});
}
test("an arrow's shaft goes when its tip cannot be drawn", (t) => {
const [makeHarness] = CCTV_HARNESSES;
const harness = makeHarness();
t.after(() => harness.destroy());
const display = createCctvDisplay(harness.engine, { state: {} });
harness.engine.movers.createSet = () => {
throw new Error('allocation failed');
};
const renderer = display.createGizmoRenderer();
const color = { withAlpha: () => color, rgba: () => [1, 1, 1, 1], alpha: 1 };
assert.throws(
() => renderer.createLine('east', color, 8, true),
/allocation failed/,
);
assert.equal(harness.engine.lines.list().length, 0);
display.destroy();
});
test('a handle construction failure releases already-created resources', () => {
const handles = new Set();
let destroys = 0;
const renderer = {
createLine(part) {
if (part === 'move-east') throw new Error('ID already owned');
const handle = { part };
handles.add(handle);
return handle;
},
destroy() {
destroys++;
handles.clear();
},
};
assert.throws(() => createCalibrationGizmo({ renderer }), /already owned/);
assert.equal(handles.size, 0);
assert.equal(destroys, 1);
});