diff --git a/scripts/package-boundaries.json b/scripts/package-boundaries.json index f2c313561..355eaa106 100644 --- a/scripts/package-boundaries.json +++ b/scripts/package-boundaries.json @@ -20,6 +20,7 @@ "src/render/cesium/query.js", "src/render/cesium/draw.js", "src/render/cesium/gpuIcons.js", + "src/render/motion/iconMotion.js", "src/render/cesium/cameraControls.js", "src/render/cesium/credits.js", "src/render/creditMarkup.js", @@ -896,6 +897,7 @@ "src/queries/heights.js", "src/render/cesium/draw.js", "src/render/cesium/gpuIcons.js", + "src/render/motion/iconMotion.js", "src/render/cesium/cameraControls.js", "src/render/cesium/credits.js", "src/render/creditMarkup.js", @@ -1040,6 +1042,7 @@ "src/queries/heights.js", "src/render/cesium/draw.js", "src/render/cesium/gpuIcons.js", + "src/render/motion/iconMotion.js", "src/data/followed.js", "src/render/cesium/camera.js", "src/render/cesium/cameraControls.js", @@ -1324,6 +1327,7 @@ "src/queries/heights.js", "src/render/cesium/draw.js", "src/render/cesium/gpuIcons.js", + "src/render/motion/iconMotion.js", "src/render/cesium/cameraControls.js", "src/render/cesium/credits.js", "src/render/creditMarkup.js", @@ -1475,6 +1479,7 @@ "src/queries/heights.js", "src/render/cesium/draw.js", "src/render/cesium/gpuIcons.js", + "src/render/motion/iconMotion.js", "src/render/cesium/cameraControls.js", "src/render/cesium/credits.js", "src/render/creditMarkup.js", @@ -1576,6 +1581,7 @@ "src/queries/heights.js", "src/render/cesium/draw.js", "src/render/cesium/gpuIcons.js", + "src/render/motion/iconMotion.js", "src/render/cesium/cameraControls.js", "src/render/cesium/credits.js", "src/render/creditMarkup.js", @@ -1659,6 +1665,7 @@ "src/data/entityRecords.js", "src/data/aircraftModelLighting.js", "src/data/motionModel.js", + "src/geo/vector.js", "src/data/renderAltitude.js", "src/data/routePlausible.js", "src/geoDistance.js", @@ -1769,6 +1776,7 @@ "src/render/cesium/effects.js", "src/render/cesium/draw.js", "src/render/cesium/gpuIcons.js", + "src/render/motion/iconMotion.js", "src/render/cesium/camera.js", "src/render/camera/events.js", "src/geo/index.js", @@ -1853,6 +1861,7 @@ "src/render/creditMarkup.js", "src/render/cesium/draw.js", "src/render/cesium/gpuIcons.js", + "src/render/motion/iconMotion.js", "src/layers/firms/index.js", "src/layers/firms/ingestion.js", "src/layers/firms/lifecycle.js", @@ -1942,6 +1951,7 @@ "src/queries/heights.js", "src/render/cesium/draw.js", "src/render/cesium/gpuIcons.js", + "src/render/motion/iconMotion.js", "src/render/cesium/cameraControls.js", "src/render/cesium/credits.js", "src/render/creditMarkup.js", @@ -2006,6 +2016,7 @@ "src/queries/heights.js", "src/render/cesium/draw.js", "src/render/cesium/gpuIcons.js", + "src/render/motion/iconMotion.js", "src/render/cesium/cameraControls.js", "src/render/cesium/credits.js", "src/render/creditMarkup.js", @@ -2088,6 +2099,7 @@ "src/queries/heights.js", "src/render/cesium/draw.js", "src/render/cesium/gpuIcons.js", + "src/render/motion/iconMotion.js", "src/render/cesium/cameraControls.js", "src/render/cesium/credits.js", "src/render/creditMarkup.js", @@ -2266,6 +2278,7 @@ "src/data/aircraftRecession.js", "src/data/iconSegment.js", "src/data/motionModel.js", + "src/render/motion/iconMotion.js", "src/data/iconAtlas.js", "src/render/cesium/camera.js", "src/render/camera/events.js", @@ -2404,6 +2417,7 @@ "src/queries/heights.js", "src/render/cesium/draw.js", "src/render/cesium/gpuIcons.js", + "src/render/motion/iconMotion.js", "src/render/cesium/cameraControls.js", "src/render/cesium/credits.js", "src/render/creditMarkup.js", @@ -2487,6 +2501,7 @@ "src/render/cesium/query.js", "src/render/cesium/draw.js", "src/render/cesium/gpuIcons.js", + "src/render/motion/iconMotion.js", "src/geo/index.js", "src/layers/streetLevel/credits.js", "src/render/creditMarkup.js", @@ -2680,6 +2695,7 @@ "src/queries/heights.js", "src/render/cesium/draw.js", "src/render/cesium/gpuIcons.js", + "src/render/motion/iconMotion.js", "src/render/cesium/cameraControls.js", "src/render/cesium/credits.js", "src/render/creditMarkup.js", @@ -2851,6 +2867,7 @@ "src/render/cesium/tileRequests.js", "src/render/cesium/draw.js", "src/render/cesium/gpuIcons.js", + "src/render/motion/iconMotion.js", "src/render/cesium/cameraInput.js", "src/render/cesium/camera.js", "src/render/cesium/cameraControls.js", @@ -3068,6 +3085,7 @@ "src/render/rendererOf.js", "src/render/cesium/ports.js", "src/render/cesium/gpuIcons.js", + "src/render/motion/iconMotion.js", "src/render/cesium/query.js", "src/render/cesium/globe.js", "src/render/cesium/drawHit.js", @@ -4066,6 +4084,7 @@ "src/render/cesium/query.js", "src/render/cesium/draw.js", "src/render/cesium/gpuIcons.js", + "src/render/motion/iconMotion.js", "src/render/cesium/imagery.js", "src/maps/google3d.js", "src/render/cesium/cameraControls.js", @@ -4739,6 +4758,7 @@ "src/render/cesium/drawHit.js", "src/render/cesium/draw.js", "src/render/cesium/gpuIcons.js", + "src/render/motion/iconMotion.js", "src/render/cesium/cameraControls.js", "src/render/cesium/cameraInput.js", "src/render/cesium/credits.js", @@ -4862,6 +4882,7 @@ "src/render/cesium/drawHit.js", "src/render/cesium/draw.js", "src/render/cesium/gpuIcons.js", + "src/render/motion/iconMotion.js", "src/render/cesium/cameraControls.js", "src/render/cesium/cameraInput.js", "src/render/cesium/credits.js", @@ -5405,6 +5426,7 @@ "src/render/cesium/query.js", "src/render/cesium/draw.js", "src/render/cesium/gpuIcons.js", + "src/render/motion/iconMotion.js", "src/render/cesium/cameraControls.js", "src/render/cesium/credits.js", "src/render/creditMarkup.js", @@ -5544,6 +5566,7 @@ "src/render/creditMarkup.js", "src/render/cesium/draw.js", "src/render/cesium/gpuIcons.js", + "src/render/motion/iconMotion.js", "src/data/cameraSensitivity.js", "src/data/contactPlayback.js", "src/data/contactTrailRenderer.js", @@ -6074,6 +6097,7 @@ "modules": [ "src/render/contract/index.js", "src/render/stub/index.js", + "src/render/motion/iconMotion.js", "src/geo/horizon.js", "src/render/conformance/index.js", "src/render/conformance/scenes.js", @@ -6089,7 +6113,6 @@ "src/geo/frames.js", "src/geo/celestial.js", "src/geo/quaternion.js", - "src/render/stub/motion.js", "src/render/stub/camera.js", "src/geo/geodesic.js", "src/render/camera/events.js", diff --git a/src/data/iconSegment.js b/src/data/iconSegment.js index a5a4e58d3..73fdddf0e 100644 --- a/src/data/iconSegment.js +++ b/src/data/iconSegment.js @@ -153,12 +153,22 @@ export function writeArcSegment(out, row, segment) { * @param {number} fromCourseDeg * @param {number} t0 * @param {number} rateDps + * @param {number} [offsetDeg] The glide's start offset from the segment's + * course, when the caller has it (render/motion/iconMotion's + * `glideOffset`): its sign then picks the way an exact half turn goes. */ -export function writeCourseGlide(out, row, fromCourseDeg, t0, rateDps) { +export function writeCourseGlide( + out, + row, + fromCourseDeg, + t0, + rateDps, + offsetDeg, +) { const o = row * SEGMENT_STRIDE; out[o + SLEW_OFFSET] = 0; const target = targetCourse(out, o, t0); - out[o + SLEW_OFFSET] = norm180(fromCourseDeg - target); + out[o + SLEW_OFFSET] = offsetDeg ?? norm180(fromCourseDeg - target); out[o + SLEW_T0] = t0; out[o + SLEW_RATE] = rateDps > 0 ? rateDps : -1; } diff --git a/src/data/infrastructure.test.mjs b/src/data/infrastructure.test.mjs index cf6bdd86f..506606c8b 100644 --- a/src/data/infrastructure.test.mjs +++ b/src/data/infrastructure.test.mjs @@ -205,6 +205,7 @@ test('consumer build includes only infrastructure code and resolves assets under 'heights.js', 'horizon.js', 'iconAtlas.js', + 'iconMotion.js', 'iconSegment.js', 'iconSet.js', 'iconSetPrimitive.js', diff --git a/src/data/motionModel.js b/src/data/motionModel.js index eb6020850..00520658c 100644 --- a/src/data/motionModel.js +++ b/src/data/motionModel.js @@ -7,15 +7,12 @@ * skylight (https://github.com/cpaczek/skylight, MIT) shared/src/aim.ts. */ import * as Cesium from 'cesium'; +import { norm180, norm360 } from '../geo/vector.js'; const DEG = Math.PI / 180; const _mmCarto = new Cesium.Cartographic(); const _mmCandidateCarto = new Cesium.Cartographic(); -export function norm360(deg) { - return ((deg % 360) + 360) % 360; -} - /** * Lift a repeated grounded fix to a newly resolved render floor without * manufacturing a new source timestamp or changing its reported lat/lon. @@ -49,11 +46,8 @@ export function liftRepeatedGroundFix(newest, candidatePosition, onGround) { return true; } -/** Normalize to (−180, 180]. */ -export function norm180(deg) { - const n = norm360(deg); - return n > 180 ? n - 360 : n; -} +/** Degrees in [0, 360) and (−180, 180]. */ +export { norm180, norm360 }; /** * Course over ground (deg from north, clockwise) of the chord from → to, diff --git a/src/geo/ellipsoid.js b/src/geo/ellipsoid.js index 2bc8d408e..3871396b9 100644 --- a/src/geo/ellipsoid.js +++ b/src/geo/ellipsoid.js @@ -110,6 +110,16 @@ export function fromRadians(lon, lat, height = 0) { export const fromDegrees = (lon, lat, height = 0) => fromRadians(toRadians(lon), toRadians(lat), height); +/** + * A position as ECEF (a new object): `{lon, lat, height?}` (degrees, metres + * above the ellipsoid) or `{x, y, z}`, the render contract's `Position`. + */ +export function positionToEcef(position) { + if ('lon' in position && 'lat' in position) + return fromDegrees(position.lon, position.lat, position.height ?? 0); + return vec(position.x, position.y, position.z); +} + /** The point on the ellipsoid along the geodetic normal, or null at the centre. */ export function scaleToGeodeticSurface(p, ellipsoid) { const e = ellipsoidArg(ellipsoid); diff --git a/src/geo/vector.js b/src/geo/vector.js index 8bf11593a..f6bbf3f9b 100644 --- a/src/geo/vector.js +++ b/src/geo/vector.js @@ -56,6 +56,13 @@ export const DEGREES_PER_RADIAN = 180 / Math.PI; export const toRadians = (degrees) => degrees * RADIANS_PER_DEGREE; export const toDegrees = (radians) => radians * DEGREES_PER_RADIAN; export const clamp = (v, lo, hi) => Math.min(hi, Math.max(lo, v)); +/** Degrees in [0, 360). */ +export const norm360 = (deg) => ((deg % 360) + 360) % 360; +/** Degrees in (-180, 180]. */ +export function norm180(deg) { + const n = norm360(deg); + return n > 180 ? n - 360 : n; +} export const acosClamped = (v) => Math.acos(clamp(v, -1, 1)); export const equalsEpsilon = (a, b, eps) => Math.abs(a - b) <= eps; diff --git a/src/render/cesium/gpuIcons.js b/src/render/cesium/gpuIcons.js index 28ea541ab..3cb421e3d 100644 --- a/src/render/cesium/gpuIcons.js +++ b/src/render/cesium/gpuIcons.js @@ -43,7 +43,6 @@ import { ICON_ROTATES, } from '../../data/iconSet.js'; import { - segmentCourse, writeArcSegment, writeCourseGlide, writeHoldSegment, @@ -51,7 +50,13 @@ import { } from '../../data/iconSegment.js'; import { IconSetPrimitive } from '../../data/iconSetPrimitive.js'; import { iconAtlas, MAX_ICON_SLOTS } from '../../data/iconAtlas.js'; -import { norm180 } from '../../data/motionModel.js'; +import { + glideOffset, + iconCourse, + LERP_STEP_MS, + motionEndsAt, + norm180, +} from '../motion/iconMotion.js'; import { toCartesian } from './common.js'; /** The capabilities GPU icon sets bring. */ @@ -246,6 +251,10 @@ export function createGpuIcons({ const blends = new Map(); // What each row's segment was last written from, by id. const written = new Map(); + // What each row is drawn along now, by id: its position, course and + // motion as the renderer reads them, and the course glide in effect + // (`iconMotion` evaluates it as the GPU does). + const drawn = new Map(); // The ids of the last columns, while no other write came after them. let columnIds = null; // Written in full on the first write. @@ -270,7 +279,7 @@ export function createGpuIcons({ return item.course ?? 0; } - /** Write a row's segment; when it stops moving, epoch ms (-Infinity: holds). */ + /** Write a row's segment. */ function writeMotion(row, item) { const seg = set.segments; const motion = can('motionSegments') ? item.motion : undefined; @@ -279,7 +288,7 @@ export function createGpuIcons({ case 'lerp': { const t0 = tauOf(motion.t0); // Not after t0: a step 1 ms after it. - const t1 = Math.max(tauOf(motion.t1), t0 + 0.001); + const t1 = Math.max(tauOf(motion.t1), t0 + LERP_STEP_MS / 1000); writeLerpSegment(seg, row, { from: toCartesian(motion.from), to: toCartesian(motion.to), @@ -288,7 +297,7 @@ export function createGpuIcons({ course0Deg: courseOf(item), courseSlopeDeg: motion.courseSlopeDeg ?? 0, }); - return Math.max(motion.t1, motion.t0 + 1); + return; } case 'arc': { const t0 = tauOf(motion.t0); @@ -302,11 +311,10 @@ export function createGpuIcons({ turnRateDps: motion.turnRateDps, climbMps: motion.climbMps ?? 0, }); - return motion.t0 + motion.maxDtS * 1000; + return; } default: writeHoldSegment(seg, row, pos, courseOf(item)); - return -Infinity; } } @@ -319,10 +327,12 @@ export function createGpuIcons({ let row = set.row(item.id); const added = row < 0; if (added) row = set.add(item.id); - const tau = set.seconds(clockTime()); + const now = clockMs(); // The course on screen before this write, to ease from. const shownCourse = - added || set.expires(row) === -Infinity ? null : set.course(row, tau); + added || !drawn.has(item.id) + ? null + : iconCourse(drawn.get(item.id), now); const [width = 0, height = 0] = item.size ?? []; const rasterPx = rasterPxOf(item); const far = slotOf(item.image, rasterPx); @@ -358,22 +368,32 @@ export function createGpuIcons({ const from = motionOf(item); if (!added && sameMotion(written.get(item.id), from)) return; written.set(item.id, from); - let end = writeMotion(row, item); - const glide = can('courseGlide') ? item.courseGlide : undefined; - if (glide) { + writeMotion(row, item); + // The row as the renderer reads it (copies, as `written`'s are): its + // motion, without a glide yet. + const along = { + pos: from.pos, + course: from.course, + motion: can('motionSegments') ? from.motion : undefined, + }; + // A glide written with the offset the app reads, so a half turn goes the + // way `iconMotion` says. + const writeGlide = (g) => writeCourseGlide( set.segments, row, - glide.fromCourseDeg, - tauOf(glide.t0), - glide.rateDps, + g.fromCourseDeg, + tauOf(g.t0), + g.rateDps, + glideOffset(along.motion, along.course, g), ); - // Turning at most half a turn at its rate. - if (glide.rateDps > 0) - end = Math.max(end, glide.t0 + (180 / glide.rateDps) * 1000); + let end = motionEndsAt(along); + let glide = can('courseGlide') ? from.courseGlide : undefined; + if (glide) { + writeGlide(glide); + end = motionEndsAt({ ...along, courseGlide: glide }); } else if (item.courseBlendMs > 0 && shownCourse !== null) { - const target = segmentCourse(set.segments, row, tau); - const now = clockMs(); + const target = iconCourse(along, now); let blend = blends.get(item.id); // The same course as the blend under way heads for: it goes on. if (!blend || blend.target !== target) { @@ -393,16 +413,16 @@ export function createGpuIcons({ else blends.delete(item.id); } if (blend) { - writeCourseGlide( - set.segments, - row, - blend.from, - tauOf(blend.t0), - blend.rate, - ); + glide = { + fromCourseDeg: blend.from, + t0: blend.t0, + rateDps: blend.rate, + }; + writeGlide(glide); end = Math.max(end, blend.until); } } else blends.delete(item.id); + drawn.set(item.id, { ...along, courseGlide: glide }); set.segmentWritten(row); if (end > movingUntil) movingUntil = end; } @@ -420,6 +440,7 @@ export function createGpuIcons({ set.remove(id); blends.delete(id); written.delete(id); + drawn.delete(id); } } for (const item of record.items.values()) writeRow(item); @@ -443,6 +464,7 @@ export function createGpuIcons({ set.remove(id); blends.delete(id); written.delete(id); + drawn.delete(id); } if (record.items.size === 0) movingUntil = -Infinity; }, @@ -467,6 +489,7 @@ export function createGpuIcons({ set.remove(id); blends.delete(id); written.delete(id); + drawn.delete(id); } } const runs = diff --git a/src/render/cesium/gpuIcons.test.mjs b/src/render/cesium/gpuIcons.test.mjs index 62303e262..ef293acd5 100644 --- a/src/render/cesium/gpuIcons.test.mjs +++ b/src/render/cesium/gpuIcons.test.mjs @@ -9,7 +9,11 @@ import { ICON_RECEDES, ICON_ROTATES, } from '../../data/iconSet.js'; -import { motionPosition } from '../stub/motion.js'; +import { + iconCourse, + iconPosition, + motionPosition, +} from '../motion/iconMotion.js'; /** A scene with what the draw port touches, and a frame time to set. */ function fakeScene() { @@ -548,3 +552,129 @@ test('a write that moves a row as it already moves keeps its segment', () => { set.upsert([{ ...item, motion: { kind: 'hold', courseDeg: 41 } }]); assert.ok(!rowsSet.dirty.segments.empty, 'a new course is'); }); + +test('rows draw where the shared evaluator puts them, with the course it gives', () => { + const t0 = Date.now(); + const fix = ecef(Cesium.Cartesian3.fromDegrees(-97.04, 32.9, 10_500)); + const to = { x: fix.x + 4000, y: fix.y - 2500, z: fix.z + 1200 }; + const items = [ + icon('hold', { + motion: { kind: 'hold', courseDeg: 300 }, + courseGlide: { fromCourseDeg: 10, t0, rateDps: 7 }, + }), + icon('lerp', { + course: 15, + motion: { + kind: 'lerp', + from: fix, + to, + t0: t0 - 3000, + t1: t0 + 9000, + courseSlopeDeg: -40, + }, + }), + icon('step', { + motion: { kind: 'lerp', from: fix, to, t0: t0 + 2000, t1: t0 }, + }), + icon('arc', { + motion: { + kind: 'arc', + fix, + t0, + speedMps: 240, + trackDeg: 355, + turnRateDps: -3, + climbMps: 12, + minDtS: -30, + maxDtS: 90, + }, + courseGlide: { fromCourseDeg: 200, t0: t0 + 1000, rateDps: 4 }, + }), + icon('straight', { + motion: { + kind: 'arc', + fix, + t0, + speedMps: 80, + trackDeg: 45, + turnRateDps: 0.003, + minDtS: 0, + maxDtS: 60, + }, + courseGlide: { fromCourseDeg: 90, t0, rateDps: 0 }, + }), + // An exact half turn: either way is a turn; both read the same one. + icon('half', { + motion: { kind: 'hold', courseDeg: 0.7 }, + courseGlide: { fromCourseDeg: 180.7, t0, rateDps: 10 }, + }), + ]; + const { part, draw } = drawOver(); + const set = draw.icons({ id: 'gpu:shared', motionClock: { kind: 'scene' } }); + set.set(items); + const { position, course } = rows(part, 'gpu:shared'); + for (const item of items) + for (const dt of [ + -40_000, -1000, 0, 1, 1500, 2001, 5000, 30_000, 200_000, + ]) { + const t = t0 + dt; + const drawnAt = position(item.id, t); + const shared = iconPosition(item, t); + assert.ok( + distance(drawnAt, shared) < 0.1, + `${item.id} at ${dt} ms: ${distance(drawnAt, shared)} m apart`, + ); + const turn = Math.abs( + ((course(item.id, t) - iconCourse(item, t) + 540) % 360) - 180, + ); + assert.ok(turn < 1e-3, `${item.id} at ${dt} ms: ${turn}° apart`); + } +}); + +test('a course blend starts from the course shown, as the shared evaluator reads it', () => { + const t0 = Date.now(); + const { part, draw } = drawOver(); + const set = draw.icons({ id: 'gpu:blend-parity' }); + const motion = { kind: 'hold', courseDeg: 0 }; + const item = (courseDeg) => + icon('a', { + motion: { ...motion, courseDeg }, + rotationMode: 'course', + courseBlendMs: 4000, + }); + set.set([item(0)]); + set.upsert([item(80)]); + const { course } = rows(part, 'gpu:blend-parity'); + // Partway through, a new course: the next blend starts where this one is. + const before = course('a', Date.now()); + set.upsert([item(150)]); + const at = Date.now(); + const turn = (x, y) => Math.abs(((x - y + 540) % 360) - 180); + assert.ok( + turn(course('a', at), before) < 0.5, + `no jump: ${before} then ${course('a', at)}`, + ); + assert.ok(Math.abs(course('a', at + 10_000) - 150) < 0.01, 'then there'); + assert.ok(at - t0 < 4000, 'the test ran inside the first blend'); +}); + +test('a row keeps the course it showed when its item’s motion is changed in place', () => { + const { part, draw } = drawOver(); + const set = draw.icons({ id: 'gpu:in-place' }); + const motion = { kind: 'hold', courseDeg: 0 }; + const item = icon('a', { + motion, + rotationMode: 'course', + courseBlendMs: 2000, + }); + set.set([item]); + motion.courseDeg = 90; + set.upsert([item]); + const { course } = rows(part, 'gpu:in-place'); + const at = Date.now(); + assert.ok( + course('a', at) < 1, + `blends from 0, not from 90: ${course('a', at)}`, + ); + assert.ok(Math.abs(course('a', at + 2500) - 90) < 0.01, 'there by 2 s'); +}); diff --git a/src/render/conformance/checks/ports.js b/src/render/conformance/checks/ports.js index eec6223fa..dc3d6cc98 100644 --- a/src/render/conformance/checks/ports.js +++ b/src/render/conformance/checks/ports.js @@ -19,6 +19,7 @@ import { twoline2satrec, } from 'satellite.js'; import { deepEqual, equal, near, nearVec, ok, throws } from '../expect.js'; +import { iconPosition } from '../../motion/iconMotion.js'; /** 1 × 1 PNGs (red, green, blue), so imagery checks load nothing from the network. */ const PIXEL_PNGS = [ @@ -925,6 +926,87 @@ export function portChecks() { }, { capability: 'motionSegments' }, ), + check( + 'draw.motion.shared', + 'icons are drawn where the shared motion evaluator puts them', + async (h, r) => { + await overAustin(h, r, 30000); + const t0 = r.frame.time(); + const fix = h.toEcef({ ...AUSTIN, height: 9000 }); + const to = h.toEcef({ + ...AUSTIN, + lon: AUSTIN.lon + 0.03, + height: 9000, + }); + const items = [ + { + id: 'arc', + pos: fix, + image: DOT, + size: [16, 16], + motion: { + kind: 'arc', + fix, + t0, + speedMps: 180, + trackDeg: 200, + turnRateDps: -2.5, + climbMps: 100, + minDtS: -20, + maxDtS: 15, + }, + }, + { + id: 'step', + pos: fix, + image: DOT, + size: [16, 16], + // No span: at `from` until t0, at `to` after it. + motion: { kind: 'lerp', from: fix, to, t0: t0 + 4000, t1: t0 }, + }, + ]; + const set = r.draw.icons({ + id: 'conf:shared', + motionClock: { kind: 'scene' }, + }); + set.set(items); + const find = (id, p) => { + const s = r.query.project(p); + return r.query + .drillPick(s.x, s.y) + .find((x) => x.set === 'conf:shared' && x.id === id); + }; + const apart = (a, b) => { + const sa = r.query.project(a); + const sb = r.query.project(b); + return Math.hypot(sa.x - sb.x, sa.y - sb.y); + }; + // Each sample against where the item is at another of them: the + // evaluator's answer, and not the other. + const samples = [-30, 9, 40]; + for (const [k, dtS] of samples.entries()) { + const t = t0 + dtS * 1000; + const other = t0 + samples[(k + 1) % samples.length] * 1000; + r.frame.setTime(t); + await drawn(h, r); + for (const item of items) { + const want = iconPosition(item, t); + const elsewhere = iconPosition(item, other); + if (apart(want, elsewhere) < 20) continue; + const hit = find(item.id, want); + ok(hit, `${item.id} at ${dtS} s: picked there`); + if (hit?.position) + nearVec(hit.position, want, 50, `${item.id} at ${dtS} s: there`); + ok( + !find(item.id, elsewhere), + `${item.id} at ${dtS} s: not elsewhere`, + ); + } + } + set.dispose(); + }, + { capability: 'motionSegments' }, + ), check( 'draw.lodGroup', 'members of a lodGroup show over their own ranges and picks report the group', diff --git a/src/render/motion/iconMotion.js b/src/render/motion/iconMotion.js new file mode 100644 index 000000000..e59403080 --- /dev/null +++ b/src/render/motion/iconMotion.js @@ -0,0 +1,205 @@ +/** + * @module render/motion/iconMotion + * @description Where an icon item is drawn at a time, and the course it is + * drawn with, from its contract `motion` segment and `courseGlide` (see + * `MotionSegment` and `CourseGlide` in `render/contract/draw.d.ts`). The app + * reads a moving icon back with it (to follow it, label it, judge a pick or + * its horizon) instead of asking the renderer; the renderer evaluates its + * segments with it on the CPU, and the default renderer's GPU icons draw the + * same path (`data/iconSegment.js` packs it, the shader evaluates it). + * + * Times are epoch ms on the set's motion clock. Plain numbers and `{x, y, z}` + * objects only: no renderer is imported. + * + * Choices the contract leaves open, as the default renderer takes them: + * - A `lerp` whose `t1` is not after `t0` runs from `from` to `to` over the + * 1 ms after `t0` (the default renderer's GPU icons widen its span so). + * - Turn rates under 1e-4 rad/s fly straight. + * - A segment's course is its own (`hold.courseDeg`, `lerp.course0Deg`, the + * arc's track), else the item's `course`, else north. + * - A glide turns the shorter way from `fromCourseDeg` onto the segment's + * course at `rateDps`, then follows it; a rate of 0 or less holds + * `fromCourseDeg` for the whole segment. + */ +import { positionToEcef } from '../../geo/ellipsoid.js'; +import { eastNorthUp } from '../../geo/frames.js'; +import { norm180, norm360 } from '../../geo/vector.js'; + +export { norm180, norm360 }; + +/** Turn rates under this (rad/s) fly straight. */ +export const STRAIGHT_BELOW_RAD_PER_S = 1e-4; +/** How long (ms) a `lerp` with no span takes to step to `to`. */ +export const LERP_STEP_MS = 1; + +const DEG = Math.PI / 180; + +/** A lerp's elapsed fraction at `t`, 0..1. */ +function lerpFraction(segment, t) { + const span = Math.max(segment.t1 - segment.t0, LERP_STEP_MS); + return Math.min(1, Math.max(0, (t - segment.t0) / span)); +} + +/** An arc's elapsed seconds at `t`, clamped to `[minDtS, maxDtS]`. */ +function arcSeconds(segment, t) { + return Math.min( + segment.maxDtS, + Math.max(segment.minDtS, (t - segment.t0) / 1000), + ); +} + +/** An arc's turn rate, rad/s: 0 under the straight threshold. */ +function arcOmega(segment) { + const omega = (segment.turnRateDps || 0) * DEG; + return Math.abs(omega) < STRAIGHT_BELOW_RAD_PER_S ? 0 : omega; +} + +function sinc(x) { + return Math.abs(x) < 1e-4 ? 1 - (x * x) / 6 : Math.sin(x) / x; +} + +/** + * Where `segment` puts an icon at `t`, ECEF. + * @param {import('../contract/draw').MotionSegment|undefined} segment + * @param {import('../contract/common').Position} pos The item's own + * position (a `hold`'s, or with no segment). + * @param {number} t Epoch ms on the set's motion clock. + * @returns {{x: number, y: number, z: number}} + */ +export function motionPosition(segment, pos, t) { + switch (segment?.kind) { + case 'lerp': { + const f = lerpFraction(segment, t); + const { from, to } = segment; + return { + x: from.x + (to.x - from.x) * f, + y: from.y + (to.y - from.y) * f, + z: from.z + (to.z - from.z) * f, + }; + } + case 'arc': { + const dt = arcSeconds(segment, t); + // cos a − cos(a+b) = 2 sin(b/2) sin(a+b/2): the same path straight or + // turning, with no cancellation. + const half = arcOmega(segment) * dt * 0.5; + const reach = (segment.speedMps || 0) * dt * sinc(half); + const heading = (segment.trackDeg || 0) * DEG + half; + const e = reach * Math.sin(heading); + const n = reach * Math.cos(heading); + const u = (segment.climbMps || 0) * dt; + const { x: east, y: north, z: up } = eastNorthUp(segment.fix); + const fix = segment.fix; + return { + x: fix.x + east.x * e + north.x * n + up.x * u, + y: fix.y + east.y * e + north.y * n + up.y * u, + z: fix.z + east.z * e + north.z * n + up.z * u, + }; + } + case 'hold': + case undefined: + return positionToEcef(pos); + default: + throw new Error(`unknown motion segment ${segment.kind}`); + } +} + +/** + * The segment's own course at `t`, unwrapped (before any glide), from its + * start course in [0, 360). + */ +function segmentCourseAt(segment, course, t) { + switch (segment?.kind) { + case 'lerp': + return ( + norm360(segment.course0Deg ?? course ?? 0) + + (segment.courseSlopeDeg || 0) * lerpFraction(segment, t) + ); + case 'arc': + return ( + norm360(segment.trackDeg || 0) + + (arcOmega(segment) / DEG) * arcSeconds(segment, t) + ); + case 'hold': + return norm360(segment.courseDeg ?? course ?? 0); + default: + return norm360(course ?? 0); + } +} + +/** + * How far (degrees, (-180, 180]) a glide starts from its segment's course at + * the glide's start: its sign is the way it turns. A renderer packing the + * glide writes this offset, so an exact half turn goes the way the app reads. + * @param {import('../contract/draw').MotionSegment|undefined} segment + * @param {number|undefined} course The item's own `course`. + * @param {import('../contract/draw').CourseGlide} glide + */ +export function glideOffset(segment, course, glide) { + return norm180( + glide.fromCourseDeg - segmentCourseAt(segment, course, glide.t0), + ); +} + +/** + * The course an icon is drawn with at `t`, degrees clockwise from north in + * [0, 360): its segment's, eased by `glide` when it has one. + * @param {import('../contract/draw').MotionSegment|undefined} segment + * @param {number|undefined} course The item's own `course`. + * @param {number} t Epoch ms on the set's motion clock. + * @param {import('../contract/draw').CourseGlide} [glide] + * @returns {number} + */ +export function motionCourse(segment, course, t, glide) { + const target = segmentCourseAt(segment, course, t); + if (!glide) return norm360(target); + const start = segmentCourseAt(segment, course, glide.t0); + const offset = glideOffset(segment, course, glide); + // Held: the course at the glide's start, whatever the segment does. + if (!(glide.rateDps > 0)) return norm360(start + offset); + if (offset === 0) return norm360(target); + // Turning toward the segment's (unwrapped, maybe turning) course at the + // rate until it reaches it, then following it. + const toward = -Math.sign(offset); + const turned = glide.rateDps * (Math.max(0, t - glide.t0) / 1000); + const gap = toward * (target - start - offset); + return norm360(turned < gap ? start + offset + toward * turned : target); +} + +/** + * Where an icon item is drawn at `t`: along its `motion`, else at its `pos`. + * Pass the item without `motion` for a renderer without `motionSegments`. + * @param {{pos: object, motion?: object}} item + * @param {number} t Epoch ms on the set's motion clock. + */ +export function iconPosition(item, t) { + return motionPosition(item.motion, item.pos, t); +} + +/** + * The course an icon item is drawn with at `t` (for `rotationMode: + * 'course'`): its motion's, eased by its `courseGlide`. Pass the item without + * them for a renderer without `motionSegments` or `courseGlide`. + * @param {{course?: number, motion?: object, courseGlide?: object}} item + * @param {number} t Epoch ms on the set's motion clock. + */ +export function iconCourse(item, t) { + return motionCourse(item.motion, item.course, t, item.courseGlide); +} + +/** + * Until when (epoch ms) an item's drawn position or course may still + * change: -Infinity for one that holds still, Infinity for none that ends. + * @param {{motion?: object, courseGlide?: object}} item + */ +export function motionEndsAt(item) { + const motion = item.motion; + let end = -Infinity; + if (motion?.kind === 'lerp') + end = Math.max(motion.t1, motion.t0 + LERP_STEP_MS); + else if (motion?.kind === 'arc') end = motion.t0 + motion.maxDtS * 1000; + const glide = item.courseGlide; + // Turning at most half a turn at its rate. + if (glide?.rateDps > 0) + end = Math.max(end, glide.t0 + (180 / glide.rateDps) * 1000); + return end; +} diff --git a/src/render/motion/iconMotion.test.mjs b/src/render/motion/iconMotion.test.mjs new file mode 100644 index 000000000..19899fb0c --- /dev/null +++ b/src/render/motion/iconMotion.test.mjs @@ -0,0 +1,176 @@ +import assert from 'node:assert/strict'; +import { readFileSync } from 'node:fs'; +import test from 'node:test'; +import { fromDegrees } from '../../geo/ellipsoid.js'; +import { eastNorthUp } from '../../geo/frames.js'; +import { + iconCourse, + iconPosition, + motionCourse, + motionEndsAt, + motionPosition, +} from './iconMotion.js'; + +const FIX = fromDegrees(-97.04, 32.9, 10_500); +const T0 = Date.UTC(2026, 10, 7, 12); +const near = (a, b, tol, what) => + assert.ok(Math.abs(a - b) <= tol, `${what}: ${a} vs ${b}`); +const nearVec = (a, b, tol, what) => + assert.ok( + Math.hypot(a.x - b.x, a.y - b.y, a.z - b.z) <= tol, + `${what}: ${JSON.stringify(a)} vs ${JSON.stringify(b)}`, + ); + +test('the evaluator imports no renderer', () => { + const source = readFileSync( + new URL('./iconMotion.js', import.meta.url), + 'utf8', + ); + assert.doesNotMatch(source, /from 'cesium'/); +}); + +test('hold and no motion stay at the item’s position, geodetic or ECEF', () => { + const geo = { lon: 4.1, lat: 51.95, height: 2 }; + nearVec( + iconPosition({ pos: geo }, T0), + fromDegrees(4.1, 51.95, 2), + 1e-6, + 'none', + ); + nearVec( + iconPosition({ pos: FIX, motion: { kind: 'hold' } }, T0 + 9e6), + FIX, + 0, + 'hold', + ); + assert.equal(iconCourse({ pos: FIX, course: -30 }, T0), 330, 'its course'); + assert.equal( + iconCourse( + { pos: FIX, course: 10, motion: { kind: 'hold', courseDeg: 45 } }, + T0, + ), + 45, + 'the hold’s course first', + ); +}); + +test('a lerp runs from `from` to `to`, holds at the ends, and steps when it has no span', () => { + const to = { x: FIX.x + 1000, y: FIX.y, z: FIX.z }; + const motion = { kind: 'lerp', from: FIX, to, t0: T0, t1: T0 + 10_000 }; + nearVec(motionPosition(motion, FIX, T0 - 5000), FIX, 0, 'before'); + near(motionPosition(motion, FIX, T0 + 2500).x, FIX.x + 250, 1e-6, 'quarter'); + nearVec(motionPosition(motion, FIX, T0 + 60_000), to, 0, 'after'); + const step = { ...motion, t1: T0 - 50 }; + nearVec(motionPosition(step, FIX, T0), FIX, 0, 'no span: still at from'); + nearVec(motionPosition(step, FIX, T0 + 1), to, 0, 'at to 1 ms later'); + const course = { ...motion, course0Deg: 350, courseSlopeDeg: 20 }; + near(motionCourse(course, 0, T0 + 5000), 0, 1e-9, 'course wraps north'); + near(motionCourse(course, 0, T0 + 20_000), 10, 1e-9, 'holds its end course'); +}); + +test('an arc turns in the fix’s tangent plane, climbs, coasts and warms up', () => { + const arc = { + kind: 'arc', + fix: FIX, + t0: T0, + speedMps: 200, + trackDeg: 90, + turnRateDps: 3, + climbMps: 10, + minDtS: -10, + maxDtS: 20, + }; + const enu = eastNorthUp(FIX); + // The contract's closed form. + const at = (dt) => { + const track = (arc.trackDeg * Math.PI) / 180; + const w = (arc.turnRateDps * Math.PI) / 180; + const e = (arc.speedMps / w) * (Math.cos(track) - Math.cos(track + w * dt)); + const n = (arc.speedMps / w) * (Math.sin(track + w * dt) - Math.sin(track)); + const u = arc.climbMps * dt; + return { + x: FIX.x + enu.x.x * e + enu.y.x * n + enu.z.x * u, + y: FIX.y + enu.x.y * e + enu.y.y * n + enu.z.y * u, + z: FIX.z + enu.x.z * e + enu.y.z * n + enu.z.z * u, + }; + }; + nearVec(motionPosition(arc, FIX, T0 + 8000), at(8), 1e-6, 'at 8 s'); + nearVec( + motionPosition(arc, FIX, T0 + 60_000), + at(20), + 1e-6, + 'coasts to maxDtS', + ); + nearVec( + motionPosition(arc, FIX, T0 - 60_000), + at(-10), + 1e-6, + 'warms up from minDtS', + ); + near(motionCourse(arc, 0, T0 + 10_000), 120, 1e-9, 'its track turns'); + const slow = { ...arc, turnRateDps: 0.001, climbMps: 0 }; + // Under 1e-4 rad/s: straight east. + const straight = motionPosition(slow, FIX, T0 + 10_000); + nearVec( + straight, + { + x: FIX.x + enu.x.x * 2000, + y: FIX.y + enu.x.y * 2000, + z: FIX.z + enu.x.z * 2000, + }, + 1e-6, + 'straight', + ); + near(motionCourse(slow, 0, T0 + 10_000), 90, 1e-9, 'straight course'); +}); + +test('a glide turns the short way at its rate, then follows; 0 holds', () => { + const hold = { kind: 'hold', courseDeg: 30 }; + const glide = (rateDps, fromCourseDeg = 330) => ({ + fromCourseDeg, + t0: T0, + rateDps, + }); + near(motionCourse(hold, 0, T0 - 1000, glide(10)), 330, 1e-9, 'before t0'); + near(motionCourse(hold, 0, T0 + 3000, glide(10)), 0, 1e-9, 'through north'); + near(motionCourse(hold, 0, T0 + 10_000, glide(10)), 30, 1e-9, 'there'); + near(motionCourse(hold, 0, T0 + 60_000, glide(0)), 330, 1e-9, 'held'); + near(motionCourse(hold, 0, T0, glide(10, 30)), 30, 1e-9, 'already there'); + // Onto a turning track: catches it and follows. + const arc = { + kind: 'arc', + fix: FIX, + t0: T0, + speedMps: 100, + trackDeg: 0, + turnRateDps: 2, + minDtS: 0, + maxDtS: 600, + }; + near(motionCourse(arc, 0, T0 + 2000, glide(6, 340)), 352, 1e-9, 'gliding'); + near(motionCourse(arc, 0, T0 + 30_000, glide(6, 340)), 60, 1e-9, 'following'); +}); + +test('motion ends when its segment and glide do', () => { + assert.equal(motionEndsAt({ motion: { kind: 'hold' } }), -Infinity); + assert.equal(motionEndsAt({}), -Infinity); + assert.equal( + motionEndsAt({ motion: { kind: 'lerp', t0: T0, t1: T0 + 10 } }), + T0 + 10, + ); + assert.equal( + motionEndsAt({ motion: { kind: 'lerp', t0: T0, t1: T0 } }), + T0 + 1, + ); + assert.equal( + motionEndsAt({ motion: { kind: 'arc', t0: T0, maxDtS: 5 } }), + T0 + 5000, + ); + assert.equal( + motionEndsAt({ + motion: { kind: 'hold' }, + courseGlide: { fromCourseDeg: 0, t0: T0, rateDps: 10 }, + }), + T0 + 18_000, + ); +}); diff --git a/src/render/stub/index.js b/src/render/stub/index.js index d08378c48..9bdbe4583 100644 --- a/src/render/stub/index.js +++ b/src/render/stub/index.js @@ -13,7 +13,7 @@ import { horizonFrom } from '../../geo/horizon.js'; import { createInputRouter } from '../input/router.js'; import { CAPABILITIES, CONTRACT_VERSION } from '../contract/index.js'; import { createStubCamera } from './camera.js'; -import { motionPosition } from './motion.js'; +import { motionPosition } from '../motion/iconMotion.js'; import { orbitPosition } from './orbits.js'; import { computeViewRectangle } from './viewRectangle.js'; import { diff --git a/src/render/stub/math.js b/src/render/stub/math.js index 896309eca..b879b551b 100644 --- a/src/render/stub/math.js +++ b/src/render/stub/math.js @@ -14,8 +14,6 @@ import { clamp, equalsEpsilon, negate, - clone, - fromDegrees, rotate, scale, vec, @@ -25,11 +23,7 @@ import { export * from '../../geo/index.js'; /** A contract `Position` (`Ecef` or `LonLatH`, degrees), as ECEF. */ -export function toEcef(position) { - if ('lon' in position && 'lat' in position) - return fromDegrees(position.lon, position.lat, position.height ?? 0); - return clone(position); -} +export { positionToEcef as toEcef } from '../../geo/ellipsoid.js'; const EPSILON3 = 1e-3; const EPSILON10 = 1e-10; diff --git a/src/render/stub/motion.js b/src/render/stub/motion.js deleted file mode 100644 index 473b63b0d..000000000 --- a/src/render/stub/motion.js +++ /dev/null @@ -1,57 +0,0 @@ -/** - * @module render/stub/motion - * @description Where a motion segment puts an icon at a time, exactly as - * the contract's `MotionSegment` describes it. - */ -import { add, eastNorthUp, scale, sub } from './math.js'; - -const STRAIGHT_RAD_PER_S = 1e-4; - -/** - * @param {import('../contract/draw').MotionSegment} segment - * @param {{x: number, y: number, z: number}} pos The item's own position (for `hold`). - * @param {number} t Epoch ms on the set's motion clock. - * @returns {{x: number, y: number, z: number}} - */ -export function motionPosition(segment, pos, t) { - switch (segment.kind) { - case 'hold': - return pos; - case 'lerp': { - const span = segment.t1 - segment.t0; - const s = - span > 0 - ? Math.min(1, Math.max(0, (t - segment.t0) / span)) - : t >= segment.t1 - ? 1 - : 0; - return add(segment.from, scale(sub(segment.to, segment.from), s)); - } - case 'arc': { - const dt = Math.min( - segment.maxDtS, - Math.max(segment.minDtS, (t - segment.t0) / 1000), - ); - const track = (segment.trackDeg * Math.PI) / 180; - const w = (segment.turnRateDps * Math.PI) / 180; - const v = segment.speedMps; - let east; - let north; - if (Math.abs(w) < STRAIGHT_RAD_PER_S) { - east = v * dt * Math.sin(track); - north = v * dt * Math.cos(track); - } else { - east = (v / w) * (Math.cos(track) - Math.cos(track + w * dt)); - north = (v / w) * (Math.sin(track + w * dt) - Math.sin(track)); - } - const up = (segment.climbMps ?? 0) * dt; - const enu = eastNorthUp(segment.fix); - return add( - segment.fix, - add(add(scale(enu.x, east), scale(enu.y, north)), scale(enu.z, up)), - ); - } - default: - throw new Error(`unknown motion segment ${segment.kind}`); - } -}