mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
Street level view/cameraFollow/groundClick/index, traffic footprint/viewport/ingestion, transit viewport/trails/state/rendering, FIRMS state/viewport, installations controls/viewport/state, radio queries/navigation/state, bikeshare viewport, submarine cable overlay and data/detection now use src/geo for Math, Cartesian2/3, Cartographic and WGS84 maths. Cesium stays for Rectangle, Ray, BoundingSphere, HeadingPitchRange, EasingFunction, EllipsoidGeodesic, EllipsoidalOccluder and the Ellipsoids passed into Cesium (pickEllipsoid, occluders). traffic/state.js keeps its Cesium lerp scratch (written per frame by Cesium.Cartesian3.lerp in animation.js; a geo scratch cost +4-7% there) and traffic/timing.js stays on Cesium (a test keeps it source-equivalent to model.parseRoads, outside this batch). street-level-layer, traffic-layer, installations-layer, bikeshare-layer and submarine-cables own the src/geo files.
139 lines
4.5 KiB
JavaScript
139 lines
4.5 KiB
JavaScript
import * as Cesium from 'cesium';
|
|
import * as geo from '../../geo/index.js';
|
|
import {
|
|
ACTIVATION_ENTER_ALTITUDE_M,
|
|
ACTIVATION_EXIT_ALTITUDE_M,
|
|
CAMERA_DEBOUNCE_MS,
|
|
RANGE_SLACK_KM,
|
|
inflateViewBounds,
|
|
} from './policy.js';
|
|
import { transitFeedsInRange } from '../../data/transitFeeds.js';
|
|
|
|
/**
|
|
* Camera-driven feed activation: only the feeds whose coverage circle contains
|
|
* the look-at point are polled, and only below the altitude gate.
|
|
* @param {object} context
|
|
* @returns {object}
|
|
*/
|
|
export function createViewport({ state, services, parts }) {
|
|
const { governorRequestRender } = services.render;
|
|
|
|
function getCameraAltitude(viewer = state._viewer) {
|
|
const carto = viewer?.camera?.positionCartographic;
|
|
return carto && Number.isFinite(carto.height) ? carto.height : Infinity;
|
|
}
|
|
|
|
function getCameraCenterLatLon(viewer = state._viewer) {
|
|
const rect = viewer?.camera?.computeViewRectangle?.(
|
|
viewer.scene.globe?.ellipsoid,
|
|
);
|
|
if (rect) {
|
|
const center = Cesium.Rectangle.center(rect);
|
|
return {
|
|
lat: geo.math.toDegrees(center.latitude),
|
|
lon: geo.math.toDegrees(center.longitude),
|
|
};
|
|
}
|
|
const carto = viewer?.camera?.positionCartographic;
|
|
if (carto) {
|
|
return {
|
|
lat: geo.math.toDegrees(carto.latitude),
|
|
lon: geo.math.toDegrees(carto.longitude),
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function altitudeGateOpen(altitude) {
|
|
if (state._altitudeGateOpen) return altitude <= ACTIVATION_EXIT_ALTITUDE_M;
|
|
return altitude <= ACTIVATION_ENTER_ALTITUDE_M;
|
|
}
|
|
|
|
/**
|
|
* Refresh the bounds the per-frame pass uses to decide what is worth
|
|
* animating. Cheap, and only on a settled camera — never per frame.
|
|
*/
|
|
function refreshViewBounds() {
|
|
const viewer = state._viewer;
|
|
const rect = viewer?.camera?.computeViewRectangle?.(
|
|
viewer.scene.globe?.ellipsoid,
|
|
);
|
|
state._viewBounds = rect
|
|
? inflateViewBounds({
|
|
south: geo.math.toDegrees(rect.south),
|
|
north: geo.math.toDegrees(rect.north),
|
|
west: geo.math.toDegrees(rect.west),
|
|
east: geo.math.toDegrees(rect.east),
|
|
})
|
|
: null;
|
|
// The sweep itself is deferred: `marker.show` is a dirty flag, and a drag
|
|
// fires this on every frame, so re-deciding visibility inline would put the
|
|
// whole fleet back into the vertex buffer once a frame — the exact cost
|
|
// this is meant to remove.
|
|
state._visibilityDirty = true;
|
|
state._cameraRevision++;
|
|
parts.rendering.requestVisibility();
|
|
}
|
|
|
|
function runProximityCheck() {
|
|
if (!state._enabled || !state._viewer) return;
|
|
refreshViewBounds();
|
|
const altitude = getCameraAltitude();
|
|
state._altitudeGateOpen = altitudeGateOpen(altitude);
|
|
const center = state._altitudeGateOpen ? getCameraCenterLatLon() : null;
|
|
const desired = new Map();
|
|
if (center) {
|
|
for (const feed of transitFeedsInRange(center.lat, center.lon)) {
|
|
desired.set(feed.id, feed);
|
|
}
|
|
// Hysteresis: a feed already active stays active a little past its edge.
|
|
for (const feed of transitFeedsInRange(
|
|
center.lat,
|
|
center.lon,
|
|
RANGE_SLACK_KM,
|
|
)) {
|
|
if (state._activeFeeds.has(feed.id)) desired.set(feed.id, feed);
|
|
}
|
|
}
|
|
for (const feedId of [...state._activeFeeds.keys()]) {
|
|
if (!desired.has(feedId)) {
|
|
state._inFlight.get(feedId)?.controller.abort();
|
|
state._inFlight.delete(feedId);
|
|
state._activeFeeds.delete(feedId);
|
|
state._feedStatus.delete(feedId);
|
|
parts.ingestion.removeFeedVehicles(feedId);
|
|
}
|
|
}
|
|
for (const [feedId, feed] of desired) {
|
|
if (!state._activeFeeds.has(feedId)) {
|
|
state._activeFeeds.set(feedId, feed);
|
|
void parts.ingestion.pollFeed(feed, state._generation);
|
|
}
|
|
}
|
|
parts.ingestion.sweepAgedVehicles(Date.now());
|
|
parts.rendering.syncRenderHold();
|
|
governorRequestRender('transit-proximity');
|
|
}
|
|
|
|
function onCameraChanged() {
|
|
// The bounds update immediately; the feed proximity check stays debounced.
|
|
// A person dragging the globe should not wait a third of a second for the
|
|
// vehicles under the cursor to start moving again.
|
|
refreshViewBounds();
|
|
clearTimeout(state._cameraDebounceTimer);
|
|
state._cameraDebounceTimer = setTimeout(() => {
|
|
state._cameraDebounceTimer = null;
|
|
runProximityCheck();
|
|
}, CAMERA_DEBOUNCE_MS);
|
|
}
|
|
|
|
return {
|
|
refreshViewBounds,
|
|
getCameraAltitude,
|
|
getCameraCenterLatLon,
|
|
altitudeGateOpen,
|
|
runProximityCheck,
|
|
onCameraChanged,
|
|
};
|
|
}
|