diff --git a/src/geo/ellipsoid.js b/src/geo/ellipsoid.js index 3871396b9..5b745097a 100644 --- a/src/geo/ellipsoid.js +++ b/src/geo/ellipsoid.js @@ -110,6 +110,43 @@ export function fromRadians(lon, lat, height = 0) { export const fromDegrees = (lon, lat, height = 0) => fromRadians(toRadians(lon), toRadians(lat), height); +/** + * `fromDegrees` written into `out` (any object with x, y, z) without + * allocating, for per-frame paths. Same steps in the same order, so the + * same bits. + * @template {{x: number, y: number, z: number}} T + * @param {number} lon + * @param {number} lat + * @param {number} height + * @param {T} out + * @returns {T} + */ +export function fromDegreesInto(lon, lat, height, out) { + const lonR = toRadians(lon); + const latR = toRadians(lat); + const cosLat = Math.cos(latR); + let nx = cosLat * Math.cos(lonR); + let ny = cosLat * Math.sin(lonR); + let nz = Math.sin(latR); + const m = Math.sqrt(nx * nx + ny * ny + nz * nz); + nx /= m; + ny /= m; + nz /= m; + const r = WGS84.radiiSquared; + let kx = r.x * nx; + let ky = r.y * ny; + let kz = r.z * nz; + const gamma = Math.sqrt(nx * kx + ny * ky + nz * kz); + kx /= gamma; + ky /= gamma; + kz /= gamma; + const h = height ?? 0; + out.x = kx + nx * h; + out.y = ky + ny * h; + out.z = kz + nz * h; + return out; +} + /** * A position as ECEF (a new object): `{lon, lat, height?}` (degrees, metres * above the ellipsoid) or `{x, y, z}`, the render contract's `Position`. diff --git a/src/geo/geo.test.mjs b/src/geo/geo.test.mjs index a502e0f34..79dee2a42 100644 --- a/src/geo/geo.test.mjs +++ b/src/geo/geo.test.mjs @@ -8,6 +8,7 @@ import { eastNorthUp, ellipsoidOf, fromDegrees, + fromDegreesInto, geodeticSurfaceNormal, lerpInto, offsetEllipsoid, @@ -79,6 +80,34 @@ test('geodetic to ECEF matches the default renderer', () => { } }); +test('geodetic to ECEF in place gives the same bits, into the object given', () => { + const out = { x: 0, y: 0, z: 0 }; + const cartesian = new Cesium.Cartesian3(); + for (const p of points()) { + const at = `${p.lon},${p.lat},${p.height}`; + assert.equal(fromDegreesInto(p.lon, p.lat, p.height, out), out, at); + assert.deepEqual( + { ...out }, + { ...fromDegrees(p.lon, p.lat, p.height) }, + at, + ); + assert.equal( + fromDegreesInto(p.lon, p.lat, p.height, cartesian), + cartesian, + at, + ); + assert.ok( + Cesium.Cartesian3.equals( + cartesian, + Cesium.Cartesian3.fromDegrees(p.lon, p.lat, p.height), + ), + at, + ); + } + fromDegreesInto(10, 20, undefined, out); + assert.deepEqual({ ...out }, { ...fromDegrees(10, 20) }); +}); + test('ECEF to geodetic matches the default renderer', () => { for (const p of points()) { const ecef = Cesium.Cartesian3.fromDegrees(p.lon, p.lat, p.height); diff --git a/src/layers/satellites/catalog.js b/src/layers/satellites/catalog.js index 70cf03812..26130bd0f 100644 --- a/src/layers/satellites/catalog.js +++ b/src/layers/satellites/catalog.js @@ -1,5 +1,5 @@ import { twoline2satrec } from 'satellite.js'; -import { fromDegrees } from '../../geo/ellipsoid.js'; +import { fromDegrees, fromDegreesInto } from '../../geo/ellipsoid.js'; import { noradIdFromSatnum, tleLineLaunchYear } from '../../sources/tle.js'; import { DENSE_REFRESH_FRAMES, @@ -9,6 +9,8 @@ import { } from './policy.js'; export function createCatalog({ state: layerState, services, parts, source }) { + // _movePoint copies the position, so one scratch point serves every move. + const scratch = { x: 0, y: 0, z: 0 }; function _abortActiveUpdates() { for (const controller of layerState._activeUpdateControllers) controller.abort(); @@ -44,7 +46,7 @@ export function createCatalog({ state: layerState, services, parts, source }) { if (pos) { parts.rendering._movePoint( noradId, - fromDegrees(pos.longitude, pos.latitude, pos.altitude), + fromDegreesInto(pos.longitude, pos.latitude, pos.altitude, scratch), ); } } diff --git a/src/layers/satellites/offCesium.test.mjs b/src/layers/satellites/offCesium.test.mjs index a7c3269a7..eb99d38fc 100644 --- a/src/layers/satellites/offCesium.test.mjs +++ b/src/layers/satellites/offCesium.test.mjs @@ -93,11 +93,18 @@ test('only the rendering part drives Cesium', () => { /\.scene\b/, /\bscene\./, /\.primitive\b/, - // The dots and rings are the rendering part's: ids only elsewhere. - /_pointCollection\./, - /_points\.(get|values|entries|forEach)\b/, - /of (layerState|state)\._points\)/, - /_orbitPaths\.(get|values|entries|forEach)\b/, + // The dots, rings and tracked entity are the rendering part's. Elsewhere + // the handles are only stored, cleared, tested or passed on whole, and + // the maps answer by id: no member of a handle is read, with or without + // optional chaining, and no handle is taken into a local name. + /\b_(pointCollection|trackedEntity)\s*(\?\.|\.|\[)/, + /\b_(points|orbitPaths)\s*(\?\.|\.|\[)\s*(?!(has|keys|size|clear|set|delete)\b)/, + /of\s+[\w.?]*\b_(points|orbitPaths)\s*\)/, + /(\.\.\.|Array\.from\()\s*[\w.?]*\b_(points|orbitPaths)\b(?!\s*\??\.)/, + /(^|[;{}(\s])\w+\s*=\s*\(*\s*[\w.?]*\b_(pointCollection|trackedEntity|points|orbitPaths)\s*(;|,|\)|\?\?|\|\||&&)/, + /\}\s*=\s*\(*\s*[\w.?]*\b_(pointCollection|trackedEntity|points|orbitPaths)\b/, + /[\w)\]]\s*(\?\.)?\[\s*['"`]_(pointCollection|trackedEntity|points|orbitPaths)\b/, + /\{[^}]*\b_(pointCollection|trackedEntity|points|orbitPaths)\b[^}]*\}\s*=[^=]/, ]; for (const name of offList) { const source = readFileSync( @@ -107,6 +114,51 @@ test('only the rendering part drives Cesium', () => { for (const pattern of forbidden) assert.doesNotMatch(source, pattern, `${name}.js: ${pattern}`); } + // The patterns themselves still catch what they are for. + const caught = [ + 'layerState._trackedEntity.position.setValue(p)', + 'layerState._pointCollection?.removeAll()', + 'state._trackedEntity?.["position"]', + 'layerState._points.get(id).show = false', + 'layerState._orbitPaths?.values()', + 'for (const p of layerState._points) {}', + 'const pc = layerState._pointCollection;', + 'const { _trackedEntity: e } = layerState;', + 'const e = layerState?._trackedEntity;', + 'for (const p of layerState?._points) {}', + '[...layerState._points]', + 'Array.from(layerState._orbitPaths)', + 'e = layerState._trackedEntity;', + 'let e; e = layerState._trackedEntity;', + 'const { position } = layerState._trackedEntity;', + 'const e = layerState._trackedEntity ?? null;', + 'const e = (layerState._trackedEntity);', + 'const { get } = layerState._points;', + 'layerState["_trackedEntity"].position', + ]; + for (const line of caught) + assert.ok( + forbidden.some((pattern) => pattern.test(line)), + `not caught: ${line}`, + ); + const allowed = [ + 'layerState._points.has(id)', + 'layerState._points.size', + 'layerState._trackedEntity = null;', + 'parts.rendering._removeTrackedEntity(layerState._trackedEntity);', + 'layerState._trackedEntityChangedRemove();', + 'const had = layerState._points.has(id);', + 'const t = layerState._trackedEntity\n ? a\n : b;', + 'const ids = ["_points", "_orbitPaths"];', + 'const n = layerState._orbitPaths.size;', + '[...layerState._points.keys()]', + 'layerState._trackedEntity = parts.rendering._addTrackedEntity({', + ]; + for (const line of allowed) + assert.ok( + !forbidden.some((pattern) => pattern.test(line)), + `wrongly caught: ${line}`, + ); }); test('the rendering part draws the dots and rings in Cesium colours', () => { diff --git a/src/layers/satellites/tracking.js b/src/layers/satellites/tracking.js index e9beb09e9..74e6f980d 100644 --- a/src/layers/satellites/tracking.js +++ b/src/layers/satellites/tracking.js @@ -1,8 +1,8 @@ import { cameraOf } from '../../render/rendererOf.js'; import { entityFollowTarget, stopFollowing } from '../../data/followed.js'; import { satelliteClassLabel } from '../../data/satelliteClass.js'; -import { fromDegrees } from '../../geo/ellipsoid.js'; -import { clone, copyInto, scale } from '../../geo/vector.js'; +import { fromDegreesInto } from '../../geo/ellipsoid.js'; +import { clone, scale } from '../../geo/vector.js'; import { t } from '../../i18n/index.js'; import { formatQuantity, unitLabel } from '../../ui/unitLabels.js'; import { @@ -155,8 +155,10 @@ export function createTracking({ state: layerState, services, parts, source }) { ); if (!pos) return layerState._trackedFrameGeo; // propagation hiccup — keep last good sample layerState._trackedFrameGeo = pos; - copyInto( - fromDegrees(pos.longitude, pos.latitude, pos.altitude), + fromDegreesInto( + pos.longitude, + pos.latitude, + pos.altitude, layerState._trackedFrameCartesian, ); layerState._trackedFrameNumber = frameNumber;