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.
This commit is contained in:
Sameh Khamis committed 2026-11-08 05:33:20 -08:00
1 parent 09f2b88bf9
commit d8d6db4389
5 files changed
+201 -65

No files matched your search

+75 -30
View File
@@ -18,9 +18,10 @@
* as `arcOffsetEnu` does.
*
* Times are seconds since the owning set's epoch. `base` is stored as high and low
* float32 parts (Cesium's EncodedCartesian3) so the GPU can draw it relative to the eye.
* float32 parts (split at multiples of 65536 m, as the default renderer's
* EncodedCartesian3 splits them) so the GPU can draw it relative to the eye.
*/
import * as Cesium from 'cesium';
import { WGS84 } from '../geo/ellipsoid.js';
import { norm180, norm360 } from './motionModel.js';
/** Floats per packed segment. */
@@ -52,17 +53,73 @@ const SLEW_RATE = 23; // deg/s; negative holds the course at the glide's start
const DEG = Math.PI / 180;
const STRAIGHT_BELOW_RAD_PER_SEC = 1e-4;
const _encoded = new Cesium.EncodedCartesian3();
const _enu = new Cesium.Matrix4();
const SPLIT = 65536.0;
/**
* One coordinate as its high part (a multiple of 65536 toward zero) and the
* remainder, written at `high` and `low`: `EncodedCartesian3.encode` step
* for step, so the stored floats are the same.
*/
function writeSplit(out, high, low, value) {
let doubleHigh;
if (value >= 0.0) {
doubleHigh = Math.floor(value / SPLIT) * SPLIT;
out[high] = doubleHigh;
out[low] = value - doubleHigh;
} else {
doubleHigh = Math.floor(-value / SPLIT) * SPLIT;
out[high] = -doubleHigh;
out[low] = value + doubleHigh;
}
}
const POLE_EPSILON = 1e-14;
/**
* The east and north unit axes at `p` into `out` at `u` and `v`: the
* default renderer's `Transforms.eastNorthUpToFixedFrame` step for step (signed zeros at a pole included), without allocating.
*/
function writeEastNorth(out, u, v, p) {
if (Math.abs(p.x) <= POLE_EPSILON && Math.abs(p.y) <= POLE_EPSILON) {
// At the centre the default renderer's fixed frame; at a pole east is +y
// and north (its signed zeros included) points away from the pole.
const sign = Math.abs(p.z) <= POLE_EPSILON ? 1 : p.z > 0 ? 1 : -1;
out[u] = 0;
out[u + 1] = 1;
out[u + 2] = 0;
out[v] = -1 * sign;
out[v + 1] = 0 * sign;
out[v + 2] = 0 * sign;
return;
}
const o = WGS84.oneOverRadiiSquared;
let ux = p.x * o.x;
let uy = p.y * o.y;
let uz = p.z * o.z;
const um = Math.sqrt(ux * ux + uy * uy + uz * uz);
ux /= um;
uy /= um;
uz /= um;
let ex = -p.y;
let ey = p.x;
let ez = 0;
const em = Math.sqrt(ex * ex + ey * ey + ez * ez);
ex /= em;
ey /= em;
ez /= em;
out[u] = ex;
out[u + 1] = ey;
out[u + 2] = ez;
// North is up × east.
out[v] = uy * ez - uz * ey;
out[v + 1] = uz * ex - ux * ez;
out[v + 2] = ux * ey - uy * ex;
}
function writeBase(out, o, position) {
Cesium.EncodedCartesian3.fromCartesian(position, _encoded);
out[o + BASE_HIGH] = _encoded.high.x;
out[o + BASE_HIGH + 1] = _encoded.high.y;
out[o + BASE_HIGH + 2] = _encoded.high.z;
out[o + BASE_LOW] = _encoded.low.x;
out[o + BASE_LOW + 1] = _encoded.low.y;
out[o + BASE_LOW + 2] = _encoded.low.z;
writeSplit(out, o + BASE_HIGH, o + BASE_LOW, position.x);
writeSplit(out, o + BASE_HIGH + 1, o + BASE_LOW + 1, position.y);
writeSplit(out, o + BASE_HIGH + 2, o + BASE_LOW + 2, position.z);
}
function clearMotion(out, o) {
@@ -73,7 +130,7 @@ function clearMotion(out, o) {
* Stay at `position` facing `courseDeg`.
* @param {Float32Array} out
* @param {number} row
* @param {Cesium.Cartesian3} position
* @param {{x: number, y: number, z: number}} position ECEF.
* @param {number} courseDeg
*/
export function writeHoldSegment(out, row, position, courseDeg) {
@@ -89,7 +146,7 @@ export function writeHoldSegment(out, row, position, courseDeg) {
* The course runs from `course0Deg` by `courseSlopeDeg` over the segment.
* @param {Float32Array} out
* @param {number} row
* @param {{from: Cesium.Cartesian3, to: Cesium.Cartesian3, t0: number, t1: number,
* @param {{from: object, to: object, t0: number, t1: number,
* course0Deg: number, courseSlopeDeg: number}} segment
*/
export function writeLerpSegment(out, row, segment) {
@@ -113,7 +170,7 @@ export function writeLerpSegment(out, row, segment) {
* `climbMps` (default 0).
* @param {Float32Array} out
* @param {number} row
* @param {{fix: Cesium.Cartesian3, t0: number, minDt: number, maxDt: number,
* @param {{fix: object, t0: number, minDt: number, maxDt: number,
* speedMps: number, trackDeg: number, turnRateDps: number,
* climbMps?: number}} segment
*/
@@ -122,14 +179,7 @@ export function writeArcSegment(out, row, segment) {
const { fix } = segment;
writeBase(out, o, fix);
clearMotion(out, o);
Cesium.Transforms.eastNorthUpToFixedFrame(fix, Cesium.Ellipsoid.WGS84, _enu);
// Column-major: columns 0 and 1 are the east and north unit vectors.
out[o + AXIS_U] = _enu[0];
out[o + AXIS_U + 1] = _enu[1];
out[o + AXIS_U + 2] = _enu[2];
out[o + AXIS_V] = _enu[4];
out[o + AXIS_V + 1] = _enu[5];
out[o + AXIS_V + 2] = _enu[6];
writeEastNorth(out, o + AXIS_U, o + AXIS_V, fix);
const omega = (segment.turnRateDps || 0) * DEG;
out[o + KIND] = SEGMENT_ARC;
out[o + T0] = segment.t0;
@@ -252,15 +302,10 @@ function targetCourse(seg, o, tau) {
* @param {Float32Array} seg
* @param {number} row
* @param {number} tau
* @param {Cesium.Cartesian3} [result]
* @returns {Cesium.Cartesian3}
* @param {{x: number, y: number, z: number}} [result]
* @returns {{x: number, y: number, z: number}}
*/
export function segmentPosition(
seg,
row,
tau,
result = new Cesium.Cartesian3(),
) {
export function segmentPosition(seg, row, tau, result = { x: 0, y: 0, z: 0 }) {
const o = row * SEGMENT_STRIDE;
let x = seg[o + BASE_HIGH] + seg[o + BASE_LOW];
let y = seg[o + BASE_HIGH + 1] + seg[o + BASE_LOW + 1];
+35
View File
@@ -296,3 +296,38 @@ test('an arc written without a climb stays in its tangent plane', () => {
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}`);
}
});
+53 -33
View File
@@ -13,8 +13,9 @@
* {@link rebase} once a frame so they stay small (sub-millisecond steps) however
* long the session runs.
*/
import * as Cesium from 'cesium';
import { WGS84 } from '../geo/ellipsoid.js';
import { HORIZON_LIFT_M } from '../geo/horizon.js';
import { cloneJulian, julianNow, secondsDifference } from '../geo/julian.js';
import {
SEGMENT_STRIDE,
segmentCourse,
@@ -145,8 +146,14 @@ export class DirtyRows {
}
}
/** A 0..1 colour channel as a byte, as the default renderer's `Color.floatToByte`. */
const floatToByte = (value) => (value === 1.0 ? 255.0 : (value * 256.0) | 0);
/**
* @param {{ capacity?: number, epoch?: Cesium.JulianDate }} [options]
* @param {{ capacity?: number,
* epoch?: {dayNumber: number, secondsOfDay: number} }} [options] `epoch`
* is a Julian date (`geo/julian.js`, or the renderer's own, which is the
* same numbers).
*/
export function createIconSet({ capacity = 1024, epoch } = {}) {
capacity = Math.max(1, capacity);
@@ -160,7 +167,7 @@ export function createIconSet({ capacity = 1024, epoch } = {}) {
// CPU only: until when (seconds since the epoch) the row's segment holds.
let expires = new Float64Array(capacity);
let count = 0;
const epochTime = Cesium.JulianDate.clone(epoch ?? Cesium.JulianDate.now());
const epochTime = cloneJulian(epoch ?? julianNow(), {});
const set = {
/** Bumped when the columns are reallocated; the renderer rebuilds its buffers. */
@@ -195,7 +202,7 @@ export function createIconSet({ capacity = 1024, epoch } = {}) {
/** Seconds since the set's epoch: the clock segments are written in. */
seconds(julianDate) {
return Cesium.JulianDate.secondsDifference(julianDate, epochTime);
return secondsDifference(julianDate, epochTime);
},
/**
@@ -211,7 +218,7 @@ export function createIconSet({ capacity = 1024, epoch } = {}) {
shiftSegmentTimes(segments, row, -shift);
expires[row] -= shift;
}
Cesium.JulianDate.clone(julianDate, epochTime);
cloneJulian(julianDate, epochTime);
set.dirty.segments.markAll();
return true;
},
@@ -442,7 +449,10 @@ export function createIconSet({ capacity = 1024, epoch } = {}) {
return style[row * STYLE_STRIDE + DEPTH_TEST_DISTANCE];
},
/** Size by camera distance, as Cesium's NearFarScalar (unset: 1 everywhere). */
/**
* Size by camera distance, as a `{near, nearValue, far, farValue}` curve
* (the default renderer's NearFarScalar; unset: 1 everywhere).
*/
setScaleByDistance(row, scalar) {
const o = row * STYLE_STRIDE;
const near = scalar?.near ?? 0;
@@ -462,7 +472,10 @@ export function createIconSet({ capacity = 1024, epoch } = {}) {
style[o + FAR_VALUE] = farValue;
set.dirty.style.mark(row);
},
scaleByDistance(row, result = new Cesium.NearFarScalar()) {
scaleByDistance(
row,
result = { near: 0, nearValue: 0, far: 0, farValue: 0 },
) {
const o = row * STYLE_STRIDE;
result.near = style[o + NEAR_DISTANCE];
result.nearValue = style[o + NEAR_VALUE];
@@ -471,13 +484,16 @@ export function createIconSet({ capacity = 1024, epoch } = {}) {
return result;
},
/** Tint as a Cesium.Color; its alpha multiplies the row's own alpha. */
/**
* Tint as `{red, green, blue, alpha}` (0..1, the default renderer's
* Color); its alpha multiplies the row's own alpha.
*/
setColor(row, color) {
const o = row * 4;
const r = Cesium.Color.floatToByte(color.red);
const g = Cesium.Color.floatToByte(color.green);
const b = Cesium.Color.floatToByte(color.blue);
const a = Cesium.Color.floatToByte(color.alpha);
const r = floatToByte(color.red);
const g = floatToByte(color.green);
const b = floatToByte(color.blue);
const a = floatToByte(color.alpha);
if (
colors[o] === r &&
colors[o + 1] === g &&
@@ -491,15 +507,14 @@ export function createIconSet({ capacity = 1024, epoch } = {}) {
colors[o + 3] = a;
set.dirty.colors.mark(row);
},
color(row, result = new Cesium.Color()) {
/** The row's tint as `{red, green, blue, alpha}`, 0..1. */
color(row, result = { red: 0, green: 0, blue: 0, alpha: 0 }) {
const o = row * 4;
return Cesium.Color.fromBytes(
colors[o],
colors[o + 1],
colors[o + 2],
colors[o + 3],
result,
);
result.red = colors[o] / 255.0;
result.green = colors[o + 1] / 255.0;
result.blue = colors[o + 2] / 255.0;
result.alpha = colors[o + 3] / 255.0;
return result;
},
/** Where the row is at `tau` (seconds since the epoch). */
@@ -511,22 +526,27 @@ export function createIconSet({ capacity = 1024, epoch } = {}) {
return segmentCourse(segments, row, tau);
},
/** The point the horizon test judges for a row drawn at `position`. */
cullPoint(row, position, result = new Cesium.Cartesian3()) {
if (!(style[row * STYLE_STRIDE + FLAGS] & ICON_CULL_LIFT))
return Cesium.Cartesian3.clone(position, result);
cullPoint(row, position, result = { x: 0, y: 0, z: 0 }) {
const { x, y, z } = position;
if (!(style[row * STYLE_STRIDE + FLAGS] & ICON_CULL_LIFT)) {
result.x = x;
result.y = y;
result.z = z;
return result;
}
// Height above the ellipsoid along the geocentric ray, as the shader
// computes it: exact to millimetres this close to the surface.
const radius = Cesium.Cartesian3.magnitude(position);
Cesium.Cartesian3.multiplyComponents(
position,
Cesium.Ellipsoid.WGS84.oneOverRadii,
result,
);
const height = radius * (1 - 1 / Cesium.Cartesian3.magnitude(result));
const o = WGS84.oneOverRadii;
const radius = Math.sqrt(x * x + y * y + z * z);
const sx = x * o.x;
const sy = y * o.y;
const sz = z * o.z;
const height = radius * (1 - 1 / Math.sqrt(sx * sx + sy * sy + sz * sz));
const lift = Math.max(0, ICON_CULL_HEIGHT_M - height);
Cesium.Cartesian3.normalize(position, result);
Cesium.Cartesian3.multiplyByScalar(result, lift, result);
return Cesium.Cartesian3.add(position, result, result);
result.x = x + (x / radius) * lift;
result.y = y + (y / radius) * lift;
result.z = z + (z / radius) * lift;
return result;
},
};
+38
View File
@@ -372,3 +372,41 @@ test('a horizon-culled draw set lifts what the aircraft layers lift, and lifts t
assert.ok(judged > aircraft(h, d), 'judged higher');
}
});
test('colours and cull points read as the default renderer computes them', () => {
const set = createIconSet({ capacity: 2 });
const row = set.add('a');
for (const css of ['#FFB800', '#00000080', 'rgba(12, 200, 99, 0.37)']) {
const color = Cesium.Color.fromCssColorString(css);
set.setColor(row, color);
const want = Cesium.Color.fromBytes(
Cesium.Color.floatToByte(color.red),
Cesium.Color.floatToByte(color.green),
Cesium.Color.floatToByte(color.blue),
Cesium.Color.floatToByte(color.alpha),
);
assert.ok(Cesium.Color.equals(set.color(row), want), css);
}
set.setFlag(row, ICON_CULL_LIFT, true);
for (const h of [-31, 0, 5, 11.9]) {
const p = Cesium.Cartesian3.fromDegrees(-84.43, 33.64, h);
const scaled = Cesium.Cartesian3.multiplyComponents(
p,
Cesium.Ellipsoid.WGS84.oneOverRadii,
new Cesium.Cartesian3(),
);
const radius = Cesium.Cartesian3.magnitude(p);
const height = radius * (1 - 1 / Cesium.Cartesian3.magnitude(scaled));
const lift = Math.max(0, ICON_CULL_HEIGHT_M - height);
const want = Cesium.Cartesian3.add(
p,
Cesium.Cartesian3.multiplyByScalar(
Cesium.Cartesian3.normalize(p, new Cesium.Cartesian3()),
lift,
new Cesium.Cartesian3(),
),
new Cesium.Cartesian3(),
);
assert.ok(Cesium.Cartesian3.equals(set.cullPoint(row, p), want), `h=${h}`);
}
});
-2
View File
@@ -12,8 +12,6 @@
"src/data/focusDeemphasis.js",
"src/data/geoImportPins.js",
"src/data/iconOrientation.js",
"src/data/iconSegment.js",
"src/data/iconSet.js",
"src/input/dispatcher.js",
"src/input/sensorPick.js",
"src/layers/cctv/projection.js",