mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-22 03:02:20 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d37e8cf17d | ||
|
|
3e1070a2cb |
No files matched your search
@@ -1,4 +0,0 @@
|
|||||||
- Cut the stutter while zooming and panning with Street Traffic on in dense
|
|
||||||
cities. Coarse roads are no longer clipped against every detail tile in one
|
|
||||||
task, roads already parsed in a load are not parsed again, and ground floors
|
|
||||||
are read once per cell (mazy1998, #1705).
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
- A still-image CCTV camera no longer keeps the scene rendering 60 times a
|
||||||
|
second. Only a video feed holds continuous rendering; a still redraws when a
|
||||||
|
new image arrives (mazy1998, #1710).
|
||||||
@@ -144,33 +144,35 @@ export function createFrames({ state: layerState, services, parts, source }) {
|
|||||||
* the double-buffer canvas reference, throttled to PROJECTION_TEXTURE_SWAP_MS.
|
* the double-buffer canvas reference, throttled to PROJECTION_TEXTURE_SWAP_MS.
|
||||||
*
|
*
|
||||||
* @param {Object} record - Camera record with an initialized projection runtime.
|
* @param {Object} record - Camera record with an initialized projection runtime.
|
||||||
|
* @returns {boolean} Whether a new image was put on the plane.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function refreshProjectionTextures(record) {
|
function refreshProjectionTextures(record) {
|
||||||
const runtime = record?.projection;
|
const runtime = record?.projection;
|
||||||
if (!runtime || runtime.mode === 'video') return;
|
if (!runtime || runtime.mode === 'video') return false;
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
if (
|
if (
|
||||||
now - parts.model.safeNumber(runtime.lastTextureSwapAt, 0) <
|
now - parts.model.safeNumber(runtime.lastTextureSwapAt, 0) <
|
||||||
PROJECTION_TEXTURE_SWAP_MS
|
PROJECTION_TEXTURE_SWAP_MS
|
||||||
)
|
)
|
||||||
return;
|
return false;
|
||||||
|
|
||||||
const planeShowing = !!(runtime.planeEntity?.show && runtime.planeMaterial);
|
const planeShowing = !!(runtime.planeEntity?.show && runtime.planeMaterial);
|
||||||
if (!planeShowing) return;
|
if (!planeShowing) return false;
|
||||||
|
|
||||||
// Only swap when the canvas content actually changed since the last swap.
|
// Only swap when the canvas content actually changed since the last swap.
|
||||||
// Frames land every ~10 s but this runs at 1 Hz — swapping an UNCHANGED
|
// Frames land every ~10 s but this runs at 1 Hz — swapping an UNCHANGED
|
||||||
// canvas re-uploads the texture for nothing, and each material image
|
// canvas re-uploads the texture for nothing, and each material image
|
||||||
// reassignment is a flash opportunity on the live plane (owner field test
|
// reassignment is a flash opportunity on the live plane (owner field test
|
||||||
// 2026-07-04: intermittent white flashes on the monitor plane).
|
// 2026-07-04: intermittent white flashes on the monitor plane).
|
||||||
if (runtime.canvasStamp === runtime.lastSwappedCanvasStamp) return;
|
if (runtime.canvasStamp === runtime.lastSwappedCanvasStamp) return false;
|
||||||
|
|
||||||
const buffer = paintNextProjectionBuffer(runtime);
|
const buffer = paintNextProjectionBuffer(runtime);
|
||||||
if (!buffer) return;
|
if (!buffer) return false;
|
||||||
runtime.lastTextureSwapAt = now;
|
runtime.lastTextureSwapAt = now;
|
||||||
runtime.lastSwappedCanvasStamp = runtime.canvasStamp;
|
runtime.lastSwappedCanvasStamp = runtime.canvasStamp;
|
||||||
runtime.planeMaterial.image = buffer;
|
runtime.planeMaterial.image = buffer;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -123,6 +123,11 @@ export const CCTV_CALIBRATION_STORAGE_KEY_V2 =
|
|||||||
|
|
||||||
export const PROJECTION_TEXTURE_SWAP_MS = 1000;
|
export const PROJECTION_TEXTURE_SWAP_MS = 1000;
|
||||||
|
|
||||||
|
// A still-image projection changes only when a new still is swapped onto the
|
||||||
|
// plane, so the scene renders for this long after a swap (Cesium binds the new
|
||||||
|
// image over the next frames) and is otherwise left idle.
|
||||||
|
export const PROJECTION_SWAP_RENDER_MS = 500;
|
||||||
|
|
||||||
// V2 frustum geometry (design §2a/§6): the far-cap center + corners never sink
|
// V2 frustum geometry (design §2a/§6): the far-cap center + corners never sink
|
||||||
// below groundAlt + this clearance, so a fabricated pitch (-24°) cannot bury
|
// below groundAlt + this clearance, so a fabricated pitch (-24°) cannot bury
|
||||||
// the monitor plane in the 3D tiles. Exported for the unit suite.
|
// the monitor plane in the 3D tiles. Exported for the unit suite.
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
PLANE_OUTLINE_COLOR,
|
PLANE_OUTLINE_COLOR,
|
||||||
PROJECTION_CANVAS_WIDTH,
|
PROJECTION_CANVAS_WIDTH,
|
||||||
PROJECTION_CANVAS_HEIGHT,
|
PROJECTION_CANVAS_HEIGHT,
|
||||||
|
PROJECTION_SWAP_RENDER_MS,
|
||||||
} from './policy.js';
|
} from './policy.js';
|
||||||
|
|
||||||
export function createProjection({
|
export function createProjection({
|
||||||
@@ -14,7 +15,15 @@ export function createProjection({
|
|||||||
parts,
|
parts,
|
||||||
source,
|
source,
|
||||||
}) {
|
}) {
|
||||||
const { holdContinuousRender, releaseContinuousRender } = services.render;
|
const {
|
||||||
|
holdContinuousRender,
|
||||||
|
releaseContinuousRender,
|
||||||
|
governorRequestRender = () => {},
|
||||||
|
} = services.render;
|
||||||
|
// Until this time (performance.now) the projection loop asks for a frame on
|
||||||
|
// every tick: after a still is swapped onto the plane, and after an edge
|
||||||
|
// that re-arms the loop (activation, projection shown).
|
||||||
|
let renderUntil = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build the protected label associated with one active monitor plane.
|
* Build the protected label associated with one active monitor plane.
|
||||||
@@ -352,12 +361,9 @@ export function createProjection({
|
|||||||
}
|
}
|
||||||
|
|
||||||
function startProjectionLoop() {
|
function startProjectionLoop() {
|
||||||
|
renderUntil = performance.now() + PROJECTION_SWAP_RENDER_MS;
|
||||||
if (layerState._projectionRaf) return;
|
if (layerState._projectionRaf) return;
|
||||||
if (!parts.model.projectionLoopIsNeeded()) return;
|
if (!parts.model.projectionLoopIsNeeded()) return;
|
||||||
// The armed projection loop uploads video textures / runs focus fades per
|
|
||||||
// frame — the scene must render continuously while it runs. Released when
|
|
||||||
// the tick self-stops. (perf wave 2)
|
|
||||||
holdContinuousRender('cctv-projection');
|
|
||||||
|
|
||||||
const tick = () => {
|
const tick = () => {
|
||||||
if (!layerState._viewer || !parts.model.projectionLoopIsNeeded()) {
|
if (!layerState._viewer || !parts.model.projectionLoopIsNeeded()) {
|
||||||
@@ -366,15 +372,30 @@ export function createProjection({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
parts.rendering.refreshCctvFocusStyles(performance.now());
|
const now = performance.now();
|
||||||
|
const restyled = parts.rendering.refreshCctvFocusStyles(now);
|
||||||
|
|
||||||
const active = parts.selection.getActiveRecord();
|
const active = parts.selection.getActiveRecord();
|
||||||
|
let video = false;
|
||||||
if (layerState._enabled && layerState._showProjection && active) {
|
if (layerState._enabled && layerState._showProjection && active) {
|
||||||
ensureProjectionRuntime(active);
|
ensureProjectionRuntime(active);
|
||||||
if (active.projection && !active.projection.video) {
|
if (active.projection && !active.projection.video) {
|
||||||
parts.frames.drawProjectionFrame(active);
|
parts.frames.drawProjectionFrame(active);
|
||||||
parts.frames.refreshProjectionTextures(active);
|
if (parts.frames.refreshProjectionTextures(active))
|
||||||
}
|
renderUntil = now + PROJECTION_SWAP_RENDER_MS;
|
||||||
|
} else video = Boolean(active.projection?.video);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only a video feed changes the scene on every frame, so only video
|
||||||
|
// holds the continuous loop. A still lands about every ten seconds and
|
||||||
|
// a focus fade restyles icons every 80 ms; each asks for the frames it
|
||||||
|
// needs. Holding for stills rendered the scene 60 times a second for as
|
||||||
|
// long as a camera was active. (perf wave 2 held for both.)
|
||||||
|
if (video) holdContinuousRender('cctv-projection');
|
||||||
|
else {
|
||||||
|
releaseContinuousRender('cctv-projection');
|
||||||
|
if (restyled || now < renderUntil)
|
||||||
|
governorRequestRender('cctv-projection');
|
||||||
}
|
}
|
||||||
|
|
||||||
layerState._projectionRaf = requestAnimationFrame(tick);
|
layerState._projectionRaf = requestAnimationFrame(tick);
|
||||||
|
|||||||
@@ -0,0 +1,211 @@
|
|||||||
|
import test from 'node:test';
|
||||||
|
import assert from 'node:assert/strict';
|
||||||
|
import { createProjection } from './projection.js';
|
||||||
|
import { createFrames } from './frames.js';
|
||||||
|
import {
|
||||||
|
PROJECTION_SWAP_RENDER_MS,
|
||||||
|
PROJECTION_TEXTURE_SWAP_MS,
|
||||||
|
} from './policy.js';
|
||||||
|
|
||||||
|
/** The projection loop on a hand-cranked clock, with the render governor's calls recorded. */
|
||||||
|
function harness(t, { video = false, showProjection = true } = {}) {
|
||||||
|
let clock = 1000;
|
||||||
|
const frames = [];
|
||||||
|
const original = {
|
||||||
|
now: performance.now,
|
||||||
|
request: globalThis.requestAnimationFrame,
|
||||||
|
cancel: globalThis.cancelAnimationFrame,
|
||||||
|
};
|
||||||
|
performance.now = () => clock;
|
||||||
|
globalThis.requestAnimationFrame = (step) => frames.push(step);
|
||||||
|
globalThis.cancelAnimationFrame = () => frames.splice(0);
|
||||||
|
t.after(() => {
|
||||||
|
performance.now = original.now;
|
||||||
|
globalThis.requestAnimationFrame = original.request;
|
||||||
|
globalThis.cancelAnimationFrame = original.cancel;
|
||||||
|
});
|
||||||
|
|
||||||
|
const holds = new Set();
|
||||||
|
const calls = { hold: 0, requests: 0, draws: 0 };
|
||||||
|
const record = {
|
||||||
|
camera: { id: 'cam-1' },
|
||||||
|
projection: { video: video ? {} : null, mode: video ? 'video' : 'image' },
|
||||||
|
};
|
||||||
|
const scene = { needed: true, swap: false, restyled: false };
|
||||||
|
const state = {
|
||||||
|
_viewer: {},
|
||||||
|
_enabled: true,
|
||||||
|
_showProjection: showProjection,
|
||||||
|
_projectionRaf: 0,
|
||||||
|
_projectionEntities: [],
|
||||||
|
};
|
||||||
|
const projection = createProjection({
|
||||||
|
state,
|
||||||
|
services: {
|
||||||
|
render: {
|
||||||
|
holdContinuousRender(owner) {
|
||||||
|
calls.hold++;
|
||||||
|
holds.add(owner);
|
||||||
|
},
|
||||||
|
releaseContinuousRender: (owner) => holds.delete(owner),
|
||||||
|
governorRequestRender: () => calls.requests++,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
parts: {
|
||||||
|
model: { projectionLoopIsNeeded: () => scene.needed },
|
||||||
|
rendering: { refreshCctvFocusStyles: () => scene.restyled },
|
||||||
|
selection: { getActiveRecord: () => record },
|
||||||
|
frames: {
|
||||||
|
drawProjectionFrame: () => calls.draws++,
|
||||||
|
refreshProjectionTextures() {
|
||||||
|
const swapped = scene.swap;
|
||||||
|
scene.swap = false;
|
||||||
|
return swapped;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
source: {},
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
projection,
|
||||||
|
state,
|
||||||
|
record,
|
||||||
|
scene,
|
||||||
|
holds,
|
||||||
|
calls,
|
||||||
|
/** Run `count` animation frames 16 ms apart; returns the frames requested in them. */
|
||||||
|
run(count = 1) {
|
||||||
|
const before = calls.requests;
|
||||||
|
for (let i = 0; i < count; i++) {
|
||||||
|
clock += 16;
|
||||||
|
frames.shift()?.();
|
||||||
|
}
|
||||||
|
return calls.requests - before;
|
||||||
|
},
|
||||||
|
advance(ms) {
|
||||||
|
clock += ms;
|
||||||
|
},
|
||||||
|
get queued() {
|
||||||
|
return frames.length;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
test('a still-image camera never holds the continuous render loop', (t) => {
|
||||||
|
const h = harness(t);
|
||||||
|
h.projection.startProjectionLoop();
|
||||||
|
h.run(600);
|
||||||
|
assert.equal(h.calls.hold, 0);
|
||||||
|
assert.equal(h.holds.size, 0);
|
||||||
|
assert.equal(h.calls.draws, 600, 'the loop still polls for a new still');
|
||||||
|
assert.equal(h.queued, 1, 'and stays armed');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a new still on the plane is rendered, then the scene goes idle again', (t) => {
|
||||||
|
const h = harness(t);
|
||||||
|
h.projection.startProjectionLoop();
|
||||||
|
// Arming the loop (activation, projection shown) renders its first frames.
|
||||||
|
assert.ok(h.run(5) === 5);
|
||||||
|
h.advance(PROJECTION_SWAP_RENDER_MS);
|
||||||
|
assert.equal(h.run(120), 0, 'an unchanged still asks for no frame');
|
||||||
|
|
||||||
|
h.scene.swap = true;
|
||||||
|
assert.equal(h.run(1), 1, 'the tick that swaps the image asks for a frame');
|
||||||
|
const settling = Math.floor(PROJECTION_SWAP_RENDER_MS / 16) - 2;
|
||||||
|
assert.equal(h.run(settling), settling, 'and so do the ticks while it binds');
|
||||||
|
h.advance(PROJECTION_SWAP_RENDER_MS);
|
||||||
|
assert.equal(h.run(120), 0);
|
||||||
|
assert.equal(h.holds.size, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('activating another camera while the loop runs renders its first frames', (t) => {
|
||||||
|
const h = harness(t);
|
||||||
|
h.projection.startProjectionLoop();
|
||||||
|
h.advance(PROJECTION_SWAP_RENDER_MS * 2);
|
||||||
|
assert.equal(h.run(10), 0);
|
||||||
|
h.projection.startProjectionLoop();
|
||||||
|
assert.equal(h.run(10), 10);
|
||||||
|
assert.equal(h.queued, 1, 'still one loop');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a video camera holds continuous render until it stops being video', (t) => {
|
||||||
|
const h = harness(t, { video: true });
|
||||||
|
h.projection.startProjectionLoop();
|
||||||
|
h.run(3);
|
||||||
|
assert.deepEqual([...h.holds], ['cctv-projection']);
|
||||||
|
assert.equal(h.calls.draws, 0, 'Cesium updates the video texture itself');
|
||||||
|
// The stream failed over to stills.
|
||||||
|
h.record.projection.video = null;
|
||||||
|
h.run(1);
|
||||||
|
assert.equal(h.holds.size, 0);
|
||||||
|
h.record.projection.video = {};
|
||||||
|
h.run(1);
|
||||||
|
assert.deepEqual([...h.holds], ['cctv-projection']);
|
||||||
|
h.projection.stopProjectionLoop();
|
||||||
|
assert.equal(h.holds.size, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a focus fade asks for a frame only on the ticks that restyle an icon', (t) => {
|
||||||
|
const h = harness(t, { showProjection: false });
|
||||||
|
h.projection.startProjectionLoop();
|
||||||
|
h.advance(PROJECTION_SWAP_RENDER_MS * 2);
|
||||||
|
assert.equal(h.run(10), 0);
|
||||||
|
h.scene.restyled = true;
|
||||||
|
assert.equal(h.run(4), 4);
|
||||||
|
h.scene.restyled = false;
|
||||||
|
assert.equal(h.run(10), 0);
|
||||||
|
assert.equal(h.holds.size, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('the loop releases and stops when it has no work', (t) => {
|
||||||
|
const h = harness(t, { video: true });
|
||||||
|
h.projection.startProjectionLoop();
|
||||||
|
h.run(2);
|
||||||
|
h.scene.needed = false;
|
||||||
|
h.run(1);
|
||||||
|
assert.equal(h.holds.size, 0);
|
||||||
|
assert.equal(h.state._projectionRaf, 0);
|
||||||
|
assert.equal(h.queued, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('refreshProjectionTextures reports whether it put a new image on the plane', (t) => {
|
||||||
|
const previousDocument = globalThis.document;
|
||||||
|
globalThis.document = {
|
||||||
|
createElement: () => ({
|
||||||
|
getContext: () => ({ clearRect() {}, drawImage() {} }),
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
t.after(() => {
|
||||||
|
if (previousDocument === undefined) delete globalThis.document;
|
||||||
|
else globalThis.document = previousDocument;
|
||||||
|
});
|
||||||
|
const frames = createFrames({
|
||||||
|
state: {},
|
||||||
|
services: {},
|
||||||
|
parts: { model: { safeNumber: (value, fallback) => value ?? fallback } },
|
||||||
|
source: {},
|
||||||
|
});
|
||||||
|
const runtime = {
|
||||||
|
mode: 'image',
|
||||||
|
canvas: {},
|
||||||
|
canvasStamp: 1,
|
||||||
|
planeEntity: { show: true },
|
||||||
|
planeMaterial: { image: null },
|
||||||
|
};
|
||||||
|
const record = { projection: runtime };
|
||||||
|
assert.equal(frames.refreshProjectionTextures(record), true);
|
||||||
|
const first = runtime.planeMaterial.image;
|
||||||
|
assert.ok(first);
|
||||||
|
// Unchanged canvas: nothing to render.
|
||||||
|
runtime.lastTextureSwapAt -= PROJECTION_TEXTURE_SWAP_MS;
|
||||||
|
assert.equal(frames.refreshProjectionTextures(record), false);
|
||||||
|
assert.equal(runtime.planeMaterial.image, first);
|
||||||
|
runtime.canvasStamp = 2;
|
||||||
|
assert.equal(frames.refreshProjectionTextures(record), true);
|
||||||
|
assert.notEqual(runtime.planeMaterial.image, first);
|
||||||
|
runtime.planeEntity.show = false;
|
||||||
|
runtime.canvasStamp = 3;
|
||||||
|
runtime.lastTextureSwapAt -= PROJECTION_TEXTURE_SWAP_MS;
|
||||||
|
assert.equal(frames.refreshProjectionTextures(record), false);
|
||||||
|
assert.equal(frames.refreshProjectionTextures({ projection: { mode: 'video' } }), false);
|
||||||
|
});
|
||||||
@@ -61,8 +61,8 @@ export function createRendering({
|
|||||||
!layerState._viewer ||
|
!layerState._viewer ||
|
||||||
!focusPassIsNeeded(target, layerState._activeFocusStyleCount)
|
!focusPassIsNeeded(target, layerState._activeFocusStyleCount)
|
||||||
)
|
)
|
||||||
return;
|
return false;
|
||||||
if (nowMs - layerState._lastFocusStyleAt < 80) return;
|
if (nowMs - layerState._lastFocusStyleAt < 80) return false;
|
||||||
layerState._lastFocusStyleAt = nowMs;
|
layerState._lastFocusStyleAt = nowMs;
|
||||||
const scene = layerState._viewer.scene;
|
const scene = layerState._viewer.scene;
|
||||||
const camera = layerState._viewer.camera;
|
const camera = layerState._viewer.camera;
|
||||||
@@ -82,6 +82,8 @@ export function createRendering({
|
|||||||
baseColorFor: cameraColorFor,
|
baseColorFor: cameraColorFor,
|
||||||
});
|
});
|
||||||
layerState._activeFocusStyleCount = result.activeCount;
|
layerState._activeFocusStyleCount = result.activeCount;
|
||||||
|
// Whether an icon was restyled, so the caller can ask for a frame.
|
||||||
|
return result.writes > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { roadIdentity } from './retention.js';
|
import { roadIdentity } from './retention.js';
|
||||||
import * as Cesium from 'cesium';
|
import * as Cesium from 'cesium';
|
||||||
import {
|
import {
|
||||||
floorCellReader,
|
|
||||||
prepareRoadSurfaces,
|
prepareRoadSurfaces,
|
||||||
roadHeightStations,
|
roadHeightStations,
|
||||||
trafficSurfaceReady,
|
trafficSurfaceReady,
|
||||||
@@ -302,7 +301,6 @@ export function createFlow({ state: layerState, services, parts, source }) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (depthHeight !== undefined) layerState._viewHeightEstimate = depthHeight;
|
if (depthHeight !== undefined) layerState._viewHeightEstimate = depthHeight;
|
||||||
const floorAt = floorCellReader(services.ground);
|
|
||||||
for (const road of roads) {
|
for (const road of roads) {
|
||||||
road.key ||= roadIdentity(road);
|
road.key ||= roadIdentity(road);
|
||||||
const prepared = !refining && preparedRoads.get(road.key);
|
const prepared = !refining && preparedRoads.get(road.key);
|
||||||
@@ -321,7 +319,7 @@ export function createFlow({ state: layerState, services, parts, source }) {
|
|||||||
);
|
);
|
||||||
for (let i = 0; i < road.coords.length; i++) {
|
for (let i = 0; i < road.coords.length; i++) {
|
||||||
const [lon, lat] = road.coords[i];
|
const [lon, lat] = road.coords[i];
|
||||||
const floor = floorAt(lat, lon);
|
const floor = services.ground?.cachedGroundFloor?.(lat, lon);
|
||||||
const height =
|
const height =
|
||||||
Number.isFinite(floor) && Math.abs(floor) <= 9000
|
Number.isFinite(floor) && Math.abs(floor) <= 9000
|
||||||
? Math.max(floor, layerState._viewHeightEstimate)
|
? Math.max(floor, layerState._viewHeightEstimate)
|
||||||
|
|||||||
@@ -39,10 +39,10 @@ export function createTrafficLayer({ services, source }) {
|
|||||||
parts.controls = createControls(context);
|
parts.controls = createControls(context);
|
||||||
parts.lifecycle = createLifecycle(context);
|
parts.lifecycle = createLifecycle(context);
|
||||||
state._parseRoads = TRAFFIC_TIMING_ENABLED
|
state._parseRoads = TRAFFIC_TIMING_ENABLED
|
||||||
? (data, trace, parsedBySource) =>
|
? (data, trace) =>
|
||||||
trace
|
trace
|
||||||
? parts.timing.parseRoadsTimed(data, trace, parsedBySource)
|
? parts.timing.parseRoadsTimed(data, trace)
|
||||||
: parts.model.parseRoads(data, trace, parsedBySource)
|
: parts.model.parseRoads(data)
|
||||||
: parts.model.parseRoads;
|
: parts.model.parseRoads;
|
||||||
|
|
||||||
state._loadRoadsForBounds = TRAFFIC_TIMING_ENABLED
|
state._loadRoadsForBounds = TRAFFIC_TIMING_ENABLED
|
||||||
|
|||||||
@@ -348,16 +348,11 @@ export function createIngestion({
|
|||||||
layerState._retainedPreview = false;
|
layerState._retainedPreview = false;
|
||||||
layerState._detailRoadsReady = false;
|
layerState._detailRoadsReady = false;
|
||||||
let paintRevision = 0;
|
let paintRevision = 0;
|
||||||
// The snapshot that ends a pass replaces the streamed roads with the
|
|
||||||
// composed set, which is mostly the tile records already parsed and
|
|
||||||
// drawn. Remembering those per pass means only the records new to that
|
|
||||||
// snapshot (the clipped coarse roads) are parsed, not the whole set again.
|
|
||||||
let parsedBySource = new WeakMap();
|
|
||||||
const onTile = (data, passSignal, preview) => {
|
const onTile = (data, passSignal, preview) => {
|
||||||
if (generation !== layerState._loadGeneration || passSignal.aborted)
|
if (generation !== layerState._loadGeneration || passSignal.aborted)
|
||||||
return;
|
return;
|
||||||
const start = performance.now();
|
const start = performance.now();
|
||||||
const parsed = layerState._parseRoads(data, trace, parsedBySource);
|
const parsed = layerState._parseRoads(data, trace);
|
||||||
phaseTiming('parse', start, { roads: parsed.length, incremental: true });
|
phaseTiming('parse', start, { roads: parsed.length, incremental: true });
|
||||||
if (data.replace) streamed = parsed;
|
if (data.replace) streamed = parsed;
|
||||||
else streamed.push(...parsed);
|
else streamed.push(...parsed);
|
||||||
@@ -536,7 +531,6 @@ export function createIngestion({
|
|||||||
if (altitude > FAST_FETCH_ALTITUDE) return;
|
if (altitude > FAST_FETCH_ALTITUDE) return;
|
||||||
|
|
||||||
streamed = [];
|
streamed = [];
|
||||||
parsedBySource = new WeakMap();
|
|
||||||
majorPass = false;
|
majorPass = false;
|
||||||
// Detailed pass: fetch the full road graph (tertiary, residential, etc.)
|
// Detailed pass: fetch the full road graph (tertiary, residential, etc.)
|
||||||
console.log(`[Data:Traffic] Full fetch local roads [${cacheKey}]`);
|
console.log(`[Data:Traffic] Full fetch local roads [${cacheKey}]`);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import * as Cesium from 'cesium';
|
import * as Cesium from 'cesium';
|
||||||
import { floorCellReader, roadSurfaceChunks } from './surface.js';
|
import { roadSurfaceChunks } from './surface.js';
|
||||||
import { t, translateKnown } from '../../i18n/index.js';
|
import { t, translateKnown } from '../../i18n/index.js';
|
||||||
import {
|
import {
|
||||||
flowDensityMult,
|
flowDensityMult,
|
||||||
@@ -13,35 +13,22 @@ import {
|
|||||||
} from './policy.js';
|
} from './policy.js';
|
||||||
|
|
||||||
export function createModel({ state: layerState, services, parts, source }) {
|
export function createModel({ state: layerState, services, parts, source }) {
|
||||||
/**
|
/** Build scene waypoints from source records; thinning and terrain remain rendering policy. */
|
||||||
* Build scene waypoints from source records; thinning and terrain remain rendering policy.
|
function parseRoads(roadData) {
|
||||||
*
|
|
||||||
* `parsedBySource` (source record → its parsed roads) is filled as records
|
|
||||||
* are parsed, and a record already in it is answered from it.
|
|
||||||
*/
|
|
||||||
function parseRoads(roadData, _trace, parsedBySource) {
|
|
||||||
if (!roadData || !roadData.roads) {
|
if (!roadData || !roadData.roads) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const roads = [];
|
const roads = [];
|
||||||
const floorAt = floorCellReader(services.ground);
|
|
||||||
for (const road of roadData.roads) {
|
for (const road of roadData.roads) {
|
||||||
if (!road.coordinates || road.coordinates.length < 2) continue;
|
if (!road.coordinates || road.coordinates.length < 2) continue;
|
||||||
// A replacing snapshot repeats records this load already parsed.
|
|
||||||
const known = parsedBySource?.get(road);
|
|
||||||
if (known) {
|
|
||||||
for (const parsed of known) roads.push(parsed);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const first = roads.length;
|
|
||||||
|
|
||||||
for (const coords of roadSurfaceChunks(road.coordinates)) {
|
for (const coords of roadSurfaceChunks(road.coordinates)) {
|
||||||
const type = road.type;
|
const type = road.type;
|
||||||
const oneway = road.oneway;
|
const oneway = road.oneway;
|
||||||
// Pre-compute Cartesian3 waypoints (lon, lat, height) for fast lerp animation
|
// Pre-compute Cartesian3 waypoints (lon, lat, height) for fast lerp animation
|
||||||
const waypoints = coords.map(([lng, lat]) => {
|
const waypoints = coords.map(([lng, lat]) => {
|
||||||
const floor = floorAt(lat, lng);
|
const floor = services.ground?.cachedGroundFloor?.(lat, lng);
|
||||||
const h =
|
const h =
|
||||||
(Number.isFinite(floor) && Math.abs(floor) <= 9000
|
(Number.isFinite(floor) && Math.abs(floor) <= 9000
|
||||||
? floor
|
? floor
|
||||||
@@ -73,7 +60,6 @@ export function createModel({ state: layerState, services, parts, source }) {
|
|||||||
flow: road.flow || null,
|
flow: road.flow || null,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
parsedBySource?.set(road, roads.slice(first));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return roads;
|
return roads;
|
||||||
|
|||||||
@@ -1,444 +0,0 @@
|
|||||||
import test from 'node:test';
|
|
||||||
import assert from 'node:assert/strict';
|
|
||||||
import * as Cesium from 'cesium';
|
|
||||||
import { createTrafficSource, outsideDetailTiles } from './source.js';
|
|
||||||
import { createIngestion } from './ingestion.js';
|
|
||||||
import { createModel } from './model.js';
|
|
||||||
import { floorCellReader, roadSurfaceChunks } from './surface.js';
|
|
||||||
import { DOT_HEIGHT_OFFSET } from './policy.js';
|
|
||||||
import { clipTileLine } from '../../sources/openFreeMap.js';
|
|
||||||
import { tileToBBox } from '../../data/tomtomTiles.js';
|
|
||||||
|
|
||||||
// The clip as it was before the extent shortcut: every road against the four
|
|
||||||
// regions around every tile. The shortcut must return exactly this.
|
|
||||||
function clipEveryRoadAgainstEveryTile(roads, tiles) {
|
|
||||||
return roads.flatMap((road) => {
|
|
||||||
let lines = [road.coordinates];
|
|
||||||
for (const tile of tiles) {
|
|
||||||
const box = tileToBBox(tile.z, tile.x, tile.y);
|
|
||||||
const regions = [
|
|
||||||
{ west: -180, east: box.west, south: -85, north: 85 },
|
|
||||||
{ west: box.east, east: 180, south: -85, north: 85 },
|
|
||||||
{ west: box.west, east: box.east, south: -85, north: box.south },
|
|
||||||
{ west: box.west, east: box.east, south: box.north, north: 85 },
|
|
||||||
];
|
|
||||||
lines = lines.flatMap((line) =>
|
|
||||||
regions.flatMap((region) => clipTileLine(line, region)),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return lines.map((coordinates) => ({ ...road, coordinates }));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function seeded(seed) {
|
|
||||||
let state = seed >>> 0;
|
|
||||||
return () => {
|
|
||||||
state = (Math.imul(state, 1664525) + 1013904223) >>> 0;
|
|
||||||
return state / 2 ** 32;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function tileBlock(x, y, size = 4) {
|
|
||||||
const tiles = [];
|
|
||||||
for (let dx = 0; dx < size; dx++)
|
|
||||||
for (let dy = 0; dy < size; dy++)
|
|
||||||
tiles.push({ z: 14, x: x + dx, y: y + dy });
|
|
||||||
return tiles;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Roads in and around a tile block: wandering, straight, tiny, and on tile edges. */
|
|
||||||
function roadsAround(tiles, random, count = 400) {
|
|
||||||
const boxes = tiles.map((tile) => tileToBBox(tile.z, tile.x, tile.y));
|
|
||||||
const west = Math.min(...boxes.map((box) => box.west)),
|
|
||||||
east = Math.max(...boxes.map((box) => box.east)),
|
|
||||||
south = Math.min(...boxes.map((box) => box.south)),
|
|
||||||
north = Math.max(...boxes.map((box) => box.north));
|
|
||||||
const width = east - west,
|
|
||||||
height = north - south;
|
|
||||||
const roads = [];
|
|
||||||
for (let i = 0; i < count; i++) {
|
|
||||||
const step = i % 7 === 0 ? 0.00002 : i % 3 === 0 ? 0.01 : 0.002;
|
|
||||||
let lon = west - width / 2 + random() * width * 2,
|
|
||||||
lat = south - height / 2 + random() * height * 2;
|
|
||||||
const coordinates = [[lon, lat]];
|
|
||||||
const points = 1 + Math.floor(random() * 12);
|
|
||||||
for (let j = 0; j < points; j++) {
|
|
||||||
lon += (random() - 0.5) * step;
|
|
||||||
lat += (random() - 0.5) * step;
|
|
||||||
coordinates.push([lon, lat]);
|
|
||||||
}
|
|
||||||
roads.push({ type: 'primary', oneway: i % 2, id: i, coordinates });
|
|
||||||
}
|
|
||||||
const box = boxes[5];
|
|
||||||
const midLat = (box.south + box.north) / 2,
|
|
||||||
midLon = (box.west + box.east) / 2;
|
|
||||||
roads.push(
|
|
||||||
// Along a tile edge, ending on a corner, and touching an edge from outside.
|
|
||||||
{
|
|
||||||
id: 'edge',
|
|
||||||
coordinates: [
|
|
||||||
[box.west, box.south - 0.01],
|
|
||||||
[box.west, box.north + 0.01],
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'corner',
|
|
||||||
coordinates: [
|
|
||||||
[box.west - 0.01, box.south - 0.01],
|
|
||||||
[box.west, box.south],
|
|
||||||
[box.east, box.north],
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'touch',
|
|
||||||
coordinates: [
|
|
||||||
[box.west - 0.01, midLat],
|
|
||||||
[box.west, midLat],
|
|
||||||
],
|
|
||||||
},
|
|
||||||
// Across the whole block, and a repeated vertex.
|
|
||||||
{
|
|
||||||
id: 'across',
|
|
||||||
coordinates: [
|
|
||||||
[west - 0.01, midLat],
|
|
||||||
[east + 0.01, midLat + 0.0003],
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'repeat',
|
|
||||||
coordinates: [
|
|
||||||
[midLon, midLat],
|
|
||||||
[midLon, midLat],
|
|
||||||
[midLon + 0.001, midLat],
|
|
||||||
],
|
|
||||||
},
|
|
||||||
// Records the clip drops or leaves alone.
|
|
||||||
{ id: 'single', coordinates: [[west - 0.02, midLat]] },
|
|
||||||
{ id: 'empty', coordinates: [] },
|
|
||||||
{
|
|
||||||
id: 'negative-zero',
|
|
||||||
coordinates: [
|
|
||||||
[-0, south - 0.02],
|
|
||||||
[0.001, south - 0.02],
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'polar',
|
|
||||||
coordinates: [
|
|
||||||
[west - 0.02, 86],
|
|
||||||
[west - 0.02, 86.01],
|
|
||||||
],
|
|
||||||
},
|
|
||||||
);
|
|
||||||
return roads;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const [place, x, y] of [
|
|
||||||
['Manhattan', 4823, 6158],
|
|
||||||
['London, across longitude 0', 8190, 5446],
|
|
||||||
['longitude 0 and latitude 0', 8190, 8190],
|
|
||||||
['the southern and eastern hemispheres', 15070, 9826],
|
|
||||||
])
|
|
||||||
test(`coarse roads clipped around detail tiles match the every-tile clip: ${place}`, () => {
|
|
||||||
const tiles = tileBlock(x, y);
|
|
||||||
const roads = roadsAround(tiles, seeded(x * 31 + y));
|
|
||||||
for (const loaded of [tiles, tiles.slice(0, 5), tiles.slice(3, 4), []]) {
|
|
||||||
const expected = clipEveryRoadAgainstEveryTile(roads, loaded);
|
|
||||||
assert.ok(loaded.length === 0 || expected.length !== roads.length);
|
|
||||||
// deepStrictEqual separates -0 from 0 and compares every vertex exactly.
|
|
||||||
assert.deepStrictEqual(outsideDetailTiles(roads, loaded), expected);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
test('only a road that reaches a detail tile is clipped against it', () => {
|
|
||||||
const tiles = tileBlock(4823, 6158);
|
|
||||||
const block = tiles.map((tile) => tileToBBox(tile.z, tile.x, tile.y));
|
|
||||||
const west = Math.min(...block.map((box) => box.west)),
|
|
||||||
north = Math.max(...block.map((box) => box.north));
|
|
||||||
// 300 roads beside the block and between its tile edges, none touching one.
|
|
||||||
const clear = [];
|
|
||||||
for (let i = 0; i < 300; i++) {
|
|
||||||
const box = block[i % block.length];
|
|
||||||
const lon = i % 2 ? west - 0.02 - i * 1e-5 : box.west + 0.004;
|
|
||||||
const lat = i % 2 ? box.south + 0.004 : north + 0.01 + i * 1e-5;
|
|
||||||
clear.push({
|
|
||||||
coordinates: [
|
|
||||||
[lon, lat],
|
|
||||||
[lon + 0.002, lat + 0.0005],
|
|
||||||
[lon + 0.003, lat + 0.001],
|
|
||||||
],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const first = block[0];
|
|
||||||
const crossing = {
|
|
||||||
coordinates: [
|
|
||||||
[first.west + 0.004, north + 0.01],
|
|
||||||
[first.west + 0.004, (first.south + first.north) / 2],
|
|
||||||
],
|
|
||||||
};
|
|
||||||
let clips = 0;
|
|
||||||
const counted = (line, region) => {
|
|
||||||
clips++;
|
|
||||||
return clipTileLine(line, region);
|
|
||||||
};
|
|
||||||
const roads = outsideDetailTiles([...clear, crossing], tiles, counted);
|
|
||||||
assert.deepStrictEqual(
|
|
||||||
roads,
|
|
||||||
clipEveryRoadAgainstEveryTile([...clear, crossing], tiles),
|
|
||||||
);
|
|
||||||
assert.equal(roads.length, 301);
|
|
||||||
// Four regions for each tile the crossing road reaches, never the 19,264
|
|
||||||
// clips of every road against every tile.
|
|
||||||
assert.ok(clips >= 4, `expected the crossing road to be clipped: ${clips}`);
|
|
||||||
assert.ok(clips <= 4 * tiles.length, `clipped ${clips} times`);
|
|
||||||
});
|
|
||||||
|
|
||||||
/** A DEM-regime ground service: one floor per toFixed(3) cell, counting reads. */
|
|
||||||
function cellGround({ mesh = false, warm = () => true } = {}) {
|
|
||||||
const ground = {
|
|
||||||
reads: 0,
|
|
||||||
meshFloorPreferred: () => mesh,
|
|
||||||
cachedGroundFloor(lat, lon) {
|
|
||||||
ground.reads++;
|
|
||||||
if (!Number.isFinite(lat) || !Number.isFinite(lon)) return null;
|
|
||||||
// Number() as the shared cache does it: "-0.000" and "0.000" are one cell.
|
|
||||||
const cell = `${Number(lat.toFixed(3))},${Number(lon.toFixed(3))}`;
|
|
||||||
if (!warm(cell)) return null;
|
|
||||||
let hash = 0;
|
|
||||||
for (const character of cell)
|
|
||||||
hash = (Math.imul(hash, 31) + character.charCodeAt(0)) | 0;
|
|
||||||
// The mesh regime answers per point, not per cell.
|
|
||||||
return (hash % 4000) / 10 + (mesh ? lat - Math.floor(lat * 1000) / 1000 : 0);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
return ground;
|
|
||||||
}
|
|
||||||
|
|
||||||
test('floor reads are shared per cell and equal the per-coordinate reads', () => {
|
|
||||||
const random = seeded(7);
|
|
||||||
const points = [];
|
|
||||||
for (let i = 0; i < 4000; i++)
|
|
||||||
points.push([40.75 + random() * 0.01, -73.99 + random() * 0.01]);
|
|
||||||
// Cell edges, where toFixed(3) and Math.round can disagree, in each sign.
|
|
||||||
for (const lat of [40.7505, -40.7505, 0.0005, -0.0005, 1.0005, 8.5745])
|
|
||||||
for (const lon of [-73.9905, 73.9905, 0.0005, -0.0005, 1.0005, 2.3515])
|
|
||||||
points.push([lat, lon], [lat + 1e-9, lon - 1e-9], [lat - 1e-9, lon]);
|
|
||||||
points.push([Number.NaN, 0], [0, Infinity], [0, 0], [-0, -0]);
|
|
||||||
|
|
||||||
const direct = cellGround();
|
|
||||||
const expected = points.map(([lat, lon]) =>
|
|
||||||
direct.cachedGroundFloor(lat, lon),
|
|
||||||
);
|
|
||||||
const ground = cellGround();
|
|
||||||
const floorAt = floorCellReader(ground);
|
|
||||||
assert.deepStrictEqual(
|
|
||||||
points.map(([lat, lon]) => floorAt(lat, lon)),
|
|
||||||
expected,
|
|
||||||
);
|
|
||||||
// 4000 points in a 0.01 degree square share about 121 cells.
|
|
||||||
assert.ok(ground.reads < 600, `read the shared cache ${ground.reads} times`);
|
|
||||||
|
|
||||||
// A cold cell is remembered as cold for the pass, not asked again.
|
|
||||||
const cold = cellGround({ warm: () => false });
|
|
||||||
const coldAt = floorCellReader(cold);
|
|
||||||
assert.equal(coldAt(40.7512, -73.9812), null);
|
|
||||||
assert.equal(coldAt(40.7513, -73.9813), null);
|
|
||||||
assert.equal(cold.reads, 1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('mesh floors and unknown ground services are read per coordinate', () => {
|
|
||||||
const mesh = cellGround({ mesh: true });
|
|
||||||
const meshAt = floorCellReader(mesh);
|
|
||||||
const a = meshAt(40.7512, -73.9812),
|
|
||||||
b = meshAt(40.75121, -73.9812);
|
|
||||||
assert.equal(mesh.reads, 2);
|
|
||||||
assert.notEqual(a, b);
|
|
||||||
|
|
||||||
// No regime to ask: nothing says a floor is the same across a cell.
|
|
||||||
let reads = 0;
|
|
||||||
const bareAt = floorCellReader({ cachedGroundFloor: () => ++reads });
|
|
||||||
bareAt(40.7512, -73.9812);
|
|
||||||
bareAt(40.7512, -73.9812);
|
|
||||||
assert.equal(reads, 2);
|
|
||||||
assert.equal(floorCellReader(undefined)(1, 2), undefined);
|
|
||||||
assert.equal(floorCellReader({})(1, 2), undefined);
|
|
||||||
});
|
|
||||||
|
|
||||||
function road(lon, lat, points = 40) {
|
|
||||||
const coordinates = [];
|
|
||||||
for (let i = 0; i < points; i++)
|
|
||||||
coordinates.push([lon + i * 0.00004, lat + i * 0.00003]);
|
|
||||||
return { type: 'residential', oneway: 1, coordinates };
|
|
||||||
}
|
|
||||||
|
|
||||||
test('parsing reads each floor cell once and builds the same waypoints', () => {
|
|
||||||
const ground = cellGround({ warm: (cell) => !cell.endsWith('3') });
|
|
||||||
const state = { _viewHeightEstimate: 12 };
|
|
||||||
const model = createModel({
|
|
||||||
state,
|
|
||||||
services: { ground },
|
|
||||||
parts: {},
|
|
||||||
source: {},
|
|
||||||
});
|
|
||||||
const records = [road(-73.99, 40.75), road(-73.9899, 40.7501)];
|
|
||||||
const parsed = model.parseRoads({ roads: records });
|
|
||||||
const coordinates = records.reduce(
|
|
||||||
(count, record) => count + record.coordinates.length,
|
|
||||||
0,
|
|
||||||
);
|
|
||||||
assert.ok(
|
|
||||||
ground.reads < coordinates / 3,
|
|
||||||
`${ground.reads} floor reads for ${coordinates} coordinates`,
|
|
||||||
);
|
|
||||||
|
|
||||||
const direct = cellGround({ warm: (cell) => !cell.endsWith('3') });
|
|
||||||
const expected = records.flatMap((record) =>
|
|
||||||
roadSurfaceChunks(record.coordinates).map((coords) =>
|
|
||||||
coords.map(([lon, lat]) => {
|
|
||||||
const floor = direct.cachedGroundFloor(lat, lon);
|
|
||||||
return Cesium.Cartesian3.fromDegrees(
|
|
||||||
lon,
|
|
||||||
lat,
|
|
||||||
(floor ?? state._viewHeightEstimate) + DOT_HEIGHT_OFFSET,
|
|
||||||
);
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
assert.ok(direct.reads >= coordinates);
|
|
||||||
assert.deepStrictEqual(
|
|
||||||
parsed.map((entry) => entry.waypoints),
|
|
||||||
expected,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('a record parsed earlier in the pass is not parsed again', () => {
|
|
||||||
const ground = cellGround();
|
|
||||||
const model = createModel({
|
|
||||||
state: {},
|
|
||||||
services: { ground },
|
|
||||||
parts: {},
|
|
||||||
source: {},
|
|
||||||
});
|
|
||||||
const first = { ...road(-73.99, 40.75), oneway: 0 },
|
|
||||||
second = road(-73.98, 40.76),
|
|
||||||
short = { type: 'primary', coordinates: [[-73.97, 40.77]] };
|
|
||||||
const parsedBySource = new WeakMap();
|
|
||||||
const tile = model.parseRoads({ roads: [first] }, null, parsedBySource);
|
|
||||||
assert.equal(tile.length, 2, 'a two-way record parses to both directions');
|
|
||||||
const reads = ground.reads;
|
|
||||||
const replaced = model.parseRoads(
|
|
||||||
{ roads: [second, short, first], replace: true },
|
|
||||||
null,
|
|
||||||
parsedBySource,
|
|
||||||
);
|
|
||||||
assert.equal(replaced.length, 3);
|
|
||||||
// The same parsed roads, in the snapshot's order: dots stay attached.
|
|
||||||
assert.equal(replaced[1], tile[0]);
|
|
||||||
assert.equal(replaced[2], tile[1]);
|
|
||||||
const fresh = cellGround();
|
|
||||||
createModel({
|
|
||||||
state: {},
|
|
||||||
services: { ground: fresh },
|
|
||||||
parts: {},
|
|
||||||
source: {},
|
|
||||||
}).parseRoads({ roads: [second] });
|
|
||||||
assert.equal(ground.reads - reads, fresh.reads, 'only the new record read');
|
|
||||||
|
|
||||||
// Without the map every call parses, as before.
|
|
||||||
const again = model.parseRoads({ roads: [first] });
|
|
||||||
assert.notEqual(again[0], tile[0]);
|
|
||||||
assert.deepStrictEqual(again, tile);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('the snapshot that ends a pass parses only the records it adds', async () => {
|
|
||||||
const detail = { z: 14, x: 3743, y: 6745 };
|
|
||||||
const core = tileToBBox(detail.z, detail.x, detail.y);
|
|
||||||
const local = {
|
|
||||||
type: 'residential',
|
|
||||||
oneway: 1,
|
|
||||||
coordinates: [
|
|
||||||
[core.west + 0.002, core.south + 0.002],
|
|
||||||
[core.west + 0.004, core.south + 0.004],
|
|
||||||
],
|
|
||||||
};
|
|
||||||
const distant = {
|
|
||||||
type: 'primary',
|
|
||||||
oneway: 1,
|
|
||||||
coordinates: [
|
|
||||||
[core.east + 0.01, core.south],
|
|
||||||
[core.east + 0.02, core.south],
|
|
||||||
],
|
|
||||||
};
|
|
||||||
const bounds = {
|
|
||||||
...core,
|
|
||||||
east: core.east + 0.03,
|
|
||||||
coverage: {
|
|
||||||
detail: [detail],
|
|
||||||
coarse: [{ z: 12, x: 935, y: 1686 }],
|
|
||||||
key: 'test',
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const source = createTrafficSource({
|
|
||||||
mapTiles: {
|
|
||||||
async fetchBounds(box, { zoom, onTile }) {
|
|
||||||
const tile = { roads: zoom === 14 ? [local] : [distant] };
|
|
||||||
onTile?.(tile);
|
|
||||||
return {
|
|
||||||
tiles: [tile],
|
|
||||||
loadedTiles: zoom === 14 ? [detail] : bounds.coverage.coarse,
|
|
||||||
partial: false,
|
|
||||||
};
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const parsedRecords = [];
|
|
||||||
const painted = [];
|
|
||||||
const state = {
|
|
||||||
_enabled: true,
|
|
||||||
_loadGeneration: 0,
|
|
||||||
_tileCache: new Map(),
|
|
||||||
_roadMode: null,
|
|
||||||
_liveMode: false,
|
|
||||||
// The model's contract: fill the map, answer known records from it.
|
|
||||||
_parseRoads: (data, trace, parsedBySource) =>
|
|
||||||
data.roads.flatMap((record) => {
|
|
||||||
const known = parsedBySource?.get(record);
|
|
||||||
if (known) return known;
|
|
||||||
parsedRecords.push(record);
|
|
||||||
const parsed = [{ record }];
|
|
||||||
parsedBySource?.set(record, parsed);
|
|
||||||
return parsed;
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
const ingestion = createIngestion({
|
|
||||||
state,
|
|
||||||
source,
|
|
||||||
services: {},
|
|
||||||
parts: {
|
|
||||||
viewport: {
|
|
||||||
clampBounds: (b) => b,
|
|
||||||
getBoundsCenter: () => ({ lat: 30.26, lon: -97.74 }),
|
|
||||||
},
|
|
||||||
flow: {
|
|
||||||
warmFlow: () => Promise.resolve([]),
|
|
||||||
applyFlowThenRender: async (roads, box, generation, altitude, label) => {
|
|
||||||
if (label === 'Loaded full') painted.push(roads);
|
|
||||||
return true;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
await ingestion.loadRoadsForBounds(bounds, 350);
|
|
||||||
assert.equal(
|
|
||||||
parsedRecords.filter((record) => record === local).length,
|
|
||||||
1,
|
|
||||||
'the detail tile record is parsed when its tile streams, not again',
|
|
||||||
);
|
|
||||||
// The full graph is still the detail record plus the coarse road outside it.
|
|
||||||
assert.equal(painted.at(-1).length, 2);
|
|
||||||
assert.equal(painted.at(-1)[0].record, local);
|
|
||||||
assert.deepStrictEqual(
|
|
||||||
painted.at(-1)[1].record.coordinates,
|
|
||||||
distant.coordinates,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
+15
-100
@@ -33,108 +33,23 @@ export function trafficDetailBounds(box) {
|
|||||||
return detail;
|
return detail;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Longitude/latitude extent of one line; NaN coordinates leave it unusable. */
|
/** Keep coarse roads only outside the detailed tile cores. */
|
||||||
function lineExtent(coordinates) {
|
export function outsideDetailTiles(roads, tiles) {
|
||||||
let west = Infinity,
|
|
||||||
east = -Infinity,
|
|
||||||
south = Infinity,
|
|
||||||
north = -Infinity;
|
|
||||||
for (const [lon, lat] of coordinates) {
|
|
||||||
if (!(lon >= west)) west = lon;
|
|
||||||
if (!(lon <= east)) east = lon;
|
|
||||||
if (!(lat >= south)) south = lat;
|
|
||||||
if (!(lat <= north)) north = lat;
|
|
||||||
}
|
|
||||||
return { coordinates, west, east, south, north, settled: false };
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* What `clipTileLine` returns for a line lying wholly inside the clip region:
|
|
||||||
* the same line with each vertex recomputed from the one before it. Returns
|
|
||||||
* null for a line under the 12 m floor `clipTileLine` drops.
|
|
||||||
*/
|
|
||||||
function unclippedLine(coordinates) {
|
|
||||||
if (coordinates.length < 2) return null;
|
|
||||||
const out = [];
|
|
||||||
let metres = 0;
|
|
||||||
for (let i = 1; i < coordinates.length; i++) {
|
|
||||||
const a = coordinates[i - 1],
|
|
||||||
b = coordinates[i];
|
|
||||||
const dx = b[0] - a[0],
|
|
||||||
dy = b[1] - a[1];
|
|
||||||
if (i === 1) out.push([a[0] + 0 * dx, a[1] + 0 * dy]);
|
|
||||||
out.push([a[0] + dx, a[1] + dy]);
|
|
||||||
metres +=
|
|
||||||
Math.hypot(
|
|
||||||
(out[i][0] - out[i - 1][0]) * Math.cos((out[i][1] * Math.PI) / 180),
|
|
||||||
out[i][1] - out[i - 1][1],
|
|
||||||
) * 111320;
|
|
||||||
}
|
|
||||||
return metres >= 12 ? out : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const sameLine = (a, b) =>
|
|
||||||
a.every(
|
|
||||||
(point, i) => Object.is(point[0], b[i][0]) && Object.is(point[1], b[i][1]),
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Keep coarse roads only outside the detailed tile cores.
|
|
||||||
*
|
|
||||||
* Each tile splits a line into the four regions around its core. Nearly every
|
|
||||||
* coarse road lies wholly inside one of those regions for nearly every tile,
|
|
||||||
* where the clip hands the line back unchanged, so that case is answered from
|
|
||||||
* the line's extent and only a line that reaches a tile's core or edge is
|
|
||||||
* clipped. Clipping every road against every tile froze the page for hundreds
|
|
||||||
* of milliseconds per road load at street level.
|
|
||||||
*
|
|
||||||
* `clip` is injectable for tests.
|
|
||||||
*/
|
|
||||||
export function outsideDetailTiles(roads, tiles, clip = clipTileLine) {
|
|
||||||
const boxes = tiles.map((tile) => tileToBBox(tile.z, tile.x, tile.y));
|
|
||||||
return roads.flatMap((road) => {
|
return roads.flatMap((road) => {
|
||||||
let lines = [lineExtent(road.coordinates)];
|
let lines = [road.coordinates];
|
||||||
for (const box of boxes) {
|
for (const tile of tiles) {
|
||||||
const next = [];
|
const box = tileToBBox(tile.z, tile.x, tile.y);
|
||||||
for (const line of lines) {
|
const regions = [
|
||||||
// Strictly inside one region, and inside the bounds every region
|
{ west: -180, east: box.west, south: -85, north: 85 },
|
||||||
// shares: the other three regions return nothing.
|
{ west: box.east, east: 180, south: -85, north: 85 },
|
||||||
const clear =
|
{ west: box.west, east: box.east, south: -85, north: box.south },
|
||||||
line.west > -180 &&
|
{ west: box.west, east: box.east, south: box.north, north: 85 },
|
||||||
line.east < 180 &&
|
];
|
||||||
line.south > -85 &&
|
lines = lines.flatMap((line) =>
|
||||||
line.north < 85 &&
|
regions.flatMap((region) => clipTileLine(line, region)),
|
||||||
(line.east < box.west ||
|
);
|
||||||
line.west > box.east ||
|
|
||||||
(line.west > box.west &&
|
|
||||||
line.east < box.east &&
|
|
||||||
(line.north < box.south || line.south > box.north)));
|
|
||||||
if (!clear) {
|
|
||||||
const regions = [
|
|
||||||
{ west: -180, east: box.west, south: -85, north: 85 },
|
|
||||||
{ west: box.east, east: 180, south: -85, north: 85 },
|
|
||||||
{ west: box.west, east: box.east, south: -85, north: box.south },
|
|
||||||
{ west: box.west, east: box.east, south: box.north, north: 85 },
|
|
||||||
];
|
|
||||||
for (const region of regions)
|
|
||||||
for (const coordinates of clip(line.coordinates, region))
|
|
||||||
next.push(lineExtent(coordinates));
|
|
||||||
} else if (line.settled) {
|
|
||||||
next.push(line);
|
|
||||||
} else {
|
|
||||||
// The first unclipped pass can still move a vertex by a rounding
|
|
||||||
// step (a road crossing longitude or latitude 0); once a pass
|
|
||||||
// changes nothing, no later one will.
|
|
||||||
const coordinates = unclippedLine(line.coordinates);
|
|
||||||
if (!coordinates) continue;
|
|
||||||
const kept = lineExtent(coordinates);
|
|
||||||
kept.settled = sameLine(coordinates, line.coordinates);
|
|
||||||
next.push(kept);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
lines = next;
|
|
||||||
}
|
}
|
||||||
return lines.map(({ coordinates }) => ({ ...road, coordinates }));
|
return lines.map((coordinates) => ({ ...road, coordinates }));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -69,45 +69,6 @@ export function trafficSurfaceReady(scene) {
|
|||||||
return currentRenderedFrame(scene)?.ready === true;
|
return currentRenderedFrame(scene)?.ready === true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Ground-floor reads for one synchronous pass over road coordinates, asking
|
|
||||||
* the shared cache once per floor cell instead of once per coordinate.
|
|
||||||
*
|
|
||||||
* A road tile puts thousands of coordinates into a few hundred ~111 m cells,
|
|
||||||
* and each shared read formats and looks up two string keys, which added up
|
|
||||||
* to seconds per street-level load. Mesh floors answer per point (the nearest
|
|
||||||
* reading in the cell), so they are read through unchanged; only the DEM
|
|
||||||
* regime is one value per cell. Build a reader per pass: cells warm up
|
|
||||||
* between passes.
|
|
||||||
*/
|
|
||||||
export function floorCellReader(ground) {
|
|
||||||
const read = ground?.cachedGroundFloor;
|
|
||||||
if (!read) return () => undefined;
|
|
||||||
if (ground.meshFloorPreferred?.() !== false)
|
|
||||||
return (lat, lon) => read(lat, lon);
|
|
||||||
const cells = new Map();
|
|
||||||
return (lat, lon) => {
|
|
||||||
const y = lat * 1000,
|
|
||||||
x = lon * 1000;
|
|
||||||
const row = Math.round(y),
|
|
||||||
column = Math.round(x);
|
|
||||||
// The shared cache rounds with toFixed(3). At a cell edge that and
|
|
||||||
// Math.round can pick different cells, so an edge point asks directly.
|
|
||||||
if (!(
|
|
||||||
Math.abs(Math.abs(y - row) - 0.5) > 1e-6 &&
|
|
||||||
Math.abs(Math.abs(x - column) - 0.5) > 1e-6
|
|
||||||
))
|
|
||||||
return read(lat, lon);
|
|
||||||
const key = row * 1e6 + column;
|
|
||||||
let floor = cells.get(key);
|
|
||||||
if (floor === undefined) {
|
|
||||||
floor = read(lat, lon) ?? null;
|
|
||||||
cells.set(key, floor);
|
|
||||||
}
|
|
||||||
return floor;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Preserve bends and insert height samples at most 150 m apart, splitting long roads. */
|
/** Preserve bends and insert height samples at most 150 m apart, splitting long roads. */
|
||||||
export function roadSurfaceChunks(coordinates) {
|
export function roadSurfaceChunks(coordinates) {
|
||||||
const chunks = [];
|
const chunks = [];
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import * as Cesium from 'cesium';
|
import * as Cesium from 'cesium';
|
||||||
import { floorCellReader, roadSurfaceChunks } from './surface.js';
|
import { roadSurfaceChunks } from './surface.js';
|
||||||
import { TRAFFIC_TIMING_ENABLED, DOT_HEIGHT_OFFSET } from './policy.js';
|
import { TRAFFIC_TIMING_ENABLED, DOT_HEIGHT_OFFSET } from './policy.js';
|
||||||
|
|
||||||
export function createTiming({ state: layerState, services, parts, source }) {
|
export function createTiming({ state: layerState, services, parts, source }) {
|
||||||
@@ -190,7 +190,7 @@ export function createTiming({ state: layerState, services, parts, source }) {
|
|||||||
* waypoint-materialization time independently.
|
* waypoint-materialization time independently.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function parseRoadsTimed(roadData, trace, parsedBySource) {
|
function parseRoadsTimed(roadData, trace) {
|
||||||
/* TRACE_ONLY_BEGIN */
|
/* TRACE_ONLY_BEGIN */
|
||||||
const _trafficTimingState = trafficTimingPass(
|
const _trafficTimingState = trafficTimingPass(
|
||||||
trace,
|
trace,
|
||||||
@@ -231,19 +231,11 @@ export function createTiming({ state: layerState, services, parts, source }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const roads = [];
|
const roads = [];
|
||||||
const floorAt = floorCellReader(services.ground);
|
|
||||||
/* TRACE_ONLY_BEGIN */
|
/* TRACE_ONLY_BEGIN */
|
||||||
let _trafficTimingWaypointMaterializationMs = 0;
|
let _trafficTimingWaypointMaterializationMs = 0;
|
||||||
/* TRACE_ONLY_END */
|
/* TRACE_ONLY_END */
|
||||||
for (const road of roadData.roads) {
|
for (const road of roadData.roads) {
|
||||||
if (!road.coordinates || road.coordinates.length < 2) continue;
|
if (!road.coordinates || road.coordinates.length < 2) continue;
|
||||||
// A replacing snapshot repeats records this load already parsed.
|
|
||||||
const known = parsedBySource?.get(road);
|
|
||||||
if (known) {
|
|
||||||
for (const parsed of known) roads.push(parsed);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const first = roads.length;
|
|
||||||
|
|
||||||
for (const coords of roadSurfaceChunks(road.coordinates)) {
|
for (const coords of roadSurfaceChunks(road.coordinates)) {
|
||||||
const type = road.type;
|
const type = road.type;
|
||||||
@@ -253,7 +245,7 @@ export function createTiming({ state: layerState, services, parts, source }) {
|
|||||||
const _trafficTimingMaterializeStart = performance.now();
|
const _trafficTimingMaterializeStart = performance.now();
|
||||||
/* TRACE_ONLY_END */
|
/* TRACE_ONLY_END */
|
||||||
const waypoints = coords.map(([lng, lat]) => {
|
const waypoints = coords.map(([lng, lat]) => {
|
||||||
const floor = floorAt(lat, lng);
|
const floor = services.ground?.cachedGroundFloor?.(lat, lng);
|
||||||
const h =
|
const h =
|
||||||
(Number.isFinite(floor) && Math.abs(floor) <= 9000
|
(Number.isFinite(floor) && Math.abs(floor) <= 9000
|
||||||
? floor
|
? floor
|
||||||
@@ -287,7 +279,6 @@ export function createTiming({ state: layerState, services, parts, source }) {
|
|||||||
flow: road.flow || null,
|
flow: road.flow || null,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
parsedBySource?.set(road, roads.slice(first));
|
|
||||||
}
|
}
|
||||||
/* TRACE_ONLY_BEGIN */
|
/* TRACE_ONLY_BEGIN */
|
||||||
const _trafficTimingMetrics = {
|
const _trafficTimingMetrics = {
|
||||||
|
|||||||
Reference in new issue
Block a user