Merge branch 'sameh/renderer-transit' into sameh/renderer-seam

# Conflicts:
#	src/render/cesium/draw.test.mjs
This commit is contained in:
Sameh Khamis committed 2026-11-09 01:00:43 -08:00
commit 9d4e438e63
27 files changed
+2016 -465

No files matched your search

+90
View File
@@ -747,3 +747,93 @@ test('the perspective helper is continuous through the sub-pixel band, not a 23
`exact alone at ${s.separation.toFixed(2)} px`,
);
});
test('the port-side perspective rotation matches the Cesium one through the whole band', async () => {
// The transit layer turns its icons with `perspectiveCourseRotation`, which
// projects through the query port and blends with the camera-port basis;
// given the same projection and basis it must answer as the scene helper.
const { perspectiveCourseRotation } = await import('./screenRotation.js');
let compared = 0;
for (let altitude = 500; altitude <= 600_000; altitude *= 1.05) {
const scene = obliqueScene(altitude, 45);
const project = (p) => {
const s = Cesium.SceneTransforms.worldToWindowCoordinates(
scene,
new Cesium.Cartesian3(p.x, p.y, p.z),
);
return s ? { x: s.x, y: s.y } : null;
};
const basis = { right: scene.camera.rightWC, up: scene.camera.upWC };
for (const [east, north] of [
[0.7, 1.2],
[-0.4, 0.9],
[0, 0.5],
[0, -2],
])
for (const course of [0, 37, 90, 181, 300]) {
const contact = Cesium.Cartesian3.fromDegrees(
(east * altitude) / 111_320,
(north * altitude) / 111_320,
0,
);
for (const previous of [null, 0.25]) {
const expected = perspectiveProjectedRotation(
scene,
contact,
course,
previous,
);
const actual = perspectiveCourseRotation(
project,
basis,
{ x: contact.x, y: contact.y, z: contact.z },
course,
previous,
);
if (expected === null || actual === null)
assert.equal(actual, expected);
else
assert.ok(
Math.abs(actual - expected) < 1e-9,
`${altitude.toFixed(0)} m, course ${course}: ${actual} vs ${expected}`,
);
compared += 1;
}
}
}
assert.ok(compared > 1000);
assert.equal(perspectiveCourseRotation(null, null, null, 0, 0.5), 0.5);
});
test('the port-side perspective rotation probes along the east-north frame, poles included', async () => {
const { perspectiveCourseRotation, PERSPECTIVE_PROBE_M: probeM } =
await import('./screenRotation.js');
const { eastNorthUp, frameVector } = await import('../geo/frames.js');
const { fromDegrees } = await import('../geo/ellipsoid.js');
for (const position of [
fromDegrees(-71.06, 42.36, 12),
fromDegrees(150, -60, 0),
{ x: 0, y: 0, z: 6_356_752.314245 },
])
for (const course of [0, 45, 200]) {
const seen = [];
perspectiveCourseRotation(
(p) => (seen.push({ x: p.x, y: p.y, z: p.z }), { x: 0, y: 0 }),
null,
position,
course,
);
const rad = (course * Math.PI) / 180;
const forward = frameVector(eastNorthUp(position), {
x: Math.sin(rad) * probeM,
y: Math.cos(rad) * probeM,
z: 0,
});
const [, ahead] = seen;
for (const axis of ['x', 'y', 'z'])
assert.ok(
Math.abs(ahead[axis] - (position[axis] + forward[axis])) < 1e-6,
`${axis} at course ${course}`,
);
}
});
+70 -1
View File
@@ -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
@@ -183,3 +183,72 @@ test('sampler: with `sets` the probe asks the query port, naming those sets', as
assert.ok(Math.abs(asked[0].point.lat - cell.lat) < 0.05);
assert.equal(cachedMeshFloor(cell.lat, cell.lon), 138.0);
});
test('sampler: the probe goes where the point is, not to the cell centre', async () => {
reset();
const cell = nextSite();
await seedDem(cell, 120.0);
const asked = [];
const scene = fakeScene((carto) => {
asked.push(Cesium.Math.toDegrees(carto.longitude));
return 122.0;
});
const point = { lat: cell.lat + 0.0003, lon: cell.lon + 0.0004 };
sampleMeshFloorCells(scene, [point], { viewerLat: cell.lat, viewerLon: cell.lon });
assert.equal(asked.length, 1);
assert.ok(Math.abs(asked[0] - point.lon) < 1e-9, `probed at ${asked[0]}, not ${point.lon}`);
});
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 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.latitude) - deckLat) < 1e-7 ? 129.0 : 121.0;
});
const opts = { viewerLat: cell.lat, viewerLon: cell.lon };
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 () => {
reset();
const cell = nextSite();
await seedDem(cell, 120.0);
let calls = 0;
const scene = fakeScene(() => {
calls += 1;
return 121.0;
});
const opts = { viewerLat: cell.lat, viewerLon: cell.lon };
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);
});
+115
View File
@@ -6,6 +6,7 @@
* render noise. See `iconOrientation.js` for why the course is projected
* onto the camera basis. No engine.
*/
import { WGS84 } from '../geo/ellipsoid.js';
import { eastNorthUp, frameVector } from '../geo/frames.js';
import { dot, toRadians, vec } from '../geo/vector.js';
@@ -79,3 +80,117 @@ export function stabilizeScreenRotation(
);
return Math.abs(delta) < Math.max(0, deadbandRad) ? previous : next;
}
/**
* Metres ahead `perspectiveCourseRotation` probes: short, so the projection
* is local to the contact (a two kilometre probe on a bus seen from six
* hundred metres would sample a part of the view the bus is nowhere near).
*/
export const PERSPECTIVE_PROBE_M = 30;
/**
* Probe separations (CSS px) across which the exact and camera-basis
* answers are blended: at or below the floor the camera-basis answer alone,
* at or above the ceiling the exact one.
*/
export const PERSPECTIVE_BLEND_MIN_PX = 0.5;
export const PERSPECTIVE_BLEND_MAX_PX = 3;
/**
* The icon rotation (as `courseScreenRotation`'s) that points a SURFACE
* contact along `courseDeg`, projected exactly, perspective division
* included: the contact and a point `PERSPECTIVE_PROBE_M` ahead of it are
* projected through `project` (a query port's `project`) and the angle
* between them read on screen. The camera-basis answer is orthographic,
* exact at the view's centre and wrong by twenty-odd degrees for a
* street-level contact away from it in an oblique view. Where either point
* cannot be projected it falls back to the camera-basis answer; below a few
* pixels of separation, where projection noise rules the exact one, the
* two are blended the short way round, so the rotation is continuous.
* @param {(p: {x: number, y: number, z: number}) => ({x: number, y: number}|null)} project
* @param {{right?: object, up?: object}|null} basis The camera's axes.
* @param {{x: number, y: number, z: number}|null} position ECEF.
* @param {number} courseDeg Degrees clockwise from north.
* @param {number|null} [previous] Kept when the rotation is unknown.
* @returns {number|null}
*/
export function perspectiveCourseRotation(
project,
basis,
position,
courseDeg,
previous = null,
) {
if (!project || !position || !Number.isFinite(courseDeg)) return previous;
if (!courseProbeInto(position, courseDeg, probeScratch)) {
// At a pole, through the frame `eastNorthUp` defines there.
const courseRad = toRadians(courseDeg);
const forward = frameVector(
eastNorthUp(position),
vec(
Math.sin(courseRad) * PERSPECTIVE_PROBE_M,
Math.cos(courseRad) * PERSPECTIVE_PROBE_M,
0,
),
);
probeScratch.x = position.x + forward.x;
probeScratch.y = position.y + forward.y;
probeScratch.z = position.z + forward.z;
}
const at = project(position);
const atX = at?.x;
const atY = at?.y;
const ahead = at ? project(probeScratch) : null;
if (!at || !ahead)
return courseScreenRotation(basis, position, courseDeg, previous);
const dx = ahead.x - atX;
const dy = ahead.y - atY;
const separation = Math.hypot(dx, dy);
// Window y grows downward; rotation 0 points the icon screen-up.
const exact = Math.atan2(-dx, -dy);
if (separation >= PERSPECTIVE_BLEND_MAX_PX) return exact;
const camera = courseScreenRotation(basis, position, courseDeg, previous);
if (!Number.isFinite(camera)) return exact;
if (separation <= PERSPECTIVE_BLEND_MIN_PX) return camera;
const weight =
(separation - PERSPECTIVE_BLEND_MIN_PX) /
(PERSPECTIVE_BLEND_MAX_PX - PERSPECTIVE_BLEND_MIN_PX);
const delta = Math.atan2(Math.sin(exact - camera), Math.cos(exact - camera));
return camera + delta * weight;
}
// The probe point, reused: the rotation pass visits every visible contact.
const probeScratch = { x: 0, y: 0, z: 0 };
/**
* The point `PERSPECTIVE_PROBE_M` ahead of `position` along `courseDeg` on
* its east-north plane (as `eastNorthUp` and `frameVector` give it), into
* `out`, allocating nothing. False at a pole, where `eastNorthUp` picks
* its own east.
*/
function courseProbeInto(position, courseDeg, out) {
const { x, y, z } = position;
// `eastNorthUp`'s own test for a pole.
if (Math.abs(x) <= 1e-14 && Math.abs(y) <= 1e-14) return false;
const eastLength = Math.hypot(x, y);
const o = WGS84.oneOverRadiiSquared;
let ux = x * o.x,
uy = y * o.y,
uz = z * o.z;
const upLength = Math.sqrt(ux * ux + uy * uy + uz * uz);
ux /= upLength;
uy /= upLength;
uz /= upLength;
const ex = -y / eastLength,
ey = x / eastLength;
// North is up × east (east has no z).
const nx = -uz * ey,
ny = uz * ex,
nz = ux * ey - uy * ex;
const courseRad = toRadians(courseDeg);
const e = Math.sin(courseRad) * PERSPECTIVE_PROBE_M;
const n = Math.cos(courseRad) * PERSPECTIVE_PROBE_M;
out.x = x + ex * e + nx * n;
out.y = y + ey * e + ny * n;
out.z = z + nz * n;
return true;
}