mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Draw the ALPR layer's markers through the renderer's ports
The ALPR badges were Cesium entities whose billboards the layer changed in place: the canvas overlay swapped a badge to a faint pick target at its anchor, selection swapped the image, and unresolved floors clamped to the ground. They are now one icon set from the draw port, updated in place through upsert, so selection and the overlay swap change the same billboard without a rebuild or a blink. Badges keep their scale by distance, ground clamping and depth-test settings. Marker state lives in the app (markerState.js): a plain carrier per camera, registered as the camera's context entity, and a marker set that batches changes and sends only the icons that draw differently. Clicks on native badges come from the dispatcher's draw hit. Projection, the on-screen count, height samples (excluding the layer's own set), the screen-centre ellipsoid pick and the camera reach the renderer through the query, camera and frame ports. A resync of the overlay now hands back only the cameras that leave it, so kept ones are not written twice. index.js, overlay.js and presentation.js leave the Cesium allow-list; the layer's entry is Cesium-free, and a caller's own viewer loads the unbound factories. Differences the contract does not let us avoid: - Double-clicking a badge no longer starts the viewer's entity follow, since draw items are not entities. - The terrain fallback for anchors is no longer skipped while the globe is hidden: the globe port has no visibility getter. - Without depth support the query port answers no heights, terrain included, so anchors wait for a cached floor there. - On a viewer with no renderer bound, the surface counts as settled when anchors are refined.
This commit is contained in:
1 parent
faded8a418
commit
2592289612
11 files changed
+722
-537
No files matched your search
+152
-207
@@ -1,34 +1,35 @@
|
||||
import { isFollowing } from '../../data/followed.js';
|
||||
import * as Cesium from 'cesium';
|
||||
import { inputFor } from '../../input/dispatcher.js';
|
||||
import { distance, fromDegrees, toLonLatH } from '../../geo/index.js';
|
||||
import {
|
||||
LAYER_ID,
|
||||
MAX_VIEWPORT_DEGREES,
|
||||
MAX_RENDERED,
|
||||
ALPR_COLOR,
|
||||
ALPR_SELECTED_COLOR,
|
||||
MARKER_ICON_SIZE,
|
||||
SELECTED_MARKER_ICON_SIZE,
|
||||
CREDIT_DISPLAY_MS,
|
||||
MARKER_FLOOR_LIFT_M,
|
||||
MARKER_SCALE_BY_DISTANCE,
|
||||
} from './policy.js';
|
||||
import { boxContains } from './model.js';
|
||||
import { msg } from '../../i18n/index.js';
|
||||
import { createAlprFloorResolver } from './floors.js';
|
||||
import { createAlprOverlay } from './overlay.js';
|
||||
import {
|
||||
MARKER_IMAGE,
|
||||
SELECTED_IMAGE,
|
||||
alprDisplayId,
|
||||
alprLabelDetails,
|
||||
validAlprGroundHeight,
|
||||
} from './visuals.js';
|
||||
import { entityRecord, readEntityRecord } from '../../data/entityRecords.js';
|
||||
import { alprMarkers } from './markerState.js';
|
||||
import { heightsFor } from '../../queries/heights.js';
|
||||
import { cameraOf } from '../../render/cesium/camera.js';
|
||||
import { findCredits } from '../../render/rendererOf.js';
|
||||
import { alprIconPosition, alprMarkers } from './markerState.js';
|
||||
import { horizonFrom } from '../../geo/horizon.js';
|
||||
import { cameraOf, findCredits, findQuery } from '../../render/rendererOf.js';
|
||||
|
||||
/** A `#rrggbb` colour as `rgb(r,g,b)`, the form the readout card has always had. */
|
||||
function cssRgb(hex) {
|
||||
const n = Number.parseInt(hex.slice(1), 16);
|
||||
return `rgb(${(n >> 16) & 255},${(n >> 8) & 255},${n & 255})`;
|
||||
}
|
||||
|
||||
export function createAlprPresentation({ state, services, source }) {
|
||||
const { governorRequestRender } = services.render;
|
||||
@@ -41,6 +42,7 @@ export function createAlprPresentation({ state, services, source }) {
|
||||
} = services.context;
|
||||
|
||||
const { cachedGroundFloor } = services.groundFloor;
|
||||
const markers = state.markers;
|
||||
const overlay = createAlprOverlay({
|
||||
state,
|
||||
services,
|
||||
@@ -48,11 +50,35 @@ export function createAlprPresentation({ state, services, source }) {
|
||||
});
|
||||
let visibleRecords = [];
|
||||
let selectionStartedAt = 0;
|
||||
/** Markers still clamped while their floor cell resolves (id -> entity). */
|
||||
/** Markers still clamped while their floor cell resolves (id -> carrier). */
|
||||
const clampedMarkers = new Map();
|
||||
let floorResolve = null;
|
||||
const floors = createAlprFloorResolver(services.groundFloor);
|
||||
|
||||
/** Heights under a camera now, through the renderer's query port. */
|
||||
function heightNow(record, against) {
|
||||
const query = findQuery(state.viewer);
|
||||
if (!query) return null;
|
||||
const point = { lon: record.longitude, lat: record.latitude };
|
||||
// A miss is tiles still streaming. The markers are no ground: the
|
||||
// height sees through them.
|
||||
return against === 'terrain'
|
||||
? query.sampleHeightNow(point, { against: 'terrain' })
|
||||
: query.sampleHeightNow(point, { exclude: [markers.icons?.id] });
|
||||
}
|
||||
|
||||
/**
|
||||
* The ground under a camera: the rendered surface, else the terrain,
|
||||
* else the cached floor; null when none is plausible.
|
||||
*/
|
||||
function groundHeight(record) {
|
||||
let height = heightNow(record);
|
||||
if (!validAlprGroundHeight(height)) height = heightNow(record, 'terrain');
|
||||
if (!validAlprGroundHeight(height))
|
||||
height = cachedGroundFloor(record.latitude, record.longitude);
|
||||
return validAlprGroundHeight(height) ? height : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Marker placement from the cached ground floor (validated mesh or DEM, the
|
||||
* ellipsoidal datum every layer shares). Ground clamping on photoreal tiles
|
||||
@@ -60,14 +86,14 @@ export function createAlprPresentation({ state, services, source }) {
|
||||
* whole-city view stutter while zooming, so it is only the placeholder for
|
||||
* a cell that has not resolved yet.
|
||||
* @param {{latitude:number, longitude:number}} record Camera.
|
||||
* @returns {{position: Cesium.Cartesian3, clamped: boolean}} Placement.
|
||||
* @returns {{position: {x:number, y:number, z:number}, clamped: boolean}} Placement.
|
||||
*/
|
||||
function markerPlacement(record) {
|
||||
const floor = cachedGroundFloor(record.latitude, record.longitude);
|
||||
const clamped = !validAlprGroundHeight(floor);
|
||||
return {
|
||||
clamped,
|
||||
position: Cesium.Cartesian3.fromDegrees(
|
||||
position: fromDegrees(
|
||||
record.longitude,
|
||||
record.latitude,
|
||||
clamped ? 0 : floor + MARKER_FLOOR_LIFT_M,
|
||||
@@ -136,25 +162,18 @@ export function createAlprPresentation({ state, services, source }) {
|
||||
* @returns {number|null} Count, or null without a scene to project into.
|
||||
*/
|
||||
function countOnScreen() {
|
||||
const scene = state.viewer?.scene;
|
||||
const canvas = scene?.canvas;
|
||||
const canvas = state.viewer?.scene?.canvas;
|
||||
const width = canvas?.clientWidth || canvas?.width;
|
||||
const height = canvas?.clientHeight || canvas?.height;
|
||||
const camera = state.viewer?.camera;
|
||||
if (!scene || !width || !height || !camera?.positionWC) return null;
|
||||
const occluder = new Cesium.EllipsoidalOccluder(
|
||||
Cesium.Ellipsoid.WGS84,
|
||||
camera.positionWC,
|
||||
);
|
||||
const query = findQuery(state.viewer);
|
||||
if (!query || !width || !height) return null;
|
||||
let count = 0;
|
||||
try {
|
||||
for (const entity of state.dataSource.entities.values) {
|
||||
const position = entity.position?.getValue?.(Cesium.JulianDate.now());
|
||||
if (!position || !occluder.isPointVisible(position)) continue;
|
||||
const point = Cesium.SceneTransforms.worldToWindowCoordinates(
|
||||
scene,
|
||||
position,
|
||||
);
|
||||
const horizon = horizonFrom(cameraOf(state.viewer).pose().position);
|
||||
for (const carrier of markers.values()) {
|
||||
const position = alprIconPosition(carrier);
|
||||
if (!position || !horizon.isPointVisible(position)) continue;
|
||||
const point = query.project(position);
|
||||
if (
|
||||
point &&
|
||||
point.x >= 0 &&
|
||||
@@ -171,33 +190,26 @@ export function createAlprPresentation({ state, services, source }) {
|
||||
return count;
|
||||
}
|
||||
|
||||
/** Write a marker position, respecting the overlay's pick-target swap. */
|
||||
function setMarkerPosition(entity, position, clamped) {
|
||||
const heightReference = clamped
|
||||
? Cesium.HeightReference.CLAMP_TO_GROUND
|
||||
: Cesium.HeightReference.NONE;
|
||||
const saved = alprMarkers.read(entity)?.nativeAppearance;
|
||||
if (saved) {
|
||||
saved.position = position;
|
||||
saved.heightReference = heightReference;
|
||||
} else {
|
||||
entity.position = position;
|
||||
entity.billboard.heightReference = heightReference;
|
||||
}
|
||||
alprMarkers.write(entity).clamped = clamped;
|
||||
/** Write a marker's native placement; the canvas anchor is left alone. */
|
||||
function setMarkerPosition(carrier, position, clamped) {
|
||||
const marker = alprMarkers.write(carrier);
|
||||
marker.position = position;
|
||||
marker.clamped = clamped;
|
||||
markers.changed(carrier);
|
||||
}
|
||||
|
||||
/** Refresh all native placements, including the appearances held by the canvas. */
|
||||
/** Refresh all native placements, including those under canvas badges. */
|
||||
function refreshMarkerPlacements() {
|
||||
if (!state.enabled || !state.dataSource) return;
|
||||
if (!state.enabled || !markers.icons) return;
|
||||
clampedMarkers.clear();
|
||||
for (const entity of state.dataSource.entities.values) {
|
||||
for (const carrier of markers.values()) {
|
||||
const { position, clamped } = markerPlacement(
|
||||
alprMarkers.read(entity)?.record,
|
||||
alprMarkers.read(carrier)?.record,
|
||||
);
|
||||
setMarkerPosition(entity, position, clamped);
|
||||
if (clamped) clampedMarkers.set(entity.id, entity);
|
||||
setMarkerPosition(carrier, position, clamped);
|
||||
if (clamped) clampedMarkers.set(carrier.id, carrier);
|
||||
}
|
||||
markers.flush();
|
||||
resolveClampedFloors();
|
||||
governorRequestRender('alpr-surface');
|
||||
}
|
||||
@@ -210,8 +222,8 @@ export function createAlprPresentation({ state, services, source }) {
|
||||
const batch = [...clampedMarkers.values()];
|
||||
const request = Promise.resolve(
|
||||
floors.resolve(
|
||||
batch.map((entity) => {
|
||||
const { record } = alprMarkers.read(entity);
|
||||
batch.map((carrier) => {
|
||||
const { record } = alprMarkers.read(carrier);
|
||||
return { lat: record.latitude, lon: record.longitude };
|
||||
}),
|
||||
),
|
||||
@@ -220,65 +232,55 @@ export function createAlprPresentation({ state, services, source }) {
|
||||
.then(() => {
|
||||
if (floorResolve !== request) return;
|
||||
floorResolve = null;
|
||||
if (!state.enabled || !state.dataSource) return;
|
||||
if (!state.enabled || !markers.icons) return;
|
||||
let moved = false;
|
||||
for (const entity of batch) {
|
||||
if (clampedMarkers.get(entity.id) !== entity) continue;
|
||||
clampedMarkers.delete(entity.id);
|
||||
for (const carrier of batch) {
|
||||
if (clampedMarkers.get(carrier.id) !== carrier) continue;
|
||||
clampedMarkers.delete(carrier.id);
|
||||
const { position, clamped } = markerPlacement(
|
||||
alprMarkers.read(entity)?.record,
|
||||
alprMarkers.read(carrier)?.record,
|
||||
);
|
||||
if (clamped) continue;
|
||||
setMarkerPosition(entity, position, false);
|
||||
setMarkerPosition(carrier, position, false);
|
||||
moved = true;
|
||||
}
|
||||
markers.flush();
|
||||
if (moved) governorRequestRender('alpr-floor');
|
||||
// Cells that never resolved stay clamped; later renders retry them.
|
||||
});
|
||||
floorResolve = request;
|
||||
}
|
||||
|
||||
function updateAppearance(entity, selected) {
|
||||
const color = Cesium.Color.fromCssColorString(
|
||||
selected ? ALPR_SELECTED_COLOR : ALPR_COLOR,
|
||||
);
|
||||
entity.billboard.image = selected ? SELECTED_IMAGE : MARKER_IMAGE;
|
||||
entity.billboard.width = entity.billboard.height = selected
|
||||
? SELECTED_MARKER_ICON_SIZE
|
||||
: MARKER_ICON_SIZE;
|
||||
const labelModel = readEntityRecord(entity)?.labelModel;
|
||||
function updateAppearance(carrier, selected) {
|
||||
alprMarkers.write(carrier).selected = selected;
|
||||
markers.changed(carrier);
|
||||
const labelModel = readEntityRecord(carrier)?.labelModel;
|
||||
if (labelModel) {
|
||||
labelModel.accent = color.toCssColorString();
|
||||
labelModel.accent = cssRgb(selected ? ALPR_SELECTED_COLOR : ALPR_COLOR);
|
||||
labelModel.leaderAnimationStartedAt = selected ? selectionStartedAt : 0;
|
||||
}
|
||||
}
|
||||
|
||||
function markerColor() {
|
||||
return Cesium.Color.fromCssColorString(ALPR_COLOR);
|
||||
}
|
||||
|
||||
// The horizon rectangle is unstable during low-angle orbits and can exclude
|
||||
// the ground point in the center of the screen. Bound nearby coverage around
|
||||
// that point instead; the camera-to-ground range keeps zoom-out queries capped.
|
||||
function viewportBox(viewer) {
|
||||
const camera = viewer?.camera;
|
||||
const canvas = viewer?.scene.canvas;
|
||||
if (typeof camera?.pickEllipsoid === 'function' && canvas) {
|
||||
const query = findQuery(viewer);
|
||||
const canvas = viewer?.scene?.canvas;
|
||||
if (typeof query?.pickEllipsoid === 'function' && canvas) {
|
||||
const width = canvas.clientWidth || canvas.width;
|
||||
const height = canvas.clientHeight || canvas.height;
|
||||
if (!width || !height) return null;
|
||||
const focus = camera.pickEllipsoid(
|
||||
new Cesium.Cartesian2(width / 2, height / 2),
|
||||
viewer.scene.globe.ellipsoid,
|
||||
);
|
||||
const focus = query.pickEllipsoid(width / 2, height / 2);
|
||||
if (!focus) return null;
|
||||
const location = Cesium.Cartographic.fromCartesian(focus);
|
||||
const range = Cesium.Cartesian3.distance(camera.positionWC, focus);
|
||||
const location = toLonLatH(focus);
|
||||
if (!location) return null;
|
||||
const range = distance(cameraOf(viewer).pose().position, focus);
|
||||
const radius = Math.max(1000, 2 * range);
|
||||
const latitude = Cesium.Math.toDegrees(location.latitude);
|
||||
const longitude = Cesium.Math.toDegrees(location.longitude);
|
||||
const latitude = location.lat;
|
||||
const longitude = location.lon;
|
||||
const latSpan = radius / 111000;
|
||||
const lonSpan = latSpan / Math.cos(location.latitude);
|
||||
const lonSpan = latSpan / Math.cos((latitude * Math.PI) / 180);
|
||||
if (
|
||||
!Number.isFinite(latSpan + lonSpan) ||
|
||||
2 * Math.max(latSpan, lonSpan) > MAX_VIEWPORT_DEGREES ||
|
||||
@@ -294,14 +296,9 @@ export function createAlprPresentation({ state, services, source }) {
|
||||
};
|
||||
}
|
||||
// Rectangle-only viewers retain the same bounded-area contract.
|
||||
const rectangle = viewer?.camera?.computeViewRectangle(
|
||||
viewer.scene.globe.ellipsoid,
|
||||
);
|
||||
const rectangle = viewer ? cameraOf(viewer).viewRectangle() : null;
|
||||
if (!rectangle) return null;
|
||||
const south = Cesium.Math.toDegrees(rectangle.south);
|
||||
const north = Cesium.Math.toDegrees(rectangle.north);
|
||||
const west = Cesium.Math.toDegrees(rectangle.west);
|
||||
const east = Cesium.Math.toDegrees(rectangle.east);
|
||||
const { south, north, west, east } = rectangle;
|
||||
if (
|
||||
!Number.isFinite(south + north + west + east) ||
|
||||
east <= west ||
|
||||
@@ -319,7 +316,7 @@ export function createAlprPresentation({ state, services, source }) {
|
||||
overlay.clear();
|
||||
visibleRecords = [];
|
||||
clampedMarkers.clear();
|
||||
if (state.dataSource?.entities) state.dataSource.entities.removeAll();
|
||||
markers.clear();
|
||||
removeEntityContextsForLayer(LAYER_ID);
|
||||
}
|
||||
|
||||
@@ -369,62 +366,45 @@ export function createAlprPresentation({ state, services, source }) {
|
||||
governorRequestRender('alpr-render');
|
||||
const visibleIds = new Set(visible.map((record) => record.id));
|
||||
let changed = false;
|
||||
state.dataSource.entities.suspendEvents();
|
||||
for (const entity of [...state.dataSource.entities.values]) {
|
||||
if (!visibleIds.has(entity.id)) {
|
||||
state.dataSource.entities.remove(entity);
|
||||
clampedMarkers.delete(entity.id);
|
||||
changed = true;
|
||||
}
|
||||
const gone = [];
|
||||
for (const carrier of markers.values())
|
||||
if (!visibleIds.has(carrier.id)) gone.push(carrier.id);
|
||||
if (gone.length) {
|
||||
markers.remove(gone);
|
||||
for (const id of gone) clampedMarkers.delete(id);
|
||||
changed = true;
|
||||
}
|
||||
removeEntityContextsForLayer(LAYER_ID, { retainIds: visibleIds });
|
||||
for (const record of visible) {
|
||||
const existing = state.dataSource.entities.getById(record.id);
|
||||
const existing = markers.get(record.id);
|
||||
if (alprMarkers.read(existing)?.record === record) {
|
||||
updateAppearance(existing, record.id === state.selectedId);
|
||||
continue;
|
||||
}
|
||||
const color = markerColor();
|
||||
const selected = record.id === state.selectedId;
|
||||
const { position, clamped } = markerPlacement(record);
|
||||
// Direction wedges are painted by the overlay for the nearest cameras.
|
||||
// Native ground-clamped wedges (a ground polyline plus a classification
|
||||
// polygon per camera) cost ~40 ms per frame for 200 downtown cameras on
|
||||
// photoreal tiles, so markers stay billboards only.
|
||||
const entityDef = {
|
||||
id: record.id,
|
||||
position,
|
||||
billboard: {
|
||||
image: selected ? SELECTED_IMAGE : MARKER_IMAGE,
|
||||
width: selected ? SELECTED_MARKER_ICON_SIZE : MARKER_ICON_SIZE,
|
||||
height: selected ? SELECTED_MARKER_ICON_SIZE : MARKER_ICON_SIZE,
|
||||
heightReference: clamped
|
||||
? Cesium.HeightReference.CLAMP_TO_GROUND
|
||||
: Cesium.HeightReference.NONE,
|
||||
disableDepthTestDistance: Number.POSITIVE_INFINITY,
|
||||
// City-wide views hold hundreds of badges; let them shrink.
|
||||
scaleByDistance: new Cesium.NearFarScalar(
|
||||
...MARKER_SCALE_BY_DISTANCE,
|
||||
),
|
||||
},
|
||||
};
|
||||
let entity = existing;
|
||||
// Keep the Cesium entity and its ground-clamping subscription while its
|
||||
// geometry is unchanged. Updating metadata must not rebuild the marker.
|
||||
const previous = alprMarkers.read(entity)?.record;
|
||||
if (!entity) {
|
||||
entity = state.dataSource.entities.add(entityDef);
|
||||
// photoreal tiles, so markers stay icons only.
|
||||
let carrier = existing;
|
||||
// Keep the marker and its icon while its geometry is unchanged.
|
||||
// Updating metadata must not rebuild the marker.
|
||||
const previous = alprMarkers.read(carrier)?.record;
|
||||
if (!carrier) {
|
||||
carrier = markers.add(record.id);
|
||||
changed = true;
|
||||
}
|
||||
const marker = alprMarkers.write(entity);
|
||||
const marker = alprMarkers.write(carrier);
|
||||
if (!existing) {
|
||||
marker.position = position;
|
||||
marker.clamped = clamped;
|
||||
} else {
|
||||
if (
|
||||
previous.latitude !== record.latitude ||
|
||||
previous.longitude !== record.longitude
|
||||
) {
|
||||
setMarkerPosition(entity, position, clamped);
|
||||
setMarkerPosition(carrier, position, clamped);
|
||||
marker.canvasPosition = null;
|
||||
marker.displayPosition = null;
|
||||
}
|
||||
@@ -438,16 +418,16 @@ export function createAlprPresentation({ state, services, source }) {
|
||||
}
|
||||
}
|
||||
marker.record = record;
|
||||
entityRecord(entity).trackedId = record.id;
|
||||
entityRecord(carrier).trackedId = record.id;
|
||||
// The mapped camera datum has no elevation; it is not the clamped marker's
|
||||
// visual anchor. Only the selected marker samples the rendered surface,
|
||||
// at most once per second, through Cesium's public height APIs.
|
||||
// at most once per second, through the renderer's height queries.
|
||||
marker.displayPosition ??= null;
|
||||
entityRecord(entity).displayPosition = () => marker.displayPosition;
|
||||
entityRecord(entity).labelModel = {
|
||||
entityRecord(carrier).displayPosition = () => marker.displayPosition;
|
||||
entityRecord(carrier).labelModel = {
|
||||
title: alprDisplayId(record),
|
||||
details: alprLabelDetails(record, source),
|
||||
accent: color.toCssColorString(),
|
||||
accent: cssRgb(ALPR_COLOR),
|
||||
cardStyle: 'tactical',
|
||||
selected: true,
|
||||
leaderStyle: 'elbow',
|
||||
@@ -457,11 +437,12 @@ export function createAlprPresentation({ state, services, source }) {
|
||||
anchorRadiusPx: SELECTED_MARKER_ICON_SIZE / 2,
|
||||
anchorRadiusScale: null,
|
||||
};
|
||||
updateAppearance(entity, selected);
|
||||
registerEntityContext(entity, {
|
||||
updateAppearance(carrier, selected);
|
||||
registerEntityContext(carrier, {
|
||||
id: record.id,
|
||||
layerId: LAYER_ID,
|
||||
dataSource: state.dataSource,
|
||||
// Hidden with the layer: the context store reads its `show`.
|
||||
dataSource: markers.icons,
|
||||
layerName: 'ALPR Cameras',
|
||||
source:
|
||||
source.attribution?.description || source.label || 'Camera source',
|
||||
@@ -481,64 +462,46 @@ export function createAlprPresentation({ state, services, source }) {
|
||||
},
|
||||
});
|
||||
}
|
||||
state.dataSource.entities.resumeEvents();
|
||||
if (changed) state.renderRevision += 1;
|
||||
state.onScreen = countOnScreen();
|
||||
for (const entity of state.dataSource.entities.values)
|
||||
if (alprMarkers.read(entity)?.clamped)
|
||||
clampedMarkers.set(entity.id, entity);
|
||||
for (const carrier of markers.values())
|
||||
if (alprMarkers.read(carrier)?.clamped)
|
||||
clampedMarkers.set(carrier.id, carrier);
|
||||
resolveClampedFloors();
|
||||
const selectedEntity = state.selectedId
|
||||
? state.dataSource.entities.getById(state.selectedId)
|
||||
const selectedCarrier = state.selectedId
|
||||
? markers.get(state.selectedId)
|
||||
: null;
|
||||
if (!selectedEntity) state.selectedId = null;
|
||||
if (!selectedCarrier) state.selectedId = null;
|
||||
updateSelectedAnchor();
|
||||
overlay.sync(visible);
|
||||
if (selectedEntity) services.overlays?.refreshReadout?.(selectedEntity);
|
||||
markers.flush();
|
||||
if (selectedCarrier) services.overlays?.refreshReadout?.(selectedCarrier);
|
||||
// Keep the on-map credit tied to displayed data, not loading or zoom prompts.
|
||||
if (visible.length) presentOnMapCredit();
|
||||
else hideOnMapCredit();
|
||||
}
|
||||
|
||||
function focusNearest() {
|
||||
const camera = state.viewer?.camera;
|
||||
if (!state.enabled || !camera?.positionWC || isFollowing(state.viewer))
|
||||
if (!state.enabled || !state.viewer || isFollowing(state.viewer))
|
||||
return false;
|
||||
const port = cameraOf(state.viewer);
|
||||
const eye = port.pose().position;
|
||||
let nearest = null,
|
||||
distance = Infinity;
|
||||
for (const entity of state.dataSource.entities.values) {
|
||||
const position = entity.position.getValue(Cesium.JulianDate.now());
|
||||
const candidate = Cesium.Cartesian3.distanceSquared(
|
||||
camera.positionWC,
|
||||
position,
|
||||
);
|
||||
if (candidate < distance) {
|
||||
nearest = entity;
|
||||
distance = candidate;
|
||||
best = Infinity;
|
||||
for (const carrier of markers.values()) {
|
||||
const position = alprIconPosition(carrier);
|
||||
if (!position) continue;
|
||||
const candidate = distance(eye, position);
|
||||
if (candidate < best) {
|
||||
nearest = carrier;
|
||||
best = candidate;
|
||||
}
|
||||
}
|
||||
if (!nearest) return false;
|
||||
const record = state.recordById.get(nearest.id);
|
||||
const location = Cesium.Cartographic.fromDegrees(
|
||||
record.longitude,
|
||||
record.latitude,
|
||||
);
|
||||
const heights = heightsFor(state.viewer.scene);
|
||||
// A miss is tiles still streaming.
|
||||
let height = state.viewer.scene.sampleHeightSupported
|
||||
? heights.now(location, { exclude: [nearest] })
|
||||
: null;
|
||||
if (!validAlprGroundHeight(height) && state.viewer.scene.globe.show)
|
||||
height = heights.now(location, { against: 'terrain' });
|
||||
if (!validAlprGroundHeight(height))
|
||||
height = cachedGroundFloor(record.latitude, record.longitude);
|
||||
const center = Cesium.Cartesian3.fromDegrees(
|
||||
record.longitude,
|
||||
record.latitude,
|
||||
validAlprGroundHeight(height) ? height : 0,
|
||||
);
|
||||
const height = groundHeight(record);
|
||||
const center = fromDegrees(record.longitude, record.latitude, height ?? 0);
|
||||
if (!selectRecord(nearest.id)) return false;
|
||||
const port = cameraOf(state.viewer);
|
||||
port.flyToBounds(
|
||||
{ center, radius: 30 },
|
||||
{
|
||||
@@ -555,29 +518,29 @@ export function createAlprPresentation({ state, services, source }) {
|
||||
}
|
||||
|
||||
function selectRecord(id) {
|
||||
const entity = state.dataSource?.entities.getById(id);
|
||||
if (!entity || !state.recordById.has(id)) return false;
|
||||
const carrier = markers.get(id);
|
||||
if (!carrier || !state.recordById.has(id)) return false;
|
||||
clearSelection();
|
||||
state.selectedId = id;
|
||||
selectionStartedAt = globalThis.performance?.now?.() ?? Date.now();
|
||||
updateAppearance(entity, true);
|
||||
updateAppearance(carrier, true);
|
||||
state.lastAnchorSampleAt = 0;
|
||||
selectEntityContext(entity);
|
||||
selectEntityContext(carrier);
|
||||
updateSelectedAnchor();
|
||||
overlay.sync(visibleRecords);
|
||||
markers.flush();
|
||||
governorRequestRender('alpr-selection');
|
||||
return true;
|
||||
}
|
||||
|
||||
function clearSelection() {
|
||||
const entity = state.selectedId
|
||||
? state.dataSource?.entities.getById(state.selectedId)
|
||||
: null;
|
||||
const carrier = state.selectedId ? markers.get(state.selectedId) : null;
|
||||
const record = state.recordById.get(state.selectedId);
|
||||
if (entity && record) updateAppearance(entity, false);
|
||||
if (carrier && record) updateAppearance(carrier, false);
|
||||
state.selectedId = null;
|
||||
selectionStartedAt = 0;
|
||||
overlay.sync(visibleRecords);
|
||||
markers.flush();
|
||||
clearSelectedEntityContextForLayer(LAYER_ID);
|
||||
governorRequestRender('alpr-selection');
|
||||
}
|
||||
@@ -589,39 +552,22 @@ export function createAlprPresentation({ state, services, source }) {
|
||||
clearSelection();
|
||||
return;
|
||||
}
|
||||
const entity = state.dataSource?.entities.getById(state.selectedId);
|
||||
const carrier = markers.get(state.selectedId);
|
||||
const record = state.recordById.get(state.selectedId);
|
||||
const marker = alprMarkers.read(entity);
|
||||
const marker = alprMarkers.read(carrier);
|
||||
if (marker?.canvasPosition) {
|
||||
marker.displayPosition = marker.canvasPosition;
|
||||
return;
|
||||
}
|
||||
if (!entity || !record || Date.now() - state.lastAnchorSampleAt < 1000)
|
||||
if (!carrier || !record || Date.now() - state.lastAnchorSampleAt < 1000)
|
||||
return;
|
||||
state.lastAnchorSampleAt = Date.now();
|
||||
const scene = state.viewer.scene;
|
||||
const location = Cesium.Cartographic.fromDegrees(
|
||||
record.longitude,
|
||||
record.latitude,
|
||||
);
|
||||
const heights = heightsFor(scene);
|
||||
// A miss is tiles still streaming.
|
||||
let height = scene.sampleHeightSupported
|
||||
? heights.now(location, { exclude: [entity] })
|
||||
: null;
|
||||
if (!validAlprGroundHeight(height) && scene.globe.show)
|
||||
height = heights.now(location, { against: 'terrain' });
|
||||
if (!validAlprGroundHeight(height))
|
||||
height = cachedGroundFloor(record.latitude, record.longitude);
|
||||
if (!validAlprGroundHeight(height)) return;
|
||||
const next = Cesium.Cartesian3.fromDegrees(
|
||||
record.longitude,
|
||||
record.latitude,
|
||||
height,
|
||||
);
|
||||
const height = groundHeight(record);
|
||||
if (height === null) return;
|
||||
const next = fromDegrees(record.longitude, record.latitude, height);
|
||||
const shown = marker?.displayPosition;
|
||||
if (!shown || Cesium.Cartesian3.distance(next, shown) > 0.1) {
|
||||
alprMarkers.write(entity).displayPosition = next;
|
||||
if (!shown || distance(next, shown) > 0.1) {
|
||||
alprMarkers.write(carrier).displayPosition = next;
|
||||
governorRequestRender('alpr-anchor');
|
||||
}
|
||||
}
|
||||
@@ -631,10 +577,10 @@ export function createAlprPresentation({ state, services, source }) {
|
||||
state.clickSubscription = inputFor(viewer).onClick((event) => {
|
||||
if (!state.enabled) return;
|
||||
// The painted overlay answers first; the scene is picked only if not.
|
||||
const pickedId = event.pick?.id?.id;
|
||||
const hit = event.drawHit;
|
||||
const id =
|
||||
overlay.pick(event.screenPosition) ||
|
||||
(typeof pickedId === 'string' ? pickedId : null);
|
||||
(hit && hit.set === markers.icons?.id ? hit.id : null);
|
||||
if (
|
||||
id &&
|
||||
state.recordById.has(id) &&
|
||||
@@ -647,7 +593,6 @@ export function createAlprPresentation({ state, services, source }) {
|
||||
return {
|
||||
initOverlay: () => overlay.init(),
|
||||
destroyOverlay: () => overlay.destroy(),
|
||||
markerColor,
|
||||
viewportBox,
|
||||
clearRendered,
|
||||
hideOnMapCredit,
|
||||
|
||||
Reference in new issue
Block a user