mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Read a moving icon's position and course through one shared evaluator
render/motion/iconMotion evaluates a draw item's motion segment and course glide at a time on the set's motion clock, without a renderer: where the item is drawn, the course it is drawn with, and when its motion ends. The app can now read a moving icon back (to follow it, label it, judge a pick or its horizon) the way the renderer draws it. The stub renderer evaluates its segments with it, in place of its own copy, and so now steps a lerp with no span 1 ms after t0, as the Cesium renderer does. The Cesium GPU icon sets read the course shown before a write, a blend's target and when rows stop moving through it, from copies of what they wrote, and write each glide with the offset it computes, so an exact half turn goes the way the app reads it. norm360/norm180 and Position-to-ECEF move to geo for both to share. Conformance gains a check that icons are drawn where the evaluator puts them.
This commit is contained in:
1 parent
b774bd84c8
commit
0349941b71
14 files changed
+703
-105
No files matched your search
@@ -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;
|
||||
}
|
||||
@@ -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,
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user