mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
vessels: project label candidates with the camera read once per pass
The label pass projects every vessel in view each time it runs. It asked Cesium for each one, which reads the camera and allocates for every point. createWindowProjector reads the camera's matrices and the canvas size once and runs the same arithmetic in the same order, so the window positions are the same numbers (a test compares them bit for bit with Cesium); outside a 3D perspective view it asks Cesium as before. Grid cells are keyed by number, a cell's candidate is made only for its first vessel and updated in place, and whether a name is worth a card is kept per vessel until its name, MMSI or id changes. Measured in the browser (perf harness, 9,000 vessels, 1 min/s): the label pass 33 ms -> 11 ms, the forced visibility pass 42 ms -> 27 ms.
This commit is contained in:
1 parent
663b56f620
commit
b65bb86db3
4 files changed
+239
-14
No files matched your search
@@ -1734,6 +1734,7 @@
|
||||
"src/layers/vessels/policy.js",
|
||||
"src/layers/vessels/queries.js",
|
||||
"src/layers/vessels/rendering.js",
|
||||
"src/data/windowProjection.js",
|
||||
"src/layers/vessels/selection.js",
|
||||
"src/layers/vessels/state.js",
|
||||
"src/layers/vessels/testing.js",
|
||||
@@ -3038,6 +3039,7 @@
|
||||
"src/layers/vessels/policy.js",
|
||||
"src/layers/vessels/queries.js",
|
||||
"src/layers/vessels/rendering.js",
|
||||
"src/data/windowProjection.js",
|
||||
"src/layers/vessels/selection.js",
|
||||
"src/layers/vessels/state.js",
|
||||
"src/layers/vessels/testing.js",
|
||||
@@ -4232,6 +4234,7 @@
|
||||
"src/layers/vessels/policy.js",
|
||||
"src/layers/vessels/queries.js",
|
||||
"src/layers/vessels/rendering.js",
|
||||
"src/data/windowProjection.js",
|
||||
"src/layers/vessels/selection.js",
|
||||
"src/layers/vessels/state.js",
|
||||
"src/layers/vessels/testing.js",
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
import * as Cesium from 'cesium';
|
||||
|
||||
/**
|
||||
* A projector from world positions to window coordinates for one pass over
|
||||
* many positions under one camera, giving what
|
||||
* `Cesium.SceneTransforms.worldToWindowCoordinates` gives: in a 3D scene with
|
||||
* a perspective camera it runs the same arithmetic in the same order once the
|
||||
* camera's matrices and the canvas size are read, so the results are the same
|
||||
* numbers; otherwise it asks Cesium for each.
|
||||
*
|
||||
* Read the camera again (a new projector) whenever it may have moved.
|
||||
* @param {Cesium.Scene} scene
|
||||
* @returns {(position: Cesium.Cartesian3, result: Cesium.Cartesian2) =>
|
||||
* Cesium.Cartesian2|undefined} The window position in `result`, or
|
||||
* undefined behind the camera.
|
||||
*/
|
||||
export function createWindowProjector(scene) {
|
||||
const camera = scene?.camera;
|
||||
const frustum = camera?.frustum;
|
||||
if (
|
||||
scene?.frameState?.mode !== Cesium.SceneMode.SCENE3D ||
|
||||
!camera ||
|
||||
frustum instanceof Cesium.OrthographicFrustum ||
|
||||
frustum instanceof Cesium.OrthographicOffCenterFrustum
|
||||
)
|
||||
return (position, result) =>
|
||||
Cesium.SceneTransforms.worldToWindowCoordinates(scene, position, result);
|
||||
const v = camera.viewMatrix;
|
||||
const p = frustum.projectionMatrix;
|
||||
const width = scene.canvas.clientWidth;
|
||||
const height = scene.canvas.clientHeight;
|
||||
const halfWidth = width * 0.5;
|
||||
const halfHeight = height * 0.5;
|
||||
return (position, result) => {
|
||||
if (!position)
|
||||
return Cesium.SceneTransforms.worldToWindowCoordinates(
|
||||
scene,
|
||||
position,
|
||||
result,
|
||||
);
|
||||
const { x, y, z } = position;
|
||||
// Eye coordinates, as Matrix4.multiplyByVector computes them (w = 1).
|
||||
const ex = v[0] * x + v[4] * y + v[8] * z + v[12];
|
||||
const ey = v[1] * x + v[5] * y + v[9] * z + v[13];
|
||||
const ez = v[2] * x + v[6] * y + v[10] * z + v[14];
|
||||
const ew = v[3] * x + v[7] * y + v[11] * z + v[15];
|
||||
// At the eye itself Cesium's own steps have no direction to take.
|
||||
if (ex === 0 && ey === 0 && ez === 0)
|
||||
return Cesium.SceneTransforms.worldToWindowCoordinates(
|
||||
scene,
|
||||
position,
|
||||
result,
|
||||
);
|
||||
// Clip coordinates.
|
||||
const cx = p[0] * ex + p[4] * ey + p[8] * ez + p[12] * ew;
|
||||
const cy = p[1] * ex + p[5] * ey + p[9] * ez + p[13] * ew;
|
||||
const cz = p[2] * ex + p[6] * ey + p[10] * ez + p[14] * ew;
|
||||
const cw = p[3] * ex + p[7] * ey + p[11] * ez + p[15] * ew;
|
||||
if (cz < 0) return undefined;
|
||||
// The viewport transform of the canvas, then window y downward.
|
||||
result.x = halfWidth * (cx / cw) + halfWidth;
|
||||
result.y = height - (halfHeight * (cy / cw) + halfHeight);
|
||||
return result;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import * as Cesium from 'cesium';
|
||||
import { createWindowProjector } from './windowProjection.js';
|
||||
|
||||
function sceneLooking(
|
||||
lon,
|
||||
lat,
|
||||
heightM,
|
||||
pitchDeg,
|
||||
mode = Cesium.SceneMode.SCENE3D,
|
||||
) {
|
||||
const scene = {
|
||||
canvas: { clientWidth: 1713, clientHeight: 941 },
|
||||
drawingBufferWidth: 1713,
|
||||
drawingBufferHeight: 941,
|
||||
mapProjection: new Cesium.GeographicProjection(),
|
||||
ellipsoid: Cesium.Ellipsoid.WGS84,
|
||||
frameState: { mode, mapProjection: new Cesium.GeographicProjection() },
|
||||
globe: undefined,
|
||||
tweens: { add() {} },
|
||||
screenSpaceCameraController: { minimumZoomDistance: 1 },
|
||||
};
|
||||
scene.camera = new Cesium.Camera(scene);
|
||||
scene.camera.frustum.aspectRatio = 1713 / 941;
|
||||
scene.camera.setView({
|
||||
destination: Cesium.Cartesian3.fromDegrees(lon, lat, heightM),
|
||||
orientation: {
|
||||
heading: 0.3,
|
||||
pitch: Cesium.Math.toRadians(pitchDeg),
|
||||
roll: 0,
|
||||
},
|
||||
});
|
||||
return scene;
|
||||
}
|
||||
|
||||
function positionsAround(lon, lat, spreadDeg, count) {
|
||||
let seed = 7;
|
||||
const rnd = () => (seed = (seed * 16807) % 2147483647) / 2147483647;
|
||||
return Array.from({ length: count }, () =>
|
||||
Cesium.Cartesian3.fromDegrees(
|
||||
lon + (rnd() - 0.5) * spreadDeg,
|
||||
lat + (rnd() - 0.5) * spreadDeg,
|
||||
rnd() * 50,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
test('a 3D perspective projector gives the same numbers Cesium gives', () => {
|
||||
for (const [pitch, spread, heightM] of [
|
||||
[-90, 1, 60_000],
|
||||
[-35, 4, 60_000],
|
||||
[-5, 40, 60_000],
|
||||
// Just above the sea, points near and behind the near plane.
|
||||
[-10, 0.002, 3],
|
||||
]) {
|
||||
const scene = sceneLooking(4.1, 51.95, heightM, pitch);
|
||||
const project = createWindowProjector(scene);
|
||||
let behind = 0;
|
||||
for (const position of positionsAround(4.1, 51.95, spread, 2000)) {
|
||||
const expected = Cesium.SceneTransforms.worldToWindowCoordinates(
|
||||
scene,
|
||||
position,
|
||||
new Cesium.Cartesian2(),
|
||||
);
|
||||
const actual = project(position, new Cesium.Cartesian2());
|
||||
if (!expected) {
|
||||
behind++;
|
||||
assert.equal(actual, undefined);
|
||||
continue;
|
||||
}
|
||||
assert.equal(actual.x, expected.x);
|
||||
assert.equal(actual.y, expected.y);
|
||||
}
|
||||
if (pitch === -5)
|
||||
assert.ok(behind > 0, 'some positions fall behind the camera');
|
||||
}
|
||||
});
|
||||
|
||||
test('outside a 3D scene the projector asks Cesium', () => {
|
||||
const scene = sceneLooking(
|
||||
4.1,
|
||||
51.95,
|
||||
60_000,
|
||||
-90,
|
||||
Cesium.SceneMode.COLUMBUS_VIEW,
|
||||
);
|
||||
const calls = [];
|
||||
const original = Cesium.SceneTransforms.worldToWindowCoordinates;
|
||||
Cesium.SceneTransforms.worldToWindowCoordinates = (...args) => {
|
||||
calls.push(args);
|
||||
return args[2];
|
||||
};
|
||||
try {
|
||||
const result = new Cesium.Cartesian2();
|
||||
const position = Cesium.Cartesian3.fromDegrees(4.1, 51.95);
|
||||
assert.equal(createWindowProjector(scene)(position, result), result);
|
||||
assert.equal(calls.length, 1);
|
||||
assert.equal(calls[0][1], position);
|
||||
} finally {
|
||||
Cesium.SceneTransforms.worldToWindowCoordinates = original;
|
||||
}
|
||||
});
|
||||
|
||||
test('at the eye itself, or with no position, the projector asks Cesium', () => {
|
||||
const scene = sceneLooking(4.1, 51.95, 60_000, -90);
|
||||
const original = Cesium.SceneTransforms.worldToWindowCoordinates;
|
||||
const asked = [];
|
||||
Cesium.SceneTransforms.worldToWindowCoordinates = (_scene, position) => {
|
||||
asked.push(position);
|
||||
return undefined;
|
||||
};
|
||||
try {
|
||||
const project = createWindowProjector(scene);
|
||||
const eye = Cesium.Cartesian3.clone(scene.camera.positionWC);
|
||||
assert.equal(project(eye, new Cesium.Cartesian2()), undefined);
|
||||
assert.equal(project(undefined, new Cesium.Cartesian2()), undefined);
|
||||
assert.deepEqual(asked, [eye, undefined]);
|
||||
} finally {
|
||||
Cesium.SceneTransforms.worldToWindowCoordinates = original;
|
||||
}
|
||||
});
|
||||
@@ -31,6 +31,12 @@ import {
|
||||
} from '../../data/fleetModels.js';
|
||||
import { createFleetModelLayer } from '../../data/fleetModelInstances.js';
|
||||
import { isAidToNavigation } from '../../data/vesselTypes.js';
|
||||
import { createWindowProjector } from '../../data/windowProjection.js';
|
||||
|
||||
// Label grid cells are keyed by column * LABEL_CELL_ROW + row, unique while
|
||||
// rows stay within half of it either side; a cell further off screen is keyed
|
||||
// by its text.
|
||||
const LABEL_CELL_ROW = 1 << 20;
|
||||
|
||||
/** A moving vessel's next frame is asked for once it would move this far on
|
||||
* screen (pixels), so it glides when it moves visibly and the scene idles
|
||||
@@ -70,6 +76,7 @@ export function createRendering({
|
||||
}) {
|
||||
const { state } = vesselState;
|
||||
let visualRecords = new WeakMap();
|
||||
const labelScreenScratch = new Cesium.Cartesian2();
|
||||
|
||||
// The fallback preserves helper calls with externally supplied render records.
|
||||
function getVisual(record) {
|
||||
@@ -1038,25 +1045,30 @@ export function createRendering({
|
||||
return;
|
||||
}
|
||||
|
||||
// Every vessel in view is projected, so the camera is read once and each
|
||||
// is a few multiplications (see windowProjection.js) into one screen
|
||||
// point; a cell gets a candidate only for the vessel that leads it.
|
||||
const project = createWindowProjector(scene);
|
||||
const cells = new Map();
|
||||
for (const record of records) {
|
||||
const visual = getVisual(record);
|
||||
if (record === selected) continue;
|
||||
const screen = Cesium.SceneTransforms.worldToWindowCoordinates(
|
||||
scene,
|
||||
visual.position,
|
||||
);
|
||||
const screen = project(getVisual(record).position, labelScreenScratch);
|
||||
if (!screen) continue;
|
||||
const key = `${Math.floor(screen.x / LABEL_GRID_PX)}:${Math.floor(screen.y / LABEL_GRID_PX)}`;
|
||||
const candidate = {
|
||||
record,
|
||||
score: labelPriority(record, selected),
|
||||
x: screen.x,
|
||||
y: screen.y,
|
||||
};
|
||||
const column = Math.floor(screen.x / LABEL_GRID_PX);
|
||||
const row = Math.floor(screen.y / LABEL_GRID_PX);
|
||||
const key =
|
||||
Math.abs(row) < LABEL_CELL_ROW / 2 && Math.abs(column) < LABEL_CELL_ROW
|
||||
? column * LABEL_CELL_ROW + row
|
||||
: `${column}:${row}`;
|
||||
const score = labelPriority(record, selected);
|
||||
const existing = cells.get(key);
|
||||
if (!existing || candidate.score > existing.score) {
|
||||
cells.set(key, candidate);
|
||||
if (!existing) {
|
||||
cells.set(key, { record, score, x: screen.x, y: screen.y });
|
||||
} else if (score > existing.score) {
|
||||
existing.record = record;
|
||||
existing.score = score;
|
||||
existing.x = screen.x;
|
||||
existing.y = screen.y;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1148,7 +1160,30 @@ export function createRendering({
|
||||
return score;
|
||||
}
|
||||
|
||||
// Whether a vessel's name is worth a card, kept per vessel until what it
|
||||
// is judged from changes: the label pass asks for every vessel in view.
|
||||
const usefulNames = new WeakMap();
|
||||
|
||||
function hasUsefulName(record) {
|
||||
const known = usefulNames.get(record);
|
||||
if (
|
||||
known &&
|
||||
known.name === record.name &&
|
||||
known.mmsi === record.mmsi &&
|
||||
known.id === record.id
|
||||
)
|
||||
return known.useful;
|
||||
const useful = nameIsUseful(record);
|
||||
usefulNames.set(record, {
|
||||
name: record.name,
|
||||
mmsi: record.mmsi,
|
||||
id: record.id,
|
||||
useful,
|
||||
});
|
||||
return useful;
|
||||
}
|
||||
|
||||
function nameIsUseful(record) {
|
||||
const text = String(record.name || '').trim();
|
||||
return Boolean(
|
||||
text &&
|
||||
|
||||
Reference in new issue
Block a user