markerProjection.test.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import assert from 'node:assert/strict'
  2. import { test } from 'node:test'
  3. import {
  4. Cartesian2,
  5. Cartesian3,
  6. Cartesian4,
  7. Matrix3,
  8. Matrix4,
  9. PerspectiveFrustum,
  10. SceneMode,
  11. SceneTransforms,
  12. } from 'cesium'
  13. import { projectWorldToWindow } from '../src/modules/cockpit/services/markerProjection.js'
  14. test('matches Cesium window projection for points visible through a 3D perspective camera', () => {
  15. const width = 1234
  16. const height = 511
  17. const frustum = new PerspectiveFrustum({
  18. fov: Math.PI / 3,
  19. aspectRatio: width / height,
  20. near: 1,
  21. far: 100000,
  22. })
  23. const view = Matrix4.fromRotationTranslation(
  24. Matrix3.fromRotationZ(Math.PI / 7, new Matrix3()),
  25. new Cartesian3(-2, 1, -10),
  26. new Matrix4(),
  27. )
  28. const scene = {
  29. frameState: { mode: SceneMode.SCENE3D },
  30. canvas: { clientWidth: width, clientHeight: height },
  31. camera: { viewMatrix: view, frustum },
  32. }
  33. const viewProjection = Matrix4.multiply(frustum.projectionMatrix, view, new Matrix4())
  34. const result = new Cartesian2()
  35. const clipPosition = new Cartesian4()
  36. for (const point of [
  37. new Cartesian3(0, 0, 0),
  38. new Cartesian3(4, 2, -20),
  39. new Cartesian3(-12, 8, -50),
  40. ]) {
  41. const expected = SceneTransforms.worldToWindowCoordinates(scene, point, new Cartesian2())
  42. const actual = projectWorldToWindow(point, viewProjection, width, height, result, clipPosition)
  43. assert.ok(actual)
  44. assert.ok(Math.abs(actual.x - expected.x) < 1e-8)
  45. assert.ok(Math.abs(actual.y - expected.y) < 1e-8)
  46. assert.equal(actual, result)
  47. }
  48. })
  49. test('rejects points behind the camera like Cesium perspective projection', () => {
  50. const width = 800
  51. const height = 600
  52. const frustum = new PerspectiveFrustum({
  53. fov: Math.PI / 3,
  54. aspectRatio: width / height,
  55. near: 1,
  56. far: 1000,
  57. })
  58. const scene = {
  59. frameState: { mode: SceneMode.SCENE3D },
  60. canvas: { clientWidth: width, clientHeight: height },
  61. camera: { viewMatrix: Matrix4.IDENTITY, frustum },
  62. }
  63. const viewProjection = Matrix4.clone(frustum.projectionMatrix, new Matrix4())
  64. const pointBehind = new Cartesian3(0, 0, 1)
  65. assert.equal(SceneTransforms.worldToWindowCoordinates(scene, pointBehind, new Cartesian2()), undefined)
  66. assert.equal(
  67. projectWorldToWindow(pointBehind, viewProjection, width, height, new Cartesian2(), new Cartesian4()),
  68. null,
  69. )
  70. })