Files
gods-eye-view/src/geo/cartographic.test.mjs
T
German Khokhlov 7abf74518f Cut duplicated and dead code across the engines, the layers and the QA scripts
- src/geo drops the ported debug argument checks and the tests that
  bitExact already covers; package boundaries accept a directory entry,
  so src/geo/ is listed once per group.
- Cesium no longer carries movers and lines nobody draws with: they are
  optional engine capabilities. The fake engine and the Cesium camera use
  the shared cameraMath; unused contract tables, clock.ticked, the
  'finest' surface detail and Navara click statistics are gone.
- ALPR keeps one overlay core and one presentation model for both
  engines, bikeshare one station bookkeeping, the Navara ground effects
  one definition helper; image atlases, view centres, mover banks and
  colours are shared.
- The legacy switch route, the clock offset and rate controls and other
  unused exports are removed.
- The acceptance runs the Navara draw, geo import and scope checks;
  scenarios use the harness switch and page helpers; the main chunk
  check keeps the size budget only and runs once in CI.
2026-11-05 13:32:39 +04:00

114 lines
3.7 KiB
JavaScript

// src/geo/cartographic.test.mjs
import { test } from 'node:test';
import assert from 'node:assert/strict';
import * as Cesium from 'cesium';
import * as cartographic from './cartographic.js';
test('fromDegrees and fromRadians equal Cesium', () => {
for (const [lon, lat, h] of [
[0, 0, 0],
[-73.9857, 40.7484, 381],
[139.76, 35.68, -20],
[180, -90, 1e4],
]) {
const ours = cartographic.fromDegrees(lon, lat, h);
const theirs = Cesium.Cartographic.fromDegrees(lon, lat, h);
assert.deepEqual(
{ ...ours },
{
longitude: theirs.longitude,
latitude: theirs.latitude,
height: theirs.height,
},
);
}
});
test('result objects are reused', () => {
const out = cartographic.create();
assert.equal(cartographic.fromDegrees(1, 2, 3, out), out);
assert.equal(cartographic.clone(out).longitude, out.longitude);
});
const POLAR_RADIUS = 6356752.3142451793;
const sameFields = (ours, theirs, keys, label) => {
for (const k of keys) {
assert.ok(
Object.is(ours[k], theirs[k]),
`${label}.${k}: ${ours[k]} vs ${theirs[k]}`,
);
}
};
test('fromCartesian is undefined at the exact centre, as in Cesium', () => {
for (const [x, y, z] of [
[0, 0, 0],
[-0, -0, -0],
]) {
assert.equal(
Cesium.Cartographic.fromCartesian(new Cesium.Cartesian3(x, y, z)),
undefined,
);
assert.equal(cartographic.fromCartesian({ x, y, z }), undefined);
}
});
test('fromCartesian and toCartesian equal Cesium bit for bit at the poles and the antimeridian', () => {
for (const [x, y, z, label] of [
[0, 0, POLAR_RADIUS, 'north pole'],
[0, 0, -POLAR_RADIUS, 'south pole'],
[-6378137, 0, 0, 'antimeridian, y = +0'],
[-6378137, -0, 0, 'antimeridian, y = -0'],
[-6378137, -0, -0, 'antimeridian, y = z = -0'],
]) {
const ours = cartographic.fromCartesian({ x, y, z });
const theirs = Cesium.Cartographic.fromCartesian(
new Cesium.Cartesian3(x, y, z),
);
sameFields(ours, theirs, ['longitude', 'latitude', 'height'], label);
sameFields(
cartographic.toCartesian(ours),
Cesium.Cartographic.toCartesian(theirs),
['x', 'y', 'z'],
`${label} back`,
);
}
// atan2 keeps the sign of a zero y: +π on one side of the antimeridian, -π on the other.
assert.equal(
cartographic.fromCartesian({ x: -6378137, y: 0, z: 0 }).longitude,
Math.PI,
);
assert.equal(
cartographic.fromCartesian({ x: -6378137, y: -0, z: 0 }).longitude,
-Math.PI,
);
});
// layers/streetLevel/view.js passes a Cesium Ellipsoid (scene.globe.ellipsoid),
// which has no public `centerToleranceSquared`; geo then reads Cesium's private
// `_centerToleranceSquared`. This pins that coupling to a Cesium private field:
// if a Cesium upgrade renames it, the centre case below stops matching.
test('fromCartesian with a Cesium Ellipsoid equals Cesium, including the centre', () => {
const ellipsoid = Cesium.Ellipsoid.WGS84;
assert.equal(ellipsoid.centerToleranceSquared, undefined);
assert.equal(typeof ellipsoid._centerToleranceSquared, 'number');
for (const [x, y, z, label] of [
[0, 0, 0, 'centre'],
[0.1, 0.1, 0.1, 'inside the centre tolerance'],
[6378137, 0, 0, 'equator'],
[1334000, -4654000, 4138000, 'mid-latitude'],
[0, 0, POLAR_RADIUS + 500, 'above the north pole'],
]) {
const ours = cartographic.fromCartesian({ x, y, z }, ellipsoid);
const theirs = Cesium.Cartographic.fromCartesian(
new Cesium.Cartesian3(x, y, z),
ellipsoid,
);
if (theirs === undefined) {
assert.equal(ours, undefined, label);
continue;
}
sameFields(ours, theirs, ['longitude', 'latitude', 'height'], label);
}
});