mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Compute street traffic roads, colours and state without Cesium
Road waypoints are plain ECEF points from the geo helpers (the same formulas as before, so every waypoint and segment length is unchanged), and the palette is renderer colours. The style part, still on the point primitives, turns those into the points' colours once per colour and owns the dot painting the model used to do itself; the rendering part converts them for the heat lines. The animation keeps its own lerp scratch, and the ingestion's centre height asks with plain degrees. Ingestion, model, policy, state and timing leave the allow-list.
This commit is contained in:
1 parent
d6f09f9860
commit
10db177251
15 files changed
+247
-73
No files matched your search
@@ -526,6 +526,7 @@
|
||||
"src/layers/traffic/model.js",
|
||||
"src/layers/traffic/timing.js",
|
||||
"src/layers/traffic/style.js",
|
||||
"src/layers/traffic/style.test.mjs",
|
||||
"src/layers/bikeshare/lifecycle.js",
|
||||
"src/layers/bikeshare/viewport.js",
|
||||
"src/layers/bikeshare/registry.js",
|
||||
|
||||
@@ -2175,7 +2175,10 @@
|
||||
"src/sources/requestDeadline.js",
|
||||
"src/layers/traffic/lifecycle.js",
|
||||
"src/layers/traffic/model.js",
|
||||
"src/geo/ellipsoid.js",
|
||||
"src/geo/vector.js",
|
||||
"src/layers/traffic/policy.js",
|
||||
"src/render/color.js",
|
||||
"src/layers/traffic/rendering.js",
|
||||
"src/layers/traffic/retention.js",
|
||||
"src/layers/traffic/source.js",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* @file Queue-spawn helpers for jammed roads — platoon position math only.
|
||||
* Cesium-free so bumper-to-bumper spacing is unit-testable; the traffic
|
||||
* layer maps the returned distances onto Cartesian3 waypoints at spawn time.
|
||||
* layer maps the returned distances onto ECEF waypoints at spawn time.
|
||||
*
|
||||
* Only ever used for live-flow jam-bucket roads (jamViz 'density'/'both');
|
||||
* the keyless simulation never calls into this module.
|
||||
|
||||
@@ -20,6 +20,9 @@ export function createAnimation({
|
||||
parts,
|
||||
source,
|
||||
}) {
|
||||
/** Scratch point for the per-frame lerp, so a frame allocates nothing. */
|
||||
const scratchLerp = new Cesium.Cartesian3();
|
||||
|
||||
function syncRenderDemand() {
|
||||
const needed = Boolean(
|
||||
layerState._enabled &&
|
||||
@@ -44,7 +47,7 @@ export function createAnimation({
|
||||
* randomized speed (base +/-30%), and given a direction (alternating
|
||||
* forward/backward to simulate two-way traffic).
|
||||
*
|
||||
* @param {{waypoints:Cesium.Cartesian3[], segmentDist:number[], type:string, coords:number[][]}} road
|
||||
* @param {{waypoints:Array<{x:number, y:number, z:number}>, segmentDist:number[], type:string, coords:number[][]}} road
|
||||
* Parsed road object with pre-computed waypoints.
|
||||
* @param {number} altitude - Camera altitude (used if budgetCount is null).
|
||||
* @param {number|null} [budgetCount=null] - Pre-allocated dot count. Falls back
|
||||
@@ -74,7 +77,6 @@ export function createAnimation({
|
||||
parts.style.baseDotSize(road.type, bucket) +
|
||||
(bucket === 'jam' ? 1 : 0) +
|
||||
parts.style.activeSizeDelta(bucket);
|
||||
const flowColor = bucket ? layerState._activeBucketColors[bucket] : null;
|
||||
// Dark-halo outline under styled presets (null = shipped no-outline).
|
||||
const outlineSpec =
|
||||
bucket && layerState._presetDots === 'on'
|
||||
@@ -143,7 +145,7 @@ export function createAnimation({
|
||||
road.waypoints[segIdx],
|
||||
road.waypoints[segIdx + 1],
|
||||
t,
|
||||
layerState._scratchLerp,
|
||||
scratchLerp,
|
||||
);
|
||||
|
||||
// Grounded traffic respects building/terrain occlusion. The former
|
||||
@@ -151,10 +153,10 @@ export function createAnimation({
|
||||
const jamProminent = bucket === 'jam' && parts.style.jamDensityOn();
|
||||
const id = layerState._nextDotId++;
|
||||
const point = layerState._pointCollection.add({
|
||||
position: Cesium.Cartesian3.clone(layerState._scratchLerp),
|
||||
position: Cesium.Cartesian3.clone(scratchLerp),
|
||||
pixelSize,
|
||||
// No flow data → today's exact simulated white.
|
||||
color: flowColor || Cesium.Color.WHITE.withAlpha(0.85),
|
||||
color: parts.style.dotColor(bucket),
|
||||
scaleByDistance: new Cesium.NearFarScalar(
|
||||
100,
|
||||
1.5,
|
||||
@@ -312,8 +314,8 @@ export function createAnimation({
|
||||
// garbage across 6000 dots. (perf item 5)
|
||||
const a = dot.waypoints[dot.segIdx];
|
||||
const b = dot.waypoints[dot.segIdx + 1];
|
||||
Cesium.Cartesian3.lerp(a, b, dot.t, layerState._scratchLerp);
|
||||
dot.point.position = layerState._scratchLerp;
|
||||
Cesium.Cartesian3.lerp(a, b, dot.t, scratchLerp);
|
||||
dot.point.position = scratchLerp;
|
||||
}
|
||||
|
||||
// Jam heat-lines throb (~1.6 s period) — one shared material uniform for
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import { requestWithDeadline } from '../../sources/requestDeadline.js';
|
||||
import { resolveRoadMode, ROAD_SOURCE_LABELS } from './roadModes.js';
|
||||
import { phaseTiming } from '../../sources/phaseTiming.js';
|
||||
@@ -272,7 +271,7 @@ export function createIngestion({
|
||||
const centerLat = (clamped.south + clamped.north) / 2;
|
||||
const centerLon = (clamped.west + clamped.east) / 2;
|
||||
const scene = layerState._viewer?.scene;
|
||||
const location = Cesium.Cartographic.fromDegrees(centerLon, centerLat);
|
||||
const location = { lon: centerLon, lat: centerLat };
|
||||
let estimated = services.ground?.cachedGroundFloor?.(centerLat, centerLon);
|
||||
// A miss is the local mesh still loading.
|
||||
if (!Number.isFinite(estimated) && scene) {
|
||||
|
||||
+10
-22
@@ -1,4 +1,5 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import { fromDegrees } from '../../geo/ellipsoid.js';
|
||||
import { distance } from '../../geo/vector.js';
|
||||
import { roadSurfaceChunks } from './surface.js';
|
||||
import { t, translateKnown } from '../../i18n/index.js';
|
||||
import {
|
||||
@@ -6,11 +7,7 @@ import {
|
||||
flowBucket,
|
||||
flowSpeedScale,
|
||||
} from '../../data/trafficFlowStyle.js';
|
||||
import {
|
||||
DOT_HEIGHT_OFFSET,
|
||||
DENSITY_MULT,
|
||||
JAM_DOT_FAR_SCALE,
|
||||
} from './policy.js';
|
||||
import { DOT_HEIGHT_OFFSET, DENSITY_MULT } from './policy.js';
|
||||
|
||||
export function createModel({ state: layerState, services, parts, source }) {
|
||||
/** Build scene waypoints from source records; thinning and terrain remain rendering policy. */
|
||||
@@ -26,22 +23,20 @@ export function createModel({ state: layerState, services, parts, source }) {
|
||||
for (const coords of roadSurfaceChunks(road.coordinates)) {
|
||||
const type = road.type;
|
||||
const oneway = road.oneway;
|
||||
// Pre-compute Cartesian3 waypoints (lon, lat, height) for fast lerp animation
|
||||
// Pre-compute ECEF waypoints (lon, lat, height) for fast lerp animation
|
||||
const waypoints = coords.map(([lng, lat]) => {
|
||||
const floor = services.ground?.cachedGroundFloor?.(lat, lng);
|
||||
const h =
|
||||
(Number.isFinite(floor) && Math.abs(floor) <= 9000
|
||||
? floor
|
||||
: layerState._viewHeightEstimate || 0) + DOT_HEIGHT_OFFSET;
|
||||
return Cesium.Cartesian3.fromDegrees(lng, lat, h);
|
||||
return fromDegrees(lng, lat, h);
|
||||
});
|
||||
|
||||
// Pre-compute segment distances in meters for speed-to-t conversion
|
||||
const segmentDist = [];
|
||||
for (let i = 0; i < waypoints.length - 1; i++) {
|
||||
segmentDist.push(
|
||||
Cesium.Cartesian3.distance(waypoints[i], waypoints[i + 1]),
|
||||
);
|
||||
segmentDist.push(distance(waypoints[i], waypoints[i + 1]));
|
||||
}
|
||||
|
||||
for (const direction of oneway ? [oneway] : [1, -1])
|
||||
@@ -357,17 +352,15 @@ export function createModel({ state: layerState, services, parts, source }) {
|
||||
const flow = dot.road?.source ? dot.road.source.flow : dot.road?.flow;
|
||||
dot.road.flow = flow;
|
||||
if (flow?.closure || (!flow && layerState._uncoveredMode === 'hide')) {
|
||||
dot.point.show = false;
|
||||
parts.style.showDot(dot, false);
|
||||
dot.mps = 0;
|
||||
closedDots += 1;
|
||||
continue;
|
||||
}
|
||||
dot.point.show = true;
|
||||
parts.style.showDot(dot, true);
|
||||
const bucket = flow ? flowBucket(flow.level) : null;
|
||||
dot.bucket = bucket;
|
||||
dot.point.color = bucket
|
||||
? layerState._activeBucketColors[bucket]
|
||||
: Cesium.Color.WHITE.withAlpha(0.85);
|
||||
parts.style.paintDot(dot, bucket);
|
||||
dot.nominalSize =
|
||||
parts.style.baseDotSize(dot.road?.type, bucket) +
|
||||
(bucket === 'jam' ? 1 : 0) +
|
||||
@@ -386,12 +379,7 @@ export function createModel({ state: layerState, services, parts, source }) {
|
||||
moving: Math.random() < 0.4,
|
||||
until: now + Math.random() * 2000,
|
||||
};
|
||||
dot.point.scaleByDistance = new Cesium.NearFarScalar(
|
||||
100,
|
||||
1.5,
|
||||
layerState._fadeScaleFar,
|
||||
JAM_DOT_FAR_SCALE,
|
||||
);
|
||||
parts.style.emphasizeJamDot(dot);
|
||||
} else {
|
||||
dot.creep = null;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import { createState } from './state.js';
|
||||
import { createStyle } from './style.js';
|
||||
import { createModel } from './model.js';
|
||||
import { createAnimation } from './animation.js';
|
||||
import { DOT_HEIGHT_OFFSET } from './policy.js';
|
||||
import {
|
||||
createRetention,
|
||||
DOT_CHANGE_BUDGET,
|
||||
@@ -671,3 +672,39 @@ test('retained road density follows the settled view without requiring a toggle'
|
||||
'fresh toggle and retained view have the same budget',
|
||||
);
|
||||
});
|
||||
|
||||
test('parsed roads carry plain ECEF waypoints that dots spawn on and move along', (t) => {
|
||||
const h = setup(t);
|
||||
const [road] = h.parts.model.parseRoads({
|
||||
roads: [
|
||||
{
|
||||
type: 'primary',
|
||||
oneway: 1,
|
||||
coordinates: [
|
||||
[0, 0],
|
||||
[0.003, 0],
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
assert.ok(!(road.waypoints[0] instanceof C.Cartesian3), 'plain points');
|
||||
const expected = C.Cartesian3.fromDegrees(
|
||||
...road.coords[1],
|
||||
DOT_HEIGHT_OFFSET,
|
||||
);
|
||||
assert.deepEqual({ ...road.waypoints[1] }, { ...expected });
|
||||
for (let i = 0; i < road.segmentDist.length; i++)
|
||||
assert.equal(
|
||||
road.segmentDist[i],
|
||||
C.Cartesian3.distance(road.waypoints[i], road.waypoints[i + 1]),
|
||||
);
|
||||
h.parts.retention.reconcile([road], 450, true);
|
||||
h.settle();
|
||||
assert.ok(h.state._dots.length > 0);
|
||||
const dot = h.state._dots[0];
|
||||
const before = C.Cartesian3.clone(dot.point.position);
|
||||
h.frame(500);
|
||||
const after = dot.point.position;
|
||||
assert.ok(Number.isFinite(after.x) && Number.isFinite(after.z));
|
||||
assert.ok(C.Cartesian3.distance(before, after) > 0, 'the dot moves');
|
||||
});
|
||||
@@ -1,9 +1,9 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import { hexColor } from '../../render/color.js';
|
||||
|
||||
/**
|
||||
* @file Street Traffic selects TomTom, OpenMapTiles or Hybrid motor roads,
|
||||
* with directional TomTom congestion in live mode. Tile acquisition is
|
||||
* bounded and camera-driven; animation lerps precomputed Cartesian waypoints.
|
||||
* bounded and camera-driven; animation lerps precomputed ECEF waypoints.
|
||||
*/
|
||||
|
||||
/** @const {number} Meters — hide all traffic dots above this camera altitude */
|
||||
@@ -108,14 +108,20 @@ export const SIZE_BY_TYPE = {
|
||||
* Live-flow bucket colors (thresholds live in `trafficFlowStyle.js`):
|
||||
* green free flow / amber slow / red jam, all at 0.9 alpha.
|
||||
* Roads without flow data (`road.flow == null`) keep the sim's white.
|
||||
* @const {Object<string, Cesium.Color>}
|
||||
* Renderer colours (`[r, g, b, a]`, 0..1), frozen: painted colours are
|
||||
* cached by identity.
|
||||
* @const {Object<string, number[]>}
|
||||
*/
|
||||
|
||||
export const FLOW_BUCKET_COLORS = {
|
||||
free: Cesium.Color.fromCssColorString('#2ecc71').withAlpha(0.9),
|
||||
slow: Cesium.Color.fromCssColorString('#f0b23e').withAlpha(0.9),
|
||||
jam: Cesium.Color.fromCssColorString('#e05252').withAlpha(0.9),
|
||||
};
|
||||
export const FLOW_BUCKET_COLORS = Object.freeze({
|
||||
free: Object.freeze(hexColor('#2ecc71', 0.9)),
|
||||
slow: Object.freeze(hexColor('#f0b23e', 0.9)),
|
||||
jam: Object.freeze(hexColor('#e05252', 0.9)),
|
||||
});
|
||||
|
||||
/** @const {number[]} Simulated (no flow data) dot colour: white at 0.85. */
|
||||
|
||||
export const SIM_DOT_COLOR = Object.freeze([1, 1, 1, 0.85]);
|
||||
|
||||
// ─── Jam-viz prototype (live mode only — see 2026-07-21 design doc) ────────
|
||||
/** @const {number} Max congestion heat-line polylines per render (jam first). */
|
||||
@@ -138,14 +144,13 @@ export const HEAT_JAM_BASE_ALPHA = 0.55;
|
||||
|
||||
export const HEAT_JAM_PULSE_ALPHA = 0.2;
|
||||
|
||||
/** @const {Cesium.Color} Jam corridor color (bucket red, alpha pulsed live). */
|
||||
/** @const {number[]} Jam corridor color (bucket red, alpha pulsed live). */
|
||||
|
||||
export const HEAT_JAM_COLOR = Cesium.Color.fromCssColorString('#e05252');
|
||||
export const HEAT_JAM_COLOR = Object.freeze(hexColor('#e05252'));
|
||||
|
||||
/** @const {Cesium.Color} Slow corridor color (bucket amber, faint + static). */
|
||||
/** @const {number[]} Slow corridor color (bucket amber, faint + static). */
|
||||
|
||||
export const HEAT_SLOW_COLOR =
|
||||
Cesium.Color.fromCssColorString('#f0b23e').withAlpha(0.2);
|
||||
export const HEAT_SLOW_COLOR = Object.freeze(hexColor('#f0b23e', 0.2));
|
||||
|
||||
/** @const {number} Far-distance scale floor for jam dots (shipped: 0.3). */
|
||||
|
||||
|
||||
@@ -106,7 +106,10 @@ export function createRendering({
|
||||
(c) =>
|
||||
new Cesium.GeometryInstance({
|
||||
geometry: new Cesium.GroundPolylineGeometry({
|
||||
positions: c.road.waypoints,
|
||||
// A snapshot of the waypoints, as the renderer's points.
|
||||
positions: c.road.waypoints.map((p) =>
|
||||
Cesium.Cartesian3.clone(p),
|
||||
),
|
||||
width,
|
||||
}),
|
||||
}),
|
||||
@@ -117,10 +120,12 @@ export function createRendering({
|
||||
const monoHeat =
|
||||
layerState._presetDots === 'on' &&
|
||||
trafficStyleProfile(layerState._stylePreset) === 'mono';
|
||||
const jamLineColor = monoHeat ? Cesium.Color.WHITE : HEAT_JAM_COLOR;
|
||||
const jamLineColor = monoHeat
|
||||
? Cesium.Color.WHITE
|
||||
: new Cesium.Color(...HEAT_JAM_COLOR);
|
||||
const slowLineColor = monoHeat
|
||||
? new Cesium.Color(0.7, 0.7, 0.7, HEAT_SLOW_COLOR.alpha)
|
||||
: HEAT_SLOW_COLOR;
|
||||
? new Cesium.Color(0.7, 0.7, 0.7, HEAT_SLOW_COLOR[3])
|
||||
: new Cesium.Color(...HEAT_SLOW_COLOR);
|
||||
|
||||
const jamInstances = instancesFor('jam', HEAT_LINE_JAM_WIDTH);
|
||||
if (jamInstances.length) {
|
||||
|
||||
@@ -436,6 +436,11 @@ async function matchingContext(fetchFlowForBounds) {
|
||||
jamDensityOn: () => false,
|
||||
baseDotSize: () => 2,
|
||||
activeSizeDelta: () => 0,
|
||||
showDot: (dot, show) => {
|
||||
dot.point.show = show;
|
||||
},
|
||||
paintDot() {},
|
||||
emphasizeJamDot() {},
|
||||
},
|
||||
rendering: {
|
||||
rebuildHeatLines() {},
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { normalizeRoadMode } from './roadModes.js';
|
||||
import * as Cesium from 'cesium';
|
||||
import { FLOW_BUCKET_COLORS } from './policy.js';
|
||||
|
||||
export function createState({ services }) {
|
||||
@@ -14,15 +13,15 @@ export function createState({ services }) {
|
||||
layerState._roadModeUrlOverride = layerState._roadMode;
|
||||
|
||||
// ─── Module State ──────────────────────────────────────────
|
||||
/** @type {Cesium.Viewer|null} */
|
||||
/** @type {object|null} The scene's viewer. */
|
||||
|
||||
layerState._viewer = null;
|
||||
|
||||
/** @type {Cesium.PointPrimitiveCollection|null} */
|
||||
/** @type {object|null} The dots' point collection. */
|
||||
|
||||
layerState._pointCollection = null;
|
||||
|
||||
/** @type {Array<{point:Cesium.PointPrimitive, waypoints:Cesium.Cartesian3[], segmentDist:number[], numSegments:number, segIdx:number, t:number, mps:number, direction:number, stoppedUntil:number}>} Active animated dots */
|
||||
/** @type {Array<{point:object, waypoints:Array<{x:number, y:number, z:number}>, segmentDist:number[], numSegments:number, segIdx:number, t:number, mps:number, direction:number, stoppedUntil:number}>} Active animated dots */
|
||||
|
||||
layerState._dots = [];
|
||||
layerState._nextDotId = 1;
|
||||
@@ -34,7 +33,7 @@ export function createState({ services }) {
|
||||
recycled: 0,
|
||||
};
|
||||
|
||||
/** @type {Array<{coords:number[][], type:string, waypoints:Cesium.Cartesian3[], segmentDist:number[]}>} Parsed roads with pre-computed Cartesian3 waypoints */
|
||||
/** @type {Array<{coords:number[][], type:string, waypoints:Array<{x:number, y:number, z:number}>, segmentDist:number[]}>} Parsed roads with pre-computed ECEF waypoints */
|
||||
|
||||
layerState._roads = [];
|
||||
|
||||
@@ -172,12 +171,12 @@ export function createState({ services }) {
|
||||
* rendered 3D tiles (ClassificationType.CESIUM_3D_TILE). One
|
||||
* primitive per bucket so the jam batch can pulse via a single shared
|
||||
* material uniform.
|
||||
* @type {Cesium.GroundPolylinePrimitive|null}
|
||||
* @type {object|null}
|
||||
*/
|
||||
|
||||
layerState._heatJamPrim = null;
|
||||
|
||||
/** @type {Cesium.GroundPolylinePrimitive|null} Slow-bucket heat-line batch. */
|
||||
/** @type {object|null} Slow-bucket heat-line batch. */
|
||||
|
||||
layerState._heatSlowPrim = null;
|
||||
|
||||
@@ -217,7 +216,8 @@ export function createState({ services }) {
|
||||
* Effective per-bucket dot colors: preset override when one applies, else
|
||||
* the shipped FLOW_BUCKET_COLORS. Rebuilt on style/param change only —
|
||||
* spawn/recolor/restyle all read from here, no per-dot allocation.
|
||||
* @type {{free:Cesium.Color, slow:Cesium.Color, jam:Cesium.Color}}
|
||||
* Renderer colours (`[r, g, b, a]`, 0..1).
|
||||
* @type {{free:number[], slow:number[], jam:number[]}}
|
||||
*/
|
||||
|
||||
layerState._activeBucketColors = { ...FLOW_BUCKET_COLORS };
|
||||
@@ -288,10 +288,6 @@ export function createState({ services }) {
|
||||
|
||||
layerState._tileCache = new Map();
|
||||
|
||||
/** Reusable scratch Cartesian3 to avoid per-frame allocation / GC pressure */
|
||||
|
||||
layerState._scratchLerp = new Cesium.Cartesian3();
|
||||
|
||||
// ─── Animation ─────────────────────────────────────────────
|
||||
|
||||
/** @type {number} Timestamp of the last animation tick (ms) */
|
||||
|
||||
@@ -9,9 +9,52 @@ import {
|
||||
SIZE_BY_TYPE,
|
||||
STYLED_MIN_BASE_PX,
|
||||
FLOW_BUCKET_COLORS,
|
||||
JAM_DOT_FAR_SCALE,
|
||||
SIM_DOT_COLOR,
|
||||
} from './policy.js';
|
||||
|
||||
export function createStyle({ state: layerState, services, parts, source }) {
|
||||
// The dots' renderer colours as the point primitives take them, one per
|
||||
// colour, so painting a dot allocates nothing. Keyed by identity: the
|
||||
// colours are frozen, and a preset change makes new ones.
|
||||
const pointColors = new WeakMap();
|
||||
|
||||
/** A renderer colour (`[r, g, b, a]`) as a point primitive colour. */
|
||||
function pointColor(rgba) {
|
||||
let color = pointColors.get(rgba);
|
||||
if (!color) {
|
||||
color = new Cesium.Color(rgba[0], rgba[1], rgba[2], rgba[3]);
|
||||
pointColors.set(rgba, color);
|
||||
}
|
||||
return color;
|
||||
}
|
||||
|
||||
/** The point colour of a dot in `bucket` (null: simulated white). */
|
||||
function dotColor(bucket) {
|
||||
return pointColor(
|
||||
bucket ? layerState._activeBucketColors[bucket] : SIM_DOT_COLOR,
|
||||
);
|
||||
}
|
||||
|
||||
/** Paint a dot's point in its bucket's colour, as a late flow update does. */
|
||||
function paintDot(dot, bucket) {
|
||||
dot.point.color = dotColor(bucket);
|
||||
}
|
||||
|
||||
/** Show or hide a dot's point. */
|
||||
function showDot(dot, show) {
|
||||
dot.point.show = show;
|
||||
}
|
||||
|
||||
/** Keep a prominent jam dot larger at range. */
|
||||
function emphasizeJamDot(dot) {
|
||||
dot.point.scaleByDistance = new Cesium.NearFarScalar(
|
||||
100,
|
||||
1.5,
|
||||
layerState._fadeScaleFar,
|
||||
JAM_DOT_FAR_SCALE,
|
||||
);
|
||||
}
|
||||
/** @returns {boolean} A non-normal preset profile is active and enabled. */
|
||||
|
||||
function presetProfileActive() {
|
||||
@@ -58,7 +101,7 @@ export function createStyle({ state: layerState, services, parts, source }) {
|
||||
? presetDotRgba(layerState._stylePreset, bucket)
|
||||
: null;
|
||||
layerState._activeBucketColors[bucket] = rgba
|
||||
? new Cesium.Color(rgba[0] / 255, rgba[1] / 255, rgba[2] / 255, rgba[3])
|
||||
? Object.freeze([rgba[0] / 255, rgba[1] / 255, rgba[2] / 255, rgba[3]])
|
||||
: FLOW_BUCKET_COLORS[bucket];
|
||||
}
|
||||
}
|
||||
@@ -103,7 +146,7 @@ export function createStyle({ state: layerState, services, parts, source }) {
|
||||
for (const dot of layerState._dots) {
|
||||
const bucket = dot.bucket;
|
||||
if (!bucket) continue; // sim/uncovered dots stay byte-identical
|
||||
dot.point.color = layerState._activeBucketColors[bucket];
|
||||
dot.point.color = dotColor(bucket);
|
||||
dot.nominalSize = dot.point.pixelSize =
|
||||
baseDotSize(dot.road?.type, bucket) +
|
||||
(bucket === 'jam' ? 1 : 0) +
|
||||
@@ -143,6 +186,10 @@ export function createStyle({ state: layerState, services, parts, source }) {
|
||||
return layerState._jamViz === 'heatline' || layerState._jamViz === 'both';
|
||||
}
|
||||
return {
|
||||
dotColor,
|
||||
paintDot,
|
||||
showDot,
|
||||
emphasizeJamDot,
|
||||
presetProfileActive,
|
||||
activeSizeDelta,
|
||||
baseDotSize,
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
// src/layers/traffic/style.test.mjs — the traffic layer keeps its dot
|
||||
// colours as renderer colours and paints them onto the points as the colours
|
||||
// they always were.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import * as Cesium from 'cesium';
|
||||
import { createStyle } from './style.js';
|
||||
import {
|
||||
FLOW_BUCKET_COLORS,
|
||||
JAM_DOT_FAR_SCALE,
|
||||
SIM_DOT_COLOR,
|
||||
} from './policy.js';
|
||||
import { presetDotRgba } from '../../data/trafficPresetStyle.js';
|
||||
import { createState } from './state.js';
|
||||
|
||||
function style(preset = 'normal') {
|
||||
const state = createState({ services: {} });
|
||||
state._stylePreset = preset;
|
||||
const parts = {};
|
||||
parts.style = createStyle({ state, services: {}, parts, source: {} });
|
||||
parts.style.refreshBucketColors();
|
||||
return { state, style: parts.style };
|
||||
}
|
||||
|
||||
test('bucket colours are the shipped palette, painted as before', () => {
|
||||
const { state, style: s } = style();
|
||||
assert.deepEqual(state._activeBucketColors.free, FLOW_BUCKET_COLORS.free);
|
||||
const expected = {
|
||||
free: Cesium.Color.fromCssColorString('#2ecc71').withAlpha(0.9),
|
||||
slow: Cesium.Color.fromCssColorString('#f0b23e').withAlpha(0.9),
|
||||
jam: Cesium.Color.fromCssColorString('#e05252').withAlpha(0.9),
|
||||
};
|
||||
for (const bucket of ['free', 'slow', 'jam'])
|
||||
assert.ok(
|
||||
Cesium.Color.equals(s.dotColor(bucket), expected[bucket]),
|
||||
bucket,
|
||||
);
|
||||
assert.ok(
|
||||
Cesium.Color.equals(s.dotColor(null), Cesium.Color.WHITE.withAlpha(0.85)),
|
||||
);
|
||||
assert.deepEqual(SIM_DOT_COLOR, [1, 1, 1, 0.85]);
|
||||
assert.equal(s.dotColor('jam'), s.dotColor('jam'), 'one colour per bucket');
|
||||
});
|
||||
|
||||
test('a dot is painted, shown and emphasised on its point', () => {
|
||||
const { state, style: s } = style();
|
||||
state._fadeScaleFar = 9000;
|
||||
const dot = { point: { show: true, color: null } };
|
||||
s.paintDot(dot, 'slow');
|
||||
assert.equal(dot.point.color, s.dotColor('slow'));
|
||||
s.showDot(dot, false);
|
||||
assert.equal(dot.point.show, false);
|
||||
s.emphasizeJamDot(dot);
|
||||
assert.ok(
|
||||
Cesium.NearFarScalar.equals(
|
||||
dot.point.scaleByDistance,
|
||||
new Cesium.NearFarScalar(100, 1.5, 9000, JAM_DOT_FAR_SCALE),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test('a styled preset recolours the buckets', () => {
|
||||
const { state, style: s } = style('surveillance');
|
||||
const before = FLOW_BUCKET_COLORS.jam;
|
||||
assert.notDeepEqual(state._activeBucketColors.jam, before);
|
||||
assert.equal(state._activeBucketColors.jam.length, 4);
|
||||
const [r, g, b, a] = presetDotRgba('surveillance', 'jam');
|
||||
assert.ok(
|
||||
Cesium.Color.equals(
|
||||
s.dotColor('jam'),
|
||||
new Cesium.Color(r / 255, g / 255, b / 255, a),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test('switching presets and back repaints dots with the shipped colours', () => {
|
||||
const state = createState({ services: {} });
|
||||
const parts = {
|
||||
rendering: { rebuildHeatLines() {}, visibleRoadsForAltitude: () => [] },
|
||||
};
|
||||
parts.style = createStyle({ state, services: {}, parts, source: {} });
|
||||
parts.style.refreshBucketColors();
|
||||
const dot = { bucket: 'free', road: { type: 'primary' }, point: {} };
|
||||
state._dots = [dot];
|
||||
const shipped = Cesium.Color.fromCssColorString('#2ecc71').withAlpha(0.9);
|
||||
parts.style.setStylePreset('surveillance');
|
||||
assert.ok(!Cesium.Color.equals(dot.point.color, shipped));
|
||||
assert.ok(Cesium.Color.equals(dot.point.color, parts.style.dotColor('free')));
|
||||
parts.style.setStylePreset('normal');
|
||||
assert.ok(Cesium.Color.equals(dot.point.color, shipped));
|
||||
assert.deepEqual(state._activeBucketColors.free, FLOW_BUCKET_COLORS.free);
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import { fromDegrees } from '../../geo/ellipsoid.js';
|
||||
import { distance } from '../../geo/vector.js';
|
||||
import { roadSurfaceChunks } from './surface.js';
|
||||
import { TRAFFIC_TIMING_ENABLED, DOT_HEIGHT_OFFSET } from './policy.js';
|
||||
|
||||
@@ -250,13 +251,11 @@ export function createTiming({ state: layerState, services, parts, source }) {
|
||||
(Number.isFinite(floor) && Math.abs(floor) <= 9000
|
||||
? floor
|
||||
: layerState._viewHeightEstimate || 0) + DOT_HEIGHT_OFFSET;
|
||||
return Cesium.Cartesian3.fromDegrees(lng, lat, h);
|
||||
return fromDegrees(lng, lat, h);
|
||||
});
|
||||
const segmentDist = [];
|
||||
for (let i = 0; i < waypoints.length - 1; i++) {
|
||||
segmentDist.push(
|
||||
Cesium.Cartesian3.distance(waypoints[i], waypoints[i + 1]),
|
||||
);
|
||||
segmentDist.push(distance(waypoints[i], waypoints[i + 1]));
|
||||
}
|
||||
/* TRACE_ONLY_BEGIN */
|
||||
_trafficTimingWaypointMaterializationMs +=
|
||||
|
||||
@@ -97,16 +97,11 @@
|
||||
"src/layers/traffic/animation.js",
|
||||
"src/layers/traffic/flow.js",
|
||||
"src/layers/traffic/footprint.js",
|
||||
"src/layers/traffic/ingestion.js",
|
||||
"src/layers/traffic/lifecycle.js",
|
||||
"src/layers/traffic/model.js",
|
||||
"src/layers/traffic/policy.js",
|
||||
"src/layers/traffic/rendering.js",
|
||||
"src/layers/traffic/retention.js",
|
||||
"src/layers/traffic/state.js",
|
||||
"src/layers/traffic/style.js",
|
||||
"src/layers/traffic/surface.js",
|
||||
"src/layers/traffic/timing.js",
|
||||
"src/layers/traffic/viewport.js",
|
||||
"src/layers/transit/height.js",
|
||||
"src/layers/transit/lifecycle.js",
|
||||
|
||||
Reference in new issue
Block a user