Revert "Merge pull request #1757 from bilawalsidhu/perf/vessel-labels-merge"

This reverts commit 15d1855dd3, reversing
changes made to 663b56f620.
This commit is contained in:
Sameh Khamis committed 2026-11-15 18:18:42 -08:00
1 parent 50e3974217
commit 33c98118ba
8 files changed
+27 -374

No files matched your search

-65
View File
@@ -1,65 +0,0 @@
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;
};
}
-122
View File
@@ -1,122 +0,0 @@
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;
}
});