Keep each mesh floor reading at its point and answer from the nearest one

Keeping a cell's lowest reading let a street probe pull a vehicle on a viaduct
or bridge in the same cell down to street level. Readings now stay at the point
they were taken (up to eight per cell, about 17 m apart), a read answers from
the reading nearest the vehicle, and two readings within about 5 m keep the
lower.
This commit is contained in:
Sameh Khamis committed 2026-11-08 23:19:16 -08:00
1 parent 18a9e0470d
commit 76b5d99e24
4 files changed
+100 -54

No files matched your search

+44 -16
View File
@@ -418,14 +418,21 @@ export function createGroundFloor({ terrain, signal }) {
// is the photogrammetric MESH, which sits above it (measured ~17 m at the
// Austin airport apron). DEM-flooring therefore still buried sprites/trails
// inside the mesh. These cells hold the RENDERED surface height, sampled
// a few probes per coarse cell near the viewer, lowest passing reading kept
// (a probe can land on a roof) (scene.sampleHeight — the same
// at the callers' points, a few per coarse cell near the viewer, each kept
// where it was taken and read back from the nearest (scene.sampleHeight — the same
// source groundSnap uses, which is why 3D models always looked right) and
// reported here by meshFloorSampler.js. Design principle (owner): NEVER
// below the visible surface; slightly above is always fine. The DEM stays
// the instant global prior/fallback and the sanity gate for samples.
/** @type {Map<string, number>} coarse cell key -> rendered-surface height (m, ellipsoidal). */
/** @type {Map<string, Array<{lat: number, lon: number, h: number}>>}
* coarse cell key -> rendered-surface readings (m, ellipsoidal), each kept
* at the point it was probed. A read answers from the reading nearest the
* asked point, so a viaduct and the street beside it in one cell keep their
* own heights. */
const _meshCells = new Map();
/** @constant {number} Readings closer than this (degrees, about 5 m) are
* one spot: the lower one is kept there. */
const MESH_READING_SAME_SPOT_DEG = 0.00005;
/** @type {boolean} Whether mesh cells apply — true only in the google-3d
* regime (photoreal stack). On globe stacks the rendered surface IS the
* terrain provider (Re:Earth on keyless), so the DEM is already correct
@@ -445,8 +452,8 @@ export function createGroundFloor({ terrain, signal }) {
/**
* Records a sampled rendered-surface height for the cell containing the
* coordinate, unchecked; the first record wins. The sampler goes through
* `reportValidatedMeshFloorCell`, which keeps the lowest passing reading.
* coordinate, unchecked, when the cell has no reading yet. The sampler goes
* through `reportValidatedMeshFloorCell`, which keeps a reading per point.
* @param {number} lat @param {number} lon @param {number} heightM
*/
function reportMeshFloorCell(lat, lon, heightM) {
@@ -458,7 +465,7 @@ export function createGroundFloor({ terrain, signal }) {
return;
const c = coarseFloorCoord(lat, lon);
const key = `${c.lat},${c.lon}`;
if (!_meshCells.has(key)) _meshCells.set(key, heightM);
if (!_meshCells.has(key)) _meshCells.set(key, [{ lat, lon, h: heightM }]);
}
/** Return whether a rendered-mesh sample is plausible against a real DEM prior. */
@@ -475,8 +482,8 @@ export function createGroundFloor({ terrain, signal }) {
* Validate and record a rendered-mesh floor through the single shared gate.
* Callers must not bypass this with raw `reportMeshFloorCell`: a coarse-LOD
* or aircraft hit would otherwise poison every floor consumer in the cell.
* The lowest passing reading is kept, so a rooftop hit within the window
* gives way to a later probe that reached the street.
* The reading is kept at the point it was taken; a second reading at the
* same spot (within about 5 m) keeps the lower of the two.
* @returns {boolean} Whether the sample passed and was reported.
*/
function reportValidatedMeshFloorCell(lat, lon, heightM) {
@@ -484,17 +491,26 @@ export function createGroundFloor({ terrain, signal }) {
const cell = coarseFloorCoord(lat, lon);
const prior = cachedRealEllipsoidalGround(cell.lat, cell.lon);
if (!meshFloorSampleWithinPrior(heightM, prior)) return false;
// The lowest passing reading wins: a probe that hit a roof is replaced
// by a later one that reached the street.
const key = `${cell.lat},${cell.lon}`;
const held = _meshCells.get(key);
if (held == null || heightM < held) _meshCells.set(key, heightM);
const readings = _meshCells.get(key);
if (!readings) {
_meshCells.set(key, [{ lat, lon, h: heightM }]);
return true;
}
const same = readings.find(
(r) =>
Math.abs(r.lat - lat) < MESH_READING_SAME_SPOT_DEG &&
Math.abs(r.lon - lon) < MESH_READING_SAME_SPOT_DEG,
);
if (!same) readings.push({ lat, lon, h: heightM });
else if (heightM < same.h) same.h = heightM;
return true;
}
/**
* Synchronous mesh-cell read — null when the cell is unsampled OR the
* google-3d regime is inactive (globe stacks must floor on the DEM).
* Synchronous mesh-cell read — the cell's reading nearest the point, or
* null when the cell is unsampled OR the google-3d regime is inactive (globe
* stacks must floor on the DEM).
* @param {number} lat @param {number} lon
* @returns {number|null}
*/
@@ -502,8 +518,20 @@ export function createGroundFloor({ terrain, signal }) {
if (!_meshPreferred) return null;
if (!Number.isFinite(lat) || !Number.isFinite(lon)) return null;
const c = coarseFloorCoord(lat, lon);
const h = _meshCells.get(`${c.lat},${c.lon}`);
return h != null ? h : null;
const readings = _meshCells.get(`${c.lat},${c.lon}`);
if (!readings) return null;
if (readings.length === 1) return readings[0].h;
const k = Math.cos((lat * Math.PI) / 180) ** 2;
let best = readings[0],
bestD = Infinity;
for (const r of readings) {
const d = (r.lat - lat) ** 2 + k * (r.lon - lon) ** 2;
if (d < bestD) {
bestD = d;
best = r;
}
}
return best.h;
}
/** Test hook: clears mesh cells (never called at runtime — cells are valid all session). */