mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Ground snapping and mesh floor sampling through the renderer ports, off the Cesium list
src/services/groundSnap.js and src/services/meshFloorSampler.js no longer reach Cesium. Whether the surface has streamed comes from the globe port (the snap waits for the 'tiles' and 'surface' settle kinds; the sampler runs only with the globe hidden and 'surface' settled), the camera height gate from camera.pose(), heights from the query port, and the maths from src/geo/ellipsoid.js. The raw exclusion path is gone: callers see through their own draw sets by id. Cockpit's ground probe names the tracked contact's icon and model sets, which the flights and military layers keep on its entity record (groundSets), instead of the tracked holder object. Probing is unchanged: at the caller's point, up to eight readings per cell about 17 m apart, a read answering from the nearest one. The application operations entry is now Cesium-free: a viewer with no renderer bound needs ./render/cesium/unbound loaded first. Tests that used to get the adapter's factories through ground snapping load it themselves.
This commit is contained in:
1 parent
9d4e438e63
commit
f53f691a80
20 files changed
+296
-199
No files matched your search
+47
-91
@@ -46,15 +46,10 @@
|
||||
* are per-layer, keyed by that layer's icao space).
|
||||
*/
|
||||
|
||||
import * as Cesium from 'cesium';
|
||||
import { heightsFor } from '../render/cesium/heights.js';
|
||||
import { scaleToGeodeticSurface, toLonLatH } from '../geo/ellipsoid.js';
|
||||
import { distanceSquared } from '../geo/vector.js';
|
||||
import { heightsOf } from '../queries/heights.js';
|
||||
// The application operations entry reaches the annotation resolver and
|
||||
// locations.js, whose flights and heights go through the camera and query
|
||||
// ports and which cannot load the Cesium adapter themselves; this listed
|
||||
// module, on the same entry, loads the factories a viewer with no renderer
|
||||
// bound gets them from.
|
||||
import '../render/cesium/camera.js';
|
||||
import { findGlobe } from '../render/rendererOf.js';
|
||||
|
||||
/** Taxi threshold: a cached snap answers directly for moves up to this far from
|
||||
* the sampled spot (m). Past it the value is demoted to a held last-known and a
|
||||
@@ -139,40 +134,32 @@ const SAMPLE_BUDGET_PER_WINDOW = 4;
|
||||
const SAMPLE_WINDOW_MS = 250;
|
||||
|
||||
/**
|
||||
* True when the active photoreal tileset has finished streaming the tiles in
|
||||
* view (the pattern CCTV's projectionTilesReady once used: first
|
||||
* Cesium3DTileset found in scene.primitives, duck-typed via its boolean
|
||||
* tilesLoaded). Sampling before that both blows the frame budget on forced
|
||||
* synchronous loads and produces misses that would burn retry backoff for
|
||||
* nothing. With NO tileset present
|
||||
* (OSM fallback) this returns true so the sample path is not permanently
|
||||
* blocked — the sample just misses and backs off.
|
||||
* @param {Cesium.Viewer} viewer
|
||||
* True when the drawn surface has finished streaming what is in view: the
|
||||
* base tiles (the globe port's `tiles` settle kind) and whatever surface
|
||||
* heights answer from (`surface`: the globe's terrain while it is shown,
|
||||
* otherwise every shown 3D tileset, whoever added it). Sampling
|
||||
* before that both blows the frame budget on forced synchronous loads and
|
||||
* produces misses that would burn retry backoff for nothing. With the globe
|
||||
* hidden and NO tiles drawn the sample could only miss, and this says not
|
||||
* ready, which is the same miss and backoff without the probe. False
|
||||
* without a globe port to ask.
|
||||
* @param {object} viewer
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function _tilesReady(viewer) {
|
||||
const prims = viewer?.scene?.primitives;
|
||||
if (!prims) return false;
|
||||
for (let i = 0; i < prims.length; i++) {
|
||||
let p = null;
|
||||
try {
|
||||
p = prims.get(i);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
if (p && p.show !== false && typeof p.tilesLoaded === 'boolean') {
|
||||
return p.tilesLoaded === true;
|
||||
}
|
||||
try {
|
||||
const globe = findGlobe(viewer);
|
||||
return globe?.isSettled('tiles') === true && globe.isSettled('surface');
|
||||
} catch {
|
||||
return false; // mid-teardown
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a per-layer ground-snap cache.
|
||||
* @returns {{
|
||||
* heightFor: (viewer: Cesium.Viewer, icao: string, pos: Cesium.Cartesian3,
|
||||
* getExclusions?: (() => Array<unknown>)|{sets: string[]})
|
||||
* => number|null,
|
||||
* heightFor: (viewer: object, icao: string, pos: {x: number, y: number, z: number},
|
||||
* exclusions?: {sets: string[]}) => number|null,
|
||||
* forget: (icao: string) => void,
|
||||
* clear: () => void,
|
||||
* }}
|
||||
@@ -180,7 +167,7 @@ function _tilesReady(viewer) {
|
||||
export function createGroundSnap({ groundFloor }) {
|
||||
const { cachedMeshFloor, meshFloorPreferred, reportValidatedMeshFloorCell } =
|
||||
groundFloor;
|
||||
/** @type {Map<string, {h: number|null, samplePos: Cesium.Cartesian3|null,
|
||||
/** @type {Map<string, {h: number|null, samplePos: {x: number, y: number, z: number}|null,
|
||||
* held: boolean, nextRetryMs: number, misses: number}>} Per-icao snap state:
|
||||
* the sampled height and the surface point it was measured at, whether that
|
||||
* pair is still the fresh answer or has been demoted to a bounded last-known
|
||||
@@ -188,13 +175,6 @@ export function createGroundSnap({ groundFloor }) {
|
||||
const entries = new Map();
|
||||
let windowStartMs = 0;
|
||||
let windowCount = 0;
|
||||
const scratchCarto = new Cesium.Cartographic();
|
||||
const scratchSurfacePos = new Cesium.Cartesian3();
|
||||
/** Separate from `scratchCarto`: the held-evidence check runs on paths that
|
||||
* return BEFORE the sample writes `scratchCarto`, and on the sampling path it
|
||||
* must not disturb the cartographic that `reportValidatedMeshFloorCell` reads
|
||||
* back after the sample lands. */
|
||||
const scratchHeldCarto = new Cesium.Cartographic();
|
||||
|
||||
/**
|
||||
* The last measurement for a contact whose fresh snap has been demoted — it
|
||||
@@ -207,15 +187,15 @@ export function createGroundSnap({ groundFloor }) {
|
||||
* moved does not change with time, so what invalidates the value is the
|
||||
* contact MOVING, which is exactly what this measures.
|
||||
*
|
||||
* @param {{h: number|null, samplePos: Cesium.Cartesian3|null, held: boolean}|undefined} entry
|
||||
* @param {Cesium.Cartesian3} surfacePos - Contact's position on the ellipsoid.
|
||||
* @param {{h: number|null, samplePos: {x: number, y: number, z: number}|null, held: boolean}|undefined} entry
|
||||
* @param {{x: number, y: number, z: number}} surfacePos - Contact's position on the ellipsoid (ECEF).
|
||||
* @returns {number|null}
|
||||
*/
|
||||
function heldSnapM(entry, surfacePos) {
|
||||
if (!entry || !entry.held || entry.h == null || !entry.samplePos)
|
||||
return null;
|
||||
if (
|
||||
Cesium.Cartesian3.distanceSquared(surfacePos, entry.samplePos) >
|
||||
distanceSquared(surfacePos, entry.samplePos) >
|
||||
HELD_SNAP_MAX_DRIFT_M * HELD_SNAP_MAX_DRIFT_M
|
||||
) {
|
||||
return dropHold(entry);
|
||||
@@ -259,20 +239,13 @@ export function createGroundSnap({ groundFloor }) {
|
||||
* to answer from a single sample — and borrowing one to DISCARD a real
|
||||
* measurement would let a roof cell hide a correctly placed model.
|
||||
*
|
||||
* @param {Cesium.Cartesian3} surfacePos - Contact's position on the ellipsoid.
|
||||
* @param {{x: number, y: number, z: number}} surfacePos - Contact's position on the ellipsoid (ECEF).
|
||||
* @returns {number|null}
|
||||
*/
|
||||
function freshMeasuredFloorAt(surfacePos) {
|
||||
const carto = Cesium.Cartographic.fromCartesian(
|
||||
surfacePos,
|
||||
Cesium.Ellipsoid.WGS84,
|
||||
scratchHeldCarto,
|
||||
);
|
||||
if (!carto) return null;
|
||||
return cachedMeshFloor(
|
||||
Cesium.Math.toDegrees(carto.latitude),
|
||||
Cesium.Math.toDegrees(carto.longitude),
|
||||
);
|
||||
const at = toLonLatH(surfacePos);
|
||||
if (!at) return null;
|
||||
return cachedMeshFloor(at.lat, at.lon);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -282,30 +255,26 @@ export function createGroundSnap({ groundFloor }) {
|
||||
* A contact that HAS resolved once keeps that answer through a resample outage;
|
||||
* see `heldSnapM`.
|
||||
*
|
||||
* @param {Cesium.Viewer} viewer - Live viewer (sampling + tiles-ready check).
|
||||
* @param {object} viewer - Live viewer (sampling + tiles-ready check).
|
||||
* @param {string} icao - Cache key (per-layer icao space).
|
||||
* @param {Cesium.Cartesian3} pos - Current display position. Cache movement
|
||||
* is measured after projecting this input to the WGS84 ellipsoid, so a
|
||||
* poll-time altitude/datum change cannot invalidate a stationary snap.
|
||||
* @param {(() => Array<unknown>)|undefined} getExclusions - Lazily builds the
|
||||
* scene.sampleHeight objectsToExclude list (own billboards/models/entities —
|
||||
* the vertical pick ray at a plane's OWN lat/lon lands on its icon/model
|
||||
* otherwise). Only invoked when a sample actually fires. `{sets}` (draw
|
||||
* set ids) instead samples through the renderer's query port
|
||||
* @param {{x: number, y: number, z: number}} pos - Current display position
|
||||
* (ECEF). Cache movement is measured after projecting this input to the
|
||||
* WGS84 ellipsoid, so a poll-time altitude/datum change cannot invalidate
|
||||
* a stationary snap.
|
||||
* @param {{sets?: string[]}} [exclusions] - The caller's own draw set ids
|
||||
* (its markers and models). The sample asks the renderer's query port
|
||||
* (`heightsOf`), which sees through every draw set not drawn as ground
|
||||
* and these: for a caller whose markers and models are on the draw port.
|
||||
* and these: the vertical ray at a plane's OWN lat/lon would otherwise
|
||||
* land on its icon or model.
|
||||
* @returns {number|null}
|
||||
*/
|
||||
function heightFor(viewer, icao, pos, getExclusions) {
|
||||
const surfacePos = Cesium.Ellipsoid.WGS84.scaleToGeodeticSurface(
|
||||
pos,
|
||||
scratchSurfacePos,
|
||||
);
|
||||
function heightFor(viewer, icao, pos, exclusions) {
|
||||
const surfacePos = scaleToGeodeticSurface(pos);
|
||||
if (!surfacePos) return null;
|
||||
const cached = entries.get(icao);
|
||||
if (cached && cached.h != null && cached.samplePos && !cached.held) {
|
||||
if (
|
||||
Cesium.Cartesian3.distanceSquared(surfacePos, cached.samplePos) <=
|
||||
distanceSquared(surfacePos, cached.samplePos) <=
|
||||
MOVE_INVALIDATE_M * MOVE_INVALIDATE_M
|
||||
) {
|
||||
return cached.h;
|
||||
@@ -360,35 +329,22 @@ export function createGroundSnap({ groundFloor }) {
|
||||
};
|
||||
if (!_tilesReady(viewer)) return miss();
|
||||
windowCount += 1;
|
||||
const carto = Cesium.Cartographic.fromCartesian(
|
||||
pos,
|
||||
Cesium.Ellipsoid.WGS84,
|
||||
scratchCarto,
|
||||
);
|
||||
// Draw-set exclusions ask the renderer's query port, which sees through
|
||||
// every draw set not drawn as ground (the caller's markers and models).
|
||||
const sampled = !carto
|
||||
? null
|
||||
: Array.isArray(getExclusions?.sets)
|
||||
? heightsOf(viewer).now(carto, { exclude: getExclusions.sets })
|
||||
: heightsFor(viewer.scene).now(carto, {
|
||||
exclude: getExclusions ? getExclusions() : undefined,
|
||||
});
|
||||
const at = toLonLatH(pos);
|
||||
const exclude = Array.isArray(exclusions?.sets)
|
||||
? exclusions.sets
|
||||
: undefined;
|
||||
const sampled = !at ? null : heightsOf(viewer).now(at, { exclude });
|
||||
// Same sanity floor as cctv.js's sampleGroundHeight: a hit far below the
|
||||
// ellipsoid is pick garbage, not ground.
|
||||
if (!Number.isFinite(sampled) || sampled < -150) return miss();
|
||||
if (meshFloorPreferred()) {
|
||||
reportValidatedMeshFloorCell(
|
||||
Cesium.Math.toDegrees(scratchCarto.latitude),
|
||||
Cesium.Math.toDegrees(scratchCarto.longitude),
|
||||
sampled,
|
||||
);
|
||||
reportValidatedMeshFloorCell(at.lat, at.lon, sampled);
|
||||
}
|
||||
// A fresh sample RELEASES any hold: this is a measurement of where the
|
||||
// contact is now, and it outranks a memory of where it was.
|
||||
entries.set(icao, {
|
||||
h: sampled,
|
||||
samplePos: Cesium.Cartesian3.clone(surfacePos),
|
||||
samplePos: surfacePos,
|
||||
held: false,
|
||||
nextRetryMs: 0,
|
||||
misses: 0,
|
||||
|
||||
Reference in new issue
Block a user