mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-15 15:52:21 +00:00
Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
72d9f91ad6 | ||
|
|
fda54f6611 | ||
|
|
0760733780 | ||
|
|
6b4b9e43c8 | ||
|
|
c16bfa5abd | ||
|
|
22bdf14be1 | ||
|
|
5aed9ce064 | ||
|
|
96847d5856 | ||
|
|
5eb6515f44 |
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,322 @@
|
||||
# Slowdown audit, November 2026
|
||||
|
||||
A sweep of `main` (`c02ede3bb`, 13 November 2026) for bugs that freeze the page
|
||||
or hold the frame rate down. This page lists what was found, why it happens and
|
||||
how to fix it, and names the pull request that carries each fix.
|
||||
|
||||
[PERFORMANCE.md](PERFORMANCE.md) is the measured baseline of a healthy build;
|
||||
this page is the list of things that fall outside it.
|
||||
|
||||
## How it was measured
|
||||
|
||||
- Keyless dev server, headless Chrome at 1440 x 900 on Apple silicon (Metal
|
||||
through ANGLE), default globe map.
|
||||
- Per scenario: load, a spell at rest, scripted drags and wheel zooms, then a
|
||||
settle. Long tasks were counted with a `PerformanceObserver`, the hot function
|
||||
named from a CPU profile, and `globe.getHeight`, `scene.sampleHeight`,
|
||||
`scene.pick` and friends wrapped to count calls and time.
|
||||
- A long task is one main-thread task over 50 ms. With no layers on, the
|
||||
baseline is two at load (longest about 240 ms) and none afterwards, with zero
|
||||
frames rendered while the camera is still.
|
||||
- Four profiling sessions ran on one machine at the same time, so absolute
|
||||
timings carry some noise. Ratios against the baseline are what to read.
|
||||
|
||||
Nothing here was measured on Google Photorealistic 3D Tiles, on a discrete or
|
||||
integrated PC GPU, or with keyed feeds (AISStream, FIRMS, TomTom). Those rows
|
||||
are marked "read in code".
|
||||
|
||||
## Findings and fixes
|
||||
|
||||
Worst first. "Fix" names the pull request that carries the fix.
|
||||
|
||||
### Measured
|
||||
|
||||
| # | Where | What happens | Fix |
|
||||
| ---: | --- | --- | --- |
|
||||
| 1 | Bike Share, local data layers, Transit, Rail | 18 s freeze switching Bike Share on over New York (2,519 stations); 2.7 s on Data Centers; multi-second freezes with Transit while zooming | #1688, #1689 |
|
||||
| 2 | Shipwrecks | 3 to 5 s freeze when the markers are built over loaded terrain; a share link with the layer on held the loading screen for 22.9 s in one run | #1703 |
|
||||
| 3 | Camera | Rendering stops until reload: at a whole-globe view looking straight down, pan a few times, then scroll one notch | #1704 |
|
||||
| 4 | Street Traffic | Zooming at 250 m over Manhattan: 25 long tasks in 24 s, longest 746 ms, 6.6 s in total | #1705 |
|
||||
| 5 | ALPR cameras | Depth samples inside the overlay paint, repeated on every re-enable: 240 samples and about 1 s across eight toggles. A first-sample freeze of up to 1.9 s was seen in the sweep but not on a second day | #1706 |
|
||||
| 6 | Wind | First zoom after load: up to 14 long tasks (longest 414 ms). Re-measured as one-off shader links, not a standing cost: see below | none proposed |
|
||||
| 7 | Submarine cables | About 2 s of long tasks on load, heap briefly at 1 GB, about 0.5 s again on each re-enable | #1707 (first step), #1722 (batched primitives) |
|
||||
| 8 | Live Flights at whole-globe view with detection labels | Fine on the test machine; at 4x CPU throttle, 8 fps with 143 long tasks in 15 s | #1708 (fleet pass), #1719 (overlay) |
|
||||
| 9 | Radio | 7 to 10 long tasks when the directory loads, longest 447 ms | #1709 |
|
||||
| 10 | Pointer pick | Not reproduced on a second look: see below | none needed |
|
||||
| 11 | CCTV | A still-image camera holds continuous rendering at 60 frames a second although its picture changes about every 10 s | #1710 |
|
||||
|
||||
### Read in code
|
||||
|
||||
| # | Where | What happens | Fix |
|
||||
| ---: | --- | --- | --- |
|
||||
| 12 | `/api/firms` | The whole fire set is filtered, serialised and sent uncompressed on every request; 15 to 55 MB estimated in a heavy season | #1711 |
|
||||
| 13 | AIS store | Both vessel maps are walked on every position message, and sorted whole once past the cap | #1712 |
|
||||
| 14 | Satellite clouds on the 3D Tiles map | A CPU pass over a 4096 x 2048 image, 90 to 180 ms measured on a synthetic canvas, twice in a row | #1713 (canvas path), #1721 (shader) |
|
||||
| 15 | NVG and FLIR styles | Over 100 texture reads per pixel, and 60 renders a second while the style is on. Measured afterwards: the reads cost 0.4 ms a frame at 1440 x 900 and 1.2 ms at 2560 x 1440 | #1718 |
|
||||
| 16 | Wind server | Full 0.25 degree GRIB fields decoded on the event loop. Measured afterwards: 68 ms blocked per warm refresh, about 250 ms on the first, about 400 ms when all six keys refresh together | #1717 |
|
||||
| 17 | Imported geo files and other per-feature ground clamps | Measured afterwards: imported files do not freeze; see below | none needed |
|
||||
|
||||
## Cause and fix, per finding
|
||||
|
||||
### 1. Terrain height reads
|
||||
|
||||
`globe.getHeight` casts a ray against the mesh of the tile under the point, and
|
||||
`scene.sampleHeight` renders a depth pass. Transit asked for every trail fix of
|
||||
every vehicle several times per poll; Bike Share sampled every station in one
|
||||
go. The fix keeps heights per point in one shared reader with a read budget
|
||||
(`src/services/terrainSurface.js`) and spreads the depth samples across frames.
|
||||
|
||||
### 2. Shipwrecks
|
||||
|
||||
`rebuild()` in `src/layers/shipwrecks/index.js` adds 7,958 billboards with
|
||||
`heightReference: CLAMP_TO_GROUND`. Each constructor runs
|
||||
`Billboard._updateClamping`, which intersects a ray with the terrain mesh,
|
||||
synchronously: 4.8 of the 4.9 s. The cost is only paid when terrain is loaded
|
||||
at the time the markers are built, so a cold start with the layer in the URL
|
||||
sometimes escapes it.
|
||||
|
||||
Fix: do not clamp. Wrecks are at sea and the markers already ignore the depth
|
||||
test, so place them on the ellipsoid with `HeightReference.NONE`.
|
||||
|
||||
### 3. Camera: rendering stops after pan then zoom
|
||||
|
||||
Pure Cesium; no application code is involved. Above 1,000 km,
|
||||
`ScreenSpaceCameraController` `handleZoom` rotates the camera about the cross
|
||||
product of the picked point's normal and the screen centre's normal whenever
|
||||
their dot product is between 0 and 1. With the pointer on the exact centre pixel
|
||||
the two are the same vector, yet `n · n` often rounds to `0.9999999999999999`,
|
||||
so the check passes with a zero axis. `Camera.rotate` then throws
|
||||
`DeveloperError: normalized result is not a number` from
|
||||
`Scene.initializeFrame`, outside the scene's own try/catch, so
|
||||
`scene.renderError` never fires and Cesium stops its render loop for good. The
|
||||
pans in the first reproduction only moved the camera to a pose where the
|
||||
rounding goes that way.
|
||||
|
||||
Fix: make `camera.rotate` ignore a zero or non-finite axis, and recover from any
|
||||
stopped loop by restoring the last drawn camera pose and restarting it a bounded
|
||||
number of times before showing Cesium's error panel.
|
||||
|
||||
### 4. Street Traffic road loading
|
||||
|
||||
Three costs in one task after the detail tiles arrive, in
|
||||
`src/layers/traffic/source.js`:
|
||||
|
||||
- `outsideDetailTiles` clips every coarse z12 road against every loaded detail
|
||||
tile (up to 16) for four regions.
|
||||
- The composed set is then handed on with `replace: true`, so
|
||||
`ingestion.js` parses every road again although each tile was parsed as it
|
||||
streamed.
|
||||
- `parseRoads` (`model.js`) asks `cachedGroundFloor` once per coordinate, and
|
||||
each call builds several string keys.
|
||||
|
||||
Fix: clip once per coarse tile against the detail tiles' union box and slice the
|
||||
work across frames (or do it in the vector-tile worker); reuse the roads parsed
|
||||
while streaming and parse only the clipped coarse remainder; look the floor up
|
||||
once per floor cell within a parse.
|
||||
|
||||
This is separate from the one-viewport-tile-at-a-time loading recorded in
|
||||
[KNOWN-ISSUES.md](KNOWN-ISSUES.md).
|
||||
|
||||
### 5. ALPR cameras
|
||||
|
||||
`refineAnchors` and `anchorFor` in `src/layers/alpr/overlay.js` call
|
||||
`scene.sampleHeight` from the overlay's paint, four per paint. Each is a depth
|
||||
render and a `readPixels`; the first one also compiles the pick shader. Results
|
||||
are kept on the entity, so a rebuild or a re-enable samples everything again.
|
||||
|
||||
Fix: on a terrain globe read `globe.getHeight` through the shared terrain reader
|
||||
instead of sampling; keep the answers in a map keyed by camera that outlives the
|
||||
entities; move what sampling remains out of the paint into a queue of one per
|
||||
frame.
|
||||
|
||||
### 6. Wind
|
||||
|
||||
The first reading blamed the layer's 916,476 vertices for slow depth reads
|
||||
during zoom. Measuring again did not support that: depth reads cost the same
|
||||
with and without Wind, hiding every wind primitive left pick time unchanged, and
|
||||
the stalls appear on the first zoom only, as shader programs link (seven
|
||||
programs, about 0.5 s, long tasks up to 166 ms). Later zooms had none.
|
||||
|
||||
The roughly 30 renders a second are the animation itself. It is already capped
|
||||
at 30 Hz and stops when paused, hidden, out of view or under reduced motion;
|
||||
lowering the rate would change how the layer looks. `disable()` releases the
|
||||
baked geometry on purpose, at the price of one task of about 130 ms on
|
||||
re-enable.
|
||||
|
||||
A third look, timing every program link on a preview build, settled it: Wind
|
||||
owns two shader programs, both link during load in 9 to 18 ms, and none ever
|
||||
linked during a zoom. The programs that do link on a zoom are Cesium's own
|
||||
globe-surface variants, with Wind on or off, and Cesium links them
|
||||
synchronously. No fix is proposed.
|
||||
|
||||
### 7. Submarine cables
|
||||
|
||||
`src/layers/submarineCables/ingestion.js` loads both collections through
|
||||
`Cesium.GeoJsonDataSource` with `clampToGround`. Switching the layer on is one
|
||||
task of about 1.4 s, and 1.2 s of it is Cesium reading the terrain height for
|
||||
the 1,917 ground-clamped landing markers, all created in the first frame,
|
||||
including the ones behind the globe.
|
||||
|
||||
Fix, first step: start the landing markers hidden and let the existing horizon
|
||||
sweep show the visible ones a few dozen per frame.
|
||||
|
||||
Fix, second step: draw from batched primitives in place of the entity data
|
||||
sources: one ground polyline primitive for the lines, one billboard collection
|
||||
for the landing markers, one point collection and one static polyline primitive
|
||||
for the reference stems, all kept while the layer is off. That removes the
|
||||
visualiser tasks, the stall on switch-off and on re-enable, and about 900 MB of
|
||||
heap. The first switch-on still links shaders and builds the ground-line
|
||||
geometry once.
|
||||
|
||||
### 8. Live Flights at whole-globe view
|
||||
|
||||
Two costs that scale with the 7,500 aircraft of a full poll:
|
||||
|
||||
- The fleet pass (`src/layers/flights/rendering.js`, every 80 ms) dead-reckons
|
||||
each visible aircraft and asks `cachedGroundFloor` for it, including aircraft
|
||||
at cruise altitude that can never touch the floor. Each lookup formats numbers
|
||||
to strings several times and rewrites a cache entry: 40% of the busy time.
|
||||
- The detection overlay (`src/data/detection.js` `_drawOverlay`) collects every
|
||||
detectable object and strokes every bracket on every frame.
|
||||
|
||||
Fix for the first: skip the floor path for a contact clearly above any terrain,
|
||||
make the floor-cell key a number, and stop promoting the cache entry on every
|
||||
read.
|
||||
|
||||
The second turned out smaller than it looked. The bracket strokes are already
|
||||
one path per colour, and collecting the objects is about 3 ms of a 56 ms frame
|
||||
at 4x throttle once the first fix is in. What costs is the per-object loop in
|
||||
`_drawOverlay` (projection, occlusion, alpha, twelve path calls per bracket).
|
||||
|
||||
Fix for the second: keep what a sweep built and reuse it on the next paint when
|
||||
the view and every contact are exactly what it was built from, compared value
|
||||
by value so no layer has to announce a change. Between two fleet passes most
|
||||
paints qualify, which cuts frame work by a fifth to a third on a still camera
|
||||
at full speed. At 4x throttle a frame outlasts a fleet pass, no paint
|
||||
qualifies, and the gain is a few milliseconds; the fleet pass is what
|
||||
dominates there.
|
||||
|
||||
### 9. Radio
|
||||
|
||||
`stationTags()` in `src/layers/radio/categories.js` splits and normalises a
|
||||
station's tags on every `hasTag` call, and `buildRadioCategories` runs that for
|
||||
every station against every category, twice.
|
||||
|
||||
Fix: normalise once per station at ingest and count categories from that.
|
||||
|
||||
### 10. Pointer pick
|
||||
|
||||
Three sessions saw a single `scene.pick` of 207 to 342 ms during a gesture and
|
||||
attributed it to `pickPointer` in `src/voice/pointerPick.js`. Timing every pick
|
||||
across pointer rests, drags and wheel bursts did not bear that out: of 78 picks
|
||||
with CCTV on over London all but two took 3 to 8 ms, and with five heavy layers
|
||||
on, 81 picks took 5 to 30 ms apart from the first (135 ms). `pickPointer` picks
|
||||
at most once per pointer rest.
|
||||
|
||||
The slow pick is the first pick after a layer appears, once per page load, most
|
||||
likely the pick shader compiling, and its stack is CCTV's own hover pick
|
||||
(`src/layers/cctv/hover.js`). No fix is proposed: removing the dwell pick would
|
||||
only move the one-off cost to the next click.
|
||||
|
||||
### 11. CCTV still-image cameras
|
||||
|
||||
`src/layers/cctv/projection.js` holds continuous rendering for as long as any
|
||||
projected camera is active and redraws the projection every frame.
|
||||
|
||||
Fix: hold only for video. For a still image, request one render when a new image
|
||||
lands or while a focus fade runs.
|
||||
|
||||
### 12. `/api/firms`
|
||||
|
||||
`server/providers/firms.js` builds the payload and calls `JSON.stringify` per
|
||||
request, with no kept body and no compression, and the client refetches every
|
||||
ten minutes.
|
||||
|
||||
Fix: keep the response bytes and their gzip and serve those, as the OpenSky
|
||||
provider does. The route filters to the trailing 24 hours at serve time, so the
|
||||
bytes can only be reused while that filter keeps the same records (about a
|
||||
minute at a time): bursts and several clients gain most, and every response is
|
||||
about seven times smaller.
|
||||
|
||||
### 13. AIS store
|
||||
|
||||
`server/providers/vessels/ais-store.js` calls `pruneAisStreamCache()` for every
|
||||
message; the static-data map is never pruned.
|
||||
|
||||
Fix: prune on a timer, evict in insertion order, and drop a vessel's static
|
||||
entry with the vessel.
|
||||
|
||||
### 14. Satellite clouds on the 3D Tiles map
|
||||
|
||||
`src/layers/weather/infraredImage.js` runs `getImageData`, a per-pixel power
|
||||
curve and `putImageData` over the whole canvas.
|
||||
|
||||
Fix: a 256-entry lookup table applied in place for the canvas path, which
|
||||
halves it; then, for the shell on the 3D Tiles map, the same curve in its
|
||||
fragment shader from a strength uniform, so the image is never read back and a
|
||||
strength change builds no new image.
|
||||
|
||||
### 15. NVG and FLIR styles
|
||||
|
||||
`src/styles/surveillance.js` loops 11 x 11 texture reads per pixel and
|
||||
`src/styles/thermal.js` totals 106. Timed on the GPU, those loops cost about
|
||||
0.4 ms a frame at 1440 x 900 and 1.2 ms at 2560 x 1440 on the test machine:
|
||||
real, but small there. The standing cost is that a style with animated grain
|
||||
holds continuous rendering at 60 frames a second.
|
||||
|
||||
Fix: split the bloom into a row pass and a column pass, which gives the same
|
||||
picture from a fifth to a half of the reads, and step the grain's clock at
|
||||
15 Hz, asking for one frame per step instead of holding the render loop. The
|
||||
grain then changes 15 times a second, which is visible in motion.
|
||||
|
||||
Switching on any style stage at all costs more than the bloom did (1.8 ms a
|
||||
frame at 1440 x 900 for the one-line noir style). That was not looked into.
|
||||
|
||||
### 16. Wind server
|
||||
|
||||
`server/providers/wind/decode.js` decodes about a million doubles per message
|
||||
synchronously, hourly, for six keys.
|
||||
|
||||
Fix: decode on a worker thread, and keep the sampled U and V of the last
|
||||
forecast file per model so that choosing an overlay fetches only its field.
|
||||
|
||||
### 17. Per-feature ground clamps
|
||||
|
||||
`heightReference: CLAMP_TO_GROUND` per feature is also set in
|
||||
`installations/rendering.js`, `alpr/presentation.js`, `ghostMaps/index.js`,
|
||||
`earthquakes/index.js`, `airQuality/index.js`, `news/index.js` and
|
||||
`osmPlaces/index.js`. Their counts were small in these runs (30 to 1,500) and
|
||||
none froze.
|
||||
|
||||
Imported geo files were the one unbounded case, and measuring them cleared
|
||||
them: 20,000 generated points (the import cap) finish in about 1.5 s with a
|
||||
longest task near 0.5 s, because `settleGroundPins` in
|
||||
`src/data/geoFileImport.js` already places pins with `HeightReference.NONE` and
|
||||
resolves heights 256 at a time with a yield between batches. No fix is needed.
|
||||
|
||||
## Checked and fine
|
||||
|
||||
- Start-up of a production build: viewer ready in about 0.5 s, loading screen
|
||||
gone at 1.6 s, three long tasks totalling about 300 ms.
|
||||
- Idle: no frames rendered with no layers on; every render hold was released
|
||||
when its layer went off.
|
||||
- Leaks: all 41 layers toggled three times and ten of them eight times; no
|
||||
timer, scene listener, primitive or data source grew per cycle. The heap
|
||||
levelled off near 320 MB over 7.5 minutes of toggling eleven layers.
|
||||
- At most one long task after load: Military, Satellites (including 11,903
|
||||
Starlink points), Earthquakes, News, Wildlife, GNSS, Drone Airspace, Military
|
||||
Installations, Fire Perimeters, rain radar, lightning, and satellite clouds on
|
||||
the globe map.
|
||||
- Live Flights at city scale with the same 7,500 aircraft loaded: 60 fps, no
|
||||
long tasks.
|
||||
|
||||
## Not covered
|
||||
|
||||
- Google Photorealistic 3D Tiles: nothing was run there. Depth sampling is the
|
||||
only height source on that map, so findings 4, 5 and 17 may be worse.
|
||||
- Live Vessels, FIRMS with data, TomTom traffic, Local ADS-B.
|
||||
- Frame rate on anything but one Apple silicon machine.
|
||||
- One all-layers run over Manhattan grew the heap from 346 to 638 MB in 60 s;
|
||||
whether that is retained was not established.
|
||||
- One Satellites load showed a 5.2 s long task once and never again in four more
|
||||
loads.
|
||||
@@ -6,6 +6,9 @@ specification and should not be used to predict performance on untested systems.
|
||||
The original capture artifacts are not included here, so this page records
|
||||
results rather than defining a runnable benchmark.
|
||||
|
||||
For slowdowns found since, with their causes and fixes, see
|
||||
[PERFORMANCE-AUDIT-2026-11.md](PERFORMANCE-AUDIT-2026-11.md).
|
||||
|
||||
## Test context
|
||||
|
||||
The baseline was captured on 22 August 2026 with these conditions:
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { roadIdentity } from './retention.js';
|
||||
import * as Cesium from 'cesium';
|
||||
import {
|
||||
floorCellReader,
|
||||
prepareRoadSurfaces,
|
||||
roadHeightStations,
|
||||
trafficSurfaceReady,
|
||||
@@ -302,7 +301,6 @@ export function createFlow({ state: layerState, services, parts, source }) {
|
||||
}
|
||||
}
|
||||
if (depthHeight !== undefined) layerState._viewHeightEstimate = depthHeight;
|
||||
const floorAt = floorCellReader(services.ground);
|
||||
for (const road of roads) {
|
||||
road.key ||= roadIdentity(road);
|
||||
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++) {
|
||||
const [lon, lat] = road.coords[i];
|
||||
const floor = floorAt(lat, lon);
|
||||
const floor = services.ground?.cachedGroundFloor?.(lat, lon);
|
||||
const height =
|
||||
Number.isFinite(floor) && Math.abs(floor) <= 9000
|
||||
? Math.max(floor, layerState._viewHeightEstimate)
|
||||
|
||||
@@ -39,10 +39,10 @@ export function createTrafficLayer({ services, source }) {
|
||||
parts.controls = createControls(context);
|
||||
parts.lifecycle = createLifecycle(context);
|
||||
state._parseRoads = TRAFFIC_TIMING_ENABLED
|
||||
? (data, trace, parsedBySource) =>
|
||||
? (data, trace) =>
|
||||
trace
|
||||
? parts.timing.parseRoadsTimed(data, trace, parsedBySource)
|
||||
: parts.model.parseRoads(data, trace, parsedBySource)
|
||||
? parts.timing.parseRoadsTimed(data, trace)
|
||||
: parts.model.parseRoads(data)
|
||||
: parts.model.parseRoads;
|
||||
|
||||
state._loadRoadsForBounds = TRAFFIC_TIMING_ENABLED
|
||||
|
||||
@@ -348,16 +348,11 @@ export function createIngestion({
|
||||
layerState._retainedPreview = false;
|
||||
layerState._detailRoadsReady = false;
|
||||
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) => {
|
||||
if (generation !== layerState._loadGeneration || passSignal.aborted)
|
||||
return;
|
||||
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 });
|
||||
if (data.replace) streamed = parsed;
|
||||
else streamed.push(...parsed);
|
||||
@@ -536,7 +531,6 @@ export function createIngestion({
|
||||
if (altitude > FAST_FETCH_ALTITUDE) return;
|
||||
|
||||
streamed = [];
|
||||
parsedBySource = new WeakMap();
|
||||
majorPass = false;
|
||||
// Detailed pass: fetch the full road graph (tertiary, residential, etc.)
|
||||
console.log(`[Data:Traffic] Full fetch local roads [${cacheKey}]`);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import { floorCellReader, roadSurfaceChunks } from './surface.js';
|
||||
import { roadSurfaceChunks } from './surface.js';
|
||||
import { t, translateKnown } from '../../i18n/index.js';
|
||||
import {
|
||||
flowDensityMult,
|
||||
@@ -13,35 +13,22 @@ import {
|
||||
} from './policy.js';
|
||||
|
||||
export function createModel({ state: layerState, services, parts, source }) {
|
||||
/**
|
||||
* Build scene waypoints from source records; thinning and terrain remain rendering policy.
|
||||
*
|
||||
* `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) {
|
||||
/** Build scene waypoints from source records; thinning and terrain remain rendering policy. */
|
||||
function parseRoads(roadData) {
|
||||
if (!roadData || !roadData.roads) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const roads = [];
|
||||
const floorAt = floorCellReader(services.ground);
|
||||
for (const road of roadData.roads) {
|
||||
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)) {
|
||||
const type = road.type;
|
||||
const oneway = road.oneway;
|
||||
// Pre-compute Cartesian3 waypoints (lon, lat, height) for fast lerp animation
|
||||
const waypoints = coords.map(([lng, lat]) => {
|
||||
const floor = floorAt(lat, lng);
|
||||
const floor = services.ground?.cachedGroundFloor?.(lat, lng);
|
||||
const h =
|
||||
(Number.isFinite(floor) && Math.abs(floor) <= 9000
|
||||
? floor
|
||||
@@ -73,7 +60,6 @@ export function createModel({ state: layerState, services, parts, source }) {
|
||||
flow: road.flow || null,
|
||||
});
|
||||
}
|
||||
parsedBySource?.set(road, roads.slice(first));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/** Longitude/latitude extent of one line; NaN coordinates leave it unusable. */
|
||||
function lineExtent(coordinates) {
|
||||
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));
|
||||
/** Keep coarse roads only outside the detailed tile cores. */
|
||||
export function outsideDetailTiles(roads, tiles) {
|
||||
return roads.flatMap((road) => {
|
||||
let lines = [lineExtent(road.coordinates)];
|
||||
for (const box of boxes) {
|
||||
const next = [];
|
||||
for (const line of lines) {
|
||||
// Strictly inside one region, and inside the bounds every region
|
||||
// shares: the other three regions return nothing.
|
||||
const clear =
|
||||
line.west > -180 &&
|
||||
line.east < 180 &&
|
||||
line.south > -85 &&
|
||||
line.north < 85 &&
|
||||
(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;
|
||||
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 }));
|
||||
return lines.map((coordinates) => ({ ...road, coordinates }));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -69,45 +69,6 @@ export function trafficSurfaceReady(scene) {
|
||||
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. */
|
||||
export function roadSurfaceChunks(coordinates) {
|
||||
const chunks = [];
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
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';
|
||||
|
||||
export function createTiming({ state: layerState, services, parts, source }) {
|
||||
@@ -190,7 +190,7 @@ export function createTiming({ state: layerState, services, parts, source }) {
|
||||
* waypoint-materialization time independently.
|
||||
*/
|
||||
|
||||
function parseRoadsTimed(roadData, trace, parsedBySource) {
|
||||
function parseRoadsTimed(roadData, trace) {
|
||||
/* TRACE_ONLY_BEGIN */
|
||||
const _trafficTimingState = trafficTimingPass(
|
||||
trace,
|
||||
@@ -231,19 +231,11 @@ export function createTiming({ state: layerState, services, parts, source }) {
|
||||
}
|
||||
|
||||
const roads = [];
|
||||
const floorAt = floorCellReader(services.ground);
|
||||
/* TRACE_ONLY_BEGIN */
|
||||
let _trafficTimingWaypointMaterializationMs = 0;
|
||||
/* TRACE_ONLY_END */
|
||||
for (const road of roadData.roads) {
|
||||
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)) {
|
||||
const type = road.type;
|
||||
@@ -253,7 +245,7 @@ export function createTiming({ state: layerState, services, parts, source }) {
|
||||
const _trafficTimingMaterializeStart = performance.now();
|
||||
/* TRACE_ONLY_END */
|
||||
const waypoints = coords.map(([lng, lat]) => {
|
||||
const floor = floorAt(lat, lng);
|
||||
const floor = services.ground?.cachedGroundFloor?.(lat, lng);
|
||||
const h =
|
||||
(Number.isFinite(floor) && Math.abs(floor) <= 9000
|
||||
? floor
|
||||
@@ -287,7 +279,6 @@ export function createTiming({ state: layerState, services, parts, source }) {
|
||||
flow: road.flow || null,
|
||||
});
|
||||
}
|
||||
parsedBySource?.set(road, roads.slice(first));
|
||||
}
|
||||
/* TRACE_ONLY_BEGIN */
|
||||
const _trafficTimingMetrics = {
|
||||
|
||||
Reference in new issue
Block a user