import assert from 'node:assert/strict' import { test } from 'node:test' import { Cartesian2, Cartesian3, Cartesian4, Matrix3, Matrix4, PerspectiveFrustum, SceneMode, SceneTransforms, } from 'cesium' import { projectWorldToWindow } from '../src/modules/cockpit/services/markerProjection.js' test('matches Cesium window projection for points visible through a 3D perspective camera', () => { const width = 1234 const height = 511 const frustum = new PerspectiveFrustum({ fov: Math.PI / 3, aspectRatio: width / height, near: 1, far: 100000, }) const view = Matrix4.fromRotationTranslation( Matrix3.fromRotationZ(Math.PI / 7, new Matrix3()), new Cartesian3(-2, 1, -10), new Matrix4(), ) const scene = { frameState: { mode: SceneMode.SCENE3D }, canvas: { clientWidth: width, clientHeight: height }, camera: { viewMatrix: view, frustum }, } const viewProjection = Matrix4.multiply(frustum.projectionMatrix, view, new Matrix4()) const result = new Cartesian2() const clipPosition = new Cartesian4() for (const point of [ new Cartesian3(0, 0, 0), new Cartesian3(4, 2, -20), new Cartesian3(-12, 8, -50), ]) { const expected = SceneTransforms.worldToWindowCoordinates(scene, point, new Cartesian2()) const actual = projectWorldToWindow(point, viewProjection, width, height, result, clipPosition) assert.ok(actual) assert.ok(Math.abs(actual.x - expected.x) < 1e-8) assert.ok(Math.abs(actual.y - expected.y) < 1e-8) assert.equal(actual, result) } }) test('rejects points behind the camera like Cesium perspective projection', () => { const width = 800 const height = 600 const frustum = new PerspectiveFrustum({ fov: Math.PI / 3, aspectRatio: width / height, near: 1, far: 1000, }) const scene = { frameState: { mode: SceneMode.SCENE3D }, canvas: { clientWidth: width, clientHeight: height }, camera: { viewMatrix: Matrix4.IDENTITY, frustum }, } const viewProjection = Matrix4.clone(frustum.projectionMatrix, new Matrix4()) const pointBehind = new Cartesian3(0, 0, 1) assert.equal(SceneTransforms.worldToWindowCoordinates(scene, pointBehind, new Cartesian2()), undefined) assert.equal( projectWorldToWindow(pointBehind, viewProjection, width, height, new Cartesian2(), new Cartesian4()), null, ) })