Files
gods-eye-view/src/data/iconSegment.test.mjs
T
Sameh Khamis d8d6db4389 Keep icon sets and their motion segments off the renderer
iconSet.js and iconSegment.js no longer import Cesium and leave the
allow-list. A segment's base is split into high and low floats, and an
arc's east and north axes are computed, the way the default renderer
did it, so the GPU reads the same bits (tests compare every stored float
against Cesium, poles and the centre included). The set's epoch is a
geo/julian date; colours, distance curves and cull points come back as
plain objects. Renderer dates and colours passed in still work.
2026-11-08 05:33:20 -08:00

334 lines
9.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import test from 'node:test';
import assert from 'node:assert/strict';
import * as Cesium from 'cesium';
import {
SEGMENT_STRIDE,
segmentCourse,
segmentPosition,
writeArcSegment,
writeCourseGlide,
writeHoldSegment,
writeLerpSegment,
} from './iconSegment.js';
import {
arcOffsetEnu,
lerpAngleDeg,
limitCourseStep,
norm180,
} from './motionModel.js';
const fix = Cesium.Cartesian3.fromDegrees(-97.04, 32.9, 10_500);
/** What the flights layer's _extrapolateFix draws for the same fix and dt. */
function productionArc(position, speed, trackDeg, turnRateDps, dt) {
const arc = arcOffsetEnu(speed, trackDeg, turnRateDps, dt, {});
const enu = Cesium.Transforms.eastNorthUpToFixedFrame(position);
const offset = new Cesium.Cartesian3(arc.east, arc.north, 0);
return {
position: Cesium.Matrix4.multiplyByPoint(
enu,
offset,
new Cesium.Cartesian3(),
),
course: arc.endCourseDeg,
};
}
function courseGap(a, b) {
return Math.abs(norm180(a - b));
}
test('a hold stays put to float32 encoding precision', () => {
const seg = new Float32Array(SEGMENT_STRIDE * 2);
writeHoldSegment(seg, 1, fix, 271);
const at = segmentPosition(seg, 1, 1234);
assert.ok(Cesium.Cartesian3.distance(at, fix) < 0.005);
assert.equal(segmentCourse(seg, 1, 99), 271);
});
test('a lerp matches Cesium.Cartesian3.lerp between two fixes and holds at both ends', () => {
const seg = new Float32Array(SEGMENT_STRIDE);
const to = Cesium.Cartesian3.fromDegrees(-97.0, 32.93, 10_650);
writeLerpSegment(seg, 0, {
from: fix,
to,
t0: 100,
t1: 110,
course0Deg: 40,
courseSlopeDeg: 6,
});
for (const tau of [100, 102.5, 107, 110]) {
const want = Cesium.Cartesian3.lerp(
fix,
to,
(tau - 100) / 10,
new Cesium.Cartesian3(),
);
assert.ok(
Cesium.Cartesian3.distance(segmentPosition(seg, 0, tau), want) < 0.01,
`τ=${tau}`,
);
}
assert.ok(
Cesium.Cartesian3.distance(segmentPosition(seg, 0, 50), fix) < 0.01,
);
assert.ok(
Cesium.Cartesian3.distance(segmentPosition(seg, 0, 500), to) < 0.01,
);
assert.ok(courseGap(segmentCourse(seg, 0, 105), 43) < 1e-4);
});
test('a lerp course reproduces the blended track-and-chord course across the segment', () => {
// _deadReckon: lerpAngle(lerpAngle(trackFrom, trackTo, t), chord, w).
const trackFrom = 350;
const trackTo = 20;
const chord = 10;
const w = 0.6;
const at = (t) => lerpAngleDeg(lerpAngleDeg(trackFrom, trackTo, t), chord, w);
const course0 = at(0);
const slope = norm180(trackTo - trackFrom) * (1 - w);
const seg = new Float32Array(SEGMENT_STRIDE);
const to = Cesium.Cartesian3.fromDegrees(-97.0, 32.93, 10_650);
writeLerpSegment(seg, 0, {
from: fix,
to,
t0: 0,
t1: 10,
course0Deg: course0,
courseSlopeDeg: slope,
});
for (const t of [0, 0.25, 0.5, 0.9, 1])
assert.ok(courseGap(segmentCourse(seg, 0, t * 10), at(t)) < 1e-3, `t=${t}`);
});
test('an arc matches the production turn for straight, slow, standard and tight turns', () => {
const seg = new Float32Array(SEGMENT_STRIDE);
for (const turnRateDps of [0, 0.004, 0.01, 0.5, 3, -3, 12]) {
for (const dt of [0.08, 1, 30, 120, 300]) {
writeArcSegment(seg, 0, {
fix,
t0: 1000,
minDt: -60,
maxDt: 300,
speedMps: 240,
trackDeg: 305,
turnRateDps,
});
const want = productionArc(fix, 240, 305, turnRateDps, dt);
const got = segmentPosition(seg, 0, 1000 + dt);
assert.ok(
Cesium.Cartesian3.distance(got, want.position) < 0.05,
`ω=${turnRateDps}°/s dt=${dt}s off by ${Cesium.Cartesian3.distance(got, want.position)} m`,
);
assert.ok(
courseGap(segmentCourse(seg, 0, 1000 + dt), want.course) < 1e-3,
);
}
}
});
test('an arc extrapolates backward for warm-up and clamps both ends', () => {
const seg = new Float32Array(SEGMENT_STRIDE);
writeArcSegment(seg, 0, {
fix,
t0: 500,
minDt: -60,
maxDt: 0,
speedMps: 200,
trackDeg: 90,
turnRateDps: 1.5,
});
const back = productionArc(fix, 200, 90, 1.5, -20);
assert.ok(
Cesium.Cartesian3.distance(segmentPosition(seg, 0, 480), back.position) <
0.05,
);
const earliest = productionArc(fix, 200, 90, 1.5, -60);
assert.ok(
Cesium.Cartesian3.distance(segmentPosition(seg, 0, 0), earliest.position) <
0.05,
);
assert.ok(
Cesium.Cartesian3.distance(segmentPosition(seg, 0, 900), fix) < 0.01,
);
});
test('the course glide follows limitCourseStep onto a steady target', () => {
const seg = new Float32Array(SEGMENT_STRIDE);
writeHoldSegment(seg, 0, fix, 10);
writeCourseGlide(seg, 0, 300, 50, 24);
let prev = 300;
for (let tau = 50; tau <= 56; tau += 0.08) {
assert.ok(courseGap(segmentCourse(seg, 0, tau), prev) < 1e-3, `τ=${tau}`);
prev = limitCourseStep(prev, 10, 24, 0.08);
}
assert.equal(segmentCourse(seg, 0, 80), 10);
});
test('writing a segment clears the previous one', () => {
const seg = new Float32Array(SEGMENT_STRIDE);
writeArcSegment(seg, 0, {
fix,
t0: 0,
minDt: 0,
maxDt: 300,
speedMps: 250,
trackDeg: 0,
turnRateDps: 3,
});
writeCourseGlide(seg, 0, 90, 0, 30);
writeHoldSegment(seg, 0, fix, 5);
assert.ok(
Cesium.Cartesian3.distance(segmentPosition(seg, 0, 100), fix) < 0.005,
);
assert.equal(segmentCourse(seg, 0, 0), 5);
});
test('the course glide never outruns the rate while its target turns', () => {
const seg = new Float32Array(SEGMENT_STRIDE);
writeArcSegment(seg, 0, {
fix,
t0: 0,
minDt: 0,
maxDt: 300,
speedMps: 200,
trackDeg: 90,
turnRateDps: 3,
});
writeCourseGlide(seg, 0, 60, 0, 24);
let prev = 60;
for (let tau = 0; tau <= 4; tau += 0.01) {
assert.ok(
courseGap(segmentCourse(seg, 0, tau), prev) < 0.05,
`τ=${tau.toFixed(2)}`,
);
prev = limitCourseStep(prev, 90 + 3 * (tau + 0.01), 24, 0.01);
}
assert.ok(
courseGap(segmentCourse(seg, 0, 1), 84) < 0.05,
'24° in the first second, not 27°',
);
assert.ok(
courseGap(segmentCourse(seg, 0, 10), 120) < 1e-3,
'then it follows the turn',
);
});
test('a zero-rate glide holds the course however the segment turns', () => {
const seg = new Float32Array(SEGMENT_STRIDE);
const to = Cesium.Cartesian3.fromDegrees(-97.04, 32.90001, 10_500);
for (const from of [0, 30]) {
writeLerpSegment(seg, 0, {
from: fix,
to,
t0: 0,
t1: 30,
course0Deg: 0,
courseSlopeDeg: 90,
});
writeCourseGlide(seg, 0, from, 0, 0);
for (const tau of [0, 10, 20, 30, 60])
assert.ok(
courseGap(segmentCourse(seg, 0, tau), from) < 1e-4,
`from ${from}° at τ=${tau}`,
);
}
writeArcSegment(seg, 0, {
fix,
t0: 0,
minDt: 0,
maxDt: 300,
speedMps: 1,
trackDeg: 90,
turnRateDps: 3,
});
writeCourseGlide(seg, 0, 90, 0, 0);
assert.ok(courseGap(segmentCourse(seg, 0, 20), 90) < 1e-4, 'arc turn held');
});
test('an arc climbs along the fix’s up axis at its climb rate, inside its clamped time', () => {
const seg = new Float32Array(SEGMENT_STRIDE);
writeArcSegment(seg, 0, {
fix,
t0: 100,
minDt: -10,
maxDt: 20,
speedMps: 200,
trackDeg: 90,
turnRateDps: 3,
climbMps: 15,
});
const up = Cesium.Ellipsoid.WGS84.geodeticSurfaceNormal(fix);
for (const [tau, dt] of [
[108, 8],
[130, 20],
[80, -10],
]) {
const flat = productionArc(fix, 200, 90, 3, dt).position;
const want = Cesium.Cartesian3.add(
flat,
Cesium.Cartesian3.multiplyByScalar(up, 15 * dt, new Cesium.Cartesian3()),
new Cesium.Cartesian3(),
);
const at = segmentPosition(seg, 0, tau);
assert.ok(
Cesium.Cartesian3.distance(at, want) < 0.05,
`at ${dt} s: ${Cesium.Cartesian3.distance(at, want)} m off`,
);
}
// The climb does not turn the course.
assert.ok(courseGap(segmentCourse(seg, 0, 108), 90 + 3 * 8) < 1e-3);
});
test('an arc written without a climb stays in its tangent plane', () => {
const seg = new Float32Array(SEGMENT_STRIDE);
seg.fill(7);
writeArcSegment(seg, 0, {
fix,
t0: 0,
minDt: 0,
maxDt: 30,
speedMps: 100,
trackDeg: 0,
turnRateDps: 0,
});
const at = segmentPosition(seg, 0, 30);
const want = productionArc(fix, 100, 0, 0, 30).position;
assert.ok(Cesium.Cartesian3.distance(at, want) < 0.05);
});
test('segments store the floats the default renderer encodes, east and north included', () => {
const points = [
fix,
Cesium.Cartesian3.fromDegrees(151.2, -33.9, 40),
Cesium.Cartesian3.fromDegrees(-0.0001, 0.0001, -31),
Cesium.Cartesian3.fromDegrees(12, 89.9999, 9_000),
new Cesium.Cartesian3(0, 0, 6_356_752.3),
new Cesium.Cartesian3(0, 0, -6_356_752.3),
new Cesium.Cartesian3(-65536, 131072.5, -1),
new Cesium.Cartesian3(0, 0, 0),
new Cesium.Cartesian3(0, 0, -1e-15),
];
const encoded = new Cesium.EncodedCartesian3();
const enu = new Cesium.Matrix4();
for (const p of points) {
const seg = new Float32Array(SEGMENT_STRIDE);
writeArcSegment(seg, 0, {
fix: p,
t0: 0,
minDt: 0,
maxDt: 10,
speedMps: 100,
trackDeg: 30,
turnRateDps: 1,
});
const want = new Float32Array(SEGMENT_STRIDE);
Cesium.EncodedCartesian3.fromCartesian(p, encoded);
Cesium.Transforms.eastNorthUpToFixedFrame(p, Cesium.Ellipsoid.WGS84, enu);
want.set([encoded.high.x, encoded.high.y, encoded.high.z], 0);
want.set([encoded.low.x, encoded.low.y, encoded.low.z], 3);
want.set([enu[0], enu[1], enu[2], enu[4], enu[5], enu[6]], 6);
assert.deepStrictEqual(seg.slice(0, 12), want.slice(0, 12), `at ${p}`);
}
});