Draw the traffic dots through the draw port and read the road surface through renderer ports

The traffic layer's vehicle dots are now a points set on the draw port,
moved, added to and taken from in place each frame, instead of a Cesium
point collection. Road surface heights and readiness read the query,
camera, frame and globe ports and the heights service instead of the
scene; the surface identity and "any tileset drawn" checks, which the
contract cannot answer, move to rendering.js with the heat lines.

animation.js, style.js and surface.js leave the Cesium allow-list.

The Cesium draw port compares a moved point's look without allocating:
colours by index, and a frozen colour or curve of plain data by identity,
so a thousand dots moved every frame cost about what direct writes did.
The traffic QA scripts read the dots' draw items.
This commit is contained in:
Sameh Khamis committed 2026-11-07 23:16:11 -08:00
1 parent d9db5199ed
commit bbd6049e21
24 files changed
+872 -467

No files matched your search

+73 -14
View File
@@ -1049,8 +1049,31 @@ export function createCesiumDraw({
'displayRange',
];
/**
* What a point's look keeps of an item member: a frozen value of plain
* members (a frozen colour or curve) as it is, since it cannot change and
* is then told unchanged by identity; anything else as a `plainCopy`.
*/
function lookCopy(value) {
if (!value || typeof value !== 'object' || !Object.isFrozen(value))
return plainCopy(value);
// Only plain data cannot change: no getters, nothing inherited.
const proto = Object.getPrototypeOf(value);
if (proto !== Object.prototype && proto !== Array.prototype)
return plainCopy(value);
for (const key of Reflect.ownKeys(value)) {
const member = Object.getOwnPropertyDescriptor(value, key);
if (
!('value' in member) ||
(member.value !== null && typeof member.value === 'object')
)
return plainCopy(value);
}
return value;
}
/** A copy of the item members a point's look is drawn from. */
const pointLookOf = (item) => LOOK_FROM.map((key) => plainCopy(item[key]));
const pointLookOf = (item) => LOOK_FROM.map((key) => lookCopy(item[key]));
/**
* Whether `value` holds what `kept` (a `plainCopy` of a colour, curve or
@@ -1067,6 +1090,13 @@ export function createCesiumDraw({
)
return false;
if (Array.isArray(kept) !== Array.isArray(value)) return false;
if (Array.isArray(value)) {
// Colours, read by index: a `for...in` over an array is far slower.
if (kept.length !== value.length) return false;
for (let i = 0; i < value.length; i++)
if (!Object.is(kept[i], value[i])) return false;
return true;
}
let count = 0;
for (const key in value) {
if (value[key] === undefined) continue;
@@ -1083,28 +1113,57 @@ export function createCesiumDraw({
const value = item[LOOK_FROM[i]];
if (sameLookValue(was.look[i], value)) continue;
was.primitive[LOOK[i][0]] = LOOK[i][1](item);
was.look[i] = plainCopy(value);
was.look[i] = lookCopy(value);
}
}
const PLACE_KEYS = ['x', 'y', 'z', 'lon', 'lat', 'height'];
/**
* Where an item is, as its values when drawn: a caller may change the
* position object it passes and send the same one again.
*/
const placeOf = (item) =>
PLACE_KEYS.map((key) => item.pos?.[key]).concat(item.height);
const placeOf = (item) => {
const pos = item.pos;
return [
pos?.x,
pos?.y,
pos?.z,
pos?.lon,
pos?.lat,
pos?.height,
item.height,
];
};
/** Whether an item is somewhere else than `at`; if so, `at` takes it. */
/**
* Whether an item is somewhere else than `at`; if so, `at` takes it. Read
* member by member, by name: points moved every frame are compared every
* frame.
*/
function movedFrom(at, item) {
let moved = !Object.is(item.height, at[PLACE_KEYS.length]);
for (let i = 0; !moved && i < PLACE_KEYS.length; i++)
moved = !Object.is(item.pos?.[PLACE_KEYS[i]], at[i]);
if (!moved) return false;
for (let i = 0; i < PLACE_KEYS.length; i++)
at[i] = item.pos?.[PLACE_KEYS[i]];
at[PLACE_KEYS.length] = item.height;
const pos = item.pos;
const x = pos?.x;
const y = pos?.y;
const z = pos?.z;
const lon = pos?.lon;
const lat = pos?.lat;
const height = pos?.height;
if (
Object.is(x, at[0]) &&
Object.is(y, at[1]) &&
Object.is(z, at[2]) &&
Object.is(lon, at[3]) &&
Object.is(lat, at[4]) &&
Object.is(height, at[5]) &&
Object.is(item.height, at[6])
)
return false;
at[0] = x;
at[1] = y;
at[2] = z;
at[3] = lon;
at[4] = lat;
at[5] = height;
at[6] = item.height;
return true;
}
+150
View File
@@ -912,6 +912,127 @@ test('the bikeshare layer draws its stations as point primitives on a viewer of
assert.equal(scene.added.length, 0);
});
test('the traffic layer draws its dots as one point collection moved, added to and taken from in place on a viewer of its own', async (t) => {
const { createState } = await import('../../layers/traffic/state.js');
const { createStyle } = await import('../../layers/traffic/style.js');
const { createModel } = await import('../../layers/traffic/model.js');
const { createAnimation } = await import('../../layers/traffic/animation.js');
const { JAM_DOT_FAR_SCALE } = await import('../../layers/traffic/policy.js');
let now = 10000;
t.mock.method(Date, 'now', () => now);
const { scene, draw } = drawOver();
const state = createState({ services: {} });
state._enabled = true;
state._fadeScaleFar = 8000;
state._fadeTransFar = 10000;
state._dotSet = draw.points({ id: 'traffic:1:dots', blend: 'translucent' });
const parts = {
rendering: { removeHeatLines() {}, rebuildHeatLines() {} },
retention: { easeHeights() {}, finishHeights() {}, clear() {} },
};
const context = {
state,
services: {
render: { holdContinuousRender() {}, releaseContinuousRender() {} },
},
parts,
source: {},
};
parts.style = createStyle(context);
parts.model = createModel(context);
parts.animation = createAnimation(context);
parts.style.refreshBucketColors();
const [road] = parts.model.parseRoads({
roads: [
{
type: 'primary',
oneway: 1,
coordinates: [
[-97.74, 30.26],
[-97.737, 30.26],
],
},
],
});
parts.animation.spawnDotsForRoad(road, 450, 3);
const host = scene.added[0];
assert.ok(host instanceof Cesium.PointPrimitiveCollection);
assert.equal(host.blendOption, Cesium.BlendOption.TRANSLUCENT);
assert.equal(host.length, 3, 'drawn as they join');
const point = host.get(0);
assert.equal(point.id.set, 'traffic:1:dots');
assert.ok(
Cesium.Color.equals(point.color, Cesium.Color.WHITE.withAlpha(0.85)),
'the simulated white',
);
assert.equal(point.disableDepthTestDistance, 0);
const before = Cesium.Cartesian3.clone(point.position);
now += 500;
parts.animation.animate();
assert.equal(scene.added[0], host, 'moved where it is');
assert.equal(host.get(0), point);
assert.ok(!Cesium.Cartesian3.equals(point.position, before), 'it moves');
assert.ok(
Cesium.NearFarScalar.equals(
point.scaleByDistance,
new Cesium.NearFarScalar(100, 1.5, 8000, 0.3),
),
);
assert.ok(
Cesium.NearFarScalar.equals(
point.translucencyByDistance,
new Cesium.NearFarScalar(100, 1, 10000, 0),
),
);
// What the style part changes on a dot reaches its point at the next
// frame, on the point it has.
const dot = state._dots[0];
parts.style.paintDot(dot, 'jam');
parts.style.emphasizeJamDot(dot);
dot.point.outlineColor = Object.freeze([0, 0, 0, 0.5]);
dot.point.outlineWidth = 2;
now += 16;
parts.animation.animate();
assert.equal(host.get(0), point);
assert.ok(
Cesium.Color.equals(
point.color,
new Cesium.Color(...state._activeBucketColors.jam),
),
'painted',
);
assert.ok(
Cesium.NearFarScalar.equals(
point.scaleByDistance,
new Cesium.NearFarScalar(100, 1.5, 8000, JAM_DOT_FAR_SCALE),
),
'emphasised',
);
assert.ok(
Cesium.Color.equals(point.outlineColor, new Cesium.Color(0, 0, 0, 0.5)),
);
assert.equal(point.outlineWidth, 2);
parts.style.showDot(dot, false);
now += 16;
parts.animation.animate();
assert.equal(point.show, false, 'hidden');
parts.style.showDot(dot, true);
now += 16;
parts.animation.animate();
assert.equal(point.show, true);
parts.animation.spawnDotsForRoad(road, 450, 2);
assert.equal(scene.added[0], host, 'joined where it is');
assert.equal(host.length, 5);
// A dot that has faded out leaves the collection where it is, as the
// retention part takes it.
state._dotSet.remove([state._dots[0].point.id]);
assert.equal(scene.added[0], host);
assert.equal(state._dotSet.size(), 4);
parts.animation.clearDots();
state._dotSet.dispose();
assert.equal(scene.added.length, 0);
});
test('a rebuilt set keeps its place among the scene primitives, and names its host', async () => {
const {
registerSpriteSet,
@@ -1186,6 +1307,35 @@ test('a point moved in place sets only the members of its look that changed', ()
item.color[1] = 1;
set.upsert([item]);
assert.deepEqual(written, ['color', 'scaleByDistance', 'color']);
// A frozen colour or curve is kept as it is, and told unchanged by
// identity; an equal one sent anew still sets nothing.
item.color = Object.freeze([0, 0, 1, 1]);
set.upsert([item]);
set.upsert([item]);
item.color = Object.freeze([0, 0, 1, 1]);
set.upsert([item]);
assert.deepEqual(written, ['color', 'scaleByDistance', 'color', 'color']);
// A frozen curve read through a getter can change, so it is copied and
// compared by value.
let farValue = 0.3;
item.scaleByDistance = Object.freeze(
Object.defineProperties(
{},
{
near: { value: 100, enumerable: true },
nearValue: { value: 1.5, enumerable: true },
far: { value: 12000, enumerable: true },
farValue: { get: () => farValue, enumerable: true },
},
),
);
set.upsert([item]);
const writes = written.length;
set.upsert([item]);
assert.equal(written.length, writes, 'unchanged');
farValue = 0.8;
set.upsert([item]);
assert.equal(written.at(-1), 'scaleByDistance', 'the change is seen');
set.dispose();
});