mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Select street traffic roads through the query and camera ports
Road selection reads the depth probe and each waypoint's window position through the query port and the eye through the camera port, and keeps its point maths in plain geo helpers. Flow leaves the Cesium allow-list.
This commit is contained in:
1 parent
a8cf63999b
commit
ecaa965b69
3 files changed
+131
-35
No files matched your search
+38
-34
@@ -1,5 +1,7 @@
|
|||||||
import { roadIdentity } from './retention.js';
|
import { roadIdentity } from './retention.js';
|
||||||
import * as Cesium from 'cesium';
|
import { fromDegrees, toCartographicRadians } from '../../geo/ellipsoid.js';
|
||||||
|
import { distance, dot, sub } from '../../geo/vector.js';
|
||||||
|
import { findCamera, findQuery } from '../../render/rendererOf.js';
|
||||||
import {
|
import {
|
||||||
prepareRoadSurfaces,
|
prepareRoadSurfaces,
|
||||||
roadHeightStations,
|
roadHeightStations,
|
||||||
@@ -14,6 +16,19 @@ import {
|
|||||||
import { msg } from '../../i18n/index.js';
|
import { msg } from '../../i18n/index.js';
|
||||||
import { nextTask } from '../../sources/vectorTiles.js';
|
import { nextTask } from '../../sources/vectorTiles.js';
|
||||||
|
|
||||||
|
/** Squared straight-line distance, as the default renderer computes it. */
|
||||||
|
function distanceSquared(a, b) {
|
||||||
|
const d = sub(a, b);
|
||||||
|
return dot(d, d);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Copy `from`'s coordinates into the point `to`, in place. */
|
||||||
|
function copyPoint(from, to) {
|
||||||
|
to.x = from.x;
|
||||||
|
to.y = from.y;
|
||||||
|
to.z = from.z;
|
||||||
|
}
|
||||||
|
|
||||||
export function createFlow({ state: layerState, services, parts, source }) {
|
export function createFlow({ state: layerState, services, parts, source }) {
|
||||||
const { registerDynamicCredit, TOMTOM_CREDIT } = services.credits;
|
const { registerDynamicCredit, TOMTOM_CREDIT } = services.credits;
|
||||||
const { fetchFlowForBounds } = source;
|
const { fetchFlowForBounds } = source;
|
||||||
@@ -264,8 +279,7 @@ export function createFlow({ state: layerState, services, parts, source }) {
|
|||||||
const localHeights = [];
|
const localHeights = [];
|
||||||
for (const prepared of preparedRoads.values()) {
|
for (const prepared of preparedRoads.values()) {
|
||||||
const height =
|
const height =
|
||||||
Cesium.Cartographic.fromCartesian(prepared.waypoints[0]).height -
|
toCartographicRadians(prepared.waypoints[0]).height - DOT_HEIGHT_OFFSET;
|
||||||
DOT_HEIGHT_OFFSET;
|
|
||||||
if (Number.isFinite(height) && Math.abs(height) <= 9000)
|
if (Number.isFinite(height) && Math.abs(height) <= 9000)
|
||||||
localHeights.push(height);
|
localHeights.push(height);
|
||||||
if (localHeights.length === 9) break;
|
if (localHeights.length === 9) break;
|
||||||
@@ -277,15 +291,17 @@ export function createFlow({ state: layerState, services, parts, source }) {
|
|||||||
}
|
}
|
||||||
// A rendered center-depth pick follows the current city's elevation even
|
// A rendered center-depth pick follows the current city's elevation even
|
||||||
// while older coarse samples are being replaced. It is selection-only.
|
// while older coarse samples are being replaced. It is selection-only.
|
||||||
|
// The renderer's query port answers the pick and the projections below;
|
||||||
|
// its camera port says where the eye is.
|
||||||
|
const query = findQuery(layerState._viewer);
|
||||||
let depthHeight;
|
let depthHeight;
|
||||||
if (scene.pickPositionSupported && scene.canvas?.clientWidth) {
|
if (query && scene.canvas?.clientWidth) {
|
||||||
const probe = new Cesium.Cartesian2(
|
try {
|
||||||
|
const hit = query.pickPosition(
|
||||||
scene.canvas.clientWidth / 2,
|
scene.canvas.clientWidth / 2,
|
||||||
scene.canvas.clientHeight * 0.6,
|
scene.canvas.clientHeight * 0.6,
|
||||||
);
|
);
|
||||||
try {
|
const height = hit && toCartographicRadians(hit)?.height;
|
||||||
const hit = scene.pickPosition(probe);
|
|
||||||
const height = hit && Cesium.Cartographic.fromCartesian(hit).height;
|
|
||||||
if (Number.isFinite(height) && Math.abs(height) <= 9000)
|
if (Number.isFinite(height) && Math.abs(height) <= 9000)
|
||||||
depthHeight = height;
|
depthHeight = height;
|
||||||
} catch {
|
} catch {
|
||||||
@@ -298,7 +314,7 @@ export function createFlow({ state: layerState, services, parts, source }) {
|
|||||||
const prepared = !refining && preparedRoads.get(road.key);
|
const prepared = !refining && preparedRoads.get(road.key);
|
||||||
if (prepared) {
|
if (prepared) {
|
||||||
for (let i = 0; i < road.waypoints.length; i++)
|
for (let i = 0; i < road.waypoints.length; i++)
|
||||||
Cesium.Cartesian3.clone(prepared.waypoints[i], road.waypoints[i]);
|
copyPoint(prepared.waypoints[i], road.waypoints[i]);
|
||||||
} else if (
|
} else if (
|
||||||
(!road.surfaceReady || refining) &&
|
(!road.surfaceReady || refining) &&
|
||||||
(localHeights.length || depthHeight !== undefined)
|
(localHeights.length || depthHeight !== undefined)
|
||||||
@@ -306,9 +322,7 @@ export function createFlow({ state: layerState, services, parts, source }) {
|
|||||||
road.surfaceReady = false;
|
road.surfaceReady = false;
|
||||||
// Opposite source directions may share waypoint arrays. Provisional
|
// Opposite source directions may share waypoint arrays. Provisional
|
||||||
// selection must not overwrite an already measured sibling.
|
// selection must not overwrite an already measured sibling.
|
||||||
road.waypoints = road.waypoints.map((point) =>
|
road.waypoints = road.waypoints.map(({ x, y, z }) => ({ x, y, z }));
|
||||||
Cesium.Cartesian3.clone(point),
|
|
||||||
);
|
|
||||||
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 = services.ground?.cachedGroundFloor?.(lat, lon);
|
const floor = services.ground?.cachedGroundFloor?.(lat, lon);
|
||||||
@@ -316,20 +330,17 @@ export function createFlow({ state: layerState, services, parts, source }) {
|
|||||||
Number.isFinite(floor) && Math.abs(floor) <= 9000
|
Number.isFinite(floor) && Math.abs(floor) <= 9000
|
||||||
? Math.max(floor, layerState._viewHeightEstimate)
|
? Math.max(floor, layerState._viewHeightEstimate)
|
||||||
: layerState._viewHeightEstimate;
|
: layerState._viewHeightEstimate;
|
||||||
Cesium.Cartesian3.fromDegrees(
|
copyPoint(
|
||||||
lon,
|
fromDegrees(lon, lat, height + DOT_HEIGHT_OFFSET),
|
||||||
lat,
|
|
||||||
height + DOT_HEIGHT_OFFSET,
|
|
||||||
undefined,
|
|
||||||
road.waypoints[i],
|
road.waypoints[i],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const camera = findCamera(layerState._viewer)?.pose().position;
|
||||||
const selected = parts.rendering
|
const selected = parts.rendering
|
||||||
.visibleRoadsForAltitude(roads, altitude)
|
.visibleRoadsForAltitude(roads, altitude)
|
||||||
.filter((road) => {
|
.filter((road) => {
|
||||||
const camera = layerState._viewer.camera?.positionWC;
|
|
||||||
const localHeight = Math.max(
|
const localHeight = Math.max(
|
||||||
0,
|
0,
|
||||||
altitude - (layerState._viewHeightEstimate || 0),
|
altitude - (layerState._viewHeightEstimate || 0),
|
||||||
@@ -340,9 +351,7 @@ export function createFlow({ state: layerState, services, parts, source }) {
|
|||||||
if (
|
if (
|
||||||
camera &&
|
camera &&
|
||||||
road.waypoints.every(
|
road.waypoints.every(
|
||||||
(point) =>
|
(point) => distanceSquared(camera, point) > radius * radius,
|
||||||
Cesium.Cartesian3.distanceSquared(camera, point) >
|
|
||||||
radius * radius,
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return false;
|
return false;
|
||||||
@@ -352,11 +361,8 @@ export function createFlow({ state: layerState, services, parts, source }) {
|
|||||||
minY = Infinity,
|
minY = Infinity,
|
||||||
maxX = -Infinity,
|
maxX = -Infinity,
|
||||||
maxY = -Infinity;
|
maxY = -Infinity;
|
||||||
for (const point of road.waypoints) {
|
for (const point of query ? road.waypoints : []) {
|
||||||
const screen = Cesium.SceneTransforms.worldToWindowCoordinates(
|
const screen = query.project(point);
|
||||||
scene,
|
|
||||||
point,
|
|
||||||
);
|
|
||||||
if (!screen) continue;
|
if (!screen) continue;
|
||||||
minX = Math.min(minX, screen.x);
|
minX = Math.min(minX, screen.x);
|
||||||
maxX = Math.max(maxX, screen.x);
|
maxX = Math.max(maxX, screen.x);
|
||||||
@@ -368,11 +374,10 @@ export function createFlow({ state: layerState, services, parts, source }) {
|
|||||||
road.baseDensityWeight ??= road.densityWeight || 1;
|
road.baseDensityWeight ??= road.densityWeight || 1;
|
||||||
road.densityWeight =
|
road.densityWeight =
|
||||||
road.baseDensityWeight /
|
road.baseDensityWeight /
|
||||||
Math.max(
|
Math.max(1, distance(camera, road.waypoints[0]) / 1500);
|
||||||
1,
|
|
||||||
Cesium.Cartesian3.distance(camera, road.waypoints[0]) / 1500,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
// Without a query port nothing is projected: every road is kept.
|
||||||
|
if (!query) return true;
|
||||||
return (
|
return (
|
||||||
maxX >= -100 &&
|
maxX >= -100 &&
|
||||||
minX <= scene.canvas.clientWidth + 100 &&
|
minX <= scene.canvas.clientWidth + 100 &&
|
||||||
@@ -386,12 +391,11 @@ export function createFlow({ state: layerState, services, parts, source }) {
|
|||||||
roadDotBudget(altitude),
|
roadDotBudget(altitude),
|
||||||
);
|
);
|
||||||
const admitted = selected.filter((r, i) => budgets[i] > 0);
|
const admitted = selected.filter((r, i) => budgets[i] > 0);
|
||||||
const camera = layerState._viewer.camera?.positionWC;
|
|
||||||
if (camera)
|
if (camera)
|
||||||
admitted.sort(
|
admitted.sort(
|
||||||
(a, b) =>
|
(a, b) =>
|
||||||
Cesium.Cartesian3.distanceSquared(camera, a.waypoints[0]) -
|
distanceSquared(camera, a.waypoints[0]) -
|
||||||
Cesium.Cartesian3.distanceSquared(camera, b.waypoints[0]),
|
distanceSquared(camera, b.waypoints[0]),
|
||||||
);
|
);
|
||||||
// The wide pass is a fast preview when detailed tiles follow. Preparing
|
// The wide pass is a fast preview when detailed tiles follow. Preparing
|
||||||
// the entire coarse graph only to replace it doubles surface work.
|
// the entire coarse graph only to replace it doubles surface work.
|
||||||
@@ -438,7 +442,7 @@ export function createFlow({ state: layerState, services, parts, source }) {
|
|||||||
const prepared = !refining && preparedRoads.get(road.key);
|
const prepared = !refining && preparedRoads.get(road.key);
|
||||||
if (prepared) {
|
if (prepared) {
|
||||||
for (let i = 0; i < road.waypoints.length; i++)
|
for (let i = 0; i < road.waypoints.length; i++)
|
||||||
Cesium.Cartesian3.clone(prepared.waypoints[i], road.waypoints[i]);
|
copyPoint(prepared.waypoints[i], road.waypoints[i]);
|
||||||
for (let i = 0; i < road.segmentDist.length; i++)
|
for (let i = 0; i < road.segmentDist.length; i++)
|
||||||
road.segmentDist[i] = prepared.segmentDist[i];
|
road.segmentDist[i] = prepared.segmentDist[i];
|
||||||
road.surfaceReady = true;
|
road.surfaceReady = true;
|
||||||
|
|||||||
@@ -708,3 +708,96 @@ test('parsed roads carry plain ECEF waypoints that dots spawn on and move along'
|
|||||||
assert.ok(Number.isFinite(after.x) && Number.isFinite(after.z));
|
assert.ok(Number.isFinite(after.x) && Number.isFinite(after.z));
|
||||||
assert.ok(C.Cartesian3.distance(before, after) > 0, 'the dot moves');
|
assert.ok(C.Cartesian3.distance(before, after) > 0, 'the dot moves');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('selection asks the query port for depth and screen positions, and the camera port for the eye', async () => {
|
||||||
|
const { createFlow } = await import('./flow.js');
|
||||||
|
const { bindRenderer } = await import('../../render/rendererOf.js');
|
||||||
|
const road = (lon) => ({
|
||||||
|
type: 'primary',
|
||||||
|
oneway: 1,
|
||||||
|
coords: [
|
||||||
|
[lon, 0],
|
||||||
|
[lon + 0.0001, 0],
|
||||||
|
],
|
||||||
|
waypoints: [
|
||||||
|
C.Cartesian3.fromDegrees(lon, 0),
|
||||||
|
C.Cartesian3.fromDegrees(lon + 0.0001, 0),
|
||||||
|
],
|
||||||
|
segmentDist: [11],
|
||||||
|
});
|
||||||
|
const viewer = {
|
||||||
|
scene: {
|
||||||
|
canvas: { clientWidth: 1000, clientHeight: 800 },
|
||||||
|
globe: { show: true, tilesLoaded: true, getHeight: () => 0 },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const picks = [];
|
||||||
|
let poses = 0;
|
||||||
|
const eye = C.Cartesian3.fromDegrees(0, 0, 1250);
|
||||||
|
// Near roads land 300 px per 0.001° east of the centre; anything past
|
||||||
|
// 0.1° is drawn on screen, so only the eye's range can drop it.
|
||||||
|
const unbind = bindRenderer(viewer, {
|
||||||
|
camera: {
|
||||||
|
pose: () => (poses++, { position: { x: eye.x, y: eye.y, z: eye.z } }),
|
||||||
|
},
|
||||||
|
query: {
|
||||||
|
pickPosition(x, y) {
|
||||||
|
picks.push([x, y]);
|
||||||
|
return C.Cartesian3.fromDegrees(0, 0, 400);
|
||||||
|
},
|
||||||
|
project(point) {
|
||||||
|
const lon = C.Math.toDegrees(
|
||||||
|
C.Cartographic.fromCartesian(point).longitude,
|
||||||
|
);
|
||||||
|
return { x: lon > 0.1 ? 500 : 500 + lon * 300000, y: 400 };
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const rendered = [];
|
||||||
|
const state = {
|
||||||
|
_dots: [],
|
||||||
|
_viewer: viewer,
|
||||||
|
_enabled: true,
|
||||||
|
_loadGeneration: 1,
|
||||||
|
_roadSource: 'OpenStreetMap',
|
||||||
|
_flowPending: 0,
|
||||||
|
_activeFetchAbort: new AbortController(),
|
||||||
|
};
|
||||||
|
const flow = createFlow({
|
||||||
|
state,
|
||||||
|
services: { credits: { registerDynamicCredit() {} } },
|
||||||
|
source: {},
|
||||||
|
parts: {
|
||||||
|
model: {
|
||||||
|
allocateRoadDotBudgets: (roads) => roads.map(() => 1),
|
||||||
|
recolorDotsInPlace() {},
|
||||||
|
},
|
||||||
|
rendering: {
|
||||||
|
visibleRoadsForAltitude: (roads) => roads,
|
||||||
|
renderRoadsForAltitude(roads) {
|
||||||
|
rendered.push(...roads);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
const near = road(0.0001),
|
||||||
|
offCanvas = road(0.005),
|
||||||
|
outOfRange = road(0.2);
|
||||||
|
await flow.applyFlowThenRender(
|
||||||
|
[near, offCanvas, outOfRange],
|
||||||
|
{},
|
||||||
|
1,
|
||||||
|
1250,
|
||||||
|
'Loaded major',
|
||||||
|
);
|
||||||
|
assert.deepEqual(picks[0], [500, 480], 'the depth probe below centre');
|
||||||
|
assert.equal(state._viewHeightEstimate, 400);
|
||||||
|
assert.equal(poses, 1, 'the eye is read once a pass');
|
||||||
|
assert.ok(rendered.includes(near), 'an on-canvas road is admitted');
|
||||||
|
assert.ok(!rendered.includes(offCanvas), 'an off-canvas road is not');
|
||||||
|
assert.ok(!rendered.includes(outOfRange), 'nor one out of range');
|
||||||
|
} finally {
|
||||||
|
unbind();
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -95,7 +95,6 @@
|
|||||||
"src/layers/streetLevel/sequenceCones.js",
|
"src/layers/streetLevel/sequenceCones.js",
|
||||||
"src/layers/streetLevel/view.js",
|
"src/layers/streetLevel/view.js",
|
||||||
"src/layers/traffic/animation.js",
|
"src/layers/traffic/animation.js",
|
||||||
"src/layers/traffic/flow.js",
|
|
||||||
"src/layers/traffic/lifecycle.js",
|
"src/layers/traffic/lifecycle.js",
|
||||||
"src/layers/traffic/rendering.js",
|
"src/layers/traffic/rendering.js",
|
||||||
"src/layers/traffic/retention.js",
|
"src/layers/traffic/retention.js",
|
||||||
|
|||||||
Reference in new issue
Block a user