diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index f91aaced8..95f5dc7ca 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -1136,15 +1136,17 @@ On the photoreal stack the shared floor (`cachedGroundFloor`) answers from a rendered-mesh reading per coarse cell (0.001°, about 110 × 80 m), sampled by `src/services/meshFloorSampler.js` and gated against the DEM by `reportValidatedMeshFloorCell` in `src/services/groundFloor.js`. The probe goes -where the caller's point is (a vehicle or contact), not to the cell's centre, -and a cell is read up to four times at points about 17 m or more apart; the -lowest reading that passes the gate is kept. Before this, one probe at the -cell's centre latched for the session, so a centre on a roof lifted everything -in the cell (a Haymarket bus drew 23.5 m above the street, on the garage roof) -and a centre in the river put Red Line trains on the Longfellow Bridge 6–9 m -under the deck. Accepted limit: a cell holds one floor, so a vehicle on a -bridge or viaduct whose cell also gets a street-level probe beneath it is drawn -at that lower level. +where the caller's point is (a vehicle or contact), not to the cell's centre; +a cell is read up to eight times, at points about 17 m or more apart, and each +reading that passes the gate is kept at its point. A read answers from the +cell's reading nearest the asked point, so a viaduct and the street beside it +in one cell keep their own heights; two readings within about 5 m of each other +keep the lower. Before this, one probe at the cell's centre latched for the +session, so a centre on a roof lifted everything in the cell (a Haymarket bus +drew 23.5 m above the street, on the garage roof) and a centre in the river put +Red Line trains on the Longfellow Bridge 6–9 m under the deck. Residual: the +probe is vertical, so a vehicle directly under a deck or a roof overhang reads +the structure above it. ## Vessel components and sources @@ -2558,7 +2560,7 @@ This is the current runtime/source-of-truth snapshot for the project. > `HELD_SNAP_CONTRADICTION_M` (5 m) ABOVE the held value drops the hold and the > contact is COLD again, back to the floored 2D billboard. The rule is > ONE-SIDED on purpose — it targets BURIAL. A cell reading BELOW a real sample -> is the floor chain's expected under-read (lowest of up to four probes over ~111 m, +> is the floor chain's expected under-read (a few point readings over ~111 m, > neighbours lean lowest, `displayFloorHeightM` only ever raises), not evidence > of anything, and a two-sided cut was disproved on the track rig by a planted > cell 66.7 m below a real sample at the same spot. Accepted residual, stated diff --git a/src/data/meshFloorSampler.test.mjs b/src/data/meshFloorSampler.test.mjs index f26e7254d..b15c7f782 100644 --- a/src/data/meshFloorSampler.test.mjs +++ b/src/data/meshFloorSampler.test.mjs @@ -25,7 +25,7 @@ import { bindRenderer } from '../render/rendererOf.js'; import { resolveEllipsoidalGround } from './terrainHeights.js'; import { cachedGroundFloor, cachedMeshFloor, setMeshFloorPreferred, - _clearMeshFloorCellsForTest, + _clearMeshFloorCellsForTest, reportValidatedMeshFloorCell, } from './groundFloor.js'; /** Distinct site per test — terrainHeights' cache is module-global and warm @@ -199,27 +199,39 @@ test('sampler: the probe goes where the point is, not to the cell centre', async assert.ok(Math.abs(asked[0] - point.lon) < 1e-9, `probed at ${asked[0]}, not ${point.lon}`); }); -test('sampler: a rooftop reading gives way to a lower one from another point in the cell', async () => { +test('sampler: a viaduct and the street beside it in one cell keep their own heights', async () => { reset(); const cell = nextSite(); await seedDem(cell, 120.0); - // A roof 22 m up at the first point, the street at the others. - const roofLon = cell.lon + 0.0004; + // A viaduct deck 9 m up along one line of the cell, the street elsewhere. + const deckLat = cell.lat + 0.0003; let calls = 0; const scene = fakeScene((carto) => { calls += 1; - return Math.abs(Cesium.Math.toDegrees(carto.longitude) - roofLon) < 1e-7 ? 143.0 : 121.0; + return Math.abs(Cesium.Math.toDegrees(carto.latitude) - deckLat) < 1e-7 ? 129.0 : 121.0; }); const opts = { viewerLat: cell.lat, viewerLon: cell.lon }; - sampleMeshFloorCells(scene, [{ lat: cell.lat, lon: roofLon }], opts); - assert.equal(cachedMeshFloor(cell.lat, cell.lon), 143.0, 'the roof passes the +80 m window'); - sampleMeshFloorCells(scene, [{ lat: cell.lat, lon: roofLon + 0.00001 }], opts); - assert.equal(calls, 1, 'a point next to one already probed is not probed again'); - sampleMeshFloorCells(scene, [{ lat: cell.lat, lon: cell.lon - 0.0003 }], opts); - assert.equal(cachedMeshFloor(cell.lat, cell.lon), 121.0, 'the street reading replaces it'); - assert.equal(cachedGroundFloor(cell.lat, cell.lon), 121.0); - sampleMeshFloorCells(scene, [{ lat: cell.lat + 0.0003, lon: cell.lon + 0.0001 }], opts); - assert.equal(cachedMeshFloor(cell.lat, cell.lon), 121.0, 'a higher reading never raises it'); + const onDeck = { lat: deckLat, lon: cell.lon }; + const street = { lat: cell.lat - 0.0003, lon: cell.lon }; + sampleMeshFloorCells(scene, [onDeck], opts); + sampleMeshFloorCells(scene, [street], opts); + assert.equal(calls, 2); + assert.equal(cachedMeshFloor(onDeck.lat, onDeck.lon + 0.0001), 129.0, 'the train stays on the deck'); + assert.equal(cachedMeshFloor(street.lat, street.lon - 0.0001), 121.0, 'the bus stays on the street'); + assert.equal(cachedGroundFloor(street.lat, street.lon), 121.0); + sampleMeshFloorCells(scene, [{ lat: deckLat, lon: cell.lon + 0.00001 }], opts); + assert.equal(calls, 2, 'a point next to one already probed is not probed again'); +}); + +test('sampler: a second reading at the same spot keeps the lower one', async () => { + reset(); + const cell = nextSite(); + await seedDem(cell, 120.0); + assert.equal(reportValidatedMeshFloorCell(cell.lat, cell.lon, 131.0), true); + assert.equal(reportValidatedMeshFloorCell(cell.lat + 0.00002, cell.lon, 122.0), true); + assert.equal(cachedMeshFloor(cell.lat, cell.lon), 122.0); + assert.equal(reportValidatedMeshFloorCell(cell.lat, cell.lon + 0.00002, 140.0), true); + assert.equal(cachedMeshFloor(cell.lat, cell.lon), 122.0, 'a higher one never raises it'); }); test('sampler: a latched cell is read a few times at most', async () => { @@ -229,11 +241,14 @@ test('sampler: a latched cell is read a few times at most', async () => { let calls = 0; const scene = fakeScene(() => { calls += 1; - return 125.0 - calls; + return 121.0; }); const opts = { viewerLat: cell.lat, viewerLon: cell.lon }; - for (let i = 0; i < 10; i++) - sampleMeshFloorCells(scene, [{ lat: cell.lat - 0.0004 + i * 0.0001, lon: cell.lon }], opts); - assert.equal(calls, 4); - assert.equal(cachedMeshFloor(cell.lat, cell.lon), 121.0); + for (let i = 0; i < 20; i++) + sampleMeshFloorCells( + scene, + [{ lat: cell.lat - 0.0004 + (i % 5) * 0.0002, lon: cell.lon - 0.00045 + Math.floor(i / 5) * 0.0003 }], + opts, + ); + assert.equal(calls, 8); }); diff --git a/src/services/groundFloor.js b/src/services/groundFloor.js index d8bf59c6d..06ed5e814 100644 --- a/src/services/groundFloor.js +++ b/src/services/groundFloor.js @@ -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} coarse cell key -> rendered-surface height (m, ellipsoidal). */ + /** @type {Map>} + * 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). */ diff --git a/src/services/meshFloorSampler.js b/src/services/meshFloorSampler.js index 0548f25fd..3f85a2b16 100644 --- a/src/services/meshFloorSampler.js +++ b/src/services/meshFloorSampler.js @@ -26,8 +26,9 @@ export function createMeshFloorSampler({ // airport apron) — so DEM-floored sprites/trails still buried themselves in // the mesh while 3D models (which groundSnap against the mesh) looked right. // This module samples the mesh height at the callers' points, a few probes - // per coarse cell, and reports them to groundFloor.js, which keeps the - // lowest that passes its gate; cachedGroundFloor() then answers mesh-first. + // per coarse cell, and reports them to groundFloor.js, which keeps each + // reading that passes its gate at its own point; cachedGroundFloor() then + // answers mesh-first, from the reading nearest the asked point. // // Rationing (the CCTV loading-jitter lesson): scene.sampleHeight forces // tile-load prioritization at the probe point, so sampling is (a) gated to @@ -53,10 +54,10 @@ export function createMeshFloorSampler({ * streamed LOD under a high camera is coarse everywhere, so every probe * would latch junk. */ const MAX_CAMERA_HEIGHT_M = 25_000; - /** @constant {number} Readings kept per cell: a cell is probed at most - * this many times, each at a different point, and the lowest reading - * that passes the DEM gate is the cell's floor. */ - const MAX_PROBES_PER_CELL = 4; + /** @constant {number} Readings per cell: a cell is probed at most this + * many times, each at a point apart from the others, and groundFloor keeps + * each reading at its point (a read answers from the nearest one). */ + const MAX_PROBES_PER_CELL = 8; /** @constant {number} How far apart (degrees, about 17 m) a new probe must * be from the cell's earlier ones to be worth taking. */ const PROBE_APART_DEG = 0.00015; @@ -149,8 +150,8 @@ export function createMeshFloorSampler({ const key = `${cell.lat},${cell.lon}`; if (attempted.has(key)) continue; // A latched cell is probed again only at a point well apart from the - // ones already probed, a few times at most: a later, lower reading - // replaces a rooftop the first probe may have hit. + // ones already probed, a few times at most, so the cell's readings + // follow where its vehicles actually are. const probes = _probedPoints.get(key); if ( cachedMeshFloor(cell.lat, cell.lon) != null && @@ -211,7 +212,7 @@ export function createMeshFloorSampler({ // feasible and deliberately deferred — see the P2 in // the project roadmap, which bundles it with the fly_route probe so // one post-launch pass settles rendered-mesh sampling everywhere. - reportValidatedMeshFloorCell(cell.lat, cell.lon, height); + reportValidatedMeshFloorCell(p.lat, p.lon, height); } }